added more options
[anytun.git] / PracticalSocket.h
blob0192cfd0991a9475481123575312ac1cdca1bf8e
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 * Get the foreign address. Call connect() before calling recv()
206 * @return foreign address
207 * @exception SocketException thrown if unable to fetch foreign address
209 string getForeignAddress() throw(SocketException);
212 * Get the foreign port. Call connect() before calling recv()
213 * @return foreign port
214 * @exception SocketException thrown if unable to fetch foreign port
216 unsigned short getForeignPort() throw(SocketException);
218 protected:
219 CommunicatingSocket(int type, int protocol) throw(SocketException);
220 CommunicatingSocket(int newConnSD);
224 * TCP socket for communication with other TCP sockets
226 class TCPSocket : public CommunicatingSocket {
227 public:
229 * Construct a TCP socket with no connection
230 * @exception SocketException thrown if unable to create TCP socket
232 TCPSocket() throw(SocketException);
235 * Construct a TCP socket with a connection to the given foreign address
236 * and port
237 * @param foreignAddress foreign address (IP address or name)
238 * @param foreignPort foreign port
239 * @exception SocketException thrown if unable to create TCP socket
241 TCPSocket(const string &foreignAddress, unsigned short foreignPort)
242 throw(SocketException);
244 private:
245 // Access for TCPServerSocket::accept() connection creation
246 friend class TCPServerSocket;
247 TCPSocket(int newConnSD);
251 * TCP socket class for servers
253 class TCPServerSocket : public Socket {
254 public:
256 * Construct a TCP socket for use with a server, accepting connections
257 * on the specified port on any interface
258 * @param localPort local port of server socket, a value of zero will
259 * give a system-assigned unused port
260 * @param queueLen maximum queue length for outstanding
261 * connection requests (default 5)
262 * @exception SocketException thrown if unable to create TCP server socket
264 TCPServerSocket(unsigned short localPort, int queueLen = 5)
265 throw(SocketException);
268 * Construct a TCP socket for use with a server, accepting connections
269 * on the specified port on the interface specified by the given address
270 * @param localAddress local interface (address) of server socket
271 * @param localPort local port of server socket
272 * @param queueLen maximum queue length for outstanding
273 * connection requests (default 5)
274 * @exception SocketException thrown if unable to create TCP server socket
276 TCPServerSocket(const string &localAddress, unsigned short localPort,
277 int queueLen = 5) throw(SocketException);
280 * Blocks until a new connection is established on this socket or error
281 * @return new connection socket
282 * @exception SocketException thrown if attempt to accept a new connection fails
284 TCPSocket *accept() throw(SocketException);
286 private:
287 void setListen(int queueLen) throw(SocketException);
291 * UDP socket class
293 class UDPSocket : public CommunicatingSocket {
294 public:
296 * Construct a UDP socket
297 * @exception SocketException thrown if unable to create UDP socket
299 UDPSocket() throw(SocketException);
302 * Construct a UDP socket with the given local port
303 * @param localPort local port
304 * @exception SocketException thrown if unable to create UDP socket
306 UDPSocket(unsigned short localPort) throw(SocketException);
309 * Construct a UDP socket with the given local port and address
310 * @param localAddress local address
311 * @param localPort local port
312 * @exception SocketException thrown if unable to create UDP socket
314 UDPSocket(const string &localAddress, unsigned short localPort)
315 throw(SocketException);
318 * Unset foreign address and port
319 * @return true if disassociation is successful
320 * @exception SocketException thrown if unable to disconnect UDP socket
322 void disconnect() throw(SocketException);
325 * Send the given buffer as a UDP datagram to the
326 * specified address/port
327 * @param buffer buffer to be written
328 * @param bufferLen number of bytes to write
329 * @param foreignAddress address (IP address or name) to send to
330 * @param foreignPort port number to send to
331 * @return true if send is successful
332 * @exception SocketException thrown if unable to send datagram
334 void sendTo(const void *buffer, int bufferLen, const string &foreignAddress,
335 unsigned short foreignPort) throw(SocketException);
338 * Read read up to bufferLen bytes data from this socket. The given buffer
339 * is where the data will be placed
340 * @param buffer buffer to receive data
341 * @param bufferLen maximum number of bytes to receive
342 * @param sourceAddress address of datagram source
343 * @param sourcePort port of data source
344 * @return number of bytes received and -1 for error
345 * @exception SocketException thrown if unable to receive datagram
347 int recvFrom(void *buffer, int bufferLen, string &sourceAddress,
348 unsigned short &sourcePort) throw(SocketException);
351 * Set the multicast TTL
352 * @param multicastTTL multicast TTL
353 * @exception SocketException thrown if unable to set TTL
355 void setMulticastTTL(unsigned char multicastTTL) throw(SocketException);
358 * Join the specified multicast group
359 * @param multicastGroup multicast group address to join
360 * @exception SocketException thrown if unable to join group
362 void joinGroup(const string &multicastGroup) throw(SocketException);
365 * Leave the specified multicast group
366 * @param multicastGroup multicast group address to leave
367 * @exception SocketException thrown if unable to leave group
369 void leaveGroup(const string &multicastGroup) throw(SocketException);
371 private:
372 void setBroadcast();
375 #endif