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_COMMONCPP
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 #ifdef CCXX_IPV6
106  union {
107  struct sockaddr_in6 ipv6;
108  struct sockaddr_in ipv4;
109  } peer;
110 #else
111  union {
112  struct sockaddr_in ipv4;
113  } peer;
114 #endif
115 
116  Family family;
117 
118 public:
122  UDPSocket(Family family = IPV4);
123 
127  UDPSocket(const char *name, Family family = IPV4);
128 
138  UDPSocket(const IPV4Address &bind, tpport_t port);
139 #ifdef CCXX_IPV6
140  UDPSocket(const IPV6Address &bind, tpport_t port);
141 #endif
142 
146  virtual ~UDPSocket();
147 
151  inline Error setLoopback(bool enable)
152  {return Socket::setLoopbackByFamily(enable, family);}
153 
157  inline Error setMulticast(bool enable)
158  {return Socket::setMulticastByFamily(enable, family);}
159 
163  inline Error setTimeToLive(char ttl)
164  {return Socket::setTimeToLiveByFamily(ttl, family);}
165 
173  void setPeer(const IPV4Host &host, tpport_t port);
174  void connect(const IPV4Host &host, tpport_t port);
175 #ifdef CCXX_IPV6
176  void setPeer(const IPV6Host &host, tpport_t port);
177  void connect(const IPV6Host &host, tpport_t port);
178 #endif
179 
187  Socket::Error getInterfaceIndex(const char *ethX,int& InterfaceIndex);
188 
197  Socket::Error join(const IPV4Multicast &ia,int InterfaceIndex);
198 
206  ssize_t send(const void *buf, size_t len);
207 
216  ssize_t receive(void *buf, size_t len, bool reply = false);
217 
226  IPV4Host getIPV4Peer(tpport_t *port = NULL) const;
227  inline IPV4Host getPeer(tpport_t *port = NULL) const
228  {return getIPV4Peer(port);}
229 
230 #ifdef CCXX_IPV6
231  IPV6Host getIPV6Peer(tpport_t *port = NULL) const;
232 #endif
233 
241  inline ssize_t peek(void *buf, size_t len)
242  {return ::recv(so, (char *)buf, len, MSG_PEEK);};
243 
247  void setPeer(const char *service);
248  void connect(const char *service);
249 
254  Error disconnect(void);
255 };
256 
265 class __EXPORT UDPBroadcast : public UDPSocket
266 {
267 private:
268  void setPeer(const IPV4Host &ia, tpport_t port);
269 
270  Error setBroadcast(bool enable)
271  {return Socket::setBroadcast(enable);};
272 
273 public:
280  UDPBroadcast(const IPV4Address &ia, tpport_t port);
281 
288  void setPeer(const IPV4Broadcast &subnet, tpport_t port);
289 };
290 
299 class __EXPORT UDPTransmit : protected UDPSocket
300 {
301 private:
309  Error cConnect(const IPV4Address &ia, tpport_t port);
310 
311 protected:
315  UDPTransmit(Family family = IPV4);
316 
328  UDPTransmit(const IPV4Address &bind, tpport_t port = 5005);
329 #ifdef CCXX_IPV6
330  UDPTransmit(const IPV6Address &bind, tpport_t port = 5005);
331 #endif
332 
342  Error connect(const IPV4Host &host, tpport_t port);
343 #ifdef CCXX_IPV6
344  Error connect(const IPV6Address &host, tpport_t port);
345 #endif
346 
356  Error connect(const IPV4Broadcast &subnet, tpport_t port);
357 
365  Error connect(const IPV4Multicast &mgroup, tpport_t port);
366 #ifdef CCXX_IPV6
367  Error connect(const IPV6Multicast &mgroup, tpport_t port);
368 #endif
369 
377  inline ssize_t send(const void *buf, size_t len)
378  {return ::send(so, (const char *)buf, len, MSG_NOSIGNAL);}
379 
383  inline void endTransmitter(void)
384  {Socket::endSocket();}
385 
386  /*
387  * Get transmitter socket.
388  *
389  * @return transmitter.
390  */
391  inline SOCKET getTransmitter(void)
392  {return so;};
393 
394  inline Error setMulticast(bool enable)
395  {return Socket::setMulticastByFamily(enable, family);}
396 
397  inline Error setTimeToLive(unsigned char ttl)
398  {return Socket::setTimeToLiveByFamily(ttl, family);};
399 
400 public:
410  inline ssize_t transmit(const char *buffer, size_t len)
411  {return ::send(so, buffer, len, MSG_DONTWAIT|MSG_NOSIGNAL);}
412 
419  inline bool isOutputReady(unsigned long timeout = 0l)
420  {return Socket::isPending(Socket::pendingOutput, timeout);};
421 
422 
423  inline Error setRouting(bool enable)
424  {return Socket::setRouting(enable);};
425 
426  inline Error setTypeOfService(Tos tos)
427  {return Socket::setTypeOfService(tos);};
428 
429  inline Error setBroadcast(bool enable)
430  {return Socket::setBroadcast(enable);};
431 };
432 
441 class __EXPORT UDPReceive : protected UDPSocket
442 {
443 protected:
454  UDPReceive(const IPV4Address &bind, tpport_t port);
455 #ifdef CCXX_IPV6
456  UDPReceive(const IPV6Address &bind, tpport_t port);
457 #endif
458 
468  Error connect(const IPV4Host &host, tpport_t port);
469 #ifdef CCXX_IPV6
470  Error connect(const IPV6Host &host, tpport_t port);
471 #endif
472 
480  {return Socket::isPending(Socket::pendingInput, timeout);};
481 
485  inline void endReceiver(void)
486  {Socket::endSocket();}
487 
488  inline SOCKET getReceiver(void) const
489  {return so;};
490 
491  inline Error setRouting(bool enable)
492  {return Socket::setRouting(enable);}
493 
494  inline Error setMulticast(bool enable)
495  {return Socket::setMulticastByFamily(enable, family);}
496 
497  inline Error join(const IPV4Multicast &ia)
498  {return Socket::join(ia);}
499 
500 #ifdef CCXX_IPV6
501  inline Error join(const IPV6Multicast &ia)
502  {return Socket::join(ia);}
503 #endif
504 
505  inline Error drop(const IPV4Multicast &ia)
506  {return Socket::drop(ia);}
507 
508 #ifdef CCXX_IPV6
509  inline Error drop(const IPV6Multicast &ia)
510  {return Socket::drop(ia);}
511 #endif
512 
513 public:
521  inline ssize_t receive(void *buf, size_t len)
522  {return ::recv(so, (char *)buf, len, 0);};
523 
530  inline bool isInputReady(timeout_t timeout = TIMEOUT_INF)
531  {return Socket::isPending(Socket::pendingInput, timeout);};
532 };
533 
544 class __EXPORT UDPDuplex : public UDPTransmit, public UDPReceive
545 {
546 public:
554  UDPDuplex(const IPV4Address &bind, tpport_t port);
555 #ifdef CCXX_IPV6
556  UDPDuplex(const IPV6Address &bind, tpport_t port);
557 #endif
558 
568  Error connect(const IPV4Host &host, tpport_t port);
569 #ifdef CCXX_IPV6
570  Error connect(const IPV6Host &host, tpport_t port);
571 #endif
572 
579  Error disconnect(void);
580 };
581 
582 END_NAMESPACE
583 
584 #endif
Error disconnect(void)
Disassociate this socket from any host connection.
void setPeer(const IPV4Host &host, tpport_t port)
set the peer address to send message packets to.
ssize_t transmit(const char *buffer, size_t len)
Transmit "send" to use "connected" send rather than sendto.
Definition: udp.h:410
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:521
Error connect(const IPV4Host &host, tpport_t port)
Associate this socket with a specified peer host.
A specialization of IPV6Address that provides address validation for multicast addresses.
Definition: address.h:991
Representing half of a two-way UDP connection, the UDP receiver can receive data from another peer ho...
Definition: udp.h:441
bool isInputReady(timeout_t timeout=ucommon::Timer::inf)
See if input queue has data packets available.
Definition: udp.h:530
The broadcast address object is used to store the broadcast address for a specific subnet...
Definition: address.h:593
Error setLoopback(bool enable)
Set the loopback.
Definition: udp.h:151
Common C++ generic string class.
UDP duplex connections impliment a bi-directional point-to-point UDP session between two peer hosts...
Definition: udp.h:544
ssize_t peek(void *buf, size_t len)
Examine contents of next waiting packet.
Definition: udp.h:241
ssize_t send(const void *buf, size_t len)
Transmit "send" to use "connected" send rather than sendto.
Definition: udp.h:377
void endReceiver(void)
End receiver.
Definition: udp.h:485
The network name and address objects are all derived from a common IPV4Address base class...
Definition: address.h:331
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:299
bool isPendingReceive(timeout_t timeout)
Check for pending data.
Definition: udp.h:479
void endTransmitter(void)
Stop transmitter.
Definition: udp.h:383
Error setTimeToLive(char ttl)
Set time to live.
Definition: udp.h:163
Socket::Error join(const IPV4Multicast &ia, int InterfaceIndex)
join a multicast group on a particular interface
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:157
unsigned long timeout_t
Typedef for millisecond timer values.
Definition: platform.h:326
socket operations.
Representing a UDP socket used for subnet broadcasts, this class provides an alternate binding and se...
Definition: udp.h:265
Network addresses and sockets related classes.
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:419
This object is used to hold the actual and valid internet address of a specific host machine that wil...
Definition: address.h:929