From e544a3ad5a8d3cdf67554384f53895fad34ff9bc Mon Sep 17 00:00:00 2001
From: Tobias Markmann <tm@ayena.de>
Date: Wed, 20 May 2015 18:01:06 +0200
Subject: Fix crash when sending a file to yourself

Use scoped_connection to prevent missing disconnection from signals.
Stop and free S5BServer when stopping SOCKS5BytestreamServerManager.

Test-Information:

Tried sending a file to myself multiple times and it did not crash.

Change-Id: If32075d8e9c243cab254776b924248227520e030

diff --git a/Swiften/FileTransfer/SOCKS5BytestreamServerManager.cpp b/Swiften/FileTransfer/SOCKS5BytestreamServerManager.cpp
index 43d3e46..3137163 100644
--- a/Swiften/FileTransfer/SOCKS5BytestreamServerManager.cpp
+++ b/Swiften/FileTransfer/SOCKS5BytestreamServerManager.cpp
@@ -192,6 +192,10 @@ void SOCKS5BytestreamServerManager::stop() {
 		forwardPortRequest->stop();
 		forwardPortRequest.reset();
 	}
+	if (server) {
+		server->stop();
+		server = NULL;
+	}
 	if (connectionServer) {
 		connectionServer->stop();
 		connectionServer.reset();
@@ -223,6 +227,7 @@ void SOCKS5BytestreamServerManager::handleForwardPortResult(boost::optional<NATP
 	}
 
 	portMapping = mapping;
+	onPortForwardingSetup(mapping.is_initialized());
 
 	forwardPortRequest->stop();
 	forwardPortRequest.reset();
diff --git a/Swiften/FileTransfer/SOCKS5BytestreamServerPortForwardingUser.cpp b/Swiften/FileTransfer/SOCKS5BytestreamServerPortForwardingUser.cpp
index 9ab097b..e3b6d34 100644
--- a/Swiften/FileTransfer/SOCKS5BytestreamServerPortForwardingUser.cpp
+++ b/Swiften/FileTransfer/SOCKS5BytestreamServerPortForwardingUser.cpp
@@ -15,14 +15,16 @@ namespace Swift {
 SOCKS5BytestreamServerPortForwardingUser::SOCKS5BytestreamServerPortForwardingUser(SOCKS5BytestreamServerManager* s5bServerManager) : s5bServerManager_(s5bServerManager) {
 	// the server should be initialized, so we know what port to setup a forward for
 	assert(s5bServerManager->isInitialized());
-	assert(!s5bServerManager_->isPortForwardingReady());
-
-	s5bServerManager_->onPortForwardingSetup.connect(boost::bind(&SOCKS5BytestreamServerPortForwardingUser::handleServerManagerPortForwardingSetup, this, _1));
-	s5bServerManager_->setupPortForwarding();
+	if (s5bServerManager_->isPortForwardingReady()) {
+		onSetup(!s5bServerManager_->getAssistedHostAddressPorts().empty());
+	}
+	else {
+		onPortForwardingSetupConnection_ = s5bServerManager_->onPortForwardingSetup.connect(boost::bind(&SOCKS5BytestreamServerPortForwardingUser::handleServerManagerPortForwardingSetup, this, _1));
+		s5bServerManager_->setupPortForwarding();
+	}
 }
 
 SOCKS5BytestreamServerPortForwardingUser::~SOCKS5BytestreamServerPortForwardingUser() {
-	s5bServerManager_->onPortForwardingSetup.disconnect(boost::bind(&SOCKS5BytestreamServerPortForwardingUser::handleServerManagerPortForwardingSetup, this, _1));
 	if (s5bServerManager_->isPortForwardingReady()) {
 		s5bServerManager_->removePortForwarding();
 	}
diff --git a/Swiften/FileTransfer/SOCKS5BytestreamServerPortForwardingUser.h b/Swiften/FileTransfer/SOCKS5BytestreamServerPortForwardingUser.h
index f486836..c4550d3 100644
--- a/Swiften/FileTransfer/SOCKS5BytestreamServerPortForwardingUser.h
+++ b/Swiften/FileTransfer/SOCKS5BytestreamServerPortForwardingUser.h
@@ -26,6 +26,7 @@ class SOCKS5BytestreamServerPortForwardingUser {
 
 	private:
 		SOCKS5BytestreamServerManager* s5bServerManager_;
+		boost::bsignals::scoped_connection onPortForwardingSetupConnection_;
 };
 
 }
diff --git a/Swiften/FileTransfer/SOCKS5BytestreamServerResourceUser.cpp b/Swiften/FileTransfer/SOCKS5BytestreamServerResourceUser.cpp
index bf7d4e8..b369347 100644
--- a/Swiften/FileTransfer/SOCKS5BytestreamServerResourceUser.cpp
+++ b/Swiften/FileTransfer/SOCKS5BytestreamServerResourceUser.cpp
@@ -14,7 +14,7 @@ namespace Swift {
 
 SOCKS5BytestreamServerResourceUser::SOCKS5BytestreamServerResourceUser(SOCKS5BytestreamServerManager* s5bServerManager) : s5bServerManager_(s5bServerManager) {
 	assert(!s5bServerManager_->isInitialized());
-	s5bServerManager_->onInitialized.connect(boost::bind(&SOCKS5BytestreamServerResourceUser::handleServerManagerInitialized, this, _1));
+	onInitializedConnection_ = s5bServerManager_->onInitialized.connect(boost::bind(&SOCKS5BytestreamServerResourceUser::handleServerManagerInitialized, this, _1));
 	s5bServerManager_->initialize();
 }
 
diff --git a/Swiften/FileTransfer/SOCKS5BytestreamServerResourceUser.h b/Swiften/FileTransfer/SOCKS5BytestreamServerResourceUser.h
index 014689b..fa7f42c 100644
--- a/Swiften/FileTransfer/SOCKS5BytestreamServerResourceUser.h
+++ b/Swiften/FileTransfer/SOCKS5BytestreamServerResourceUser.h
@@ -26,6 +26,7 @@ class SOCKS5BytestreamServerResourceUser {
 
 	private:
 		SOCKS5BytestreamServerManager* s5bServerManager_;
+		boost::bsignals::scoped_connection onInitializedConnection_;
 };
 
 }
-- 
cgit v0.10.2-6-g49f6