Search code examples
c#udphole-punching

How to see what port was opened by router


If I do the following:

UdpClient c = new UdpClient();
c.Connect(new System.Net.IPEndPoint(IPAddress.Parse("69.65.85.125"), 9900));
c.Send(new byte[] { 1,2,3,4,5 }, 5);

then I will be sending a packet to my router then my router will send that packet to the ip "69.65.85.125".

If I where to capture that packet on the computer that has the ip "69.65.85.125" I will be able to see the port that was oppened by the router (client.RemoteEndpoint). How will it be possible to see that information without capturing the packet at the other enpoint? Is there a way to query the router?


Solution

  • If your router supports it you can query it via UPnP. Here is a wrapper library for UPnP I found for .NET, I have never used it so I cant give you any advice if it is good or not.

    Look at the ComponetsTest program for example code in the zip for the library. You will need to reference the UPnP documentation to find out what calls you will need to make to the service.

    From the message board of the library of someone asking a how to find port mappings.

    The WANPPPConnection and WANIPConnection services have actions called GetSpecificPortMappingEntry, simply call this iterating through the indexes from 0 until an error is returned, each call will return another UPnP port mapping, you can also get the static mappings with a different service.