简体   繁体   English

Java IPAddress 库和非零 CIDR

[英]Java IPAddress library and non-zero CIDRs

I am using Sean Foley's Java library IPAddress and I'm running into an issue generating IPs from a CIDR.我正在使用 Sean Foley 的 Java 库 IPAddress,我遇到了从 CIDR 生成 IP 的问题。 IPAddress does not consider 10.90.6.14/30 a CIDR and addr.isMultiple() returns false. IPAddress 不认为 10.90.6.14/30 是 CIDR,并且 addr.isMultiple() 返回 false。

Our legacy code (Golang) and a couple of online CIDR calculators I've tried return:我们的旧代码(Golang)和我尝试过的几个在线 CIDR 计算器返回:

10.90.6.12 10.90.6.13 10.90.6.14 10.90.6.15 10.90.6.12 10.90.6.13 10.90.6.14 10.90.6.15

Can anyone advise me on how to achieve this result using the IPAddress library?谁能告诉我如何使用 IPAddress 库来实现这个结果?

The author of the IPAddress library, Sean Foley, was kind enough to post this answer on the github site (thank you Sean:): IPAddress 库的作者 Sean Foley 很乐意在 github 网站上发布这个答案(谢谢 Sean:):

In summary, 10.90.6.14/30 is the same as 10.90.6.14 because the host is not zero.综上所述,10.90.6.14/30 和 10.90.6.14 是一样的,因为主机不为零。 10.90.6.12/30 is the subnet of 4 addresses because the host is zero. 10.90.6.12/30 是 4 个地址的子网,因为主机为零。 If you want to convert to the subnet, then use toPrefixBlock.如果要转换为子网,则使用 toPrefixBlock。

list(new IPAddressString("10.90.6.12/30").getAddress()); list(new IPAddressString("10.90.6.12/30").getAddress());
list(new IPAddressString("10.90.6.14/30").getAddress().toPrefixBlock()); list(new IPAddressString("10.90.6.14/30").getAddress().toPrefixBlock());

static void list(IPAddress addr) { System.out.println(addr + " has count " + addr.getCount()); static void list(IPAddress addr) { System.out.println(addr + " has count " + addr.getCount()); for(IPAddress addr2: addr.getIterable()) { System.out.println(addr2); for(IPAddress addr2: addr.getIterable()) { System.out.println(addr2); } System.out.println(); } System.out.println(); } Output: Output:

10.90.6.12/30 has count 4 10.90.6.12/30 10.90.6.13/30 10.90.6.14/30 10.90.6.15/30 10.90.6.12/30 有计数 4 10.90.6.12/30 10.90.6.13/30 10.90.6.14/30 10.90.6.15/30

10.90.6.12/30 has count 4 10.90.6.12/30 10.90.6.13/30 10.90.6.14/30 10.90.6.15/30 10.90.6.12/30 有计数 4 10.90.6.12/30 10.90.6.13/30 10.90.6.14/30 10.90.6.15/30

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 数组中非零值的平均值(Java) - Average of non-zero values in array (Java) 使用MPAndroid图表库时以非零退出值1结束java.exe'' - java.exe'' finished with non-zero exit value 1 when using MPAndroid chart library 访问 Commons Math 库中稀疏矩阵的非零元素 - Access the non-zero elements of a sparse matrix in the Commons Math library 不断收到错误Gradle Build失败并发生以下异常:添加库后,java.exe'以非零退出值2结束 - keep getting error Gradle Build failed with an exception : java.exe' finished with non-zero exit value 2 after adding library java.exe以非零退出值1结束 - java.exe finished with non-zero exit value 1 android studio java用非零退出值2完成 - android studio java finished with non-zero exit value 2 java中允许空或非零正数的正则表达式 - Regular expression to allow null or positive non-zero numbers in java Java列表,按3组统计对象的非零属性 - Java List, Counting non-zero attributes of Object, by group of 3 gradle 处理命令 java 以非零退出值 1 完成 - gradle process command java finished with non-zero exit value 1 compileRetrolambdaRelease Java完成,退出值非零1 - compileRetrolambdaRelease java finished with non-zero exit value 1
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM