14 #include "socks5bytestreamserver.h"
15 #include "mutexguard.h"
22 const std::string& ip )
23 : m_server( 0 ), m_logInstance( logInstance ), m_ip( ip ), m_port( port )
32 ConnectionMap::const_iterator it = m_connections.begin();
33 for( ; it != m_connections.end(); ++it )
71 ConnectionMap::const_iterator it = m_connections.begin();
72 ConnectionMap::const_iterator it2;
73 while( it != m_connections.end() )
76 (*it2).first->recv( timeout );
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;
144 m_connections[connection] = ci;
148 const std::string& data )
150 ConnectionMap::iterator it = m_connections.find( const_cast<ConnectionBase*>( connection ) );
151 if( it == m_connections.end() )
154 switch( (*it).second.state )
156 case StateDisconnected:
157 (*it).first->disconnect();
159 case StateUnnegotiated:
163 c[1] = (char)(
unsigned char)0xFF;
164 (*it).second.state = StateDisconnected;
166 if( data.length() >= 3 && data[0] == 0x05 )
168 unsigned int sz = ( data.length() - 2 <
static_cast<unsigned int>( data[1] ) )
169 ?
static_cast<unsigned int>( data.length() - 2 )
170 : static_cast<unsigned int>( data[1] );
171 for(
unsigned int i = 2; i < sz + 2; ++i )
173 if( data[i] == 0x00 )
176 (*it).second.state = StateAuthAccepted;
181 (*it).first->send( std::string( c, 2 ) );
184 case StateAuthmethodAccepted:
187 case StateAuthAccepted:
189 std::string reply = data;
190 if( reply.length() < 2 )
195 (*it).second.state = StateDisconnected;
197 if( data.length() == 47 && data[0] == 0x05 && data[1] == 0x01 && data[2] == 0x00
198 && data[3] == 0x03 && data[4] == 0x28 && data[45] == 0x00 && data[46] == 0x00 )
200 const std::string hash = data.substr( 5, 40 );
202 HashMap::const_iterator ith = m_hashes.begin();
203 for( ; ith != m_hashes.end() && (*ith) != hash; ++ith )
206 if( ith != m_hashes.end() )
209 (*it).second.hash = hash;
210 (*it).second.state = StateDestinationAccepted;
213 (*it).first->send( reply );
216 case StateDestinationAccepted:
231 m_connections.erase( const_cast<ConnectionBase*>( connection ) );
232 m_oldConnections.push_back( connection );