upnp_miniupnp.cpp 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334
  1. /**************************************************************************/
  2. /* upnp_miniupnp.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. #ifndef WEB_ENABLED
  31. #include "upnp_miniupnp.h"
  32. #include "upnp_device_miniupnp.h"
  33. #include <miniupnpc/miniwget.h>
  34. #include <miniupnpc/upnpcommands.h>
  35. #include <cstdlib>
  36. void UPNPMiniUPNP::make_default() {
  37. UPNP::_create = UPNPMiniUPNP::_create;
  38. }
  39. bool UPNPMiniUPNP::is_common_device(const String &dev) const {
  40. return dev.is_empty() ||
  41. dev.contains("InternetGatewayDevice") ||
  42. dev.contains("WANIPConnection") ||
  43. dev.contains("WANPPPConnection") ||
  44. dev.contains("rootdevice");
  45. }
  46. int UPNPMiniUPNP::discover(int timeout, int ttl, const String &device_filter) {
  47. ERR_FAIL_COND_V_MSG(timeout < 0, UPNP_RESULT_INVALID_PARAM, "The response's wait time can't be negative.");
  48. 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).");
  49. devices.clear();
  50. int error = 0;
  51. struct UPNPDev *devlist;
  52. CharString cs = discover_multicast_if.utf8();
  53. const char *m_if = cs.length() ? cs.get_data() : nullptr;
  54. if (is_common_device(device_filter)) {
  55. devlist = upnpDiscover(timeout, m_if, nullptr, discover_local_port, discover_ipv6, ttl, &error);
  56. } else {
  57. devlist = upnpDiscoverAll(timeout, m_if, nullptr, discover_local_port, discover_ipv6, ttl, &error);
  58. }
  59. if (error != UPNPDISCOVER_SUCCESS) {
  60. switch (error) {
  61. case UPNPDISCOVER_SOCKET_ERROR:
  62. return UPNP_RESULT_SOCKET_ERROR;
  63. case UPNPDISCOVER_MEMORY_ERROR:
  64. return UPNP_RESULT_MEM_ALLOC_ERROR;
  65. default:
  66. return UPNP_RESULT_UNKNOWN_ERROR;
  67. }
  68. }
  69. if (!devlist) {
  70. return UPNP_RESULT_NO_DEVICES;
  71. }
  72. struct UPNPDev *dev = devlist;
  73. while (dev) {
  74. if (device_filter.is_empty() || strstr(dev->st, device_filter.utf8().get_data())) {
  75. add_device_to_list(dev, devlist);
  76. }
  77. dev = dev->pNext;
  78. }
  79. freeUPNPDevlist(devlist);
  80. return UPNP_RESULT_SUCCESS;
  81. }
  82. void UPNPMiniUPNP::add_device_to_list(UPNPDev *dev, UPNPDev *devlist) {
  83. Ref<UPNPDeviceMiniUPNP> new_device;
  84. new_device.instantiate();
  85. new_device->set_description_url(dev->descURL);
  86. new_device->set_service_type(dev->st);
  87. parse_igd(new_device, devlist);
  88. devices.push_back(new_device);
  89. }
  90. char *UPNPMiniUPNP::load_description(const String &url, int *size, int *status_code) const {
  91. return (char *)miniwget(url.utf8().get_data(), size, 0, status_code);
  92. }
  93. void UPNPMiniUPNP::parse_igd(Ref<UPNPDevice> dev, UPNPDev *devlist) {
  94. int size = 0;
  95. int status_code = -1;
  96. char *xml = load_description(dev->get_description_url(), &size, &status_code);
  97. if (status_code != 200) {
  98. dev->set_igd_status(UPNPDevice::IGD_STATUS_HTTP_ERROR);
  99. return;
  100. }
  101. if (!xml || size < 1) {
  102. dev->set_igd_status(UPNPDevice::IGD_STATUS_HTTP_EMPTY);
  103. return;
  104. }
  105. struct UPNPUrls urls = {};
  106. struct IGDdatas data;
  107. parserootdesc(xml, size, &data);
  108. free(xml);
  109. xml = nullptr;
  110. GetUPNPUrls(&urls, &data, dev->get_description_url().utf8().get_data(), 0);
  111. char addr[16];
  112. #if MINIUPNPC_API_VERSION >= 18
  113. int i = UPNP_GetValidIGD(devlist, &urls, &data, (char *)&addr, 16, nullptr, 0);
  114. #else
  115. int i = UPNP_GetValidIGD(devlist, &urls, &data, (char *)&addr, 16);
  116. #endif
  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 UPNPMiniUPNP::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 UPNPMiniUPNP::get_device_count() const {
  195. return devices.size();
  196. }
  197. Ref<UPNPDevice> UPNPMiniUPNP::get_device(int index) const {
  198. ERR_FAIL_INDEX_V(index, devices.size(), nullptr);
  199. return devices.get(index);
  200. }
  201. void UPNPMiniUPNP::add_device(Ref<UPNPDevice> device) {
  202. ERR_FAIL_COND(device.is_null());
  203. devices.push_back(device);
  204. }
  205. void UPNPMiniUPNP::set_device(int index, Ref<UPNPDevice> device) {
  206. ERR_FAIL_INDEX(index, devices.size());
  207. ERR_FAIL_COND(device.is_null());
  208. devices.set(index, device);
  209. }
  210. void UPNPMiniUPNP::remove_device(int index) {
  211. ERR_FAIL_INDEX(index, devices.size());
  212. devices.remove_at(index);
  213. }
  214. void UPNPMiniUPNP::clear_devices() {
  215. devices.clear();
  216. }
  217. Ref<UPNPDevice> UPNPMiniUPNP::get_gateway() const {
  218. ERR_FAIL_COND_V_MSG(devices.is_empty(), 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.is_valid() && dev->is_valid_gateway()) {
  222. return dev;
  223. }
  224. }
  225. return nullptr;
  226. }
  227. void UPNPMiniUPNP::set_discover_multicast_if(const String &m_if) {
  228. discover_multicast_if = m_if;
  229. }
  230. String UPNPMiniUPNP::get_discover_multicast_if() const {
  231. return discover_multicast_if;
  232. }
  233. void UPNPMiniUPNP::set_discover_local_port(int port) {
  234. discover_local_port = port;
  235. }
  236. int UPNPMiniUPNP::get_discover_local_port() const {
  237. return discover_local_port;
  238. }
  239. void UPNPMiniUPNP::set_discover_ipv6(bool ipv6) {
  240. discover_ipv6 = ipv6;
  241. }
  242. bool UPNPMiniUPNP::is_discover_ipv6() const {
  243. return discover_ipv6;
  244. }
  245. String UPNPMiniUPNP::query_external_address() const {
  246. Ref<UPNPDevice> dev = get_gateway();
  247. if (dev.is_null()) {
  248. return "";
  249. }
  250. return dev->query_external_address();
  251. }
  252. int UPNPMiniUPNP::add_port_mapping(int port, int port_internal, String desc, String proto, int duration) const {
  253. Ref<UPNPDevice> dev = get_gateway();
  254. if (dev.is_null()) {
  255. return UPNP_RESULT_NO_GATEWAY;
  256. }
  257. return dev->add_port_mapping(port, port_internal, desc, proto, duration);
  258. }
  259. int UPNPMiniUPNP::delete_port_mapping(int port, String proto) const {
  260. Ref<UPNPDevice> dev = get_gateway();
  261. if (dev.is_null()) {
  262. return UPNP_RESULT_NO_GATEWAY;
  263. }
  264. return dev->delete_port_mapping(port, proto);
  265. }
  266. #endif // WEB_ENABLED