OpenPort.Net 1.0.0

dotnet add package OpenPort.Net --version 1.0.0
                    
NuGet\Install-Package OpenPort.Net -Version 1.0.0
                    
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="OpenPort.Net" Version="1.0.0" />
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="OpenPort.Net" Version="1.0.0" />
                    
Directory.Packages.props
<PackageReference Include="OpenPort.Net" />
                    
Project file
For projects that support Central Package Management (CPM), copy this XML node into the solution Directory.Packages.props file to version the package.
paket add OpenPort.Net --version 1.0.0
                    
#r "nuget: OpenPort.Net, 1.0.0"
                    
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
#:package OpenPort.Net@1.0.0
                    
#:package directive can be used in C# file-based apps starting in .NET 10 preview 4. Copy this into a .cs file before any lines of code to reference the package.
#addin nuget:?package=OpenPort.Net&version=1.0.0
                    
Install as a Cake Addin
#tool nuget:?package=OpenPort.Net&version=1.0.0
                    
Install as a Cake Tool

OpenPort.Net

OpenPort.Net is a lightweight cross-platform .NET library for automatic NAT port mapping. It lets desktop apps, game servers, voice chat servers, P2P apps and self-hosted tools open router ports automatically using UPnP IGD, NAT-PMP and PCP. No external binaries are required.

Install

dotnet add package OpenPort.Net

Target frameworks:

  • net8.0
  • netstandard2.1

Current package version: 1.0.0.

Quick Start

using System.Net;
using OpenPort.Net;
using OpenPort.Net.Models;

var client = new OpenPortClient();

var result = await client.OpenAsync(new PortMapping
{
    InternalPort = 19132,
    ExternalPort = 19132,
    Protocol = PortProtocol.Udp,
    Description = "My App",
    Lifetime = TimeSpan.FromHours(1)
});

if (result.Status is OpenPortStatus.Success or OpenPortStatus.ExternalPortChanged)
{
    Console.WriteLine($"Opened with {result.Provider} on external port {result.ExternalPort}");
    await client.CloseAsync(result.Mapping!);
}

For automatic renew and cleanup:

await using var lease = await client.OpenLeaseAsync(new PortMapping
{
    InternalPort = 19132,
    ExternalPort = 19132,
    Protocol = PortProtocol.Udp,
    Description = "My App",
    Lifetime = TimeSpan.FromHours(1)
});

OpenPortLease renews temporary mappings and closes the mapping when disposed.

Provider Customization

The default order is:

  1. PCP
  2. NAT-PMP
  3. UPnP IGD

For full customisation, pass provider instances. This controls both order and implementation:

using OpenPort.Net.Providers;

var client = new OpenPortClient(new OpenPortOptions
{
    Providers =
    [
        new UpnpIgdProvider(TimeSpan.FromSeconds(5)),
        new NatPmpProvider(TimeSpan.FromSeconds(5)),
        new PcpProvider(TimeSpan.FromSeconds(5))
    ],
    Timeout = TimeSpan.FromSeconds(5)
});

You can also implement IPortMappingProvider and put your own provider in the list.

For simple built-in provider reordering, PreferredProtocols is still available:

var client = new OpenPortClient(new OpenPortOptions
{
    PreferredProtocols =
    [
        PortMappingProtocol.UpnpIgd,
        PortMappingProtocol.NatPmp,
        PortMappingProtocol.Pcp
    ]
});

If you already know the gateway or UPnP root device URL, provide hints:

var client = new OpenPortClient(new OpenPortOptions
{
    GatewayAddress = IPAddress.Parse("192.168.1.1"),
    UpnpRootDeviceUris = [new Uri("http://192.168.1.1:5000/root.xml")]
});

Behavior

  • All operations are async and accept CancellationToken.
  • A mapping is not considered open until the gateway reports success.
  • If a gateway assigns a different external port, the result status is ExternalPortChanged and result.Mapping contains the assigned port.
  • Mappings opened by an OpenPortClient instance are renewed and closed through the same provider that opened them.
  • UPnP supports WANIPConnection:1, WANIPConnection:2 and WANPPPConnection:1.
  • NAT-PMP supports external address lookup, TCP/UDP map, renew and delete.
  • PCP supports ANNOUNCE and MAP for TCP/UDP.

Sample

dotnet run --project samples/OpenPort.Net.Sample -- 19132 19132 udp

Development

dotnet build OpenPort.Net.sln
dotnet test OpenPort.Net.sln
dotnet pack OpenPort.Net/OpenPort.Net.csproj -c Release -o artifacts

The test suite includes protocol codec tests and mocked UPnP, NAT-PMP and PCP routers.

License

MIT

Product Compatible and additional computed target framework versions.
.NET net5.0 was computed.  net5.0-windows was computed.  net6.0 was computed.  net6.0-android was computed.  net6.0-ios was computed.  net6.0-maccatalyst was computed.  net6.0-macos was computed.  net6.0-tvos was computed.  net6.0-windows was computed.  net7.0 was computed.  net7.0-android was computed.  net7.0-ios was computed.  net7.0-maccatalyst was computed.  net7.0-macos was computed.  net7.0-tvos was computed.  net7.0-windows was computed.  net8.0 is compatible.  net8.0-android was computed.  net8.0-browser was computed.  net8.0-ios was computed.  net8.0-maccatalyst was computed.  net8.0-macos was computed.  net8.0-tvos was computed.  net8.0-windows was computed.  net9.0 was computed.  net9.0-android was computed.  net9.0-browser was computed.  net9.0-ios was computed.  net9.0-maccatalyst was computed.  net9.0-macos was computed.  net9.0-tvos was computed.  net9.0-windows was computed.  net10.0 was computed.  net10.0-android was computed.  net10.0-browser was computed.  net10.0-ios was computed.  net10.0-maccatalyst was computed.  net10.0-macos was computed.  net10.0-tvos was computed.  net10.0-windows was computed. 
.NET Core netcoreapp3.0 was computed.  netcoreapp3.1 was computed. 
.NET Standard netstandard2.1 is compatible. 
MonoAndroid monoandroid was computed. 
MonoMac monomac was computed. 
MonoTouch monotouch was computed. 
Tizen tizen60 was computed. 
Xamarin.iOS xamarinios was computed. 
Xamarin.Mac xamarinmac was computed. 
Xamarin.TVOS xamarintvos was computed. 
Xamarin.WatchOS xamarinwatchos was computed. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.
  • .NETStandard 2.1

    • No dependencies.
  • net8.0

    • No dependencies.

NuGet packages

This package is not used by any NuGet packages.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last Updated
1.0.0 183 5/18/2026

Initial v1.0 test release with UPnP IGD, NAT-PMP, PCP, provider fallback, renew leases, XML docs, sample app and mocked router tests.