package mnet-tls

  1. Overview
  2. Docs
Legend:
Page
Library
Module
Module type
Parameter
Class
Class type
Source

Module Mnet_tlsSource

TLS sessions over Mnet TCP connections.

This module wraps ocaml-tls (a pure OCaml TLS implementation) with the effectful I/O provided by Mnet.TCP. It provides the same read/write/close interface as Mnet.TCP but with transparent encryption.

A TLS session is created from an existing Mnet.TCP.flow using either client_of_fd (for outgoing connections) or server_of_fd (for incoming connections). The TLS handshake is performed during creation.

let flow = Mnet.TCP.connect tcp (Ipaddr.V4 server, 443) in
let tls_config = Tls.Config.client ~authenticator () in
let tls = Mnet_tls.client_of_fd tls_config flow in
Mnet_tls.write tls "GET / HTTP/1.1\r\n\r\n";
let buf = Bytes.create 4096 in
let len = Mnet_tls.read tls buf in
Mnet_tls.close tls
Sourceexception Tls_alert of Tls.Packet.alert_type
Sourceexception Tls_failure of Tls.Engine.failure
Sourceexception Closed_by_peer
Sourcetype t

Abstract type of a session.

Sourceval file_descr : t -> Mnet.TCP.flow

file_descr returns the underlying file-descriptor used by the given TLS socket.

Sourceval read : t -> ?off:int -> ?len:int -> bytes -> int

read t buf ~off ~len reads up to len bytes (defaults to Bytes.length buf - off) from the given TLS session t, storing them in byte sequence buf, starting at position off in buf (defaults to 0). It returns the actual number of characters read, between 0 and len (inclusive).

  • raises Tls_alert

    if a TLS alert is received during the read.

Sourceval really_read : t -> ?off:int -> ?len:int -> bytes -> unit

really_read fd buf ~off ~len reads len bytes (defaults to Bytes.length buf - off) from the given TLS socket fd, storing them in byte sequence buf, starting at position off in buf (defaults to 0). If len = 0, really_read does nothing.

  • raises End_of_file

    if Unix.read returns 0 before len characters have been read.

Sourceval write : t -> ?off:int -> ?len:int -> string -> unit

write t str ~off ~len writes len bytes (defaults to String.length str - off) from byte sequence str, starting at offset off (defaults to 0), to the given TLS socket fd.

  • raises Closed_by_peer

    if t is connected to a peer whose reading end is closed. Similar to the EPIPE error for pipe/socket connected.

Sourceval close : t -> unit

close flow closes the TLS session and the underlying file-descriptor.

Sourceval shutdown : t -> [ `read | `write | `read_write ] -> unit

shutdown t direction closes the direction of the TLS session t. If `read_write or `write is closed, a TLS close-notify is sent to the other endpoint. If this results in a fully-closed session (or an errorneous session), the underlying file descriptor is closed.

Sourceval client_of_fd : Tls.Config.client -> ?host:[ `host ] Domain_name.t -> ?ip:Ipaddr.t -> Mnet.TCP.flow -> t

client_of_flow client ~host ~ip fd is t, after client-side TLS handshake of fd using client configuration and host or ip.

  • raises End_of_file

    if we are not able to complete the handshake.

Sourceval server_of_fd : Tls.Config.server -> Mnet.TCP.flow -> t

server_of_fd server fd is t, after server-side TLS handshake of fd using server configuration.

  • raises End_of_file

    if we are not able to complete the handshake.

Sourceval epoch : t -> Tls.Core.epoch_data option

epoch t returns epoch, which contains information of the active session.