5 ms·
Probably doesn't matter much with only a few networks, but this is using the wrong data structure: if let Some(net) = ip_nets.load() .iter()
by justinsaccount 4y ago
Probably doesn't matter much with only a few networks, but this is using the wrong data structure:
if let Some(net) = ip_nets.load()
.iter()
.find(|net| net.contains(&addr.ip()))
ip_nets is a 'HashSet<IpNet>' but it should be a radix/patricia tree.
Something like https://lib.rs/crates/iprange https://lib.rs/crates/iprange
- mleonhard 4y agoThe `iprange` library contains two bits of `unsafe` code [0]. [0] https://github.com/sticnarf/iprange-rs/search?q=unsafe https://github.com/sticnarf/iprange-rs/search?q=unsafe
- LegionMammal978 4y agoThe first one seems to be a necessary workaround for the lack of GATs; its safety comment is pretty trivially correct. The second one is entirely unnecessary, and I've just filed a PR to get rid of it.
- asadawadia 4y agowhy
- justinsaccount 4y agoBecause a radix tree has O(1) lookups even if you have 500,000 ip ranges. It's how routers route.
- fasterthanlime 4y agoI looked for that kind of data structure for 30 seconds in the ipnet crate itself, didn't find it, noticed there were only 23 IP ranges and decided it was fine. (Keep in mind this happened during the attack, so compromises)