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

CIDR parsing fails to apply mask. #144

Open
haight6716 opened this issue Jul 31, 2020 · 1 comment
Open

CIDR parsing fails to apply mask. #144

haight6716 opened this issue Jul 31, 2020 · 1 comment

Comments

@haight6716
Copy link

While writing tests for that other issue I ran into this one - shouldn't this pass?

assert.equal(ipaddr.IPv4.parseCIDR('8.3.4.8/24').toString(), '8.3.4.0/24');

Admittedly it's invalid input, but shouldn't we silently fix it or throw? Instead, we produce invalid output.

@kennethtran93
Copy link
Contributor

kennethtran93 commented Aug 1, 2020

This is because it parses the IP and CIDR separately. CIDR is used for checking through another function.

The octets stored is the one used from parsing the IP address irregardless of the existence of CIDR, thus toString will only return the parsed IP address, and if CIDR was given, then appends it afterwards.

You'll need to use the match function to test for CIDR (which only exists when using the parse function, not parseCIDR.

so

ipaddr.IPv4.parse('8.3.4.8').match(ipaddr.IPv4.parseCIDR('8.3.4.0/24'))

or

ipaddr.IPv4.parse('8.3.4.8').match(ipaddr.parse('8.3.4.0'), 24)

or

ipaddr.IPv4.parse('8.3.4.0').match(ipaddr.parse('8.3.4.8'), 24)

or

const test = ipaddr.IPv4.parseCIDR('8.3.4.8/24');
ipaddr.IPv4.parse('8.3.4.0').match(test[0], test[1]);

The parser doesn't know about any CIDR notations. It is only passed in the IP address string (without CIDR) for parsing. This is the same if you use parseCIDR, as it send everything before the slash to the parser for IP Address.

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