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

Short CIDR notation #70

Open
cfcs opened this issue Dec 15, 2017 · 2 comments
Open

Short CIDR notation #70

cfcs opened this issue Dec 15, 2017 · 2 comments

Comments

@cfcs
Copy link

cfcs commented Dec 15, 2017

Hi,

I'm trying to parse a configuration file (in the pf firewall format) that has entries like:

utop # Ipaddr.V4.Prefix.of_string "172.16/12";;
- : Ipaddr.V4.Prefix.t option = None
utop # Ipaddr.V4.Prefix.of_string "10/8";;
- : Ipaddr.V4.Prefix.t option = None

I can work around that by using something like

let expand_ipv4 cidr =
  let [prefix;mask] = String.split_on_char '/' cidr in
  let provided_octets = List.length (String.split_on_char '.' prefix) in
  let padding = String.init ((4 - provided_octets)*2)
    (function | i when i mod 2 = 0 -> '.'
              | _ -> '0')
  in prefix ^ padding ^ "/" ^ mask
utop # Ipaddr.Prefix.of_string "10.0/8";;
- : Ipaddr.Prefix.t option = None

utop # Ipaddr.Prefix.of_string (expand_ipv4 "10.0/8") ;;
- : Ipaddr.Prefix.t option = Some 10.0.0.0/8

It would be very convenient for me if this type of short notation was handled by the ipaddr upstream, but I wanted to hear your thoughts first before writing a PR.

Ping @djs55

@avsm
Copy link
Member

avsm commented Jul 10, 2019

I think this would be fine to include in ipaddr

@cfcs
Copy link
Author

cfcs commented Jul 11, 2019

Oh, and Mindy found a bug in that example code; if provided_octets > 4 then it would complain.

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