MACAddress.cc
Go to the documentation of this file.
1 //
2 // Copyright (c) 2020 Fraunhofer Institute for Applied Information Technology (FIT)
3 // Network Research Group (NET)
4 // Schloss Birlinghoven, 53754 Sankt Augustin, GERMANY
5 // Contact: support@wiback.org
6 //
7 // This file is part of the SENF code tree.
8 // It is licensed under the 3-clause BSD License (aka New BSD License).
9 // See LICENSE.txt in the top level directory for details or visit
10 // https://opensource.org/licenses/BSD-3-Clause
11 //
12 
13 
17 #include "MACAddress.hh"
18 //#include "MACAddress.ih"
19 
20 // Custom includes
21 #include <iomanip>
22 #include <boost/io/ios_state.hpp>
24 #include "ParseString.hh"
25 #include "EUI64.hh"
26 
27 //#include "MACAddress.mpp"
28 #define prefix_
29 //-/////////////////////////////////////////////////////////////////////////////////////////////////
30 
31 //-/////////////////////////////////////////////////////////////////////////////////////////////////
32 // senf::MACAddress
33 
35 {
37  detail::parseHexString(s, ":-", mac.begin(), mac.end());
38  return mac;
39 }
40 
42 {
43  if (eui[3] != 0xffu || eui[4] != 0xfeu)
44  throw AddressSyntaxException() << ": EUI64 is not MAC compatible: " << eui;
46  mac[0] = eui[0];
47  mac[1] = eui[1];
48  mac[2] = eui[2];
49  mac[3] = eui[5];
50  mac[4] = eui[6];
51  mac[5] = eui[7];
52  return mac;
53 }
54 
57 
58 
59 //-/////////////////////////////////////////////////////////////////////////////////////////////////
60 // namespace members
61 
62 prefix_ std::ostream & senf::operator<<(std::ostream & os, MACAddress const & mac)
63 {
64  boost::io::ios_all_saver ias (os);
65  os << std::hex << std::setfill('0') << std::right;
66  for (MACAddress::const_iterator i (mac.begin()); i != mac.end(); ++i) {
67  if (i != mac.begin())
68  os << ':';
69  os << std::setw(2) << unsigned(*i);
70  }
71  return os;
72 }
73 
74 prefix_ std::istream & senf::operator>>(std::istream & is, MACAddress & mac)
75 {
76  std::string s;
77  if (!(is >> s))
78  return is;
79  try {
80  mac = MACAddress::from_string(s);
81  }
82  catch (AddressException &) {
83  is.setstate(std::ios::failbit);
84  }
85  return is;
86 }
87 
88 //-/////////////////////////////////////////////////////////////////////////////////////////////////
89 #undef prefix_
90 //#include "MACAddress.mpp"
91 
92 
93 // Local Variables:
94 // mode: c++
95 // fill-column: 100
96 // comment-column: 40
97 // c-file-style: "senf"
98 // indent-tabs-mode: nil
99 // ispell-local-dictionary: "american"
100 // compile-command: "scons -u test"
101 // End:
MACAddress public header.
static MACAddress const None
The empty (0) address.
Definition: MACAddress.hh:76
EUI-64 data type.
Definition: EUI64.hh:66
std::uint8_t mac[6]
Ethernet MAC address.
Definition: MACAddress.hh:72
ParseString public header.
std::ostream & operator<<(std::ostream &os, Packet const &packet)
static MACAddress from_string(std::string const &s)
Construct address from string representation.
Definition: MACAddress.cc:34
noinit
static MACAddress from_eui64(senf::EUI64 const &eui)
Construct address from EUI-64.
Definition: MACAddress.cc:41
std::istream & operator>>(std::istream &is, INet4SocketAddress &addr)
EUI64 public header.
AddressExceptions public header.
#define prefix_
Definition: MACAddress.cc:28
void parseHexString(std::string const &value, char const *separator, ForwardIterator f, ForwardIterator l)
Base-class for Address exceptions.
Invalid address syntax.
static MACAddress const Broadcast
The broadcast address.
Definition: MACAddress.hh:75