2

i need to perform port forwarding in a desktop application written in C#

i used this code :

using System;
using System.Threading;
using NATUPNPLib;

namespace iSpyApplication
{
    public static class NATControl
    {
        public static UPnPNAT NAT = new UPnPNAT();
        private static IStaticPortMappingCollection _mappings;

        public static IStaticPortMappingCollection Mappings
        {
            get
            {
               if (_mappings==null)
               {

                   try
                   {
                       if (NAT.NATEventManager != null)
                        _mappings = NAT.StaticPortMappingCollection;
                   }
                   catch
                   {
                   }
               }

               return _mappings;
            }
         }

        public static bool SetPorts(int wanPort, int lanPort)
        {
           bool b = false;
           int i = 3;
           while (Mappings == null && i > 0)
           {
               Thread.Sleep(2000);
               i--;
           }

           if (Mappings != null)
           {
               try
               {
                   Mappings.Remove(wanPort, "TCP");
               }
               catch (Exception ex)
               {
                   // do something
               }
               try
               {
                   Mappings.Add(wanPort, "TCP", lanPort, internalIP, true, "iSpy");
                   b = true;
               }
               catch (Exception ex)
               {
                   // do something
               }
           }

           return b;

        } // method

  } // class    

} // namespace

UPnP is enabled in my linksys router

the code is running and not giving any errors or exceptions, however, mapping is simply not happening.

here is how i am testing it:

  1. i have an application that listens to an internal port e.g. 8080.
  2. i manually add a mapping entry to my router:

    application : HTTP , external port 8080 , internal port 8080, protocol: TCP , internal IP , enabled

  3. i request the following URL in my browser: http : // publicIP : externalport /

  4. the application receives a socket on the internal port and replies with a message.

i remove the mapping entry and try to add it programmatically

  1. the same
  2. i run a code that performs mapping
  3. the same
  4. browser displays the followin message :

    Unable to connect

    Firefox can't establish a connection to the server at publicIP:externalPort

i have searched for C# code that performs mapping, and they all look the same. Why would mapping fail ? Thanks in advance

Nina
  • 508
  • 4
  • 21

1 Answers1

0

Idea 1: IStaticPortMappingCollection.Add does have a return value, which you should check for errors.

Idea 2: Your router may not allow you setting a static route over UPnP.

Run DeviceSpy from UPnP Developer Tools and look for your router. Its URN ends in *:InternetGatewayDevice. Under the device, look for service WANIPConnection. Look for the number after the service name. If it is 2, then you are out of luck. WANIPConnection service description (downloadable at the UPnP website) states at the PortMappingLeaseDuration:

In release 1.0, a value of 0 was used to create a static port mapping. In version 2.0, it is no longer possible to create static port mappings via UPnP actions. Instead, an out-of-band mechanism is REQUIRED to do so (cf. WWW-administration, remote management or local management).

Even if your WANIPConnection is still version 1, you can use DeviceSpy for finding out whether the route is really being set by your code or not.

Pavel Zdenek
  • 7,146
  • 1
  • 23
  • 38
  • Thanks for the reply. Considering Idea1 : in C#, the return type of IStaticPortMappingCollection.Add method is IStaticPortMapping, and it's returning an object that doesn't seem to have errors... Considering Idea2: i found my router ( Its URN ends in *:InternetGatewayDevice) and under it there is a service called (Layer3Forwarding), LANDevice and WANDevice,they are all version 1, but i found no (WANIPConnection) service! How can i use Device Spy to check if the route is being set by my code? Thanks again. – Nina Nov 19 '12 at 20:15
  • 1. i am not talking about *output parameter* but *return value* 2. The devices are hierarchic: `WANDevice/WanConnectionDevice/WANIPConnection`. You are looking for `PortMappingNumberOfEntries` and then `GetGenericPortMappingEntry`. You might also try `AddPortMapping` as that's what your code would have to use. – Pavel Zdenek Nov 19 '12 at 21:00
  • IStaticPortMapping m = mappings.Add(ExternalPort, Protocol, InternalPort, LocalIP, true, Description) ; this line of code is from the project : http://www.codeproject.com/Articles/110338/UpnpStat-List-and-Modify-Port-Mappings-on-your-UPn ... i ran the project. No errors, and no EFFECT neither (mapping didn't seem to take place) – Nina Dec 07 '12 at 20:25
  • Ok so you are left with **Idea 2**. – Pavel Zdenek Dec 07 '12 at 22:29