1

I want to know whether a given hostname (which can be FQDN also) is localhost. I didn't find any direct API in InetAddress which can tell me so. What is the best way to do that?

Edit: I think I didn't explain the problem clearly. I have an input like "abc.xyz.com". I need to validate that this string represents a host which is localhost.

keenUser
  • 1,279
  • 3
  • 16
  • 33
  • 4
    possible duplicate of [Recommended way to get hostname in Java](http://stackoverflow.com/questions/7348711/recommended-way-to-get-hostname-in-java) – singhakash Aug 21 '15 at 09:30
  • No it's not. I have edited my question for more clarification. Sorry for confusion. – keenUser Aug 21 '15 at 09:35

3 Answers3

2

getHostAddress outputs the IP address, so if the IP address resolves to 127.0.0.1 OR InetAddress.getLocalHost().getHostAddress() then its the local host

For example:

    // change 192.168.154.169 to ip of your machine to test
    String[] hosts = { "192.168.154.169", "localhost", "google.com" };

    for (String host : hosts) {
        InetAddress address = InetAddress.getByName(host);
        boolean isLocalhost = "127.0.0.1".equals(address.getHostAddress())
                || InetAddress.getLocalHost().getHostAddress().equals(address.getHostAddress());
        System.out.println("Hostname: " + host + ", is local host: " + isLocalhost);
    }

Output:

Hostname: 192.168.154.169, is local host: true

Hostname: localhost, is local host: true

Hostname: google.com, is local host: false

Conor Roche
  • 201
  • 1
  • 3
  • This didn't work as it is. Needed a small change: use **InetAddress.getLocalHost().getHostAddress()** instead of hardcoded **"127.0.0.1"**. Please edit the answer so that I can accept it. – keenUser Aug 21 '15 at 09:50
  • i have revised the answer above as suggested – Conor Roche Aug 21 '15 at 10:09
  • on my Ubuntu system the machine names resolves to 127.0.1.1 wheras localhost resolves to 127.0.0.1 so they are not always equal. localhost can also be ::1 (IPv6) – Rodney Aug 21 '15 at 10:30
0

The answer is not simple. An external URL (e.g. abc.xyz.com) may reference an IP that is a load balancer, HTTP gateway or similar. Internal routing may mean that some completely different machine handles the request. This machine may be a member of a cluster of several machines.

So in the general case, an external URL does not map uniquely to a single machine. There may be specific cases where it does, but they will be uncommon for a moderately busy website.

kiwiron
  • 1,677
  • 11
  • 17
  • By "abc.xyz.com" I actually meant the FQDN and not really an external URL. Anyway, the accepted answer is useful for my particular use-case. – keenUser Aug 21 '15 at 10:42
0

To find a given host is local or not requires us to get the IPaddress corresponding to the host and then check whether it belongs to any one of the network adapters in our system. The following code should help:

package snippet;

import java.net.InetAddress;
import java.net.NetworkInterface;
import java.net.SocketException;
import java.net.UnknownHostException;
import java.util.Enumeration;

public class InetAddressTest {

    public static void main(String[] args) throws UnknownHostException, SocketException {
        System.out.println(isLocalHost("localhost"));
        System.out.println(isLocalHost("mylocalhost.com"));
        System.out.println(isLocalHost("127.0.0.1"));
        System.out.println(isLocalHost("192.168.1.100"));
        System.out.println(isLocalHost("google.com"));
    }

    public static boolean isLocalHost(String name) throws UnknownHostException, SocketException {
        InetAddress[] addresses = InetAddress.getAllByName(name);
        Enumeration<NetworkInterface> networkInterfaces = NetworkInterface.getNetworkInterfaces();
        while (networkInterfaces.hasMoreElements()) {
            NetworkInterface networkInterface = (NetworkInterface) networkInterfaces.nextElement();
            if(!networkInterface.isUp())
                continue;
            Enumeration<InetAddress> inetAddresses = networkInterface.getInetAddresses();
            while (inetAddresses.hasMoreElements()) {
                InetAddress inetAddress = (InetAddress) inetAddresses.nextElement();
                if(addressMatch(addresses, inetAddress))
                    return true ;
            }

        }
        return false;
    }

    private static boolean addressMatch(InetAddress[] addresses, InetAddress inetAddress) {
        for (InetAddress address : addresses) {
            if(addrMatch(address, inetAddress))
                return true;
        }
        return false;
    }

    private static boolean addrMatch(InetAddress address, InetAddress inetAddress) {
        byte[] address2 = inetAddress.getAddress();
        byte[] address3 = address.getAddress();
        for(int i = 0; i < 4; i++)
            if(address2[i] != address3[i])
                return false ;
        return true;
    }

}

Surprisingly long.. InetAddress should have a helper method to do this.

Dakshinamurthy Karra
  • 5,353
  • 1
  • 17
  • 28