Skip to content
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
1 change: 1 addition & 0 deletions CHANGES.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ Unreleased
than just RSA).
- Fix a segmentation fault in the ALPN selection callback under OCaml 5 (#89).
- Audit the C FFI and add `CAMLparamX` and `CAMLreturn` calls (#90).
- Add `Ssl.close_notify` to perform a one-way shutdown (#63, #96).

0.5.11 (2022-07-24)
=====
Expand Down
8 changes: 7 additions & 1 deletion src/ssl.ml
Original file line number Diff line number Diff line change
Expand Up @@ -282,7 +282,7 @@ external accept : socket -> unit = "ocaml_ssl_accept"

external flush : socket -> unit = "ocaml_ssl_flush"

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

let open_connection_with_context context sockaddr =
let domain = Unix.domain_of_sockaddr sockaddr in
Expand All @@ -298,6 +298,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 = ssl_shutdown

let shutdown sock =
if not (close_notify sock)
then ignore (close_notify sock : bool)

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 @@ -432,7 +432,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 @@ -1767,12 +1767,16 @@ CAMLprim value ocaml_ssl_shutdown(value socket)

caml_release_runtime_system();
ret = SSL_shutdown(ssl);
if (!ret)
SSL_shutdown(ssl);
caml_acquire_runtime_system();
/* 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