Thrill  0.1
SocketAddress Class Reference

Detailed Description

SocketAddress is a super class used to unify the two different IPv4 and IPv6 socket address representation.

It is derived into IPv4Address and IPv6Address only for direct initialization, in general one just uses SocketAddress("localhost:1234") for appropriate resolution into a sockaddr. The SocketAddress object can then be given to various Connect(), Bind() and similar functions of Socket.

Definition at line 41 of file socket_address.hpp.

+ Inheritance diagram for SocketAddress:
+ Collaboration diagram for SocketAddress:

#include <socket_address.hpp>

Classes

union  SockAddrUnion
 

Public Member Functions

 SocketAddress ()
 Create empty invalid address object by clearing all bytes. More...
 
 SocketAddress (struct sockaddr *sa, socklen_t salen)
 Create a socket address object with the given sockaddr data. More...
 
 SocketAddress (const std::string &hostport)
 
 SocketAddress (const char *hostname, const char *servicename)
 
uint16_t GetPort () const
 Return the currently set port address in host byte-order. More...
 
const char * GetResolveError () const
 
bool IsIPv4 () const
 Returns true if the enclosed socket address is a IPv4 address. More...
 
bool IsIPv6 () const
 Returns true if the enclosed socket address is a IPv6 address. More...
 
bool IsValid () const
 
bool Resolve (const char *hostname, const char *servicename=nullptr)
 
void SetPort (uint16_t port)
 Change the currently set port address. More...
 
struct sockaddr * sockaddr ()
 Return pointer to enclosed address as a generic sockattr struct. More...
 
const struct sockaddr * sockaddr () const
 Return pointer to enclosed address as a generic sockattr struct. More...
 
struct sockaddr_in * sockaddr_in ()
 Cast the enclosed sockaddr into the sockaddr_in IPv4 structure. More...
 
const struct sockaddr_in * sockaddr_in () const
 
struct sockaddr_in6 * sockaddr_in6 ()
 Cast the enclosed sockaddr into the sockaddr_in6 IPv6 structure. More...
 
const struct sockaddr_in6 * sockaddr_in6 () const
 
socklen_t socklen () const
 Return total length of enclosed sockaddr structure. More...
 
std::string ToStringHost () const
 Return the enclosed socket address as a string without the port number. More...
 
std::string ToStringHostPort () const
 Return the enclosed socket address as a string with the port number. More...
 

Static Public Member Functions

static std::vector< SocketAddressResolveAll (const char *hostname, const char *servicename=nullptr)
 
static SocketAddress ResolveOne (const char *hostname, const char *servicename=nullptr)
 
static SocketAddress ResolveWithPort (const char *hostname, const char *defaultservice)
 

Protected Attributes

union thrill::net::tcp::SocketAddress::SockAddrUnion sockaddr_
 

Private Attributes

int resolve_error_
 

Static Private Attributes

static constexpr bool debug = false
 

Friends

std::ostream & operator<< (std::ostream &os, const SocketAddress &sa)
 Make the socket address ostream-able: outputs address:port. More...
 

Constructor & Destructor Documentation

◆ SocketAddress() [1/4]

SocketAddress ( )
inline

Create empty invalid address object by clearing all bytes.

Definition at line 62 of file socket_address.hpp.

References SocketAddress::sockaddr().

Referenced by SocketAddress::Resolve(), SocketAddress::ResolveAll(), and SocketAddress::ResolveOne().

◆ SocketAddress() [2/4]

SocketAddress ( struct sockaddr sa,
socklen_t  salen 
)

Create a socket address object with the given sockaddr data.

Definition at line 30 of file socket_address.cpp.

References SocketAddress::sockaddr_.

◆ SocketAddress() [3/4]

SocketAddress ( const std::string &  hostport)
explicit

Create a socket address object and resolve the given host:port using getaddrinfo(). Check result with IsValid().

Definition at line 35 of file socket_address.cpp.

References SocketAddress::Resolve().

◆ SocketAddress() [4/4]

SocketAddress ( const char *  hostname,
const char *  servicename 
)

Create a socket address object and resolve the given host name using getaddrinfo(). Check result with IsValid().

Definition at line 50 of file socket_address.cpp.

References SocketAddress::Resolve().

Member Function Documentation

◆ GetPort()

uint16_t GetPort ( ) const
inline

Return the currently set port address in host byte-order.

Definition at line 146 of file socket_address.hpp.

References SocketAddress::sockaddr(), SocketAddress::sockaddr_in(), and SocketAddress::sockaddr_in6().

Referenced by SocketAddress::ToStringHostPort().

◆ GetResolveError()

const char * GetResolveError ( ) const

Return textual message of the last error occurring in the resolve method.

Definition at line 113 of file socket_address.cpp.

References SocketAddress::resolve_error_.

Referenced by SocketAddress::SetPort().

◆ IsIPv4()

bool IsIPv4 ( ) const
inline

Returns true if the enclosed socket address is a IPv4 address.

Definition at line 104 of file socket_address.hpp.

References SocketAddress::sockaddr().

◆ IsIPv6()

bool IsIPv6 ( ) const
inline

Returns true if the enclosed socket address is a IPv6 address.

Definition at line 109 of file socket_address.hpp.

References SocketAddress::sockaddr().

◆ IsValid()

bool IsValid ( ) const
inline

Returns true if the enclosed socket address is a valid IPv4 or IPv6 address.

Definition at line 98 of file socket_address.hpp.

References SocketAddress::sockaddr().

Referenced by Socket::bind(), Socket::connect(), and SocketAddress::Resolve().

◆ Resolve()

bool Resolve ( const char *  hostname,
const char *  servicename = nullptr 
)

Resolve the given host name using getaddrinfo() and replace this object with the first socket address if found.

Definition at line 88 of file socket_address.cpp.

References SocketAddress::IsValid(), SocketAddress::resolve_error_, SocketAddress::sockaddr_, and SocketAddress::SocketAddress().

Referenced by SocketAddress::SetPort(), and SocketAddress::SocketAddress().

◆ ResolveAll()

std::vector< SocketAddress > ResolveAll ( const char *  hostname,
const char *  servicename = nullptr 
)
static

Resolve the given host name using getaddrinfo() and return all resulting socket addresses as a vector.

Definition at line 156 of file socket_address.cpp.

References SocketAddress::SocketAddress().

Referenced by SocketAddress::SetPort().

◆ ResolveOne()

SocketAddress ResolveOne ( const char *  hostname,
const char *  servicename = nullptr 
)
static

Resolve the given host name using getaddrinfo() and return only the first socket address if found.

Definition at line 118 of file socket_address.cpp.

References SocketAddress::SocketAddress().

Referenced by SocketAddress::ResolveWithPort(), and SocketAddress::SetPort().

◆ ResolveWithPort()

SocketAddress ResolveWithPort ( const char *  hostname,
const char *  defaultservice 
)
static

Parse the address for a :port notation and then resolve the given host name using getaddrinfo() and return only the first socket address if found. Uses defaultservice if no port is found in the hostname.

Definition at line 141 of file socket_address.cpp.

References SocketAddress::ResolveOne().

Referenced by SocketAddress::SetPort().

◆ SetPort()

◆ sockaddr() [1/2]

◆ sockaddr() [2/2]

const struct sockaddr* sockaddr ( ) const
inline

Return pointer to enclosed address as a generic sockattr struct.

Definition at line 84 of file socket_address.hpp.

◆ sockaddr_in() [1/2]

struct sockaddr_in* sockaddr_in ( )
inline

Cast the enclosed sockaddr into the sockaddr_in IPv4 structure.

Definition at line 114 of file socket_address.hpp.

Referenced by SocketAddress::GetPort(), IPv4Address::IPv4Address(), SocketAddress::SetPort(), and SocketAddress::ToStringHost().

◆ sockaddr_in() [2/2]

const struct sockaddr_in* sockaddr_in ( ) const
inline

Cast the enclosed sockaddr into the sockaddr_in IPv4 structure. Const version.

Definition at line 120 of file socket_address.hpp.

◆ sockaddr_in6() [1/2]

struct sockaddr_in6* sockaddr_in6 ( )
inline

Cast the enclosed sockaddr into the sockaddr_in6 IPv6 structure.

Definition at line 125 of file socket_address.hpp.

Referenced by SocketAddress::GetPort(), IPv6Address::IPv6Address(), SocketAddress::SetPort(), and SocketAddress::ToStringHost().

◆ sockaddr_in6() [2/2]

const struct sockaddr_in6* sockaddr_in6 ( ) const
inline

Cast the enclosed sockaddr into the sockaddr_in6 IPv6 structure. Const version.

Definition at line 131 of file socket_address.hpp.

References SocketAddress::operator<<, SocketAddress::ToStringHost(), and SocketAddress::ToStringHostPort().

◆ socklen()

socklen_t socklen ( ) const
inline

Return total length of enclosed sockaddr structure.

Definition at line 89 of file socket_address.hpp.

References SocketAddress::sockaddr().

Referenced by Socket::bind(), Socket::connect(), and Socket::sendto().

◆ ToStringHost()

std::string ToStringHost ( ) const

Return the enclosed socket address as a string without the port number.

Definition at line 54 of file socket_address.cpp.

References sLOG, SocketAddress::sockaddr(), SocketAddress::sockaddr_in(), and SocketAddress::sockaddr_in6().

Referenced by SocketAddress::sockaddr_in6(), and SocketAddress::ToStringHostPort().

◆ ToStringHostPort()

std::string ToStringHostPort ( ) const

Return the enclosed socket address as a string with the port number.

Definition at line 80 of file socket_address.cpp.

References SocketAddress::GetPort(), thrill::mem::to_string(), and SocketAddress::ToStringHost().

Referenced by Connection::GetPeerAddress(), thrill::net::tcp::operator<<(), and SocketAddress::sockaddr_in6().

Friends And Related Function Documentation

◆ operator<<

std::ostream& operator<< ( std::ostream &  os,
const SocketAddress sa 
)
friend

Make the socket address ostream-able: outputs address:port.

Definition at line 84 of file socket_address.cpp.

Referenced by SocketAddress::sockaddr_in6().

Member Data Documentation

◆ debug

constexpr bool debug = false
staticprivate

Definition at line 43 of file socket_address.hpp.

◆ resolve_error_

int resolve_error_
private

Return value of the last getaddrinfo() call. Used to output nice user messages.

Definition at line 58 of file socket_address.hpp.

Referenced by SocketAddress::GetResolveError(), and SocketAddress::Resolve().

◆ sockaddr_


The documentation for this class was generated from the following files: