From 77d868f3281fc1905b16b5957893781274db33ddcfdb15be98e9abb3fc96ca3a Mon Sep 17 00:00:00 2001 From: ADAM David Alan Martin Date: Wed, 6 May 2026 01:27:21 -0400 Subject: [PATCH] OpenSSL Server list is now a vector. --- src/IO/tls_openssl.cc | 70 +++++++++++++++++++++++-------------------- 1 file changed, 37 insertions(+), 33 deletions(-) diff --git a/src/IO/tls_openssl.cc b/src/IO/tls_openssl.cc index 0e22874..5333017 100644 --- a/src/IO/tls_openssl.cc +++ b/src/IO/tls_openssl.cc @@ -65,6 +65,9 @@ #define CERT_STATUS_USER_ACCEPTED 4 #include +#include +#include +#include using namespace std::literals::string_literals; @@ -74,6 +77,12 @@ struct Server_t { int cert_status; }; +static auto +toServer( const DilloUrl *const url ) +{ + return std::make_unique< Server_t >( Server_t{ URL_HOST( url ), URL_PORT( url ), CERT_STATUS_NONE } ); +} + struct FdMapEntry_t{ int fd; int connkey; @@ -98,7 +107,7 @@ static Klist_t *conn_list = NULL; * If ssl_context is still NULL, this corresponds to TLS being disabled. */ static SSL_CTX *ssl_context; -static Dlist *servers; +static std::vector< std::unique_ptr< Server_t > > servers; static Dlist *fd_map; static void Tls_connect_cb(int fd, void *vconnkey); @@ -317,7 +326,6 @@ void a_Tls_openssl_init(void) Tls_load_certificates(); fd_map = dList_new(20); - servers = dList_new(8); } /* @@ -378,20 +386,12 @@ static int Tls_servers_cmp(const void *v1, const void *v2) cmp = s1->port - s2->port; return cmp; } -/* - * Ordered comparison of server with URL. - */ -static int Tls_servers_by_url_cmp(const void *v1, const void *v2) + +static bool +Tls_servers_lt( const std::unique_ptr< Server_t > &lhs, const std::unique_ptr< Server_t > &rhs ) { - const Server_t *s = (const Server_t *)v1; - const DilloUrl *url = (const DilloUrl *)v2; - - int cmp = dStrAsciiCasecmp(s->hostname.c_str(), URL_HOST(url)); - - if (!cmp) - cmp = s->port - URL_PORT(url); - return cmp; -} + return Tls_servers_cmp( lhs.get(), rhs.get() ) < 0 ; +}; /* * The purpose here is to permit a single initial connection to a server. @@ -402,13 +402,14 @@ static int Tls_servers_by_url_cmp(const void *v1, const void *v2) */ int a_Tls_openssl_connect_ready(const DilloUrl *url) { - Server_t *s; int ret = TLS_CONNECT_READY; if (ssl_context == NULL) return TLS_CONNECT_NEVER; - if ((s = reinterpret_cast< Server_t * >( dList_find_sorted(servers, url, Tls_servers_by_url_cmp) ))) { + if (std::binary_search( begin( servers ), end( servers ), toServer( url ), Tls_servers_lt ) ) { + auto s = std::lower_bound( begin( servers ), end( servers ), toServer( url ), Tls_servers_lt )->get(); + if (s->cert_status == CERT_STATUS_RECEIVING) ret = TLS_CONNECT_NOT_YET; else if (s->cert_status == CERT_STATUS_BAD) @@ -417,21 +418,26 @@ int a_Tls_openssl_connect_ready(const DilloUrl *url) if (s->cert_status == CERT_STATUS_NONE) s->cert_status = CERT_STATUS_RECEIVING; } else { - s = std::make_unique< Server_t >().release(); + auto s = std::make_unique< Server_t >(); s->hostname = URL_HOST(url); s->port = URL_PORT(url); s->cert_status = CERT_STATUS_RECEIVING; - dList_insert_sorted(servers, s, Tls_servers_cmp); + servers.push_back( std::move( s ) ); + std::sort( begin( servers ), end( servers ), Tls_servers_lt ); } return ret; } static int Tls_cert_status(const DilloUrl *url) { - Server_t *s = reinterpret_cast< Server_t * >( dList_find_sorted(servers, url, Tls_servers_by_url_cmp) ); + if (not std::binary_search( begin( servers ), end( servers ), toServer( url ), Tls_servers_lt ) ) + { + return CERT_STATUS_NONE; + } + auto s = std::lower_bound( begin( servers ), end( servers ), toServer( url ), Tls_servers_lt )->get(); - return s ? s->cert_status : CERT_STATUS_NONE; + return s->cert_status; } /* @@ -1064,12 +1070,12 @@ static int Tls_examine_certificate(SSL *ssl, Server_t *srv) */ void a_Tls_openssl_reset_server_state(const DilloUrl *url) { - if (servers) { - Server_t *s = reinterpret_cast< Server_t * >( dList_find_sorted(servers, url, Tls_servers_by_url_cmp) ); + const bool found= std::binary_search( begin( servers ), end( servers ), toServer( url ), Tls_servers_lt ); + const auto s= std::lower_bound( begin( servers ), end( servers ), toServer( url ), Tls_servers_lt ); - if (s && s->cert_status == CERT_STATUS_RECEIVING) - s->cert_status = CERT_STATUS_NONE; - } + if (found and (*s)->cert_status == CERT_STATUS_RECEIVING) { + (*s)->cert_status = CERT_STATUS_NONE; + } } /* @@ -1188,8 +1194,8 @@ static void Tls_connect(int fd, int connkey) MSG("SSL_get_error() returned %d on a connect.\n", err1_ret); } } else { - Server_t *srv = reinterpret_cast< Server_t * >( dList_find_sorted(servers, conn->url, - Tls_servers_by_url_cmp) ); + Server_t *srv = std::lower_bound( begin( servers ), end( servers ), + toServer( conn->url ), Tls_servers_lt )->get(); if (srv->cert_status == CERT_STATUS_RECEIVING) { /* Making first connection with the server. Show cipher used. */ @@ -1374,14 +1380,12 @@ void a_Tls_openssl_close_by_fd(int fd) static void Tls_servers_freeall(void) { - if (servers) { - int i, n = dList_length(servers); + int i, n = servers.size(); for (i = 0; i < n; i++) { - std::unique_ptr< Server_t > s { reinterpret_cast< Server_t * >( dList_nth_data(servers, i) ) }; + std::unique_ptr< Server_t > s { std::move( servers.at( i ) ) }; } - dList_free(servers); - } + servers.clear(); } static void Tls_fd_map_remove_all(void)