class_upnp.rst 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598
  1. :github_url: hide
  2. .. DO NOT EDIT THIS FILE!!!
  3. .. Generated automatically from Godot engine sources.
  4. .. Generator: https://github.com/godotengine/godot/tree/master/doc/tools/make_rst.py.
  5. .. XML source: https://github.com/godotengine/godot/tree/master/modules/upnp/doc_classes/UPNP.xml.
  6. .. _class_UPNP:
  7. UPNP
  8. ====
  9. **Inherits:** :ref:`RefCounted<class_RefCounted>` **<** :ref:`Object<class_Object>`
  10. Universal Plug and Play (UPnP) functions for network device discovery, querying and port forwarding.
  11. .. rst-class:: classref-introduction-group
  12. Description
  13. -----------
  14. 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.
  15. 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):
  16. ::
  17. var upnp = UPNP.new()
  18. upnp.discover()
  19. upnp.add_port_mapping(7777)
  20. To close a specific port (e.g. after you have finished using it):
  21. ::
  22. upnp.delete_port_mapping(port)
  23. \ **Note:** UPnP discovery blocks the current thread. To perform discovery without blocking the main thread, use :ref:`Thread<class_Thread>`\ s like this:
  24. ::
  25. # Emitted when UPnP port mapping setup is completed (regardless of success or failure).
  26. signal upnp_completed(error)
  27. # Replace this with your own server port number between 1024 and 65535.
  28. const SERVER_PORT = 3928
  29. var thread = null
  30. func _upnp_setup(server_port):
  31. # UPNP queries take some time.
  32. var upnp = UPNP.new()
  33. var err = upnp.discover()
  34. if err != OK:
  35. push_error(str(err))
  36. upnp_completed.emit(err)
  37. return
  38. if upnp.get_gateway() and upnp.get_gateway().is_valid_gateway():
  39. upnp.add_port_mapping(server_port, server_port, ProjectSettings.get_setting("application/config/name"), "UDP")
  40. upnp.add_port_mapping(server_port, server_port, ProjectSettings.get_setting("application/config/name"), "TCP")
  41. upnp_completed.emit(OK)
  42. func _ready():
  43. thread = Thread.new()
  44. thread.start(_upnp_setup.bind(SERVER_PORT))
  45. func _exit_tree():
  46. # Wait for thread finish here to handle game exit while the thread is running.
  47. thread.wait_to_finish()
  48. \ **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".
  49. \ **Pitfalls:**\
  50. - 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!
  51. - 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.
  52. - 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).
  53. - 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.).
  54. - 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).
  55. \ **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>`__.
  56. .. rst-class:: classref-reftable-group
  57. Properties
  58. ----------
  59. .. table::
  60. :widths: auto
  61. +-----------------------------+-------------------------------------------------------------------------+-----------+
  62. | :ref:`bool<class_bool>` | :ref:`discover_ipv6<class_UPNP_property_discover_ipv6>` | ``false`` |
  63. +-----------------------------+-------------------------------------------------------------------------+-----------+
  64. | :ref:`int<class_int>` | :ref:`discover_local_port<class_UPNP_property_discover_local_port>` | ``0`` |
  65. +-----------------------------+-------------------------------------------------------------------------+-----------+
  66. | :ref:`String<class_String>` | :ref:`discover_multicast_if<class_UPNP_property_discover_multicast_if>` | ``""`` |
  67. +-----------------------------+-------------------------------------------------------------------------+-----------+
  68. .. rst-class:: classref-reftable-group
  69. Methods
  70. -------
  71. .. table::
  72. :widths: auto
  73. +-------------------------------------+--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
  74. | |void| | :ref:`add_device<class_UPNP_method_add_device>`\ (\ device\: :ref:`UPNPDevice<class_UPNPDevice>`\ ) |
  75. +-------------------------------------+--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
  76. | :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| |
  77. +-------------------------------------+--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
  78. | |void| | :ref:`clear_devices<class_UPNP_method_clear_devices>`\ (\ ) |
  79. +-------------------------------------+--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
  80. | :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| |
  81. +-------------------------------------+--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
  82. | :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"\ ) |
  83. +-------------------------------------+--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
  84. | :ref:`UPNPDevice<class_UPNPDevice>` | :ref:`get_device<class_UPNP_method_get_device>`\ (\ index\: :ref:`int<class_int>`\ ) |const| |
  85. +-------------------------------------+--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
  86. | :ref:`int<class_int>` | :ref:`get_device_count<class_UPNP_method_get_device_count>`\ (\ ) |const| |
  87. +-------------------------------------+--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
  88. | :ref:`UPNPDevice<class_UPNPDevice>` | :ref:`get_gateway<class_UPNP_method_get_gateway>`\ (\ ) |const| |
  89. +-------------------------------------+--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
  90. | :ref:`String<class_String>` | :ref:`query_external_address<class_UPNP_method_query_external_address>`\ (\ ) |const| |
  91. +-------------------------------------+--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
  92. | |void| | :ref:`remove_device<class_UPNP_method_remove_device>`\ (\ index\: :ref:`int<class_int>`\ ) |
  93. +-------------------------------------+--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
  94. | |void| | :ref:`set_device<class_UPNP_method_set_device>`\ (\ index\: :ref:`int<class_int>`, device\: :ref:`UPNPDevice<class_UPNPDevice>`\ ) |
  95. +-------------------------------------+--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
  96. .. rst-class:: classref-section-separator
  97. ----
  98. .. rst-class:: classref-descriptions-group
  99. Enumerations
  100. ------------
  101. .. _enum_UPNP_UPNPResult:
  102. .. rst-class:: classref-enumeration
  103. enum **UPNPResult**: :ref:`🔗<enum_UPNP_UPNPResult>`
  104. .. _class_UPNP_constant_UPNP_RESULT_SUCCESS:
  105. .. rst-class:: classref-enumeration-constant
  106. :ref:`UPNPResult<enum_UPNP_UPNPResult>` **UPNP_RESULT_SUCCESS** = ``0``
  107. UPNP command or discovery was successful.
  108. .. _class_UPNP_constant_UPNP_RESULT_NOT_AUTHORIZED:
  109. .. rst-class:: classref-enumeration-constant
  110. :ref:`UPNPResult<enum_UPNP_UPNPResult>` **UPNP_RESULT_NOT_AUTHORIZED** = ``1``
  111. Not authorized to use the command on the :ref:`UPNPDevice<class_UPNPDevice>`. May be returned when the user disabled UPNP on their router.
  112. .. _class_UPNP_constant_UPNP_RESULT_PORT_MAPPING_NOT_FOUND:
  113. .. rst-class:: classref-enumeration-constant
  114. :ref:`UPNPResult<enum_UPNP_UPNPResult>` **UPNP_RESULT_PORT_MAPPING_NOT_FOUND** = ``2``
  115. No port mapping was found for the given port, protocol combination on the given :ref:`UPNPDevice<class_UPNPDevice>`.
  116. .. _class_UPNP_constant_UPNP_RESULT_INCONSISTENT_PARAMETERS:
  117. .. rst-class:: classref-enumeration-constant
  118. :ref:`UPNPResult<enum_UPNP_UPNPResult>` **UPNP_RESULT_INCONSISTENT_PARAMETERS** = ``3``
  119. Inconsistent parameters.
  120. .. _class_UPNP_constant_UPNP_RESULT_NO_SUCH_ENTRY_IN_ARRAY:
  121. .. rst-class:: classref-enumeration-constant
  122. :ref:`UPNPResult<enum_UPNP_UPNPResult>` **UPNP_RESULT_NO_SUCH_ENTRY_IN_ARRAY** = ``4``
  123. No such entry in array. May be returned if a given port, protocol combination is not found on an :ref:`UPNPDevice<class_UPNPDevice>`.
  124. .. _class_UPNP_constant_UPNP_RESULT_ACTION_FAILED:
  125. .. rst-class:: classref-enumeration-constant
  126. :ref:`UPNPResult<enum_UPNP_UPNPResult>` **UPNP_RESULT_ACTION_FAILED** = ``5``
  127. The action failed.
  128. .. _class_UPNP_constant_UPNP_RESULT_SRC_IP_WILDCARD_NOT_PERMITTED:
  129. .. rst-class:: classref-enumeration-constant
  130. :ref:`UPNPResult<enum_UPNP_UPNPResult>` **UPNP_RESULT_SRC_IP_WILDCARD_NOT_PERMITTED** = ``6``
  131. The :ref:`UPNPDevice<class_UPNPDevice>` does not allow wildcard values for the source IP address.
  132. .. _class_UPNP_constant_UPNP_RESULT_EXT_PORT_WILDCARD_NOT_PERMITTED:
  133. .. rst-class:: classref-enumeration-constant
  134. :ref:`UPNPResult<enum_UPNP_UPNPResult>` **UPNP_RESULT_EXT_PORT_WILDCARD_NOT_PERMITTED** = ``7``
  135. The :ref:`UPNPDevice<class_UPNPDevice>` does not allow wildcard values for the external port.
  136. .. _class_UPNP_constant_UPNP_RESULT_INT_PORT_WILDCARD_NOT_PERMITTED:
  137. .. rst-class:: classref-enumeration-constant
  138. :ref:`UPNPResult<enum_UPNP_UPNPResult>` **UPNP_RESULT_INT_PORT_WILDCARD_NOT_PERMITTED** = ``8``
  139. The :ref:`UPNPDevice<class_UPNPDevice>` does not allow wildcard values for the internal port.
  140. .. _class_UPNP_constant_UPNP_RESULT_REMOTE_HOST_MUST_BE_WILDCARD:
  141. .. rst-class:: classref-enumeration-constant
  142. :ref:`UPNPResult<enum_UPNP_UPNPResult>` **UPNP_RESULT_REMOTE_HOST_MUST_BE_WILDCARD** = ``9``
  143. The remote host value must be a wildcard.
  144. .. _class_UPNP_constant_UPNP_RESULT_EXT_PORT_MUST_BE_WILDCARD:
  145. .. rst-class:: classref-enumeration-constant
  146. :ref:`UPNPResult<enum_UPNP_UPNPResult>` **UPNP_RESULT_EXT_PORT_MUST_BE_WILDCARD** = ``10``
  147. The external port value must be a wildcard.
  148. .. _class_UPNP_constant_UPNP_RESULT_NO_PORT_MAPS_AVAILABLE:
  149. .. rst-class:: classref-enumeration-constant
  150. :ref:`UPNPResult<enum_UPNP_UPNPResult>` **UPNP_RESULT_NO_PORT_MAPS_AVAILABLE** = ``11``
  151. No port maps are available. May also be returned if port mapping functionality is not available.
  152. .. _class_UPNP_constant_UPNP_RESULT_CONFLICT_WITH_OTHER_MECHANISM:
  153. .. rst-class:: classref-enumeration-constant
  154. :ref:`UPNPResult<enum_UPNP_UPNPResult>` **UPNP_RESULT_CONFLICT_WITH_OTHER_MECHANISM** = ``12``
  155. 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.
  156. .. _class_UPNP_constant_UPNP_RESULT_CONFLICT_WITH_OTHER_MAPPING:
  157. .. rst-class:: classref-enumeration-constant
  158. :ref:`UPNPResult<enum_UPNP_UPNPResult>` **UPNP_RESULT_CONFLICT_WITH_OTHER_MAPPING** = ``13``
  159. Conflict with an existing port mapping.
  160. .. _class_UPNP_constant_UPNP_RESULT_SAME_PORT_VALUES_REQUIRED:
  161. .. rst-class:: classref-enumeration-constant
  162. :ref:`UPNPResult<enum_UPNP_UPNPResult>` **UPNP_RESULT_SAME_PORT_VALUES_REQUIRED** = ``14``
  163. External and internal port values must be the same.
  164. .. _class_UPNP_constant_UPNP_RESULT_ONLY_PERMANENT_LEASE_SUPPORTED:
  165. .. rst-class:: classref-enumeration-constant
  166. :ref:`UPNPResult<enum_UPNP_UPNPResult>` **UPNP_RESULT_ONLY_PERMANENT_LEASE_SUPPORTED** = ``15``
  167. Only permanent leases are supported. Do not use the ``duration`` parameter when adding port mappings.
  168. .. _class_UPNP_constant_UPNP_RESULT_INVALID_GATEWAY:
  169. .. rst-class:: classref-enumeration-constant
  170. :ref:`UPNPResult<enum_UPNP_UPNPResult>` **UPNP_RESULT_INVALID_GATEWAY** = ``16``
  171. Invalid gateway.
  172. .. _class_UPNP_constant_UPNP_RESULT_INVALID_PORT:
  173. .. rst-class:: classref-enumeration-constant
  174. :ref:`UPNPResult<enum_UPNP_UPNPResult>` **UPNP_RESULT_INVALID_PORT** = ``17``
  175. Invalid port.
  176. .. _class_UPNP_constant_UPNP_RESULT_INVALID_PROTOCOL:
  177. .. rst-class:: classref-enumeration-constant
  178. :ref:`UPNPResult<enum_UPNP_UPNPResult>` **UPNP_RESULT_INVALID_PROTOCOL** = ``18``
  179. Invalid protocol.
  180. .. _class_UPNP_constant_UPNP_RESULT_INVALID_DURATION:
  181. .. rst-class:: classref-enumeration-constant
  182. :ref:`UPNPResult<enum_UPNP_UPNPResult>` **UPNP_RESULT_INVALID_DURATION** = ``19``
  183. Invalid duration.
  184. .. _class_UPNP_constant_UPNP_RESULT_INVALID_ARGS:
  185. .. rst-class:: classref-enumeration-constant
  186. :ref:`UPNPResult<enum_UPNP_UPNPResult>` **UPNP_RESULT_INVALID_ARGS** = ``20``
  187. Invalid arguments.
  188. .. _class_UPNP_constant_UPNP_RESULT_INVALID_RESPONSE:
  189. .. rst-class:: classref-enumeration-constant
  190. :ref:`UPNPResult<enum_UPNP_UPNPResult>` **UPNP_RESULT_INVALID_RESPONSE** = ``21``
  191. Invalid response.
  192. .. _class_UPNP_constant_UPNP_RESULT_INVALID_PARAM:
  193. .. rst-class:: classref-enumeration-constant
  194. :ref:`UPNPResult<enum_UPNP_UPNPResult>` **UPNP_RESULT_INVALID_PARAM** = ``22``
  195. Invalid parameter.
  196. .. _class_UPNP_constant_UPNP_RESULT_HTTP_ERROR:
  197. .. rst-class:: classref-enumeration-constant
  198. :ref:`UPNPResult<enum_UPNP_UPNPResult>` **UPNP_RESULT_HTTP_ERROR** = ``23``
  199. HTTP error.
  200. .. _class_UPNP_constant_UPNP_RESULT_SOCKET_ERROR:
  201. .. rst-class:: classref-enumeration-constant
  202. :ref:`UPNPResult<enum_UPNP_UPNPResult>` **UPNP_RESULT_SOCKET_ERROR** = ``24``
  203. Socket error.
  204. .. _class_UPNP_constant_UPNP_RESULT_MEM_ALLOC_ERROR:
  205. .. rst-class:: classref-enumeration-constant
  206. :ref:`UPNPResult<enum_UPNP_UPNPResult>` **UPNP_RESULT_MEM_ALLOC_ERROR** = ``25``
  207. Error allocating memory.
  208. .. _class_UPNP_constant_UPNP_RESULT_NO_GATEWAY:
  209. .. rst-class:: classref-enumeration-constant
  210. :ref:`UPNPResult<enum_UPNP_UPNPResult>` **UPNP_RESULT_NO_GATEWAY** = ``26``
  211. No gateway available. You may need to call :ref:`discover<class_UPNP_method_discover>` first, or discovery didn't detect any valid IGDs (InternetGatewayDevices).
  212. .. _class_UPNP_constant_UPNP_RESULT_NO_DEVICES:
  213. .. rst-class:: classref-enumeration-constant
  214. :ref:`UPNPResult<enum_UPNP_UPNPResult>` **UPNP_RESULT_NO_DEVICES** = ``27``
  215. 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.
  216. .. _class_UPNP_constant_UPNP_RESULT_UNKNOWN_ERROR:
  217. .. rst-class:: classref-enumeration-constant
  218. :ref:`UPNPResult<enum_UPNP_UPNPResult>` **UPNP_RESULT_UNKNOWN_ERROR** = ``28``
  219. Unknown error.
  220. .. rst-class:: classref-section-separator
  221. ----
  222. .. rst-class:: classref-descriptions-group
  223. Property Descriptions
  224. ---------------------
  225. .. _class_UPNP_property_discover_ipv6:
  226. .. rst-class:: classref-property
  227. :ref:`bool<class_bool>` **discover_ipv6** = ``false`` :ref:`🔗<class_UPNP_property_discover_ipv6>`
  228. .. rst-class:: classref-property-setget
  229. - |void| **set_discover_ipv6**\ (\ value\: :ref:`bool<class_bool>`\ )
  230. - :ref:`bool<class_bool>` **is_discover_ipv6**\ (\ )
  231. If ``true``, IPv6 is used for :ref:`UPNPDevice<class_UPNPDevice>` discovery.
  232. .. rst-class:: classref-item-separator
  233. ----
  234. .. _class_UPNP_property_discover_local_port:
  235. .. rst-class:: classref-property
  236. :ref:`int<class_int>` **discover_local_port** = ``0`` :ref:`🔗<class_UPNP_property_discover_local_port>`
  237. .. rst-class:: classref-property-setget
  238. - |void| **set_discover_local_port**\ (\ value\: :ref:`int<class_int>`\ )
  239. - :ref:`int<class_int>` **get_discover_local_port**\ (\ )
  240. 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.
  241. .. rst-class:: classref-item-separator
  242. ----
  243. .. _class_UPNP_property_discover_multicast_if:
  244. .. rst-class:: classref-property
  245. :ref:`String<class_String>` **discover_multicast_if** = ``""`` :ref:`🔗<class_UPNP_property_discover_multicast_if>`
  246. .. rst-class:: classref-property-setget
  247. - |void| **set_discover_multicast_if**\ (\ value\: :ref:`String<class_String>`\ )
  248. - :ref:`String<class_String>` **get_discover_multicast_if**\ (\ )
  249. Multicast interface to use for discovery. Uses the default multicast interface if empty.
  250. .. rst-class:: classref-section-separator
  251. ----
  252. .. rst-class:: classref-descriptions-group
  253. Method Descriptions
  254. -------------------
  255. .. _class_UPNP_method_add_device:
  256. .. rst-class:: classref-method
  257. |void| **add_device**\ (\ device\: :ref:`UPNPDevice<class_UPNPDevice>`\ ) :ref:`🔗<class_UPNP_method_add_device>`
  258. Adds the given :ref:`UPNPDevice<class_UPNPDevice>` to the list of discovered devices.
  259. .. rst-class:: classref-item-separator
  260. ----
  261. .. _class_UPNP_method_add_port_mapping:
  262. .. rst-class:: classref-method
  263. :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>`
  264. 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.
  265. 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.
  266. If ``port_internal`` is ``0`` (the default), the same port number is used for both the external and the internal port (the ``port`` value).
  267. The description (``desc``) is shown in some routers management UIs and can be used to point out which application added the mapping.
  268. 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).
  269. See :ref:`UPNPResult<enum_UPNP_UPNPResult>` for possible return values.
  270. .. rst-class:: classref-item-separator
  271. ----
  272. .. _class_UPNP_method_clear_devices:
  273. .. rst-class:: classref-method
  274. |void| **clear_devices**\ (\ ) :ref:`🔗<class_UPNP_method_clear_devices>`
  275. Clears the list of discovered devices.
  276. .. rst-class:: classref-item-separator
  277. ----
  278. .. _class_UPNP_method_delete_port_mapping:
  279. .. rst-class:: classref-method
  280. :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>`
  281. 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.
  282. .. rst-class:: classref-item-separator
  283. ----
  284. .. _class_UPNP_method_discover:
  285. .. rst-class:: classref-method
  286. :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>`
  287. Discovers local :ref:`UPNPDevice<class_UPNPDevice>`\ s. Clears the list of previously discovered devices.
  288. 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.
  289. See :ref:`UPNPResult<enum_UPNP_UPNPResult>` for possible return values.
  290. .. rst-class:: classref-item-separator
  291. ----
  292. .. _class_UPNP_method_get_device:
  293. .. rst-class:: classref-method
  294. :ref:`UPNPDevice<class_UPNPDevice>` **get_device**\ (\ index\: :ref:`int<class_int>`\ ) |const| :ref:`🔗<class_UPNP_method_get_device>`
  295. Returns the :ref:`UPNPDevice<class_UPNPDevice>` at the given ``index``.
  296. .. rst-class:: classref-item-separator
  297. ----
  298. .. _class_UPNP_method_get_device_count:
  299. .. rst-class:: classref-method
  300. :ref:`int<class_int>` **get_device_count**\ (\ ) |const| :ref:`🔗<class_UPNP_method_get_device_count>`
  301. Returns the number of discovered :ref:`UPNPDevice<class_UPNPDevice>`\ s.
  302. .. rst-class:: classref-item-separator
  303. ----
  304. .. _class_UPNP_method_get_gateway:
  305. .. rst-class:: classref-method
  306. :ref:`UPNPDevice<class_UPNPDevice>` **get_gateway**\ (\ ) |const| :ref:`🔗<class_UPNP_method_get_gateway>`
  307. Returns the default gateway. That is the first discovered :ref:`UPNPDevice<class_UPNPDevice>` that is also a valid IGD (InternetGatewayDevice).
  308. .. rst-class:: classref-item-separator
  309. ----
  310. .. _class_UPNP_method_query_external_address:
  311. .. rst-class:: classref-method
  312. :ref:`String<class_String>` **query_external_address**\ (\ ) |const| :ref:`🔗<class_UPNP_method_query_external_address>`
  313. 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.
  314. .. rst-class:: classref-item-separator
  315. ----
  316. .. _class_UPNP_method_remove_device:
  317. .. rst-class:: classref-method
  318. |void| **remove_device**\ (\ index\: :ref:`int<class_int>`\ ) :ref:`🔗<class_UPNP_method_remove_device>`
  319. Removes the device at ``index`` from the list of discovered devices.
  320. .. rst-class:: classref-item-separator
  321. ----
  322. .. _class_UPNP_method_set_device:
  323. .. rst-class:: classref-method
  324. |void| **set_device**\ (\ index\: :ref:`int<class_int>`, device\: :ref:`UPNPDevice<class_UPNPDevice>`\ ) :ref:`🔗<class_UPNP_method_set_device>`
  325. Sets the device at ``index`` from the list of discovered devices to ``device``.
  326. .. |virtual| replace:: :abbr:`virtual (This method should typically be overridden by the user to have any effect.)`
  327. .. |const| replace:: :abbr:`const (This method has no side effects. It doesn't modify any of the instance's member variables.)`
  328. .. |vararg| replace:: :abbr:`vararg (This method accepts any number of arguments after the ones described here.)`
  329. .. |constructor| replace:: :abbr:`constructor (This method is used to construct a type.)`
  330. .. |static| replace:: :abbr:`static (This method doesn't need an instance to be called, so it can be called directly using the class name.)`
  331. .. |operator| replace:: :abbr:`operator (This method describes a valid operator to use with this type as left-hand operand.)`
  332. .. |bitfield| replace:: :abbr:`BitField (This value is an integer composed as a bitmask of the following flags.)`
  333. .. |void| replace:: :abbr:`void (No return value.)`