core: add free function to enumerate network interfaces.

Change-Id: Icd89662afe9e1295cd18a615970c5b4299529b38
Refs: #1306
diff --git a/daemon/core/network-interface.cpp b/daemon/core/network-interface.cpp
new file mode 100644
index 0000000..65a690c
--- /dev/null
+++ b/daemon/core/network-interface.cpp
@@ -0,0 +1,109 @@
+/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
+/**
+ * Copyright (C) 2014 Named Data Networking Project
+ * See COPYING for copyright and distribution information.
+ */
+
+#include "network-interface.hpp"
+
+#include <boost/foreach.hpp>
+
+#include <arpa/inet.h>   // for inet_ntop()
+#include <netinet/in.h>  // for struct sockaddr_in{,6}
+#include <ifaddrs.h>     // for getifaddrs()
+
+#if defined(__linux__)
+#include <net/if_arp.h>        // for ARPHRD_* constants
+#include <netpacket/packet.h>  // for struct sockaddr_ll
+#elif defined(__APPLE__)
+#include <net/if_dl.h>         // for struct sockaddr_dl
+#else
+#error Platform not supported
+#endif
+
+namespace nfd {
+
+NFD_LOG_INIT("NetworkInterfaceInfo")
+
+std::list< shared_ptr<NetworkInterfaceInfo> >
+listNetworkInterfaces()
+{
+  typedef std::map< std::string, shared_ptr<NetworkInterfaceInfo> > InterfacesMap;
+  InterfacesMap ifmap;
+
+  ifaddrs* ifa_list;
+  if (::getifaddrs(&ifa_list) < 0)
+    throw std::runtime_error("getifaddrs() failed");
+
+  for (ifaddrs* ifa = ifa_list; ifa != 0; ifa = ifa->ifa_next)
+    {
+      shared_ptr<NetworkInterfaceInfo> netif;
+      std::string ifname(ifa->ifa_name);
+      InterfacesMap::iterator i = ifmap.find(ifname);
+      if (i == ifmap.end())
+        {
+          netif = make_shared<NetworkInterfaceInfo>();
+          netif->name = ifname;
+          netif->flags = ifa->ifa_flags;
+          ifmap[ifname] = netif;
+        }
+      else
+        {
+          netif = i->second;
+        }
+
+      if (!ifa->ifa_addr)
+        continue;
+
+      switch (ifa->ifa_addr->sa_family)
+        {
+        case AF_INET: {
+            const sockaddr_in* sin = reinterpret_cast<sockaddr_in*>(ifa->ifa_addr);
+            char address[INET_ADDRSTRLEN];
+            if (::inet_ntop(AF_INET, &sin->sin_addr, address, sizeof(address)))
+              netif->ipv4Addresses.push_back(boost::asio::ip::address_v4::from_string(address));
+            else
+              NFD_LOG_WARN("inet_ntop() failed on " << ifname);
+          }
+          break;
+        case AF_INET6: {
+            const sockaddr_in6* sin6 = reinterpret_cast<sockaddr_in6*>(ifa->ifa_addr);
+            char address[INET6_ADDRSTRLEN];
+            if (::inet_ntop(AF_INET6, &sin6->sin6_addr, address, sizeof(address)))
+              netif->ipv6Addresses.push_back(boost::asio::ip::address_v6::from_string(address));
+            else
+              NFD_LOG_WARN("inet_ntop() failed on " << ifname);
+          }
+          break;
+#if defined(__linux__)
+        case AF_PACKET: {
+            const sockaddr_ll* sll = reinterpret_cast<sockaddr_ll*>(ifa->ifa_addr);
+            netif->index = sll->sll_ifindex;
+            if (sll->sll_hatype == ARPHRD_ETHER && sll->sll_halen == ethernet::ADDR_LEN)
+              netif->etherAddress = ethernet::Address(sll->sll_addr);
+            else if (sll->sll_hatype != ARPHRD_LOOPBACK)
+              NFD_LOG_WARN("Unrecognized hardware address on " << ifname);
+          }
+          break;
+#elif defined(__APPLE__)
+        case AF_LINK: {
+            const sockaddr_dl* sdl = reinterpret_cast<sockaddr_dl*>(ifa->ifa_addr);
+            netif->index = sdl->sdl_index;
+            netif->etherAddress = ethernet::Address(reinterpret_cast<uint8_t*>(LLADDR(sdl)));
+          }
+          break;
+#endif
+        }
+    }
+
+  ::freeifaddrs(ifa_list);
+
+  std::list< shared_ptr<NetworkInterfaceInfo> > list;
+  BOOST_FOREACH(InterfacesMap::value_type elem, ifmap) {
+    list.push_back(elem.second);
+  }
+
+  return list;
+}
+
+} // namespace nfd
diff --git a/daemon/core/network-interface.hpp b/daemon/core/network-interface.hpp
new file mode 100644
index 0000000..d02171a
--- /dev/null
+++ b/daemon/core/network-interface.hpp
@@ -0,0 +1,60 @@
+/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
+/**
+ * Copyright (C) 2014 Named Data Networking Project
+ * See COPYING for copyright and distribution information.
+ */
+
+#ifndef NFD_CORE_NETWORK_INTERFACE_HPP
+#define NFD_CORE_NETWORK_INTERFACE_HPP
+
+#include "common.hpp"
+#include "face/ethernet.hpp"
+
+#include <net/if.h>
+
+namespace nfd {
+
+class NetworkInterfaceInfo
+{
+public:
+  int index;
+  std::string name;
+  ethernet::Address etherAddress;
+  std::vector<boost::asio::ip::address_v4> ipv4Addresses;
+  std::vector<boost::asio::ip::address_v6> ipv6Addresses;
+  unsigned int flags;
+
+  bool
+  isLoopback() const;
+
+  bool
+  isMulticastCapable() const;
+
+  bool
+  isUp() const;
+};
+
+inline bool
+NetworkInterfaceInfo::isLoopback() const
+{
+  return (flags & IFF_LOOPBACK) != 0;
+}
+
+inline bool
+NetworkInterfaceInfo::isMulticastCapable() const
+{
+  return (flags & IFF_MULTICAST) != 0;
+}
+
+inline bool
+NetworkInterfaceInfo::isUp() const
+{
+  return (flags & IFF_UP) != 0;
+}
+
+std::list< shared_ptr<NetworkInterfaceInfo> >
+listNetworkInterfaces();
+
+} // namespace nfd
+
+#endif // NFD_CORE_NETWORK_INTERFACE_HPP
diff --git a/daemon/face/ethernet.hpp b/daemon/face/ethernet.hpp
index 66ee715..f12b3fb 100644
--- a/daemon/face/ethernet.hpp
+++ b/daemon/face/ethernet.hpp
@@ -52,6 +52,10 @@
   bool
   isMulticast() const;
 
+  /// True if this is a null address (00-00-00-00-00-00)
+  bool
+  isNull() const;
+
   std::string
   toString(char sep = '-') const;
 };
@@ -114,6 +118,13 @@
   return (elems[0] & 1) != 0;
 }
 
+inline bool
+Address::isNull() const
+{
+  return elems[0] == 0x0 && elems[1] == 0x0 && elems[2] == 0x0 &&
+         elems[3] == 0x0 && elems[4] == 0x0 && elems[5] == 0x0;
+}
+
 inline std::string
 Address::toString(char sep) const
 {
diff --git a/tests/core/network-interface.cpp b/tests/core/network-interface.cpp
new file mode 100644
index 0000000..bdfd21f
--- /dev/null
+++ b/tests/core/network-interface.cpp
@@ -0,0 +1,39 @@
+/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
+/**
+ * Copyright (C) 2014 Named Data Networking Project
+ * See COPYING for copyright and distribution information.
+ */
+
+#include "core/network-interface.hpp"
+#include "tests/test-common.hpp"
+
+#include <boost/foreach.hpp>
+
+namespace nfd {
+namespace tests {
+
+BOOST_FIXTURE_TEST_SUITE(CoreNetworkInterface, BaseFixture)
+
+BOOST_AUTO_TEST_CASE(ListNetworkInterfaces)
+{
+  std::list< shared_ptr<NetworkInterfaceInfo> > netifs;
+  BOOST_CHECK_NO_THROW(netifs = listNetworkInterfaces());
+
+  BOOST_FOREACH(shared_ptr<NetworkInterfaceInfo> netif, netifs)
+  {
+    BOOST_TEST_MESSAGE(netif->index << ": " << netif->name);
+    BOOST_TEST_MESSAGE("\tether " << netif->etherAddress);
+    BOOST_FOREACH(boost::asio::ip::address_v4 address, netif->ipv4Addresses)
+      BOOST_TEST_MESSAGE("\tinet  " << address);
+    BOOST_FOREACH(boost::asio::ip::address_v6 address, netif->ipv6Addresses)
+      BOOST_TEST_MESSAGE("\tinet6 " << address);
+    BOOST_TEST_MESSAGE("\tloopback  : " << netif->isLoopback());
+    BOOST_TEST_MESSAGE("\tmulticast : " << netif->isMulticastCapable());
+    BOOST_TEST_MESSAGE("\tup        : " << netif->isUp());
+  }
+}
+
+BOOST_AUTO_TEST_SUITE_END()
+
+} // namespace tests
+} // namespace nfd