begin multi threading
[anytun.git] / PracticalSocket.h
blobdcb39a9669359819acce6946b7540d7396b7303a
1 /*
2 * anytun
4 * The secure anycast tunneling protocol (satp) defines a protocol used
5 * for communication between any combination of unicast and anycast
6 * tunnel endpoints. It has less protocol overhead than IPSec in Tunnel
7 * mode and allows tunneling of every ETHER TYPE protocol (e.g.
8 * ethernet, ip, arp ...). satp directly includes cryptography and
9 * message authentication based on the methodes used by SRTP. It is
10 * intended to deliver a generic, scaleable and secure solution for
11 * tunneling and relaying of packets of any protocol.
14 * Copyright (C) 2007 anytun.org <satp@wirdorange.org>
16 * This program is free software; you can redistribute it and/or modify
17 * it under the terms of the GNU General Public License version 2
18 * as published by the Free Software Foundation.
20 * This program is distributed in the hope that it will be useful,
21 * but WITHOUT ANY WARRANTY; without even the implied warranty of
22 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
23 * GNU General Public License for more details.
25 * You should have received a copy of the GNU General Public License
26 * along with this program (see the file COPYING included with this
27 * distribution); if not, write to the Free Software Foundation, Inc.,
28 * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
31 // this is from: http://cs.ecs.baylor.edu/~donahoo/practical/CSockets/practical/
32 // and this is their header:
34 * C++ sockets on Unix and Windows
35 * Copyright (C) 2002
37 * This program is free software; you can redistribute it and/or modify
38 * it under the terms of the GNU General Public License as published by
39 * the Free Software Foundation; either version 2 of the License, or
40 * (at your option) any later version.
42 * This program is distributed in the hope that it will be useful,
43 * but WITHOUT ANY WARRANTY; without even the implied warranty of
44 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
45 * GNU General Public License for more details.
47 * You should have received a copy of the GNU General Public License
48 * along with this program; if not, write to the Free Software
49 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
52 #ifndef __PRACTICALSOCKET_INCLUDED__
53 #define __PRACTICALSOCKET_INCLUDED__
55 #include <string> // For string
56 #include <exception> // For exception class
58 using namespace std;
60 /**
61 * Signals a problem with the execution of a socket call.
63 class SocketException : public exception {
64 public:
65 /**
66 * Construct a SocketException with a explanatory message.
67 * @param message explanatory message
68 * @param incSysMsg true if system message (from strerror(errno))
69 * should be postfixed to the user provided message
71 SocketException(const string &message, bool inclSysMsg = false) throw();
73 /**
74 * Provided just to guarantee that no exceptions are thrown.
76 ~SocketException() throw();
78 /**
79 * Get the exception message
80 * @return exception message
82 const char *what() const throw();
84 private:
85 string userMessage; // Exception message
88 /**
89 * Base class representing basic communication endpoint
91 class Socket {
92 public:
93 /**
94 * Close and deallocate this socket
96 ~Socket();
98 /**
99 * Get the local address
100 * @return local address of socket
101 * @exception SocketException thrown if fetch fails
103 string getLocalAddress() throw(SocketException);
106 * Get the local port
107 * @return local port of socket
108 * @exception SocketException thrown if fetch fails
110 unsigned short getLocalPort() throw(SocketException);
113 * Set the local port to the specified port and the local address
114 * to any interface
115 * @param localPort local port
116 * @exception SocketException thrown if setting local port fails
118 void setLocalPort(unsigned short localPort) throw(SocketException);
121 * Set the local port to the specified port and the local address
122 * to the specified address. If you omit the port, a random port
123 * will be selected.
124 * @param localAddress local address
125 * @param localPort local port
126 * @exception SocketException thrown if setting local port or address fails
128 void setLocalAddressAndPort(const string &localAddress,
129 unsigned short localPort = 0) throw(SocketException);
132 * If WinSock, unload the WinSock DLLs; otherwise do nothing. We ignore
133 * this in our sample client code but include it in the library for
134 * completeness. If you are running on Windows and you are concerned
135 * about DLL resource consumption, call this after you are done with all
136 * Socket instances. If you execute this on Windows while some instance of
137 * Socket exists, you are toast. For portability of client code, this is
138 * an empty function on non-Windows platforms so you can always include it.
139 * @param buffer buffer to receive the data
140 * @param bufferLen maximum number of bytes to read into buffer
141 * @return number of bytes read, 0 for EOF, and -1 for error
142 * @exception SocketException thrown WinSock clean up fails
144 static void cleanUp() throw(SocketException);
147 * Resolve the specified service for the specified protocol to the
148 * corresponding port number in host byte order
149 * @param service service to resolve (e.g., "http")
150 * @param protocol protocol of service to resolve. Default is "tcp".
152 static unsigned short resolveService(const string &service,
153 const string &protocol = "tcp");
155 private:
156 // Prevent the user from trying to use value semantics on this object
157 Socket(const Socket &sock);
158 void operator=(const Socket &sock);
160 protected:
161 int sockDesc; // Socket descriptor
162 Socket(int type, int protocol) throw(SocketException);
163 Socket(int sockDesc);
167 * Socket which is able to connect, send, and receive
169 class CommunicatingSocket : public Socket {
170 public:
172 * Establish a socket connection with the given foreign
173 * address and port
174 * @param foreignAddress foreign address (IP address or name)
175 * @param foreignPort foreign port
176 * @exception SocketException thrown if unable to establish connection
178 void connect(const string &foreignAddress, unsigned short foreignPort)
179 throw(SocketException);
182 * Write the given buffer to this socket. Call connect() before
183 * calling send()
184 * @param buffer buffer to be written
185 * @param bufferLen number of bytes from buffer to be written
186 * @exception SocketException thrown if unable to send data
188 void send(const void *buffer, int bufferLen) throw(SocketException);
191 * Read into the given buffer up to bufferLen bytes data from this
192 * socket. Call connect() before calling recv()
193 * @param buffer buffer to receive the data
194 * @param bufferLen maximum number of bytes to read into buffer
195 * @return number of bytes read, 0 for EOF, and -1 for error
196 * @exception SocketException thrown if unable to receive data
198 int recv(void *buffer, int bufferLen) throw(SocketException);
201 * Get the foreign address. Call connect() before calling recv()
202 * @return foreign address
203 * @exception SocketException thrown if unable to fetch foreign address
205 string getForeignAddress() throw(SocketException);
208 * Get the foreign port. Call connect() before calling recv()
209 * @return foreign port
210 * @exception SocketException thrown if unable to fetch foreign port
212 unsigned short getForeignPort() throw(SocketException);
214 protected:
215 CommunicatingSocket(int type, int protocol) throw(SocketException);
216 CommunicatingSocket(int newConnSD);
220 * TCP socket for communication with other TCP sockets
222 class TCPSocket : public CommunicatingSocket {
223 public:
225 * Construct a TCP socket with no connection
226 * @exception SocketException thrown if unable to create TCP socket
228 TCPSocket() throw(SocketException);
231 * Construct a TCP socket with a connection to the given foreign address
232 * and port
233 * @param foreignAddress foreign address (IP address or name)
234 * @param foreignPort foreign port
235 * @exception SocketException thrown if unable to create TCP socket
237 TCPSocket(const string &foreignAddress, unsigned short foreignPort)
238 throw(SocketException);
240 private:
241 // Access for TCPServerSocket::accept() connection creation
242 friend class TCPServerSocket;
243 TCPSocket(int newConnSD);
247 * TCP socket class for servers
249 class TCPServerSocket : public Socket {
250 public:
252 * Construct a TCP socket for use with a server, accepting connections
253 * on the specified port on any interface
254 * @param localPort local port of server socket, a value of zero will
255 * give a system-assigned unused port
256 * @param queueLen maximum queue length for outstanding
257 * connection requests (default 5)
258 * @exception SocketException thrown if unable to create TCP server socket
260 TCPServerSocket(unsigned short localPort, int queueLen = 5)
261 throw(SocketException);
264 * Construct a TCP socket for use with a server, accepting connections
265 * on the specified port on the interface specified by the given address
266 * @param localAddress local interface (address) of server socket
267 * @param localPort local port of server socket
268 * @param queueLen maximum queue length for outstanding
269 * connection requests (default 5)
270 * @exception SocketException thrown if unable to create TCP server socket
272 TCPServerSocket(const string &localAddress, unsigned short localPort,
273 int queueLen = 5) throw(SocketException);
276 * Blocks until a new connection is established on this socket or error
277 * @return new connection socket
278 * @exception SocketException thrown if attempt to accept a new connection fails
280 TCPSocket *accept() throw(SocketException);
282 private:
283 void setListen(int queueLen) throw(SocketException);
287 * UDP socket class
289 class UDPSocket : public CommunicatingSocket {
290 public:
292 * Construct a UDP socket
293 * @exception SocketException thrown if unable to create UDP socket
295 UDPSocket() throw(SocketException);
298 * Construct a UDP socket with the given local port
299 * @param localPort local port
300 * @exception SocketException thrown if unable to create UDP socket
302 UDPSocket(unsigned short localPort) throw(SocketException);
305 * Construct a UDP socket with the given local port and address
306 * @param localAddress local address
307 * @param localPort local port
308 * @exception SocketException thrown if unable to create UDP socket
310 UDPSocket(const string &localAddress, unsigned short localPort)
311 throw(SocketException);
314 * Unset foreign address and port
315 * @return true if disassociation is successful
316 * @exception SocketException thrown if unable to disconnect UDP socket
318 void disconnect() throw(SocketException);
321 * Send the given buffer as a UDP datagram to the
322 * specified address/port
323 * @param buffer buffer to be written
324 * @param bufferLen number of bytes to write
325 * @param foreignAddress address (IP address or name) to send to
326 * @param foreignPort port number to send to
327 * @return true if send is successful
328 * @exception SocketException thrown if unable to send datagram
330 void sendTo(const void *buffer, int bufferLen, const string &foreignAddress,
331 unsigned short foreignPort) throw(SocketException);
334 * Read read up to bufferLen bytes data from this socket. The given buffer
335 * is where the data will be placed
336 * @param buffer buffer to receive data
337 * @param bufferLen maximum number of bytes to receive
338 * @param sourceAddress address of datagram source
339 * @param sourcePort port of data source
340 * @return number of bytes received and -1 for error
341 * @exception SocketException thrown if unable to receive datagram
343 int recvFrom(void *buffer, int bufferLen, string &sourceAddress,
344 unsigned short &sourcePort) throw(SocketException);
347 * Set the multicast TTL
348 * @param multicastTTL multicast TTL
349 * @exception SocketException thrown if unable to set TTL
351 void setMulticastTTL(unsigned char multicastTTL) throw(SocketException);
354 * Join the specified multicast group
355 * @param multicastGroup multicast group address to join
356 * @exception SocketException thrown if unable to join group
358 void joinGroup(const string &multicastGroup) throw(SocketException);
361 * Leave the specified multicast group
362 * @param multicastGroup multicast group address to leave
363 * @exception SocketException thrown if unable to leave group
365 void leaveGroup(const string &multicastGroup) throw(SocketException);
367 private:
368 void setBroadcast();
371 #endif