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. 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=((void *) 0))
 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, unsigned service=0)
 Construct a socket address from host and service. More...
 
 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...
 
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...
 
struct sockaddr * get (void) const
 Get the first socket address in our address list. More...
 
struct sockaddr * get (int family) const
 Get the first socket address of specified family from our list. More...
 
struct sockaddr * getAddr (void) const
 
struct addrinfogetList (void) const
 Get the full socket address list from the object. More...
 
unsigned insert (struct addrinfo *address)
 Insert unique members from another socket address list to ours. More...
 
bool insert (struct sockaddr *address)
 Insert an individual socket address to our address list only if unique. 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 * () const
 Get the first socket address by casted reference. More...
 
 operator struct sockaddr_in * () const
 
 operator struct sockaddr_in6 * () const
 
bool operator! () const
 Test if we have no address list. More...
 
struct sockaddr * operator() (void) const
 
struct sockaddr * operator() (int family) const
 
struct addrinfooperator* () const
 Return the full socket address list by pointer reference. More...
 
unsigned remove (struct addrinfo *address)
 Remove members from another socket address list from ours. More...
 
bool remove (struct sockaddr *address)
 Remove an individual socket address from our address list. More...
 
void set (const char *hostname, const char *service=((void *) 0), 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, unsigned service=0)
 Set a socket address from host and service. More...
 
 ~address ()
 Destroy address. More...
 

Static Public Member Functions

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

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 343 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,
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.
ucommon::Socket::address::address ( const address reference)

Copy constructor.

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

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

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

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

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

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

unsigned ucommon::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 ucommon::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* 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.
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.
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 482 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 468 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 424 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 489 of file socket.h.

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

unsigned ucommon::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 ucommon::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 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,
unsigned  service = 0 
)

Set a socket address from host and service.

Parameters
hostnameor address to use.
serviceport or 0.

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