Skip to content

Commit

Permalink
separate pure from effectful bits
Browse files Browse the repository at this point in the history
  • Loading branch information
hannesm committed Mar 11, 2015
1 parent 21d523e commit 1e3e291
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 17 deletions.
27 changes: 10 additions & 17 deletions lib/ethif.ml
Original file line number Diff line number Diff line change
Expand Up @@ -42,23 +42,16 @@ module Make(Netif : V1_LWT.NETWORK) = struct
let of_interest dest =
Macaddr.compare dest (mac t) = 0 || not (Macaddr.is_unicast dest)
in
if Cstruct.len frame >= 60 then
(* minimum payload is 46 + source + destination + type *)
match Macaddr.of_bytes (Wire_structs.copy_ethernet_dst frame) with
| Some frame_mac when of_interest frame_mac ->
let payload = Cstruct.shift frame Wire_structs.sizeof_ethernet
and ethertype = Wire_structs.get_ethernet_ethertype frame
in
Wire_structs.(
match int_to_ethertype ethertype with
| Some ARP -> arpv4 frame
| Some IPv4 -> ipv4 payload
| Some IPv6 -> ipv6 payload
| None -> return_unit (* TODO default etype payload *)
)
| _ -> return_unit
else
return_unit
match Wire_structs.parse_ethernet_frame frame with
| Some (typ, destination, payload) when of_interest destination ->
begin
match typ with
| Some Wire_structs.ARP -> arpv4 frame
| Some Wire_structs.IPv4 -> ipv4 payload
| Some Wire_structs.IPv6 -> ipv6 payload
| None -> return_unit (* TODO: default ethertype payload handler *)
end
| _ -> return_unit

let write t frame =
MProf.Trace.label "ethif.write";
Expand Down
13 changes: 13 additions & 0 deletions lib/wire_structs.ml
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,19 @@ cenum ethertype {
IPv6 = 0x86dd;
} as uint16_t

let parse_ethernet_frame frame =
if Cstruct.len frame >= 60 then
(* minimum payload is 46 + source + destination + type *)
let payload = Cstruct.shift frame sizeof_ethernet
and typ = get_ethernet_ethertype frame
in
match Macaddr.of_bytes (copy_ethernet_dst frame) with
| Some destination_mac -> Some (int_to_ethertype typ, destination_mac, payload)
| None -> None
else
None


cstruct udp {
uint16_t source_port;
uint16_t dest_port;
Expand Down

0 comments on commit 1e3e291

Please sign in to comment.