ucommon
|
00001 // Copyright (C) 1999-2005 Open Source Telecom Corporation. 00002 // Copyright (C) 2006-2014 David Sugar, Tycho Softworks. 00003 // 00004 // This program is free software; you can redistribute it and/or modify 00005 // it under the terms of the GNU General Public License as published by 00006 // the Free Software Foundation; either version 2 of the License, or 00007 // (at your option) any later version. 00008 // 00009 // This program is distributed in the hope that it will be useful, 00010 // but WITHOUT ANY WARRANTY; without even the implied warranty of 00011 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 00012 // GNU General Public License for more details. 00013 // 00014 // You should have received a copy of the GNU Lesser General Public License 00015 // along with this program. If not, see <http://www.gnu.org/licenses/>. 00016 // 00017 // As a special exception, you may use this file as part of a free software 00018 // library without restriction. Specifically, if other files instantiate 00019 // templates or use macros or inline functions from this file, or you compile 00020 // this file and link it with other files to produce an executable, this 00021 // file does not by itself cause the resulting executable to be covered by 00022 // the GNU General Public License. This exception does not however 00023 // invalidate any other reasons why the executable file might be covered by 00024 // the GNU General Public License. 00025 // 00026 // This exception applies only to the code released under the name GNU 00027 // Common C++. If you copy code from other releases into a copy of GNU 00028 // Common C++, as the General Public License permits, the exception does 00029 // not apply to the code that you add in this way. To avoid misleading 00030 // anyone as to the status of such modified files, you must delete 00031 // this exception notice from them. 00032 // 00033 // If you write modifications of your own for GNU Common C++, it is your choice 00034 // whether to permit this exception to apply to your modifications. 00035 // If you do not wish that, delete this exception notice. 00036 // 00037 00043 #ifndef COMMONCPP_UDP_H_ 00044 #define COMMONCPP_UDP_H_ 00045 00046 #include <cstdio> 00047 00048 #ifndef COMMONCPP_CONFIG_H_ 00049 #include <commoncpp/config.h> 00050 #endif 00051 00052 #ifndef COMMONCPP_STRING_H_ 00053 #include <commoncpp/string.h> 00054 #endif 00055 00056 #ifndef COMMONCPP_ADDRESS_H_ 00057 #include <commoncpp/address.h> 00058 #endif 00059 00060 #ifndef COMMONCPP_SOCKET_H_ 00061 #include <commoncpp/socket.h> 00062 #endif 00063 00064 namespace ost { 00065 00098 class __EXPORT UDPSocket : public Socket 00099 { 00100 private: 00101 inline Error setKeepAlive(bool enable) 00102 {return Socket::setKeepAlive(enable);} 00103 00104 protected: 00105 Socket::address peer; 00106 00107 Family family; 00108 00109 public: 00113 UDPSocket(Family family = IPV4); 00114 00118 UDPSocket(const char *name, Family family = IPV4); 00119 00129 UDPSocket(const ucommon::Socket::address &bind); 00130 UDPSocket(const IPV4Address &bind, tpport_t port); 00131 #ifdef CCXX_IPV6 00132 UDPSocket(const IPV6Address &bind, tpport_t port); 00133 #endif 00134 00138 virtual ~UDPSocket(); 00139 00143 inline Error setLoopback(bool enable) 00144 {return Socket::setLoopbackByFamily(enable, family);} 00145 00149 inline Error setMulticast(bool enable) 00150 {return Socket::setMulticastByFamily(enable, family);} 00151 00155 inline Error setTimeToLive(char ttl) 00156 {return Socket::setTimeToLiveByFamily(ttl, family);} 00157 00165 void setPeer(const ucommon::Socket::address &host); 00166 void connect(const ucommon::Socket::address &host); 00167 00168 void setPeer(const IPV4Host &host, tpport_t port); 00169 void connect(const IPV4Host &host, tpport_t port); 00170 #ifdef CCXX_IPV6 00171 void setPeer(const IPV6Host &host, tpport_t port); 00172 void connect(const IPV6Host &host, tpport_t port); 00173 #endif 00174 00182 Socket::Error getInterfaceIndex(const char *ethX,int& InterfaceIndex); 00183 00192 Socket::Error join(const ucommon::Socket::address &ia, int InterfaceIndex=0); 00193 Socket::Error join(const IPV4Multicast &ia,int InterfaceIndex); 00194 00202 ssize_t send(const void *buf, size_t len); 00203 00212 ssize_t receive(void *buf, size_t len, bool reply = false); 00213 00222 ucommon::Socket::address getPeer(); 00223 00224 IPV4Host getIPV4Peer(tpport_t *port = NULL); 00225 inline IPV4Host getPeer(tpport_t *port) 00226 {return getIPV4Peer(port);} 00227 00228 #ifdef CCXX_IPV6 00229 IPV6Host getIPV6Peer(tpport_t *port = NULL); 00230 #endif 00231 00239 inline ssize_t peek(void *buf, size_t len) 00240 {return ::recv(so, (char *)buf, len, MSG_PEEK);} 00241 00245 void setPeer(const char *service); 00246 void connect(const char *service); 00247 00252 Error disconnect(void); 00253 }; 00254 00263 class __EXPORT UDPBroadcast : public UDPSocket 00264 { 00265 private: 00266 void setPeer(const IPV4Host &ia, tpport_t port); 00267 00268 Error setBroadcast(bool enable) 00269 {return Socket::setBroadcast(enable);} 00270 00271 public: 00278 UDPBroadcast(const IPV4Address &ia, tpport_t port); 00279 00286 void setPeer(const IPV4Broadcast &subnet, tpport_t port); 00287 }; 00288 00297 class __EXPORT UDPTransmit : protected UDPSocket 00298 { 00299 private: 00307 Error cConnect(const IPV4Address &ia, tpport_t port); 00308 00309 protected: 00313 UDPTransmit(Family family = IPV4); 00314 00327 UDPTransmit(const ucommon::Socket::address &bind); 00328 00329 UDPTransmit(const IPV4Address &bind, tpport_t port = 5005); 00330 #ifdef CCXX_IPV6 00331 UDPTransmit(const IPV6Address &bind, tpport_t port = 5005); 00332 #endif 00333 00343 Error connect(const ucommon::Socket::address &host); 00344 00345 Error connect(const IPV4Host &host, tpport_t port); 00346 #ifdef CCXX_IPV6 00347 Error connect(const IPV6Address &host, tpport_t port); 00348 #endif 00349 00359 Error connect(const IPV4Broadcast &subnet, tpport_t port); 00360 00368 Error connect(const IPV4Multicast &mgroup, tpport_t port); 00369 #ifdef CCXX_IPV6 00370 Error connect(const IPV6Multicast &mgroup, tpport_t port); 00371 #endif 00372 00380 inline ssize_t send(const void *buf, size_t len) 00381 {return ::send(so, (const char *)buf, len, MSG_NOSIGNAL);} 00382 00386 inline void endTransmitter(void) 00387 {Socket::endSocket();} 00388 00389 /* 00390 * Get transmitter socket. 00391 * 00392 * @return transmitter. 00393 */ 00394 inline SOCKET getTransmitter(void) 00395 {return so;}; 00396 00397 inline Error setMulticast(bool enable) 00398 {return Socket::setMulticastByFamily(enable, family);} 00399 00400 inline Error setTimeToLive(unsigned char ttl) 00401 {return Socket::setTimeToLiveByFamily(ttl, family);} 00402 00403 public: 00413 inline ssize_t transmit(const char *buffer, size_t len) 00414 {return ::send(so, buffer, len, MSG_DONTWAIT|MSG_NOSIGNAL);} 00415 00422 inline bool isOutputReady(unsigned long timeout = 0l) 00423 {return Socket::isPending(Socket::pendingOutput, timeout);} 00424 00425 00426 inline Error setRouting(bool enable) 00427 {return Socket::setRouting(enable);} 00428 00429 inline Error setTypeOfService(Tos tos) 00430 {return Socket::setTypeOfService(tos);} 00431 00432 inline Error setBroadcast(bool enable) 00433 {return Socket::setBroadcast(enable);} 00434 }; 00435 00444 class __EXPORT UDPReceive : protected UDPSocket 00445 { 00446 protected: 00456 UDPReceive(const ucommon::Socket::address &bind); 00457 UDPReceive(const IPV4Address &bind, tpport_t port); 00458 #ifdef CCXX_IPV6 00459 UDPReceive(const IPV6Address &bind, tpport_t port); 00460 #endif 00461 00471 Error connect(const ucommon::Socket::address &host); 00472 Error connect(const IPV4Host &host, tpport_t port); 00473 #ifdef CCXX_IPV6 00474 Error connect(const IPV6Host &host, tpport_t port); 00475 #endif 00476 00483 bool isPendingReceive(timeout_t timeout) 00484 {return Socket::isPending(Socket::pendingInput, timeout);} 00485 00489 inline void endReceiver(void) 00490 {Socket::endSocket();} 00491 00492 inline SOCKET getReceiver(void) const 00493 {return so;} 00494 00495 inline Error setRouting(bool enable) 00496 {return Socket::setRouting(enable);} 00497 00498 inline Error setMulticast(bool enable) 00499 {return Socket::setMulticastByFamily(enable, family);} 00500 00501 inline Error join(const ucommon::Socket::address &ia) 00502 {return Socket::join(ia);} 00503 00504 inline Error join(const IPV4Multicast &ia) 00505 {return Socket::join(ia);} 00506 00507 #ifdef CCXX_IPV6 00508 inline Error join(const IPV6Multicast &ia) 00509 {return Socket::join(ia);} 00510 #endif 00511 00512 inline Error drop(const IPV4Multicast &ia) 00513 {return Socket::drop(ia);} 00514 00515 #ifdef CCXX_IPV6 00516 inline Error drop(const IPV6Multicast &ia) 00517 {return Socket::drop(ia);} 00518 #endif 00519 00520 public: 00528 inline ssize_t receive(void *buf, size_t len) 00529 {return ::recv(so, (char *)buf, len, 0);} 00530 00537 inline bool isInputReady(timeout_t timeout = TIMEOUT_INF) 00538 {return Socket::isPending(Socket::pendingInput, timeout);} 00539 }; 00540 00551 class __EXPORT UDPDuplex : public UDPTransmit, public UDPReceive 00552 { 00553 public: 00561 UDPDuplex(const ucommon::Socket::address &bind); 00562 UDPDuplex(const IPV4Address &bind, tpport_t port); 00563 #ifdef CCXX_IPV6 00564 UDPDuplex(const IPV6Address &bind, tpport_t port); 00565 #endif 00566 00576 Error connect(const ucommon::Socket::address &host); 00577 Error connect(const IPV4Host &host, tpport_t port); 00578 #ifdef CCXX_IPV6 00579 Error connect(const IPV6Host &host, tpport_t port); 00580 #endif 00581 00588 Error disconnect(void); 00589 }; 00590 00591 } // namespace ost 00592 00593 #endif