ipnetwork icon indicating copy to clipboard operation
ipnetwork copied to clipboard

IPNetwork.Parse(string, ParseOptions)

Open billpg opened this issue 2 years ago • 1 comments

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 avatar Dec 22 '22 18:12 billpg

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 avatar Dec 23 '22 18:12 billpg