summaryrefslogtreecommitdiffstats
blob: 5ed500c43f068f81a33b89b8bbff954b0173ef79 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
/*
 * Copyright (c) 2011 Tobias Markmann
 * Licensed under the simplified BSD license.
 * See Documentation/Licenses/BSD-simplified.txt for more information.
 */

#include "ConnectivityManager.h"

#include <boost/bind.hpp>

#include <Swiften/Base/foreach.h>
#include <Swiften/Base/Log.h>
#include <Swiften/Network/NetworkInterface.h>
#include <Swiften/Network/NATTraversalGetPublicIPRequest.h>
#include <Swiften/Network/NATTraversalRemovePortForwardingRequest.h>
#include <Swiften/Network/NATTraverser.h>
#include <Swiften/Network/PlatformNetworkEnvironment.h>

namespace Swift {

ConnectivityManager::ConnectivityManager(NATTraverser* worker) : natTraversalWorker(worker) {

}

ConnectivityManager::~ConnectivityManager() {
	std::set<int> leftOpenPorts = ports;
	foreach(int port, leftOpenPorts) {
		removeListeningPort(port);
	}
}

void ConnectivityManager::addListeningPort(int port) {
	ports.insert(port);
	boost::shared_ptr<NATTraversalGetPublicIPRequest> getIPRequest = natTraversalWorker->createGetPublicIPRequest();
	if (getIPRequest) {
		getIPRequest->onResult.connect(boost::bind(&ConnectivityManager::natTraversalGetPublicIPResult, this, _1));
		getIPRequest->start();
	}

	boost::shared_ptr<NATTraversalForwardPortRequest> forwardPortRequest = natTraversalWorker->createForwardPortRequest(port, port);
	if (forwardPortRequest) {
		forwardPortRequest->onResult.connect(boost::bind(&ConnectivityManager::natTraversalForwardPortResult, this, _1));
		forwardPortRequest->start();
	}
}

void ConnectivityManager::removeListeningPort(int port) {
	SWIFT_LOG(debug) << "remove listening port " << port << std::endl;
	ports.erase(port);
	boost::shared_ptr<NATTraversalRemovePortForwardingRequest> removePortForwardingRequest = natTraversalWorker->createRemovePortForwardingRequest(port, port);
	if (removePortForwardingRequest) {
		removePortForwardingRequest->start();
	}
}

std::vector<HostAddressPort> ConnectivityManager::getHostAddressPorts() const {
	PlatformNetworkEnvironment env;
	std::vector<HostAddressPort> results;

	std::vector<HostAddress> addresses;

	std::vector<NetworkInterface> networkInterfaces;
	foreach (const NetworkInterface& iface, networkInterfaces) {
		foreach (const HostAddress& address, iface.getAddresses()) {
			foreach (int port, ports) {
				results.push_back(HostAddressPort(address, port));
			}
		}
	}

	return results;
}

std::vector<HostAddressPort> ConnectivityManager::getAssistedHostAddressPorts() const {
	std::vector<HostAddressPort> results;

	if (publicAddress) {
		foreach (int port, ports) {
			results.push_back(HostAddressPort(publicAddress.get(), port));
		}
	}

	return results;
}

void ConnectivityManager::natTraversalGetPublicIPResult(boost::optional<HostAddress> address) {
	if (address) {
		publicAddress = address;
		SWIFT_LOG(debug) << "Public IP discovered as " << publicAddress.get().toString() << "." << std::endl;
	} else {
		SWIFT_LOG(debug) << "No public IP discoverable." << std::endl;
	}
}

void ConnectivityManager::natTraversalForwardPortResult(boost::optional<NATPortMapping> mapping) {
	if (mapping) {
		SWIFT_LOG(debug) << "Mapping port was successful." << std::endl;
	} else {
		SWIFT_LOG(debug) << "Mapping port has failed." << std::endl;
	}
}


}