diff options
author | Tobias Markmann <tm@ayena.de> | 2015-03-08 15:36:49 (GMT) |
---|---|---|
committer | Kevin Smith <kevin.smith@isode.com> | 2015-05-01 17:14:55 (GMT) |
commit | 794fec2873e69ec047974416768b32b69754dad1 (patch) | |
tree | a3cc8cb221e9ee7ad5c40a9de0ccc03e5f9d9562 /Swiften/FileTransfer/LocalJingleTransportCandidateGenerator.cpp | |
parent | d7bc3a37f4bb261c8520a8ddedcda2369f97361c (diff) | |
download | swift-794fec2873e69ec047974416768b32b69754dad1.zip swift-794fec2873e69ec047974416768b32b69754dad1.tar.bz2 |
Add resource management for S5B server and port forwardings
This patchs adds management of the SOCKS5 bytestream server and port
forwarding setup to Swiften. The first file-transfer, regardless of
incoming or outgoing transfer, will start and initialize the S5B server
and if configured try to set up port forwarding. The last file-transfer
to finish will will close the server and remove the port forwarding.
Test-Information:
Tested with upcoming ConcurrentFileTransferTest.cpp and running multiple
S5B file-transfers in parallel.
Change-Id: Idd09d3d0ef9498fc9435b0aee81f2b1061783342
Diffstat (limited to 'Swiften/FileTransfer/LocalJingleTransportCandidateGenerator.cpp')
-rw-r--r-- | Swiften/FileTransfer/LocalJingleTransportCandidateGenerator.cpp | 101 |
1 files changed, 53 insertions, 48 deletions
diff --git a/Swiften/FileTransfer/LocalJingleTransportCandidateGenerator.cpp b/Swiften/FileTransfer/LocalJingleTransportCandidateGenerator.cpp index c684a90..6a059bf 100644 --- a/Swiften/FileTransfer/LocalJingleTransportCandidateGenerator.cpp +++ b/Swiften/FileTransfer/LocalJingleTransportCandidateGenerator.cpp @@ -23,7 +23,8 @@ #include <Swiften/Elements/JingleS5BTransportPayload.h> #include <Swiften/FileTransfer/SOCKS5BytestreamProxiesManager.h> #include <Swiften/FileTransfer/SOCKS5BytestreamServerManager.h> -#include <Swiften/FileTransfer/SOCKS5BytestreamServerInitializeRequest.h> +#include <Swiften/FileTransfer/SOCKS5BytestreamServerResourceUser.h> +#include <Swiften/FileTransfer/SOCKS5BytestreamServerPortForwardingUser.h> static const unsigned int LOCAL_PREFERENCE = 0; @@ -39,7 +40,10 @@ LocalJingleTransportCandidateGenerator::LocalJingleTransportCandidateGenerator( s5bProxy(s5bProxy), ownJID(ownJID), idGenerator(idGenerator), - options_(options) { + options_(options), + triedServerInit_(false), + triedForwarding_(false), + triedProxyDiscovery_(false) { } LocalJingleTransportCandidateGenerator::~LocalJingleTransportCandidateGenerator() { @@ -48,43 +52,69 @@ LocalJingleTransportCandidateGenerator::~LocalJingleTransportCandidateGenerator( void LocalJingleTransportCandidateGenerator::start() { assert(!s5bServerInitializeRequest); - s5bServerInitializeRequest = s5bServerManager->createInitializeRequest(); - s5bServerInitializeRequest->onFinished.connect( - boost::bind(&LocalJingleTransportCandidateGenerator::handleS5BServerInitialized, this, _1)); - s5bServerInitializeRequest->start(); -} + if (options_.isDirectAllowed() || options_.isAssistedAllowed()) { + s5bServerResourceUser_ = s5bServerManager->aquireResourceUser(); + if (s5bServerResourceUser_->isInitialized()) { + handleS5BServerInitialized(true); + } + else { + s5bServerResourceUser_->onSuccessfulInitialized.connect(boost::bind(&LocalJingleTransportCandidateGenerator::handleS5BServerInitialized, this, _1)); + } + } else { + handleS5BServerInitialized(false); + } -void LocalJingleTransportCandidateGenerator::stop() { - if (s5bServerInitializeRequest) { - s5bServerInitializeRequest->stop(); - s5bServerInitializeRequest.reset(); + if (options_.isProxiedAllowed()) { + s5bProxy->onDiscoveredProxiesChanged.connect(boost::bind(&LocalJingleTransportCandidateGenerator::handleDiscoveredProxiesChanged, this)); + if (s5bProxy->getOrDiscoverS5BProxies().is_initialized()) { + handleDiscoveredProxiesChanged(); + } } +} - s5bServerManager->stop(); +void LocalJingleTransportCandidateGenerator::stop() { + s5bServerResourceUser_.reset(); } void LocalJingleTransportCandidateGenerator::handleS5BServerInitialized(bool success) { + triedServerInit_ = true; if (success) { - if (options_.isProxiedAllowed()) { - if (s5bProxy->getOrDiscoverS5BProxies()) { - emitOnLocalTransportCandidatesGenerated(); - } else { - s5bProxy->onDiscoveredProxiesChanged.connect(boost::bind(&LocalJingleTransportCandidateGenerator::handleDiscoveredProxiesChanged, this)); + if (options_.isAssistedAllowed()) { + // try to setup port forwarding + s5bServerPortForwardingUser_ = s5bServerManager->aquirePortForwardingUser(); + if (s5bServerPortForwardingUser_->isForwardingSetup()) { + handlePortForwardingSetup(true); } } - else { - emitOnLocalTransportCandidatesGenerated(); - } } else { SWIFT_LOG(warning) << "Unable to start SOCKS5 server" << std::endl; + s5bServerResourceUser_.reset(); + handlePortForwardingSetup(false); } + checkS5BCandidatesReady(); +} - s5bServerInitializeRequest->stop(); - s5bServerInitializeRequest.reset(); +void LocalJingleTransportCandidateGenerator::handlePortForwardingSetup(bool success) { + triedForwarding_ = true; + checkS5BCandidatesReady(); +} +void LocalJingleTransportCandidateGenerator::handleDiscoveredProxiesChanged() { + s5bProxy->onDiscoveredProxiesChanged.disconnect(boost::bind(&LocalJingleTransportCandidateGenerator::handleDiscoveredProxiesChanged, this)); + triedProxyDiscovery_ = true; + checkS5BCandidatesReady(); } +void LocalJingleTransportCandidateGenerator::checkS5BCandidatesReady() { + if ((!options_.isDirectAllowed() || (options_.isDirectAllowed() && triedServerInit_)) && + (!options_.isProxiedAllowed() || (options_.isProxiedAllowed() && triedProxyDiscovery_)) && + (!options_.isDirectAllowed() || (options_.isDirectAllowed() && triedServerInit_))) { + emitOnLocalTransportCandidatesGenerated(); + } +} + + void LocalJingleTransportCandidateGenerator::emitOnLocalTransportCandidatesGenerated() { std::vector<JingleS5BTransportPayload::Candidate> candidates; @@ -116,7 +146,7 @@ void LocalJingleTransportCandidateGenerator::emitOnLocalTransportCandidatesGener } } - if (options_.isProxiedAllowed()) { + if (options_.isProxiedAllowed() && s5bProxy->getOrDiscoverS5BProxies().is_initialized()) { foreach(S5BProxyRequest::ref proxy, s5bProxy->getOrDiscoverS5BProxies().get()) { if (proxy->getStreamHost()) { // FIXME: Added this test, because there were cases where this wasn't initialized. Investigate this. (Remko) JingleS5BTransportPayload::Candidate candidate; @@ -135,29 +165,4 @@ void LocalJingleTransportCandidateGenerator::emitOnLocalTransportCandidatesGener onLocalTransportCandidatesGenerated(candidates); } -void LocalJingleTransportCandidateGenerator::handleDiscoveredProxiesChanged() { - s5bProxy->onDiscoveredProxiesChanged.disconnect(boost::bind(&LocalJingleTransportCandidateGenerator::handleDiscoveredProxiesChanged, this)); - emitOnLocalTransportCandidatesGenerated(); -} - -/*void LocalJingleTransportCandidateGenerator::handleS5BProxiesDiscovered() { - foreach(S5BProxyRequest::ref proxy, s5bProxiesDiscoverRequest->getS5BProxies()) { - if (proxy->getStreamHost()) { // FIXME: Added this test, because there were cases where this wasn't initialized. Investigate this. (Remko) - JingleS5BTransportPayload::Candidate candidate; - candidate.type = JingleS5BTransportPayload::Candidate::ProxyType; - candidate.jid = (*proxy->getStreamHost()).jid; - candidate.hostPort = (*proxy->getStreamHost()).addressPort; - candidate.priority = 65536 * 10 + LOCAL_PREFERENCE; - candidate.cid = idGenerator.generateID(); - s5bCandidatesPayload->addCandidate(candidate); - } - } - - haveS5BProxyCandidates = true; - s5bProxiesDiscoverRequest->stop(); - s5bProxiesDiscoverRequest.reset(); - - checkS5BCandidatesReady(); -}*/ - } |