upnp.cpp 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387
  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. int i = UPNP_GetValidIGD(devlist, &urls, &data, (char *)&addr, 16);
  108. if (i != 1) {
  109. FreeUPNPUrls(&urls);
  110. switch (i) {
  111. case 0:
  112. dev->set_igd_status(UPNPDevice::IGD_STATUS_NO_IGD);
  113. return;
  114. case 2:
  115. dev->set_igd_status(UPNPDevice::IGD_STATUS_DISCONNECTED);
  116. return;
  117. case 3:
  118. dev->set_igd_status(UPNPDevice::IGD_STATUS_UNKNOWN_DEVICE);
  119. return;
  120. default:
  121. dev->set_igd_status(UPNPDevice::IGD_STATUS_UNKNOWN_ERROR);
  122. return;
  123. }
  124. }
  125. if (urls.controlURL[0] == '\0') {
  126. FreeUPNPUrls(&urls);
  127. dev->set_igd_status(UPNPDevice::IGD_STATUS_INVALID_CONTROL);
  128. return;
  129. }
  130. dev->set_igd_control_url(urls.controlURL);
  131. dev->set_igd_service_type(data.first.servicetype);
  132. dev->set_igd_our_addr(addr);
  133. dev->set_igd_status(UPNPDevice::IGD_STATUS_OK);
  134. FreeUPNPUrls(&urls);
  135. }
  136. int UPNP::upnp_result(int in) {
  137. switch (in) {
  138. case UPNPCOMMAND_SUCCESS:
  139. return UPNP_RESULT_SUCCESS;
  140. case UPNPCOMMAND_UNKNOWN_ERROR:
  141. return UPNP_RESULT_UNKNOWN_ERROR;
  142. case UPNPCOMMAND_INVALID_ARGS:
  143. return UPNP_RESULT_INVALID_ARGS;
  144. case UPNPCOMMAND_HTTP_ERROR:
  145. return UPNP_RESULT_HTTP_ERROR;
  146. case UPNPCOMMAND_INVALID_RESPONSE:
  147. return UPNP_RESULT_INVALID_RESPONSE;
  148. case UPNPCOMMAND_MEM_ALLOC_ERROR:
  149. return UPNP_RESULT_MEM_ALLOC_ERROR;
  150. case 402:
  151. return UPNP_RESULT_INVALID_ARGS;
  152. case 403:
  153. return UPNP_RESULT_NOT_AUTHORIZED;
  154. case 501:
  155. return UPNP_RESULT_ACTION_FAILED;
  156. case 606:
  157. return UPNP_RESULT_NOT_AUTHORIZED;
  158. case 714:
  159. return UPNP_RESULT_NO_SUCH_ENTRY_IN_ARRAY;
  160. case 715:
  161. return UPNP_RESULT_SRC_IP_WILDCARD_NOT_PERMITTED;
  162. case 716:
  163. return UPNP_RESULT_EXT_PORT_WILDCARD_NOT_PERMITTED;
  164. case 718:
  165. return UPNP_RESULT_CONFLICT_WITH_OTHER_MAPPING;
  166. case 724:
  167. return UPNP_RESULT_SAME_PORT_VALUES_REQUIRED;
  168. case 725:
  169. return UPNP_RESULT_ONLY_PERMANENT_LEASE_SUPPORTED;
  170. case 726:
  171. return UPNP_RESULT_REMOTE_HOST_MUST_BE_WILDCARD;
  172. case 727:
  173. return UPNP_RESULT_EXT_PORT_MUST_BE_WILDCARD;
  174. case 728:
  175. return UPNP_RESULT_NO_PORT_MAPS_AVAILABLE;
  176. case 729:
  177. return UPNP_RESULT_CONFLICT_WITH_OTHER_MECHANISM;
  178. case 732:
  179. return UPNP_RESULT_INT_PORT_WILDCARD_NOT_PERMITTED;
  180. case 733:
  181. return UPNP_RESULT_INCONSISTENT_PARAMETERS;
  182. }
  183. return UPNP_RESULT_UNKNOWN_ERROR;
  184. }
  185. int UPNP::get_device_count() const {
  186. return devices.size();
  187. }
  188. Ref<UPNPDevice> UPNP::get_device(int index) const {
  189. ERR_FAIL_INDEX_V(index, devices.size(), nullptr);
  190. return devices.get(index);
  191. }
  192. void UPNP::add_device(Ref<UPNPDevice> device) {
  193. ERR_FAIL_NULL(device);
  194. devices.push_back(device);
  195. }
  196. void UPNP::set_device(int index, Ref<UPNPDevice> device) {
  197. ERR_FAIL_INDEX(index, devices.size());
  198. ERR_FAIL_NULL(device);
  199. devices.set(index, device);
  200. }
  201. void UPNP::remove_device(int index) {
  202. ERR_FAIL_INDEX(index, devices.size());
  203. devices.remove_at(index);
  204. }
  205. void UPNP::clear_devices() {
  206. devices.clear();
  207. }
  208. Ref<UPNPDevice> UPNP::get_gateway() const {
  209. ERR_FAIL_COND_V_MSG(devices.is_empty(), nullptr, "Couldn't find any UPNPDevices.");
  210. for (int i = 0; i < devices.size(); i++) {
  211. Ref<UPNPDevice> dev = get_device(i);
  212. if (dev != nullptr && dev->is_valid_gateway()) {
  213. return dev;
  214. }
  215. }
  216. return nullptr;
  217. }
  218. void UPNP::set_discover_multicast_if(const String &m_if) {
  219. discover_multicast_if = m_if;
  220. }
  221. String UPNP::get_discover_multicast_if() const {
  222. return discover_multicast_if;
  223. }
  224. void UPNP::set_discover_local_port(int port) {
  225. discover_local_port = port;
  226. }
  227. int UPNP::get_discover_local_port() const {
  228. return discover_local_port;
  229. }
  230. void UPNP::set_discover_ipv6(bool ipv6) {
  231. discover_ipv6 = ipv6;
  232. }
  233. bool UPNP::is_discover_ipv6() const {
  234. return discover_ipv6;
  235. }
  236. String UPNP::query_external_address() const {
  237. Ref<UPNPDevice> dev = get_gateway();
  238. if (dev == nullptr) {
  239. return "";
  240. }
  241. return dev->query_external_address();
  242. }
  243. int UPNP::add_port_mapping(int port, int port_internal, String desc, String proto, int duration) const {
  244. Ref<UPNPDevice> dev = get_gateway();
  245. if (dev == nullptr) {
  246. return UPNP_RESULT_NO_GATEWAY;
  247. }
  248. return dev->add_port_mapping(port, port_internal, desc, proto, duration);
  249. }
  250. int UPNP::delete_port_mapping(int port, String proto) const {
  251. Ref<UPNPDevice> dev = get_gateway();
  252. if (dev == nullptr) {
  253. return UPNP_RESULT_NO_GATEWAY;
  254. }
  255. return dev->delete_port_mapping(port, proto);
  256. }
  257. void UPNP::_bind_methods() {
  258. ClassDB::bind_method(D_METHOD("get_device_count"), &UPNP::get_device_count);
  259. ClassDB::bind_method(D_METHOD("get_device", "index"), &UPNP::get_device);
  260. ClassDB::bind_method(D_METHOD("add_device", "device"), &UPNP::add_device);
  261. ClassDB::bind_method(D_METHOD("set_device", "index", "device"), &UPNP::set_device);
  262. ClassDB::bind_method(D_METHOD("remove_device", "index"), &UPNP::remove_device);
  263. ClassDB::bind_method(D_METHOD("clear_devices"), &UPNP::clear_devices);
  264. ClassDB::bind_method(D_METHOD("get_gateway"), &UPNP::get_gateway);
  265. ClassDB::bind_method(D_METHOD("discover", "timeout", "ttl", "device_filter"), &UPNP::discover, DEFVAL(2000), DEFVAL(2), DEFVAL("InternetGatewayDevice"));
  266. ClassDB::bind_method(D_METHOD("query_external_address"), &UPNP::query_external_address);
  267. 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));
  268. ClassDB::bind_method(D_METHOD("delete_port_mapping", "port", "proto"), &UPNP::delete_port_mapping, DEFVAL("UDP"));
  269. ClassDB::bind_method(D_METHOD("set_discover_multicast_if", "m_if"), &UPNP::set_discover_multicast_if);
  270. ClassDB::bind_method(D_METHOD("get_discover_multicast_if"), &UPNP::get_discover_multicast_if);
  271. ADD_PROPERTY(PropertyInfo(Variant::STRING, "discover_multicast_if"), "set_discover_multicast_if", "get_discover_multicast_if");
  272. ClassDB::bind_method(D_METHOD("set_discover_local_port", "port"), &UPNP::set_discover_local_port);
  273. ClassDB::bind_method(D_METHOD("get_discover_local_port"), &UPNP::get_discover_local_port);
  274. ADD_PROPERTY(PropertyInfo(Variant::INT, "discover_local_port", PROPERTY_HINT_RANGE, "0,65535"), "set_discover_local_port", "get_discover_local_port");
  275. ClassDB::bind_method(D_METHOD("set_discover_ipv6", "ipv6"), &UPNP::set_discover_ipv6);
  276. ClassDB::bind_method(D_METHOD("is_discover_ipv6"), &UPNP::is_discover_ipv6);
  277. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "discover_ipv6"), "set_discover_ipv6", "is_discover_ipv6");
  278. BIND_ENUM_CONSTANT(UPNP_RESULT_SUCCESS);
  279. BIND_ENUM_CONSTANT(UPNP_RESULT_NOT_AUTHORIZED);
  280. BIND_ENUM_CONSTANT(UPNP_RESULT_PORT_MAPPING_NOT_FOUND);
  281. BIND_ENUM_CONSTANT(UPNP_RESULT_INCONSISTENT_PARAMETERS);
  282. BIND_ENUM_CONSTANT(UPNP_RESULT_NO_SUCH_ENTRY_IN_ARRAY);
  283. BIND_ENUM_CONSTANT(UPNP_RESULT_ACTION_FAILED);
  284. BIND_ENUM_CONSTANT(UPNP_RESULT_SRC_IP_WILDCARD_NOT_PERMITTED);
  285. BIND_ENUM_CONSTANT(UPNP_RESULT_EXT_PORT_WILDCARD_NOT_PERMITTED);
  286. BIND_ENUM_CONSTANT(UPNP_RESULT_INT_PORT_WILDCARD_NOT_PERMITTED);
  287. BIND_ENUM_CONSTANT(UPNP_RESULT_REMOTE_HOST_MUST_BE_WILDCARD);
  288. BIND_ENUM_CONSTANT(UPNP_RESULT_EXT_PORT_MUST_BE_WILDCARD);
  289. BIND_ENUM_CONSTANT(UPNP_RESULT_NO_PORT_MAPS_AVAILABLE);
  290. BIND_ENUM_CONSTANT(UPNP_RESULT_CONFLICT_WITH_OTHER_MECHANISM);
  291. BIND_ENUM_CONSTANT(UPNP_RESULT_CONFLICT_WITH_OTHER_MAPPING);
  292. BIND_ENUM_CONSTANT(UPNP_RESULT_SAME_PORT_VALUES_REQUIRED);
  293. BIND_ENUM_CONSTANT(UPNP_RESULT_ONLY_PERMANENT_LEASE_SUPPORTED);
  294. BIND_ENUM_CONSTANT(UPNP_RESULT_INVALID_GATEWAY);
  295. BIND_ENUM_CONSTANT(UPNP_RESULT_INVALID_PORT);
  296. BIND_ENUM_CONSTANT(UPNP_RESULT_INVALID_PROTOCOL);
  297. BIND_ENUM_CONSTANT(UPNP_RESULT_INVALID_DURATION);
  298. BIND_ENUM_CONSTANT(UPNP_RESULT_INVALID_ARGS);
  299. BIND_ENUM_CONSTANT(UPNP_RESULT_INVALID_RESPONSE);
  300. BIND_ENUM_CONSTANT(UPNP_RESULT_INVALID_PARAM);
  301. BIND_ENUM_CONSTANT(UPNP_RESULT_HTTP_ERROR);
  302. BIND_ENUM_CONSTANT(UPNP_RESULT_SOCKET_ERROR);
  303. BIND_ENUM_CONSTANT(UPNP_RESULT_MEM_ALLOC_ERROR);
  304. BIND_ENUM_CONSTANT(UPNP_RESULT_NO_GATEWAY);
  305. BIND_ENUM_CONSTANT(UPNP_RESULT_NO_DEVICES);
  306. BIND_ENUM_CONSTANT(UPNP_RESULT_UNKNOWN_ERROR);
  307. }
  308. UPNP::UPNP() {
  309. }
  310. UPNP::~UPNP() {
  311. }