summaryrefslogtreecommitdiffstats
blob: e0dcab53267f2ec8f3ccf318045c423a33958b1e (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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
/*
 * Copyright (c) 2011 Tobias Markmann
 * Licensed under the simplified BSD license.
 * See Documentation/Licenses/BSD-simplified.txt for more information.
 */

#include "PlatformNATTraversalWorker.h"

#include <boost/smart_ptr/make_shared.hpp>
#include <Swiften/Base/Log.h>
#include <Swiften/Network/UPnPNATTraversalGetPublicIPRequest.h>
#include <Swiften/Network/UPnPNATTraversalForwardPortRequest.h>
#include <Swiften/Network/UPnPNATTraversalRemovePortForwardingRequest.h>
#include <Swiften/Network/NATPMPNATTraversalGetPublicIPRequest.h>
#include <Swiften/Network/NATPMPNATTraversalForwardPortRequest.h>
#include <Swiften/Network/NATPMPNATTraversalRemovePortForwardingRequest.h>

namespace Swift {

PlatformNATTraversalWorker::PlatformNATTraversalWorker(EventLoop* eventLoop) : backendType(NotYetDecided), eventLoop(eventLoop), stopRequested(false) {
	checkAvailableNATTraversalProtocols();
	thread = new boost::thread(boost::bind(&PlatformNATTraversalWorker::run, this));
}

PlatformNATTraversalWorker::~PlatformNATTraversalWorker() {
	stopRequested = true;
	addRequestToQueue(boost::shared_ptr<PlatformNATTraversalRequest>());
	thread->join();
	delete thread;
}

boost::shared_ptr<NATTraversalGetPublicIPRequest> PlatformNATTraversalWorker::createGetPublicIPRequest() {
	switch(backendType) {
		case UPnP:
			return boost::make_shared<UPnPNATTraversalGetPublicIPRequest>(this);
		case NATPMP:
			return boost::make_shared<NATPMPNATTraversalGetPublicIPRequest>(this);
		case NotYetDecided:
		case None:
			break;
	}
	return boost::shared_ptr<NATTraversalGetPublicIPRequest>();
}

boost::shared_ptr<NATTraversalForwardPortRequest> PlatformNATTraversalWorker::createForwardPortRequest(unsigned int localPort, unsigned int publicPort) {
	NATTraversalForwardPortRequest::PortMapping mapping;
	mapping.protocol = NATTraversalForwardPortRequest::PortMapping::TCP;
	mapping.leaseInSeconds = 60 * 60 * 24;
	mapping.localPort = localPort;
	mapping.publicPort = publicPort;

	switch(backendType) {
		case UPnP:
			return boost::make_shared<UPnPNATTraversalForwardPortRequest>(mapping, this);
		case NATPMP:
			return boost::make_shared<NATPMPNATTraversalForwardPortRequest>(mapping, this);
		case NotYetDecided:
		case None:
			break;
	}
	return boost::shared_ptr<NATTraversalForwardPortRequest>();
}

boost::shared_ptr<NATTraversalRemovePortForwardingRequest> PlatformNATTraversalWorker::createRemovePortForwardingRequest(unsigned int localPort, unsigned int publicPort) {
	NATTraversalRemovePortForwardingRequest::PortMapping mapping;
	mapping.protocol = NATTraversalRemovePortForwardingRequest::PortMapping::TCP;
	mapping.leaseInSeconds = 60 * 60 * 24;
	mapping.localPort = localPort;
	mapping.publicPort = publicPort;

	switch(backendType) {
		case UPnP:
			return boost::make_shared<UPnPNATTraversalRemovePortForwardingRequest>(mapping, this);
		case NATPMP:
			return boost::make_shared<NATPMPNATTraversalRemovePortForwardingRequest>(mapping, this);
		case NotYetDecided:
		case None:
			break;
	}
	return boost::shared_ptr<NATTraversalRemovePortForwardingRequest>();
}

void PlatformNATTraversalWorker::run() {
	while (!stopRequested) {
		PlatformNATTraversalRequest::ref request;
		{
			boost::unique_lock<boost::mutex> lock(queueMutex);
			while (queue.empty()) {
				queueNonEmpty.wait(lock);
			}
			request = queue.front();
			queue.pop_front();
		}
		// Check whether we don't have a non-null request (used to stop the
		// worker)
		if (request) {
			request->runBlocking();
		}
	}
}

void PlatformNATTraversalWorker::addRequestToQueue(PlatformNATTraversalRequest::ref request) {
	{
		boost::lock_guard<boost::mutex> lock(queueMutex);
		queue.push_back(request);
	}
	queueNonEmpty.notify_one();
}

void PlatformNATTraversalWorker::checkAvailableNATTraversalProtocols() {
	boost::shared_ptr<UPnPNATTraversalGetPublicIPRequest> upnpRequest = boost::make_shared<UPnPNATTraversalGetPublicIPRequest>(this);
	upnpRequest->onResult.connect(boost::bind(&PlatformNATTraversalWorker::handleUPnPGetPublicIPResult, this, _1));

	boost::shared_ptr<NATPMPNATTraversalGetPublicIPRequest> natpmpRequest = boost::make_shared<NATPMPNATTraversalGetPublicIPRequest>(this);
	natpmpRequest->onResult.connect(boost::bind(&PlatformNATTraversalWorker::handleNATPMPGetPublicIPResult, this, _1));

	upnpRequest->run();
	natpmpRequest->run();
}

void PlatformNATTraversalWorker::handleUPnPGetPublicIPResult(boost::optional<HostAddress> address) {
	if (backendType == NotYetDecided || backendType == None) {
		if (address) {
			SWIFT_LOG(debug) << "Found UPnP IGD in the local network." << std::endl;
			backendType = UPnP;
		}
	}
}

void PlatformNATTraversalWorker::handleNATPMPGetPublicIPResult(boost::optional<HostAddress> address) {
	if (backendType == NotYetDecided || backendType == None) {
		if (address) {
			SWIFT_LOG(debug) << "Found NAT-PMP device in the local network." << std::endl;
			backendType = NATPMP;
		}
	}
}

}