UPNP network functions.
    public class UPNP : Reference

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.

Inheritance Chain


    public UPNP()


    public bool DiscoverIpv6 { get; set; }

If true, IPv6 is used for UPNPDevice discovery.

    public int DiscoverLocalPort { get; set; }

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.

    public string DiscoverMulticastIf { get; set; }

Multicast interface to use for discovery. Uses the default multicast interface if empty.


    public void AddDevice(UPNPDevice device)

Adds the given UPNPDevice to the list of discovered devices.

    public int AddPortMapping(int port, int portInternal = 0, string desc = "", string proto = "UDP", int duration = 0)

Adds a mapping to forward the external port (between 1 and 65535) on the default gateway (see GetGateway()) 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 GetGateway() and call AddPortMapping(int, int, string, string, int) 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, and the lifetime of the mapping can be limited by duration. 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.

    public void ClearDevices()

Clears the list of discovered devices.

    public int DeletePortMapping(int port, string proto = "UDP")

Deletes the port mapping for the given port and protocol combination on the default gateway (see GetGateway()) 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.

    public int Discover(int timeout = 2000, int ttl = 2, string deviceFilter = "InternetGatewayDevice")

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.

    public UPNPDevice GetDevice(int index)

Returns the UPNPDevice at the given index.

    public int GetDeviceCount()

Returns the number of discovered UPNPDevices.

    public int GetDiscoverLocalPort()

Getter for DiscoverLocalPort

    public string GetDiscoverMulticastIf()

Getter for DiscoverMulticastIf

    public UPNPDevice GetGateway()

Returns the default gateway. That is the first discovered UPNPDevice that is also a valid IGD (InternetGatewayDevice).

    public bool IsDiscoverIpv6()

Getter for DiscoverIpv6

    public string QueryExternalAddress()

Returns the external IP address of the default gateway (see GetGateway()) as string. Returns an empty string on error.

    public void RemoveDevice(int index)

Removes the device at index from the list of discovered devices.

    public void SetDevice(int index, UPNPDevice device)

Sets the device at index from the list of discovered devices to device.

    public void SetDiscoverIpv6(bool ipv6)

Setter for DiscoverIpv6

    public void SetDiscoverLocalPort(int port)

Setter for DiscoverLocalPort

    public void SetDiscoverMulticastIf(string mIf)

Setter for DiscoverMulticastIf

Inner Types


Name Value Description
Success 0 UPNP command or discovery was successful.
NotAuthorized 1 Not authorized to use the command on the [UPNPDevice]. May be returned when the user disabled UPNP on their router.
PortMappingNotFound 2 No port mapping was found for the given port, protocol combination on the given [UPNPDevice].
InconsistentParameters 3 Inconsistent parameters.
NoSuchEntryInArray 4 No such entry in array. May be returned if a given port, protocol combination is not found on an [UPNPDevice].
ActionFailed 5 The action failed.
SrcIpWildcardNotPermitted 6 The [UPNPDevice] does not allow wildcard values for the source IP address.
ExtPortWildcardNotPermitted 7 The [UPNPDevice] does not allow wildcard values for the external port.
IntPortWildcardNotPermitted 8 The [UPNPDevice] does not allow wildcard values for the internal port.
RemoteHostMustBeWildcard 9 The remote host value must be a wildcard.
ExtPortMustBeWildcard 10 The external port value must be a wildcard.
NoPortMapsAvailable 11 No port maps are available. May also be returned if port mapping functionality is not available.
ConflictWithOtherMechanism 12 Conflict with other mechanism. May be returned instead of [code]UPNP_RESULT_CONFLICT_WITH_OTHER_MAPPING[/code] if a port mapping conflicts with an existing one.
ConflictWithOtherMapping 13 Conflict with an existing port mapping.
SamePortValuesRequired 14 External and internal port values must be the same.
OnlyPermanentLeaseSupported 15 Only permanent leases are supported. Do not use the [code]duration[/code] parameter when adding port mappings.
InvalidGateway 16 Invalid gateway.
InvalidPort 17 Invalid port.
InvalidProtocol 18 Invalid protocol.
InvalidDuration 19 Invalid duration.
InvalidArgs 20 Invalid arguments.
InvalidResponse 21 Invalid response.
InvalidParam 22 Invalid parameter.
HttpError 23 HTTP error.
SocketError 24 Socket error.
MemAllocError 25 Error allocating memory.
NoGateway 26 No gateway available. You may need to call [method discover] first, or discovery didn’t detect any valid IGDs (InternetGatewayDevices).
NoDevices 27 No devices available. You may need to call [method discover] first, or discovery didn’t detect any valid [UPNPDevice]s.
UnknownError 28 Unknown error.