A lightweight and easy-to-use class library to allow port forwarding in NAT devices (Network Address Translator) that support Universal Plug and Play (UPNP) and/or Port Mapping Protocol (PMP).
$ dotnet add package SharpOpenNatSharpOpenNat is a lightweight and easy-to-use class library to allow port forwarding in NAT devices that support UPNP (Universal Plug & Play) and/or PMP (Port Mapping Protocol).
NATed computers cannot be reached from outside and this is particularly painful for peer-to-peer or friend-to-friend software. The main goal is to simplify communication amoung computers behind NAT devices that support UPNP and/or PMP providing a clean and easy interface to get the external IP address and map ports and helping you to achieve peer-to-peer communication.
With nuget :
Install-Package SharpOpenNat
Go on the nuget website for more information.
The simplest scenario:
var device = await OpenNat.Discoverer.DiscoverDeviceAsync();
var ip = await device.GetExternalIPAsync();
Console.WriteLine("The external IP Address is: {0} ", ip);
The following piece of code shows a common scenario: It starts the discovery process for a NAT-UPNP device and onces discovered it creates a port mapping. If no device is found before ten seconds, it fails with NatDeviceNotFoundException.
using var cts = new CancellationTokenSource(10000);
var device = await OpenNat.Discoverer.DiscoverDeviceAsync(PortMapper.Upnp, cts.Token);
await device.CreatePortMapAsync(new Mapping(Protocol.Tcp, 1600, 1700, "The mapping name"));
You are welcome to contribute code. You can send code both as a patch or a GitHub pull request.
| Description | Value |
|---|---|
| License | The MIT License (MIT) |
| Documentation | http://jeremyansel.github.io/SharpOpenNat |
| Source code | https://github.com/JeremyAnsel/SharpOpenNat |
| Nuget | https://www.nuget.org/packages/SharpOpenNat |
| Build | https://ci.appveyor.com/project/JeremyAnsel/sharpopennat/branch/main |