commit 7a08224d33d3bdb1b8f45665daa8b72dbc3fa168
parent 2f7e863cb5cbe52cd24c9021d637c5528c47bba6
Author: hhvn <dev@hhvn.uk>
Date: Sun, 20 Feb 2022 17:13:08 +0000
Fix tls reconnects by allocating tls structs on connect
Diffstat:
M | src/serv.c | | | 71 | ++++++++++++++++++++++++++++++++++++++++++++++------------------------- |
1 file changed, 46 insertions(+), 25 deletions(-)
diff --git a/src/serv.c b/src/serv.c
@@ -106,28 +106,10 @@ serv_create(char *name, char *host, char *port, char *nick,
server->lastconnected = server->lastrecv = server->pingsent = 0;
#ifdef TLS
+ server->tls_verify = tls_verify;
server->tls = tls;
server->tls_ctx = NULL;
server->tls_conf = NULL;
- if (server->tls && (server->tls_conf = tls_config_new()) == NULL) {
- ui_tls_config_error(server->tls_conf, "tls_config_new()");
- server->tls = 0;
- }
-
- if (server->tls && !tls_verify) {
- tls_config_insecure_noverifycert(server->tls_conf);
- tls_config_insecure_noverifyname(server->tls_conf);
- }
-
- if (server->tls && (server->tls_ctx = tls_client()) == NULL) {
- ui_perror("tls_client()");
- server->tls = 0;
- }
-
- if (server->tls && tls_configure(server->tls_ctx, server->tls_conf) == -1) {
- ui_tls_error(server->tls_ctx, "tls_configure()");
- server->tls = 0;
- }
#else
if (tls)
hist_format(server->history, Activity_error, HIST_SHOW,
@@ -267,6 +249,33 @@ serv_connect(struct Server *server) {
#ifdef TLS
if (server->tls) {
+ if (server->tls_conf)
+ tls_config_free(server->tls_conf);
+ if (server->tls_ctx)
+ tls_free(server->tls_ctx);
+ server->tls_conf = NULL;
+ server->tls_ctx = NULL;
+
+ if ((server->tls_conf = tls_config_new()) == NULL) {
+ ui_tls_config_error(server->tls_conf, "tls_config_new()");
+ server->tls = 0;
+ }
+
+ if (!server->tls_verify) {
+ tls_config_insecure_noverifycert(server->tls_conf);
+ tls_config_insecure_noverifyname(server->tls_conf);
+ }
+
+ if ((server->tls_ctx = tls_client()) == NULL) {
+ ui_perror("tls_client()");
+ server->tls = 0;
+ }
+
+ if (tls_configure(server->tls_ctx, server->tls_conf) == -1) {
+ ui_tls_error(server->tls_ctx, "tls_configure()");
+ server->tls = 0;
+ }
+
if (tls_connect_socket(server->tls_ctx, fd, server->host) == -1) {
hist_format(server->history, Activity_error, HIST_SHOW,
"SELF_CONNECTLOST %s %s %s :%s",
@@ -346,13 +355,25 @@ serv_disconnect(struct Server *server, int reconnect, char *msg) {
if (msg)
ircprintf(server, "QUIT %s\r\n", msg);
#ifdef TLS
- if (server->tls)
- tls_close(server->tls_ctx);
+ if (server->tls) {
+ if (server->tls_ctx) {
+ tls_close(server->tls_ctx);
+ tls_reset(server->tls_ctx);
+ tls_free(server->tls_ctx);
+ }
+ if (server->tls_conf)
+ tls_config_free(server->tls_conf);
+ server->tls_ctx = NULL;
+ server->tls_conf = NULL;
+ } else {
+#endif /* TLS */
+ shutdown(server->rfd, SHUT_RDWR);
+ shutdown(server->wfd, SHUT_RDWR);
+ close(server->rfd);
+ close(server->wfd);
+#ifdef TLS
+ }
#endif /* TLS */
- shutdown(server->rfd, SHUT_RDWR);
- shutdown(server->wfd, SHUT_RDWR);
- close(server->rfd);
- close(server->wfd);
server->rfd = server->wfd = server->rpollfd->fd = -1;
server->status = ConnStatus_notconnected;