alanmcgovern / Mono.Nat

UPNP and NAT-PMP port forwarding for .NET
https://github.com/mono/Mono.Nat
MIT License
160 stars 156 forks source link

GetExternalIPAsync() does not return public IP sitting behind router #39

Closed tuk864 closed 4 weeks ago

tuk864 commented 4 months ago

Hello,

First of all i would like to thank you such an awesome library to deal with p2p problems. That being said, my current network topology is fairly simple - I have a router (Lets call it RouteB) in front of my ISP router(RouteA). When I run NatConsole while being connected to RouteB, it does not return a public IP for forwarding.

But when I connect to RouteA, it gives public IP without any hitch - is there any way to traverse in a mesh/layers of router?

Thank you again! This question maybe dumb but I am trying to figure out a way forward. image

alanmcgovern commented 4 weeks ago

Unfortunately not within the NAT-PMP or uPnP spec. The API you're looking at returns whatever the device uses as it's external IP address.

In your case it sounds like you're double-NATd, which means the router you call "routeb" is getting assigned an "external" address which is as actually just one of RouteA's internal addresses.

While you can implement your own "GetExternalIPAddress" by parsing data from sites like whatismyipaddress.com, it won't be much use from a port forwarding perspective.

If you connect to RouteB then you'll only be able to forward ports between RouteB and RouteA. You won't be able to forward ports automatically between RouteA and the actual internet.

Typically the way to solve this is to put your ISP provided router into "Bridge" mode. This disables all advanced networking functionality and passes through all connections to the device connected to the primary Ethernet port - in your case it would be RouteB. That device is then responsible for DHCP, port forwarding, etc, and then you sidestep all the complications of a double-NAT.

LMK if you need anything else or I've misunderstood the issue.