Skip to content

Commit 3201fb0

Browse files
committed
Fix Eio signatures
Forgot to commit the interface before, also send_packet should take a Cstruct.t like the other backends.
1 parent e2646b7 commit 3201fb0

File tree

4 files changed

+60
-6
lines changed

4 files changed

+60
-6
lines changed

bin/test_arp.ml

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,8 +27,7 @@ let pp hdr buf =
2727
print_newline ()
2828

2929
let send link hdr pkt =
30-
Eio_rawlink.send_packet link
31-
(Eio.Flow.cstruct_source [ pkt ]);
30+
Eio_rawlink.send_packet link pkt;
3231
pp hdr pkt
3332

3433
let recv link hdr =

lib/eio_rawlink.ml

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,14 +23,17 @@ type t = {
2323
buffer : Cstruct.t;
2424
}
2525

26+
let dhcp_server_filter = Lowlevel.dhcp_server_filter
27+
let dhcp_client_filter = Lowlevel.dhcp_client_filter
28+
2629
let open_link ?filter ?(promisc=false) ifname ~sw =
2730
let fd = Lowlevel.opensock ?filter:filter ~promisc ifname in
2831
let flow = Eio_unix.FD.as_socket ~sw ~close_unix:true fd in
2932
{ flow; fd; packets = ref []; buffer = (Cstruct.create 65536) }
3033

3134
let close_link t = t.flow#close
3235

33-
let send_packet t buf = t.flow#copy buf
36+
let send_packet t buf = t.flow#copy @@ Eio.Flow.cstruct_source [ buf ]
3437

3538
let rec read_packet t =
3639
match !(t.packets) with

lib/eio_rawlink.mli

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
(*
2+
* Copyright (c) 2015-2022 Christiano F. Haesbaert <haesbaert@haesbaert.org>
3+
*
4+
* Permission to use, copy, modify, and distribute this software for any
5+
* purpose with or without fee is hereby granted, provided that the above
6+
* copyright notice and this permission notice appear in all copies.
7+
*
8+
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9+
* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10+
* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
11+
* ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12+
* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
13+
* ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
14+
* OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
15+
*)
16+
17+
(** A portable API to send and receive raw packets.
18+
19+
There are times when one needs to construct the full ethernet frame to
20+
send/receive. Most unixes support BPF (BSD Packet Filter) to achieve it, but
21+
linux provides the same functionality via AF_SOCKET. This API works with
22+
either a BPF or AF_SOCKET backend, so it should work on every usable UNIX,
23+
as well as linux out there.
24+
*)
25+
26+
type t
27+
(** Type of a rawlink. *)
28+
29+
val open_link : ?filter:string -> ?promisc:bool -> string -> sw:Eio.Switch.t -> t
30+
(** [open_link ~filter ~promisc interface sw]. Creates a rawlink on the
31+
specified [interface], a BPF program [filter] can be passed to
32+
filter out incoming packets. If [promisc] is true, sets [interface]
33+
to promiscuous mode, defaults to false.*)
34+
35+
val close_link : t -> unit
36+
(** [close_link]. Closes a rawlink. *)
37+
38+
val read_packet : t -> Cstruct.t
39+
(** [read_packet t]. Reads a full packet, may raise Unix.Unix_error. *)
40+
41+
val send_packet : t -> Cstruct.t -> unit
42+
(** [send_packet t]. Sends a full packet, may raise Unix.Unix_error. *)
43+
44+
val dhcp_server_filter : unit -> string
45+
(** [dhcp_server_filter]. Returns a BPF program suitable to be passed in
46+
[open_link ~filter], it accepts UDP packets destined to
47+
port 67 (DHCP server). *)
48+
49+
val dhcp_client_filter : unit -> string
50+
(** [dhcp_client_filter]. Returns a BPF program suitable to be passed in
51+
[open_link ~filter], it accepts UDP packets destined to
52+
port 68 (DHCP client). *)

lib/lwt_rawlink.ml

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,9 @@ type t = {
2626
buffer : Cstruct.t;
2727
}
2828

29+
let dhcp_server_filter = Lowlevel.dhcp_server_filter
30+
let dhcp_client_filter = Lowlevel.dhcp_client_filter
31+
2932
let open_link ?filter ?(promisc=false) ifname =
3033
let fd = Lwt_unix.of_unix_file_descr (Lowlevel.opensock ?filter:filter ~promisc ifname) in
3134
let () = Lwt_unix.set_blocking fd false in
@@ -54,6 +57,3 @@ let send_packet t buf =
5457
Lwt.fail (Unix.Unix_error(Unix.ENOBUFS, "send_packet: short write", ""))
5558
else
5659
Lwt.return_unit)
57-
58-
let dhcp_server_filter = Lowlevel.dhcp_server_filter
59-
let dhcp_client_filter = Lowlevel.dhcp_client_filter

0 commit comments

Comments
 (0)