|
1 | 1 | // This file is a part of the IncludeOS unikernel - www.includeos.org |
2 | 2 | // |
3 | | -// Copyright 2015-2016 Oslo and Akershus University College of Applied Sciences |
| 3 | +// Copyright 2015-2017 Oslo and Akershus University College of Applied Sciences |
4 | 4 | // and Alfred Bratterud |
5 | 5 | // |
6 | 6 | // Licensed under the Apache License, Version 2.0 (the "License"); |
|
14 | 14 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
15 | 15 | // See the License for the specific language governing permissions and |
16 | 16 | // limitations under the License. |
17 | | -#pragma once |
18 | 17 |
|
| 18 | +#pragma once |
19 | 19 | #ifndef HW_MAC_ADDR_HPP |
20 | 20 | #define HW_MAC_ADDR_HPP |
21 | 21 |
|
22 | | -#include <stddef.h> // size_t |
23 | | -#include <stdint.h> // uint |
24 | | -#include <cstdio> // snprintf |
| 22 | +#include <cstddef> |
| 23 | +#include <cstdint> |
| 24 | +#include <cstdio> |
| 25 | +#include <cstring> |
25 | 26 | #include <string> |
26 | | -#include <cstring> // strncmp |
27 | 27 |
|
28 | 28 | namespace MAC { |
29 | 29 |
|
30 | | - // MAC address |
31 | | - union Addr { |
32 | | - // no. parts in a MAC address |
33 | | - static constexpr size_t PARTS_LEN = 6; |
34 | | - // The parts of the MAC address |
35 | | - uint8_t part[PARTS_LEN]; |
36 | | - |
37 | | - struct { |
38 | | - uint16_t minor; |
39 | | - uint32_t major; |
40 | | - } __attribute__((packed)); |
41 | | - |
42 | | - Addr() noexcept : part{} {} |
43 | | - |
44 | | - Addr(const uint8_t a, const uint8_t b, const uint8_t c, |
45 | | - const uint8_t d, const uint8_t e, const uint8_t f) noexcept |
46 | | - : part{a,b,c,d,e,f} |
47 | | - {} |
48 | | - |
49 | | - Addr& operator=(const Addr cpy) noexcept { |
50 | | - minor = cpy.minor; |
51 | | - major = cpy.major; |
52 | | - return *this; |
53 | | - } |
54 | | - |
55 | | - |
56 | | - /** |
57 | | - * @brief Hex representation of a MAC address |
58 | | - * |
59 | | - * @return hex string representation |
60 | | - */ |
61 | | - std::string hex_str() const { |
62 | | - char hex_addr[18]; |
63 | | - snprintf(hex_addr, sizeof(hex_addr), "%02x:%02x:%02x:%02x:%02x:%02x", |
64 | | - part[0], part[1], part[2], |
65 | | - part[3], part[4], part[5]); |
66 | | - return hex_addr; |
67 | | - } |
68 | | - |
69 | | - /** |
70 | | - * @brief String representation of a MAC address |
71 | | - * @note default is hex |
72 | | - * @return string representation of the MAC address |
73 | | - */ |
74 | | - std::string str() const |
75 | | - { return hex_str(); } |
76 | | - |
77 | | - std::string to_string() const |
78 | | - { return str(); } |
79 | | - |
80 | | - operator std::string () const |
81 | | - { return str(); } |
82 | | - |
83 | | - /** Check for equality */ |
84 | | - bool operator==(const Addr mac) const noexcept |
85 | | - { |
86 | | - return strncmp( |
87 | | - reinterpret_cast<const char*>(part), |
88 | | - reinterpret_cast<const char*>(mac.part), |
89 | | - PARTS_LEN) == 0; |
90 | | - } |
91 | | - |
92 | | - bool operator!=(const Addr mac) const noexcept |
93 | | - { return not(*this == mac); } |
94 | | - |
95 | | - } __attribute__((packed)); //< union addr |
96 | | - |
97 | | - const Addr EMPTY {0x0, 0x0, 0x0, 0x0, 0x0, 0x0}; |
98 | | - |
99 | | - // uint16_t(0xFFFF), uint32_t(0xFFFFFFFF) |
100 | | - const Addr BROADCAST {0xff,0xff,0xff,0xff,0xff,0xff}; |
101 | | - |
102 | | - // uint16_t(0x0000), uint32_t(0x01000000) |
103 | | - const Addr MULTICAST {0,0,0x01,0,0,0}; |
104 | | - |
105 | | - // uint16_t(0x3333), uint32_t(0x01000000) |
106 | | - const Addr IPv6mcast_01 {0x33,0x33,0x01,0,0,0}; |
107 | | - |
108 | | - // uint16_t(0x3333), uint32_t(0x02000000) |
109 | | - const Addr IPv6mcast_02 {0x33,0x33,0x02,0,0,0}; |
110 | | - |
111 | | - |
112 | | -} |
113 | | - |
114 | | -#endif |
| 30 | +/** |
| 31 | + * MAC address representation |
| 32 | + */ |
| 33 | +union Addr { |
| 34 | + /** |
| 35 | + * Default constructor |
| 36 | + */ |
| 37 | + constexpr Addr() noexcept : part{} {} |
| 38 | + |
| 39 | + /** |
| 40 | + * Constructor |
| 41 | + * |
| 42 | + * Create a MAC address by specifying each part of the address |
| 43 | + * |
| 44 | + * @param a |
| 45 | + * The first part of the MAC address |
| 46 | + * |
| 47 | + * @param b |
| 48 | + * The second part of the MAC address |
| 49 | + * |
| 50 | + * @param c |
| 51 | + * The third part of the MAC address |
| 52 | + * |
| 53 | + * @param d |
| 54 | + * The fourth part of the MAC address |
| 55 | + * |
| 56 | + * @param e |
| 57 | + * The fifth part of the MAC address |
| 58 | + * |
| 59 | + * @param f |
| 60 | + * The sixth part of the MAC address |
| 61 | + */ |
| 62 | + constexpr Addr(const uint8_t a, const uint8_t b, const uint8_t c, |
| 63 | + const uint8_t d, const uint8_t e, const uint8_t f) noexcept |
| 64 | + : part{a,b,c,d,e,f} |
| 65 | + {} |
| 66 | + |
| 67 | + /** |
| 68 | + * Assignment operator |
| 69 | + * |
| 70 | + * @param other |
| 71 | + * The MAC address object to assign from |
| 72 | + * |
| 73 | + * @return The object that invoked this method |
| 74 | + */ |
| 75 | + constexpr Addr& operator=(const Addr other) noexcept { |
| 76 | + minor = other.minor; |
| 77 | + major = other.major; |
| 78 | + return *this; |
| 79 | + } |
| 80 | + |
| 81 | + |
| 82 | + /** |
| 83 | + * Get a hex string representation of a MAC address |
| 84 | + * |
| 85 | + * @return A hex string representation of a MAC address |
| 86 | + */ |
| 87 | + std::string hex_str() const { |
| 88 | + char hex_addr[18]; |
| 89 | + |
| 90 | + snprintf(hex_addr, sizeof(hex_addr), "%02x:%02x:%02x:%02x:%02x:%02x", |
| 91 | + part[0], part[1], part[2], part[3], part[4], part[5]); |
| 92 | + |
| 93 | + return hex_addr; |
| 94 | + } |
| 95 | + |
| 96 | + /** |
| 97 | + * Get a string representation of this type |
| 98 | + * |
| 99 | + * @return A string representation of this type |
| 100 | + * |
| 101 | + * @note String representation format is in hex |
| 102 | + */ |
| 103 | + std::string str() const |
| 104 | + { return hex_str(); } |
| 105 | + |
| 106 | + /** |
| 107 | + * Get a string representation of this type |
| 108 | + * |
| 109 | + * @return A string representation of this type |
| 110 | + * |
| 111 | + * @note String representation format is in hex |
| 112 | + */ |
| 113 | + std::string to_string() const |
| 114 | + { return hex_str(); } |
| 115 | + |
| 116 | + /** |
| 117 | + * Operator to transform this type into string form |
| 118 | + * |
| 119 | + * @note String representation format is in hex |
| 120 | + */ |
| 121 | + operator std::string () const |
| 122 | + { return hex_str(); } |
| 123 | + |
| 124 | + /** |
| 125 | + * Operator to check for equality |
| 126 | + * |
| 127 | + * @param other |
| 128 | + * The MAC address object to check for equality |
| 129 | + * |
| 130 | + * @return true if this object is equal to other, false otherwise |
| 131 | + */ |
| 132 | + constexpr bool operator==(const Addr other) const noexcept { |
| 133 | + return (minor == other.minor) |
| 134 | + and (major == other.major); |
| 135 | + } |
| 136 | + |
| 137 | + /** |
| 138 | + * Operator to check for inequality |
| 139 | + * |
| 140 | + * @param other |
| 141 | + * The MAC address object to check for inequality |
| 142 | + * |
| 143 | + * @return true if this object is not equal to other, false otherwise |
| 144 | + */ |
| 145 | + constexpr bool operator!=(const Addr other) const noexcept |
| 146 | + { return not (*this == other); } |
| 147 | + |
| 148 | + |
| 149 | + static constexpr const size_t PARTS_LEN {6}; //< Number of parts in a MAC address |
| 150 | + uint8_t part[PARTS_LEN]; //< The parts of the MAC address |
| 151 | + |
| 152 | + struct { |
| 153 | + uint16_t minor; |
| 154 | + uint32_t major; |
| 155 | + } __attribute__((packed)); |
| 156 | +} __attribute__((packed)); //< union Addr |
| 157 | + |
| 158 | +constexpr const Addr EMPTY {}; |
| 159 | +constexpr const Addr BROADCAST {0xff,0xff,0xff,0xff,0xff,0xff}; //< uint16_t(0xFFFF), uint32_t(0xFFFFFFFF) |
| 160 | +constexpr const Addr MULTICAST {0x00,0x00,0x01,0x00,0x00,0x00}; //< uint16_t(0x0000), uint32_t(0x01000000) |
| 161 | +constexpr const Addr IPv6mcast_01 {0x33,0x33,0x01,0x00,0x00,0x00}; //< uint16_t(0x3333), uint32_t(0x01000000) |
| 162 | +constexpr const Addr IPv6mcast_02 {0x33,0x33,0x02,0x00,0x00,0x00}; //< uint16_t(0x3333), uint32_t(0x02000000) |
| 163 | +} //< namespace MAC |
| 164 | + |
| 165 | +#endif //< HW_MAC_ADDR_HPP |
0 commit comments