upnp_device_miniupnp.cpp 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  1. /**************************************************************************/
  2. /* upnp_device_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_device_miniupnp.h"
  32. #include "upnp_miniupnp.h"
  33. #include <miniupnpc/upnpcommands.h>
  34. void UPNPDeviceMiniUPNP::make_default() {
  35. UPNPDevice::_create = UPNPDeviceMiniUPNP::_create;
  36. }
  37. String UPNPDeviceMiniUPNP::query_external_address() const {
  38. ERR_FAIL_COND_V_MSG(!is_valid_gateway(), "", "The Internet Gateway Device must be valid.");
  39. char addr[16];
  40. int i = UPNP_GetExternalIPAddress(
  41. igd_control_url.utf8().get_data(),
  42. igd_service_type.utf8().get_data(),
  43. (char *)&addr);
  44. ERR_FAIL_COND_V_MSG(i != UPNPCOMMAND_SUCCESS, "", "Couldn't get external IP address.");
  45. return String(addr);
  46. }
  47. int UPNPDeviceMiniUPNP::add_port_mapping(int port, int port_internal, String desc, String proto, int duration) const {
  48. ERR_FAIL_COND_V_MSG(!is_valid_gateway(), UPNP::UPNP_RESULT_INVALID_GATEWAY, "The Internet Gateway Device must be valid.");
  49. 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).");
  50. 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"
  51. ERR_FAIL_COND_V_MSG(proto != "UDP" && proto != "TCP", UPNP::UPNP_RESULT_INVALID_PROTOCOL, "The protocol must be either TCP or UDP.");
  52. ERR_FAIL_COND_V_MSG(duration < 0, UPNP::UPNP_RESULT_INVALID_DURATION, "The port mapping's lease duration can't be negative.");
  53. if (port_internal < 1) {
  54. port_internal = port;
  55. }
  56. int i = UPNP_AddPortMapping(
  57. igd_control_url.utf8().get_data(),
  58. igd_service_type.utf8().get_data(),
  59. itos(port).utf8().get_data(),
  60. itos(port_internal).utf8().get_data(),
  61. igd_our_addr.utf8().get_data(),
  62. desc.is_empty() ? nullptr : desc.utf8().get_data(),
  63. proto.utf8().get_data(),
  64. nullptr, // Remote host, always nullptr as IGDs don't support it
  65. duration > 0 ? itos(duration).utf8().get_data() : nullptr);
  66. ERR_FAIL_COND_V_MSG(i != UPNPCOMMAND_SUCCESS, UPNPMiniUPNP::upnp_result(i), "Couldn't add port mapping.");
  67. return UPNP::UPNP_RESULT_SUCCESS;
  68. }
  69. int UPNPDeviceMiniUPNP::delete_port_mapping(int port, String proto) const {
  70. 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).");
  71. ERR_FAIL_COND_V_MSG(proto != "UDP" && proto != "TCP", UPNP::UPNP_RESULT_INVALID_PROTOCOL, "The protocol must be either TCP or UDP.");
  72. int i = UPNP_DeletePortMapping(
  73. igd_control_url.utf8().get_data(),
  74. igd_service_type.utf8().get_data(),
  75. itos(port).utf8().get_data(),
  76. proto.utf8().get_data(),
  77. nullptr // Remote host, always nullptr as IGDs don't support it
  78. );
  79. ERR_FAIL_COND_V_MSG(i != UPNPCOMMAND_SUCCESS, UPNPMiniUPNP::upnp_result(i), "Couldn't delete port mapping.");
  80. return UPNP::UPNP_RESULT_SUCCESS;
  81. }
  82. void UPNPDeviceMiniUPNP::set_description_url(const String &url) {
  83. description_url = url;
  84. }
  85. String UPNPDeviceMiniUPNP::get_description_url() const {
  86. return description_url;
  87. }
  88. void UPNPDeviceMiniUPNP::set_service_type(const String &type) {
  89. service_type = type;
  90. }
  91. String UPNPDeviceMiniUPNP::get_service_type() const {
  92. return service_type;
  93. }
  94. void UPNPDeviceMiniUPNP::set_igd_control_url(const String &url) {
  95. igd_control_url = url;
  96. }
  97. String UPNPDeviceMiniUPNP::get_igd_control_url() const {
  98. return igd_control_url;
  99. }
  100. void UPNPDeviceMiniUPNP::set_igd_service_type(const String &type) {
  101. igd_service_type = type;
  102. }
  103. String UPNPDeviceMiniUPNP::get_igd_service_type() const {
  104. return igd_service_type;
  105. }
  106. void UPNPDeviceMiniUPNP::set_igd_our_addr(const String &addr) {
  107. igd_our_addr = addr;
  108. }
  109. String UPNPDeviceMiniUPNP::get_igd_our_addr() const {
  110. return igd_our_addr;
  111. }
  112. void UPNPDeviceMiniUPNP::set_igd_status(IGDStatus status) {
  113. igd_status = status;
  114. }
  115. UPNPDeviceMiniUPNP::IGDStatus UPNPDeviceMiniUPNP::get_igd_status() const {
  116. return igd_status;
  117. }
  118. bool UPNPDeviceMiniUPNP::is_valid_gateway() const {
  119. return igd_status == IGD_STATUS_OK;
  120. }
  121. #endif // WEB_ENABLED