Programmatic Router Port Mapping

I'm working on some peer-to-peer stuff and I need to make this service available to those behind routers. Two things need to be accomplished:

1. Detect whether or not the user is behind a router.
2. If the user is behind a router, forward port X to the user's network IP.

I've seen this done using COM and C, but I can't translate into C#.

Thanks!
[398 byte] By [124256] at [2007-11-20 10:30:33]
# 1 Re: Programmatic Router Port Mapping
The easiest thing to do is use Mono.Nat ;) It's a pure c# library for handling port forwarding via uPnP. It runs on windows, macos and linux and i believe any portable device capable of running the compact .NET framework.

http://anonsvn.mono-project.com/viewcvs/trunk/Mono.Nat/

If you have any questions about it's use, just ask. But the example code should make it clear enough how it should be used.
Mutant_Fruit at 2007-11-9 11:35:31 >
# 2 Re: Programmatic Router Port Mapping
I don't see any dll files in there. What do I need to download?
124256 at 2007-11-9 11:36:32 >
# 3 Re: Programmatic Router Port Mapping
Thats what we programmers like to refer to as 'source code'. You download it (by clicking on the 'Download Tarball' link) and then you can open it up in Visual Studio (or whatever) using the .csproj file. Then you compile it and add it as a reference to whatever project you like. Alternatively, you can add all the .cs files directly to your project so that you don't create a seperate dll.
Mutant_Fruit at 2007-11-9 11:37:31 >
# 4 Re: Programmatic Router Port Mapping
When I run the example it stops while sending a UDP request and says:

System.Net.Sockets.SocketException was unhandled
Message="The system detected an invalid pointer address in attempting to use a pointer argument in a call"
Source="System"
ErrorCode=10014
NativeErrorCode=10014
StackTrace:
at System.Net.Sockets.Socket.SendTo(Byte[] buffer, Int32 offset, Int32 size, SocketFlags socketFlags, EndPoint remoteEP)
at System.Net.Sockets.UdpClient.Send(Byte[] dgram, Int32 bytes, IPEndPoint endPoint)
at Nat.NatController.Search() in D:\Noah\Documents\Visual Studio 2005\Projects\Mono.Nat\Mono.Nat\NatController.cs:line 130
at Nat.NatController.Start() in D:\Noah\Documents\Visual Studio 2005\Projects\Mono.Nat\Mono.Nat\NatController.cs:line 103
at TestClient.Program.Main(String[] args) in D:\Noah\Documents\Visual Studio 2005\Projects\Mono.Nat\Mono.Nat\Examples\Program.cs:line 19
at System.AppDomain._nExecuteAssembly(Assembly assembly, String[] args)
at System.AppDomain.ExecuteAssembly(String assemblyFile, Evidence assemblySecurity, String[] args)
at Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly()
at System.Threading.ThreadHelper.ThreadStart_Context(Object state)
at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
at System.Threading.ThreadHelper.ThreadStart()
InnerException:
124256 at 2007-11-9 11:38:26 >
# 5 Re: Programmatic Router Port Mapping
Now that should be impossible ;)

What OS are you on? Also, could you disconnect yourself from the internet and try disabling any anti-virus software or spyware software you have running and trying again. Specifically if you have NOD32, i've heard of it causing issues before.

Thing is, that's a problem from within the .NET framework, which is odd. I assume you're on Vista?
Mutant_Fruit at 2007-11-9 11:39:25 >
# 6 Re: Programmatic Router Port Mapping
Yes I'm on Vista, which, as we all know, is a bit odd itself. I'm using .NET 3.5. Think they have a bug?
124256 at 2007-11-9 11:40:35 >
# 7 Re: Programmatic Router Port Mapping
Most definitely. I've had complaints off other people who had similar problems where the UDP stuff was breaking in vista. However as i don't have vista i can't try fix the issue.

If you could find a workaround for the issue, that'd be amazing. If you rewrote the UdpClient stuff in the NatController clss to use a standard socket, that may fix your issue. After that, i have no idea. You could try messing around with the socket options to see if there's anything you can do that'll make it not break on vista.
Mutant_Fruit at 2007-11-9 11:41:29 >