summaryrefslogtreecommitdiffstats
blob: 4ed2f5f3d4e445e9fa8fadd95865b9062037600c (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
/*
 * Copyright (c) 2011 Tobias Markmann
 * Licensed under the simplified BSD license.
 * See Documentation/Licenses/BSD-simplified.txt for more information.
 */

#include "UPnPNATTraversalGetPublicIPRequest.h"

#include <miniupnpc.h>
#include <upnpcommands.h>
#include <upnperrors.h>

namespace Swift {

UPnPNATTraversalGetPublicIPRequest::UPnPNATTraversalGetPublicIPRequest(PlatformNATTraversalWorker* worker) : PlatformNATTraversalRequest(worker) {

}

UPnPNATTraversalGetPublicIPRequest::~UPnPNATTraversalGetPublicIPRequest() {

}

void UPnPNATTraversalGetPublicIPRequest::runBlocking() {
	boost::optional<HostAddress> result;

	UPNPDev* deviceList = 0;
	int error = 0;
	char lanAddrress[64];
	char externalIPAddress[40];
	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;
		} else {
			result = HostAddress(std::string(externalIPAddress));
		}
	} while(false);

	freeUPNPDevlist(deviceList); deviceList = 0;

	onResult(result);
}

}