ucommon
socket.h
Go to the documentation of this file.
1 // Copyright (C) 2006-2010 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 #else
50 #include <unistd.h>
51 #include <sys/socket.h>
52 #include <net/if.h>
53 #include <netinet/in.h>
54 #include <netdb.h>
55 #endif
56 
57 #include <errno.h>
58 #include <stdio.h>
59 
60 #ifndef IPTOS_LOWDELAY
61 #define IPTOS_LOWDELAY 0x10
62 #define IPTOS_THROUGHPUT 0x08
63 #define IPTOS_RELIABILITY 0x04
64 #define IPTOS_MINCOST 0x02
65 #endif
66 
67 #ifdef AF_UNSPEC
68 #define DEFAULT_FAMILY AF_UNSPEC
69 #else
70 #define DEFAULT_FAMILY AF_INET
71 #endif
72 
73 struct sockaddr_internet;
74 
75 typedef struct sockaddr *sockaddr_t;
76 
80 typedef struct hostaddr_internet
81 {
82  union
83  {
84  struct in_addr ipv4;
85 #ifdef AF_INET6
86  struct in6_addr ipv6;
87 #endif
88  };
90 
91 #if defined(AF_INET6) || defined(__CYGWIN__)
92 
99 typedef struct sockaddr_internet
100 {
101  union {
102 #ifdef AF_INET6
103  struct sockaddr_in6 ipv6;
104 #endif
105  struct sockaddr_in ipv4;
106  struct sockaddr address;
107  };
109 #else
110 typedef struct sockaddr_internet
111 {
112  union {
113  struct sockaddr_in ipv4;
114  struct sockaddr address;
115  };
117 
118 struct sockaddr_storage
119 {
120 #ifdef AF_UNIX
121  char sa_data[128];
122 #else
123  char sa_data[sizeof(struct sockaddr_in)];
124 #endif
125 };
126 #endif
127 
128 #ifndef SOCK_DCCP
129 #define SOCK_DCCP 6
130 #endif
131 
132 #ifndef IPPROTO_DCCP
133 #define IPPROTO_DCCP 23
134 #endif
135 
136 #ifndef SOL_DCCP
137 #define SOL_DCCP 269
138 #endif
139 
140 #define DCCP_SOCKOPT_AVAILABLE_CCIDS 12
141 #define DCCP_SOCKOPT_CCID 13
142 #define DCCP_SOCKOPT_TX_CCID 14
143 #define DCCP_SOCKOPT_RX_CCID 15
144 
145 NAMESPACE_UCOMMON
146 
156 class __EXPORT cidr : public LinkedObject
157 {
158 protected:
159  int family;
160  inethostaddr_t netmask, network;
161  char name[16];
162  unsigned getMask(const char *cp) const;
163 
164 public:
169 
173  cidr();
174 
181  cidr(const char *string);
182 
188  cidr(policy **policy, const char *string);
189 
196  cidr(policy **policy, const char *string, const char *name);
197 
202  cidr(const cidr& existing);
203 
210  static cidr *find(policy *policy, const struct sockaddr *address);
211 
219  static cidr *container(policy *policy, const struct sockaddr *address);
220 
228  inline const char *getName(void) const
229  {return name;};
230 
235  inline int getFamily(void) const
236  {return family;};
237 
242  inline inethostaddr_t getNetwork(void) const
243  {return network;};
244 
249  inline inethostaddr_t getNetmask(void) const
250  {return netmask;};
251 
256  inethostaddr_t getBroadcast(void) const;
257 
262  unsigned getMask(void) const;
263 
268  void set(const char *string);
269 
275  bool isMember(const struct sockaddr *address) const;
276 
282  inline bool operator==(const struct sockaddr *address) const
283  {return isMember(address);};
284 
290  inline bool operator!=(const struct sockaddr *address) const
291  {return !isMember(address);};
292 };
293 
301 class __EXPORT Socket
302 {
303 protected:
304  socket_t so;
305  int ioerr;
306  timeout_t iowait;
307 
308 public:
317  static struct addrinfo *getaddress(const char *host, const char *service, int type = SOCK_STREAM, int protocol = 0);
318 
324  static void release(struct addrinfo *list);
325 
331  typedef void *set_t;
332 
333  static const size_t masksize;
334 
343  class __EXPORT address
344  {
345  protected:
346  struct addrinfo *list;
347 
348  public:
359  address(int family, const char *address, int type = SOCK_STREAM, int protocol = 0);
360 
373  address(int family, const char *hostname, const char *service = NULL);
374 
381  address(const char *host, const char *service, int type = SOCK_STREAM);
382 
390  address(const char *hostname, unsigned service = 0);
391 
395  address();
396 
401  address(const address& reference);
402 
406  ~address();
407 
412  struct sockaddr *getAddr(void) const;
413 
419  struct sockaddr *get(int family) const;
420 
425  int getfamily(void) const;
426 
431  struct sockaddr *find(struct sockaddr *addr) const;
432 
437  inline struct addrinfo *getList(void) const
438  {return list;};
439 
444  inline operator struct addrinfo *() const
445  {return list;};
446 
451  inline struct addrinfo *operator*() const
452  {return list;};
453 
458  inline operator bool() const
459  {return list != NULL;};
460 
465  inline bool operator!() const
466  {return list == NULL;};
467 
472  inline operator struct sockaddr *() const
473  {return getAddr();};
474 
478  void clear(void);
479 
486  void set(const char *hostname, const char *service = NULL, int type = SOCK_STREAM);
487 
494  void add(const char *hostname, const char *service = NULL, int type = SOCK_STREAM);
495 
503  void set(int family, const char *address, int type = SOCK_STREAM, int protocol = 0);
504 
509  void add(sockaddr *address);
510 
516  unsigned insert(struct addrinfo *address);
517 
523  unsigned remove(struct addrinfo *address);
524 
530  bool remove(struct sockaddr *address);
531 
538  bool insert(struct sockaddr *address);
539 
545  void copy(const struct addrinfo *address);
546 
551  void set(struct sockaddr *address);
552 
558  void set(const char *hostname, unsigned service = 0);
559 
565  static struct sockaddr *dup(struct sockaddr *address);
566 
572  static struct sockaddr_in *ipv4(struct sockaddr *address);
573 
574 #ifdef AF_INET6
575 
580  static struct sockaddr_in6 *ipv6(struct sockaddr *address);
581 #endif
582  };
583 
584  friend class address;
585 
589  Socket();
590 
595  Socket(const Socket& existing);
596 
601  Socket(socket_t socket);
602 
608  Socket(struct addrinfo *address);
609 
616  Socket(int family, int type, int protocol = 0);
617 
627  Socket(const char *address, const char *port, int family = AF_UNSPEC, int type = 0, int protocol = 0);
628 
632  virtual ~Socket();
633 
637  void cancel(void);
638 
643  static void cancel(socket_t socket);
644 
648  void release(void);
649 
653  inline int err(void) const
654  {return ioerr;}
655 
661  bool isPending(unsigned value) const;
662 
667  bool isConnected(void) const;
668 
675  bool waitPending(timeout_t timeout = 0) const;
676 
681  inline int nodelay(void) const
682  {return nodelay(so);};
683 
691  static bool wait(socket_t socket, timeout_t timeout = 0);
692 
699  bool waitSending(timeout_t timeout = 0) const;
700 
705  inline unsigned getPending(void) const
706  {return pending(so);};
707 
713  inline int broadcast(bool enable)
714  {return broadcast(so, enable);};
715 
721  inline int keepalive(bool enable)
722  {return keepalive(so, enable);};
723 
729  inline int blocking(bool enable)
730  {return blocking(so, enable);};
731 
737  inline int multicast(unsigned ttl = 1)
738  {return multicast(so, ttl);};
739 
745  inline int loopback(bool enable)
746  {return loopback(so, enable);};
747 
752  inline int getError(void)
753  {return error(so);};
754 
760  inline int ttl(unsigned char time)
761  {return ttl(so, time);};
762 
768  inline int sendsize(unsigned size)
769  {return sendsize(so, size);};
770 
776  inline int sendwait(unsigned size)
777  {return sendwait(so, size);};
778 
779 
785  inline int recvsize(unsigned size)
786  {return recvsize(so, size);};
787 
793  static int gettype(socket_t socket);
794 
801  static unsigned segsize(socket_t socket, unsigned size = 0);
802 
809  static bool setccid(socket_t socket, uint8_t ccid);
810 
815  inline int gettype(void)
816  {return gettype(so);};
817 
823  inline unsigned segsize(unsigned size)
824  {return segsize(so, size);};
825 
831  inline bool setccid(uint8_t ccid)
832  {return setccid(so, ccid);};
833 
842  inline int tos(int type)
843  {return tos(so, type);};
844 
851  inline int priority(int scheduling)
852  {return priority(so, scheduling);};
853 
857  inline void shutdown(void)
858  {::shutdown(so, SHUT_RDWR);};
859 
867  int connectto(struct addrinfo *list);
868 
875  int disconnect(void);
876 
882  int join(struct addrinfo *list);
883 
889  int drop(struct addrinfo *list);
890 
896  int wait(timeout_t timeout = Timer::inf);
897 
904  size_t peek(void *data, size_t number) const;
905 
913  size_t readfrom(void *data, size_t number, struct sockaddr_storage *address = NULL);
914 
922  size_t writeto(const void *data, size_t number, struct sockaddr *address = NULL);
923 
936  size_t readline(char *data, size_t size);
937 
943  size_t printf(const char *format, ...) __PRINTF(2,3);
944 
956  size_t readline(string& buffer);
957 
969  static ssize_t readline(socket_t socket, char *data, size_t size, timeout_t timeout = Timer::inf);
970 
977  static ssize_t printf(socket_t socket, const char *format, ...) __PRINTF(2,3);
978 
986  size_t writes(const char *string);
987 
992  operator bool();
993 
998  bool operator!() const;
999 
1005  Socket& operator=(socket_t socket);
1006 
1011  inline operator socket_t() const
1012  {return so;};
1013 
1018  inline socket_t operator*() const
1019  {return so;};
1020 
1027  static unsigned pending(socket_t socket);
1028 
1035  static int sendsize(socket_t socket, unsigned size);
1036 
1043  static int sendwait(socket_t socket, unsigned size);
1044 
1051  static int recvsize(socket_t socket, unsigned size);
1052 
1061  static int connectto(socket_t socket, struct addrinfo *list);
1062 
1068  static int disconnect(socket_t socket);
1069 
1076  static int drop(socket_t socket, struct addrinfo *list);
1077 
1084  static int join(socket_t socket, struct addrinfo *list);
1085 
1091  static int error(socket_t socket);
1092 
1099  static int multicast(socket_t socket, unsigned ttl = 1);
1100 
1107  static int loopback(socket_t socket, bool enable);
1108 
1115  static int blocking(socket_t socket, bool enable);
1116 
1123  static int keepalive(socket_t socket, bool enable);
1124 
1131  static int broadcast(socket_t socket, bool enable);
1132 
1138  static int nodelay(socket_t socket);
1139 
1146  static int priority(socket_t socket, int scheduling);
1147 
1154  static int tos(socket_t socket, int type);
1155 
1162  static int ttl(socket_t socket, unsigned char time);
1163 
1168  static int getfamily(socket_t socket);
1169 
1175  inline static int getfamily(struct sockaddr_storage& address)
1176  {return ((struct sockaddr *)&address)->sa_family;};
1177 
1183  inline static int getfamily(struct sockaddr_internet& address)
1184  {return address.address.sa_family;};
1185 
1195  static ssize_t recvfrom(socket_t socket, void *buffer, size_t size, int flags = 0, struct sockaddr_storage *address = NULL);
1196 
1206  static ssize_t sendto(socket_t socket, const void *buffer, size_t size, int flags = 0, struct sockaddr *address = NULL);
1207 
1217  inline static ssize_t replyto(socket_t socket, const void *buffer, size_t size, int flags, struct sockaddr_storage *address)
1218  {return sendto(socket, buffer, size, flags, (struct sockaddr *)address);};
1219 
1229  inline static ssize_t sendinet(socket_t socket, const void *buffer, size_t size, int flags, struct sockaddr_internet *address)
1230  {return sendto(socket, buffer, size, flags, (struct sockaddr *)address);};
1231 
1241  static ssize_t recvinet(socket_t socket, void *buffer, size_t size, int flags = 0, struct sockaddr_internet *address = NULL);
1242 
1251  static int bindto(socket_t socket, const char *address, const char *service, int protocol = 0);
1252 
1260  static int listento(socket_t socket, struct sockaddr *address, int backlog = 5);
1261 
1268  static int bindto(socket_t socket, struct sockaddr *address);
1269 
1276  static socket_t acceptfrom(socket_t socket, struct sockaddr_storage *address = NULL);
1277 
1285  static socket_t create(int family, int type, int protocol);
1286 
1294  static socket_t create(struct addrinfo *address, int type, int protocol);
1295 
1305  static socket_t create(const char *iface, const char *service, int family = AF_UNSPEC, int type = 0, int protocol = 0);
1306 
1312  static socket_t create(Socket::address &address);
1313 
1318  static void release(socket_t socket);
1319 
1328  inline static size_t writeto(Socket& socket, const char *buffer, size_t size, struct sockaddr *address)
1329  {return socket.writeto(buffer, size, address);};
1330 
1339  inline static size_t readfrom(Socket& socket, char *buffer, size_t size, struct sockaddr_storage *address)
1340  {return socket.readfrom(buffer, size, address);};
1341 
1347  inline static void connectto(Socket& socket, Socket::address &address)
1348  {socket.connectto(address);};
1349 
1354  inline static void disconnect(Socket& socket)
1355  {socket.disconnect();};
1356 
1363  inline static Socket acceptfrom(Socket& socket, struct sockaddr_storage *address)
1364  {return Socket(acceptfrom(socket.so, address));};
1365 
1373  static char *gethostname(struct sockaddr *address, char *buffer, size_t size);
1374 
1382  static struct addrinfo *gethint(socket_t socket, struct addrinfo *hint);
1383 
1394  static socklen_t getaddr(socket_t socket, struct sockaddr_storage *address, const char *hostname, const char *service);
1395 
1401  static socklen_t getlen(struct sockaddr *address);
1402 
1410  static bool equal(struct sockaddr *address1, struct sockaddr *address2);
1411 
1418  static unsigned copy(struct sockaddr *target, struct sockaddr *origin);
1419 
1426  inline static unsigned store(struct sockaddr_storage *storage, struct sockaddr *address)
1427  {return copy((struct sockaddr*)storage, address);};
1428 
1435  static unsigned store(struct sockaddr_internet *storage, struct sockaddr *address);
1436 
1444  static bool equalhost(struct sockaddr *address1, struct sockaddr *address2);
1445 
1453  inline static bool equalfrom(struct sockaddr_storage *address1, struct sockaddr_storage *address2)
1454  {return equal((struct sockaddr *)address1, (struct sockaddr *)address2);};
1455 
1463  inline static bool equalinet(struct sockaddr_internet *address1, struct sockaddr_internet *address2)
1464  {return equal((struct sockaddr *)address1, (struct sockaddr *)address2);};
1465 
1473  static bool subnet(struct sockaddr *address1, struct sockaddr *address2);
1474 
1482  static int getinterface(struct sockaddr *address, struct sockaddr *destination);
1483 
1491  static char *getaddress(struct sockaddr *address, char *buffer, socklen_t size);
1492 
1498  static short getservice(struct sockaddr *address);
1499 
1505  inline static short inetservice(struct sockaddr_internet *address)
1506  {return getservice((struct sockaddr *)address);};
1507 
1514  static unsigned keyindex(struct sockaddr *address, unsigned size);
1515 
1522  static unsigned keyhost(struct sockaddr *address, unsigned size);
1523 
1527  static void init(void);
1528 
1533  static void init(const char *program);
1534 
1540  static void family(int query);
1541 
1548  static void v4mapping(bool enable);
1549 
1554  static int error(void);
1555 
1564  static bool isNull(const char *string);
1565 
1573  static bool isNumeric(const char *string);
1574 
1583  static int getlocal(socket_t socket, struct sockaddr_storage *address);
1584 
1593  static int getremote(socket_t socket, struct sockaddr_storage *address);
1594 
1602  static int select(int max, set_t read, set_t write, set_t error);
1603 
1612  static int select(int max, set_t read, set_t write, set_t error, timeout_t timeout);
1613 
1618  static set_t getmask(void);
1619 
1624  static void clear(set_t mask);
1625 
1630  static void release(set_t mask);
1631 
1637  static void set(socket_t socket, set_t mask);
1638 
1644  static void clear(socket_t socket, set_t mask);
1645 
1652  static bool test(socket_t socket, set_t mask);
1653 };
1654 
1660 class __EXPORT ListenSocket : protected Socket
1661 {
1662 public:
1672  ListenSocket(const char *address, const char *service, unsigned backlog = 5, int family = AF_UNSPEC, int type = 0, int protocol = 0);
1673 
1684  static socket_t create(const char *address, const char *service, unsigned backlog = 5, int family = AF_UNSPEC, int type = 0, int protocol = 0);
1685 
1691  socket_t accept(struct sockaddr_storage *address = NULL) const;
1692 
1698  inline bool waitConnection(timeout_t timeout = Timer::inf) const
1699  {return Socket::waitPending(timeout);};
1700 
1705  inline operator socket_t() const
1706  {return so;};
1707 
1712  inline socket_t operator*() const
1713  {return so;};
1714 
1719  inline socket_t getsocket(void) const
1720  {return so;};
1721 
1722 };
1723 
1729 class __EXPORT TCPServer : public ListenSocket
1730 {
1731 public:
1739  TCPServer(const char *address, const char *service, unsigned backlog = 5);
1740 };
1741 
1745 __EXPORT struct addrinfo *_nextaddrinfo(struct addrinfo *addrinfo);
1746 
1750 __EXPORT struct sockaddr *_getaddrinfo(struct addrinfo *addrinfo);
1751 
1755 __EXPORT socket_t _getaddrsock(struct addrinfo *addrinfo);
1756 
1762 template <>
1763 class linked_pointer<struct sockaddr>
1764 {
1765 private:
1766  struct addrinfo *ptr;
1767 
1768 public:
1769  inline linked_pointer(struct addrinfo *list)
1770  {ptr = list;}
1771 
1772  inline linked_pointer()
1773  {ptr = NULL;}
1774 
1775  inline linked_pointer(Socket::address& list)
1776  {ptr = list.getList();};
1777 
1782  inline operator struct sockaddr *() const
1783  {return _getaddrinfo(ptr);};
1784 
1789  inline struct sockaddr *operator*() const
1790  {return _getaddrinfo(ptr);};
1791 
1792  inline operator struct sockaddr_in *() const
1793  {return (struct sockaddr_in *)_getaddrinfo(ptr);};
1794 
1795  inline struct sockaddr_in *in(void) const
1796  {return (struct sockaddr_in *)_getaddrinfo(ptr);};
1797 
1798 #ifdef AF_INET6
1799  inline operator struct sockaddr_in6 *() const
1800  {return (struct sockaddr_in6 *)_getaddrinfo(ptr);};
1801 
1802  inline struct sockaddr_in6 *in6(void) const
1803  {return (struct sockaddr_in6 *)_getaddrinfo(ptr);};
1804 #endif
1805 
1809  inline socket_t operator()(void) const
1810  {return _getaddrsock(ptr);};
1811 
1816  inline operator bool() const
1817  {return ptr != NULL;};
1818 
1823  inline void operator=(struct addrinfo *list)
1824  {ptr = list;};
1825 
1830  inline void operator=(Socket::address& list)
1831  {ptr = list.getList();};
1832 
1837  inline void set(struct addrinfo *list)
1838  {ptr = list;};
1839 
1844  inline void set(Socket::address& list)
1845  {ptr = list.getList();};
1846 
1847 
1852  inline struct sockaddr* operator->() const
1853  {return _getaddrinfo(ptr);};
1854 
1859  inline bool operator!() const
1860  {return ptr == NULL;};
1861 
1862  inline void next(void)
1863  {ptr = _nextaddrinfo(ptr);};
1864 };
1865 
1869 typedef Socket socket;
1870 
1876 inline struct addrinfo *addrinfo(socket::address& address)
1877  {return address.getList();}
1878 
1885 inline struct sockaddr *addr(socket::address& address)
1886  {return address.getAddr();}
1887 
1895 inline bool eq(struct sockaddr *s1, struct sockaddr *s2)
1896  {return Socket::equal(s1, s2);}
1897 
1905 inline bool eq(struct sockaddr_storage *s1, struct sockaddr_storage *s2)
1906  {return Socket::equal((struct sockaddr *)s1, (struct sockaddr *)s2);}
1907 
1915 inline bool host_eq(struct sockaddr *s1, struct sockaddr *s2)
1916  {return Socket::equalhost(s1, s2);}
1917 
1918 // to be depreciated...
1919 inline bool ieq(struct sockaddr *s1, struct sockaddr *s2)
1920  {return Socket::equalhost(s1, s2);}
1921 
1922 String str(Socket& so, strsize_t size);
1923 
1924 typedef TCPServer tcpserv_t;
1925 
1926 
1927 END_NAMESPACE
1928 
1929 #endif