ucommon
socket.h
Go to the documentation of this file.
1 // Copyright (C) 1999-2005 Open Source Telecom Corporation.
2 // Copyright (C) 2006-2014 David Sugar, Tycho Softworks.
3 //
4 // This program is free software; you can redistribute it and/or modify
5 // it under the terms of the GNU General Public License as published by
6 // the Free Software Foundation; either version 2 of the License, or
7 // (at your option) any later version.
8 //
9 // This program is distributed in the hope that it will be useful,
10 // but WITHOUT ANY WARRANTY; without even the implied warranty of
11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 // GNU General Public License for more details.
13 //
14 // You should have received a copy of the GNU Lesser General Public License
15 // along with this program. If not, see <http://www.gnu.org/licenses/>.
16 //
17 // As a special exception, you may use this file as part of a free software
18 // library without restriction. Specifically, if other files instantiate
19 // templates or use macros or inline functions from this file, or you compile
20 // this file and link it with other files to produce an executable, this
21 // file does not by itself cause the resulting executable to be covered by
22 // the GNU General Public License. This exception does not however
23 // invalidate any other reasons why the executable file might be covered by
24 // the GNU General Public License.
25 //
26 // This exception applies only to the code released under the name GNU
27 // Common C++. If you copy code from other releases into a copy of GNU
28 // Common C++, as the General Public License permits, the exception does
29 // not apply to the code that you add in this way. To avoid misleading
30 // anyone as to the status of such modified files, you must delete
31 // this exception notice from them.
32 //
33 // If you write modifications of your own for GNU Common C++, it is your choice
34 // whether to permit this exception to apply to your modifications.
35 // If you do not wish that, delete this exception notice.
36 //
37 
43 #ifndef COMMONCPP_SOCKET_H_
44 #define COMMONCPP_SOCKET_H_
45 
46 #include <cstdio>
47 
48 #ifndef COMMONCPP_CONFIG_H_
49 #include <commoncpp/config.h>
50 #endif
51 
52 #ifndef COMMONCPP_STRING_H_
53 #include <commoncpp/string.h>
54 #endif
55 
56 #ifndef COMMONCPP_ADDRESS_H_
57 #include <commoncpp/address.h>
58 #endif
59 
60 #ifndef COMMONCPP_EXCEPTION_H_
61 #include <commoncpp/exception.h>
62 #endif
63 
64 #ifndef MSG_DONTWAIT
65 #define MSG_DONTWAIT 0
66 #endif
67 
68 #ifndef MSG_NOSIGNAL
69 #define MSG_NOSIGNAL 0
70 #endif
71 
72 #ifndef SOCK_DCCP
73 #define SOCK_DCCP 6
74 #endif
75 #ifndef IPPROTO_DCCP
76 #define IPPROTO_DCCP 33
77 #endif
78 #ifndef SOL_DCCP
79 #define SOL_DCCP 269
80 #endif
81 #define DCCP_SOCKOPT_AVAILABLE_CCIDS 12
82 #define DCCP_SOCKOPT_CCID 13
83 #define DCCP_SOCKOPT_TX_CCID 14
84 #define DCCP_SOCKOPT_RX_CCID 15
85 
86 NAMESPACE_COMMONCPP
87 
88 typedef socket_t SOCKET;
89 
90 class __EXPORT Socket : protected ucommon::Socket
91 {
92 public:
93  enum State {
94  INITIAL,
95  AVAILABLE,
96  BOUND,
97  CONNECTED,
98  CONNECTING,
99  STREAM
100  };
101  typedef enum State State;
102 
103  enum Family {
104 #ifdef CCXX_IPV6
105  IPV6 = AF_INET6,
106 #endif
107  IPV4 = AF_INET
108  };
109 
110  typedef enum Family Family;
111 
112  enum Error {
113  errSuccess = 0,
114  errCreateFailed,
115  errCopyFailed,
116  errInput,
117  errInputInterrupt,
118  errResourceFailure,
119  errOutput,
120  errOutputInterrupt,
121  errNotConnected,
122  errConnectRefused,
123  errConnectRejected,
124  errConnectTimeout,
125  errConnectFailed,
126  errConnectInvalid,
127  errConnectBusy,
128  errConnectNoRoute,
129  errBindingFailed,
130  errBroadcastDenied,
131  errRoutingDenied,
132  errKeepaliveDenied,
133  errServiceDenied,
134  errServiceUnavailable,
135  errMulticastDisabled,
136  errTimeout,
137  errNoDelay,
138  errExtended,
139  errLookupFail,
140  errSearchErr,
141  errInvalidValue
142  };
143 
144  typedef enum Error Error;
145 
146  enum Tos {
147  tosLowDelay = 0,
148  tosThroughput,
149  tosReliability,
150  tosMinCost,
151  tosInvalid
152  };
153  typedef enum Tos Tos;
154 
155  enum Pending {
156  pendingInput,
157  pendingOutput,
158  pendingError
159  };
160  typedef enum Pending Pending;
161 
162 private:
163  // used by exception handlers....
164  mutable Error errid;
165  mutable const char *errstr;
166  mutable long syserr;
167 
168  void setSocket(void);
169 
170 protected:
171  static socket_t dupSocket(socket_t s,Socket::State state);
172 
173  static Mutex mutex;
174 
175  mutable struct {
176  bool thrown: 1;
177  bool broadcast: 1;
178  bool route: 1;
179  bool keepalive: 1;
180  bool loopback: 1;
181  bool multicast: 1;
182  bool completion: 1;
183  bool linger: 1;
184  unsigned ttl: 8;
185  } flags;
186 
187  State volatile state;
188 
197  Error error(Error error, const char *err = NULL, long systemError = 0) const;
198 
205  inline void error(const char *err) const
206  {error(errExtended, err);};
207 
214  inline void setError(bool enable)
215  {flags.thrown = !enable;};
216 
222  void endSocket(void);
223 
229  Error connectError(void);
230 
234  Error sendLimit(int limit = 2048);
235 
239  Error receiveLimit(int limit = 1);
240 
247  Error sendTimeout(timeout_t timer);
248 
255  Error receiveTimeout(timeout_t timer);
256 
264  Error sendBuffer(unsigned size);
265 
273  Error receiveBuffer(unsigned size);
274 
282  Error bufferSize(unsigned size);
283 
292  Error setBroadcast(bool enable);
293 
305  Error setMulticastByFamily(bool enable, Family family = IPV4);
306 
315  Error setLoopbackByFamily(bool enable, Family family = IPV4);
316 
324  Error setTimeToLiveByFamily(unsigned char ttl, Family fam = IPV4);
325 
332  Error join(const IPV4Multicast &ia);
333 #ifdef CCXX_IPV6
334  Error join(const IPV6Multicast &ia);
335 #endif
336 
343  Error drop(const IPV4Multicast &ia);
344 #ifdef CCXX_IPV6
345  Error drop(const IPV6Multicast &ia);
346 #endif
347 
355  Error setRouting(bool enable);
356 
363  Error setNoDelay(bool enable);
364 
376  Socket(int domain, int type, int protocol = 0);
377 
385  Socket(socket_t fd);
386 
390  Socket();
391 
399  Socket(const Socket &source);
400 
410  ssize_t readLine(char *buf, size_t len, timeout_t timeout = 0);
411 
423  virtual ssize_t readData(void * buf,size_t len,char separator=0,timeout_t t=0);
424 
433  virtual ssize_t writeData(const void* buf,size_t len,timeout_t t=0);
434 
435 public:
436  ~Socket();
437 
444  inline Error getErrorNumber(void) const {return errid;}
445 
452  inline const char *getErrorString(void) const {return errstr;}
453 
454  inline long getSystemError(void) const {return syserr;}
455 
456  const char *getSystemErrorString(void) const;
457 
467  virtual bool isPending(Pending pend, timeout_t timeout = TIMEOUT_INF);
468 
475  static bool check(Family fam);
476 
481  bool operator!() const;
482 
483  operator bool() const;
484 
488  Socket &operator=(const Socket &from);
489 
499  virtual IPV4Host getIPV4Sender(tpport_t *port = NULL) const;
500 
501  inline IPV4Host getSender(tpport_t *port = NULL) const
502  {return getIPV4Sender(port);}
503 
504 #ifdef CCXX_IPV6
505  virtual IPV6Host getIPV6Sender(tpport_t *port = NULL) const;
506 #endif
507 
517  IPV4Host getIPV4Peer(tpport_t *port = NULL) const;
518 
519  inline IPV4Host getPeer(tpport_t *port = NULL) const
520  {return getIPV4Peer(port);}
521 
522 #ifdef CCXX_IPV6
523  IPV6Host getIPV6Peer(tpport_t *port = NULL) const;
524 #endif
525 
533  IPV4Host getIPV4Local(tpport_t *port = NULL) const;
534 
535  inline IPV4Host getLocal(tpport_t *port = NULL) const
536  {return getIPV4Local(port);}
537 
538 #ifdef CCXX_IPV6
539  IPV6Host getIPV6Local(tpport_t *port = NULL) const;
540 #endif
541 
552  void setCompletion(bool immediate);
553 
559  Error setLinger(bool linger);
560 
568  Error setKeepAlive(bool enable);
569 
578  Error setTypeOfService(Tos service);
579 
588  bool isConnected(void) const;
589 
597  bool isActive(void) const;
598 
605  inline bool isBroadcast(void) const
606  {return flags.broadcast;};
607 
613  inline bool isRouted(void) const
614  {return flags.route;};
615 
616 
617  inline struct in_addr getaddress(const IPV4Address &ia)
618  {return ia.getAddress();}
619 
620 #ifdef CCXX_IPV6
621  inline struct in6_addr getaddress(const IPV6Address &ia)
622  {return ia.getAddress();}
623 #endif
624 
625 };
626 
627 #if defined(CCXX_EXCEPTIONS)
628 
629 class __EXPORT SockException : public IOException
630 {
631 private:
632  Socket::Error _socketError;
633 
634 public:
635  inline SockException(const String &str, Socket::Error socketError, long systemError = 0) :
636  IOException(str, systemError), _socketError(socketError) {};
637 
638  inline Socket::Error getSocketError() const
639  { return _socketError; }
640 };
641 
642 #endif
643 
644 END_NAMESPACE
645 
646 #endif
int join(const struct addrinfo *list)
Join socket to multicast group.
Socket & operator=(socket_t socket)
Assign socket from a socket descriptor.
static int error(void)
Return error code of last socket operation,.
GNU Common C++ exception model base classes.
A generic socket base class.
Definition: socket.h:310
Common C++ generic string class.
virtual ~Socket()
Shutdown, close, and destroy socket.
T &() limit(T &value, T &low, T &high)
Convenience macro to range restrict values.
Definition: generics.h:568
Socket()
Create a socket object for use.
int drop(const struct addrinfo *list)
Drop socket from multicast group.
unsigned long timeout_t
Typedef for millisecond timer values.
Definition: platform.h:326
bool operator!() const
Test if socket is invalid.
Network addresses and sockets related classes.