LEVIATHAN v962456e · 962456eee1

Standard Library

class TlsDrive

Drives a TLS handshake to completion on the event loop.

since 0.1.0-alpha.1linux

Overview

Arming a socket for TLS only starts the handshake; it needs several rounds of reading and writing before the connection is secure. A TlsDrive performs those rounds: it steps the handshake, waits for the socket to become readable or writable in whichever direction the handshake needs next, and when the handshake ends it calls your function once, with the descriptor on success or -1 on failure. It never throws. Most programs call tlsDrive or tlsConnect instead of using the class directly.

Examples

Driving the handshake of a socket that was armed for TLS

int fd = std::sysTcpConnect("192.0.2.10", 443);
if (fd >= 0 && std::sysTlsConnect(fd, "example.com") >= 0) {
    TlsDrive drive = TlsDrive();
    drive.begin(fd, (secured) => {
        if (secured < 0) {
            console.writeln("handshake failed");
        } else {
            console.writeln("secure connection ready");
        }
    });
}
not run — needs a TLS server to complete a handshake with

Methods

begin

begin(int f, (int) => void cb) -> void

Start driving the handshake of an armed descriptor.

The first step runs immediately; if the handshake needs to wait, the rest continues on the event loop. The callback is called once.

Parameters

f
The descriptor of a socket that was already armed for TLS.
cb
The function called with the descriptor when the handshake completes, or with -1 when it fails.

See also

  • tlsDrive — Drive the TLS handshake of a descriptor that is already armed.
  • tlsConnect — Start TLS on a connected client socket and wait for the handshake to finish.