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

#include <ipv4_address.h>

Public Member Functions

 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< IPv4Addressfrom_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()

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

Definition at line 25 of file ipv4_address.h.

25: m_ip(ip) {}

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

Member Function Documentation

◆ from_string()

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

Definition at line 17 of file ipv4_address.cpp.

17 {
18 if(auto ipv4 = string_to_ipv4(str)) {
19 return IPv4Address(*ipv4);
20 } else {
21 return {};
22 }
23}
IPv4Address(uint32_t ip)
std::optional< uint32_t > string_to_ipv4(std::string_view str)
Definition parsing.cpp:155

References IPv4Address(), and Botan::string_to_ipv4().

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

◆ host_mask()

IPv4Address Botan::IPv4Address::host_mask ( )
inlinestatic

Definition at line 35 of file ipv4_address.h.

35{ 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 26 of file ipv4_address.cpp.

26 {
27 BOTAN_ARG_CHECK(bits <= 32, "IPv4 netmask prefix length must be at most 32");
28 if(bits == 0) {
29 return IPv4Address(0);
30 }
31 return IPv4Address(0xFFFFFFFF << (32 - bits));
32}
#define BOTAN_ARG_CHECK(expr, msg)
Definition assert.h:33

References BOTAN_ARG_CHECK, and IPv4Address().

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

◆ operator&()

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

Definition at line 37 of file ipv4_address.h.

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

References IPv4Address().

◆ operator<=>()

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

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 44 of file ipv4_address.cpp.

44 {
45 // A 32-bit mask m is a CIDR prefix iff (~m) + 1 is a power of two or zero,
46 // i.e. (~m) & (~m + 1) == 0. If so, the prefix length is the leading-one count.
47 const uint32_t inv = ~m_ip;
48 if((inv & (inv + 1)) != 0) {
49 return std::nullopt;
50 }
51 return std::countl_one(m_ip);
52}

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 34 of file ipv4_address.cpp.

34 {
35 std::array<uint8_t, 4> out{};
36 store_be(m_ip, out);
37 return out;
38}
constexpr auto store_be(ParamTs &&... params)
Definition loadstor.h:745

References Botan::store_be().

◆ to_string()

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

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

Definition at line 40 of file ipv4_address.cpp.

40 {
41 return ipv4_to_string(m_ip);
42}
std::string ipv4_to_string(uint32_t ip)
Definition parsing.cpp:361

References Botan::ipv4_to_string().

◆ value()

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

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

Definition at line 42 of file ipv4_address.h.

42{ return m_ip; }

Referenced by Botan::AlternativeName::add_ipv4_address().


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