Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
godotengine
GitHub Repository: godotengine/godot
Path: blob/master/modules/upnp/upnp_device_miniupnp.cpp
10277 views
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 <miniupnpc/upnpcommands.h>
38
39
void UPNPDeviceMiniUPNP::make_default() {
40
UPNPDevice::_create = UPNPDeviceMiniUPNP::_create;
41
}
42
43
String UPNPDeviceMiniUPNP::query_external_address() const {
44
ERR_FAIL_COND_V_MSG(!is_valid_gateway(), "", "The Internet Gateway Device must be valid.");
45
46
char addr[16];
47
int i = UPNP_GetExternalIPAddress(
48
igd_control_url.utf8().get_data(),
49
igd_service_type.utf8().get_data(),
50
(char *)&addr);
51
52
ERR_FAIL_COND_V_MSG(i != UPNPCOMMAND_SUCCESS, "", "Couldn't get external IP address.");
53
54
return String(addr);
55
}
56
57
int UPNPDeviceMiniUPNP::add_port_mapping(int port, int port_internal, String desc, String proto, int duration) const {
58
ERR_FAIL_COND_V_MSG(!is_valid_gateway(), UPNP::UPNP_RESULT_INVALID_GATEWAY, "The Internet Gateway Device must be valid.");
59
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).");
60
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"
61
ERR_FAIL_COND_V_MSG(proto != "UDP" && proto != "TCP", UPNP::UPNP_RESULT_INVALID_PROTOCOL, "The protocol must be either TCP or UDP.");
62
ERR_FAIL_COND_V_MSG(duration < 0, UPNP::UPNP_RESULT_INVALID_DURATION, "The port mapping's lease duration can't be negative.");
63
64
if (port_internal < 1) {
65
port_internal = port;
66
}
67
68
int i = UPNP_AddPortMapping(
69
igd_control_url.utf8().get_data(),
70
igd_service_type.utf8().get_data(),
71
itos(port).utf8().get_data(),
72
itos(port_internal).utf8().get_data(),
73
igd_our_addr.utf8().get_data(),
74
desc.is_empty() ? nullptr : desc.utf8().get_data(),
75
proto.utf8().get_data(),
76
nullptr, // Remote host, always nullptr as IGDs don't support it
77
duration > 0 ? itos(duration).utf8().get_data() : nullptr);
78
79
ERR_FAIL_COND_V_MSG(i != UPNPCOMMAND_SUCCESS, UPNPMiniUPNP::upnp_result(i), "Couldn't add port mapping.");
80
81
return UPNP::UPNP_RESULT_SUCCESS;
82
}
83
84
int UPNPDeviceMiniUPNP::delete_port_mapping(int port, String proto) const {
85
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).");
86
ERR_FAIL_COND_V_MSG(proto != "UDP" && proto != "TCP", UPNP::UPNP_RESULT_INVALID_PROTOCOL, "The protocol must be either TCP or UDP.");
87
88
int i = UPNP_DeletePortMapping(
89
igd_control_url.utf8().get_data(),
90
igd_service_type.utf8().get_data(),
91
itos(port).utf8().get_data(),
92
proto.utf8().get_data(),
93
nullptr // Remote host, always nullptr as IGDs don't support it
94
);
95
96
ERR_FAIL_COND_V_MSG(i != UPNPCOMMAND_SUCCESS, UPNPMiniUPNP::upnp_result(i), "Couldn't delete port mapping.");
97
98
return UPNP::UPNP_RESULT_SUCCESS;
99
}
100
101
void UPNPDeviceMiniUPNP::set_description_url(const String &url) {
102
description_url = url;
103
}
104
105
String UPNPDeviceMiniUPNP::get_description_url() const {
106
return description_url;
107
}
108
109
void UPNPDeviceMiniUPNP::set_service_type(const String &type) {
110
service_type = type;
111
}
112
113
String UPNPDeviceMiniUPNP::get_service_type() const {
114
return service_type;
115
}
116
117
void UPNPDeviceMiniUPNP::set_igd_control_url(const String &url) {
118
igd_control_url = url;
119
}
120
121
String UPNPDeviceMiniUPNP::get_igd_control_url() const {
122
return igd_control_url;
123
}
124
125
void UPNPDeviceMiniUPNP::set_igd_service_type(const String &type) {
126
igd_service_type = type;
127
}
128
129
String UPNPDeviceMiniUPNP::get_igd_service_type() const {
130
return igd_service_type;
131
}
132
133
void UPNPDeviceMiniUPNP::set_igd_our_addr(const String &addr) {
134
igd_our_addr = addr;
135
}
136
137
String UPNPDeviceMiniUPNP::get_igd_our_addr() const {
138
return igd_our_addr;
139
}
140
141
void UPNPDeviceMiniUPNP::set_igd_status(IGDStatus status) {
142
igd_status = status;
143
}
144
145
UPNPDeviceMiniUPNP::IGDStatus UPNPDeviceMiniUPNP::get_igd_status() const {
146
return igd_status;
147
}
148
149
bool UPNPDeviceMiniUPNP::is_valid_gateway() const {
150
return igd_status == IGD_STATUS_OK;
151
}
152
153
#endif // WEB_ENABLED
154
155