Sortix cross-nightly manual
This manual documents Sortix cross-nightly. You can instead view this document in the latest official manual.
NAME
tls_read, tls_write, tls_handshake, tls_error, tls_close — use a TLS connectionSYNOPSIS
#include <tls.h>tls_read(struct tls *ctx, void *buf, size_t buflen);
tls_write(struct tls *ctx, const void *buf, size_t buflen);
tls_handshake(struct tls *ctx);
tls_error(struct tls *ctx);
tls_close(struct tls *ctx);
DESCRIPTION
tls_read() reads buflen bytes of data from the socket into buf. It returns the amount of data read.RETURN VALUES
tls_read() and tls_write() return a size on success or -1 on error.- TLS_WANT_POLLIN
- The underlying read file descriptor needs to be readable in order to continue.
- TLS_WANT_POLLOUT
- The underlying write file descriptor needs to be writeable in order to continue.
EXAMPLES
The following example demonstrates how to handle TLS writes on a blocking file descriptor:... while (len > 0) { ssize_t ret; ret = tls_write(ctx, buf, len); if (ret == TLS_WANT_POLLIN || ret == TLS_WANT_POLLOUT) continue; if (ret == -1) errx(1, "tls_write: %s", tls_error(ctx)); buf += ret; len -= ret; } ...
... pfd[0].fd = fd; pfd[0].events = POLLIN|POLLOUT; while (len > 0) { nready = poll(pfd, 1, 0); if (nready == -1) err(1, "poll"); if ((pfd[0].revents & (POLLERR|POLLNVAL))) errx(1, "bad fd %d", pfd[0].fd); if ((pfd[0].revents & (pfd[0].events|POLLHUP))) { ssize_t ret; ret = tls_write(ctx, buf, len); if (ret == TLS_WANT_POLLIN) pfd[0].events = POLLIN; else if (ret == TLS_WANT_POLLOUT) pfd[0].events = POLLOUT; else if (ret == -1) errx(1, "tls_write: %s", tls_error(ctx)); else { buf += ret; len -= ret; } } } ...