upnp.cpp 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391
  1. /**************************************************************************/
  2. /* upnp.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. #include "upnp.h"
  31. #include <miniwget.h>
  32. #include <upnpcommands.h>
  33. #include <stdlib.h>
  34. bool UPNP::is_common_device(const String &dev) const {
  35. return dev.is_empty() ||
  36. dev.contains("InternetGatewayDevice") ||
  37. dev.contains("WANIPConnection") ||
  38. dev.contains("WANPPPConnection") ||
  39. dev.contains("rootdevice");
  40. }
  41. int UPNP::discover(int timeout, int ttl, const String &device_filter) {
  42. ERR_FAIL_COND_V_MSG(timeout < 0, UPNP_RESULT_INVALID_PARAM, "The response's wait time can't be negative.");
  43. ERR_FAIL_COND_V_MSG(ttl < 0 || ttl > 255, UPNP_RESULT_INVALID_PARAM, "The time-to-live must be set between 0 and 255 (inclusive).");
  44. devices.clear();
  45. int error = 0;
  46. struct UPNPDev *devlist;
  47. CharString cs = discover_multicast_if.utf8();
  48. const char *m_if = cs.length() ? cs.get_data() : nullptr;
  49. if (is_common_device(device_filter)) {
  50. devlist = upnpDiscover(timeout, m_if, nullptr, discover_local_port, discover_ipv6, ttl, &error);
  51. } else {
  52. devlist = upnpDiscoverAll(timeout, m_if, nullptr, discover_local_port, discover_ipv6, ttl, &error);
  53. }
  54. if (error != UPNPDISCOVER_SUCCESS) {
  55. switch (error) {
  56. case UPNPDISCOVER_SOCKET_ERROR:
  57. return UPNP_RESULT_SOCKET_ERROR;
  58. case UPNPDISCOVER_MEMORY_ERROR:
  59. return UPNP_RESULT_MEM_ALLOC_ERROR;
  60. default:
  61. return UPNP_RESULT_UNKNOWN_ERROR;
  62. }
  63. }
  64. if (!devlist) {
  65. return UPNP_RESULT_NO_DEVICES;
  66. }
  67. struct UPNPDev *dev = devlist;
  68. while (dev) {
  69. if (device_filter.is_empty() || strstr(dev->st, device_filter.utf8().get_data())) {
  70. add_device_to_list(dev, devlist);
  71. }
  72. dev = dev->pNext;
  73. }
  74. freeUPNPDevlist(devlist);
  75. return UPNP_RESULT_SUCCESS;
  76. }
  77. void UPNP::add_device_to_list(UPNPDev *dev, UPNPDev *devlist) {
  78. Ref<UPNPDevice> new_device;
  79. new_device.instantiate();
  80. new_device->set_description_url(dev->descURL);
  81. new_device->set_service_type(dev->st);
  82. parse_igd(new_device, devlist);
  83. devices.push_back(new_device);
  84. }
  85. char *UPNP::load_description(const String &url, int *size, int *status_code) const {
  86. return (char *)miniwget(url.utf8().get_data(), size, 0, status_code);
  87. }
  88. void UPNP::parse_igd(Ref<UPNPDevice> dev, UPNPDev *devlist) {
  89. int size = 0;
  90. int status_code = -1;
  91. char *xml = load_description(dev->get_description_url(), &size, &status_code);
  92. if (status_code != 200) {
  93. dev->set_igd_status(UPNPDevice::IGD_STATUS_HTTP_ERROR);
  94. return;
  95. }
  96. if (!xml || size < 1) {
  97. dev->set_igd_status(UPNPDevice::IGD_STATUS_HTTP_EMPTY);
  98. return;
  99. }
  100. struct UPNPUrls urls = {};
  101. struct IGDdatas data;
  102. parserootdesc(xml, size, &data);
  103. free(xml);
  104. xml = nullptr;
  105. GetUPNPUrls(&urls, &data, dev->get_description_url().utf8().get_data(), 0);
  106. char addr[16];
  107. #if MINIUPNPC_API_VERSION >= 18
  108. int i = UPNP_GetValidIGD(devlist, &urls, &data, (char *)&addr, 16, nullptr, 0);
  109. #else
  110. int i = UPNP_GetValidIGD(devlist, &urls, &data, (char *)&addr, 16);
  111. #endif
  112. if (i != 1) {
  113. FreeUPNPUrls(&urls);
  114. switch (i) {
  115. case 0:
  116. dev->set_igd_status(UPNPDevice::IGD_STATUS_NO_IGD);
  117. return;
  118. case 2:
  119. dev->set_igd_status(UPNPDevice::IGD_STATUS_DISCONNECTED);
  120. return;
  121. case 3:
  122. dev->set_igd_status(UPNPDevice::IGD_STATUS_UNKNOWN_DEVICE);
  123. return;
  124. default:
  125. dev->set_igd_status(UPNPDevice::IGD_STATUS_UNKNOWN_ERROR);
  126. return;
  127. }
  128. }
  129. if (urls.controlURL[0] == '\0') {
  130. FreeUPNPUrls(&urls);
  131. dev->set_igd_status(UPNPDevice::IGD_STATUS_INVALID_CONTROL);
  132. return;
  133. }
  134. dev->set_igd_control_url(urls.controlURL);
  135. dev->set_igd_service_type(data.first.servicetype);
  136. dev->set_igd_our_addr(addr);
  137. dev->set_igd_status(UPNPDevice::IGD_STATUS_OK);
  138. FreeUPNPUrls(&urls);
  139. }
  140. int UPNP::upnp_result(int in) {
  141. switch (in) {
  142. case UPNPCOMMAND_SUCCESS:
  143. return UPNP_RESULT_SUCCESS;
  144. case UPNPCOMMAND_UNKNOWN_ERROR:
  145. return UPNP_RESULT_UNKNOWN_ERROR;
  146. case UPNPCOMMAND_INVALID_ARGS:
  147. return UPNP_RESULT_INVALID_ARGS;
  148. case UPNPCOMMAND_HTTP_ERROR:
  149. return UPNP_RESULT_HTTP_ERROR;
  150. case UPNPCOMMAND_INVALID_RESPONSE:
  151. return UPNP_RESULT_INVALID_RESPONSE;
  152. case UPNPCOMMAND_MEM_ALLOC_ERROR:
  153. return UPNP_RESULT_MEM_ALLOC_ERROR;
  154. case 402:
  155. return UPNP_RESULT_INVALID_ARGS;
  156. case 403:
  157. return UPNP_RESULT_NOT_AUTHORIZED;
  158. case 501:
  159. return UPNP_RESULT_ACTION_FAILED;
  160. case 606:
  161. return UPNP_RESULT_NOT_AUTHORIZED;
  162. case 714:
  163. return UPNP_RESULT_NO_SUCH_ENTRY_IN_ARRAY;
  164. case 715:
  165. return UPNP_RESULT_SRC_IP_WILDCARD_NOT_PERMITTED;
  166. case 716:
  167. return UPNP_RESULT_EXT_PORT_WILDCARD_NOT_PERMITTED;
  168. case 718:
  169. return UPNP_RESULT_CONFLICT_WITH_OTHER_MAPPING;
  170. case 724:
  171. return UPNP_RESULT_SAME_PORT_VALUES_REQUIRED;
  172. case 725:
  173. return UPNP_RESULT_ONLY_PERMANENT_LEASE_SUPPORTED;
  174. case 726:
  175. return UPNP_RESULT_REMOTE_HOST_MUST_BE_WILDCARD;
  176. case 727:
  177. return UPNP_RESULT_EXT_PORT_MUST_BE_WILDCARD;
  178. case 728:
  179. return UPNP_RESULT_NO_PORT_MAPS_AVAILABLE;
  180. case 729:
  181. return UPNP_RESULT_CONFLICT_WITH_OTHER_MECHANISM;
  182. case 732:
  183. return UPNP_RESULT_INT_PORT_WILDCARD_NOT_PERMITTED;
  184. case 733:
  185. return UPNP_RESULT_INCONSISTENT_PARAMETERS;
  186. }
  187. return UPNP_RESULT_UNKNOWN_ERROR;
  188. }
  189. int UPNP::get_device_count() const {
  190. return devices.size();
  191. }
  192. Ref<UPNPDevice> UPNP::get_device(int index) const {
  193. ERR_FAIL_INDEX_V(index, devices.size(), nullptr);
  194. return devices.get(index);
  195. }
  196. void UPNP::add_device(Ref<UPNPDevice> device) {
  197. ERR_FAIL_COND(device.is_null());
  198. devices.push_back(device);
  199. }
  200. void UPNP::set_device(int index, Ref<UPNPDevice> device) {
  201. ERR_FAIL_INDEX(index, devices.size());
  202. ERR_FAIL_COND(device.is_null());
  203. devices.set(index, device);
  204. }
  205. void UPNP::remove_device(int index) {
  206. ERR_FAIL_INDEX(index, devices.size());
  207. devices.remove_at(index);
  208. }
  209. void UPNP::clear_devices() {
  210. devices.clear();
  211. }
  212. Ref<UPNPDevice> UPNP::get_gateway() const {
  213. ERR_FAIL_COND_V_MSG(devices.is_empty(), nullptr, "Couldn't find any UPNPDevices.");
  214. for (int i = 0; i < devices.size(); i++) {
  215. Ref<UPNPDevice> dev = get_device(i);
  216. if (dev.is_valid() && dev->is_valid_gateway()) {
  217. return dev;
  218. }
  219. }
  220. return nullptr;
  221. }
  222. void UPNP::set_discover_multicast_if(const String &m_if) {
  223. discover_multicast_if = m_if;
  224. }
  225. String UPNP::get_discover_multicast_if() const {
  226. return discover_multicast_if;
  227. }
  228. void UPNP::set_discover_local_port(int port) {
  229. discover_local_port = port;
  230. }
  231. int UPNP::get_discover_local_port() const {
  232. return discover_local_port;
  233. }
  234. void UPNP::set_discover_ipv6(bool ipv6) {
  235. discover_ipv6 = ipv6;
  236. }
  237. bool UPNP::is_discover_ipv6() const {
  238. return discover_ipv6;
  239. }
  240. String UPNP::query_external_address() const {
  241. Ref<UPNPDevice> dev = get_gateway();
  242. if (dev.is_null()) {
  243. return "";
  244. }
  245. return dev->query_external_address();
  246. }
  247. int UPNP::add_port_mapping(int port, int port_internal, String desc, String proto, int duration) const {
  248. Ref<UPNPDevice> dev = get_gateway();
  249. if (dev.is_null()) {
  250. return UPNP_RESULT_NO_GATEWAY;
  251. }
  252. return dev->add_port_mapping(port, port_internal, desc, proto, duration);
  253. }
  254. int UPNP::delete_port_mapping(int port, String proto) const {
  255. Ref<UPNPDevice> dev = get_gateway();
  256. if (dev.is_null()) {
  257. return UPNP_RESULT_NO_GATEWAY;
  258. }
  259. return dev->delete_port_mapping(port, proto);
  260. }
  261. void UPNP::_bind_methods() {
  262. ClassDB::bind_method(D_METHOD("get_device_count"), &UPNP::get_device_count);
  263. ClassDB::bind_method(D_METHOD("get_device", "index"), &UPNP::get_device);
  264. ClassDB::bind_method(D_METHOD("add_device", "device"), &UPNP::add_device);
  265. ClassDB::bind_method(D_METHOD("set_device", "index", "device"), &UPNP::set_device);
  266. ClassDB::bind_method(D_METHOD("remove_device", "index"), &UPNP::remove_device);
  267. ClassDB::bind_method(D_METHOD("clear_devices"), &UPNP::clear_devices);
  268. ClassDB::bind_method(D_METHOD("get_gateway"), &UPNP::get_gateway);
  269. ClassDB::bind_method(D_METHOD("discover", "timeout", "ttl", "device_filter"), &UPNP::discover, DEFVAL(2000), DEFVAL(2), DEFVAL("InternetGatewayDevice"));
  270. ClassDB::bind_method(D_METHOD("query_external_address"), &UPNP::query_external_address);
  271. ClassDB::bind_method(D_METHOD("add_port_mapping", "port", "port_internal", "desc", "proto", "duration"), &UPNP::add_port_mapping, DEFVAL(0), DEFVAL(""), DEFVAL("UDP"), DEFVAL(0));
  272. ClassDB::bind_method(D_METHOD("delete_port_mapping", "port", "proto"), &UPNP::delete_port_mapping, DEFVAL("UDP"));
  273. ClassDB::bind_method(D_METHOD("set_discover_multicast_if", "m_if"), &UPNP::set_discover_multicast_if);
  274. ClassDB::bind_method(D_METHOD("get_discover_multicast_if"), &UPNP::get_discover_multicast_if);
  275. ADD_PROPERTY(PropertyInfo(Variant::STRING, "discover_multicast_if"), "set_discover_multicast_if", "get_discover_multicast_if");
  276. ClassDB::bind_method(D_METHOD("set_discover_local_port", "port"), &UPNP::set_discover_local_port);
  277. ClassDB::bind_method(D_METHOD("get_discover_local_port"), &UPNP::get_discover_local_port);
  278. ADD_PROPERTY(PropertyInfo(Variant::INT, "discover_local_port", PROPERTY_HINT_RANGE, "0,65535"), "set_discover_local_port", "get_discover_local_port");
  279. ClassDB::bind_method(D_METHOD("set_discover_ipv6", "ipv6"), &UPNP::set_discover_ipv6);
  280. ClassDB::bind_method(D_METHOD("is_discover_ipv6"), &UPNP::is_discover_ipv6);
  281. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "discover_ipv6"), "set_discover_ipv6", "is_discover_ipv6");
  282. BIND_ENUM_CONSTANT(UPNP_RESULT_SUCCESS);
  283. BIND_ENUM_CONSTANT(UPNP_RESULT_NOT_AUTHORIZED);
  284. BIND_ENUM_CONSTANT(UPNP_RESULT_PORT_MAPPING_NOT_FOUND);
  285. BIND_ENUM_CONSTANT(UPNP_RESULT_INCONSISTENT_PARAMETERS);
  286. BIND_ENUM_CONSTANT(UPNP_RESULT_NO_SUCH_ENTRY_IN_ARRAY);
  287. BIND_ENUM_CONSTANT(UPNP_RESULT_ACTION_FAILED);
  288. BIND_ENUM_CONSTANT(UPNP_RESULT_SRC_IP_WILDCARD_NOT_PERMITTED);
  289. BIND_ENUM_CONSTANT(UPNP_RESULT_EXT_PORT_WILDCARD_NOT_PERMITTED);
  290. BIND_ENUM_CONSTANT(UPNP_RESULT_INT_PORT_WILDCARD_NOT_PERMITTED);
  291. BIND_ENUM_CONSTANT(UPNP_RESULT_REMOTE_HOST_MUST_BE_WILDCARD);
  292. BIND_ENUM_CONSTANT(UPNP_RESULT_EXT_PORT_MUST_BE_WILDCARD);
  293. BIND_ENUM_CONSTANT(UPNP_RESULT_NO_PORT_MAPS_AVAILABLE);
  294. BIND_ENUM_CONSTANT(UPNP_RESULT_CONFLICT_WITH_OTHER_MECHANISM);
  295. BIND_ENUM_CONSTANT(UPNP_RESULT_CONFLICT_WITH_OTHER_MAPPING);
  296. BIND_ENUM_CONSTANT(UPNP_RESULT_SAME_PORT_VALUES_REQUIRED);
  297. BIND_ENUM_CONSTANT(UPNP_RESULT_ONLY_PERMANENT_LEASE_SUPPORTED);
  298. BIND_ENUM_CONSTANT(UPNP_RESULT_INVALID_GATEWAY);
  299. BIND_ENUM_CONSTANT(UPNP_RESULT_INVALID_PORT);
  300. BIND_ENUM_CONSTANT(UPNP_RESULT_INVALID_PROTOCOL);
  301. BIND_ENUM_CONSTANT(UPNP_RESULT_INVALID_DURATION);
  302. BIND_ENUM_CONSTANT(UPNP_RESULT_INVALID_ARGS);
  303. BIND_ENUM_CONSTANT(UPNP_RESULT_INVALID_RESPONSE);
  304. BIND_ENUM_CONSTANT(UPNP_RESULT_INVALID_PARAM);
  305. BIND_ENUM_CONSTANT(UPNP_RESULT_HTTP_ERROR);
  306. BIND_ENUM_CONSTANT(UPNP_RESULT_SOCKET_ERROR);
  307. BIND_ENUM_CONSTANT(UPNP_RESULT_MEM_ALLOC_ERROR);
  308. BIND_ENUM_CONSTANT(UPNP_RESULT_NO_GATEWAY);
  309. BIND_ENUM_CONSTANT(UPNP_RESULT_NO_DEVICES);
  310. BIND_ENUM_CONSTANT(UPNP_RESULT_UNKNOWN_ERROR);
  311. }
  312. UPNP::UPNP() {
  313. }
  314. UPNP::~UPNP() {
  315. }