MACAddress.hh
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 #ifndef HH_SENF_Socket_Protocols_Raw_MACAddress_
18 #define HH_SENF_Socket_Protocols_Raw_MACAddress_ 1
19 
20 // Custom includes
21 #include <iosfwd>
22 #include <cstdint>
23 #include <boost/array.hpp>
24 #include <functional>
25 #include <senf/Utils/Tags.hh>
26 #include <senf/autoconf.hh>
27 
28 //#include "MACAddress.mpp"
29 //-/////////////////////////////////////////////////////////////////////////////////////////////////
30 
31 namespace senf {
32 
33  struct EUI64;
34 
72  struct MACAddress
73  : public boost::array<std::uint8_t,6>
74  {
75  static MACAddress const Broadcast;
76  static MACAddress const None;
77 
78  // make sure we are compatible with older code expecting boost::array
79  // static constexpr unsigned static_size = 6;
80 
81  MACAddress();
83  explicit MACAddress(std::uint64_t const & v);
84 
85  static MACAddress from_string(std::string const & s);
87 
93  template <class InputIterator>
94  static MACAddress from_data(InputIterator i);
96 
100  static MACAddress from_eui64(senf::EUI64 const & eui);
102 
110  explicit operator bool() const;
111  bool operator<(MACAddress const & other) const;
112  bool operator==(EUI64 const & eui64) const;
113 
114  bool local() const;
115  bool multicast() const;
116  bool broadcast() const;
117 
118  void local(bool flag);
119  void multicast(bool flag);
120 
121  std::uint32_t oui() const;
122  std::uint32_t nic() const;
123 
124  void oui(std::uint32_t oui);
125  void nic(std::uint32_t oui);
126 
127  std::uint64_t eui64() const;
128  std::uint64_t uint64() const;
129 
130  std::uint32_t const & hash32() const;
131  void hash(std::uint64_t * hash, std::uint16_t const & otherKey = 0) const;
132  };
133 
137  std::ostream & operator<<(std::ostream & os, MACAddress const & mac);
138 
144  std::istream & operator>>(std::istream & is, MACAddress & mac);
145 
146  bool operator==(EUI64 const & eui64, MACAddress const & mac);
147 
148 # if __SIZEOF_SIZE_T__ == 8
149  std::size_t hash_value(MACAddress const & mac) noexcept;
150 #elif __SIZEOF_SIZE_T__ == 4
151  std::size_t const & hash_value(MACAddress const & mac) noexcept;
152 #else
153  std::size_t hash_value(MACAddress const & mac) noexcept;
154 #endif
155 }
156 
157 #ifdef SENF_CXX11_ENABLED
158 namespace std {
159  template <> struct hash<senf::MACAddress>
160  {
161 # if __SIZEOF_SIZE_T__ == 8
162  std::size_t operator()(senf::MACAddress const & mac) const noexcept {
163  return senf::hash_value(mac);
164  }
165 #elif __SIZEOF_SIZE_T__ == 4
166  std::size_t const & operator()(senf::MACAddress const & mac) const noexcept {
167  return mac.hash32();
168  }
169 #endif
170  };
171 }
172 #endif
173 
174 //-/////////////////////////////////////////////////////////////////////////////////////////////////
175 #include "MACAddress.cci"
176 #include "MACAddress.ct"
177 //#include "MACAddress.cti"
178 #endif
179 
180 
181 // Local Variables:
182 // mode: c++
183 // fill-column: 100
184 // comment-column: 40
185 // c-file-style: "senf"
186 // indent-tabs-mode: nil
187 // ispell-local-dictionary: "american"
188 // compile-command: "scons -u test"
189 // End:
bool broadcast() const
true, if address is the broadcast address
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]
std::istream & operator>>(std::istream &is, MACAddress &mac)
Try to initialize MACAddress instance from a string representation sets std::ios::failbit on the stre...
Ethernet MAC address.
Definition: MACAddress.hh:72
std::uint32_t const & hash32() const
MACAddress()
Construct zero-initialized address.
STL namespace.
static MACAddress from_string(std::string const &s)
Construct address from string representation.
Definition: MACAddress.cc:34
std::uint64_t eui64() const
Build EUI-64 from the MAC address.
NoInit_t
static MACAddress from_eui64(senf::EUI64 const &eui)
Construct address from EUI-64.
Definition: MACAddress.cc:41
void hash(std::uint64_t *hash, std::uint16_t const &otherKey=0) const
computes a fast uint64 hash mixing in another 16bit value
std::uint32_t nic() const
Return last 3 bytes of the address.
std::ostream & operator<<(std::ostream &os, MACAddress const &mac)
Output MAC instance as it&#39;s string representation.
std::uint64_t uint64() const
Return MAC address as uint64 value.
bool operator<(MACAddress const &other) const
bool local() const
true, if address is locally administered
std::size_t hash_value(MACAddress const &mac) noexcept
std::uint32_t oui() const
Return first 3 bytes of the address.
bool multicast() const
true, if address is a group/multicast address
bool operator==(EUI64 const &eui64) const
static MACAddress from_data(InputIterator i)
Construct address from raw data.
static MACAddress const Broadcast
The broadcast address.
Definition: MACAddress.hh:75