Botan 3.13.0
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 (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

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(), netmask(), operator&(), and operator<=>().

Member Function Documentation

◆ address()

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

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

Definition at line 67 of file ipv4_address.h.

67{ 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 90 of file ipv4_address.cpp.

90 {
91 if(auto ipv4 = string_to_ipv4(str)) {
92 return IPv4Address(*ipv4);
93 } else {
94 return {};
95 }
96}
IPv4Address(uint32_t ip)

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 48 of file ipv4_address.h.

48{ 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 99 of file ipv4_address.cpp.

99 {
100 BOTAN_ARG_CHECK(bits <= 32, "IPv4 netmask prefix length must be at most 32");
101 if(bits == 0) {
102 return IPv4Address(0);
103 }
104 return IPv4Address(0xFFFFFFFF << (32 - bits));
105}
#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(), 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 55 of file ipv4_address.h.

55{ 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 129 of file ipv4_address.cpp.

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

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

107 {
108 std::array<uint8_t, 4> out{};
109 store_be(m_ip, out);
110 return out;
111}
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 113 of file ipv4_address.cpp.

113 {
114 const auto addr = this->to_bytes();
115
116 std::string str;
117 str.reserve(15); // maximum possible size
118
119 for(size_t i = 0; i != 4; ++i) {
120 if(i > 0) {
121 str += ".";
122 }
123 str += std::to_string(addr[i]);
124 }
125
126 return str;
127}
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 64 of file ipv4_address.h.

64{ return m_ip; }

References value().

Referenced by value().


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