ucommon  7.0.0
About: GNU uCommon C++ is a portable and optimized class framework for writing C++ applications that need to use threads and support concurrent synchronization, and that use sockets, XML parsing, object serialization, thread-optimized string and data structure classes, etc..
  Fossies Dox: ucommon-7.0.0.tar.gz  ("inofficial" and yet experimental doxygen-generated source code documentation)  

ucommon::Socket::address Class Reference

#include <socket.h>

Collaboration diagram for ucommon::Socket::address:
[legend]

Public Member Functions

 address (int family, const char *address, int type=SOCK_STREAM, int protocol=0)
 
 address (int family, const char *hostname, const char *service=NULL)
 
 address (const char *host, const char *service, int type=SOCK_STREAM)
 
 address (const char *hostname, in_port_t port=0)
 
 address (const in_addr &address, in_port_t port=0)
 
 address (const in6_addr &address, in_port_t port=0)
 
 address (const sockaddr &address)
 
 address (const addrinfo *address)
 
 address ()
 
 address (const address &reference)
 
addressoperator= (const address &rhs)
 
 ~address ()
 
bool operator== (const address &other) const
 
bool operator!= (const address &other) const
 
bool equals (const address &other) const
 
const struct sockaddr * get (void) const
 
struct sockaddr * modify (void)
 
const struct sockaddr * getAddr (void) const
 
const struct sockaddr * operator() (void) const
 
 operator struct sockaddr * ()
 
const struct sockaddr * get (int family) const
 
struct sockaddr * modify (int family)
 
const struct sockaddr * operator() (int family) const
 
 operator struct sockaddr_in * ()
 
int family (void) const
 
size_t getLength (void) const
 
in_port_t getPort (void) const
 
void setPort (in_port_t port)
 
address withPort (in_port_t port) const
 
struct sockaddr * find (const struct sockaddr *addr) const
 
struct addrinfogetList (void) const
 
 operator struct addrinfo * () const
 
struct addrinfooperator* () const
 
size_t print (char *dst, size_t dst_sz, bool port=false, bool force_brackets=false) const
 
 operator bool () const
 
bool is_valid () const
 
bool isValid () const
 
bool operator! () const
 
bool is_any () const
 
bool isAny () const
 
void setAny (int family=AF_UNSPEC)
 
bool is_loopback () const
 
bool isLoopback () const
 
void setLoopback (int family=AF_UNSPEC)
 
void clear (void)
 
void set (const char *hostname, const char *service=NULL, int type=SOCK_STREAM)
 
void add (const char *hostname, const char *service=NULL, int type=SOCK_STREAM)
 
void set (int family, const char *address, int type=SOCK_STREAM, int protocol=0)
 
void add (sockaddr *address)
 
unsigned insert (const struct addrinfo *address)
 
unsigned remove (const struct addrinfo *address)
 
bool remove (const struct sockaddr *address)
 
bool insert (const struct sockaddr *address)
 
void copy (const struct addrinfo *address)
 
void set (struct sockaddr *address)
 
void set (const char *hostname, in_port_t service=0)
 

Static Public Member Functions

static size_t getLength (const struct sockaddr *address)
 
static in_port_t getPort (const struct sockaddr *address)
 
static void setPort (struct sockaddr *address, in_port_t port)
 
static bool isAny (const struct sockaddr *address)
 
static void setAny (struct sockaddr *sa)
 
static sockaddr_storage any (int family)
 
static bool isLoopback (const struct sockaddr *address)
 
static void setLoopback (struct sockaddr *sa)
 
static sockaddr_storage loopback (int family)
 
static struct sockaddr * dup (struct sockaddr *address)
 
static struct sockaddr_in * ipv4 (struct sockaddr *address)
 
static size_t print (const struct sockaddr *src, char *dst, size_t dst_sz, bool port=false, bool ipv6_brackets=false)
 

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/10]

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.

Definition at line 850 of file socket.cpp.

References family(), ucommon::Socket::init(), list, set(), and ucommon::Socket::type().

Referenced by ucommon::Socket::family().

◆ address() [2/10]

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.

Definition at line 872 of file socket.cpp.

References addrinfo::ai_family, ucommon::Socket::family(), ucommon::getaddrinfo(), and ucommon::Socket::init().

◆ address() [3/10]

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.

Definition at line 929 of file socket.cpp.

References ucommon::Socket::type().

◆ address() [4/10]

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.

Definition at line 861 of file socket.cpp.

References ucommon::Socket::init(), and ucommon::Socket::port().

◆ address() [5/10]

ucommon::Socket::address::address ( const in_addr &  address,
in_port_t  port = 0 
)

Construct a socket address from an IPv4 address and a port number.

Definition at line 889 of file socket.cpp.

References ucommon::addr(), address(), insert(), and ucommon::Socket::port().

◆ address() [6/10]

ucommon::Socket::address::address ( const in6_addr &  address,
in_port_t  port = 0 
)

Construct a socket address from an IPv6 address and a port number.

Definition at line 898 of file socket.cpp.

References ucommon::addr(), address(), insert(), and ucommon::Socket::port().

◆ address() [7/10]

ucommon::Socket::address::address ( const sockaddr &  address)
inline

Construct a socket address from a sockaddr object.

Definition at line 426 of file socket.h.

◆ address() [8/10]

ucommon::Socket::address::address ( const addrinfo address)
inline

Construct a socket address from an addrinfo structure.

Definition at line 433 of file socket.h.

◆ address() [9/10]

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

Construct an empty address.

Definition at line 907 of file socket.cpp.

Referenced by address().

◆ address() [10/10]

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

Copy constructor.

Parameters
referenceto object to copy from.

Definition at line 912 of file socket.cpp.

References ucommon::Socket::copy(), and list.

◆ ~address()

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

Destroy address. Deallocate addrinfo structure.

Definition at line 935 of file socket.cpp.

References ucommon::clear().

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

Definition at line 1455 of file socket.cpp.

References ucommon::Socket::join(), and ucommon::Socket::query().

◆ add() [2/2]

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

Add an individual socket address to our address list.

Parameters
addressto add.

Definition at line 1437 of file socket.cpp.

References ucommon::addr(), buffer, ucommon::Socket::port(), and ucommon::Socket::query().

◆ any()

sockaddr_storage ucommon::Socket::address::any ( int  family)
static

Get a ADDR_ANY socket address of the given family.

Definition at line 1387 of file socket.cpp.

References ucommon::Socket::family().

Referenced by setAny().

◆ clear()

void ucommon::Socket::address::clear ( void  )

Clear current object.

Definition at line 940 of file socket.cpp.

References ucommon::freeaddrinfo().

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

Definition at line 1286 of file socket.cpp.

References ucommon::addr(), and ucommon::clear().

◆ dup()

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

Duplicate a socket address.

Parameters
addressto duplicate.
Returns
duplicate address object.

Definition at line 1420 of file socket.cpp.

References ucommon::addr(), and ucommon::Socket::len().

◆ equals()

bool ucommon::Socket::address::equals ( const address other) const
inline

Definition at line 470 of file socket.h.

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

Definition at line 1136 of file socket.cpp.

Referenced by address(), ucommon::tcpstream::tcpstream(), and ost::UDPSocket::UDPSocket().

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

Definition at line 1476 of file socket.cpp.

References ucommon::addr(), addrinfo::ai_addr, addrinfo::ai_next, and ucommon::Socket::equal().

◆ get() [1/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.

Definition at line 1186 of file socket.cpp.

References addrinfo::ai_addr, addrinfo::ai_next, and ucommon::Socket::family().

◆ get() [2/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.

Definition at line 1128 of file socket.cpp.

Referenced by ucommon::addr(), and ost::UDPSocket::connect().

◆ getAddr()

const struct sockaddr* ucommon::Socket::address::getAddr ( void  ) const
inline

Definition at line 482 of file socket.h.

◆ getLength() [1/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.

◆ getLength() [2/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.

Referenced by ost::UDPSocket::connect(), and ost::UDPReceive::connect().

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

Referenced by ucommon::addrinfo(), ucommon::linked_pointer< sockaddr_struct >::linked_pointer(), ucommon::linked_pointer< sockaddr_struct >::operator=(), and ucommon::linked_pointer< sockaddr_struct >::set().

◆ getPort() [1/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.

References ucommon::Socket::port().

◆ getPort() [2/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.

Referenced by ost::UDPDuplex::connect().

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

Definition at line 1233 of file socket.cpp.

References addrinfo::ai_addr, and addrinfo::ai_next.

Referenced by address(), ost::Socket::getLocal(), and ost::Socket::getPeer().

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

Definition at line 1255 of file socket.cpp.

References ucommon::addr(), addrinfo::ai_addr, addrinfo::ai_next, AI_NUMERICHOST, AI_NUMERICSERV, ucommon::Socket::equal(), ucommon::freeaddrinfo(), ucommon::getaddrinfo(), ucommon::Socket::port(), and ucommon::Socket::query().

◆ ipv4()

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.

Definition at line 1402 of file socket.cpp.

References ucommon::addr().

Referenced by ost::UDPSocket::getIPV4Peer().

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

◆ is_valid()

bool ucommon::Socket::address::is_valid ( void  ) const
inline

Definition at line 609 of file socket.h.

◆ isAny() [1/2]

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

Definition at line 634 of file socket.h.

Referenced by ost::UDPReceive::connect().

◆ isAny() [2/2]

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.

Definition at line 1311 of file socket.cpp.

◆ isLoopback() [1/2]

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

Definition at line 655 of file socket.h.

◆ isLoopback() [2/2]

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.

Definition at line 1329 of file socket.cpp.

References INADDR_LOOPBACK.

◆ isValid()

bool ucommon::Socket::address::isValid ( void  ) const
inline

Definition at line 613 of file socket.h.

◆ loopback()

sockaddr_storage ucommon::Socket::address::loopback ( int  family)
static

Get a ADDR_LOOPBACK socket address of the given family.

Definition at line 1395 of file socket.cpp.

References ucommon::Socket::family().

Referenced by setLoopback().

◆ modify() [1/2]

struct sockaddr * ucommon::Socket::address::modify ( int  family)

Definition at line 1170 of file socket.cpp.

References addrinfo::ai_addr, addrinfo::ai_next, and ucommon::Socket::family().

◆ modify() [2/2]

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

Definition at line 1120 of file socket.cpp.

◆ 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 struct sockaddr_in *()

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

Definition at line 511 of file socket.h.

◆ operator!()

bool ucommon::Socket::address::operator! ( void  ) 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!=()

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

Definition at line 466 of file socket.h.

◆ operator()() [1/2]

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

Definition at line 507 of file socket.h.

◆ operator()() [2/2]

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

Definition at line 486 of file socket.h.

◆ 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=()

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

Assignment operator.

Parameters
referenceto object to copy from.

Definition at line 919 of file socket.cpp.

References ucommon::Socket::copy(), and list.

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

Definition at line 1030 of file socket.cpp.

References addrinfo::ai_addr, addrinfo::ai_next, ucommon::Socket::equal(), and list.

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

References port.

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

Definition at line 1244 of file socket.cpp.

References addrinfo::ai_addr, and addrinfo::ai_next.

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

Definition at line 1208 of file socket.cpp.

References ucommon::addr(), addrinfo::ai_addr, addrinfo::ai_next, ucommon::Socket::equal(), and ucommon::freeaddrinfo().

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

Definition at line 1449 of file socket.cpp.

References ucommon::clear().

Referenced by address().

◆ set() [2/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.

Definition at line 1046 of file socket.cpp.

References ucommon::clear(), ucommon::Socket::port(), and ucommon::Socket::query().

◆ set() [3/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.

Definition at line 1062 of file socket.cpp.

References ucommon::addr(), addrinfo::ai_family, addrinfo::ai_flags, AI_PASSIVE, addrinfo::ai_protocol, addrinfo::ai_socktype, ucommon::clear(), ucommon::Socket::family(), ucommon::getaddrinfo(), strfree(), ucommon::Socket::type(), and ucommon::v6only.

◆ set() [4/4]

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

Set an individual socket address for our address list.

Parameters
addressto add.

Definition at line 1202 of file socket.cpp.

References ucommon::addr(), and ucommon::clear().

◆ setAny() [1/2]

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

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.

Definition at line 1347 of file socket.cpp.

References AF_UNSPEC, any(), ucommon::clear(), and ucommon::Socket::family().

◆ setAny() [2/2]

void ucommon::Socket::address::setAny ( struct sockaddr *  sa)
static

Set the socket address to ADDR_ANY: 0.0.0.0 or ::0

Definition at line 1365 of file socket.cpp.

References ucommon::Socket::family(), and ucommon::Socket::len().

◆ setLoopback() [1/2]

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

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.

Definition at line 1356 of file socket.cpp.

References AF_UNSPEC, ucommon::clear(), ucommon::Socket::family(), and loopback().

Referenced by ost::UDPReceive::connect().

◆ setLoopback() [2/2]

void ucommon::Socket::address::setLoopback ( struct sockaddr *  sa)
static

Set the socket address to ADDR_LOOPBACK: 127.0.0.1 or ::1 depending on the family of the pointed address.

Definition at line 1372 of file socket.cpp.

References INADDR_LOOPBACK.

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

Definition at line 1149 of file socket.cpp.

References addrinfo::ai_addr, addrinfo::ai_next, and ucommon::Socket::port().

◆ setPort() [2/2]

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.

Definition at line 1296 of file socket.cpp.

References ucommon::Socket::port().

◆ withPort()

Socket::address ucommon::Socket::address::withPort ( in_port_t  port) const

Returns a copy of this address list with the specified port set.

Definition at line 1163 of file socket.cpp.

References ucommon::Socket::copy(), and ucommon::Socket::port().

Referenced by ost::UDPDuplex::connect().

Member Data Documentation

◆ list

struct addrinfo* ucommon::Socket::address::list
protected

Definition at line 367 of file socket.h.

Referenced by address(), operator=(), and operator==().


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