Friday, December 22, 2006

On yet another new topic, i'm proud to have the first implementation of Mono.Nat available here. Mono.Nat is a C# implementation of the uPnP protocol implementing just the bits needed to forward a port from a uPnP enabled router to your computer. There are plans to implement the PMP protocol aswell, along with any other port forwarding type protocols that get devised in the future. The idea is that regardless of what kind of router you have, you'll be able to map a port with a 3 (or so) lines of code.

It comes with a code example showing you one way in which to use the code. But it is fairly self explanatory. It's as easy as: natDevice.BeginMapPort(12321, TCP); natDevice.EndMapPort();

If anyone has a NAT enabled router, i'd appreciate it if you could load up the TestClient (in the examples directory) and see if it will map and unmap the 4 different ports. If it does, then all is well. If not, then either tell me what the problem is, or try fix it yourself and send me a patch. It's all good :)


Dan Smith said...

Excellent work Alan, I'll be testing this out

RichB said...

First time it failed. Second time it appeared to succeed. Outputs are shown below.

Now, I'm using MacOS->m0n0wall->DSL router. I'm not even sure if any of these are uPnP, but I know they're NAT'd.

Original Output

$ mono Program.exe

Unhandled Exception: System.TypeInitializationException: An exception was thrown by the type initializer for Nat.NatController ---> System.Net.Sockets.SocketException: No such host is known
at System.Net.Dns.hostent_to_IPHostEntry (System.String h_name, System.String[] h_aliases, System.String[] h_addrlist) [0x00000]
at System.Net.Dns.GetHostByName (System.String hostName) [0x00000]
at System.Net.Dns.GetHostEntry (System.String hostNameOrAddress) [0x00000]
at System.Net.Dns.GetHostAddresses (System.String hostNameOrAddress) [0x00000]
at Nat.NatController..cctor () [0x00000] --- End of inner exception stack trace ---

at <0x00000> <unknown method>
at TestClient.Program.Main (System.String[] args) [0x00000]

Second run output
$ mono Program.exe

listening started
If you haven't seen a port map/unmap by now, no router has been found.

Press a key to exit...

Exyll said...

Hi your code is working. But I refactored it a bit.

I would like to send it to you but I cannot find a way to send the code to you.

If you send me an email then I can mail you the code:

ramon dot smits at gmail dot com

Maitre Dédé said...


I've just tested it under WinXP :
Source downloaded 2007-01-31
Compiled under VS2005
First run : Error when sending SearchData

I've noticed that when looking up my local IP, it returns IPv4 addresses, but also IPv6. The first element in the array is IPv6 for me, and this causes an exception when trying to send data.

I've put this function :

private static IPAddress[] GetLocalIPs()
List< IPAddress > lst = new List< IPAddress >();
IPAddress[] a= Dns.GetHostAddresses(Dns.GetHostName());
foreach(IPAddress addr in a)
if (addr.AddressFamily == AddressFamily.InterNetwork)
return lst.ToArray();

and it work fine for me (but I don't have any upnp elements in my network :/)

Hit Counter