dodo  0.0.1
A C++ library to create containerized Linux services
dodo::network::Address Class Reference

Generic network Address, supporting ipv4 and ipv6 transparently. More...

#include <address.hpp>

Inheritance diagram for dodo::network::Address:
Collaboration diagram for dodo::network::Address:

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...
 
Addressoperator= (const Address &address)
 Assign (copy) from Address. More...
 
Addressoperator= (const sockaddr_storage &address)
 Assign from sockaddr_storage. More...
 
Addressoperator= (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...
 
- Public Member Functions inherited from dodo::common::DebugObject
 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 &params, 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

- Protected Member Functions inherited from dodo::common::DebugObject
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...
 

Detailed Description

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

using namespace dodo::common;
using namespace dodo::network;
Address address;
SocketParams sock_params = SocketParams( SocketParams::afUNSPEC,
SocketParams::stSTREAM,
SocketParams::pnTCP );
std::string canonicalname;
SystemError error = Address::getHostAddrInfo( "httpbin.org", sock_params, address, canonicalname );

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:

if ( error == SystemError::ecOK ) {
address.setPort( 443 );
Socket socket( true, sock_params );
error = socket.connect( address );
if ( error == SystemError::ecOK ) {
// connected
}
}

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.

Constructor & Destructor Documentation

◆ Address() [1/6]

dodo::network::Address::Address ( )
inline

Construct an invalid (unspecified) address.

Definition at line 96 of file address.hpp.

References addr_.

Referenced by getHostAddrInfo().

Here is the caller graph for this function:

◆ Address() [2/6]

dodo::network::Address::Address ( const std::string &  ip,
uint16_t  port 
)

Construct from "ip", port.

Parameters
ipThe string representation of the ip, either ipv4 or ipv6.
portThe port number (only specified for listen addresses).

Definition at line 40 of file address.cpp.

References init(), and setPort().

Here is the call graph for this function:

◆ Address() [3/6]

dodo::network::Address::Address ( const std::string &  ip)

Construct from ip, do not set port (only a listening socket needs a port preset).

Parameters
ipThe string representation of the ip, either ipv4 or ipv6.

Definition at line 35 of file address.cpp.

References init().

Here is the call graph for this function:

◆ Address() [4/6]

dodo::network::Address::Address ( const sockaddr_storage &  address)

Construct from sockaddr_storage.

Parameters
addressA sockaddr_storage structure.

Definition at line 46 of file address.cpp.

References addr_.

◆ Address() [5/6]

dodo::network::Address::Address ( const sockaddr *  address,
socklen_t  len 
)

Construct from sockaddr *.

Parameters
addressA const sockaddr*
lenAnd its length.

Definition at line 51 of file address.cpp.

References addr_.

◆ Address() [6/6]

dodo::network::Address::Address ( const Address address)

Copy from Address.

Parameters
addressSource address to copy from.

Definition at line 56 of file address.cpp.

References addr_.

Member Function Documentation

◆ asIPv4Address()

struct sockaddr_in* dodo::network::Address::asIPv4Address ( ) const
inlineprivate

Explicit cast of addr_ as sockaddr_in*.

Returns
The pointer.

Definition at line 278 of file address.hpp.

References addr_.

Referenced by asString(), getPort(), operator=(), operator==(), and setPort().

Here is the caller graph for this function:

◆ asIPv6Address()

struct sockaddr_in6* dodo::network::Address::asIPv6Address ( ) const
inlineprivate

Explicit cast of addr_ as sockaddr_in6*.

Returns
The pointer.

Definition at line 284 of file address.hpp.

References addr_.

Referenced by asString(), getPort(), operator=(), operator==(), and setPort().

Here is the caller graph for this function:

◆ asString()

std::string dodo::network::Address::asString ( bool  withport = false) const

Return a string representation of this Address.

Parameters
withportIf true, append ':' + port number.
Returns
string representation of this Address.

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().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ getAddress()

const sockaddr_storage* dodo::network::Address::getAddress ( ) const
inline

Get to the underlying sockaddr_storage.

Returns
A pointer to the underlying socket storage.

Definition at line 165 of file address.hpp.

References addr_.

Referenced by dodo::network::BaseSocket::connect().

Here is the caller graph for this function:

◆ getAddressFamily()

SocketParams::AddressFamily dodo::network::Address::getAddressFamily ( ) const
inline

Get this Address family.

Returns
The address family of this Address.

Definition at line 159 of file address.hpp.

References addr_.

Referenced by dodo::network::TCPListener::construct(), and getHostAddrInfo().

Here is the caller graph for this function:

◆ getHostAddrInfo() [1/2]

common::SystemError dodo::network::Address::getHostAddrInfo ( const std::string &  hostname,
AddrInfo info 
)
static

Get Address information on a host in a AddrInfo struct.

Could return the following SystemError

  • SystemError::ecOK
  • SystemError::ecEAI_ADDRFAMILY (The hosts exists but has not address in the requested address family)
  • SystemError::ecEAI_NODATA (The hosts exists in DNS but has no addresses)
  • SystemError::ecEAI_NONAME (The hosts does not exist in DNS)
Parameters
hostnameThe host name to lookup.
infoThe AddrInfo structure to fill.
Returns
SystemError::ecOK if the lookup resulted in at least one address.

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().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ getHostAddrInfo() [2/2]

common::SystemError dodo::network::Address::getHostAddrInfo ( const std::string &  hostname,
SocketParams params,
Address address,
std::string &  canonicalname 
)
static

Return the first hit on SocketParams in address and canonicalname.

Could return the following SystemError

  • SystemError::ecOK
  • SystemError::ecEAI_ADDRFAMILY (The hosts exists but has not address in the requested address family)
  • SystemError::ecEAI_NODATA (The hosts exists in DNS but has no addresses)
  • SystemError::ecEAI_NONAME (The hosts does not exist in DNS)
Parameters
hostnameThe host name to lookup.
paramsThe SocketParams the entry must match to.
addressThe result address (set to invalid when this call returns an error).
canonicalnameThe canonicalname of hostname (set to "" when this call returns an error).
Returns
SystemError::ecOK if the lookup resulted in an address.

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().

Here is the call graph for this function:

◆ getNameInfo() [1/3]

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

  • SystemError::ecOK
  • SystemError::ecEAI_AGAIN
  • SystemError::ecEAI_BADFLAGS
  • SystemError::ecEAI_FAIL
  • SystemError::ecEAI_FAMILY
  • SystemError::ecEAI_MEMORY
  • SystemError::ecEAI_NONAME
  • SystemError::ecEAI_OVERFLOW
  • SystemError::ecEAI_SYSTEM is silently replaced by errno
    Parameters
    hostnameThe string to receive the hostname.
    Returns
    The error or SystemError::ecOK on success.

Definition at line 200 of file address.cpp.

References addr_, and dodo::common::SystemError::ecEAI_SYSTEM.

◆ getNameInfo() [2/3]

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.

Parameters
hostnameThe string variable to receive the hostname.
serviceThe string variable to receive the service name.
Returns
The error or SystemError::ecOK on success.
See also
getNameInfo() for a full list of possible errors returned.

Definition at line 214 of file address.cpp.

References addr_, and dodo::common::SystemError::ecEAI_SYSTEM.

◆ getNameInfo() [3/3]

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.

Parameters
hostnameThe string variable to receive the hostname.
portThe uint16_t variable to receive the port number.
Returns
The error or SystemError::ecOK on success.
See also
getNameInfo() for a full list of possible errors returned.

Definition at line 229 of file address.cpp.

References addr_, dodo::common::SystemError::ecEAI_SYSTEM, and throw_SystemException.

◆ getPort()

uint16_t dodo::network::Address::getPort ( ) const

Return the port number.

Returns
The port number.

Definition at line 127 of file address.cpp.

References addr_, asIPv4Address(), and asIPv6Address().

Referenced by asString().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ init()

void dodo::network::Address::init ( )
private

Initialize the internals.

Definition at line 61 of file address.cpp.

References addr_.

Referenced by Address().

Here is the caller graph for this function:

◆ isValid()

bool dodo::network::Address::isValid ( ) const
inline

True if this Address is valid.

Returns
true when 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().

Here is the caller graph for this function:

◆ operator=() [1/3]

Address & dodo::network::Address::operator= ( const Address address)

Assign (copy) from Address.

Parameters
addressThe Address to assign.
Returns
This Address.

Definition at line 82 of file address.cpp.

References addr_.

◆ operator=() [2/3]

Address & dodo::network::Address::operator= ( const sockaddr_storage &  address)

Assign from sockaddr_storage.

Parameters
addressThe address to assign in sockaddr_storage form.
Returns
This Address.

Definition at line 87 of file address.cpp.

References addr_.

◆ operator=() [3/3]

Address & dodo::network::Address::operator= ( const std::string &  address)

Assign from "ip" string.

Parameters
addressThe address to assign in string form.
Returns
This Address.

Definition at line 66 of file address.cpp.

References addr_, asIPv4Address(), and asIPv6Address().

Here is the call graph for this function:

◆ operator==()

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.

Parameters
addressThe address to compare to.
Returns
True if the addresses are equal.

Definition at line 92 of file address.cpp.

References addr_, asIPv4Address(), and asIPv6Address().

Here is the call graph for this function:

◆ setPort()

void dodo::network::Address::setPort ( uint16_t  port)

Set the port number.

Parameters
portThe new port number.

Definition at line 136 of file address.cpp.

References addr_, asIPv4Address(), and asIPv6Address().

Referenced by Address().

Here is the call graph for this function:
Here is the caller graph for this function:

Friends And Related Function Documentation

◆ BaseSocket

friend class BaseSocket
friend

BaseSocket may access this class directly.

Definition at line 298 of file address.hpp.

◆ Socket

friend class Socket
friend

Socket may access this class directly.

Definition at line 294 of file address.hpp.

Field Documentation

◆ addr_

struct sockaddr_storage dodo::network::Address::addr_
private

The documentation for this class was generated from the following files:
dodo::network::Address
Generic network Address, supporting ipv4 and ipv6 transparently.
Definition: address.hpp:90
dodo::network::Address::getHostAddrInfo
static common::SystemError getHostAddrInfo(const std::string &hostname, AddrInfo &info)
Get Address information on a host in a AddrInfo struct.
Definition: address.cpp:144
dodo::network::Address::setPort
void setPort(uint16_t port)
Set the port number.
Definition: address.cpp:136
dodo::network::SocketParams
Socket parameters - the family (domain), socket type and protocol triplet.
Definition: socketparams.hpp:35
dodo::network
Interface for network communication.
Definition: address.hpp:37
dodo::common
Common and utility interfaces.
Definition: application.hpp:29
dodo::common::SystemError
Linux system error primitive to provide a consistent interface to Linux error codes.
Definition: systemerror.hpp:53
dodo::network::Address::Socket
friend class Socket
Socket may access this class directly.
Definition: address.hpp:294