UPNP
Hereda: RefCounted < Object
Funciones de Universal Plug and Play (UPnP) para el descubrimiento de dispositivos de red, consulta y redirección de puertos.
Descripción
This class can be used to discover compatible UPNPDevices 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 discover() and 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 Threads 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 is a good first stop, the specification can be found at the Open Connectivity Foundation and Godot's implementation is based on the MiniUPnP client.
Propiedades
|
||
|
||
|
Métodos
void |
add_device(device: UPNPDevice) |
add_port_mapping(port: int, port_internal: int = 0, desc: String = "", proto: String = "UDP", duration: int = 0) const |
|
void |
|
delete_port_mapping(port: int, proto: String = "UDP") const |
|
discover(timeout: int = 2000, ttl: int = 2, device_filter: String = "InternetGatewayDevice") |
|
get_device(index: int) const |
|
get_device_count() const |
|
get_gateway() const |
|
query_external_address() const |
|
void |
remove_device(index: int) |
void |
set_device(index: int, device: UPNPDevice) |
Enumeraciones
enum UPNPResult: 🔗
UPNPResult UPNP_RESULT_SUCCESS = 0
El comando o descubrimiento de la UPNP fue exitoso.
UPNPResult UPNP_RESULT_NOT_AUTHORIZED = 1
No está autorizado a usar el comando en el UPNPDevice. Puede ser devuelto cuando el usuario deshabilite el UPNP en su router.
UPNPResult UPNP_RESULT_PORT_MAPPING_NOT_FOUND = 2
No se encontró ningún mapeo de puerto para la combinación de protocolos del UPNPDevice dado.
UPNPResult UPNP_RESULT_INCONSISTENT_PARAMETERS = 3
Parámetros inconsistentes.
UPNPResult UPNP_RESULT_NO_SUCH_ENTRY_IN_ARRAY = 4
No existe tal entrada en el array. Puede ser devuelto si una combinación dada de puerto y protocolo no se encuentra en un UPNPDevice.
UPNPResult UPNP_RESULT_ACTION_FAILED = 5
La acción falló.
UPNPResult UPNP_RESULT_SRC_IP_WILDCARD_NOT_PERMITTED = 6
El UPNPDevice no permite valores comodín para la dirección IP de origen.
UPNPResult UPNP_RESULT_EXT_PORT_WILDCARD_NOT_PERMITTED = 7
El UPNPDevice no permite valores comodín para el puerto externo.
UPNPResult UPNP_RESULT_INT_PORT_WILDCARD_NOT_PERMITTED = 8
El UPNPDevice no permite valores comodín para el puerto interno.
UPNPResult UPNP_RESULT_REMOTE_HOST_MUST_BE_WILDCARD = 9
El valor del host remoto debe ser un comodín.
UPNPResult UPNP_RESULT_EXT_PORT_MUST_BE_WILDCARD = 10
El valor del puerto externo debe ser un comodín.
UPNPResult UPNP_RESULT_NO_PORT_MAPS_AVAILABLE = 11
No hay mapas de puertos disponibles. También puede ser devuelto si la funcionalidad de mapas de puertos no está disponible.
UPNPResult UPNP_RESULT_CONFLICT_WITH_OTHER_MECHANISM = 12
Conflicto con otro mecanismo. Puede ser devuelto en lugar de UPNP_RESULT_CONFLICT_WITH_OTHER_MAPPING si un mapeo de puertos entra en conflicto con uno existente.
UPNPResult UPNP_RESULT_CONFLICT_WITH_OTHER_MAPPING = 13
Conflicto con una mapeo de puertos existente.
UPNPResult UPNP_RESULT_SAME_PORT_VALUES_REQUIRED = 14
Los valores de los puertos externos e internos deben ser los mismos.
UPNPResult UPNP_RESULT_ONLY_PERMANENT_LEASE_SUPPORTED = 15
Solo se admiten los arrendamientos permanentes. No utilices el parámetro duration cuando añada mapeos de puertos.
UPNPResult UPNP_RESULT_INVALID_GATEWAY = 16
Gateway invalida.
UPNPResult UPNP_RESULT_INVALID_PORT = 17
Puerto inválido.
UPNPResult UPNP_RESULT_INVALID_PROTOCOL = 18
Protocolo inválido.
UPNPResult UPNP_RESULT_INVALID_DURATION = 19
Duración inválida.
UPNPResult UPNP_RESULT_INVALID_ARGS = 20
Argumentos inválidos.
UPNPResult UPNP_RESULT_INVALID_RESPONSE = 21
Respuesta inválida.
UPNPResult UPNP_RESULT_INVALID_PARAM = 22
Parámetro inválido.
UPNPResult UPNP_RESULT_HTTP_ERROR = 23
Error HTTP.
UPNPResult UPNP_RESULT_SOCKET_ERROR = 24
Error de socket.
UPNPResult UPNP_RESULT_MEM_ALLOC_ERROR = 25
Error en la asignación de la memoria.
UPNPResult UPNP_RESULT_NO_GATEWAY = 26
No hay ninguna gateway disponible. Puede que tengas que llamar a discover() primero, o el descubrimiento no detectó ningúna IGD (InternetGatewayDevices) válida.
UPNPResult UPNP_RESULT_NO_DEVICES = 27
No hay dispositivos disponibles. Puede que tengas que llamar a discover() primero, o el descubrimiento no detectó ninguna UPNPDevice válida.
UPNPResult UPNP_RESULT_UNKNOWN_ERROR = 28
Error desconocido.
Descripciones de Propiedades
Si es true, IPv6 se utiliza para el descubrimiento de UPNPDevice.
Si 0, el puerto local a utilizar para el descubrimiento es elegido automáticamente por el sistema. Si 1, el descubrimiento se hará desde el puerto de origen 1900 (el mismo que el puerto de destino). De lo contrario, el valor se utilizará como el puerto.
String discover_multicast_if = "" 🔗
Interfaz de multidifusión para usar para el descubrimiento. Utiliza la interfaz de multidifusión predeterminada si está vacía.
Descripciones de Métodos
void add_device(device: UPNPDevice) 🔗
Añade el UPNPDevice dado a la lista de dispositivos descubiertos.
int add_port_mapping(port: int, port_internal: int = 0, desc: String = "", proto: String = "UDP", duration: int = 0) const 🔗
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 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 get_gateway() and call 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 UPNPResult for possible return values.
void clear_devices() 🔗
Borra la lista de dispositivos descubiertos.
int delete_port_mapping(port: int, proto: String = "UDP") const 🔗
Elimina la asignación de puertos para la combinación de puerto y protocolo dada en la puerta de enlace predeterminada (ver get_gateway()) si existe. port debe ser un puerto válido entre 1 y 65535, proto puede ser "TCP" o "UDP". Puede ser rechazado para asignaciones que apunten a direcciones que no sean esta, para puertos conocidos (por debajo de 1024), o para asignaciones no añadidas a través de UPnP. Véase UPNPResult para ver los posibles valores de retorno.
int discover(timeout: int = 2000, ttl: int = 2, device_filter: String = "InternetGatewayDevice") 🔗
Descubre los UPNPDevices locales. Borra la lista de dispositivos descubiertos anteriormente.
Filtra los dispositivos de tipo IGD (InternetGatewayDevice) por defecto, ya que estos gestionan el reenvío de puertos. timeout es el tiempo de espera de las respuestas en milisegundos. ttl es el tiempo de vida; solo toca esto si sabes lo que estás haciendo.
Véase UPNPResult para ver los posibles valores de retorno.
UPNPDevice get_device(index: int) const 🔗
Devuelve el UPNPDevice en el index dado.
int get_device_count() const 🔗
Devuelve el número de UPNPDevice descubiertos.
UPNPDevice get_gateway() const 🔗
Devuelve la puerta de enlace por defecto. Es el primer dispositivo descubierto UPNPDevice que también es un IGD (InternetGatewayDevice) válido.
String query_external_address() const 🔗
Devuelve la dirección IP externa de la pasarela por defecto (véase get_gateway()) como string. Devuelve una string vacía en caso de error.
void remove_device(index: int) 🔗
Elimina el dispositivo en index de la lista de dispositivos descubiertos.
void set_device(index: int, device: UPNPDevice) 🔗
Establece el dispositivo en index de la lista de dispositivos descubiertos a device.