[英]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.