NS-3 based Named Data Networking (NDN) simulator
ndnSIM 2.5: NDN, CCN, CCNx, content centric networks
API Documentation
ethernet.cpp
Go to the documentation of this file.
1 /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2 /*
3  * Copyright (c) 2014-2018 Regents of the University of California,
4  * Arizona Board of Regents,
5  * Colorado State University,
6  * University Pierre & Marie Curie, Sorbonne University,
7  * Washington University in St. Louis,
8  * Beijing Institute of Technology,
9  * The University of Memphis.
10  *
11  * This file is part of ndn-cxx library (NDN C++ library with eXperimental eXtensions).
12  *
13  * ndn-cxx library is free software: you can redistribute it and/or modify it under the
14  * terms of the GNU Lesser General Public License as published by the Free Software
15  * Foundation, either version 3 of the License, or (at your option) any later version.
16  *
17  * ndn-cxx library is distributed in the hope that it will be useful, but WITHOUT ANY
18  * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A
19  * PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details.
20  *
21  * You should have received copies of the GNU General Public License and GNU Lesser
22  * General Public License along with ndn-cxx, e.g., in COPYING.md file. If not, see
23  * <http://www.gnu.org/licenses/>.
24  *
25  * See AUTHORS.md for complete list of ndn-cxx authors and contributors.
26  */
27 
28 #include "ndn-cxx/net/ethernet.hpp"
29 
30 #include <boost/functional/hash.hpp>
31 
32 #include <stdio.h>
33 #include <cstdio>
34 #include <ostream>
35 
36 namespace ndn {
37 namespace ethernet {
38 
40 {
41  fill(0);
42 }
43 
44 Address::Address(uint8_t a1, uint8_t a2, uint8_t a3, uint8_t a4, uint8_t a5, uint8_t a6)
45 {
46  data()[0] = a1;
47  data()[1] = a2;
48  data()[2] = a3;
49  data()[3] = a4;
50  data()[4] = a5;
51  data()[5] = a6;
52 }
53 
54 Address::Address(const uint8_t octets[ADDR_LEN])
55 {
56  std::copy(octets, octets + size(), begin());
57 }
58 
59 bool
61 {
62  return *this == getBroadcastAddress();
63 }
64 
65 bool
67 {
68  return (at(0) & 1) != 0;
69 }
70 
71 bool
73 {
74  return *this == Address();
75 }
76 
77 std::string
78 Address::toString(char sep) const
79 {
80  char s[18]; // 12 digits + 5 separators + null terminator
81 
82  // - apparently gcc-4.6 does not support the 'hh' type modifier
83  // - std::snprintf not found in some environments
84  // https://redmine.named-data.net/issues/2299 for more information
85  snprintf(s, sizeof(s), "%02x%c%02x%c%02x%c%02x%c%02x%c%02x",
86  at(0), sep, at(1), sep, at(2), sep, at(3), sep, at(4), sep, at(5));
87 
88  return std::string(s);
89 }
90 
91 Address
92 Address::fromString(const std::string& str)
93 {
94  Address a;
95  unsigned short temp[a.size()];
96  char sep[5][2]; // 5 * (1 separator char + 1 null terminator)
97  int n = 0; // num of chars read from the input string
98 
99  // apparently gcc-4.6 does not support the 'hh' type modifier
100  int ret = std::sscanf(str.c_str(), "%2hx%1[:-]%2hx%1[:-]%2hx%1[:-]%2hx%1[:-]%2hx%1[:-]%2hx%n",
101  &temp[0], &sep[0][0], &temp[1], &sep[1][0], &temp[2], &sep[2][0],
102  &temp[3], &sep[3][0], &temp[4], &sep[4][0], &temp[5], &n);
103 
104  if (ret < 11 || static_cast<size_t>(n) != str.length())
105  return Address();
106 
107  for (size_t i = 0; i < a.size(); ++i) {
108  // check that all separators are actually the same char (: or -)
109  if (i < 5 && sep[i][0] != sep[0][0])
110  return Address();
111 
112  // check that each value fits into a uint8_t
113  if (temp[i] > 0xFF)
114  return Address();
115 
116  a[i] = static_cast<uint8_t>(temp[i]);
117  }
118 
119  return a;
120 }
121 
122 Address
124 {
125  return { 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF };
126 }
127 
128 Address
130 {
131  return { 0x01, 0x00, 0x5E, 0x00, 0x17, 0xAA };
132 }
133 
134 std::ostream&
135 operator<<(std::ostream& o, const Address& a)
136 {
137  return o << a.toString();
138 }
139 
140 } // namespace ethernet
141 } // namespace ndn
142 
143 std::size_t
145 {
146  return boost::hash_range(a.cbegin(), a.cend());
147 }
ndn::ethernet::Address::Address
Address()
Constructs a null Ethernet address (00:00:00:00:00:00)
Definition: ethernet.cpp:39
ndn::ethernet::Address::isBroadcast
bool isBroadcast() const
True if this is a broadcast address (ff:ff:ff:ff:ff:ff)
Definition: ethernet.cpp:60
ndn::ethernet::operator<<
std::ostream & operator<<(std::ostream &o, const Address &a)
Definition: ethernet.cpp:135
ndn::ethernet::Address
represents an Ethernet hardware address
Definition: ethernet.hpp:53
ndn::ethernet::getDefaultMulticastAddress
Address getDefaultMulticastAddress()
Returns the default Ethernet multicast address for NDN.
Definition: ethernet.cpp:129
ndn::ethernet::Address::fromString
static Address fromString(const std::string &str)
Creates an Address from a string containing an Ethernet address in hexadecimal notation,...
Definition: ethernet.cpp:92
std::hash< ndn::ethernet::Address >::operator()
size_t operator()(const ndn::ethernet::Address &a) const noexcept
Definition: ethernet.cpp:144
ndn::ethernet::Address::isNull
bool isNull() const
True if this is a null address (00:00:00:00:00:00)
Definition: ethernet.cpp:72
ndn::ethernet::Address::toString
std::string toString(char sep=':') const
Converts the address to a human-readable string.
Definition: ethernet.cpp:78
ndn::ethernet::ADDR_LEN
const size_t ADDR_LEN
Octets in one Ethernet address.
Definition: ethernet.hpp:41
ndn::ethernet::getBroadcastAddress
Address getBroadcastAddress()
Returns the Ethernet broadcast address (ff:ff:ff:ff:ff:ff)
Definition: ethernet.cpp:123
ndn::ethernet::Address::isMulticast
bool isMulticast() const
True if this is a multicast address.
Definition: ethernet.cpp:66
ethernet.hpp
ndn
Copyright (c) 2011-2015 Regents of the University of California.
Definition: ndn-strategy-choice-helper.hpp:34