123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598 |
- :github_url: hide
- .. DO NOT EDIT THIS FILE!!!
- .. Generated automatically from Godot engine sources.
- .. Generator: https://github.com/godotengine/godot/tree/master/doc/tools/make_rst.py.
- .. XML source: https://github.com/godotengine/godot/tree/master/modules/upnp/doc_classes/UPNP.xml.
- .. _class_UPNP:
- UPNP
- ====
- **Inherits:** :ref:`RefCounted<class_RefCounted>` **<** :ref:`Object<class_Object>`
- Universal Plug and Play (UPnP) functions for network device discovery, querying and port forwarding.
- .. rst-class:: classref-introduction-group
- Description
- -----------
- This class can be used to discover compatible :ref:`UPNPDevice<class_UPNPDevice>`\ s on the local network and execute commands on them, like managing port mappings (for port forwarding/NAT traversal) and querying the local and remote network IP address. Note that methods on this class are synchronous and block the calling thread.
- To forward a specific port (here ``7777``, note both :ref:`discover<class_UPNP_method_discover>` and :ref:`add_port_mapping<class_UPNP_method_add_port_mapping>` can return errors that should be checked):
- ::
- var upnp = UPNP.new()
- upnp.discover()
- upnp.add_port_mapping(7777)
- To close a specific port (e.g. after you have finished using it):
- ::
- upnp.delete_port_mapping(port)
- \ **Note:** UPnP discovery blocks the current thread. To perform discovery without blocking the main thread, use :ref:`Thread<class_Thread>`\ s like this:
- ::
- # Emitted when UPnP port mapping setup is completed (regardless of success or failure).
- signal upnp_completed(error)
-
- # Replace this with your own server port number between 1024 and 65535.
- const SERVER_PORT = 3928
- var thread = null
-
- func _upnp_setup(server_port):
- # UPNP queries take some time.
- var upnp = UPNP.new()
- var err = upnp.discover()
-
- if err != OK:
- push_error(str(err))
- upnp_completed.emit(err)
- return
-
- if upnp.get_gateway() and upnp.get_gateway().is_valid_gateway():
- upnp.add_port_mapping(server_port, server_port, ProjectSettings.get_setting("application/config/name"), "UDP")
- upnp.add_port_mapping(server_port, server_port, ProjectSettings.get_setting("application/config/name"), "TCP")
- upnp_completed.emit(OK)
-
- func _ready():
- thread = Thread.new()
- thread.start(_upnp_setup.bind(SERVER_PORT))
-
- func _exit_tree():
- # Wait for thread finish here to handle game exit while the thread is running.
- thread.wait_to_finish()
- \ **Terminology:** In the context of UPnP networking, "gateway" (or "internet gateway device", short IGD) refers to network devices that allow computers in the local network to access the internet ("wide area network", WAN). These gateways are often also called "routers".
- \ **Pitfalls:**\
- - As explained above, these calls are blocking and shouldn't be run on the main thread, especially as they can block for multiple seconds at a time. Use threading!
- - Networking is physical and messy. Packets get lost in transit or get filtered, addresses, free ports and assigned mappings change, and devices may leave or join the network at any time. Be mindful of this, be diligent when checking and handling errors, and handle these gracefully if you can: add clear error UI, timeouts and re-try handling.
- - Port mappings may change (and be removed) at any time, and the remote/external IP address of the gateway can change likewise. You should consider re-querying the external IP and try to update/refresh the port mapping periodically (for example, every 5 minutes and on networking failures).
- - Not all devices support UPnP, and some users disable UPnP support. You need to handle this (e.g. documenting and requiring the user to manually forward ports, or adding alternative methods of NAT traversal, like a relay/mirror server, or NAT hole punching, STUN/TURN, etc.).
- - Consider what happens on mapping conflicts. Maybe multiple users on the same network would like to play your game at the same time, or maybe another application uses the same port. Make the port configurable, and optimally choose a port automatically (re-trying with a different port on failure).
- \ **Further reading:** If you want to know more about UPnP (and the Internet Gateway Device (IGD) and Port Control Protocol (PCP) specifically), `Wikipedia <https://en.wikipedia.org/wiki/Universal_Plug_and_Play>`__ is a good first stop, the specification can be found at the `Open Connectivity Foundation <https://openconnectivity.org/developer/specifications/upnp-resources/upnp/>`__ and Godot's implementation is based on the `MiniUPnP client <https://github.com/miniupnp/miniupnp>`__.
- .. rst-class:: classref-reftable-group
- Properties
- ----------
- .. table::
- :widths: auto
- +-----------------------------+-------------------------------------------------------------------------+-----------+
- | :ref:`bool<class_bool>` | :ref:`discover_ipv6<class_UPNP_property_discover_ipv6>` | ``false`` |
- +-----------------------------+-------------------------------------------------------------------------+-----------+
- | :ref:`int<class_int>` | :ref:`discover_local_port<class_UPNP_property_discover_local_port>` | ``0`` |
- +-----------------------------+-------------------------------------------------------------------------+-----------+
- | :ref:`String<class_String>` | :ref:`discover_multicast_if<class_UPNP_property_discover_multicast_if>` | ``""`` |
- +-----------------------------+-------------------------------------------------------------------------+-----------+
- .. rst-class:: classref-reftable-group
- Methods
- -------
- .. table::
- :widths: auto
- +-------------------------------------+--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
- | |void| | :ref:`add_device<class_UPNP_method_add_device>`\ (\ device\: :ref:`UPNPDevice<class_UPNPDevice>`\ ) |
- +-------------------------------------+--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
- | :ref:`int<class_int>` | :ref:`add_port_mapping<class_UPNP_method_add_port_mapping>`\ (\ port\: :ref:`int<class_int>`, port_internal\: :ref:`int<class_int>` = 0, desc\: :ref:`String<class_String>` = "", proto\: :ref:`String<class_String>` = "UDP", duration\: :ref:`int<class_int>` = 0\ ) |const| |
- +-------------------------------------+--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
- | |void| | :ref:`clear_devices<class_UPNP_method_clear_devices>`\ (\ ) |
- +-------------------------------------+--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
- | :ref:`int<class_int>` | :ref:`delete_port_mapping<class_UPNP_method_delete_port_mapping>`\ (\ port\: :ref:`int<class_int>`, proto\: :ref:`String<class_String>` = "UDP"\ ) |const| |
- +-------------------------------------+--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
- | :ref:`int<class_int>` | :ref:`discover<class_UPNP_method_discover>`\ (\ timeout\: :ref:`int<class_int>` = 2000, ttl\: :ref:`int<class_int>` = 2, device_filter\: :ref:`String<class_String>` = "InternetGatewayDevice"\ ) |
- +-------------------------------------+--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
- | :ref:`UPNPDevice<class_UPNPDevice>` | :ref:`get_device<class_UPNP_method_get_device>`\ (\ index\: :ref:`int<class_int>`\ ) |const| |
- +-------------------------------------+--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
- | :ref:`int<class_int>` | :ref:`get_device_count<class_UPNP_method_get_device_count>`\ (\ ) |const| |
- +-------------------------------------+--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
- | :ref:`UPNPDevice<class_UPNPDevice>` | :ref:`get_gateway<class_UPNP_method_get_gateway>`\ (\ ) |const| |
- +-------------------------------------+--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
- | :ref:`String<class_String>` | :ref:`query_external_address<class_UPNP_method_query_external_address>`\ (\ ) |const| |
- +-------------------------------------+--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
- | |void| | :ref:`remove_device<class_UPNP_method_remove_device>`\ (\ index\: :ref:`int<class_int>`\ ) |
- +-------------------------------------+--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
- | |void| | :ref:`set_device<class_UPNP_method_set_device>`\ (\ index\: :ref:`int<class_int>`, device\: :ref:`UPNPDevice<class_UPNPDevice>`\ ) |
- +-------------------------------------+--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
- .. rst-class:: classref-section-separator
- ----
- .. rst-class:: classref-descriptions-group
- Enumerations
- ------------
- .. _enum_UPNP_UPNPResult:
- .. rst-class:: classref-enumeration
- enum **UPNPResult**: :ref:`🔗<enum_UPNP_UPNPResult>`
- .. _class_UPNP_constant_UPNP_RESULT_SUCCESS:
- .. rst-class:: classref-enumeration-constant
- :ref:`UPNPResult<enum_UPNP_UPNPResult>` **UPNP_RESULT_SUCCESS** = ``0``
- UPNP command or discovery was successful.
- .. _class_UPNP_constant_UPNP_RESULT_NOT_AUTHORIZED:
- .. rst-class:: classref-enumeration-constant
- :ref:`UPNPResult<enum_UPNP_UPNPResult>` **UPNP_RESULT_NOT_AUTHORIZED** = ``1``
- Not authorized to use the command on the :ref:`UPNPDevice<class_UPNPDevice>`. May be returned when the user disabled UPNP on their router.
- .. _class_UPNP_constant_UPNP_RESULT_PORT_MAPPING_NOT_FOUND:
- .. rst-class:: classref-enumeration-constant
- :ref:`UPNPResult<enum_UPNP_UPNPResult>` **UPNP_RESULT_PORT_MAPPING_NOT_FOUND** = ``2``
- No port mapping was found for the given port, protocol combination on the given :ref:`UPNPDevice<class_UPNPDevice>`.
- .. _class_UPNP_constant_UPNP_RESULT_INCONSISTENT_PARAMETERS:
- .. rst-class:: classref-enumeration-constant
- :ref:`UPNPResult<enum_UPNP_UPNPResult>` **UPNP_RESULT_INCONSISTENT_PARAMETERS** = ``3``
- Inconsistent parameters.
- .. _class_UPNP_constant_UPNP_RESULT_NO_SUCH_ENTRY_IN_ARRAY:
- .. rst-class:: classref-enumeration-constant
- :ref:`UPNPResult<enum_UPNP_UPNPResult>` **UPNP_RESULT_NO_SUCH_ENTRY_IN_ARRAY** = ``4``
- No such entry in array. May be returned if a given port, protocol combination is not found on an :ref:`UPNPDevice<class_UPNPDevice>`.
- .. _class_UPNP_constant_UPNP_RESULT_ACTION_FAILED:
- .. rst-class:: classref-enumeration-constant
- :ref:`UPNPResult<enum_UPNP_UPNPResult>` **UPNP_RESULT_ACTION_FAILED** = ``5``
- The action failed.
- .. _class_UPNP_constant_UPNP_RESULT_SRC_IP_WILDCARD_NOT_PERMITTED:
- .. rst-class:: classref-enumeration-constant
- :ref:`UPNPResult<enum_UPNP_UPNPResult>` **UPNP_RESULT_SRC_IP_WILDCARD_NOT_PERMITTED** = ``6``
- The :ref:`UPNPDevice<class_UPNPDevice>` does not allow wildcard values for the source IP address.
- .. _class_UPNP_constant_UPNP_RESULT_EXT_PORT_WILDCARD_NOT_PERMITTED:
- .. rst-class:: classref-enumeration-constant
- :ref:`UPNPResult<enum_UPNP_UPNPResult>` **UPNP_RESULT_EXT_PORT_WILDCARD_NOT_PERMITTED** = ``7``
- The :ref:`UPNPDevice<class_UPNPDevice>` does not allow wildcard values for the external port.
- .. _class_UPNP_constant_UPNP_RESULT_INT_PORT_WILDCARD_NOT_PERMITTED:
- .. rst-class:: classref-enumeration-constant
- :ref:`UPNPResult<enum_UPNP_UPNPResult>` **UPNP_RESULT_INT_PORT_WILDCARD_NOT_PERMITTED** = ``8``
- The :ref:`UPNPDevice<class_UPNPDevice>` does not allow wildcard values for the internal port.
- .. _class_UPNP_constant_UPNP_RESULT_REMOTE_HOST_MUST_BE_WILDCARD:
- .. rst-class:: classref-enumeration-constant
- :ref:`UPNPResult<enum_UPNP_UPNPResult>` **UPNP_RESULT_REMOTE_HOST_MUST_BE_WILDCARD** = ``9``
- The remote host value must be a wildcard.
- .. _class_UPNP_constant_UPNP_RESULT_EXT_PORT_MUST_BE_WILDCARD:
- .. rst-class:: classref-enumeration-constant
- :ref:`UPNPResult<enum_UPNP_UPNPResult>` **UPNP_RESULT_EXT_PORT_MUST_BE_WILDCARD** = ``10``
- The external port value must be a wildcard.
- .. _class_UPNP_constant_UPNP_RESULT_NO_PORT_MAPS_AVAILABLE:
- .. rst-class:: classref-enumeration-constant
- :ref:`UPNPResult<enum_UPNP_UPNPResult>` **UPNP_RESULT_NO_PORT_MAPS_AVAILABLE** = ``11``
- No port maps are available. May also be returned if port mapping functionality is not available.
- .. _class_UPNP_constant_UPNP_RESULT_CONFLICT_WITH_OTHER_MECHANISM:
- .. rst-class:: classref-enumeration-constant
- :ref:`UPNPResult<enum_UPNP_UPNPResult>` **UPNP_RESULT_CONFLICT_WITH_OTHER_MECHANISM** = ``12``
- Conflict with other mechanism. May be returned instead of :ref:`UPNP_RESULT_CONFLICT_WITH_OTHER_MAPPING<class_UPNP_constant_UPNP_RESULT_CONFLICT_WITH_OTHER_MAPPING>` if a port mapping conflicts with an existing one.
- .. _class_UPNP_constant_UPNP_RESULT_CONFLICT_WITH_OTHER_MAPPING:
- .. rst-class:: classref-enumeration-constant
- :ref:`UPNPResult<enum_UPNP_UPNPResult>` **UPNP_RESULT_CONFLICT_WITH_OTHER_MAPPING** = ``13``
- Conflict with an existing port mapping.
- .. _class_UPNP_constant_UPNP_RESULT_SAME_PORT_VALUES_REQUIRED:
- .. rst-class:: classref-enumeration-constant
- :ref:`UPNPResult<enum_UPNP_UPNPResult>` **UPNP_RESULT_SAME_PORT_VALUES_REQUIRED** = ``14``
- External and internal port values must be the same.
- .. _class_UPNP_constant_UPNP_RESULT_ONLY_PERMANENT_LEASE_SUPPORTED:
- .. rst-class:: classref-enumeration-constant
- :ref:`UPNPResult<enum_UPNP_UPNPResult>` **UPNP_RESULT_ONLY_PERMANENT_LEASE_SUPPORTED** = ``15``
- Only permanent leases are supported. Do not use the ``duration`` parameter when adding port mappings.
- .. _class_UPNP_constant_UPNP_RESULT_INVALID_GATEWAY:
- .. rst-class:: classref-enumeration-constant
- :ref:`UPNPResult<enum_UPNP_UPNPResult>` **UPNP_RESULT_INVALID_GATEWAY** = ``16``
- Invalid gateway.
- .. _class_UPNP_constant_UPNP_RESULT_INVALID_PORT:
- .. rst-class:: classref-enumeration-constant
- :ref:`UPNPResult<enum_UPNP_UPNPResult>` **UPNP_RESULT_INVALID_PORT** = ``17``
- Invalid port.
- .. _class_UPNP_constant_UPNP_RESULT_INVALID_PROTOCOL:
- .. rst-class:: classref-enumeration-constant
- :ref:`UPNPResult<enum_UPNP_UPNPResult>` **UPNP_RESULT_INVALID_PROTOCOL** = ``18``
- Invalid protocol.
- .. _class_UPNP_constant_UPNP_RESULT_INVALID_DURATION:
- .. rst-class:: classref-enumeration-constant
- :ref:`UPNPResult<enum_UPNP_UPNPResult>` **UPNP_RESULT_INVALID_DURATION** = ``19``
- Invalid duration.
- .. _class_UPNP_constant_UPNP_RESULT_INVALID_ARGS:
- .. rst-class:: classref-enumeration-constant
- :ref:`UPNPResult<enum_UPNP_UPNPResult>` **UPNP_RESULT_INVALID_ARGS** = ``20``
- Invalid arguments.
- .. _class_UPNP_constant_UPNP_RESULT_INVALID_RESPONSE:
- .. rst-class:: classref-enumeration-constant
- :ref:`UPNPResult<enum_UPNP_UPNPResult>` **UPNP_RESULT_INVALID_RESPONSE** = ``21``
- Invalid response.
- .. _class_UPNP_constant_UPNP_RESULT_INVALID_PARAM:
- .. rst-class:: classref-enumeration-constant
- :ref:`UPNPResult<enum_UPNP_UPNPResult>` **UPNP_RESULT_INVALID_PARAM** = ``22``
- Invalid parameter.
- .. _class_UPNP_constant_UPNP_RESULT_HTTP_ERROR:
- .. rst-class:: classref-enumeration-constant
- :ref:`UPNPResult<enum_UPNP_UPNPResult>` **UPNP_RESULT_HTTP_ERROR** = ``23``
- HTTP error.
- .. _class_UPNP_constant_UPNP_RESULT_SOCKET_ERROR:
- .. rst-class:: classref-enumeration-constant
- :ref:`UPNPResult<enum_UPNP_UPNPResult>` **UPNP_RESULT_SOCKET_ERROR** = ``24``
- Socket error.
- .. _class_UPNP_constant_UPNP_RESULT_MEM_ALLOC_ERROR:
- .. rst-class:: classref-enumeration-constant
- :ref:`UPNPResult<enum_UPNP_UPNPResult>` **UPNP_RESULT_MEM_ALLOC_ERROR** = ``25``
- Error allocating memory.
- .. _class_UPNP_constant_UPNP_RESULT_NO_GATEWAY:
- .. rst-class:: classref-enumeration-constant
- :ref:`UPNPResult<enum_UPNP_UPNPResult>` **UPNP_RESULT_NO_GATEWAY** = ``26``
- No gateway available. You may need to call :ref:`discover<class_UPNP_method_discover>` first, or discovery didn't detect any valid IGDs (InternetGatewayDevices).
- .. _class_UPNP_constant_UPNP_RESULT_NO_DEVICES:
- .. rst-class:: classref-enumeration-constant
- :ref:`UPNPResult<enum_UPNP_UPNPResult>` **UPNP_RESULT_NO_DEVICES** = ``27``
- No devices available. You may need to call :ref:`discover<class_UPNP_method_discover>` first, or discovery didn't detect any valid :ref:`UPNPDevice<class_UPNPDevice>`\ s.
- .. _class_UPNP_constant_UPNP_RESULT_UNKNOWN_ERROR:
- .. rst-class:: classref-enumeration-constant
- :ref:`UPNPResult<enum_UPNP_UPNPResult>` **UPNP_RESULT_UNKNOWN_ERROR** = ``28``
- Unknown error.
- .. rst-class:: classref-section-separator
- ----
- .. rst-class:: classref-descriptions-group
- Property Descriptions
- ---------------------
- .. _class_UPNP_property_discover_ipv6:
- .. rst-class:: classref-property
- :ref:`bool<class_bool>` **discover_ipv6** = ``false`` :ref:`🔗<class_UPNP_property_discover_ipv6>`
- .. rst-class:: classref-property-setget
- - |void| **set_discover_ipv6**\ (\ value\: :ref:`bool<class_bool>`\ )
- - :ref:`bool<class_bool>` **is_discover_ipv6**\ (\ )
- If ``true``, IPv6 is used for :ref:`UPNPDevice<class_UPNPDevice>` discovery.
- .. rst-class:: classref-item-separator
- ----
- .. _class_UPNP_property_discover_local_port:
- .. rst-class:: classref-property
- :ref:`int<class_int>` **discover_local_port** = ``0`` :ref:`🔗<class_UPNP_property_discover_local_port>`
- .. rst-class:: classref-property-setget
- - |void| **set_discover_local_port**\ (\ value\: :ref:`int<class_int>`\ )
- - :ref:`int<class_int>` **get_discover_local_port**\ (\ )
- If ``0``, the local port to use for discovery is chosen automatically by the system. If ``1``, discovery will be done from the source port 1900 (same as destination port). Otherwise, the value will be used as the port.
- .. rst-class:: classref-item-separator
- ----
- .. _class_UPNP_property_discover_multicast_if:
- .. rst-class:: classref-property
- :ref:`String<class_String>` **discover_multicast_if** = ``""`` :ref:`🔗<class_UPNP_property_discover_multicast_if>`
- .. rst-class:: classref-property-setget
- - |void| **set_discover_multicast_if**\ (\ value\: :ref:`String<class_String>`\ )
- - :ref:`String<class_String>` **get_discover_multicast_if**\ (\ )
- Multicast interface to use for discovery. Uses the default multicast interface if empty.
- .. rst-class:: classref-section-separator
- ----
- .. rst-class:: classref-descriptions-group
- Method Descriptions
- -------------------
- .. _class_UPNP_method_add_device:
- .. rst-class:: classref-method
- |void| **add_device**\ (\ device\: :ref:`UPNPDevice<class_UPNPDevice>`\ ) :ref:`🔗<class_UPNP_method_add_device>`
- Adds the given :ref:`UPNPDevice<class_UPNPDevice>` to the list of discovered devices.
- .. rst-class:: classref-item-separator
- ----
- .. _class_UPNP_method_add_port_mapping:
- .. rst-class:: classref-method
- :ref:`int<class_int>` **add_port_mapping**\ (\ port\: :ref:`int<class_int>`, port_internal\: :ref:`int<class_int>` = 0, desc\: :ref:`String<class_String>` = "", proto\: :ref:`String<class_String>` = "UDP", duration\: :ref:`int<class_int>` = 0\ ) |const| :ref:`🔗<class_UPNP_method_add_port_mapping>`
- Adds a mapping to forward the external ``port`` (between 1 and 65535, although recommended to use port 1024 or above) on the default gateway (see :ref:`get_gateway<class_UPNP_method_get_gateway>`) to the ``port_internal`` on the local machine for the given protocol ``proto`` (either ``"TCP"`` or ``"UDP"``, with UDP being the default). If a port mapping for the given port and protocol combination already exists on that gateway device, this method tries to overwrite it. If that is not desired, you can retrieve the gateway manually with :ref:`get_gateway<class_UPNP_method_get_gateway>` and call :ref:`add_port_mapping<class_UPNP_method_add_port_mapping>` on it, if any. Note that forwarding a well-known port (below 1024) with UPnP may fail depending on the device.
- Depending on the gateway device, if a mapping for that port already exists, it will either be updated or it will refuse this command due to that conflict, especially if the existing mapping for that port wasn't created via UPnP or points to a different network address (or device) than this one.
- If ``port_internal`` is ``0`` (the default), the same port number is used for both the external and the internal port (the ``port`` value).
- The description (``desc``) is shown in some routers management UIs and can be used to point out which application added the mapping.
- The mapping's lease ``duration`` can be limited by specifying a duration in seconds. The default of ``0`` means no duration, i.e. a permanent lease and notably some devices only support these permanent leases. Note that whether permanent or not, this is only a request and the gateway may still decide at any point to remove the mapping (which usually happens on a reboot of the gateway, when its external IP address changes, or on some models when it detects a port mapping has become inactive, i.e. had no traffic for multiple minutes). If not ``0`` (permanent), the allowed range according to spec is between ``120`` (2 minutes) and ``86400`` seconds (24 hours).
- See :ref:`UPNPResult<enum_UPNP_UPNPResult>` for possible return values.
- .. rst-class:: classref-item-separator
- ----
- .. _class_UPNP_method_clear_devices:
- .. rst-class:: classref-method
- |void| **clear_devices**\ (\ ) :ref:`🔗<class_UPNP_method_clear_devices>`
- Clears the list of discovered devices.
- .. rst-class:: classref-item-separator
- ----
- .. _class_UPNP_method_delete_port_mapping:
- .. rst-class:: classref-method
- :ref:`int<class_int>` **delete_port_mapping**\ (\ port\: :ref:`int<class_int>`, proto\: :ref:`String<class_String>` = "UDP"\ ) |const| :ref:`🔗<class_UPNP_method_delete_port_mapping>`
- Deletes the port mapping for the given port and protocol combination on the default gateway (see :ref:`get_gateway<class_UPNP_method_get_gateway>`) if one exists. ``port`` must be a valid port between 1 and 65535, ``proto`` can be either ``"TCP"`` or ``"UDP"``. May be refused for mappings pointing to addresses other than this one, for well-known ports (below 1024), or for mappings not added via UPnP. See :ref:`UPNPResult<enum_UPNP_UPNPResult>` for possible return values.
- .. rst-class:: classref-item-separator
- ----
- .. _class_UPNP_method_discover:
- .. rst-class:: classref-method
- :ref:`int<class_int>` **discover**\ (\ timeout\: :ref:`int<class_int>` = 2000, ttl\: :ref:`int<class_int>` = 2, device_filter\: :ref:`String<class_String>` = "InternetGatewayDevice"\ ) :ref:`🔗<class_UPNP_method_discover>`
- Discovers local :ref:`UPNPDevice<class_UPNPDevice>`\ s. Clears the list of previously discovered devices.
- Filters for IGD (InternetGatewayDevice) type devices by default, as those manage port forwarding. ``timeout`` is the time to wait for responses in milliseconds. ``ttl`` is the time-to-live; only touch this if you know what you're doing.
- See :ref:`UPNPResult<enum_UPNP_UPNPResult>` for possible return values.
- .. rst-class:: classref-item-separator
- ----
- .. _class_UPNP_method_get_device:
- .. rst-class:: classref-method
- :ref:`UPNPDevice<class_UPNPDevice>` **get_device**\ (\ index\: :ref:`int<class_int>`\ ) |const| :ref:`🔗<class_UPNP_method_get_device>`
- Returns the :ref:`UPNPDevice<class_UPNPDevice>` at the given ``index``.
- .. rst-class:: classref-item-separator
- ----
- .. _class_UPNP_method_get_device_count:
- .. rst-class:: classref-method
- :ref:`int<class_int>` **get_device_count**\ (\ ) |const| :ref:`🔗<class_UPNP_method_get_device_count>`
- Returns the number of discovered :ref:`UPNPDevice<class_UPNPDevice>`\ s.
- .. rst-class:: classref-item-separator
- ----
- .. _class_UPNP_method_get_gateway:
- .. rst-class:: classref-method
- :ref:`UPNPDevice<class_UPNPDevice>` **get_gateway**\ (\ ) |const| :ref:`🔗<class_UPNP_method_get_gateway>`
- Returns the default gateway. That is the first discovered :ref:`UPNPDevice<class_UPNPDevice>` that is also a valid IGD (InternetGatewayDevice).
- .. rst-class:: classref-item-separator
- ----
- .. _class_UPNP_method_query_external_address:
- .. rst-class:: classref-method
- :ref:`String<class_String>` **query_external_address**\ (\ ) |const| :ref:`🔗<class_UPNP_method_query_external_address>`
- Returns the external :ref:`IP<class_IP>` address of the default gateway (see :ref:`get_gateway<class_UPNP_method_get_gateway>`) as string. Returns an empty string on error.
- .. rst-class:: classref-item-separator
- ----
- .. _class_UPNP_method_remove_device:
- .. rst-class:: classref-method
- |void| **remove_device**\ (\ index\: :ref:`int<class_int>`\ ) :ref:`🔗<class_UPNP_method_remove_device>`
- Removes the device at ``index`` from the list of discovered devices.
- .. rst-class:: classref-item-separator
- ----
- .. _class_UPNP_method_set_device:
- .. rst-class:: classref-method
- |void| **set_device**\ (\ index\: :ref:`int<class_int>`, device\: :ref:`UPNPDevice<class_UPNPDevice>`\ ) :ref:`🔗<class_UPNP_method_set_device>`
- Sets the device at ``index`` from the list of discovered devices to ``device``.
- .. |virtual| replace:: :abbr:`virtual (This method should typically be overridden by the user to have any effect.)`
- .. |const| replace:: :abbr:`const (This method has no side effects. It doesn't modify any of the instance's member variables.)`
- .. |vararg| replace:: :abbr:`vararg (This method accepts any number of arguments after the ones described here.)`
- .. |constructor| replace:: :abbr:`constructor (This method is used to construct a type.)`
- .. |static| replace:: :abbr:`static (This method doesn't need an instance to be called, so it can be called directly using the class name.)`
- .. |operator| replace:: :abbr:`operator (This method describes a valid operator to use with this type as left-hand operand.)`
- .. |bitfield| replace:: :abbr:`BitField (This value is an integer composed as a bitmask of the following flags.)`
- .. |void| replace:: :abbr:`void (No return value.)`
|