Botan 3.14.1
Crypto and TLS for C&
Botan::IPv4Address Class Referencefinal

#include <ipv4_address.h>

Public Member Functions

uint32_t address () const
 The address as a 32-bit big-endian integer.
 IPv4Address (std::array< uint8_t, 4 > ip)
 IPv4Address (std::span< const uint8_t, 4 > ip)
 IPv4Address (uint32_t ip)
IPv4Address operator& (const IPv4Address &other) const
auto operator<=> (const IPv4Address &) const =default
std::optional< size_t > prefix_length () const
std::array< uint8_t, 4 > to_bytes () const
 The address as four bytes, network-byte-order.
std::string to_string () const
 Dotted-decimal form, e.g. "10.0.0.1".
uint32_t value () const
 The address as a 32-bit big-endian integer.

Static Public Member Functions

static std::optional< IPv4Address > from_string (std::string_view str)
static IPv4Address host_mask ()
static IPv4Address netmask (size_t bits)

Detailed Description

IPv4 Address

Definition at line 23 of file ipv4_address.h.

Constructor & Destructor Documentation

◆ IPv4Address() [1/3]

Botan::IPv4Address::IPv4Address ( uint32_t ip)
inlineexplicit

Create an address from its integer value

Parameters
ipthe address as a 32-bit big-endian integer

Definition at line 29 of file ipv4_address.h.

29: m_ip(ip) {}

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

◆ IPv4Address() [2/3]

Botan::IPv4Address::IPv4Address ( std::span< const uint8_t, 4 > ip)
explicit

Create an address from its 4 byte big-endian encoding

Parameters
ipthe bytes of the address

Definition at line 89 of file ipv4_address.cpp.

89: m_ip(load_be<uint32_t>(ip)) {}
constexpr auto load_be(ParamTs &&... params)
Definition loadstor.h:504

References Botan::load_be().

◆ IPv4Address() [3/3]

Botan::IPv4Address::IPv4Address ( std::array< uint8_t, 4 > ip)
inlineexplicit

Create an address from its 4 byte big-endian encoding

Parameters
ipthe bytes of the address

Definition at line 41 of file ipv4_address.h.

41: IPv4Address(std::span<const uint8_t, 4>(ip)) {}
IPv4Address(uint32_t ip)

References IPv4Address().

Member Function Documentation

◆ address()

uint32_t Botan::IPv4Address::address ( ) const
inline

The address as a 32-bit big-endian integer.

Definition at line 79 of file ipv4_address.h.

79{ return m_ip; }

◆ from_string()

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

Convert a dotted-decimal string to an IPv4Address

Parameters
strthe address to parse
Returns
the parsed address, or nullopt if str is not a valid IPv4 address

Definition at line 92 of file ipv4_address.cpp.

92 {
93 if(auto ipv4 = string_to_ipv4(str)) {
94 return IPv4Address(*ipv4);
95 } else {
96 return {};
97 }
98}

References IPv4Address().

Referenced by Botan::AlternativeName::add_attribute(), Botan::AlternativeName::AlternativeName(), Botan::IPv4Subnet::from_string(), Botan::URI::Authority::from_string(), Botan::TLS::Server_Name_Indicator::hostname_acceptable_for_sni(), Botan::NameConstraints::is_excluded(), Botan::NameConstraints::is_permitted(), Botan::GeneralName::matches(), and Botan::X509_Certificate::matches_dns_name().

◆ host_mask()

IPv4Address Botan::IPv4Address::host_mask ( )
inlinestatic

Return the netmask matching a single host

Returns
an address with all 32 bits set

Definition at line 60 of file ipv4_address.h.

60{ return netmask(32); }
static IPv4Address netmask(size_t bits)

References IPv4Address(), and netmask().

◆ netmask()

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

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

Definition at line 101 of file ipv4_address.cpp.

101 {
102 BOTAN_ARG_CHECK(bits <= 32, "IPv4 netmask prefix length must be at most 32");
103 if(bits == 0) {
104 return IPv4Address(0);
105 }
106 return IPv4Address(0xFFFFFFFF << (32 - bits));
107}
#define BOTAN_ARG_CHECK(expr, msg)
Definition assert.h:33

References BOTAN_ARG_CHECK, and IPv4Address().

Referenced by Botan::IPv4Subnet::contains(), Botan::GeneralName::encode_into(), host_mask(), Botan::IPv4Subnet::last_address(), and Botan::IPv4Subnet::serialize().

◆ operator&()

IPv4Address Botan::IPv4Address::operator& ( const IPv4Address & other) const
inline

Bitwise AND of two addresses, typically used to apply a netmask

Parameters
otherthe address to AND with
Returns
the bitwise AND of the two addresses

Definition at line 67 of file ipv4_address.h.

67{ return IPv4Address(m_ip & other.m_ip); }

References IPv4Address().

◆ operator<=>()

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

Order two addresses numerically

Returns
the ordering of this address relative to the other

References IPv4Address().

◆ prefix_length()

std::optional< size_t > Botan::IPv4Address::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 131 of file ipv4_address.cpp.

131 {
132 // A 32-bit mask m is a CIDR prefix iff (~m) + 1 is a power of two or zero,
133 // i.e. (~m) & (~m + 1) == 0. If so, the prefix length is the leading-one count.
134 const uint32_t inv = ~m_ip;
135 if((inv & (inv + 1)) != 0) {
136 return std::nullopt;
137 }
138 return std::countl_one(m_ip);
139}

Referenced by Botan::IPv4Subnet::from_address_and_mask().

◆ to_bytes()

std::array< uint8_t, 4 > Botan::IPv4Address::to_bytes ( ) const

The address as four bytes, network-byte-order.

Definition at line 109 of file ipv4_address.cpp.

109 {
110 std::array<uint8_t, 4> out{};
111 store_be(m_ip, out);
112 return out;
113}
constexpr auto store_be(ParamTs &&... params)
Definition loadstor.h:745

References Botan::store_be().

Referenced by to_string().

◆ to_string()

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

Dotted-decimal form, e.g. "10.0.0.1".

Definition at line 115 of file ipv4_address.cpp.

115 {
116 const auto addr = this->to_bytes();
117
118 std::string str;
119 str.reserve(15); // maximum possible size
120
121 for(size_t i = 0; i != 4; ++i) {
122 if(i > 0) {
123 str += '.';
124 }
125 str += std::to_string(addr[i]);
126 }
127
128 return str;
129}
std::array< uint8_t, 4 > to_bytes() const
The address as four bytes, network-byte-order.

References to_bytes().

◆ value()

uint32_t Botan::IPv4Address::value ( ) const
inline

The address as a 32-bit big-endian integer.

Definition at line 76 of file ipv4_address.h.

76{ return m_ip; }

References value().

Referenced by value().


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