14 #include "socks5bytestreammanager.h"
15 #include "socks5bytestreamhandler.h"
16 #include "socks5bytestreamserver.h"
17 #include "socks5bytestream.h"
18 #include "clientbase.h"
20 #include "connectionbase.h"
29 : m_parent( parent ), m_socks5BytestreamHandler( s5bh ), m_server( 0 )
43 S5BMap::iterator it = m_s5bMap.begin();
44 for( ; it != m_s5bMap.end(); ++it )
57 m_hosts.push_back( sh );
61 const std::string& sid )
69 "No stream hosts set, cannot request bytestream." );
73 const std::string& msid = sid.empty() ? m_parent->
getID() : sid;
74 const std::string&
id = m_parent->
getID();
83 StreamHostList::const_iterator it = m_hosts.begin();
84 for( ; it != m_hosts.end(); ++it )
86 Tag* s =
new Tag( q,
"streamhost",
"jid", (*it).jid.full() );
97 m_server->registerHash( sha.
hex() );
101 asi.sHosts = m_hosts;
104 asi.incoming =
false;
105 m_asyncTrackMap[msid] = asi;
107 m_trackMap[id] = msid;
108 m_parent->
trackID(
this,
id, S5BOpenStream );
109 m_parent->
send( iq );
114 void SOCKS5BytestreamManager::acknowledgeStreamHost(
bool success,
const JID& jid,
115 const std::string& sid )
117 AsyncTrackMap::const_iterator it = m_asyncTrackMap.find( sid );
118 if( it == m_asyncTrackMap.end() || !m_parent )
121 Tag *iq =
new Tag(
"iq" );
123 if( (*it).second.incoming )
132 new Tag( q,
"streamhost-used",
"jid", jid.
full() );
137 Tag* e =
new Tag( iq,
"error" );
138 e->addAttribute(
"code",
"404" );
139 e->addAttribute(
"type",
"cancel" );
147 const std::string&
id = m_parent->
getID();
152 q->addAttribute(
"sid", sid );
153 new Tag( q,
"activate", (*it).second.from.full() );
155 m_trackMap[id] = sid;
156 m_parent->
trackID(
this,
id, S5BActivateStream );
160 m_parent->
send( iq );
166 if( !q || !m_socks5BytestreamHandler )
169 if( m_trackMap.find( stanza->
id() ) != m_trackMap.end() )
178 if( haveStream( stanza->
from() ) || sid.empty() || mode ==
"udp" )
185 Tag::TagList::const_iterator it = l.begin();
186 for( ; it != l.end(); ++it )
188 if( (*it)->name() ==
"streamhost" && (*it)->hasAttribute(
"jid" )
189 && (*it)->hasAttribute(
"host" ) && (*it)->hasAttribute(
"port" ) )
192 sh.
jid = (*it)->findAttribute(
"jid" );
193 sh.
host = (*it)->findAttribute(
"host" );
194 sh.
port = atoi( (*it)->findAttribute(
"port" ).c_str() );
195 asi.sHosts.push_back( sh );
198 asi.id = stanza->
id();
199 asi.from = stanza->
from();
201 m_asyncTrackMap[sid] = asi;
215 const StreamHost* SOCKS5BytestreamManager::findProxy(
const JID& from,
const std::string& hostjid,
216 const std::string& sid )
218 AsyncTrackMap::const_iterator it = m_asyncTrackMap.find( sid );
219 if( it == m_asyncTrackMap.end() )
222 if( (*it).second.from == from )
224 StreamHostList::const_iterator it2 = (*it).second.sHosts.begin();
225 for( ; it2 != (*it).second.sHosts.end(); ++it2 )
227 if( (*it2).jid == hostjid )
237 bool SOCKS5BytestreamManager::haveStream(
const JID& from )
239 S5BMap::const_iterator it = m_s5bMap.begin();
240 for( ; it != m_s5bMap.end(); ++it )
242 if( (*it).second && (*it).second->target() == from )
250 AsyncTrackMap::iterator it = m_asyncTrackMap.find( sid );
251 if( it == m_asyncTrackMap.end() || !m_socks5BytestreamHandler )
256 (*it).second.from, m_parent->
jid(), sid );
264 AsyncTrackMap::iterator it = m_asyncTrackMap.find( sid );
265 if( it != m_asyncTrackMap.end() )
268 m_asyncTrackMap.erase( it );
275 Tag *iq =
new Tag(
"iq" );
279 Tag *e =
new Tag( iq,
"error" );
287 Tag *f =
new Tag( e,
"forbidden" );
295 Tag *f =
new Tag( e,
"item-not-found" );
304 Tag *f =
new Tag( e,
"not-allowed" );
313 Tag *f =
new Tag( e,
"not-acceptable" );
318 m_parent->
send( iq );
323 StringMap::iterator it = m_trackMap.find( stanza->
id() );
324 if( it == m_trackMap.end() )
336 if( !q || !m_socks5BytestreamHandler )
344 const StreamHost* sh = findProxy( stanza->
from(), proxy, (*it).second );
348 bool selfProxy = ( proxy == m_parent->
jid().
full() && m_server );
352 sha.
feed( (*it).second );
357 m_parent->
jid(), stanza->
from(),
364 m_parent->
jid(), stanza->
from(),
367 shl.push_back( *sh );
370 m_s5bMap[(*it).second] = s5b;
385 case S5BActivateStream:
391 S5BMap::const_iterator it5 = m_s5bMap.find( (*it).second );
392 if( it5 != m_s5bMap.end() )
393 (*it5).second->activate();
407 m_trackMap.erase( it );
414 S5BMap::iterator it = m_s5bMap.find( s5b->
sid() );
415 if( it != m_s5bMap.end() )
418 m_s5bMap.erase( it );