core: use ethernet::Address from ndn-cxx

refs #2142

Change-Id: I1258a0a1cadbedb31c506c69c84b7906a61fc761
diff --git a/common.hpp b/common.hpp
index b77604e..70943a6 100644
--- a/common.hpp
+++ b/common.hpp
@@ -50,6 +50,7 @@
 #include <ndn-cxx/interest.hpp>
 #include <ndn-cxx/data.hpp>
 #include <ndn-cxx/util/event-emitter.hpp>
+#include <ndn-cxx/util/ethernet.hpp>
 
 #include <boost/algorithm/string.hpp>
 #include <boost/asio.hpp>
@@ -96,6 +97,7 @@
 
 namespace name = ndn::name;
 namespace time = ndn::time;
+namespace ethernet = ndn::util::ethernet;
 
 } // namespace nfd
 
diff --git a/core/ethernet.cpp b/core/ethernet.cpp
deleted file mode 100644
index 1aeaca6..0000000
--- a/core/ethernet.cpp
+++ /dev/null
@@ -1,82 +0,0 @@
-/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
-/**
- * Copyright (c) 2014  Regents of the University of California,
- *                     Arizona Board of Regents,
- *                     Colorado State University,
- *                     University Pierre & Marie Curie, Sorbonne University,
- *                     Washington University in St. Louis,
- *                     Beijing Institute of Technology
- *
- * This file is part of NFD (Named Data Networking Forwarding Daemon).
- * See AUTHORS.md for complete list of NFD authors and contributors.
- *
- * NFD is free software: you can redistribute it and/or modify it under the terms
- * of the GNU General Public License as published by the Free Software Foundation,
- * either version 3 of the License, or (at your option) any later version.
- *
- * NFD is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
- * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
- * PURPOSE.  See the GNU General Public License for more details.
- *
- * You should have received a copy of the GNU General Public License along with
- * NFD, e.g., in COPYING.md file.  If not, see <http://www.gnu.org/licenses/>.
- **/
-
-#include "ethernet.hpp"
-
-#include <stdio.h>
-
-namespace nfd {
-namespace ethernet {
-
-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);
-}
-
-Address
-Address::fromString(const std::string& str)
-{
-  unsigned short temp[ADDR_LEN];
-  char sep[5][2]; // 5 * (1 separator char + 1 null terminator)
-  int n = 0; // num of chars read from the input string
-
-  // ISO C++98 does not support the 'hh' type modifier
-  /// \todo use SCNx8 (cinttypes) when we enable C++11
-  int ret = ::sscanf(str.c_str(), "%2hx%1[:-]%2hx%1[:-]%2hx%1[:-]%2hx%1[:-]%2hx%1[:-]%2hx%n",
-                     &temp[0], &sep[0][0], &temp[1], &sep[1][0], &temp[2], &sep[2][0],
-                     &temp[3], &sep[3][0], &temp[4], &sep[4][0], &temp[5], &n);
-
-  if (ret < 11 || static_cast<size_t>(n) != str.length())
-    return Address();
-
-  Address a;
-  for (size_t i = 0; i < ADDR_LEN; ++i)
-    {
-      // check that all separators are actually the same char (: or -)
-      if (i < 5 && sep[i][0] != sep[0][0])
-        return Address();
-
-      // check that each value fits into a uint8_t
-      if (temp[i] > 0xFF)
-        return Address();
-
-      a[i] = static_cast<uint8_t>(temp[i]);
-    }
-
-  return a;
-}
-
-std::ostream&
-operator<<(std::ostream& o, const Address& a)
-{
-  return o << a.toString();
-}
-
-} // namespace ethernet
-} // namespace nfd
diff --git a/core/ethernet.hpp b/core/ethernet.hpp
deleted file mode 100644
index 56888f6..0000000
--- a/core/ethernet.hpp
+++ /dev/null
@@ -1,167 +0,0 @@
-/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
-/**
- * Copyright (c) 2014  Regents of the University of California,
- *                     Arizona Board of Regents,
- *                     Colorado State University,
- *                     University Pierre & Marie Curie, Sorbonne University,
- *                     Washington University in St. Louis,
- *                     Beijing Institute of Technology
- *
- * This file is part of NFD (Named Data Networking Forwarding Daemon).
- * See AUTHORS.md for complete list of NFD authors and contributors.
- *
- * NFD is free software: you can redistribute it and/or modify it under the terms
- * of the GNU General Public License as published by the Free Software Foundation,
- * either version 3 of the License, or (at your option) any later version.
- *
- * NFD is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
- * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
- * PURPOSE.  See the GNU General Public License for more details.
- *
- * You should have received a copy of the GNU General Public License along with
- * NFD, e.g., in COPYING.md file.  If not, see <http://www.gnu.org/licenses/>.
- **/
-
-#ifndef NFD_CORE_ETHERNET_HPP
-#define NFD_CORE_ETHERNET_HPP
-
-#include "common.hpp"
-
-#include <boost/array.hpp>
-
-#define ETHERTYPE_NDN 0x8624
-
-namespace nfd {
-namespace ethernet {
-
-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;
-
-  /// True if this is a null address (00:00:00:00:00:00)
-  bool
-  isNull() const;
-
-  /**
-   * @brief Converts the address to a human-readable string
-   *
-   * @param sep A character used to visually separate the octets,
-   *            usually ':' (the default value) or '-'
-   */
-  std::string
-  toString(char sep = ':') const;
-
-  /**
-   * @brief Creates an Address from a string containing an Ethernet address
-   *        in hexadecimal notation, with colons or hyphens as separators
-   *
-   * @param str The string to be parsed
-   * @return Always an instance of Address, which will be null
-   *         if the parsing fails
-   */
-  static Address
-  fromString(const std::string& str);
-};
-
-/// 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 bool
-Address::isNull() const
-{
-  return elems[0] == 0x0 && elems[1] == 0x0 && elems[2] == 0x0 &&
-         elems[3] == 0x0 && elems[4] == 0x0 && elems[5] == 0x0;
-}
-
-std::ostream&
-operator<<(std::ostream& o, const Address& a);
-
-} // namespace ethernet
-} // namespace nfd
-
-#endif // NFD_FACE_ETHERNET_HPP
diff --git a/core/face-uri.cpp b/core/face-uri.cpp
index 47c265e..635d8d3 100644
--- a/core/face-uri.cpp
+++ b/core/face-uri.cpp
@@ -1,12 +1,12 @@
 /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
 /**
- * Copyright (c) 2014  Regents of the University of California,
- *                     Arizona Board of Regents,
- *                     Colorado State University,
- *                     University Pierre & Marie Curie, Sorbonne University,
- *                     Washington University in St. Louis,
- *                     Beijing Institute of Technology,
- *                     The University of Memphis
+ * Copyright (c) 2014,  Regents of the University of California,
+ *                      Arizona Board of Regents,
+ *                      Colorado State University,
+ *                      University Pierre & Marie Curie, Sorbonne University,
+ *                      Washington University in St. Louis,
+ *                      Beijing Institute of Technology,
+ *                      The University of Memphis
  *
  * This file is part of NFD (Named Data Networking Forwarding Daemon).
  * See AUTHORS.md for complete list of NFD authors and contributors.
@@ -21,15 +21,11 @@
  *
  * You should have received a copy of the GNU General Public License along with
  * NFD, e.g., in COPYING.md file.  If not, see <http://www.gnu.org/licenses/>.
- **/
+ */
 
 #include "face-uri.hpp"
 #include "core/logger.hpp"
 
-#ifdef HAVE_LIBPCAP
-#include "ethernet.hpp"
-#endif // HAVE_LIBPCAP
-
 #include <boost/regex.hpp>
 
 NFD_LOG_INIT("FaceUri");
diff --git a/core/face-uri.hpp b/core/face-uri.hpp
index 9173d8a..17cda71 100644
--- a/core/face-uri.hpp
+++ b/core/face-uri.hpp
@@ -1,11 +1,12 @@
 /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
 /**
- * Copyright (c) 2014  Regents of the University of California,
- *                     Arizona Board of Regents,
- *                     Colorado State University,
- *                     University Pierre & Marie Curie, Sorbonne University,
- *                     Washington University in St. Louis,
- *                     Beijing Institute of Technology
+ * Copyright (c) 2014,  Regents of the University of California,
+ *                      Arizona Board of Regents,
+ *                      Colorado State University,
+ *                      University Pierre & Marie Curie, Sorbonne University,
+ *                      Washington University in St. Louis,
+ *                      Beijing Institute of Technology,
+ *                      The University of Memphis
  *
  * This file is part of NFD (Named Data Networking Forwarding Daemon).
  * See AUTHORS.md for complete list of NFD authors and contributors.
@@ -20,7 +21,7 @@
  *
  * You should have received a copy of the GNU General Public License along with
  * NFD, e.g., in COPYING.md file.  If not, see <http://www.gnu.org/licenses/>.
- **/
+ */
 
 #ifndef NFD_CORE_FACE_URI_H
 #define NFD_CORE_FACE_URI_H
@@ -29,12 +30,6 @@
 
 namespace nfd {
 
-#ifdef HAVE_LIBPCAP
-namespace ethernet {
-class Address;
-} // namespace ethernet
-#endif // HAVE_LIBPCAP
-
 /** \brief represents the underlying protocol and address used by a Face
  *  \sa http://redmine.named-data.net/projects/nfd/wiki/FaceMgmt#FaceUri
  */
diff --git a/core/network-interface.hpp b/core/network-interface.hpp
index 08824fa..baaa85b 100644
--- a/core/network-interface.hpp
+++ b/core/network-interface.hpp
@@ -1,11 +1,12 @@
 /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
 /**
- * Copyright (c) 2014  Regents of the University of California,
- *                     Arizona Board of Regents,
- *                     Colorado State University,
- *                     University Pierre & Marie Curie, Sorbonne University,
- *                     Washington University in St. Louis,
- *                     Beijing Institute of Technology
+ * Copyright (c) 2014,  Regents of the University of California,
+ *                      Arizona Board of Regents,
+ *                      Colorado State University,
+ *                      University Pierre & Marie Curie, Sorbonne University,
+ *                      Washington University in St. Louis,
+ *                      Beijing Institute of Technology,
+ *                      The University of Memphis
  *
  * This file is part of NFD (Named Data Networking Forwarding Daemon).
  * See AUTHORS.md for complete list of NFD authors and contributors.
@@ -20,13 +21,12 @@
  *
  * You should have received a copy of the GNU General Public License along with
  * NFD, e.g., in COPYING.md file.  If not, see <http://www.gnu.org/licenses/>.
- **/
+ */
 
 #ifndef NFD_CORE_NETWORK_INTERFACE_HPP
 #define NFD_CORE_NETWORK_INTERFACE_HPP
 
 #include "common.hpp"
-#include "ethernet.hpp"
 
 #include <net/if.h>
 
diff --git a/daemon/face/ethernet-face.cpp b/daemon/face/ethernet-face.cpp
index 66957db..fe110ad 100644
--- a/daemon/face/ethernet-face.cpp
+++ b/daemon/face/ethernet-face.cpp
@@ -21,7 +21,7 @@
  *
  * You should have received a copy of the GNU General Public License along with
  * NFD, e.g., in COPYING.md file.  If not, see <http://www.gnu.org/licenses/>.
- **/
+ */
 
 #include "ethernet-face.hpp"
 #include "core/logger.hpp"
@@ -78,7 +78,7 @@
   char filter[100];
   ::snprintf(filter, sizeof(filter),
              "(ether proto 0x%x) && (ether dst %s) && (not ether src %s)",
-             ETHERTYPE_NDN,
+             ethernet::ETHERTYPE_NDN,
              m_destAddress.toString().c_str(),
              m_srcAddress.toString().c_str());
   setPacketFilter(filter);
@@ -199,7 +199,7 @@
     }
 
   // construct and prepend the ethernet header
-  static uint16_t ethertype = htons(ETHERTYPE_NDN);
+  static uint16_t ethertype = htons(ethernet::ETHERTYPE_NDN);
   buffer.prependByteArray(reinterpret_cast<const uint8_t*>(&ethertype), ethernet::TYPE_LEN);
   buffer.prependByteArray(m_srcAddress.data(), m_srcAddress.size());
   buffer.prependByteArray(m_destAddress.data(), m_destAddress.size());
@@ -245,7 +245,7 @@
         throw Error("Received packet is too short");
 
       const ether_header* eh = reinterpret_cast<const ether_header*>(packet);
-      if (ntohs(eh->ether_type) != ETHERTYPE_NDN)
+      if (ntohs(eh->ether_type) != ethernet::ETHERTYPE_NDN)
         throw Error("Unrecognized ethertype");
 
       packet += ethernet::HDR_LEN;
diff --git a/daemon/face/ethernet-face.hpp b/daemon/face/ethernet-face.hpp
index 8fd2b08..7530648 100644
--- a/daemon/face/ethernet-face.hpp
+++ b/daemon/face/ethernet-face.hpp
@@ -27,7 +27,6 @@
 #define NFD_DAEMON_FACE_ETHERNET_FACE_HPP
 
 #include "config.hpp"
-#include "ethernet.hpp"
 #include "face.hpp"
 
 #ifndef HAVE_LIBPCAP
diff --git a/tests/core/ethernet.cpp b/tests/core/ethernet.cpp
deleted file mode 100644
index 157f574..0000000
--- a/tests/core/ethernet.cpp
+++ /dev/null
@@ -1,89 +0,0 @@
-/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
-/**
- * Copyright (c) 2014  Regents of the University of California,
- *                     Arizona Board of Regents,
- *                     Colorado State University,
- *                     University Pierre & Marie Curie, Sorbonne University,
- *                     Washington University in St. Louis,
- *                     Beijing Institute of Technology
- *
- * This file is part of NFD (Named Data Networking Forwarding Daemon).
- * See AUTHORS.md for complete list of NFD authors and contributors.
- *
- * NFD is free software: you can redistribute it and/or modify it under the terms
- * of the GNU General Public License as published by the Free Software Foundation,
- * either version 3 of the License, or (at your option) any later version.
- *
- * NFD is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
- * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
- * PURPOSE.  See the GNU General Public License for more details.
- *
- * You should have received a copy of the GNU General Public License along with
- * NFD, e.g., in COPYING.md file.  If not, see <http://www.gnu.org/licenses/>.
- **/
-
-#include "core/ethernet.hpp"
-#include "tests/test-common.hpp"
-
-namespace nfd {
-namespace tests {
-
-BOOST_FIXTURE_TEST_SUITE(FaceEthernetAddress, BaseFixture)
-
-BOOST_AUTO_TEST_CASE(Checks)
-{
-  BOOST_CHECK(ethernet::Address().isNull());
-  BOOST_CHECK(ethernet::getBroadcastAddress().isBroadcast());
-  BOOST_CHECK(ethernet::getDefaultMulticastAddress().isMulticast());
-}
-
-BOOST_AUTO_TEST_CASE(ToString)
-{
-  BOOST_CHECK_EQUAL(ethernet::Address().toString('-'),
-                    "00-00-00-00-00-00");
-  BOOST_CHECK_EQUAL(ethernet::getBroadcastAddress().toString(),
-                    "ff:ff:ff:ff:ff:ff");
-  BOOST_CHECK_EQUAL(ethernet::Address(0x01, 0x23, 0x45, 0x67, 0x89, 0xAB).toString('-'),
-                    "01-23-45-67-89-ab");
-  BOOST_CHECK_EQUAL(ethernet::Address(0x01, 0x23, 0x45, 0x67, 0x89, 0xAB).toString(),
-                    "01:23:45:67:89:ab");
-}
-
-BOOST_AUTO_TEST_CASE(FromString)
-{
-  BOOST_CHECK_EQUAL(ethernet::Address::fromString("0:0:0:0:0:0"),
-                    ethernet::Address());
-  BOOST_CHECK_EQUAL(ethernet::Address::fromString("ff-ff-ff-ff-ff-ff"),
-                    ethernet::getBroadcastAddress());
-  BOOST_CHECK_EQUAL(ethernet::Address::fromString("de:ad:be:ef:1:2"),
-                    ethernet::Address(0xde, 0xad, 0xbe, 0xef, 0x01, 0x02));
-  BOOST_CHECK_EQUAL(ethernet::Address::fromString("DE:AD:BE:EF:1:2"),
-                    ethernet::Address(0xde, 0xad, 0xbe, 0xef, 0x01, 0x02));
-
-  // malformed inputs
-  BOOST_CHECK_EQUAL(ethernet::Address::fromString("01.23.45.67.89.ab"),
-                    ethernet::Address());
-  BOOST_CHECK_EQUAL(ethernet::Address::fromString("01:23:45 :67:89:ab"),
-                    ethernet::Address());
-  BOOST_CHECK_EQUAL(ethernet::Address::fromString("01:23:45:67:89::1"),
-                    ethernet::Address());
-  BOOST_CHECK_EQUAL(ethernet::Address::fromString("01-23-45-67-89"),
-                    ethernet::Address());
-  BOOST_CHECK_EQUAL(ethernet::Address::fromString("01:23:45:67:89:ab:cd"),
-                    ethernet::Address());
-  BOOST_CHECK_EQUAL(ethernet::Address::fromString("01:23:45:67-89-ab"),
-                    ethernet::Address());
-  BOOST_CHECK_EQUAL(ethernet::Address::fromString("qw-er-ty-12-34-56"),
-                    ethernet::Address());
-  BOOST_CHECK_EQUAL(ethernet::Address::fromString("this-is-not-an-ethernet-address"),
-                    ethernet::Address());
-  BOOST_CHECK_EQUAL(ethernet::Address::fromString("foobar"),
-                    ethernet::Address());
-  BOOST_CHECK_EQUAL(ethernet::Address::fromString(""),
-                    ethernet::Address());
-}
-
-BOOST_AUTO_TEST_SUITE_END()
-
-} // namespace tests
-} // namespace nfd
diff --git a/tests/core/face-uri.cpp b/tests/core/face-uri.cpp
index c3060fd..11d7083 100644
--- a/tests/core/face-uri.cpp
+++ b/tests/core/face-uri.cpp
@@ -1,12 +1,12 @@
 /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
 /**
- * Copyright (c) 2014  Regents of the University of California,
- *                     Arizona Board of Regents,
- *                     Colorado State University,
- *                     University Pierre & Marie Curie, Sorbonne University,
- *                     Washington University in St. Louis,
- *                     Beijing Institute of Technology,
- *                     The University of Memphis
+ * Copyright (c) 2014,  Regents of the University of California,
+ *                      Arizona Board of Regents,
+ *                      Colorado State University,
+ *                      University Pierre & Marie Curie, Sorbonne University,
+ *                      Washington University in St. Louis,
+ *                      Beijing Institute of Technology,
+ *                      The University of Memphis
  *
  * This file is part of NFD (Named Data Networking Forwarding Daemon).
  * See AUTHORS.md for complete list of NFD authors and contributors.
@@ -21,12 +21,9 @@
  *
  * You should have received a copy of the GNU General Public License along with
  * NFD, e.g., in COPYING.md file.  If not, see <http://www.gnu.org/licenses/>.
- **/
+ */
 
 #include "core/face-uri.hpp"
-#ifdef HAVE_LIBPCAP
-#include "core/ethernet.hpp"
-#endif // HAVE_LIBPCAP
 
 #include "tests/test-common.hpp"