PracticalSocket.h

Go to the documentation of this file.
00001 /*
00002  *  anytun
00003  *
00004  *  The secure anycast tunneling protocol (satp) defines a protocol used
00005  *  for communication between any combination of unicast and anycast
00006  *  tunnel endpoints.  It has less protocol overhead than IPSec in Tunnel
00007  *  mode and allows tunneling of every ETHER TYPE protocol (e.g.
00008  *  ethernet, ip, arp ...). satp directly includes cryptography and
00009  *  message authentication based on the methodes used by SRTP.  It is
00010  *  intended to deliver a generic, scaleable and secure solution for
00011  *  tunneling and relaying of packets of any protocol.
00012  *
00013  *
00014  *  Copyright (C) 2007 anytun.org <satp@wirdorange.org>
00015  *
00016  *  This program is free software; you can redistribute it and/or modify
00017  *  it under the terms of the GNU General Public License version 2
00018  *  as published by the Free Software Foundation.
00019  *
00020  *  This program is distributed in the hope that it will be useful,
00021  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
00022  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00023  *  GNU General Public License for more details.
00024  *
00025  *  You should have received a copy of the GNU General Public License
00026  *  along with this program (see the file COPYING included with this
00027  *  distribution); if not, write to the Free Software Foundation, Inc.,
00028  *  59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
00029  */
00030 
00031 // this is from: http://cs.ecs.baylor.edu/~donahoo/practical/CSockets/practical/
00032 // and this is their header:
00033 /*
00034  *   C++ sockets on Unix and Windows
00035  *   Copyright (C) 2002
00036  *
00037  *   This program is free software; you can redistribute it and/or modify
00038  *   it under the terms of the GNU General Public License as published by
00039  *   the Free Software Foundation; either version 2 of the License, or
00040  *   (at your option) any later version.
00041  *
00042  *   This program is distributed in the hope that it will be useful,
00043  *   but WITHOUT ANY WARRANTY; without even the implied warranty of
00044  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00045  *   GNU General Public License for more details.
00046  *
00047  *   You should have received a copy of the GNU General Public License
00048  *   along with this program; if not, write to the Free Software
00049  *   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
00050  */
00051 
00052 #ifndef __PRACTICALSOCKET_INCLUDED__
00053 #define __PRACTICALSOCKET_INCLUDED__
00054 
00055 #include <string>            // For string
00056 #include <exception>         // For exception class
00057 
00058 using namespace std;
00059 
00063 class SocketException : public exception {
00064 public:
00071   SocketException(const string &message, bool inclSysMsg = false) throw();
00072 
00076   ~SocketException() throw();
00077 
00082   const char *what() const throw();
00083 
00084 private:
00085   string userMessage;  // Exception message
00086 };
00087 
00091 class Socket {
00092 public:
00096   ~Socket();
00097 
00103   string getLocalAddress() throw(SocketException);
00104 
00110   unsigned short getLocalPort() throw(SocketException);
00111 
00118   void setLocalPort(unsigned short localPort) throw(SocketException);
00119 
00128   void setLocalAddressAndPort(const string &localAddress, 
00129     unsigned short localPort = 0) throw(SocketException);
00130 
00144   static void cleanUp() throw(SocketException);
00145 
00152   static unsigned short resolveService(const string &service,
00153                                        const string &protocol = "tcp");
00154 
00155 private:
00156   // Prevent the user from trying to use value semantics on this object
00157   Socket(const Socket &sock);
00158   void operator=(const Socket &sock);
00159 
00160 protected:
00161   int sockDesc;              // Socket descriptor
00162   Socket(int type, int protocol) throw(SocketException);
00163   Socket(int sockDesc);
00164 };
00165 
00169 class CommunicatingSocket : public Socket {
00170 public:
00178   void connect(const string &foreignAddress, unsigned short foreignPort)
00179     throw(SocketException);
00180 
00188   void send(const void *buffer, int bufferLen) throw(SocketException);
00189 
00198   int recv(void *buffer, int bufferLen) throw(SocketException);
00199 
00205   string getForeignAddress() throw(SocketException);
00206 
00212   unsigned short getForeignPort() throw(SocketException);
00213 
00214 protected:
00215   CommunicatingSocket(int type, int protocol) throw(SocketException);
00216   CommunicatingSocket(int newConnSD);
00217 };
00218 
00222 class TCPSocket : public CommunicatingSocket {
00223 public:
00228   TCPSocket() throw(SocketException);
00229 
00237   TCPSocket(const string &foreignAddress, unsigned short foreignPort) 
00238       throw(SocketException);
00239 
00240 private:
00241   // Access for TCPServerSocket::accept() connection creation
00242   friend class TCPServerSocket;
00243   TCPSocket(int newConnSD);
00244 };
00245 
00249 class TCPServerSocket : public Socket {
00250 public:
00260   TCPServerSocket(unsigned short localPort, int queueLen = 5) 
00261       throw(SocketException);
00262 
00272   TCPServerSocket(const string &localAddress, unsigned short localPort,
00273       int queueLen = 5) throw(SocketException);
00274 
00280   TCPSocket *accept() throw(SocketException);
00281 
00282 private:
00283   void setListen(int queueLen) throw(SocketException);
00284 };
00285 
00289 class UDPSocket : public CommunicatingSocket {
00290 public:
00295   UDPSocket() throw(SocketException);
00296 
00302   UDPSocket(unsigned short localPort) throw(SocketException);
00303 
00310   UDPSocket(const string &localAddress, unsigned short localPort) 
00311       throw(SocketException);
00312 
00318   void disconnect() throw(SocketException);
00319 
00330   void sendTo(const void *buffer, int bufferLen, const string &foreignAddress,
00331             unsigned short foreignPort) throw(SocketException);
00332 
00343   int recvFrom(void *buffer, int bufferLen, string &sourceAddress, 
00344                unsigned short &sourcePort) throw(SocketException);
00345 
00351   void setMulticastTTL(unsigned char multicastTTL) throw(SocketException);
00352 
00358   void joinGroup(const string &multicastGroup) throw(SocketException);
00359 
00365   void leaveGroup(const string &multicastGroup) throw(SocketException);
00366 
00367 private:
00368   void setBroadcast();
00369 };
00370 
00371 #endif

Generated on Sun Oct 7 23:43:49 2007 for anytun by  doxygen 1.5.1