Skip to content

Commit

Permalink
Merge pull request #10 from hannesm/no-ppx
Browse files Browse the repository at this point in the history
remove ppx_cstruct usage, fold Ethernet_wire into Ethernet_packet
  • Loading branch information
hannesm authored Mar 9, 2023
2 parents 6c93d92 + ec5aaf7 commit b0b7c99
Show file tree
Hide file tree
Showing 7 changed files with 29 additions and 55 deletions.
18 changes: 0 additions & 18 deletions .travis.yml

This file was deleted.

1 change: 0 additions & 1 deletion ethernet.opam
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ depends: [
"dune"
"ocaml" {>= "4.08.0"}
"cstruct" {>= "6.0.0"}
"ppx_cstruct"
"mirage-net" {>= "3.0.0"}
"macaddr" {>= "4.0.0"}
"mirage-profile" {>= "0.5"}
Expand Down
3 changes: 1 addition & 2 deletions src/dune
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
(library
(name ethernet)
(public_name ethernet)
(libraries cstruct macaddr mirage-net logs lwt mirage-profile)
(preprocess (pps ppx_cstruct)))
(libraries cstruct macaddr mirage-net logs lwt mirage-profile))
4 changes: 2 additions & 2 deletions src/ethernet.ml
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ module Packet = struct
}


let sizeof_ethernet = Ethernet_wire.sizeof_ethernet
let sizeof_ethernet = Ethernet_packet.sizeof_ethernet

let of_cstruct = Ethernet_packet.Unmarshal.of_cstruct

Expand Down Expand Up @@ -93,7 +93,7 @@ module Make (Netif : Mirage_net.S) = struct
let write t ?src destination ethertype ?size payload =
MProf.Trace.label "ethernet.write";
let source = match src with None -> mac t | Some x -> x
and eth_hdr_size = Ethernet_wire.sizeof_ethernet
and eth_hdr_size = Ethernet_packet.sizeof_ethernet
and mtu = mtu t
in
match
Expand Down
38 changes: 24 additions & 14 deletions src/ethernet_packet.ml
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,19 @@ type t = {
ethertype : proto;
}

let sizeof_ethernet = 14

let ethertype_to_int = function
| `ARP -> 0x0806
| `IPv4 -> 0x0800
| `IPv6 -> 0x86dd

let int_to_ethertype = function
| 0x0806 -> Some `ARP
| 0x0800 -> Some `IPv4
| 0x86dd -> Some `IPv6
| _ -> None

type error = string

let pp fmt t =
Expand All @@ -20,20 +33,19 @@ let pp fmt t =
let equal {source; destination; ethertype} q =
(Macaddr.compare source q.source) = 0 &&
(Macaddr.compare destination q.destination) = 0 &&
Ethernet_wire.(compare (ethertype_to_int ethertype) (ethertype_to_int q.ethertype)) = 0
compare (ethertype_to_int ethertype) (ethertype_to_int q.ethertype) = 0

module Unmarshal = struct

let of_cstruct frame =
let open Ethernet_wire in
if Cstruct.length frame >= sizeof_ethernet then
match get_ethernet_ethertype frame |> int_to_ethertype with
| None -> Error (Printf.sprintf "unknown ethertype 0x%x in frame"
(get_ethernet_ethertype frame))
let raw_typ = Cstruct.BE.get_uint16 frame 12 in
match raw_typ |> int_to_ethertype with
| None -> Error (Printf.sprintf "unknown ethertype 0x%x in frame" raw_typ)
| Some ethertype ->
let payload = Cstruct.shift frame sizeof_ethernet
and source = Macaddr.of_octets_exn (copy_ethernet_src frame)
and destination = Macaddr.of_octets_exn (copy_ethernet_dst frame)
and source = Macaddr.of_octets_exn (Cstruct.to_string ~off:6 ~len:6 frame)
and destination = Macaddr.of_octets_exn (Cstruct.to_string ~off:0 ~len:6 frame)
in
Ok ({ destination; source; ethertype;}, payload)
else
Expand All @@ -42,22 +54,20 @@ end

module Marshal = struct
let check_len buf =
if Ethernet_wire.sizeof_ethernet > Cstruct.length buf then
if sizeof_ethernet > Cstruct.length buf then
Error "Not enough space for an Ethernet header"
else Ok ()

let unsafe_fill t buf =
let open Ethernet_wire in
set_ethernet_dst (Macaddr.to_octets t.destination) 0 buf;
set_ethernet_src (Macaddr.to_octets t.source) 0 buf;
set_ethernet_ethertype buf (ethertype_to_int t.ethertype);
()
Cstruct.blit_from_string (Macaddr.to_octets t.destination) 0 buf 0 6;
Cstruct.blit_from_string (Macaddr.to_octets t.source) 0 buf 6 6;
Cstruct.BE.set_uint16 buf 12 (ethertype_to_int t.ethertype)

let into_cstruct t buf =
Result.map (fun () -> unsafe_fill t buf) (check_len buf)

let make_cstruct t =
let buf = Cstruct.create Ethernet_wire.sizeof_ethernet in
let buf = Cstruct.create sizeof_ethernet in
unsafe_fill t buf;
buf
end
2 changes: 2 additions & 0 deletions src/ethernet_packet.mli
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ type t = {
ethertype : proto;
}

val sizeof_ethernet : int

type error = string

val pp : Format.formatter -> t -> unit
Expand Down
18 changes: 0 additions & 18 deletions src/ethernet_wire.ml

This file was deleted.

0 comments on commit b0b7c99

Please sign in to comment.