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
86 typedef struct sockaddr *sockaddr_t;
88 typedef struct sockaddr sockaddr_struct;
105 #if defined(AF_INET6) || defined(__CYGWIN__)
117 struct sockaddr_in6 ipv6;
119 struct sockaddr_in ipv4;
120 struct sockaddr address;
127 struct sockaddr_in ipv4;
128 struct sockaddr address;
132 struct sockaddr_storage
137 char sa_data[
sizeof(
struct sockaddr_in)];
147 #define IPPROTO_DCCP 23
154 #define DCCP_SOCKOPT_AVAILABLE_CCIDS 12
155 #define DCCP_SOCKOPT_CCID 13
156 #define DCCP_SOCKOPT_TX_CCID 14
157 #define DCCP_SOCKOPT_RX_CCID 15
177 unsigned mask(
const char *cp)
const;
181 unsigned mask(
void)
const;
200 cidr(
const char *
string);
207 cidr(policy **policy,
const char *
string);
215 cidr(policy **policy,
const char *
string,
const char *name);
229 static const cidr *find(
const policy *policy,
const struct sockaddr *address);
238 static const cidr *container(
const policy *policy,
const struct sockaddr *address);
276 {
return broadcast();}
289 void set(
const char *
string);
296 bool is_member(
const struct sockaddr *address)
const;
304 {
return is_member(address);}
312 {
return !is_member(address);}
342 static struct addrinfo *query(
const char *host,
const char *service,
int type = SOCK_STREAM,
int protocol = 0);
375 address(
int family,
const char *
address,
int type = SOCK_STREAM,
int protocol = 0);
389 address(
int family,
const char *hostname,
const char *service = NULL);
397 address(
const char *host,
const char *service,
int type = SOCK_STREAM);
406 address(
const char *hostname, in_port_t port = 0);
411 address(
const in_addr& address, in_port_t port = 0);
416 address(
const in6_addr& address, in_port_t port = 0);
439 address(
const address& reference);
445 address& operator=(
const address& rhs);
457 bool operator==(
const address& other)
const;
459 inline bool operator!=(
const address& other)
const
460 {
return !(*
this==other);}
462 inline bool equals(
const address& other)
const
463 {
return *
this == other;}
469 struct sockaddr *
get(void)
const;
471 inline struct sockaddr *getAddr(
void)
const
474 inline struct sockaddr *operator()(
void)
const
481 inline operator struct sockaddr *()
const
489 struct sockaddr *
get(
int family)
const;
491 inline struct sockaddr *operator()(
int family)
const
492 {
return get(family);}
494 inline operator struct sockaddr_in *()
const
495 {
return (
struct sockaddr_in *)
get(AF_INET);}
498 inline operator struct sockaddr_in6 *()
const
499 {
return (
struct sockaddr_in6 *)
get(AF_INET6);}
506 int family(
void)
const;
520 {
return getPort(
get());}
526 void setPort(in_port_t port);
532 address withPort(in_port_t port)
const;
538 struct sockaddr *find(
const struct sockaddr *
addr)
const;
573 size_t print(
char* dst,
size_t dst_sz,
bool port=
false,
bool force_brackets=
false)
const
574 {
return print(
get(), dst, dst_sz, port, force_brackets);}
580 inline operator bool()
const
581 {
return list != NULL;}
583 inline bool isValid()
const
584 {
return list != NULL;}
591 {
return list == NULL;}
599 {
return isAny(
get());}
607 void setAny(
int family = AF_UNSPEC);
615 {
return isLoopback(
get());}
623 void setLoopback(
int family = AF_UNSPEC);
636 void set(
const char *hostname,
const char *service = NULL,
int type = SOCK_STREAM);
644 void add(
const char *hostname,
const char *service = NULL,
int type = SOCK_STREAM);
653 void set(
int family,
const char *address,
int type = SOCK_STREAM,
int protocol = 0);
659 void add(sockaddr *address);
666 unsigned insert(
const struct addrinfo *address);
673 unsigned remove(
const struct addrinfo *address);
680 bool remove(
const struct sockaddr *address);
688 bool insert(
const struct sockaddr *address);
689 inline bool insert(
const struct sockaddr& address)
690 {
return insert(&address);}
703 void set(
struct sockaddr *address);
710 void set(
const char *hostname, in_port_t service = 0);
717 {
return len(address);}
723 static in_port_t getPort(
const struct sockaddr *address);
730 static void setPort(
struct sockaddr *address, in_port_t port);
737 static bool isAny(
const struct sockaddr *address);
743 static void setAny(
struct sockaddr *sa);
748 static sockaddr_storage any(
int family);
755 static bool isLoopback(
const struct sockaddr *address);
762 static void setLoopback(
struct sockaddr *sa);
767 static sockaddr_storage loopback(
int family);
774 static struct sockaddr *
dup(
struct sockaddr *address);
781 static struct sockaddr_in *ipv4(
struct sockaddr *address);
789 static struct sockaddr_in6 *ipv6(
struct sockaddr *address);
804 static size_t print(
const struct sockaddr *src,
char* dst,
size_t dst_sz,
bool port=
false,
bool ipv6_brackets=
false);
807 friend class address;
839 Socket(
int family,
int type,
int protocol = 0);
850 Socket(
const char *address,
const char *port,
int family = AF_UNSPEC,
int type = 0,
int protocol = 0);
866 static void cancel(socket_t socket);
876 inline int err(
void)
const
884 bool is_pending(
unsigned value);
890 bool connected(
void)
const;
898 bool wait(timeout_t timeout = 0)
const;
905 {
return nodelay(so);}
914 static bool wait(socket_t socket, timeout_t timeout = 0);
922 bool waitSending(timeout_t timeout = 0)
const;
929 {
return pending(so);}
937 {
return broadcast(so, enable);}
945 {
return keepalive(so, enable);}
953 {
return blocking(so, enable);}
961 {
return multicast(so, ttl);}
969 {
return loopback(so, enable);}
983 inline int ttl(
unsigned char time)
984 {
return ttl(so, time);}
992 {
return sendsize(so, size);}
1000 {
return sendwait(so, size);}
1009 {
return recvsize(so, size);}
1016 static int type(socket_t socket);
1024 static unsigned segsize(socket_t socket,
unsigned size = 0);
1032 static bool ccid(socket_t socket, uint8_t
id);
1047 {
return segsize(so, size);}
1055 {
return ccid(so,
id);}
1066 {
return tos(so, type);}
1075 {
return priority(so, scheduling);}
1081 {::shutdown(so, SHUT_RDWR);}
1090 int connectto(
struct addrinfo *list);
1098 int disconnect(
void);
1105 int join(
const struct addrinfo *list,
const int ifindex = 0);
1112 int drop(
const struct addrinfo *list,
const int ifindex = 0);
1127 size_t peek(
void *data,
size_t number)
const;
1136 size_t readfrom(
void *data,
size_t number,
struct sockaddr_storage *address = NULL);
1145 size_t writeto(
const void *data,
size_t number,
const struct sockaddr *address = NULL);
1159 size_t readline(
char *data,
size_t size);
1166 size_t printf(
const char *format, ...) __PRINTF(2,3);
1179 size_t readline(
String& buffer);
1192 static ssize_t readline(socket_t socket,
char *data,
size_t size, timeout_t timeout =
Timer::inf);
1200 static ssize_t printf(socket_t socket, const
char *format, ...) __PRINTF(2,3);
1209 size_t writes(const
char *
string);
1221 bool operator!() const;
1228 Socket& operator=(socket_t socket);
1234 inline operator socket_t()
const
1250 static unsigned pending(socket_t socket);
1258 static int sendsize(socket_t socket,
unsigned size);
1266 static int sendwait(socket_t socket,
unsigned size);
1274 static int recvsize(socket_t socket,
unsigned size);
1284 static int connectto(socket_t socket,
struct addrinfo *list);
1291 static int disconnect(socket_t socket);
1299 static int drop(socket_t socket,
const struct addrinfo *list,
const int ifindex = 0);
1307 static int join(socket_t socket,
const struct addrinfo *list,
const int ifindex = 0);
1314 static int error(socket_t socket);
1322 static int multicast(socket_t socket,
unsigned ttl = 1);
1330 static int loopback(socket_t socket,
bool enable);
1338 static int blocking(socket_t socket,
bool enable);
1346 static int keepalive(socket_t socket,
bool enable);
1354 static int broadcast(socket_t socket,
bool enable);
1361 static int nodelay(socket_t socket);
1369 static int priority(socket_t socket,
int scheduling);
1377 static int tos(socket_t socket,
int type);
1385 static int ttl(socket_t socket,
unsigned char time);
1391 static int family(socket_t socket);
1398 inline static int family(
const struct sockaddr_storage& address)
1399 {
return ((
const struct sockaddr *)&address)->sa_family;}
1407 {
return address.address.sa_family;}
1418 static ssize_t recvfrom(socket_t socket,
void *buffer,
size_t size,
int flags = 0,
struct sockaddr_storage *address = NULL);
1429 static ssize_t sendto(socket_t socket,
const void *buffer,
size_t size,
int flags = 0,
const struct sockaddr *address = NULL);
1440 inline static ssize_t
replyto(socket_t socket,
const void *buffer,
size_t size,
int flags,
const struct sockaddr_storage *address)
1441 {
return sendto(socket, buffer, size, flags, (
const struct sockaddr *)address);}
1453 {
return sendto(socket, buffer, size, flags, (
const struct sockaddr *)address);}
1464 static ssize_t recvinet(socket_t socket,
void *buffer,
size_t size,
int flags = 0,
struct sockaddr_internet *address = NULL);
1474 static int bindto(socket_t socket,
const char *address,
const char *service,
int protocol = 0);
1483 static int listento(socket_t socket,
const struct sockaddr *address,
int backlog = 5);
1491 static int bindto(socket_t socket,
const struct sockaddr *address);
1499 static socket_t acceptfrom(socket_t socket,
struct sockaddr_storage *address = NULL);
1508 static socket_t create(
int family,
int type,
int protocol);
1517 static socket_t create(
const struct addrinfo *address,
int type,
int protocol);
1528 static socket_t create(
const char *iface,
const char *service,
int family = AF_UNSPEC,
int type = 0,
int protocol = 0);
1541 static void release(socket_t socket);
1550 static char *hostname(
const struct sockaddr *address,
char *buffer,
size_t size);
1571 static socklen_t query(socket_t socket,
struct sockaddr_storage *address,
const char *hostname,
const char *service);
1578 static socklen_t len(
const struct sockaddr *address);
1587 static bool equal(
const struct sockaddr *address1,
const struct sockaddr *address2);
1595 static unsigned copy(
struct sockaddr *target,
const struct sockaddr *origin);
1603 static unsigned store(
struct sockaddr_storage *storage,
const struct sockaddr *address);
1611 static unsigned store(
struct sockaddr_internet *storage,
const struct sockaddr *address);
1620 static bool eq_host(
const struct sockaddr *address1,
const struct sockaddr *address2);
1629 inline static bool eq_from(
const struct sockaddr_storage *address1,
const struct sockaddr_storage *address2)
1630 {
return equal((
const struct sockaddr *)address1, (
const struct sockaddr *)address2);}
1640 {
return equal((
const struct sockaddr *)address1, (
const struct sockaddr *)address2);}
1649 static bool eq_subnet(
const struct sockaddr *address1,
const struct sockaddr *address2);
1658 static int via(
struct sockaddr *address,
const struct sockaddr *destination);
1667 static char *query(
const struct sockaddr *address,
char *buffer, socklen_t size);
1674 static short service(
const struct sockaddr *address);
1676 inline static in_port_t port(
const struct sockaddr *address) {
1677 return service(address);
1686 return service((
const struct sockaddr *)address);
1690 return service((
const struct sockaddr *)address);
1699 static unsigned keyindex(
const struct sockaddr *address,
unsigned size);
1707 static unsigned keyhost(
const struct sockaddr *address,
unsigned size);
1712 static void init(
void);
1718 static void init(
const char *program);
1725 static void query(
int family);
1733 static void v4mapping(
bool enable);
1739 static int error(
void);
1749 static bool is_null(
const char *
string);
1758 static bool is_numeric(
const char *
string);
1768 static int local(socket_t socket,
struct sockaddr_storage *address);
1778 static int remote(socket_t socket,
struct sockaddr_storage *address);
1801 ListenSocket(
const char *address,
const char *service,
unsigned backlog = 5,
int family = AF_UNSPEC,
int type = 0,
int protocol = 0);
1813 static socket_t create(
const char *address,
const char *service,
unsigned backlog = 5,
int family = AF_UNSPEC,
int type = 0,
int protocol = 0);
1820 socket_t accept(
struct sockaddr_storage *address = NULL)
const;
1834 inline operator socket_t()
const
1851 inline socket_t handle(
void)
const
1874 TCPServer(
const char *address,
const char *service,
unsigned backlog = 5);
1917 inline operator struct sockaddr *()
const
1927 inline operator struct sockaddr_in *()
const
1930 inline struct sockaddr_in *in(
void)
const
1934 inline operator struct sockaddr_in6 *()
const
1937 inline struct sockaddr_in6 *in6(
void)
const
1951 inline operator bool()
const
1952 {
return ptr != NULL;}
1995 {
return ptr == NULL;}
1997 inline void next(
void)
2016 {
return address.
get();}
2025 inline bool eq(
const struct sockaddr *s1,
const struct sockaddr *s2)
2035 inline bool eq(
const struct sockaddr_storage *s1,
const struct sockaddr_storage *s2)
2036 {
return Socket::equal((
const struct sockaddr *)s1, (
const struct sockaddr *)s2);}
2045 inline bool eq_host(
const struct sockaddr *s1,
const struct sockaddr *s2)
2048 inline bool eq_subnet(
const struct sockaddr *s1,
const struct sockaddr *s2)
2058 struct sockaddr_storage storage;
2061 inline SockAddress() {
2062 memset(&storage, 0,
sizeof(storage));
2065 inline SockAddress(
const SockAddress&
copy) {
2066 memcpy(&storage, ©.storage,
sizeof(storage));
2069 inline SockAddress(
const struct sockaddr *
addr) {
2073 inline SockAddress& operator=(
const SockAddress&
copy) {
2074 memcpy(&storage, ©.storage,
sizeof(storage));
2078 inline SockAddress& operator=(
const struct sockaddr *
addr) {
2083 inline operator const struct sockaddr *()
const {
2084 return (
const struct sockaddr*)&storage;
2087 inline struct sockaddr *operator*() {
2088 return (
struct sockaddr *)&storage;
2091 inline const struct sockaddr *
get()
const {
2092 return (
const struct sockaddr *)&storage;
2095 inline socklen_t size() {
2096 return sizeof(storage);
2099 inline bool operator==(
const SockAddress& check)
const {
2103 inline bool operator!=(
const SockAddress& check)
const {
2107 inline bool operator==(
const struct sockaddr *check)
const {
2111 inline bool operator!=(
const struct sockaddr *check)
const {
2122 inline InetAddress() {
2123 memset(&storage, 0,
sizeof(storage));
2126 inline InetAddress(
const InetAddress&
copy) {
2127 memcpy(&storage, ©.storage,
sizeof(storage));
2130 inline InetAddress(
const struct sockaddr *
addr) {
2134 inline InetAddress& operator=(
const InetAddress&
copy) {
2135 memcpy(&storage, ©.storage,
sizeof(storage));
2139 inline InetAddress& operator=(
const struct sockaddr *
addr) {
2144 inline operator const struct sockaddr *()
const {
2145 return (
const struct sockaddr*)&storage;
2148 inline struct sockaddr *operator*() {
2149 return (
struct sockaddr *)&storage;
2152 inline const struct sockaddr *
get()
const {
2153 return (
const struct sockaddr *)&storage;
2156 inline socklen_t size() {
2157 return sizeof(storage);
2160 inline bool operator==(
const SockAddress& check)
const {
2164 inline bool operator!=(
const SockAddress& check)
const {
2168 inline bool operator==(
const struct sockaddr *check)
const {
2172 inline bool operator!=(
const struct sockaddr *check)
const {
2178 typedef TCPServer tcpserv_t;
struct addrinfo * _nextaddrinfo(struct addrinfo *addrinfo)
Helper function for linked_pointer.
A common string class and character string support functions.
static unsigned store(struct sockaddr_storage *storage, const struct sockaddr *address)
Store an address into an address object.
bool operator==(const struct sockaddr *address) const
Test if a given socket address falls within this cidr.
void release(SharedAccess &object)
Convenience function to unlock shared object through it's protocol.
socket_t getsocket(void) const
Get the socket descriptor of the listener.
int priority(int scheduling)
Set packet priority, 0 to 6 unless privileged.
int loopback(bool enable)
Set loopback to read multicast packets we broadcast.
static bool eq_host(const struct sockaddr *address1, const struct sockaddr *address2)
Compare socket host addresses.
bool eq_host(const struct sockaddr *s1, const struct sockaddr *s2)
Compare two host addresses to see if equal.
struct sockaddr * _getaddrinfo(struct addrinfo *addrinfo)
Helper function for linked_pointer.
A thread-safe atomic heap management system.
void operator=(Socket::address &list)
Assign our pointer from an address list.
int nodelay(void) const
Set nodelay option for tcp socket.
A generic socket base class.
An object that holds ipv4 or ipv6 binary encoded host addresses.
Common base class for all objects that can be formed into a linked list.
int tos(int type)
Set the type of service field of outgoing packets.
int ttl(unsigned char time)
Set the time to live before packets expire.
A class to hold internet segment routing rules.
struct sockaddr_internet inetsockaddr_t
An object that can hold a ipv4 or ipv6 socket address.
struct addrinfo * addrinfo(Socket::address &address)
A convenience function to convert a socket address list into an addrinfo.
inethostaddr_t getNetmask(void) const
Get the effective network mask for our cidr block.
socket_t operator*() const
Get the socket descriptor of the listener by pointer reference.
static int family(const struct sockaddr_storage &address)
Get the address family of a socket address object.
unsigned short strsize_t
A convenience class for size of strings.
ObjectProtocol * copy(ObjectProtocol *object)
Convenience function to access object copy.
A copy-on-write string class that operates by reference count.
socket_t operator()(void) const
Get socket as expression operator.
in_port_t getPort(void) const
Get the port of the first address .
bool isLoopback() const
Test if the first socket address is ADDR_LOOPBACK: 127.0.0.1 or ::1.
Linked objects, lists, templates, and containers.
int sendwait(unsigned size)
Set the size to wait before sending.
bool operator!() const
Test if we have no address list.
bool eq(const struct sockaddr *s1, const struct sockaddr *s2)
Compare two socket addresses to see if equal.
struct sockaddr * operator*() const
Return the full socket address list by pointer reference.
struct addrinfo * operator*() const
Return the full socket address list by pointer reference.
int broadcast(bool enable)
Set socket for unicast mode broadcasts.
T * dup(const T &object)
Convenience function to duplicate object pointer to heap.
T * init(T *memory)
Template function to initialize memory by invoking default constructor.
static size_t getLength(const struct sockaddr *address)
Returns the size of the socket address according to the family.
unsigned getMask(void) const
Get the number of bits in the cidr bitmask.
A bound socket used to listen for inbound socket connections.
bool isAny() const
Test if the first socket address is ADDR_ANY: 0.0.0.0 or ::0.
static bool equal(const struct sockaddr *address1, const struct sockaddr *address2)
Compare socket addresses.
LinkedObject policy
A convenience type for using a pointer to a linked list as a policy chain.
void set(struct addrinfo *list)
Assign our pointer from an address list.
inethostaddr_t getNetwork(void) const
Get the network host base address of our cidr block.
unsigned segsize(unsigned size)
Set segment size and get mtu of a socket.
An object that can hold a ipv4 or ipv6 socket address.
static bool eq_inet(const struct sockaddr_internet *address1, const struct sockaddr_internet *address2)
Compare socket addresses.
Realtime timers and timer queues.
struct sockaddr * addr(Socket::address &address)
A convenience function to convert a socket address list into a socket address.
void shutdown(void)
Shutdown the socket communication channel.
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...
int recvsize(unsigned size)
Set the size of the socket receive buffer.
bool ccid(uint8_t id)
Set ccid of dccp socket.
const char * getName(void) const
Get the saved name of our cidr.
int blocking(bool enable)
Set socket blocking I/O mode.
int multicast(unsigned ttl=1)
Set multicast mode and multicast broadcast range.
Common namespace for all ucommon objects.
struct addrinfo * getList(void) const
Get the full socket address list from the object.
int keepalive(bool enable)
Set socket for keepalive packets.
bool wait(timeout_t timeout=Timer::inf) const
Wait for a pending connection.
socket_t _getaddrsock(struct addrinfo *addrinfo)
Helper function for linked_pointer.
size_t getLength(void) const
Get the address size of the first address.
struct hostaddr_internet inethostaddr_t
An object that holds ipv4 or ipv6 binary encoded host addresses.
void next(void)
Move (iterate) pointer to next member in linked list.
int getError(void)
Get socket error code.
int getFamily(void) const
Get the address family of our cidr block object.
static short service(const struct sockaddr_internet *address)
Get the service port of an inet socket.
Timer class to use when scheduling realtime events.
A smart pointer template for iterating linked lists.
void wait(barrier_t &barrier)
Convenience function to wait on a barrier.
struct sockaddr * get(void) const
Get the first socket address in our address list.
static bool eq_subnet(const struct sockaddr *address1, const struct sockaddr *address2)
See if both addresses are in the same subnet.
socket_t operator*() const
Get the socket descriptor by pointer reference.
static ssize_t sendinet(socket_t socket, const void *buffer, size_t size, int flags, const struct sockaddr_internet *address)
Send to internet socket.
static int family(const struct sockaddr_internet &address)
Get the address family of an internet socket address object.
void set(Socket::address &list)
Assign our pointer from an address list.
bool wait(timeout_t timeout=0) const
Test for pending input data.
unsigned pending(void) const
Get the number of bytes of data in the socket receive buffer.
static const timeout_t inf
A value to use for infinite time.
static ssize_t replyto(socket_t socket, const void *buffer, size_t size, int flags, const struct sockaddr_storage *address)
Send reply on socket.
linked_pointer()
Create a linked pointer not attached to a list.
void operator=(struct addrinfo *list)
Assign our pointer from an address list.
static bool eq_from(const struct sockaddr_storage *address1, const struct sockaddr_storage *address2)
Compare socket addresses.
address(const addrinfo *address)
Construct a socket address from an addrinfo structure.
struct sockaddr * operator->() const
Return member from typed object our pointer references.
int type(void)
Get the type of a socket.
bool operator!=(const struct sockaddr *address) const
Test if a given socket address falls outside this cidr.
inethostaddr_t getBroadcast(void) const
Get the broadcast host address represented by our cidr.
bool operator!() const
Test if we have no address list.
int err(void) const
Get error code.
A generic tcp server class.
int sendsize(unsigned size)
Set the size of the socket send buffer.
address(const sockaddr &address)
Construct a socket address from a sockaddr object.
A generic socket address class.