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

validate_range and range_boundaries issue #34

Open
AristarXXX opened this issue Nov 27, 2016 · 0 comments
Open

validate_range and range_boundaries issue #34

AristarXXX opened this issue Nov 27, 2016 · 0 comments
Assignees
Labels

Comments

@AristarXXX
Copy link

AristarXXX commented Nov 27, 2016

Hello!
I have found a logical error in iptools.
Iptools validate_range does not see the errors in network/netmask pair.

write

> iptools::validate_range("10.0.0.0/8")
[1] TRUE

wrong

> iptools::validate_range("10.0.1.0/8")
[1] TRUE

I am working with ip networks and i know that 10.0.1.0/8 is not a valid network.
10.0.1.0 is ip address that is part of 10.0.0.0/8 network. Next /8 network will be 11.0.0.0/8.
10.0.1.0 can be /24 or smaller network only.

Also it affects range_boundaries function:

write

> range_boundaries("10.0.0.0/8")
  minimum_ip     maximum_ip min_numeric max_numeric      range
1   10.0.0.0 10.255.255.255   167772160   184549375 10.0.0.0/8

wrong

> range_boundaries("10.0.1.0/8")
  minimum_ip     maximum_ip min_numeric max_numeric      range
1   10.0.1.0 10.255.255.255   167772416   184549375 10.0.1.0/8

You can see, that maximum_ip is the same (which is correct), but minimum ip on 10.0.1.0/8 is 10.0.1.0 which is wrong for /8 network.
range_boundaries("10.0.1.0/8"), how i see it, should have one of two option:

  1. It should tell you that your network is wrong and tell you, that probably your network is "10.0.0.0/8"
  2. It should calculate minimum_ip based on probably network.

You can calculate probably network by conjuction ip on netmask (like network devices do it) or by devision on hosts per subnet. Actually i had to do something like this. I made named vector of usable ips and netmask as name. Like this:

> netmask_ip_vector
        /0         /1         /2         /3         /4         /5         /6         /7         /8         /9 
4294967296 2147483648 1073741824  536870912  268435456  134217728   67108864   33554432   16777216    8388608 
       /10        /11        /12        /13        /14        /15        /16        /17        /18        /19 
   4194304    2097152    1048576     524288     262144     131072      65536      32768      16384       8192 
       /20        /21        /22        /23        /24        /25        /26        /27        /28        /29 
      4096       2048       1024        512        256        128         64         32         16          8 
       /30        /31        /32 
         4          2          1 

Than i calculate numeric value of first ip of that network (with cutting network part)

> network <- "10.0.0.0/8"
> network
[1] "10.0.0.0/8"
> numeric_ip <- gsub(pattern = "/.*$", replacement = "", x = "10.0.0.0/8") %>% ip_to_numeric()
> numeric_ip
[1] 167772160

Than i check how many ips should be in that network (using my vector)

> ips_for_network <- gsub(pattern = "^.*(/.*$)", replacement = "\\1", x = network) %>% netmask_ip_vector[[.]]
> ips_for_network
[1] 16777216

Than i check remainder of devision first ip of network and ip's for that network

> numeric_ip %% ips_for_network
[1] 0

If it's equal to zero, everything is fine, if not - you should subtract remainder from numeric_ip and transform result to ip. This will be your network address. Example with uncorrect network is below:

> network <- "10.0.1.0/8"
> network
[1] "10.0.1.0/8"
> netmask_ip_vector[[gsub(pattern = "^.*(/.*$)", replacement = "\\1", x = network)]] -> ips_for_network
> ips_for_network # ips_for_network is the same as 10.0.0.0/8
[1] 16777216
> gsub(pattern = "/.*$", replacement = "", x = network) %>% ip_to_numeric() -> numeric_ip
> numeric_ip # different from 10.0.0.0/8
[1] 167772416
> numeric_ip %% ips_for_network -> remainder
> remainder # not equal to zero, so the network part is uncorrect to /8 netmask
[1] 256
> currect_network <- (numeric_ip - remainder) %>% numeric_to_ip()
> currect_network
[1] "10.0.0.0"

Using this logic or something similar you can make your library more usefull.
Sorry for many letters and my English.
Best Regards, Konstantin.

P.S.
Have found another problem with validate_range function

> validate_range("10.0.1.0/4,545645764567")
[1] TRUE

4,545645764567 is also not valid netmask.

@hrbrmstr hrbrmstr added the bug label Nov 27, 2016
@hrbrmstr hrbrmstr self-assigned this Nov 27, 2016
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants