Go to the documentation of this file.
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
54 typedef uint16_t in_port_t;
55 typedef uint32_t in_addr_t;
58 #include <sys/socket.h>
60 #include <netinet/in.h>
64 #if defined(__ANDROID__)
65 typedef uint16_t in_port_t;
71 #ifndef IPTOS_LOWDELAY
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
84 typedef struct sockaddr *sockaddr_t;
86 typedef 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;
129 struct 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;
252 inline int getFamily(
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);
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 {
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 {
1869 inline operator socket_t()
const {
1889 inline socket_t handle(
void)
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);
2090 return address.
get();
2100 inline bool eq(
const struct sockaddr *s1,
const struct sockaddr *s2) {
2111 inline bool eq(
const struct sockaddr_storage *s1,
const struct sockaddr_storage *s2) {
2112 return Socket::equal((
const struct sockaddr *)s1, (
const struct sockaddr *)s2);
2122 inline bool eq_host(
const struct sockaddr *s1,
const struct sockaddr *s2) {
2126 inline bool eq_subnet(
const struct sockaddr *s1,
const struct sockaddr *s2) {
2130 String 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 {
2326 typedef TCPServer tcpserv_t;
Socket(const struct addrinfo *address)
Create and connect a socket to an address from an address list.
A generic tcp server class.
bool insert(const struct sockaddr *address)
Insert an individual socket address to our address list only if unique.
Socket(const Socket &existing)
Create socket as duped handle of existing socket.
void clear(void)
Clear current object.
int wait(timeout_t timeout=Timer::inf)
Socket i/o timer setting.
static socket_t create(int family, int type, int protocol)
Create a socket object unbound.
static char * hostname(const struct sockaddr *address, char *buffer, size_t size)
Lookup and return the host name associated with a socket address.
cidr()
Create an uninitialized cidr.
void set(const struct addrinfo *list)
Assign our pointer from an address list.
struct addrinfo * getList(void) const
Get the full socket address list from the object.
static int nodelay(socket_t socket)
Set tcp nodelay option on socket descriptor.
static int family(const struct sockaddr_internet &address)
Get the address family of an internet socket address object.
static unsigned store(struct sockaddr_storage *storage, const struct sockaddr *address)
Store an address into an address object.
bool connected(void) const
Test if socket is connected.
struct addrinfo * operator*() const
Return the full socket address list by pointer reference.
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...
unsigned remove(const struct addrinfo *address)
Remove members from another socket address list from ours.
Timer class to use when scheduling realtime events.
int type(void) const
Get the type of a socket.
int keepalive(bool enable)
Set socket for keepalive packets.
static in_port_t getPort(const struct sockaddr *address)
Returns the port of the socket address.
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.
address(const address &reference)
Copy constructor.
T copy(const T &src)
Convenience function to copy objects.
int nodelay(void) const
Set nodelay option for tcp socket.
static struct addrinfo * query(const char *host, const char *service, int type=SOCK_STREAM, int protocol=0)
Get an address list directly.
A bound socket used to listen for inbound socket connections.
const struct addrinfo * _nextaddrinfo(const struct addrinfo *addrinfo) const
Helper function for linked_pointer<struct sockaddr>.
bool operator==(const address &other) const
Compare two address lists.
int join(const struct addrinfo *list, const int ifindex=0)
Join socket to multicast group.
static int disconnect(socket_t socket)
Disconnect a connected socket descriptor.
bool ccid(uint8_t id)
Set ccid of dccp socket.
A generic socket address class.
int getError(void) const
Get socket error code.
Socket(const char *address, const char *port, int family=0, int type=0, int protocol=0)
Create a bound socket.
static int remote(socket_t socket, struct sockaddr_storage *address)
Get remote address to which the socket is connected.
address(const char *host, const char *service, int type=SOCK_STREAM)
Construct a socket address list for a service.
static unsigned keyhost(const struct sockaddr *address, unsigned size)
Convert a socket host address into a hash map index.
virtual ~Socket()
Shutdown, close, and destroy socket.
address(const addrinfo *address)
Construct a socket address from an addrinfo structure.
void copy(const struct addrinfo *address)
Copy an existing addrinfo into our object.
static void v4mapping(bool enable)
Set the default socket behavior for v6-v4 mapping.
static bool isLoopback(const struct sockaddr *address)
Test if the socket address is ADDR_LOOPBACK: 127.0.0.1 or ::1.
static bool is_numeric(const char *string)
Simple function to validate that a given IP address string is a numeric address.
static char * query(const struct sockaddr *address, char *buffer, socklen_t size)
Get the hostname of a socket address.
static int local(socket_t socket, struct sockaddr_storage *address)
Get local address to which the socket is bound.
void add(const char *hostname, const char *service=NULL, int type=SOCK_STREAM)
Append additional host addresses to our list.
void setAny(int family=0)
Clear the address list and set the first address to be the ADDR_ANY of the current family,...
static int recvsize(socket_t socket, unsigned size)
Set the receive size of a socket descriptor.
const struct sockaddr * operator*() const
Return the full socket address list by pointer reference.
static int sendsize(socket_t socket, unsigned size)
Set the send size of a socket descriptor.
int disconnect(void)
Disconnect a connected socket.
bool operator!() const
Test if we have no address list.
size_t peek(void *data, size_t number) const
Peek at data waiting in the socket receive buffer.
const char * getName(void) const
Get the saved name of our cidr.
const struct sockaddr * _getaddrinfo(const struct addrinfo *addrinfo) const
Helper function for linked_pointer<struct sockaddr>.
static socklen_t len(const struct sockaddr *address)
Get the size of a socket address.
bool is_pending(unsigned value)
See the number of bytes in the receive queue.
TCPServer(const char *address, const char *service, unsigned backlog=5)
Create and bind a tcp server.
bool remove(const struct sockaddr *address)
Remove an individual socket address from our address list.
static size_t getLength(const struct sockaddr *address)
Returns the size of the socket address according to the family.
bool eq_host(const struct sockaddr *s1, const struct sockaddr *s2)
Compare two host addresses to see if equal.
static bool is_null(const char *string)
Simple function to validate that a given IP address string is a "zero" address.
static void setPort(struct sockaddr *address, in_port_t port)
Set the port of the socket address.
static bool eq_inet(const struct sockaddr_internet *address1, const struct sockaddr_internet *address2)
Compare socket addresses.
Common base class for all objects that can be formed into a linked list.
Socket()
Create a socket object for use.
static int drop(socket_t socket, const struct addrinfo *list, const int ifindex=0)
Drop socket descriptor from multicast group.
A generic socket base class.
int priority(int scheduling)
Set packet priority, 0 to 6 unless privileged.
static int blocking(socket_t socket, bool enable)
Set socket blocking I/O mode of socket descriptor.
static bool eq_from(const struct sockaddr_storage *address1, const struct sockaddr_storage *address2)
Compare socket addresses.
bool eq(const struct sockaddr *s1, const struct sockaddr *s2)
Compare two socket addresses to see if equal.
Socket(socket_t socket)
Create socket from existing socket descriptor.
bool waitSending(timeout_t timeout=0) const
Test for output data sent.
int blocking(bool enable)
Set socket blocking I/O mode.
Linked objects, lists, templates, and containers.
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.
socket_t getsocket(void) const
Get the socket descriptor of the listener.
static int multicast(socket_t socket, unsigned ttl=1)
Set multicast mode and multicast broadcast range for socket descriptor.
address(const in6_addr &address, in_port_t port=0)
Construct a socket address from an IPv6 address and a port number.
static unsigned keyindex(const struct sockaddr *address, unsigned size)
Convert a socket address and service into a hash map index.
static bool eq_subnet(const struct sockaddr *address1, const struct sockaddr *address2)
See if both addresses are in the same subnet.
unsigned insert(const struct addrinfo *address)
Insert unique members from another socket address list to ours.
int ttl(uint8_t time)
Set the time to live before packets expire.
const struct sockaddr * addr(Socket::address &address)
A convenience function to convert a socket address list into a socket address.
void set(const char *string)
Set our cidr to a string address.
void set(Socket::address &list)
Assign our pointer from an address list.
A copy-on-write string class that operates by reference count.
static int family(socket_t socket)
Get the address family of the socket descriptor.
An object that holds ipv4 or ipv6 binary encoded host addresses.
void set(struct sockaddr *address)
Set an individual socket address for our address list.
static unsigned store(struct sockaddr_internet *storage, const struct sockaddr *address)
Store an address into an internet address object.
cidr(const char *string)
Create an unlinked cidr from a string.
bool operator!=(const struct sockaddr *address) const
Test if a given socket address falls outside this cidr.
static int bindto(socket_t socket, const struct sockaddr *address)
Bind the socket descriptor to a known interface.
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.
unsigned getMask(void) const
Get the number of bits in the cidr bitmask.
static void cancel(socket_t socket)
Cancel pending i/o by shutting down the socket.
bool is_any() const
Test if the first socket address is ADDR_ANY: 0.0.0.0 or ::0.
void set(int family, const char *address, int type=SOCK_STREAM, int protocol=0)
Set an entry for host binding.
int connectto(struct addrinfo *list)
Connect our socket to a remote host from an address list.
static int ttl(socket_t socket, uint8_t time)
Set the time to live for the socket descriptor.
const struct sockaddr * operator->() const
Return member from typed object our pointer references.
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 sockaddr_storage any(int family)
Get a ADDR_ANY socket address of the given family.
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...
bool wait(timeout_t timeout=Timer::inf) const
Wait for a pending connection.
static int broadcast(socket_t socket, bool enable)
Set socket for unicast mode broadcasts on socket descriptor.
static int loopback(socket_t socket, bool enable)
Set loopback to read multicast packets socket descriptor broadcasts.
A thread-safe atomic heap management system.
static ssize_t replyto(socket_t socket, const void *buffer, size_t size, int flags, const struct sockaddr_storage *address)
Send reply on socket.
An object that can hold a ipv4 or ipv6 socket address.
Helper class for linked_pointer template.
static socket_t acceptfrom(socket_t socket, struct sockaddr_storage *address=NULL)
Accept a socket connection from a remote host.
static in_port_t port(const struct sockaddr_internet *address)
Get the service port of an inet socket.
static struct sockaddr * dup(struct sockaddr *address)
Duplicate a socket address.
int broadcast(bool enable)
Set socket for unicast mode broadcasts.
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.
static int error(const socket_t socket)
Get socket error code of socket descriptor.
const struct sockaddr * get(void) const
Get the first socket address in our address list.
static int priority(socket_t socket, int scheduling)
Set packet priority of socket descriptor.
address()
Construct an empty address.
static int connectto(socket_t socket, struct addrinfo *list)
Connect socket descriptor to a remote host from an address list.
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...
static socket_t create(const Socket::address &address)
Create a connected socket for a service.
void shutdown(void)
Shutdown the socket communication channel.
static int error(void)
Return error code of last socket operation,.
Socket(int family, int type, int protocol=0)
Create an unbound socket of a specific type.
void setLoopback(int family=0)
Clear the address list and set the first address to be the ADDR_LOOPBACK of the current family,...
int tos(int type)
Set the type of service field of outgoing packets.
Common namespace for all ucommon objects.
int sendsize(unsigned size)
Set the size of the socket send buffer.
socket_t _getaddrsock(const struct addrinfo *addrinfo) const
Helper function for linked_pointer<struct sockaddr>.
void release(void)
Shutdown and close the socket.
size_t readline(char *data, size_t size)
Read a newline of text data from the socket and save in NULL terminated string.
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 const cidr * container(const policy *policy, const struct sockaddr *address)
Get the largest container cidr entry in a list that matches the socket address.
static void init(void)
Initialize socket subsystem.
unsigned pending(void) const
Get the number of bytes of data in the socket receive buffer.
static sockaddr_storage loopback(int family)
Get a ADDR_LOOPBACK socket address of the given family.
int loopback(bool enable)
Set loopback to read multicast packets we broadcast.
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.
void set(const char *hostname, const char *service=NULL, int type=SOCK_STREAM)
Set the host addresses to form a new list.
size_t printf(const char *format,...)
Print formatted string to socket.
static void query(int family)
Set default socket family preference for query options when the socket type is otherwise not specifie...
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.
static int join(socket_t socket, const struct addrinfo *list, const int ifindex=0)
Join socket descriptor to multicast group.
A smart pointer template for iterating linked lists.
void setPort(in_port_t port)
Set the port of all addresses in the list.
address(int family, const char *address, int type=SOCK_STREAM, int protocol=0)
Construct a socket address.
static const cidr * find(const policy *policy, const struct sockaddr *address)
Find the smallest cidr entry in a list that matches the socket address.
address(const char *hostname, in_port_t port=0)
Construct a socket address from host and service.
bool operator==(const struct sockaddr *address) const
Test if a given socket address falls within this cidr.
static void release(socket_t socket)
Release (close) a socket.
address(const sockaddr &address)
Construct a socket address from a sockaddr object.
in_port_t getPort(void) const
Get the port of the first address .
int multicast(unsigned ttl=1)
Set multicast mode and multicast broadcast range.
cidr(policy **policy, const char *string, const char *name)
Create a named cidr entry on a specified policy chain.
int recvsize(unsigned size)
Set the size of the socket receive buffer.
address & operator=(const address &rhs)
Assignment operator.
void add(sockaddr *address)
Add an individual socket address to our address list.
static unsigned segsize(socket_t socket, unsigned size=0)
Set segment size and get MTU.
socket_t operator*() const
Get the socket descriptor by pointer reference.
static bool isAny(const struct sockaddr *address)
Test if the socket address is ADDR_ANY: 0.0.0.0 or ::0.
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 addr...
static socket_t create(const struct addrinfo *address, int type, int protocol)
Create a connected socket.
void set(const char *hostname, in_port_t service=0)
Set a socket address from host and service.
static int tos(socket_t socket, int type)
Set type of service of socket descriptor.
int sendwait(unsigned size)
Set the size to wait before sending.
static int type(const socket_t socket)
Get the type of a socket.
bool is_member(const struct sockaddr *address) const
Test if a given socket address falls within this cidr.
static bool eq_host(const struct sockaddr *address1, const struct sockaddr *address2)
Compare socket host addresses.
socket_t operator()(void) const
Get socket as expression operator.
static int family(const struct sockaddr_storage &address)
Get the address family of a socket address object.
address withPort(in_port_t port) const
Returns a copy of this address list with the specified port set.
const struct sockaddr * get(int family) const
Get the first socket address of specified family from our list.
size_t writeto(const void *data, size_t number, const struct sockaddr *address=NULL)
Write data to the socket send buffer.
int drop(const struct addrinfo *list, const int ifindex=0)
Drop socket from multicast group.
Realtime timers and timer queues.
static unsigned pending(socket_t socket)
Get the number of bytes pending in the receive buffer of a socket descriptor.
linked_pointer & operator=(Socket::address &list)
Assign our pointer from an address list.
static int keepalive(socket_t socket, bool enable)
Set socket for keepalive packets for socket descriptor.
void cancel(void)
Cancel pending i/o by shutting down the socket.
unsigned segsize(unsigned size)
Set segment size and get mtu of a socket.
struct sockaddr * find(const struct sockaddr *addr) const
Find a specific socket address in our address list.
socket_t accept(struct sockaddr_storage *address=NULL) const
Accept a socket connection.
bool operator!() const
Test if we have no address list.
size_t getLength(void) const
Get the address size of the first address.
socket_t operator*() const
Get the socket descriptor of the listener by pointer reference.
static in_port_t port(const struct sockaddr *address)
Get the service port of a socket.
struct addrinfo * addrinfo(Socket::address &address)
A convenience function to convert a socket address list into an addrinfo.
A class to hold internet segment routing rules.
int family(void) const
Get the family of the first member in a list of services.
linked_pointer & operator=(const struct addrinfo *list)
Assign our pointer from an address list.
size_t readfrom(void *data, size_t number, struct sockaddr_storage *address=NULL)
Read data from the socket receive buffer.
static struct sockaddr_in * ipv4(struct sockaddr *address)
Convert address object into ipv4 address.
static unsigned copy(struct sockaddr *target, const struct sockaddr *origin)
Copy a socket address.
cidr(policy **policy, const char *string)
Create an unnamed cidr entry on a specified policy chain.
cidr(const cidr &existing)
Construct a copy of an existing cidr.
int err(void) const
Get error code.
bool is_loopback() const
Test if the first socket address is ADDR_LOOPBACK: 127.0.0.1 or ::1.
static bool equal(const struct sockaddr *address1, const struct sockaddr *address2)
Compare socket addresses.
static void release(struct addrinfo *list)
Release an address list directly.
~address()
Destroy address.
address(const in_addr &address, in_port_t port=0)
Construct a socket address from an IPv4 address and a port number.
static int sendwait(socket_t socket, unsigned size)
Set the size to wait before sending.
linked_pointer()
Create a linked pointer not attached to a list.
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.
LinkedObject policy
A convenience type for using a pointer to a linked list as a policy chain.
static struct sockaddr_in6 * ipv6(struct sockaddr *address)
Convert address object into ipv6 address.
static bool wait(socket_t socket, timeout_t timeout=0)
Test for pending input data.
bool wait(timeout_t timeout=0) const
Test for pending input data.
void next(void)
Move (iterate) pointer to next member in linked list.
address(int family, const char *hostname, const char *service=NULL)
Construct a socket address for an existing socket.
static bool ccid(socket_t socket, uint8_t id)
Set congestion control id.
A common string class and character string support functions.