UCommon
socket.h
Go to the documentation of this file.
1 // Copyright (C) 2006-2014 David Sugar, Tycho Softworks.
2 //
3 // This file is part of GNU uCommon C++.
4 //
5 // GNU uCommon C++ is free software: you can redistribute it and/or modify
6 // it under the terms of the GNU Lesser General Public License as published
7 // by the Free Software Foundation, either version 3 of the License, or
8 // (at your option) any later version.
9 //
10 // GNU uCommon C++ is distributed in the hope that it will be useful,
11 // but WITHOUT ANY WARRANTY; without even the implied warranty of
12 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 // GNU Lesser General Public License for more details.
14 //
15 // You should have received a copy of the GNU Lesser General Public License
16 // along with GNU uCommon C++. If not, see <http://www.gnu.org/licenses/>.
17 
26 #ifndef _UCOMMON_SOCKET_H_
27 #define _UCOMMON_SOCKET_H_
28 
29 #ifndef _UCOMMON_TIMERS_H_
30 #include <ucommon/timers.h>
31 #endif
32 
33 #ifndef _UCOMMON_LINKED_H_
34 #include <ucommon/linked.h>
35 #endif
36 
37 #ifndef _UCOMMON_STRING_H_
38 #include <ucommon/string.h>
39 #endif
40 
41 extern "C" {
42  struct addrinfo;
43 }
44 
45 #ifdef _MSWINDOWS_
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;
51 #else
52 #include <unistd.h>
53 #include <sys/socket.h>
54 #include <net/if.h>
55 #include <netinet/in.h>
56 #include <netdb.h>
57 #endif
58 
59 #if defined(__ANDROID__)
60 typedef uint16_t in_port_t;
61 #endif
62 
63 #include <errno.h>
64 #include <stdio.h>
65 
66 #ifndef IPTOS_LOWDELAY
67 #define IPTOS_LOWDELAY 0x10
68 #define IPTOS_THROUGHPUT 0x08
69 #define IPTOS_RELIABILITY 0x04
70 #define IPTOS_MINCOST 0x02
71 #endif
72 
73 #ifdef AF_UNSPEC
74 #define DEFAULT_FAMILY AF_UNSPEC
75 #else
76 #define DEFAULT_FAMILY AF_INET
77 #endif
78 
79 struct sockaddr_internet;
80 
81 typedef struct sockaddr *sockaddr_t;
82 
83 typedef struct sockaddr sockaddr_struct; // older gcc needs...?
84 
88 typedef struct hostaddr_internet
89 {
90  union
91  {
92  struct in_addr ipv4;
93 #ifdef AF_INET6
94  struct in6_addr ipv6;
95 #endif
96  };
98 
99 #if defined(AF_INET6) || defined(__CYGWIN__)
100 
107 typedef struct sockaddr_internet
108 {
109  union {
110 #ifdef AF_INET6
111  struct sockaddr_in6 ipv6;
112 #endif
113  struct sockaddr_in ipv4;
114  struct sockaddr address;
115  };
117 #else
118 typedef struct sockaddr_internet
119 {
120  union {
121  struct sockaddr_in ipv4;
122  struct sockaddr address;
123  };
125 
126 struct sockaddr_storage
127 {
128 #ifdef AF_UNIX
129  char sa_data[128];
130 #else
131  char sa_data[sizeof(struct sockaddr_in)];
132 #endif
133 };
134 #endif
135 
136 #ifndef SOCK_DCCP
137 #define SOCK_DCCP 6
138 #endif
139 
140 #ifndef IPPROTO_DCCP
141 #define IPPROTO_DCCP 23
142 #endif
143 
144 #ifndef SOL_DCCP
145 #define SOL_DCCP 269
146 #endif
147 
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
152 
153 namespace ucommon {
154 
164 class __EXPORT cidr : public LinkedObject
165 {
166 protected:
167  int Family;
168  inethostaddr_t Netmask, Network;
169  char Name[16];
170 
171  unsigned mask(const char *cp) const;
172 
173  inethostaddr_t broadcast(void) const;
174 
175  unsigned mask(void) const;
176 
177 public:
182 
186  cidr();
187 
194  cidr(const char *string);
195 
201  cidr(policy **policy, const char *string);
202 
209  cidr(policy **policy, const char *string, const char *name);
210 
215  cidr(const cidr& existing);
216 
223  static const cidr *find(const policy *policy, const struct sockaddr *address);
224 
232  static const cidr *container(const policy *policy, const struct sockaddr *address);
233 
241  inline const char *getName(void) const
242  {return Name;}
243 
248  inline int getFamily(void) const
249  {return Family;}
250 
255  inline inethostaddr_t getNetwork(void) const
256  {return Network;}
257 
262  inline inethostaddr_t getNetmask(void) const
263  {return Netmask;}
264 
269  inline inethostaddr_t getBroadcast(void) const
270  {return broadcast();}
271 
276  inline unsigned getMask(void) const
277  {return mask();}
278 
283  void set(const char *string);
284 
290  bool is_member(const struct sockaddr *address) const;
291 
297  inline bool operator==(const struct sockaddr *address) const
298  {return is_member(address);}
299 
305  inline bool operator!=(const struct sockaddr *address) const
306  {return !is_member(address);}
307 };
308 
316 class __EXPORT Socket
317 {
318 protected:
319  socket_t so;
320  int ioerr;
321  timeout_t iowait;
322 
323 public:
332  static struct addrinfo *query(const char *host, const char *service, int type = SOCK_STREAM, int protocol = 0);
333 
339  static void release(struct addrinfo *list);
340 
349  class __EXPORT address
350  {
351  protected:
352  struct addrinfo *list;
353 
354  public:
365  address(int family, const char *address, int type = SOCK_STREAM, int protocol = 0);
366 
379  address(int family, const char *hostname, const char *service = NULL);
380 
387  address(const char *host, const char *service, int type = SOCK_STREAM);
388 
396  address(const char *hostname, in_port_t port = 0);
397 
401  address(const in_addr& address, in_port_t port = 0);
402 
406  address(const in6_addr& address, in_port_t port = 0);
407 
411  address(const sockaddr& address) : list(NULL)
412  {insert(address);}
413 
417  address(const addrinfo* list) : list(NULL)
418  {insert(list);}
419 
423  address();
424 
429  address(const address& reference);
430 
435  address& operator=(const address& rhs);
436 
440  ~address();
441 
447  bool operator==(const address& other) const;
448 
449  inline bool operator!=(const address& other) const
450  {return !(*this==other);}
451 
452  inline bool equals(const address& other) const
453  {return *this == other;}
454 
459  struct sockaddr *get(void) const;
460 
461  inline struct sockaddr *getAddr(void) const
462  {return get();}
463 
464  inline struct sockaddr *operator()(void) const
465  {return get();}
466 
471  inline operator struct sockaddr *() const
472  {return get();}
473 
479  struct sockaddr *get(int family) const;
480 
481  inline struct sockaddr *operator()(int family) const
482  {return get(family);}
483 
484  inline operator struct sockaddr_in *() const
485  {return (struct sockaddr_in *)get(AF_INET);}
486 
487 #ifdef AF_INET6
488  inline operator struct sockaddr_in6 *() const
489  {return (struct sockaddr_in6 *)get(AF_INET6);}
490 #endif
491 
496  int family(void) const;
497 
502  inline size_t getLength(void) const
503  {return len(get());}
504 
509  inline in_port_t getPort(void) const
510  {return getPort(get());}
511 
516  void setPort(in_port_t port);
517 
522  address withPort(in_port_t port) const;
523 
528  struct sockaddr *find(const struct sockaddr *addr) const;
529 
534  inline struct addrinfo *getList(void) const
535  {return list;}
536 
541  inline operator struct addrinfo *() const
542  {return list;}
543 
548  inline struct addrinfo *operator*() const
549  {return list;}
550 
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);}
565 
570  inline operator bool() const
571  {return list != NULL;}
572 
577  inline bool operator!() const
578  {return list == NULL;}
579 
585  inline bool isAny() const
586  {return isAny(get());}
587 
594  void setAny(int family = AF_UNSPEC);
595 
601  inline bool isLoopback() const
602  {return isLoopback(get());}
603 
610  void setLoopback(int family = AF_UNSPEC);
611 
615  void clear(void);
616 
623  void set(const char *hostname, const char *service = NULL, int type = SOCK_STREAM);
624 
631  void add(const char *hostname, const char *service = NULL, int type = SOCK_STREAM);
632 
640  void set(int family, const char *address, int type = SOCK_STREAM, int protocol = 0);
641 
646  void add(sockaddr *address);
647 
653  unsigned insert(const struct addrinfo *address);
654 
660  unsigned remove(const struct addrinfo *address);
661 
667  bool remove(const struct sockaddr *address);
668 
675  bool insert(const struct sockaddr *address);
676  inline bool insert(const struct sockaddr& address)
677  {return insert(&address);}
678 
684  void copy(const struct addrinfo *address);
685 
690  void set(struct sockaddr *address);
691 
697  void set(const char *hostname, in_port_t service = 0);
698 
703  static size_t getLength(const struct sockaddr *address)
704  {return len(address);}
705 
710  static in_port_t getPort(const struct sockaddr *address);
711 
717  static void setPort(struct sockaddr *address, in_port_t port);
718 
724  static bool isAny(const struct sockaddr *address);
725 
730  static void setAny(struct sockaddr *sa);
731 
735  static sockaddr_storage any(int family);
736 
742  static bool isLoopback(const struct sockaddr *address);
743 
749  static void setLoopback(struct sockaddr *sa);
750 
754  static sockaddr_storage loopback(int family);
755 
761  static struct sockaddr *dup(struct sockaddr *address);
762 
768  static struct sockaddr_in *ipv4(struct sockaddr *address);
769 
770 #ifdef AF_INET6
771 
776  static struct sockaddr_in6 *ipv6(struct sockaddr *address);
777 #endif
778 
791  static size_t print(const struct sockaddr *src, char* dst, size_t dst_sz, bool port=false, bool ipv6_brackets=false);
792  };
793 
794  friend class address;
795 
799  Socket();
800 
805  Socket(const Socket& existing);
806 
811  Socket(socket_t socket);
812 
818  Socket(const struct addrinfo *address);
819 
826  Socket(int family, int type, int protocol = 0);
827 
837  Socket(const char *address, const char *port, int family = AF_UNSPEC, int type = 0, int protocol = 0);
838 
842  virtual ~Socket();
843 
847  void cancel(void);
848 
853  static void cancel(socket_t socket);
854 
858  void release(void);
859 
863  inline int err(void) const
864  {return ioerr;}
865 
871  bool is_pending(unsigned value);
872 
877  bool connected(void) const;
878 
885  bool wait(timeout_t timeout = 0) const;
886 
891  inline int nodelay(void) const
892  {return nodelay(so);}
893 
901  static bool wait(socket_t socket, timeout_t timeout = 0);
902 
909  bool waitSending(timeout_t timeout = 0) const;
910 
915  inline unsigned pending(void) const
916  {return pending(so);}
917 
923  inline int broadcast(bool enable)
924  {return broadcast(so, enable);}
925 
931  inline int keepalive(bool enable)
932  {return keepalive(so, enable);}
933 
939  inline int blocking(bool enable)
940  {return blocking(so, enable);}
941 
947  inline int multicast(unsigned ttl = 1)
948  {return multicast(so, ttl);}
949 
955  inline int loopback(bool enable)
956  {return loopback(so, enable);}
957 
962  inline int getError(void)
963  {return error(so);}
964 
970  inline int ttl(unsigned char time)
971  {return ttl(so, time);}
972 
978  inline int sendsize(unsigned size)
979  {return sendsize(so, size);}
980 
986  inline int sendwait(unsigned size)
987  {return sendwait(so, size);}
988 
989 
995  inline int recvsize(unsigned size)
996  {return recvsize(so, size);}
997 
1003  static int type(socket_t socket);
1004 
1011  static unsigned segsize(socket_t socket, unsigned size = 0);
1012 
1019  static bool ccid(socket_t socket, uint8_t id);
1020 
1025  inline int type(void)
1026  {return type(so);}
1027 
1033  inline unsigned segsize(unsigned size)
1034  {return segsize(so, size);}
1035 
1041  inline bool ccid(uint8_t id)
1042  {return ccid(so, id);}
1043 
1052  inline int tos(int type)
1053  {return tos(so, type);}
1054 
1061  inline int priority(int scheduling)
1062  {return priority(so, scheduling);}
1063 
1067  inline void shutdown(void)
1068  {::shutdown(so, SHUT_RDWR);}
1069 
1077  int connectto(struct addrinfo *list);
1078 
1085  int disconnect(void);
1086 
1092  int join(const struct addrinfo *list, const int ifindex = 0);
1093 
1099  int drop(const struct addrinfo *list, const int ifindex = 0);
1100 
1106  int wait(timeout_t timeout = Timer::inf);
1107 
1114  size_t peek(void *data, size_t number) const;
1115 
1123  size_t readfrom(void *data, size_t number, struct sockaddr_storage *address = NULL);
1124 
1132  size_t writeto(const void *data, size_t number, const struct sockaddr *address = NULL);
1133 
1146  size_t readline(char *data, size_t size);
1147 
1153  size_t printf(const char *format, ...) __PRINTF(2,3);
1154 
1166  size_t readline(String& buffer);
1167 
1179  static ssize_t readline(socket_t socket, char *data, size_t size, timeout_t timeout = Timer::inf);
1180 
1187  static ssize_t printf(socket_t socket, const char *format, ...) __PRINTF(2,3);
1188 
1196  size_t writes(const char *string);
1197 
1202  operator bool();
1203 
1208  bool operator!() const;
1209 
1215  Socket& operator=(socket_t socket);
1216 
1221  inline operator socket_t() const
1222  {return so;}
1223 
1228  inline socket_t operator*() const
1229  {return so;}
1230 
1237  static unsigned pending(socket_t socket);
1238 
1245  static int sendsize(socket_t socket, unsigned size);
1246 
1253  static int sendwait(socket_t socket, unsigned size);
1254 
1261  static int recvsize(socket_t socket, unsigned size);
1262 
1271  static int connectto(socket_t socket, struct addrinfo *list);
1272 
1278  static int disconnect(socket_t socket);
1279 
1286  static int drop(socket_t socket, const struct addrinfo *list, const int ifindex = 0);
1287 
1294  static int join(socket_t socket, const struct addrinfo *list, const int ifindex = 0);
1295 
1301  static int error(socket_t socket);
1302 
1309  static int multicast(socket_t socket, unsigned ttl = 1);
1310 
1317  static int loopback(socket_t socket, bool enable);
1318 
1325  static int blocking(socket_t socket, bool enable);
1326 
1333  static int keepalive(socket_t socket, bool enable);
1334 
1341  static int broadcast(socket_t socket, bool enable);
1342 
1348  static int nodelay(socket_t socket);
1349 
1356  static int priority(socket_t socket, int scheduling);
1357 
1364  static int tos(socket_t socket, int type);
1365 
1372  static int ttl(socket_t socket, unsigned char time);
1373 
1378  static int family(socket_t socket);
1379 
1385  inline static int family(const struct sockaddr_storage& address)
1386  {return ((const struct sockaddr *)&address)->sa_family;}
1387 
1393  inline static int family(const struct sockaddr_internet& address)
1394  {return address.address.sa_family;}
1395 
1405  static ssize_t recvfrom(socket_t socket, void *buffer, size_t size, int flags = 0, struct sockaddr_storage *address = NULL);
1406 
1416  static ssize_t sendto(socket_t socket, const void *buffer, size_t size, int flags = 0, const struct sockaddr *address = NULL);
1417 
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);}
1429 
1439  inline static ssize_t sendinet(socket_t socket, const void *buffer, size_t size, int flags, const struct sockaddr_internet *address)
1440  {return sendto(socket, buffer, size, flags, (const struct sockaddr *)address);}
1441 
1451  static ssize_t recvinet(socket_t socket, void *buffer, size_t size, int flags = 0, struct sockaddr_internet *address = NULL);
1452 
1461  static int bindto(socket_t socket, const char *address, const char *service, int protocol = 0);
1462 
1470  static int listento(socket_t socket, const struct sockaddr *address, int backlog = 5);
1471 
1478  static int bindto(socket_t socket, const struct sockaddr *address);
1479 
1486  static socket_t acceptfrom(socket_t socket, struct sockaddr_storage *address = NULL);
1487 
1495  static socket_t create(int family, int type, int protocol);
1496 
1504  static socket_t create(const struct addrinfo *address, int type, int protocol);
1505 
1515  static socket_t create(const char *iface, const char *service, int family = AF_UNSPEC, int type = 0, int protocol = 0);
1516 
1522  static socket_t create(const Socket::address &address);
1523 
1528  static void release(socket_t socket);
1529 
1537  static char *hostname(const struct sockaddr *address, char *buffer, size_t size);
1538 
1546  static struct addrinfo *hinting(socket_t socket, struct addrinfo *hint);
1547 
1558  static socklen_t query(socket_t socket, struct sockaddr_storage *address, const char *hostname, const char *service);
1559 
1565  static socklen_t len(const struct sockaddr *address);
1566 
1574  static bool equal(const struct sockaddr *address1, const struct sockaddr *address2);
1575 
1582  static unsigned copy(struct sockaddr *target, const struct sockaddr *origin);
1583 
1590  inline static unsigned store(struct sockaddr_storage *storage, const struct sockaddr *address)
1591  {return copy((struct sockaddr*)storage, address);}
1592 
1599  static unsigned store(struct sockaddr_internet *storage, const struct sockaddr *address);
1600 
1608  static bool eq_host(const struct sockaddr *address1, const struct sockaddr *address2);
1609 
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);}
1619 
1627  inline static bool eq_inet(const struct sockaddr_internet *address1, const struct sockaddr_internet *address2)
1628  {return equal((const struct sockaddr *)address1, (const struct sockaddr *)address2);}
1629 
1637  static bool eq_subnet(const struct sockaddr *address1, const struct sockaddr *address2);
1638 
1646  static int via(struct sockaddr *address, const struct sockaddr *destination);
1647 
1655  static char *query(const struct sockaddr *address, char *buffer, socklen_t size);
1656 
1662  static short service(const struct sockaddr *address);
1663 
1669  inline static short service(const struct sockaddr_internet *address)
1670  {return service((const struct sockaddr *)address);}
1671 
1678  static unsigned keyindex(const struct sockaddr *address, unsigned size);
1679 
1686  static unsigned keyhost(const struct sockaddr *address, unsigned size);
1687 
1691  static void init(void);
1692 
1697  static void init(const char *program);
1698 
1704  static void query(int family);
1705 
1712  static void v4mapping(bool enable);
1713 
1718  static int error(void);
1719 
1728  static bool is_null(const char *string);
1729 
1737  static bool is_numeric(const char *string);
1738 
1747  static int local(socket_t socket, struct sockaddr_storage *address);
1748 
1757  static int remote(socket_t socket, struct sockaddr_storage *address);
1758 };
1759 
1765 class __EXPORT ListenSocket : protected Socket
1766 {
1767 public:
1777  ListenSocket(const char *address, const char *service, unsigned backlog = 5, int family = AF_UNSPEC, int type = 0, int protocol = 0);
1778 
1789  static socket_t create(const char *address, const char *service, unsigned backlog = 5, int family = AF_UNSPEC, int type = 0, int protocol = 0);
1790 
1796  socket_t accept(struct sockaddr_storage *address = NULL) const;
1797 
1803  inline bool wait(timeout_t timeout = Timer::inf) const
1804  {return Socket::wait(timeout);}
1805 
1810  inline operator socket_t() const
1811  {return so;}
1812 
1817  inline socket_t operator*() const
1818  {return so;}
1819 
1824  inline socket_t getsocket(void) const
1825  {return so;}
1826 
1827  inline socket_t handle(void) const
1828  {return so;}
1829 
1830 };
1831 
1837 class __EXPORT TCPServer : public ListenSocket
1838 {
1839 public:
1847  TCPServer(const char *address, const char *service, unsigned backlog = 5);
1848 };
1849 
1853 __EXPORT struct addrinfo *_nextaddrinfo(struct addrinfo *addrinfo);
1854 
1858 __EXPORT struct sockaddr *_getaddrinfo(struct addrinfo *addrinfo);
1859 
1863 __EXPORT socket_t _getaddrsock(struct addrinfo *addrinfo);
1864 
1870 template <>
1871 class linked_pointer<sockaddr_struct>
1872 {
1873 private:
1874  struct addrinfo *ptr;
1875 
1876 public:
1877  inline linked_pointer(struct addrinfo *list)
1878  {ptr = list;}
1879 
1880  inline linked_pointer()
1881  {ptr = NULL;}
1882 
1883  inline linked_pointer(Socket::address& list)
1884  {ptr = list.getList();}
1885 
1890  inline operator struct sockaddr *() const
1891  {return _getaddrinfo(ptr);}
1892 
1897  inline struct sockaddr *operator*() const
1898  {return _getaddrinfo(ptr);}
1899 
1900  inline operator struct sockaddr_in *() const
1901  {return (struct sockaddr_in *)_getaddrinfo(ptr);}
1902 
1903  inline struct sockaddr_in *in(void) const
1904  {return (struct sockaddr_in *)_getaddrinfo(ptr);}
1905 
1906 #ifdef AF_INET6
1907  inline operator struct sockaddr_in6 *() const
1908  {return (struct sockaddr_in6 *)_getaddrinfo(ptr);}
1909 
1910  inline struct sockaddr_in6 *in6(void) const
1911  {return (struct sockaddr_in6 *)_getaddrinfo(ptr);}
1912 #endif
1913 
1917  inline socket_t operator()(void) const
1918  {return _getaddrsock(ptr);}
1919 
1924  inline operator bool() const
1925  {return ptr != NULL;}
1926 
1931  inline void operator=(struct addrinfo *list)
1932  {ptr = list;}
1933 
1938  inline void operator=(Socket::address& list)
1939  {ptr = list.getList();}
1940 
1945  inline void set(struct addrinfo *list)
1946  {ptr = list;}
1947 
1952  inline void set(Socket::address& list)
1953  {ptr = list.getList();}
1954 
1955 
1960  inline struct sockaddr* operator->() const
1961  {return _getaddrinfo(ptr);}
1962 
1967  inline bool operator!() const
1968  {return ptr == NULL;}
1969 
1970  inline void next(void)
1971  {ptr = _nextaddrinfo(ptr);}
1972 };
1973 
1979 inline struct addrinfo *addrinfo(Socket::address& address)
1980  {return address.getList();}
1981 
1988 inline struct sockaddr *addr(Socket::address& address)
1989  {return address.get();}
1990 
1998 inline bool eq(const struct sockaddr *s1, const struct sockaddr *s2)
1999  {return Socket::equal(s1, s2);}
2000 
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);}
2010 
2018 inline bool eq_host(const struct sockaddr *s1, const struct sockaddr *s2)
2019  {return Socket::eq_host(s1, s2);}
2020 
2021 inline bool eq_subnet(const struct sockaddr *s1, const struct sockaddr *s2)
2022  {return Socket::eq_subnet(s1, s2);}
2023 
2024 String str(Socket& so, strsize_t size);
2025 
2026 typedef TCPServer tcpserv_t;
2027 
2028 } // namespace ucommon
2029 
2030 #endif
unsigned segsize(unsigned size)
Set segment size and get mtu of a socket.
Definition: socket.h:1033
bool operator==(const struct sockaddr *address) const
Test if a given socket address falls within this cidr.
Definition: socket.h:297
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.
Definition: socket.h:107
void set(struct addrinfo *list)
Assign our pointer from an address list.
Definition: socket.h:1945
void next(void)
Move (iterate) pointer to next member in linked list.
Definition: linked.h:1393
bool isAny() const
Test if the first socket address is ADDR_ANY: 0.0.0.0 or ::0.
Definition: socket.h:585
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.
Definition: socket.h:978
static size_t getLength(const struct sockaddr *address)
Returns the size of the socket address according to the family.
Definition: socket.h:703
address(const addrinfo *list)
Construct a socket address from an addrinfo structure.
Definition: socket.h:417
T * dup(const T &object)
Convenience function to duplicate object pointer to heap.
Definition: generics.h:478
int multicast(unsigned ttl=1)
Set multicast mode and multicast broadcast range.
Definition: socket.h:947
Linked objects, lists, templates, and containers.
int type(void)
Get the type of a socket.
Definition: socket.h:1025
unsigned long timeout_t
Typedef for millisecond timer values.
Definition: platform.h:341
A bound socket used to listen for inbound socket connections.
Definition: socket.h:1765
void wait(barrier_t &barrier)
Convenience function to wait on a barrier.
Definition: thread.h:1925
void shutdown(void)
Shutdown the socket communication channel.
Definition: socket.h:1067
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.
Definition: string.h:82
bool operator!() const
Test if we have no address list.
Definition: socket.h:577
int keepalive(bool enable)
Set socket for keepalive packets.
Definition: socket.h:931
T * init(T *memory)
Template function to initialize memory by invoking default constructor.
Definition: platform.h:435
size_t getLength(void) const
Get the address size of the first address.
Definition: socket.h:502
A generic tcp server class.
Definition: socket.h:1837
An object that holds ipv4 or ipv6 binary encoded host addresses.
Definition: socket.h:88
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.
Definition: socket.h:534
struct sockaddr * operator*() const
Return the full socket address list by pointer reference.
Definition: socket.h:1897
static const timeout_t inf
A value to use for infinite time.
Definition: timers.h:82
int ttl(unsigned char time)
Set the time to live before packets expire.
Definition: socket.h:970
A smart pointer template for iterating linked lists.
Definition: linked.h:1292
int priority(int scheduling)
Set packet priority, 0 to 6 unless privileged.
Definition: socket.h:1061
Realtime timers and timer queues.
bool ccid(uint8_t id)
Set ccid of dccp socket.
Definition: socket.h:1041
socket_t operator*() const
Get the socket descriptor of the listener by pointer reference.
Definition: socket.h:1817
Common base class for all objects that can be formed into a linked list.
Definition: linked.h:58
void release(SharedAccess &object)
Convenience function to unlock shared object through it's protocol.
Definition: access.h:260
socket_t operator*() const
Get the socket descriptor by pointer reference.
Definition: socket.h:1228
bool eq_host(const struct sockaddr *s1, const struct sockaddr *s2)
Compare two host addresses to see if equal.
Definition: socket.h:2018
int sendwait(unsigned size)
Set the size to wait before sending.
Definition: socket.h:986
inethostaddr_t getBroadcast(void) const
Get the broadcast host address represented by our cidr.
Definition: socket.h:269
bool wait(timeout_t timeout=0) const
Test for pending input data.
int getError(void)
Get socket error code.
Definition: socket.h:962
unsigned short strsize_t
A convenience class for size of strings.
Definition: string.h:70
struct addrinfo * _nextaddrinfo(struct addrinfo *addrinfo)
Helper function for linked_pointer.
address(const sockaddr &address)
Construct a socket address from a sockaddr object.
Definition: socket.h:411
socket_t operator()(void) const
Get socket as expression operator.
Definition: socket.h:1917
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.
Definition: socket.h:1938
struct sockaddr * get(void) const
Get the first socket address in our address list.
int blocking(bool enable)
Set socket blocking I/O mode.
Definition: socket.h:939
struct addrinfo * addrinfo(Socket::address &address)
A convenience function to convert a socket address list into an addrinfo.
Definition: socket.h:1979
bool operator!() const
Test if we have no address list.
Definition: socket.h:1967
static int family(const struct sockaddr_storage &address)
Get the address family of a socket address object.
Definition: socket.h:1385
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:1439
linked_pointer()
Create a linked pointer not attached to a list.
Definition: linked.h:1332
static int family(const struct sockaddr_internet &address)
Get the address family of an internet socket address object.
Definition: socket.h:1393
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:563
int recvsize(unsigned size)
Set the size of the socket receive buffer.
Definition: socket.h:995
int nodelay(void) const
Set nodelay option for tcp socket.
Definition: socket.h:891
int broadcast(bool enable)
Set socket for unicast mode broadcasts.
Definition: socket.h:923
in_port_t getPort(void) const
Get the port of the first address .
Definition: socket.h:509
bool wait(timeout_t timeout=Timer::inf) const
Wait for a pending connection.
Definition: socket.h:1803
unsigned getMask(void) const
Get the number of bits in the cidr bitmask.
Definition: socket.h:276
A generic socket address class.
Definition: socket.h:349
static unsigned store(struct sockaddr_storage *storage, const struct sockaddr *address)
Store an address into an address object.
Definition: socket.h:1590
socket_t getsocket(void) const
Get the socket descriptor of the listener.
Definition: socket.h:1824
static bool eq_inet(const struct sockaddr_internet *address1, const struct sockaddr_internet *address2)
Compare socket addresses.
Definition: socket.h:1627
void set(Socket::address &list)
Assign our pointer from an address list.
Definition: socket.h:1952
struct sockaddr * addr(Socket::address &address)
A convenience function to convert a socket address list into a socket address.
Definition: socket.h:1988
int tos(int type)
Set the type of service field of outgoing packets.
Definition: socket.h:1052
int getFamily(void) const
Get the address family of our cidr block object.
Definition: socket.h:248
struct addrinfo * operator*() const
Return the full socket address list by pointer reference.
Definition: socket.h:548
struct sockaddr * operator->() const
Return member from typed object our pointer references.
Definition: socket.h:1960
A class to hold internet segment routing rules.
Definition: socket.h:164
Timer class to use when scheduling realtime events.
Definition: timers.h:49
const char * getName(void) const
Get the saved name of our cidr.
Definition: socket.h:241
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:1427
inethostaddr_t getNetmask(void) const
Get the effective network mask for our cidr block.
Definition: socket.h:262
bool operator!=(const struct sockaddr *address) const
Test if a given socket address falls outside this cidr.
Definition: socket.h:305
void operator=(struct addrinfo *list)
Assign our pointer from an address list.
Definition: socket.h:1931
unsigned pending(void) const
Get the number of bytes of data in the socket receive buffer.
Definition: socket.h:915
A generic socket base class.
Definition: socket.h:316
A common string class and character string support functions.
ObjectProtocol * copy(ObjectProtocol *object)
Convenience function to access object copy.
Definition: object.h:479
struct sockaddr * _getaddrinfo(struct addrinfo *addrinfo)
Helper function for linked_pointer.
int err(void) const
Get error code.
Definition: socket.h:863
static bool eq_from(const struct sockaddr_storage *address1, const struct sockaddr_storage *address2)
Compare socket addresses.
Definition: socket.h:1617
LinkedObject policy
A convenience type for using a pointer to a linked list as a policy chain.
Definition: socket.h:181
static short service(const struct sockaddr_internet *address)
Get the service port of an inet socket.
Definition: socket.h:1669
int loopback(bool enable)
Set loopback to read multicast packets we broadcast.
Definition: socket.h:955
bool eq(const struct sockaddr *s1, const struct sockaddr *s2)
Compare two socket addresses to see if equal.
Definition: socket.h:1998
inethostaddr_t getNetwork(void) const
Get the network host base address of our cidr block.
Definition: socket.h:255
bool isLoopback() const
Test if the first socket address is ADDR_LOOPBACK: 127.0.0.1 or ::1.
Definition: socket.h:601