how to upnp forward ports via two levels of routers
Asked Answered
R

3

8

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);
Rhodian answered 4/12, 2012 at 21:29 Comment(0)
S
5

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.

Stick answered 20/4, 2013 at 14:10 Comment(7)
this is not an option... i am developing a software, i cant tell my users to do itRhodian
maybe you can help me with this? #16093247Rhodian
How are you sure that your users have 2 routers one after each other ?Stick
This specific case only occurs in an advanced users setups. All softwares including BitTorrent are not capable of this. If it was possible it would be a huge security issue.Stick
i have a cable modem with built in router, and another simple router connected to it, my computer is connected to the simple router and still, uTorrent works somehow... many people in my country have this kind of modem and they prefer their own routerRhodian
@Dima, if you're not visible from outside world, you can still communicate with those peers whose ports are open (as is the case with standard client-server scenario). This decreases the number of peers you can communicate with though, decreasing bandwidth.Remediosremedy
@Dima, I think you've misunderstood me. In my previous comment I was referring to your comment above mine, where you expressed that you're not sure how uTorrent can work for peers behind two routers. That is, if your ports are closed, uTorrent can still work but it can only connect to peers whose ports are open, which results in reduced number of peers, which then results in reduced bandwidth. For the main question, I'll create a new answer as it won't fit in a comment.Remediosremedy
R
5

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).

Remediosremedy answered 27/10, 2013 at 17:37 Comment(0)
S
0

It's a little bit more complicated than you think. I'm trying to do the same thing. I have access to the modem connected to the ISP (I know this because I can access the router from my notebook). But I'm behind a router (my gateway). In order to make it work you will need to make the mapping from the modem to the router of the port that you need first. Then another from the router to your PC.

There are two issues with this.

1- The thing is that UPNP software allways ask the mapping to the gateway of yor current pc (your router). I can't find software that let you customize the gateway you want to ask the UPnP configuration to be run of. (Usually when router is 192.168.0.1, modem is 192.168.1.1 or 192.168.2.##).

2- For mapping the router, you need to know wich is the designated adress of your router in your modems network. This way the UPnP will configure the port to foward directly to it.

So... it's possible but not with current software arround. PS: People who ask this kind of things in stackoverflow don't need answers saying. "you can go to the modem port fowarding menu and do it manually".

Steviestevy answered 16/8, 2022 at 18:32 Comment(1)
I need the same configuration you are seeking for a company notebook that I need to control through SSH when connecting to different unknown networks.Steviestevy

© 2022 - 2024 — McMap. All rights reserved.