Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix building on OpenBSD, allow one-way shutdown #63

Merged
merged 1 commit into from
Sep 26, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 7 additions & 1 deletion src/ssl.ml
Original file line number Diff line number Diff line change
Expand Up @@ -276,7 +276,7 @@ external accept : socket -> unit = "ocaml_ssl_accept"

external flush : socket -> unit = "ocaml_ssl_flush"

external shutdown : socket -> unit = "ocaml_ssl_shutdown"
external shutdown : socket -> bool = "ocaml_ssl_shutdown"

let open_connection_with_context context sockaddr =
let domain = Unix.domain_of_sockaddr sockaddr in
Expand All @@ -292,6 +292,12 @@ let open_connection_with_context context sockaddr =
let open_connection ssl_method sockaddr =
open_connection_with_context (create_context ssl_method Client_context) sockaddr

let close_notify = shutdown

let rec shutdown sock =
if not (close_notify sock)
then shutdown sock
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should this be close_notify to prevent unbounded recursion?

Suggested change
then shutdown sock
then close_notify sock


let shutdown_connection = shutdown

let output_string ssl s =
Expand Down
8 changes: 7 additions & 1 deletion src/ssl.mli
Original file line number Diff line number Diff line change
Expand Up @@ -421,7 +421,13 @@ val accept : socket -> unit
(** Flush an SSL connection. *)
val flush : socket -> unit

(** Close an SSL connection. *)
(** Send close notify to the peer. This is SSL_shutdown(3).
* returns [true] if shutdown is finished, [false] in case [close_notify]
* needs to be called a second time. *)
val close_notify : socket -> bool

(** Close a SSL connection.
* Send close notify to the peer and wait for close notify from peer. *)
val shutdown : socket -> unit


Expand Down
14 changes: 9 additions & 5 deletions src/ssl_stubs.c
Original file line number Diff line number Diff line change
Expand Up @@ -1660,12 +1660,16 @@ CAMLprim value ocaml_ssl_shutdown(value socket)

caml_enter_blocking_section();
ret = SSL_shutdown(ssl);
if (!ret)
SSL_shutdown(ssl);
caml_leave_blocking_section();
/* close(SSL_get_fd(SSL_val(socket))); */

CAMLreturn(Val_unit);
switch (ret) {
case 0:
case 1:
/* close(SSL_get_fd(SSL_val(socket))); */
CAMLreturn(Val_int(ret));
default:
ret = SSL_get_error(ssl, ret);
caml_raise_with_arg(*caml_named_value("ssl_exn_connection_error"), Val_int(ret));
}
}

/* ======================================================== */
Expand Down