有两个CIDR块--我如何在Java中验证它们不是重叠的?我想出了解决办法,但我不确定:
下面的代码使用SubnetUtils。
void isSubnetOverlap(String cidr1, String cidr2) {
SubnetUtils.SubnetInfo subnetCidr = new SubnetUtils(cidr1).getInfo();
SubnetUtils.SubnetInfo clusterCidr = new SubnetUtils(cidr2).getInfo();
if (subnetCidr.isInRange(clusterCidr.getLowAddress())
|| subnetCidr.isInRange(clusterCidr.getHighAddress())
|| clusterCidr.isInRange(subnetCidr.getLowAddress())
|| clusterCidr.isInRange(subnetCidr.getHighAddress())) {
// e.g. throw some exception
}
}
发布于 2022-08-11 07:04:09
我编写了以下函数来确定网络重叠函数,如果网络重叠,则返回true。
public static boolean isNetworkOverlaped(String network1, String network2){
inet.ipaddr.IPAddressString ipaddress1 = new IPAddressString(network1);
inet.ipaddr.IPAddressString ipaddress2 = new IPAddressString(network2);
int ipaddress1Prefix = ipaddress1.getNetworkPrefixLength();
int ipaddress2Prefix = ipaddress2.getNetworkPrefixLength();
boolean isOverlapped = false;
if(ipaddress1Prefix < ipaddress2Prefix){
IPAddress firstIPAddress = ipaddress2.getAddress().iterator().next();
isOverlapped = ipaddress1.contains(firstIPAddress.toAddressString());
}else {
IPAddress firstIPAddress = ipaddress1.getAddress().iterator().next();
isOverlapped = ipaddress2.contains(firstIPAddress.toAddressString());
}
return isOverlapped;
}
发布于 2019-10-21 22:21:15
您的解决方案有效;您不需要检查高地址。我不建议为已经起作用的东西编写自己的解决方案,但将网络作为另一个网络的子网进行检查并不困难。
只需使用较大网络的网络掩码,看看较小的网络是否是较大网络的子集。
这里有一个java的例子..。
String[] c1 = s1.split("/");
String[] c2 = s2.split("/");
if (c1.length != 2 || c2.length != 2) {
throw new Exception("malformed cidr address" + " " + "(s1=" + s1
+ ", " + "s2=" + s2 + ")");
}
String[] a1 = c1[0].split("\\.");
String[] a2 = c2[0].split("\\.");
if (a1.length != 4 || a2.length != 4) {
throw new Exception("malformed cidr address" + " " + "(s1=" + s1
+ ", " + "s2=" + s2 + ")");
}
int m1, m2;
try {
m1 = Integer.parseInt(c1[1], 10);
m2 = Integer.parseInt(c2[1], 10);
} catch (NumberFormatException ne) {
throw new Exception("malformed cidr address" + " " + "(s1=" + s1
+ ", " + "s2=" + s2 + ")");
}
if ((m1 < 0) || (m1 > 32) || (m2 < 0) || (m2 > 32)) {
throw new Exception("malformed cidr address" + " " + "(s1=" + s1
+ ", " + "s2=" + s2 + ")");
}
int i1 = 0, i2 = 0;
for (int i = 0; i < 4; i++) {
try {
int x1 = Integer.parseInt(a1[i], 10);
int x2 = Integer.parseInt(a2[i], 10);
if ((x1 < 0) || (x1 > 255) || (x2 < 0) || (x2 > 255)) {
throw new Exception("malformed cidr address" + " " + "(s1=" + s1
+ ", " + "s2=" + s2 + ")");
}
i1 = i1 | (x1 << (24 - (i << 3)));
i2 = i2 | (x2 << (24 - (i << 3)));
} catch (NumberFormatException ne) {
throw new Exception("malformed cidr address" + " " + "(s1=" + s1
+ ", " + "s2=" + s2 + ")");
}
}
int l1 = (int) (((long)1 << (32 - m1)) - 1);
int l2 = (int) (((long)1 << (32 - m2)) - 1);
i1 = i1 & ~l1;
i2 = i2 & ~l2;
if ((i1 & ~l2) == i2 ||
(i2 & ~l1) == i1)
return true;
return false;
发布于 2022-09-10 02:04:09
IPAddress Java库可以使用上面答案中的代码或这里的代码来为您完成这一任务。免责声明:我是项目经理。
当CIDR块重叠时,其中一个必须包含另一个。与CIDR块没有部分重叠。
static boolean overlaps(String one, String two) {
IPAddressString addrOne = new IPAddressString(one);
IPAddressString addrTwo = new IPAddressString(two);
return addrOne.contains(addrTwo) || addrTwo.contains(addrOne);
}
https://stackoverflow.com/questions/58489158
复制相似问题