Sean Foley的IPAddress库和包含在CIDR内部。

huangapple go评论79阅读模式
英文:

Sean Foley IPAddress library and containment inside CIDR

问题

此问题类似,尽管不完全相同,我有困难理解为什么Sean Foley的IPAddress库认为IP地址不包含在CIDR内。

根据我所了解,CIDR/掩码“20.108.106.0/22”应包含范围从20.108.104.0到20.108.107.255的地址。我使用的这个范围计算器这里建议如此。

鉴于此,我认为地址20.108.106.72应包含在CIDR范围20.108.106.0/22内。这个CIDR范围检查器似乎也同意这一点。

然而,尝试使用IPAddress库的一些示例代码似乎不符合这一点。这段代码是Kotlin的,但应该与Java的工作方式相同:

@Test
fun testCidr() {
    val cidr = IPAddressString("20.108.106.0/22").getAddress()
    val ip = IPAddressString("20.108.106.72").getAddress()
    println("$ip is inside $cidr = ${cidr.contains(ip)}")
}
输出:
20.108.106.72 is inside 20.108.106.0/22 = false

我已经阅读了附加的问题,但我认为我的CIDR范围不是非零主机(例如20.108.106.72),因为最后一部分是。我使用过的其他范围检查工具似乎都表明它应该起作用。我确信这不是一个bug,而必定是我理解上的一些误解。我试图编写能够处理多种形式的IP地址输入的代码,例如IPv6、单个IP地址、CIDR范围,这就是我想要使用这种库而不需要首先解析或理解地址的原因。我看到当我像这样指定我的范围时它起作用:“20.108.106.0/23”,但我仍然不明白为什么会这样。谢谢。

英文:

Similar to this question although not quite the same, I'm having trouble understanding why the Sean Foley IPAddress library doesn't think that an IP address is contained inside a CIDR.

From what I've read, I thought that the CIDR/mask of "20.108.106.0/22" encompasses addresses in the range from 20.108.104.0 to 20.108.107.255. This range calculator I used here suggests it does.

Given that, I believe that the address 20.108.106.72 should be contained inside the CIDR range 20.108.106.0/22. This CIDR range checker seems to agree that it should as well.

Trying out using the IPAddress library with some sample code doesn't seem to match this though. This code is Kotlin but should work the same as the Java:

@Test
fun testCidr() {
    val cidr = IPAddressString("20.108.106.0/22").getAddress()
    val ip = IPAddressString("20.108.106.72").getAddress()
    println("$ip is inside $cidr = ${cidr.contains(ip)}")
}
Output:
20.108.106.72 is inside 20.108.106.0/22 = false

I've read the attached question, but I don't think my CIDR range is a non-zero host (eg. 20.108.106.72) because the last section is zero. and the other range checking tools I've used seem to suggest it should work. I'm sure this isn't a bug, but must be some misunderstanding on my part. I'm trying to write code that will cope with any input of IP addresses in several forms eg. IPv6, single IP addresses, CIDR ranges which is why I was wanting to use a library like this one without needing to parse or understand the address first. I see that it works when I specify my range like "20.108.106.0/23" but again I don't understand why that would be. Thanks

答案1

得分: 1

我认为答案实际上与链接的问题相同。
引用相关部分:

> 总之,10.90.6.14/30与10.90.6.14相同,因为主机不是零。 10.90.6.12/30是子网,有4个地址,因为主机是零。如果要转换为子网,则使用toPrefixBlock。

这基本上是说,如果地址是由您的CIDR掩码给出的子网中的第一个地址,则主机为零。

这意味着对于您的示例:20.108.104.0/22将是所需的CIDR范围。

再次阅读链接的问题,似乎您可以在示例代码中使用toPrefixBlock()方法来获得所需的结果,而无需实际更改提供的CIDR范围。

@Test
fun testCidr() {
    val cidr = IPAddressString("20.108.106.0/22").getAddress().toPrefixBlock()
    val ip = IPAddressString("20.108.106.72").getAddress()
    println("$ip is inside $cidr = ${cidr.contains(ip)}")
}

顺便说一下,您的20.108.106.0/23范围有效,因为20.108.106.0是该子网中的第一个地址。

英文:

I believe the answer is actually the same as in the linked question.
To quote the relevant part:

> In summary, 10.90.6.14/30 is the same as 10.90.6.14 because the host is not zero. 10.90.6.12/30 is the subnet of 4 addresses because the host is zero. If you want to convert to the subnet, then use toPrefixBlock.

Which basically says a host is zero if the address is the first address in the subnet given by your CIDR mask.

Which means for your example: 20.108.104.0/22 would be the needed CIDR range.

Reading the linked question again, it seems like you can use the toPrefixBlock() method in your example code to get the wanted result without actually changing the supplied CIDR range.

@Test
fun testCidr() {
    val cidr = IPAddressString("20.108.106.0/22").getAddress().toPrefixBlock()
    val ip = IPAddressString("20.108.106.72").getAddress()
    println("$ip is inside $cidr = ${cidr.contains(ip)}")
}

Incidentally your 20.108.106.0/23 range works since 20.108.106.0 is the first address in that subnet.

huangapple
  • 本文由 发表于 2023年7月20日 17:54:31
  • 转载请务必保留本文链接:https://go.coder-hub.com/76728680.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定