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..
![]() ![]() |
#include <socket.h>
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) | |
address & | operator= (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 addrinfo * | getList (void) const |
operator struct addrinfo * () const | |
struct addrinfo * | operator* () 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 addrinfo * | list |
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.
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.
family | of socket address. Needed when host names are used. |
address | or hostname. |
type | of socket (stream, dgram, etc). |
protocol | number 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().
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.
family | of hosts to filter by or AF_UNSPEC. |
hostname | or ip address. The socket family is used for hostnames. |
service | port 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().
ucommon::Socket::address::address | ( | const char * | host, |
const char * | service, | ||
int | type = SOCK_STREAM |
||
) |
Construct a socket address list for a service.
host | address for service. |
service | name or port number. |
type | of service, stream, dgram, etc. |
Definition at line 929 of file socket.cpp.
References ucommon::Socket::type().
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.
hostname | or address to use. |
service | port or 0. |
Definition at line 861 of file socket.cpp.
References ucommon::Socket::init(), and ucommon::Socket::port().
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().
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().
|
inline |
|
inline |
ucommon::Socket::address::address | ( | ) |
ucommon::Socket::address::address | ( | const address & | reference | ) |
Copy constructor.
reference | to object to copy from. |
Definition at line 912 of file socket.cpp.
References ucommon::Socket::copy(), and list.
ucommon::Socket::address::~address | ( | ) |
Destroy address. Deallocate addrinfo structure.
Definition at line 935 of file socket.cpp.
References ucommon::clear().
void ucommon::Socket::address::add | ( | const char * | hostname, |
const char * | service = NULL , |
||
int | type = SOCK_STREAM |
||
) |
Append additional host addresses to our list.
hostname | or address to resolve. |
service | name or port number, or NULL if not used. |
type | of socket (stream or dgram). |
Definition at line 1455 of file socket.cpp.
References ucommon::Socket::join(), and ucommon::Socket::query().
void ucommon::Socket::address::add | ( | sockaddr * | address | ) |
Add an individual socket address to our address list.
address | to add. |
Definition at line 1437 of file socket.cpp.
References ucommon::addr(), buffer, ucommon::Socket::port(), and ucommon::Socket::query().
|
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().
void ucommon::Socket::address::clear | ( | void | ) |
Clear current object.
Definition at line 940 of file socket.cpp.
References ucommon::freeaddrinfo().
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.
address | list to copy from. |
Definition at line 1286 of file socket.cpp.
References ucommon::addr(), and ucommon::clear().
|
static |
Duplicate a socket address.
address | to duplicate. |
Definition at line 1420 of file socket.cpp.
References ucommon::addr(), and ucommon::Socket::len().
|
inline |
int ucommon::Socket::address::family | ( | void | ) | const |
Get the family of the first member in a list of services.
Definition at line 1136 of file socket.cpp.
Referenced by address(), ucommon::tcpstream::tcpstream(), and ost::UDPSocket::UDPSocket().
struct sockaddr * ucommon::Socket::address::find | ( | const struct sockaddr * | addr | ) | const |
Find a specific socket address in our address list.
Definition at line 1476 of file socket.cpp.
References ucommon::addr(), addrinfo::ai_addr, addrinfo::ai_next, and ucommon::Socket::equal().
const struct sockaddr * ucommon::Socket::address::get | ( | int | family | ) | const |
Get the first socket address of specified family from our list.
family | to seek. |
Definition at line 1186 of file socket.cpp.
References addrinfo::ai_addr, addrinfo::ai_next, and ucommon::Socket::family().
const struct sockaddr * ucommon::Socket::address::get | ( | void | ) | const |
Get the first socket address in our address list.
Definition at line 1128 of file socket.cpp.
Referenced by ucommon::addr(), and ost::UDPSocket::connect().
|
inline |
|
inlinestatic |
|
inline |
Get the address size of the first address.
Definition at line 531 of file socket.h.
Referenced by ost::UDPSocket::connect(), and ost::UDPReceive::connect().
|
inline |
Get the full socket address list from the object.
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().
|
inlinestatic |
Returns the port of the socket address.
Definition at line 764 of file socket.h.
References ucommon::Socket::port().
|
inline |
Get the port of the first address .
Definition at line 539 of file socket.h.
Referenced by ost::UDPDuplex::connect().
unsigned ucommon::Socket::address::insert | ( | const struct addrinfo * | address | ) |
Insert unique members from another socket address list to ours.
address | list to insert into list. |
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().
bool ucommon::Socket::address::insert | ( | const struct sockaddr * | address | ) |
Insert an individual socket address to our address list only if unique.
address | to insert into list. |
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().
|
static |
Convert address object into ipv4 address.
address | to convert. |
Definition at line 1402 of file socket.cpp.
References ucommon::addr().
Referenced by ost::UDPSocket::getIPV4Peer().
|
inline |
|
inline |
|
inline |
|
inline |
Definition at line 634 of file socket.h.
Referenced by ost::UDPReceive::connect().
|
static |
Test if the socket address is ADDR_ANY: 0.0.0.0 or ::0
Definition at line 1311 of file socket.cpp.
|
static |
Test if the socket address is ADDR_LOOPBACK: 127.0.0.1 or ::1
Definition at line 1329 of file socket.cpp.
References INADDR_LOOPBACK.
|
inline |
|
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().
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().
struct sockaddr * ucommon::Socket::address::modify | ( | void | ) |
Definition at line 1120 of file socket.cpp.
|
inline |
|
inline |
|
inline |
|
inline |
|
inline |
|
inline |
|
inline |
|
inline |
|
inline |
Socket::address & ucommon::Socket::address::operator= | ( | const address & | rhs | ) |
Assignment operator.
reference | to object to copy from. |
Definition at line 919 of file socket.cpp.
References ucommon::Socket::copy(), and list.
bool ucommon::Socket::address::operator== | ( | const address & | other | ) | const |
Compare two address lists.
Definition at line 1030 of file socket.cpp.
References addrinfo::ai_addr, addrinfo::ai_next, ucommon::Socket::equal(), and list.
|
inline |
Print the first socket address as a human-readable string to the provided buffer and returns the printed string length.
src | Address to print. |
dst | Destination buffer to print the socket address on. |
dst_sz | Size of the provided buffer. Strongly recommended to be at least INET6_ADDRSTRLEN (or INET_ADDRSTRLEN if compiled without IPv6). |
port | If true, print port number with address. If true, ipv6_brackets will also be forced to true. |
ipv6_brackets | If true, force printing IPv6 brackets. Ignored if address is an IPv4. |
Definition at line 597 of file socket.h.
References port.
|
static |
Print socket address as a human-readable string to the provided buffer and returns the printed string length.
src | Address to print. |
dst | Destination buffer to print the socket address on. |
dst_sz | Size of the provided buffer. Strongly recommended to be at least INET6_ADDRSTRLEN (or INET_ADDRSTRLEN if compiled without IPv6). |
port | If true, print port number with address. If true, ipv6_brackets will also be forced to true. |
ipv6_brackets | If true, force printing IPv6 brackets. Ignored if address is an IPv4. |
unsigned ucommon::Socket::address::remove | ( | const struct addrinfo * | address | ) |
Remove members from another socket address list from ours.
address | list to remove from list. |
Definition at line 1244 of file socket.cpp.
References addrinfo::ai_addr, and addrinfo::ai_next.
bool ucommon::Socket::address::remove | ( | const struct sockaddr * | address | ) |
Remove an individual socket address from our address list.
address | to remove. |
Definition at line 1208 of file socket.cpp.
References ucommon::addr(), addrinfo::ai_addr, addrinfo::ai_next, ucommon::Socket::equal(), and ucommon::freeaddrinfo().
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.
hostname | or address to resolve. |
service | name or port number, or NULL if not used. |
type | of socket (stream or dgram) to filter list by. |
Definition at line 1449 of file socket.cpp.
References ucommon::clear().
Referenced by address().
void ucommon::Socket::address::set | ( | const char * | hostname, |
in_port_t | service = 0 |
||
) |
Set a socket address from host and service.
hostname | or address to use. |
service | port or 0. |
Definition at line 1046 of file socket.cpp.
References ucommon::clear(), ucommon::Socket::port(), and ucommon::Socket::query().
void ucommon::Socket::address::set | ( | int | family, |
const char * | address, | ||
int | type = SOCK_STREAM , |
||
int | protocol = 0 |
||
) |
Set an entry for host binding.
family | of socket address. Needed when hostnames are used. |
address | or hostname. |
type | of socket (stream, dgram, etc). |
protocol | number 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.
void ucommon::Socket::address::set | ( | struct sockaddr * | address | ) |
Set an individual socket address for our address list.
address | to add. |
Definition at line 1202 of file socket.cpp.
References ucommon::addr(), and ucommon::clear().
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).
family | address family to set. |
Definition at line 1347 of file socket.cpp.
References AF_UNSPEC, any(), ucommon::clear(), and ucommon::Socket::family().
|
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().
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).
family | address 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().
|
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.
void ucommon::Socket::address::setPort | ( | in_port_t | port | ) |
Set the port of all addresses in the list.
port | the port to set. |
Definition at line 1149 of file socket.cpp.
References addrinfo::ai_addr, addrinfo::ai_next, and ucommon::Socket::port().
|
static |
Set the port of the socket address.
address | to edit. |
port | to associate to the socket address. |
Definition at line 1296 of file socket.cpp.
References ucommon::Socket::port().
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().
|
protected |
Definition at line 367 of file socket.h.
Referenced by address(), operator=(), and operator==().