dodo
0.0.1
A C++ library to create containerized Linux services
|
Generic network Address, supporting ipv4 and ipv6 transparently. More...
#include <address.hpp>
Public Member Functions | |
Address () | |
Construct an invalid (unspecified) address. More... | |
Address (const Address &address) | |
Copy from Address. More... | |
Address (const sockaddr *address, socklen_t len) | |
Construct from sockaddr *. More... | |
Address (const sockaddr_storage &address) | |
Construct from sockaddr_storage. More... | |
Address (const std::string &ip) | |
Construct from ip, do not set port (only a listening socket needs a port preset). More... | |
Address (const std::string &ip, uint16_t port) | |
Construct from "ip", port. More... | |
std::string | asString (bool withport=false) const |
Return a string representation of this Address. More... | |
const sockaddr_storage * | getAddress () const |
Get to the underlying sockaddr_storage. More... | |
SocketParams::AddressFamily | getAddressFamily () const |
Get this Address family. More... | |
common::SystemError | getNameInfo (std::string &hostname) const |
Do a reverse DNS lookup on this Address and return in hostname. More... | |
common::SystemError | getNameInfo (std::string &hostname, std::string &service) const |
Do a reverse DNS lookup on this Address and its port, and return in hostname and service. More... | |
common::SystemError | getNameInfo (std::string &hostname, uint16_t &port) const |
Do a reverse DNS lookup on this Address and its port, and return in hostname and port. More... | |
uint16_t | getPort () const |
Return the port number. More... | |
bool | isValid () const |
True if this Address is valid. More... | |
Address & | operator= (const Address &address) |
Assign (copy) from Address. More... | |
Address & | operator= (const sockaddr_storage &address) |
Assign from sockaddr_storage. More... | |
Address & | operator= (const std::string &address) |
Assign from "ip" string. More... | |
bool | operator== (const Address &address) const |
Test for equality. More... | |
void | setPort (uint16_t port) |
Set the port number. More... | |
![]() | |
DebugObject () | |
Default constructor does nothing. More... | |
virtual | ~DebugObject () |
Destructor does nothing. More... | |
std::string | debugString () const |
Return the object dump to string. More... | |
Static Public Member Functions | |
static common::SystemError | getHostAddrInfo (const std::string &hostname, AddrInfo &info) |
Get Address information on a host in a AddrInfo struct. More... | |
static common::SystemError | getHostAddrInfo (const std::string &hostname, SocketParams ¶ms, Address &address, std::string &canonicalname) |
Return the first hit on SocketParams in address and canonicalname. More... | |
Private Member Functions | |
struct sockaddr_in * | asIPv4Address () const |
Explicit cast of addr_ as sockaddr_in*. More... | |
struct sockaddr_in6 * | asIPv6Address () const |
Explicit cast of addr_ as sockaddr_in6*. More... | |
void | init () |
Initialize the internals. More... | |
Private Attributes | |
struct sockaddr_storage | addr_ |
The address storage (for either ipv4 or ipv6). More... | |
Friends | |
class | BaseSocket |
BaseSocket may access this class directly. More... | |
class | Socket |
Socket may access this class directly. More... | |
Additional Inherited Members | |
![]() | |
virtual std::string | debugDetail () const |
Descendant classes can override to dump details specific to the class. More... | |
std::string | debugHeader () const |
Generates a debug header (address of this object and a demangled class name. More... | |
Generic network Address, supporting ipv4 and ipv6 transparently.
Typical client use is to first resolve a host name. We specify SocketParams::afUNSPEC for the address family, allowing for both ipv4 and ipv6 and specify the TCP protocol (stSTRAM + pnTCP ).
The canonical name is set and may differ from the host name passed to getHostAddrInfo.
After the getHostAddrInfo (if error == SystemError::ecOK ) the sock_params will have its address family set to either network::SocketParams::afINET or network::SocketParams::afINET6, depending on what the server (httpbin.org) supports. But we can continue to connect without any reference to the actual address family being used:
There are two prototypes for getHostAddrInfo, one returns the preferred Address (as above), the other a list of addresses (an AddrInfo structure), the preferred address the first in the list. Subsequent calls do not necessarily (under DNS round robin for example) return the same address as preferred address.
As the getHostAddrInfo calls are quite expensive, prefer reuse of obtained Address objects where possible.
Special addresses | ipv4 | ipv6 |
---|---|---|
INADDR_ANY | 0.0.0.0 | ::0 |
localhost | 127.0.0.1 | ::1 |
Definition at line 90 of file address.hpp.
|
inline |
Construct an invalid (unspecified) address.
Definition at line 96 of file address.hpp.
References addr_.
Referenced by getHostAddrInfo().
dodo::network::Address::Address | ( | const std::string & | ip, |
uint16_t | port | ||
) |
Construct from "ip", port.
ip | The string representation of the ip, either ipv4 or ipv6. |
port | The port number (only specified for listen addresses). |
Definition at line 40 of file address.cpp.
References init(), and setPort().
dodo::network::Address::Address | ( | const std::string & | ip | ) |
Construct from ip, do not set port (only a listening socket needs a port preset).
ip | The string representation of the ip, either ipv4 or ipv6. |
Definition at line 35 of file address.cpp.
References init().
dodo::network::Address::Address | ( | const sockaddr_storage & | address | ) |
Construct from sockaddr_storage.
address | A sockaddr_storage structure. |
Definition at line 46 of file address.cpp.
References addr_.
dodo::network::Address::Address | ( | const sockaddr * | address, |
socklen_t | len | ||
) |
Construct from sockaddr *.
address | A const sockaddr* |
len | And its length. |
Definition at line 51 of file address.cpp.
References addr_.
dodo::network::Address::Address | ( | const Address & | address | ) |
Copy from Address.
address | Source address to copy from. |
Definition at line 56 of file address.cpp.
References addr_.
|
inlineprivate |
Explicit cast of addr_ as sockaddr_in*.
Definition at line 278 of file address.hpp.
References addr_.
Referenced by asString(), getPort(), operator=(), operator==(), and setPort().
|
inlineprivate |
Explicit cast of addr_ as sockaddr_in6*.
Definition at line 284 of file address.hpp.
References addr_.
Referenced by asString(), getPort(), operator=(), operator==(), and setPort().
std::string dodo::network::Address::asString | ( | bool | withport = false | ) | const |
Return a string representation of this Address.
withport | If true, append ':' + port number. |
Definition at line 107 of file address.cpp.
References addr_, asIPv4Address(), asIPv6Address(), getPort(), and throw_SystemExceptionObject.
Referenced by dodo::network::AddrInfoItem::asString(), dodo::network::BaseSocket::bind(), dodo::network::TCPListener::construct(), dodo::network::BaseSocket::listen(), dodo::network::TCPServer::run(), and dodo::network::TCPListener::run().
|
inline |
Get to the underlying sockaddr_storage.
Definition at line 165 of file address.hpp.
References addr_.
Referenced by dodo::network::BaseSocket::connect().
|
inline |
Get this Address family.
Definition at line 159 of file address.hpp.
References addr_.
Referenced by dodo::network::TCPListener::construct(), and getHostAddrInfo().
|
static |
Get Address information on a host in a AddrInfo struct.
Could return the following SystemError
hostname | The host name to lookup. |
info | The AddrInfo structure to fill. |
Definition at line 144 of file address.cpp.
References Address(), dodo::network::AddrInfoItem::address, dodo::network::AddrInfo::canonicalname, dodo::common::SystemError::ecEAI_ADDRFAMILY, dodo::common::SystemError::ecEAI_NODATA, dodo::common::SystemError::ecEAI_NONAME, dodo::common::SystemError::ecOK, dodo::network::AddrInfo::items, dodo::network::AddrInfoItem::params, dodo::network::SocketParams::setAddressFamily(), dodo::network::SocketParams::setProtocol(), dodo::network::SocketParams::setSocketType(), and throw_SystemException.
Referenced by getHostAddrInfo().
|
static |
Return the first hit on SocketParams in address and canonicalname.
Could return the following SystemError
hostname | The host name to lookup. |
params | The SocketParams the entry must match to. |
address | The result address (set to invalid when this call returns an error). |
canonicalname | The canonicalname of hostname (set to "" when this call returns an error). |
Definition at line 177 of file address.cpp.
References Address(), dodo::network::AddrInfo::canonicalname, dodo::common::SystemError::ecEAI_NODATA, dodo::common::SystemError::ecOK, dodo::network::SocketParams::getAddressFamily(), getAddressFamily(), getHostAddrInfo(), dodo::network::SocketParams::getProtocol(), dodo::network::SocketParams::getSocketType(), dodo::network::AddrInfo::items, and dodo::network::SocketParams::setAddressFamily().
common::SystemError dodo::network::Address::getNameInfo | ( | std::string & | hostname | ) | const |
Do a reverse DNS lookup on this Address and return in hostname.
Could return the following SystemError
hostname | The string to receive the hostname. |
Definition at line 200 of file address.cpp.
References addr_, and dodo::common::SystemError::ecEAI_SYSTEM.
common::SystemError dodo::network::Address::getNameInfo | ( | std::string & | hostname, |
std::string & | service | ||
) | const |
Do a reverse DNS lookup on this Address and its port, and return in hostname and service.
hostname | The string variable to receive the hostname. |
service | The string variable to receive the service name. |
Definition at line 214 of file address.cpp.
References addr_, and dodo::common::SystemError::ecEAI_SYSTEM.
common::SystemError dodo::network::Address::getNameInfo | ( | std::string & | hostname, |
uint16_t & | port | ||
) | const |
Do a reverse DNS lookup on this Address and its port, and return in hostname and port.
hostname | The string variable to receive the hostname. |
port | The uint16_t variable to receive the port number. |
Definition at line 229 of file address.cpp.
References addr_, dodo::common::SystemError::ecEAI_SYSTEM, and throw_SystemException.
uint16_t dodo::network::Address::getPort | ( | ) | const |
Return the port number.
Definition at line 127 of file address.cpp.
References addr_, asIPv4Address(), and asIPv6Address().
Referenced by asString().
|
private |
Initialize the internals.
Definition at line 61 of file address.cpp.
References addr_.
Referenced by Address().
|
inline |
True if this Address is valid.
Definition at line 134 of file address.hpp.
References addr_.
Referenced by dodo::network::TCPListener::construct(), dodo::network::TCPListener::TCPListener(), and dodo::network::X509Certificate::verifyIP().
Address & dodo::network::Address::operator= | ( | const sockaddr_storage & | address | ) |
Assign from sockaddr_storage.
address | The address to assign in sockaddr_storage form. |
Definition at line 87 of file address.cpp.
References addr_.
Address & dodo::network::Address::operator= | ( | const std::string & | address | ) |
Assign from "ip" string.
address | The address to assign in string form. |
Definition at line 66 of file address.cpp.
References addr_, asIPv4Address(), and asIPv6Address().
bool dodo::network::Address::operator== | ( | const Address & | address | ) | const |
Test for equality.
The comparison is a byte compare on the internal sockaddr_in / sockaddr_in6 structures, which guarantees that Address objects assigned "::1" and "0:0:0:0:0:0:0:1" are considered equal.
address | The address to compare to. |
Definition at line 92 of file address.cpp.
References addr_, asIPv4Address(), and asIPv6Address().
void dodo::network::Address::setPort | ( | uint16_t | port | ) |
Set the port number.
port | The new port number. |
Definition at line 136 of file address.cpp.
References addr_, asIPv4Address(), and asIPv6Address().
Referenced by Address().
|
friend |
BaseSocket may access this class directly.
Definition at line 298 of file address.hpp.
|
friend |
Socket may access this class directly.
Definition at line 294 of file address.hpp.
|
private |
The address storage (for either ipv4 or ipv6).
Definition at line 289 of file address.hpp.
Referenced by Address(), asIPv4Address(), asIPv6Address(), asString(), dodo::network::BaseSocket::bind(), getAddress(), getAddressFamily(), getNameInfo(), getPort(), init(), isValid(), operator=(), operator==(), dodo::network::Socket::sendTo(), and setPort().