UCommon
Public Member Functions | Static Public Member Functions | Protected Attributes
ucommon::Socket::address Class Reference

A generic socket address class. More...

#include <socket.h>

Public Member Functions

void add (const char *hostname, const char *service=NULL, int type=SOCK_STREAM)
 Append additional host addresses to our list. More...
 
void add (sockaddr *address)
 Add an individual socket address to our address list. More...
 
 address (int family, const char *address, int type=SOCK_STREAM, int protocol=0)
 Construct a socket address. More...
 
 address (int family, const char *hostname, const char *service=NULL)
 Construct a socket address for an existing socket. More...
 
 address (const char *host, const char *service, int type=SOCK_STREAM)
 Construct a socket address list for a service. More...
 
 address (const char *hostname, in_port_t port=0)
 Construct a socket address from host and service. More...
 
 address (const in_addr &address, in_port_t port=0)
 Construct a socket address from an IPv4 address and a port number.
 
 address (const in6_addr &address, in_port_t port=0)
 Construct a socket address from an IPv6 address and a port number.
 
 address (const sockaddr &address)
 Construct a socket address from a sockaddr object.
 
 address (const addrinfo *address)
 Construct a socket address from an addrinfo structure.
 
 address ()
 Construct an empty address.
 
 address (const address &reference)
 Copy constructor. More...
 
void clear (void)
 Clear current object.
 
void copy (const struct addrinfo *address)
 Copy an existing addrinfo into our object. More...
 
bool equals (const address &other) const
 
int family (void) const
 Get the family of the first member in a list of services. More...
 
struct sockaddr * find (const struct sockaddr *addr) const
 Find a specific socket address in our address list. More...
 
const struct sockaddr * get (void) const
 Get the first socket address in our address list. More...
 
const struct sockaddr * get (int family) const
 Get the first socket address of specified family from our list. More...
 
const struct sockaddr * getAddr (void) const
 
size_t getLength (void) const
 Get the address size of the first address. More...
 
struct addrinfogetList (void) const
 Get the full socket address list from the object. More...
 
in_port_t getPort (void) const
 Get the port of the first address . More...
 
unsigned insert (const struct addrinfo *address)
 Insert unique members from another socket address list to ours. More...
 
bool insert (const struct sockaddr *address)
 Insert an individual socket address to our address list only if unique. More...
 
bool is_any () const
 Test if the first socket address is ADDR_ANY: 0.0.0.0 or ::0. More...
 
bool is_loopback () const
 Test if the first socket address is ADDR_LOOPBACK: 127.0.0.1 or ::1. More...
 
bool is_valid () const
 
bool isAny () const
 
bool isLoopback () const
 
bool isValid () const
 
struct sockaddr * modify (void)
 
struct sockaddr * modify (int family)
 
struct addrinfooperator * () const
 Return the full socket address list by pointer reference. More...
 
 operator bool () const
 Test if the address list is valid. More...
 
 operator struct addrinfo * () const
 Get the full socket address list by casted reference. More...
 
 operator struct sockaddr * ()
 Get the first socket address by casted reference. More...
 
 operator struct sockaddr_in * ()
 
 operator struct sockaddr_in6 * ()
 
bool operator! () const
 Test if we have no address list. More...
 
bool operator!= (const address &other) const
 
const struct sockaddr * operator() (void) const
 
const struct sockaddr * operator() (int family) const
 
addressoperator= (const address &rhs)
 Assignment operator. More...
 
bool operator== (const address &other) const
 Compare two address lists. More...
 
size_t print (char *dst, size_t dst_sz, bool port=false, bool force_brackets=false) const
 Print the first socket address as a human-readable string to the provided buffer and returns the printed string length. More...
 
unsigned remove (const struct addrinfo *address)
 Remove members from another socket address list from ours. More...
 
bool remove (const struct sockaddr *address)
 Remove an individual socket address from our address list. More...
 
void set (const char *hostname, const char *service=NULL, int type=SOCK_STREAM)
 Set the host addresses to form a new list. More...
 
void set (int family, const char *address, int type=SOCK_STREAM, int protocol=0)
 Set an entry for host binding. More...
 
void set (struct sockaddr *address)
 Set an individual socket address for our address list. More...
 
void set (const char *hostname, in_port_t service=0)
 Set a socket address from host and service. More...
 
void setAny (int family=0)
 Clear the address list and set the first address to be the ADDR_ANY of the current family, or of the specified family (if set). More...
 
void setLoopback (int family=0)
 Clear the address list and set the first address to be the ADDR_LOOPBACK of the current family, or of the specified family (if set). More...
 
void setPort (in_port_t port)
 Set the port of all addresses in the list. More...
 
address withPort (in_port_t port) const
 Returns a copy of this address list with the specified port set.
 
 ~address ()
 Destroy address. More...
 

Static Public Member Functions

static sockaddr_storage any (int family)
 Get a ADDR_ANY socket address of the given family.
 
static struct sockaddr * dup (struct sockaddr *address)
 Duplicate a socket address. More...
 
static size_t getLength (const struct sockaddr *address)
 Returns the size of the socket address according to the family. More...
 
static in_port_t getPort (const struct sockaddr *address)
 Returns the port of the socket address. More...
 
static struct sockaddr_in * ipv4 (struct sockaddr *address)
 Convert address object into ipv4 address. More...
 
static struct sockaddr_in6 * ipv6 (struct sockaddr *address)
 Convert address object into ipv6 address. More...
 
static bool isAny (const struct sockaddr *address)
 Test if the socket address is ADDR_ANY: 0.0.0.0 or ::0. More...
 
static bool isLoopback (const struct sockaddr *address)
 Test if the socket address is ADDR_LOOPBACK: 127.0.0.1 or ::1. More...
 
static sockaddr_storage loopback (int family)
 Get a ADDR_LOOPBACK socket address of the given family.
 
static size_t print (const struct sockaddr *src, char *dst, size_t dst_sz, bool port=false, bool ipv6_brackets=false)
 Print socket address as a human-readable string to the provided buffer and returns the printed string length. More...
 
static void setAny (struct sockaddr *sa)
 Set the socket address to ADDR_ANY: 0.0.0.0 or ::0.
 
static void setLoopback (struct sockaddr *sa)
 Set the socket address to ADDR_LOOPBACK: 127.0.0.1 or ::1 depending on the family of the pointed address.
 
static void setPort (struct sockaddr *address, in_port_t port)
 Set the port of the socket address. More...
 

Protected Attributes

struct addrinfolist
 

Detailed Description

A generic socket address class.

This class uses the addrinfo list to store socket multiple addresses in a protocol and family independent manner. Hence, this address class can be used for ipv4 and ipv6 sockets, for assigning connections to multiple hosts, etc. The address class will call the resolver when passed host names.

Author
David Sugar dyfet.nosp@m.@gnu.nosp@m.telep.nosp@m.hony.nosp@m..org

Definition at line 364 of file socket.h.

Constructor & Destructor Documentation

◆ address() [1/5]

ucommon::Socket::address::address ( int  family,
const char *  address,
int  type = SOCK_STREAM,
int  protocol = 0 
)

Construct a socket address.

This is used to get an address to bind a socket interface to. The address can be specified as the ip address of the interface or as a "hostname". If a hostname is used, then family should be specified for clarity.

Parameters
familyof socket address. Needed when host names are used.
addressor hostname.
typeof socket (stream, dgram, etc).
protocolnumber of socket.

◆ address() [2/5]

ucommon::Socket::address::address ( int  family,
const char *  hostname,
const char *  service = NULL 
)

Construct a socket address for an existing socket.

This can be the name of a host or to perform a lookup in a domain for a service. Family can be used to restrict the set of results returned, however since things like connecto() already filter by family and create will use family from the addrinfo, in most cases AF_UNSPEC can be used. This may be depreciated in favor of the constructor that matches a set() method.

Parameters
familyof hosts to filter by or AF_UNSPEC.
hostnameor ip address. The socket family is used for hostnames.
serviceport or name we are referencing or NULL.

◆ address() [3/5]

ucommon::Socket::address::address ( const char *  host,
const char *  service,
int  type = SOCK_STREAM 
)

Construct a socket address list for a service.

Parameters
hostaddress for service.
servicename or port number.
typeof service, stream, dgram, etc.

◆ address() [4/5]

ucommon::Socket::address::address ( const char *  hostname,
in_port_t  port = 0 
)

Construct a socket address from host and service.

This is primarily used to construct a list of potential service connections by pure port numbers or for host lookup only.

Parameters
hostnameor address to use.
serviceport or 0.

◆ address() [5/5]

ucommon::Socket::address::address ( const address reference)

Copy constructor.

Parameters
referenceto object to copy from.

◆ ~address()

ucommon::Socket::address::~address ( )

Destroy address.

Deallocate addrinfo structure.

Member Function Documentation

◆ add() [1/2]

void ucommon::Socket::address::add ( const char *  hostname,
const char *  service = NULL,
int  type = SOCK_STREAM 
)

Append additional host addresses to our list.

Parameters
hostnameor address to resolve.
servicename or port number, or NULL if not used.
typeof socket (stream or dgram).

◆ add() [2/2]

void ucommon::Socket::address::add ( sockaddr *  address)

Add an individual socket address to our address list.

Parameters
addressto add.

◆ copy()

void ucommon::Socket::address::copy ( const struct addrinfo address)

Copy an existing addrinfo into our object.

This is also used to support the copy constructor.

Parameters
addresslist to copy from.

◆ dup()

static struct sockaddr* ucommon::Socket::address::dup ( struct sockaddr *  address)
static

Duplicate a socket address.

Parameters
addressto duplicate.
Returns
duplicate address object.

◆ family()

int ucommon::Socket::address::family ( void  ) const

Get the family of the first member in a list of services.

Returns
family of first socket address or 0 if none.

◆ find()

struct sockaddr* ucommon::Socket::address::find ( const struct sockaddr *  addr) const

Find a specific socket address in our address list.

Returns
matching address from list or NULL if not found.

◆ get() [1/2]

const struct sockaddr* ucommon::Socket::address::get ( void  ) const

Get the first socket address in our address list.

Returns
first socket address or NULL if none.

◆ get() [2/2]

const struct sockaddr* ucommon::Socket::address::get ( int  family) const

Get the first socket address of specified family from our list.

Parameters
familyto seek.
Returns
first socket address of family or NULL if none.

◆ getLength() [1/2]

size_t ucommon::Socket::address::getLength ( void  ) const
inline

Get the address size of the first address.

Returns
size in bytes of first socket address or 0 if none.

Definition at line 531 of file socket.h.

◆ getLength() [2/2]

static size_t ucommon::Socket::address::getLength ( const struct sockaddr *  address)
inlinestatic

Returns the size of the socket address according to the family.

Returns
size in bytes of the valid part of the socket address.

Definition at line 756 of file socket.h.

◆ getList()

struct addrinfo* ucommon::Socket::address::getList ( void  ) const
inline

Get the full socket address list from the object.

Returns
addrinfo list we resolved or NULL if none.

Definition at line 565 of file socket.h.

◆ getPort() [1/2]

in_port_t ucommon::Socket::address::getPort ( void  ) const
inline

Get the port of the first address .

Returns
port of first socket address or 0 if none.

Definition at line 539 of file socket.h.

◆ getPort() [2/2]

static in_port_t ucommon::Socket::address::getPort ( const struct sockaddr *  address)
inlinestatic

Returns the port of the socket address.

Returns
port associated to the socket address.

Definition at line 764 of file socket.h.

Here is the call graph for this function:

◆ insert() [1/2]

unsigned ucommon::Socket::address::insert ( const struct addrinfo address)

Insert unique members from another socket address list to ours.

Parameters
addresslist to insert into list.
Returns
count of addresses added.

◆ insert() [2/2]

bool ucommon::Socket::address::insert ( const struct sockaddr *  address)

Insert an individual socket address to our address list only if unique.

Parameters
addressto insert into list.
Returns
true if inserted, false if duplicate.

◆ ipv4()

static struct sockaddr_in* ucommon::Socket::address::ipv4 ( struct sockaddr *  address)
static

Convert address object into ipv4 address.

Parameters
addressto convert.
Returns
new ipv4 address or NULL if not ipv4.

◆ ipv6()

static struct sockaddr_in6* ucommon::Socket::address::ipv6 ( struct sockaddr *  address)
static

Convert address object into ipv6 address.

Parameters
addressto convert.
Returns
new ipv6 address or NULL if not ipv6.

◆ is_any()

bool ucommon::Socket::address::is_any ( ) const
inline

Test if the first socket address is ADDR_ANY: 0.0.0.0 or ::0.

Returns
true if the address is one of the above.

Definition at line 630 of file socket.h.

◆ is_loopback()

bool ucommon::Socket::address::is_loopback ( ) const
inline

Test if the first socket address is ADDR_LOOPBACK: 127.0.0.1 or ::1.

Returns
true if the address is one of the above.

Definition at line 651 of file socket.h.

◆ isAny()

static bool ucommon::Socket::address::isAny ( const struct sockaddr *  address)
static

Test if the socket address is ADDR_ANY: 0.0.0.0 or ::0.

Returns
true if the address is one of the above.

◆ isLoopback()

static bool ucommon::Socket::address::isLoopback ( const struct sockaddr *  address)
static

Test if the socket address is ADDR_LOOPBACK: 127.0.0.1 or ::1.

Returns
true if the address is one of the above.

◆ operator *()

struct addrinfo* ucommon::Socket::address::operator * ( ) const
inline

Return the full socket address list by pointer reference.

Returns
addrinfo list we resolved or NULL if none.

Definition at line 581 of file socket.h.

◆ operator bool()

ucommon::Socket::address::operator bool ( ) const
inline

Test if the address list is valid.

Returns
true if we have an address list.

Definition at line 605 of file socket.h.

◆ operator struct addrinfo *()

ucommon::Socket::address::operator struct addrinfo * ( ) const
inline

Get the full socket address list by casted reference.

Returns
addrinfo list we resolved or NULL if none.

Definition at line 573 of file socket.h.

◆ operator struct sockaddr *()

ucommon::Socket::address::operator struct sockaddr * ( )
inline

Get the first socket address by casted reference.

Returns
first socket address we resolved or NULL if none.

Definition at line 494 of file socket.h.

◆ operator!()

bool ucommon::Socket::address::operator! ( ) const
inline

Test if we have no address list.

Returns
true if we have no address list.

Definition at line 621 of file socket.h.

◆ operator=()

address& ucommon::Socket::address::operator= ( const address rhs)

Assignment operator.

Parameters
referenceto object to copy from.

◆ operator==()

bool ucommon::Socket::address::operator== ( const address other) const

Compare two address lists.

Returns
true if the two lists are the same (same addresses in the same order).

◆ print() [1/2]

size_t ucommon::Socket::address::print ( char *  dst,
size_t  dst_sz,
bool  port = false,
bool  force_brackets = false 
) const
inline

Print the first socket address as a human-readable string to the provided buffer and returns the printed string length.

Parameters
srcAddress to print.
dstDestination buffer to print the socket address on.
dst_szSize of the provided buffer. Strongly recommended to be at least INET6_ADDRSTRLEN (or INET_ADDRSTRLEN if compiled without IPv6).
portIf true, print port number with address. If true, ipv6_brackets will also be forced to true.
ipv6_bracketsIf true, force printing IPv6 brackets. Ignored if address is an IPv4.
Returns
length (in bytes) of the printed string, excluding trailing zero.

Definition at line 597 of file socket.h.

◆ print() [2/2]

static size_t ucommon::Socket::address::print ( const struct sockaddr *  src,
char *  dst,
size_t  dst_sz,
bool  port = false,
bool  ipv6_brackets = false 
)
static

Print socket address as a human-readable string to the provided buffer and returns the printed string length.

Parameters
srcAddress to print.
dstDestination buffer to print the socket address on.
dst_szSize of the provided buffer. Strongly recommended to be at least INET6_ADDRSTRLEN (or INET_ADDRSTRLEN if compiled without IPv6).
portIf true, print port number with address. If true, ipv6_brackets will also be forced to true.
ipv6_bracketsIf true, force printing IPv6 brackets. Ignored if address is an IPv4.
Returns
length (in bytes) of the printed string, excluding trailing zero.

◆ remove() [1/2]

unsigned ucommon::Socket::address::remove ( const struct addrinfo address)

Remove members from another socket address list from ours.

Parameters
addresslist to remove from list.
Returns
count of addresses removed.

◆ remove() [2/2]

bool ucommon::Socket::address::remove ( const struct sockaddr *  address)

Remove an individual socket address from our address list.

Parameters
addressto remove.
Returns
true if found and removed, false if not found.

◆ set() [1/4]

void ucommon::Socket::address::set ( const char *  hostname,
const char *  service = NULL,
int  type = SOCK_STREAM 
)

Set the host addresses to form a new list.

Parameters
hostnameor address to resolve.
servicename or port number, or NULL if not used.
typeof socket (stream or dgram) to filter list by.

◆ set() [2/4]

void ucommon::Socket::address::set ( int  family,
const char *  address,
int  type = SOCK_STREAM,
int  protocol = 0 
)

Set an entry for host binding.

Parameters
familyof socket address. Needed when hostnames are used.
addressor hostname.
typeof socket (stream, dgram, etc).
protocolnumber of socket.

◆ set() [3/4]

void ucommon::Socket::address::set ( struct sockaddr *  address)

Set an individual socket address for our address list.

Parameters
addressto add.

◆ set() [4/4]

void ucommon::Socket::address::set ( const char *  hostname,
in_port_t  service = 0 
)

Set a socket address from host and service.

Parameters
hostnameor address to use.
serviceport or 0.

◆ setAny()

void ucommon::Socket::address::setAny ( int  family = 0)

Clear the address list and set the first address to be the ADDR_ANY of the current family, or of the specified family (if set).

Parameters
familyaddress family to set.

◆ setLoopback()

void ucommon::Socket::address::setLoopback ( int  family = 0)

Clear the address list and set the first address to be the ADDR_LOOPBACK of the current family, or of the specified family (if set).

Parameters
familyaddress family to set.

◆ setPort() [1/2]

void ucommon::Socket::address::setPort ( in_port_t  port)

Set the port of all addresses in the list.

Parameters
portthe port to set.

◆ setPort() [2/2]

static void ucommon::Socket::address::setPort ( struct sockaddr *  address,
in_port_t  port 
)
static

Set the port of the socket address.

Parameters
addressto edit.
portto associate to the socket address.

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