|
| 1 | +// This file is a part of the IncludeOS unikernel - www.includeos.org |
| 2 | +// |
| 3 | +// Copyright 2015-2016 Oslo and Akershus University College of Applied Sciences |
| 4 | +// and Alfred Bratterud |
| 5 | +// |
| 6 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 7 | +// you may not use this file except in compliance with the License. |
| 8 | +// You may obtain a copy of the License at |
| 9 | +// |
| 10 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | +// |
| 12 | +// Unless required by applicable law or agreed to in writing, software |
| 13 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 14 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 15 | +// See the License for the specific language governing permissions and |
| 16 | +// limitations under the License. |
| 17 | +#pragma once |
| 18 | + |
| 19 | +#ifndef HW_MAC_ADDR_HPP |
| 20 | +#define HW_MAC_ADDR_HPP |
| 21 | + |
| 22 | +namespace hw { |
| 23 | +// MAC address |
| 24 | +union MAC_addr { |
| 25 | + // no. parts in a MAC address |
| 26 | + static constexpr size_t PARTS_LEN = 6; |
| 27 | + // The parts of the MAC address |
| 28 | + uint8_t part[PARTS_LEN]; |
| 29 | + |
| 30 | + struct { |
| 31 | + uint16_t minor; |
| 32 | + uint32_t major; |
| 33 | + } __attribute__((packed)); |
| 34 | + |
| 35 | + MAC_addr() noexcept : part{} {} |
| 36 | + |
| 37 | + MAC_addr(const uint8_t a, const uint8_t b, const uint8_t c, |
| 38 | + const uint8_t d, const uint8_t e, const uint8_t f) noexcept |
| 39 | + : part{a,b,c,d,e,f} |
| 40 | + {} |
| 41 | + |
| 42 | + MAC_addr& operator=(const MAC_addr cpy) noexcept { |
| 43 | + minor = cpy.minor; |
| 44 | + major = cpy.major; |
| 45 | + return *this; |
| 46 | + } |
| 47 | + |
| 48 | + /** |
| 49 | + * @brief Hex representation of a MAC address |
| 50 | + * |
| 51 | + * @return hex string representation |
| 52 | + */ |
| 53 | + std::string hex_str() const { |
| 54 | + char hex_addr[18]; |
| 55 | + snprintf(hex_addr, sizeof(hex_addr), "%02x:%02x:%02x:%02x:%02x:%02x", |
| 56 | + part[0], part[1], part[2], |
| 57 | + part[3], part[4], part[5]); |
| 58 | + return hex_addr; |
| 59 | + } |
| 60 | + |
| 61 | + /** |
| 62 | + * @brief String representation of a MAC address |
| 63 | + * @note default is hex |
| 64 | + * @return string representation of the MAC address |
| 65 | + */ |
| 66 | + std::string str() const |
| 67 | + { return hex_str(); } |
| 68 | + |
| 69 | + std::string to_string() const |
| 70 | + { return str(); } |
| 71 | + |
| 72 | + operator std::string () const |
| 73 | + { return str(); } |
| 74 | + |
| 75 | + /** Check for equality */ |
| 76 | + bool operator==(const MAC_addr mac) const noexcept |
| 77 | + { |
| 78 | + return strncmp( |
| 79 | + reinterpret_cast<const char*>(part), |
| 80 | + reinterpret_cast<const char*>(mac.part), |
| 81 | + PARTS_LEN) == 0; |
| 82 | + } |
| 83 | + |
| 84 | +} __attribute__((packed)); //< union addr |
| 85 | +} |
| 86 | + |
| 87 | +#endif |
0 commit comments