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
00032
00033
00034
00035
00036
00037
00038
00039
00040
00041
00042
00043
00044
00045
00046
00047
00048
00049
00050
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
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
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
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
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
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
00340 virtual PObject * Clone() const;
00341
00342
00343
00355 virtual BOOL Connect(
00356 const PString & address
00357 );
00358 virtual BOOL Connect(
00359 const Address & addr
00360 );
00361
00377 virtual BOOL Listen(
00378 unsigned queueSize = 5,
00379 WORD port = 0,
00380 Reusability reuse = AddressIsExclusive
00381 );
00382
00383
00389 virtual BOOL GetLocalAddress(
00390 Address & addr
00391 );
00392 virtual BOOL GetLocalAddress(
00393 Address & addr,
00394 WORD & port
00395 );
00396
00403 virtual BOOL GetPeerAddress(
00404 Address & addr
00405 );
00406 virtual BOOL GetPeerAddress(
00407 Address & addr,
00408 WORD & port
00409 );
00410
00411
00412
00418 virtual BOOL ReadFrom(
00419 void * buf,
00420 PINDEX len,
00421 Address & addr,
00422 WORD & port
00423 );
00424
00430 virtual BOOL WriteTo(
00431 const void * buf,
00432 PINDEX len,
00433 const Address & addr,
00434 WORD port
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