socks5bytestream.cpp

00001 /*
00002   Copyright (c) 2006-2008 by Jakob Schroeter <js@camaya.net>
00003   This file is part of the gloox library. http://camaya.net/gloox
00004 
00005   This software is distributed under a license. The full license
00006   agreement can be found in the file LICENSE in this distribution.
00007   This software may not be copied, modified, sold or distributed
00008   other than expressed in the named license agreement.
00009 
00010   This software is distributed without any warranty.
00011 */
00012 
00013 
00014 #include "socks5bytestream.h"
00015 #include "socks5bytestreamdatahandler.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     : m_manager( manager ), m_connection( 0 ), m_socks5( 0 ), m_logInstance( logInstance ),
00029       m_socks5BytestreamDataHandler( 0 ), m_initiator( initiator ), m_target( target ),
00030       m_sid( sid ), m_open( false )
00031   {
00032     if( connection && connection->state() == StateConnected )
00033       m_open = true;
00034 
00035     setConnectionImpl( connection );
00036   }
00037 
00038   SOCKS5Bytestream::~SOCKS5Bytestream()
00039   {
00040     if( m_open )
00041       close();
00042 
00043     if( m_socks5 )
00044       delete m_socks5;
00045   }
00046 
00047   void SOCKS5Bytestream::setConnectionImpl( ConnectionBase* connection )
00048   {
00049     if( m_socks5 )
00050       delete m_socks5; // deletes m_connection as well
00051 
00052     m_connection = connection;
00053 
00054     SHA sha;
00055     sha.feed( m_sid );
00056     sha.feed( m_initiator.full() );
00057     sha.feed( m_target.full() );
00058     m_socks5 = new ConnectionSOCKS5Proxy( this, connection, m_logInstance, sha.hex(), 0 );
00059   }
00060 
00061   bool SOCKS5Bytestream::connect()
00062   {
00063     if( !m_connection || !m_socks5 || !m_manager )
00064       return false;
00065 
00066     if( m_open )
00067       return true;
00068 
00069     StreamHostList::const_iterator it = m_hosts.begin();
00070     for( ; it != m_hosts.end(); ++it )
00071     {
00072       m_connection->setServer( (*it).host, (*it).port );
00073       if( m_socks5->connect() == ConnNoError )
00074       {
00075         m_proxy = (*it).jid;
00076         return true;
00077       }
00078     }
00079 
00080     m_manager->acknowledgeStreamHost( false, JID(), std::string() );
00081     return false;
00082   }
00083 
00084   bool SOCKS5Bytestream::send( const std::string& data )
00085   {
00086     if( !m_open || !m_connection || !m_socks5 || !m_manager )
00087       return false;
00088 
00089     return m_socks5->send( data );
00090   }
00091 
00092   ConnectionError SOCKS5Bytestream::recv( int timeout )
00093   {
00094     if( !m_connection || !m_socks5 || !m_manager )
00095       return ConnNotConnected;
00096 
00097     return m_socks5->recv( timeout );
00098   }
00099 
00100   void SOCKS5Bytestream::activate()
00101   {
00102     m_open = true;
00103     if( m_socks5BytestreamDataHandler )
00104       m_socks5BytestreamDataHandler->handleSOCKS5Open( this );
00105   }
00106 
00107   void SOCKS5Bytestream::close()
00108   {
00109     if( m_open )
00110     {
00111       m_open = false;
00112       m_socks5->disconnect();
00113       m_socks5BytestreamDataHandler->handleSOCKS5Close( this );
00114     }
00115   }
00116 
00117   void SOCKS5Bytestream::handleReceivedData( const ConnectionBase* /*connection*/, const std::string& data )
00118   {
00119     if( !m_socks5BytestreamDataHandler )
00120       return;
00121 
00122     if( !m_open )
00123     {
00124       m_open = true;
00125       m_socks5BytestreamDataHandler->handleSOCKS5Open( this );
00126     }
00127 
00128 //     if( !m_open && data.length() == 2 && data[0] == 0x05 && data[1] == 0x00 )
00129 //     {
00130 //       printf( "received acknowleding zero byte, stream is now open\n" );
00131 //       m_open = true;
00132 //       m_socks5BytestreamDataHandler->handleSOCKS5Open( this );
00133 //       return;
00134 //     }
00135 
00136     if( m_open )
00137       m_socks5BytestreamDataHandler->handleSOCKS5Data( this, data );
00138   }
00139 
00140   void SOCKS5Bytestream::handleConnect( const ConnectionBase* /*connection*/ )
00141   {
00142     m_manager->acknowledgeStreamHost( true, m_proxy, m_sid );
00143   }
00144 
00145   void SOCKS5Bytestream::handleDisconnect( const ConnectionBase* /*connection*/, ConnectionError /*reason*/ )
00146   {
00147     if( m_socks5BytestreamDataHandler )
00148       m_socks5BytestreamDataHandler->handleSOCKS5Close( this );
00149   }
00150 
00151 }

Generated on Mon Dec 7 13:28:19 2009 for gloox by  doxygen 1.6.1