socks5bytestream.cpp
/*
Copyright (c) 2006-2023 by Jakob Schröter <js@camaya.net>
This file is part of the gloox library. http://camaya.net/gloox
This software is distributed under a license. The full license
agreement can be found in the file LICENSE in this distribution.
This software may not be copied, modified, sold or distributed
other than expressed in the named license agreement.
This software is distributed without any warranty.
*/
#include "socks5bytestream.h"
#include "bytestreamdatahandler.h"
#include "clientbase.h"
#include "connectionbase.h"
#include "connectionsocks5proxy.h"
#include "sha.h"
#include "logsink.h"
namespace gloox
{
SOCKS5Bytestream::SOCKS5Bytestream( SOCKS5BytestreamManager* manager, ConnectionBase* connection,
LogSink& logInstance, const JID& initiator, const JID& target,
const std::string& sid )
: Bytestream( Bytestream::S5B, logInstance, initiator, target, sid ),
m_manager( manager ), m_connection( 0 ), m_socks5( 0 ), m_connected( false )
{
if( connection && connection->state() == StateConnected )
m_open = true;
setConnectionImpl( connection );
}
SOCKS5Bytestream::~SOCKS5Bytestream()
{
if( m_open )
close();
if( m_socks5 )
delete m_socks5;
}
void SOCKS5Bytestream::setConnectionImpl( ConnectionBase* connection )
{
if( m_socks5 )
delete m_socks5; // deletes m_connection as well
m_connection = connection;
SHA sha;
sha.feed( m_sid );
sha.feed( m_initiator.full() );
sha.feed( m_target.full() );
m_socks5 = new ConnectionSOCKS5Proxy( this, connection, m_logInstance, sha.hex(), 0 );
}
bool SOCKS5Bytestream::connect()
{
if( !m_connection || !m_socks5 || !m_manager )
return false;
if( m_open )
return true;
StreamHostList::const_iterator it = m_hosts.begin();
for( ; it != m_hosts.end(); ++it )
{
if( ++it == m_hosts.end() )
m_connected = true;
--it; // FIXME ++it followed by --it is kinda ugly
m_connection->setServer( (*it).host, (*it).port );
if( m_socks5->connect() == ConnNoError )
{
m_proxy = (*it).jid;
m_connected = true;
return true;
}
}
m_manager->acknowledgeStreamHost( false, JID(), EmptyString );
return false;
}
bool SOCKS5Bytestream::send( const std::string& data )
{
if( !m_open || !m_connection || !m_socks5 || !m_manager )
return false;
return m_socks5->send( data );
}
ConnectionError SOCKS5Bytestream::recv( int timeout )
{
if( !m_connection || !m_socks5 || !m_manager )
return ConnNotConnected;
return m_socks5->recv( timeout );
}
void SOCKS5Bytestream::activate()
{
m_open = true;
if( m_handler )
m_handler->handleBytestreamOpen( this );
}
void SOCKS5Bytestream::close()
{
if( m_open && m_handler )
{
m_open = false;
m_connected = false;
m_socks5->disconnect();
m_handler->handleBytestreamClose( this );
}
}
void SOCKS5Bytestream::handleReceivedData( const ConnectionBase* /*connection*/, const std::string& data )
{
if( !m_handler )
return;
if( !m_open )
{
m_open = true;
m_handler->handleBytestreamOpen( this );
}
// if( !m_open && data.length() == 2 && data[0] == 0x05 && data[1] == 0x00 )
// {
// printf( "received acknowleding zero byte, stream is now open\n" );
// m_open = true;
// m_handler->handleBytestream5Open( this );
// return;
// }
if( m_open )
m_handler->handleBytestreamData( this, data );
}
void SOCKS5Bytestream::handleConnect( const ConnectionBase* /*connection*/ )
{
m_manager->acknowledgeStreamHost( true, m_proxy, m_sid );
if( !m_open )
{
m_open = true;
m_handler->handleBytestreamOpen( this );
}
}
void SOCKS5Bytestream::handleDisconnect( const ConnectionBase* /*connection*/, ConnectionError /*reason*/ )
{
close();
}
}
gloox::Bytestream
An abstraction of a single bytestream.
Definition bytestream.h:37
gloox::Bytestream::m_open
bool m_open
Definition bytestream.h:171
gloox::Bytestream::m_target
const JID m_target
Definition bytestream.h:162
gloox::Bytestream::m_sid
std::string m_sid
Definition bytestream.h:168
gloox::Bytestream::m_initiator
const JID m_initiator
Definition bytestream.h:159
gloox::Bytestream::m_handler
BytestreamDataHandler * m_handler
Definition bytestream.h:153
gloox::Bytestream::m_logInstance
const LogSink & m_logInstance
Definition bytestream.h:156
gloox::ConnectionBase
An abstract base class for a connection.
Definition connectionbase.h:35
gloox::ConnectionSOCKS5Proxy
This is an implementation of a simple SOCKS5 Proxying connection (RFC 1928 + RFC 1929).
Definition connectionsocks5proxy.h:54
gloox::JID
An abstraction of a JID.
Definition jid.h:31
gloox::LogSink
An implementation of log sink and source.
Definition logsink.h:39
gloox::SHA
An implementation of SHA1.
Definition sha.h:30
gloox::SHA::feed
void feed(const unsigned char *data, unsigned length)
Definition sha.cpp:89
gloox::SHA::hex
const std::string hex()
Definition sha.cpp:53
gloox::SOCKS5BytestreamManager
An SOCKS5BytestreamManager dispatches SOCKS5 Bytestreams.
Definition socks5bytestreammanager.h:52
gloox::SOCKS5Bytestream::~SOCKS5Bytestream
virtual ~SOCKS5Bytestream()
Definition socks5bytestream.cpp:37
gloox::SOCKS5Bytestream::handleDisconnect
virtual void handleDisconnect(const ConnectionBase *connection, ConnectionError reason)
Definition socks5bytestream.cpp:155
gloox::SOCKS5Bytestream::recv
virtual ConnectionError recv(int timeout=-1)
Definition socks5bytestream.cpp:95
gloox::SOCKS5Bytestream::close
virtual void close()
Definition socks5bytestream.cpp:110
gloox::SOCKS5Bytestream::handleConnect
virtual void handleConnect(const ConnectionBase *connection)
Definition socks5bytestream.cpp:144
gloox::SOCKS5Bytestream::handleReceivedData
virtual void handleReceivedData(const ConnectionBase *connection, const std::string &data)
Definition socks5bytestream.cpp:121
gloox::SOCKS5Bytestream::connect
virtual bool connect()
Definition socks5bytestream.cpp:60
gloox::SOCKS5Bytestream::setConnectionImpl
void setConnectionImpl(ConnectionBase *connection)
Definition socks5bytestream.cpp:46
gloox::SOCKS5Bytestream::send
virtual bool send(const std::string &data)
Definition socks5bytestream.cpp:87
gloox
The namespace for the gloox library.
Definition adhoc.cpp:28
gloox::ConnectionError
ConnectionError
Definition gloox.h:684
gloox::ConnNotConnected
@ ConnNotConnected
Definition gloox.h:715
gloox::ConnNoError
@ ConnNoError
Definition gloox.h:685
gloox::EmptyString
const std::string EmptyString
Definition gloox.cpp:124