From f9dcd7a6e81e6c244e111aa1a22992f0604d999c Mon Sep 17 00:00:00 2001
From: Tobias Markmann <tm@ayena.de>
Date: Sun, 8 Mar 2015 17:10:30 +0100
Subject: Only close unneedded S5B connections on the S5B server if a S5B
 server is running

Add a check on whether we have a running SOCKS5 bytestream server
before trying to access it to close unneeded S5B connections to it
after we decided on a candidate to use.

Test-Information:

Without this FileTransferTest crashes in the configuration that does
not allow direct or assisted S5B connections. This patch fixes this
issue.

Change-Id: Ifcf59f82755b9620e7f5fa5841e45815a41ac442

diff --git a/Swiften/FileTransfer/DefaultFileTransferTransporter.cpp b/Swiften/FileTransfer/DefaultFileTransferTransporter.cpp
index 568fa7c..dffc39b 100644
--- a/Swiften/FileTransfer/DefaultFileTransferTransporter.cpp
+++ b/Swiften/FileTransfer/DefaultFileTransferTransporter.cpp
@@ -145,7 +145,9 @@ void DefaultFileTransferTransporter::stopActivatingProxy() {
 
 boost::shared_ptr<TransportSession> DefaultFileTransferTransporter::createIBBSendSession(
 		const std::string& sessionID, unsigned int blockSize, boost::shared_ptr<ReadBytestream> stream) {
-	closeLocalSession();
+	if (s5bServerManager->getServer()) {
+		closeLocalSession();
+	}
 	closeRemoteSession();
 	boost::shared_ptr<IBBSendSession> ibbSession = boost::make_shared<IBBSendSession>(
 			sessionID, initiator, responder, stream, router);
@@ -157,8 +159,8 @@ boost::shared_ptr<TransportSession> DefaultFileTransferTransporter::createIBBRec
 		const std::string& sessionID, unsigned long long size, boost::shared_ptr<WriteBytestream> stream) {
 	if (s5bServerManager->getServer()) {
 		closeLocalSession();
-		closeRemoteSession();
 	}
+	closeRemoteSession();
 	boost::shared_ptr<IBBReceiveSession> ibbSession = boost::make_shared<IBBReceiveSession>(
 			sessionID, initiator, responder, size, stream, router);
 	return boost::make_shared<IBBReceiveTransportSession>(ibbSession);
@@ -277,10 +279,11 @@ std::string DefaultFileTransferTransporter::getLocalCandidateSOCKS5DstAddr() con
 
 void DefaultFileTransferTransporter::closeLocalSession() {
 	s5bRegistry->setHasBytestream(getSOCKS5DstAddr(), false);
-	std::vector<boost::shared_ptr<SOCKS5BytestreamServerSession> > serverSessions = 
-		s5bServerManager->getServer()->getSessions(getSOCKS5DstAddr());
-	foreach(boost::shared_ptr<SOCKS5BytestreamServerSession> session, serverSessions) {
-		session->stop();
+	if (s5bServerManager->getServer()) {
+		std::vector<boost::shared_ptr<SOCKS5BytestreamServerSession> > serverSessions = s5bServerManager->getServer()->getSessions(getSOCKS5DstAddr());
+		foreach(boost::shared_ptr<SOCKS5BytestreamServerSession> session, serverSessions) {
+			session->stop();
+		}
 	}
 }
 
-- 
cgit v0.10.2-6-g49f6