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=((void *) 0), int type=SOCK_STREAM)
 Append additional host addresses to our list.
void add (sockaddr *address)
 Add an individual socket address to our address list.
 address (int family, const char *address, int type=SOCK_STREAM, int protocol=0)
 Construct a socket address.
 address (int family, const char *hostname, const char *service=((void *) 0))
 Construct a socket address for an existing socket.
 address (const char *host, const char *service, int type=SOCK_STREAM)
 Construct a socket address list for a service.
 address (const char *hostname, in_port_t port=0)
 Construct a socket address from host and service.
 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.
void clear (void)
 Clear current object.
void copy (const struct addrinfo *address)
 Copy an existing addrinfo into our object.
bool equals (const address &other) const
int family (void) const
 Get the family of the first member in a list of services.
struct sockaddr * find (const struct sockaddr *addr) const
 Find a specific socket address in our address list.
struct sockaddr * get (void) const
 Get the first socket address in our address list.
struct sockaddr * get (int family) const
 Get the first socket address of specified family from our list.
struct sockaddr * getAddr (void) const
size_t getLength (void) const
 Get the address size of the first address.
struct addrinfogetList (void) const
 Get the full socket address list from the object.
in_port_t getPort (void) const
 Get the port of the first address .
unsigned insert (const struct addrinfo *address)
 Insert unique members from another socket address list to ours.
bool insert (const struct sockaddr *address)
 Insert an individual socket address to our address list only if unique.
bool insert (const struct sockaddr &address)
bool isAny () const
 Test if the first socket address is ADDR_ANY: 0.0.0.0 or ::0.
bool isLoopback () const
 Test if the first socket address is ADDR_LOOPBACK: 127.0.0.1 or ::1.
 operator bool () const
 Test if the address list is valid.
 operator struct addrinfo * () const
 Get the full socket address list by casted reference.
 operator struct sockaddr * () const
 Get the first socket address by casted reference.
 operator struct sockaddr_in * () const
 operator struct sockaddr_in6 * () const
bool operator! () const
 Test if we have no address list.
bool operator!= (const address &other) const
struct sockaddr * operator() (void) const
struct sockaddr * operator() (int family) const
struct addrinfooperator* () const
 Return the full socket address list by pointer reference.
addressoperator= (const address &rhs)
 Assignment operator.
bool operator== (const address &other) const
 Compare two address lists.
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.
unsigned remove (const struct addrinfo *address)
 Remove members from another socket address list from ours.
bool remove (const struct sockaddr *address)
 Remove an individual socket address from our address list.
void set (const char *hostname, const char *service=((void *) 0), int type=SOCK_STREAM)
 Set the host addresses to form a new list.
void set (int family, const char *address, int type=SOCK_STREAM, int protocol=0)
 Set an entry for host binding.
void set (struct sockaddr *address)
 Set an individual socket address for our address list.
void set (const char *hostname, in_port_t service=0)
 Set a socket address from host and service.
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).
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).
void setPort (in_port_t port)
 Set the port of all addresses in the list.
address withPort (in_port_t port) const
 Returns a copy of this address list with the specified port set.
 ~address ()
 Destroy address.

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.
static size_t getLength (const struct sockaddr *address)
 Returns the size of the socket address according to the family.
static in_port_t getPort (const struct sockaddr *address)
 Returns the port of the socket address.
static struct sockaddr_in * ipv4 (struct sockaddr *address)
 Convert address object into ipv4 address.
static struct sockaddr_in6 * ipv6 (struct sockaddr *address)
 Convert address object into ipv6 address.
static bool isAny (const struct sockaddr *address)
 Test if the socket address is ADDR_ANY: 0.0.0.0 or ::0.
static bool isLoopback (const struct sockaddr *address)
 Test if the socket address is ADDR_LOOPBACK: 127.0.0.1 or ::1.
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.
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.

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@gnutelephony.org>

Definition at line 350 of file socket.h.


Constructor & Destructor Documentation

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.
ucommon::Socket::address::address ( int  family,
const char *  hostname,
const char *  service = ((void *) 0) 
)

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

Copy constructor.

Parameters:
referenceto object to copy from.

Destroy address.

Deallocate addrinfo structure.


Member Function Documentation

void ucommon::Socket::address::add ( const char *  hostname,
const char *  service = ((void *) 0),
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).
void ucommon::Socket::address::add ( sockaddr *  address)

Add an individual socket address to our address list.

Parameters:
addressto add.
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.
static struct sockaddr* ucommon::Socket::address::dup ( struct sockaddr *  address) [static, read]

Duplicate a socket address.

Parameters:
addressto duplicate.
Returns:
duplicate address object.
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.
struct sockaddr* ucommon::Socket::address::find ( const struct sockaddr *  addr) const [read]

Find a specific socket address in our address list.

Returns:
matching address from list or NULL if not found.
struct sockaddr* ucommon::Socket::address::get ( void  ) const [read]

Get the first socket address in our address list.

Returns:
first socket address or NULL if none.
struct sockaddr* ucommon::Socket::address::get ( int  family) const [read]

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

Parameters:
familyto seek.
Returns:
first socket address of family or NULL if none.
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 503 of file socket.h.

static size_t ucommon::Socket::address::getLength ( const struct sockaddr *  address) [inline, static]

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 704 of file socket.h.

struct addrinfo* ucommon::Socket::address::getList ( void  ) const [inline, read]

Get the full socket address list from the object.

Returns:
addrinfo list we resolved or NULL if none.

Definition at line 535 of file socket.h.

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 510 of file socket.h.

Here is the call graph for this function:

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

Returns the port of the socket address.

Returns:
port associated to the socket address.
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.
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.
static struct sockaddr_in* ucommon::Socket::address::ipv4 ( struct sockaddr *  address) [static, read]

Convert address object into ipv4 address.

Parameters:
addressto convert.
Returns:
new ipv4 address or NULL if not ipv4.
static struct sockaddr_in6* ucommon::Socket::address::ipv6 ( struct sockaddr *  address) [static, read]

Convert address object into ipv6 address.

Parameters:
addressto convert.
Returns:
new ipv6 address or NULL if not ipv6.
bool ucommon::Socket::address::isAny ( ) 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 586 of file socket.h.

Here is the call graph for this function:

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.
bool ucommon::Socket::address::isLoopback ( ) 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 602 of file socket.h.

Here is the call graph for this function:

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.
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 571 of file socket.h.

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 542 of file socket.h.

ucommon::Socket::address::operator struct sockaddr * ( ) const [inline]

Get the first socket address by casted reference.

Returns:
first socket address we resolved or NULL if none.

Definition at line 472 of file socket.h.

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 578 of file socket.h.

struct addrinfo* ucommon::Socket::address::operator* ( ) const [inline, read]

Return the full socket address list by pointer reference.

Returns:
addrinfo list we resolved or NULL if none.

Definition at line 549 of file socket.h.

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

Assignment operator.

Parameters:
referenceto object to copy from.
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).
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 564 of file socket.h.

Here is the call graph for this function:

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.
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.
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.
void ucommon::Socket::address::set ( const char *  hostname,
const char *  service = ((void *) 0),
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.
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.
void ucommon::Socket::address::set ( struct sockaddr *  address)

Set an individual socket address for our address list.

Parameters:
addressto add.
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.
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:
family,:address family to set.
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:
family,:address family to set.
void ucommon::Socket::address::setPort ( in_port_t  port)

Set the port of all addresses in the list.

Parameters:
portthe port to set.
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: