Recover External IP without third party services

Asked

Viewed 922 times

2

I have a WPF application. I need to know the external IP of the machine running this application.

I found some tutorials that teach to do this through the method DownloadString() class WebClient consuming some services such as dyndns.org, icanhazip.com, finally several services.

It would be something like that:

string meuIp = new WebClient().DownloadString("http://icanhazip.com");  

To use such services I need to implement the query in at least two or three to ensure a contingency in case some service stops working or is discontinued.

Does anyone know if it is possible to do this natively with . Net Framework without using third-party services?

  • What kind of application are you using? could put like this trying to do.

2 answers

8


[...] you can do this natively with . Net Framework without using third party services?

No, it is not possible. Your machine has access to local IP only, provided by the internal DNS service or set manually.

The external IP is, in most of the times, the one provided by your provider for you to be accessible via NAT (Network address Translation), as in the image below:

NAT categorization according to RFC

NAT categorization According to RFC, Wikipedia

In this case you will always need an external service that tells you, from his point of view, with which IP you are connecting.

  • Is that what this is? Every time a package goes out to the Internet from my router, it comes out loading my external IP (IP of my router, assigned by my Internet service), right? If I didn’t put this information in the package, the destination host would never know where to return the answer. So a service that reveals my external IP actually just returns me back information that my own router sent to it. The information from my external IP is stored in my router. The question is: how to recover it without sending packets out of my network?

  • @Leonardo the model you described is correct if you assume that there is no other NAT layer between your router and the internet, which is often not true; your router can receive an IP from an intermediate layer, such as via CGNAT (Carrier Grade Network Address Translation).

  • you are telling me then that my external IP may not be in possession of my router? I really have difficulty understanding this. Why, even in this case, when I make a request out, does the ISP have to know where it came from and the IP that identifies me for the ISP might not be my external IP? Is that it? If so, in the case of CGNAT, does the ISP provide me with a private IP? And, being the CGNAT as an ordinary NAT, in the domain of the ISP, it can happen that two networks on the Internet have the same public IP, no?

  • @Leonardo In the case of the most common CGNAT, (NAT444 - Private to Private to Public NAT) there is a translation of network addresses between the internal network and the intermediary (mediated by the gateway of your router) and another between the intermediate network and the internet (mediated by the ISP). Addresses are unique per layer. Reference: https://www.a10networks.com/blog/carrier-grade-nat/

0

You can use the IP Public Knowledge.

install the Nuget package:

Install-Package Ippublicknowledge

An example of use would be this:

var ip = IPK.GetMyPublicIp();
var IPinfo = IPK.GetIpInfo(ip);
    
Console.WriteLine("IP Publico: " + IPinfo.IP);
Console.WriteLine("ISP: " + IPinfo.isp);
Console.WriteLine("País: " + IPinfo.country);

Another way, as you said yourself, would be to use some external site, for example:

public static void Main(string[] args)
{
    string externalip = new WebClient().DownloadString("https://api.ipify.org");            
    Console.WriteLine(externalip);
}

In that case, this answer shows more examples to ensure contingency.

If you would like other examples, this answer has several ways to do this, including one of how to create your own Http Server for this.

  • 1

    Does this library internally not use these sites? Just to be sure. Thanks for the answer

  • @Murilo Use yes. Her code you can see in this code.

  • @Murilo Onosendai’s answer better explains why you have to use some service.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.