Alexandre Lision | ddd731e | 2014-01-31 11:50:08 -0500 | [diff] [blame] | 1 | // Copyright (C) 1999-2005 Open Source Telecom Corporation. |
| 2 | // Copyright (C) 2006-2010 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 General Public License |
| 15 | // along with this program; if not, write to the Free Software |
| 16 | // Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. |
| 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_DCCP_H_ |
| 45 | #define COMMONCPP_DCCP_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_COMMONCPP |
| 66 | |
| 67 | /** |
| 68 | * DCCP sockets are used for stream based connected sessions between two |
| 69 | * sockets. Both error recovery and flow control operate transparently |
| 70 | * for a DCCP socket connection. The DCCP socket base class is used both |
| 71 | * for client connections and to bind a DCCP "server" for accepting DCCP |
| 72 | * streams. |
| 73 | * |
| 74 | * An implicit and unique DCCPSocket object exists in Common C++ to represent |
| 75 | * a bound DCCP socket acting as a "server" for receiving connection requests. |
| 76 | * This class is not part of DCCPStream because such objects normally perform |
| 77 | * no physical I/O (read or write operations) other than to specify a listen |
| 78 | * backlog queue and perform "accept" operations for pending connections. |
| 79 | * The Common C++ DCCPSocket offers a Peek method to examine where the next |
| 80 | * pending connection is coming from, and a Reject method to flush the next |
| 81 | * request from the queue without having to create a session. |
| 82 | * |
| 83 | * The DCCPSocket also supports a "OnAccept" method which can be called when a |
| 84 | * DCCPStream related object is created from a DCCPSocket. By creating a |
| 85 | * DCCPStream from a DCCPSocket, an accept operation automatically occurs, and |
| 86 | * the DCCPSocket can then still reject the client connection through the |
| 87 | * return status of it's OnAccept method. |
| 88 | * |
| 89 | * @author Leandro Sales <leandroal@gmail.com> |
| 90 | * @author Heverton Stuart <hevertonsns@gmail.com> |
| 91 | * @short bound server for DCCP streams and sessions. |
| 92 | */ |
| 93 | class __EXPORT DCCPSocket : public Socket |
| 94 | { |
| 95 | union { |
| 96 | struct sockaddr_in ipv4; |
| 97 | #ifdef CCXX_IPV6 |
| 98 | struct sockaddr_in6 ipv6; |
| 99 | #endif |
| 100 | } peer; |
| 101 | |
| 102 | Family family; |
| 103 | |
| 104 | public: |
| 105 | /** |
| 106 | * A method to call in a derived DCCPSocket class that is acting |
| 107 | * as a server when a connection request is being accepted. The |
| 108 | * server can implement protocol specific rules to exclude the |
| 109 | * remote socket from being accepted by returning false. The |
| 110 | * Peek method can also be used for this purpose. |
| 111 | * |
| 112 | * @return true if client should be accepted. |
| 113 | * @param ia internet host address of the client. |
| 114 | * @param port number of the client. |
| 115 | */ |
| 116 | virtual bool onAccept(const IPV4Host &ia, tpport_t port); |
| 117 | #ifdef CCXX_IPV6 |
| 118 | virtual bool onAccept(const IPV6Host &ia, tpport_t port); |
| 119 | #endif |
| 120 | |
| 121 | virtual IPV4Host getIPV4Sender(tpport_t *port = NULL) const; |
| 122 | |
| 123 | #ifdef CCXX_IPV6 |
| 124 | virtual IPV6Host getIPV6Sender(tpport_t *port = NULL) const; |
| 125 | #endif |
| 126 | |
| 127 | /** |
| 128 | * A DCCP "server" is created as a DCCP socket that is bound |
| 129 | * to a hardware address and port number on the local machine |
| 130 | * and that has a backlog queue to listen for remote connection |
| 131 | * requests. If the server cannot be created, an exception is |
| 132 | * thrown. |
| 133 | * |
| 134 | * @param bind local ip address or interface to use. |
| 135 | * @param port number to bind socket under. |
| 136 | * @param backlog size of connection request queue. |
| 137 | */ |
| 138 | DCCPSocket(const IPV4Address &bind, tpport_t port, unsigned backlog = 5); |
| 139 | #ifdef CCXX_IPV6 |
| 140 | DCCPSocket(const IPV6Address &bind, tpport_t port, unsigned backlog = 5); |
| 141 | #endif |
| 142 | |
| 143 | /** |
| 144 | * Create a named dccp socket by service and/or interface id. |
| 145 | * For IPV4 we use [host:]svc or [host/]svc for the string. |
| 146 | * If we have getaddrinfo, we use that to obtain the addr to |
| 147 | * bind for. |
| 148 | * |
| 149 | * @param name of host interface and service port to bind. |
| 150 | * @param backlog size of connection request queue. |
| 151 | */ |
| 152 | DCCPSocket(const char *name, Family family = IPV4, unsigned backlog = 5); |
| 153 | |
| 154 | /** |
| 155 | * Create an unconnected ephemeral DCCP client socket. |
| 156 | */ |
| 157 | DCCPSocket(Family family = IPV4); |
| 158 | |
| 159 | /** |
| 160 | * Create a server session by accepting a DCCP Socket. |
| 161 | */ |
| 162 | DCCPSocket(DCCPSocket& server, timeout_t timeout = 0); |
| 163 | |
| 164 | /** |
| 165 | * Used to reject the next incoming connection request. |
| 166 | */ |
| 167 | void reject(void); |
| 168 | |
| 169 | /** |
| 170 | * Disconnect active dccp connection (client use). |
| 171 | */ |
| 172 | void disconnect(void); |
| 173 | |
| 174 | /** |
| 175 | * Set CCID DCCP. |
| 176 | */ |
| 177 | bool setCCID(uint8_t ccid); |
| 178 | |
| 179 | /** |
| 180 | * Get TX CCID DCCP. |
| 181 | */ |
| 182 | int getTxCCID(); |
| 183 | |
| 184 | /** |
| 185 | * Get RX CCID DCCP. |
| 186 | */ |
| 187 | int getRxCCID(); |
| 188 | |
| 189 | /** |
| 190 | * Return number of bytes to be read |
| 191 | */ |
| 192 | size_t available(); |
| 193 | |
| 194 | /** |
| 195 | * Create a DCCP client connection to a DCCP socket (on |
| 196 | * a remote machine). |
| 197 | * |
| 198 | * @param host address of remote DCCP server. |
| 199 | * @param port number to connect. |
| 200 | */ |
| 201 | void connect(const IPV4Host &host, tpport_t port, timeout_t timeout = 0); |
| 202 | #ifdef CCXX_IPV6 |
| 203 | void connect(const IPV6Host &host, tpport_t port, timeout_t timeout = 0); |
| 204 | #endif |
| 205 | |
| 206 | /** |
| 207 | * Connect to a named client. |
| 208 | */ |
| 209 | void connect(const char *name); |
| 210 | |
| 211 | /** |
| 212 | * Used to wait for pending connection requests. |
| 213 | * @return true if data packets available. |
| 214 | * @param timeout in milliseconds. TIMEOUT_INF if not specified. |
| 215 | */ |
| 216 | inline bool isPendingConnection(timeout_t timeout = TIMEOUT_INF) /* not const -- jfc */ |
| 217 | {return Socket::isPending(Socket::pendingInput, timeout);} |
| 218 | |
| 219 | /** |
| 220 | * Use base socket handler for ending this socket. |
| 221 | */ |
| 222 | virtual ~DCCPSocket(); |
| 223 | }; |
| 224 | |
| 225 | END_NAMESPACE |
| 226 | |
| 227 | #endif |