UCommon
Data Structures | Public Types | Public Member Functions | Static Public Member Functions | Protected Attributes | Friends
ucommon::Socket Class Reference

A generic socket base class. More...

#include <socket.h>

Inheritance diagram for ucommon::Socket:
Inheritance graph
[legend]

Data Structures

class  address
 A generic socket address class. More...
 

Public Types

typedef cidr cidr_t
 
typedef struct hostaddr_internet host_t
 

Public Member Functions

int blocking (bool enable)
 Set socket blocking I/O mode. More...
 
int broadcast (bool enable)
 Set socket for unicast mode broadcasts. More...
 
void cancel (void)
 Cancel pending i/o by shutting down the socket.
 
bool ccid (uint8_t id)
 Set ccid of dccp socket. More...
 
bool connected (void) const
 Test if socket is connected. More...
 
int connectto (struct addrinfo *list)
 Connect our socket to a remote host from an address list. More...
 
int disconnect (void)
 Disconnect a connected socket. More...
 
int drop (const struct addrinfo *list, const int ifindex=0)
 Drop socket from multicast group. More...
 
int err (void) const
 Get error code.
 
int getError (void) const
 Get socket error code. More...
 
bool is_pending (unsigned value)
 See the number of bytes in the receive queue. More...
 
int join (const struct addrinfo *list, const int ifindex=0)
 Join socket to multicast group. More...
 
int keepalive (bool enable)
 Set socket for keepalive packets. More...
 
int loopback (bool enable)
 Set loopback to read multicast packets we broadcast. More...
 
int multicast (unsigned ttl=1)
 Set multicast mode and multicast broadcast range. More...
 
int nodelay (void) const
 Set nodelay option for tcp socket. More...
 
socket_t operator * () const
 Get the socket descriptor by pointer reference. More...
 
 operator bool () const
 Test if socket is valid. More...
 
 operator socket_t () const
 Get the socket descriptor by casting. More...
 
bool operator! () const
 Test if socket is invalid. More...
 
Socketoperator= (socket_t socket)
 Assign socket from a socket descriptor. More...
 
size_t peek (void *data, size_t number) const
 Peek at data waiting in the socket receive buffer. More...
 
unsigned pending (void) const
 Get the number of bytes of data in the socket receive buffer. More...
 
size_t printf (const char *format,...)
 Print formatted string to socket. More...
 
int priority (int scheduling)
 Set packet priority, 0 to 6 unless privileged. More...
 
size_t readfrom (void *data, size_t number, struct sockaddr_storage *address=NULL)
 Read data from the socket receive buffer. More...
 
size_t readline (char *data, size_t size)
 Read a newline of text data from the socket and save in NULL terminated string. More...
 
size_t readline (String &buffer)
 Read a string of input from the socket and strip trailing newline. More...
 
stringref_t readline (size_t maxsize)
 
int recvsize (unsigned size)
 Set the size of the socket receive buffer. More...
 
void release (void)
 Shutdown and close the socket.
 
unsigned segsize (unsigned size)
 Set segment size and get mtu of a socket. More...
 
int sendsize (unsigned size)
 Set the size of the socket send buffer. More...
 
int sendwait (unsigned size)
 Set the size to wait before sending. More...
 
void shutdown (void)
 Shutdown the socket communication channel.
 
 Socket ()
 Create a socket object for use.
 
 Socket (const Socket &existing)
 Create socket as duped handle of existing socket. More...
 
 Socket (socket_t socket)
 Create socket from existing socket descriptor. More...
 
 Socket (const struct addrinfo *address)
 Create and connect a socket to an address from an address list. More...
 
 Socket (int family, int type, int protocol=0)
 Create an unbound socket of a specific type. More...
 
 Socket (const char *address, const char *port, int family=0, int type=0, int protocol=0)
 Create a bound socket. More...
 
int tos (int type)
 Set the type of service field of outgoing packets. More...
 
int ttl (uint8_t time)
 Set the time to live before packets expire. More...
 
int type (void) const
 Get the type of a socket. More...
 
bool wait (timeout_t timeout=0) const
 Test for pending input data. More...
 
int wait (timeout_t timeout=Timer::inf)
 Socket i/o timer setting. More...
 
bool waitSending (timeout_t timeout=0) const
 Test for output data sent. More...
 
size_t writes (const char *string)
 Write a null terminated string to the socket. More...
 
size_t writeto (const void *data, size_t number, const struct sockaddr *address=NULL)
 Write data to the socket send buffer. More...
 
virtual ~Socket ()
 Shutdown, close, and destroy socket.
 

Static Public Member Functions

static socket_t acceptfrom (socket_t socket, struct sockaddr_storage *address=NULL)
 Accept a socket connection from a remote host. More...
 
static int bindto (socket_t socket, const char *address, const char *service, int protocol=0)
 Bind the socket descriptor to a known interface and service port. More...
 
static int bindto (socket_t socket, const struct sockaddr *address)
 Bind the socket descriptor to a known interface. More...
 
static int blocking (socket_t socket, bool enable)
 Set socket blocking I/O mode of socket descriptor. More...
 
static int broadcast (socket_t socket, bool enable)
 Set socket for unicast mode broadcasts on socket descriptor. More...
 
static void cancel (socket_t socket)
 Cancel pending i/o by shutting down the socket. More...
 
static bool ccid (socket_t socket, uint8_t id)
 Set congestion control id. More...
 
static int connectto (socket_t socket, struct addrinfo *list)
 Connect socket descriptor to a remote host from an address list. More...
 
static unsigned copy (struct sockaddr *target, const struct sockaddr *origin)
 Copy a socket address. More...
 
static socket_t create (int family, int type, int protocol)
 Create a socket object unbound. More...
 
static socket_t create (const struct addrinfo *address, int type, int protocol)
 Create a connected socket. More...
 
static socket_t create (const char *iface, const char *service, int family=0, int type=0, int protocol=0)
 Create a bound socket for a service. More...
 
static socket_t create (const Socket::address &address)
 Create a connected socket for a service. More...
 
static int disconnect (socket_t socket)
 Disconnect a connected socket descriptor. More...
 
static int drop (socket_t socket, const struct addrinfo *list, const int ifindex=0)
 Drop socket descriptor from multicast group. More...
 
static bool eq_from (const struct sockaddr_storage *address1, const struct sockaddr_storage *address2)
 Compare socket addresses. More...
 
static bool eq_host (const struct sockaddr *address1, const struct sockaddr *address2)
 Compare socket host addresses. More...
 
static bool eq_inet (const struct sockaddr_internet *address1, const struct sockaddr_internet *address2)
 Compare socket addresses. More...
 
static bool eq_subnet (const struct sockaddr *address1, const struct sockaddr *address2)
 See if both addresses are in the same subnet. More...
 
static bool equal (const struct sockaddr *address1, const struct sockaddr *address2)
 Compare socket addresses. More...
 
static int error (const socket_t socket)
 Get socket error code of socket descriptor. More...
 
static int error (void)
 Return error code of last socket operation,. More...
 
static int family (socket_t socket)
 Get the address family of the socket descriptor. More...
 
static int family (const struct sockaddr_storage &address)
 Get the address family of a socket address object. More...
 
static int family (const struct sockaddr_internet &address)
 Get the address family of an internet socket address object. More...
 
static struct addrinfohinting (socket_t socket, struct addrinfo *hint)
 Create an address info lookup hint based on the family and type properties of a socket descriptor. More...
 
static char * hostname (const struct sockaddr *address, char *buffer, size_t size)
 Lookup and return the host name associated with a socket address. More...
 
static void init (void)
 Initialize socket subsystem.
 
static bool is_null (const char *string)
 Simple function to validate that a given IP address string is a "zero" address. More...
 
static bool is_numeric (const char *string)
 Simple function to validate that a given IP address string is a numeric address. More...
 
static int join (socket_t socket, const struct addrinfo *list, const int ifindex=0)
 Join socket descriptor to multicast group. More...
 
static int keepalive (socket_t socket, bool enable)
 Set socket for keepalive packets for socket descriptor. More...
 
static unsigned keyhost (const struct sockaddr *address, unsigned size)
 Convert a socket host address into a hash map index. More...
 
static unsigned keyindex (const struct sockaddr *address, unsigned size)
 Convert a socket address and service into a hash map index. More...
 
static socklen_t len (const struct sockaddr *address)
 Get the size of a socket address. More...
 
static int listento (socket_t socket, const struct sockaddr *address, int backlog=5)
 Bind the socket descriptor to a known interface listen on service port. More...
 
static int local (socket_t socket, struct sockaddr_storage *address)
 Get local address to which the socket is bound. More...
 
static int loopback (socket_t socket, bool enable)
 Set loopback to read multicast packets socket descriptor broadcasts. More...
 
static int multicast (socket_t socket, unsigned ttl=1)
 Set multicast mode and multicast broadcast range for socket descriptor. More...
 
static int nodelay (socket_t socket)
 Set tcp nodelay option on socket descriptor. More...
 
static unsigned pending (socket_t socket)
 Get the number of bytes pending in the receive buffer of a socket descriptor. More...
 
static in_port_t port (const struct sockaddr *address)
 Get the service port of a socket. More...
 
static in_port_t port (const struct sockaddr_internet *address)
 Get the service port of an inet socket. More...
 
static ssize_t printf (socket_t socket, const char *format,...)
 Print formatted string to socket. More...
 
static int priority (socket_t socket, int scheduling)
 Set packet priority of socket descriptor. More...
 
static struct addrinfoquery (const char *host, const char *service, int type=SOCK_STREAM, int protocol=0)
 Get an address list directly. More...
 
static socklen_t query (socket_t socket, struct sockaddr_storage *address, const char *hostname, const char *service)
 Lookup a host name and service address based on the addressing family and socket type of a socket descriptor. More...
 
static char * query (const struct sockaddr *address, char *buffer, socklen_t size)
 Get the hostname of a socket address. More...
 
static void query (int family)
 Set default socket family preference for query options when the socket type is otherwise not specified. More...
 
static ssize_t readline (socket_t socket, char *data, size_t size, timeout_t timeout=Timer::inf)
 Read a newline of text data from the socket and save in NULL terminated string. More...
 
static ssize_t recvfrom (socket_t socket, void *buffer, size_t size, int flags=0, struct sockaddr_storage *address=NULL)
 Get data waiting in receive queue. More...
 
static int recvsize (socket_t socket, unsigned size)
 Set the receive size of a socket descriptor. More...
 
static void release (struct addrinfo *list)
 Release an address list directly. More...
 
static void release (socket_t socket)
 Release (close) a socket. More...
 
static int remote (socket_t socket, struct sockaddr_storage *address)
 Get remote address to which the socket is connected. More...
 
static ssize_t replyto (socket_t socket, const void *buffer, size_t size, int flags, const struct sockaddr_storage *address)
 Send reply on socket. More...
 
static unsigned segsize (socket_t socket, unsigned size=0)
 Set segment size and get MTU. More...
 
static int sendsize (socket_t socket, unsigned size)
 Set the send size of a socket descriptor. More...
 
static ssize_t sendto (socket_t socket, const void *buffer, size_t size, int flags=0, const struct sockaddr *address=NULL)
 Send data on socket. More...
 
static int sendwait (socket_t socket, unsigned size)
 Set the size to wait before sending. More...
 
static unsigned store (struct sockaddr_storage *storage, const struct sockaddr *address)
 Store an address into an address object. More...
 
static unsigned store (struct sockaddr_internet *storage, const struct sockaddr *address)
 Store an address into an internet address object. More...
 
static int tos (socket_t socket, int type)
 Set type of service of socket descriptor. More...
 
static int ttl (socket_t socket, uint8_t time)
 Set the time to live for the socket descriptor. More...
 
static int type (const socket_t socket)
 Get the type of a socket. More...
 
static void v4mapping (bool enable)
 Set the default socket behavior for v6-v4 mapping. More...
 
static int via (struct sockaddr *address, const struct sockaddr *target, socklen_t size=0)
 Get the socket address of the interface needed to reach a destination address. More...
 
static bool wait (socket_t socket, timeout_t timeout=0)
 Test for pending input data. More...
 

Protected Attributes

int ioerr
 
timeout_t iowait
 
socket_t so
 

Friends

class address
 

Detailed Description

A generic socket base class.

This class can be used directly or as a base class for building network protocol stacks. This common base tries to handle UDP and TCP sockets, as well as support multicast, IPV4/IPV6 addressing, and additional addressing domains (such as Unix domain sockets).

Author
David Sugar dyfet.nosp@m.@gnu.nosp@m.telep.nosp@m.hony.nosp@m..org

Definition at line 327 of file socket.h.

Constructor & Destructor Documentation

◆ Socket() [1/5]

ucommon::Socket::Socket ( const Socket existing)

Create socket as duped handle of existing socket.

Parameters
existingsocket to dup.

◆ Socket() [2/5]

ucommon::Socket::Socket ( socket_t  socket)

Create socket from existing socket descriptor.

Parameters
socketdescriptor to use.

◆ Socket() [3/5]

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

Create and connect a socket to an address from an address list.

The type of socket created is based on the type we are connecting to.

Parameters
addresslist to connect with.

◆ Socket() [4/5]

ucommon::Socket::Socket ( int  family,
int  type,
int  protocol = 0 
)

Create an unbound socket of a specific type.

Parameters
familyof our new socket.
type(stream, udp, etc) of our new socket.
protocolnumber of our new socket.'

◆ Socket() [5/5]

ucommon::Socket::Socket ( const char *  address,
const char *  port,
int  family = 0,
int  type = 0,
int  protocol = 0 
)

Create a bound socket.

If one wishes to listen for connections on a protocol, then ListenSocket should be used instead.

Parameters
addressto bind or "*" for all.
portnumber of service to bind.
familyto bind as.
typeof socket to bind (stream, udp, etc).
protocolof socket to bind.

Member Function Documentation

◆ acceptfrom()

static socket_t ucommon::Socket::acceptfrom ( socket_t  socket,
struct sockaddr_storage *  address = NULL 
)
static

Accept a socket connection from a remote host.

Parameters
socketdescriptor to accept from.
addressof socket accepting.
Returns
new socket accepted.

◆ bindto() [1/2]

static int ucommon::Socket::bindto ( socket_t  socket,
const char *  address,
const char *  service,
int  protocol = 0 
)
static

Bind the socket descriptor to a known interface and service port.

Parameters
socketdescriptor to bind.
addressto bind to or "*" for all.
serviceport to bind.
protocolto use or 0 if default.
Returns
0 on success, -1 if error.

◆ bindto() [2/2]

static int ucommon::Socket::bindto ( socket_t  socket,
const struct sockaddr *  address 
)
static

Bind the socket descriptor to a known interface.

Parameters
socketdescriptor to bind.
addressof interface to bind to.
Returns
0 on success, -1 if error.

◆ blocking() [1/2]

int ucommon::Socket::blocking ( bool  enable)
inline

Set socket blocking I/O mode.

Parameters
enabletrue for blocking I/O.
Returns
0 on success, -1 if error.

Definition at line 1000 of file socket.h.

◆ blocking() [2/2]

static int ucommon::Socket::blocking ( socket_t  socket,
bool  enable 
)
static

Set socket blocking I/O mode of socket descriptor.

Parameters
socketdescriptor.
enabletrue for blocking I/O.
Returns
0 if success, -1 if error.

◆ broadcast() [1/2]

int ucommon::Socket::broadcast ( bool  enable)
inline

Set socket for unicast mode broadcasts.

Parameters
enablebroadcasting if true.
Returns
0 on success, -1 if error.

Definition at line 982 of file socket.h.

◆ broadcast() [2/2]

static int ucommon::Socket::broadcast ( socket_t  socket,
bool  enable 
)
static

Set socket for unicast mode broadcasts on socket descriptor.

Parameters
socketdescriptor.
enablebroadcasting if true.
Returns
0 if success, -1 if error.

◆ cancel()

static void ucommon::Socket::cancel ( socket_t  socket)
static

Cancel pending i/o by shutting down the socket.

Parameters
socketto shutdown.

◆ ccid() [1/2]

static bool ucommon::Socket::ccid ( socket_t  socket,
uint8_t  id 
)
static

Set congestion control id.

Parameters
socketto modify.
ccidvalue to set.
Returns
true if success, false if not dccp or not supported ccid used.

◆ ccid() [2/2]

bool ucommon::Socket::ccid ( uint8_t  id)
inline

Set ccid of dccp socket.

Parameters
ccidto set.
Returns
true if success, false if not dccp or not supported ccid used.

Definition at line 1111 of file socket.h.

◆ connected()

bool ucommon::Socket::connected ( void  ) const

Test if socket is connected.

Returns
true if connected.

◆ connectto() [1/2]

int ucommon::Socket::connectto ( struct addrinfo list)

Connect our socket to a remote host from an address list.

For TCP (and DCCP) sockets, the entire list may be tried. For UDP, connect is only a state and the first valid entry in the list is used.

Parameters
listof addresses to connect to.
Returns
0 on success or error.

◆ connectto() [2/2]

static int ucommon::Socket::connectto ( socket_t  socket,
struct addrinfo list 
)
static

Connect socket descriptor to a remote host from an address list.

For TCP (and DCCP) sockets, the entire list may be tried. For UDP, connect is only a state and the first valid entry in the list is used.

Parameters
socketdescriptor.
listof addresses to connect to.
Returns
0 on success, -1 on error.

◆ copy()

static unsigned ucommon::Socket::copy ( struct sockaddr *  target,
const struct sockaddr *  origin 
)
static

Copy a socket address.

Parameters
targetaddress pointer to copy into.
originaddress pointer to copy from.
Returns
number of bytes copied, 0 if invalid.

◆ create() [1/4]

static socket_t ucommon::Socket::create ( int  family,
int  type,
int  protocol 
)
static

Create a socket object unbound.

Parameters
familyof socket.
typeof socket.
protocolof socket.
Returns
socket descriptor created or INVALID_SOCKET.

◆ create() [2/4]

static socket_t ucommon::Socket::create ( const struct addrinfo address,
int  type,
int  protocol 
)
static

Create a connected socket.

Parameters
addresslist to connect to.
typeof socket to create.
protocolof socket.
Returns
socket descriptor created or INVALID_SOCKET.

◆ create() [3/4]

static socket_t ucommon::Socket::create ( const char *  iface,
const char *  service,
int  family = 0,
int  type = 0,
int  protocol = 0 
)
static

Create a bound socket for a service.

Parameters
ifaceto bind.
serviceport to bind.
familyto select or AF_UNSPEC
typeof socket to create.
protocolof socket to create.
Returns
socket descriptor created or INVALID_SOCKET.

◆ create() [4/4]

static socket_t ucommon::Socket::create ( const Socket::address address)
static

Create a connected socket for a service.

Parameters
addressof service for connect.
Returns
socket descriptor.

◆ disconnect() [1/2]

int ucommon::Socket::disconnect ( void  )

Disconnect a connected socket.

Depending on the implementation, this might be done by connecting to AF_UNSPEC, connecting to a 0 address, or connecting to self.

Returns
0 on success or error.

◆ disconnect() [2/2]

static int ucommon::Socket::disconnect ( socket_t  socket)
static

Disconnect a connected socket descriptor.

Parameters
socketdescriptor.
Returns
0 on success, -1 on error.

◆ drop() [1/2]

int ucommon::Socket::drop ( const struct addrinfo list,
const int  ifindex = 0 
)

Drop socket from multicast group.

Parameters
listof groups to drop.
Returns
0 on success, -1 on error.

◆ drop() [2/2]

static int ucommon::Socket::drop ( socket_t  socket,
const struct addrinfo list,
const int  ifindex = 0 
)
static

Drop socket descriptor from multicast group.

Parameters
socketdescriptor.
listof groups to drop.
Returns
0 on success, -1 on error.

◆ eq_from()

static bool ucommon::Socket::eq_from ( const struct sockaddr_storage *  address1,
const struct sockaddr_storage *  address2 
)
inlinestatic

Compare socket addresses.

Test if the stored addresses received match. or if there is no service, then just the host address values.

Parameters
address1to compare.
address2to compare.
Returns
true if same family and equal.

Definition at line 1674 of file socket.h.

◆ eq_host()

static bool ucommon::Socket::eq_host ( const struct sockaddr *  address1,
const struct sockaddr *  address2 
)
static

Compare socket host addresses.

Test if the host address matches or if there is no service, then just the host address values.

Parameters
address1to compare.
address2to compare.
Returns
true if same family and equal.

◆ eq_inet()

static bool ucommon::Socket::eq_inet ( const struct sockaddr_internet address1,
const struct sockaddr_internet address2 
)
inlinestatic

Compare socket addresses.

Test if the internet addresses received match. or if there is no service, then just the host address values.

Parameters
address1to compare.
address2to compare.
Returns
true if same family and equal.

Definition at line 1685 of file socket.h.

◆ eq_subnet()

static bool ucommon::Socket::eq_subnet ( const struct sockaddr *  address1,
const struct sockaddr *  address2 
)
static

See if both addresses are in the same subnet.

This is only relevant to IPV4 and class domain routing.

Parameters
address1to test.
address2to test.
Returns
true if in same subnet.

◆ equal()

static bool ucommon::Socket::equal ( const struct sockaddr *  address1,
const struct sockaddr *  address2 
)
static

Compare socket addresses.

Test if the address and service matches or if there is no service, then just the host address values.

Parameters
address1to compare.
address2to compare.
Returns
true if same family and equal.

◆ error() [1/2]

static int ucommon::Socket::error ( const socket_t  socket)
static

Get socket error code of socket descriptor.

Parameters
socketdescriptor.
Returns
socket error code.

◆ error() [2/2]

static int ucommon::Socket::error ( void  )
static

Return error code of last socket operation,.

Returns
errno style error code.

◆ family() [1/3]

static int ucommon::Socket::family ( socket_t  socket)
static

Get the address family of the socket descriptor.

Returns
address family.

◆ family() [2/3]

static int ucommon::Socket::family ( const struct sockaddr_storage &  address)
inlinestatic

Get the address family of a socket address object.

Parameters
addressto examine.
Returns
address family.

Definition at line 1463 of file socket.h.

◆ family() [3/3]

static int ucommon::Socket::family ( const struct sockaddr_internet address)
inlinestatic

Get the address family of an internet socket address object.

Parameters
addressto examine.
Returns
address family.

Definition at line 1472 of file socket.h.

Here is the call graph for this function:

◆ getError()

int ucommon::Socket::getError ( void  ) const
inline

Get socket error code.

Returns
socket error code.

Definition at line 1026 of file socket.h.

◆ hinting()

static struct addrinfo* ucommon::Socket::hinting ( socket_t  socket,
struct addrinfo hint 
)
static

Create an address info lookup hint based on the family and type properties of a socket descriptor.

Parameters
socketdescriptor.
hintbuffer.
Returns
hint buffer.

◆ hostname()

static char* ucommon::Socket::hostname ( const struct sockaddr *  address,
char *  buffer,
size_t  size 
)
static

Lookup and return the host name associated with a socket address.

Parameters
addressto lookup.
bufferto save hostname into.
sizeof buffer to save hostname into.
Returns
buffer or NULL if lookup fails.

◆ is_null()

static bool ucommon::Socket::is_null ( const char *  string)
static

Simple function to validate that a given IP address string is a "zero" address.

Such address strings are used for example in SIP to indicate "hold" by re-inviting peers to a null address. Supports IPV4 and IPV6 addresses.

Parameters
stringaddress to check.
Returns
true if zero/null address.

◆ is_numeric()

static bool ucommon::Socket::is_numeric ( const char *  string)
static

Simple function to validate that a given IP address string is a numeric address.

This can be used to verify an address is not a "host" name. Supports IPV4 and IPV6 address strings.

Parameters
stringaddress to check.
Returns
true if zero/null address.

◆ is_pending()

bool ucommon::Socket::is_pending ( unsigned  value)

See the number of bytes in the receive queue.

Parameters
valueto test for.
Returns
true if at least that many bytes waiting in receive queue.

◆ join() [1/2]

int ucommon::Socket::join ( const struct addrinfo list,
const int  ifindex = 0 
)

Join socket to multicast group.

Parameters
listof groups to join.
Returns
0 on success, -1 on error.

◆ join() [2/2]

static int ucommon::Socket::join ( socket_t  socket,
const struct addrinfo list,
const int  ifindex = 0 
)
static

Join socket descriptor to multicast group.

Parameters
socketdescriptor.
listof groups to join.
Returns
0 on success, -1 on error.

◆ keepalive() [1/2]

int ucommon::Socket::keepalive ( bool  enable)
inline

Set socket for keepalive packets.

Parameters
enablekeep-alive if true.
Returns
0 on success, -1 if error.

Definition at line 991 of file socket.h.

◆ keepalive() [2/2]

static int ucommon::Socket::keepalive ( socket_t  socket,
bool  enable 
)
static

Set socket for keepalive packets for socket descriptor.

Parameters
socketdescriptor.
enablekeep-alive if true.
Returns
0 if success, -1 if error.

◆ keyhost()

static unsigned ucommon::Socket::keyhost ( const struct sockaddr *  address,
unsigned  size 
)
static

Convert a socket host address into a hash map index.

Parameters
addressto convert.
sizeof map index.
Returns
key index path.

◆ keyindex()

static unsigned ucommon::Socket::keyindex ( const struct sockaddr *  address,
unsigned  size 
)
static

Convert a socket address and service into a hash map index.

Parameters
addressto convert.
sizeof map index.
Returns
key index path.

◆ len()

static socklen_t ucommon::Socket::len ( const struct sockaddr *  address)
static

Get the size of a socket address.

Parameters
addressof socket.
Returns
size to use for this socket address object.

◆ listento()

static int ucommon::Socket::listento ( socket_t  socket,
const struct sockaddr *  address,
int  backlog = 5 
)
static

Bind the socket descriptor to a known interface listen on service port.

Parameters
socketdescriptor to bind.
addressof interface to bind to.
backlogfor service.
Returns
0 on success, -1 if error.

◆ local()

static int ucommon::Socket::local ( socket_t  socket,
struct sockaddr_storage *  address 
)
static

Get local address to which the socket is bound.

This is defined here because we may re-define the backend linkage for the socks proxy in the future.

Parameters
socketdescriptor to examine.
addressstorage for local address.
Returns
0 on success, -1 on failure.

◆ loopback() [1/2]

int ucommon::Socket::loopback ( bool  enable)
inline

Set loopback to read multicast packets we broadcast.

Parameters
enabletrue to loopback, false to ignore.
Returns
0 on success, -1 if error.

Definition at line 1018 of file socket.h.

◆ loopback() [2/2]

static int ucommon::Socket::loopback ( socket_t  socket,
bool  enable 
)
static

Set loopback to read multicast packets socket descriptor broadcasts.

Parameters
socketdescriptor.
enabletrue to loopback, false to ignore.
Returns
0 if success, -1 if error.

◆ multicast() [1/2]

int ucommon::Socket::multicast ( unsigned  ttl = 1)
inline

Set multicast mode and multicast broadcast range.

Parameters
ttlto set for multicast socket or 0 to disable multicast.
Returns
0 on success, -1 if error.

Definition at line 1009 of file socket.h.

◆ multicast() [2/2]

static int ucommon::Socket::multicast ( socket_t  socket,
unsigned  ttl = 1 
)
static

Set multicast mode and multicast broadcast range for socket descriptor.

Parameters
socketdescriptor.
ttlto set for multicast socket or 0 to disable multicast.
Returns
0 if success, -1 if error.

◆ nodelay() [1/2]

int ucommon::Socket::nodelay ( void  ) const
inline

Set nodelay option for tcp socket.

Returns
0 if successful, -1 on error.

Definition at line 948 of file socket.h.

◆ nodelay() [2/2]

static int ucommon::Socket::nodelay ( socket_t  socket)
static

Set tcp nodelay option on socket descriptor.

Parameters
socketdescriptor.
Returns
0 if success, -1 if error.

◆ operator *()

socket_t ucommon::Socket::operator * ( ) const
inline

Get the socket descriptor by pointer reference.

Returns
socket descriptor of object.

Definition at line 1305 of file socket.h.

◆ operator bool()

ucommon::Socket::operator bool ( ) const

Test if socket is valid.

Returns
true if valid socket.

◆ operator socket_t()

ucommon::Socket::operator socket_t ( ) const
inline

Get the socket descriptor by casting.

Returns
socket descriptor of object.

Definition at line 1297 of file socket.h.

◆ operator!()

bool ucommon::Socket::operator! ( ) const

Test if socket is invalid.

Returns
true if socket is invalid.

◆ operator=()

Socket& ucommon::Socket::operator= ( socket_t  socket)

Assign socket from a socket descriptor.

Release existing socket if one present.

Parameters
socketdescriptor to assign to object.

◆ peek()

size_t ucommon::Socket::peek ( void *  data,
size_t  number 
) const

Peek at data waiting in the socket receive buffer.

Parameters
datapointer to save data in.
numberof bytes to peek.
Returns
number of bytes actually read, or 0 if no data waiting.

◆ pending() [1/2]

unsigned ucommon::Socket::pending ( void  ) const
inline

Get the number of bytes of data in the socket receive buffer.

Returns
bytes pending.

Definition at line 973 of file socket.h.

◆ pending() [2/2]

static unsigned ucommon::Socket::pending ( socket_t  socket)
static

Get the number of bytes pending in the receive buffer of a socket descriptor.

Parameters
socketdescriptor.
Returns
number of pending bytes.

◆ port() [1/2]

static in_port_t ucommon::Socket::port ( const struct sockaddr *  address)
static

Get the service port of a socket.

Parameters
addressof socket to examine.
Returns
service port number.

◆ port() [2/2]

static in_port_t ucommon::Socket::port ( const struct sockaddr_internet address)
inlinestatic

Get the service port of an inet socket.

Parameters
addressof internet socket to examine.
Returns
service port number.

Definition at line 1729 of file socket.h.

◆ printf() [1/2]

size_t ucommon::Socket::printf ( const char *  format,
  ... 
)

Print formatted string to socket.

Parameters
formatstring.
Returns
number of bytes sent.

◆ printf() [2/2]

static ssize_t ucommon::Socket::printf ( socket_t  socket,
const char *  format,
  ... 
)
static

Print formatted string to socket.

Parameters
socketto write to.
formatstring.
Returns
number of bytes sent, -1 if error.

◆ priority() [1/2]

int ucommon::Socket::priority ( int  scheduling)
inline

Set packet priority, 0 to 6 unless privileged.

Should be set before type-of-service.

Parameters
schedulingpriority for packet scheduling.
Returns
0 on success, -1 on error.

Definition at line 1133 of file socket.h.

◆ priority() [2/2]

static int ucommon::Socket::priority ( socket_t  socket,
int  scheduling 
)
static

Set packet priority of socket descriptor.

Parameters
socketdescriptor.
schedulingpriority for packet scheduling.
Returns
0 on success, -1 on error.

◆ query() [1/4]

static struct addrinfo* ucommon::Socket::query ( const char *  host,
const char *  service,
int  type = SOCK_STREAM,
int  protocol = 0 
)
static

Get an address list directly.

This is used internally by some derived socket types when generic address lists would be invalid.

Parameters
hostname in the form address or "address:port"
serviceid or port to use if not specified in host string.
typeof service to get.
protocolof service to get.

◆ query() [2/4]

static socklen_t ucommon::Socket::query ( socket_t  socket,
struct sockaddr_storage *  address,
const char *  hostname,
const char *  service 
)
static

Lookup a host name and service address based on the addressing family and socket type of a socket descriptor.

Store the result in a socket address structure.

Parameters
socketdescriptor.
addressthat is resolved.
hostnameto resolve.
serviceport.
Returns
socket address size.

◆ query() [3/4]

static char* ucommon::Socket::query ( const struct sockaddr *  address,
char *  buffer,
socklen_t  size 
)
static

Get the hostname of a socket address.

Parameters
addressto lookup.
bufferto save hostname in.
sizeof hostname buffer.
Returns
buffer if found or NULL if not.

◆ query() [4/4]

static void ucommon::Socket::query ( int  family)
static

Set default socket family preference for query options when the socket type is otherwise not specified.

Parameters
familyto select.

◆ readfrom()

size_t ucommon::Socket::readfrom ( void *  data,
size_t  number,
struct sockaddr_storage *  address = NULL 
)

Read data from the socket receive buffer.

This will be used in abi 4.

Parameters
datapointer to save data in.
numberof bytes to read.
addressof peer data was received from.
Returns
number of bytes actually read, 0 if none, -1 if error.

◆ readline() [1/3]

size_t ucommon::Socket::readline ( char *  data,
size_t  size 
)

Read a newline of text data from the socket and save in NULL terminated string.

This uses an optimized I/O method that takes advantage of socket peeking. This presumes a connected socket on a streamble protocol. Because the trailing newline is dropped, the return size may be greater than the string length. If there was no data read because of eof of data, an error has occured, or timeout without input, then 0 will be returned.

Parameters
datato save input line.
sizeof input line buffer.
Returns
number of bytes read, 0 if none, err() has error.

◆ readline() [2/3]

size_t ucommon::Socket::readline ( String buffer)

Read a string of input from the socket and strip trailing newline.

This uses an optimized I/O method that takes advantage of socket peeking. This presumes a connected socket on a streamble protocol. Because the trailing newline is dropped, the return size may be greater than the string length. If there was no data read because of eof of data, an error has occured, or timeout without input, then 0 will be returned.

Parameters
bufferto save input line.
Returns
number of bytes read, 0 if none, err() has error.

◆ readline() [3/3]

static ssize_t ucommon::Socket::readline ( socket_t  socket,
char *  data,
size_t  size,
timeout_t  timeout = Timer::inf 
)
static

Read a newline of text data from the socket and save in NULL terminated string.

This uses an optimized I/O method that takes advantage of socket peeking. As such, it has to be rewritten to be used in a ssl layer socket.

Parameters
socketto read from.
datato save input line.
sizeof input line buffer.
timeoutto wait for a complete input line.
Returns
number of bytes read, 0 if none, -1 if error.

◆ recvfrom()

static ssize_t ucommon::Socket::recvfrom ( socket_t  socket,
void *  buffer,
size_t  size,
int  flags = 0,
struct sockaddr_storage *  address = NULL 
)
static

Get data waiting in receive queue.

Parameters
socketto get from.
bufferto save.
sizeof data buffer to request.
flagsfor i/o operation (MSG_OOB, MSG_PEEK, etc).
addressof source.
Returns
number of bytes received, -1 if error.

◆ recvsize() [1/2]

int ucommon::Socket::recvsize ( unsigned  size)
inline

Set the size of the socket receive buffer.

Parameters
sizeof recv buffer to set.
Returns
0 on success, -1 on error.

Definition at line 1062 of file socket.h.

◆ recvsize() [2/2]

static int ucommon::Socket::recvsize ( socket_t  socket,
unsigned  size 
)
static

Set the receive size of a socket descriptor.

Parameters
socketdescriptor.
sizeof receive buffer to set.
Returns
0 on success, -1 on error.

◆ release() [1/2]

static void ucommon::Socket::release ( struct addrinfo list)
static

Release an address list directly.

This is used internally by some derived socket types which do not use generic address lists.

Parameters
listof addresses.

◆ release() [2/2]

static void ucommon::Socket::release ( socket_t  socket)
static

Release (close) a socket.

Parameters
socketto close.

◆ remote()

static int ucommon::Socket::remote ( socket_t  socket,
struct sockaddr_storage *  address 
)
static

Get remote address to which the socket is connected.

This is defined here because we may re-define the backend linkage for the socks proxy in the future.

Parameters
socketdescriptor to examine.
addressstorage for remote address.
Returns
0 on success, -1 on failure.

◆ replyto()

static ssize_t ucommon::Socket::replyto ( socket_t  socket,
const void *  buffer,
size_t  size,
int  flags,
const struct sockaddr_storage *  address 
)
inlinestatic

Send reply on socket.

Used to reply to a recvfrom message.

Parameters
socketto send to.
bufferto send.
sizeof data buffer to send.
flagsfor i/o operation (MSG_OOB, MSG_PEEK, etc).
addressto reply to.
Returns
number of bytes sent, -1 if error.

Definition at line 1507 of file socket.h.

◆ segsize() [1/2]

static unsigned ucommon::Socket::segsize ( socket_t  socket,
unsigned  size = 0 
)
static

Set segment size and get MTU.

Parameters
socketto modify.
sizeof segment or zero to not set.
Returns
mtu size of socket.

◆ segsize() [2/2]

unsigned ucommon::Socket::segsize ( unsigned  size)
inline

Set segment size and get mtu of a socket.

Parameters
sizeof segment or 0 to leave unchanged.
Returns
mtu size.

Definition at line 1102 of file socket.h.

◆ sendsize() [1/2]

int ucommon::Socket::sendsize ( unsigned  size)
inline

Set the size of the socket send buffer.

Parameters
sizeof send buffer to set.
Returns
0 on success, -1 on error.

Definition at line 1044 of file socket.h.

◆ sendsize() [2/2]

static int ucommon::Socket::sendsize ( socket_t  socket,
unsigned  size 
)
static

Set the send size of a socket descriptor.

Parameters
socketdescriptor.
sizeof send buffer to set.
Returns
0 on success, -1 on error.

◆ sendto()

static ssize_t ucommon::Socket::sendto ( socket_t  socket,
const void *  buffer,
size_t  size,
int  flags = 0,
const struct sockaddr *  address = NULL 
)
static

Send data on socket.

Parameters
socketto send to.
bufferto send.
sizeof data buffer to send.
flagsfor i/o operation (MSG_OOB, MSG_PEEK, etc).
addressof destination, NULL if connected.
Returns
number of bytes sent, -1 if error.

◆ sendwait() [1/2]

int ucommon::Socket::sendwait ( unsigned  size)
inline

Set the size to wait before sending.

Parameters
sizeof send wait buffer to set.
Returns
0 on success, -1 on error.

Definition at line 1053 of file socket.h.

◆ sendwait() [2/2]

static int ucommon::Socket::sendwait ( socket_t  socket,
unsigned  size 
)
static

Set the size to wait before sending.

Parameters
socketdescriptor.
sizeof send wait buffer to set.
Returns
0 on success, -1 on error.

◆ store() [1/2]

static unsigned ucommon::Socket::store ( struct sockaddr_storage *  storage,
const struct sockaddr *  address 
)
static

Store an address into an address object.

Parameters
storagefor address.
addressto store.
Returns
number of bytes stored.

◆ store() [2/2]

static unsigned ucommon::Socket::store ( struct sockaddr_internet storage,
const struct sockaddr *  address 
)
static

Store an address into an internet address object.

Parameters
storagefor address.
addressto store.
Returns
number of bytes stored.

◆ tos() [1/2]

int ucommon::Socket::tos ( int  type)
inline

Set the type of service field of outgoing packets.

Some useful values include IPTOS_LOWDELAY to minimize delay for interactive traffic, IPTOS_THROUGHPUT to optimize throughput, OPTOS_RELIABILITY to optimize for reliability, and IPTOS_MINCOST for low speed use.

Parameters
typeof service value.
Returns
0 on success or -1 on error.

Definition at line 1123 of file socket.h.

◆ tos() [2/2]

static int ucommon::Socket::tos ( socket_t  socket,
int  type 
)
static

Set type of service of socket descriptor.

Parameters
socketdescriptor.
typeof service.
Returns
0 on success, -1 on error.

◆ ttl() [1/2]

int ucommon::Socket::ttl ( uint8_t  time)
inline

Set the time to live before packets expire.

Parameters
timeto live to set.
Returns
0 on success, -1 on error.

Definition at line 1035 of file socket.h.

◆ ttl() [2/2]

static int ucommon::Socket::ttl ( socket_t  socket,
uint8_t  time 
)
static

Set the time to live for the socket descriptor.

Parameters
socketdescriptor.
timeto live to set.
Returns
0 on success, -1 on error.

◆ type() [1/2]

static int ucommon::Socket::type ( const socket_t  socket)
static

Get the type of a socket.

Parameters
socketdescriptor.
Returns
socket type.

◆ type() [2/2]

int ucommon::Socket::type ( void  ) const
inline

Get the type of a socket.

Returns
socket type.

Definition at line 1093 of file socket.h.

◆ v4mapping()

static void ucommon::Socket::v4mapping ( bool  enable)
static

Set the default socket behavior for v6-v4 mapping.

This also effects v6 address lookup as to whether v4 remapped addresses can be used if no v6 address is found.

Parameters
enabletrue to set mapping. This is default.

◆ via()

static int ucommon::Socket::via ( struct sockaddr *  address,
const struct sockaddr *  target,
socklen_t  size = 0 
)
static

Get the socket address of the interface needed to reach a destination address.

Parameters
addressof interface found.
targetaddress.
sizeof interface, 0 used for older code
Returns
0 on success, -1 on error.

◆ wait() [1/3]

bool ucommon::Socket::wait ( timeout_t  timeout = 0) const

Test for pending input data.

This function can wait up to a specified timeout for data to appear.

Parameters
timeoutor 0 if none.
Returns
true if input data waiting.

◆ wait() [2/3]

static bool ucommon::Socket::wait ( socket_t  socket,
timeout_t  timeout = 0 
)
static

Test for pending input data.

This function can wait up to a specified timeout for data to appear.

Parameters
socketto test.
timeoutor 0 if none.
Returns
true if input data waiting.

◆ wait() [3/3]

int ucommon::Socket::wait ( timeout_t  timeout = Timer::inf)

Socket i/o timer setting.

Parameters
timeoutto wait, inf for blocking, 0 pure non-blocking.
Returns
0 on success or error code.

◆ waitSending()

bool ucommon::Socket::waitSending ( timeout_t  timeout = 0) const

Test for output data sent.

This function can wait up to a specified timeout for data to appear sent.

Parameters
timeoutor 0 if none.
Returns
false if cannot send more output/out of buffer space.

◆ writes()

size_t ucommon::Socket::writes ( const char *  string)

Write a null terminated string to the socket.

This exists because we messed up consistency with the original puts() method. In the future there will be a single puts() that has a NULL default.

Parameters
stringto write.
Returns
number of bytes sent, 0 if none, -1 if error.

◆ writeto()

size_t ucommon::Socket::writeto ( const void *  data,
size_t  number,
const struct sockaddr *  address = NULL 
)

Write data to the socket send buffer.

This will be used in abi 4.

Parameters
datapointer to write data from.
numberof bytes to write.
addressof peer to send data to if not connected.
Returns
number of bytes actually sent, 0 if none, -1 if error.

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