简体   繁体   中英

Add a NAT UPnP mapping on a complex network using C#

I am creating an application that listens for connections (server) from a different application (client) via a tcp connection. So if both application where to be on the same network it will be easy to establish the connection. so I am trying to add a method to the server so that users do not have to open ports on their routers in order to make the app work when using it over the internet. so let me show you a few different scenarios:

1)---------------------------------------------------SCENARIO_1-------------------------------------------------------------

the server is a computer in a house. That computer is connected to a router and that router is connected to the internet. The client is a computer on a office that has access to the internet as well.

for this scenario to work, before I used to open the ports on the house router and forward them to the server computer. on the client I just had to supply the correct IP address and same port in order to establish the connection.

now I avoided opening the ports on the house router with a really cool technique:

first I add this reference:

在此输入图像描述

then I tell the router which ports I want to be forwarded to my computer (the server):

    NATUPNPLib.UPnPNATClass upnpnat;
    NATUPNPLib.IStaticPortMappingCollection mappings;

    public ServerExample()
    {
        InitializeComponent();

        //                           server local IP address
        mappings.Add(1300, "TCP", 1300, "192.168.0.120", true, "my server");
        //.... etc
       ..

when doing this I am able to connect from my office computer by providing port 1300, the WAN ip address of the server computer which is a different one. (that address can be found at: whatismyipaddress.com) THE COOL THING IS THAT I DO NOT HAVE TO DO ANY CONFIGURATION ON THE ROUTER!!!

2)-----------------------------------------------SCENARIO_2----------------------------------------------

know here comes my problem. The server happens to be on a office. that server is connected to router A, router A is connected to router B and router B is connected to the internet. In other this example is like the last example on the server with an extra router in between. and the client is somewhere else. we do not care how the network setup is on the client computer as long as it has internet access.

the way I used to solve this was by forwarding the packages from router x to the server computer and also port forwarding the traffic from port x of router B to the ip address of router A. when setting up that configuration I was able to establish a connection with the client when providing the WAN ip address of the server. (the ip address that is shown at: whatismyipaddress.com when retrieved from the network of the office).

so it will be nice if I can avoid doing all this configuration on the routers and do something similar to:

    public delegate void SomeDelegate(string parameters);

    NATUPNPLib.UPnPNATClass upnpnat;
    NATUPNPLib.IStaticPortMappingCollection mappings;

    public ServerExample()
    {
        InitializeComponent();

        //                           server local IP address
        mappings.Add(1300, "TCP", 1300, "192.168.150.141", true, "my server");
        mappings.Add(1300, "TCP", 1300, "another ip", true, "router's ip");

so I have been playing around with that and I have not been able to make it work. also it will be nice if I could find out if the server connection is like scenario 1 with c# or scenario 2 or maybe a different scenario so that the users setting up the server do not have to specify all that info. for example I am sure that limewire does something similar.


Edit:

here is scenario 1 illustrated:

在此输入图像描述

and here is scenario 2 illustrated

在此输入图像描述

The feature that you are using is called Universal Plug and Play (UPnP) which is basically a way for a device to tell another device how to communicate with it. What you are doing is telling a computer to tell the router what ports you want forwarded to it. Unfortunately UPnP is a non-routable protocol which means that it can't jump across routers. So the short answer is that doing what you are trying will not work with multiple routers without manually configuring the ones "farthest" away from the client.

It might be possible to do something with Internet Gateway Device protocol (IDG) but you'd have to research that more and the library you are targeting doesn't support that. Post this question to Server Fault and you might get a better answer. (Don't post your code, just explain the basics of your problem, that you're trying to have a machine register itself with a router using UPnP which is working but you have another router in front of that that you want to automatically forward, too.)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM