00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014 #include "socks5bytestream.h"
00015 #include "bytestreamdatahandler.h"
00016 #include "clientbase.h"
00017 #include "connectionbase.h"
00018 #include "connectionsocks5proxy.h"
00019 #include "sha.h"
00020 #include "logsink.h"
00021
00022 namespace gloox
00023 {
00024
00025 SOCKS5Bytestream::SOCKS5Bytestream( SOCKS5BytestreamManager* manager, ConnectionBase* connection,
00026 LogSink& logInstance, const JID& initiator, const JID& target,
00027 const std::string& sid )
00028 : Bytestream( Bytestream::S5B, logInstance, initiator, target, sid ),
00029 m_manager( manager ), m_connection( 0 ), m_socks5( 0 )
00030 {
00031 if( connection && connection->state() == StateConnected )
00032 m_open = true;
00033
00034 setConnectionImpl( connection );
00035 }
00036
00037 SOCKS5Bytestream::~SOCKS5Bytestream()
00038 {
00039 if( m_open )
00040 close();
00041
00042 if( m_socks5 )
00043 delete m_socks5;
00044 }
00045
00046 void SOCKS5Bytestream::setConnectionImpl( ConnectionBase* connection )
00047 {
00048 if( m_socks5 )
00049 delete m_socks5;
00050
00051 m_connection = connection;
00052
00053 SHA sha;
00054 sha.feed( m_sid );
00055 sha.feed( m_initiator.full() );
00056 sha.feed( m_target.full() );
00057 m_socks5 = new ConnectionSOCKS5Proxy( this, connection, m_logInstance, sha.hex(), 0 );
00058 }
00059
00060 bool SOCKS5Bytestream::connect()
00061 {
00062 if( !m_connection || !m_socks5 || !m_manager )
00063 return false;
00064
00065 if( m_open )
00066 return true;
00067
00068 StreamHostList::const_iterator it = m_hosts.begin();
00069 for( ; it != m_hosts.end(); ++it )
00070 {
00071 m_connection->setServer( (*it).host, (*it).port );
00072 if( m_socks5->connect() == ConnNoError )
00073 {
00074 m_proxy = (*it).jid;
00075 return true;
00076 }
00077 }
00078
00079 m_manager->acknowledgeStreamHost( false, JID(), EmptyString );
00080 return false;
00081 }
00082
00083 bool SOCKS5Bytestream::send( const std::string& data )
00084 {
00085 if( !m_open || !m_connection || !m_socks5 || !m_manager )
00086 return false;
00087
00088 return m_socks5->send( data );
00089 }
00090
00091 ConnectionError SOCKS5Bytestream::recv( int timeout )
00092 {
00093 if( !m_connection || !m_socks5 || !m_manager )
00094 return ConnNotConnected;
00095
00096 return m_socks5->recv( timeout );
00097 }
00098
00099 void SOCKS5Bytestream::activate()
00100 {
00101 m_open = true;
00102 if( m_handler )
00103 m_handler->handleBytestreamOpen( this );
00104 }
00105
00106 void SOCKS5Bytestream::close()
00107 {
00108 if( m_open && m_handler )
00109 {
00110 m_open = false;
00111 m_socks5->disconnect();
00112 m_handler->handleBytestreamClose( this );
00113 }
00114 }
00115
00116 void SOCKS5Bytestream::handleReceivedData( const ConnectionBase* , const std::string& data )
00117 {
00118 if( !m_handler )
00119 return;
00120
00121 if( !m_open )
00122 {
00123 m_open = true;
00124 m_handler->handleBytestreamOpen( this );
00125 }
00126
00127
00128
00129
00130
00131
00132
00133
00134
00135 if( m_open )
00136 m_handler->handleBytestreamData( this, data );
00137 }
00138
00139 void SOCKS5Bytestream::handleConnect( const ConnectionBase* )
00140 {
00141 m_manager->acknowledgeStreamHost( true, m_proxy, m_sid );
00142 }
00143
00144 void SOCKS5Bytestream::handleDisconnect( const ConnectionBase* , ConnectionError )
00145 {
00146 if( m_handler )
00147 m_handler->handleBytestreamClose( this );
00148 }
00149
00150 }