Botan 3.12.0
Crypto and TLS for C&
Botan::IPv6Address Class Referencefinal

#include <ipv6_address.h>

Public Member Functions

std::span< const uint8_t, 16 > address () const
std::optional< IPv4Addressas_ipv4 () const
 IPv6Address (std::array< uint8_t, 16 > ip)
 IPv6Address (std::span< const uint8_t, 16 > ip)
IPv6Address operator& (const IPv6Address &other) const
auto operator<=> (const IPv6Address &) const =default
std::optional< size_t > prefix_length () const
std::string to_string () const

Static Public Member Functions

static std::optional< IPv6Addressfrom_string (std::string_view str)
static IPv6Address host_mask ()
static IPv6Address netmask (size_t bits)

Detailed Description

IPv6 Address

Definition at line 25 of file ipv6_address.h.

Constructor & Destructor Documentation

◆ IPv6Address() [1/2]

Botan::IPv6Address::IPv6Address ( std::span< const uint8_t, 16 > ip)
explicit

Definition at line 17 of file ipv6_address.cpp.

17 : m_ip{} {
18 for(size_t i = 0; i != 16; ++i) {
19 m_ip[i] = ip[i];
20 }
21}

Referenced by from_string(), host_mask(), netmask(), operator&(), and operator<=>().

◆ IPv6Address() [2/2]

Botan::IPv6Address::IPv6Address ( std::array< uint8_t, 16 > ip)
inlineexplicit

Definition at line 29 of file ipv6_address.h.

29: m_ip(ip) {}

Member Function Documentation

◆ address()

std::span< const uint8_t, 16 > Botan::IPv6Address::address ( ) const
inline

Definition at line 45 of file ipv6_address.h.

45{ return m_ip; }

Referenced by Botan::IPv6Subnet::serialize().

◆ as_ipv4()

std::optional< IPv4Address > Botan::IPv6Address::as_ipv4 ( ) const

If this IPv6 address is an IPv4-compatible IPv6 address (RFC 4291 2.5.5.1) or an IPv4-mapped IPv6 address (RFC 4291 2.5.5.2), return the embedded IPv4 address.

Definition at line 81 of file ipv6_address.cpp.

81 {
82 const uint32_t ip0 = load_be<uint32_t>(m_ip.data(), 0);
83 const uint32_t ip1 = load_be<uint32_t>(m_ip.data(), 1);
84 const uint32_t ip2 = load_be<uint32_t>(m_ip.data(), 2);
85 const uint32_t ip3 = load_be<uint32_t>(m_ip.data(), 3);
86
87 if(ip0 == 0x00000000 && ip1 == 0x00000000 && (ip2 == 0x00000000 || ip2 == 0x0000FFFF)) {
88 return IPv4Address(ip3);
89 } else {
90 return {};
91 }
92}
constexpr auto load_be(ParamTs &&... params)
Definition loadstor.h:504

References Botan::load_be().

◆ from_string()

std::optional< IPv6Address > Botan::IPv6Address::from_string ( std::string_view str)
static

Definition at line 24 of file ipv6_address.cpp.

24 {
25 if(auto ipv6 = string_to_ipv6(str)) {
26 return IPv6Address(*ipv6);
27 } else {
28 return {};
29 }
30}
IPv6Address(std::span< const uint8_t, 16 > ip)
std::optional< std::array< uint8_t, 16 > > string_to_ipv6(std::string_view str)
Definition parsing.cpp:221

References IPv6Address(), and Botan::string_to_ipv6().

Referenced by Botan::IPv6Subnet::from_string(), and Botan::X509_Certificate::matches_dns_name().

◆ host_mask()

IPv6Address Botan::IPv6Address::host_mask ( )
inlinestatic

Definition at line 39 of file ipv6_address.h.

39{ return netmask(128); }
static IPv6Address netmask(size_t bits)

References IPv6Address(), and netmask().

◆ netmask()

IPv6Address Botan::IPv6Address::netmask ( size_t bits)
static

Return an address with the leading bits set to one and the remainder zero. Throws Invalid_Argument if bits > 128.

Definition at line 33 of file ipv6_address.cpp.

33 {
34 BOTAN_ARG_CHECK(bits <= 128, "IPv6 netmask prefix length must be at most 128");
35
36 const size_t full_bytes = bits / 8;
37 const size_t leftover = bits % 8;
38
39 std::array<uint8_t, 16> m{};
40 for(size_t i = 0; i != full_bytes; ++i) {
41 m[i] = 0xFF;
42 }
43
44 if(leftover > 0) {
45 m[full_bytes] = static_cast<uint8_t>(0xFF << (8 - leftover));
46 }
47
48 return IPv6Address(m);
49}
#define BOTAN_ARG_CHECK(expr, msg)
Definition assert.h:33

References BOTAN_ARG_CHECK, and IPv6Address().

Referenced by Botan::IPv6Subnet::contains(), host_mask(), prefix_length(), and Botan::IPv6Subnet::serialize().

◆ operator&()

IPv6Address Botan::IPv6Address::operator& ( const IPv6Address & other) const

Definition at line 55 of file ipv6_address.cpp.

55 {
56 std::array<uint8_t, 16> masked{};
57 for(size_t i = 0; i != 16; ++i) {
58 masked[i] = m_ip[i] & other.m_ip[i];
59 }
60 return IPv6Address(masked);
61}

References IPv6Address().

◆ operator<=>()

auto Botan::IPv6Address::operator<=> ( const IPv6Address & ) const
default

References IPv6Address().

◆ prefix_length()

std::optional< size_t > Botan::IPv6Address::prefix_length ( ) const

If this value is a netmask consisting of a run of one bits followed by a run of zero bits, return the number of one bits.

Otherwise return nullopt.

Definition at line 63 of file ipv6_address.cpp.

63 {
64 // Count leading one bits, stopping at the first byte that isn't fully set.
65 size_t leading = 0;
66 for(size_t i = 0; i != 16; ++i) {
67 const size_t hw = (m_ip[i] == 0xFF) ? 8 : std::countl_one(m_ip[i]);
68 leading += hw;
69 if(hw != 8) {
70 break;
71 }
72 }
73
74 // Verify this is exactly equal to a netmask of that size
75 if(*this != netmask(leading)) {
76 return std::nullopt;
77 }
78 return leading;
79}

References netmask().

◆ to_string()

std::string Botan::IPv6Address::to_string ( ) const

Definition at line 51 of file ipv6_address.cpp.

51 {
52 return ipv6_to_string(m_ip);
53}
std::string ipv6_to_string(std::span< const uint8_t, 16 > a)
Definition parsing.cpp:334

References Botan::ipv6_to_string().


The documentation for this class was generated from the following files: