upnp.cpp 13 KB

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