upnp.cpp 13 KB

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