working version with crypto
[anytun.git] / PracticalSocket.h
blob58c64249c2709bd2f1ba3f393cf993ec79140d88
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 void setSocketOpt(int optionName, const void* optionValue, socklen_t optionLen)
133 throw(SocketException);
136 * If WinSock, unload the WinSock DLLs; otherwise do nothing. We ignore
137 * this in our sample client code but include it in the library for
138 * completeness. If you are running on Windows and you are concerned
139 * about DLL resource consumption, call this after you are done with all
140 * Socket instances. If you execute this on Windows while some instance of
141 * Socket exists, you are toast. For portability of client code, this is
142 * an empty function on non-Windows platforms so you can always include it.
143 * @param buffer buffer to receive the data
144 * @param bufferLen maximum number of bytes to read into buffer
145 * @return number of bytes read, 0 for EOF, and -1 for error
146 * @exception SocketException thrown WinSock clean up fails
148 static void cleanUp() throw(SocketException);
151 * Resolve the specified service for the specified protocol to the
152 * corresponding port number in host byte order
153 * @param service service to resolve (e.g., "http")
154 * @param protocol protocol of service to resolve. Default is "tcp".
156 static unsigned short resolveService(const string &service,
157 const string &protocol = "tcp");
159 private:
160 // Prevent the user from trying to use value semantics on this object
161 Socket(const Socket &sock);
162 void operator=(const Socket &sock);
164 protected:
165 int sockDesc; // Socket descriptor
166 Socket(int type, int protocol) throw(SocketException);
167 Socket(int sockDesc);
171 * Socket which is able to connect, send, and receive
173 class CommunicatingSocket : public Socket {
174 public:
176 * Establish a socket connection with the given foreign
177 * address and port
178 * @param foreignAddress foreign address (IP address or name)
179 * @param foreignPort foreign port
180 * @exception SocketException thrown if unable to establish connection
182 void connect(const string &foreignAddress, unsigned short foreignPort)
183 throw(SocketException);
186 * Write the given buffer to this socket. Call connect() before
187 * calling send()
188 * @param buffer buffer to be written
189 * @param bufferLen number of bytes from buffer to be written
190 * @exception SocketException thrown if unable to send data
192 void send(const void *buffer, int bufferLen) throw(SocketException);
195 * Read into the given buffer up to bufferLen bytes data from this
196 * socket. Call connect() before calling recv()
197 * @param buffer buffer to receive the data
198 * @param bufferLen maximum number of bytes to read into buffer
199 * @return number of bytes read, 0 for EOF, and -1 for error
200 * @exception SocketException thrown if unable to receive data
202 int recv(void *buffer, int bufferLen) throw(SocketException);
205 * Read into the given buffer up to bufferLen bytes data from this
206 * socket. Call connect() before recvNonBlocking().
207 * @param buffer buffer to receive the data
208 * @param bufferLen maximum number of bytes to read into buffer
209 * @param timeout timout in ms
210 * @return number of bytes read, 0 for timeout, and -1 for error
211 * @exception SocketException thrown if unable to receive data
213 int recvNonBlocking(void *buffer, int bufferLen, int timeout) throw(SocketException);
216 * Get the foreign address. Call connect() before calling recv()
217 * @return foreign address
218 * @exception SocketException thrown if unable to fetch foreign address
220 string getForeignAddress() throw(SocketException);
223 * Get the foreign port. Call connect() before calling recv()
224 * @return foreign port
225 * @exception SocketException thrown if unable to fetch foreign port
227 unsigned short getForeignPort() throw(SocketException);
229 protected:
230 CommunicatingSocket(int type, int protocol) throw(SocketException);
231 CommunicatingSocket(int newConnSD);
235 * TCP socket for communication with other TCP sockets
237 class TCPSocket : public CommunicatingSocket {
238 public:
240 * Construct a TCP socket with no connection
241 * @exception SocketException thrown if unable to create TCP socket
243 TCPSocket() throw(SocketException);
246 * Construct a TCP socket with a connection to the given foreign address
247 * and port
248 * @param foreignAddress foreign address (IP address or name)
249 * @param foreignPort foreign port
250 * @exception SocketException thrown if unable to create TCP socket
252 TCPSocket(const string &foreignAddress, unsigned short foreignPort)
253 throw(SocketException);
255 private:
256 // Access for TCPServerSocket::accept() connection creation
257 friend class TCPServerSocket;
258 TCPSocket(int newConnSD);
262 * TCP socket class for servers
264 class TCPServerSocket : public Socket {
265 public:
267 * Construct a TCP socket for use with a server, accepting connections
268 * on the specified port on any interface
269 * @param localPort local port of server socket, a value of zero will
270 * give a system-assigned unused port
271 * @param queueLen maximum queue length for outstanding
272 * connection requests (default 5)
273 * @exception SocketException thrown if unable to create TCP server socket
275 TCPServerSocket(unsigned short localPort, int queueLen = 5)
276 throw(SocketException);
279 * Construct a TCP socket for use with a server, accepting connections
280 * on the specified port on the interface specified by the given address
281 * @param localAddress local interface (address) of server socket
282 * @param localPort local port of server socket
283 * @param queueLen maximum queue length for outstanding
284 * connection requests (default 5)
285 * @exception SocketException thrown if unable to create TCP server socket
287 TCPServerSocket(const string &localAddress, unsigned short localPort,
288 int queueLen = 5) throw(SocketException);
291 * Blocks until a new connection is established on this socket or error
292 * @return new connection socket
293 * @exception SocketException thrown if attempt to accept a new connection fails
295 TCPSocket *accept() throw(SocketException);
297 private:
298 void setListen(int queueLen) throw(SocketException);
302 * UDP socket class
304 class UDPSocket : public CommunicatingSocket {
305 public:
307 * Construct a UDP socket
308 * @exception SocketException thrown if unable to create UDP socket
310 UDPSocket() throw(SocketException);
313 * Construct a UDP socket with the given local port
314 * @param localPort local port
315 * @exception SocketException thrown if unable to create UDP socket
317 UDPSocket(unsigned short localPort) throw(SocketException);
320 * Construct a UDP socket with the given local port and address
321 * @param localAddress local address
322 * @param localPort local port
323 * @exception SocketException thrown if unable to create UDP socket
325 UDPSocket(const string &localAddress, unsigned short localPort)
326 throw(SocketException);
329 * Unset foreign address and port
330 * @return true if disassociation is successful
331 * @exception SocketException thrown if unable to disconnect UDP socket
333 void disconnect() throw(SocketException);
336 * Send the given buffer as a UDP datagram to the
337 * specified address/port
338 * @param buffer buffer to be written
339 * @param bufferLen number of bytes to write
340 * @param foreignAddress address (IP address or name) to send to
341 * @param foreignPort port number to send to
342 * @return true if send is successful
343 * @exception SocketException thrown if unable to send datagram
345 void sendTo(const void *buffer, int bufferLen, const string &foreignAddress,
346 unsigned short foreignPort) throw(SocketException);
349 * Read read up to bufferLen bytes data from this socket. The given buffer
350 * is where the data will be placed
351 * @param buffer buffer to receive data
352 * @param bufferLen maximum number of bytes to receive
353 * @param sourceAddress address of datagram source
354 * @param sourcePort port of data source
355 * @return number of bytes received and -1 for error
356 * @exception SocketException thrown if unable to receive datagram
358 int recvFrom(void *buffer, int bufferLen, string &sourceAddress,
359 unsigned short &sourcePort) throw(SocketException);
362 * Read read up to bufferLen bytes data from this socket. The given buffer
363 * is where the data will be placed
364 * @param buffer buffer to receive data
365 * @param bufferLen maximum number of bytes to receive
366 * @param sourceAddress address of datagram source
367 * @param sourcePort port of data source
368 * @param timeout int ms
369 * @return number of bytes received and -1 for error
370 * @exception SocketException thrown if unable to receive datagram
372 int recvFromNonBlocking(void *buffer, int bufferLen, string &sourceAddress,
373 unsigned short &sourcePort, int timeout) throw(SocketException);
376 * Set the multicast TTL
377 * @param multicastTTL multicast TTL
378 * @exception SocketException thrown if unable to set TTL
380 void setMulticastTTL(unsigned char multicastTTL) throw(SocketException);
383 * Join the specified multicast group
384 * @param multicastGroup multicast group address to join
385 * @exception SocketException thrown if unable to join group
387 void joinGroup(const string &multicastGroup) throw(SocketException);
390 * Leave the specified multicast group
391 * @param multicastGroup multicast group address to leave
392 * @exception SocketException thrown if unable to leave group
394 void leaveGroup(const string &multicastGroup) throw(SocketException);
396 private:
397 void setBroadcast();
400 #endif