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 struct sockaddr_internet;
85 
86 typedef struct sockaddr *sockaddr_t;
87 
88 typedef struct sockaddr sockaddr_struct; // older gcc needs...?
89 
93 typedef struct hostaddr_internet
94 {
95  union
96  {
97  struct in_addr ipv4;
98 #ifdef AF_INET6
99  struct in6_addr ipv6;
100 #endif
101  };
103 
104 
105 #if defined(AF_INET6) || defined(__CYGWIN__)
106 
113 typedef struct sockaddr_internet
114 {
115  union {
116 #ifdef AF_INET6
117  struct sockaddr_in6 ipv6;
118 #endif
119  struct sockaddr_in ipv4;
120  struct sockaddr address;
121  };
123 #else
124 typedef struct sockaddr_internet
125 {
126  union {
127  struct sockaddr_in ipv4;
128  struct sockaddr address;
129  };
131 
132 struct sockaddr_storage
133 {
134 #ifdef AF_UNIX
135  char sa_data[128];
136 #else
137  char sa_data[sizeof(struct sockaddr_in)];
138 #endif
139 };
140 #endif
141 
142 #ifndef SOCK_DCCP
143 #define SOCK_DCCP 6
144 #endif
145 
146 #ifndef IPPROTO_DCCP
147 #define IPPROTO_DCCP 23
148 #endif
149 
150 #ifndef SOL_DCCP
151 #define SOL_DCCP 269
152 #endif
153 
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
158 
159 namespace ucommon {
160 
170 class __EXPORT cidr : public LinkedObject
171 {
172 protected:
173  int Family;
174  inethostaddr_t Netmask, Network;
175  char Name[16];
176 
177  unsigned mask(const char *cp) const;
178 
179  inethostaddr_t broadcast(void) const;
180 
181  unsigned mask(void) const;
182 
183 public:
188 
192  cidr();
193 
200  cidr(const char *string);
201 
207  cidr(policy **policy, const char *string);
208 
215  cidr(policy **policy, const char *string, const char *name);
216 
221  cidr(const cidr& existing);
222 
229  static const cidr *find(const policy *policy, const struct sockaddr *address);
230 
238  static const cidr *container(const policy *policy, const struct sockaddr *address);
239 
247  inline const char *getName(void) const
248  {return Name;}
249 
254  inline int getFamily(void) const
255  {return Family;}
256 
261  inline inethostaddr_t getNetwork(void) const
262  {return Network;}
263 
268  inline inethostaddr_t getNetmask(void) const
269  {return Netmask;}
270 
275  inline inethostaddr_t getBroadcast(void) const
276  {return broadcast();}
277 
282  inline unsigned getMask(void) const
283  {return mask();}
284 
289  void set(const char *string);
290 
296  bool is_member(const struct sockaddr *address) const;
297 
303  inline bool operator==(const struct sockaddr *address) const
304  {return is_member(address);}
305 
311  inline bool operator!=(const struct sockaddr *address) const
312  {return !is_member(address);}
313 };
314 
322 class __EXPORT Socket
323 {
324 protected:
325  socket_t so;
326  int ioerr;
327  timeout_t iowait;
328 
329 public:
330  // temporary splints...
331  typedef inethostaddr_t host_t;
332  typedef cidr cidr_t;
333 
342  static struct addrinfo *query(const char *host, const char *service, int type = SOCK_STREAM, int protocol = 0);
343 
349  static void release(struct addrinfo *list);
350 
359  class __EXPORT address
360  {
361  protected:
362  struct addrinfo *list;
363 
364  public:
375  address(int family, const char *address, int type = SOCK_STREAM, int protocol = 0);
376 
389  address(int family, const char *hostname, const char *service = NULL);
390 
397  address(const char *host, const char *service, int type = SOCK_STREAM);
398 
406  address(const char *hostname, in_port_t port = 0);
407 
411  address(const in_addr& address, in_port_t port = 0);
412 
416  address(const in6_addr& address, in_port_t port = 0);
417 
421  address(const sockaddr& address) : list(NULL)
422  {insert(address);}
423 
427  address(const addrinfo* address) : list(NULL)
428  {insert(address);}
429 
433  address();
434 
439  address(const address& reference);
440 
445  address& operator=(const address& rhs);
446 
450  ~address();
451 
457  bool operator==(const address& other) const;
458 
459  inline bool operator!=(const address& other) const
460  {return !(*this==other);}
461 
462  inline bool equals(const address& other) const
463  {return *this == other;}
464 
469  struct sockaddr *get(void) const;
470 
471  inline struct sockaddr *getAddr(void) const
472  {return get();}
473 
474  inline struct sockaddr *operator()(void) const
475  {return get();}
476 
481  inline operator struct sockaddr *() const
482  {return get();}
483 
489  struct sockaddr *get(int family) const;
490 
491  inline struct sockaddr *operator()(int family) const
492  {return get(family);}
493 
494  inline operator struct sockaddr_in *() const
495  {return (struct sockaddr_in *)get(AF_INET);}
496 
497 #ifdef AF_INET6
498  inline operator struct sockaddr_in6 *() const
499  {return (struct sockaddr_in6 *)get(AF_INET6);}
500 #endif
501 
506  int family(void) const;
507 
512  inline size_t getLength(void) const
513  {return len(get());}
514 
519  inline in_port_t getPort(void) const
520  {return getPort(get());}
521 
526  void setPort(in_port_t port);
527 
532  address withPort(in_port_t port) const;
533 
538  struct sockaddr *find(const struct sockaddr *addr) const;
539 
544  inline struct addrinfo *getList(void) const
545  {return list;}
546 
551  inline operator struct addrinfo *() const
552  {return list;}
553 
558  inline struct addrinfo *operator*() const
559  {return list;}
560 
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);}
575 
580  inline operator bool() const
581  {return list != NULL;}
582 
583  inline bool isValid() const
584  {return list != NULL;}
585 
590  inline bool operator!() const
591  {return list == NULL;}
592 
598  inline bool isAny() const
599  {return isAny(get());}
600 
607  void setAny(int family = AF_UNSPEC);
608 
614  inline bool isLoopback() const
615  {return isLoopback(get());}
616 
623  void setLoopback(int family = AF_UNSPEC);
624 
628  void clear(void);
629 
636  void set(const char *hostname, const char *service = NULL, int type = SOCK_STREAM);
637 
644  void add(const char *hostname, const char *service = NULL, int type = SOCK_STREAM);
645 
653  void set(int family, const char *address, int type = SOCK_STREAM, int protocol = 0);
654 
659  void add(sockaddr *address);
660 
666  unsigned insert(const struct addrinfo *address);
667 
673  unsigned remove(const struct addrinfo *address);
674 
680  bool remove(const struct sockaddr *address);
681 
688  bool insert(const struct sockaddr *address);
689  inline bool insert(const struct sockaddr& address)
690  {return insert(&address);}
691 
697  void copy(const struct addrinfo *address);
698 
703  void set(struct sockaddr *address);
704 
710  void set(const char *hostname, in_port_t service = 0);
711 
716  static size_t getLength(const struct sockaddr *address)
717  {return len(address);}
718 
723  static in_port_t getPort(const struct sockaddr *address);
724 
730  static void setPort(struct sockaddr *address, in_port_t port);
731 
737  static bool isAny(const struct sockaddr *address);
738 
743  static void setAny(struct sockaddr *sa);
744 
748  static sockaddr_storage any(int family);
749 
755  static bool isLoopback(const struct sockaddr *address);
756 
762  static void setLoopback(struct sockaddr *sa);
763 
767  static sockaddr_storage loopback(int family);
768 
774  static struct sockaddr *dup(struct sockaddr *address);
775 
781  static struct sockaddr_in *ipv4(struct sockaddr *address);
782 
783 #ifdef AF_INET6
784 
789  static struct sockaddr_in6 *ipv6(struct sockaddr *address);
790 #endif
791 
804  static size_t print(const struct sockaddr *src, char* dst, size_t dst_sz, bool port=false, bool ipv6_brackets=false);
805  };
806 
807  friend class address;
808 
812  Socket();
813 
818  Socket(const Socket& existing);
819 
824  Socket(socket_t socket);
825 
831  Socket(const struct addrinfo *address);
832 
839  Socket(int family, int type, int protocol = 0);
840 
850  Socket(const char *address, const char *port, int family = AF_UNSPEC, int type = 0, int protocol = 0);
851 
855  virtual ~Socket();
856 
860  void cancel(void);
861 
866  static void cancel(socket_t socket);
867 
871  void release(void);
872 
876  inline int err(void) const
877  {return ioerr;}
878 
884  bool is_pending(unsigned value);
885 
890  bool connected(void) const;
891 
898  bool wait(timeout_t timeout = 0) const;
899 
904  inline int nodelay(void) const
905  {return nodelay(so);}
906 
914  static bool wait(socket_t socket, timeout_t timeout = 0);
915 
922  bool waitSending(timeout_t timeout = 0) const;
923 
928  inline unsigned pending(void) const
929  {return pending(so);}
930 
936  inline int broadcast(bool enable)
937  {return broadcast(so, enable);}
938 
944  inline int keepalive(bool enable)
945  {return keepalive(so, enable);}
946 
952  inline int blocking(bool enable)
953  {return blocking(so, enable);}
954 
960  inline int multicast(unsigned ttl = 1)
961  {return multicast(so, ttl);}
962 
968  inline int loopback(bool enable)
969  {return loopback(so, enable);}
970 
975  inline int getError(void)
976  {return error(so);}
977 
983  inline int ttl(unsigned char time)
984  {return ttl(so, time);}
985 
991  inline int sendsize(unsigned size)
992  {return sendsize(so, size);}
993 
999  inline int sendwait(unsigned size)
1000  {return sendwait(so, size);}
1001 
1002 
1008  inline int recvsize(unsigned size)
1009  {return recvsize(so, size);}
1010 
1016  static int type(socket_t socket);
1017 
1024  static unsigned segsize(socket_t socket, unsigned size = 0);
1025 
1032  static bool ccid(socket_t socket, uint8_t id);
1033 
1038  inline int type(void)
1039  {return type(so);}
1040 
1046  inline unsigned segsize(unsigned size)
1047  {return segsize(so, size);}
1048 
1054  inline bool ccid(uint8_t id)
1055  {return ccid(so, id);}
1056 
1065  inline int tos(int type)
1066  {return tos(so, type);}
1067 
1074  inline int priority(int scheduling)
1075  {return priority(so, scheduling);}
1076 
1080  inline void shutdown(void)
1081  {::shutdown(so, SHUT_RDWR);}
1082 
1090  int connectto(struct addrinfo *list);
1091 
1098  int disconnect(void);
1099 
1105  int join(const struct addrinfo *list, const int ifindex = 0);
1106 
1112  int drop(const struct addrinfo *list, const int ifindex = 0);
1113 
1119  int wait(timeout_t timeout = Timer::inf);
1120 
1127  size_t peek(void *data, size_t number) const;
1128 
1136  size_t readfrom(void *data, size_t number, struct sockaddr_storage *address = NULL);
1137 
1145  size_t writeto(const void *data, size_t number, const struct sockaddr *address = NULL);
1146 
1159  size_t readline(char *data, size_t size);
1160 
1166  size_t printf(const char *format, ...) __PRINTF(2,3);
1167 
1179  size_t readline(String& buffer);
1180 
1192  static ssize_t readline(socket_t socket, char *data, size_t size, timeout_t timeout = Timer::inf);
1193 
1200  static ssize_t printf(socket_t socket, const char *format, ...) __PRINTF(2,3);
1201 
1209  size_t writes(const char *string);
1210 
1215  operator bool();
1216 
1221  bool operator!() const;
1222 
1228  Socket& operator=(socket_t socket);
1229 
1234  inline operator socket_t() const
1235  {return so;}
1236 
1241  inline socket_t operator*() const
1242  {return so;}
1243 
1250  static unsigned pending(socket_t socket);
1251 
1258  static int sendsize(socket_t socket, unsigned size);
1259 
1266  static int sendwait(socket_t socket, unsigned size);
1267 
1274  static int recvsize(socket_t socket, unsigned size);
1275 
1284  static int connectto(socket_t socket, struct addrinfo *list);
1285 
1291  static int disconnect(socket_t socket);
1292 
1299  static int drop(socket_t socket, const struct addrinfo *list, const int ifindex = 0);
1300 
1307  static int join(socket_t socket, const struct addrinfo *list, const int ifindex = 0);
1308 
1314  static int error(socket_t socket);
1315 
1322  static int multicast(socket_t socket, unsigned ttl = 1);
1323 
1330  static int loopback(socket_t socket, bool enable);
1331 
1338  static int blocking(socket_t socket, bool enable);
1339 
1346  static int keepalive(socket_t socket, bool enable);
1347 
1354  static int broadcast(socket_t socket, bool enable);
1355 
1361  static int nodelay(socket_t socket);
1362 
1369  static int priority(socket_t socket, int scheduling);
1370 
1377  static int tos(socket_t socket, int type);
1378 
1385  static int ttl(socket_t socket, unsigned char time);
1386 
1391  static int family(socket_t socket);
1392 
1398  inline static int family(const struct sockaddr_storage& address)
1399  {return ((const struct sockaddr *)&address)->sa_family;}
1400 
1406  inline static int family(const struct sockaddr_internet& address)
1407  {return address.address.sa_family;}
1408 
1418  static ssize_t recvfrom(socket_t socket, void *buffer, size_t size, int flags = 0, struct sockaddr_storage *address = NULL);
1419 
1429  static ssize_t sendto(socket_t socket, const void *buffer, size_t size, int flags = 0, const struct sockaddr *address = NULL);
1430 
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);}
1442 
1452  inline static ssize_t sendinet(socket_t socket, const void *buffer, size_t size, int flags, const struct sockaddr_internet *address)
1453  {return sendto(socket, buffer, size, flags, (const struct sockaddr *)address);}
1454 
1464  static ssize_t recvinet(socket_t socket, void *buffer, size_t size, int flags = 0, struct sockaddr_internet *address = NULL);
1465 
1474  static int bindto(socket_t socket, const char *address, const char *service, int protocol = 0);
1475 
1483  static int listento(socket_t socket, const struct sockaddr *address, int backlog = 5);
1484 
1491  static int bindto(socket_t socket, const struct sockaddr *address);
1492 
1499  static socket_t acceptfrom(socket_t socket, struct sockaddr_storage *address = NULL);
1500 
1508  static socket_t create(int family, int type, int protocol);
1509 
1517  static socket_t create(const struct addrinfo *address, int type, int protocol);
1518 
1528  static socket_t create(const char *iface, const char *service, int family = AF_UNSPEC, int type = 0, int protocol = 0);
1529 
1535  static socket_t create(const Socket::address &address);
1536 
1541  static void release(socket_t socket);
1542 
1550  static char *hostname(const struct sockaddr *address, char *buffer, size_t size);
1551 
1559  static struct addrinfo *hinting(socket_t socket, struct addrinfo *hint);
1560 
1571  static socklen_t query(socket_t socket, struct sockaddr_storage *address, const char *hostname, const char *service);
1572 
1578  static socklen_t len(const struct sockaddr *address);
1579 
1587  static bool equal(const struct sockaddr *address1, const struct sockaddr *address2);
1588 
1595  static unsigned copy(struct sockaddr *target, const struct sockaddr *origin);
1596 
1603  static unsigned store(struct sockaddr_storage *storage, const struct sockaddr *address);
1604 
1611  static unsigned store(struct sockaddr_internet *storage, const struct sockaddr *address);
1612 
1620  static bool eq_host(const struct sockaddr *address1, const struct sockaddr *address2);
1621 
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);}
1631 
1639  inline static bool eq_inet(const struct sockaddr_internet *address1, const struct sockaddr_internet *address2)
1640  {return equal((const struct sockaddr *)address1, (const struct sockaddr *)address2);}
1641 
1649  static bool eq_subnet(const struct sockaddr *address1, const struct sockaddr *address2);
1650 
1658  static int via(struct sockaddr *address, const struct sockaddr *destination);
1659 
1667  static char *query(const struct sockaddr *address, char *buffer, socklen_t size);
1668 
1674  static short service(const struct sockaddr *address);
1675 
1676  inline static in_port_t port(const struct sockaddr *address) {
1677  return service(address);
1678  }
1679 
1685  inline static short service(const struct sockaddr_internet *address) {
1686  return service((const struct sockaddr *)address);
1687  }
1688 
1689  inline static in_port_t port(const struct sockaddr_internet *address) {
1690  return service((const struct sockaddr *)address);
1691  }
1692 
1699  static unsigned keyindex(const struct sockaddr *address, unsigned size);
1700 
1707  static unsigned keyhost(const struct sockaddr *address, unsigned size);
1708 
1712  static void init(void);
1713 
1718  static void init(const char *program);
1719 
1725  static void query(int family);
1726 
1733  static void v4mapping(bool enable);
1734 
1739  static int error(void);
1740 
1749  static bool is_null(const char *string);
1750 
1758  static bool is_numeric(const char *string);
1759 
1768  static int local(socket_t socket, struct sockaddr_storage *address);
1769 
1778  static int remote(socket_t socket, struct sockaddr_storage *address);
1779 };
1780 
1786 class __EXPORT ListenSocket : protected Socket
1787 {
1788 private:
1789  __DELETE_COPY(ListenSocket);
1790 
1791 public:
1801  ListenSocket(const char *address, const char *service, unsigned backlog = 5, int family = AF_UNSPEC, int type = 0, int protocol = 0);
1802 
1813  static socket_t create(const char *address, const char *service, unsigned backlog = 5, int family = AF_UNSPEC, int type = 0, int protocol = 0);
1814 
1820  socket_t accept(struct sockaddr_storage *address = NULL) const;
1821 
1827  inline bool wait(timeout_t timeout = Timer::inf) const
1828  {return Socket::wait(timeout);}
1829 
1834  inline operator socket_t() const
1835  {return so;}
1836 
1841  inline socket_t operator*() const
1842  {return so;}
1843 
1848  inline socket_t getsocket(void) const
1849  {return so;}
1850 
1851  inline socket_t handle(void) const
1852  {return so;}
1853 
1854 };
1855 
1861 class __EXPORT TCPServer : public ListenSocket
1862 {
1863 private:
1864  __DELETE_DEFAULTS(TCPServer);
1865 
1866 public:
1874  TCPServer(const char *address, const char *service, unsigned backlog = 5);
1875 };
1876 
1880 __EXPORT struct addrinfo *_nextaddrinfo(struct addrinfo *addrinfo);
1881 
1885 __EXPORT struct sockaddr *_getaddrinfo(struct addrinfo *addrinfo);
1886 
1890 __EXPORT socket_t _getaddrsock(struct addrinfo *addrinfo);
1891 
1897 template <>
1898 class linked_pointer<sockaddr_struct>
1899 {
1900 private:
1901  struct addrinfo *ptr;
1902 
1903 public:
1904  inline linked_pointer(struct addrinfo *list)
1905  {ptr = list;}
1906 
1907  inline linked_pointer()
1908  {ptr = NULL;}
1909 
1910  inline linked_pointer(Socket::address& list)
1911  {ptr = list.getList();}
1912 
1917  inline operator struct sockaddr *() const
1918  {return _getaddrinfo(ptr);}
1919 
1924  inline struct sockaddr *operator*() const
1925  {return _getaddrinfo(ptr);}
1926 
1927  inline operator struct sockaddr_in *() const
1928  {return (struct sockaddr_in *)_getaddrinfo(ptr);}
1929 
1930  inline struct sockaddr_in *in(void) const
1931  {return (struct sockaddr_in *)_getaddrinfo(ptr);}
1932 
1933 #ifdef AF_INET6
1934  inline operator struct sockaddr_in6 *() const
1935  {return (struct sockaddr_in6 *)_getaddrinfo(ptr);}
1936 
1937  inline struct sockaddr_in6 *in6(void) const
1938  {return (struct sockaddr_in6 *)_getaddrinfo(ptr);}
1939 #endif
1940 
1944  inline socket_t operator()(void) const
1945  {return _getaddrsock(ptr);}
1946 
1951  inline operator bool() const
1952  {return ptr != NULL;}
1953 
1958  inline void operator=(struct addrinfo *list)
1959  {ptr = list;}
1960 
1965  inline void operator=(Socket::address& list)
1966  {ptr = list.getList();}
1967 
1972  inline void set(struct addrinfo *list)
1973  {ptr = list;}
1974 
1979  inline void set(Socket::address& list)
1980  {ptr = list.getList();}
1981 
1982 
1987  inline struct sockaddr* operator->() const
1988  {return _getaddrinfo(ptr);}
1989 
1994  inline bool operator!() const
1995  {return ptr == NULL;}
1996 
1997  inline void next(void)
1998  {ptr = _nextaddrinfo(ptr);}
1999 };
2000 
2006 inline struct addrinfo *addrinfo(Socket::address& address)
2007  {return address.getList();}
2008 
2015 inline struct sockaddr *addr(Socket::address& address)
2016  {return address.get();}
2017 
2025 inline bool eq(const struct sockaddr *s1, const struct sockaddr *s2)
2026  {return Socket::equal(s1, s2);}
2027 
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);}
2037 
2045 inline bool eq_host(const struct sockaddr *s1, const struct sockaddr *s2)
2046  {return Socket::eq_host(s1, s2);}
2047 
2048 inline bool eq_subnet(const struct sockaddr *s1, const struct sockaddr *s2)
2049  {return Socket::eq_subnet(s1, s2);}
2050 
2051 String str(Socket& so, strsize_t size);
2052 
2053 namespace Type {
2054 
2055  class SockAddress
2056  {
2057  private:
2058  struct sockaddr_storage storage;
2059 
2060  public:
2061  inline SockAddress() {
2062  memset(&storage, 0, sizeof(storage));
2063  }
2064 
2065  inline SockAddress(const SockAddress& copy) {
2066  memcpy(&storage, &copy.storage, sizeof(storage));
2067  }
2068 
2069  inline SockAddress(const struct sockaddr *addr) {
2070  Socket::store(&storage, addr);
2071  }
2072 
2073  inline SockAddress& operator=(const SockAddress& copy) {
2074  memcpy(&storage, &copy.storage, sizeof(storage));
2075  return *this;
2076  }
2077 
2078  inline SockAddress& operator=(const struct sockaddr *addr) {
2079  Socket::store(&storage, addr);
2080  return *this;
2081  }
2082 
2083  inline operator const struct sockaddr *() const {
2084  return (const struct sockaddr*)&storage;
2085  }
2086 
2087  inline struct sockaddr *operator*() {
2088  return (struct sockaddr *)&storage;
2089  }
2090 
2091  inline const struct sockaddr *get() const {
2092  return (const struct sockaddr *)&storage;
2093  }
2094 
2095  inline socklen_t size() {
2096  return sizeof(storage);
2097  }
2098 
2099  inline bool operator==(const SockAddress& check) const {
2100  return Socket::equal(get(), check.get());
2101  }
2102 
2103  inline bool operator!=(const SockAddress& check) const {
2104  return !Socket::equal(get(), check.get());
2105  }
2106 
2107  inline bool operator==(const struct sockaddr *check) const {
2108  return Socket::equal(get(), check);
2109  }
2110 
2111  inline bool operator!=(const struct sockaddr *check) const {
2112  return !Socket::equal(get(), check);
2113  }
2114  };
2115 
2116  class InetAddress
2117  {
2118  private:
2119  struct sockaddr_internet storage;
2120 
2121  public:
2122  inline InetAddress() {
2123  memset(&storage, 0, sizeof(storage));
2124  }
2125 
2126  inline InetAddress(const InetAddress& copy) {
2127  memcpy(&storage, &copy.storage, sizeof(storage));
2128  }
2129 
2130  inline InetAddress(const struct sockaddr *addr) {
2131  Socket::store(&storage, addr);
2132  }
2133 
2134  inline InetAddress& operator=(const InetAddress& copy) {
2135  memcpy(&storage, &copy.storage, sizeof(storage));
2136  return *this;
2137  }
2138 
2139  inline InetAddress& operator=(const struct sockaddr *addr) {
2140  Socket::store(&storage, addr);
2141  return *this;
2142  }
2143 
2144  inline operator const struct sockaddr *() const {
2145  return (const struct sockaddr*)&storage;
2146  }
2147 
2148  inline struct sockaddr *operator*() {
2149  return (struct sockaddr *)&storage;
2150  }
2151 
2152  inline const struct sockaddr *get() const {
2153  return (const struct sockaddr *)&storage;
2154  }
2155 
2156  inline socklen_t size() {
2157  return sizeof(storage);
2158  }
2159 
2160  inline bool operator==(const SockAddress& check) const {
2161  return Socket::equal(get(), check.get());
2162  }
2163 
2164  inline bool operator!=(const SockAddress& check) const {
2165  return !Socket::equal(get(), check.get());
2166  }
2167 
2168  inline bool operator==(const struct sockaddr *check) const {
2169  return Socket::equal(get(), check);
2170  }
2171 
2172  inline bool operator!=(const struct sockaddr *check) const {
2173  return !Socket::equal(get(), check);
2174  }
2175  };
2176 }
2177 
2178 typedef TCPServer tcpserv_t;
2179 
2180 } // namespace ucommon
2181 
2182 #endif
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.
Definition: socket.h:303
void release(SharedAccess &object)
Convenience function to unlock shared object through it's protocol.
Definition: access.h:280
socket_t getsocket(void) const
Get the socket descriptor of the listener.
Definition: socket.h:1848
int priority(int scheduling)
Set packet priority, 0 to 6 unless privileged.
Definition: socket.h:1074
int loopback(bool enable)
Set loopback to read multicast packets we broadcast.
Definition: socket.h:968
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.
Definition: socket.h:2045
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.
Definition: socket.h:1965
int nodelay(void) const
Set nodelay option for tcp socket.
Definition: socket.h:904
A generic socket base class.
Definition: socket.h:322
An object that holds ipv4 or ipv6 binary encoded host addresses.
Definition: socket.h:93
Common base class for all objects that can be formed into a linked list.
Definition: linked.h:59
int tos(int type)
Set the type of service field of outgoing packets.
Definition: socket.h:1065
int ttl(unsigned char time)
Set the time to live before packets expire.
Definition: socket.h:983
A class to hold internet segment routing rules.
Definition: socket.h:170
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.
Definition: socket.h:2006
inethostaddr_t getNetmask(void) const
Get the effective network mask for our cidr block.
Definition: socket.h:268
socket_t operator*() const
Get the socket descriptor of the listener by pointer reference.
Definition: socket.h:1841
static int family(const struct sockaddr_storage &address)
Get the address family of a socket address object.
Definition: socket.h:1398
unsigned short strsize_t
A convenience class for size of strings.
Definition: string.h:71
ObjectProtocol * copy(ObjectProtocol *object)
Convenience function to access object copy.
Definition: object.h:510
A copy-on-write string class that operates by reference count.
Definition: string.h:83
socket_t operator()(void) const
Get socket as expression operator.
Definition: socket.h:1944
in_port_t getPort(void) const
Get the port of the first address .
Definition: socket.h:519
bool isLoopback() const
Test if the first socket address is ADDR_LOOPBACK: 127.0.0.1 or ::1.
Definition: socket.h:614
Linked objects, lists, templates, and containers.
int sendwait(unsigned size)
Set the size to wait before sending.
Definition: socket.h:999
bool operator!() const
Test if we have no address list.
Definition: socket.h:1994
bool eq(const struct sockaddr *s1, const struct sockaddr *s2)
Compare two socket addresses to see if equal.
Definition: socket.h:2025
struct sockaddr * operator*() const
Return the full socket address list by pointer reference.
Definition: socket.h:1924
struct addrinfo * operator*() const
Return the full socket address list by pointer reference.
Definition: socket.h:558
int broadcast(bool enable)
Set socket for unicast mode broadcasts.
Definition: socket.h:936
T * dup(const T &object)
Convenience function to duplicate object pointer to heap.
Definition: generics.h:324
T * init(T *memory)
Template function to initialize memory by invoking default constructor.
Definition: platform.h:510
static size_t getLength(const struct sockaddr *address)
Returns the size of the socket address according to the family.
Definition: socket.h:716
unsigned getMask(void) const
Get the number of bits in the cidr bitmask.
Definition: socket.h:282
A bound socket used to listen for inbound socket connections.
Definition: socket.h:1786
bool isAny() const
Test if the first socket address is ADDR_ANY: 0.0.0.0 or ::0.
Definition: socket.h:598
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.
Definition: socket.h:187
void set(struct addrinfo *list)
Assign our pointer from an address list.
Definition: socket.h:1972
inethostaddr_t getNetwork(void) const
Get the network host base address of our cidr block.
Definition: socket.h:261
unsigned segsize(unsigned size)
Set segment size and get mtu of a socket.
Definition: socket.h:1046
An object that can hold a ipv4 or ipv6 socket address.
Definition: socket.h:113
static bool eq_inet(const struct sockaddr_internet *address1, const struct sockaddr_internet *address2)
Compare socket addresses.
Definition: socket.h:1639
Realtime timers and timer queues.
struct sockaddr * addr(Socket::address &address)
A convenience function to convert a socket address list into a socket address.
Definition: socket.h:2015
void shutdown(void)
Shutdown the socket communication channel.
Definition: socket.h:1080
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:573
int recvsize(unsigned size)
Set the size of the socket receive buffer.
Definition: socket.h:1008
bool ccid(uint8_t id)
Set ccid of dccp socket.
Definition: socket.h:1054
const char * getName(void) const
Get the saved name of our cidr.
Definition: socket.h:247
int blocking(bool enable)
Set socket blocking I/O mode.
Definition: socket.h:952
int multicast(unsigned ttl=1)
Set multicast mode and multicast broadcast range.
Definition: socket.h:960
Common namespace for all ucommon objects.
Definition: access.h:47
struct addrinfo * getList(void) const
Get the full socket address list from the object.
Definition: socket.h:544
int keepalive(bool enable)
Set socket for keepalive packets.
Definition: socket.h:944
bool wait(timeout_t timeout=Timer::inf) const
Wait for a pending connection.
Definition: socket.h:1827
socket_t _getaddrsock(struct addrinfo *addrinfo)
Helper function for linked_pointer.
size_t getLength(void) const
Get the address size of the first address.
Definition: socket.h:512
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.
Definition: linked.h:1463
int getError(void)
Get socket error code.
Definition: socket.h:975
int getFamily(void) const
Get the address family of our cidr block object.
Definition: socket.h:254
static short service(const struct sockaddr_internet *address)
Get the service port of an inet socket.
Definition: socket.h:1685
Timer class to use when scheduling realtime events.
Definition: timers.h:50
A smart pointer template for iterating linked lists.
Definition: linked.h:1348
void wait(barrier_t &barrier)
Convenience function to wait on a barrier.
Definition: thread.h:1921
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.
Definition: socket.h:1241
static ssize_t sendinet(socket_t socket, const void *buffer, size_t size, int flags, const struct sockaddr_internet *address)
Send to internet socket.
Definition: socket.h:1452
static int family(const struct sockaddr_internet &address)
Get the address family of an internet socket address object.
Definition: socket.h:1406
void set(Socket::address &list)
Assign our pointer from an address list.
Definition: socket.h:1979
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.
Definition: socket.h:928
static const timeout_t inf
A value to use for infinite time.
Definition: timers.h:83
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:1440
linked_pointer()
Create a linked pointer not attached to a list.
Definition: linked.h:1393
void operator=(struct addrinfo *list)
Assign our pointer from an address list.
Definition: socket.h:1958
static bool eq_from(const struct sockaddr_storage *address1, const struct sockaddr_storage *address2)
Compare socket addresses.
Definition: socket.h:1629
address(const addrinfo *address)
Construct a socket address from an addrinfo structure.
Definition: socket.h:427
struct sockaddr * operator->() const
Return member from typed object our pointer references.
Definition: socket.h:1987
int type(void)
Get the type of a socket.
Definition: socket.h:1038
bool operator!=(const struct sockaddr *address) const
Test if a given socket address falls outside this cidr.
Definition: socket.h:311
inethostaddr_t getBroadcast(void) const
Get the broadcast host address represented by our cidr.
Definition: socket.h:275
bool operator!() const
Test if we have no address list.
Definition: socket.h:590
int err(void) const
Get error code.
Definition: socket.h:876
A generic tcp server class.
Definition: socket.h:1861
int sendsize(unsigned size)
Set the size of the socket send buffer.
Definition: socket.h:991
address(const sockaddr &address)
Construct a socket address from a sockaddr object.
Definition: socket.h:421
A generic socket address class.
Definition: socket.h:359