commit 7d3b71e31c73e122b62fec68a7196586f1b93082
parent b567fedfe014dc5c795b5bc8703bc187efd13e27
Author: Quentin Rameau <quinq@fifth.space>
Date: Mon, 12 Sep 2022 00:22:30 +0200
tls: Refactor code
Diffstat:
M | io_tls.c | | | 73 | +++++++++++++++++++++++++++++++++++-------------------------------------- |
1 file changed, 35 insertions(+), 38 deletions(-)
diff --git a/io_tls.c b/io_tls.c
@@ -17,12 +17,11 @@ close_tls(struct cnx *c)
{
int r;
- if (tls) {
- if (c->tls) {
- do {
- r = tls_close(c->tls);
- } while (r == TLS_WANT_POLLIN || r == TLS_WANT_POLLOUT);
- }
+ if (tls && c->tls) {
+ do {
+ r = tls_close(c->tls);
+ } while (r == TLS_WANT_POLLIN || r == TLS_WANT_POLLOUT);
+
tls_free(c->tls);
}
@@ -33,46 +32,44 @@ static int
connect_tls(struct cnx *c, struct addrinfo *ai, const char *host)
{
struct tls *t;
- char *r;
- int s;
+ char *s;
+ int r;
c->tls = NULL;
- s = c->sock;
- if (connect(s, ai->ai_addr, ai->ai_addrlen) == -1)
+ if (connect(c->sock, ai->ai_addr, ai->ai_addrlen) == -1)
return -1;
- if (tls) {
- if ((t = tls_client()) == NULL)
- return -1;
- if (tls_connect_socket(t, s, host) == 0) {
- do {
- s = tls_handshake(t);
- } while (s == TLS_WANT_POLLIN || s == TLS_WANT_POLLOUT);
- if (s == 0) {
- c->tls = t;
- } else {
- diag("Can't establish TLS with \"%s\": %s",
- host, tls_error(t));
- r = uiprompt("Retry on cleartext? [Yn]: ");
- switch (*r) {
- case 'Y':
- case 'y':
- case '\0':
- tls = 0;
- s = -2;
- break;
- default:
- s = -3;
- }
- free(r);
- }
- } else {
- s = -1;
+ if (!tls)
+ return 0;
+
+ if ((t = tls_client()) == NULL)
+ return -1;
+ if (tls_connect_socket(t, c->sock, host) == -1)
+ return -1;
+ do {
+ r = tls_handshake(t);
+ } while (r == TLS_WANT_POLLIN || r == TLS_WANT_POLLOUT);
+ if (r == 0) {
+ c->tls = t;
+ } else {
+ diag("Can't establish TLS with \"%s\": %s",
+ host, tls_error(t));
+ s = uiprompt("Retry on cleartext? [Yn]: ");
+ switch (*s) {
+ case 'Y':
+ case 'y':
+ case '\0':
+ tls = 0;
+ r = -2;
+ break;
+ default:
+ r = -3;
}
+ free(s);
}
- return s;
+ return r;
}
static void