upnp_device.cpp 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200
  1. /**************************************************************************/
  2. /* upnp_device.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_device.h"
  31. #include "upnp.h"
  32. #include <upnpcommands.h>
  33. String UPNPDevice::query_external_address() const {
  34. ERR_FAIL_COND_V_MSG(!is_valid_gateway(), "", "The Internet Gateway Device must be valid.");
  35. char addr[16];
  36. int i = UPNP_GetExternalIPAddress(
  37. igd_control_url.utf8().get_data(),
  38. igd_service_type.utf8().get_data(),
  39. (char *)&addr);
  40. ERR_FAIL_COND_V_MSG(i != UPNPCOMMAND_SUCCESS, "", "Couldn't get external IP address.");
  41. return String(addr);
  42. }
  43. int UPNPDevice::add_port_mapping(int port, int port_internal, String desc, String proto, int duration) const {
  44. ERR_FAIL_COND_V_MSG(!is_valid_gateway(), UPNP::UPNP_RESULT_INVALID_GATEWAY, "The Internet Gateway Device must be valid.");
  45. ERR_FAIL_COND_V_MSG(port < 1 || port > 65535, UPNP::UPNP_RESULT_INVALID_PORT, "The port number must be set between 1 and 65535 (inclusive).");
  46. ERR_FAIL_COND_V_MSG(port_internal < 0 || port_internal > 65535, UPNP::UPNP_RESULT_INVALID_PORT, "The port number must be set between 0 and 65535 (inclusive)."); // Needs to allow 0 because 0 signifies "use external port as internal port"
  47. ERR_FAIL_COND_V_MSG(proto != "UDP" && proto != "TCP", UPNP::UPNP_RESULT_INVALID_PROTOCOL, "The protocol must be either TCP or UDP.");
  48. ERR_FAIL_COND_V_MSG(duration < 0, UPNP::UPNP_RESULT_INVALID_DURATION, "The port mapping's lease duration can't be negative.");
  49. if (port_internal < 1) {
  50. port_internal = port;
  51. }
  52. int i = UPNP_AddPortMapping(
  53. igd_control_url.utf8().get_data(),
  54. igd_service_type.utf8().get_data(),
  55. itos(port).utf8().get_data(),
  56. itos(port_internal).utf8().get_data(),
  57. igd_our_addr.utf8().get_data(),
  58. desc.is_empty() ? nullptr : desc.utf8().get_data(),
  59. proto.utf8().get_data(),
  60. nullptr, // Remote host, always nullptr as IGDs don't support it
  61. duration > 0 ? itos(duration).utf8().get_data() : nullptr);
  62. ERR_FAIL_COND_V_MSG(i != UPNPCOMMAND_SUCCESS, UPNP::upnp_result(i), "Couldn't add port mapping.");
  63. return UPNP::UPNP_RESULT_SUCCESS;
  64. }
  65. int UPNPDevice::delete_port_mapping(int port, String proto) const {
  66. ERR_FAIL_COND_V_MSG(port < 1 || port > 65535, UPNP::UPNP_RESULT_INVALID_PORT, "The port number must be set between 1 and 65535 (inclusive).");
  67. ERR_FAIL_COND_V_MSG(proto != "UDP" && proto != "TCP", UPNP::UPNP_RESULT_INVALID_PROTOCOL, "The protocol must be either TCP or UDP.");
  68. int i = UPNP_DeletePortMapping(
  69. igd_control_url.utf8().get_data(),
  70. igd_service_type.utf8().get_data(),
  71. itos(port).utf8().get_data(),
  72. proto.utf8().get_data(),
  73. nullptr // Remote host, always nullptr as IGDs don't support it
  74. );
  75. ERR_FAIL_COND_V_MSG(i != UPNPCOMMAND_SUCCESS, UPNP::upnp_result(i), "Couldn't delete port mapping.");
  76. return UPNP::UPNP_RESULT_SUCCESS;
  77. }
  78. void UPNPDevice::set_description_url(const String &url) {
  79. description_url = url;
  80. }
  81. String UPNPDevice::get_description_url() const {
  82. return description_url;
  83. }
  84. void UPNPDevice::set_service_type(const String &type) {
  85. service_type = type;
  86. }
  87. String UPNPDevice::get_service_type() const {
  88. return service_type;
  89. }
  90. void UPNPDevice::set_igd_control_url(const String &url) {
  91. igd_control_url = url;
  92. }
  93. String UPNPDevice::get_igd_control_url() const {
  94. return igd_control_url;
  95. }
  96. void UPNPDevice::set_igd_service_type(const String &type) {
  97. igd_service_type = type;
  98. }
  99. String UPNPDevice::get_igd_service_type() const {
  100. return igd_service_type;
  101. }
  102. void UPNPDevice::set_igd_our_addr(const String &addr) {
  103. igd_our_addr = addr;
  104. }
  105. String UPNPDevice::get_igd_our_addr() const {
  106. return igd_our_addr;
  107. }
  108. void UPNPDevice::set_igd_status(IGDStatus status) {
  109. igd_status = status;
  110. }
  111. UPNPDevice::IGDStatus UPNPDevice::get_igd_status() const {
  112. return igd_status;
  113. }
  114. bool UPNPDevice::is_valid_gateway() const {
  115. return igd_status == IGD_STATUS_OK;
  116. }
  117. void UPNPDevice::_bind_methods() {
  118. ClassDB::bind_method(D_METHOD("is_valid_gateway"), &UPNPDevice::is_valid_gateway);
  119. ClassDB::bind_method(D_METHOD("query_external_address"), &UPNPDevice::query_external_address);
  120. ClassDB::bind_method(D_METHOD("add_port_mapping", "port", "port_internal", "desc", "proto", "duration"), &UPNPDevice::add_port_mapping, DEFVAL(0), DEFVAL(""), DEFVAL("UDP"), DEFVAL(0));
  121. ClassDB::bind_method(D_METHOD("delete_port_mapping", "port", "proto"), &UPNPDevice::delete_port_mapping, DEFVAL("UDP"));
  122. ClassDB::bind_method(D_METHOD("set_description_url", "url"), &UPNPDevice::set_description_url);
  123. ClassDB::bind_method(D_METHOD("get_description_url"), &UPNPDevice::get_description_url);
  124. ADD_PROPERTY(PropertyInfo(Variant::STRING, "description_url"), "set_description_url", "get_description_url");
  125. ClassDB::bind_method(D_METHOD("set_service_type", "type"), &UPNPDevice::set_service_type);
  126. ClassDB::bind_method(D_METHOD("get_service_type"), &UPNPDevice::get_service_type);
  127. ADD_PROPERTY(PropertyInfo(Variant::STRING, "service_type"), "set_service_type", "get_service_type");
  128. ClassDB::bind_method(D_METHOD("set_igd_control_url", "url"), &UPNPDevice::set_igd_control_url);
  129. ClassDB::bind_method(D_METHOD("get_igd_control_url"), &UPNPDevice::get_igd_control_url);
  130. ADD_PROPERTY(PropertyInfo(Variant::STRING, "igd_control_url"), "set_igd_control_url", "get_igd_control_url");
  131. ClassDB::bind_method(D_METHOD("set_igd_service_type", "type"), &UPNPDevice::set_igd_service_type);
  132. ClassDB::bind_method(D_METHOD("get_igd_service_type"), &UPNPDevice::get_igd_service_type);
  133. ADD_PROPERTY(PropertyInfo(Variant::STRING, "igd_service_type"), "set_igd_service_type", "get_igd_service_type");
  134. ClassDB::bind_method(D_METHOD("set_igd_our_addr", "addr"), &UPNPDevice::set_igd_our_addr);
  135. ClassDB::bind_method(D_METHOD("get_igd_our_addr"), &UPNPDevice::get_igd_our_addr);
  136. ADD_PROPERTY(PropertyInfo(Variant::STRING, "igd_our_addr"), "set_igd_our_addr", "get_igd_our_addr");
  137. ClassDB::bind_method(D_METHOD("set_igd_status", "status"), &UPNPDevice::set_igd_status);
  138. ClassDB::bind_method(D_METHOD("get_igd_status"), &UPNPDevice::get_igd_status);
  139. ADD_PROPERTY(PropertyInfo(Variant::INT, "igd_status", PROPERTY_HINT_ENUM), "set_igd_status", "get_igd_status");
  140. BIND_ENUM_CONSTANT(IGD_STATUS_OK);
  141. BIND_ENUM_CONSTANT(IGD_STATUS_HTTP_ERROR);
  142. BIND_ENUM_CONSTANT(IGD_STATUS_HTTP_EMPTY);
  143. BIND_ENUM_CONSTANT(IGD_STATUS_NO_URLS);
  144. BIND_ENUM_CONSTANT(IGD_STATUS_NO_IGD);
  145. BIND_ENUM_CONSTANT(IGD_STATUS_DISCONNECTED);
  146. BIND_ENUM_CONSTANT(IGD_STATUS_UNKNOWN_DEVICE);
  147. BIND_ENUM_CONSTANT(IGD_STATUS_INVALID_CONTROL);
  148. BIND_ENUM_CONSTANT(IGD_STATUS_MALLOC_ERROR);
  149. BIND_ENUM_CONSTANT(IGD_STATUS_UNKNOWN_ERROR);
  150. }
  151. UPNPDevice::UPNPDevice() {
  152. description_url = "";
  153. service_type = "";
  154. igd_control_url = "";
  155. igd_service_type = "";
  156. igd_our_addr = "";
  157. igd_status = IGD_STATUS_UNKNOWN_ERROR;
  158. }
  159. UPNPDevice::~UPNPDevice() {
  160. }