/*
Copyright (c) 2006-2008 by Jakob Schroeter <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 "socks5bytestreammanager.h"
#include "socks5bytestreamhandler.h"
#include "socks5bytestreamserver.h"
#include "socks5bytestream.h"
#include "clientbase.h"
#include "disco.h"
#include "connectionbase.h"
#include "sha.h"
#include <cstdlib>
namespace gloox
{
SOCKS5BytestreamManager::SOCKS5BytestreamManager( ClientBase *parent, SOCKS5BytestreamHandler* s5bh )
: m_parent( parent ), m_socks5BytestreamHandler( s5bh )
{
if( m_parent )
m_parent->registerIqHandler( this, XMLNS_BYTESTREAMS );
}
SOCKS5BytestreamManager::~SOCKS5BytestreamManager()
{
if( m_parent )
{
m_parent->removeIqHandler( XMLNS_BYTESTREAMS );
m_parent->removeIDHandler( this );
}
S5BMap::iterator it = m_s5bMap.begin();
for( ; it != m_s5bMap.end(); ++it )
{
delete (*it).second;
(*it).second = 0;
}
}
void SOCKS5BytestreamManager::addStreamHost( const JID& jid, const std::string& host, int port )
{
StreamHost sh;
sh.jid = jid;
sh.host = host;
sh.port = port;
m_hosts.push_back( sh );
}
bool SOCKS5BytestreamManager::requestSOCKS5Bytestream( const JID& to, S5BMode /*mode*/,
const std::string& sid )
{
if( !m_parent )
return false;
if( m_hosts.empty() )
{
m_parent->logInstance().log( LogLevelWarning, LogAreaClassS5BManager,
"No stream hosts set, cannot request bytestream." );
return false;
}
const std::string& msid = sid.empty() ? m_parent->getID() : sid;
const std::string& id = m_parent->getID();
Tag *iq = new Tag( "iq" );
iq->addAttribute( "type", "set" );
iq->addAttribute( "to", to.full() );
iq->addAttribute( "id", id );
Tag *q = new Tag( iq, "query", "xmlns", XMLNS_BYTESTREAMS );
q->addAttribute( "sid", msid );
q->addAttribute( "mode", /*( mode == S5BTCP ) ?*/ "tcp" /*: "udp"*/ );
StreamHostList::const_iterator it = m_hosts.begin();
for( ; it != m_hosts.end(); ++it )
{
Tag* s = new Tag( q, "streamhost", "jid", (*it).jid.full() );
s->addAttribute( "host", (*it).host );
s->addAttribute( "port", (*it).port );
}
if( m_server )
{
SHA sha;
sha.feed( msid );
sha.feed( m_parent->jid().full() );
sha.feed( to.full() );
m_server->registerHash( sha.hex() );
}
AsyncS5BItem asi;
asi.sHosts = m_hosts;
asi.id = id;
asi.from = to;
asi.incoming = false;
m_asyncTrackMap[msid] = asi;
m_trackMap[id] = msid;
m_parent->trackID( this, id, S5BOpenStream );
m_parent->send( iq );
return true;
}
void SOCKS5BytestreamManager::acknowledgeStreamHost( bool success, const JID& jid,
const std::string& sid )
{
AsyncTrackMap::const_iterator it = m_asyncTrackMap.find( sid );
if( it == m_asyncTrackMap.end() || !m_parent )
return;
Tag *iq = new Tag( "iq" );
if( (*it).second.incoming )
{
iq->addAttribute( "to", (*it).second.from.full() );
iq->addAttribute( "id", (*it).second.id );
if( success )
{
iq->addAttribute( "type", "result" );
Tag* q = new Tag( iq, "query", "xmlns", XMLNS_BYTESTREAMS );
new Tag( q, "streamhost-used", "jid", jid.full() );
}
else
{
iq->addAttribute( "type", "error" );
Tag* e = new Tag( iq, "error" );
e->addAttribute( "code", "404" );
e->addAttribute( "type", "cancel" );
new Tag( e, "item-not-found", "xmlns", XMLNS_XMPP_STANZAS );
}
}
else
{
if( success )
{
const std::string& id = m_parent->getID();
iq->addAttribute( "to", jid.full() );
iq->addAttribute( "id", id );
iq->addAttribute( "type", "set" );
Tag* q = new Tag( iq, "query", "xmlns", XMLNS_BYTESTREAMS );
q->addAttribute( "sid", sid );
new Tag( q, "activate", (*it).second.from.full() );
m_trackMap[id] = sid;
m_parent->trackID( this, id, S5BActivateStream );
}
}
m_parent->send( iq );
}
bool SOCKS5BytestreamManager::handleIq( Stanza *stanza )
{
Tag* q = stanza->findChild( "query", "xmlns", XMLNS_BYTESTREAMS );
if( !q || !m_socks5BytestreamHandler )
return false;
if( m_trackMap.find( stanza->id() ) != m_trackMap.end() )
return false;
switch( stanza->subtype() )
{
case StanzaIqSet:
{
const std::string& sid = q->findAttribute( "sid" );
const std::string& mode = q->findAttribute( "mode" );
if( haveStream( stanza->from(),sid ) || sid.empty() || mode == "udp" )
{
rejectSOCKS5Bytestream( stanza->from(), stanza->id(), StanzaErrorNotAcceptable );
return true;
}
AsyncS5BItem asi;
Tag::TagList& l = q->children();
Tag::TagList::const_iterator it = l.begin();
for( ; it != l.end(); ++it )
{
if( (*it)->name() == "streamhost" && (*it)->hasAttribute( "jid" )
&& (*it)->hasAttribute( "host" ) && (*it)->hasAttribute( "port" ) )
{
StreamHost sh;
sh.jid = (*it)->findAttribute( "jid" );
sh.host = (*it)->findAttribute( "host" );
sh.port = atoi( (*it)->findAttribute( "port" ).c_str() );
asi.sHosts.push_back( sh );
}
}
asi.id = stanza->id();
asi.from = stanza->from();
asi.incoming = true;
m_asyncTrackMap[sid] = asi;
m_socks5BytestreamHandler->handleIncomingSOCKS5BytestreamRequest( sid, stanza->from() );
break;
}
case StanzaIqError:
m_socks5BytestreamHandler->handleSOCKS5BytestreamError( stanza, std::string() );
break;
default:
break;
}
return true;
}
const StreamHost* SOCKS5BytestreamManager::findProxy( const JID& from, const std::string& hostjid,
const std::string& sid )
{
AsyncTrackMap::const_iterator it = m_asyncTrackMap.find( sid );
if( it == m_asyncTrackMap.end() )
return 0;
if( (*it).second.from == from )
{
StreamHostList::const_iterator it2 = (*it).second.sHosts.begin();
for( ; it2 != (*it).second.sHosts.end(); ++it2 )
{
if( (*it2).jid == hostjid )
{
return &(*it2);
}
}
}
return 0;
}
bool SOCKS5BytestreamManager::haveStream( const JID& from,std::string sid )
{
/*S5BMap::const_iterator it = m_s5bMap.begin();
for( ; it != m_s5bMap.end(); ++it )
{
std::string fr = from.bare();
std::string tt = (*it).second->target().bare();
std::string sid = (*it).second->sid();
//if( (*it).second && (*it).second->target() == from )
//return true;
}*/
S5BMap::const_iterator it = m_s5bMap.find(sid);
if (it!=m_s5bMap.end())
{
return true;
}
return false;
}
void SOCKS5BytestreamManager::acceptSOCKS5Bytestream( const std::string& sid )
{
AsyncTrackMap::iterator it = m_asyncTrackMap.find( sid );
if( it == m_asyncTrackMap.end() || !m_socks5BytestreamHandler )
return;
SOCKS5Bytestream* s5b = new SOCKS5Bytestream( this,
- 1
- 2
- 3
- 4
- 5
- 6
前往页