Skip to content

Commit 725877f

Browse files
authored
Merge pull request #5 from mseri/master
Update interface for safe-string
2 parents 5d763ca + 28f1c6b commit 725877f

File tree

5 files changed

+21
-14
lines changed

5 files changed

+21
-14
lines changed

lib/fd_send_recv.ml

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,11 @@ exception Unix_error of int
1616

1717
let _ = Callback.register_exception "fd_send_recv.unix_error" (Unix_error (0))
1818

19-
external send_fd : Unix.file_descr -> string -> int -> int -> Unix.msg_flag list -> Unix.file_descr -> int = "stub_unix_send_fd_bytecode" "stub_unix_send_fd"
20-
external recv_fd : Unix.file_descr -> string -> int -> int -> Unix.msg_flag list -> int * Unix.sockaddr * Unix.file_descr = "stub_unix_recv_fd"
19+
external send_fd : Unix.file_descr -> bytes -> int -> int -> Unix.msg_flag list -> Unix.file_descr -> int = "stub_unix_send_fd_bytecode" "stub_unix_send_fd"
20+
external recv_fd : Unix.file_descr -> bytes -> int -> int -> Unix.msg_flag list -> int * Unix.sockaddr * Unix.file_descr = "stub_unix_recv_fd"
21+
22+
let send_fd_substring channel_fd buf ofs len flags fd_to_send =
23+
send_fd channel_fd (Bytes.unsafe_of_string buf) ofs len flags fd_to_send
2124

2225
let fd_of_int (x: int) : Unix.file_descr = Obj.magic x
2326

lib/fd_send_recv.mli

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
exception Unix_error of int
1818
(** Thrown by the low-level C functions *)
1919

20-
val send_fd : Unix.file_descr -> string -> int -> int ->
20+
val send_fd : Unix.file_descr -> bytes -> int -> int ->
2121
Unix.msg_flag list -> Unix.file_descr -> int
2222
(** [send_fd channel_fd buf ofs len flags fd_to_send] sends a message
2323
over [channel_fd] containing the [buf] [ofs] [len] substring, with
@@ -26,12 +26,16 @@ val send_fd : Unix.file_descr -> string -> int -> int ->
2626
(e.g. of size greater than zero) to actually have the fd
2727
passed. *)
2828

29-
val recv_fd : Unix.file_descr -> string -> int -> int ->
29+
val recv_fd : Unix.file_descr -> bytes -> int -> int ->
3030
Unix.msg_flag list -> int * Unix.sockaddr * Unix.file_descr
3131
(** [recv_fd channel_fd buf ofs len flags] receives a message into
3232
substring [buf] [ofs] [len] with [flags], returning the number of
3333
bytes read, the address of the peer and a file descriptor. *)
3434

35+
val send_fd_substring : Unix.file_descr -> string -> int -> int ->
36+
Unix.msg_flag list -> Unix.file_descr -> int
37+
(** Like [send_fd] but takes a string *)
38+
3539
val int_of_fd : Unix.file_descr -> int
3640
(** [int_of_fd fd] returns the underlying unix integer file descriptor
3741
associated with OCaml Unix.file_descr [fd]. *)

test/test.ml

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,17 +3,17 @@ open Fd_send_recv
33

44
let t_receivefd fd =
55
Printf.printf "[receiver] t_receivedfd thread started!\n%!";
6-
let buf = "**" in
6+
let buf = Bytes.of_string "**" in
77
let nb_read, remote_saddr, fd_recv = recv_fd fd buf 0 2 [] in
88
Printf.printf "[receiver] Received %d bytes, received fd = %d\n%!" nb_read (int_of_fd fd_recv);
99
let message = "[receiver] I'm the receiver, and I'm writing into the fd you passed to me :p\n" in
10-
let nb_written = write fd_recv message 0 (String.length message) in
10+
let nb_written = write_substring fd_recv message 0 (String.length message) in
1111
assert (nb_written = String.length message)
1212

1313
let t_sendfd fd =
1414
let fd_to_send = stdout in
1515
let buf = " " in
16-
let nb_sent = send_fd fd buf 0 2 [] fd_to_send in
16+
let nb_sent = send_fd_substring fd buf 0 2 [] fd_to_send in
1717
Printf.printf "[sender] sent %d bytes, sent fd = %d\n%!" nb_sent (int_of_fd fd_to_send)
1818

1919
let main () =

test/test_fork.ml

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,17 +3,17 @@ open Fd_send_recv
33

44
let t_receivefd fd =
55
Printf.printf "[receiver] t_receivedfd thread started!\n%!";
6-
let buf = "**" in
6+
let buf = Bytes.of_string "**" in
77
let nb_read, remote_saddr, fd_recv = recv_fd fd buf 0 2 [] in
88
Printf.printf "[receiver] Received %d bytes, received fd = %d\n%!" nb_read (int_of_fd fd_recv);
99
let message = "[receiver] I'm the receiver, and I'm writing into the fd you passed to me :p\n" in
10-
let nb_written = write fd_recv message 0 (String.length message) in
10+
let nb_written = write_substring fd_recv message 0 (String.length message) in
1111
assert (nb_written = String.length message)
1212

1313
let t_sendfd fd =
1414
let fd_to_send = stdout in
1515
let buf = " " in
16-
let nb_sent = send_fd fd buf 0 2 [] fd_to_send in
16+
let nb_sent = send_fd_substring fd buf 0 2 [] fd_to_send in
1717
Printf.printf "[sender] sent %d bytes, sent fd = %d\n%!" nb_sent (int_of_fd fd_to_send)
1818

1919
let main () =

test_tuntap/test_tuntap.ml

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,14 @@ open Fd_send_recv
33

44
let t_receivefd fd =
55
Printf.printf "[receiver] t_receivedfd thread started!\n%!";
6-
let buf = String.make (Tuntap.get_ifnamsiz ()) '\000' in
7-
let nb_read, remote_saddr, fd_recv = recv_fd fd buf 0 (String.length buf) [] in
6+
let buf = Bytes.make (Tuntap.get_ifnamsiz ()) '\000' in
7+
let nb_read, remote_saddr, fd_recv = recv_fd fd buf 0 (Bytes.length buf) [] in
88
Printf.printf "[receiver] Received %d bytes [%s], received fd = %d\n%!"
9-
nb_read (String.sub buf 0 nb_read) (int_of_fd fd_recv)
9+
nb_read (Bytes.sub_string buf 0 nb_read) (int_of_fd fd_recv)
1010

1111
let t_sendfd fd =
1212
let fd_to_send, iface_name = Tuntap.opentap () in
13-
let nb_sent = send_fd fd iface_name 0 (String.length iface_name) [] fd_to_send in
13+
let nb_sent = send_fd_substring fd iface_name 0 (String.length iface_name) [] fd_to_send in
1414
Printf.printf "[sender] sent %d bytes [%s], sent fd = %d\n%!" nb_sent
1515
(String.sub iface_name 0 nb_sent) (int_of_fd fd_to_send)
1616

0 commit comments

Comments
 (0)