1 /*************************************************************************/
2 /*  upnp_device.cpp                                                      */
3 /*************************************************************************/
4 /*                       This file is part of:                           */
5 /*                           GODOT ENGINE                                */
6 /*                      https://godotengine.org                          */
7 /*************************************************************************/
8 /* Copyright (c) 2007-2020 Juan Linietsky, Ariel Manzur.                 */
9 /* Copyright (c) 2014-2020 Godot Engine contributors (cf. AUTHORS.md).   */
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 #include "upnp_device.h"
32 
33 #include "upnp.h"
34 
35 #include <miniupnpc/upnpcommands.h>
36 
query_external_address() const37 String UPNPDevice::query_external_address() const {
38 	ERR_FAIL_COND_V(!is_valid_gateway(), "");
39 
40 	char addr[16];
41 	int i = UPNP_GetExternalIPAddress(
42 			igd_control_url.utf8().get_data(),
43 			igd_service_type.utf8().get_data(),
44 			(char *)&addr);
45 
46 	ERR_FAIL_COND_V(i != UPNPCOMMAND_SUCCESS, "");
47 
48 	return String(addr);
49 }
50 
add_port_mapping(int port,int port_internal,String desc,String proto,int duration) const51 int UPNPDevice::add_port_mapping(int port, int port_internal, String desc, String proto, int duration) const {
52 	ERR_FAIL_COND_V(!is_valid_gateway(), UPNP::UPNP_RESULT_INVALID_GATEWAY);
53 	ERR_FAIL_COND_V(port < 1 || port > 65535, UPNP::UPNP_RESULT_INVALID_PORT);
54 	ERR_FAIL_COND_V(port_internal < 0 || port_internal > 65535, UPNP::UPNP_RESULT_INVALID_PORT); // Needs to allow 0 because 0 signifies "use external port as internal port"
55 	ERR_FAIL_COND_V(proto != "UDP" && proto != "TCP", UPNP::UPNP_RESULT_INVALID_PROTOCOL);
56 	ERR_FAIL_COND_V(duration < 0, UPNP::UPNP_RESULT_INVALID_DURATION);
57 
58 	if (port_internal < 1) {
59 		port_internal = port;
60 	}
61 
62 	int i = UPNP_AddPortMapping(
63 			igd_control_url.utf8().get_data(),
64 			igd_service_type.utf8().get_data(),
65 			itos(port).utf8().get_data(),
66 			itos(port_internal).utf8().get_data(),
67 			igd_our_addr.utf8().get_data(),
68 			desc.empty() ? 0 : desc.utf8().get_data(),
69 			proto.utf8().get_data(),
70 			NULL, // Remote host, always NULL as IGDs don't support it
71 			duration > 0 ? itos(duration).utf8().get_data() : 0);
72 
73 	ERR_FAIL_COND_V(i != UPNPCOMMAND_SUCCESS, UPNP::upnp_result(i));
74 
75 	return UPNP::UPNP_RESULT_SUCCESS;
76 }
77 
delete_port_mapping(int port,String proto) const78 int UPNPDevice::delete_port_mapping(int port, String proto) const {
79 	ERR_FAIL_COND_V(port < 1 || port > 65535, UPNP::UPNP_RESULT_INVALID_PORT);
80 	ERR_FAIL_COND_V(proto != "UDP" && proto != "TCP", UPNP::UPNP_RESULT_INVALID_PROTOCOL);
81 
82 	int i = UPNP_DeletePortMapping(
83 			igd_control_url.utf8().get_data(),
84 			igd_service_type.utf8().get_data(),
85 			itos(port).utf8().get_data(),
86 			proto.utf8().get_data(),
87 			NULL // Remote host, always NULL as IGDs don't support it
88 	);
89 
90 	ERR_FAIL_COND_V(i != UPNPCOMMAND_SUCCESS, UPNP::upnp_result(i));
91 
92 	return UPNP::UPNP_RESULT_SUCCESS;
93 }
94 
set_description_url(const String & url)95 void UPNPDevice::set_description_url(const String &url) {
96 	description_url = url;
97 }
98 
get_description_url() const99 String UPNPDevice::get_description_url() const {
100 	return description_url;
101 }
102 
set_service_type(const String & type)103 void UPNPDevice::set_service_type(const String &type) {
104 	service_type = type;
105 }
106 
get_service_type() const107 String UPNPDevice::get_service_type() const {
108 	return service_type;
109 }
110 
set_igd_control_url(const String & url)111 void UPNPDevice::set_igd_control_url(const String &url) {
112 	igd_control_url = url;
113 }
114 
get_igd_control_url() const115 String UPNPDevice::get_igd_control_url() const {
116 	return igd_control_url;
117 }
118 
set_igd_service_type(const String & type)119 void UPNPDevice::set_igd_service_type(const String &type) {
120 	igd_service_type = type;
121 }
122 
get_igd_service_type() const123 String UPNPDevice::get_igd_service_type() const {
124 	return igd_service_type;
125 }
126 
set_igd_our_addr(const String & addr)127 void UPNPDevice::set_igd_our_addr(const String &addr) {
128 	igd_our_addr = addr;
129 }
130 
get_igd_our_addr() const131 String UPNPDevice::get_igd_our_addr() const {
132 	return igd_our_addr;
133 }
134 
set_igd_status(IGDStatus status)135 void UPNPDevice::set_igd_status(IGDStatus status) {
136 	igd_status = status;
137 }
138 
get_igd_status() const139 UPNPDevice::IGDStatus UPNPDevice::get_igd_status() const {
140 	return igd_status;
141 }
142 
is_valid_gateway() const143 bool UPNPDevice::is_valid_gateway() const {
144 	return igd_status == IGD_STATUS_OK;
145 }
146 
_bind_methods()147 void UPNPDevice::_bind_methods() {
148 	ClassDB::bind_method(D_METHOD("is_valid_gateway"), &UPNPDevice::is_valid_gateway);
149 	ClassDB::bind_method(D_METHOD("query_external_address"), &UPNPDevice::query_external_address);
150 	ClassDB::bind_method(D_METHOD("add_port_mapping", "port", "port_internal", "desc", "proto", "duration"), &UPNPDevice::add_port_mapping, DEFVAL(0), DEFVAL(""), DEFVAL("UDP"), DEFVAL(0));
151 	ClassDB::bind_method(D_METHOD("delete_port_mapping", "port", "proto"), &UPNPDevice::delete_port_mapping, DEFVAL("UDP"));
152 
153 	ClassDB::bind_method(D_METHOD("set_description_url", "url"), &UPNPDevice::set_description_url);
154 	ClassDB::bind_method(D_METHOD("get_description_url"), &UPNPDevice::get_description_url);
155 	ADD_PROPERTY(PropertyInfo(Variant::STRING, "description_url"), "set_description_url", "get_description_url");
156 
157 	ClassDB::bind_method(D_METHOD("set_service_type", "type"), &UPNPDevice::set_service_type);
158 	ClassDB::bind_method(D_METHOD("get_service_type"), &UPNPDevice::get_service_type);
159 	ADD_PROPERTY(PropertyInfo(Variant::STRING, "service_type"), "set_service_type", "get_service_type");
160 
161 	ClassDB::bind_method(D_METHOD("set_igd_control_url", "url"), &UPNPDevice::set_igd_control_url);
162 	ClassDB::bind_method(D_METHOD("get_igd_control_url"), &UPNPDevice::get_igd_control_url);
163 	ADD_PROPERTY(PropertyInfo(Variant::STRING, "igd_control_url"), "set_igd_control_url", "get_igd_control_url");
164 
165 	ClassDB::bind_method(D_METHOD("set_igd_service_type", "type"), &UPNPDevice::set_igd_service_type);
166 	ClassDB::bind_method(D_METHOD("get_igd_service_type"), &UPNPDevice::get_igd_service_type);
167 	ADD_PROPERTY(PropertyInfo(Variant::STRING, "igd_service_type"), "set_igd_service_type", "get_igd_service_type");
168 
169 	ClassDB::bind_method(D_METHOD("set_igd_our_addr", "addr"), &UPNPDevice::set_igd_our_addr);
170 	ClassDB::bind_method(D_METHOD("get_igd_our_addr"), &UPNPDevice::get_igd_our_addr);
171 	ADD_PROPERTY(PropertyInfo(Variant::STRING, "igd_our_addr"), "set_igd_our_addr", "get_igd_our_addr");
172 
173 	ClassDB::bind_method(D_METHOD("set_igd_status", "status"), &UPNPDevice::set_igd_status);
174 	ClassDB::bind_method(D_METHOD("get_igd_status"), &UPNPDevice::get_igd_status);
175 	ADD_PROPERTY(PropertyInfo(Variant::INT, "igd_status", PROPERTY_HINT_ENUM), "set_igd_status", "get_igd_status");
176 
177 	BIND_ENUM_CONSTANT(IGD_STATUS_OK);
178 	BIND_ENUM_CONSTANT(IGD_STATUS_HTTP_ERROR);
179 	BIND_ENUM_CONSTANT(IGD_STATUS_HTTP_EMPTY);
180 	BIND_ENUM_CONSTANT(IGD_STATUS_NO_URLS);
181 	BIND_ENUM_CONSTANT(IGD_STATUS_NO_IGD);
182 	BIND_ENUM_CONSTANT(IGD_STATUS_DISCONNECTED);
183 	BIND_ENUM_CONSTANT(IGD_STATUS_UNKNOWN_DEVICE);
184 	BIND_ENUM_CONSTANT(IGD_STATUS_INVALID_CONTROL);
185 	BIND_ENUM_CONSTANT(IGD_STATUS_MALLOC_ERROR);
186 	BIND_ENUM_CONSTANT(IGD_STATUS_UNKNOWN_ERROR);
187 }
188 
UPNPDevice()189 UPNPDevice::UPNPDevice() {
190 	description_url = "";
191 	service_type = "";
192 	igd_control_url = "";
193 	igd_service_type = "";
194 	igd_our_addr = "";
195 	igd_status = IGD_STATUS_UNKNOWN_ERROR;
196 }
197 
~UPNPDevice()198 UPNPDevice::~UPNPDevice() {
199 }
200