ucommon  7.0.0
About: GNU uCommon C++ is a portable and optimized class framework for writing C++ applications that need to use threads and support concurrent synchronization, and that use sockets, XML parsing, object serialization, thread-optimized string and data structure classes, etc..
  Fossies Dox: ucommon-7.0.0.tar.gz  ("unofficial" and yet experimental doxygen-generated source code documentation)  

Loading...
Searching...
No Matches
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
39/**
40 * @file commoncpp/udp.h
41 * @short udp derived socket classes.
42 **/
43
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
65namespace ost {
66
67/**
68 * UDP sockets implement the TCP SOCK_DGRAM UDP protocol. They can be
69 * used to pass unverified messages between hosts, or to broadcast a
70 * specific message to an entire subnet. Please note that Streaming of
71 * realtime data commonly use UDPDuplex related classes rather than
72 * UDPSocket.
73 *
74 * In addition to connected TCP sessions, Common C++ supports UDP sockets and
75 * these also cover a range of functionality. Like a TCPSocket, A UDPSocket
76 * can be created bound to a specific network interface and/or port address,
77 * though this is not required. UDP sockets also are usually either
78 * connected or otherwise "associated" with a specific "peer" UDP socket.
79 * Since UDP sockets operate through discreet packets, there are no streaming
80 * operators used with UDP sockets.
81 *
82 * In addition to the UDP "socket" class, there is a "UDPBroadcast" class.
83 * The UDPBroadcast is a socket that is set to send messages to a subnet as a
84 * whole rather than to an individual peer socket that it may be associated
85 * with.
86 *
87 * UDP sockets are often used for building "realtime" media streaming
88 * protocols and full duplex messaging services. When used in this manner,
89 * typically a pair of UDP sockets are used together; one socket is used to
90 * send and the other to receive data with an associated pair of UDP sockets
91 * on a "peer" host. This concept is represented through the Common C++
92 * UDPDuplex object, which is a pair of sockets that communicate with another
93 * UDPDuplex pair.
94 *
95 *
96 * @author David Sugar <dyfet@ostel.com>
97 * @short Unreliable Datagram Protocol sockets.
98 */
100{
101private:
102 inline Error setKeepAlive(bool enable)
103 {return Socket::setKeepAlive(enable);}
104
106
107protected:
109
111
112public:
113 /**
114 * Create an unbound UDP socket, mostly for internal use.
115 */
116 UDPSocket(Family family = IPV4);
117
118 /**
119 * Create a UDP socket bound by a service name.
120 */
121 UDPSocket(const char *name, Family family = IPV4);
122
123 /**
124 * Create a UDP socket and bind it to a specific interface
125 * and port address so that other UDP sockets on remote
126 * machines (or the same host) may find and send UDP messages
127 * to it. On failure to bind, an exception is thrown.
128 *
129 * @param bind address to bind this socket to.
130 * @param port number to bind this socket to.
131 */
133 UDPSocket(const IPV4Address &bind, tpport_t port);
134#ifdef CCXX_IPV6
135 UDPSocket(const IPV6Address &bind, tpport_t port);
136#endif
137
138 /**
139 * Destroy a UDP socket as a socket.
140 */
141 virtual ~UDPSocket();
142
143 /**
144 * Set the loopback.
145 */
146 inline Error setLoopback(bool enable)
147 {return Socket::setLoopbackByFamily(enable, family);}
148
149 /**
150 * Set the multicast.
151 */
152 inline Error setMulticast(bool enable)
153 {return Socket::setMulticastByFamily(enable, family);}
154
155 /**
156 * Set time to live.
157 */
158 inline Error setTimeToLive(char ttl)
159 {return Socket::setTimeToLiveByFamily(ttl, family);}
160
161 /**
162 * set the peer address to send message packets to. This can be
163 * set before every send() call if nessisary.
164 *
165 * @param host address to send packets to.
166 * @param port number to deliver packets to.
167 */
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
178 /**
179 * get the interface index for a named network device
180 *
181 * @param ethX is device name, like "eth0" or "eth1"
182 * @param InterfaceIndex is the index value returned by os
183 * @todo Win32 and ipv6 specific implementation.
184 */
185 Socket::Error getInterfaceIndex(const char *ethX,int& InterfaceIndex);
186
187 /**
188 * join a multicast group on a particular interface
189 *
190 * @param ia is the multicast address to use
191 * @param InterfaceIndex is the index value returned by
192 * getInterfaceIndex
193 * @todo Win32 and ipv6 specific implementation.
194 */
195 Socket::Error join(const ucommon::Socket::address &ia, int InterfaceIndex=0);
196 Socket::Error join(const IPV4Multicast &ia,int InterfaceIndex);
197
198 /**
199 * Send a message packet to a peer host.
200 *
201 * @param buf pointer to packet buffer to send.
202 * @param len of packet buffer to send.
203 * @return number of bytes sent.
204 */
205 ssize_t send(const void *buf, size_t len);
206
207 /**
208 * Receive a message from any host.
209 *
210 * @param buf pointer to packet buffer to receive.
211 * @param len of packet buffer to receive.
212 * @param reply save sender address for reply if true.
213 * @return number of bytes received.
214 */
215 ssize_t receive(void *buf, size_t len, bool reply = false);
216
217 /**
218 * Examine address of sender of next waiting packet. This also
219 * sets "peer" address to the sender so that the next "send"
220 * message acts as a "reply". This additional behavior overides
221 * the standard socket getSender behavior.
222 *
223 * @param port pointer to hold port number.
224 */
225 ucommon::Socket::address getPeer();
226
227 IPV4Host getIPV4Peer(tpport_t *port = NULL);
229 {return getIPV4Peer(port);}
230
231#ifdef CCXX_IPV6
232 IPV6Host getIPV6Peer(tpport_t *port = NULL);
233#endif
234
235 /**
236 * Examine contents of next waiting packet.
237 *
238 * @param buf pointer to packet buffer for contents.
239 * @param len of packet buffer.
240 * @return number of bytes examined.
241 */
242 inline ssize_t peek(void *buf, size_t len)
243 {return ::recv(so, (char *)buf, (socksize_t)len, MSG_PEEK);}
244
245 /**
246 * Associate socket with a named connection
247 */
248 void setPeer(const char *service);
249 void connect(const char *service);
250
251 /**
252 * Disassociate this socket from any host connection. No data
253 * should be read or written until a connection is established.
254 */
255 Error disconnect(void);
256};
257
258/**
259 * Representing a UDP socket used for subnet broadcasts, this class
260 * provides an alternate binding and setPeer() capability for UDP
261 * sockets.
262 *
263 * @author David Sugar <dyfet@ostel.com>
264 * @short Unreliable Datagram for subnet broadcasts.
265 */
267{
268private:
269 void setPeer(const IPV4Host &ia, tpport_t port);
270
271 Error setBroadcast(bool enable)
272 {return Socket::setBroadcast(enable);}
273
275
276public:
277 /**
278 * Create and bind a subnet broadcast socket.
279 *
280 * @param ia address to bind socket under locally.
281 * @param port to bind socket under locally.
282 */
284
285 /**
286 * Set peer by subnet rather than specific host.
287 *
288 * @param subnet of peer hosts to send to.
289 * @param port number to use.
290 */
291 void setPeer(const IPV4Broadcast &subnet, tpport_t port);
292};
293
294/**
295 * Representing half of a two-way UDP connection, the UDP transmitter
296 * can broadcast data to another selected peer host or to an entire
297 * subnet.
298 *
299 * @author David Sugar <dyfet@ostel.com>
300 * @short Unreliable Datagram Peer Associations.
301 */
303{
304private:
305 /**
306 * Common code for diferent flavours of Connect (host, broadcast,
307 * multicast).
308 *
309 * @param ia network address to associate with
310 * @param port port number to associate with
311 */
312 Error cConnect(const IPV4Address &ia, tpport_t port);
313
315
316protected:
317 /**
318 * Create a UDP transmitter.
319 */
320 UDPTransmit(Family family = IPV4);
321
322 /**
323 * Create a UDP transmitter, bind it to a specific interface
324 * and port address so that other UDP sockets on remote
325 * machines (or the same host) may find and send UDP messages
326 * to it, and associate it with a given port on a peer host.
327 * On failure to bind, an exception is thrown. This class is
328 * only used to build the UDP Duplex.
329 *
330 * @param bind address to bind this socket to.
331 * @param port number to bind this socket to. If 0 or not specified,
332 * the bind socket address port is used.
333 */
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
341 /**
342 * Associate this socket with a specified peer host. The port
343 * number from the constructor will be used. All UDP packets
344 * will be sent to and received from the specified host.
345 *
346 * @return 0 on success, -1 on error.
347 * @param host address to connect socket to.
348 * @param port to connect socket to.
349 */
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
357 /**
358 * Associate this socket with a subnet of peer hosts for
359 * subnet broadcasting. The server must be able to assert
360 * broadcast permission for the socket.
361 *
362 * @return 0 on success, -1 on error.
363 * @param subnet subnet address to broadcast into.
364 * @param port transport port to broadcast into.
365 */
366 Error connect(const IPV4Broadcast &subnet, tpport_t port);
367
368 /**
369 * Associate this socket with a multicast group.
370 *
371 * @return 0 on success, -1 on error.
372 * @param mgroup address of the multicast group to send to.
373 * @param port port number
374 */
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
380 /**
381 * Transmit "send" to use "connected" send rather than sendto.
382 *
383 * @return number of bytes sent.
384 * @param buf address of buffer to send.
385 * @param len of bytes to send.
386 */
387 inline ssize_t send(const void *buf, size_t len)
388 {return ::send(so, (const char *)buf, (socksize_t)len, MSG_NOSIGNAL);}
389
390 /**
391 * Stop transmitter.
392 */
393 inline void endTransmitter(void)
394 {Socket::endSocket();}
395
396 /*
397 * Get transmitter socket.
398 *
399 * @return transmitter.
400 */
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
410public:
411 /**
412 * Transmit "send" to use "connected" send rather than sendto.
413 *
414 * @note Windows does not support MSG_DONTWAIT, so it is defined
415 * as 0 on that platform.
416 * @return number of bytes sent.
417 * @param buffer address of buffer to send.
418 * @param len of bytes to send.
419 */
420 inline ssize_t transmit(const char *buffer, size_t len)
421 {return ::send(so, buffer, (socksize_t)len, MSG_DONTWAIT|MSG_NOSIGNAL);}
422
423 /**
424 * See if output queue is empty for sending more packets.
425 *
426 * @return true if output available.
427 * @param timeout in milliseconds to wait.
428 */
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
438 {return Socket::setTypeOfService(tos);}
439
440 inline Error setBroadcast(bool enable)
441 {return Socket::setBroadcast(enable);}
442};
443
444/**
445 * Representing half of a two-way UDP connection, the UDP receiver
446 * can receive data from another peer host or subnet. This class is
447 * used exclusivily to derive the UDPDuplex.
448 *
449 * @author David Sugar <dyfet@ostel.com>
450 * @short Unreliable Datagram Peer Associations.
451 */
453{
454private:
456
457protected:
458 /**
459 * Create a UDP receiver, bind it to a specific interface
460 * and port address so that other UDP sockets on remote
461 * machines (or the same host) may find and send UDP messages
462 * to it, and associate it with a given port on a peer host.
463 * On failure to bind, an exception is thrown.
464 *
465 * @param bind address to bind this socket to.
466 */
468 UDPReceive(const IPV4Address &bind, tpport_t port);
469#ifdef CCXX_IPV6
470 UDPReceive(const IPV6Address &bind, tpport_t port);
471#endif
472
473 /**
474 * Associate this socket with a specified peer host. The port
475 * number from the constructor will be used. All UDP packets
476 * will be sent received from the specified host.
477 *
478 * @return 0 on success, -1 on error.
479 * @param host host network address to connect socket to.
480 * @param port host transport port to connect socket to.
481 */
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
488 /**
489 * Check for pending data.
490 *
491 * @return true if data is waiting.
492 * @param timeout in milliseconds.
493 */
495 return Socket::isPending(Socket::pendingInput, timeout);
496 }
497
498 /**
499 * End receiver.
500 */
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
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
532public:
533 /**
534 * Receive a data packet from the connected peer host.
535 *
536 * @return num of bytes actually received.
537 * @param buf address of data receive buffer.
538 * @param len size of data receive buffer.
539 */
540 inline ssize_t receive(void *buf, size_t len)
541 {return ::recv(so, (char *)buf, (socksize_t)len, 0);}
542
543 /**
544 * See if input queue has data packets available.
545 *
546 * @return true if data packets available.
547 * @param timeout in milliseconds.
548 */
550 return Socket::isPending(Socket::pendingInput, timeout);
551 }
552};
553
554/**
555 * UDP duplex connections impliment a bi-directional point-to-point UDP
556 * session between two peer hosts. Two UDP sockets are typically used
557 * on alternating port addresses to assure that sender and receiver
558 * data does not collide or echo back. A UDP Duplex is commonly used
559 * for full duplex real-time streaming of UDP data between hosts.
560 *
561 * @author David Sugar <dyfet@ostel.com>
562 * @short Unreliable Datagram Peer Associations.
563 */
565{
566private:
568
569public:
570 /**
571 * Create a UDP duplex as a pair of UDP simplex objects
572 * bound to alternating and interconnected port addresses.
573 *
574 * @param bind address to bind this socket to.
575 * @param port number to bind sender.
576 */
578 UDPDuplex(const IPV4Address &bind, tpport_t port);
579#ifdef CCXX_IPV6
580 UDPDuplex(const IPV6Address &bind, tpport_t port);
581#endif
582
583 /**
584 * Associate the duplex with a specified peer host. Both
585 * the sender and receiver will be interconnected with
586 * the remote host.
587 *
588 * @return 0 on success, error code on error.
589 * @param host address to connect socket to.
590 * @param port number to connect socket to.
591 */
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
598 /**
599 * Disassociate this duplex from any host connection. No data
600 * should be read or written until a connection is established.
601 *
602 * @return 0 on success, error code on error.
603 */
604 Error disconnect(void);
605};
606
607} // namespace ost
608
609#endif
Network addresses and sockets related classes.
The network name and address objects are all derived from a common IPV4Address base class.
Definition: address.h:363
The broadcast address object is used to store the broadcast address for a specific subnet.
Definition: address.h:630
This object is used to hold the actual and valid internet address of a specific host machine that wil...
Definition: address.h:579
A specialization of IPV4Address that provides address validation for multicast addresses.
Definition: address.h:652
Representing a UDP socket used for subnet broadcasts, this class provides an alternate binding and se...
Definition: udp.h:267
__DELETE_COPY(UDPBroadcast)
void setPeer(const IPV4Host &ia, tpport_t port)
Error setBroadcast(bool enable)
Definition: udp.h:271
UDP duplex connections impliment a bi-directional point-to-point UDP session between two peer hosts.
Definition: udp.h:565
__DELETE_COPY(UDPDuplex)
Representing half of a two-way UDP connection, the UDP receiver can receive data from another peer ho...
Definition: udp.h:453
Error join(const IPV4Multicast &ia)
Definition: udp.h:516
__DELETE_COPY(UDPReceive)
ssize_t receive(void *buf, size_t len)
Receive a data packet from the connected peer host.
Definition: udp.h:540
bool isInputReady(timeout_t timeout=TIMEOUT_INF)
See if input queue has data packets available.
Definition: udp.h:549
Error drop(const IPV4Multicast &ia)
Definition: udp.h:524
Error setMulticast(bool enable)
Definition: udp.h:510
SOCKET getReceiver(void) const
Definition: udp.h:504
Error join(const ucommon::Socket::address &ia)
Definition: udp.h:513
Error setRouting(bool enable)
Definition: udp.h:507
void endReceiver(void)
End receiver.
Definition: udp.h:501
bool isPendingReceive(timeout_t timeout)
Check for pending data.
Definition: udp.h:494
UDP sockets implement the TCP SOCK_DGRAM UDP protocol.
Definition: udp.h:100
Socket::address peer
Definition: udp.h:108
ssize_t peek(void *buf, size_t len)
Examine contents of next waiting packet.
Definition: udp.h:242
Error setKeepAlive(bool enable)
Definition: udp.h:102
IPV4Host getPeer(tpport_t *port)
Definition: udp.h:228
Error setMulticast(bool enable)
Set the multicast.
Definition: udp.h:152
Error setTimeToLive(char ttl)
Set time to live.
Definition: udp.h:158
__DELETE_COPY(UDPSocket)
Error setLoopback(bool enable)
Set the loopback.
Definition: udp.h:146
Family family
Definition: udp.h:110
Representing half of a two-way UDP connection, the UDP transmitter can broadcast data to another sele...
Definition: udp.h:303
Error setRouting(bool enable)
Definition: udp.h:434
Error setTypeOfService(Tos tos)
Definition: udp.h:437
Error setTimeToLive(uint8_t ttl)
Definition: udp.h:407
Error setMulticast(bool enable)
Definition: udp.h:404
SOCKET getTransmitter(void)
Definition: udp.h:401
__DELETE_COPY(UDPTransmit)
Error setBroadcast(bool enable)
Definition: udp.h:440
ssize_t transmit(const char *buffer, size_t len)
Transmit "send" to use "connected" send rather than sendto.
Definition: udp.h:420
ssize_t send(const void *buf, size_t len)
Transmit "send" to use "connected" send rather than sendto.
Definition: udp.h:387
bool isOutputReady(unsigned long timeout=0l)
See if output queue is empty for sending more packets.
Definition: udp.h:429
void endTransmitter(void)
Stop transmitter.
Definition: udp.h:393
A generic socket address class.
Definition: socket.h:365
socket operations.
Common C++ generic string class.
#define TIMEOUT_INF
Definition: config.h:58
#define __EXPORT
Definition: config.h:49
#define MSG_NOSIGNAL
Definition: socket.cpp:62
#define MSG_DONTWAIT
Definition: socket.cpp:58
static shell::numericopt timeout('t', "--timeout", _TEXT("optional keyboard input timeout"), "seconds", 0)
Definition: address.cpp:63
socket_t SOCKET
Definition: socket.h:90
in_port_t tpport_t
Transport Protocol Ports.
Definition: address.h:80
size_t socksize_t
Definition: platform.h:296
unsigned long timeout_t
Definition: platform.h:453
static shell::numericopt port('p', "--port", _TEXT("port to use"), "port", 0)
static uint8_t buffer[65536]
Definition: zerofill.cpp:27