-
Notifications
You must be signed in to change notification settings - Fork 145
fix: fix net.is_IP_in_CIDR
with IPv6
#1908
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
Conversation
Signed-off-by: 黑怕 <[email protected]>
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM
cc @johngmyers 😄 |
Pull Request Test Coverage Report for Build 14196324909Details
💛 - Coveralls |
return None; | ||
} | ||
let ip = IpAddr::from_str(parts[0]).ok()?; | ||
let mask_bits = parts[1].parse::<u32>().ok(); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This incorrectly succeeds if ip
has bits set beyond mask_bits
. For example, 10.11.0.0/8
.
IpAddr::from_str(ip).map_err(|_| ()) | ||
} | ||
|
||
fn _check_ipv4_cidr(ip: Ipv4Addr, cidr_ip: Ipv4Addr, mask_bits: u32) -> bool { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why not use the cidr
crate? I was just working on reimplementing net.parse_cidr
using it.
} | ||
|
||
fn _check_ipv4_cidr(ip: Ipv4Addr, cidr_ip: Ipv4Addr, mask_bits: u32) -> bool { | ||
let mask = !((1u32 << (32 - mask_bits)) - 1); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Does this not overflow for 0.0.0.0/0
?
@@ -5,3 +5,4 @@ cidr1 = net.hosts_in_CIDR("192.168.1.0/24") | |||
cidr2 = net.subnets_from_CIDR("192.168.1.0/24") | |||
cidr3 = net.is_IP_in_CIDR("192.168.1.1", "192.168.1.0/24") | |||
cidr4 = net.is_IP_in_CIDR("192.168.0.1", "192.168.1.0/24") | |||
cidr5 = net.is_IP_in_CIDR('2001:db8::1', "2001:db8::/64") |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There should be a LOT more tests:
- First in CIDR
- Last in CIDR
- One before first in CIDR
- One after last in CIDR
0.0.0.0/0
and::/0
- IPv4 in
::/0
- IPv4 in IPv6 CIDR and vice versa
- Invalid CIDRs
- Invalid IPs
- IPv4 /32 and IPv6 /128
1. Does this PR affect any open issues?(Y/N) and add issue references (e.g. "fix #123", "re #123".):
2. What is the scope of this PR (e.g. component or file name):
3. Provide a description of the PR(e.g. more details, effects, motivations or doc link):
4. Are there any breaking changes?(Y/N) and describe the breaking changes(e.g. more details, motivations or doc link):
5. Are there test cases for these changes?(Y/N) select and add more details, references or doc links: