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