William A. Kennington III | bb0eacc | 2022-10-21 15:22:06 -0700 | [diff] [blame] | 1 | #include "types.hpp" |
| 2 | |
William A. Kennington III | b9d7cba | 2022-11-08 10:54:11 -0800 | [diff] [blame] | 3 | #include <fmt/format.h> |
| 4 | |
William A. Kennington III | bb0eacc | 2022-10-21 15:22:06 -0700 | [diff] [blame] | 5 | #include <charconv> |
| 6 | |
William A. Kennington III | b9d7cba | 2022-11-08 10:54:11 -0800 | [diff] [blame] | 7 | namespace phosphor::network |
| 8 | { |
| 9 | |
| 10 | void IfAddr::invalidPfx(uint8_t pfx) |
| 11 | { |
| 12 | throw std::invalid_argument(fmt::format("Invalid prefix {}", pfx)); |
| 13 | } |
| 14 | |
| 15 | namespace detail |
William A. Kennington III | bb0eacc | 2022-10-21 15:22:06 -0700 | [diff] [blame] | 16 | { |
| 17 | |
| 18 | std::string_view AddrBufMaker<ether_addr>::operator()(ether_addr val) noexcept |
| 19 | { |
| 20 | for (char* ptr = buf.data() + 2; ptr < buf.end(); ptr += 3) |
| 21 | { |
| 22 | *ptr = ':'; |
| 23 | } |
| 24 | for (size_t i = 0; i < 6; ++i) |
| 25 | { |
| 26 | char* tmp = buf.data() + i * 3; |
| 27 | uint8_t byte = val.ether_addr_octet[i]; |
| 28 | if (byte < 16) |
| 29 | { |
| 30 | *(tmp++) = '0'; |
| 31 | } |
| 32 | std::to_chars(tmp, buf.end(), byte, 16); |
| 33 | } |
| 34 | return {buf.begin(), buf.size()}; |
| 35 | } |
| 36 | |
William A. Kennington III | 4f07798 | 2022-11-04 02:10:48 -0700 | [diff] [blame^] | 37 | static char* makeInAddr(char* ptr, char* end, in_addr val) noexcept |
William A. Kennington III | bb0eacc | 2022-10-21 15:22:06 -0700 | [diff] [blame] | 38 | { |
William A. Kennington III | 71de63a | 2022-11-08 10:50:54 -0800 | [diff] [blame] | 39 | auto v = bswap(ntoh(val.s_addr)); |
William A. Kennington III | bb0eacc | 2022-10-21 15:22:06 -0700 | [diff] [blame] | 40 | for (size_t i = 0; i < 3; ++i) |
| 41 | { |
William A. Kennington III | 4f07798 | 2022-11-04 02:10:48 -0700 | [diff] [blame^] | 42 | const auto res = std::to_chars(ptr, end, v & 0xff, 10); |
William A. Kennington III | bb0eacc | 2022-10-21 15:22:06 -0700 | [diff] [blame] | 43 | *res.ptr = '.'; |
| 44 | ptr = res.ptr + 1; |
| 45 | v >>= 8; |
| 46 | } |
William A. Kennington III | 4f07798 | 2022-11-04 02:10:48 -0700 | [diff] [blame^] | 47 | return std::to_chars(ptr, end, v & 0xff, 10).ptr; |
| 48 | } |
| 49 | |
| 50 | std::string_view AddrBufMaker<in_addr>::operator()(in_addr val) noexcept |
| 51 | { |
| 52 | return {buf.data(), makeInAddr(buf.data(), buf.end(), val)}; |
William A. Kennington III | bb0eacc | 2022-10-21 15:22:06 -0700 | [diff] [blame] | 53 | } |
| 54 | |
| 55 | std::string_view AddrBufMaker<in6_addr>::operator()(in6_addr val) noexcept |
| 56 | { |
William A. Kennington III | 4f07798 | 2022-11-04 02:10:48 -0700 | [diff] [blame^] | 57 | // IPv4 in IPv6 Addr |
| 58 | if (val.s6_addr32[0] == 0 && val.s6_addr32[1] == 0 && |
| 59 | val.s6_addr32[2] == hton(uint32_t(0xffff))) |
| 60 | { |
| 61 | constexpr auto prefix = std::string_view("::ffff:"); |
| 62 | return {buf.data(), |
| 63 | makeInAddr(std::copy(prefix.begin(), prefix.end(), buf.begin()), |
| 64 | buf.end(), {val.s6_addr32[3]})}; |
| 65 | } |
| 66 | |
William A. Kennington III | bb0eacc | 2022-10-21 15:22:06 -0700 | [diff] [blame] | 67 | size_t skip_start = 0; |
| 68 | size_t skip_size = 0; |
| 69 | { |
| 70 | size_t new_start = 0; |
| 71 | size_t new_size = 0; |
| 72 | for (size_t i = 0; i < 9; ++i) |
| 73 | { |
| 74 | if (i < 8 && val.s6_addr16[i] == 0) |
| 75 | { |
| 76 | if (new_start + new_size == i) |
| 77 | { |
| 78 | new_size++; |
| 79 | } |
| 80 | else |
| 81 | { |
| 82 | new_start = i; |
| 83 | new_size = 1; |
| 84 | } |
| 85 | } |
| 86 | else if (new_start + new_size == i && new_size > skip_size) |
| 87 | { |
| 88 | skip_start = new_start; |
| 89 | skip_size = new_size; |
| 90 | } |
| 91 | } |
| 92 | } |
| 93 | char* ptr = buf.begin(); |
| 94 | for (size_t i = 0; i < 8; ++i) |
| 95 | { |
William A. Kennington III | 4f07798 | 2022-11-04 02:10:48 -0700 | [diff] [blame^] | 96 | if (i == skip_start && skip_size > 1) |
William A. Kennington III | bb0eacc | 2022-10-21 15:22:06 -0700 | [diff] [blame] | 97 | { |
| 98 | if (i == 0) |
| 99 | { |
| 100 | *(ptr++) = ':'; |
| 101 | } |
| 102 | *(ptr++) = ':'; |
| 103 | i += skip_size - 1; |
| 104 | continue; |
| 105 | } |
| 106 | const auto res = |
William A. Kennington III | 71de63a | 2022-11-08 10:50:54 -0800 | [diff] [blame] | 107 | std::to_chars(ptr, buf.end(), ntoh(val.s6_addr16[i]), 16); |
William A. Kennington III | bb0eacc | 2022-10-21 15:22:06 -0700 | [diff] [blame] | 108 | ptr = res.ptr; |
| 109 | if (i < 7) |
| 110 | { |
| 111 | *(ptr++) = ':'; |
| 112 | } |
| 113 | } |
| 114 | return {buf.data(), ptr}; |
| 115 | } |
| 116 | |
William A. Kennington III | b9d7cba | 2022-11-08 10:54:11 -0800 | [diff] [blame] | 117 | } // namespace detail |
| 118 | } // namespace phosphor::network |
William A. Kennington III | bb0eacc | 2022-10-21 15:22:06 -0700 | [diff] [blame] | 119 | |
William A. Kennington III | 653114f | 2022-11-01 22:34:04 -0700 | [diff] [blame] | 120 | std::size_t std::hash<in_addr>::operator()(in_addr addr) const noexcept |
| 121 | { |
| 122 | return std::hash<decltype(addr.s_addr)>{}(addr.s_addr); |
| 123 | } |
| 124 | |
| 125 | std::size_t std::hash<in6_addr>::operator()(in6_addr addr) const noexcept |
| 126 | { |
| 127 | return phosphor::network::hash_multi(addr.s6_addr32[0], addr.s6_addr32[1], |
| 128 | addr.s6_addr32[2], addr.s6_addr32[3]); |
| 129 | } |
| 130 | |
William A. Kennington III | b9d7cba | 2022-11-08 10:54:11 -0800 | [diff] [blame] | 131 | std::size_t std::hash<phosphor::network::IfAddr>::operator()( |
| 132 | phosphor::network::IfAddr addr) const noexcept |
| 133 | { |
| 134 | return phosphor::network::hash_multi(addr.getAddr(), addr.getPfx()); |
| 135 | } |
| 136 | |
William A. Kennington III | bb0eacc | 2022-10-21 15:22:06 -0700 | [diff] [blame] | 137 | std::string std::to_string(ether_addr value) |
| 138 | { |
| 139 | return string(phosphor::network::detail::AddrBufMaker<ether_addr>{}(value)); |
| 140 | } |
| 141 | std::string std::to_string(in_addr value) |
| 142 | { |
| 143 | return string(phosphor::network::detail::AddrBufMaker<in_addr>{}(value)); |
| 144 | } |
| 145 | std::string std::to_string(in6_addr value) |
| 146 | { |
| 147 | return string(phosphor::network::detail::AddrBufMaker<in6_addr>{}(value)); |
| 148 | } |
| 149 | std::string std::to_string(phosphor::network::InAddrAny value) |
| 150 | { |
| 151 | return std::visit([](auto v) { return std::to_string(v); }, value); |
| 152 | } |
William A. Kennington III | b9d7cba | 2022-11-08 10:54:11 -0800 | [diff] [blame] | 153 | |
| 154 | std::string std::to_string(phosphor::network::IfAddr value) |
| 155 | { |
| 156 | return fmt::to_string(value); |
| 157 | } |