26 #ifndef _UCOMMON_SOCKET_H_
27 #define _UCOMMON_SOCKET_H_
29 #ifndef _UCOMMON_TIMERS_H_
33 #ifndef _UCOMMON_LINKED_H_
37 #ifndef _UCOMMON_STRING_H_
46 #define SHUT_RDWR SD_BOTH
47 #define SHUT_WR SD_SEND
48 #define SHUT_RD SD_RECV
49 typedef uint16_t in_port_t;
50 typedef uint32_t in_addr_t;
53 #include <sys/socket.h>
55 #include <netinet/in.h>
59 #if defined(__ANDROID__)
60 typedef uint16_t in_port_t;
66 #ifndef IPTOS_LOWDELAY
67 #define IPTOS_LOWDELAY 0x10
68 #define IPTOS_THROUGHPUT 0x08
69 #define IPTOS_RELIABILITY 0x04
70 #define IPTOS_MINCOST 0x02
74 #define DEFAULT_FAMILY AF_UNSPEC
76 #define DEFAULT_FAMILY AF_INET
81 typedef struct sockaddr *sockaddr_t;
83 typedef struct sockaddr sockaddr_struct;
99 #if defined(AF_INET6) || defined(__CYGWIN__)
111 struct sockaddr_in6 ipv6;
113 struct sockaddr_in ipv4;
114 struct sockaddr address;
121 struct sockaddr_in ipv4;
122 struct sockaddr address;
126 struct sockaddr_storage
131 char sa_data[
sizeof(
struct sockaddr_in)];
141 #define IPPROTO_DCCP 23
148 #define DCCP_SOCKOPT_AVAILABLE_CCIDS 12
149 #define DCCP_SOCKOPT_CCID 13
150 #define DCCP_SOCKOPT_TX_CCID 14
151 #define DCCP_SOCKOPT_RX_CCID 15
171 unsigned mask(
const char *cp)
const;
175 unsigned mask(
void)
const;
194 cidr(
const char *
string);
201 cidr(policy **policy,
const char *
string);
209 cidr(policy **policy,
const char *
string,
const char *name);
223 static const cidr *find(
const policy *policy,
const struct sockaddr *address);
232 static const cidr *container(
const policy *policy,
const struct sockaddr *address);
270 {
return broadcast();}
283 void set(
const char *
string);
290 bool is_member(
const struct sockaddr *address)
const;
298 {
return is_member(address);}
306 {
return !is_member(address);}
332 static struct addrinfo *query(
const char *host,
const char *service,
int type = SOCK_STREAM,
int protocol = 0);
365 address(
int family,
const char *
address,
int type = SOCK_STREAM,
int protocol = 0);
379 address(
int family,
const char *hostname,
const char *service = NULL);
387 address(
const char *host,
const char *service,
int type = SOCK_STREAM);
396 address(
const char *hostname, in_port_t port = 0);
401 address(
const in_addr& address, in_port_t port = 0);
406 address(
const in6_addr& address, in_port_t port = 0);
429 address(
const address& reference);
435 address& operator=(
const address& rhs);
447 bool operator==(
const address& other)
const;
449 inline bool operator!=(
const address& other)
const
450 {
return !(*
this==other);}
452 inline bool equals(
const address& other)
const
453 {
return *
this == other;}
459 struct sockaddr *
get(void)
const;
461 inline struct sockaddr *getAddr(
void)
const
464 inline struct sockaddr *operator()(
void)
const
471 inline operator struct sockaddr *()
const
479 struct sockaddr *
get(
int family)
const;
481 inline struct sockaddr *operator()(
int family)
const
482 {
return get(family);}
484 inline operator struct sockaddr_in *()
const
485 {
return (
struct sockaddr_in *)
get(AF_INET);}
488 inline operator struct sockaddr_in6 *()
const
489 {
return (
struct sockaddr_in6 *)
get(AF_INET6);}
496 int family(
void)
const;
510 {
return getPort(
get());}
516 void setPort(in_port_t port);
522 address withPort(in_port_t port)
const;
528 struct sockaddr *find(
const struct sockaddr *
addr)
const;
563 size_t print(
char* dst,
size_t dst_sz,
bool port=
false,
bool force_brackets=
false)
const
564 {
return print(
get(), dst, dst_sz, port, force_brackets);}
570 inline operator bool()
const
571 {
return list != NULL;}
578 {
return list == NULL;}
586 {
return isAny(
get());}
594 void setAny(
int family = AF_UNSPEC);
602 {
return isLoopback(
get());}
610 void setLoopback(
int family = AF_UNSPEC);
623 void set(
const char *hostname,
const char *service = NULL,
int type = SOCK_STREAM);
631 void add(
const char *hostname,
const char *service = NULL,
int type = SOCK_STREAM);
640 void set(
int family,
const char *address,
int type = SOCK_STREAM,
int protocol = 0);
646 void add(sockaddr *address);
653 unsigned insert(
const struct addrinfo *address);
660 unsigned remove(
const struct addrinfo *address);
667 bool remove(
const struct sockaddr *address);
675 bool insert(
const struct sockaddr *address);
676 inline bool insert(
const struct sockaddr& address)
677 {
return insert(&address);}
690 void set(
struct sockaddr *address);
697 void set(
const char *hostname, in_port_t service = 0);
704 {
return len(address);}
710 static in_port_t getPort(
const struct sockaddr *address);
717 static void setPort(
struct sockaddr *address, in_port_t port);
724 static bool isAny(
const struct sockaddr *address);
730 static void setAny(
struct sockaddr *sa);
735 static sockaddr_storage any(
int family);
742 static bool isLoopback(
const struct sockaddr *address);
749 static void setLoopback(
struct sockaddr *sa);
754 static sockaddr_storage loopback(
int family);
761 static struct sockaddr *
dup(
struct sockaddr *address);
768 static struct sockaddr_in *ipv4(
struct sockaddr *address);
776 static struct sockaddr_in6 *ipv6(
struct sockaddr *address);
791 static size_t print(
const struct sockaddr *src,
char* dst,
size_t dst_sz,
bool port=
false,
bool ipv6_brackets=
false);
794 friend class address;
826 Socket(
int family,
int type,
int protocol = 0);
837 Socket(
const char *address,
const char *port,
int family = AF_UNSPEC,
int type = 0,
int protocol = 0);
853 static void cancel(socket_t socket);
863 inline int err(
void)
const
871 bool is_pending(
unsigned value);
877 bool connected(
void)
const;
892 {
return nodelay(so);}
909 bool waitSending(
timeout_t timeout = 0)
const;
916 {
return pending(so);}
924 {
return broadcast(so, enable);}
932 {
return keepalive(so, enable);}
940 {
return blocking(so, enable);}
948 {
return multicast(so, ttl);}
956 {
return loopback(so, enable);}
970 inline int ttl(
unsigned char time)
971 {
return ttl(so, time);}
979 {
return sendsize(so, size);}
987 {
return sendwait(so, size);}
996 {
return recvsize(so, size);}
1003 static int type(socket_t socket);
1011 static unsigned segsize(socket_t socket,
unsigned size = 0);
1019 static bool ccid(socket_t socket, uint8_t
id);
1034 {
return segsize(so, size);}
1042 {
return ccid(so,
id);}
1053 {
return tos(so, type);}
1062 {
return priority(so, scheduling);}
1068 {::shutdown(so, SHUT_RDWR);}
1077 int connectto(
struct addrinfo *list);
1085 int disconnect(
void);
1092 int join(
const struct addrinfo *list,
const int ifindex = 0);
1099 int drop(
const struct addrinfo *list,
const int ifindex = 0);
1114 size_t peek(
void *data,
size_t number)
const;
1123 size_t readfrom(
void *data,
size_t number,
struct sockaddr_storage *address = NULL);
1132 size_t writeto(
const void *data,
size_t number,
const struct sockaddr *address = NULL);
1146 size_t readline(
char *data,
size_t size);
1153 size_t printf(
const char *format, ...) __PRINTF(2,3);
1166 size_t readline(
String& buffer);
1179 static ssize_t readline(socket_t socket,
char *data,
size_t size,
timeout_t timeout =
Timer::inf);
1187 static ssize_t printf(socket_t socket, const
char *format, ...) __PRINTF(2,3);
1196 size_t writes(const
char *
string);
1208 bool operator!() const;
1215 Socket& operator=(socket_t socket);
1221 inline operator socket_t()
const
1237 static unsigned pending(socket_t socket);
1245 static int sendsize(socket_t socket,
unsigned size);
1253 static int sendwait(socket_t socket,
unsigned size);
1261 static int recvsize(socket_t socket,
unsigned size);
1271 static int connectto(socket_t socket,
struct addrinfo *list);
1278 static int disconnect(socket_t socket);
1286 static int drop(socket_t socket,
const struct addrinfo *list,
const int ifindex = 0);
1294 static int join(socket_t socket,
const struct addrinfo *list,
const int ifindex = 0);
1301 static int error(socket_t socket);
1309 static int multicast(socket_t socket,
unsigned ttl = 1);
1317 static int loopback(socket_t socket,
bool enable);
1325 static int blocking(socket_t socket,
bool enable);
1333 static int keepalive(socket_t socket,
bool enable);
1341 static int broadcast(socket_t socket,
bool enable);
1348 static int nodelay(socket_t socket);
1356 static int priority(socket_t socket,
int scheduling);
1364 static int tos(socket_t socket,
int type);
1372 static int ttl(socket_t socket,
unsigned char time);
1378 static int family(socket_t socket);
1385 inline static int family(
const struct sockaddr_storage& address)
1386 {
return ((
const struct sockaddr *)&address)->sa_family;}
1394 {
return address.address.sa_family;}
1405 static ssize_t recvfrom(socket_t socket,
void *buffer,
size_t size,
int flags = 0,
struct sockaddr_storage *address = NULL);
1416 static ssize_t sendto(socket_t socket,
const void *buffer,
size_t size,
int flags = 0,
const struct sockaddr *address = NULL);
1427 inline static ssize_t
replyto(socket_t socket,
const void *buffer,
size_t size,
int flags,
const struct sockaddr_storage *address)
1428 {
return sendto(socket, buffer, size, flags, (
const struct sockaddr *)address);}
1440 {
return sendto(socket, buffer, size, flags, (
const struct sockaddr *)address);}
1451 static ssize_t recvinet(socket_t socket,
void *buffer,
size_t size,
int flags = 0,
struct sockaddr_internet *address = NULL);
1461 static int bindto(socket_t socket,
const char *address,
const char *service,
int protocol = 0);
1470 static int listento(socket_t socket,
const struct sockaddr *address,
int backlog = 5);
1478 static int bindto(socket_t socket,
const struct sockaddr *address);
1486 static socket_t acceptfrom(socket_t socket,
struct sockaddr_storage *address = NULL);
1495 static socket_t create(
int family,
int type,
int protocol);
1504 static socket_t create(
const struct addrinfo *address,
int type,
int protocol);
1515 static socket_t create(
const char *iface,
const char *service,
int family = AF_UNSPEC,
int type = 0,
int protocol = 0);
1528 static void release(socket_t socket);
1537 static char *hostname(
const struct sockaddr *address,
char *buffer,
size_t size);
1558 static socklen_t query(socket_t socket,
struct sockaddr_storage *address,
const char *hostname,
const char *service);
1565 static socklen_t len(
const struct sockaddr *address);
1574 static bool equal(
const struct sockaddr *address1,
const struct sockaddr *address2);
1582 static unsigned copy(
struct sockaddr *target,
const struct sockaddr *origin);
1590 inline static unsigned store(
struct sockaddr_storage *storage,
const struct sockaddr *address)
1591 {
return copy((
struct sockaddr*)storage, address);}
1599 static unsigned store(
struct sockaddr_internet *storage,
const struct sockaddr *address);
1608 static bool eq_host(
const struct sockaddr *address1,
const struct sockaddr *address2);
1617 inline static bool eq_from(
const struct sockaddr_storage *address1,
const struct sockaddr_storage *address2)
1618 {
return equal((
const struct sockaddr *)address1, (
const struct sockaddr *)address2);}
1628 {
return equal((
const struct sockaddr *)address1, (
const struct sockaddr *)address2);}
1637 static bool eq_subnet(
const struct sockaddr *address1,
const struct sockaddr *address2);
1646 static int via(
struct sockaddr *address,
const struct sockaddr *destination);
1655 static char *query(
const struct sockaddr *address,
char *buffer, socklen_t size);
1662 static short service(
const struct sockaddr *address);
1670 {
return service((
const struct sockaddr *)address);}
1678 static unsigned keyindex(
const struct sockaddr *address,
unsigned size);
1686 static unsigned keyhost(
const struct sockaddr *address,
unsigned size);
1691 static void init(
void);
1697 static void init(
const char *program);
1704 static void query(
int family);
1712 static void v4mapping(
bool enable);
1718 static int error(
void);
1728 static bool is_null(
const char *
string);
1737 static bool is_numeric(
const char *
string);
1747 static int local(socket_t socket,
struct sockaddr_storage *address);
1757 static int remote(socket_t socket,
struct sockaddr_storage *address);
1777 ListenSocket(
const char *address,
const char *service,
unsigned backlog = 5,
int family = AF_UNSPEC,
int type = 0,
int protocol = 0);
1789 static socket_t create(
const char *address,
const char *service,
unsigned backlog = 5,
int family = AF_UNSPEC,
int type = 0,
int protocol = 0);
1796 socket_t accept(
struct sockaddr_storage *address = NULL)
const;
1810 inline operator socket_t()
const
1827 inline socket_t handle(
void)
const
1847 TCPServer(
const char *address,
const char *service,
unsigned backlog = 5);
1890 inline operator struct sockaddr *()
const
1900 inline operator struct sockaddr_in *()
const
1903 inline struct sockaddr_in *in(
void)
const
1907 inline operator struct sockaddr_in6 *()
const
1910 inline struct sockaddr_in6 *in6(
void)
const
1924 inline operator bool()
const
1925 {
return ptr != NULL;}
1968 {
return ptr == NULL;}
1970 inline void next(
void)
1989 {
return address.
get();}
1998 inline bool eq(
const struct sockaddr *s1,
const struct sockaddr *s2)
2008 inline bool eq(
const struct sockaddr_storage *s1,
const struct sockaddr_storage *s2)
2009 {
return Socket::equal((
const struct sockaddr *)s1, (
const struct sockaddr *)s2);}
2018 inline bool eq_host(
const struct sockaddr *s1,
const struct sockaddr *s2)
2021 inline bool eq_subnet(
const struct sockaddr *s1,
const struct sockaddr *s2)
2026 typedef TCPServer tcpserv_t;
unsigned segsize(unsigned size)
Set segment size and get mtu of a socket.
bool operator==(const struct sockaddr *address) const
Test if a given socket address falls within this cidr.
struct sockaddr_internet inetsockaddr_t
An object that can hold a ipv4 or ipv6 socket address.
An object that can hold a ipv4 or ipv6 socket address.
void set(struct addrinfo *list)
Assign our pointer from an address list.
void next(void)
Move (iterate) pointer to next member in linked list.
bool isAny() const
Test if the first socket address is ADDR_ANY: 0.0.0.0 or ::0.
struct hostaddr_internet inethostaddr_t
An object that holds ipv4 or ipv6 binary encoded host addresses.
int sendsize(unsigned size)
Set the size of the socket send buffer.
static size_t getLength(const struct sockaddr *address)
Returns the size of the socket address according to the family.
address(const addrinfo *list)
Construct a socket address from an addrinfo structure.
T * dup(const T &object)
Convenience function to duplicate object pointer to heap.
int multicast(unsigned ttl=1)
Set multicast mode and multicast broadcast range.
Linked objects, lists, templates, and containers.
int type(void)
Get the type of a socket.
unsigned long timeout_t
Typedef for millisecond timer values.
A bound socket used to listen for inbound socket connections.
void wait(barrier_t &barrier)
Convenience function to wait on a barrier.
void shutdown(void)
Shutdown the socket communication channel.
static bool eq_host(const struct sockaddr *address1, const struct sockaddr *address2)
Compare socket host addresses.
A copy-on-write string class that operates by reference count.
bool operator!() const
Test if we have no address list.
int keepalive(bool enable)
Set socket for keepalive packets.
T * init(T *memory)
Template function to initialize memory by invoking default constructor.
size_t getLength(void) const
Get the address size of the first address.
A generic tcp server class.
An object that holds ipv4 or ipv6 binary encoded host addresses.
static bool equal(const struct sockaddr *address1, const struct sockaddr *address2)
Compare socket addresses.
Common namespace for all ucommon objects.
socket_t _getaddrsock(struct addrinfo *addrinfo)
Helper function for linked_pointer.
struct addrinfo * getList(void) const
Get the full socket address list from the object.
struct sockaddr * operator*() const
Return the full socket address list by pointer reference.
static const timeout_t inf
A value to use for infinite time.
int ttl(unsigned char time)
Set the time to live before packets expire.
A smart pointer template for iterating linked lists.
int priority(int scheduling)
Set packet priority, 0 to 6 unless privileged.
Realtime timers and timer queues.
bool ccid(uint8_t id)
Set ccid of dccp socket.
socket_t operator*() const
Get the socket descriptor of the listener by pointer reference.
Common base class for all objects that can be formed into a linked list.
void release(SharedAccess &object)
Convenience function to unlock shared object through it's protocol.
socket_t operator*() const
Get the socket descriptor by pointer reference.
bool eq_host(const struct sockaddr *s1, const struct sockaddr *s2)
Compare two host addresses to see if equal.
int sendwait(unsigned size)
Set the size to wait before sending.
inethostaddr_t getBroadcast(void) const
Get the broadcast host address represented by our cidr.
bool wait(timeout_t timeout=0) const
Test for pending input data.
int getError(void)
Get socket error code.
unsigned short strsize_t
A convenience class for size of strings.
struct addrinfo * _nextaddrinfo(struct addrinfo *addrinfo)
Helper function for linked_pointer.
address(const sockaddr &address)
Construct a socket address from a sockaddr object.
socket_t operator()(void) const
Get socket as expression operator.
static bool eq_subnet(const struct sockaddr *address1, const struct sockaddr *address2)
See if both addresses are in the same subnet.
void operator=(Socket::address &list)
Assign our pointer from an address list.
struct sockaddr * get(void) const
Get the first socket address in our address list.
int blocking(bool enable)
Set socket blocking I/O mode.
struct addrinfo * addrinfo(Socket::address &address)
A convenience function to convert a socket address list into an addrinfo.
bool operator!() const
Test if we have no address list.
static int family(const struct sockaddr_storage &address)
Get the address family of a socket address object.
static ssize_t sendinet(socket_t socket, const void *buffer, size_t size, int flags, const struct sockaddr_internet *address)
Send to internet socket.
linked_pointer()
Create a linked pointer not attached to a list.
static int family(const struct sockaddr_internet &address)
Get the address family of an internet socket address object.
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.
int nodelay(void) const
Set nodelay option for tcp socket.
int broadcast(bool enable)
Set socket for unicast mode broadcasts.
in_port_t getPort(void) const
Get the port of the first address .
bool wait(timeout_t timeout=Timer::inf) const
Wait for a pending connection.
unsigned getMask(void) const
Get the number of bits in the cidr bitmask.
A generic socket address class.
static unsigned store(struct sockaddr_storage *storage, const struct sockaddr *address)
Store an address into an address object.
socket_t getsocket(void) const
Get the socket descriptor of the listener.
static bool eq_inet(const struct sockaddr_internet *address1, const struct sockaddr_internet *address2)
Compare socket addresses.
void set(Socket::address &list)
Assign our pointer from an address list.
struct sockaddr * addr(Socket::address &address)
A convenience function to convert a socket address list into a socket address.
int tos(int type)
Set the type of service field of outgoing packets.
int getFamily(void) const
Get the address family of our cidr block object.
struct addrinfo * operator*() const
Return the full socket address list by pointer reference.
struct sockaddr * operator->() const
Return member from typed object our pointer references.
A class to hold internet segment routing rules.
Timer class to use when scheduling realtime events.
const char * getName(void) const
Get the saved name of our cidr.
static ssize_t replyto(socket_t socket, const void *buffer, size_t size, int flags, const struct sockaddr_storage *address)
Send reply on socket.
inethostaddr_t getNetmask(void) const
Get the effective network mask for our cidr block.
bool operator!=(const struct sockaddr *address) const
Test if a given socket address falls outside this cidr.
void operator=(struct addrinfo *list)
Assign our pointer from an address list.
unsigned pending(void) const
Get the number of bytes of data in the socket receive buffer.
A generic socket base class.
A common string class and character string support functions.
ObjectProtocol * copy(ObjectProtocol *object)
Convenience function to access object copy.
struct sockaddr * _getaddrinfo(struct addrinfo *addrinfo)
Helper function for linked_pointer.
int err(void) const
Get error code.
static bool eq_from(const struct sockaddr_storage *address1, const struct sockaddr_storage *address2)
Compare socket addresses.
LinkedObject policy
A convenience type for using a pointer to a linked list as a policy chain.
static short service(const struct sockaddr_internet *address)
Get the service port of an inet socket.
int loopback(bool enable)
Set loopback to read multicast packets we broadcast.
bool eq(const struct sockaddr *s1, const struct sockaddr *s2)
Compare two socket addresses to see if equal.
inethostaddr_t getNetwork(void) const
Get the network host base address of our cidr block.
bool isLoopback() const
Test if the first socket address is ADDR_LOOPBACK: 127.0.0.1 or ::1.