Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Unexpected result with IPv4Address.toSequentialRange method #39

Closed
martinhansen opened this issue Apr 28, 2020 · 2 comments
Closed

Unexpected result with IPv4Address.toSequentialRange method #39

martinhansen opened this issue Apr 28, 2020 · 2 comments

Comments

@martinhansen
Copy link

martinhansen commented Apr 28, 2020

The following ip: 127.0.0.158 (2130706590 in Integer form), with a subnet mask of 24.

When i create a range of ips with toSequentialRange() i don't get a complete range of ips. I get a IPv4AddressSeqRange with 1 IP inside it. (127.0.0.158 -> 127.0.0.158), when i expect it to be (127.0.0.0 -> 127.0.0.250), calculated with: http://jodies.de/ipcalc?host=130.225.157.76&mask1=24&mask2=

When i create the ip-range with toZeroHost and toMaxHost the range comes out as expected.

Using scala, jdk11, and v5.1.0 or 5.0.2 of this lib.

Code to reproduce:

    val ip = 2130706590
    val mask = 24
    val ipAdd = new IPv4Address(ip, mask)

    println(ipAdd)

    val ipRange = ipAdd.toSequentialRange
    val ipRange2 = ipAdd.toZeroHost.toSequentialRange(ipAdd.toMaxHost)

    if (ipRange != ipRange2) {
      println(s"$ipRange not the same as $ipRange2")
    }

same thing happens with 194.105.145.69/31 where the range is still just 1 IP, but i expect it to be 194.105.145.68 -> 194.105.145.69. (Again, using toZeroHost and toMaxHost works)

Is this a bug, or is my expectation of toSequentialRange wrong?

@seancfoley
Copy link
Owner

Hello,
In this library, when parsing a string with a prefix length, or when constructing an address with a prefix length directly like in your example, you will get a subnet if the host is zero, otherwise it will be a single address. In this example, 127.0.0.158/24 is not a subnet, it is just a single address, because the last segment is the host, and that last segment is 158, not 0. When you convert to IPSequentialRange, it will remain just a single address.

To get the subnet is not difficult, just call toPrefixBlock on your address object.

val ip = 2130706590
val mask = 24
val ipAdd = new IPv4Address(ip, mask).toPrefixBlock
val ipAdd2 = new IPv4Address(Array(127, 0, 0, 158).map(_.toByte), mask).toPrefixBlock
val ipAdd3 = new IPv4Address(Array(127, 0, 0, 0).map(_.toByte), mask)

println(ipAdd)
println(ipAdd2)
println(ipAdd3)

println(ipAdd.getCount) // subnet size
println(ipAdd2.getCount)
println(ipAdd3.getCount)

val ipRange = ipAdd.toSequentialRange
val ipRange2 = ipAdd.toZeroHost.toSequentialRange(ipAdd.toMaxHost)

if (ipRange != ipRange2) {
  println(s"$ipRange not the same as $ipRange2")
} else {
  println(s"$ipRange is the same as $ipRange2")
}

More details here: https://github.com/seancfoley/IPAddress/wiki/Code-Examples#parse-prefixed-as-subnet-or-ip-address

@martinhansen
Copy link
Author

martinhansen commented Apr 29, 2020

That makes sense. Thank you for your explanation. I was confused about it, because the toString method writes out the IP Address like i was a ip/cidr: 127.0.0.158/24, and so i assumed it would be the subnet.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants