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
|
/*
* Copyright (c) 2011 Tobias Markmann
* Licensed under the simplified BSD license.
* See Documentation/Licenses/BSD-simplified.txt for more information.
*/
#include "UPnPNATTraversalRemovePortForwardingRequest.h"
#include <boost/format.hpp>
#include <miniupnpc.h>
#include <upnpcommands.h>
#include <upnperrors.h>
#include <Swiften/Base/foreach.h>
#include <Swiften/Base/Log.h>
#include <Swiften/Network/NetworkInterface.h>
#include <Swiften/Network/PlatformNetworkEnvironment.h>
namespace Swift {
UPnPNATTraversalRemovePortForwardingRequest::UPnPNATTraversalRemovePortForwardingRequest(NATTraversalRemovePortForwardingRequest::PortMapping mapping, PlatformNATTraversalWorker* worker) : PlatformNATTraversalRequest(worker), mapping(mapping) {
}
UPnPNATTraversalRemovePortForwardingRequest::~UPnPNATTraversalRemovePortForwardingRequest() {
}
void UPnPNATTraversalRemovePortForwardingRequest::runBlocking() {
boost::optional<bool> result;
UPNPDev* deviceList = 0;
int error = 0;
char lanAddrress[64];
std::string publicPort = str(boost::format("%d") % mapping.publicPort);
std::string localPort = str(boost::format("%d") % mapping.localPort);
std::string internalClient = getLocalClient().toString();
std::string leaseSeconds = str(boost::format("%d") % mapping.leaseInSeconds);
UPNPUrls urls;
IGDdatas data;
do {
// find valid IGD
deviceList = upnpDiscover(1500 /* timeout in ms */, 0, 0, 0, 0 /* do IPv6? */, &error);
if (!deviceList) {
break;
}
if (!UPNP_GetValidIGD(deviceList, &urls, &data, lanAddrress, sizeof(lanAddrress))) {
break;
}
/*
int ret = UPNP_GetExternalIPAddress(urls.controlURL, data.first.servicetype, externalIPAddress);
if (ret != UPNPCOMMAND_SUCCESS) {
break;
}*/
SWIFT_LOG(debug) << "Start removing port forwarding..." << std::endl;
int ret = UPNP_DeletePortMapping(urls.controlURL, data.first.servicetype, publicPort.c_str(), mapping.protocol == NATTraversalRemovePortForwardingRequest::PortMapping::TCP ? "TCP" : "UDP", 0);
if (ret == UPNPCOMMAND_SUCCESS) {
SWIFT_LOG(debug) << "Removing port " << publicPort << " successfull." << std::endl;
result = true;
} else {
SWIFT_LOG(debug) << "Removing port " << publicPort << " failed." << std::endl;
result = false;
}
} while(false);
freeUPNPDevlist(deviceList); deviceList = 0;
onResult(result);
}
HostAddress UPnPNATTraversalRemovePortForwardingRequest::getLocalClient() {
PlatformNetworkEnvironment env;
foreach (NetworkInterface::ref iface, env.getNetworkInterfaces()) {
if (!iface->isLoopback()) {
foreach (HostAddress address, iface->getAddresses()) {
if (address.getRawAddress().is_v4()) {
return address;
}
}
}
}
return HostAddress();
}
}
|