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
|
/*
* 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->run();
}
boost::shared_ptr<NATTraversalForwardPortRequest> forwardPortRequest = natTraversalWorker->createForwardPortRequest(port, port);
if (forwardPortRequest) {
forwardPortRequest->onResult.connect(boost::bind(&ConnectivityManager::natTraversalForwardPortResult, this, _1));
forwardPortRequest->run();
}
}
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->run();
}
}
std::vector<HostAddressPort> ConnectivityManager::getHostAddressPorts() const {
PlatformNetworkEnvironment env;
std::vector<HostAddressPort> results;
std::vector<HostAddress> addresses;
foreach (NetworkInterface::ref iface, env.getNetworkInterfaces()) {
foreach (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<NATTraversalForwardPortRequest::PortMapping> mapping) {
if (mapping) {
SWIFT_LOG(debug) << "Mapping port was successful." << std::endl;
} else {
SWIFT_LOG(debug) << "Mapping port has failed." << std::endl;
}
}
}
|