hrbrmstr/iptools

error in range_boundaries_to_cidr

Opened this issue · 3 comments

Thank you for this wonderful package.

I've run range_boundaries_to_cidr, but there were lots of errors.
When I was using it with ip addresses starts with 2xx.xxx.xxx.xxx, it returns 128.0.0./32 for all addresses.
You may try with ("210.98.224.0", "210.98.227.255"), for example.
I would really appreciate if I can use this package to figure out the ranges for those 2xx~~~~ addresses.
Thank you in advance.

Can you provide a minimum viable example (perhaps use the reprex package) and also the output of sessionInfo()

I've run the following on Ubuntu and macOS without fail:

library(iptools)

range_boundaries_to_cidr(
  ip_to_numeric("210.98.224.0"), 
  ip_to_numeric("210.98.227.255")
)
## [1] "210.98.224.0/22"

Dear hrbrmstr,
Thank you for your reply.
Here is what I got.

library(iptools)

range_boundaries_to_cidr(

  • ip_to_numeric("210.98.224.0"),
  • ip_to_numeric("210.98.227.255")
  • )
    [1] "128.0.0.0/32"

sessionInfo()
R version 3.5.3 (2019-03-11)
Platform: x86_64-w64-mingw32/x64 (64-bit)
Running under: Windows >= 8 x64 (build 9200)

Matrix products: default

locale:
[1] LC_COLLATE=English_United States.1252
[2] LC_CTYPE=English_United States.1252
[3] LC_MONETARY=English_United States.1252
[4] LC_NUMERIC=C
[5] LC_TIME=English_United States.1252

attached base packages:
[1] stats graphics grDevices utils datasets methods base

other attached packages:
[1] readstata13_0.9.2 foreign_0.8-71 iptools_0.6.1
[4] dplyr_0.8.0.1 gtools_3.8.1 jsonlite_1.6
[7] stringr_1.4.0

loaded via a namespace (and not attached):
[1] tools_3.5.3 R6_2.4.0 tidyselect_0.2.5
[4] compiler_3.5.3 cli_1.1.0 triebeard_0.3.0
[7] readr_1.3.1 digest_0.6.18 pkgconfig_2.0.2
[10] rlang_0.3.3 rstudioapi_0.10 magrittr_1.5
[13] Rcpp_1.0.1 stringi_1.4.3 AsioHeaders_1.12.1-1
[16] crayon_1.3.4 hms_0.4.2 pillar_1.3.1
[19] glue_1.3.1 purrr_0.3.2 assertthat_0.2.1
[22] tibble_2.1.1 memoise_1.1.0

image
In this table, the first column is start ip, the second one is end ip, and the last column is cider I got from range_boundaries_to_cidr.
Again, I really appreciate your help.

I can confirm this bug occurs on Windows 10/R 3.5.3 64-bit. At a guess, I'd say the problem is in iptools.cpp, where a couple of vars are defined as long int. This type is 64 bits on Unix-alikes, but 32 bits on Win32.

iptools/src/iptools.cpp

Lines 76 to 77 in a65fc5e

long int mask = 1;
long int new_ip;

Note that the output is correct for addresses that are smaller than 127.255.255.255:

> range_boundaries_to_cidr(ip_to_numeric("127.255.255.0"), ip_to_numeric("127.255.255.255"))
[1] "127.255.255.0/24"
> range_boundaries_to_cidr(ip_to_numeric("128.0.0.0"), ip_to_numeric("128.0.0.255"))
[1] "128.0.0.0/32"