Class socket_address
Defined in File socket_address.h
Inheritance Relationships
Base Type
public liberate::cpp::comparison_operators< socket_address >
(Template Struct comparison_operators)
Class Documentation
-
class socket_address : public liberate::cpp::comparison_operators<socket_address>
Make it possible to use
struct sockaddr
as a map key.Public Functions
-
socket_address()
Default constructor. The resulting socket address does not point anywhere.
-
inline socket_address(socket_address &&other) = default
Move semantics should remain intact; copies and assignment on the other hand need to create a new, distinct address.
-
socket_address(socket_address const &other)
Copy constructor
-
socket_address &operator=(socket_address const &other)
Assignment operator
-
virtual ~socket_address() = default
Virtual destructor
-
socket_address(void const *buf, size_t len)
Constructor. The ‘buf’ parameter is expected to be a
struct sockaddr
of the given length.- Parameters
buf – [in] A POSIX
struct sockaddr
.len – [in] The size of the
buf
parameter.
-
socket_address(address_type type, void const *buf, size_t len, uint16_t port = 0)
Construct from address type and buffer. This constructor selects the type of
struct sockaddr
used internally.- Parameters
type – [in] The type of address to construct.
buf – [in] A POSIX
struct sockaddr
.len – [in] The size of the
buf
parameter.port – [in] optional; defaults to
0
.
-
explicit socket_address(std::string const &address, uint16_t port = 0)
Alternative constructor. The string is expected to be a network address in CIDR notation (without the netmask).
Results in an address of type AT_UNSPEC if parsing fails.
- Parameters
address – [in] The CIDR address to parse and assign to this socket_address.
port – [in] optional; defaults to
0
.
-
explicit socket_address(char const *address, uint16_t port = 0, size_t size = 0)
Alternative constructor. The string is expected to be a network address in CIDR notation (without the netmask).
Results in an address of type AT_UNSPEC if parsing fails.
- Parameters
address – [in] The CIDR address to parse and assign to this socket_address.
port – [in] optional; defaults to
0
.size – [in] The length of the
address
.
-
bool verify_netmask(size_t const &netmask) const
Verifies that the given netmask would work for the given socket address. This really tests whether the socket address is an IPv4 or IPv6 address, and whether the netmask fits that.
- Parameters
netmask – [in] The network mask to test against the address.
- Returns
true if the netmask fits the address type, false otherwise.
-
size_t max_netmask() const
- Returns
the maximum possible network mask for this address type.
-
std::string cidr_str() const
Return a CIDR-style string representation of this address (minus port). Only applicable to IP addresses.
- Returns
a CIDR-style address or an empty string.
-
uint16_t port() const
Only applicable to IP addresses.
- Returns
the port part of this address.
-
address_type type() const
- Returns
the socket’s address_type
-
std::string full_str() const
- Returns
a full string representation of the address, including port.
-
size_t bufsize() const
- Returns
the type specific, used size of the raw address buffer.
-
void const *buffer() const
- Returns
the raw address buffer, i.e. a pointer to a
struct sockaddr
.
-
void *buffer()
- Returns
the raw address buffer, i.e. a pointer to a
struct sockaddr
.
-
size_t min_bufsize(bool with_type = true, bool with_port = true) const
Returns the minimum buffer size required to serialize the address with or without type and port. For IPv4 this is 4 Bytes, for example, without a port or type. Note: This is only defined for IPv4 and IPv6 addresses.
- Parameters
with_type – [in] If provided, adds an extra byte to encode the address type.
with_port – [in] If provided, adds two bytes for a port.
- Returns
The calculated buffer size.
-
size_t serialize(void *buf, size_t len, bool with_type = true, bool with_port = true) const
Serialize addresses to mininum buffer. Returns the number of bytes serialized.
This function is useful for writing to IP headers.
See also deserialize.
Note: This is only defined for IPv4 and IPv6 addresses.
- Parameters
buf – [out] The output buffer to write to.
len – [in] The length of the output buffer.
with_type – [in] Whether the address_type should also be encoded.
with_port – [in] Whether the port should also be encoded.
- Returns
the amount of
buf
consumed, or0
on errors.
-
bool set_port(uint16_t port)
Sets/overwrites the port used for this socket address. Returns Returns ERR_INVALID_OPTION if used on the wrong (AT_LOCAL) socket address type.
-
bool is_any() const
- Returns
true if this is an IP “ANY” address (e.g. INADDR_ANY), false if not - or if the address type does not contain such a definition.
-
bool is_loopback() const
- Returns
true if this is an IP loopback address, false if not - or if the address type does not contain such a definition.
-
void swap(socket_address &other)
Swap values of two socket addresses.
- Parameters
other – [inout] The address to swap values with.
-
size_t hash() const
- Returns
a hash for this address, to be used in unordered maps.
-
void operator++()
Increment. Returns the address + 1, e.g. 192.168.0.2 if the address is 192.168.0.1.
This function does not care about overflows.
-
bool is_equal_to(socket_address const &other) const
Used by cpp::comparison_operators
-
bool is_less_than(socket_address const &other) const
Used by cpp::comparison_operators
Public Static Functions
-
static bool verify_cidr(std::string const &address)
Verifies the given address string would create a valid IP socket address.
- Parameters
address – [in] Address string in CIDR notation.
- Returns
true if the
address
could be parsed, false otherwise.
-
static inline constexpr size_t bufsize_available()
- Returns
the available size of the raw address buffer.
-
static std::tuple<size_t, socket_address> deserialize(void const *buf, size_t len, bool with_port = true)
Reconstruct address from minimal buffer size. If a type is provided in the call, it is assumed that the buffer does not contain a type. Otherwise, the type is expected in the first Byte. Be aware of this when deserializing addresses.
This function is useful for reading from IP headers.
The first result is the number of Bytes consumed, the second the result. If the number of Bytes is zero, this indicates an error in parsing.
See also serialize.
Note: This is only defined for IPv4 and IPv6 addresses.
- Parameters
buf – [in] The input buffer to parse.
len – [in] The length of the input buffer.
with_port – [in] Set if the function should also parse a port.
- Returns
Tuple containing:
The amount of input buffer consumed (
0
on errors)The resulting socket_address.
-
static std::tuple<size_t, socket_address> deserialize(address_type type, void const *buf, size_t len, bool with_port = true)
Reconstruct address from minimal buffer size. If a type is provided in the call, it is assumed that the buffer does not contain a type. Otherwise, the type is expected in the first Byte. Be aware of this when deserializing addresses.
This function is useful for reading from IP headers.
The first result is the number of Bytes consumed, the second the result. If the number of Bytes is zero, this indicates an error in parsing.
See also serialize.
Note: This is only defined for IPv4 and IPv6 addresses.
- Parameters
buf – [in] The input buffer to parse.
len – [in] The length of the input buffer.
with_port – [in] Set if the function should also parse a port.
type – [in] Specify the address type to look for; this is only defined for AT_INET4 and AT_INET6 addresses. Anything else or parse errors results in an empty result.
- Returns
Tuple containing:
The amount of input buffer consumed (
0
on errors)The resulting socket_address.
-
socket_address()