Check whether the ipAddress is in private range

I use this:

public boolean isPrivateIP(String ipAddress) {
        boolean isValid = false;

        if (ipAddress != null && !ipAddress.isEmpty()) {
            String[] ip = ipAddress.split("\\.");
            short[] ipNumber = new short[] { 
                    Short.parseShort(ip[0]), 
                    Short.parseShort(ip[1]), 
                    Short.parseShort(ip[2]),
                    Short.parseShort(ip[3])
                };

            if (ipNumber[0] == 10) { // Class A
                isValid = true;
            } else if (ipNumber[0] == 172 && (ipNumber[1] >= 16 && ipNumber[1] <= 31)) { // Class B
                isValid = true;
            } else if (ipNumber[0] == 192 && ipNumber[1] == 168) { // Class C
                isValid = true;
            }
        }

        return isValid;
    }

This is a quick hack I generated to test my own address.

import java.net.InetAddress;
import java.net.UnknownHostException;

public class LocalAddress {

    public static void main(String[] args) {
        InetAddress address = null;
        try {
            address = InetAddress.getLocalHost();
        } catch (UnknownHostException e) {
             e.printStackTrace();
        }
        if (address.isSiteLocalAddress()) {
            System.out.println("Site Local Address: " + address.getHostAddress());
        } else {
            System.out.println("Routeable Address: " + address.getHostAddress());
        }
    }

}

EDIT: This code has not been tested for the link local addresses, localhost, or address blocks reserved for documentation. The first two cases have methods that return them. The last is not referenced in the documentation of the class.


The correct method is InetAddress.isSiteLocalAddress().

Utility routine to check if the InetAddress is a site local address.

Returns: a boolean indicating if the InetAddress is a site local address; or false if address is not a site local unicast address.


First of all, Private networks can use IPv4 addresses anywhere in the following ranges:

  • a) 192.168.0.0 - 192.168.255.255 (65,536 IP addresses)
  • b) 172.16.0.0 - 172.31.255.255 (1,048,576 IP addresses)
  • c) 10.0.0.0 - 10.255.255.255 (16,777,216 IP addresses)

As we can see from method isSiteLocalAddress in Inet4Address.java :

 public boolean isSiteLocalAddress() {
    // refer to RFC 1918
    // 10/8 prefix
    // 172.16/12 prefix
    // 192.168/16 prefix
    int address = holder().getAddress();
    return (((address >>> 24) & 0xFF) == 10)
        || ((((address >>> 24) & 0xFF) == 172)
            && (((address >>> 16) & 0xF0) == 16))
        || ((((address >>> 24) & 0xFF) == 192)
            && (((address >>> 16) & 0xFF) == 168));
}

So case b) 172.16.0.0 - 172.31.255.255 (1,048,576 IP addresses) is not satisfied. But you can easily write you own version of how to tell whether a address is private address.Here is my version:

import com.google.common.net.InetAddresses;

private static boolean isPrivateV4Address(String ip) {
    int address = InetAddresses.coerceToInteger(InetAddresses.forString(ip));
    return (((address >>> 24) & 0xFF) == 10)
            || ((((address >>> 24) & 0xFF) == 172) 
              && ((address >>> 16) & 0xFF) >= 16 
              && ((address >>> 16) & 0xFF) <= 31)
            || ((((address >>> 24) & 0xFF) == 192) 
              && (((address >>> 16) & 0xFF) == 168));
}

Tags:

Java