Skip to content

Commit 422a115

Browse files
author
Christopher Zimmermann
committed
add close_notify as raw SSL_shutdown
this is needed for sending EOF, aka one-way shutdown.
1 parent 4960600 commit 422a115

File tree

3 files changed

+23
-7
lines changed

3 files changed

+23
-7
lines changed

src/ssl.ml

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -276,7 +276,7 @@ external accept : socket -> unit = "ocaml_ssl_accept"
276276

277277
external flush : socket -> unit = "ocaml_ssl_flush"
278278

279-
external shutdown : socket -> unit = "ocaml_ssl_shutdown"
279+
external shutdown : socket -> bool = "ocaml_ssl_shutdown"
280280

281281
let open_connection_with_context context sockaddr =
282282
let domain =
@@ -296,6 +296,12 @@ let open_connection_with_context context sockaddr =
296296
let open_connection ssl_method sockaddr =
297297
open_connection_with_context (create_context ssl_method Client_context) sockaddr
298298

299+
let close_notify = shutdown
300+
301+
let rec shutdown sock =
302+
if not (close_notify sock)
303+
then shutdown sock
304+
299305
let shutdown_connection = shutdown
300306

301307
let output_string ssl s =

src/ssl.mli

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -421,7 +421,13 @@ val accept : socket -> unit
421421
(** Flush an SSL connection. *)
422422
val flush : socket -> unit
423423

424-
(** Close an SSL connection. *)
424+
(** send close notify to the peer. This is SSL_shutdown(3).
425+
* returns [true] if shutdown is finished, [false] in case [close_notify]
426+
* needs to be called a second time. *)
427+
val close_notify : socket -> bool
428+
429+
(** Close a SSL connection.
430+
* Send close notify to the peer and wait for close notify from peer. *)
425431
val shutdown : socket -> unit
426432

427433

src/ssl_stubs.c

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1660,12 +1660,16 @@ CAMLprim value ocaml_ssl_shutdown(value socket)
16601660

16611661
caml_enter_blocking_section();
16621662
ret = SSL_shutdown(ssl);
1663-
if (!ret)
1664-
SSL_shutdown(ssl);
16651663
caml_leave_blocking_section();
1666-
/* close(SSL_get_fd(SSL_val(socket))); */
1667-
1668-
CAMLreturn(Val_unit);
1664+
switch (ret) {
1665+
case 0:
1666+
case 1:
1667+
/* close(SSL_get_fd(SSL_val(socket))); */
1668+
CAMLreturn(Val_int(ret));
1669+
default:
1670+
ret = SSL_get_error(ssl, ret);
1671+
caml_raise_with_arg(*caml_named_value("ssl_exn_connection_error"), Val_int(ret));
1672+
}
16691673
}
16701674

16711675
/* ======================================================== */

0 commit comments

Comments
 (0)