face: use NetworkInterfaceInfo directly in EthernetFace.

Finally getting rid of EthernetFactory::findAllInterfaces()
and EthernetFace::getInterfaceAddress().

Change-Id: I94fe2016cc98778af3115569b1d21d5c48425d9c
diff --git a/daemon/face/ethernet-face.cpp b/daemon/face/ethernet-face.cpp
index 2b6e2bc..2ea6ca9 100644
--- a/daemon/face/ethernet-face.cpp
+++ b/daemon/face/ethernet-face.cpp
@@ -5,9 +5,11 @@
  */
 
 #include "ethernet-face.hpp"
+#include "core/network-interface.hpp"
 
 #include <pcap/pcap.h>
 
+#include <cstring>        // for std::strncpy()
 #include <arpa/inet.h>    // for htons() and ntohs()
 #include <net/ethernet.h> // for struct ether_header
 #include <net/if.h>       // for struct ifreq
@@ -15,30 +17,21 @@
 #include <sys/ioctl.h>    // for ioctl()
 #include <unistd.h>       // for dup()
 
-#ifndef SIOCGIFHWADDR
-#include <net/if_dl.h>    // for struct sockaddr_dl
-// must be included *after* <net/if.h>
-#include <ifaddrs.h>      // for getifaddrs()
-#endif
-
 namespace nfd {
 
 NFD_LOG_INIT("EthernetFace")
 
-static const uint8_t MAX_PADDING[ethernet::MIN_DATA_LEN] = {
-  0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
-  0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0
-};
-
-
 EthernetFace::EthernetFace(const shared_ptr<boost::asio::posix::stream_descriptor>& socket,
-                           const ethernet::Endpoint& interface,
+                           const shared_ptr<NetworkInterfaceInfo>& interface,
                            const ethernet::Address& address)
-  : Face(FaceUri("ether://" + interface + "/" + address.toString(':')))
+  : Face(FaceUri("ether://" + interface->name + "/" + address.toString(':')))
   , m_socket(socket)
-  , m_interface(interface)
+  , m_interfaceName(interface->name)
+  , m_srcAddress(interface->etherAddress)
   , m_destAddress(address)
 {
+  NFD_LOG_INFO("Creating ethernet face on " << m_interfaceName << ": "
+               << m_srcAddress << " <--> " << m_destAddress);
   pcapInit();
 
   int fd = pcap_get_selectable_fd(m_pcap);
@@ -50,11 +43,8 @@
   // same fd and one of them will fail
   m_socket->assign(::dup(fd));
 
-  m_sourceAddress = getInterfaceAddress();
-  NFD_LOG_DEBUG("[id:" << getId() << ",endpoint:" << m_interface
-                << "] Local MAC address is: " << m_sourceAddress);
   m_interfaceMtu = getInterfaceMtu();
-  NFD_LOG_DEBUG("[id:" << getId() << ",endpoint:" << m_interface
+  NFD_LOG_DEBUG("[id:" << getId() << ",endpoint:" << m_interfaceName
                 << "] Interface MTU is: " << m_interfaceMtu);
 
   char filter[100];
@@ -62,7 +52,7 @@
              "(ether proto 0x%x) && (ether dst %s) && (not ether src %s)",
              ETHERTYPE_NDN,
              m_destAddress.toString(':').c_str(),
-             m_sourceAddress.toString(':').c_str());
+             m_srcAddress.toString(':').c_str());
   setPacketFilter(filter);
 
   m_socket->async_read_some(boost::asio::null_buffers(),
@@ -105,12 +95,9 @@
 void
 EthernetFace::pcapInit()
 {
-  NFD_LOG_TRACE("[id:" << getId() << ",endpoint:" << m_interface
-                << "] Initializing pcap");
-
   char errbuf[PCAP_ERRBUF_SIZE];
   errbuf[0] = '\0';
-  m_pcap = pcap_create(m_interface.c_str(), errbuf);
+  m_pcap = pcap_create(m_interfaceName.c_str(), errbuf);
   if (!m_pcap)
     throw Error("pcap_create(): " + std::string(errbuf));
 
@@ -147,7 +134,7 @@
 {
   if (!m_pcap)
     {
-      NFD_LOG_WARN("[id:" << getId() << ",endpoint:" << m_interface
+      NFD_LOG_WARN("[id:" << getId() << ",endpoint:" << m_interfaceName
                    << "] Trying to send on closed face");
       onFail("Face closed");
       return;
@@ -156,7 +143,8 @@
   /// \todo Fragmentation
   if (block.size() > m_interfaceMtu)
     {
-      NFD_LOG_ERROR("Fragmentation not implemented: dropping packet larger than MTU");
+      NFD_LOG_ERROR("[id:" << getId() << ",endpoint:" << m_interfaceName
+                    << "] Fragmentation not implemented: dropping packet larger than MTU");
       return;
     }
 
@@ -166,13 +154,14 @@
 
   if (block.size() < ethernet::MIN_DATA_LEN)
     {
-      buffer.appendByteArray(MAX_PADDING, ethernet::MIN_DATA_LEN - block.size());
+      static const uint8_t padding[ethernet::MIN_DATA_LEN] = {0};
+      buffer.appendByteArray(padding, ethernet::MIN_DATA_LEN - block.size());
     }
 
   // construct and prepend the ethernet header
   static uint16_t ethertype = htons(ETHERTYPE_NDN);
   buffer.prependByteArray(reinterpret_cast<const uint8_t*>(&ethertype), ethernet::TYPE_LEN);
-  buffer.prependByteArray(m_sourceAddress.data(), m_sourceAddress.size());
+  buffer.prependByteArray(m_srcAddress.data(), m_srcAddress.size());
   buffer.prependByteArray(m_destAddress.data(), m_destAddress.size());
 
   // send the packet
@@ -186,7 +175,7 @@
       throw Error("Failed to send packet");
     }
 
-  NFD_LOG_TRACE("[id:" << getId() << ",endpoint:" << m_interface
+  NFD_LOG_TRACE("[id:" << getId() << ",endpoint:" << m_interfaceName
                 << "] Successfully sent: " << buffer.size() << " bytes");
 }
 
@@ -205,7 +194,7 @@
     }
   else if (ret == 0)
     {
-      NFD_LOG_WARN("[id:" << getId() << ",endpoint:" << m_interface
+      NFD_LOG_WARN("[id:" << getId() << ",endpoint:" << m_interfaceName
                    << "] pcap_next_ex() timed out");
     }
   else
@@ -220,7 +209,7 @@
 
       packet += ethernet::HDR_LEN;
       length -= ethernet::HDR_LEN;
-      NFD_LOG_TRACE("[id:" << getId() << ",endpoint:" << m_interface
+      NFD_LOG_TRACE("[id:" << getId() << ",endpoint:" << m_interfaceName
                     << "] Received: " << length << " bytes");
 
       /// \todo Eliminate reliance on exceptions in this path
@@ -230,14 +219,14 @@
         ndn::Block element(packet, length);
         if (!decodeAndDispatchInput(element))
           {
-            NFD_LOG_WARN("[id:" << getId() << ",endpoint:" << m_interface
-                         << "] Received unrecognized block of type ["
-                         << element.type() << "]");
+            NFD_LOG_WARN("[id:" << getId() << ",endpoint:" << m_interfaceName
+                         << "] Received unrecognized block of type " << element.type());
             // ignore unknown packet
           }
-      } catch (const tlv::Error&) {
-        NFD_LOG_WARN("[id:" << getId() << ",endpoint:" << m_interface
-                     << "] Received input is invalid or too large to process");
+      }
+      catch (const tlv::Error&) {
+        NFD_LOG_WARN("[id:" << getId() << ",endpoint:" << m_interfaceName
+                     << "] Received block is invalid or too large to process");
       }
     }
 
@@ -264,66 +253,30 @@
   if (error == boost::asio::error::eof)
     {
       msg = "Face closed";
-      NFD_LOG_INFO("[id:" << getId() << ",endpoint:" << m_interface << "] " << msg);
+      NFD_LOG_INFO("[id:" << getId() << ",endpoint:" << m_interfaceName << "] " << msg);
     }
   else
     {
-      msg = "Send or receive operation failed, closing face: "
-          + error.category().message(error.value());
-      NFD_LOG_WARN("[id:" << getId() << ",endpoint:" << m_interface << "] " << msg);
+      msg = "Receive operation failed, closing face: " + error.category().message(error.value());
+      NFD_LOG_WARN("[id:" << getId() << ",endpoint:" << m_interfaceName << "] " << msg);
     }
 
   close();
   onFail(msg);
 }
 
-ethernet::Address
-EthernetFace::getInterfaceAddress() const
-{
-#ifdef SIOCGIFHWADDR
-  ifreq ifr = {};
-  ::strncpy(ifr.ifr_name, m_interface.c_str(), sizeof(ifr.ifr_name));
-
-  if (::ioctl(m_socket->native_handle(), SIOCGIFHWADDR, &ifr) < 0)
-    throw Error("ioctl(SIOCGIFHWADDR) failed");
-
-  uint8_t* hwaddr = reinterpret_cast<uint8_t*>(ifr.ifr_hwaddr.sa_data);
-  return ethernet::Address(hwaddr);
-#else
-  ifaddrs* addrlist;
-  if (::getifaddrs(&addrlist) < 0)
-    throw Error("getifaddrs() failed");
-
-  ethernet::Address address;
-  for (ifaddrs* ifa = addrlist; ifa != 0; ifa = ifa->ifa_next)
-    {
-      if (std::string(ifa->ifa_name) == m_interface
-          && ifa->ifa_addr != 0
-          && ifa->ifa_addr->sa_family == AF_LINK)
-        {
-          sockaddr_dl* sa = reinterpret_cast<sockaddr_dl*>(ifa->ifa_addr);
-          address = ethernet::Address(reinterpret_cast<uint8_t*>(LLADDR(sa)));
-          break;
-        }
-    }
-
-  ::freeifaddrs(addrlist);
-  return address;
-#endif
-}
-
 size_t
 EthernetFace::getInterfaceMtu() const
 {
   size_t mtu = ethernet::MAX_DATA_LEN;
 
 #ifdef SIOCGIFMTU
-  ifreq ifr = {};
-  ::strncpy(ifr.ifr_name, m_interface.c_str(), sizeof(ifr.ifr_name));
+  ifreq ifr = {0};
+  std::strncpy(ifr.ifr_name, m_interfaceName.c_str(), sizeof(ifr.ifr_name) - 1);
 
   if (::ioctl(m_socket->native_handle(), SIOCGIFMTU, &ifr) < 0)
     {
-      NFD_LOG_WARN("[id:" << getId() << ",endpoint:" << m_interface
+      NFD_LOG_WARN("[id:" << getId() << ",endpoint:" << m_interfaceName
                    << "] Failed to get interface MTU, assuming " << mtu);
     }
   else
diff --git a/daemon/face/ethernet-face.hpp b/daemon/face/ethernet-face.hpp
index a6394ce..a42f889 100644
--- a/daemon/face/ethernet-face.hpp
+++ b/daemon/face/ethernet-face.hpp
@@ -20,6 +20,8 @@
 
 namespace nfd {
 
+class NetworkInterfaceInfo;
+
 /**
  * \brief Implementation of Face abstraction that uses raw
  *        Ethernet frames as underlying transport mechanism
@@ -36,7 +38,7 @@
   };
 
   EthernetFace(const shared_ptr<boost::asio::posix::stream_descriptor>& socket,
-               const ethernet::Endpoint& interface,
+               const shared_ptr<NetworkInterfaceInfo>& interface,
                const ethernet::Address& address);
 
   virtual
@@ -76,16 +78,13 @@
   void
   processErrorCode(const boost::system::error_code& error);
 
-  ethernet::Address
-  getInterfaceAddress() const;
-
   size_t
   getInterfaceMtu() const;
 
 private:
   shared_ptr<boost::asio::posix::stream_descriptor> m_socket;
-  ethernet::Endpoint m_interface;
-  ethernet::Address m_sourceAddress;
+  std::string m_interfaceName;
+  ethernet::Address m_srcAddress;
   ethernet::Address m_destAddress;
   size_t m_interfaceMtu;
   pcap_t* m_pcap;
diff --git a/daemon/face/ethernet-factory.cpp b/daemon/face/ethernet-factory.cpp
index 78adf6f..2801c4f 100644
--- a/daemon/face/ethernet-factory.cpp
+++ b/daemon/face/ethernet-factory.cpp
@@ -6,6 +6,7 @@
 
 #include "ethernet-factory.hpp"
 #include "core/global-io.hpp"
+#include "core/network-interface.hpp"
 
 #include <boost/algorithm/string/predicate.hpp>
 #include <pcap/pcap.h>
@@ -15,17 +16,14 @@
 NFD_LOG_INIT("EthernetFactory")
 
 shared_ptr<EthernetFace>
-EthernetFactory::createMulticastFace(const ethernet::Endpoint& interface,
-                                     const ethernet::Address& address)
+EthernetFactory::createMulticastFace(const shared_ptr<NetworkInterfaceInfo> &interface,
+                                     const ethernet::Address &address)
 {
-  std::vector<ethernet::Endpoint> ifs = findAllInterfaces();
-  if (std::find(ifs.begin(), ifs.end(), interface) == ifs.end())
-    throw Error(interface + " does not exist");
-
   if (!address.isMulticast())
     throw Error(address.toString() + " is not a multicast address");
 
-  shared_ptr<EthernetFace> face = findMulticastFace(interface, address);
+  const std::string& name = interface->name;
+  shared_ptr<EthernetFace> face = findMulticastFace(name, address);
   if (face)
     return face;
 
@@ -36,66 +34,25 @@
                                    boost::cref(interface),
                                    boost::cref(address));
   face->onFail += bind(&EthernetFactory::afterFaceFailed,
-                       this, interface, address);
-  m_multicastFaces[std::make_pair(interface, address)] = face;
+                       this, name, address);
+  m_multicastFaces[std::make_pair(name, address)] = face;
 
   return face;
 }
 
-std::vector<ethernet::Endpoint>
-EthernetFactory::findAllInterfaces()
-{
-  std::vector<ethernet::Endpoint> interfaces;
-  char errbuf[PCAP_ERRBUF_SIZE];
-  errbuf[0] = '\0';
-
-  pcap_if_t* alldevs;
-  if (pcap_findalldevs(&alldevs, errbuf) < 0)
-    {
-      NFD_LOG_WARN("pcap_findalldevs() failed: " << errbuf);
-      return interfaces;
-    }
-
-  for (pcap_if_t* device = alldevs; device != 0; device = device->next)
-    {
-      if ((device->flags & PCAP_IF_LOOPBACK) != 0)
-        // ignore loopback devices
-        continue;
-
-      ethernet::Endpoint interface(device->name);
-      if (interface == "any")
-        // ignore libpcap "any" pseudo-device
-        continue;
-      if (boost::starts_with(interface, "nflog") ||
-          boost::starts_with(interface, "nfqueue"))
-        // ignore Linux netfilter devices
-        continue;
-      if (boost::starts_with(interface, "fw"))
-        // ignore OSX firewire interface
-        continue;
-
-      // maybe add interface addresses too
-      // interface.addAddress ...
-      interfaces.push_back(interface);
-    }
-
-  pcap_freealldevs(alldevs);
-  return interfaces;
-}
-
 void
-EthernetFactory::afterFaceFailed(const ethernet::Endpoint& interface,
+EthernetFactory::afterFaceFailed(const std::string& interfaceName,
                                  const ethernet::Address& address)
 {
-  NFD_LOG_DEBUG("afterFaceFailed: " << interface << "/" << address);
-  m_multicastFaces.erase(std::make_pair(interface, address));
+  NFD_LOG_DEBUG("afterFaceFailed: " << interfaceName << "/" << address);
+  m_multicastFaces.erase(std::make_pair(interfaceName, address));
 }
 
 shared_ptr<EthernetFace>
-EthernetFactory::findMulticastFace(const ethernet::Endpoint& interface,
+EthernetFactory::findMulticastFace(const std::string& interfaceName,
                                    const ethernet::Address& address) const
 {
-  MulticastFacesMap::const_iterator i = m_multicastFaces.find(std::make_pair(interface, address));
+  MulticastFacesMap::const_iterator i = m_multicastFaces.find(std::make_pair(interfaceName, address));
   if (i != m_multicastFaces.end())
     return i->second;
   else
diff --git a/daemon/face/ethernet-factory.hpp b/daemon/face/ethernet-factory.hpp
index 2eeca7b..b8e4511 100644
--- a/daemon/face/ethernet-factory.hpp
+++ b/daemon/face/ethernet-factory.hpp
@@ -12,6 +12,8 @@
 
 namespace nfd {
 
+class NetworkInterfaceInfo;
+
 class EthernetFactory : public ProtocolFactory
 {
 public:
@@ -23,10 +25,16 @@
     Error(const std::string& what) : ProtocolFactory::Error(what) {}
   };
 
+  // from ProtocolFactory
+  virtual void
+  createFace(const FaceUri& uri,
+             const FaceCreatedCallback& onCreated,
+             const FaceConnectFailedCallback& onConnectFailed);
+
   /**
    * \brief Create an EthernetFace to communicate with the given multicast group
    *
-   * If this method is called twice with the same endpoint and group, only
+   * If this method is called twice with the same interface and group, only
    * one face will be created. Instead, the second call will just retrieve
    * the existing face.
    *
@@ -39,27 +47,12 @@
    * \throws EthernetFactory::Error or EthernetFace::Error
    */
   shared_ptr<EthernetFace>
-  createMulticastFace(const ethernet::Endpoint& interface,
+  createMulticastFace(const shared_ptr<NetworkInterfaceInfo>& interface,
                       const ethernet::Address& address);
 
-  /**
-   * \brief Get a list of devices that can be opened for a live capture
-   *
-   * This function is a wrapper for pcap_findalldevs()/pcap_freealldevs()
-   */
-  static std::vector<ethernet::Endpoint>
-  findAllInterfaces();
-
-  // from Factory
-
-  virtual void
-  createFace(const FaceUri& uri,
-             const FaceCreatedCallback& onCreated,
-             const FaceConnectFailedCallback& onConnectFailed);
-
 private:
   void
-  afterFaceFailed(const ethernet::Endpoint& endpoint,
+  afterFaceFailed(const std::string& interfaceName,
                   const ethernet::Address& address);
 
   /**
@@ -71,11 +64,11 @@
    * \throws never
    */
   shared_ptr<EthernetFace>
-  findMulticastFace(const ethernet::Endpoint& interface,
+  findMulticastFace(const std::string& interfaceName,
                     const ethernet::Address& address) const;
 
 private:
-  typedef std::map< std::pair<ethernet::Endpoint, ethernet::Address>,
+  typedef std::map< std::pair<std::string, ethernet::Address>,
                     shared_ptr<EthernetFace> > MulticastFacesMap;
   MulticastFacesMap m_multicastFaces;
 };
diff --git a/daemon/face/ethernet.hpp b/daemon/face/ethernet.hpp
index 573ea56..c72e742 100644
--- a/daemon/face/ethernet.hpp
+++ b/daemon/face/ethernet.hpp
@@ -16,8 +16,6 @@
 namespace nfd {
 namespace ethernet {
 
-typedef std::string Endpoint;
-
 const size_t ADDR_LEN     = 6;      ///< Octets in one Ethernet address
 const size_t TYPE_LEN     = 2;      ///< Octets in Ethertype field
 const size_t HDR_LEN      = 14;     ///< Total octets in Ethernet header (without 802.1Q tag)
diff --git a/daemon/mgmt/face-manager.cpp b/daemon/mgmt/face-manager.cpp
index 28f8a16..bbd495e 100644
--- a/daemon/mgmt/face-manager.cpp
+++ b/daemon/mgmt/face-manager.cpp
@@ -489,7 +489,7 @@
                   try
                     {
                       shared_ptr<EthernetFace> newFace =
-                        factory->createMulticastFace(nic->name, mcastGroup);
+                        factory->createMulticastFace(nic, mcastGroup);
                       NFD_LOG_INFO("Created multicast Face ID " << newFace->getId());
                     }
                   catch (const EthernetFactory::Error& factoryError)