summaryrefslogtreecommitdiffstats
blob: 4ac7e6ad7b57587cd9619f29705859f0f60405fc (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
/*
 * Copyright (c) 2011 Remko Tronçon
 * Licensed under the GNU General Public License v3.
 * See Documentation/Licenses/GPLv3.txt for more information.
 */

#include <iostream>
#include <boost/lexical_cast.hpp>

#include <Swiften/EventLoop/SimpleEventLoop.h>
#include <Swiften/Network/PlatformNATTraversalWorker.h>
#include <Swiften/Network/NATTraversalGetPublicIPRequest.h>
#include <Swiften/Network/NATTraversalForwardPortRequest.h>
#include <Swiften/Network/NATTraversalRemovePortForwardingRequest.h>
#include <Swiften/Network/PlatformNetworkEnvironment.h>

using namespace Swift;

static SimpleEventLoop eventLoop;

static void handleGetPublicIPRequestResponse(const boost::optional<HostAddress>& result) {
	if (result) {
		std::cerr << "Result: " << result->toString() << std::endl;;
	}
	else {
		std::cerr << "No result" << std::endl;
	}
	eventLoop.stop();
}

static void handleGetForwardPortRequestResponse(const boost::optional<NATPortMapping>& result) {
	if (result) {
		std::cerr << "Result: " << result->getPublicPort() << " -> " << result->getLocalPort() << std::endl;;
	}
	else {
		std::cerr << "No result" << std::endl;
	}
	eventLoop.stop();
}

static void handleRemovePortForwardingRequestResponse(bool result) {
	if (result) {
		std::cerr << "Result: OK" << std::endl;
	}
	else {
		std::cerr << "Result: ERROR" << std::endl;
	}
	eventLoop.stop();
}

int main(int argc, char* argv[]) {
	if (argc < 2) {
		std::cerr << "Invalid parameters" << std::endl;
		return -1;
	}

	PlatformNATTraversalWorker natTraverser(&eventLoop);
	if (std::string(argv[1]) == "get-public-ip") {
		boost::shared_ptr<NATTraversalGetPublicIPRequest> query = natTraverser.createGetPublicIPRequest();
		query->onResult.connect(boost::bind(&handleGetPublicIPRequestResponse, _1));
		query->run();
		eventLoop.run();
	}
	else if (std::string(argv[1]) == "add-port-forward") {
		if (argc < 4) {
			std::cerr << "Invalid parameters" << std::endl;
		}
		boost::shared_ptr<NATTraversalForwardPortRequest> query = natTraverser.createForwardPortRequest(boost::lexical_cast<int>(argv[2]), boost::lexical_cast<int>(argv[3]));
		query->onResult.connect(boost::bind(&handleGetForwardPortRequestResponse, _1));
		query->run();
		eventLoop.run();
	}
	else if (std::string(argv[1]) == "remove-port-forward") {
		if (argc < 4) {
			std::cerr << "Invalid parameters" << std::endl;
		}
		boost::shared_ptr<NATTraversalRemovePortForwardingRequest> query = natTraverser.createRemovePortForwardingRequest(boost::lexical_cast<int>(argv[2]), boost::lexical_cast<int>(argv[3]));
		query->onResult.connect(boost::bind(&handleRemovePortForwardingRequestResponse, _1));
		query->run();
		eventLoop.run();
	}
	else if (std::string(argv[1]) == "get-local-ip") {
		std::cout << PlatformNetworkEnvironment().getLocalAddress().toString() << std::endl;
	}
}