ip¶
IP address and prefix types.
This module provides definitions of a variety of IP address types used throughout the SDK, including an IPv4 or IPv6 address, an IP prefix (a network address with a prefix length), and an IP address and mask. IPv4 and IPv6 are supported by each of these types, so there are no separate classes for each address family. For example, the eos::ip_addr_t IP address type has an af() accessor to inspect the address family.
Utility functions are provided to parse or validate string format IP addresses and CIDR-style IP prefixes.
Some examples of the manipulations available are as follows, see the function descriptions for more information.
// Constructs an IPv4 address from a string. Must be a valid IP.
eos::ip_addr_t v4("1.2.3.4");
// Constructs an IPv6 address from a string.
eos::ip_addr_t v6("dead:beef::1");
// Parses and validates an IP address
eos::ip_addr_t address; // the IPv4/IPv6 address type
eos::ip_prefix_t prefix; // the IPv4/IPv6 prefix type
bool valid;
// valid == true, and address is changed to contain the
// representation of 1.1.1.1 with its af() == eos::AF_IPV4
valid = eos::parse_ip_addr("1.1.1.1", &address);
// valid == false, address is unchanged
valid = eos::parse_ip_addr("not_an_ip", &address);
// Parses and validates an IP prefix
// The IP address must be the prefix network address, else the
// prefix is invalid. Like parse_ip_address, prefix is updated to
// contain the representation of the parsed prefix if and only if
// valid == true.
valid = eos::parse_ip_prefix("192.0.2.0/24", &prefix); // valid == true
valid = eos::parse_ip_prefix("192.0.2.1/24", &prefix); // valid == false
valid = eos::parse_ip_prefix("192.0.2.1/32", &prefix); // valid == true
valid = eos::parse_ip_prefix("::1/128", &prefix); // valid == true
// Construct an IP address and mask for 192.0.2.200/24 (e.g., used to
// plumb a network interface in ip_intf.h)
eos::ip_addr_t intf_addr("192.0.2.200");
eos::ip_addr_mask_t intf_addr_mask(intf_addr, 24);
-
namespace
eos
-
namespace
std
¶ STL namespace.
-
template<> ip_addr_t >
-
Type definitions in ip¶
-
namespace
eos
Enums
Functions
-
bool
parse_ip_addr
(char const *addr, ip_addr_t *result)¶ Parses a string IP address, returning true if the address is valid. Supports both IPv4 and IPv6 addresses.
- Parameters
addr – [in] Pointer to a C string containing an address to parse.
result – [out] Mutable arugment to place a successfully parsed result into.
- Returns
true if the address parsed successfully, false otherwise.
-
bool
parse_ip_prefix
(char const *addr, ip_prefix_t *result)¶ Parses an IP route prefix, returns true if the prefix is valid. Supports both IPv4 and IPv6 route prefixes.
For example, “10.1.2.0/24” or “fffe:abab:1234::/96” are legal, but “10.1.2.7/24” is not, as 10.1.2.7 is not a valid network address for a 24-bit prefix length.
- Parameters
addr – [in] Pointer to a C string containing a prefix to parse.
result – [out] Mutable arugment to place a successfully parsed result into.
- Returns
true if the route prefix parsed successfully, false otherwise.
-
class
ip_addr_t
¶ - #include <ip.h>
An IP address. Both IPv4 and IPv6 addresses are supported by this class.
Public Functions
-
ip_addr_t
()¶
-
ip_addr_t
(af_t af, uint8_t const *addr)¶ Creates an address from an address family and a network order array of bytes. Pass exactly 4 bytes for AF_IPV4 address type or 16 bytes for AF_IPV6.
-
explicit
ip_addr_t
(in_addr const &addr)¶ Creates an IP address from a POSIX in_addr (IPv4).
-
explicit
ip_addr_t
(in6_addr const &addr)¶ Creates an IP address from a POSIX in6_addr (IPv6).
-
explicit
ip_addr_t
(std::string const &address_string)¶ Creates an IP address from the provided string. panic() is called if an invalid (non IPv4 or IPv6) address is supplied.
- Parameters
addr – [in] An IPv4 or IPv6 address as a string.
-
explicit
ip_addr_t
(char const *address_string)¶ Creates an IP address from the provided string. panic() is called if an invalid (non IPv4 or IPv6) address is supplied.
- Parameters
addr – [in] An IPv4 or IPv6 address as a string.
-
explicit
ip_addr_t
(uint32_be_t addr_v4)¶ Takes a network order 32-bit unsigned integer as an IPv4 address.
-
uint8_t const *
addr
() const¶ The IP address as a big endian array of bytes.
- Returns
A byte array, 4 bytes long if af() == AF_IPV4 and 16 for AF_IPV6.
-
uint32_be_t
addr_v4
() const¶ The IP address as a big endian 32-bit integer.
Only valid for IPv4 addresses; panic() is called if used on an AF_IPV6 family address.
-
std::string
to_string
() const¶ String representation of the IP address, e.g. “1.2.3.4” or “f00d::1”.
-
explicit
operator bool
() const¶
Friends
-
friend std::ostream &
operator<<
(std::ostream &os, const ip_addr_t &obj)¶ A utility stream operator that adds a string representation of ip_addr_t to the ostream.
-
friend bool
parse_ip_addr
(char const*, ip_addr_t *result)¶ Parses a string IP address, returning true if the address is valid. Supports both IPv4 and IPv6 addresses.
- Parameters
addr – [in] Pointer to a C string containing an address to parse.
result – [out] Mutable arugment to place a successfully parsed result into.
- Returns
true if the address parsed successfully, false otherwise.
-
-
class
ip_prefix_t
¶ - #include <ip.h>
An IPv4 or IPv6 route prefix.
A route prefix combines a network address and a prefix length. A network address has no 1 bits in the host component of the address (i.e., only the first prefix_length bits of the address can be non-zero).
Public Functions
-
ip_prefix_t
()¶
-
ip_prefix_t
(ip_addr_t const &addr, uint8_t prefix_length)¶ Constructs a prefix from an IP network address and prefix length in bits.
panic() is called if the address provided is not a valid network address given the provided prefix length.
-
explicit
ip_prefix_t
(char const *prefix_string)¶ Constructs a prefix with the provided IP prefix string. Supports IPv4 and IPv6 address prefixes. This constructor will call panic() if the string passed is not a valid network prefix (e.g., “10.1.2.7/24” is invalid, while “10.1.2.0/24” is OK).
-
uint8_t
prefix_length
() const¶ Getter for ‘prefix_length’: the prefix length in bits.
-
ip_addr_t
mask
() const¶ The prefix as mask. A prefix length of 8 on a v4 address will yield the mask 255.0.0.0.
-
std::string
to_string
() const¶ String representation of the IP prefix, e.g. “10.2.3.4/24” or “cafe::1/218”.
-
bool
operator==
(ip_prefix_t const &other) const¶
-
bool
operator!=
(ip_prefix_t const &other) const¶
-
bool
operator<
(ip_prefix_t const &other) const¶
-
uint32_t
hash
() const¶ The hash function for type ip_prefix_t.
-
void
mix_me
(hash_mix &h) const¶ The hash mix function for type ip_prefix_t.
Friends
-
friend std::ostream &
operator<<
(std::ostream &os, const ip_prefix_t &obj)¶ A utility stream operator that adds a string representation of ip_prefix_t to the ostream.
-
friend bool
parse_ip_prefix
(char const*, ip_prefix_t *result)¶ Parses an IP route prefix, returns true if the prefix is valid. Supports both IPv4 and IPv6 route prefixes.
For example, “10.1.2.0/24” or “fffe:abab:1234::/96” are legal, but “10.1.2.7/24” is not, as 10.1.2.7 is not a valid network address for a 24-bit prefix length.
- Parameters
addr – [in] Pointer to a C string containing a prefix to parse.
result – [out] Mutable arugment to place a successfully parsed result into.
- Returns
true if the route prefix parsed successfully, false otherwise.
-
-
class
ip_addr_mask_t
¶ - #include <ip.h>
An IP address with a subnet mask.
This allows for contiguous subnet masks only for IPv4 and IPv6 addresses.
IPv6 addresses in EOS only support contiguous masks, so for IPv6 addresses mask() provides the same value as mask_length().
Public Functions
-
ip_addr_mask_t
()¶
-
ip_addr_mask_t
(const ip_addr_mask_t &other)¶
-
ip_addr_mask_t &
operator=
(ip_addr_mask_t const &other)¶
-
ip_addr_mask_t
(ip_addr_mask_t &&other) noexcept¶
-
ip_addr_mask_t &
operator=
(ip_addr_mask_t &&other) noexcept¶
-
uint8_t
mask_length
() const¶ Getter for ‘mask_length’: the bit mask length, e.g., 24.
-
uint32_be_t
mask
() const¶ The bit mask itself, e.g., 0xFFFFFF00 (IPv4). For IPv6, same as mask_length().
-
bool
operator==
(ip_addr_mask_t const &other) const¶
-
bool
operator!=
(ip_addr_mask_t const &other) const¶
-
bool
operator<
(ip_addr_mask_t const &other) const¶
-
uint32_t
hash
() const¶ The hash function for type ip_addr_mask_t.
-
void
mix_me
(hash_mix &h) const¶ The hash mix function for type ip_addr_mask_t.
Public Static Functions
-
static void
operator delete
(void*) noexcept¶
Friends
-
friend std::ostream &
operator<<
(std::ostream &os, const ip_addr_mask_t &obj)¶ A utility stream operator that adds a string representation of ip_addr_mask_t to the ostream.
-
-
class
address_overlap_error
: public eos::configuration_error¶ - #include <ip.h>
Tried to configure an internal VLAN on a trunk port.
Public Functions
-
explicit
address_overlap_error
(ip_addr_mask_t const &addr) noexcept¶
-
virtual
~address_overlap_error
() noexcept¶
-
ip_addr_mask_t
addr
() const noexcept¶
-
virtual void
raise
() const¶ Throws this exception.
-
uint32_t
hash
() const¶ The hash function for type address_overlap_error.
-
void
mix_me
(hash_mix &h) const¶ The hash mix function for type address_overlap_error.
Private Members
-
ip_addr_mask_t
addr_
¶
Friends
-
friend std::ostream &
operator<<
(std::ostream &os, const address_overlap_error &obj)¶ A utility stream operator that adds a string representation of address_overlap_error to the ostream.
-
explicit
-
bool