iprange-rs icon indicating copy to clipboard operation
iprange-rs copied to clipboard

Incorrect remove on IpRange

Open ntrippar opened this issue 2 years ago • 2 comments

I'm trying to remove a host from a IpRange by doing a remove <ip>/32 and I'm getting this is returning way less hosts than it should. Here is the POC for this issue

use iprange::IpRange;
use ipnet::Ipv4Net;

let mut ip_range: IpRange<Ipv4Net> = IpRange::new();

ip_range.add("10.0.0.0/24".parse()?);

let remove: Ipv4Net = "10.0.0.50/32".parse()?;
ip_range.remove(remove);

let total_ip_count: usize = ip_range
    .iter()
    .map(|r| r.hosts().count())
    .sum();

println!("hosts: {}", total_ip_count);

this returns hosts: 243 and we should have 253

ntrippar avatar Jul 26 '23 03:07 ntrippar

This is because you remove a subnet instead of an address from the range. This operation splits the original 10.0.0.0/24 into a lot more smaller ranges. So, the hosts method provided by ipnet excludes more network and broadcast addresses than you expected.

sticnarf avatar Jul 26 '23 16:07 sticnarf

Ah, its makes completely sense. although on the /32 the broadcast should be the same IP, but as you said for sure the ipnet doesn't print the broadcast etc on the smaller / spitted networks I will check tonight on the code, and see if I implement something on my side. as the difference seems 10 IP's. will close later when validate the code

ntrippar avatar Jul 26 '23 18:07 ntrippar