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; -*- */
28 #include "ethernet.hpp"
29 
30 #include <boost/functional/hash.hpp>
31 
32 #include <stdio.h>
33 #include <ostream>
34 
35 namespace ndn {
36 namespace ethernet {
37 
39 {
40  fill(0);
41 }
42 
43 Address::Address(uint8_t a1, uint8_t a2, uint8_t a3, uint8_t a4, uint8_t a5, uint8_t a6)
44 {
45  data()[0] = a1;
46  data()[1] = a2;
47  data()[2] = a3;
48  data()[3] = a4;
49  data()[4] = a5;
50  data()[5] = a6;
51 }
52 
53 Address::Address(const uint8_t octets[ADDR_LEN])
54 {
55  std::copy(octets, octets + size(), begin());
56 }
57 
58 bool
60 {
61  return *this == getBroadcastAddress();
62 }
63 
64 bool
66 {
67  return (at(0) & 1) != 0;
68 }
69 
70 bool
72 {
73  return *this == Address();
74 }
75 
76 std::string
77 Address::toString(char sep) const
78 {
79  char s[18]; // 12 digits + 5 separators + null terminator
80 
81  // - apparently gcc-4.6 does not support the 'hh' type modifier
82  // - std::snprintf not found in some environments
83  // https://redmine.named-data.net/issues/2299 for more information
84  snprintf(s, sizeof(s), "%02x%c%02x%c%02x%c%02x%c%02x%c%02x",
85  at(0), sep, at(1), sep, at(2), sep, at(3), sep, at(4), sep, at(5));
86 
87  return std::string(s);
88 }
89 
90 Address
91 Address::fromString(const std::string& str)
92 {
93  Address a;
94  unsigned short temp[a.size()];
95  char sep[5][2]; // 5 * (1 separator char + 1 null terminator)
96  int n = 0; // num of chars read from the input string
97 
98  // apparently gcc-4.6 does not support the 'hh' type modifier
99  int ret = std::sscanf(str.c_str(), "%2hx%1[:-]%2hx%1[:-]%2hx%1[:-]%2hx%1[:-]%2hx%1[:-]%2hx%n",
100  &temp[0], &sep[0][0], &temp[1], &sep[1][0], &temp[2], &sep[2][0],
101  &temp[3], &sep[3][0], &temp[4], &sep[4][0], &temp[5], &n);
102 
103  if (ret < 11 || static_cast<size_t>(n) != str.length())
104  return Address();
105 
106  for (size_t i = 0; i < a.size(); ++i)
107  {
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 }
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:91
Copyright (c) 2011-2015 Regents of the University of California.
std::ostream & operator<<(std::ostream &o, const Address &a)
Definition: ethernet.cpp:135
const size_t ADDR_LEN
Octets in one Ethernet address.
Definition: ethernet.hpp:41
bool isBroadcast() const
True if this is a broadcast address (ff:ff:ff:ff:ff:ff)
Definition: ethernet.cpp:59
Address getDefaultMulticastAddress()
Returns the default Ethernet multicast address for NDN.
Definition: ethernet.cpp:129
Address getBroadcastAddress()
Returns the Ethernet broadcast address (ff:ff:ff:ff:ff:ff)
Definition: ethernet.cpp:123
bool isMulticast() const
True if this is a multicast address.
Definition: ethernet.cpp:65
std::string toString(char sep= ':') const
Converts the address to a human-readable string.
Definition: ethernet.cpp:77
represents an Ethernet hardware address
Definition: ethernet.hpp:52
Address()
Constructs a null Ethernet address (00:00:00:00:00:00)
Definition: ethernet.cpp:38
size_t operator()(const ndn::ethernet::Address &a) const noexcept
Definition: ethernet.cpp:144
bool isNull() const
True if this is a null address (00:00:00:00:00:00)
Definition: ethernet.cpp:71