gloox  1.1-svn
socks5bytestreamserver.cpp
1 /*
2  Copyright (c) 2007-2009 by Jakob Schroeter <js@camaya.net>
3  This file is part of the gloox library. http://camaya.net/gloox
4 
5  This software is distributed under a license. The full license
6  agreement can be found in the file LICENSE in this distribution.
7  This software may not be copied, modified, sold or distributed
8  other than expressed in the named license agreement.
9 
10  This software is distributed without any warranty.
11 */
12 
13 
14 #include "socks5bytestreamserver.h"
15 #include "mutexguard.h"
16 #include "util.h"
17 
18 namespace gloox
19 {
20 
22  const std::string& ip )
23  : m_server( 0 ), m_logInstance( logInstance ), m_ip( ip ), m_port( port )
24  {
25  }
26 
28  {
29  if( m_server )
30  delete m_server;
31 
32  ConnectionMap::const_iterator it = m_connections.begin();
33  for( ; it != m_connections.end(); ++it )
34  delete (*it).first;
35  }
36 
38  {
40 
41  m_server = server;
42  m_server->setServer( m_ip, m_port );
43  }
44 
46  {
47  if( m_server )
48  {
49  delete m_server;
50  m_server = 0;
51  }
52  }
53 
55  {
56  if( !m_server )
57  m_server = new ConnectionTCPServer( this, m_logInstance, m_ip, m_port );
58 
59  return m_server->connect();
60  }
61 
63  {
64  if( !m_server )
65  return ConnNotConnected;
66 
67  ConnectionError ce = m_server->recv( timeout );
68  if( ce != ConnNoError )
69  return ce;
70 
71  ConnectionMap::const_iterator it = m_connections.begin();
72  ConnectionMap::const_iterator it2;
73  while( it != m_connections.end() )
74  {
75  it2 = it++;
76  (*it2).first->recv( timeout );
77  }
78 
79  util::clearList( m_oldConnections );
80  return ConnNoError;
81  }
82 
84  {
85  if( m_server )
86  {
87  m_server->disconnect();
88  m_server->cleanup();
89  }
90  }
91 
93  {
94  if( m_server )
95  return m_server->localPort();
96 
97  return m_port;
98  }
99 
100  const std::string SOCKS5BytestreamServer::localInterface() const
101  {
102  if( m_server )
103  return m_server->localInterface();
104 
105  return m_ip;
106  }
107 
108  ConnectionBase* SOCKS5BytestreamServer::getConnection( const std::string& hash )
109  {
110  util::MutexGuard mg( m_mutex );
111 
112  ConnectionMap::iterator it = m_connections.begin();
113  for( ; it != m_connections.end(); ++it )
114  {
115  if( (*it).second.hash == hash )
116  {
117  ConnectionBase* conn = (*it).first;
119  m_connections.erase( it );
120  return conn;
121  }
122  }
123 
124  return 0;
125  }
126 
127  void SOCKS5BytestreamServer::registerHash( const std::string& hash )
128  {
129  util::MutexGuard mg( m_mutex );
130  m_hashes.push_back( hash );
131  }
132 
133  void SOCKS5BytestreamServer::removeHash( const std::string& hash )
134  {
135  util::MutexGuard mg( m_mutex );
136  m_hashes.remove( hash );
137  }
138 
140  {
141  connection->registerConnectionDataHandler( this );
142  ConnectionInfo ci;
143  ci.state = StateUnnegotiated;
144  m_connections[connection] = ci;
145  }
146 
148  const std::string& data )
149  {
150  ConnectionMap::iterator it = m_connections.find( const_cast<ConnectionBase*>( connection ) );
151  if( it == m_connections.end() )
152  return;
153 
154  switch( (*it).second.state )
155  {
156  case StateDisconnected:
157  (*it).first->disconnect();
158  break;
159  case StateUnnegotiated:
160  {
161  char c[2];
162  c[0] = 0x05;
163  c[1] = (char)(unsigned char)0xFF;
164  (*it).second.state = StateDisconnected;
165 
166  if( data.length() >= 3 && data[0] == 0x05 )
167  {
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 )
172  {
173  if( data[i] == 0x00 )
174  {
175  c[1] = 0x00;
176  (*it).second.state = StateAuthAccepted;
177  break;
178  }
179  }
180  }
181  (*it).first->send( std::string( c, 2 ) );
182  break;
183  }
184  case StateAuthmethodAccepted:
185  // place to implement any future auth support
186  break;
187  case StateAuthAccepted:
188  {
189  std::string reply = data;
190  if( reply.length() < 2 )
191  reply.resize( 2 );
192 
193  reply[0] = 0x05;
194  reply[1] = 0x01; // general SOCKS server failure
195  (*it).second.state = StateDisconnected;
196 
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 )
199  {
200  const std::string hash = data.substr( 5, 40 );
201 
202  HashMap::const_iterator ith = m_hashes.begin();
203  for( ; ith != m_hashes.end() && (*ith) != hash; ++ith )
204  ;
205 
206  if( ith != m_hashes.end() )
207  {
208  reply[1] = 0x00;
209  (*it).second.hash = hash;
210  (*it).second.state = StateDestinationAccepted;
211  }
212  }
213  (*it).first->send( reply );
214  break;
215  }
216  case StateDestinationAccepted:
217  case StateActive:
218  // should not happen
219  break;
220  }
221  }
222 
224  {
225  // should never happen, TCP connection is already established
226  }
227 
229  ConnectionError /*reason*/ )
230  {
231  m_connections.erase( const_cast<ConnectionBase*>( connection ) );
232  m_oldConnections.push_back( connection );
233  }
234 
235 }