ucommon
udp.h
Go to the documentation of this file.
1 // Copyright (C) 1999-2005 Open Source Telecom Corporation.
2 // Copyright (C) 2006-2010 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 General Public License
15 // along with this program; if not, write to the Free Software
16 // Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
17 //
18 // As a special exception, you may use this file as part of a free software
19 // library without restriction. Specifically, if other files instantiate
20 // templates or use macros or inline functions from this file, or you compile
21 // this file and link it with other files to produce an executable, this
22 // file does not by itself cause the resulting executable to be covered by
23 // the GNU General Public License. This exception does not however
24 // invalidate any other reasons why the executable file might be covered by
25 // the GNU General Public License.
26 //
27 // This exception applies only to the code released under the name GNU
28 // Common C++. If you copy code from other releases into a copy of GNU
29 // Common C++, as the General Public License permits, the exception does
30 // not apply to the code that you add in this way. To avoid misleading
31 // anyone as to the status of such modified files, you must delete
32 // this exception notice from them.
33 //
34 // If you write modifications of your own for GNU Common C++, it is your choice
35 // whether to permit this exception to apply to your modifications.
36 // If you do not wish that, delete this exception notice.
37 //
38 
44 #ifndef COMMONCPP_UDP_H_
45 #define COMMONCPP_UDP_H_
46 
47 #include <cstdio>
48 
49 #ifndef COMMONCPP_CONFIG_H_
50 #include <commoncpp/config.h>
51 #endif
52 
53 #ifndef COMMONCPP_STRING_H_
54 #include <commoncpp/string.h>
55 #endif
56 
57 #ifndef COMMONCPP_ADDRESS_H_
58 #include <commoncpp/address.h>
59 #endif
60 
61 #ifndef COMMONCPP_SOCKET_H_
62 #include <commoncpp/socket.h>
63 #endif
64 
65 NAMESPACE_COMMONCPP
66 
99 class __EXPORT UDPSocket : public Socket
100 {
101 private:
102  inline Error setKeepAlive(bool enable)
103  {return Socket::setKeepAlive(enable);};
104 
105 protected:
106 #ifdef CCXX_IPV6
107  union {
108  struct sockaddr_in6 ipv6;
109  struct sockaddr_in ipv4;
110  } peer;
111 #else
112  union {
113  struct sockaddr_in ipv4;
114  } peer;
115 #endif
116 
117  Family family;
118 
119 public:
123  UDPSocket(Family family = IPV4);
124 
128  UDPSocket(const char *name, Family family = IPV4);
129 
139  UDPSocket(const IPV4Address &bind, tpport_t port);
140 #ifdef CCXX_IPV6
141  UDPSocket(const IPV6Address &bind, tpport_t port);
142 #endif
143 
147  virtual ~UDPSocket();
148 
152  inline Error setLoopback(bool enable)
153  {return Socket::setLoopbackByFamily(enable, family);}
154 
158  inline Error setMulticast(bool enable)
159  {return Socket::setMulticastByFamily(enable, family);}
160 
164  inline Error setTimeToLive(char ttl)
165  {return Socket::setTimeToLiveByFamily(ttl, family);}
166 
174  void setPeer(const IPV4Host &host, tpport_t port);
175  void connect(const IPV4Host &host, tpport_t port);
176 #ifdef CCXX_IPV6
177  void setPeer(const IPV6Host &host, tpport_t port);
178  void connect(const IPV6Host &host, tpport_t port);
179 #endif
180 
188  Socket::Error getInterfaceIndex(const char *ethX,int& InterfaceIndex);
189 
198  Socket::Error join(const IPV4Multicast &ia,int InterfaceIndex);
199 
207  ssize_t send(const void *buf, size_t len);
208 
217  ssize_t receive(void *buf, size_t len, bool reply = false);
218 
227  IPV4Host getIPV4Peer(tpport_t *port = NULL) const;
228  inline IPV4Host getPeer(tpport_t *port = NULL) const
229  {return getIPV4Peer(port);}
230 
231 #ifdef CCXX_IPV6
232  IPV6Host getIPV6Peer(tpport_t *port = NULL) const;
233 #endif
234 
242  inline ssize_t peek(void *buf, size_t len)
243  {return ::recv(so, (char *)buf, len, MSG_PEEK);};
244 
248  void setPeer(const char *service);
249  void connect(const char *service);
250 
255  Error disconnect(void);
256 };
257 
266 class __EXPORT UDPBroadcast : public UDPSocket
267 {
268 private:
269  void setPeer(const IPV4Host &ia, tpport_t port);
270 
271  Error setBroadcast(bool enable)
272  {return Socket::setBroadcast(enable);};
273 
274 public:
281  UDPBroadcast(const IPV4Address &ia, tpport_t port);
282 
289  void setPeer(const IPV4Broadcast &subnet, tpport_t port);
290 };
291 
300 class __EXPORT UDPTransmit : protected UDPSocket
301 {
302 private:
310  Error cConnect(const IPV4Address &ia, tpport_t port);
311 
312 protected:
316  UDPTransmit(Family family = IPV4);
317 
329  UDPTransmit(const IPV4Address &bind, tpport_t port = 5005);
330 #ifdef CCXX_IPV6
331  UDPTransmit(const IPV6Address &bind, tpport_t port = 5005);
332 #endif
333 
343  Error connect(const IPV4Host &host, tpport_t port);
344 #ifdef CCXX_IPV6
345  Error connect(const IPV6Address &host, tpport_t port);
346 #endif
347 
357  Error connect(const IPV4Broadcast &subnet, tpport_t port);
358 
366  Error connect(const IPV4Multicast &mgroup, tpport_t port);
367 #ifdef CCXX_IPV6
368  Error connect(const IPV6Multicast &mgroup, tpport_t port);
369 #endif
370 
378  inline ssize_t send(const void *buf, size_t len)
379  {return ::send(so, (const char *)buf, len, MSG_NOSIGNAL);}
380 
384  inline void endTransmitter(void)
385  {Socket::endSocket();}
386 
387  /*
388  * Get transmitter socket.
389  *
390  * @return transmitter.
391  */
392  inline SOCKET getTransmitter(void)
393  {return so;};
394 
395  inline Error setMulticast(bool enable)
396  {return Socket::setMulticastByFamily(enable, family);}
397 
398  inline Error setTimeToLive(unsigned char ttl)
399  {return Socket::setTimeToLiveByFamily(ttl, family);};
400 
401 public:
411  inline ssize_t transmit(const char *buffer, size_t len)
412  {return ::send(so, buffer, len, MSG_DONTWAIT|MSG_NOSIGNAL);}
413 
420  inline bool isOutputReady(unsigned long timeout = 0l)
421  {return Socket::isPending(Socket::pendingOutput, timeout);};
422 
423 
424  inline Error setRouting(bool enable)
425  {return Socket::setRouting(enable);};
426 
427  inline Error setTypeOfService(Tos tos)
428  {return Socket::setTypeOfService(tos);};
429 
430  inline Error setBroadcast(bool enable)
431  {return Socket::setBroadcast(enable);};
432 };
433 
442 class __EXPORT UDPReceive : protected UDPSocket
443 {
444 protected:
455  UDPReceive(const IPV4Address &bind, tpport_t port);
456 #ifdef CCXX_IPV6
457  UDPReceive(const IPV6Address &bind, tpport_t port);
458 #endif
459 
469  Error connect(const IPV4Host &host, tpport_t port);
470 #ifdef CCXX_IPV6
471  Error connect(const IPV6Host &host, tpport_t port);
472 #endif
473 
480  bool isPendingReceive(timeout_t timeout)
481  {return Socket::isPending(Socket::pendingInput, timeout);};
482 
486  inline void endReceiver(void)
487  {Socket::endSocket();}
488 
489  inline SOCKET getReceiver(void) const
490  {return so;};
491 
492  inline Error setRouting(bool enable)
493  {return Socket::setRouting(enable);}
494 
495  inline Error setMulticast(bool enable)
496  {return Socket::setMulticastByFamily(enable, family);}
497 
498  inline Error join(const IPV4Multicast &ia)
499  {return Socket::join(ia);}
500 
501 #ifdef CCXX_IPV6
502  inline Error join(const IPV6Multicast &ia)
503  {return Socket::join(ia);}
504 #endif
505 
506  inline Error drop(const IPV4Multicast &ia)
507  {return Socket::drop(ia);}
508 
509 #ifdef CCXX_IPV6
510  inline Error drop(const IPV6Multicast &ia)
511  {return Socket::drop(ia);}
512 #endif
513 
514 public:
522  inline ssize_t receive(void *buf, size_t len)
523  {return ::recv(so, (char *)buf, len, 0);};
524 
531  inline bool isInputReady(timeout_t timeout = TIMEOUT_INF)
532  {return Socket::isPending(Socket::pendingInput, timeout);};
533 };
534 
545 class __EXPORT UDPDuplex : public UDPTransmit, public UDPReceive
546 {
547 public:
555  UDPDuplex(const IPV4Address &bind, tpport_t port);
556 #ifdef CCXX_IPV6
557  UDPDuplex(const IPV6Address &bind, tpport_t port);
558 #endif
559 
569  Error connect(const IPV4Host &host, tpport_t port);
570 #ifdef CCXX_IPV6
571  Error connect(const IPV6Host &host, tpport_t port);
572 #endif
573 
580  Error disconnect(void);
581 };
582 
583 END_NAMESPACE
584 
585 #endif