NS-3 based Named Data Networking (NDN) simulator
ndnSIM 2.3: 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; -*- */
22 #include "ethernet.hpp"
23 
24 #include <boost/functional/hash.hpp>
25 
26 #include <stdio.h>
27 #include <ostream>
28 
29 namespace ndn {
30 namespace util {
31 namespace ethernet {
32 
34 {
35  fill(0);
36 }
37 
38 Address::Address(uint8_t a1, uint8_t a2, uint8_t a3, uint8_t a4, uint8_t a5, uint8_t a6)
39 {
40  data()[0] = a1;
41  data()[1] = a2;
42  data()[2] = a3;
43  data()[3] = a4;
44  data()[4] = a5;
45  data()[5] = a6;
46 }
47 
48 Address::Address(const uint8_t octets[ADDR_LEN])
49 {
50  std::copy(octets, octets + size(), begin());
51 }
52 
53 bool
55 {
56  return *this == getBroadcastAddress();
57 }
58 
59 bool
61 {
62  return (at(0) & 1) != 0;
63 }
64 
65 bool
67 {
68  return *this == Address();
69 }
70 
71 std::string
72 Address::toString(char sep) const
73 {
74  char s[18]; // 12 digits + 5 separators + null terminator
75 
76  // - apparently gcc-4.6 does not support the 'hh' type modifier
77  // - std::snprintf not found in some environments
78  // http://redmine.named-data.net/issues/2299 for more information
79  snprintf(s, sizeof(s), "%02x%c%02x%c%02x%c%02x%c%02x%c%02x",
80  at(0), sep, at(1), sep, at(2), sep, at(3), sep, at(4), sep, at(5));
81 
82  return std::string(s);
83 }
84 
85 Address
86 Address::fromString(const std::string& str)
87 {
88  Address a;
89  unsigned short temp[a.size()];
90  char sep[5][2]; // 5 * (1 separator char + 1 null terminator)
91  int n = 0; // num of chars read from the input string
92 
93  // apparently gcc-4.6 does not support the 'hh' type modifier
94  int ret = std::sscanf(str.c_str(), "%2hx%1[:-]%2hx%1[:-]%2hx%1[:-]%2hx%1[:-]%2hx%1[:-]%2hx%n",
95  &temp[0], &sep[0][0], &temp[1], &sep[1][0], &temp[2], &sep[2][0],
96  &temp[3], &sep[3][0], &temp[4], &sep[4][0], &temp[5], &n);
97 
98  if (ret < 11 || static_cast<size_t>(n) != str.length())
99  return Address();
100 
101  for (size_t i = 0; i < a.size(); ++i)
102  {
103  // check that all separators are actually the same char (: or -)
104  if (i < 5 && sep[i][0] != sep[0][0])
105  return Address();
106 
107  // check that each value fits into a uint8_t
108  if (temp[i] > 0xFF)
109  return Address();
110 
111  a[i] = static_cast<uint8_t>(temp[i]);
112  }
113 
114  return a;
115 }
116 
117 Address
119 {
120  return { 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF };
121 }
122 
123 Address
125 {
126  return { 0x01, 0x00, 0x5E, 0x00, 0x17, 0xAA };
127 }
128 
129 std::ostream&
130 operator<<(std::ostream& o, const Address& a)
131 {
132  return o << a.toString();
133 }
134 
135 } // namespace ethernet
136 } // namespace util
137 } // namespace ndn
138 
139 
140 std::size_t
142 {
143  return boost::hash_range(a.cbegin(), a.cend());
144 }
Copyright (c) 2011-2015 Regents of the University of California.
const size_t ADDR_LEN
Octets in one Ethernet address.
Definition: ethernet.hpp:42
represents an Ethernet hardware address
Definition: ethernet.hpp:53
static Address fromString(const std::string &str)
Creates an Address from a string containing an Ethernet address in hexadecimal notation, with colons or hyphens as separators.
Definition: ethernet.cpp:86
Address getBroadcastAddress()
Returns the Ethernet broadcast address (ff:ff:ff:ff:ff:ff)
Definition: ethernet.cpp:118
size_t operator()(const ndn::util::ethernet::Address &a) const noexcept
Definition: ethernet.cpp:141
Address getDefaultMulticastAddress()
Returns the default Ethernet multicast address for NDN.
Definition: ethernet.cpp:124
Address()
Constructs a null Ethernet address (00:00:00:00:00:00)
Definition: ethernet.cpp:33
bool isBroadcast() const
True if this is a broadcast address (ff:ff:ff:ff:ff:ff)
Definition: ethernet.cpp:54
std::ostream & operator<<(std::ostream &o, const Address &a)
Definition: ethernet.cpp:130
bool isNull() const
True if this is a null address (00:00:00:00:00:00)
Definition: ethernet.cpp:66
bool isMulticast() const
True if this is a multicast address.
Definition: ethernet.cpp:60
std::string toString(char sep=':') const
Converts the address to a human-readable string.
Definition: ethernet.cpp:72