upnp.h 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. /**************************************************************************/
  2. /* upnp.h */
  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 UPNP_H
  31. #define UPNP_H
  32. #include "upnp_device.h"
  33. #include "core/object/ref_counted.h"
  34. #include <miniupnpc.h>
  35. class UPNP : public RefCounted {
  36. GDCLASS(UPNP, RefCounted);
  37. private:
  38. String discover_multicast_if = "";
  39. int discover_local_port = 0;
  40. bool discover_ipv6 = false;
  41. Vector<Ref<UPNPDevice>> devices;
  42. bool is_common_device(const String &dev) const;
  43. void add_device_to_list(UPNPDev *dev, UPNPDev *devlist);
  44. void parse_igd(Ref<UPNPDevice> dev, UPNPDev *devlist);
  45. char *load_description(const String &url, int *size, int *status_code) const;
  46. protected:
  47. static void _bind_methods();
  48. public:
  49. enum UPNPResult {
  50. UPNP_RESULT_SUCCESS,
  51. UPNP_RESULT_NOT_AUTHORIZED,
  52. UPNP_RESULT_PORT_MAPPING_NOT_FOUND,
  53. UPNP_RESULT_INCONSISTENT_PARAMETERS,
  54. UPNP_RESULT_NO_SUCH_ENTRY_IN_ARRAY,
  55. UPNP_RESULT_ACTION_FAILED,
  56. UPNP_RESULT_SRC_IP_WILDCARD_NOT_PERMITTED,
  57. UPNP_RESULT_EXT_PORT_WILDCARD_NOT_PERMITTED,
  58. UPNP_RESULT_INT_PORT_WILDCARD_NOT_PERMITTED,
  59. UPNP_RESULT_REMOTE_HOST_MUST_BE_WILDCARD,
  60. UPNP_RESULT_EXT_PORT_MUST_BE_WILDCARD,
  61. UPNP_RESULT_NO_PORT_MAPS_AVAILABLE,
  62. UPNP_RESULT_CONFLICT_WITH_OTHER_MECHANISM,
  63. UPNP_RESULT_CONFLICT_WITH_OTHER_MAPPING,
  64. UPNP_RESULT_SAME_PORT_VALUES_REQUIRED,
  65. UPNP_RESULT_ONLY_PERMANENT_LEASE_SUPPORTED,
  66. UPNP_RESULT_INVALID_GATEWAY,
  67. UPNP_RESULT_INVALID_PORT,
  68. UPNP_RESULT_INVALID_PROTOCOL,
  69. UPNP_RESULT_INVALID_DURATION,
  70. UPNP_RESULT_INVALID_ARGS,
  71. UPNP_RESULT_INVALID_RESPONSE,
  72. UPNP_RESULT_INVALID_PARAM,
  73. UPNP_RESULT_HTTP_ERROR,
  74. UPNP_RESULT_SOCKET_ERROR,
  75. UPNP_RESULT_MEM_ALLOC_ERROR,
  76. UPNP_RESULT_NO_GATEWAY,
  77. UPNP_RESULT_NO_DEVICES,
  78. UPNP_RESULT_UNKNOWN_ERROR,
  79. };
  80. static int upnp_result(int in);
  81. int get_device_count() const;
  82. Ref<UPNPDevice> get_device(int index) const;
  83. void add_device(Ref<UPNPDevice> device);
  84. void set_device(int index, Ref<UPNPDevice> device);
  85. void remove_device(int index);
  86. void clear_devices();
  87. Ref<UPNPDevice> get_gateway() const;
  88. int discover(int timeout = 2000, int ttl = 2, const String &device_filter = "InternetGatewayDevice");
  89. String query_external_address() const;
  90. int add_port_mapping(int port, int port_internal = 0, String desc = "", String proto = "UDP", int duration = 0) const;
  91. int delete_port_mapping(int port, String proto = "UDP") const;
  92. void set_discover_multicast_if(const String &m_if);
  93. String get_discover_multicast_if() const;
  94. void set_discover_local_port(int port);
  95. int get_discover_local_port() const;
  96. void set_discover_ipv6(bool ipv6);
  97. bool is_discover_ipv6() const;
  98. UPNP();
  99. ~UPNP();
  100. };
  101. VARIANT_ENUM_CAST(UPNP::UPNPResult)
  102. #endif // UPNP_H