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 
165  unsigned mask(const char *cp) const;
166 
167  inethostaddr_t broadcast(void) const;
168 
169  unsigned mask(void) const;
170 
171 public:
176 
180  cidr();
181 
188  cidr(const char *string);
189 
195  cidr(policy **policy, const char *string);
196 
203  cidr(policy **policy, const char *string, const char *name);
204 
209  cidr(const cidr& existing);
210 
217  static const cidr *find(const policy *policy, const struct sockaddr *address);
218 
226  static const cidr *container(const policy *policy, const struct sockaddr *address);
227 
235  inline const char *getName(void) const
236  {return Name;};
237 
242  inline int getFamily(void) const
243  {return Family;};
244 
249  inline inethostaddr_t getNetwork(void) const
250  {return Network;};
251 
256  inline inethostaddr_t getNetmask(void) const
257  {return Netmask;};
258 
263  inline inethostaddr_t getBroadcast(void) const
264  {return broadcast();}
265 
270  inline unsigned getMask(void) const
271  {return mask();}
272 
277  void set(const char *string);
278 
284  bool is_member(const struct sockaddr *address) const;
285 
291  inline bool operator==(const struct sockaddr *address) const
292  {return is_member(address);};
293 
299  inline bool operator!=(const struct sockaddr *address) const
300  {return !is_member(address);};
301 };
302 
310 class __EXPORT Socket
311 {
312 protected:
313  socket_t so;
314  int ioerr;
315  timeout_t iowait;
316 
317 public:
326  static struct addrinfo *query(const char *host, const char *service, int type = SOCK_STREAM, int protocol = 0);
327 
333  static void release(struct addrinfo *list);
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 *get(void) const;
413 
414  inline struct sockaddr *getAddr(void) const
415  {return get();}
416 
417  inline struct sockaddr *operator()(void) const
418  {return get();}
419 
424  inline operator struct sockaddr *() const
425  {return get();};
426 
432  struct sockaddr *get(int family) const;
433 
434  inline struct sockaddr *operator()(int family) const
435  {return get(family);}
436 
437  inline operator struct sockaddr_in *() const
438  {return (struct sockaddr_in *)get(AF_INET);}
439 
440 #ifdef AF_INET6
441  inline operator struct sockaddr_in6 *() const
442  {return (struct sockaddr_in6 *)get(AF_INET6);}
443 #endif
444 
449  int family(void) const;
450 
455  struct sockaddr *find(const struct sockaddr *addr) const;
456 
461  inline struct addrinfo *getList(void) const
462  {return list;};
463 
468  inline operator struct addrinfo *() const
469  {return list;};
470 
475  inline struct addrinfo *operator*() const
476  {return list;};
477 
482  inline operator bool() const
483  {return list != NULL;};
484 
489  inline bool operator!() const
490  {return list == NULL;};
491 
492 
496  void clear(void);
497 
504  void set(const char *hostname, const char *service = NULL, int type = SOCK_STREAM);
505 
512  void add(const char *hostname, const char *service = NULL, int type = SOCK_STREAM);
513 
521  void set(int family, const char *address, int type = SOCK_STREAM, int protocol = 0);
522 
527  void add(sockaddr *address);
528 
534  unsigned insert(struct addrinfo *address);
535 
541  unsigned remove(struct addrinfo *address);
542 
548  bool remove(struct sockaddr *address);
549 
556  bool insert(struct sockaddr *address);
557 
563  void copy(const struct addrinfo *address);
564 
569  void set(struct sockaddr *address);
570 
576  void set(const char *hostname, unsigned service = 0);
577 
583  static struct sockaddr *dup(struct sockaddr *address);
584 
590  static struct sockaddr_in *ipv4(struct sockaddr *address);
591 
592 #ifdef AF_INET6
593 
598  static struct sockaddr_in6 *ipv6(struct sockaddr *address);
599 #endif
600  };
601 
602  friend class address;
603 
607  Socket();
608 
613  Socket(const Socket& existing);
614 
619  Socket(socket_t socket);
620 
626  Socket(struct addrinfo *address);
627 
634  Socket(int family, int type, int protocol = 0);
635 
645  Socket(const char *address, const char *port, int family = AF_UNSPEC, int type = 0, int protocol = 0);
646 
650  virtual ~Socket();
651 
655  void cancel(void);
656 
661  static void cancel(socket_t socket);
662 
666  void release(void);
667 
671  inline int err(void) const
672  {return ioerr;}
673 
679  bool is_pending(unsigned value) const;
680 
685  bool connected(void) const;
686 
693  bool wait(timeout_t timeout = 0) const;
694 
699  inline int nodelay(void) const
700  {return nodelay(so);};
701 
709  static bool wait(socket_t socket, timeout_t timeout = 0);
710 
717  bool waitSending(timeout_t timeout = 0) const;
718 
723  inline unsigned pending(void) const
724  {return pending(so);};
725 
731  inline int broadcast(bool enable)
732  {return broadcast(so, enable);};
733 
739  inline int keepalive(bool enable)
740  {return keepalive(so, enable);};
741 
747  inline int blocking(bool enable)
748  {return blocking(so, enable);};
749 
755  inline int multicast(unsigned ttl = 1)
756  {return multicast(so, ttl);};
757 
763  inline int loopback(bool enable)
764  {return loopback(so, enable);};
765 
770  inline int getError(void)
771  {return error(so);};
772 
778  inline int ttl(unsigned char time)
779  {return ttl(so, time);};
780 
786  inline int sendsize(unsigned size)
787  {return sendsize(so, size);};
788 
794  inline int sendwait(unsigned size)
795  {return sendwait(so, size);};
796 
797 
803  inline int recvsize(unsigned size)
804  {return recvsize(so, size);};
805 
811  static int type(socket_t socket);
812 
819  static unsigned segsize(socket_t socket, unsigned size = 0);
820 
827  static bool ccid(socket_t socket, uint8_t id);
828 
833  inline int type(void)
834  {return type(so);};
835 
841  inline unsigned segsize(unsigned size)
842  {return segsize(so, size);};
843 
849  inline bool ccid(uint8_t id)
850  {return ccid(so, id);};
851 
860  inline int tos(int type)
861  {return tos(so, type);};
862 
869  inline int priority(int scheduling)
870  {return priority(so, scheduling);};
871 
875  inline void shutdown(void)
876  {::shutdown(so, SHUT_RDWR);};
877 
885  int connectto(struct addrinfo *list);
886 
893  int disconnect(void);
894 
900  int join(const struct addrinfo *list);
901 
907  int drop(const struct addrinfo *list);
908 
914  int wait(timeout_t timeout = Timer::inf);
915 
922  size_t peek(void *data, size_t number) const;
923 
931  size_t readfrom(void *data, size_t number, struct sockaddr_storage *address = NULL);
932 
940  size_t writeto(const void *data, size_t number, const struct sockaddr *address = NULL);
941 
954  size_t readline(char *data, size_t size);
955 
961  size_t printf(const char *format, ...) __PRINTF(2,3);
962 
974  size_t readline(String& buffer);
975 
987  static ssize_t readline(socket_t socket, char *data, size_t size, timeout_t timeout = Timer::inf);
988 
995  static ssize_t printf(socket_t socket, const char *format, ...) __PRINTF(2,3);
996 
1004  size_t writes(const char *string);
1005 
1010  operator bool();
1011 
1016  bool operator!() const;
1017 
1023  Socket& operator=(socket_t socket);
1024 
1029  inline operator socket_t() const
1030  {return so;};
1031 
1036  inline socket_t operator*() const
1037  {return so;};
1038 
1045  static unsigned pending(socket_t socket);
1046 
1053  static int sendsize(socket_t socket, unsigned size);
1054 
1061  static int sendwait(socket_t socket, unsigned size);
1062 
1069  static int recvsize(socket_t socket, unsigned size);
1070 
1079  static int connectto(socket_t socket, struct addrinfo *list);
1080 
1086  static int disconnect(socket_t socket);
1087 
1094  static int drop(socket_t socket, const struct addrinfo *list);
1095 
1102  static int join(socket_t socket, const struct addrinfo *list);
1103 
1109  static int error(socket_t socket);
1110 
1117  static int multicast(socket_t socket, unsigned ttl = 1);
1118 
1125  static int loopback(socket_t socket, bool enable);
1126 
1133  static int blocking(socket_t socket, bool enable);
1134 
1141  static int keepalive(socket_t socket, bool enable);
1142 
1149  static int broadcast(socket_t socket, bool enable);
1150 
1156  static int nodelay(socket_t socket);
1157 
1164  static int priority(socket_t socket, int scheduling);
1165 
1172  static int tos(socket_t socket, int type);
1173 
1180  static int ttl(socket_t socket, unsigned char time);
1181 
1186  static int family(socket_t socket);
1187 
1193  inline static int family(const struct sockaddr_storage& address)
1194  {return ((const struct sockaddr *)&address)->sa_family;};
1195 
1201  inline static int family(const struct sockaddr_internet& address)
1202  {return address.address.sa_family;};
1203 
1213  static ssize_t recvfrom(socket_t socket, void *buffer, size_t size, int flags = 0, struct sockaddr_storage *address = NULL);
1214 
1224  static ssize_t sendto(socket_t socket, const void *buffer, size_t size, int flags = 0, const struct sockaddr *address = NULL);
1225 
1235  inline static ssize_t replyto(socket_t socket, const void *buffer, size_t size, int flags, const struct sockaddr_storage *address)
1236  {return sendto(socket, buffer, size, flags, (const struct sockaddr *)address);};
1237 
1247  inline static ssize_t sendinet(socket_t socket, const void *buffer, size_t size, int flags, const struct sockaddr_internet *address)
1248  {return sendto(socket, buffer, size, flags, (const struct sockaddr *)address);};
1249 
1259  static ssize_t recvinet(socket_t socket, void *buffer, size_t size, int flags = 0, struct sockaddr_internet *address = NULL);
1260 
1269  static int bindto(socket_t socket, const char *address, const char *service, int protocol = 0);
1270 
1278  static int listento(socket_t socket, const struct sockaddr *address, int backlog = 5);
1279 
1286  static int bindto(socket_t socket, const struct sockaddr *address);
1287 
1294  static socket_t acceptfrom(socket_t socket, struct sockaddr_storage *address = NULL);
1295 
1303  static socket_t create(int family, int type, int protocol);
1304 
1312  static socket_t create(const struct addrinfo *address, int type, int protocol);
1313 
1323  static socket_t create(const char *iface, const char *service, int family = AF_UNSPEC, int type = 0, int protocol = 0);
1324 
1330  static socket_t create(const Socket::address &address);
1331 
1336  static void release(socket_t socket);
1337 
1345  static char *hostname(const struct sockaddr *address, char *buffer, size_t size);
1346 
1354  static struct addrinfo *hinting(socket_t socket, struct addrinfo *hint);
1355 
1366  static socklen_t query(socket_t socket, struct sockaddr_storage *address, const char *hostname, const char *service);
1367 
1373  static socklen_t len(const struct sockaddr *address);
1374 
1382  static bool equal(const struct sockaddr *address1, const struct sockaddr *address2);
1383 
1390  static unsigned copy(struct sockaddr *target, const struct sockaddr *origin);
1391 
1398  inline static unsigned store(struct sockaddr_storage *storage, const struct sockaddr *address)
1399  {return copy((struct sockaddr*)storage, address);};
1400 
1407  static unsigned store(struct sockaddr_internet *storage, const struct sockaddr *address);
1408 
1416  static bool eq_host(const struct sockaddr *address1, const struct sockaddr *address2);
1417 
1425  inline static bool eq_from(const struct sockaddr_storage *address1, const struct sockaddr_storage *address2)
1426  {return equal((const struct sockaddr *)address1, (const struct sockaddr *)address2);};
1427 
1435  inline static bool eq_inet(const struct sockaddr_internet *address1, const struct sockaddr_internet *address2)
1436  {return equal((const struct sockaddr *)address1, (const struct sockaddr *)address2);};
1437 
1445  static bool eq_subnet(const struct sockaddr *address1, const struct sockaddr *address2);
1446 
1454  static int via(struct sockaddr *address, const struct sockaddr *destination);
1455 
1463  static char *query(const struct sockaddr *address, char *buffer, socklen_t size);
1464 
1470  static short service(const struct sockaddr *address);
1471 
1477  inline static short service(const struct sockaddr_internet *address)
1478  {return service((const struct sockaddr *)address);};
1479 
1486  static unsigned keyindex(const struct sockaddr *address, unsigned size);
1487 
1494  static unsigned keyhost(const struct sockaddr *address, unsigned size);
1495 
1499  static void init(void);
1500 
1505  static void init(const char *program);
1506 
1512  static void query(int family);
1513 
1520  static void v4mapping(bool enable);
1521 
1526  static int error(void);
1527 
1536  static bool is_null(const char *string);
1537 
1545  static bool is_numeric(const char *string);
1546 
1555  static int local(socket_t socket, struct sockaddr_storage *address);
1556 
1565  static int remote(socket_t socket, struct sockaddr_storage *address);
1566 };
1567 
1573 class __EXPORT ListenSocket : protected Socket
1574 {
1575 public:
1585  ListenSocket(const char *address, const char *service, unsigned backlog = 5, int family = AF_UNSPEC, int type = 0, int protocol = 0);
1586 
1597  static socket_t create(const char *address, const char *service, unsigned backlog = 5, int family = AF_UNSPEC, int type = 0, int protocol = 0);
1598 
1604  socket_t accept(struct sockaddr_storage *address = NULL) const;
1605 
1611  inline bool wait(timeout_t timeout = Timer::inf) const
1612  {return Socket::wait(timeout);};
1613 
1618  inline operator socket_t() const
1619  {return so;}
1620 
1625  inline socket_t operator*() const
1626  {return so;}
1627 
1632  inline socket_t getsocket(void) const
1633  {return so;}
1634 
1635  inline socket_t handle(void) const
1636  {return so;}
1637 
1638 };
1639 
1645 class __EXPORT TCPServer : public ListenSocket
1646 {
1647 public:
1655  TCPServer(const char *address, const char *service, unsigned backlog = 5);
1656 };
1657 
1661 __EXPORT struct addrinfo *_nextaddrinfo(struct addrinfo *addrinfo);
1662 
1666 __EXPORT struct sockaddr *_getaddrinfo(struct addrinfo *addrinfo);
1667 
1671 __EXPORT socket_t _getaddrsock(struct addrinfo *addrinfo);
1672 
1678 template <>
1679 class linked_pointer<sockaddr_struct>
1680 {
1681 private:
1682  struct addrinfo *ptr;
1683 
1684 public:
1685  inline linked_pointer(struct addrinfo *list)
1686  {ptr = list;}
1687 
1688  inline linked_pointer()
1689  {ptr = NULL;}
1690 
1691  inline linked_pointer(Socket::address& list)
1692  {ptr = list.getList();};
1693 
1698  inline operator struct sockaddr *() const
1699  {return _getaddrinfo(ptr);};
1700 
1705  inline struct sockaddr *operator*() const
1706  {return _getaddrinfo(ptr);};
1707 
1708  inline operator struct sockaddr_in *() const
1709  {return (struct sockaddr_in *)_getaddrinfo(ptr);};
1710 
1711  inline struct sockaddr_in *in(void) const
1712  {return (struct sockaddr_in *)_getaddrinfo(ptr);};
1713 
1714 #ifdef AF_INET6
1715  inline operator struct sockaddr_in6 *() const
1716  {return (struct sockaddr_in6 *)_getaddrinfo(ptr);};
1717 
1718  inline struct sockaddr_in6 *in6(void) const
1719  {return (struct sockaddr_in6 *)_getaddrinfo(ptr);};
1720 #endif
1721 
1725  inline socket_t operator()(void) const
1726  {return _getaddrsock(ptr);};
1727 
1732  inline operator bool() const
1733  {return ptr != NULL;};
1734 
1739  inline void operator=(struct addrinfo *list)
1740  {ptr = list;};
1741 
1746  inline void operator=(Socket::address& list)
1747  {ptr = list.getList();};
1748 
1753  inline void set(struct addrinfo *list)
1754  {ptr = list;};
1755 
1760  inline void set(Socket::address& list)
1761  {ptr = list.getList();};
1762 
1763 
1768  inline struct sockaddr* operator->() const
1769  {return _getaddrinfo(ptr);};
1770 
1775  inline bool operator!() const
1776  {return ptr == NULL;};
1777 
1778  inline void next(void)
1779  {ptr = _nextaddrinfo(ptr);};
1780 };
1781 
1787 inline struct addrinfo *addrinfo(Socket::address& address)
1788  {return address.getList();}
1789 
1796 inline struct sockaddr *addr(Socket::address& address)
1797  {return address.get();}
1798 
1806 inline bool eq(const struct sockaddr *s1, const struct sockaddr *s2)
1807  {return Socket::equal(s1, s2);}
1808 
1816 inline bool eq(const struct sockaddr_storage *s1, const struct sockaddr_storage *s2)
1817  {return Socket::equal((const struct sockaddr *)s1, (const struct sockaddr *)s2);}
1818 
1826 inline bool eq_host(const struct sockaddr *s1, const struct sockaddr *s2)
1827  {return Socket::eq_host(s1, s2);}
1828 
1829 inline bool eq_subnet(const struct sockaddr *s1, const struct sockaddr *s2)
1830  {return Socket::eq_subnet(s1, s2);}
1831 
1832 String str(Socket& so, strsize_t size);
1833 
1834 typedef TCPServer tcpserv_t;
1835 
1836 END_NAMESPACE
1837 
1838 #endif
bool operator==(const struct sockaddr *address) const
Test if a given socket address falls within this cidr.
Definition: socket.h:291
An object that can hold a ipv4 or ipv6 socket address.
Definition: socket.h:101
struct sockaddr * addr(Socket::address &address)
A convenience function to convert a socket address list into a socket address.
Definition: socket.h:1796
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:1235
struct sockaddr * operator*() const
Return the full socket address list by pointer reference.
Definition: socket.h:1705
int multicast(unsigned ttl=1)
Set multicast mode and multicast broadcast range.
Definition: socket.h:755
static socket_t create(int family, int type, int protocol)
Create a socket object unbound.
bool eq(const struct sockaddr_storage *s1, const struct sockaddr_storage *s2)
Compare two stored socket addresses to see if equal.
Definition: socket.h:1816
A common string class and character string support functions.
int keepalive(bool enable)
Set socket for keepalive packets.
Definition: socket.h:739
int err(void) const
Get error code.
Definition: socket.h:671
T * dup(const T &object)
Convenience function to duplicate object pointer to heap.
Definition: generics.h:478
socket_t operator()(void) const
Get socket as expression operator.
Definition: socket.h:1725
void operator=(struct addrinfo *list)
Assign our pointer from an address list.
Definition: socket.h:1739
void set(Socket::address &list)
Assign our pointer from an address list.
Definition: socket.h:1760
An object that holds ipv4 or ipv6 binary encoded host addresses.
Definition: socket.h:82
bool wait(timeout_t timeout=0) const
Test for pending input data.
int getError(void)
Get socket error code.
Definition: socket.h:770
T * init(T *memory)
Template function to initialize memory by invoking default constructor.
Definition: platform.h:420
struct sockaddr * _getaddrinfo(struct addrinfo *addrinfo)
Helper function for linked_pointer<struct sockaddr>.
A generic socket base class.
Definition: socket.h:310
int ttl(unsigned char time)
Set the time to live before packets expire.
Definition: socket.h:778
struct addrinfo * _nextaddrinfo(struct addrinfo *addrinfo)
Helper function for linked_pointer<struct sockaddr>.
int sendsize(unsigned size)
Set the size of the socket send buffer.
Definition: socket.h:786
struct addrinfo * operator*() const
Return the full socket address list by pointer reference.
Definition: socket.h:475
A bound socket used to listen for inbound socket connections.
Definition: socket.h:1573
int priority(int scheduling)
Set packet priority, 0 to 6 unless privileged.
Definition: socket.h:869
unsigned long timeout_t
Typedef for millisecond timer values.
Definition: platform.h:326
int sendwait(unsigned size)
Set the size to wait before sending.
Definition: socket.h:794
int recvsize(unsigned size)
Set the size of the socket receive buffer.
Definition: socket.h:803
A generic tcp server class.
Definition: socket.h:1645
socket_t operator*() const
Get the socket descriptor by pointer reference.
Definition: socket.h:1036
bool eq_host(const struct sockaddr *s1, const struct sockaddr *s2)
Compare two host addresses to see if equal.
Definition: socket.h:1826
inethostaddr_t getBroadcast(void) const
Get the broadcast host address represented by our cidr.
Definition: socket.h:263
socket_t operator*() const
Get the socket descriptor of the listener by pointer reference.
Definition: socket.h:1625
struct sockaddr_internet inetsockaddr_t
An object that can hold a ipv4 or ipv6 socket address.
bool operator!() const
Test if we have no address list.
Definition: socket.h:1775
static int family(const struct sockaddr_storage &address)
Get the address family of a socket address object.
Definition: socket.h:1193
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:1247
unsigned short strsize_t
A convenience class for size of strings.
Definition: string.h:70
A copy-on-write string class that operates by reference count.
Definition: string.h:82
A smart pointer template for iterating linked lists.
Definition: linked.h:1288
Realtime timers and timer queues.
static int family(const struct sockaddr_internet &address)
Get the address family of an internet socket address object.
Definition: socket.h:1201
void operator=(Socket::address &list)
Assign our pointer from an address list.
Definition: socket.h:1746
bool ccid(uint8_t id)
Set ccid of dccp socket.
Definition: socket.h:849
int blocking(bool enable)
Set socket blocking I/O mode.
Definition: socket.h:747
int broadcast(bool enable)
Set socket for unicast mode broadcasts.
Definition: socket.h:731
static unsigned store(struct sockaddr_storage *storage, const struct sockaddr *address)
Store an address into an address object.
Definition: socket.h:1398
unsigned getMask(void) const
Get the number of bits in the cidr bitmask.
Definition: socket.h:270
struct addrinfo * getList(void) const
Get the full socket address list from the object.
Definition: socket.h:461
int tos(int type)
Set the type of service field of outgoing packets.
Definition: socket.h:860
void release(SharedAccess &object)
Convenience function to unlock shared object through it's protocol.
Definition: access.h:260
static bool eq_from(const struct sockaddr_storage *address1, const struct sockaddr_storage *address2)
Compare socket addresses.
Definition: socket.h:1425
Common base class for all objects that can be formed into a linked list.
Definition: linked.h:54
static short service(const struct sockaddr_internet *address)
Get the service port of an inet socket.
Definition: socket.h:1477
int getFamily(void) const
Get the address family of our cidr block object.
Definition: socket.h:242
socket_t _getaddrsock(struct addrinfo *addrinfo)
Helper function for linked_pointer<struct sockaddr>.
A class to hold internet segment routing rules.
Definition: socket.h:158
static bool eq_inet(const struct sockaddr_internet *address1, const struct sockaddr_internet *address2)
Compare socket addresses.
Definition: socket.h:1435
int loopback(bool enable)
Set loopback to read multicast packets we broadcast.
Definition: socket.h:763
bool operator!() const
Test if we have no address list.
Definition: socket.h:489
struct sockaddr * get(void) const
Get the first socket address in our address list.
struct addrinfo * addrinfo(Socket::address &address)
A convenience function to convert a socket address list into an addrinfo.
Definition: socket.h:1787
unsigned segsize(unsigned size)
Set segment size and get mtu of a socket.
Definition: socket.h:841
const char * getName(void) const
Get the saved name of our cidr.
Definition: socket.h:235
inethostaddr_t getNetmask(void) const
Get the effective network mask for our cidr block.
Definition: socket.h:256
bool is_member(LinkedObject *list) const
Search to see if we are a member of a specific list.
socket_t getsocket(void) const
Get the socket descriptor of the listener.
Definition: socket.h:1632
unsigned pending(void) const
Get the number of bytes of data in the socket receive buffer.
Definition: socket.h:723
struct hostaddr_internet inethostaddr_t
An object that holds ipv4 or ipv6 binary encoded host addresses.
bool operator!=(const struct sockaddr *address) const
Test if a given socket address falls outside this cidr.
Definition: socket.h:299
int nodelay(void) const
Set nodelay option for tcp socket.
Definition: socket.h:699
A generic socket address class.
Definition: socket.h:343
bool wait(timeout_t timeout=Timer::inf) const
Wait for a pending connection.
Definition: socket.h:1611
int type(void)
Get the type of a socket.
Definition: socket.h:833
void wait(barrier_t &barrier)
Convenience function to wait on a barrier.
Definition: thread.h:1869
void shutdown(void)
Shutdown the socket communication channel.
Definition: socket.h:875
Linked objects, lists, templates, and containers.
LinkedObject policy
A convenience type for using a pointer to a linked list as a policy chain.
Definition: socket.h:175
ObjectProtocol * copy(ObjectProtocol *object)
Convenience function to access object copy.
Definition: object.h:479
Timer class to use when scheduling realtime events.
Definition: timers.h:49
void set(struct addrinfo *list)
Assign our pointer from an address list.
Definition: socket.h:1753
struct sockaddr * operator->() const
Return member from typed object our pointer references.
Definition: socket.h:1768
inethostaddr_t getNetwork(void) const
Get the network host base address of our cidr block.
Definition: socket.h:249