# socks.h

Go to the documentation of this file.

    00001 /*
    00002  * socks.h
    00003  *
    00004  * SOCKS protocol
    00005  *
    00006  * Portable Windows Library
    00007  *
    00008  * Copyright (c) 1993-2001 Equivalence Pty. Ltd.
    00009  *
    00010  * The contents of this file are subject to the Mozilla Public License
    00011  * Version 1.0 (the "License"); you may not use this file except in
    00012  * compliance with the License. You may obtain a copy of the License at
    00013  * http://www.mozilla.org/MPL/
    00014  *
    00015  * Software distributed under the License is distributed on an "AS IS"
    00016  * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See
    00017  * the License for the specific language governing rights and limitations
    00018  * under the License.
    00019  *
    00020  * The Original Code is Portable Windows Library.
    00021  *
    00022  * The Initial Developer of the Original Code is Equivalence Pty. Ltd.
    00023  *
    00024  * Contributor(s): ______________________________________.
    00025  *
    00026  * $Revision: 24177 $
    00027  * $Author: rjongbloed $
    00028  * $Date: 2010-04-05 06:52:04 -0500 (Mon, 05 Apr 2010) $
    00029  */
    00030
    00031 #ifndef PTLIB_SOCKS_H
    00032 #define PTLIB_SOCKS_H
    00033
    00034 #ifdef P_USE_PRAGMA
    00035 #pragma interface
    00036 #endif
    00037
    00038
    00039 #include <ptlib/sockets.h>
    00040
    00041
    00047 class PSocksProtocol
    00048 {
    00049   public:
    00050     PSocksProtocol(WORD port);
    00051     virtual ~PSocksProtocol() { }
    00052
    00053   // New functions for class
    00054     enum {
    00055       DefaultServerPort = 1080
    00056     };
    00057     PBoolean SetServer(
    00058       const PString & hostname,
    00059       const char * service = "socks 1080"
    00060     );
    00061     PBoolean SetServer(
    00062       const PString & hostname,
    00063       WORD port
    00064     );
    00065
    00070     void SetAuthentication(
    00071       const PString & username,
    00072       const PString & password
    00073     );
    00074
    00075   protected:
    00076     PBoolean ConnectSocksServer(PTCPSocket & thisSocket);
    00077
    00078     virtual void SetErrorCodes(PChannel::Errors errCode, int osErr) = 0;
    00079
    00080     virtual PBoolean SendSocksCommand(PTCPSocket & socket,
    00081                                   BYTE command,
    00082                                   const char * hostname,
    00083                                   PIPSocket::Address addr);
    00084     virtual PBoolean ReceiveSocksResponse(PTCPSocket & socket,
    00085                                       PIPSocket::Address & addr,
    00086                                       WORD & port);
    00087
    00088
    00089     PString            serverHost;
    00090     WORD               serverPort;
    00091     PString            authenticationUsername;
    00092     PString            authenticationPassword;
    00093     PIPSocket::Address remoteAddress;
    00094     WORD               remotePort;
    00095     PIPSocket::Address localAddress;
    00096     WORD               localPort;
    00097 };
    00098
    00099
    00102 class PSocksSocket : public PTCPSocket, public PSocksProtocol
    00103 {
    00104   PCLASSINFO(PSocksSocket, PTCPSocket)
    00105
    00106   public:
    00107     PSocksSocket(
    00108       WORD port = 0
    00109     );
    00110
    00111   // Overrides from class PSocket.
    00123     virtual PBoolean Connect(
    00124       const PString & address
    00125     );
    00126     virtual PBoolean Connect(
    00127       const Address & addr
    00128     );
    00129
    00145     virtual PBoolean Listen(
    00146       unsigned queueSize = 5,
    00147       WORD port = 0,
    00148       Reusability reuse = AddressIsExclusive
    00149     );
    00150
    00170     PBoolean Accept();
    00171     virtual PBoolean Accept(
    00172       PSocket & socket
    00173     );
    00174
    00175
    00176   // Overrides from class PIPSocket.
    00182     virtual PBoolean GetLocalAddress(
    00183       Address & addr
    00184     );
    00185     virtual PBoolean GetLocalAddress(
    00186       Address & addr,
    00187       WORD & port
    00188     );
    00189
    00196     virtual PBoolean GetPeerAddress(
    00197       Address & addr
    00198     );
    00199     virtual PBoolean GetPeerAddress(
    00200       Address & addr,
    00201       WORD & port
    00202     );
    00203
    00204
    00205   protected:
    00206     virtual void SetErrorCodes(PChannel::Errors errCode, int osErr);
    00207     int TransferHandle(PSocksSocket & destination);
    00208
    00209   private:
    00210     virtual PBoolean Connect(WORD localPort, const Address & addr);
    00211 };
    00212
    00213
    00216 class PSocks4Socket : public PSocksSocket
    00217 {
    00218   PCLASSINFO(PSocks4Socket, PSocksSocket)
    00219
    00220   public:
    00221     PSocks4Socket(
    00222       WORD port = 0
    00223     );
    00224     PSocks4Socket(
    00225       const PString & host,
    00226       WORD port = 0
    00227     );
    00228
    00229   // Overrides from class PObject
    00242     virtual PObject * Clone() const;
    00243
    00244
    00245   protected:
    00246     virtual PBoolean SendSocksCommand(PTCPSocket & socket,
    00247                                   BYTE command,
    00248                                   const char * hostname,
    00249                                   PIPSocket::Address addr);
    00250     virtual PBoolean ReceiveSocksResponse(PTCPSocket & socket,
    00251                                       PIPSocket::Address & addr,
    00252                                       WORD & port);
    00253 };
    00254
    00255
    00258 class PSocks5Socket : public PSocksSocket
    00259 {
    00260   PCLASSINFO(PSocks5Socket, PSocksSocket)
    00261
    00262   public:
    00263     PSocks5Socket(
    00264       WORD port = 0
    00265     );
    00266     PSocks5Socket(
    00267       const PString & host,
    00268       WORD port = 0
    00269     );
    00270
    00271   // Overrides from class PObject
    00284     virtual PObject * Clone() const;
    00285 };
    00286
    00287
    00290 class PSocksUDPSocket : public PUDPSocket, public PSocksProtocol
    00291 {
    00292   PCLASSINFO(PSocksUDPSocket, PUDPSocket)
    00293
    00294   public:
    00295     PSocksUDPSocket(
    00296       WORD port = 0
    00297     );
    00298     PSocksUDPSocket(
    00299       const PString & host,
    00300       WORD port = 0
    00301     );
    00302
    00303
    00304   // Overrides from class PObject
    00317     virtual PObject * Clone() const;
    00318
    00319
    00320   // Overrides from class PSocket.
    00332     virtual PBoolean Connect(
    00333       const PString & address   // Address of remote machine to connect to.
    00334     );
    00335     virtual PBoolean Connect(
    00336       const Address & addr      // Address of remote machine to connect to.
    00337     );
    00338
    00354     virtual PBoolean Listen(
    00355       unsigned queueSize = 5,  // Number of pending accepts that may be queued.
    00356       WORD port = 0,           // Port number to use for the connection.
    00357       Reusability reuse = AddressIsExclusive // Can/Cant listen more than once.
    00358     );
    00359
    00360   // Overrides from class PIPSocket.
    00366     virtual PBoolean GetLocalAddress(
    00367       Address & addr    // Variable to receive hosts IP address
    00368     );
    00369     virtual PBoolean GetLocalAddress(
    00370       Address & addr,    // Variable to receive peer hosts IP address
    00371       WORD & port        // Variable to receive peer hosts port number
    00372     );
    00373
    00380     virtual PBoolean GetPeerAddress(
    00381       Address & addr    // Variable to receive hosts IP address
    00382     );
    00383     virtual PBoolean GetPeerAddress(
    00384       Address & addr,    // Variable to receive peer hosts IP address
    00385       WORD & port        // Variable to receive peer hosts port number
    00386     );
    00387
    00388
    00389   // Overrides from class PIPDatagramSocket.
    00395     virtual PBoolean ReadFrom(
    00396       void * buf,     // Data to be written as URGENT TCP data.
    00397       PINDEX len,     // Number of bytes pointed to by <CODE>buf</CODE>.
    00398       Address & addr, // Address from which the datagram was received.
    00399       WORD & port     // Port from which the datagram was received.
    00400     );
    00401
    00407     virtual PBoolean WriteTo(
    00408       const void * buf,   // Data to be written as URGENT TCP data.
    00409       PINDEX len,         // Number of bytes pointed to by <CODE>buf</CODE>.
    00410       const Address & addr, // Address to which the datagram is sent.
    00411       WORD port           // Port to which the datagram is sent.
    00412     );
    00413
    00414
    00415   protected:
    00416     virtual void SetErrorCodes(PChannel::Errors errCode, int osErr);
    00417
    00418     PTCPSocket socksControl;
    00419     Address    serverAddress;
    00420
    00421   private:
    00422     virtual PBoolean Connect(WORD localPort, const Address & addr);
    00423 };
    00424
    00425
    00426 #endif  // PTLIB_SOCKS_H
    00427
    00428
    00429 // End of File ///////////////////////////////////////////////////////////////