socks.h
Go to the documentation of this file.
/*
* socks.h
*
* SOCKS protocol
*
* Portable Windows Library
*
* Copyright (c) 1993-2001 Equivalence Pty. Ltd.
*
* The contents of this file are subject to the Mozilla Public License
* Version 1.0 (the "License"); you may not use this file except in
* compliance with the License. You may obtain a copy of the License at
* http://www.mozilla.org/MPL/
*
* Software distributed under the License is distributed on an "AS IS"
* basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See
* the License for the specific language governing rights and limitations
* under the License.
*
* The Original Code is Portable Windows Library.
*
* The Initial Developer of the Original Code is Equivalence Pty. Ltd.
*
* Contributor(s): ______________________________________.
*
* $Revision: 24177 $
* $Author: rjongbloed $
* $Date: 2010-04-05 06:52:04 -0500 (Mon, 05 Apr 2010) $
*/
#ifndef PTLIB_SOCKS_H
#define PTLIB_SOCKS_H
#ifdef P_USE_PRAGMA
#pragma interface
#endif
#include <ptlib/sockets.h>
class PSocksProtocol
{
public:
PSocksProtocol(WORD port);
virtual ~PSocksProtocol() { }
// New functions for class
enum {
DefaultServerPort = 1080
};
PBoolean SetServer(
const PString & hostname,
const char * service = "socks 1080"
);
PBoolean SetServer(
const PString & hostname,
WORD port
);
void SetAuthentication(
const PString & username,
const PString & password
);
protected:
PBoolean ConnectSocksServer(PTCPSocket & thisSocket);
virtual void SetErrorCodes(PChannel::Errors errCode, int osErr) = 0;
virtual PBoolean SendSocksCommand(PTCPSocket & socket,
BYTE command,
const char * hostname,
PIPSocket::Address addr);
virtual PBoolean ReceiveSocksResponse(PTCPSocket & socket,
PIPSocket::Address & addr,
WORD & port);
PString serverHost;
WORD serverPort;
PString authenticationUsername;
PString authenticationPassword;
PIPSocket::Address remoteAddress;
WORD remotePort;
PIPSocket::Address localAddress;
WORD localPort;
};
class PSocksSocket : public PTCPSocket, public PSocksProtocol
{
PCLASSINFO(PSocksSocket, PTCPSocket)
public:
PSocksSocket(
WORD port = 0
);
// Overrides from class PSocket.
virtual PBoolean Connect(
const PString & address
);
virtual PBoolean Connect(
const Address & addr
);
virtual PBoolean Listen(
unsigned queueSize = 5,
WORD port = 0,
Reusability reuse = AddressIsExclusive
);
PBoolean Accept();
virtual PBoolean Accept(
PSocket & socket
);
// Overrides from class PIPSocket.
virtual PBoolean GetLocalAddress(
Address & addr
);
virtual PBoolean GetLocalAddress(
Address & addr,
WORD & port
);
virtual PBoolean GetPeerAddress(
Address & addr
);
virtual PBoolean GetPeerAddress(
Address & addr,
WORD & port
);
protected:
virtual void SetErrorCodes(PChannel::Errors errCode, int osErr);
int TransferHandle(PSocksSocket & destination);
private:
virtual PBoolean Connect(WORD localPort, const Address & addr);
};
class PSocks4Socket : public PSocksSocket
{
PCLASSINFO(PSocks4Socket, PSocksSocket)
public:
PSocks4Socket(
WORD port = 0
);
PSocks4Socket(
const PString & host,
WORD port = 0
);
// Overrides from class PObject
virtual PObject * Clone() const;
protected:
virtual PBoolean SendSocksCommand(PTCPSocket & socket,
BYTE command,
const char * hostname,
PIPSocket::Address addr);
virtual PBoolean ReceiveSocksResponse(PTCPSocket & socket,
PIPSocket::Address & addr,
WORD & port);
};
class PSocks5Socket : public PSocksSocket
{
PCLASSINFO(PSocks5Socket, PSocksSocket)
public:
PSocks5Socket(
WORD port = 0
);
PSocks5Socket(
const PString & host,
WORD port = 0
);
// Overrides from class PObject
virtual PObject * Clone() const;
};
class PSocksUDPSocket : public PUDPSocket, public PSocksProtocol
{
PCLASSINFO(PSocksUDPSocket, PUDPSocket)
public:
PSocksUDPSocket(
WORD port = 0
);
PSocksUDPSocket(
const PString & host,
WORD port = 0
);
// Overrides from class PObject
virtual PObject * Clone() const;
// Overrides from class PSocket.
virtual PBoolean Connect(
const PString & address // Address of remote machine to connect to.
);
virtual PBoolean Connect(
const Address & addr // Address of remote machine to connect to.
);
virtual PBoolean Listen(
unsigned queueSize = 5, // Number of pending accepts that may be queued.
WORD port = 0, // Port number to use for the connection.
Reusability reuse = AddressIsExclusive // Can/Cant listen more than once.
);
// Overrides from class PIPSocket.
virtual PBoolean GetLocalAddress(
Address & addr // Variable to receive hosts IP address
);
virtual PBoolean GetLocalAddress(
Address & addr, // Variable to receive peer hosts IP address
WORD & port // Variable to receive peer hosts port number
);
virtual PBoolean GetPeerAddress(
Address & addr // Variable to receive hosts IP address
);
virtual PBoolean GetPeerAddress(
Address & addr, // Variable to receive peer hosts IP address
WORD & port // Variable to receive peer hosts port number
);
// Overrides from class PIPDatagramSocket.
virtual PBoolean ReadFrom(
void * buf, // Data to be written as URGENT TCP data.
PINDEX len, // Number of bytes pointed to by <CODE>buf</CODE>.
Address & addr, // Address from which the datagram was received.
WORD & port // Port from which the datagram was received.
);
virtual PBoolean WriteTo(
const void * buf, // Data to be written as URGENT TCP data.
PINDEX len, // Number of bytes pointed to by <CODE>buf</CODE>.
const Address & addr, // Address to which the datagram is sent.
WORD port // Port to which the datagram is sent.
);
protected:
virtual void SetErrorCodes(PChannel::Errors errCode, int osErr);
PTCPSocket socksControl;
Address serverAddress;
private:
virtual PBoolean Connect(WORD localPort, const Address & addr);
};
#endif // PTLIB_SOCKS_H
// End of File ///////////////////////////////////////////////////////////////
PSocket::AddressIsExclusive
Definition: socket.h:86
PSocksUDPSocket::PSocksUDPSocket
PSocksUDPSocket(WORD port=0)
PCLASSINFO
#define PCLASSINFO(cls, par)
Declare all the standard PTLib class information.
Definition: object.h:1049
PSocks4Socket::Clone
virtual PObject * Clone() const
Create a copy of the class on the heap.
PSocksProtocol::localPort
WORD localPort
Definition: socks.h:96
PSocksSocket::TransferHandle
int TransferHandle(PSocksSocket &destination)
PSocksProtocol
Base class for implementing the SOCKS protocol for use with PSocksSocket and PSocksUDPSocket.
Definition: socks.h:47
PUDPSocket
A socket channel that uses the UDP transport on the Internet Protocol.
Definition: udpsock.h:47
PSocksProtocol::localAddress
PIPSocket::Address localAddress
Definition: socks.h:95
PSocksSocket::Listen
virtual PBoolean Listen(unsigned queueSize=5, WORD port=0, Reusability reuse=AddressIsExclusive)
Listen on a socket for a remote host on the specified port number.
PIPSocket::GetLocalAddress
virtual PString GetLocalAddress()
Get the Internet Protocol address and port for the local host.
PSocksProtocol::PSocksProtocol
PSocksProtocol(WORD port)
PSocksProtocol::DefaultServerPort
Definition: socks.h:55
PSocksProtocol::serverHost
PString serverHost
Definition: socks.h:89
PSocksUDPSocket::WriteTo
virtual PBoolean WriteTo(const void *buf, PINDEX len, const Address &addr, WORD port)
Write a datagram to a remote computer.
PSocks4Socket::SendSocksCommand
virtual PBoolean SendSocksCommand(PTCPSocket &socket, BYTE command, const char *hostname, PIPSocket::Address addr)
PSocket::port
WORD port
Port to be used by the socket when opening the channel.
Definition: socket.h:444
PSocksProtocol::~PSocksProtocol
virtual ~PSocksProtocol()
Definition: socks.h:51
PSocksSocket::Accept
PBoolean Accept()
Open a socket to a remote host on the specified port number.
PSocksSocket::PSocksSocket
PSocksSocket(WORD port=0)
PChannel::Errors
Errors
Normalised error codes.
Definition: channel.h:529
PBoolean
BOOL PBoolean
Definition: object.h:102
PSocks5Socket
This class allows access to RFC1928 compliant SOCKS server.
Definition: socks.h:258
PSocksUDPSocket::socksControl
PTCPSocket socksControl
Definition: socks.h:418
PSocks4Socket::ReceiveSocksResponse
virtual PBoolean ReceiveSocksResponse(PTCPSocket &socket, PIPSocket::Address &addr, WORD &port)
PSocksSocket
This is an ancestor class allowing access to a SOCKS servers (version 4 and 5).
Definition: socks.h:102
PSocksProtocol::SendSocksCommand
virtual PBoolean SendSocksCommand(PTCPSocket &socket, BYTE command, const char *hostname, PIPSocket::Address addr)
PString
The character string class.
Definition: pstring.h:108
PSocksProtocol::remotePort
WORD remotePort
Definition: socks.h:94
PSocksProtocol::remoteAddress
PIPSocket::Address remoteAddress
Definition: socks.h:93
PSocksSocket::Connect
virtual PBoolean Connect(const PString &address)
Connect a socket to a remote host on the specified port number.
PTCPSocket
A socket that uses the TCP transport on the Internet Protocol.
Definition: tcpsock.h:44
PSocksProtocol::authenticationUsername
PString authenticationUsername
Definition: socks.h:91
PSocksProtocol::SetServer
PBoolean SetServer(const PString &hostname, const char *service="socks 1080")
PSocksSocket::SetErrorCodes
virtual void SetErrorCodes(PChannel::Errors errCode, int osErr)
PSocksUDPSocket::ReadFrom
virtual PBoolean ReadFrom(void *buf, PINDEX len, Address &addr, WORD &port)
Read a datagram from a remote computer.
PIPSocket::Address
A class describing an IP address.
Definition: ipsock.h:75
PSocksUDPSocket
This class allows access to RFC1928 compliant SOCKS server.
Definition: socks.h:290
PIPSocket::GetPeerAddress
virtual PString GetPeerAddress()
Get the Internet Protocol address for the peer host and port the socket is connected to...
PSocksProtocol::ReceiveSocksResponse
virtual PBoolean ReceiveSocksResponse(PTCPSocket &socket, PIPSocket::Address &addr, WORD &port)
PSocksUDPSocket::SetErrorCodes
virtual void SetErrorCodes(PChannel::Errors errCode, int osErr)
PSocksProtocol::ConnectSocksServer
PBoolean ConnectSocksServer(PTCPSocket &thisSocket)
PSocks4Socket
This class allows access to RFC1928 compliant SOCKS server.
Definition: socks.h:216
PSocks4Socket::PSocks4Socket
PSocks4Socket(WORD port=0)
sockets.h
PSocksUDPSocket::Listen
virtual PBoolean Listen(unsigned queueSize=5, WORD port=0, Reusability reuse=AddressIsExclusive)
Listen on a socket for a remote host on the specified port number.
PSocks5Socket::PSocks5Socket
PSocks5Socket(WORD port=0)
PSocksProtocol::SetErrorCodes
virtual void SetErrorCodes(PChannel::Errors errCode, int osErr)=0
PSocksProtocol::SetAuthentication
void SetAuthentication(const PString &username, const PString &password)
Set the username and password for the SOCKS server authentication.
PSocket
A network communications channel.
Definition: socket.h:58
PSocksUDPSocket::Connect
virtual PBoolean Connect(const PString &address)
Connect a socket to a remote host on the specified port number.
PSocksProtocol::serverPort
WORD serverPort
Definition: socks.h:90
PSocksUDPSocket::serverAddress
Address serverAddress
Definition: socks.h:419
PSocket::Reusability
Reusability
Flags to reuse of port numbers in Listen() function.
Definition: socket.h:84
PObject
Ultimate parent class for all objects in the class library.
Definition: object.h:1118
PSocksUDPSocket::Clone
virtual PObject * Clone() const
Create a copy of the class on the heap.
PSocksProtocol::authenticationPassword
PString authenticationPassword
Definition: socks.h:92
PSocks5Socket::Clone
virtual PObject * Clone() const
Create a copy of the class on the heap.