Skip to content
Browse files

add string_to_ipv4; expose ipv4/6 regexes through interface

  • Loading branch information...
1 parent 76a77d8 commit e4897c98fedd3f9f3313a7d46840d86894ea2d4a @mor1 mor1 committed Mar 19, 2012
Showing with 20 additions and 10 deletions.
  1. +13 −10 lib/Uri_IP.ml
  2. +3 −0 lib/Uri_IP.mli
  3. +2 −0 lib/uri_re.ml
  4. +2 −0 lib/uri_re.mli
View
23 lib/Uri_IP.ml
@@ -44,6 +44,19 @@ let ipv4_to_string i =
((i &&& 0x0_ff000000_l) >>> 24) ((i &&& 0x0_00ff0000_l) >>> 16)
((i &&& 0x0_0000ff00_l) >>> 8) ((i &&& 0x0_000000ff_l) )
+let string_to_ipv4 (s:string) : ipv4 =
+ match Re.execp Uri_re.ipv4_address s with
+ | true -> (match (Re_str.split_delim (Re_str.regexp "\\.") s) with
+ | [a; b; c; d] -> (
+ let a = Int32.of_string a in
+ let b = Int32.of_string b in
+ let c = Int32.of_string c in
+ let d = Int32.of_string d in
+ (a <<< 24) ||| (b <<< 16) ||| (c <<< 8) ||| d)
+ | _ -> failwith "invalid ipv4 address"
+ )
+ | false -> failwith "invalid ipv4 address"
+
let bytes_to_ipv4 bs =
((bs.[0] |> byte_to_int32 <<< 24) ||| (bs.[1] |> byte_to_int32 <<< 16)
||| (bs.[2] |> byte_to_int32 <<< 8) ||| (bs.[3] |> byte_to_int32))
@@ -64,13 +77,3 @@ let ipv6_to_string i =
let bytes_to_ipv6 bs =
(bytes_to_ipv4 (String.sub bs 0 4), bytes_to_ipv4 (String.sub bs 4 4),
bytes_to_ipv4 (String.sub bs 8 4), bytes_to_ipv4 (String.sub bs 12 4))
-
-
-
-
-
-
-
-
-
-
View
3 lib/Uri_IP.mli
@@ -37,6 +37,9 @@ type ipv4 = Int32.t
(** Standard dotted quad string representation of an IPv4 address. *)
val ipv4_to_string : ipv4 -> string
+(** Parse standard dotted quad string representation of an IPv4 address. *)
+val string_to_ipv4 : string -> ipv4
+
(** Generate numeric IPv4 address from a packed bytestring. *)
val bytes_to_ipv4 : bytes -> ipv4
View
2 lib/uri_re.ml
@@ -105,5 +105,7 @@ module Raw = struct
let uri_reference = Re_posix.re "^(([^:/?#]+):)?(//([^/?#]*))?([^?#]*)(\\?([^#]*))?(#(.*))?"
end
+let ipv4_address = Re_posix.compile Raw.ipv4_address
+let ipv6_address = Re_posix.compile Raw.ipv6_address
let uri_reference = Re_posix.compile Raw.uri_reference
let authority = Re_posix.compile Raw.authority
View
2 lib/uri_re.mli
@@ -15,5 +15,7 @@
*
*)
+val ipv4_address : Re.re
+val ipv6_address : Re.re
val uri_reference : Re.re
val authority : Re.re

0 comments on commit e4897c9

Please sign in to comment.
Something went wrong with that request. Please try again.