Check whether the ipAddress is in private range

前端 未结 4 487
执笔经年
执笔经年 2021-02-07 06:43

How would I check to see whether the ip address is in private category ?

    if(isPrivateIPAddress(ipAddress)) {
                


        
相关标签:
4条回答
  • 2021-02-07 07:00

    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.

    0 讨论(0)
  • 2021-02-07 07:07

    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));
    }
    
    0 讨论(0)
  • 2021-02-07 07:08

    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;
        }
    
    0 讨论(0)
  • 2021-02-07 07:24

    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.

    0 讨论(0)
提交回复
热议问题