27#ifndef _UCOMMON_SOCKET_H_
28#define _UCOMMON_SOCKET_H_
30#ifndef _UCOMMON_TIMERS_H_
34#ifndef _UCOMMON_LINKED_H_
38#ifndef _UCOMMON_STRING_H_
42#ifndef _UCOMMON_TYPEREF_H_
51#define SHUT_RDWR SD_BOTH
52#define SHUT_WR SD_SEND
53#define SHUT_RD SD_RECV
54typedef uint16_t in_port_t;
55typedef uint32_t in_addr_t;
58#include <sys/socket.h>
60#include <netinet/in.h>
64#if defined(__ANDROID__)
65typedef uint16_t in_port_t;
72#define IPTOS_LOWDELAY 0x10
73#define IPTOS_THROUGHPUT 0x08
74#define IPTOS_RELIABILITY 0x04
75#define IPTOS_MINCOST 0x02
79#define DEFAULT_FAMILY AF_UNSPEC
81#define DEFAULT_FAMILY AF_INET
84typedef struct sockaddr *sockaddr_t;
86typedef struct sockaddr sockaddr_struct;
102#if defined(AF_INET6) || defined(__CYGWIN__)
114 struct sockaddr_in6 ipv6;
116 struct sockaddr_in ipv4;
117 struct sockaddr address;
124 struct sockaddr_in ipv4;
125 struct sockaddr address;
129struct sockaddr_storage
134 char sa_data[
sizeof(
struct sockaddr_in)];
144#define IPPROTO_DCCP 23
151#define DCCP_SOCKOPT_AVAILABLE_CCIDS 12
152#define DCCP_SOCKOPT_CCID 13
153#define DCCP_SOCKOPT_TX_CCID 14
154#define DCCP_SOCKOPT_RX_CCID 15
174 unsigned mask(
const char *cp)
const;
178 unsigned mask(
void)
const;
292 void set(
const char *
string);
306 inline bool operator==(
const struct sockaddr *address)
const {
307 return is_member(address);
315 inline bool operator!=(
const struct sockaddr *address)
const {
316 return !is_member(address);
347 static struct addrinfo *
query(
const char *host,
const char *service,
int type = SOCK_STREAM,
int protocol = 0);
380 address(
int family,
const char *
address,
int type = SOCK_STREAM,
int protocol = 0);
394 address(
int family,
const char *hostname,
const char *service = NULL);
402 address(
const char *host,
const char *service,
int type = SOCK_STREAM);
411 address(
const char *hostname, in_port_t port = 0);
466 inline bool operator!=(
const address& other)
const {
467 return !(*
this==other);
470 inline bool equals(
const address& other)
const {
471 return *
this == other;
478 const struct sockaddr *
get(
void)
const;
480 struct sockaddr *modify(
void);
482 inline const struct sockaddr *getAddr(
void)
const {
486 inline const struct sockaddr *operator()(
void)
const {
494 inline operator struct sockaddr *() {
503 const struct sockaddr *
get(
int family)
const;
505 struct sockaddr *modify(
int family);
507 inline const struct sockaddr *operator()(
int family)
const {
511 inline operator struct sockaddr_in *() {
512 return (
struct sockaddr_in *)modify(AF_INET);
516 inline operator struct sockaddr_in6 *() {
517 return (
struct sockaddr_in6 *)modify(AF_INET6);
540 return getPort(get());
559 struct sockaddr *
find(
const struct sockaddr *addr)
const;
597 size_t print(
char* dst,
size_t dst_sz,
bool port=
false,
bool force_brackets=
false)
const {
598 return print(get(), dst, dst_sz, port, force_brackets);
605 inline operator bool()
const {
606 return list !=
nullptr;
609 inline bool is_valid()
const {
610 return list !=
nullptr;
613 inline bool isValid()
const {
614 return list !=
nullptr;
622 return list ==
nullptr;
634 inline bool isAny()
const {
652 return isLoopback(get());
655 inline bool isLoopback()
const {
656 return isLoopback(get());
678 void set(
const char *hostname,
const char *service = NULL,
int type = SOCK_STREAM);
686 void add(
const char *hostname,
const char *service = NULL,
int type = SOCK_STREAM);
695 void set(
int family,
const char *
address,
int type = SOCK_STREAM,
int protocol = 0);
750 void set(
const char *hostname, in_port_t service = 0);
791 static sockaddr_storage
any(
int family);
847 static size_t print(
const struct sockaddr *src,
char* dst,
size_t dst_sz,
bool port=
false,
bool ipv6_brackets=
false);
882 Socket(
int family,
int type,
int protocol = 0);
893 Socket(
const char *
address,
const char *port,
int family = AF_UNSPEC,
int type = 0,
int protocol = 0);
919 inline int err(
void)
const {
942 bool wait(timeout_t timeout = 0)
const;
959 static bool wait(socket_t socket, timeout_t timeout = 0);
983 return broadcast(so, enable);
992 return keepalive(so, enable);
1001 return blocking(so, enable);
1010 return multicast(so, ttl);
1019 return loopback(so, enable);
1035 inline int ttl(uint8_t time) {
1036 return ttl(so, time);
1045 return sendsize(so, size);
1054 return sendwait(so, size);
1063 return recvsize(so, size);
1071 static int type(
const socket_t socket);
1079 static unsigned segsize(socket_t socket,
unsigned size = 0);
1087 static bool ccid(socket_t socket, uint8_t
id);
1103 return segsize(so, size);
1112 return ccid(so,
id);
1124 return tos(so, type);
1134 return priority(so, scheduling);
1141 ::shutdown(so, SHUT_RDWR);
1166 int join(
const struct addrinfo *list,
const int ifindex = 0);
1173 int drop(
const struct addrinfo *list,
const int ifindex = 0);
1180 int wait(timeout_t timeout = Timer::inf);
1188 size_t peek(
void *data,
size_t number)
const;
1206 size_t writeto(
const void *data,
size_t number,
const struct sockaddr *
address = NULL);
1227 size_t printf(
const char *format, ...) __PRINTF(2,3);
1242 stringref_t readline(
size_t maxsize);
1255 static ssize_t readline(socket_t socket,
char *data,
size_t size, timeout_t timeout =
Timer::inf);
1263 static ssize_t printf(socket_t socket, const
char *format, ...) __PRINTF(2,3);
1272 size_t writes(const
char *
string);
1278 operator
bool() const;
1284 bool operator!() const;
1297 inline operator socket_t()
const {
1349 static int connectto(socket_t socket,
struct addrinfo *list);
1364 static int drop(socket_t socket,
const struct addrinfo *list,
const int ifindex = 0);
1372 static int join(socket_t socket,
const struct addrinfo *list,
const int ifindex = 0);
1379 static int error(
const socket_t socket);
1442 static int tos(socket_t socket,
int type);
1450 static int ttl(socket_t socket, uint8_t time);
1464 return ((
const struct sockaddr *)&
address)->sa_family;
1485 static ssize_t
recvfrom(socket_t socket,
void *buffer,
size_t size,
int flags = 0,
struct sockaddr_storage *
address = NULL);
1496 static ssize_t
sendto(socket_t socket,
const void *buffer,
size_t size,
int flags = 0,
const struct sockaddr *
address = NULL);
1507 inline static ssize_t
replyto(socket_t socket,
const void *buffer,
size_t size,
int flags,
const struct sockaddr_storage *
address) {
1508 return sendto(socket, buffer, size, flags, (
const struct sockaddr *)
address);
1519 static int bindto(socket_t socket,
const char *
address,
const char *service,
int protocol = 0);
1553 static socket_t
create(
int family,
int type,
int protocol);
1573 static socket_t
create(
const char *iface,
const char *service,
int family = AF_UNSPEC,
int type = 0,
int protocol = 0);
1616 static socklen_t
query(socket_t socket,
struct sockaddr_storage *
address,
const char *hostname,
const char *service);
1632 static bool equal(
const struct sockaddr *address1,
const struct sockaddr *address2);
1640 static unsigned copy(
struct sockaddr *target,
const struct sockaddr *origin);
1648 static unsigned store(
struct sockaddr_storage *storage,
const struct sockaddr *
address);
1665 static bool eq_host(
const struct sockaddr *address1,
const struct sockaddr *address2);
1674 inline static bool eq_from(
const struct sockaddr_storage *address1,
const struct sockaddr_storage *address2) {
1675 return equal((
const struct sockaddr *)address1, (
const struct sockaddr *)address2);
1686 return equal((
const struct sockaddr *)address1, (
const struct sockaddr *)address2);
1696 static bool eq_subnet(
const struct sockaddr *address1,
const struct sockaddr *address2);
1706 static int via(
struct sockaddr *
address,
const struct sockaddr *target, socklen_t size = 0);
1715 static char *
query(
const struct sockaddr *
address,
char *buffer, socklen_t size);
1730 return port((
const struct sockaddr *)
address);
1835 ListenSocket(
const char *
address,
const char *service,
unsigned backlog = 5,
int family = AF_UNSPEC,
int type = 0,
int protocol = 0);
1847 static socket_t
create(
const char *
address,
const char *service,
unsigned backlog = 5,
int family = AF_UNSPEC,
int type = 0,
int protocol = 0);
1861 inline bool wait(timeout_t timeout = Timer::inf)
const {
1862 return Socket::wait(timeout);
1869 inline operator socket_t()
const {
1889 inline socket_t handle(
void)
const {
1933 const struct sockaddr *
_getaddrinfo(
const struct addrinfo *addrinfo)
const;
1973 inline operator const struct sockaddr *()
const {
1974 return _getaddrinfo(ptr);
1982 return _getaddrinfo(ptr);
1985 inline operator const struct sockaddr_in *()
const {
1986 return (
struct sockaddr_in *)_getaddrinfo(ptr);
1989 inline const struct sockaddr_in *in(
void)
const {
1990 return (
struct sockaddr_in *)_getaddrinfo(ptr);
1994 inline operator const struct sockaddr_in6 *()
const {
1995 return (
struct sockaddr_in6 *)_getaddrinfo(ptr);
1998 inline const struct sockaddr_in6 *in6(
void)
const {
1999 return (
struct sockaddr_in6 *)_getaddrinfo(ptr);
2007 return _getaddrsock(ptr);
2014 inline operator bool()
const {
2015 return ptr !=
nullptr;
2058 return _getaddrinfo(ptr);
2066 return ptr ==
nullptr;
2069 inline void next(
void) {
2070 ptr = _nextaddrinfo(ptr);
2080 return address.getList();
2090 return address.get();
2100inline bool eq(
const struct sockaddr *s1,
const struct sockaddr *s2) {
2111inline bool eq(
const struct sockaddr_storage *s1,
const struct sockaddr_storage *s2) {
2112 return Socket::equal((
const struct sockaddr *)s1, (
const struct sockaddr *)s2);
2122inline bool eq_host(
const struct sockaddr *s1,
const struct sockaddr *s2) {
2126inline bool eq_subnet(
const struct sockaddr *s1,
const struct sockaddr *s2) {
2130String str(Socket& so,
size_t size);
2140 inline HostAddress() {
2141 memset(&storage, 0,
sizeof(storage));
2144 inline HostAddress(
const HostAddress&
copy) {
2145 memcpy(&storage, &
copy.storage,
sizeof(storage));
2152 inline HostAddress(
const in_addr *
addr) {
2153 memset(&storage, 0,
sizeof(storage));
2154 memcpy(&storage,
addr,
sizeof(
struct in_addr));
2158 inline HostAddress(
const in6_addr *
addr) {
2159 memset(&storage, 0,
sizeof(storage));
2160 memcpy(&storage,
addr,
sizeof(
struct in6_addr));
2172 inline socklen_t size() {
2173 return sizeof(storage);
2176 inline HostAddress& operator=(
const HostAddress&
copy) {
2177 memcpy(&storage, &
copy.storage,
sizeof(storage));
2186 inline bool operator==(
const HostAddress& check)
const {
2187 return (memcmp(&check.storage, &storage,
sizeof(storage)) == 0);
2190 inline bool operator!=(
const HostAddress& check)
const {
2191 return (memcmp(&check.storage, &storage,
sizeof(storage)) != 0);
2195 return (memcmp(host, &storage,
sizeof(storage)) == 0);
2199 return (memcmp(host, &storage,
sizeof(storage)) != 0);
2206 struct sockaddr_storage storage;
2209 inline SockAddress() {
2210 memset(&storage, 0,
sizeof(storage));
2213 inline SockAddress(
const SockAddress&
copy) {
2214 memcpy(&storage, &
copy.storage,
sizeof(storage));
2217 inline SockAddress(
const struct sockaddr *
addr) {
2221 inline SockAddress& operator=(
const SockAddress&
copy) {
2222 memcpy(&storage, &
copy.storage,
sizeof(storage));
2226 inline SockAddress& operator=(
const struct sockaddr *
addr) {
2231 inline operator const struct sockaddr *()
const {
2232 return (
const struct sockaddr*)&storage;
2235 inline struct sockaddr *operator*() {
2236 return (
struct sockaddr *)&storage;
2239 inline const struct sockaddr *get()
const {
2240 return (
const struct sockaddr *)&storage;
2243 inline socklen_t size() {
2244 return sizeof(storage);
2247 inline bool operator==(
const SockAddress& check)
const {
2251 inline bool operator!=(
const SockAddress& check)
const {
2255 inline bool operator==(
const struct sockaddr *check)
const {
2259 inline bool operator!=(
const struct sockaddr *check)
const {
2270 inline InetAddress() {
2271 memset(&storage, 0,
sizeof(storage));
2274 inline InetAddress(
const InetAddress&
copy) {
2275 memcpy(&storage, &
copy.storage,
sizeof(storage));
2278 inline InetAddress(
const struct sockaddr *
addr) {
2282 inline InetAddress& operator=(
const InetAddress&
copy) {
2283 memcpy(&storage, &
copy.storage,
sizeof(storage));
2287 inline InetAddress& operator=(
const struct sockaddr *
addr) {
2292 inline operator const struct sockaddr *()
const {
2293 return (
const struct sockaddr*)&storage;
2296 inline struct sockaddr *operator*() {
2297 return (
struct sockaddr *)&storage;
2300 inline const struct sockaddr *get()
const {
2301 return (
const struct sockaddr *)&storage;
2304 inline socklen_t size() {
2305 return sizeof(storage);
2308 inline bool operator==(
const SockAddress& check)
const {
2312 inline bool operator!=(
const SockAddress& check)
const {
2316 inline bool operator==(
const struct sockaddr *check)
const {
2320 inline bool operator!=(
const struct sockaddr *check)
const {
2326typedef TCPServer tcpserv_t;
A thread-safe atomic heap management system.
Linked objects, lists, templates, and containers.
Realtime timers and timer queues.
Common namespace for all ucommon objects.
bool eq(const struct sockaddr *s1, const struct sockaddr *s2)
Compare two socket addresses to see if equal.
struct addrinfo * addrinfo(Socket::address &address)
A convenience function to convert a socket address list into an addrinfo.
bool eq_host(const struct sockaddr *s1, const struct sockaddr *s2)
Compare two host addresses to see if equal.
const struct sockaddr * addr(Socket::address &address)
A convenience function to convert a socket address list into a socket address.
T copy(const T &src)
Convenience function to copy objects.
Common base class for all objects that can be formed into a linked list.
A smart pointer template for iterating linked lists.
linked_pointer()
Create a linked pointer not attached to a list.
void next(void)
Move (iterate) pointer to next member in linked list.
An object that holds ipv4 or ipv6 binary encoded host addresses.
An object that can hold a ipv4 or ipv6 socket address.
A class to hold internet segment routing rules.
cidr(const char *string)
Create an unlinked cidr from a string.
int getFamily(void) const
Get the address family of our cidr block object.
unsigned getMask(void) const
Get the number of bits in the cidr bitmask.
cidr(const cidr &existing)
Construct a copy of an existing cidr.
cidr()
Create an uninitialized cidr.
cidr(policy **policy, const char *string, const char *name)
Create a named cidr entry on a specified policy chain.
const char * getName(void) const
Get the saved name of our cidr.
void set(const char *string)
Set our cidr to a string address.
bool is_member(const struct sockaddr *address) const
Test if a given socket address falls within this cidr.
static const cidr * find(const policy *policy, const struct sockaddr *address)
Find the smallest cidr entry in a list that matches the socket address.
bool operator!=(const struct sockaddr *address) const
Test if a given socket address falls outside this cidr.
cidr(policy **policy, const char *string)
Create an unnamed cidr entry on a specified policy chain.
bool operator==(const struct sockaddr *address) const
Test if a given socket address falls within this cidr.
LinkedObject policy
A convenience type for using a pointer to a linked list as a policy chain.
static const cidr * container(const policy *policy, const struct sockaddr *address)
Get the largest container cidr entry in a list that matches the socket address.
A generic socket base class.
static int recvsize(socket_t socket, unsigned size)
Set the receive size of a socket descriptor.
static in_port_t port(const struct sockaddr_internet *address)
Get the service port of an inet socket.
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.
size_t readline(char *data, size_t size)
Read a newline of text data from the socket and save in NULL terminated string.
int recvsize(unsigned size)
Set the size of the socket receive buffer.
static int broadcast(socket_t socket, bool enable)
Set socket for unicast mode broadcasts on socket descriptor.
bool is_pending(unsigned value)
See the number of bytes in the receive queue.
static int blocking(socket_t socket, bool enable)
Set socket blocking I/O mode of socket descriptor.
static struct addrinfo * hinting(socket_t socket, struct addrinfo *hint)
Create an address info lookup hint based on the family and type properties of a socket descriptor.
static bool eq_from(const struct sockaddr_storage *address1, const struct sockaddr_storage *address2)
Compare socket addresses.
Socket(socket_t socket)
Create socket from existing socket descriptor.
static void release(socket_t socket)
Release (close) a socket.
int join(const struct addrinfo *list, const int ifindex=0)
Join socket to multicast group.
static int family(socket_t socket)
Get the address family of the socket descriptor.
static unsigned keyhost(const struct sockaddr *address, unsigned size)
Convert a socket host address into a hash map index.
int drop(const struct addrinfo *list, const int ifindex=0)
Drop socket from multicast group.
int broadcast(bool enable)
Set socket for unicast mode broadcasts.
static void init(void)
Initialize socket subsystem.
static bool eq_host(const struct sockaddr *address1, const struct sockaddr *address2)
Compare socket host addresses.
static bool wait(socket_t socket, timeout_t timeout=0)
Test for pending input data.
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.
static int loopback(socket_t socket, bool enable)
Set loopback to read multicast packets socket descriptor broadcasts.
static socklen_t len(const struct sockaddr *address)
Get the size of a socket address.
static int nodelay(socket_t socket)
Set tcp nodelay option on socket descriptor.
static unsigned copy(struct sockaddr *target, const struct sockaddr *origin)
Copy a socket address.
static void v4mapping(bool enable)
Set the default socket behavior for v6-v4 mapping.
static int type(const socket_t socket)
Get the type of a socket.
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.
static int tos(socket_t socket, int type)
Set type of service of socket descriptor.
static int local(socket_t socket, struct sockaddr_storage *address)
Get local address to which the socket is bound.
int ttl(uint8_t time)
Set the time to live before packets expire.
socket_t operator*() const
Get the socket descriptor by pointer reference.
static unsigned store(struct sockaddr_storage *storage, const struct sockaddr *address)
Store an address into an address object.
Socket(int family, int type, int protocol=0)
Create an unbound socket of a specific type.
static char * query(const struct sockaddr *address, char *buffer, socklen_t size)
Get the hostname of a socket address.
virtual ~Socket()
Shutdown, close, and destroy socket.
static int priority(socket_t socket, int scheduling)
Set packet priority of socket descriptor.
int sendsize(unsigned size)
Set the size of the socket send buffer.
int tos(int type)
Set the type of service field of outgoing packets.
static unsigned keyindex(const struct sockaddr *address, unsigned size)
Convert a socket address and service into a hash map index.
static bool eq_inet(const struct sockaddr_internet *address1, const struct sockaddr_internet *address2)
Compare socket addresses.
int disconnect(void)
Disconnect a connected socket.
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.
int type(void) const
Get the type of a socket.
void shutdown(void)
Shutdown the socket communication channel.
unsigned segsize(unsigned size)
Set segment size and get mtu of a socket.
int sendwait(unsigned size)
Set the size to wait before sending.
int connectto(struct addrinfo *list)
Connect our socket to a remote host from an address list.
static char * hostname(const struct sockaddr *address, char *buffer, size_t size)
Lookup and return the host name associated with a socket address.
unsigned pending(void) const
Get the number of bytes of data in the socket receive buffer.
size_t peek(void *data, size_t number) const
Peek at data waiting in the socket receive buffer.
Socket(const char *address, const char *port, int family=0, int type=0, int protocol=0)
Create a bound socket.
int wait(timeout_t timeout=Timer::inf)
Socket i/o timer setting.
bool waitSending(timeout_t timeout=0) const
Test for output data sent.
int blocking(bool enable)
Set socket blocking I/O mode.
int err(void) const
Get error code.
static int error(const socket_t socket)
Get socket error code of socket descriptor.
static socket_t create(const struct addrinfo *address, int type, int protocol)
Create a connected socket.
Socket(const struct addrinfo *address)
Create and connect a socket to an address from an address list.
int multicast(unsigned ttl=1)
Set multicast mode and multicast broadcast range.
int getError(void) const
Get socket error code.
static socket_t create(const Socket::address &address)
Create a connected socket for a service.
static int connectto(socket_t socket, struct addrinfo *list)
Connect socket descriptor to a remote host from an address list.
static int join(socket_t socket, const struct addrinfo *list, const int ifindex=0)
Join socket descriptor to multicast group.
int keepalive(bool enable)
Set socket for keepalive packets.
bool connected(void) const
Test if socket is connected.
static int sendwait(socket_t socket, unsigned size)
Set the size to wait before sending.
static unsigned store(struct sockaddr_internet *storage, const struct sockaddr *address)
Store an address into an internet address object.
static int bindto(socket_t socket, const struct sockaddr *address)
Bind the socket descriptor to a known interface.
int loopback(bool enable)
Set loopback to read multicast packets we broadcast.
size_t readfrom(void *data, size_t number, struct sockaddr_storage *address=NULL)
Read data from the socket receive buffer.
static unsigned pending(socket_t socket)
Get the number of bytes pending in the receive buffer of a socket descriptor.
static bool equal(const struct sockaddr *address1, const struct sockaddr *address2)
Compare socket addresses.
static int error(void)
Return error code of last socket operation,.
static bool ccid(socket_t socket, uint8_t id)
Set congestion control id.
void cancel(void)
Cancel pending i/o by shutting down the socket.
Socket(const Socket &existing)
Create socket as duped handle of existing socket.
static socket_t create(int family, int type, int protocol)
Create a socket object unbound.
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.
static ssize_t replyto(socket_t socket, const void *buffer, size_t size, int flags, const struct sockaddr_storage *address)
Send reply on socket.
static int disconnect(socket_t socket)
Disconnect a connected socket descriptor.
static int sendsize(socket_t socket, unsigned size)
Set the send size of a socket descriptor.
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.
static void release(struct addrinfo *list)
Release an address list directly.
int nodelay(void) const
Set nodelay option for tcp socket.
static in_port_t port(const struct sockaddr *address)
Get the service port of a socket.
size_t writeto(const void *data, size_t number, const struct sockaddr *address=NULL)
Write data to the socket send buffer.
static bool eq_subnet(const struct sockaddr *address1, const struct sockaddr *address2)
See if both addresses are in the same subnet.
static int family(const struct sockaddr_internet &address)
Get the address family of an internet socket address object.
bool ccid(uint8_t id)
Set ccid of dccp socket.
static bool is_null(const char *string)
Simple function to validate that a given IP address string is a "zero" address.
static socket_t acceptfrom(socket_t socket, struct sockaddr_storage *address=NULL)
Accept a socket connection from a remote host.
int priority(int scheduling)
Set packet priority, 0 to 6 unless privileged.
static int ttl(socket_t socket, uint8_t time)
Set the time to live for the socket descriptor.
void release(void)
Shutdown and close the socket.
static bool is_numeric(const char *string)
Simple function to validate that a given IP address string is a numeric address.
Socket()
Create a socket object for use.
static int multicast(socket_t socket, unsigned ttl=1)
Set multicast mode and multicast broadcast range for socket descriptor.
bool wait(timeout_t timeout=0) const
Test for pending input data.
static unsigned segsize(socket_t socket, unsigned size=0)
Set segment size and get MTU.
static int drop(socket_t socket, const struct addrinfo *list, const int ifindex=0)
Drop socket descriptor from multicast group.
static int remote(socket_t socket, struct sockaddr_storage *address)
Get remote address to which the socket is connected.
size_t printf(const char *format,...)
Print formatted string to socket.
static void cancel(socket_t socket)
Cancel pending i/o by shutting down the socket.
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 des...
static int family(const struct sockaddr_storage &address)
Get the address family of a socket address object.
static struct addrinfo * query(const char *host, const char *service, int type=SOCK_STREAM, int protocol=0)
Get an address list directly.
static void query(int family)
Set default socket family preference for query options when the socket type is otherwise not specifie...
static int keepalive(socket_t socket, bool enable)
Set socket for keepalive packets for socket descriptor.
A generic socket address class.
void set(const char *hostname, in_port_t service=0)
Set a socket address from host and service.
bool is_loopback() const
Test if the first socket address is ADDR_LOOPBACK: 127.0.0.1 or ::1.
void copy(const struct addrinfo *address)
Copy an existing addrinfo into our object.
struct addrinfo * operator*() const
Return the full socket address list by pointer reference.
static sockaddr_storage loopback(int family)
Get a ADDR_LOOPBACK socket address of the given family.
address(const char *host, const char *service, int type=SOCK_STREAM)
Construct a socket address list for a service.
address(const addrinfo *address)
Construct a socket address from an addrinfo structure.
static void setPort(struct sockaddr *address, in_port_t port)
Set the port of the socket address.
address(const address &reference)
Copy constructor.
void clear(void)
Clear current object.
int family(void) const
Get the family of the first member in a list of services.
bool operator!() const
Test if we have no address list.
static void setAny(struct sockaddr *sa)
Set the socket address to ADDR_ANY: 0.0.0.0 or ::0.
address(const in6_addr &address, in_port_t port=0)
Construct a socket address from an IPv6 address and a port number.
const struct sockaddr * get(void) const
Get the first socket address in our address list.
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 addr...
void add(sockaddr *address)
Add an individual socket address to our address list.
void setPort(in_port_t port)
Set the port of all addresses in the list.
void setAny(int family=0)
Clear the address list and set the first address to be the ADDR_ANY of the current family,...
~address()
Destroy address.
address & operator=(const address &rhs)
Assignment operator.
address(const sockaddr &address)
Construct a socket address from a sockaddr object.
void set(struct sockaddr *address)
Set an individual socket address for our address list.
size_t getLength(void) const
Get the address size of the first address.
address(int family, const char *address, int type=SOCK_STREAM, int protocol=0)
Construct a socket address.
address(const in_addr &address, in_port_t port=0)
Construct a socket address from an IPv4 address and a port number.
struct addrinfo * getList(void) const
Get the full socket address list from the object.
bool insert(const struct sockaddr *address)
Insert an individual socket address to our address list only if unique.
static struct sockaddr * dup(struct sockaddr *address)
Duplicate a socket address.
address(const char *hostname, in_port_t port=0)
Construct a socket address from host and service.
void add(const char *hostname, const char *service=NULL, int type=SOCK_STREAM)
Append additional host addresses to our list.
void set(int family, const char *address, int type=SOCK_STREAM, int protocol=0)
Set an entry for host binding.
unsigned insert(const struct addrinfo *address)
Insert unique members from another socket address list to ours.
address(int family, const char *hostname, const char *service=NULL)
Construct a socket address for an existing socket.
void set(const char *hostname, const char *service=NULL, int type=SOCK_STREAM)
Set the host addresses to form a new list.
const struct sockaddr * get(int family) const
Get the first socket address of specified family from our list.
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 prin...
static bool isLoopback(const struct sockaddr *address)
Test if the socket address is ADDR_LOOPBACK: 127.0.0.1 or ::1.
static size_t getLength(const struct sockaddr *address)
Returns the size of the socket address according to the family.
address withPort(in_port_t port) const
Returns a copy of this address list with the specified port set.
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...
bool operator==(const address &other) const
Compare two address lists.
void setLoopback(int family=0)
Clear the address list and set the first address to be the ADDR_LOOPBACK of the current family,...
static bool isAny(const struct sockaddr *address)
Test if the socket address is ADDR_ANY: 0.0.0.0 or ::0.
in_port_t getPort(void) const
Get the port of the first address .
unsigned remove(const struct addrinfo *address)
Remove members from another socket address list from ours.
address()
Construct an empty address.
static in_port_t getPort(const struct sockaddr *address)
Returns the port of the socket address.
struct sockaddr * find(const struct sockaddr *addr) const
Find a specific socket address in our address list.
static struct sockaddr_in6 * ipv6(struct sockaddr *address)
Convert address object into ipv6 address.
bool is_any() const
Test if the first socket address is ADDR_ANY: 0.0.0.0 or ::0.
bool remove(const struct sockaddr *address)
Remove an individual socket address from our address list.
static sockaddr_storage any(int family)
Get a ADDR_ANY socket address of the given family.
static struct sockaddr_in * ipv4(struct sockaddr *address)
Convert address object into ipv4 address.
A bound socket used to listen for inbound socket connections.
socket_t getsocket(void) const
Get the socket descriptor of the listener.
socket_t accept(struct sockaddr_storage *address=NULL) const
Accept a socket connection.
socket_t operator*() const
Get the socket descriptor of the listener by pointer reference.
static socket_t create(const char *address, const char *service, unsigned backlog=5, int family=0, int type=0, int protocol=0)
Create a listen socket directly.
bool wait(timeout_t timeout=Timer::inf) const
Wait for a pending connection.
ListenSocket(const char *address, const char *service, unsigned backlog=5, int family=0, int type=0, int protocol=0)
Create and bind a listener socket.
A generic tcp server class.
TCPServer(const char *address, const char *service, unsigned backlog=5)
Create and bind a tcp server.
Helper class for linked_pointer template.
socket_t _getaddrsock(const struct addrinfo *addrinfo) const
Helper function for linked_pointer<struct sockaddr>.
const struct addrinfo * _nextaddrinfo(const struct addrinfo *addrinfo) const
Helper function for linked_pointer<struct sockaddr>.
const struct sockaddr * _getaddrinfo(const struct addrinfo *addrinfo) const
Helper function for linked_pointer<struct sockaddr>.
const struct sockaddr * operator->() const
Return member from typed object our pointer references.
socket_t operator()(void) const
Get socket as expression operator.
const struct sockaddr * operator*() const
Return the full socket address list by pointer reference.
void set(const struct addrinfo *list)
Assign our pointer from an address list.
void set(Socket::address &list)
Assign our pointer from an address list.
linked_pointer & operator=(const struct addrinfo *list)
Assign our pointer from an address list.
bool operator!() const
Test if we have no address list.
linked_pointer & operator=(Socket::address &list)
Assign our pointer from an address list.
A copy-on-write string class that operates by reference count.
Timer class to use when scheduling realtime events.
A common string class and character string support functions.