face: Ethernet{Face,ChannelFactory} implementation.

refs: #1191

Change-Id: I08b7ebfdc5d70fb494b1f23bd7b1a77cf18653a1
diff --git a/daemon/face/ethernet-channel-factory.cpp b/daemon/face/ethernet-channel-factory.cpp
new file mode 100644
index 0000000..4c47f85
--- /dev/null
+++ b/daemon/face/ethernet-channel-factory.cpp
@@ -0,0 +1,102 @@
+/* -*- 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 "ethernet-channel-factory.hpp"
+#include "core/global-io.hpp"
+
+#include <boost/algorithm/string/predicate.hpp>
+#include <pcap/pcap.h>
+
+namespace nfd {
+
+NFD_LOG_INIT("EthernetChannelFactory")
+
+shared_ptr<EthernetFace>
+EthernetChannelFactory::createMulticast(const ethernet::Endpoint& 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 = findMulticast(interface, address);
+  if (face)
+    return face;
+
+  shared_ptr<boost::asio::posix::stream_descriptor> socket =
+    make_shared<boost::asio::posix::stream_descriptor>(boost::ref(getGlobalIoService()));
+
+  face = make_shared<EthernetFace>(boost::cref(socket),
+                                   boost::cref(interface),
+                                   boost::cref(address));
+  face->onFail += bind(&EthernetChannelFactory::afterFaceFailed,
+                       this, interface, address);
+  m_multicastFaces[std::make_pair(interface, address)] = face;
+
+  return face;
+}
+
+std::vector<ethernet::Endpoint>
+EthernetChannelFactory::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;
+
+      // maybe add interface addresses too
+      // interface.addAddress ...
+      interfaces.push_back(interface);
+    }
+
+  pcap_freealldevs(alldevs);
+  return interfaces;
+}
+
+void
+EthernetChannelFactory::afterFaceFailed(const ethernet::Endpoint& interface,
+                                        const ethernet::Address& address)
+{
+  NFD_LOG_DEBUG("afterFaceFailed: " << interface << "/" << address);
+  m_multicastFaces.erase(std::make_pair(interface, address));
+}
+
+shared_ptr<EthernetFace>
+EthernetChannelFactory::findMulticast(const ethernet::Endpoint& interface,
+                                      const ethernet::Address& address) const
+{
+  MulticastFacesMap::const_iterator i = m_multicastFaces.find(std::make_pair(interface, address));
+  if (i != m_multicastFaces.end())
+    return i->second;
+  else
+    return shared_ptr<EthernetFace>();
+}
+
+} // namespace nfd
diff --git a/daemon/face/ethernet-channel-factory.hpp b/daemon/face/ethernet-channel-factory.hpp
new file mode 100644
index 0000000..4f655bd
--- /dev/null
+++ b/daemon/face/ethernet-channel-factory.hpp
@@ -0,0 +1,77 @@
+/* -*- 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_FACE_ETHERNET_CHANNEL_FACTORY_HPP
+#define NFD_FACE_ETHERNET_CHANNEL_FACTORY_HPP
+
+#include "ethernet-face.hpp"
+
+namespace nfd {
+
+class EthernetChannelFactory
+{
+public:
+  /**
+   * \brief Exception of EthernetChannelFactory
+   */
+  struct Error : public std::runtime_error
+  {
+    Error(const std::string& what) : std::runtime_error(what) {}
+  };
+
+  /**
+   * \brief Create an EthernetFace to communicate with the given multicast group
+   *
+   * If this method is called twice with the same endpoint and group, only
+   * one face will be created. Instead, the second call will just retrieve
+   * the existing face.
+   *
+   * \param interface Local network interface
+   * \param address   Ethernet broadcast/multicast destination address
+   *
+   * \returns always a valid shared pointer to an EthernetFace object,
+   *          an exception will be thrown if the creation fails
+   *
+   * \throws EthernetChannelFactory::Error or EthernetFace::Error
+   */
+  shared_ptr<EthernetFace>
+  createMulticast(const ethernet::Endpoint& 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();
+
+private:
+  void
+  afterFaceFailed(const ethernet::Endpoint& endpoint,
+                  const ethernet::Address& address);
+
+  /**
+   * \brief Look up EthernetFace using specified interface and address
+   *
+   * \returns shared pointer to the existing EthernetFace object or
+   *          empty shared pointer when such face does not exist
+   *
+   * \throws never
+   */
+  shared_ptr<EthernetFace>
+  findMulticast(const ethernet::Endpoint& interface,
+                const ethernet::Address& address) const;
+
+private:
+  typedef std::map< std::pair<ethernet::Endpoint, ethernet::Address>,
+                    shared_ptr<EthernetFace> > MulticastFacesMap;
+  MulticastFacesMap m_multicastFaces;
+};
+
+} // namespace nfd
+
+#endif // NFD_FACE_ETHERNET_CHANNEL_FACTORY_HPP
diff --git a/daemon/face/ethernet-face.cpp b/daemon/face/ethernet-face.cpp
new file mode 100644
index 0000000..a6e089f
--- /dev/null
+++ b/daemon/face/ethernet-face.cpp
@@ -0,0 +1,332 @@
+/* -*- 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 "ethernet-face.hpp"
+
+#include <pcap/pcap.h>
+
+#include <arpa/inet.h>    // for htons() and ntohs()
+#include <net/ethernet.h> // for struct ether_header
+#include <net/if.h>       // for struct ifreq
+#include <stdio.h>        // for snprintf()
+#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 ethernet::Address& address)
+  : m_socket(socket)
+  , m_interface(interface)
+  , m_destAddress(address)
+{
+  setLocal(false);
+  pcapInit();
+
+  int fd = pcap_get_selectable_fd(m_pcap);
+  if (fd < 0)
+    throw Error("pcap_get_selectable_fd() failed");
+
+  // need to duplicate the fd, otherwise both pcap_close()
+  // and stream_descriptor::close() will try to close the
+  // 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
+                << "] Interface MTU is: " << m_interfaceMtu);
+
+  char filter[100];
+  ::snprintf(filter, sizeof(filter),
+             "(ether proto 0x%x) && (ether dst %s) && (not ether src %s)",
+             ETHERTYPE_NDN,
+             m_destAddress.toString(':').c_str(),
+             m_sourceAddress.toString(':').c_str());
+  setPacketFilter(filter);
+
+  m_socket->async_read_some(boost::asio::null_buffers(),
+                            bind(&EthernetFace::handleRead, this,
+                                 boost::asio::placeholders::error,
+                                 boost::asio::placeholders::bytes_transferred));
+}
+
+EthernetFace::~EthernetFace()
+{
+  close();
+}
+
+void
+EthernetFace::sendInterest(const Interest& interest)
+{
+  sendPacket(interest.wireEncode());
+}
+
+void
+EthernetFace::sendData(const Data& data)
+{
+  sendPacket(data.wireEncode());
+}
+
+void
+EthernetFace::close()
+{
+  if (m_pcap)
+    {
+      boost::system::error_code error;
+      m_socket->close(error); // ignore errors
+      pcap_close(m_pcap);
+      m_pcap = 0;
+    }
+}
+
+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);
+  if (!m_pcap)
+    throw Error("pcap_create(): " + std::string(errbuf));
+
+  if (pcap_activate(m_pcap) < 0)
+    throw Error("pcap_activate() failed");
+
+  errbuf[0] = '\0';
+  if (pcap_setnonblock(m_pcap, 1, errbuf) < 0)
+    throw Error("pcap_setnonblock(): " + std::string(errbuf));
+
+  if (pcap_setdirection(m_pcap, PCAP_D_IN) < 0)
+    throw Error("pcap_setdirection(): " + std::string(pcap_geterr(m_pcap)));
+
+  if (pcap_set_datalink(m_pcap, DLT_EN10MB) < 0)
+    throw Error("pcap_set_datalink(): " + std::string(pcap_geterr(m_pcap)));
+}
+
+void
+EthernetFace::setPacketFilter(const char* filterString)
+{
+  bpf_program filter;
+  if (pcap_compile(m_pcap, &filter, filterString, 1, PCAP_NETMASK_UNKNOWN) < 0)
+    throw Error("pcap_compile(): " + std::string(pcap_geterr(m_pcap)));
+
+  int ret = pcap_setfilter(m_pcap, &filter);
+  pcap_freecode(&filter);
+  if (ret < 0)
+    throw Error("pcap_setfilter(): " + std::string(pcap_geterr(m_pcap)));
+}
+
+void
+EthernetFace::sendPacket(const ndn::Block& block)
+{
+  if (!m_pcap)
+    {
+      NFD_LOG_WARN("[id:" << getId() << ",endpoint:" << m_interface
+                   << "] Trying to send on closed face");
+      onFail("Face closed");
+      return;
+    }
+
+  /// @todo Fragmentation
+  if (block.size() > m_interfaceMtu)
+    throw Error("Fragmentation not implemented");
+
+  /// \todo Right now there is no reserve when packet is received, but
+  ///       we should reserve some space at the beginning and at the end
+  ndn::EncodingBuffer buffer(block);
+
+  if (block.size() < ethernet::MIN_DATA_LEN)
+    {
+      buffer.appendByteArray(MAX_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_destAddress.data(), m_destAddress.size());
+
+  // send the packet
+  int sent = pcap_inject(m_pcap, buffer.buf(), buffer.size());
+  if (sent < 0)
+    {
+      throw Error("pcap_inject(): " + std::string(pcap_geterr(m_pcap)));
+    }
+  else if (static_cast<size_t>(sent) < buffer.size())
+    {
+      throw Error("Failed to send packet");
+    }
+
+  NFD_LOG_TRACE("[id:" << getId() << ",endpoint:" << m_interface
+                << "] Successfully sent: " << buffer.size() << " bytes");
+}
+
+void
+EthernetFace::handleRead(const boost::system::error_code& error, size_t)
+{
+  if (error)
+    return processErrorCode(error);
+
+  pcap_pkthdr* pktHeader;
+  const uint8_t* packet;
+  int ret = pcap_next_ex(m_pcap, &pktHeader, &packet);
+  if (ret < 0)
+    {
+      throw Error("pcap_next_ex(): " + std::string(pcap_geterr(m_pcap)));
+    }
+  else if (ret == 0)
+    {
+      NFD_LOG_WARN("[id:" << getId() << ",endpoint:" << m_interface
+                   << "] pcap_next_ex() timed out");
+    }
+  else
+    {
+      size_t length = pktHeader->caplen;
+      if (length < ethernet::HDR_LEN + ethernet::MIN_DATA_LEN)
+        throw Error("Received packet is too short");
+
+      const ether_header* eh = reinterpret_cast<const ether_header*>(packet);
+      if (ntohs(eh->ether_type) != ETHERTYPE_NDN)
+        throw Error("Unrecognized ethertype");
+
+      packet += ethernet::HDR_LEN;
+      length -= ethernet::HDR_LEN;
+      NFD_LOG_TRACE("[id:" << getId() << ",endpoint:" << m_interface
+                    << "] Received: " << length << " bytes");
+
+      /// \todo Eliminate reliance on exceptions in this path
+      try {
+        /// \todo Reserve space in front and at the back
+        ///       of the underlying buffer
+        ndn::Block element(packet, length);
+        if (!decodeAndDispatchInput(element))
+          {
+            NFD_LOG_WARN("[id:" << getId() << ",endpoint:" << m_interface
+                         << "] 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");
+      }
+    }
+
+  m_socket->async_read_some(boost::asio::null_buffers(),
+                            bind(&EthernetFace::handleRead, this,
+                                 boost::asio::placeholders::error,
+                                 boost::asio::placeholders::bytes_transferred));
+}
+
+void
+EthernetFace::processErrorCode(const boost::system::error_code& error)
+{
+  if (error == boost::system::errc::operation_canceled)
+    // when socket is closed by someone
+    return;
+
+  if (!m_pcap)
+    {
+      onFail("Face closed");
+      return;
+    }
+
+  std::string msg;
+  if (error == boost::asio::error::eof)
+    {
+      msg = "Face closed";
+      NFD_LOG_INFO("[id:" << getId() << ",endpoint:" << m_interface << "] " << msg);
+    }
+  else
+    {
+      msg = "Send or receive operation failed, closing face: "
+          + error.category().message(error.value());
+      NFD_LOG_WARN("[id:" << getId() << ",endpoint:" << m_interface << "] " << 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));
+
+  if (::ioctl(m_socket->native_handle(), SIOCGIFMTU, &ifr) < 0)
+    {
+      NFD_LOG_WARN("[id:" << getId() << ",endpoint:" << m_interface
+                   << "] Failed to get interface MTU, assuming " << mtu);
+    }
+  else
+    {
+      mtu = std::min(mtu, static_cast<size_t>(ifr.ifr_mtu));
+    }
+#endif
+
+  return mtu;
+}
+
+} // namespace nfd
diff --git a/daemon/face/ethernet-face.hpp b/daemon/face/ethernet-face.hpp
new file mode 100644
index 0000000..a6394ce
--- /dev/null
+++ b/daemon/face/ethernet-face.hpp
@@ -0,0 +1,96 @@
+/* -*- 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_FACE_ETHERNET_FACE_HPP
+#define NFD_FACE_ETHERNET_FACE_HPP
+
+#include "ethernet.hpp"
+#include "face.hpp"
+
+#ifndef HAVE_PCAP
+#error "Cannot include this file when pcap is not available"
+#endif
+
+// forward declarations
+struct pcap;
+typedef pcap pcap_t;
+
+namespace nfd {
+
+/**
+ * \brief Implementation of Face abstraction that uses raw
+ *        Ethernet frames as underlying transport mechanism
+ */
+class EthernetFace : public Face
+{
+public:
+  /**
+   * \brief EthernetFace-related error
+   */
+  struct Error : public Face::Error
+  {
+    Error(const std::string& what) : Face::Error(what) {}
+  };
+
+  EthernetFace(const shared_ptr<boost::asio::posix::stream_descriptor>& socket,
+               const ethernet::Endpoint& interface,
+               const ethernet::Address& address);
+
+  virtual
+  ~EthernetFace();
+
+  /// send an Interest
+  virtual void
+  sendInterest(const Interest& interest);
+
+  /// send a Data
+  virtual void
+  sendData(const Data& data);
+
+  /**
+   * \brief Close the face
+   *
+   * This terminates all communication on the face and cause
+   * onFail() method event to be invoked
+   */
+  virtual void
+  close();
+
+private:
+  void
+  pcapInit();
+
+  void
+  setPacketFilter(const char* filterString);
+
+  void
+  sendPacket(const ndn::Block& block);
+
+  void
+  handleRead(const boost::system::error_code& error,
+             size_t nBytesRead);
+
+  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;
+  ethernet::Address m_destAddress;
+  size_t m_interfaceMtu;
+  pcap_t* m_pcap;
+};
+
+} // namespace nfd
+
+#endif // NFD_FACE_ETHERNET_FACE_HPP
diff --git a/daemon/face/ethernet.hpp b/daemon/face/ethernet.hpp
new file mode 100644
index 0000000..66ee715
--- /dev/null
+++ b/daemon/face/ethernet.hpp
@@ -0,0 +1,136 @@
+/* -*- 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_FACE_ETHERNET_HPP
+#define NFD_FACE_ETHERNET_HPP
+
+#include "common.hpp"
+
+#include <boost/array.hpp>
+
+#define ETHERTYPE_NDN 0x8624
+
+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)
+const size_t TAG_LEN      = 4;      ///< Octets in 802.1Q tag (TPID + priority + VLAN)
+const size_t MIN_DATA_LEN = 46;     ///< Min octets in Ethernet payload (assuming no 802.1Q tag)
+const size_t MAX_DATA_LEN = 1500;   ///< Max octets in Ethernet payload
+const size_t CRC_LEN      = 4;      ///< Octets in Ethernet frame check sequence
+
+
+class Address : public boost::array<uint8_t, ADDR_LEN>
+{
+public:
+  /// Constructs a null Ethernet address (00-00-00-00-00-00)
+  Address();
+
+  /// Constructs a new Ethernet address with the given octets
+  Address(uint8_t a1, uint8_t a2, uint8_t a3,
+          uint8_t a4, uint8_t a5, uint8_t a6);
+
+  /// Constructs a new Ethernet address with the given octets
+  explicit
+  Address(const uint8_t octets[ADDR_LEN]);
+
+  /// Copy constructor
+  Address(const Address& address);
+
+  /// True if this is a broadcast address (FF-FF-FF-FF-FF-FF)
+  bool
+  isBroadcast() const;
+
+  /// True if this is a multicast address
+  bool
+  isMulticast() const;
+
+  std::string
+  toString(char sep = '-') const;
+};
+
+/// Returns the Ethernet broadcast address (FF-FF-FF-FF-FF-FF)
+inline Address
+getBroadcastAddress()
+{
+  static Address bcast(0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF);
+  return bcast;
+}
+
+/// Returns the default Ethernet multicast address for NDN
+inline Address
+getDefaultMulticastAddress()
+{
+  static Address mcast(0x01, 0x00, 0x5E, 0x00, 0x17, 0xAA);
+  return mcast;
+}
+
+inline
+Address::Address()
+{
+  assign(0);
+}
+
+inline
+Address::Address(uint8_t a1, uint8_t a2, uint8_t a3, uint8_t a4, uint8_t a5, uint8_t a6)
+{
+  elems[0] = a1;
+  elems[1] = a2;
+  elems[2] = a3;
+  elems[3] = a4;
+  elems[4] = a5;
+  elems[5] = a6;
+}
+
+inline
+Address::Address(const uint8_t octets[])
+{
+  std::copy(octets, octets + size(), begin());
+}
+
+inline
+Address::Address(const Address& address)
+{
+  std::copy(address.begin(), address.end(), begin());
+}
+
+inline bool
+Address::isBroadcast() const
+{
+  return elems[0] == 0xFF && elems[1] == 0xFF && elems[2] == 0xFF &&
+         elems[3] == 0xFF && elems[4] == 0xFF && elems[5] == 0xFF;
+}
+
+inline bool
+Address::isMulticast() const
+{
+  return (elems[0] & 1) != 0;
+}
+
+inline std::string
+Address::toString(char sep) const
+{
+  char s[18]; // 12 digits + 5 separators + null terminator
+  ::snprintf(s, sizeof(s), "%02x%c%02x%c%02x%c%02x%c%02x%c%02x",
+             elems[0], sep, elems[1], sep, elems[2], sep,
+             elems[3], sep, elems[4], sep, elems[5]);
+  return std::string(s);
+}
+
+static std::ostream&
+operator<<(std::ostream& o, const Address& a)
+{
+  return o << a.toString();
+}
+
+} // namespace ethernet
+} // namespace nfd
+
+#endif // NFD_FACE_ETHERNET_HPP