UCommon
socket.h
Go to the documentation of this file.
1 // Copyright (C) 2006-2014 David Sugar, Tycho Softworks.
2 // Copyright (C) 2015 Cherokees of Idaho.
3 //
4 // This file is part of GNU uCommon C++.
5 //
6 // GNU uCommon C++ is free software: you can redistribute it and/or modify
7 // it under the terms of the GNU Lesser General Public License as published
8 // by the Free Software Foundation, either version 3 of the License, or
9 // (at your option) any later version.
10 //
11 // GNU uCommon C++ is distributed in the hope that it will be useful,
12 // but WITHOUT ANY WARRANTY; without even the implied warranty of
13 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 // GNU Lesser General Public License for more details.
15 //
16 // You should have received a copy of the GNU Lesser General Public License
17 // along with GNU uCommon C++. If not, see <http://www.gnu.org/licenses/>.
18 
27 #ifndef _UCOMMON_SOCKET_H_
28 #define _UCOMMON_SOCKET_H_
29 
30 #ifndef _UCOMMON_TIMERS_H_
31 #include <ucommon/timers.h>
32 #endif
33 
34 #ifndef _UCOMMON_LINKED_H_
35 #include <ucommon/linked.h>
36 #endif
37 
38 #ifndef _UCOMMON_STRING_H_
39 #include <ucommon/string.h>
40 #endif
41 
42 #ifndef _UCOMMON_TYPEREF_H_
43 #include <ucommon/typeref.h>
44 #endif
45 
46 extern "C" {
47  struct addrinfo;
48 }
49 
50 #ifdef _MSWINDOWS_
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;
56 #else
57 #include <unistd.h>
58 #include <sys/socket.h>
59 #include <net/if.h>
60 #include <netinet/in.h>
61 #include <netdb.h>
62 #endif
63 
64 #if defined(__ANDROID__)
65 typedef uint16_t in_port_t;
66 #endif
67 
68 #include <errno.h>
69 #include <stdio.h>
70 
71 #ifndef IPTOS_LOWDELAY
72 #define IPTOS_LOWDELAY 0x10
73 #define IPTOS_THROUGHPUT 0x08
74 #define IPTOS_RELIABILITY 0x04
75 #define IPTOS_MINCOST 0x02
76 #endif
77 
78 #ifdef AF_UNSPEC
79 #define DEFAULT_FAMILY AF_UNSPEC
80 #else
81 #define DEFAULT_FAMILY AF_INET
82 #endif
83 
84 typedef struct sockaddr *sockaddr_t;
85 
86 typedef struct sockaddr sockaddr_struct; // older gcc needs...?
87 
92 {
93  union
94  {
95  struct in_addr ipv4;
96 #ifdef AF_INET6
97  struct in6_addr ipv6;
98 #endif
99  };
100 };
101 
102 #if defined(AF_INET6) || defined(__CYGWIN__)
103 
111 {
112  union {
113 #ifdef AF_INET6
114  struct sockaddr_in6 ipv6;
115 #endif
116  struct sockaddr_in ipv4;
117  struct sockaddr address;
118  };
119 };
120 #else
121 struct sockaddr_internet
122 {
123  union {
124  struct sockaddr_in ipv4;
125  struct sockaddr address;
126  };
127 };
128 
129 struct sockaddr_storage
130 {
131 #ifdef AF_UNIX
132  char sa_data[128];
133 #else
134  char sa_data[sizeof(struct sockaddr_in)];
135 #endif
136 };
137 #endif
138 
139 #ifndef SOCK_DCCP
140 #define SOCK_DCCP 6
141 #endif
142 
143 #ifndef IPPROTO_DCCP
144 #define IPPROTO_DCCP 23
145 #endif
146 
147 #ifndef SOL_DCCP
148 #define SOL_DCCP 269
149 #endif
150 
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
155 
156 namespace ucommon {
157 
167 class __EXPORT cidr : public LinkedObject
168 {
169 protected:
170  int Family;
171  struct hostaddr_internet Netmask, Network;
172  char Name[16];
173 
174  unsigned mask(const char *cp) const;
175 
176  struct hostaddr_internet broadcast(void) const;
177 
178  unsigned mask(void) const;
179 
180 public:
185 
189  cidr();
190 
197  cidr(const char *string);
198 
204  cidr(policy **policy, const char *string);
205 
212  cidr(policy **policy, const char *string, const char *name);
213 
218  cidr(const cidr& existing);
219 
226  static const cidr *find(const policy *policy, const struct sockaddr *address);
227 
235  static const cidr *container(const policy *policy, const struct sockaddr *address);
236 
244  inline const char *getName(void) const {
245  return Name;
246  }
247 
252  inline int getFamily(void) const {
253  return Family;
254  }
255 
260  inline struct hostaddr_internet getNetwork(void) const {
261  return Network;
262  }
263 
268  inline struct hostaddr_internet getNetmask(void) const {
269  return Netmask;
270  }
271 
276  inline struct hostaddr_internet getBroadcast(void) const {
277  return broadcast();
278  }
279 
284  inline unsigned getMask(void) const {
285  return mask();
286  }
287 
292  void set(const char *string);
293 
299  bool is_member(const struct sockaddr *address) const;
300 
306  inline bool operator==(const struct sockaddr *address) const {
307  return is_member(address);
308  }
309 
315  inline bool operator!=(const struct sockaddr *address) const {
316  return !is_member(address);
317  }
318 };
319 
327 class __EXPORT Socket
328 {
329 protected:
330  socket_t so;
331  int ioerr;
332  timeout_t iowait;
333 
334 public:
335  // temporary splints...
336  typedef struct hostaddr_internet host_t;
337  typedef cidr cidr_t;
338 
347  static struct addrinfo *query(const char *host, const char *service, int type = SOCK_STREAM, int protocol = 0);
348 
354  static void release(struct addrinfo *list);
355 
364  class __EXPORT address
365  {
366  protected:
367  struct addrinfo *list;
368 
369  public:
380  address(int family, const char *address, int type = SOCK_STREAM, int protocol = 0);
381 
394  address(int family, const char *hostname, const char *service = NULL);
395 
402  address(const char *host, const char *service, int type = SOCK_STREAM);
403 
411  address(const char *hostname, in_port_t port = 0);
412 
416  address(const in_addr& address, in_port_t port = 0);
417 
421  address(const in6_addr& address, in_port_t port = 0);
422 
426  address(const sockaddr& address) : list(NULL) {
427  insert(&address);
428  }
429 
433  address(const addrinfo* address) : list(NULL) {
434  insert(address);
435  }
436 
440  address();
441 
446  address(const address& reference);
447 
452  address& operator=(const address& rhs);
453 
457  ~address();
458 
464  bool operator==(const address& other) const;
465 
466  inline bool operator!=(const address& other) const {
467  return !(*this==other);
468  }
469 
470  inline bool equals(const address& other) const {
471  return *this == other;
472  }
473 
478  const struct sockaddr *get(void) const;
479 
480  struct sockaddr *modify(void);
481 
482  inline const struct sockaddr *getAddr(void) const {
483  return get();
484  }
485 
486  inline const struct sockaddr *operator()(void) const {
487  return get();
488  }
489 
494  inline operator struct sockaddr *() {
495  return modify();
496  }
497 
503  const struct sockaddr *get(int family) const;
504 
505  struct sockaddr *modify(int family);
506 
507  inline const struct sockaddr *operator()(int family) const {
508  return get(family);
509  }
510 
511  inline operator struct sockaddr_in *() {
512  return (struct sockaddr_in *)modify(AF_INET);
513  }
514 
515 #ifdef AF_INET6
516  inline operator struct sockaddr_in6 *() {
517  return (struct sockaddr_in6 *)modify(AF_INET6);
518  }
519 #endif
520 
525  int family(void) const;
526 
531  inline size_t getLength(void) const {
532  return len(get());
533  }
534 
539  inline in_port_t getPort(void) const {
540  return getPort(get());
541  }
542 
547  void setPort(in_port_t port);
548 
553  address withPort(in_port_t port) const;
554 
559  struct sockaddr *find(const struct sockaddr *addr) const;
560 
565  inline struct addrinfo *getList(void) const {
566  return list;
567  }
568 
573  inline operator struct addrinfo *() const {
574  return list;
575  }
576 
581  inline struct addrinfo *operator*() const {
582  return list;
583  }
584 
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);
599  }
600 
605  inline operator bool() const {
606  return list != nullptr;
607  }
608 
609  inline bool is_valid() const {
610  return list != nullptr;
611  }
612 
613  inline bool isValid() const {
614  return list != nullptr;
615  }
616 
621  inline bool operator!() const {
622  return list == nullptr;
623  }
624 
630  inline bool is_any() const {
631  return isAny(get());
632  }
633 
634  inline bool isAny() const {
635  return isAny(get());
636  }
637 
644  void setAny(int family = AF_UNSPEC);
645 
651  inline bool is_loopback() const {
652  return isLoopback(get());
653  }
654 
655  inline bool isLoopback() const {
656  return isLoopback(get());
657  }
658 
665  void setLoopback(int family = AF_UNSPEC);
666 
670  void clear(void);
671 
678  void set(const char *hostname, const char *service = NULL, int type = SOCK_STREAM);
679 
686  void add(const char *hostname, const char *service = NULL, int type = SOCK_STREAM);
687 
695  void set(int family, const char *address, int type = SOCK_STREAM, int protocol = 0);
696 
701  void add(sockaddr *address);
702 
708  unsigned insert(const struct addrinfo *address);
709 
715  unsigned remove(const struct addrinfo *address);
716 
722  bool remove(const struct sockaddr *address);
723 
730  bool insert(const struct sockaddr *address);
731 
737  void copy(const struct addrinfo *address);
738 
743  void set(struct sockaddr *address);
744 
750  void set(const char *hostname, in_port_t service = 0);
751 
756  static size_t getLength(const struct sockaddr *address) {
757  return len(address);
758  }
759 
764  inline static in_port_t getPort(const struct sockaddr *address) {
765  return Socket::port(address);
766  }
767 
773  static void setPort(struct sockaddr *address, in_port_t port);
774 
780  static bool isAny(const struct sockaddr *address);
781 
786  static void setAny(struct sockaddr *sa);
787 
791  static sockaddr_storage any(int family);
792 
798  static bool isLoopback(const struct sockaddr *address);
799 
805  static void setLoopback(struct sockaddr *sa);
806 
810  static sockaddr_storage loopback(int family);
811 
817  static struct sockaddr *dup(struct sockaddr *address);
818 
824  static struct sockaddr_in *ipv4(struct sockaddr *address);
825 
826 #ifdef AF_INET6
827 
832  static struct sockaddr_in6 *ipv6(struct sockaddr *address);
833 #endif
834 
847  static size_t print(const struct sockaddr *src, char* dst, size_t dst_sz, bool port=false, bool ipv6_brackets=false);
848  };
849 
850  friend class address;
851 
855  Socket();
856 
861  Socket(const Socket& existing);
862 
867  Socket(socket_t socket);
868 
874  Socket(const struct addrinfo *address);
875 
882  Socket(int family, int type, int protocol = 0);
883 
893  Socket(const char *address, const char *port, int family = AF_UNSPEC, int type = 0, int protocol = 0);
894 
898  virtual ~Socket();
899 
903  void cancel(void);
904 
909  static void cancel(socket_t socket);
910 
914  void release(void);
915 
919  inline int err(void) const {
920  return ioerr;
921  }
922 
928  bool is_pending(unsigned value);
929 
934  bool connected(void) const;
935 
942  bool wait(timeout_t timeout = 0) const;
943 
948  inline int nodelay(void) const {
949  return nodelay(so);
950  }
951 
959  static bool wait(socket_t socket, timeout_t timeout = 0);
960 
967  bool waitSending(timeout_t timeout = 0) const;
968 
973  inline unsigned pending(void) const {
974  return pending(so);
975  }
976 
982  inline int broadcast(bool enable) {
983  return broadcast(so, enable);
984  }
985 
991  inline int keepalive(bool enable) {
992  return keepalive(so, enable);
993  }
994 
1000  inline int blocking(bool enable) {
1001  return blocking(so, enable);
1002  }
1003 
1009  inline int multicast(unsigned ttl = 1) {
1010  return multicast(so, ttl);
1011  }
1012 
1018  inline int loopback(bool enable) {
1019  return loopback(so, enable);
1020  }
1021 
1026  inline int getError(void) const {
1027  return error(so);
1028  }
1029 
1035  inline int ttl(uint8_t time) {
1036  return ttl(so, time);
1037  }
1038 
1044  inline int sendsize(unsigned size) {
1045  return sendsize(so, size);
1046  }
1047 
1053  inline int sendwait(unsigned size) {
1054  return sendwait(so, size);
1055  }
1056 
1062  inline int recvsize(unsigned size) {
1063  return recvsize(so, size);
1064  }
1065 
1071  static int type(const socket_t socket);
1072 
1079  static unsigned segsize(socket_t socket, unsigned size = 0);
1080 
1087  static bool ccid(socket_t socket, uint8_t id);
1088 
1093  inline int type(void) const {
1094  return type(so);
1095  }
1096 
1102  inline unsigned segsize(unsigned size) {
1103  return segsize(so, size);
1104  }
1105 
1111  inline bool ccid(uint8_t id) {
1112  return ccid(so, id);
1113  }
1114 
1123  inline int tos(int type) {
1124  return tos(so, type);
1125  }
1126 
1133  inline int priority(int scheduling) {
1134  return priority(so, scheduling);
1135  }
1136 
1140  inline void shutdown(void) {
1141  ::shutdown(so, SHUT_RDWR);
1142  }
1143 
1151  int connectto(struct addrinfo *list);
1152 
1159  int disconnect(void);
1160 
1166  int join(const struct addrinfo *list, const int ifindex = 0);
1167 
1173  int drop(const struct addrinfo *list, const int ifindex = 0);
1174 
1180  int wait(timeout_t timeout = Timer::inf);
1181 
1188  size_t peek(void *data, size_t number) const;
1189 
1197  size_t readfrom(void *data, size_t number, struct sockaddr_storage *address = NULL);
1198 
1206  size_t writeto(const void *data, size_t number, const struct sockaddr *address = NULL);
1207 
1220  size_t readline(char *data, size_t size);
1221 
1227  size_t printf(const char *format, ...) __PRINTF(2,3);
1228 
1240  size_t readline(String& buffer);
1241 
1242  stringref_t readline(size_t maxsize);
1243 
1255  static ssize_t readline(socket_t socket, char *data, size_t size, timeout_t timeout = Timer::inf);
1256 
1263  static ssize_t printf(socket_t socket, const char *format, ...) __PRINTF(2,3);
1264 
1272  size_t writes(const char *string);
1273 
1278  operator bool() const;
1279 
1284  bool operator!() const;
1285 
1291  Socket& operator=(socket_t socket);
1292 
1297  inline operator socket_t() const {
1298  return so;
1299  }
1300 
1305  inline socket_t operator*() const {
1306  return so;
1307  }
1308 
1315  static unsigned pending(socket_t socket);
1316 
1323  static int sendsize(socket_t socket, unsigned size);
1324 
1331  static int sendwait(socket_t socket, unsigned size);
1332 
1339  static int recvsize(socket_t socket, unsigned size);
1340 
1349  static int connectto(socket_t socket, struct addrinfo *list);
1350 
1356  static int disconnect(socket_t socket);
1357 
1364  static int drop(socket_t socket, const struct addrinfo *list, const int ifindex = 0);
1365 
1372  static int join(socket_t socket, const struct addrinfo *list, const int ifindex = 0);
1373 
1379  static int error(const socket_t socket);
1380 
1387  static int multicast(socket_t socket, unsigned ttl = 1);
1388 
1395  static int loopback(socket_t socket, bool enable);
1396 
1403  static int blocking(socket_t socket, bool enable);
1404 
1411  static int keepalive(socket_t socket, bool enable);
1412 
1419  static int broadcast(socket_t socket, bool enable);
1420 
1426  static int nodelay(socket_t socket);
1427 
1434  static int priority(socket_t socket, int scheduling);
1435 
1442  static int tos(socket_t socket, int type);
1443 
1450  static int ttl(socket_t socket, uint8_t time);
1451 
1456  static int family(socket_t socket);
1457 
1463  inline static int family(const struct sockaddr_storage& address) {
1464  return ((const struct sockaddr *)&address)->sa_family;
1465  }
1466 
1472  inline static int family(const struct sockaddr_internet& address) {
1473  return address.address.sa_family;
1474  }
1475 
1485  static ssize_t recvfrom(socket_t socket, void *buffer, size_t size, int flags = 0, struct sockaddr_storage *address = NULL);
1486 
1496  static ssize_t sendto(socket_t socket, const void *buffer, size_t size, int flags = 0, const struct sockaddr *address = NULL);
1497 
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);
1509  }
1510 
1519  static int bindto(socket_t socket, const char *address, const char *service, int protocol = 0);
1520 
1528  static int listento(socket_t socket, const struct sockaddr *address, int backlog = 5);
1529 
1536  static int bindto(socket_t socket, const struct sockaddr *address);
1537 
1544  static socket_t acceptfrom(socket_t socket, struct sockaddr_storage *address = NULL);
1545 
1553  static socket_t create(int family, int type, int protocol);
1554 
1562  static socket_t create(const struct addrinfo *address, int type, int protocol);
1563 
1573  static socket_t create(const char *iface, const char *service, int family = AF_UNSPEC, int type = 0, int protocol = 0);
1574 
1580  static socket_t create(const Socket::address &address);
1581 
1586  static void release(socket_t socket);
1587 
1595  static char *hostname(const struct sockaddr *address, char *buffer, size_t size);
1596 
1604  static struct addrinfo *hinting(socket_t socket, struct addrinfo *hint);
1605 
1616  static socklen_t query(socket_t socket, struct sockaddr_storage *address, const char *hostname, const char *service);
1617 
1623  static socklen_t len(const struct sockaddr *address);
1624 
1632  static bool equal(const struct sockaddr *address1, const struct sockaddr *address2);
1633 
1640  static unsigned copy(struct sockaddr *target, const struct sockaddr *origin);
1641 
1648  static unsigned store(struct sockaddr_storage *storage, const struct sockaddr *address);
1649 
1656  static unsigned store(struct sockaddr_internet *storage, const struct sockaddr *address);
1657 
1665  static bool eq_host(const struct sockaddr *address1, const struct sockaddr *address2);
1666 
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);
1676  }
1677 
1685  inline static bool eq_inet(const struct sockaddr_internet *address1, const struct sockaddr_internet *address2) {
1686  return equal((const struct sockaddr *)address1, (const struct sockaddr *)address2);
1687  }
1688 
1696  static bool eq_subnet(const struct sockaddr *address1, const struct sockaddr *address2);
1697 
1706  static int via(struct sockaddr *address, const struct sockaddr *target, socklen_t size = 0);
1707 
1715  static char *query(const struct sockaddr *address, char *buffer, socklen_t size);
1716 
1722  static in_port_t port(const struct sockaddr *address);
1723 
1729  inline static in_port_t port(const struct sockaddr_internet *address) {
1730  return port((const struct sockaddr *)address);
1731  }
1732 
1739  static unsigned keyindex(const struct sockaddr *address, unsigned size);
1740 
1747  static unsigned keyhost(const struct sockaddr *address, unsigned size);
1748 
1752  static void init(void);
1753 
1759  static void query(int family);
1760 
1767  static void v4mapping(bool enable);
1768 
1773  static int error(void);
1774 
1783  static bool is_null(const char *string);
1784 
1792  static bool is_numeric(const char *string);
1793 
1802  static int local(socket_t socket, struct sockaddr_storage *address);
1803 
1812  static int remote(socket_t socket, struct sockaddr_storage *address);
1813 };
1814 
1820 class __EXPORT ListenSocket : protected Socket
1821 {
1822 private:
1823  __DELETE_COPY(ListenSocket);
1824 
1825 public:
1835  ListenSocket(const char *address, const char *service, unsigned backlog = 5, int family = AF_UNSPEC, int type = 0, int protocol = 0);
1836 
1847  static socket_t create(const char *address, const char *service, unsigned backlog = 5, int family = AF_UNSPEC, int type = 0, int protocol = 0);
1848 
1854  socket_t accept(struct sockaddr_storage *address = NULL) const;
1855 
1861  inline bool wait(timeout_t timeout = Timer::inf) const {
1862  return Socket::wait(timeout);
1863  }
1864 
1869  inline operator socket_t() const {
1870  return so;
1871  }
1872 
1877  inline socket_t operator*() const {
1878  return so;
1879  }
1880 
1885  inline socket_t getsocket(void) const {
1886  return so;
1887  }
1888 
1889  inline socket_t handle(void) const {
1890  return so;
1891  }
1892 
1893 };
1894 
1900 class __EXPORT TCPServer : public ListenSocket
1901 {
1902 private:
1903  __DELETE_DEFAULTS(TCPServer);
1904 
1905 public:
1913  TCPServer(const char *address, const char *service, unsigned backlog = 5);
1914 };
1915 
1921 {
1922 protected:
1923  inline linked_sockaddr_operations() {}
1924 
1928  const struct addrinfo *_nextaddrinfo(const struct addrinfo *addrinfo) const;
1929 
1933  const struct sockaddr *_getaddrinfo(const struct addrinfo *addrinfo) const;
1934 
1938  socket_t _getaddrsock(const struct addrinfo *addrinfo) const;
1939 };
1940 
1946 template <>
1947 class linked_pointer<sockaddr_struct> : private linked_sockaddr_operations
1948 {
1949 private:
1950  const struct addrinfo *ptr;
1951 
1952 public:
1953  inline linked_pointer(const struct addrinfo *list) {
1954  ptr = list;
1955  }
1956 
1957  inline linked_pointer(const linked_pointer& copy) {
1958  ptr = copy.ptr;
1959  }
1960 
1961  inline linked_pointer() {
1962  ptr = nullptr;
1963  }
1964 
1965  inline linked_pointer(Socket::address& list) {
1966  ptr = list.getList();
1967  }
1968 
1973  inline operator const struct sockaddr *() const {
1974  return _getaddrinfo(ptr);
1975  }
1976 
1981  inline const struct sockaddr *operator*() const {
1982  return _getaddrinfo(ptr);
1983  }
1984 
1985  inline operator const struct sockaddr_in *() const {
1986  return (struct sockaddr_in *)_getaddrinfo(ptr);
1987  }
1988 
1989  inline const struct sockaddr_in *in(void) const {
1990  return (struct sockaddr_in *)_getaddrinfo(ptr);
1991  }
1992 
1993 #ifdef AF_INET6
1994  inline operator const struct sockaddr_in6 *() const {
1995  return (struct sockaddr_in6 *)_getaddrinfo(ptr);
1996  }
1997 
1998  inline const struct sockaddr_in6 *in6(void) const {
1999  return (struct sockaddr_in6 *)_getaddrinfo(ptr);
2000  }
2001 #endif
2002 
2006  inline socket_t operator()(void) const {
2007  return _getaddrsock(ptr);
2008  }
2009 
2014  inline operator bool() const {
2015  return ptr != nullptr;
2016  }
2017 
2022  inline linked_pointer& operator=(const struct addrinfo *list) {
2023  ptr = list;
2024  return *this;
2025  }
2026 
2032  ptr = list.getList();
2033  return *this;
2034  }
2035 
2040  inline void set(const struct addrinfo *list) {
2041  ptr = list;
2042  }
2043 
2048  inline void set(Socket::address& list) {
2049  ptr = list.getList();
2050  }
2051 
2052 
2057  inline const struct sockaddr* operator->() const {
2058  return _getaddrinfo(ptr);
2059  }
2060 
2065  inline bool operator!() const {
2066  return ptr == nullptr;
2067  }
2068 
2069  inline void next(void) {
2070  ptr = _nextaddrinfo(ptr);
2071  }
2072 };
2073 
2079 inline struct addrinfo *addrinfo(Socket::address& address) {
2080  return address.getList();
2081 }
2082 
2089 inline const struct sockaddr *addr(Socket::address& address) {
2090  return address.get();
2091 }
2092 
2100 inline bool eq(const struct sockaddr *s1, const struct sockaddr *s2) {
2101  return Socket::equal(s1, s2);
2102 }
2103 
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);
2113 }
2114 
2122 inline bool eq_host(const struct sockaddr *s1, const struct sockaddr *s2) {
2123  return Socket::eq_host(s1, s2);
2124 }
2125 
2126 inline bool eq_subnet(const struct sockaddr *s1, const struct sockaddr *s2) {
2127  return Socket::eq_subnet(s1, s2);
2128 }
2129 
2130 String str(Socket& so, size_t size);
2131 
2132 namespace Type {
2133 
2134  class HostAddress
2135  {
2136  private:
2137  struct hostaddr_internet storage;
2138 
2139  public:
2140  inline HostAddress() {
2141  memset(&storage, 0, sizeof(storage));
2142  }
2143 
2144  inline HostAddress(const HostAddress& copy) {
2145  memcpy(&storage, &copy.storage, sizeof(storage));
2146  }
2147 
2148  inline HostAddress(const struct hostaddr_internet *addr) {
2149  memcpy(&storage, addr, sizeof(struct hostaddr_internet));
2150  }
2151 
2152  inline HostAddress(const in_addr *addr) {
2153  memset(&storage, 0, sizeof(storage));
2154  memcpy(&storage, addr, sizeof(struct in_addr));
2155  }
2156 
2157 #ifdef AF_INET6
2158  inline HostAddress(const in6_addr *addr) {
2159  memset(&storage, 0, sizeof(storage));
2160  memcpy(&storage, addr, sizeof(struct in6_addr));
2161  }
2162 #endif
2163 
2164  inline operator const struct hostaddr_internet *() const {
2165  return &storage;
2166  }
2167 
2168  inline struct hostaddr_internet *operator*() {
2169  return &storage;
2170  }
2171 
2172  inline socklen_t size() {
2173  return sizeof(storage);
2174  }
2175 
2176  inline HostAddress& operator=(const HostAddress& copy) {
2177  memcpy(&storage, &copy.storage, sizeof(storage));
2178  return *this;
2179  }
2180 
2181  inline HostAddress& operator=(const struct hostaddr_internet *host) {
2182  memcpy(&storage, host, sizeof(struct hostaddr_internet));
2183  return *this;
2184  }
2185 
2186  inline bool operator==(const HostAddress& check) const {
2187  return (memcmp(&check.storage, &storage, sizeof(storage)) == 0);
2188  }
2189 
2190  inline bool operator!=(const HostAddress& check) const {
2191  return (memcmp(&check.storage, &storage, sizeof(storage)) != 0);
2192  }
2193 
2194  inline bool operator==(const struct hostaddr_internet *host) const {
2195  return (memcmp(host, &storage, sizeof(storage)) == 0);
2196  }
2197 
2198  inline bool operator!=(const struct hostaddr_internet *host) const {
2199  return (memcmp(host, &storage, sizeof(storage)) != 0);
2200  }
2201  };
2202 
2203  class SockAddress
2204  {
2205  private:
2206  struct sockaddr_storage storage;
2207 
2208  public:
2209  inline SockAddress() {
2210  memset(&storage, 0, sizeof(storage));
2211  }
2212 
2213  inline SockAddress(const SockAddress& copy) {
2214  memcpy(&storage, &copy.storage, sizeof(storage));
2215  }
2216 
2217  inline SockAddress(const struct sockaddr *addr) {
2218  Socket::store(&storage, addr);
2219  }
2220 
2221  inline SockAddress& operator=(const SockAddress& copy) {
2222  memcpy(&storage, &copy.storage, sizeof(storage));
2223  return *this;
2224  }
2225 
2226  inline SockAddress& operator=(const struct sockaddr *addr) {
2227  Socket::store(&storage, addr);
2228  return *this;
2229  }
2230 
2231  inline operator const struct sockaddr *() const {
2232  return (const struct sockaddr*)&storage;
2233  }
2234 
2235  inline struct sockaddr *operator*() {
2236  return (struct sockaddr *)&storage;
2237  }
2238 
2239  inline const struct sockaddr *get() const {
2240  return (const struct sockaddr *)&storage;
2241  }
2242 
2243  inline socklen_t size() {
2244  return sizeof(storage);
2245  }
2246 
2247  inline bool operator==(const SockAddress& check) const {
2248  return Socket::equal(get(), check.get());
2249  }
2250 
2251  inline bool operator!=(const SockAddress& check) const {
2252  return !Socket::equal(get(), check.get());
2253  }
2254 
2255  inline bool operator==(const struct sockaddr *check) const {
2256  return Socket::equal(get(), check);
2257  }
2258 
2259  inline bool operator!=(const struct sockaddr *check) const {
2260  return !Socket::equal(get(), check);
2261  }
2262  };
2263 
2264  class InetAddress
2265  {
2266  private:
2267  struct sockaddr_internet storage;
2268 
2269  public:
2270  inline InetAddress() {
2271  memset(&storage, 0, sizeof(storage));
2272  }
2273 
2274  inline InetAddress(const InetAddress& copy) {
2275  memcpy(&storage, &copy.storage, sizeof(storage));
2276  }
2277 
2278  inline InetAddress(const struct sockaddr *addr) {
2279  Socket::store(&storage, addr);
2280  }
2281 
2282  inline InetAddress& operator=(const InetAddress& copy) {
2283  memcpy(&storage, &copy.storage, sizeof(storage));
2284  return *this;
2285  }
2286 
2287  inline InetAddress& operator=(const struct sockaddr *addr) {
2288  Socket::store(&storage, addr);
2289  return *this;
2290  }
2291 
2292  inline operator const struct sockaddr *() const {
2293  return (const struct sockaddr*)&storage;
2294  }
2295 
2296  inline struct sockaddr *operator*() {
2297  return (struct sockaddr *)&storage;
2298  }
2299 
2300  inline const struct sockaddr *get() const {
2301  return (const struct sockaddr *)&storage;
2302  }
2303 
2304  inline socklen_t size() {
2305  return sizeof(storage);
2306  }
2307 
2308  inline bool operator==(const SockAddress& check) const {
2309  return Socket::equal(get(), check.get());
2310  }
2311 
2312  inline bool operator!=(const SockAddress& check) const {
2313  return !Socket::equal(get(), check.get());
2314  }
2315 
2316  inline bool operator==(const struct sockaddr *check) const {
2317  return Socket::equal(get(), check);
2318  }
2319 
2320  inline bool operator!=(const struct sockaddr *check) const {
2321  return !Socket::equal(get(), check);
2322  }
2323  };
2324 }
2325 
2326 typedef TCPServer tcpserv_t;
2327 
2328 } // namespace ucommon
2329 
2330 #endif
int broadcast(bool enable)
Set socket for unicast mode broadcasts.
Definition: socket.h:982
An object that can hold a ipv4 or ipv6 socket address.
Definition: socket.h:110
static size_t getLength(const struct sockaddr *address)
Returns the size of the socket address according to the family.
Definition: socket.h:756
T * operator *() const
Return object we currently point to.
Definition: linked.h:1084
int multicast(unsigned ttl=1)
Set multicast mode and multicast broadcast range.
Definition: socket.h:1009
socket_t getsocket(void) const
Get the socket descriptor of the listener.
Definition: socket.h:1885
Linked objects, lists, templates, and containers.
const struct sockaddr * get(void) const
Get the first socket address in our address list.
int priority(int scheduling)
Set packet priority, 0 to 6 unless privileged.
Definition: socket.h:1133
A generic tcp server class.
Definition: socket.h:1900
int getFamily(void) const
Get the address family of our cidr block object.
Definition: socket.h:252
static int family(const struct sockaddr_internet &address)
Get the address family of an internet socket address object.
Definition: socket.h:1472
bool operator!() const
Test if we have no address list.
Definition: socket.h:2065
static in_port_t port(const struct sockaddr_internet *address)
Get the service port of an inet socket.
Definition: socket.h:1729
static in_port_t port(const struct sockaddr *address)
Get the service port of a socket.
int type(void) const
Get the type of a socket.
Definition: socket.h:1093
static unsigned store(struct sockaddr_storage *storage, const struct sockaddr *address)
Store an address into an address object.
Timer class to use when scheduling realtime events.
Definition: timers.h:50
int keepalive(bool enable)
Set socket for keepalive packets.
Definition: socket.h:991
bool operator==(const struct sockaddr *address) const
Test if a given socket address falls within this cidr.
Definition: socket.h:306
unsigned segsize(unsigned size)
Set segment size and get mtu of a socket.
Definition: socket.h:1102
bool ccid(uint8_t id)
Set ccid of dccp socket.
Definition: socket.h:1111
bool is_any() const
Test if the first socket address is ADDR_ANY: 0.0.0.0 or ::0.
Definition: socket.h:630
int getError(void) const
Get socket error code.
Definition: socket.h:1026
T * init(T *memory)
Template function to initialize memory by invoking default constructor.
Definition: platform.h:551
address(const sockaddr &address)
Construct a socket address from a sockaddr object.
Definition: socket.h:426
bool eq(const struct sockaddr *s1, const struct sockaddr *s2)
Compare two socket addresses to see if equal.
Definition: socket.h:2100
int recvsize(unsigned size)
Set the size of the socket receive buffer.
Definition: socket.h:1062
A copy-on-write string class that operates by reference count.
Definition: string.h:78
static int family(const struct sockaddr_storage &address)
Get the address family of a socket address object.
Definition: socket.h:1463
const struct sockaddr * operator->() const
Return member from typed object our pointer references.
Definition: socket.h:2057
static bool eq_inet(const struct sockaddr_internet *address1, const struct sockaddr_internet *address2)
Compare socket addresses.
Definition: socket.h:1685
A class to hold internet segment routing rules.
Definition: socket.h:167
in_port_t getPort(void) const
Get the port of the first address .
Definition: socket.h:539
linked_pointer & operator=(Socket::address &list)
Assign our pointer from an address list.
Definition: socket.h:2031
bool operator!() const
Test if we have no address list.
Definition: socket.h:621
Realtime timers and timer queues.
A common string class and character string support functions.
int sendsize(unsigned size)
Set the size of the socket send buffer.
Definition: socket.h:1044
int err(void) const
Get error code.
Definition: socket.h:919
A generic socket address class.
Definition: socket.h:364
size_t getLength(void) const
Get the address size of the first address.
Definition: socket.h:531
linked_pointer & operator=(const struct addrinfo *list)
Assign our pointer from an address list.
Definition: socket.h:2022
unsigned getMask(void) const
Get the number of bits in the cidr bitmask.
Definition: socket.h:284
bool is_loopback() const
Test if the first socket address is ADDR_LOOPBACK: 127.0.0.1 or ::1.
Definition: socket.h:651
address(int family, const char *address, int type=SOCK_STREAM, int protocol=0)
Construct a socket address.
const struct sockaddr * addr(Socket::address &address)
A convenience function to convert a socket address list into a socket address.
Definition: socket.h:2089
void set(const struct addrinfo *list)
Assign our pointer from an address list.
Definition: socket.h:2040
bool operator!=(const struct sockaddr *address) const
Test if a given socket address falls outside this cidr.
Definition: socket.h:315
int loopback(bool enable)
Set loopback to read multicast packets we broadcast.
Definition: socket.h:1018
int blocking(bool enable)
Set socket blocking I/O mode.
Definition: socket.h:1000
struct addrinfo * addrinfo(Socket::address &address)
A convenience function to convert a socket address list into an addrinfo.
Definition: socket.h:2079
void set(Socket::address &list)
Assign our pointer from an address list.
Definition: socket.h:2048
struct addrinfo * getList(void) const
Get the full socket address list from the object.
Definition: socket.h:565
LinkedObject policy
A convenience type for using a pointer to a linked list as a policy chain.
Definition: socket.h:184
int nodelay(void) const
Set nodelay option for tcp socket.
Definition: socket.h:948
static bool eq_from(const struct sockaddr_storage *address1, const struct sockaddr_storage *address2)
Compare socket addresses.
Definition: socket.h:1674
static bool eq_subnet(const struct sockaddr *address1, const struct sockaddr *address2)
See if both addresses are in the same subnet.
void shutdown(void)
Shutdown the socket communication channel.
Definition: socket.h:1140
A bound socket used to listen for inbound socket connections.
Definition: socket.h:1820
int tos(int type)
Set the type of service field of outgoing packets.
Definition: socket.h:1123
static in_port_t getPort(const struct sockaddr *address)
Returns the port of the socket address.
Definition: socket.h:764
Common namespace for all ucommon objects.
Definition: access.h:47
T * dup(const T &object)
Convenience function to duplicate object pointer to heap.
Definition: generics.h:324
T copy(const T &src)
Convenience function to copy objects.
Definition: generics.h:395
static ssize_t replyto(socket_t socket, const void *buffer, size_t size, int flags, const struct sockaddr_storage *address)
Send reply on socket.
Definition: socket.h:1507
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...
Definition: socket.h:597
address(const addrinfo *address)
Construct a socket address from an addrinfo structure.
Definition: socket.h:433
void next(void)
Move (iterate) pointer to next member in linked list.
Definition: linked.h:1106
const char * getName(void) const
Get the saved name of our cidr.
Definition: socket.h:244
bool wait(timeout_t timeout=Timer::inf) const
Wait for a pending connection.
Definition: socket.h:1861
bool wait(timeout_t timeout=0) const
Test for pending input data.
A smart pointer template for iterating linked lists.
Definition: linked.h:991
int sendwait(unsigned size)
Set the size to wait before sending.
Definition: socket.h:1053
static bool eq_host(const struct sockaddr *address1, const struct sockaddr *address2)
Compare socket host addresses.
int ttl(uint8_t time)
Set the time to live before packets expire.
Definition: socket.h:1035
linked_pointer()
Create a linked pointer not attached to a list.
Definition: linked.h:1036
unsigned pending(void) const
Get the number of bytes of data in the socket receive buffer.
Definition: socket.h:973
Common base class for all objects that can be formed into a linked list.
Definition: linked.h:55
An object that holds ipv4 or ipv6 binary encoded host addresses.
Definition: socket.h:91
Helper class for linked_pointer template.
Definition: socket.h:1920
static bool equal(const struct sockaddr *address1, const struct sockaddr *address2)
Compare socket addresses.
bool eq_host(const struct sockaddr *s1, const struct sockaddr *s2)
Compare two host addresses to see if equal.
Definition: socket.h:2122
A thread-safe atomic heap management system.
A generic socket base class.
Definition: socket.h:327
socket_t operator()(void) const
Get socket as expression operator.
Definition: socket.h:2006