PWLib
1.10.10
|
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 * $Log: socks.h,v $ 00027 * Revision 1.8 2005/11/30 12:47:37 csoutheren 00028 * Removed tabs, reformatted some code, and changed tags for Doxygen 00029 * 00030 * Revision 1.7 2002/11/06 22:47:24 robertj 00031 * Fixed header comment (copyright etc) 00032 * 00033 * Revision 1.6 2002/09/16 01:08:59 robertj 00034 * Added #define so can select if #pragma interface/implementation is used on 00035 * platform basis (eg MacOS) rather than compiler, thanks Robert Monaghan. 00036 * 00037 * Revision 1.5 2002/08/05 05:40:45 robertj 00038 * Fixed missing pragma interface/implementation 00039 * 00040 * Revision 1.4 1999/05/01 03:52:20 robertj 00041 * Fixed various egcs warnings. 00042 * 00043 * Revision 1.3 1999/03/09 08:01:47 robertj 00044 * Changed comments for doc++ support (more to come). 00045 * 00046 * Revision 1.2 1998/12/23 00:33:05 robertj 00047 * UDP support 00048 * 00049 * Revision 1.1 1998/12/22 10:34:17 robertj 00050 * Initial revision 00051 * 00052 */ 00053 00054 #ifndef _SOCKS_H 00055 #define _SOCKS_H 00056 00057 #ifdef P_USE_PRAGMA 00058 #pragma interface 00059 #endif 00060 00061 00062 #include <ptlib/sockets.h> 00063 00064 00070 class PSocksProtocol 00071 { 00072 public: 00073 PSocksProtocol(WORD port); 00074 virtual ~PSocksProtocol() { } 00075 00076 // New functions for class 00077 enum { 00078 DefaultServerPort = 1080 00079 }; 00080 BOOL SetServer( 00081 const PString & hostname, 00082 const char * service = "socks 1080" 00083 ); 00084 BOOL SetServer( 00085 const PString & hostname, 00086 WORD port 00087 ); 00088 00093 void SetAuthentication( 00094 const PString & username, 00095 const PString & password 00096 ); 00097 00098 protected: 00099 BOOL ConnectSocksServer(PTCPSocket & thisSocket); 00100 00101 virtual void SetErrorCodes(PChannel::Errors errCode, int osErr) = 0; 00102 00103 virtual BOOL SendSocksCommand(PTCPSocket & socket, 00104 BYTE command, 00105 const char * hostname, 00106 PIPSocket::Address addr); 00107 virtual BOOL ReceiveSocksResponse(PTCPSocket & socket, 00108 PIPSocket::Address & addr, 00109 WORD & port); 00110 00111 00112 PString serverHost; 00113 WORD serverPort; 00114 PString authenticationUsername; 00115 PString authenticationPassword; 00116 PIPSocket::Address remoteAddress; 00117 WORD remotePort; 00118 PIPSocket::Address localAddress; 00119 WORD localPort; 00120 }; 00121 00122 00125 class PSocksSocket : public PTCPSocket, public PSocksProtocol 00126 { 00127 PCLASSINFO(PSocksSocket, PTCPSocket) 00128 00129 public: 00130 PSocksSocket( 00131 WORD port = 0 00132 ); 00133 00134 // Overrides from class PSocket. 00146 virtual BOOL Connect( 00147 const PString & address 00148 ); 00149 virtual BOOL Connect( 00150 const Address & addr 00151 ); 00152 00168 virtual BOOL Listen( 00169 unsigned queueSize = 5, 00170 WORD port = 0, 00171 Reusability reuse = AddressIsExclusive 00172 ); 00173 00193 BOOL Accept(); 00194 virtual BOOL Accept( 00195 PSocket & socket 00196 ); 00197 00198 00199 // Overrides from class PIPSocket. 00205 virtual BOOL GetLocalAddress( 00206 Address & addr 00207 ); 00208 virtual BOOL GetLocalAddress( 00209 Address & addr, 00210 WORD & port 00211 ); 00212 00219 virtual BOOL GetPeerAddress( 00220 Address & addr 00221 ); 00222 virtual BOOL GetPeerAddress( 00223 Address & addr, 00224 WORD & port 00225 ); 00226 00227 00228 protected: 00229 virtual void SetErrorCodes(PChannel::Errors errCode, int osErr); 00230 int TransferHandle(PSocksSocket & destination); 00231 00232 private: 00233 virtual BOOL Connect(WORD localPort, const Address & addr); 00234 }; 00235 00236 00239 class PSocks4Socket : public PSocksSocket 00240 { 00241 PCLASSINFO(PSocks4Socket, PSocksSocket) 00242 00243 public: 00244 PSocks4Socket( 00245 WORD port = 0 00246 ); 00247 PSocks4Socket( 00248 const PString & host, 00249 WORD port = 0 00250 ); 00251 00252 // Overrides from class PObject 00265 virtual PObject * Clone() const; 00266 00267 00268 protected: 00269 virtual BOOL SendSocksCommand(PTCPSocket & socket, 00270 BYTE command, 00271 const char * hostname, 00272 PIPSocket::Address addr); 00273 virtual BOOL ReceiveSocksResponse(PTCPSocket & socket, 00274 PIPSocket::Address & addr, 00275 WORD & port); 00276 }; 00277 00278 00281 class PSocks5Socket : public PSocksSocket 00282 { 00283 PCLASSINFO(PSocks5Socket, PSocksSocket) 00284 00285 public: 00286 PSocks5Socket( 00287 WORD port = 0 00288 ); 00289 PSocks5Socket( 00290 const PString & host, 00291 WORD port = 0 00292 ); 00293 00294 // Overrides from class PObject 00307 virtual PObject * Clone() const; 00308 }; 00309 00310 00313 class PSocksUDPSocket : public PUDPSocket, public PSocksProtocol 00314 { 00315 PCLASSINFO(PSocksUDPSocket, PUDPSocket) 00316 00317 public: 00318 PSocksUDPSocket( 00319 WORD port = 0 00320 ); 00321 PSocksUDPSocket( 00322 const PString & host, 00323 WORD port = 0 00324 ); 00325 00326 00327 // Overrides from class PObject 00340 virtual PObject * Clone() const; 00341 00342 00343 // Overrides from class PSocket. 00355 virtual BOOL Connect( 00356 const PString & address // Address of remote machine to connect to. 00357 ); 00358 virtual BOOL Connect( 00359 const Address & addr // Address of remote machine to connect to. 00360 ); 00361 00377 virtual BOOL Listen( 00378 unsigned queueSize = 5, // Number of pending accepts that may be queued. 00379 WORD port = 0, // Port number to use for the connection. 00380 Reusability reuse = AddressIsExclusive // Can/Cant listen more than once. 00381 ); 00382 00383 // Overrides from class PIPSocket. 00389 virtual BOOL GetLocalAddress( 00390 Address & addr // Variable to receive hosts IP address 00391 ); 00392 virtual BOOL GetLocalAddress( 00393 Address & addr, // Variable to receive peer hosts IP address 00394 WORD & port // Variable to receive peer hosts port number 00395 ); 00396 00403 virtual BOOL GetPeerAddress( 00404 Address & addr // Variable to receive hosts IP address 00405 ); 00406 virtual BOOL GetPeerAddress( 00407 Address & addr, // Variable to receive peer hosts IP address 00408 WORD & port // Variable to receive peer hosts port number 00409 ); 00410 00411 00412 // Overrides from class PIPDatagramSocket. 00418 virtual BOOL ReadFrom( 00419 void * buf, // Data to be written as URGENT TCP data. 00420 PINDEX len, // Number of bytes pointed to by <CODE>buf</CODE>. 00421 Address & addr, // Address from which the datagram was received. 00422 WORD & port // Port from which the datagram was received. 00423 ); 00424 00430 virtual BOOL WriteTo( 00431 const void * buf, // Data to be written as URGENT TCP data. 00432 PINDEX len, // Number of bytes pointed to by <CODE>buf</CODE>. 00433 const Address & addr, // Address to which the datagram is sent. 00434 WORD port // Port to which the datagram is sent. 00435 ); 00436 00437 00438 protected: 00439 virtual void SetErrorCodes(PChannel::Errors errCode, int osErr); 00440 00441 PTCPSocket socksControl; 00442 Address serverAddress; 00443 00444 private: 00445 virtual BOOL Connect(WORD localPort, const Address & addr); 00446 }; 00447 00448 00449 #endif // _SOCKS_H 00450 00451 00452 // End of File ///////////////////////////////////////////////////////////////