Class network

Inheritance Relationships

Base Type

Class Documentation

class network : public liberate::cpp::comparison_operators<network>

Offers operations on networks, including allocation of available addresses within a network.

Public Functions

explicit network(std::string const &netspec)

Constructor. The netspec is expected to be an IP network specification in CIDR notation. The constructor will throw an exception if the netspec cannot be parsed.

Note that due to memory limitations, it is all but impossible to manage all possible IPv6 in a large network. As such, we artificially limit the number of addresses you can manage with this class to 2^64.

Parameters

netspec[in] A network specification in CIDR notation.

Throws
  • std::invalid_argument – If the specification could not be parsed.

  • std::domain_error – If the network mask does not match the address type.

virtual ~network()

Virtual destructor

network(network const &other)

The copy constructor creates a new network with none of the state used to manage reserved addresses. It is functionally equivalent to writing

network n1{spec};
network n2{n1.netspec()};

The same applies to the assignment operator.

network &operator=(network const &other)

Standard assignment operator.

void reset(std::string const &netspec)

Throws away all state in the network class and reinitializes it with the given new netspec.

Parameters

netspec[in] A network specification in CIDR notation.

size_t mask_size() const
Returns

the network mask size in bits.

uint64_t max_size() const
Returns

the maximum amount of allocatable addresses in the network.

address_type family() const

Returns the network family as a socket_address_type. This is one of AT_INET4 or AT_INET6. May return AT_UNSPEC for badly constructed networks.

Returns

address_type of the network.

bool in_network(socket_address const &address) const
Returns

true if the given address is part of the network, false otherwise.

socket_address local_address() const
Returns

the local, network, default gateway and broadcast addresses of this network respectively.

socket_address network_address() const
Returns

the local, network, default gateway and broadcast addresses of this network respectively.

socket_address gateway_address() const
Returns

the local, network, default gateway and broadcast addresses of this network respectively.

socket_address broadcast_address() const
Returns

the local, network, default gateway and broadcast addresses of this network respectively.

std::string netspec() const
Returns

a netspec that can be used to create an equivalent network instance.

socket_address reserve_address()

This functions helps in allocating individual addresses in a network, by keeping internal state on which addresses already have been allocated.

It returns a new socket_address (with port set to 0) that is part of this network, or an empty address if there are no available addresses.

Throws

std::out_of_range – If too many addresses were reserved already.

Returns

the allocated address.

socket_address mapped_address(std::string const &identifier) const

Return a socket address for an identifier. Note that this does not reserve this address, it just performs the mapping. Use reserve_address to actually reserve the address, or is_reserved to query whether it is already reserved.

Note that the mapping does not keep internal state. Instead, the identifier is hashed, and a choice of address is made based on this hash. The hash in use is not cryptographically secure and may yield collisions.

Providing the same identifier string will always yield the same address. Two or more identifier strings may yield the same address. The chances of collisions are much lower when larger networks are used.

Parameters

identifier[in] An identifier for an address.

Returns

A socket address for an identifier.

socket_address mapped_address(void const *identifier, size_t const &length)

Return a socket address for an identifier. Note that this does not reserve this address, it just performs the mapping. Use reserve_address to actually reserve the address, or is_reserved to query whether it is already reserved.

Note that the mapping does not keep internal state. Instead, the identifier is hashed, and a choice of address is made based on this hash. The hash in use is not cryptographically secure and may yield collisions.

Providing the same identifier string will always yield the same address. Two or more identifier strings may yield the same address. The chances of collisions are much lower when larger networks are used.

Parameters
  • identifier[in] An identifier for an address.

  • length[in] The length of the identifier.

Returns

A socket address for an identifier.

socket_address reserve_address(std::string const &identifier)

This function acts like reserve_address and mapped_address combined.

Parameters

identifier[in] An identifier for an address.

Throws

std::out_of_range – If a hash collision occurred, i.e. two identifiers mapped to the same address.

Returns

the allocated address.

socket_address reserve_address(void const *identifier, size_t const &length)

This function acts like reserve_address and mapped_address combined.

Parameters
  • identifier[in] An identifier for an address.

  • length[in] The length of the identifier.

Throws

std::out_of_range – If a hash collision occurred, i.e. two identifiers mapped to the same address.

Returns

the allocated address.

bool reserve_address(socket_address const &addr)

Finally, allow reserving an address directly. We don’t return the address here, just a boolean flag to determine whether it was successful.

Parameters

addr[in] The address to reserve.

Returns

true if reservation was successful, false otherwise.

bool release_address(socket_address const &addr)

Releases an address that is part of this network back into the pool. If the address is not in the network, false is returned. Otherwise, true is returned, and the next call to reserve_address() could return the same address again.

Parameters

addr[in] The address to release.

Returns

true if release was successful, false otherwise.

bool is_reserved(std::string const &identifier) const
Parameters

identifier[in] The identifier to check.

Returns

true if an address is reserved already with the given identifier, false otherwise.

bool is_reserved(void const *identifier, size_t const &length) const
Parameters
  • identifier[in] The identifier to check.

  • length[in] The length of the identifier.

Throws

std::invalid_argument – if the identifier or length are not provided.

Returns

true if an address is reserved already with the given identifier, false otherwise.

bool is_reserved(socket_address const &addr) const
Returns

true if the given address is reserved, false otherwise.

Public Static Functions

static bool verify_netspec(std::string const &netspec)

Verifies the given netspec string would create a valid network.

Parameters

netspec[in] A network specification in CIDR notation.

Returns

true if the given netspec can be parsed and verified, otherwise false

Protected Functions

virtual bool is_equal_to(network const &other) const

Used by cpp::comparison_operators

virtual bool is_less_than(network const &other) const

Used by cpp::comparison_operators

Friends

friend struct liberate::cpp::comparison_operators< network >