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 protected:
106  Socket::address peer;
107 
108  Family family;
109 
110 public:
114  UDPSocket(Family family = IPV4);
115 
119  UDPSocket(const char *name, Family family = IPV4);
120 
130  UDPSocket(const ucommon::Socket::address &bind);
131  UDPSocket(const IPV4Address &bind, tpport_t port);
132 #ifdef CCXX_IPV6
133  UDPSocket(const IPV6Address &bind, tpport_t port);
134 #endif
135 
139  virtual ~UDPSocket();
140 
144  inline Error setLoopback(bool enable)
145  {return Socket::setLoopbackByFamily(enable, family);}
146 
150  inline Error setMulticast(bool enable)
151  {return Socket::setMulticastByFamily(enable, family);}
152 
156  inline Error setTimeToLive(char ttl)
157  {return Socket::setTimeToLiveByFamily(ttl, family);}
158 
166  void setPeer(const ucommon::Socket::address &host);
167  void connect(const ucommon::Socket::address &host);
168 
169  void setPeer(const IPV4Host &host, tpport_t port);
170  void connect(const IPV4Host &host, tpport_t port);
171 #ifdef CCXX_IPV6
172  void setPeer(const IPV6Host &host, tpport_t port);
173  void connect(const IPV6Host &host, tpport_t port);
174 #endif
175 
183  Socket::Error getInterfaceIndex(const char *ethX,int& InterfaceIndex);
184 
193  Socket::Error join(const ucommon::Socket::address &ia, int InterfaceIndex=0);
194  Socket::Error join(const IPV4Multicast &ia,int InterfaceIndex);
195 
203  ssize_t send(const void *buf, size_t len);
204 
213  ssize_t receive(void *buf, size_t len, bool reply = false);
214 
223  ucommon::Socket::address getPeer();
224 
225  IPV4Host getIPV4Peer(tpport_t *port = NULL);
226  inline IPV4Host getPeer(tpport_t *port)
227  {return getIPV4Peer(port);}
228 
229 #ifdef CCXX_IPV6
230  IPV6Host getIPV6Peer(tpport_t *port = NULL);
231 #endif
232 
240  inline ssize_t peek(void *buf, size_t len)
241  {return ::recv(so, (char *)buf, (socksize_t)len, MSG_PEEK);}
242 
246  void setPeer(const char *service);
247  void connect(const char *service);
248 
253  Error disconnect(void);
254 };
255 
264 class __EXPORT UDPBroadcast : public UDPSocket
265 {
266 private:
267  void setPeer(const IPV4Host &ia, tpport_t port);
268 
269  Error setBroadcast(bool enable)
270  {return Socket::setBroadcast(enable);}
271 
272 public:
279  UDPBroadcast(const IPV4Address &ia, tpport_t port);
280 
287  void setPeer(const IPV4Broadcast &subnet, tpport_t port);
288 };
289 
298 class __EXPORT UDPTransmit : protected UDPSocket
299 {
300 private:
308  Error cConnect(const IPV4Address &ia, tpport_t port);
309 
310 protected:
314  UDPTransmit(Family family = IPV4);
315 
329 
330  UDPTransmit(const IPV4Address &bind, tpport_t port = 5005);
331 #ifdef CCXX_IPV6
332  UDPTransmit(const IPV6Address &bind, tpport_t port = 5005);
333 #endif
334 
344  Error connect(const ucommon::Socket::address &host);
345 
346  Error connect(const IPV4Host &host, tpport_t port);
347 #ifdef CCXX_IPV6
348  Error connect(const IPV6Address &host, tpport_t port);
349 #endif
350 
360  Error connect(const IPV4Broadcast &subnet, tpport_t port);
361 
369  Error connect(const IPV4Multicast &mgroup, tpport_t port);
370 #ifdef CCXX_IPV6
371  Error connect(const IPV6Multicast &mgroup, tpport_t port);
372 #endif
373 
381  inline ssize_t send(const void *buf, size_t len)
382  {return ::send(so, (const char *)buf, (socksize_t)len, MSG_NOSIGNAL);}
383 
387  inline void endTransmitter(void)
388  {Socket::endSocket();}
389 
390  /*
391  * Get transmitter socket.
392  *
393  * @return transmitter.
394  */
395  inline SOCKET getTransmitter(void)
396  {return so;};
397 
398  inline Error setMulticast(bool enable)
399  {return Socket::setMulticastByFamily(enable, family);}
400 
401  inline Error setTimeToLive(unsigned char ttl)
402  {return Socket::setTimeToLiveByFamily(ttl, family);}
403 
404 public:
414  inline ssize_t transmit(const char *buffer, size_t len)
415  {return ::send(so, buffer, (socksize_t)len, MSG_DONTWAIT|MSG_NOSIGNAL);}
416 
423  inline bool isOutputReady(unsigned long timeout = 0l)
424  {return Socket::isPending(Socket::pendingOutput, timeout);}
425 
426 
427  inline Error setRouting(bool enable)
428  {return Socket::setRouting(enable);}
429 
430  inline Error setTypeOfService(Tos tos)
431  {return Socket::setTypeOfService(tos);}
432 
433  inline Error setBroadcast(bool enable)
434  {return Socket::setBroadcast(enable);}
435 };
436 
445 class __EXPORT UDPReceive : protected UDPSocket
446 {
447 protected:
458  UDPReceive(const IPV4Address &bind, tpport_t port);
459 #ifdef CCXX_IPV6
460  UDPReceive(const IPV6Address &bind, tpport_t port);
461 #endif
462 
472  Error connect(const ucommon::Socket::address &host);
473  Error connect(const IPV4Host &host, tpport_t port);
474 #ifdef CCXX_IPV6
475  Error connect(const IPV6Host &host, tpport_t port);
476 #endif
477 
484  bool isPendingReceive(timeout_t timeout)
485  {return Socket::isPending(Socket::pendingInput, timeout);}
486 
490  inline void endReceiver(void)
491  {Socket::endSocket();}
492 
493  inline SOCKET getReceiver(void) const
494  {return so;}
495 
496  inline Error setRouting(bool enable)
497  {return Socket::setRouting(enable);}
498 
499  inline Error setMulticast(bool enable)
500  {return Socket::setMulticastByFamily(enable, family);}
501 
502  inline Error join(const ucommon::Socket::address &ia)
503  {return Socket::join(ia);}
504 
505  inline Error join(const IPV4Multicast &ia)
506  {return Socket::join(ia);}
507 
508 #ifdef CCXX_IPV6
509  inline Error join(const IPV6Multicast &ia)
510  {return Socket::join(ia);}
511 #endif
512 
513  inline Error drop(const IPV4Multicast &ia)
514  {return Socket::drop(ia);}
515 
516 #ifdef CCXX_IPV6
517  inline Error drop(const IPV6Multicast &ia)
518  {return Socket::drop(ia);}
519 #endif
520 
521 public:
529  inline ssize_t receive(void *buf, size_t len)
530  {return ::recv(so, (char *)buf, (socksize_t)len, 0);}
531 
538  inline bool isInputReady(timeout_t timeout = TIMEOUT_INF)
539  {return Socket::isPending(Socket::pendingInput, timeout);}
540 };
541 
552 class __EXPORT UDPDuplex : public UDPTransmit, public UDPReceive
553 {
554 public:
562  UDPDuplex(const ucommon::Socket::address &bind);
563  UDPDuplex(const IPV4Address &bind, tpport_t port);
564 #ifdef CCXX_IPV6
565  UDPDuplex(const IPV6Address &bind, tpport_t port);
566 #endif
567 
577  Error connect(const ucommon::Socket::address &host);
578  Error connect(const IPV4Host &host, tpport_t port);
579 #ifdef CCXX_IPV6
580  Error connect(const IPV6Host &host, tpport_t port);
581 #endif
582 
589  Error disconnect(void);
590 };
591 
592 } // namespace ost
593 
594 #endif
ssize_t transmit(const char *buffer, size_t len)
Transmit "send" to use "connected" send rather than sendto.
Definition: udp.h:414
A specialization of IPV4Address that provides address validation for multicast addresses.
Definition: address.h:616
ssize_t receive(void *buf, size_t len)
Receive a data packet from the connected peer host.
Definition: udp.h:529
Definition: address.h:59
A specialization of IPV6Address that provides address validation for multicast addresses.
Definition: address.h:992
socket operations.
Representing half of a two-way UDP connection, the UDP receiver can receive data from another peer ho...
Definition: udp.h:445
bool isInputReady(timeout_t timeout=ucommon::Timer::inf)
See if input queue has data packets available.
Definition: udp.h:538
Network addresses and sockets related classes.
The broadcast address object is used to store the broadcast address for a specific subnet...
Definition: address.h:594
Error setLoopback(bool enable)
Set the loopback.
Definition: udp.h:144
UDP duplex connections impliment a bi-directional point-to-point UDP session between two peer hosts...
Definition: udp.h:552
ssize_t peek(void *buf, size_t len)
Examine contents of next waiting packet.
Definition: udp.h:240
ssize_t send(const void *buf, size_t len)
Transmit "send" to use "connected" send rather than sendto.
Definition: udp.h:381
void endReceiver(void)
End receiver.
Definition: udp.h:490
The network name and address objects are all derived from a common IPV4Address base class...
Definition: address.h:332
Common C++ generic string class.
This object is used to hold the actual and valid internet address of a specific host machine that wil...
Definition: address.h:543
Representing half of a two-way UDP connection, the UDP transmitter can broadcast data to another sele...
Definition: udp.h:298
bool isPendingReceive(timeout_t timeout)
Check for pending data.
Definition: udp.h:484
void endTransmitter(void)
Stop transmitter.
Definition: udp.h:387
Error setTimeToLive(char ttl)
Set time to live.
Definition: udp.h:156
The network name and address objects are all derived from a common IPV6Address base class...
Definition: address.h:735
Error setMulticast(bool enable)
Set the multicast.
Definition: udp.h:150
Representing a UDP socket used for subnet broadcasts, this class provides an alternate binding and se...
Definition: udp.h:264
UDP sockets implement the TCP SOCK_DGRAM UDP protocol.
Definition: udp.h:99
bool isOutputReady(unsigned long timeout=0l)
See if output queue is empty for sending more packets.
Definition: udp.h:423
This object is used to hold the actual and valid internet address of a specific host machine that wil...
Definition: address.h:930
A generic socket address class.
Definition: socket.h:359