00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031 #ifndef _SOCKS_H
00032 #define _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
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
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
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
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
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
00317 virtual PObject * Clone() const;
00318
00319
00320
00332 virtual PBoolean Connect(
00333 const PString & address
00334 );
00335 virtual PBoolean Connect(
00336 const Address & addr
00337 );
00338
00354 virtual PBoolean Listen(
00355 unsigned queueSize = 5,
00356 WORD port = 0,
00357 Reusability reuse = AddressIsExclusive
00358 );
00359
00360
00366 virtual PBoolean GetLocalAddress(
00367 Address & addr
00368 );
00369 virtual PBoolean GetLocalAddress(
00370 Address & addr,
00371 WORD & port
00372 );
00373
00380 virtual PBoolean GetPeerAddress(
00381 Address & addr
00382 );
00383 virtual PBoolean GetPeerAddress(
00384 Address & addr,
00385 WORD & port
00386 );
00387
00388
00389
00395 virtual PBoolean ReadFrom(
00396 void * buf,
00397 PINDEX len,
00398 Address & addr,
00399 WORD & port
00400 );
00401
00407 virtual PBoolean WriteTo(
00408 const void * buf,
00409 PINDEX len,
00410 const Address & addr,
00411 WORD port
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 // _SOCKS_H
00427
00428
00429