Skip to content

Commit

Permalink
Merge pull request #5 from mseri/master
Browse files Browse the repository at this point in the history
Update interface for safe-string
  • Loading branch information
mseri committed May 24, 2018
2 parents 5d763ca + 28f1c6b commit 725877f
Show file tree
Hide file tree
Showing 5 changed files with 21 additions and 14 deletions.
7 changes: 5 additions & 2 deletions lib/fd_send_recv.ml
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,11 @@ exception Unix_error of int

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

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"
external recv_fd : Unix.file_descr -> string -> int -> int -> Unix.msg_flag list -> int * Unix.sockaddr * Unix.file_descr = "stub_unix_recv_fd"
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"
external recv_fd : Unix.file_descr -> bytes -> int -> int -> Unix.msg_flag list -> int * Unix.sockaddr * Unix.file_descr = "stub_unix_recv_fd"

let send_fd_substring channel_fd buf ofs len flags fd_to_send =
send_fd channel_fd (Bytes.unsafe_of_string buf) ofs len flags fd_to_send

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

Expand Down
8 changes: 6 additions & 2 deletions lib/fd_send_recv.mli
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
exception Unix_error of int
(** Thrown by the low-level C functions *)

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

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

val send_fd_substring : Unix.file_descr -> string -> int -> int ->
Unix.msg_flag list -> Unix.file_descr -> int
(** Like [send_fd] but takes a string *)

val int_of_fd : Unix.file_descr -> int
(** [int_of_fd fd] returns the underlying unix integer file descriptor
associated with OCaml Unix.file_descr [fd]. *)
Expand Down
6 changes: 3 additions & 3 deletions test/test.ml
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,17 @@ open Fd_send_recv

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

let t_sendfd fd =
let fd_to_send = stdout in
let buf = " " in
let nb_sent = send_fd fd buf 0 2 [] fd_to_send in
let nb_sent = send_fd_substring fd buf 0 2 [] fd_to_send in
Printf.printf "[sender] sent %d bytes, sent fd = %d\n%!" nb_sent (int_of_fd fd_to_send)

let main () =
Expand Down
6 changes: 3 additions & 3 deletions test/test_fork.ml
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,17 @@ open Fd_send_recv

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

let t_sendfd fd =
let fd_to_send = stdout in
let buf = " " in
let nb_sent = send_fd fd buf 0 2 [] fd_to_send in
let nb_sent = send_fd_substring fd buf 0 2 [] fd_to_send in
Printf.printf "[sender] sent %d bytes, sent fd = %d\n%!" nb_sent (int_of_fd fd_to_send)

let main () =
Expand Down
8 changes: 4 additions & 4 deletions test_tuntap/test_tuntap.ml
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,14 @@ open Fd_send_recv

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

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

Expand Down

0 comments on commit 725877f

Please sign in to comment.