UCommon
udp.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 // Copyright (C) 2015 Cherokees of Idaho.
4 //
5 // This program is free software; you can redistribute it and/or modify
6 // it under the terms of the GNU General Public License as published by
7 // the Free Software Foundation; either version 2 of the License, or
8 // (at your option) any later version.
9 //
10 // This program 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 General Public License for more details.
14 //
15 // You should have received a copy of the GNU Lesser General Public License
16 // along with this program. If not, see <http://www.gnu.org/licenses/>.
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 ost {
66 
99 class __EXPORT UDPSocket : public Socket
100 {
101 private:
102  inline Error setKeepAlive(bool enable)
103  {return Socket::setKeepAlive(enable);}
104 
105  __DELETE_COPY(UDPSocket);
106 
107 protected:
108  Socket::address peer;
109 
110  Family family;
111 
112 public:
116  UDPSocket(Family family = IPV4);
117 
121  UDPSocket(const char *name, Family family = IPV4);
122 
132  UDPSocket(const ucommon::Socket::address &bind);
133  UDPSocket(const IPV4Address &bind, tpport_t port);
134 #ifdef CCXX_IPV6
135  UDPSocket(const IPV6Address &bind, tpport_t port);
136 #endif
137 
141  virtual ~UDPSocket();
142 
146  inline Error setLoopback(bool enable)
147  {return Socket::setLoopbackByFamily(enable, family);}
148 
152  inline Error setMulticast(bool enable)
153  {return Socket::setMulticastByFamily(enable, family);}
154 
158  inline Error setTimeToLive(char ttl)
159  {return Socket::setTimeToLiveByFamily(ttl, family);}
160 
168  void setPeer(const ucommon::Socket::address &host);
169  void connect(const ucommon::Socket::address &host);
170 
171  void setPeer(const IPV4Host &host, tpport_t port);
172  void connect(const IPV4Host &host, tpport_t port);
173 #ifdef CCXX_IPV6
174  void setPeer(const IPV6Host &host, tpport_t port);
175  void connect(const IPV6Host &host, tpport_t port);
176 #endif
177 
185  Socket::Error getInterfaceIndex(const char *ethX,int& InterfaceIndex);
186 
195  Socket::Error join(const ucommon::Socket::address &ia, int InterfaceIndex=0);
196  Socket::Error join(const IPV4Multicast &ia,int InterfaceIndex);
197 
205  ssize_t send(const void *buf, size_t len);
206 
215  ssize_t receive(void *buf, size_t len, bool reply = false);
216 
225  ucommon::Socket::address getPeer();
226 
227  IPV4Host getIPV4Peer(tpport_t *port = NULL);
228  inline IPV4Host getPeer(tpport_t *port)
229  {return getIPV4Peer(port);}
230 
231 #ifdef CCXX_IPV6
232  IPV6Host getIPV6Peer(tpport_t *port = NULL);
233 #endif
234 
242  inline ssize_t peek(void *buf, size_t len)
243  {return ::recv(so, (char *)buf, (socksize_t)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  __DELETE_COPY(UDPBroadcast);
275 
276 public:
283  UDPBroadcast(const IPV4Address &ia, tpport_t port);
284 
291  void setPeer(const IPV4Broadcast &subnet, tpport_t port);
292 };
293 
302 class __EXPORT UDPTransmit : protected UDPSocket
303 {
304 private:
312  Error cConnect(const IPV4Address &ia, tpport_t port);
313 
314  __DELETE_COPY(UDPTransmit);
315 
316 protected:
320  UDPTransmit(Family family = IPV4);
321 
335 
336  UDPTransmit(const IPV4Address &bind, tpport_t port = 5005);
337 #ifdef CCXX_IPV6
338  UDPTransmit(const IPV6Address &bind, tpport_t port = 5005);
339 #endif
340 
350  Error connect(const ucommon::Socket::address &host);
351 
352  Error connect(const IPV4Host &host, tpport_t port);
353 #ifdef CCXX_IPV6
354  Error connect(const IPV6Address &host, tpport_t port);
355 #endif
356 
366  Error connect(const IPV4Broadcast &subnet, tpport_t port);
367 
375  Error connect(const IPV4Multicast &mgroup, tpport_t port);
376 #ifdef CCXX_IPV6
377  Error connect(const IPV6Multicast &mgroup, tpport_t port);
378 #endif
379 
387  inline ssize_t send(const void *buf, size_t len)
388  {return ::send(so, (const char *)buf, (socksize_t)len, MSG_NOSIGNAL);}
389 
393  inline void endTransmitter(void)
394  {Socket::endSocket();}
395 
396  /*
397  * Get transmitter socket.
398  *
399  * @return transmitter.
400  */
401  inline SOCKET getTransmitter(void)
402  {return so;};
403 
404  inline Error setMulticast(bool enable)
405  {return Socket::setMulticastByFamily(enable, family);}
406 
407  inline Error setTimeToLive(uint8_t ttl)
408  {return Socket::setTimeToLiveByFamily(ttl, family);}
409 
410 public:
420  inline ssize_t transmit(const char *buffer, size_t len)
421  {return ::send(so, buffer, (socksize_t)len, MSG_DONTWAIT|MSG_NOSIGNAL);}
422 
429  inline bool isOutputReady(unsigned long timeout = 0l) {
430  return Socket::isPending(Socket::pendingOutput, timeout);
431  }
432 
433 
434  inline Error setRouting(bool enable)
435  {return Socket::setRouting(enable);}
436 
437  inline Error setTypeOfService(Tos tos)
438  {return Socket::setTypeOfService(tos);}
439 
440  inline Error setBroadcast(bool enable)
441  {return Socket::setBroadcast(enable);}
442 };
443 
452 class __EXPORT UDPReceive : protected UDPSocket
453 {
454 private:
455  __DELETE_COPY(UDPReceive);
456 
457 protected:
468  UDPReceive(const IPV4Address &bind, tpport_t port);
469 #ifdef CCXX_IPV6
470  UDPReceive(const IPV6Address &bind, tpport_t port);
471 #endif
472 
482  Error connect(const ucommon::Socket::address &host);
483  Error connect(const IPV4Host &host, tpport_t port);
484 #ifdef CCXX_IPV6
485  Error connect(const IPV6Host &host, tpport_t port);
486 #endif
487 
494  bool isPendingReceive(timeout_t timeout) {
495  return Socket::isPending(Socket::pendingInput, timeout);
496  }
497 
501  inline void endReceiver(void)
502  {Socket::endSocket();}
503 
504  inline SOCKET getReceiver(void) const
505  {return so;}
506 
507  inline Error setRouting(bool enable)
508  {return Socket::setRouting(enable);}
509 
510  inline Error setMulticast(bool enable)
511  {return Socket::setMulticastByFamily(enable, family);}
512 
513  inline Error join(const ucommon::Socket::address &ia)
514  {return Socket::join(ia);}
515 
516  inline Error join(const IPV4Multicast &ia)
517  {return Socket::join(ia);}
518 
519 #ifdef CCXX_IPV6
520  inline Error join(const IPV6Multicast &ia)
521  {return Socket::join(ia);}
522 #endif
523 
524  inline Error drop(const IPV4Multicast &ia)
525  {return Socket::drop(ia);}
526 
527 #ifdef CCXX_IPV6
528  inline Error drop(const IPV6Multicast &ia)
529  {return Socket::drop(ia);}
530 #endif
531 
532 public:
540  inline ssize_t receive(void *buf, size_t len)
541  {return ::recv(so, (char *)buf, (socksize_t)len, 0);}
542 
549  inline bool isInputReady(timeout_t timeout = TIMEOUT_INF) {
550  return Socket::isPending(Socket::pendingInput, timeout);
551  }
552 };
553 
564 class __EXPORT UDPDuplex : public UDPTransmit, public UDPReceive
565 {
566 private:
567  __DELETE_COPY(UDPDuplex);
568 
569 public:
577  UDPDuplex(const ucommon::Socket::address &bind);
578  UDPDuplex(const IPV4Address &bind, tpport_t port);
579 #ifdef CCXX_IPV6
580  UDPDuplex(const IPV6Address &bind, tpport_t port);
581 #endif
582 
592  Error connect(const ucommon::Socket::address &host);
593  Error connect(const IPV4Host &host, tpport_t port);
594 #ifdef CCXX_IPV6
595  Error connect(const IPV6Host &host, tpport_t port);
596 #endif
597 
604  Error disconnect(void);
605 };
606 
607 } // namespace ost
608 
609 #endif
Representing half of a two-way UDP connection, the UDP receiver can receive data from another peer ho...
Definition: udp.h:452
UDP sockets implement the TCP SOCK_DGRAM UDP protocol.
Definition: udp.h:99
ssize_t peek(void *buf, size_t len)
Examine contents of next waiting packet.
Definition: udp.h:242
This object is used to hold the actual and valid internet address of a specific host machine that wil...
Definition: address.h:578
bool isInputReady(timeout_t timeout=ucommon::Timer::inf)
See if input queue has data packets available.
Definition: udp.h:549
Representing half of a two-way UDP connection, the UDP transmitter can broadcast data to another sele...
Definition: udp.h:302
The network name and address objects are all derived from a common IPV6Address base class.
Definition: address.h:777
ssize_t send(const void *buf, size_t len)
Transmit "send" to use "connected" send rather than sendto.
Definition: udp.h:387
A specialization of IPV4Address that provides address validation for multicast addresses.
Definition: address.h:651
Error setTimeToLive(char ttl)
Set time to live.
Definition: udp.h:158
Error setLoopback(bool enable)
Set the loopback.
Definition: udp.h:146
This object is used to hold the actual and valid internet address of a specific host machine that wil...
Definition: address.h:981
Network addresses and sockets related classes.
void endReceiver(void)
End receiver.
Definition: udp.h:501
A generic socket address class.
Definition: socket.h:364
ssize_t receive(void *buf, size_t len)
Receive a data packet from the connected peer host.
Definition: udp.h:540
A specialization of IPV6Address that provides address validation for multicast addresses.
Definition: address.h:1043
in_port_t tpport_t
Transport Protocol Ports.
Definition: address.h:80
bool isOutputReady(unsigned long timeout=0l)
See if output queue is empty for sending more packets.
Definition: udp.h:429
ssize_t transmit(const char *buffer, size_t len)
Transmit "send" to use "connected" send rather than sendto.
Definition: udp.h:420
Common C++ generic string class.
Error setMulticast(bool enable)
Set the multicast.
Definition: udp.h:152
UDP duplex connections impliment a bi-directional point-to-point UDP session between two peer hosts.
Definition: udp.h:564
void endTransmitter(void)
Stop transmitter.
Definition: udp.h:393
Representing a UDP socket used for subnet broadcasts, this class provides an alternate binding and se...
Definition: udp.h:266
socket operations.
The broadcast address object is used to store the broadcast address for a specific subnet.
Definition: address.h:629
The network name and address objects are all derived from a common IPV4Address base class.
Definition: address.h:362
bool isPendingReceive(timeout_t timeout)
Check for pending data.
Definition: udp.h:494