Java InetAddress Check isPrivateIP(InetAddress ip)

Here you can find the source of isPrivateIP(InetAddress ip)

Description

Determines if the supplied IP Address is a private non-routed IP Address.

License

Open Source License

Declaration

public static boolean isPrivateIP(InetAddress ip) 

Method Source Code

//package com.java2s;
//License from project: Open Source License 

import java.net.*;

public class Main {
    private static final byte IP6_PRIVATE_PREFIX = (byte) 0xfd;

    /**//from  w w w.j  a  v a2s. c  o m
     * Determines if the supplied IP Address is a private non-routed IP
     * Address. RFC 1918 dictates the private IP space for IPv4
     * <p/>
     * <pre>
     *    10/8        - 10.0.0.0 through 10.255.255.255 (class A)
     *    172.16/12   - 172.16.0.0 through 172.31.255.255 (class B)
     *    192.168/16  - 192.168.0.0 through 192.168.255.255 (class C)
     * </pre>
     * <p/>
     * For IPv6 there are unique local addresses in the FD00::/8 space
     * as well as a deprecated site local addresses in the FEC0::/10
     * space which will both result in this returning {@code true}.
     */
    public static boolean isPrivateIP(InetAddress ip) {
        if (ip.isSiteLocalAddress())
            return true;
        else if (ip instanceof Inet6Address)
            return ip.getAddress()[0] == IP6_PRIVATE_PREFIX;
        return false;
    }
}

Related

  1. isPortOpen(InetAddress address, int port)
  2. isPrivate(InetAddress addr)
  3. isPrivate(InetAddress adr)
  4. isPrivateIp(final InetAddress address)
  5. isPrivateIp(InetAddress addr)
  6. isPrivateNetworkAddress(final InetAddress inetAddress)
  7. isPrivateSubnet(InetAddress address)
  8. isPublic(InetAddress inetAddress)
  9. isPublicIp(InetAddress addr)