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-2023 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 <algorithm>
33 #include <cinttypes>
34 #include <cstdio>
35 #include <ostream>
36 
37 namespace ndn::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 (front() & 1) != 0;
69 }
70 
71 bool
73 {
74  return *this == Address();
75 }
76 
77 std::string
78 Address::toString(char sep) const
79 {
80  const auto& a = *this;
81  char s[18]; // 12 digits + 5 separators + null terminator
82 
83  std::snprintf(s, sizeof(s),
84  "%02" PRIx8 "%c%02" PRIx8 "%c%02" PRIx8 "%c%02" PRIx8 "%c%02" PRIx8 "%c%02" PRIx8,
85  a[0], sep, a[1], sep, a[2], sep, a[3], sep, a[4], sep, a[5]);
86  return s;
87 }
88 
89 Address
90 Address::fromString(const std::string& str)
91 {
92  Address a;
93  char sep[5][2]; // 5 * (1 separator char + 1 null terminator)
94  int n = 0; // num of chars read from the input string
95 
96  int ret = std::sscanf(str.data(),
97  "%2" SCNx8 "%1[:-]%2" SCNx8 "%1[:-]%2" SCNx8 "%1[:-]"
98  "%2" SCNx8 "%1[:-]%2" SCNx8 "%1[:-]%2" SCNx8 "%n",
99  &a[0], &sep[0][0], &a[1], &sep[1][0], &a[2], &sep[2][0],
100  &a[3], &sep[3][0], &a[4], &sep[4][0], &a[5], &n);
101 
102  if (ret < 11 || static_cast<size_t>(n) != str.length())
103  return {};
104 
105  // check that all separators are the same char (: or -)
106  for (size_t i = 1; i < 5; ++i) {
107  if (sep[i][0] != sep[0][0])
108  return {};
109  }
110 
111  return a;
112 }
113 
114 Address
116 {
117  return { 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF };
118 }
119 
120 Address
122 {
123  return { 0x01, 0x00, 0x5E, 0x00, 0x17, 0xAA };
124 }
125 
126 std::ostream&
127 operator<<(std::ostream& o, const Address& a)
128 {
129  return o << a.toString();
130 }
131 
132 } // namespace ndn::ethernet
133 
134 std::size_t
136 {
137  return boost::hash_range(a.cbegin(), a.cend());
138 }
Represents an Ethernet hardware address.
Definition: ethernet.hpp:52
Address()
Constructs a null Ethernet address (00:00:00:00:00:00).
Definition: ethernet.cpp:39
std::string toString(char sep=':') const
Converts the address to a human-readable string.
Definition: ethernet.cpp:78
bool isMulticast() const
True if this is a multicast address.
Definition: ethernet.cpp:66
bool isBroadcast() const
True if this is a broadcast address (ff:ff:ff:ff:ff:ff).
Definition: ethernet.cpp:60
bool isNull() const
True if this is a null address (00:00:00:00:00:00).
Definition: ethernet.cpp:72
static Address fromString(const std::string &str)
Creates an Address from a string containing an Ethernet address in hexadecimal notation,...
Definition: ethernet.cpp:90
Address getDefaultMulticastAddress()
Returns the default Ethernet multicast address for NDN.
Definition: ethernet.cpp:121
constexpr size_t ADDR_LEN
Octets in one Ethernet address.
Definition: ethernet.hpp:40
Address getBroadcastAddress()
Returns the Ethernet broadcast address (ff:ff:ff:ff:ff:ff).
Definition: ethernet.cpp:115
std::ostream & operator<<(std::ostream &o, const Address &a)
Definition: ethernet.cpp:127
size_t operator()(const ndn::ethernet::Address &a) const noexcept
Definition: ethernet.cpp:135