summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
Diffstat (limited to 'Swiften/FileTransfer/SOCKS5BytestreamServerManager.cpp')
-rw-r--r--Swiften/FileTransfer/SOCKS5BytestreamServerManager.cpp60
1 files changed, 41 insertions, 19 deletions
diff --git a/Swiften/FileTransfer/SOCKS5BytestreamServerManager.cpp b/Swiften/FileTransfer/SOCKS5BytestreamServerManager.cpp
index 76c267c..cf4ae84 100644
--- a/Swiften/FileTransfer/SOCKS5BytestreamServerManager.cpp
+++ b/Swiften/FileTransfer/SOCKS5BytestreamServerManager.cpp
@@ -1,8 +1,8 @@
/*
- * Copyright (c) 2012 Isode Limited.
+ * Copyright (c) 2012-2015 Isode Limited.
* All rights reserved.
* See the COPYING file for more information.
*/
/*
* Copyright (c) 2011 Tobias Markmann
@@ -22,12 +22,13 @@
#include <Swiften/Network/ConnectionServer.h>
#include <Swiften/Network/ConnectionServerFactory.h>
#include <Swiften/Network/NetworkEnvironment.h>
#include <Swiften/Network/NATTraverser.h>
#include <Swiften/Network/NATTraversalGetPublicIPRequest.h>
#include <Swiften/Network/NATTraversalForwardPortRequest.h>
+#include <Swiften/Network/NATTraversalRemovePortForwardingRequest.h>
using namespace Swift;
static const int LISTEN_PORTS_BEGIN = 10000;
static const int LISTEN_PORTS_END = 11000;
@@ -46,37 +47,24 @@ SOCKS5BytestreamServerManager::SOCKS5BytestreamServerManager(
SOCKS5BytestreamServerManager::~SOCKS5BytestreamServerManager() {
SWIFT_LOG_ASSERT(!connectionServer, warning) << std::endl;
SWIFT_LOG_ASSERT(!getPublicIPRequest, warning) << std::endl;
SWIFT_LOG_ASSERT(!forwardPortRequest, warning) << std::endl;
SWIFT_LOG_ASSERT(state == Start, warning) << std::endl;
+ if (portMapping && !unforwardPortRequest) {
+ SWIFT_LOG(warning) << "Port forwarding still alive. Trying to remove it now." << std::endl;
+ unforwardPortRequest = natTraverser->createRemovePortForwardingRequest(portMapping.get().getLocalPort(), portMapping.get().getPublicPort());
+ unforwardPortRequest->start();
+ }
}
boost::shared_ptr<SOCKS5BytestreamServerInitializeRequest> SOCKS5BytestreamServerManager::createInitializeRequest() {
return boost::make_shared<SOCKS5BytestreamServerInitializeRequest>(this);
}
-void SOCKS5BytestreamServerManager::stop() {
- if (getPublicIPRequest) {
- getPublicIPRequest->stop();
- getPublicIPRequest.reset();
- }
- if (forwardPortRequest) {
- forwardPortRequest->stop();
- forwardPortRequest.reset();
- }
- if (connectionServer) {
- connectionServer->stop();
- connectionServer.reset();
- }
- // TODO: Remove port forwards
-
- state = Start;
-}
-
std::vector<HostAddressPort> SOCKS5BytestreamServerManager::getHostAddressPorts() const {
std::vector<HostAddressPort> result;
if (connectionServer) {
std::vector<NetworkInterface> networkInterfaces = networkEnvironment->getNetworkInterfaces();
foreach (const NetworkInterface& networkInterface, networkInterfaces) {
foreach (const HostAddress& address, networkInterface.getAddresses()) {
@@ -151,12 +139,36 @@ void SOCKS5BytestreamServerManager::initialize() {
boost::bind(&SOCKS5BytestreamServerManager::handleForwardPortResult, this, _1));
forwardPortRequest->start();
}
}
}
+void SOCKS5BytestreamServerManager::stop() {
+ if (getPublicIPRequest) {
+ getPublicIPRequest->stop();
+ getPublicIPRequest.reset();
+ }
+ if (forwardPortRequest) {
+ forwardPortRequest->stop();
+ forwardPortRequest.reset();
+ }
+ if (connectionServer) {
+ connectionServer->stop();
+ connectionServer.reset();
+ }
+
+ // remove port forwards
+ if (portMapping) {
+ unforwardPortRequest = natTraverser->createRemovePortForwardingRequest(portMapping.get().getLocalPort(), portMapping.get().getPublicPort());
+ unforwardPortRequest->onResult.connect(boost::bind(&SOCKS5BytestreamServerManager::handleUnforwardPortResult, this, _1));
+ unforwardPortRequest->start();
+ }
+
+ state = Start;
+}
+
void SOCKS5BytestreamServerManager::handleGetPublicIPResult(boost::optional<HostAddress> address) {
if (address) {
SWIFT_LOG(debug) << "Public IP discovered as " << address.get().toString() << "." << std::endl;
}
else {
SWIFT_LOG(debug) << "No public IP discoverable." << std::endl;
@@ -183,12 +195,22 @@ void SOCKS5BytestreamServerManager::handleForwardPortResult(boost::optional<NATP
forwardPortRequest->stop();
forwardPortRequest.reset();
checkInitializeFinished();
}
+void SOCKS5BytestreamServerManager::handleUnforwardPortResult(boost::optional<bool> result) {
+ if (result.is_initialized() && result.get()) {
+ portMapping.reset();
+ }
+ else {
+ SWIFT_LOG(warning) << "Failed to remove port forwarding." << std::endl;
+ }
+ unforwardPortRequest.reset();
+}
+
void SOCKS5BytestreamServerManager::checkInitializeFinished() {
assert(state == Initializing);
if (!getPublicIPRequest && !forwardPortRequest) {
state = Initialized;
onInitialized(true);
}