UPNP network functions.
Provides UPNP functionality to discover UPNPDevices on the local network and execute commands on them, like managing port mappings (port forwarding) 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:
const PORT = 7777 var upnp = UPNP.new() upnp.discover(2000, 2, "InternetGatewayDevice") upnp.add_port_mapping(port)
To close a specific port (e.g. after you have finished using it):
upnp.delete_port_mapping(port)
bool | discover_ipv6 | false |
int | discover_local_port | 0 |
String | discover_multicast_if | "" |
void | add_device ( UPNPDevice device ) |
int | add_port_mapping ( int port, int port_internal=0, String desc="", String proto="UDP", int duration=0 ) const |
void | clear_devices ( ) |
int | delete_port_mapping ( int port, String proto="UDP" ) const |
int | discover ( int timeout=2000, int ttl=2, String device_filter="InternetGatewayDevice" ) |
UPNPDevice | get_device ( int index ) const |
int | get_device_count ( ) const |
UPNPDevice | get_gateway ( ) const |
String | query_external_address ( ) const |
void | remove_device ( int index ) |
void | set_device ( int index, UPNPDevice device ) |
enum UPNPResult:
duration
parameter when adding port mappings.Default | false |
Setter | set_discover_ipv6(value) |
Getter | is_discover_ipv6() |
If true
, IPv6 is used for UPNPDevice discovery.
Default | 0 |
Setter | set_discover_local_port(value) |
Getter | 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.
Default | "" |
Setter | set_discover_multicast_if(value) |
Getter | get_discover_multicast_if() |
Multicast interface to use for discovery. Uses the default multicast interface if empty.
Adds the given UPNPDevice to the list of discovered devices.
Adds a mapping to forward the external port
(between 1 and 65535) on the default gateway (see get_gateway) to the internal_port
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 get_gateway and call add_port_mapping on it, if any.
If internal_port
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 router 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). However, some routers are incompatible with one or both of these, so use with caution and add fallback logic in case of errors to retry without them if in doubt.
See UPNPResult for possible return values.
Clears the list of discovered devices.
Deletes the port mapping for the given port and protocol combination on the default gateway (see get_gateway) if one exists. port
must be a valid port between 1 and 65535, proto
can be either TCP
or UDP
. See UPNPResult for possible return values.
Discovers local UPNPDevices. 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 UPNPResult for possible return values.
Returns the UPNPDevice at the given index
.
Returns the number of discovered UPNPDevices.
Returns the default gateway. That is the first discovered UPNPDevice that is also a valid IGD (InternetGatewayDevice).
Returns the external IP address of the default gateway (see get_gateway) as string. Returns an empty string on error.
Removes the device at index
from the list of discovered devices.
Sets the device at index
from the list of discovered devices to device
.
© 2014–2020 Juan Linietsky, Ariel Manzur, Godot Engine contributors
Licensed under the MIT License.
https://docs.godotengine.org/en/3.2/classes/class_upnp.html