Skip to content
0.2.0-dev Download ZIP
159 lines ·6.4 KB Raw
1/**************************************************************************/
2/* upnp_device_miniupnp.cpp */
3/**************************************************************************/
4/* This file is part of: */
5/* GODOT ENGINE */
6/* https://godotengine.org */
7/**************************************************************************/
8/* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */
9/* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
10/* */
11/* Permission is hereby granted, free of charge, to any person obtaining */
12/* a copy of this software and associated documentation files (the */
13/* "Software"), to deal in the Software without restriction, including */
14/* without limitation the rights to use, copy, modify, merge, publish, */
15/* distribute, sublicense, and/or sell copies of the Software, and to */
16/* permit persons to whom the Software is furnished to do so, subject to */
17/* the following conditions: */
18/* */
19/* The above copyright notice and this permission notice shall be */
20/* included in all copies or substantial portions of the Software. */
21/* */
22/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
23/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
24/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */
25/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
26/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
27/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
28/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
29/**************************************************************************/
30
31#ifndef WEB_ENABLED
32
33#include "upnp_device_miniupnp.h"
34
35#include "upnp_miniupnp.h"
36
37#include "core/object/class_db.h"
38
39#include <miniupnpc/upnpcommands.h>
40
41UPNPDevice *UPNPDeviceMiniUPNP::_create(bool p_notify_postinitialize) {
42 return static_cast<UPNPDevice *>(ClassDB::creator<UPNPDeviceMiniUPNP>(p_notify_postinitialize));
43}
44
45void UPNPDeviceMiniUPNP::make_default() {
46 UPNPDevice::_create = UPNPDeviceMiniUPNP::_create;
47}
48
49String UPNPDeviceMiniUPNP::query_external_address() const {
50 ERR_FAIL_COND_V_MSG(!is_valid_gateway(), "", "The Internet Gateway Device must be valid.");
51
52 char addr[16];
53 int i = UPNP_GetExternalIPAddress(
54 igd_control_url.utf8().get_data(),
55 igd_service_type.utf8().get_data(),
56 (char *)&addr);
57
58 ERR_FAIL_COND_V_MSG(i != UPNPCOMMAND_SUCCESS, "", "Couldn't get external IP address.");
59
60 return String(addr);
61}
62
63int UPNPDeviceMiniUPNP::add_port_mapping(int port, int port_internal, String desc, String proto, int duration) const {
64 ERR_FAIL_COND_V_MSG(!is_valid_gateway(), UPNP::UPNP_RESULT_INVALID_GATEWAY, "The Internet Gateway Device must be valid.");
65 ERR_FAIL_COND_V_MSG(port < 1 || port > 65535, UPNP::UPNP_RESULT_INVALID_PORT, "The port number must be set between 1 and 65535 (inclusive).");
66 ERR_FAIL_COND_V_MSG(port_internal < 0 || port_internal > 65535, UPNP::UPNP_RESULT_INVALID_PORT, "The port number must be set between 0 and 65535 (inclusive)."); // Needs to allow 0 because 0 signifies "use external port as internal port"
67 ERR_FAIL_COND_V_MSG(proto != "UDP" && proto != "TCP", UPNP::UPNP_RESULT_INVALID_PROTOCOL, "The protocol must be either TCP or UDP.");
68 ERR_FAIL_COND_V_MSG(duration < 0, UPNP::UPNP_RESULT_INVALID_DURATION, "The port mapping's lease duration can't be negative.");
69
70 if (port_internal < 1) {
71 port_internal = port;
72 }
73
74 int i = UPNP_AddPortMapping(
75 igd_control_url.utf8().get_data(),
76 igd_service_type.utf8().get_data(),
77 itos(port).utf8().get_data(),
78 itos(port_internal).utf8().get_data(),
79 igd_our_addr.utf8().get_data(),
80 desc.is_empty() ? nullptr : desc.utf8().get_data(),
81 proto.utf8().get_data(),
82 nullptr, // Remote host, always nullptr as IGDs don't support it
83 duration > 0 ? itos(duration).utf8().get_data() : nullptr);
84
85 ERR_FAIL_COND_V_MSG(i != UPNPCOMMAND_SUCCESS, UPNPMiniUPNP::upnp_result(i), "Couldn't add port mapping.");
86
87 return UPNP::UPNP_RESULT_SUCCESS;
88}
89
90int UPNPDeviceMiniUPNP::delete_port_mapping(int port, String proto) const {
91 ERR_FAIL_COND_V_MSG(port < 1 || port > 65535, UPNP::UPNP_RESULT_INVALID_PORT, "The port number must be set between 1 and 65535 (inclusive).");
92 ERR_FAIL_COND_V_MSG(proto != "UDP" && proto != "TCP", UPNP::UPNP_RESULT_INVALID_PROTOCOL, "The protocol must be either TCP or UDP.");
93
94 int i = UPNP_DeletePortMapping(
95 igd_control_url.utf8().get_data(),
96 igd_service_type.utf8().get_data(),
97 itos(port).utf8().get_data(),
98 proto.utf8().get_data(),
99 nullptr // Remote host, always nullptr as IGDs don't support it
100 );
101
102 ERR_FAIL_COND_V_MSG(i != UPNPCOMMAND_SUCCESS, UPNPMiniUPNP::upnp_result(i), "Couldn't delete port mapping.");
103
104 return UPNP::UPNP_RESULT_SUCCESS;
105}
106
107void UPNPDeviceMiniUPNP::set_description_url(const String &url) {
108 description_url = url;
109}
110
111String UPNPDeviceMiniUPNP::get_description_url() const {
112 return description_url;
113}
114
115void UPNPDeviceMiniUPNP::set_service_type(const String &type) {
116 service_type = type;
117}
118
119String UPNPDeviceMiniUPNP::get_service_type() const {
120 return service_type;
121}
122
123void UPNPDeviceMiniUPNP::set_igd_control_url(const String &url) {
124 igd_control_url = url;
125}
126
127String UPNPDeviceMiniUPNP::get_igd_control_url() const {
128 return igd_control_url;
129}
130
131void UPNPDeviceMiniUPNP::set_igd_service_type(const String &type) {
132 igd_service_type = type;
133}
134
135String UPNPDeviceMiniUPNP::get_igd_service_type() const {
136 return igd_service_type;
137}
138
139void UPNPDeviceMiniUPNP::set_igd_our_addr(const String &addr) {
140 igd_our_addr = addr;
141}
142
143String UPNPDeviceMiniUPNP::get_igd_our_addr() const {
144 return igd_our_addr;
145}
146
147void UPNPDeviceMiniUPNP::set_igd_status(IGDStatus status) {
148 igd_status = status;
149}
150
151UPNPDeviceMiniUPNP::IGDStatus UPNPDeviceMiniUPNP::get_igd_status() const {
152 return igd_status;
153}
154
155bool UPNPDeviceMiniUPNP::is_valid_gateway() const {
156 return igd_status == IGD_STATUS_OK;
157}
158
159#endif // WEB_ENABLED