mirror of
https://gitee.com/chinabugotech/hutool.git
synced 2025-05-09 23:51:34 +08:00
修正cidr判定问题
This commit is contained in:
parent
1f2ca2e048
commit
ccc9109197
@ -702,7 +702,7 @@ public class NetUtil {
|
|||||||
String[] ips = StrUtil.splitToArray(ip, '.');
|
String[] ips = StrUtil.splitToArray(ip, '.');
|
||||||
int ipAddr = (Integer.parseInt(ips[0]) << 24) | (Integer.parseInt(ips[1]) << 16) | (Integer.parseInt(ips[2]) << 8) | Integer.parseInt(ips[3]);
|
int ipAddr = (Integer.parseInt(ips[0]) << 24) | (Integer.parseInt(ips[1]) << 16) | (Integer.parseInt(ips[2]) << 8) | Integer.parseInt(ips[3]);
|
||||||
int type = Integer.parseInt(cidr.replaceAll(".*/", ""));
|
int type = Integer.parseInt(cidr.replaceAll(".*/", ""));
|
||||||
int mask = 0xFFFFFFFF << (32 - type);
|
int mask = (int)((-1L << 32 - type) & 0XFFFFFFFF);
|
||||||
String cidrIp = cidr.replaceAll("/.*", "");
|
String cidrIp = cidr.replaceAll("/.*", "");
|
||||||
String[] cidrIps = cidrIp.split("\\.");
|
String[] cidrIps = cidrIp.split("\\.");
|
||||||
int cidrIpAddr = (Integer.parseInt(cidrIps[0]) << 24) | (Integer.parseInt(cidrIps[1]) << 16) | (Integer.parseInt(cidrIps[2]) << 8) | Integer.parseInt(cidrIps[3]);
|
int cidrIpAddr = (Integer.parseInt(cidrIps[0]) << 24) | (Integer.parseInt(cidrIps[1]) << 16) | (Integer.parseInt(cidrIps[2]) << 8) | Integer.parseInt(cidrIps[3]);
|
||||||
|
@ -101,4 +101,15 @@ public class NetUtilTest {
|
|||||||
Console.log(txt);
|
Console.log(txt);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void isInRangeTest(){
|
||||||
|
Assert.assertTrue(NetUtil.isInRange("114.114.114.114","0.0.0.0/0"));
|
||||||
|
Assert.assertTrue(NetUtil.isInRange("192.168.3.4","192.0.0.0/8"));
|
||||||
|
Assert.assertTrue(NetUtil.isInRange("192.168.3.4","192.168.0.0/16"));
|
||||||
|
Assert.assertTrue(NetUtil.isInRange("192.168.3.4","192.168.3.0/24"));
|
||||||
|
Assert.assertTrue(NetUtil.isInRange("192.168.3.4","192.168.3.4/32"));
|
||||||
|
Assert.assertFalse(NetUtil.isInRange("8.8.8.8","192.0.0.0/8"));
|
||||||
|
Assert.assertFalse(NetUtil.isInRange("114.114.114.114","192.168.3.4/32"));
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user