blob: cc99b04399f6f05a49ecddf0e7ee39e2bb5f61b6 [file] [log] [blame]
Alexandre Lisionddd731e2014-01-31 11:50:08 -05001// 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
18/**
19 * Common socket class and address manipulation.
20 * This offers a common socket base class that exposes socket functionality
21 * based on what the target platform supports. Support for multicast, IPV6
22 * addressing, and manipulation of cidr policies are all supported here.
23 * @file ucommon/socket.h
24 */
25
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
41extern "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
73struct sockaddr_internet;
74
75typedef struct sockaddr *sockaddr_t;
76
77typedef struct sockaddr sockaddr_struct; // older gcc needs...?
78
79/**
80 * An object that holds ipv4 or ipv6 binary encoded host addresses.
81 */
82typedef struct hostaddr_internet
83{
84 union
85 {
86 struct in_addr ipv4;
87#ifdef AF_INET6
88 struct in6_addr ipv6;
89#endif
90 };
91} inethostaddr_t;
92
93#if defined(AF_INET6) || defined(__CYGWIN__)
94/**
95 * An object that can hold a ipv4 or ipv6 socket address. This would be
96 * used for tcpip socket connections. We do not use sockaddr_storage
97 * because it is not present in pre ipv6 stacks, and because the storage
98 * size also includes the size of the path of a unix domain socket on
99 * posix systems.
100 */
101typedef 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 };
110} inetsockaddr_t;
111#else
112typedef struct sockaddr_internet
113{
114 union {
115 struct sockaddr_in ipv4;
116 struct sockaddr address;
117 };
118} inetsockaddr_t;
119
120struct 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
147NAMESPACE_UCOMMON
148
149/**
150 * A class to hold internet segment routing rules. This class can be used
151 * to provide a stand-alone representation of a cidr block of internet
152 * addresses or chained together into some form of access control list. The
153 * cidr class can hold segments for both IPV4 and IPV6 addresses. The class
154 * accepts cidr's defined as C strings, typically in the form of address/bits
155 * or address/submask. These routines auto-detect ipv4 and ipv6 addresses.
156 * @author David Sugar <dyfet@gnutelephony.org>
157 */
158class __EXPORT cidr : public LinkedObject
159{
160protected:
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
171public:
172 /**
173 * A convenience type for using a pointer to a linked list as a policy chain.
174 */
175 typedef LinkedObject policy;
176
177 /**
178 * Create an uninitialized cidr.
179 */
180 cidr();
181
182 /**
183 * Create an unlinked cidr from a string. The string is typically in
184 * the form base-host-address/range, where range might be a bit count
185 * or a network mask.
186 * @param string for cidr block.
187 */
188 cidr(const char *string);
189
190 /**
191 * Create an unnamed cidr entry on a specified policy chain.
192 * @param policy chain to link cidr to.
193 * @param string for cidr block.
194 */
195 cidr(policy **policy, const char *string);
196
197 /**
198 * Create a named cidr entry on a specified policy chain.
199 * @param policy chain to link cidr to.
200 * @param string for cidr block.
201 * @param name of this policy object.
202 */
203 cidr(policy **policy, const char *string, const char *name);
204
205 /**
206 * Construct a copy of an existing cidr.
207 * @param existing cidr we copy from.
208 */
209 cidr(const cidr& existing);
210
211 /**
212 * Find the smallest cidr entry in a list that matches the socket address.
213 * @param policy chain to search.
214 * @param address to search for.
215 * @return smallest cidr or NULL if none match.
216 */
217 static const cidr *find(const policy *policy, const struct sockaddr *address);
218
219 /**
220 * Get the largest container cidr entry in a list that matches the
221 * socket address.
222 * @param policy chain to search.
223 * @param address to search for.
224 * @return largest cidr or NULL if none match.
225 */
226 static const cidr *container(const policy *policy, const struct sockaddr *address);
227
228 /**
229 * Get the saved name of our cidr. This is typically used with find
230 * when the same policy name might be associated with multiple non-
231 * overlapping cidr blocks. A typical use might to have a cidr
232 * block like 127/8 named "localdomain", as well as the ipv6 "::1".
233 * @return name of cidr.
234 */
235 inline const char *getName(void) const
236 {return Name;};
237
238 /**
239 * Get the address family of our cidr block object.
240 * @return family of our cidr.
241 */
242 inline int getFamily(void) const
243 {return Family;};
244
245 /**
246 * Get the network host base address of our cidr block.
247 * @return binary network host address.
248 */
249 inline inethostaddr_t getNetwork(void) const
250 {return Network;};
251
252 /**
253 * Get the effective network mask for our cidr block.
254 * @return binary network mask for our cidr.
255 */
256 inline inethostaddr_t getNetmask(void) const
257 {return Netmask;};
258
259 /**
260 * Get the broadcast host address represented by our cidr.
261 * @return binary broadcast host address.
262 */
263 inline inethostaddr_t getBroadcast(void) const
264 {return broadcast();}
265
266 /**
267 * Get the number of bits in the cidr bitmask.
268 * @return bit mask of cidr.
269 */
270 inline unsigned getMask(void) const
271 {return mask();}
272
273 /**
274 * Set our cidr to a string address. Replaces prior value.
275 * @param string to set for cidr.
276 */
277 void set(const char *string);
278
279 /**
280 * Test if a given socket address falls within this cidr.
281 * @param address of socket to test.
282 * @return true if address is within cidr.
283 */
284 bool is_member(const struct sockaddr *address) const;
285
286 /**
287 * Test if a given socket address falls within this cidr.
288 * @param address of socket to test.
289 * @return true if address is within cidr.
290 */
291 inline bool operator==(const struct sockaddr *address) const
292 {return is_member(address);};
293
294 /**
295 * Test if a given socket address falls outside this cidr.
296 * @param address of socket to test.
297 * @return true if address is outside cidr.
298 */
299 inline bool operator!=(const struct sockaddr *address) const
300 {return !is_member(address);};
301};
302
303/**
304 * A generic socket base class. This class can be used directly or as a
305 * base class for building network protocol stacks. This common base tries
306 * to handle UDP and TCP sockets, as well as support multicast, IPV4/IPV6
307 * addressing, and additional addressing domains (such as Unix domain sockets).
308 * @author David Sugar <dyfet@gnutelephony.org>
309 */
310class __EXPORT Socket
311{
312protected:
313 socket_t so;
314 int ioerr;
315 timeout_t iowait;
316
317public:
318 /**
319 * Get an address list directly. This is used internally by some derived
320 * socket types when generic address lists would be invalid.
321 * @param host name in the form address or "address:port"
322 * @param service id or port to use if not specified in host string.
323 * @param type of service to get.
324 * @param protocol of service to get.
325 */
326 static struct addrinfo *query(const char *host, const char *service, int type = SOCK_STREAM, int protocol = 0);
327
328 /**
329 * Release an address list directly. This is used internally by some
330 * derived socket types which do not use generic address lists.
331 * @param list of addresses.
332 */
333 static void release(struct addrinfo *list);
334
335 /**
336 * A generic socket address class. This class uses the addrinfo list
337 * to store socket multiple addresses in a protocol and family
338 * independent manner. Hence, this address class can be used for ipv4
339 * and ipv6 sockets, for assigning connections to multiple hosts, etc.
340 * The address class will call the resolver when passed host names.
341 * @author David Sugar <dyfet@gnutelephony.org>
342 */
343 class __EXPORT address
344 {
345 protected:
346 struct addrinfo *list;
347
348 public:
349 /**
350 * Construct a socket address. This is used to get an address to
351 * bind a socket interface to. The address can be specified as the
352 * ip address of the interface or as a "hostname". If a hostname
353 * is used, then family should be specified for clarity.
354 * @param family of socket address. Needed when host names are used.
355 * @param address or hostname.
356 * @param type of socket (stream, dgram, etc).
357 * @param protocol number of socket.
358 */
359 address(int family, const char *address, int type = SOCK_STREAM, int protocol = 0);
360
361 /**
362 * Construct a socket address for an existing socket. This can be the
363 * name of a host or to perform a lookup in a domain for a service.
364 * Family can be used to restrict the set of results returned, however
365 * since things like connecto() already filter by family and create
366 * will use family from the addrinfo, in most cases AF_UNSPEC can be
367 * used. This may be depreciated in favor of the constructor that
368 * matches a set() method.
369 * @param family of hosts to filter by or AF_UNSPEC.
370 * @param hostname or ip address. The socket family is used for hostnames.
371 * @param service port or name we are referencing or NULL.
372 */
373 address(int family, const char *hostname, const char *service = NULL);
374
375 /**
376 * Construct a socket address list for a service.
377 * @param host address for service.
378 * @param service name or port number.
379 * @param type of service, stream, dgram, etc.
380 */
381 address(const char *host, const char *service, int type = SOCK_STREAM);
382
383 /**
384 * Construct a socket address from host and service. This is primarily
385 * used to construct a list of potential service connections by pure
386 * port numbers or for host lookup only.
387 * @param hostname or address to use.
388 * @param service port or 0.
389 */
390 address(const char *hostname, unsigned service = 0);
391
392 /**
393 * Construct an empty address.
394 */
395 address();
396
397 /**
398 * Copy constructor.
399 * @param reference to object to copy from.
400 */
401 address(const address& reference);
402
403 /**
404 * Destroy address. Deallocate addrinfo structure.
405 */
406 ~address();
407
408 /**
409 * Get the first socket address in our address list.
410 * @return first socket address or NULL if none.
411 */
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
420 /**
421 * Get the first socket address by casted reference.
422 * @return first socket address we resolved or NULL if none.
423 */
424 inline operator struct sockaddr *() const
425 {return get();};
426
427 /**
428 * Get the first socket address of specified family from our list.
429 * @param family to seek.
430 * @return first socket address of family or NULL if none.
431 */
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
445 /**
446 * Get the family of the first member in a list of services.
447 * @return family of first socket address or 0 if none.
448 */
449 int family(void) const;
450
451 /**
452 * Find a specific socket address in our address list.
453 * @return matching address from list or NULL if not found.
454 */
455 struct sockaddr *find(const struct sockaddr *addr) const;
456
457 /**
458 * Get the full socket address list from the object.
459 * @return addrinfo list we resolved or NULL if none.
460 */
461 inline struct addrinfo *getList(void) const
462 {return list;};
463
464 /**
465 * Get the full socket address list by casted reference.
466 * @return addrinfo list we resolved or NULL if none.
467 */
468 inline operator struct addrinfo *() const
469 {return list;};
470
471 /**
472 * Return the full socket address list by pointer reference.
473 * @return addrinfo list we resolved or NULL if none.
474 */
475 inline struct addrinfo *operator*() const
476 {return list;};
477
478 /**
479 * Test if the address list is valid.
480 * @return true if we have an address list.
481 */
482 inline operator bool() const
483 {return list != NULL;};
484
485 /**
486 * Test if we have no address list.
487 * @return true if we have no address list.
488 */
489 inline bool operator!() const
490 {return list == NULL;};
491
492
493 /**
494 * Clear current object.
495 */
496 void clear(void);
497
498 /**
499 * Set the host addresses to form a new list.
500 * @param hostname or address to resolve.
501 * @param service name or port number, or NULL if not used.
502 * @param type of socket (stream or dgram) to filter list by.
503 */
504 void set(const char *hostname, const char *service = NULL, int type = SOCK_STREAM);
505
506 /**
507 * Append additional host addresses to our list.
508 * @param hostname or address to resolve.
509 * @param service name or port number, or NULL if not used.
510 * @param type of socket (stream or dgram).
511 */
512 void add(const char *hostname, const char *service = NULL, int type = SOCK_STREAM);
513
514 /**
515 * Set an entry for host binding.
516 * @param family of socket address. Needed when hostnames are used.
517 * @param address or hostname.
518 * @param type of socket (stream, dgram, etc).
519 * @param protocol number of socket.
520 */
521 void set(int family, const char *address, int type = SOCK_STREAM, int protocol = 0);
522
523 /**
524 * Add an individual socket address to our address list.
525 * @param address to add.
526 */
527 void add(sockaddr *address);
528
529 /**
530 * Insert unique members from another socket address list to ours.
531 * @param address list to insert into list.
532 * @return count of addresses added.
533 */
534 unsigned insert(struct addrinfo *address);
535
536 /**
537 * Remove members from another socket address list from ours.
538 * @param address list to remove from list.
539 * @return count of addresses removed.
540 */
541 unsigned remove(struct addrinfo *address);
542
543 /**
544 * Remove an individual socket address from our address list.
545 * @param address to remove.
546 * @return true if found and removed, false if not found.
547 */
548 bool remove(struct sockaddr *address);
549
550 /**
551 * Insert an individual socket address to our address list only if
552 * unique.
553 * @param address to insert into list.
554 * @return true if inserted, false if duplicate.
555 */
556 bool insert(struct sockaddr *address);
557
558 /**
559 * Copy an existing addrinfo into our object. This is also used
560 * to support the copy constructor.
561 * @param address list to copy from.
562 */
563 void copy(const struct addrinfo *address);
564
565 /**
566 * Set an individual socket address for our address list.
567 * @param address to add.
568 */
569 void set(struct sockaddr *address);
570
571 /**
572 * Set a socket address from host and service.
573 * @param hostname or address to use.
574 * @param service port or 0.
575 */
576 void set(const char *hostname, unsigned service = 0);
577
578 /**
579 * Duplicate a socket address.
580 * @param address to duplicate.
581 * @return duplicate address object.
582 */
583 static struct sockaddr *dup(struct sockaddr *address);
584
585 /**
586 * Convert address object into ipv4 address.
587 * @param address to convert.
588 * @return new ipv4 address or NULL if not ipv4.
589 */
590 static struct sockaddr_in *ipv4(struct sockaddr *address);
591
592#ifdef AF_INET6
593 /**
594 * Convert address object into ipv6 address.
595 * @param address to convert.
596 * @return new ipv6 address or NULL if not ipv6.
597 */
598 static struct sockaddr_in6 *ipv6(struct sockaddr *address);
599#endif
600 };
601
602 friend class address;
603
604 /**
605 * Create a socket object for use.
606 */
607 Socket();
608
609 /**
610 * Create socket as duped handle of existing socket.
611 * @param existing socket to dup.
612 */
613 Socket(const Socket& existing);
614
615 /**
616 * Create socket from existing socket descriptor.
617 * @param socket descriptor to use.
618 */
619 Socket(socket_t socket);
620
621 /**
622 * Create and connect a socket to an address from an address list. The
623 * type of socket created is based on the type we are connecting to.
624 * @param address list to connect with.
625 */
626 Socket(struct addrinfo *address);
627
628 /**
629 * Create an unbound socket of a specific type.
630 * @param family of our new socket.
631 * @param type (stream, udp, etc) of our new socket.
632 * @param protocol number of our new socket.'
633 */
634 Socket(int family, int type, int protocol = 0);
635
636 /**
637 * Create a bound socket. If one wishes to listen for connections on
638 * a protocol, then ListenSocket should be used instead.
639 * @param address to bind or "*" for all.
640 * @param port number of service to bind.
641 * @param family to bind as.
642 * @param type of socket to bind (stream, udp, etc).
643 * @param protocol of socket to bind.
644 */
645 Socket(const char *address, const char *port, int family = AF_UNSPEC, int type = 0, int protocol = 0);
646
647 /**
648 * Shutdown, close, and destroy socket.
649 */
650 virtual ~Socket();
651
652 /**
653 * Cancel pending i/o by shutting down the socket.
654 */
655 void cancel(void);
656
657 /**
658 * Cancel pending i/o by shutting down the socket.
659 * @param socket to shutdown.
660 */
661 static void cancel(socket_t socket);
662
663 /**
664 * Shutdown and close the socket.
665 */
666 void release(void);
667
668 /**
669 * Get error code.
670 */
671 inline int err(void) const
672 {return ioerr;}
673
674 /**
675 * See the number of bytes in the receive queue.
676 * @param value to test for.
677 * @return true if at least that many bytes waiting in receive queue.
678 */
679 bool is_pending(unsigned value) const;
680
681 /**
682 * Test if socket is connected.
683 * @return true if connected.
684 */
685 bool connected(void) const;
686
687 /**
688 * Test for pending input data. This function can wait up to a specified
689 * timeout for data to appear.
690 * @param timeout or 0 if none.
691 * @return true if input data waiting.
692 */
693 bool wait(timeout_t timeout = 0) const;
694
695 /**
696 * Set nodelay option for tcp socket.
697 * @return 0 if successful, -1 on error.
698 */
699 inline int nodelay(void) const
700 {return nodelay(so);};
701
702 /**
703 * Test for pending input data. This function can wait up to a specified
704 * timeout for data to appear.
705 * @param socket to test.
706 * @param timeout or 0 if none.
707 * @return true if input data waiting.
708 */
709 static bool wait(socket_t socket, timeout_t timeout = 0);
710
711 /**
712 * Test for output data sent. This function can wait up to a specified
713 * timeout for data to appear sent.
714 * @param timeout or 0 if none.
715 * @return false if cannot send more output/out of buffer space.
716 */
717 bool waitSending(timeout_t timeout = 0) const;
718
719 /**
720 * Get the number of bytes of data in the socket receive buffer.
721 * @return bytes pending.
722 */
723 inline unsigned pending(void) const
724 {return pending(so);};
725
726 /**
727 * Set socket for unicast mode broadcasts.
728 * @param enable broadcasting if true.
729 * @return 0 on success, -1 if error.
730 */
731 inline int broadcast(bool enable)
732 {return broadcast(so, enable);};
733
734 /**
735 * Set socket for keepalive packets.
736 * @param enable keep-alive if true.
737 * @return 0 on success, -1 if error.
738 */
739 inline int keepalive(bool enable)
740 {return keepalive(so, enable);};
741
742 /**
743 * Set socket blocking I/O mode.
744 * @param enable true for blocking I/O.
745 * @return 0 on success, -1 if error.
746 */
747 inline int blocking(bool enable)
748 {return blocking(so, enable);};
749
750 /**
751 * Set multicast mode and multicast broadcast range.
752 * @param ttl to set for multicast socket or 0 to disable multicast.
753 * @return 0 on success, -1 if error.
754 */
755 inline int multicast(unsigned ttl = 1)
756 {return multicast(so, ttl);};
757
758 /**
759 * Set loopback to read multicast packets we broadcast.
760 * @param enable true to loopback, false to ignore.
761 * @return 0 on success, -1 if error.
762 */
763 inline int loopback(bool enable)
764 {return loopback(so, enable);};
765
766 /**
767 * Get socket error code.
768 * @return socket error code.
769 */
770 inline int getError(void)
771 {return error(so);};
772
773 /**
774 * Set the time to live before packets expire.
775 * @param time to live to set.
776 * @return 0 on success, -1 on error.
777 */
778 inline int ttl(unsigned char time)
779 {return ttl(so, time);};
780
781 /**
782 * Set the size of the socket send buffer.
783 * @param size of send buffer to set.
784 * @return 0 on success, -1 on error.
785 */
786 inline int sendsize(unsigned size)
787 {return sendsize(so, size);};
788
789 /**
790 * Set the size to wait before sending.
791 * @param size of send wait buffer to set.
792 * @return 0 on success, -1 on error.
793 */
794 inline int sendwait(unsigned size)
795 {return sendwait(so, size);};
796
797
798 /**
799 * Set the size of the socket receive buffer.
800 * @param size of recv buffer to set.
801 * @return 0 on success, -1 on error.
802 */
803 inline int recvsize(unsigned size)
804 {return recvsize(so, size);};
805
806 /**
807 * Get the type of a socket.
808 * @param socket descriptor.
809 * @return socket type.
810 */
811 static int type(socket_t socket);
812
813 /**
814 * Set segment size and get MTU.
815 * @param socket to modify.
816 * @param size of segment or zero to not set.
817 * @return mtu size of socket.
818 */
819 static unsigned segsize(socket_t socket, unsigned size = 0);
820
821 /**
822 * Set congestion control id.
823 * @param socket to modify.
824 * @param ccid value to set.
825 * @return true if success, false if not dccp or not supported ccid used.
826 */
827 static bool ccid(socket_t socket, uint8_t id);
828
829 /**
830 * Get the type of a socket.
831 * @return socket type.
832 */
833 inline int type(void)
834 {return type(so);};
835
836 /**
837 * Set segment size and get mtu of a socket.
838 * @param size of segment or 0 to leave unchanged.
839 * @return mtu size.
840 */
841 inline unsigned segsize(unsigned size)
842 {return segsize(so, size);};
843
844 /**
845 * Set ccid of dccp socket.
846 * @param ccid to set.
847 * @return true if success, false if not dccp or not supported ccid used.
848 */
849 inline bool ccid(uint8_t id)
850 {return ccid(so, id);};
851
852 /**
853 * Set the type of service field of outgoing packets. Some useful
854 * values include IPTOS_LOWDELAY to minimize delay for interactive
855 * traffic, IPTOS_THROUGHPUT to optimize throughput, OPTOS_RELIABILITY
856 * to optimize for reliability, and IPTOS_MINCOST for low speed use.
857 * @param type of service value.
858 * @return 0 on success or -1 on error.
859 */
860 inline int tos(int type)
861 {return tos(so, type);};
862
863 /**
864 * Set packet priority, 0 to 6 unless privileged. Should be set before
865 * type-of-service.
866 * @param scheduling priority for packet scheduling.
867 * @return 0 on success, -1 on error.
868 */
869 inline int priority(int scheduling)
870 {return priority(so, scheduling);};
871
872 /**
873 * Shutdown the socket communication channel.
874 */
875 inline void shutdown(void)
876 {::shutdown(so, SHUT_RDWR);};
877
878 /**
879 * Connect our socket to a remote host from an address list.
880 * For TCP (and DCCP) sockets, the entire list may be tried. For UDP,
881 * connect is only a state and the first valid entry in the list is used.
882 * @param list of addresses to connect to.
883 * @return 0 on success or error.
884 */
885 int connectto(struct addrinfo *list);
886
887 /**
888 * Disconnect a connected socket. Depending on the implementation, this
889 * might be done by connecting to AF_UNSPEC, connecting to a 0 address,
890 * or connecting to self.
891 * @return 0 on success or error.
892 */
893 int disconnect(void);
894
895 /**
896 * Join socket to multicast group.
897 * @param list of groups to join.
898 * @return 0 on success, -1 on error.
899 */
900 int join(const struct addrinfo *list);
901
902 /**
903 * Drop socket from multicast group.
904 * @param list of groups to drop.
905 * @return 0 on success, -1 on error.
906 */
907 int drop(const struct addrinfo *list);
908
909 /**
910 * Socket i/o timer setting.
911 * @param timeout to wait, inf for blocking, 0 pure non-blocking.
912 * @return 0 on success or error code.
913 */
914 int wait(timeout_t timeout = Timer::inf);
915
916 /**
917 * Peek at data waiting in the socket receive buffer.
918 * @param data pointer to save data in.
919 * @param number of bytes to peek.
920 * @return number of bytes actually read, or 0 if no data waiting.
921 */
922 size_t peek(void *data, size_t number) const;
923
924 /**
925 * Read data from the socket receive buffer. This will be used in abi 4.
926 * @param data pointer to save data in.
927 * @param number of bytes to read.
928 * @param address of peer data was received from.
929 * @return number of bytes actually read, 0 if none, -1 if error.
930 */
931 size_t readfrom(void *data, size_t number, struct sockaddr_storage *address = NULL);
932
933 /**
934 * Write data to the socket send buffer. This will be used in abi 4.
935 * @param data pointer to write data from.
936 * @param number of bytes to write.
937 * @param address of peer to send data to if not connected.
938 * @return number of bytes actually sent, 0 if none, -1 if error.
939 */
940 size_t writeto(const void *data, size_t number, const struct sockaddr *address = NULL);
941
942 /**
943 * Read a newline of text data from the socket and save in NULL terminated
944 * string. This uses an optimized I/O method that takes advantage of
945 * socket peeking. This presumes a connected socket on a streamble
946 * protocol. Because the trailing newline is dropped, the return size
947 * may be greater than the string length. If there was no data read
948 * because of eof of data, an error has occured, or timeout without
949 * input, then 0 will be returned.
950 * @param data to save input line.
951 * @param size of input line buffer.
952 * @return number of bytes read, 0 if none, err() has error.
953 */
954 size_t readline(char *data, size_t size);
955
956 /**
957 * Print formatted string to socket.
958 * @param format string.
959 * @return number of bytes sent.
960 */
961 size_t printf(const char *format, ...) __PRINTF(2,3);
962
963 /**
964 * Read a string of input from the socket and strip trailing newline.
965 * This uses an optimized I/O method that takes advantage of
966 * socket peeking. This presumes a connected socket on a streamble
967 * protocol. Because the trailing newline is dropped, the return size
968 * may be greater than the string length. If there was no data read
969 * because of eof of data, an error has occured, or timeout without
970 * input, then 0 will be returned.
971 * @param buffer to save input line.
972 * @return number of bytes read, 0 if none, err() has error.
973 */
974 size_t readline(String& buffer);
975
976 /**
977 * Read a newline of text data from the socket and save in NULL terminated
978 * string. This uses an optimized I/O method that takes advantage of
979 * socket peeking. As such, it has to be rewritten to be used in a ssl
980 * layer socket.
981 * @param socket to read from.
982 * @param data to save input line.
983 * @param size of input line buffer.
984 * @param timeout to wait for a complete input line.
985 * @return number of bytes read, 0 if none, -1 if error.
986 */
987 static ssize_t readline(socket_t socket, char *data, size_t size, timeout_t timeout = Timer::inf);
988
989 /**
990 * Print formatted string to socket.
991 * @param socket to write to.
992 * @param format string.
993 * @return number of bytes sent, -1 if error.
994 */
995 static ssize_t printf(socket_t socket, const char *format, ...) __PRINTF(2,3);
996
997 /**
998 * Write a null terminated string to the socket. This exists because
999 * we messed up consistency with the original puts() method. In the
1000 * future there will be a single puts() that has a NULL default.
1001 * @param string to write.
1002 * @return number of bytes sent, 0 if none, -1 if error.
1003 */
1004 size_t writes(const char *string);
1005
1006 /**
1007 * Test if socket is valid.
1008 * @return true if valid socket.
1009 */
1010 operator bool();
1011
1012 /**
1013 * Test if socket is invalid.
1014 * @return true if socket is invalid.
1015 */
1016 bool operator!() const;
1017
1018 /**
1019 * Assign socket from a socket descriptor. Release existing socket if
1020 * one present.
1021 * @param socket descriptor to assign to object.
1022 */
1023 Socket& operator=(socket_t socket);
1024
1025 /**
1026 * Get the socket descriptor by casting.
1027 * @return socket descriptor of object.
1028 */
1029 inline operator socket_t() const
1030 {return so;};
1031
1032 /**
1033 * Get the socket descriptor by pointer reference.
1034 * @return socket descriptor of object.
1035 */
1036 inline socket_t operator*() const
1037 {return so;};
1038
1039 /**
1040 * Get the number of bytes pending in the receive buffer of a socket
1041 * descriptor.
1042 * @param socket descriptor.
1043 * @return number of pending bytes.
1044 */
1045 static unsigned pending(socket_t socket);
1046
1047 /**
1048 * Set the send size of a socket descriptor.
1049 * @param socket descriptor.
1050 * @param size of send buffer to set.
1051 * @return 0 on success, -1 on error.
1052 */
1053 static int sendsize(socket_t socket, unsigned size);
1054
1055 /**
1056 * Set the size to wait before sending.
1057 * @param socket descriptor.
1058 * @param size of send wait buffer to set.
1059 * @return 0 on success, -1 on error.
1060 */
1061 static int sendwait(socket_t socket, unsigned size);
1062
1063 /**
1064 * Set the receive size of a socket descriptor.
1065 * @param socket descriptor.
1066 * @param size of receive buffer to set.
1067 * @return 0 on success, -1 on error.
1068 */
1069 static int recvsize(socket_t socket, unsigned size);
1070
1071 /**
1072 * Connect socket descriptor to a remote host from an address list.
1073 * For TCP (and DCCP) sockets, the entire list may be tried. For UDP,
1074 * connect is only a state and the first valid entry in the list is used.
1075 * @param socket descriptor.
1076 * @param list of addresses to connect to.
1077 * @return 0 on success, -1 on error.
1078 */
1079 static int connectto(socket_t socket, struct addrinfo *list);
1080
1081 /**
1082 * Disconnect a connected socket descriptor.
1083 * @param socket descriptor.
1084 * @return 0 on success, -1 on error.
1085 */
1086 static int disconnect(socket_t socket);
1087
1088 /**
1089 * Drop socket descriptor from multicast group.
1090 * @param socket descriptor.
1091 * @param list of groups to drop.
1092 * @return 0 on success, -1 on error.
1093 */
1094 static int drop(socket_t socket, const struct addrinfo *list);
1095
1096 /**
1097 * Join socket descriptor to multicast group.
1098 * @param socket descriptor.
1099 * @param list of groups to join.
1100 * @return 0 on success, -1 on error.
1101 */
1102 static int join(socket_t socket, const struct addrinfo *list);
1103
1104 /**
1105 * Get socket error code of socket descriptor.
1106 * @param socket descriptor.
1107 * @return socket error code.
1108 */
1109 static int error(socket_t socket);
1110
1111 /**
1112 * Set multicast mode and multicast broadcast range for socket descriptor.
1113 * @param socket descriptor.
1114 * @param ttl to set for multicast socket or 0 to disable multicast.
1115 * @return 0 if success, -1 if error.
1116 */
1117 static int multicast(socket_t socket, unsigned ttl = 1);
1118
1119 /**
1120 * Set loopback to read multicast packets socket descriptor broadcasts.
1121 * @param socket descriptor.
1122 * @param enable true to loopback, false to ignore.
1123 * @return 0 if success, -1 if error.
1124 */
1125 static int loopback(socket_t socket, bool enable);
1126
1127 /**
1128 * Set socket blocking I/O mode of socket descriptor.
1129 * @param socket descriptor.
1130 * @param enable true for blocking I/O.
1131 * @return 0 if success, -1 if error.
1132 */
1133 static int blocking(socket_t socket, bool enable);
1134
1135 /**
1136 * Set socket for keepalive packets for socket descriptor.
1137 * @param socket descriptor.
1138 * @param enable keep-alive if true.
1139 * @return 0 if success, -1 if error.
1140 */
1141 static int keepalive(socket_t socket, bool enable);
1142
1143 /**
1144 * Set socket for unicast mode broadcasts on socket descriptor.
1145 * @param socket descriptor.
1146 * @param enable broadcasting if true.
1147 * @return 0 if success, -1 if error.
1148 */
1149 static int broadcast(socket_t socket, bool enable);
1150
1151 /**
1152 * Set tcp nodelay option on socket descriptor.
1153 * @param socket descriptor.
1154 * @return 0 if success, -1 if error.
1155 */
1156 static int nodelay(socket_t socket);
1157
1158 /**
1159 * Set packet priority of socket descriptor.
1160 * @param socket descriptor.
1161 * @param scheduling priority for packet scheduling.
1162 * @return 0 on success, -1 on error.
1163 */
1164 static int priority(socket_t socket, int scheduling);
1165
1166 /**
1167 * Set type of service of socket descriptor.
1168 * @param socket descriptor.
1169 * @param type of service.
1170 * @return 0 on success, -1 on error.
1171 */
1172 static int tos(socket_t socket, int type);
1173
1174 /**
1175 * Set the time to live for the socket descriptor.
1176 * @param socket descriptor.
1177 * @param time to live to set.
1178 * @return 0 on success, -1 on error.
1179 */
1180 static int ttl(socket_t socket, unsigned char time);
1181
1182 /**
1183 * Get the address family of the socket descriptor.
1184 * @return address family.
1185 */
1186 static int family(socket_t socket);
1187
1188 /**
1189 * Get the address family of a socket address object.
1190 * @param address to examine.
1191 * @return address family.
1192 */
1193 inline static int family(const struct sockaddr_storage& address)
1194 {return ((const struct sockaddr *)&address)->sa_family;};
1195
1196 /**
1197 * Get the address family of an internet socket address object.
1198 * @param address to examine.
1199 * @return address family.
1200 */
1201 inline static int family(const struct sockaddr_internet& address)
1202 {return address.address.sa_family;};
1203
1204 /**
1205 * Get data waiting in receive queue.
1206 * @param socket to get from.
1207 * @param buffer to save.
1208 * @param size of data buffer to request.
1209 * @param flags for i/o operation (MSG_OOB, MSG_PEEK, etc).
1210 * @param address of source.
1211 * @return number of bytes received, -1 if error.
1212 */
1213 static ssize_t recvfrom(socket_t socket, void *buffer, size_t size, int flags = 0, struct sockaddr_storage *address = NULL);
1214
1215 /**
1216 * Send data on socket.
1217 * @param socket to send to.
1218 * @param buffer to send.
1219 * @param size of data buffer to send.
1220 * @param flags for i/o operation (MSG_OOB, MSG_PEEK, etc).
1221 * @param address of destination, NULL if connected.
1222 * @return number of bytes sent, -1 if error.
1223 */
1224 static ssize_t sendto(socket_t socket, const void *buffer, size_t size, int flags = 0, const struct sockaddr *address = NULL);
1225
1226 /**
1227 * Send reply on socket. Used to reply to a recvfrom message.
1228 * @param socket to send to.
1229 * @param buffer to send.
1230 * @param size of data buffer to send.
1231 * @param flags for i/o operation (MSG_OOB, MSG_PEEK, etc).
1232 * @param address to reply to.
1233 * @return number of bytes sent, -1 if error.
1234 */
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
1238 /**
1239 * Send to internet socket.
1240 * @param socket to send to.
1241 * @param buffer to send.
1242 * @param size of data buffer to send.
1243 * @param flags for i/o operation (MSG_OOB, MSG_PEEK, etc).
1244 * @param address to send to.
1245 * @return number of bytes sent, -1 if error.
1246 */
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
1250 /**
1251 * Get internet data waiting in receive queue.
1252 * @param socket to get from.
1253 * @param buffer to save.
1254 * @param size of data buffer to request.
1255 * @param flags for i/o operation (MSG_OOB, MSG_PEEK, etc).
1256 * @param address of source.
1257 * @return number of bytes received, -1 if error.
1258 */
1259 static ssize_t recvinet(socket_t socket, void *buffer, size_t size, int flags = 0, struct sockaddr_internet *address = NULL);
1260
1261 /**
1262 * Bind the socket descriptor to a known interface and service port.
1263 * @param socket descriptor to bind.
1264 * @param address to bind to or "*" for all.
1265 * @param service port to bind.
1266 * @param protocol to use or 0 if default.
1267 * @return 0 on success, -1 if error.
1268 */
1269 static int bindto(socket_t socket, const char *address, const char *service, int protocol = 0);
1270
1271 /**
1272 * Bind the socket descriptor to a known interface listen on service port.
1273 * @param socket descriptor to bind.
1274 * @param address of interface to bind to.
1275 * @param backlog for service.
1276 * @return 0 on success, -1 if error.
1277 */
1278 static int listento(socket_t socket, const struct sockaddr *address, int backlog = 5);
1279
1280 /**
1281 * Bind the socket descriptor to a known interface.
1282 * @param socket descriptor to bind.
1283 * @param address of interface to bind to.
1284 * @return 0 on success, -1 if error.
1285 */
1286 static int bindto(socket_t socket, const struct sockaddr *address);
1287
1288 /**
1289 * Accept a socket connection from a remote host.
1290 * @param socket descriptor to accept from.
1291 * @param address of socket accepting.
1292 * @return new socket accepted.
1293 */
1294 static socket_t acceptfrom(socket_t socket, struct sockaddr_storage *address = NULL);
1295
1296 /**
1297 * Create a socket object unbound.
1298 * @param family of socket.
1299 * @param type of socket.
1300 * @param protocol of socket.
1301 * @return socket descriptor created or INVALID_SOCKET.
1302 */
1303 static socket_t create(int family, int type, int protocol);
1304
1305 /**
1306 * Create a connected socket.
1307 * @param address list to connect to.
1308 * @param type of socket to create.
1309 * @param protocol of socket.
1310 * @return socket descriptor created or INVALID_SOCKET.
1311 */
1312 static socket_t create(const struct addrinfo *address, int type, int protocol);
1313
1314 /**
1315 * Create a bound socket for a service.
1316 * @param iface to bind.
1317 * @param service port to bind.
1318 * @param family to select or AF_UNSPEC
1319 * @param type of socket to create.
1320 * @param protocol of socket to create.
1321 * @return socket descriptor created or INVALID_SOCKET.
1322 */
1323 static socket_t create(const char *iface, const char *service, int family = AF_UNSPEC, int type = 0, int protocol = 0);
1324
1325 /**
1326 * Create a connected socket for a service.
1327 * @param address of service for connect.
1328 * @return socket descriptor.
1329 */
1330 static socket_t create(const Socket::address &address);
1331
1332 /**
1333 * Release (close) a socket.
1334 * @param socket to close.
1335 */
1336 static void release(socket_t socket);
1337
1338 /**
1339 * Lookup and return the host name associated with a socket address.
1340 * @param address to lookup.
1341 * @param buffer to save hostname into.
1342 * @param size of buffer to save hostname into.
1343 * @return buffer or NULL if lookup fails.
1344 */
1345 static char *hostname(const struct sockaddr *address, char *buffer, size_t size);
1346
1347 /**
1348 * Create an address info lookup hint based on the family and type
1349 * properties of a socket descriptor.
1350 * @param socket descriptor.
1351 * @param hint buffer.
1352 * @return hint buffer.
1353 */
1354 static struct addrinfo *hinting(socket_t socket, struct addrinfo *hint);
1355
1356 /**
1357 * Lookup a host name and service address based on the addressing family
1358 * and socket type of a socket descriptor. Store the result in a socket
1359 * address structure.
1360 * @param socket descriptor.
1361 * @param address that is resolved.
1362 * @param hostname to resolve.
1363 * @param service port.
1364 * @return socket address size.
1365 */
1366 static socklen_t query(socket_t socket, struct sockaddr_storage *address, const char *hostname, const char *service);
1367
1368 /**
1369 * Get the size of a socket address.
1370 * @param address of socket.
1371 * @return size to use for this socket address object.
1372 */
1373 static socklen_t len(const struct sockaddr *address);
1374
1375 /**
1376 * Compare socket addresses. Test if the address and service matches
1377 * or if there is no service, then just the host address values.
1378 * @param address1 to compare.
1379 * @param address2 to compare.
1380 * @return true if same family and equal.
1381 */
1382 static bool equal(const struct sockaddr *address1, const struct sockaddr *address2);
1383
1384 /**
1385 * Copy a socket address.
1386 * @param target address pointer to copy into.
1387 * @param origin address pointer to copy from.
1388 * @return number of bytes copied, 0 if invalid.
1389 */
1390 static unsigned copy(struct sockaddr *target, const struct sockaddr *origin);
1391
1392 /**
1393 * Store an address into an address object.
1394 * @param storage for address.
1395 * @param address to store.
1396 * @return number of bytes stored.
1397 */
1398 inline static unsigned store(struct sockaddr_storage *storage, const struct sockaddr *address)
1399 {return copy((struct sockaddr*)storage, address);};
1400
1401 /**
1402 * Store an address into an internet address object.
1403 * @param storage for address.
1404 * @param address to store.
1405 * @return number of bytes stored.
1406 */
1407 static unsigned store(struct sockaddr_internet *storage, const struct sockaddr *address);
1408
1409 /**
1410 * Compare socket host addresses. Test if the host address matches
1411 * or if there is no service, then just the host address values.
1412 * @param address1 to compare.
1413 * @param address2 to compare.
1414 * @return true if same family and equal.
1415 */
1416 static bool eq_host(const struct sockaddr *address1, const struct sockaddr *address2);
1417
1418 /**
1419 * Compare socket addresses. Test if the stored addresses received match.
1420 * or if there is no service, then just the host address values.
1421 * @param address1 to compare.
1422 * @param address2 to compare.
1423 * @return true if same family and equal.
1424 */
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
1428 /**
1429 * Compare socket addresses. Test if the internet addresses received match.
1430 * or if there is no service, then just the host address values.
1431 * @param address1 to compare.
1432 * @param address2 to compare.
1433 * @return true if same family and equal.
1434 */
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
1438 /**
1439 * See if both addresses are in the same subnet. This is only relevant
1440 * to IPV4 and class domain routing.
1441 * @param address1 to test.
1442 * @param address2 to test.
1443 * @return true if in same subnet.
1444 */
1445 static bool eq_subnet(const struct sockaddr *address1, const struct sockaddr *address2);
1446
1447 /**
1448 * Get the socket address of the interface needed to reach a destination
1449 * address.
1450 * @param address of interface found.
1451 * @param destination address.
1452 * @return 0 on success, -1 on error.
1453 */
1454 static int via(struct sockaddr *address, const struct sockaddr *destination);
1455
1456 /**
1457 * Get the hostname of a socket address.
1458 * @param address to lookup.
1459 * @param buffer to save hostname in.
1460 * @param size of hostname buffer.
1461 * @return buffer if found or NULL if not.
1462 */
1463 static char *query(const struct sockaddr *address, char *buffer, socklen_t size);
1464
1465 /**
1466 * Get the service port of a socket.
1467 * @param address of socket to examine.
1468 * @return service port number.
1469 */
1470 static short service(const struct sockaddr *address);
1471
1472 /**
1473 * Get the service port of an inet socket.
1474 * @param address of internet socket to examine.
1475 * @return service port number.
1476 */
1477 inline static short service(const struct sockaddr_internet *address)
1478 {return service((const struct sockaddr *)address);};
1479
1480 /**
1481 * Convert a socket address and service into a hash map index.
1482 * @param address to convert.
1483 * @param size of map index.
1484 * @return key index path.
1485 */
1486 static unsigned keyindex(const struct sockaddr *address, unsigned size);
1487
1488 /**
1489 * Convert a socket host address into a hash map index.
1490 * @param address to convert.
1491 * @param size of map index.
1492 * @return key index path.
1493 */
1494 static unsigned keyhost(const struct sockaddr *address, unsigned size);
1495
1496 /**
1497 * Initialize socket subsystem.
1498 */
1499 static void init(void);
1500
1501 /**
1502 * Initialize with program name. Used by socks, for example.
1503 * @param program name.
1504 */
1505 static void init(const char *program);
1506
1507 /**
1508 * Set default socket family preference for query options when the
1509 * socket type is otherwise not specified.
1510 * @param family to select.
1511 */
1512 static void query(int family);
1513
1514 /**
1515 * Set the default socket behavior for v6-v4 mapping. This also
1516 * effects v6 address lookup as to whether v4 remapped addresses
1517 * can be used if no v6 address is found.
1518 * @param enable true to set mapping. This is default.
1519 */
1520 static void v4mapping(bool enable);
1521
1522 /**
1523 * Return error code of last socket operation,
1524 * @return errno style error code.
1525 */
1526 static int error(void);
1527
1528 /**
1529 * Simple function to validate that a given IP address string is a "zero"
1530 * address. Such address strings are used for example in SIP to indicate
1531 * "hold" by re-inviting peers to a null address. Supports IPV4 and
1532 * IPV6 addresses.
1533 * @param string address to check.
1534 * @return true if zero/null address.
1535 */
1536 static bool is_null(const char *string);
1537
1538 /**
1539 * Simple function to validate that a given IP address string is a numeric
1540 * address. This can be used to verify an address is not a "host" name.
1541 * Supports IPV4 and IPV6 address strings.
1542 * @param string address to check.
1543 * @return true if zero/null address.
1544 */
1545 static bool is_numeric(const char *string);
1546
1547 /**
1548 * Get local address to which the socket is bound. This is defined here
1549 * because we may re-define the backend linkage for the socks proxy in
1550 * the future.
1551 * @param socket descriptor to examine.
1552 * @param address storage for local address.
1553 * @return 0 on success, -1 on failure.
1554 */
1555 static int local(socket_t socket, struct sockaddr_storage *address);
1556
1557 /**
1558 * Get remote address to which the socket is connected. This is defined
1559 * here because we may re-define the backend linkage for the socks proxy in
1560 * the future.
1561 * @param socket descriptor to examine.
1562 * @param address storage for remote address.
1563 * @return 0 on success, -1 on failure.
1564 */
1565 static int remote(socket_t socket, struct sockaddr_storage *address);
1566};
1567
1568/**
1569 * A bound socket used to listen for inbound socket connections. This class
1570 * is commonly used for TCP and DCCP listener sockets.
1571 * @author David Sugar <dyfet@gnutelephony.org>
1572 */
1573class __EXPORT ListenSocket : protected Socket
1574{
1575public:
1576 /**
1577 * Create and bind a listener socket.
1578 * @param address to bind on or "*" for all.
1579 * @param service port to bind listener.
1580 * @param backlog size for buffering pending connections.
1581 * @param family of socket.
1582 * @param type of socket.
1583 * @param protocol for socket if not TCPIP.
1584 */
1585 ListenSocket(const char *address, const char *service, unsigned backlog = 5, int family = AF_UNSPEC, int type = 0, int protocol = 0);
1586
1587 /**
1588 * Create a listen socket directly.
1589 * @param address to bind on or "*" for all.
1590 * @param service port to bind listener.
1591 * @param backlog size for buffering pending connections.
1592 * @param family of socket.
1593 * @param type of socket.
1594 * @param protocol for socket if not TCPIP.
1595 * @return bound and listened to socket.
1596 */
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
1599 /**
1600 * Accept a socket connection.
1601 * @param address to save peer connecting.
1602 * @return socket descriptor of connected socket.
1603 */
1604 socket_t accept(struct sockaddr_storage *address = NULL) const;
1605
1606 /**
1607 * Wait for a pending connection.
1608 * @param timeout to wait.
1609 * @return true when acceptable connection is pending.
1610 */
1611 inline bool wait(timeout_t timeout = Timer::inf) const
1612 {return Socket::wait(timeout);};
1613
1614 /**
1615 * Get the socket descriptor of the listener.
1616 * @return socket descriptor.
1617 */
1618 inline operator socket_t() const
1619 {return so;}
1620
1621 /**
1622 * Get the socket descriptor of the listener by pointer reference.
1623 * @return socket descriptor.
1624 */
1625 inline socket_t operator*() const
1626 {return so;}
1627
1628 /**
1629 * Get the socket descriptor of the listener.
1630 * @return socket descriptor.
1631 */
1632 inline socket_t getsocket(void) const
1633 {return so;}
1634
1635 inline socket_t handle(void) const
1636 {return so;}
1637
1638};
1639
1640/**
1641 * A generic tcp server class. This saves the service id tag so that it
1642 * can be propagated.
1643 * @author David Sugar <dyfet@gnutelephony.org>
1644 */
1645class __EXPORT TCPServer : public ListenSocket
1646{
1647public:
1648 /**
1649 * Create and bind a tcp server. This mostly is used to preserve the
1650 * service tag for TCP Socket when derived from a server instance.
1651 * @param service tag to use.
1652 * @param address of interface to bind or "*" for all.
1653 * @param backlog size for pending connections.
1654 */
1655 TCPServer(const char *address, const char *service, unsigned backlog = 5);
1656};
1657
1658/**
1659 * Helper function for linked_pointer<struct sockaddr>.
1660 */
1661__EXPORT struct addrinfo *_nextaddrinfo(struct addrinfo *addrinfo);
1662
1663/**
1664 * Helper function for linked_pointer<struct sockaddr>.
1665 */
1666__EXPORT struct sockaddr *_getaddrinfo(struct addrinfo *addrinfo);
1667
1668/**
1669 * Helper function for linked_pointer<struct sockaddr>.
1670 */
1671__EXPORT socket_t _getaddrsock(struct addrinfo *addrinfo);
1672
1673/**
1674 * Linked pointer for address lists. This can be used to iterate through
1675 * the list of a Socket::address object using the linked_pointer method.
1676 * @author David Sugar <dyfet@gnutelephony.org>
1677 */
1678template <>
1679class linked_pointer<sockaddr_struct>
1680{
1681private:
1682 struct addrinfo *ptr;
1683
1684public:
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
1694 /**
1695 * Get the full socket address list by casted reference.
1696 * @return addrinfo list we resolved or NULL if none.
1697 */
1698 inline operator struct sockaddr *() const
1699 {return _getaddrinfo(ptr);};
1700
1701 /**
1702 * Return the full socket address list by pointer reference.
1703 * @return addrinfo list we resolved or NULL if none.
1704 */
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
1722 /**
1723 * Get socket as expression operator.
1724 */
1725 inline socket_t operator()(void) const
1726 {return _getaddrsock(ptr);};
1727
1728 /**
1729 * Test if the address list is valid.
1730 * @return true if we have an address list.
1731 */
1732 inline operator bool() const
1733 {return ptr != NULL;};
1734
1735 /**
1736 * Assign our pointer from an address list.
1737 * @param pointer of linked list.
1738 */
1739 inline void operator=(struct addrinfo *list)
1740 {ptr = list;};
1741
1742 /**
1743 * Assign our pointer from an address list.
1744 * @param pointer of linked list.
1745 */
1746 inline void operator=(Socket::address& list)
1747 {ptr = list.getList();};
1748
1749 /**
1750 * Assign our pointer from an address list.
1751 * @param pointer of linked list.
1752 */
1753 inline void set(struct addrinfo *list)
1754 {ptr = list;};
1755
1756 /**
1757 * Assign our pointer from an address list.
1758 * @param pointer of linked list.
1759 */
1760 inline void set(Socket::address& list)
1761 {ptr = list.getList();};
1762
1763
1764 /**
1765 * Return member from typed object our pointer references.
1766 * @return evaluated member of object we point to.
1767 */
1768 inline struct sockaddr* operator->() const
1769 {return _getaddrinfo(ptr);};
1770
1771 /**
1772 * Test if we have no address list.
1773 * @return true if we have no address list.
1774 */
1775 inline bool operator!() const
1776 {return ptr == NULL;};
1777
1778 inline void next(void)
1779 {ptr = _nextaddrinfo(ptr);};
1780};
1781
1782/**
1783 * A convenience function to convert a socket address list into an addrinfo.
1784 * @param address list object.
1785 * @return addrinfo list or NULL if empty.
1786 */
1787inline struct addrinfo *addrinfo(Socket::address& address)
1788 {return address.getList();}
1789
1790/**
1791 * A convenience function to convert a socket address list into a socket
1792 * address.
1793 * @param address list object.
1794 * @return first socket address in list or NULL if empty.
1795 */
1796inline struct sockaddr *addr(Socket::address& address)
1797 {return address.get();}
1798
1799/**
1800 * Compare two socket addresses to see if equal. If the port is zero
1801 * then this is the same as comparing host address alone.
1802 * @param s1 socket address to compare.
1803 * @param s2 socket address to compare.
1804 * @return true if addresses same.
1805 */
1806inline bool eq(const struct sockaddr *s1, const struct sockaddr *s2)
1807 {return Socket::equal(s1, s2);}
1808
1809/**
1810 * Compare two stored socket addresses to see if equal. If the port is zero
1811 * then this is the same as comparing host address alone.
1812 * @param s1 stored socket address to compare.
1813 * @param s2 stored socket address to compare.
1814 * @return true if addresses same.
1815 */
1816inline 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
1819/**
1820 * Compare two host addresses to see if equal. The port numbers are
1821 * ignored.
1822 * @param s1 socket address to compare.
1823 * @param s2 socket address to compare.
1824 * @return true if addresses same.
1825 */
1826inline bool eq_host(const struct sockaddr *s1, const struct sockaddr *s2)
1827 {return Socket::eq_host(s1, s2);}
1828
1829inline bool eq_subnet(const struct sockaddr *s1, const struct sockaddr *s2)
1830 {return Socket::eq_subnet(s1, s2);}
1831
1832String str(Socket& so, strsize_t size);
1833
1834typedef TCPServer tcpserv_t;
1835
1836END_NAMESPACE
1837
1838#endif