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, unsigned service=0)
 Construct a socket address from host and service.
 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.
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 addrinfogetList (void) const
 Get the full socket address list from the object.
unsigned insert (struct addrinfo *address)
 Insert unique members from another socket address list to ours.
bool insert (struct sockaddr *address)
 Insert an individual socket address to our address list only if unique.
 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 addrinfooperator* () const
 Return the full socket address list by pointer reference.
unsigned remove (struct addrinfo *address)
 Remove members from another socket address list from ours.
bool remove (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, unsigned service=0)
 Set a socket address from host and service.
 ~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 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 345 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)
staticread

Duplicate a socket address.

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

Get the first socket address in our address list.

Returns
first socket address or NULL if none.
int ucommon::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* ucommon::Socket::address::getList ( void  ) const
inlineread

Get the full socket address list from the object.

Returns
addrinfo list we resolved or NULL if none.

Definition at line 439 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)
staticread

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)
staticread

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 460 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 446 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 474 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 467 of file socket.h.

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

Return the full socket address list by pointer reference.

Returns
addrinfo list we resolved or NULL if none.

Definition at line 453 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: