From 08f01afee4192040f24d1846b4af317b3587b2a7 Mon Sep 17 00:00:00 2001 From: Andri Yngvason Date: Sun, 30 Apr 2023 16:37:30 +0000 Subject: [PATCH] Make stream_upgrade_to_tls a virtual method --- include/stream.h | 2 ++ src/stream-gnutls.c | 5 ++++- src/stream-tcp.c | 1 + src/stream.c | 6 ++++++ 4 files changed, 13 insertions(+), 1 deletion(-) diff --git a/include/stream.h b/include/stream.h index 706a842..ce079eb 100644 --- a/include/stream.h +++ b/include/stream.h @@ -70,6 +70,7 @@ struct stream_impl { stream_req_fn on_done, void* userdata); int (*send_first)(struct stream*, struct rcbuf* payload); void (*exec_and_send)(struct stream*, stream_exec_fn, void* userdata); + int (*upgrade_to_tls)(struct stream*, void* context); }; // TODO: Move some of these struct members into their respective implementation @@ -114,5 +115,6 @@ int stream_send_first(struct stream* self, struct rcbuf* payload); void stream_exec_and_send(struct stream* self, stream_exec_fn, void* userdata); #ifdef ENABLE_TLS +int stream_upgrade_tcp_to_tls(struct stream* self, void* context); int stream_upgrade_to_tls(struct stream* self, void* context); #endif diff --git a/src/stream-gnutls.c b/src/stream-gnutls.c index 9b81525..0c67e93 100644 --- a/src/stream-gnutls.c +++ b/src/stream-gnutls.c @@ -33,6 +33,7 @@ #include "stream.h" #include "stream-common.h" #include "sys/queue.h" +#include "neatvnc.h" static int stream__try_tls_accept(struct stream* self); @@ -241,10 +242,12 @@ static struct stream_impl impl = { .send = stream_gnutls_send, }; -int stream_upgrade_to_tls(struct stream* self, void* context) +int stream_upgrade_tcp_to_tls(struct stream* self, void* context) { int rc; + nvnc_log(NVNC_LOG_DEBUG, "Uprading stream %p to TLS", self); + rc = gnutls_init(&self->tls_session, GNUTLS_SERVER | GNUTLS_NONBLOCK); if (rc != GNUTLS_E_SUCCESS) return -1; diff --git a/src/stream-tcp.c b/src/stream-tcp.c index 03a3d9b..d3112c9 100644 --- a/src/stream-tcp.c +++ b/src/stream-tcp.c @@ -251,6 +251,7 @@ static struct stream_impl impl = { .send = stream_tcp_send, .send_first = stream_tcp_send_first, .exec_and_send = stream_tcp_exec_and_send, + .upgrade_to_tls = stream_upgrade_tcp_to_tls, }; struct stream* stream_new(int fd, stream_event_fn on_event, void* userdata) diff --git a/src/stream.c b/src/stream.c index 457035c..060bc74 100644 --- a/src/stream.c +++ b/src/stream.c @@ -65,3 +65,9 @@ void stream_exec_and_send(struct stream* self, stream_exec_fn exec_fn, else stream_send(self, exec_fn(self, userdata), NULL, NULL); } + +int stream_upgrade_to_tls(struct stream* self, void* context) +{ + assert(self->impl && self->impl->upgrade_to_tls); + return self->impl->upgrade_to_tls(self, context); +}