Networking for MoonBit: connections, listeners and dialers over TCP, TLS and QUIC.
// A TLS server: the handshake is done before the handler sees the connection.
let tls = @tls.Server::new(@x509.pems(cert_pem), @x509.PrivateKey::pem(key_pem), alpn=["h2", "http/1.1"])
@tcp.serve(tls~, (conn, peer) => {
while conn.read_until("\n") is Some(line) {
conn.write(line + "\n")
}
})
// A client: resolved, connected, the chain judged against the roots.
let conn = @tcp.dial("example.com", port=443, tls=@tls.Client::new(@roots.mozilla(), alpn=["http/1.1"]))
conn.write("GET / HTTP/1.1\r\nHost: example.com\r\n\r\n")| Package | What it does | State |
|---|---|---|
| tcp | TCP and TLS over TCP: a connection that reads and writes the same either way, the listener and accept loop, the dialler, a sniff that tells an HTTP/2 preface from an HTTP/1.1 request without consuming it, TLS and the clear on one port, and either side of STARTTLS | 0.1.4 |
| Conn::plain(sock) | A socket in the clear |
| Conn::server(sock, config) | The server side of TLS, returned after the handshake |
| Conn::client(sock, config, host) | The client side, returned after the server's chain is judged |
| starts_with(prefix) | Whether the stream opens with prefix; reads only as far as the first byte that differs |
| opens_tls() | Whether the stream opens with a TLS handshake record, without consuming it |
| secure(config) | The server side of TLS on a connection still in the clear, starting from what a sniff read ahead |
| alpn(), server_name(), peer() | What the handshake settled; None and empty in the clear |
| shutdown(), close() | close_notify, then the socket |
let listener = @tcp.listen(port=0, tls~)
let port = listener.addr().port()
listener.serve((conn, peer) => ...)| Script | What it checks |
|---|---|
| scripts/tls_interop.sh | moontls against OpenSSL both ways: every key type, suite and group, record sizes and padding, KeyUpdate, tickets, ALPN, SNI, HelloRetryRequest, and the chains and ClientHellos that must be refused |
| scripts/tls_fuzz.sh | The moontls server under tlsfuzzer's TLS 1.3 scripts, about 3,600 cases of wrong lengths, wrong order and wrong values, each with the alert it is owed |
moon build --target native
bash scripts/tls_interop.sh
TLSFUZZER=path/to/tlsfuzzer PYTHON=python-with-tlslite-ng bash scripts/tls_fuzz.shmoon add moonbitstack/moonnetInstall
Download zipNetworking for MoonBit: connections, listeners and dialers over TCP, TLS and QUIC.