java 检查ipAddress是否在私有范围内

mspsb9vt  于 2023-02-11  发布在  Java
关注(0)|答案(5)|浏览(125)

我该如何检查该ip地址是否属于私有类别?

if(isPrivateIPAddress(ipAddress)) {
        //do something
    }

如有任何建议,我们将不胜感激。

    • 更新的答案**
private static boolean isPrivateIPAddress(String ipAddress) {

            InetAddress ia = null;

            try {
                InetAddress ad = InetAddress.getByName(ipAddress);
                byte[] ip = ad.getAddress();
                ia = InetAddress.getByAddress(ip);
            } catch (UnknownHostException e) {
                e.printStackTrace();
                return false;
            }

            return ia.isSiteLocalAddress();
        }

我写了这个方法,它对我来说工作得很好。但是有没有什么情况下这个方法不起作用呢?我只是想确保它对每一种情况都起作用。

gopyfrb3

gopyfrb31#

正确的方法是InetAddress.isSiteLocalAddress()。
检查InetAddress是否为站点本地地址的实用程序例程。
返回:一个布尔值,指示InetAddress是否是站点本地地址;或者如果地址不是站点本地单播地址则为假。

kzmpq1sx

kzmpq1sx2#

首先,专用网络可以使用以下范围内的任何IPv4地址:

  • a) 192.168.0.0-192.168.255.255(65,536个IP地址)
  • B) 172.16.0.0-172.31.255.255(1 048 576个IP地址)
  • c) 10.0.0.0-10.255.255.255(16 777 216个IP地址)

从www.example.com中的isSiteLocalAddress方法可以看出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));
}

因此,案例B)172.16.0.0-172.31.255.255(1,048,576个IP地址)并不令人满意。但你可以很容易地编写自己的版本来判断一个地址是否是私有地址。以下是我的版本:

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));
}
2hh7jdfx

2hh7jdfx3#

这是一个快速黑客我生成测试我自己的地址.

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());
        }
    }

}

编辑:这段代码还没有针对链接本地地址、localhost或为文档保留的地址块进行测试。前两种情况有返回它们的方法。最后一种情况在类的文档中没有引用。

gdrx4gfi

gdrx4gfi4#

我用这个:

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;
    }
c9qzyr3d

c9qzyr3d5#

范围为

// 10.x.x.x/8

    // 172.16.x.x/12

    //192.168.x.x/16

因此我们可以使用以下代码:

public static Boolean isIntranetIP(String IP) {
    byte[] addr = IPAddressUtil.textToNumericFormatV4(IP);
    return isIntranetIPByte(addr);
}

public static Boolean isIntranetIPByte(byte[] addr) {
    final byte B0 = addr[0];
    final byte B1 = addr[1];
    // 10.x.x.x/8
    final byte section_1 = 0x0A;
    // 172.16.x.x/12
    final byte section_2 = (byte) 0xAC;
    final byte section_3 = (byte) 0x10;
    final byte section_4 = (byte) 0x1F;
    //192.168.x.x/16
    final byte section_5 = (byte) 0xc0;
    final byte section_6 = (byte) 0xa8;

    switch (B0) {
        case section_1:
            return true;
        case section_2:
            if (B1 >= section_3 && B1 <= section_4) {
                return true;
            }
        case section_5:
            switch (B1) {
                case section_6:
                    return true;
            }
        default:
            return false;
    }
}

相关问题