Standard library
TLS
TLS client and server over rustls.
Introduction
TLS client and server over rustls. Import std/net/tls to bind the module as tls. Call free functions as tls.name(...). Struct methods use a receiver value.
/ std/net/tls
This page is the package reference for std/net/tls. Private helpers used only by co-located tests are not listed.
Struct · listener
TLS listener shape returned by tls.listen. The shape is exported as tls.listener. Methods on the receiver are listed next.
/ std/net/tls
; shape: tls.listener
$ listener = tls.listen("127.0.0.1:0", cert)Parameters: No parameters. Type or shape export. Returns: Shape export used for literals and methods.
listener · accept
Accepts the next TLS connection using the given certificate and key PEM text. Failure may return a conn with handle 0. Call form: listener.accept(cert_pem, key_pem).
/ std/net/tls
$ conn = listener.accept(cert_pem, key_pem)
Parameters: cert_pem: certificate PEM text. key_pem: private key PEM text. Returns: conn product for the accepted peer.
listener · close
Closes the TLS listener. Call form: listener.close().
/ std/net/tls
listener.close()
Parameters: No parameters. Returns: The listener receiver.
Struct · conn
TLS connection shape. The shape is exported as tls.conn. Methods on the receiver are listed next.
/ std/net/tls
; shape: tls.conn
$ conn = tls.connect("example.com", 443)Parameters: No parameters. Type or shape export. Returns: Shape export used for literals and methods.
conn · read
Reads up to limit bytes from the TLS connection. Call form: conn.read(limit).
/ std/net/tls
$ data = conn.read(4096)
Parameters: limit: maximum bytes to read. Returns: Bytes read.
conn · write
Writes data over the TLS connection. Call form: conn.write(data).
/ std/net/tls
$ n = conn.write("hi")Parameters: data: bytes or string to send. Returns: Integer bytes written, or failure status.
conn · close
Closes the TLS connection and marks it closed. Call form: conn.close().
/ std/net/tls
conn.close()
Parameters: No parameters. Returns: The connection receiver.
Functions
Free functions on tls. Each function has a short description, an example, then parameters and return shape.
listen
Binds a TLS listener with certificate material. Call form: tls.listen(addr).
/ std/net/tls
$ listener = tls.listen("127.0.0.1:0", cert)Parameters: addr: listen address. cert: certificate material. Returns: TLS listener product.
accept
Accepts the next TLS connection on a listener. Call form: tls.accept(lis, cert_pem, key_pem).
/ std/net/tls
$ conn = tls.accept(listener)
Parameters: listener: TLS listener. Returns: TLS connection product.
connect
Connects a TLS client socket to host:port. Call form: tls.connect(host, port, server_name, ca_pem).
/ std/net/tls
$ conn = tls.connect("example.com", 443)Parameters: host: host name. port: port number. Returns: TLS connection product.
read
Reads bytes from a TLS connection. Call form: tls.read(c, limit).
/ std/net/tls
$ data = tls.read(conn, 4096)
Parameters: conn: TLS connection. limit: maximum bytes. Returns: Bytes read.
write
Writes bytes to a TLS connection. Call form: tls.write(c, data).
/ std/net/tls
$ n = tls.write(conn, "hi")
Parameters: conn: TLS connection. data: bytes or string. Returns: Integer bytes written, or -1 on failure.
close
Closes a TLS connection. Call form: tls.close(c).
/ std/net/tls
tls.close(conn)
Parameters: conn: TLS connection. Returns: None.
close_listener
Closes a TLS listener. Call form: tls.close_listener(lis).
/ std/net/tls
tls.close_listener(listener)
Parameters: listener: TLS listener. Returns: None.
load_pem
Loads PEM certificate or key material. Call form: tls.load_pem(path).
/ std/net/tls
$ cert = tls.load_pem(pem_text)
Parameters: pem: PEM-encoded text. Returns: Certificate material product.