Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to upnp forward ports via two levels of routers

i am using the java sbbi library to forward ports. but my router is conected to another router, so the external ip of the first router is a ip that was given by the second router dhcp. is there a way to forward the ports on the second router as well? here is my code:

InternetGatewayDevice[] devices=InternetGatewayDevice.getDevices( 1000 );
System.out.println("Found "+devices.length+" Devices");
InternetGatewayDevice dev = devices[ 0 ];
System.out.println(dev);
System.out.println( "External IP = " + dev.getExternalIPAddress() );

System.out.println( dev.addPortMapping( "Streamternet", "TCP", null, 1333,
                                        "192.168.0.105", 8888, 0 ) );

System.out.println( "waiting for connection" );
HTTPServer.main(null);
like image 503
Dima Avatar asked Dec 04 '12 21:12

Dima


People also ask

How do I set up port forwarding on a second router?

I log into router 1 and go to the DMZ settings and put the WAN IP address of the 2nd router there. Now router 1 is out of the way for port forwarding and triggering as all incoming connections are now being forwarded to router 2. Now in router 2 I can setup my port forwarding & triggering rules just like normal. So here is a quick step by step.

Can I port forward using UPnP?

If your router is UPNP compatible you can easily port forward using UPNP by mapping certain ports (TCP or UDP) with the following program. Can you use this method for other programs like discord / skype / whatever you want? Yes. This item has been added to your Favorites.

How to set up port forwarding in 2nd layer of Nat?

The first layer of NAT has the IP range 192.168.1.1-255, and then 2nd layer has IP range 192.168.50.1-255. On a machine inside the 2nd layer of NAT, just setup port forwarding with upnpc like normal. $ upnpc -a 192.168.50.123 6667 6667 tcp ... $ upnpc -l # this will confirm the rule is in place ...

How do I use UPnP port mapper?

UPnP Port Mapper communicates with your router with the UPnP protocol, so you’ll also need a router with UPnP enabled to use this application. If UPnP is disabled on your network’s router, this program can’t do anything. After downloading UPnP Port Mapper, double-click the .jar file to launch it.


2 Answers

As far as i know it's not possible with your current setup because you cannot access your router's gateway directly.

There are 2 solutions i can think of.

  1. Disable your router DHCP server so that he and you will all get your IPs from the second router and than you'll all have the same subnet and you could access each one of the routers.
  2. Have another PC that is connected to the second router network and make him a server that listens to your commands and this server will execute the UPNP tasks for you. (This will probably need a manual setup of port forwarding first)

Hope i could help.

like image 199
Danpe Avatar answered Sep 23 '22 07:09

Danpe


As @Danpe said in his answer, UPnP won't let you open ports when you're behind multiple NATs.

As I'm also working on a P2P software and from what I've gathered, here is what one can try to connect two peers:

  1. Connect using TCP or UDP hole punching.
  2. If above doesn't work, one of the routers on either side uses a Symmetric NAT. Try UPnP or NAT-PNP.
  3. If above doesn't work, app is either behind a router that doesn't support UPnP, NAT-PNP... or you're behind multiple levels of NATs. Now you can either:

    1. Communicate with the user of the app that she needs to open ports on those routers. This is, as you've mentioned in comments, an unfortunate solution as it assumes advanced technical knowledge from the user. But it does seem to be what some companies actually do. See here for example.

    2. Use a relay server such as a one using TURN protocol through which users with closed ports will communicate. This relay server may be one or more of peers whose ports are open (as is the case with uTorrent), or it can be your server, or a combination of the two: if there are peers with open ports, use those, otherwise use your own server (this is the case with Skype).

like image 22
Peter Jankuliak Avatar answered Sep 19 '22 07:09

Peter Jankuliak