7#include <botan/ipv4_address.h>
9#include <botan/internal/fmt.h>
10#include <botan/internal/loadstor.h>
11#include <botan/internal/parsing.h>
27 BOTAN_ARG_CHECK(bits <= 32,
"IPv4 netmask prefix length must be at most 32");
35 std::array<uint8_t, 4> out{};
47 const uint32_t inv = ~m_ip;
48 if((inv & (inv + 1)) != 0) {
51 return std::countl_one(m_ip);
62 std::array<uint8_t, 8> addr_and_mask{};
82 const auto slash = str.find(
'/');
83 if(slash == std::string_view::npos) {
88 if(!addr.has_value()) {
92 const auto plen_str = str.substr(slash + 1);
93 if(plen_str.empty() || plen_str.size() > 2) {
97 for(
const char c : plen_str) {
98 if(c <
'0' || c >
'9') {
101 plen = plen * 10 +
static_cast<size_t>(c -
'0');
115 return fmt(
"{}/{}", m_address.to_string(),
static_cast<size_t>(m_prefix_length));
119 std::vector<uint8_t> out;
122 store_be(m_address.value(), out.data());
126 store_be(m_address.value(), out.data());
#define BOTAN_ARG_CHECK(expr, msg)
static std::optional< IPv4Address > from_string(std::string_view str)
std::optional< size_t > prefix_length() const
std::string to_string() const
Dotted-decimal form, e.g. "10.0.0.1".
std::array< uint8_t, 4 > to_bytes() const
The address as four bytes, network-byte-order.
static IPv4Address netmask(size_t bits)
static std::optional< IPv4Subnet > from_address_and_mask(std::span< const uint8_t, 8 > addr_and_mask)
std::vector< uint8_t > serialize() const
bool contains(const IPv4Address &ip) const
True iff ip falls within this subnet.
bool is_host() const
True iff prefix_length() == 32.
IPv4Subnet(IPv4Address address, size_t prefix_length)
size_t prefix_length() const
Prefix length in [0, 32].
static std::optional< IPv4Subnet > from_string(std::string_view str)
std::string to_string() const
CIDR-style "10.0.0.0/8".
const IPv4Address & address() const
The network address (host bits already zeroed).
std::string fmt(std::string_view format, const T &... args)
std::optional< uint32_t > string_to_ipv4(std::string_view str)
std::string ipv4_to_string(uint32_t ip)
constexpr auto store_be(ParamTs &&... params)
constexpr auto load_be(ParamTs &&... params)