UCommon
|
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 () | |
Construct an empty address. | |
address (const address &reference) | |
Copy constructor. | |
address (const addrinfo *address) | |
Construct a socket address from an addrinfo structure. | |
address (const char *host, const char *service, int type=SOCK_STREAM) | |
Construct a socket address list for a service. | |
address (const char *hostname, in_port_t port=0) | |
Construct a socket address from host and service. | |
address (const in6_addr &address, in_port_t port=0) | |
Construct a socket address from an IPv6 address and a port number. | |
address (const in_addr &address, in_port_t port=0) | |
Construct a socket address from an IPv4 address and a port number. | |
address (const sockaddr &address) | |
Construct a socket address from a sockaddr object. | |
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=NULL) | |
Construct a socket address for an existing socket. | |
void | clear (void) |
Clear current object. | |
void | copy (const struct addrinfo *address) |
Copy an existing addrinfo into our object. | |
bool | equals (const address &other) const |
int | family (void) const |
Get the family of the first member in a list of services. | |
struct sockaddr * | find (const struct sockaddr *addr) const |
Find a specific socket address in our address list. | |
const struct sockaddr * | get (int family) const |
Get the first socket address of specified family from our list. | |
const struct sockaddr * | get (void) const |
Get the first socket address in our address list. | |
const struct sockaddr * | getAddr (void) const |
size_t | getLength (void) const |
Get the address size of the first address. | |
struct addrinfo * | getList (void) const |
Get the full socket address list from the object. | |
in_port_t | getPort (void) const |
Get the port of the first address . | |
unsigned | insert (const struct addrinfo *address) |
Insert unique members from another socket address list to ours. | |
bool | insert (const struct sockaddr *address) |
Insert an individual socket address to our address list only if unique. | |
bool | is_any () const |
Test if the first socket address is ADDR_ANY: 0.0.0.0 or ::0. | |
bool | is_loopback () const |
Test if the first socket address is ADDR_LOOPBACK: 127.0.0.1 or ::1. | |
bool | is_valid () const |
bool | isAny () const |
bool | isLoopback () const |
bool | isValid () const |
struct sockaddr * | modify (int family) |
struct sockaddr * | modify (void) |
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 * () | |
Get the first socket address by casted reference. | |
operator struct sockaddr_in * () | |
operator struct sockaddr_in6 * () | |
bool | operator! () const |
Test if we have no address list. | |
bool | operator!= (const address &other) const |
const struct sockaddr * | operator() (int family) const |
const struct sockaddr * | operator() (void) const |
struct addrinfo * | operator* () const |
Return the full socket address list by pointer reference. | |
address & | operator= (const address &rhs) |
Assignment operator. | |
bool | operator== (const address &other) const |
Compare two address lists. | |
size_t | print (char *dst, size_t dst_sz, bool port=false, bool force_brackets=false) const |
Print the first socket address as a human-readable string to the provided buffer and returns the printed string length. | |
unsigned | remove (const struct addrinfo *address) |
Remove members from another socket address list from ours. | |
bool | remove (const struct sockaddr *address) |
Remove an individual socket address from our address list. | |
void | set (const char *hostname, const char *service=NULL, int type=SOCK_STREAM) |
Set the host addresses to form a new list. | |
void | set (const char *hostname, in_port_t service=0) |
Set a socket address from host and service. | |
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 | setAny (int family=0) |
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). | |
void | setLoopback (int family=0) |
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). | |
void | setPort (in_port_t port) |
Set the port of all addresses in the list. | |
address | withPort (in_port_t port) const |
Returns a copy of this address list with the specified port set. | |
~address () | |
Destroy address. | |
Static Public Member Functions | |
static sockaddr_storage | any (int family) |
Get a ADDR_ANY socket address of the given family. | |
static struct sockaddr * | dup (struct sockaddr *address) |
Duplicate a socket address. | |
static size_t | getLength (const struct sockaddr *address) |
Returns the size of the socket address according to the family. | |
static in_port_t | getPort (const struct sockaddr *address) |
Returns the port of the 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. | |
static bool | isAny (const struct sockaddr *address) |
Test if the socket address is ADDR_ANY: 0.0.0.0 or ::0. | |
static bool | isLoopback (const struct sockaddr *address) |
Test if the socket address is ADDR_LOOPBACK: 127.0.0.1 or ::1. | |
static sockaddr_storage | loopback (int family) |
Get a ADDR_LOOPBACK socket address of the given family. | |
static size_t | print (const struct sockaddr *src, char *dst, size_t dst_sz, bool port=false, bool ipv6_brackets=false) |
Print socket address as a human-readable string to the provided buffer and returns the printed string length. | |
static void | setAny (struct sockaddr *sa) |
Set the socket address to ADDR_ANY: 0.0.0.0 or ::0. | |
static void | setLoopback (struct sockaddr *sa) |
Set the socket address to ADDR_LOOPBACK: 127.0.0.1 or ::1 depending on the family of the pointed address. | |
static void | setPort (struct sockaddr *address, in_port_t port) |
Set the port of the socket address. | |
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. |
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. |
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. |
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. |
|
inline |
|
inline |
ucommon::Socket::address::address | ( | const address & | reference | ) |
Copy constructor.
reference | to object to copy from. |
ucommon::Socket::address::~address | ( | ) |
Destroy address.
Deallocate addrinfo structure.
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). |
void ucommon::Socket::address::add | ( | sockaddr * | address | ) |
Add an individual socket address to our address list.
address | to 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.
address | list to copy from. |
|
static |
Duplicate a socket address.
address | to duplicate. |
|
inline |
int ucommon::Socket::address::family | ( | void | ) | const |
Get the family of the first member in a list of services.
struct sockaddr * ucommon::Socket::address::find | ( | const struct sockaddr * | addr | ) | const |
Find a specific socket address in our address list.
const struct sockaddr * ucommon::Socket::address::get | ( | int | family | ) | const |
Get the first socket address of specified family from our list.
family | to seek. |
const struct sockaddr * ucommon::Socket::address::get | ( | void | ) | const |
Get the first socket address in our address list.
|
inline |
|
inlinestatic |
|
inline |
|
inline |
|
inlinestatic |
|
inline |
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. |
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. |
|
static |
Convert address object into ipv4 address.
address | to convert. |
|
static |
Convert address object into ipv6 address.
address | to convert. |
|
inline |
|
inline |
|
static |
Test if the socket address is ADDR_ANY: 0.0.0.0 or ::0.
|
static |
Test if the socket address is ADDR_LOOPBACK: 127.0.0.1 or ::1.
|
inline |
|
inline |
|
inline |
|
inline |
|
inline |
|
inline |
|
inline |
|
inline |
|
inline |
|
inline |
Assignment operator.
reference | to object to copy from. |
bool ucommon::Socket::address::operator== | ( | const address & | other | ) | const |
Compare two address lists.
|
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. |
|
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. |
bool ucommon::Socket::address::remove | ( | const struct sockaddr * | address | ) |
Remove an individual socket address from our address list.
address | to remove. |
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. |
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. |
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. |
void ucommon::Socket::address::set | ( | struct sockaddr * | address | ) |
Set an individual socket address for our address list.
address | to add. |
void ucommon::Socket::address::setAny | ( | int | family = 0 | ) |
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. |
void ucommon::Socket::address::setLoopback | ( | int | family = 0 | ) |
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. |
void ucommon::Socket::address::setPort | ( | in_port_t | port | ) |
Set the port of all addresses in the list.
port | the port to set. |
|
static |
Set the port of the socket address.
address | to edit. |
port | to associate to the socket address. |