forked from mirage/ocaml-ipaddr
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add new
Ipaddr_cstruct
and Macaddr_cstruct
libraries
for conversion to/from cstructs based on mirage#36 Co-authored-by: Nicolás Ojeda Bär <[email protected]>
- Loading branch information
Showing
15 changed files
with
341 additions
and
9 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,3 @@ | ||
(lang dune 1.0) | ||
(lang dune 1.9) | ||
(name ipaddr) | ||
(allow_approximate_merlin) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
opam-version: "2.0" | ||
maintainer: "[email protected]" | ||
authors: ["David Sheets" "Anil Madhavapeddy" "Hugo Heuzard"] | ||
synopsis: "A library for manipulation of IP address representations using Cstructs" | ||
license: "ISC" | ||
tags: ["org:mirage" "org:xapi-project"] | ||
homepage: "https://github.com/mirage/ocaml-ipaddr" | ||
doc: "https://mirage.github.io/ocaml-ipaddr/" | ||
bug-reports: "https://github.com/mirage/ocaml-ipaddr/issues" | ||
depends: [ | ||
"ocaml" {>= "4.04.0"} | ||
"dune" {build} | ||
"ipaddr" | ||
"cstruct" | ||
] | ||
build: [ | ||
["dune" "subst"] {pinned} | ||
["dune" "build" "-p" name "-j" jobs] | ||
["dune" "runtest" "-p" name "-j" jobs] {with-test} | ||
] | ||
dev-repo: "git+https://github.com/mirage/ocaml-ipaddr.git" | ||
description: """ | ||
Cstruct convertions for macaddr | ||
""" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,7 @@ | ||
opam-version: "2.0" | ||
maintainer: "[email protected]" | ||
authors: ["David Sheets" "Anil Madhavapeddy" "Hugo Heuzard"] | ||
synopsis: "A library for manipulation of IP (and MAC) address representations" | ||
synopsis: "A library for manipulation of IP address representations usnig sexp" | ||
description: """ | ||
Sexp convertions for ipaddr | ||
""" | ||
|
@@ -15,6 +15,7 @@ depends: [ | |
"ocaml" {>= "4.04.0"} | ||
"dune" {build} | ||
"ipaddr" | ||
"ipaddr-cstruct" {with-test} | ||
"ounit" {with-test} | ||
"ppx_sexp_conv" {>= "v0.9.0"} | ||
] | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
(* | ||
* Copyright (c) 2019 Anil Madhavapeddy | ||
* Copyright (c) 2014 Nicolás Ojeda Bär | ||
* | ||
* Permission to use, copy, modify, and distribute this software for any | ||
* purpose with or without fee is hereby granted, provided that the above | ||
* copyright notice and this permission notice appear in all copies. | ||
* | ||
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES | ||
* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF | ||
* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR | ||
* ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES | ||
* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN | ||
* ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF | ||
* OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. | ||
* | ||
*) | ||
|
||
let need_more x = Ipaddr.Parse_error ("not enough data", x) | ||
|
||
let try_with_result fn a = | ||
try Ok (fn a) | ||
with Ipaddr.Parse_error (msg, _) -> Error (`Msg ("Ipaddr: " ^ msg)) | ||
|
||
module V4 = struct | ||
|
||
let of_cstruct_exn cs = | ||
let len = Cstruct.len cs in | ||
if len < 4 then raise (need_more (Cstruct.to_string cs)); | ||
Ipaddr.V4.of_int32 (Cstruct.BE.get_uint32 cs 0) | ||
|
||
let of_cstruct cs = | ||
try_with_result of_cstruct_exn cs | ||
|
||
let write_cstruct_exn i cs = | ||
let len = Cstruct.len cs in | ||
if len < 4 then raise (need_more (Cstruct.to_string cs)); | ||
Cstruct.BE.set_uint32 cs 0 (Ipaddr.V4.to_int32 i) | ||
|
||
let to_cstruct ?(allocator = Cstruct.create) i = | ||
let cs = allocator 4 in | ||
write_cstruct_exn i cs; | ||
cs | ||
|
||
end | ||
|
||
module V6 = struct | ||
|
||
open Ipaddr.V6 | ||
|
||
let of_cstruct_exn cs = | ||
let len = Cstruct.len cs in | ||
if len < 16 then raise (need_more (Cstruct.to_string cs)); | ||
let hihi = Cstruct.BE.get_uint32 cs 0 in | ||
let hilo = Cstruct.BE.get_uint32 cs 4 in | ||
let lohi = Cstruct.BE.get_uint32 cs 8 in | ||
let lolo = Cstruct.BE.get_uint32 cs 12 in | ||
of_int32 (hihi, hilo, lohi, lolo) | ||
|
||
let of_cstruct cs = | ||
try_with_result of_cstruct_exn cs | ||
|
||
let write_cstruct_exn i cs = | ||
let len = Cstruct.len cs in | ||
if len < 16 then raise (need_more (Cstruct.to_string cs)); | ||
let a, b, c, d = to_int32 i in | ||
Cstruct.BE.set_uint32 cs 0 a; | ||
Cstruct.BE.set_uint32 cs 4 b; | ||
Cstruct.BE.set_uint32 cs 8 c; | ||
Cstruct.BE.set_uint32 cs 12 d | ||
|
||
let to_cstruct ?(allocator = Cstruct.create) i = | ||
let cs = allocator 16 in | ||
write_cstruct_exn i cs; | ||
cs | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
(* | ||
* Copyright (c) 2019 Anil Madhavapeddy | ||
* Copyright (c) 2014 Nicolás Ojeda Bär | ||
* | ||
* Permission to use, copy, modify, and distribute this software for any | ||
* purpose with or without fee is hereby granted, provided that the above | ||
* copyright notice and this permission notice appear in all copies. | ||
* | ||
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES | ||
* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF | ||
* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR | ||
* ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES | ||
* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN | ||
* ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF | ||
* OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. | ||
* | ||
*) | ||
|
||
(** Convert to and from Cstructs and IP addresses *) | ||
|
||
(** Ipv4 address conversions *) | ||
module V4 : sig | ||
|
||
(** [of_cstruct c] parses the first 4 octets of [c] into an IPv4 address. *) | ||
val of_cstruct : Cstruct.t -> (Ipaddr.V4.t, [> `Msg of string ]) result | ||
|
||
(** [of_cstruct_exn] parses the first 4 octets of [c] into an IPv4 address. | ||
Raises {!Ipaddr.Parse_failure} on error. *) | ||
val of_cstruct_exn : Cstruct.t -> Ipaddr.V4.t | ||
|
||
(** [to_cstruct ipv4] is a cstruct of length 4 encoding [ipv4]. | ||
The cstruct is allocated using [allocator]. If [allocator] is | ||
not provided, [Cstruct.create] is used. *) | ||
val to_cstruct: ?allocator:(int -> Cstruct.t) -> Ipaddr.V4.t -> Cstruct.t | ||
|
||
(** [write_cstruct_exn ipv4 cs] writes 4 bytes into [cs] representing | ||
the [ipv4] address octets. Raises {!Ipaddr.Parse_error} if [cs] | ||
is not at least 4 bytes long. *) | ||
val write_cstruct_exn : Ipaddr.V4.t -> Cstruct.t -> unit | ||
end | ||
|
||
(** Ipv6 address conversions *) | ||
module V6 : sig | ||
|
||
(** [of_cstruct c] parses the first 16 octets of [c] into an IPv6 address. *) | ||
val of_cstruct : Cstruct.t -> (Ipaddr.V6.t, [> `Msg of string ]) result | ||
|
||
(** [of_cstruct_exn] parses the first 16 octets of [c] into an IPv6 address. | ||
Raises {!Ipaddr.Parse_failure} on error. *) | ||
val of_cstruct_exn : Cstruct.t -> Ipaddr.V6.t | ||
|
||
(** [to_cstruct ipv6] is a cstruct of length 16 encoding [ipv6]. | ||
The cstruct is allocated using [allocator]. If [allocator] is | ||
not provided, [Cstruct.create] is used. *) | ||
val to_cstruct: ?allocator:(int -> Cstruct.t) -> Ipaddr.V6.t -> Cstruct.t | ||
|
||
(** [write_cstruct_exn ipv6 cs] writes 16 bytes into [cs] representing | ||
the [ipv6] address octets. Raises {!Ipaddr.Parse_error} if [cs] | ||
is not at least 16 bytes long. *) | ||
val write_cstruct_exn : Ipaddr.V6.t -> Cstruct.t -> unit | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
(* | ||
* Copyright (c) 2019 Anil Madhavapeddy | ||
* Copyright (c) 2014 Nicolás Ojeda Bär | ||
* | ||
* Permission to use, copy, modify, and distribute this software for any | ||
* purpose with or without fee is hereby granted, provided that the above | ||
* copyright notice and this permission notice appear in all copies. | ||
* | ||
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES | ||
* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF | ||
* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR | ||
* ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES | ||
* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN | ||
* ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF | ||
* OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. | ||
* | ||
*) | ||
|
||
let try_with_result fn a = | ||
try Ok (fn a) | ||
with Macaddr.Parse_error (msg, _) -> Error (`Msg ("Macaddr: " ^ msg)) | ||
|
||
let of_cstruct_exn cs = | ||
if Cstruct.len cs <> 6 | ||
then raise (Macaddr.Parse_error ("MAC is exactly 6 bytes", Cstruct.to_string cs)) | ||
else Cstruct.to_string cs |> Macaddr.of_octets_exn | ||
|
||
let of_cstruct cs = | ||
try_with_result of_cstruct_exn cs | ||
|
||
let write_cstruct_exn (mac:Macaddr.t) cs = | ||
let len = Cstruct.len cs in | ||
let mac = Macaddr.to_octets mac in | ||
if len <> 6 then raise (Macaddr.Parse_error ("MAC is exactly 6 bytes", mac)); | ||
Cstruct.blit_from_string mac 0 cs 0 6 | ||
|
||
let to_cstruct ?(allocator = Cstruct.create) mac = | ||
let cs = allocator 6 in | ||
write_cstruct_exn mac cs; | ||
cs |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
(* | ||
* Copyright (c) 2019 Anil Madhavapeddy | ||
* Copyright (c) 2014 Nicolás Ojeda Bär | ||
* | ||
* Permission to use, copy, modify, and distribute this software for any | ||
* purpose with or without fee is hereby granted, provided that the above | ||
* copyright notice and this permission notice appear in all copies. | ||
* | ||
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES | ||
* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF | ||
* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR | ||
* ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES | ||
* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN | ||
* ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF | ||
* OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. | ||
* | ||
*) | ||
|
||
(** Convert to and from Cstructs and MAC address. *) | ||
|
||
(** [of_cstruct c] parses the 6 octets of [c] into a MAC address. *) | ||
val of_cstruct : Cstruct.t -> (Macaddr.t, [> `Msg of string ]) result | ||
|
||
(** [of_cstruct_exn] parses the 6 octets of [c] into a MAC address. | ||
Raises {!Macaddr.Parse_failure} on error. *) | ||
val of_cstruct_exn : Cstruct.t -> Macaddr.t | ||
|
||
(** [to_cstruct mac] is a cstruct of length 4 encoding [ipv4]. | ||
The cstruct is allocated using [allocator]. If [allocator] is | ||
not provided, [Cstruct.create] is used. *) | ||
val to_cstruct: ?allocator:(int -> Cstruct.t) -> Macaddr.t -> Cstruct.t | ||
|
||
(** [write_cstruct_exn mac cs] writes 6 bytes into [cs] representing | ||
the [mac] address octets. Raises {!Macaddr.Parse_error} if [cs] | ||
is not 6 bytes long. *) | ||
val write_cstruct_exn : Macaddr.t -> Cstruct.t -> unit |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.