#include "socks++.h"
#include "socks++4.h"
#include "socks++5.h"
#include "hosts.h"
namespace Security {
namespace IPPlusPlus {
ModuleNexus<SocksServer::Global> SocksServer::global;
SocksServer::~SocksServer()
{ }
SocksServer *SocksServer::make(Version version, const IPSockAddress &addr)
{
switch (version) {
case 0:
return NULL; case 4:
return new Socks4::Server(addr);
case 5:
return new Socks5::Server(addr);
default:
UnixError::throwMe(EINVAL);
}
}
void SocksClientSocket::open(const IPSockAddress &peer)
{
if (mServer) {
Support::connect(*this, peer);
lastConnected(mPeerAddress.address());
} else {
TCPClientSocket::open(peer);
}
}
void SocksClientSocket::open(const IPAddress &addr, IPPort port)
{
open(IPSockAddress(addr, port));
}
void SocksClientSocket::open(const Host &host, IPPort port)
{
if (mServer) {
Support::connect(*this, host, port);
lastConnected(mPeerAddress.address());
} else {
TCPClientSocket::open(host, port);
}
}
void SocksClientSocket::setFd(int fd, const IPSockAddress &local, const IPSockAddress &peer)
{
Socket::setFd(fd);
mLocalAddress = local;
mPeerAddress = peer;
}
void SocksServerSocket::open(const IPSockAddress &local, int)
{
if (mServer) {
#if BUG_GCC
if (mConnectionPeer)
Support::bind(*this, mConnectionPeer, local.port());
else
Support::bind(*this, lastConnected(), local.port());
#else
Support::bind(*this,
mConnectionPeer ? mConnectionPeer : lastConnected(),
local.port());
#endif
} else {
TCPServerSocket::open(local, 1);
}
}
void SocksServerSocket::receive(SocksClientSocket &client)
{
if (mServer) {
Support::receive(*this, client);
} else {
TCPServerSocket::receive(client);
}
}
IPSockAddress SocksServer::Support::localAddress(const Socket &me) const
{
if (mServer)
return mLocalAddress;
else
return me.localAddress();
}
IPSockAddress SocksServer::Support::peerAddress(const Socket &me) const
{
if (mServer)
return mPeerAddress;
else
return me.peerAddress();
}
} }