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 
77 typedef struct sockaddr sockaddr_struct; // older gcc needs...?
78 
82 typedef struct hostaddr_internet
83 {
84  union
85  {
86  struct in_addr ipv4;
87 #ifdef AF_INET6
88  struct in6_addr ipv6;
89 #endif
90  };
92 
93 #if defined(AF_INET6) || defined(__CYGWIN__)
94 
101 typedef struct sockaddr_internet
102 {
103  union {
104 #ifdef AF_INET6
105  struct sockaddr_in6 ipv6;
106 #endif
107  struct sockaddr_in ipv4;
108  struct sockaddr address;
109  };
111 #else
112 typedef struct sockaddr_internet
113 {
114  union {
115  struct sockaddr_in ipv4;
116  struct sockaddr address;
117  };
119 
120 struct sockaddr_storage
121 {
122 #ifdef AF_UNIX
123  char sa_data[128];
124 #else
125  char sa_data[sizeof(struct sockaddr_in)];
126 #endif
127 };
128 #endif
129 
130 #ifndef SOCK_DCCP
131 #define SOCK_DCCP 6
132 #endif
133 
134 #ifndef IPPROTO_DCCP
135 #define IPPROTO_DCCP 23
136 #endif
137 
138 #ifndef SOL_DCCP
139 #define SOL_DCCP 269
140 #endif
141 
142 #define DCCP_SOCKOPT_AVAILABLE_CCIDS 12
143 #define DCCP_SOCKOPT_CCID 13
144 #define DCCP_SOCKOPT_TX_CCID 14
145 #define DCCP_SOCKOPT_RX_CCID 15
146 
147 NAMESPACE_UCOMMON
148 
158 class __EXPORT cidr : public LinkedObject
159 {
160 protected:
161  int family;
162  inethostaddr_t netmask, network;
163  char name[16];
164  unsigned getMask(const char *cp) const;
165 
166 public:
171 
175  cidr();
176 
183  cidr(const char *string);
184 
190  cidr(policy **policy, const char *string);
191 
198  cidr(policy **policy, const char *string, const char *name);
199 
204  cidr(const cidr& existing);
205 
212  static cidr *find(policy *policy, const struct sockaddr *address);
213 
221  static cidr *container(policy *policy, const struct sockaddr *address);
222 
230  inline const char *getName(void) const
231  {return name;};
232 
237  inline int getFamily(void) const
238  {return family;};
239 
244  inline inethostaddr_t getNetwork(void) const
245  {return network;};
246 
251  inline inethostaddr_t getNetmask(void) const
252  {return netmask;};
253 
258  inethostaddr_t getBroadcast(void) const;
259 
264  unsigned getMask(void) const;
265 
270  void set(const char *string);
271 
277  bool isMember(const struct sockaddr *address) const;
278 
284  inline bool operator==(const struct sockaddr *address) const
285  {return isMember(address);};
286 
292  inline bool operator!=(const struct sockaddr *address) const
293  {return !isMember(address);};
294 };
295 
303 class __EXPORT Socket
304 {
305 protected:
306  socket_t so;
307  int ioerr;
308  timeout_t iowait;
309 
310 public:
319  static struct addrinfo *getaddress(const char *host, const char *service, int type = SOCK_STREAM, int protocol = 0);
320 
326  static void release(struct addrinfo *list);
327 
333  typedef void *set_t;
334 
335  static const size_t masksize;
336 
345  class __EXPORT address
346  {
347  protected:
348  struct addrinfo *list;
349 
350  public:
361  address(int family, const char *address, int type = SOCK_STREAM, int protocol = 0);
362 
375  address(int family, const char *hostname, const char *service = NULL);
376 
383  address(const char *host, const char *service, int type = SOCK_STREAM);
384 
392  address(const char *hostname, unsigned service = 0);
393 
397  address();
398 
403  address(const address& reference);
404 
408  ~address();
409 
414  struct sockaddr *getAddr(void) const;
415 
421  struct sockaddr *get(int family) const;
422 
427  int getfamily(void) const;
428 
433  struct sockaddr *find(struct sockaddr *addr) const;
434 
439  inline struct addrinfo *getList(void) const
440  {return list;};
441 
446  inline operator struct addrinfo *() const
447  {return list;};
448 
453  inline struct addrinfo *operator*() const
454  {return list;};
455 
460  inline operator bool() const
461  {return list != NULL;};
462 
467  inline bool operator!() const
468  {return list == NULL;};
469 
474  inline operator struct sockaddr *() const
475  {return getAddr();};
476 
480  void clear(void);
481 
488  void set(const char *hostname, const char *service = NULL, int type = SOCK_STREAM);
489 
496  void add(const char *hostname, const char *service = NULL, int type = SOCK_STREAM);
497 
505  void set(int family, const char *address, int type = SOCK_STREAM, int protocol = 0);
506 
511  void add(sockaddr *address);
512 
518  unsigned insert(struct addrinfo *address);
519 
525  unsigned remove(struct addrinfo *address);
526 
532  bool remove(struct sockaddr *address);
533 
540  bool insert(struct sockaddr *address);
541 
547  void copy(const struct addrinfo *address);
548 
553  void set(struct sockaddr *address);
554 
560  void set(const char *hostname, unsigned service = 0);
561 
567  static struct sockaddr *dup(struct sockaddr *address);
568 
574  static struct sockaddr_in *ipv4(struct sockaddr *address);
575 
576 #ifdef AF_INET6
577 
582  static struct sockaddr_in6 *ipv6(struct sockaddr *address);
583 #endif
584  };
585 
586  friend class address;
587 
591  Socket();
592 
597  Socket(const Socket& existing);
598 
603  Socket(socket_t socket);
604 
610  Socket(struct addrinfo *address);
611 
618  Socket(int family, int type, int protocol = 0);
619 
629  Socket(const char *address, const char *port, int family = AF_UNSPEC, int type = 0, int protocol = 0);
630 
634  virtual ~Socket();
635 
639  void cancel(void);
640 
645  static void cancel(socket_t socket);
646 
650  void release(void);
651 
655  inline int err(void) const
656  {return ioerr;}
657 
663  bool isPending(unsigned value) const;
664 
669  bool isConnected(void) const;
670 
677  bool waitPending(timeout_t timeout = 0) const;
678 
683  inline int nodelay(void) const
684  {return nodelay(so);};
685 
693  static bool wait(socket_t socket, timeout_t timeout = 0);
694 
701  bool waitSending(timeout_t timeout = 0) const;
702 
707  inline unsigned getPending(void) const
708  {return pending(so);};
709 
715  inline int broadcast(bool enable)
716  {return broadcast(so, enable);};
717 
723  inline int keepalive(bool enable)
724  {return keepalive(so, enable);};
725 
731  inline int blocking(bool enable)
732  {return blocking(so, enable);};
733 
739  inline int multicast(unsigned ttl = 1)
740  {return multicast(so, ttl);};
741 
747  inline int loopback(bool enable)
748  {return loopback(so, enable);};
749 
754  inline int getError(void)
755  {return error(so);};
756 
762  inline int ttl(unsigned char time)
763  {return ttl(so, time);};
764 
770  inline int sendsize(unsigned size)
771  {return sendsize(so, size);};
772 
778  inline int sendwait(unsigned size)
779  {return sendwait(so, size);};
780 
781 
787  inline int recvsize(unsigned size)
788  {return recvsize(so, size);};
789 
795  static int gettype(socket_t socket);
796 
803  static unsigned segsize(socket_t socket, unsigned size = 0);
804 
811  static bool setccid(socket_t socket, uint8_t ccid);
812 
817  inline int gettype(void)
818  {return gettype(so);};
819 
825  inline unsigned segsize(unsigned size)
826  {return segsize(so, size);};
827 
833  inline bool setccid(uint8_t ccid)
834  {return setccid(so, ccid);};
835 
844  inline int tos(int type)
845  {return tos(so, type);};
846 
853  inline int priority(int scheduling)
854  {return priority(so, scheduling);};
855 
859  inline void shutdown(void)
860  {::shutdown(so, SHUT_RDWR);};
861 
869  int connectto(struct addrinfo *list);
870 
877  int disconnect(void);
878 
884  int join(struct addrinfo *list);
885 
891  int drop(struct addrinfo *list);
892 
898  int wait(timeout_t timeout = Timer::inf);
899 
906  size_t peek(void *data, size_t number) const;
907 
915  size_t readfrom(void *data, size_t number, struct sockaddr_storage *address = NULL);
916 
924  size_t writeto(const void *data, size_t number, struct sockaddr *address = NULL);
925 
938  size_t readline(char *data, size_t size);
939 
945  size_t printf(const char *format, ...) __PRINTF(2,3);
946 
958  size_t readline(string& buffer);
959 
971  static ssize_t readline(socket_t socket, char *data, size_t size, timeout_t timeout = Timer::inf);
972 
979  static ssize_t printf(socket_t socket, const char *format, ...) __PRINTF(2,3);
980 
988  size_t writes(const char *string);
989 
994  operator bool();
995 
1000  bool operator!() const;
1001 
1007  Socket& operator=(socket_t socket);
1008 
1013  inline operator socket_t() const
1014  {return so;};
1015 
1020  inline socket_t operator*() const
1021  {return so;};
1022 
1029  static unsigned pending(socket_t socket);
1030 
1037  static int sendsize(socket_t socket, unsigned size);
1038 
1045  static int sendwait(socket_t socket, unsigned size);
1046 
1053  static int recvsize(socket_t socket, unsigned size);
1054 
1063  static int connectto(socket_t socket, struct addrinfo *list);
1064 
1070  static int disconnect(socket_t socket);
1071 
1078  static int drop(socket_t socket, struct addrinfo *list);
1079 
1086  static int join(socket_t socket, struct addrinfo *list);
1087 
1093  static int error(socket_t socket);
1094 
1101  static int multicast(socket_t socket, unsigned ttl = 1);
1102 
1109  static int loopback(socket_t socket, bool enable);
1110 
1117  static int blocking(socket_t socket, bool enable);
1118 
1125  static int keepalive(socket_t socket, bool enable);
1126 
1133  static int broadcast(socket_t socket, bool enable);
1134 
1140  static int nodelay(socket_t socket);
1141 
1148  static int priority(socket_t socket, int scheduling);
1149 
1156  static int tos(socket_t socket, int type);
1157 
1164  static int ttl(socket_t socket, unsigned char time);
1165 
1170  static int getfamily(socket_t socket);
1171 
1177  inline static int getfamily(struct sockaddr_storage& address)
1178  {return ((struct sockaddr *)&address)->sa_family;};
1179 
1185  inline static int getfamily(struct sockaddr_internet& address)
1186  {return address.address.sa_family;};
1187 
1197  static ssize_t recvfrom(socket_t socket, void *buffer, size_t size, int flags = 0, struct sockaddr_storage *address = NULL);
1198 
1208  static ssize_t sendto(socket_t socket, const void *buffer, size_t size, int flags = 0, struct sockaddr *address = NULL);
1209 
1219  inline static ssize_t replyto(socket_t socket, const void *buffer, size_t size, int flags, struct sockaddr_storage *address)
1220  {return sendto(socket, buffer, size, flags, (struct sockaddr *)address);};
1221 
1231  inline static ssize_t sendinet(socket_t socket, const void *buffer, size_t size, int flags, struct sockaddr_internet *address)
1232  {return sendto(socket, buffer, size, flags, (struct sockaddr *)address);};
1233 
1243  static ssize_t recvinet(socket_t socket, void *buffer, size_t size, int flags = 0, struct sockaddr_internet *address = NULL);
1244 
1253  static int bindto(socket_t socket, const char *address, const char *service, int protocol = 0);
1254 
1262  static int listento(socket_t socket, struct sockaddr *address, int backlog = 5);
1263 
1270  static int bindto(socket_t socket, struct sockaddr *address);
1271 
1278  static socket_t acceptfrom(socket_t socket, struct sockaddr_storage *address = NULL);
1279 
1287  static socket_t create(int family, int type, int protocol);
1288 
1296  static socket_t create(struct addrinfo *address, int type, int protocol);
1297 
1307  static socket_t create(const char *iface, const char *service, int family = AF_UNSPEC, int type = 0, int protocol = 0);
1308 
1314  static socket_t create(Socket::address &address);
1315 
1320  static void release(socket_t socket);
1321 
1330  inline static size_t writeto(Socket& socket, const char *buffer, size_t size, struct sockaddr *address)
1331  {return socket.writeto(buffer, size, address);};
1332 
1341  inline static size_t readfrom(Socket& socket, char *buffer, size_t size, struct sockaddr_storage *address)
1342  {return socket.readfrom(buffer, size, address);};
1343 
1349  inline static void connectto(Socket& socket, Socket::address &address)
1350  {socket.connectto(address);};
1351 
1356  inline static void disconnect(Socket& socket)
1357  {socket.disconnect();};
1358 
1365  inline static Socket acceptfrom(Socket& socket, struct sockaddr_storage *address)
1366  {return Socket(acceptfrom(socket.so, address));};
1367 
1375  static char *gethostname(struct sockaddr *address, char *buffer, size_t size);
1376 
1384  static struct addrinfo *gethint(socket_t socket, struct addrinfo *hint);
1385 
1396  static socklen_t getaddr(socket_t socket, struct sockaddr_storage *address, const char *hostname, const char *service);
1397 
1403  static socklen_t getlen(struct sockaddr *address);
1404 
1412  static bool equal(struct sockaddr *address1, struct sockaddr *address2);
1413 
1420  static unsigned copy(struct sockaddr *target, struct sockaddr *origin);
1421 
1428  inline static unsigned store(struct sockaddr_storage *storage, struct sockaddr *address)
1429  {return copy((struct sockaddr*)storage, address);};
1430 
1437  static unsigned store(struct sockaddr_internet *storage, struct sockaddr *address);
1438 
1446  static bool equalhost(struct sockaddr *address1, struct sockaddr *address2);
1447 
1455  inline static bool equalfrom(struct sockaddr_storage *address1, struct sockaddr_storage *address2)
1456  {return equal((struct sockaddr *)address1, (struct sockaddr *)address2);};
1457 
1465  inline static bool equalinet(struct sockaddr_internet *address1, struct sockaddr_internet *address2)
1466  {return equal((struct sockaddr *)address1, (struct sockaddr *)address2);};
1467 
1475  static bool subnet(struct sockaddr *address1, struct sockaddr *address2);
1476 
1484  static int getinterface(struct sockaddr *address, struct sockaddr *destination);
1485 
1493  static char *getaddress(struct sockaddr *address, char *buffer, socklen_t size);
1494 
1500  static short getservice(struct sockaddr *address);
1501 
1507  inline static short inetservice(struct sockaddr_internet *address)
1508  {return getservice((struct sockaddr *)address);};
1509 
1516  static unsigned keyindex(struct sockaddr *address, unsigned size);
1517 
1524  static unsigned keyhost(struct sockaddr *address, unsigned size);
1525 
1529  static void init(void);
1530 
1535  static void init(const char *program);
1536 
1542  static void family(int query);
1543 
1550  static void v4mapping(bool enable);
1551 
1556  static int error(void);
1557 
1566  static bool isNull(const char *string);
1567 
1575  static bool isNumeric(const char *string);
1576 
1585  static int getlocal(socket_t socket, struct sockaddr_storage *address);
1586 
1595  static int getremote(socket_t socket, struct sockaddr_storage *address);
1596 
1604  static int select(int max, set_t read, set_t write, set_t error);
1605 
1614  static int select(int max, set_t read, set_t write, set_t error, timeout_t timeout);
1615 
1620  static set_t getmask(void);
1621 
1626  static void clear(set_t mask);
1627 
1632  static void release(set_t mask);
1633 
1639  static void set(socket_t socket, set_t mask);
1640 
1646  static void clear(socket_t socket, set_t mask);
1647 
1654  static bool test(socket_t socket, set_t mask);
1655 };
1656 
1662 class __EXPORT ListenSocket : protected Socket
1663 {
1664 public:
1674  ListenSocket(const char *address, const char *service, unsigned backlog = 5, int family = AF_UNSPEC, int type = 0, int protocol = 0);
1675 
1686  static socket_t create(const char *address, const char *service, unsigned backlog = 5, int family = AF_UNSPEC, int type = 0, int protocol = 0);
1687 
1693  socket_t accept(struct sockaddr_storage *address = NULL) const;
1694 
1700  inline bool waitConnection(timeout_t timeout = Timer::inf) const
1701  {return Socket::waitPending(timeout);};
1702 
1707  inline operator socket_t() const
1708  {return so;};
1709 
1714  inline socket_t operator*() const
1715  {return so;};
1716 
1721  inline socket_t getsocket(void) const
1722  {return so;};
1723 
1724 };
1725 
1731 class __EXPORT TCPServer : public ListenSocket
1732 {
1733 public:
1741  TCPServer(const char *address, const char *service, unsigned backlog = 5);
1742 };
1743 
1747 __EXPORT struct addrinfo *_nextaddrinfo(struct addrinfo *addrinfo);
1748 
1752 __EXPORT struct sockaddr *_getaddrinfo(struct addrinfo *addrinfo);
1753 
1757 __EXPORT socket_t _getaddrsock(struct addrinfo *addrinfo);
1758 
1764 template <>
1765 class linked_pointer<sockaddr_struct>
1766 {
1767 private:
1768  struct addrinfo *ptr;
1769 
1770 public:
1771  inline linked_pointer(struct addrinfo *list)
1772  {ptr = list;}
1773 
1774  inline linked_pointer()
1775  {ptr = NULL;}
1776 
1777  inline linked_pointer(Socket::address& list)
1778  {ptr = list.getList();};
1779 
1784  inline operator struct sockaddr *() const
1785  {return _getaddrinfo(ptr);};
1786 
1791  inline struct sockaddr *operator*() const
1792  {return _getaddrinfo(ptr);};
1793 
1794  inline operator struct sockaddr_in *() const
1795  {return (struct sockaddr_in *)_getaddrinfo(ptr);};
1796 
1797  inline struct sockaddr_in *in(void) const
1798  {return (struct sockaddr_in *)_getaddrinfo(ptr);};
1799 
1800 #ifdef AF_INET6
1801  inline operator struct sockaddr_in6 *() const
1802  {return (struct sockaddr_in6 *)_getaddrinfo(ptr);};
1803 
1804  inline struct sockaddr_in6 *in6(void) const
1805  {return (struct sockaddr_in6 *)_getaddrinfo(ptr);};
1806 #endif
1807 
1811  inline socket_t operator()(void) const
1812  {return _getaddrsock(ptr);};
1813 
1818  inline operator bool() const
1819  {return ptr != NULL;};
1820 
1825  inline void operator=(struct addrinfo *list)
1826  {ptr = list;};
1827 
1832  inline void operator=(Socket::address& list)
1833  {ptr = list.getList();};
1834 
1839  inline void set(struct addrinfo *list)
1840  {ptr = list;};
1841 
1846  inline void set(Socket::address& list)
1847  {ptr = list.getList();};
1848 
1849 
1854  inline struct sockaddr* operator->() const
1855  {return _getaddrinfo(ptr);};
1856 
1861  inline bool operator!() const
1862  {return ptr == NULL;};
1863 
1864  inline void next(void)
1865  {ptr = _nextaddrinfo(ptr);};
1866 };
1867 
1871 typedef Socket socket;
1872 
1878 inline struct addrinfo *addrinfo(socket::address& address)
1879  {return address.getList();}
1880 
1887 inline struct sockaddr *addr(socket::address& address)
1888  {return address.getAddr();}
1889 
1897 inline bool eq(struct sockaddr *s1, struct sockaddr *s2)
1898  {return Socket::equal(s1, s2);}
1899 
1907 inline bool eq(struct sockaddr_storage *s1, struct sockaddr_storage *s2)
1908  {return Socket::equal((struct sockaddr *)s1, (struct sockaddr *)s2);}
1909 
1917 inline bool host_eq(struct sockaddr *s1, struct sockaddr *s2)
1918  {return Socket::equalhost(s1, s2);}
1919 
1920 // to be depreciated...
1921 inline bool ieq(struct sockaddr *s1, struct sockaddr *s2)
1922  {return Socket::equalhost(s1, s2);}
1923 
1924 String str(Socket& so, strsize_t size);
1925 
1926 typedef TCPServer tcpserv_t;
1927 
1928 
1929 END_NAMESPACE
1930 
1931 #endif