core: use ethernet::Address from ndn-cxx

refs #2142

Change-Id: I1258a0a1cadbedb31c506c69c84b7906a61fc761
This commit is contained in:
Junxiao Shi authored and Junxiao Shi committed 2014-11-06 20:27:23 -08:00
1 parent 6990e4c7b8
commit 3ffe66d4ef
10 files changed
+38 -387

No files matched your search

+2
View File
@@ -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 @@ using namespace ndn::tlv;
namespace name = ndn::name;
namespace time = ndn::time;
namespace ethernet = ndn::util::ethernet;
} // namespace nfd
-82
View File
@@ -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
-167
View File
@@ -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
+8 -12
View File
@@ -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");
+8 -13
View File
@@ -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
*/
+8 -8
View File
@@ -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>
+4 -4
View File
@@ -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 @@ EthernetFace::EthernetFace(const shared_ptr<boost::asio::posix::stream_descripto
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 @@ EthernetFace::sendPacket(const ndn::Block& block)
}
// 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 @@ EthernetFace::handleRead(const boost::system::error_code& error, size_t)
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;
-1
View File
@@ -27,7 +27,6 @@
#define NFD_DAEMON_FACE_ETHERNET_FACE_HPP
#include "config.hpp"
#include "ethernet.hpp"
#include "face.hpp"
#ifndef HAVE_LIBPCAP
-89
View File
@@ -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
+8 -11
View File
@@ -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"