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

IPNetwork.Parse(string, ParseOptions) #252

Open
billpg opened this issue Dec 22, 2022 · 1 comment
Open

IPNetwork.Parse(string, ParseOptions) #252

billpg opened this issue Dec 22, 2022 · 1 comment

Comments

@billpg
Copy link
Contributor

billpg commented Dec 22, 2022

I use IPNetwork.Parse to parse a user's configuration when they set up their allowed IP ranges. I'd like to allow them to type in single IP addresses and for my code to interpret that this-IP-only, but instead I get the class-A/B/C network that IP is in. (Because of this, my code calls IPAddress.Parse first, falling back to IPNetwork.Parse.)

What if we added IPNetwork.ParseFlex which is documented as being tailored for user configuration, leaving Parse as suitable for validation and code expecting legacy behaviour.

IPNetwork.ParseFlex("192.168.1.1/8") returns 192.0.0.0/8.
IPNetwork.ParseFlex("192.168.1.1") returns 192.168.1.1/32.

I'm happy to work on this but I'd like to establish a consensus that we want this before I barrel ahead and start coding.

@billpg
Copy link
Contributor Author

billpg commented Dec 23, 2022

Changed my mind, as ParseFlex would be just what I want. I'd instead add IPNetwork.Parse(string, ParseOptions)

ParseOptions would have the following get/set properties...

  • SingleIP (enum - What to do if the string is a single IP without a "/".)
    • Classful (Default - Cut at /8, /16 or /24 per IPv4 class or /64 for IPv6)
    • SingleIPNetwork (Adds /32 or /128 as applicable.)
    • Zeros (Count any ".0" or ":0000" on the right as the network size. "10.11.0.0" becomes 10.11.0.0/16)
  • RequireIP (enum - Require a particular version of IP.)
    • Any (Default - Allow IPv4 and IPv6.)
    • IPv4Only (Reject any string that isn't IPv4.)
    • IPv6Only (Reject any string that isn't IPv6.)

This class ParseOptions could be extended with new options, as long as the default is the status quo.

Thoughts?

@billpg billpg changed the title IPNetwork.ParseFlex, where single IP address strings are returned as /32 or /128. IPNetwork.Parse(string, ParseOptions) Dec 23, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants