ocaml-ipaddr
ocaml-ipaddr copied to clipboard
Short CIDR notation
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
I think this would be fine to include in ipaddr
Oh, and Mindy found a bug in that example code; if provided_octets > 4 then it would complain.