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
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
|
/*
* 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 <boost/enable_shared_from_this.hpp>
#include <boost/numeric/conversion/cast.hpp>
#include <Swiften/Base/Log.h>
#include <Swiften/EventLoop/EventLoop.h>
#include <Swiften/Network/NATTraversalGetPublicIPRequest.h>
#include <Swiften/Network/NATTraversalForwardPortRequest.h>
#include <Swiften/Network/NATTraversalRemovePortForwardingRequest.h>
#ifdef HAVE_LIBNATPMP
#include <Swiften/Network/NATPMPInterface.h>
#endif
#ifdef HAVE_LIBMINIUPNPC
#include <Swiften/Network/MiniUPnPInterface.h>
#endif
namespace Swift {
class PlatformNATTraversalRequest : public boost::enable_shared_from_this<PlatformNATTraversalRequest> {
public:
typedef boost::shared_ptr<PlatformNATTraversalRequest> ref;
public:
PlatformNATTraversalRequest(PlatformNATTraversalWorker* worker) : worker(worker) {
}
virtual ~PlatformNATTraversalRequest() {
}
virtual void doRun() {
worker->addRequestToQueue(shared_from_this());
}
NATTraversalInterface* getNATTraversalInterface() const {
return worker->getNATTraversalInterface();
}
EventLoop* getEventLoop() const {
return worker->getEventLoop();
}
virtual void runBlocking() = 0;
private:
PlatformNATTraversalWorker* worker;
};
class PlatformNATTraversalGetPublicIPRequest : public NATTraversalGetPublicIPRequest, public PlatformNATTraversalRequest {
public:
PlatformNATTraversalGetPublicIPRequest(PlatformNATTraversalWorker* worker) : PlatformNATTraversalRequest(worker) {
}
virtual void start() {
doRun();
}
virtual void stop() {
// TODO
}
virtual void runBlocking() {
getEventLoop()->postEvent(boost::bind(boost::ref(onResult), getNATTraversalInterface()->getPublicIP()));
}
};
class PlatformNATTraversalForwardPortRequest : public NATTraversalForwardPortRequest, public PlatformNATTraversalRequest {
public:
PlatformNATTraversalForwardPortRequest(PlatformNATTraversalWorker* worker, unsigned int localIP, unsigned int publicIP) : PlatformNATTraversalRequest(worker), localIP(localIP), publicIP(publicIP) {
}
virtual void start() {
doRun();
}
virtual void stop() {
// TODO
}
virtual void runBlocking() {
getEventLoop()->postEvent(boost::bind(boost::ref(onResult), getNATTraversalInterface()->addPortForward(boost::numeric_cast<int>(localIP), boost::numeric_cast<int>(publicIP))));
}
private:
unsigned int localIP;
unsigned int publicIP;
};
class PlatformNATTraversalRemovePortForwardingRequest : public NATTraversalRemovePortForwardingRequest, public PlatformNATTraversalRequest {
public:
PlatformNATTraversalRemovePortForwardingRequest(PlatformNATTraversalWorker* worker, const NATPortMapping& mapping) : PlatformNATTraversalRequest(worker), mapping(mapping) {
}
virtual void start() {
doRun();
}
virtual void stop() {
// TODO
}
virtual void runBlocking() {
getEventLoop()->postEvent(boost::bind(boost::ref(onResult), getNATTraversalInterface()->removePortForward(mapping)));
}
private:
NATPortMapping mapping;
};
PlatformNATTraversalWorker::PlatformNATTraversalWorker(EventLoop* eventLoop) : eventLoop(eventLoop), stopRequested(false), natPMPSupported(boost::logic::indeterminate), natPMPInterface(NULL), miniUPnPSupported(boost::logic::indeterminate), miniUPnPInterface(NULL) {
nullNATTraversalInterface = new NullNATTraversalInterface();
// FIXME: This should be done from start(), and the current start() should be an internal method
thread = new boost::thread(boost::bind(&PlatformNATTraversalWorker::start, this));
}
PlatformNATTraversalWorker::~PlatformNATTraversalWorker() {
stopRequested = true;
addRequestToQueue(boost::shared_ptr<PlatformNATTraversalRequest>());
thread->join();
delete thread;
#ifdef HAVE_LIBNATPMP
delete natPMPInterface;
#endif
#ifdef HAVE_LIBMINIUPNPC
delete miniUPnPInterface;
#endif
delete nullNATTraversalInterface;
}
NATTraversalInterface* PlatformNATTraversalWorker::getNATTraversalInterface() const {
#ifdef HAVE_LIBMINIUPNPC
if (boost::logic::indeterminate(miniUPnPSupported)) {
miniUPnPInterface = new MiniUPnPInterface();
miniUPnPSupported = miniUPnPInterface->isAvailable();
}
SWIFT_LOG(debug) << "UPnP NAT traversal supported: " << miniUPnPSupported << std::endl;
if (miniUPnPSupported) {
return miniUPnPInterface;
}
#endif
#ifdef HAVE_LIBNATPMP
if (boost::logic::indeterminate(natPMPSupported)) {
natPMPInterface = new NATPMPInterface();
natPMPSupported = natPMPInterface->isAvailable();
}
SWIFT_LOG(debug) << "NAT-PMP NAT traversal supported: " << natPMPSupported << std::endl;
if (natPMPSupported) {
return natPMPInterface;
}
#endif
return nullNATTraversalInterface;
}
boost::shared_ptr<NATTraversalGetPublicIPRequest> PlatformNATTraversalWorker::createGetPublicIPRequest() {
return boost::make_shared<PlatformNATTraversalGetPublicIPRequest>(this);
}
boost::shared_ptr<NATTraversalForwardPortRequest> PlatformNATTraversalWorker::createForwardPortRequest(int localPort, int publicPort) {
return boost::make_shared<PlatformNATTraversalForwardPortRequest>(this, localPort, publicPort);
}
boost::shared_ptr<NATTraversalRemovePortForwardingRequest> PlatformNATTraversalWorker::createRemovePortForwardingRequest(int localPort, int publicPort) {
NATPortMapping mapping(localPort, publicPort, NATPortMapping::TCP); // FIXME
return boost::make_shared<PlatformNATTraversalRemovePortForwardingRequest>(this, mapping);
}
void PlatformNATTraversalWorker::start() {
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::stop() {
// TODO
}
void PlatformNATTraversalWorker::addRequestToQueue(PlatformNATTraversalRequest::ref request) {
{
boost::lock_guard<boost::mutex> lock(queueMutex);
queue.push_back(request);
}
queueNonEmpty.notify_one();
}
}
|