14 #include "socks5bytestreamserver.h"
15 #include "mutexguard.h"
22 const std::string& ip )
23 : m_tcpServer( 0 ), m_logInstance( logInstance ), m_ip( ip ), m_port( port )
35 ConnectionMap::const_iterator it = m_connections.begin();
36 for( ; it != m_connections.end(); ++it )
38 m_connections.clear();
63 ConnectionMap connectionsSnapshot;
66 connectionsSnapshot.insert( m_connections.begin(), m_connections.end() );
69 ConnectionMap::const_iterator it = connectionsSnapshot.begin();
70 for( ; it != connectionsSnapshot.end(); ++it )
72 (*it).first->recv( timeout );
74 connectionsSnapshot.clear();
108 ConnectionBase* SOCKS5BytestreamServer::getConnection(
const std::string& hash )
112 ConnectionMap::iterator it = m_connections.begin();
113 for( ; it != m_connections.end(); ++it )
115 if( (*it).second.hash == hash )
119 m_connections.erase( it );
127 void SOCKS5BytestreamServer::registerHash(
const std::string& hash )
129 util::MutexGuard mg( m_mutex );
130 m_hashes.push_back( hash );
133 void SOCKS5BytestreamServer::removeHash(
const std::string& hash )
135 util::MutexGuard mg( m_mutex );
136 m_hashes.remove( hash );
143 ci.state = StateUnnegotiated;
146 m_connections[connection] = ci;
151 const std::string& data )
154 ConnectionMap::iterator it = m_connections.find(
const_cast<ConnectionBase*
>( connection ) );
155 if( it == m_connections.end() )
162 switch( (*it).second.state )
164 case StateDisconnected:
165 (*it).first->disconnect();
167 case StateUnnegotiated:
171 c[1] =
static_cast<char>( 0xFF );
172 (*it).second.state = StateDisconnected;
174 if( data.length() >= 3 && data[0] == 0x05 )
176 unsigned int sz = ( data.length() - 2 <
static_cast<unsigned int>( data[1] ) )
177 ?
static_cast<unsigned int>( data.length() - 2 )
178 :
static_cast<unsigned int>( data[1] );
179 for(
unsigned int i = 2; i < sz + 2; ++i )
181 if( data[i] == 0x00 )
184 (*it).second.state = StateAuthAccepted;
189 (*it).first->send( std::string( c, 2 ) );
192 case StateAuthmethodAccepted:
195 case StateAuthAccepted:
197 std::string reply = data;
198 if( reply.length() < 2 )
203 (*it).second.state = StateDisconnected;
205 if( data.length() == 47 && data[0] == 0x05 && data[1] == 0x01 && data[2] == 0x00
206 && data[3] == 0x03 && data[4] == 0x28 && data[45] == 0x00 && data[46] == 0x00 )
208 const std::string hash = data.substr( 5, 40 );
211 HashMap::const_iterator ith = m_hashes.begin();
212 for( ; ith != m_hashes.end() && (*ith) != hash; ++ith )
215 if( ith != m_hashes.end() )
218 (*it).second.hash = hash;
219 (*it).second.state = StateDestinationAccepted;
223 (*it).first->send( reply );
226 case StateDestinationAccepted:
243 m_oldConnections.push_back( connection );
An abstract base class for a connection.
void registerConnectionDataHandler(ConnectionDataHandler *cdh)
virtual const std::string localInterface() const
virtual void disconnect()
virtual int localPort() const
This is an implementation of a simple listening TCP connection.
virtual ConnectionError recv(int timeout=-1)
virtual ConnectionError connect()
An implementation of log sink and source.
const std::string localInterface() const
virtual void handleIncomingConnection(ConnectionBase *server, ConnectionBase *connection)
virtual void handleDisconnect(const ConnectionBase *connection, ConnectionError reason)
~SOCKS5BytestreamServer()
virtual void handleConnect(const ConnectionBase *connection)
virtual void handleReceivedData(const ConnectionBase *connection, const std::string &data)
ConnectionError recv(int timeout)
SOCKS5BytestreamServer(const LogSink &logInstance, int port, const std::string &ip=EmptyString)
A simple implementation of a mutex guard.
void clearList(std::list< T * > &L)
The namespace for the gloox library.