OpenSSL Server list is now a vector.

This commit is contained in:
2026-05-06 01:27:21 -04:00
parent facdb12ffd
commit 77d868f328
+36 -32
View File
@@ -65,6 +65,9 @@
#define CERT_STATUS_USER_ACCEPTED 4 #define CERT_STATUS_USER_ACCEPTED 4
#include <string> #include <string>
#include <vector>
#include <memory>
#include <algorithm>
using namespace std::literals::string_literals; using namespace std::literals::string_literals;
@@ -74,6 +77,12 @@ struct Server_t {
int cert_status; 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{ struct FdMapEntry_t{
int fd; int fd;
int connkey; int connkey;
@@ -98,7 +107,7 @@ static Klist_t *conn_list = NULL;
* If ssl_context is still NULL, this corresponds to TLS being disabled. * If ssl_context is still NULL, this corresponds to TLS being disabled.
*/ */
static SSL_CTX *ssl_context; static SSL_CTX *ssl_context;
static Dlist *servers; static std::vector< std::unique_ptr< Server_t > > servers;
static Dlist *fd_map; static Dlist *fd_map;
static void Tls_connect_cb(int fd, void *vconnkey); static void Tls_connect_cb(int fd, void *vconnkey);
@@ -317,7 +326,6 @@ void a_Tls_openssl_init(void)
Tls_load_certificates(); Tls_load_certificates();
fd_map = dList_new(20); 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; cmp = s1->port - s2->port;
return cmp; return cmp;
} }
/*
* Ordered comparison of server with URL. static bool
*/ Tls_servers_lt( const std::unique_ptr< Server_t > &lhs, const std::unique_ptr< Server_t > &rhs )
static int Tls_servers_by_url_cmp(const void *v1, const void *v2)
{ {
const Server_t *s = (const Server_t *)v1; return Tls_servers_cmp( lhs.get(), rhs.get() ) < 0 ;
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;
}
/* /*
* The purpose here is to permit a single initial connection to a server. * 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) int a_Tls_openssl_connect_ready(const DilloUrl *url)
{ {
Server_t *s;
int ret = TLS_CONNECT_READY; int ret = TLS_CONNECT_READY;
if (ssl_context == NULL) if (ssl_context == NULL)
return TLS_CONNECT_NEVER; 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) if (s->cert_status == CERT_STATUS_RECEIVING)
ret = TLS_CONNECT_NOT_YET; ret = TLS_CONNECT_NOT_YET;
else if (s->cert_status == CERT_STATUS_BAD) 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) if (s->cert_status == CERT_STATUS_NONE)
s->cert_status = CERT_STATUS_RECEIVING; s->cert_status = CERT_STATUS_RECEIVING;
} else { } else {
s = std::make_unique< Server_t >().release(); auto s = std::make_unique< Server_t >();
s->hostname = URL_HOST(url); s->hostname = URL_HOST(url);
s->port = URL_PORT(url); s->port = URL_PORT(url);
s->cert_status = CERT_STATUS_RECEIVING; 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; return ret;
} }
static int Tls_cert_status(const DilloUrl *url) 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,11 +1070,11 @@ static int Tls_examine_certificate(SSL *ssl, Server_t *srv)
*/ */
void a_Tls_openssl_reset_server_state(const DilloUrl *url) void a_Tls_openssl_reset_server_state(const DilloUrl *url)
{ {
if (servers) { const bool found= std::binary_search( begin( servers ), end( servers ), toServer( url ), Tls_servers_lt );
Server_t *s = reinterpret_cast< Server_t * >( dList_find_sorted(servers, url, Tls_servers_by_url_cmp) ); const auto s= std::lower_bound( begin( servers ), end( servers ), toServer( url ), Tls_servers_lt );
if (s && s->cert_status == CERT_STATUS_RECEIVING) if (found and (*s)->cert_status == CERT_STATUS_RECEIVING) {
s->cert_status = CERT_STATUS_NONE; (*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); MSG("SSL_get_error() returned %d on a connect.\n", err1_ret);
} }
} else { } else {
Server_t *srv = reinterpret_cast< Server_t * >( dList_find_sorted(servers, conn->url, Server_t *srv = std::lower_bound( begin( servers ), end( servers ),
Tls_servers_by_url_cmp) ); toServer( conn->url ), Tls_servers_lt )->get();
if (srv->cert_status == CERT_STATUS_RECEIVING) { if (srv->cert_status == CERT_STATUS_RECEIVING) {
/* Making first connection with the server. Show cipher used. */ /* 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) static void Tls_servers_freeall(void)
{ {
if (servers) { int i, n = servers.size();
int i, n = dList_length(servers);
for (i = 0; i < n; i++) { 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) static void Tls_fd_map_remove_all(void)