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

Incorrect remove on IpRange #25

Open
ntrippar opened this issue Jul 26, 2023 · 2 comments
Open

Incorrect remove on IpRange #25

ntrippar opened this issue Jul 26, 2023 · 2 comments

Comments

@ntrippar
Copy link

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

@sticnarf
Copy link
Owner

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.

@ntrippar
Copy link
Author

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

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