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 //
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_UDP_H_
44 #define COMMONCPP_UDP_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_SOCKET_H_
61 #include <commoncpp/socket.h>
62 #endif
63 
64 namespace ost {
65 
98 class __EXPORT UDPSocket : public Socket
99 {
100 private:
101  inline Error setKeepAlive(bool enable)
102  {return Socket::setKeepAlive(enable);}
103 
104 protected:
105  Socket::address peer;
106 
107  Family family;
108 
109 public:
113  UDPSocket(Family family = IPV4);
114 
118  UDPSocket(const char *name, Family family = IPV4);
119 
129  UDPSocket(const ucommon::Socket::address &bind);
130  UDPSocket(const IPV4Address &bind, tpport_t port);
131 #ifdef CCXX_IPV6
132  UDPSocket(const IPV6Address &bind, tpport_t port);
133 #endif
134 
138  virtual ~UDPSocket();
139 
143  inline Error setLoopback(bool enable)
144  {return Socket::setLoopbackByFamily(enable, family);}
145 
149  inline Error setMulticast(bool enable)
150  {return Socket::setMulticastByFamily(enable, family);}
151 
155  inline Error setTimeToLive(char ttl)
156  {return Socket::setTimeToLiveByFamily(ttl, family);}
157 
165  void setPeer(const ucommon::Socket::address &host);
166  void connect(const ucommon::Socket::address &host);
167 
168  void setPeer(const IPV4Host &host, tpport_t port);
169  void connect(const IPV4Host &host, tpport_t port);
170 #ifdef CCXX_IPV6
171  void setPeer(const IPV6Host &host, tpport_t port);
172  void connect(const IPV6Host &host, tpport_t port);
173 #endif
174 
182  Socket::Error getInterfaceIndex(const char *ethX,int& InterfaceIndex);
183 
192  Socket::Error join(const ucommon::Socket::address &ia, int InterfaceIndex=0);
193  Socket::Error join(const IPV4Multicast &ia,int InterfaceIndex);
194 
202  ssize_t send(const void *buf, size_t len);
203 
212  ssize_t receive(void *buf, size_t len, bool reply = false);
213 
222  ucommon::Socket::address getPeer();
223 
224  IPV4Host getIPV4Peer(tpport_t *port = NULL);
225  inline IPV4Host getPeer(tpport_t *port)
226  {return getIPV4Peer(port);}
227 
228 #ifdef CCXX_IPV6
229  IPV6Host getIPV6Peer(tpport_t *port = NULL);
230 #endif
231 
239  inline ssize_t peek(void *buf, size_t len)
240  {return ::recv(so, (char *)buf, len, MSG_PEEK);}
241 
245  void setPeer(const char *service);
246  void connect(const char *service);
247 
252  Error disconnect(void);
253 };
254 
263 class __EXPORT UDPBroadcast : public UDPSocket
264 {
265 private:
266  void setPeer(const IPV4Host &ia, tpport_t port);
267 
268  Error setBroadcast(bool enable)
269  {return Socket::setBroadcast(enable);}
270 
271 public:
278  UDPBroadcast(const IPV4Address &ia, tpport_t port);
279 
286  void setPeer(const IPV4Broadcast &subnet, tpport_t port);
287 };
288 
297 class __EXPORT UDPTransmit : protected UDPSocket
298 {
299 private:
307  Error cConnect(const IPV4Address &ia, tpport_t port);
308 
309 protected:
313  UDPTransmit(Family family = IPV4);
314 
328 
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 ucommon::Socket::address &host);
344 
345  Error connect(const IPV4Host &host, tpport_t port);
346 #ifdef CCXX_IPV6
347  Error connect(const IPV6Address &host, tpport_t port);
348 #endif
349 
359  Error connect(const IPV4Broadcast &subnet, tpport_t port);
360 
368  Error connect(const IPV4Multicast &mgroup, tpport_t port);
369 #ifdef CCXX_IPV6
370  Error connect(const IPV6Multicast &mgroup, tpport_t port);
371 #endif
372 
380  inline ssize_t send(const void *buf, size_t len)
381  {return ::send(so, (const char *)buf, len, MSG_NOSIGNAL);}
382 
386  inline void endTransmitter(void)
387  {Socket::endSocket();}
388 
389  /*
390  * Get transmitter socket.
391  *
392  * @return transmitter.
393  */
394  inline SOCKET getTransmitter(void)
395  {return so;};
396 
397  inline Error setMulticast(bool enable)
398  {return Socket::setMulticastByFamily(enable, family);}
399 
400  inline Error setTimeToLive(unsigned char ttl)
401  {return Socket::setTimeToLiveByFamily(ttl, family);}
402 
403 public:
413  inline ssize_t transmit(const char *buffer, size_t len)
414  {return ::send(so, buffer, len, MSG_DONTWAIT|MSG_NOSIGNAL);}
415 
422  inline bool isOutputReady(unsigned long timeout = 0l)
423  {return Socket::isPending(Socket::pendingOutput, timeout);}
424 
425 
426  inline Error setRouting(bool enable)
427  {return Socket::setRouting(enable);}
428 
429  inline Error setTypeOfService(Tos tos)
430  {return Socket::setTypeOfService(tos);}
431 
432  inline Error setBroadcast(bool enable)
433  {return Socket::setBroadcast(enable);}
434 };
435 
444 class __EXPORT UDPReceive : protected UDPSocket
445 {
446 protected:
457  UDPReceive(const IPV4Address &bind, tpport_t port);
458 #ifdef CCXX_IPV6
459  UDPReceive(const IPV6Address &bind, tpport_t port);
460 #endif
461 
471  Error connect(const ucommon::Socket::address &host);
472  Error connect(const IPV4Host &host, tpport_t port);
473 #ifdef CCXX_IPV6
474  Error connect(const IPV6Host &host, tpport_t port);
475 #endif
476 
484  {return Socket::isPending(Socket::pendingInput, timeout);}
485 
489  inline void endReceiver(void)
490  {Socket::endSocket();}
491 
492  inline SOCKET getReceiver(void) const
493  {return so;}
494 
495  inline Error setRouting(bool enable)
496  {return Socket::setRouting(enable);}
497 
498  inline Error setMulticast(bool enable)
499  {return Socket::setMulticastByFamily(enable, family);}
500 
501  inline Error join(const ucommon::Socket::address &ia)
502  {return Socket::join(ia);}
503 
504  inline Error join(const IPV4Multicast &ia)
505  {return Socket::join(ia);}
506 
507 #ifdef CCXX_IPV6
508  inline Error join(const IPV6Multicast &ia)
509  {return Socket::join(ia);}
510 #endif
511 
512  inline Error drop(const IPV4Multicast &ia)
513  {return Socket::drop(ia);}
514 
515 #ifdef CCXX_IPV6
516  inline Error drop(const IPV6Multicast &ia)
517  {return Socket::drop(ia);}
518 #endif
519 
520 public:
528  inline ssize_t receive(void *buf, size_t len)
529  {return ::recv(so, (char *)buf, len, 0);}
530 
537  inline bool isInputReady(timeout_t timeout = TIMEOUT_INF)
538  {return Socket::isPending(Socket::pendingInput, timeout);}
539 };
540 
551 class __EXPORT UDPDuplex : public UDPTransmit, public UDPReceive
552 {
553 public:
561  UDPDuplex(const ucommon::Socket::address &bind);
562  UDPDuplex(const IPV4Address &bind, tpport_t port);
563 #ifdef CCXX_IPV6
564  UDPDuplex(const IPV6Address &bind, tpport_t port);
565 #endif
566 
576  Error connect(const ucommon::Socket::address &host);
577  Error connect(const IPV4Host &host, tpport_t port);
578 #ifdef CCXX_IPV6
579  Error connect(const IPV6Host &host, tpport_t port);
580 #endif
581 
588  Error disconnect(void);
589 };
590 
591 } // namespace ost
592 
593 #endif
ssize_t transmit(const char *buffer, size_t len)
Transmit "send" to use "connected" send rather than sendto.
Definition: udp.h:413
A specialization of IPV4Address that provides address validation for multicast addresses.
Definition: address.h:615
ssize_t receive(void *buf, size_t len)
Receive a data packet from the connected peer host.
Definition: udp.h:528
A specialization of IPV6Address that provides address validation for multicast addresses.
Definition: address.h:991
Definition: address.h:58
Representing half of a two-way UDP connection, the UDP receiver can receive data from another peer ho...
Definition: udp.h:444
bool isInputReady(timeout_t timeout=ucommon::Timer::inf)
See if input queue has data packets available.
Definition: udp.h:537
The broadcast address object is used to store the broadcast address for a specific subnet...
Definition: address.h:593
socket operations.
Error setLoopback(bool enable)
Set the loopback.
Definition: udp.h:143
UDP duplex connections impliment a bi-directional point-to-point UDP session between two peer hosts...
Definition: udp.h:551
ssize_t peek(void *buf, size_t len)
Examine contents of next waiting packet.
Definition: udp.h:239
ssize_t send(const void *buf, size_t len)
Transmit "send" to use "connected" send rather than sendto.
Definition: udp.h:380
void endReceiver(void)
End receiver.
Definition: udp.h:489
The network name and address objects are all derived from a common IPV4Address base class...
Definition: address.h:331
Network addresses and sockets related classes.
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:542
Representing half of a two-way UDP connection, the UDP transmitter can broadcast data to another sele...
Definition: udp.h:297
bool isPendingReceive(timeout_t timeout)
Check for pending data.
Definition: udp.h:483
void endTransmitter(void)
Stop transmitter.
Definition: udp.h:386
Error setTimeToLive(char ttl)
Set time to live.
Definition: udp.h:155
The network name and address objects are all derived from a common IPV6Address base class...
Definition: address.h:734
Error setMulticast(bool enable)
Set the multicast.
Definition: udp.h:149
Representing a UDP socket used for subnet broadcasts, this class provides an alternate binding and se...
Definition: udp.h:263
A generic socket address class.
Definition: socket.h:349
unsigned long timeout_t
Typedef for millisecond timer values.
Definition: platform.h:334
UDP sockets implement the TCP SOCK_DGRAM UDP protocol.
Definition: udp.h:98
bool isOutputReady(unsigned long timeout=0l)
See if output queue is empty for sending more packets.
Definition: udp.h:422
This object is used to hold the actual and valid internet address of a specific host machine that wil...
Definition: address.h:929