File: port_mapping_manager_impl_libplum.cpp

package info (click to toggle)
warzone2100 4.6.3-1
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 660,320 kB
  • sloc: cpp: 676,209; ansic: 391,201; javascript: 78,238; python: 16,632; php: 4,294; sh: 4,094; makefile: 2,629; lisp: 1,492; cs: 489; xml: 404; perl: 224; ruby: 156; java: 89
file content (149 lines) | stat: -rw-r--r-- 4,243 bytes parent folder | download
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
/*
	This file is part of Warzone 2100.
	Copyright (C) 2024  Warzone 2100 Project

	Warzone 2100 is free software; you can redistribute it and/or modify
	it under the terms of the GNU General Public License as published by
	the Free Software Foundation; either version 2 of the License, or
	(at your option) any later version.

	Warzone 2100 is distributed in the hope that it will be useful,
	but WITHOUT ANY WARRANTY; without even the implied warranty of
	MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
	GNU General Public License for more details.

	You should have received a copy of the GNU General Public License
	along with Warzone 2100; if not, write to the Free Software
	Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*/
/**
 * @file port_mapping_manager_impl_libplum.cpp
 * libplum-based port mapping implementation
 */

#include "lib/framework/wzapp.h"
#include "lib/framework/debug.h"

#include "port_mapping_manager_impl_libplum.h"

#include <plum/plum.h>

void PortMappingImpl_MappingCallbackSuccess(PortMappingImpl::Type type, int mappingId, const std::string& externalHost, uint16_t externalPort);
void PortMappingImpl_MappingCallbackFailure(PortMappingImpl::Type type, int mappingId);

// MARK: - Libplum implementation

namespace
{

// This function is run in its own thread managed by LibPLum! Do not call any non-threadsafe functions!
void PlumMappingCallback(int mappingId, plum_state_t state, const plum_mapping_t* mapping)
{
	debug(LOG_NET, "LibPlum cb: Port mapping %d: state=%d\n", mappingId, (int)state);
	switch (state) {
	case PLUM_STATE_SUCCESS:
		debug(LOG_NET, "Mapping %d: success, internal=%hu, external=%s:%hu\n", mappingId, mapping->internal_port,
			mapping->external_host, mapping->external_port);
		PortMappingImpl_MappingCallbackSuccess(PortMappingImpl::Type::Libplum, mappingId, mapping->external_host, mapping->external_port);
		break;

	case PLUM_STATE_FAILURE:
		debug(LOG_NET, "Mapping %d: failed\n", mappingId);
		PortMappingImpl_MappingCallbackFailure(PortMappingImpl::Type::Libplum, mappingId);
		break;

	default:
		break;
	}
}

void PlumLogCallback(plum_log_level_t level, const char* message)
{
	code_part wz_log_level = LOG_NET;
	switch (level)
	{
	case PLUM_LOG_LEVEL_FATAL:
	case PLUM_LOG_LEVEL_ERROR:
	case PLUM_LOG_LEVEL_WARN:
		wz_log_level = LOG_INFO;
		break;
	default:
		break;
	}
	if (enabled_debug[wz_log_level])
	{
		std::string msg = (message) ? message : "";
		wzAsyncExecOnMainThread([wz_log_level, msg]() {
			_debug(__LINE__, wz_log_level, "PlumLogCallback", "%s", msg.c_str());
		});
	}
}

} // anonymous namespace

PortMappingImpl_LibPlum::~PortMappingImpl_LibPlum()
{
	try
	{
		shutdown();
	}
	catch(...) // Don't let any exceptions escape the dtor.
	{}
}

PortMappingImpl::Type PortMappingImpl_LibPlum::get_impl_type() const
{
	return PortMappingImpl::Type::Libplum;
}

int PortMappingImpl_LibPlum::get_discovery_timeout_seconds() const
{
	return 10; // 10 seconds for Libplum, to ensure we don't wait too long before falling-back to Miniupnpc
}

bool PortMappingImpl_LibPlum::init()
{
	plum_config_t config;
	memset(&config, 0, sizeof(config));
	config.log_level = PLUM_LOG_LEVEL_INFO;
	config.log_callback = &PlumLogCallback;
	auto res = plum_init(&config);
	m_isInit = (res == PLUM_ERR_SUCCESS);
	return res == PLUM_ERR_SUCCESS;
}

bool PortMappingImpl_LibPlum::shutdown()
{
	if (!m_isInit)
	{
		return false;
	}
	plum_cleanup();
	m_isInit = false;
	return true;
}

int PortMappingImpl_LibPlum::create_port_mapping(uint16_t port, PortMappingInternetProtocol protocol)
{
	plum_mapping_t m;
	memset(&m, 0, sizeof(m));
	if (protocol == PortMappingInternetProtocol::TCP_IPV4 || protocol == PortMappingInternetProtocol::TCP_IPV6)
	{
		m.protocol = PLUM_IP_PROTOCOL_TCP;
	}
	else // UDP_IPV4 / UDP_IPV6
	{
		m.protocol = PLUM_IP_PROTOCOL_UDP;
	}
	m.internal_port = port;
	m.external_port = port; // suggest an external port the same as the internal port (the router may decide otherwise)

	auto mappingId = plum_create_mapping(&m, PlumMappingCallback);
	return mappingId;
}

bool PortMappingImpl_LibPlum::destroy_port_mapping(int mappingId)
{
	const auto result = plum_destroy_mapping(mappingId);
	return result == PLUM_ERR_SUCCESS;
}