7#include <botan/ipv6_address.h>
9#include <botan/ipv4_address.h>
10#include <botan/internal/fmt.h>
11#include <botan/internal/loadstor.h>
12#include <botan/internal/parsing.h>
18 for(
size_t i = 0; i != 16; ++i) {
31 std::array<uint16_t, 8> pre{};
32 std::array<uint16_t, 8> post{};
34 size_t post_count = 0;
35 bool seen_double_colon =
false;
37 const auto hex_value = [](
char c) -> std::optional<uint8_t> {
38 if(c >=
'0' && c <=
'9') {
40 }
else if(c >=
'a' && c <=
'f') {
41 return 10 + (c -
'a');
42 }
else if(c >=
'A' && c <=
'F') {
43 return 10 + (c -
'A');
50 bool expect_group =
true;
52 while(idx < str.size()) {
54 if(idx + 1 < str.size() && str[idx + 1] ==
':') {
55 if(seen_double_colon) {
58 seen_double_colon =
true;
60 expect_group = (idx < str.size());
73 const size_t group_start = idx;
76 while(idx < str.size() && hex_chars < 4) {
77 const auto digit = hex_value(str[idx]);
78 if(digit.has_value() ==
false) {
81 group = (group << 4) | static_cast<uint32_t>(digit.value());
89 if(hex_chars == 4 && idx < str.size() && hex_value(str[idx]).has_value()) {
98 if(idx < str.size() && str[idx] ==
'.') {
99 const auto ipv4 = IPv4Address::from_string(str.substr(group_start));
100 if(!ipv4.has_value()) {
103 const uint32_t v4 = ipv4->address();
104 const std::array<uint16_t, 2> v4_groups{static_cast<uint16_t>(v4 >> 16), static_cast<uint16_t>(v4 & 0xFFFF)};
105 for(
const auto g : v4_groups) {
106 if(seen_double_colon) {
107 if(post_count >= 8) {
110 post[post_count++] = g;
115 pre[pre_count++] = g;
119 expect_group =
false;
123 if(seen_double_colon) {
124 if(post_count >= 8) {
127 post[post_count++] =
static_cast<uint16_t
>(group);
132 pre[pre_count++] =
static_cast<uint16_t
>(group);
134 expect_group =
false;
142 const size_t total_groups = pre_count + post_count;
143 if(seen_double_colon) {
145 if(total_groups > 7) {
149 if(total_groups != 8) {
154 std::array<uint8_t, 16> out{};
155 for(
size_t i = 0; i != pre_count; ++i) {
156 out[2 * i] = get_byte<0>(pre[i]);
157 out[2 * i + 1] = get_byte<1>(pre[i]);
159 const size_t gap = 8 - total_groups;
160 for(
size_t i = 0; i != post_count; ++i) {
161 const size_t target = pre_count + gap + i;
162 out[2 * target] = get_byte<0>(post[i]);
163 out[2 * target + 1] = get_byte<1>(post[i]);
165 return IPv6Address(out);
170 BOTAN_ARG_CHECK(bits <= 128,
"IPv6 netmask prefix length must be at most 128");
172 const size_t full_bytes = bits / 8;
173 const size_t leftover = bits % 8;
175 std::array<uint8_t, 16> m{};
176 for(
size_t i = 0; i != full_bytes; ++i) {
181 m[full_bytes] =
static_cast<uint8_t
>(0xFF << (8 - leftover));
188 static const char* hex =
"0123456789abcdef";
190 std::array<uint16_t, 8> groups{};
191 for(
size_t i = 0; i != 8; ++i) {
192 groups[i] =
make_uint16(m_ip[2 * i], m_ip[2 * i + 1]);
201 size_t best_start = 0;
204 for(
size_t i = 0; i != 8; ++i) {
207 if(run_len > best_len) {
209 best_start = i + 1 - run_len;
219 const auto append_group = [&](uint16_t group) {
220 bool started =
false;
222 for(
int s = 12; s >= 0; s -= 4) {
223 const auto nibble = (group >> s) & 0xF;
224 if(nibble != 0 || started || s == 0) {
225 out.push_back(hex[nibble]);
233 for(
size_t i = 0; i != 8; ++i) {
237 append_group(groups[i]);
240 for(
size_t i = 0; i != best_start; ++i) {
244 append_group(groups[i]);
247 for(
size_t i = best_start + best_len; i != 8; ++i) {
248 if(i > best_start + best_len) {
251 append_group(groups[i]);
258 std::array<uint8_t, 16> masked{};
259 for(
size_t i = 0; i != 16; ++i) {
260 masked[i] = m_ip[i] & other.m_ip[i];
268 for(
size_t i = 0; i != 16; ++i) {
269 const size_t hw = (m_ip[i] == 0xFF) ? 8 : std::countl_one(m_ip[i]);
277 if(*
this !=
netmask(leading)) {
289 if(ip0 == 0x00000000 && ip1 == 0x00000000 && (ip2 == 0x00000000 || ip2 == 0x0000FFFF)) {
304 const auto addr =
IPv6Address(addr_and_mask.first<16>());
305 const auto mask =
IPv6Address(addr_and_mask.last<16>());
307 if(
const auto plen = mask.prefix_length()) {
316 const auto slash = str.find(
'/');
317 if(slash == std::string_view::npos) {
322 if(!addr.has_value()) {
327 const auto plen_str = str.substr(slash + 1);
329 const auto plen =
parse_sz(plen_str,
true);
331 if(!plen.has_value() || plen.value() > 128) {
354 for(
size_t i = 0; i != 16; ++i) {
355 last[i] |=
static_cast<uint8_t
>(~mask[i]);
361 return fmt(
"{}/{}", m_address.to_string(),
static_cast<size_t>(m_prefix_length));
365 const auto addr = m_address.address();
367 return std::vector<uint8_t>(addr.begin(), addr.end());
370 std::vector<uint8_t> out;
372 out.insert(out.end(), addr.begin(), addr.end());
373 out.insert(out.end(), mask.begin(), mask.end());
#define BOTAN_ARG_CHECK(expr, msg)
std::array< uint8_t, 16 > address() const
std::optional< IPv4Address > as_ipv4() const
IPv6Address(std::span< const uint8_t, 16 > ip)
std::array< uint8_t, 16 > to_bytes() const
The address as sixteen bytes, network-byte-order.
std::string to_string() const
static IPv6Address netmask(size_t bits)
static std::optional< IPv6Address > from_string(std::string_view str)
IPv6Address operator&(const IPv6Address &other) const
std::optional< size_t > prefix_length() const
bool contains(const IPv6Address &ip) const
True iff ip falls within this subnet.
size_t prefix_length() const
Prefix length in [0, 128].
std::string to_string() const
CIDR-style "2001:db8::/32".
IPv6Address last_address() const
The last address in this subnet (all host bits set).
static std::optional< IPv6Subnet > from_address_and_mask(std::span< const uint8_t, 32 > addr_and_mask)
const IPv6Address & address() const
The network address (host bits already zeroed).
static std::optional< IPv6Subnet > from_string(std::string_view str)
IPv6Subnet(IPv6Address address, size_t prefix_length)
bool is_host() const
True iff prefix_length() == 128.
std::vector< uint8_t > serialize() const
std::string fmt(std::string_view format, const T &... args)
std::optional< size_t > parse_sz(std::string_view input, bool require_canonical)
constexpr auto load_be(ParamTs &&... params)
constexpr uint16_t make_uint16(uint8_t i0, uint8_t i1)