How to programmatically find the external IP address of a device without using external host?
Asked Answered
K

1

9

When I read the IP address of the device I always get the local IP address.

I use the following code snippet to do that.

public String getIpAddress() {
   try {
         for (Enumeration<NetworkInterface> en = NetworkInterface.getNetworkInterfaces(); en.hasMoreElements();) {
                NetworkInterface intf = en.nextElement();
             for (Enumeration<InetAddress> enumIpAddr = intf.getInetAddresses(); enumIpAddr.hasMoreElements();) {
                    InetAddress inetAddress = enumIpAddr.nextElement();
                    if (!inetAddress.isLoopbackAddress()) {
                        String ip = Formatter.formatIpAddress(inetAddress.hashCode());
                        Log.d("VPNConnected",ip);
                        return ip;
                    }
                }
            }

        } catch (Exception ex) {
            Log.d("exception", ex.toString());
        }
        return "EMPTY";
    }

But I need to read the external IP address without using any external host or web apis such as http://jsonip.com

Kowtko answered 17/7, 2014 at 12:42 Comment(8)
I am not aware that this is possible, for any environment, let alone Android. You have no idea how many NATs, firewalls, proxies, etc. lie between you and the outside world.Bouldin
@Bouldin Could we use something like traceroute's logic and look for the first "real world" IP ?Kowtko
To use traceroute, you need an external host that you are trying to trace the route to. Then, you would need your own algorithms for determining what a "real world IP" is.Bouldin
@Bouldin Then we will have to look for alternative solutions.Kowtko
@Bouldin FYI: We have a SDN solution that can prevent any domain based on the demand.That is why I don't want to include any external host in this processKowtko
@Bouldin : My first ip-address would be the ip-address that NAT disguises for sure.so could we use nat-traversal to find out the ip-address and that's enough for me ?Kowtko
@Bouldin I found so many reverse NAT lookup libraries in java but there is no example of how to use that in android and the link to those are as followsKowtko
jstun.javawi.de sourceforge.net/projects/pjsip-jni pjsip.org/pjnath/docs/html frozenmountain.com/products/icelink code.google.com/p/ice4jKowtko
T
0

Most replies are totally correct. The best thing would be to have an external service you connect to and this service replies with your public IP address. However, there are reasons why you would not want to connect to an external service (1) you simply do not want to run your own or pay for some 3rd party service, and (2) your application should run in "stealth" mode and not just contact some service on the internet every now and then.

What you could do however is to rely on a 100% pure Java traceroute implementation such as the one presented here: http://people.dsv.su.se/~miko1432/ip/ip1/4.3/doc/_traceroute_8java_source.html

By hooking into the traceroute implementation you could just ping one host after the other on your path to the internet. Basically this way you could just "ping" arbitrary public IP addresses and see the route that is taken to the destination.

If you look at the trace you will see private (non-routed) IP addresses such as those in the networks 192.168.0.0/16 and the 10.0.0.0/8. You can safely assume that those are private addresses and the first public IP address on your route is your public IP address.

This should also work for scenarios where the client device is assigned a public IP address such as it is the case with some mobile operators or academic networks.

Tyronetyrosinase answered 6/10, 2014 at 19:53 Comment(1)
The reason I don't want to get ip-address using web api is takes considerable amount of time.Can you give me a code snippet that does it ?Kowtko

© 2022 - 2024 — McMap. All rights reserved.