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);

template <>
struct ip_addr_t >

Public Functions

size_t operator()(eos::ip_addr_t const &) const
namespace std

STL namespace.

template <>
struct ip_addr_t >

Public Functions

size_t operator()(eos::ip_addr_t const &) const

Type definitions in ip

namespace eos

Enums

af_t enum

The address family of an IP address.

Values:

  • AF_NULL -

    Not a valid address family (e.g., the default address).

  • AF_IPV4 = 4 -

    An IPv4 address.

  • AF_IPV6 = 6 -

    An IPv6 address.

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.

Return
true if the address parsed successfully, false otherwise.
Parameters
  • addr -

    Pointer to a C string containing an address to parse.

  • result -

    Mutable arugment to place a successfully parsed result into.

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.

Return
true if the route prefix parsed successfully, false otherwise.
Parameters
  • addr -

    Pointer to a C string containing a prefix to parse.

  • result -

    Mutable arugment to place a successfully parsed result into.

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.

ip_addr_t(in_addr const & addr)

Creates an IP address from a POSIX in_addr (IPv4).

ip_addr_t(in6_addr const & addr)

Creates an IP address from a POSIX in6_addr (IPv6).

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 -

    An IPv4 or IPv6 address as a string.

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 -

    An IPv4 or IPv6 address as a string.

ip_addr_t(uint32_be_t addr_v4)

Takes a network order 32-bit unsigned integer as an IPv4 address.

af_t af() const

Getter for ‘af’: The address family of this IP address.

bool operator==(ip_addr_t const & other) const
bool operator!=(ip_addr_t const & other) const
bool operator<(ip_addr_t const & other) const
uint8_t const * addr() const

The IP address as a big endian array of bytes.

Return
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”.

operator bool() const

Public Members

uint8_t bytes[16]
uint32_t words[4]

Private Members

af_t af_
union eos::ip_addr_t::@1 addr_

Friends

friend std::ostream & operator<<

A utility stream operator that adds a string representation of ip_addr_t to the ostream.

friend bool parse_ip_addr

Parses a string IP address, returning true if the address is valid. Supports both IPv4 and IPv6 addresses.

Return
true if the address parsed successfully, false otherwise.
Parameters
  • addr -

    Pointer to a C string containing an address to parse.

  • result -

    Mutable arugment to place a successfully parsed result into.

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.

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.

af_t af() const

The address family of the prefix.

ip_addr_t network() const

The prefix’s network address.

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

Private Members

ip_addr_t addr_
uint8_t prefix_length_

Friends

friend std::ostream & operator<<

A utility stream operator that adds a string representation of ip_prefix_t to the ostream.

friend bool parse_ip_prefix

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.

Return
true if the route prefix parsed successfully, false otherwise.
Parameters
  • addr -

    Pointer to a C string containing a prefix to parse.

  • result -

    Mutable arugment to place a successfully parsed result into.

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(ip_addr_t const & addr, uint8_t mask_length)
ip_addr_t addr() const

Getter for ‘addr’: the address/mask’s IP address.

uint8_t mask_length() const

Getter for ‘mask_length’: the bit mask length, e.g., 24.

af_t af() const

The address family of the prefix.

uint32_be_t mask() const

The bit mask itself, e.g., 0xFFFFFF00 (IPv4). For IPv6, same as mask_length().

std::string to_string() const

The IP address and mask as a string, e.g., “192.0.2.1/0xFFFFFF00”.

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

Private Members

ip_addr_t addr_
uint8_t mask_length_

Friends

friend std::ostream & operator<<

A utility stream operator that adds a string representation of ip_addr_mask_t to the ostream.

class address_overlap_error
#include <ip.h>

Tried to configure an internal VLAN on a trunk port.

Public Functions

address_overlap_error(ip_addr_mask_t const & addr)
virtual ~address_overlap_error()
ip_addr_mask_t addr() const
virtual void raise() const

Throws this exception.

std::string to_string() const

Returns a string representation of the current object’s values.

Private Members

ip_addr_mask_t addr_

Friends

friend std::ostream & operator<<

A utility stream operator that adds a string representation of address_overlap_error to the ostream.

Table Of Contents

Previous topic

intf

Next topic

ip_intf