UCommon
Public Member Functions | Static Public Member Functions | Protected Attributes
ucc::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.
void add (sockaddr *address)
 Add an individual socket address to our address list.
 address (const char *hostname, unsigned service=0)
 Construct a socket address from host and service.
 address (int family, const char *address, int type=SOCK_STREAM, int protocol=0)
 Construct a socket address.
 address ()
 Construct an empty address.
 address (int family, const char *hostname, const char *service=NULL)
 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 address &reference)
 Copy constructor.
void clear (void)
 Clear current object.
void copy (const struct addrinfo *address)
 Copy an existing addrinfo into our object.
struct sockaddr * find (struct sockaddr *addr) const
 Find a specific 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
 Get the first socket address in our address list.
int getfamily (void) const
 Get the family of the first member in a list of services.
struct addrinfo * getList (void) const
 Get the full socket address list from the object.
bool insert (struct sockaddr *address)
 Insert an individual socket address to our address list only if unique.
unsigned insert (struct addrinfo *address)
 Insert unique members from another socket address list to ours.
 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.
bool operator! () const
 Test if we have no address list.
struct addrinfo * operator* () const
 Return the full socket address list by pointer reference.
bool remove (struct sockaddr *address)
 Remove an individual socket address from our address list.
unsigned remove (struct addrinfo *address)
 Remove members from another socket address list from ours.
void set (const char *hostname, const char *service=NULL, 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 (const char *hostname, unsigned service=0)
 Set a socket address from host and service.
void set (struct sockaddr *address)
 Set an individual socket address for our address list.
 ~address ()
 Destroy address.

Static Public Member Functions

static struct sockaddr * dup (struct sockaddr *address)
 Duplicate a 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.

Protected Attributes

struct addrinfo * list

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


Constructor & Destructor Documentation

ucc::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.
ucc::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.
ucc::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.
ucc::Socket::address::address ( const char *  hostname,
unsigned  service = 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.
ucc::Socket::address::address ( const address reference)

Copy constructor.

Parameters:
referenceto object to copy from.
ucc::Socket::address::~address ( )

Destroy address.

Deallocate addrinfo structure.


Member Function Documentation

void ucc::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).
void ucc::Socket::address::add ( sockaddr *  address)

Add an individual socket address to our address list.

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

Duplicate a socket address.

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

Get the first socket address in our address list.

Returns:
first socket address or NULL if none.
int ucc::Socket::address::getfamily ( 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 addrinfo* ucc::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 435 of file socket.h.

unsigned ucc::Socket::address::insert ( 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 ucc::Socket::address::insert ( 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* ucc::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* ucc::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.
ucc::Socket::address::operator bool ( ) const [inline]

Test if the address list is valid.

Returns:
true if we have an address list.

Definition at line 456 of file socket.h.

ucc::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 442 of file socket.h.

ucc::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 470 of file socket.h.

bool ucc::Socket::address::operator! ( ) const [inline]

Test if we have no address list.

Returns:
true if we have no address list.

Definition at line 463 of file socket.h.

struct addrinfo* ucc::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 449 of file socket.h.

unsigned ucc::Socket::address::remove ( struct addrinfo *  address)

Remove members from another socket address list from ours.

Parameters:
addresslist to remove from list.
Returns:
count of addresses removed.
bool ucc::Socket::address::remove ( 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 ucc::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.
void ucc::Socket::address::set ( const char *  hostname,
unsigned  service = 0 
)

Set a socket address from host and service.

Parameters:
hostnameor address to use.
serviceport or 0.
void ucc::Socket::address::set ( struct sockaddr *  address)

Set an individual socket address for our address list.

Parameters:
addressto add.
void ucc::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.

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