zygo

ncurses gopher client
Log | Files | Refs

commit 1a1744a294a88e1b916c9e771272a8c0be61ea53
parent d5e9cee0a73c92066076a476056b39759fdbb4bf
Author: hhvn <dev@hhvn.uk>
Date:   Tue, 18 Jan 2022 19:38:32 +0000

tls.c zygo.c zygo.1: fix TLS

Diffstat:
Mtls.c | 45++++++++++++++++++++++++++++++---------------
Mzygo.1 | 6++++--
Mzygo.c | 3+++
3 files changed, 37 insertions(+), 17 deletions(-)

diff --git a/tls.c b/tls.c @@ -3,15 +3,10 @@ #include <tls.h> #include "zygo.h" -static struct tls *ctx = NULL; -static struct tls_config *conf = NULL; -static int fd; -static int tls; - -#define net_free() \ - if (ai) freeaddrinfo(ai); \ - if (ctx) { tls_free(ctx); ctx = NULL; } \ - if (conf) { tls_config_free(conf); conf = NULL; } +struct tls *ctx = NULL; +struct tls_config *conf = NULL; +int fd; +int tls; int net_connect(Elem *e) { @@ -63,6 +58,7 @@ net_connect(Elem *e) { error("could not tls-ify connection to %s:%s", e->server, e->port); goto fail; } + if (tls_handshake(ctx) == -1) { error("could not perform tls handshake with %s:%s", e->server, e->port); goto fail; @@ -73,7 +69,16 @@ net_connect(Elem *e) { return 0; fail: - net_free(); + if (ai) + freeaddrinfo(ai); + if (ctx) { + tls_free(ctx); + ctx = NULL; + } + if (conf) { + tls_config_free(conf); + conf = NULL; + } return -1; } @@ -99,11 +104,19 @@ net_write(void *buf, size_t count) { int ret; if (tls) { - do { - ret = tls_write(ctx, buf, count); - } while (ret == TLS_WANT_POLLIN || ret == TLS_WANT_POLLOUT); - if (ret == -1) - error("tls_write(): %s", tls_error(ctx)); + while (count > 0) { + switch (ret = tls_write(ctx, buf, count)) { + case TLS_WANT_POLLIN: + case TLS_WANT_POLLOUT: + break; + case -1: + error("tls_write(): %s", tls_error(ctx)); + break; + default: + buf += ret; + count -= ret; + } + } } else { ret = write(fd, buf, count); } @@ -120,7 +133,9 @@ net_close(void) { ret = tls_close(ctx); } while (ret == TLS_WANT_POLLIN || TLS_WANT_POLLOUT); tls_free(ctx); + ctx = NULL; tls_config_free(conf); + conf = NULL; } return close(fd); diff --git a/zygo.1 b/zygo.1 @@ -30,9 +30,11 @@ is a rewrite of .Xr cgo 1 that tidies up a lot of stuff, and has pager functionality using ncurses. +.Ss TLS .Nm -supports gopher over tls, although it may be buggy. -The user may attempt to downgrade any tls connection if it fails. +supports gopher over TLS using the gophers:// uri schema. +If a gopher menu is accessed over TLS, +any links with the same server and port will be accessed over TLS. .Ss Name .Nm is taken from the first four letters of the gopher genus Zygogeomys. diff --git a/zygo.c b/zygo.c @@ -350,6 +350,8 @@ list_append(List **l, Elem *e) { elem = elem_dup(e); if (elem->type != 'i' && elem->type != '3') elem->id = ++(*l)->lastid; + else + elem->id = 0; if (!(*l)->elems) { (*l)->len = 1; @@ -961,6 +963,7 @@ run(void) { checkcurrent(); go(current, 0); draw_page(); + draw_bar(); break; case 'g': ui.scroll = 0;