From 566f9dd587e9fbd696cd9408cc971527e328db2b9178e91f0feb003df744ab1c Mon Sep 17 00:00:00 2001 From: ADAM David Alan Martin Date: Wed, 6 May 2026 01:38:56 -0400 Subject: [PATCH] OpenSSL fd map a real map now. --- src/IO/tls_openssl.cc | 79 ++++++++----------------------------------- 1 file changed, 14 insertions(+), 65 deletions(-) diff --git a/src/IO/tls_openssl.cc b/src/IO/tls_openssl.cc index 5333017..179a5b0 100644 --- a/src/IO/tls_openssl.cc +++ b/src/IO/tls_openssl.cc @@ -64,6 +64,7 @@ #define CERT_STATUS_BAD 3 #define CERT_STATUS_USER_ACCEPTED 4 +#include #include #include #include @@ -83,11 +84,6 @@ 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; -}; - /* * Data type for TLS connection information */ @@ -108,33 +104,18 @@ static Klist_t *conn_list = NULL; */ static SSL_CTX *ssl_context; static std::vector< std::unique_ptr< Server_t > > servers; -static Dlist *fd_map; +static std::map< int, int > fd_map; static void Tls_connect_cb(int fd, void *vconnkey); -/* - * Compare by FD. - */ -static int Tls_fd_map_cmp(const void *v1, const void *v2) -{ - int fd = VOIDP2INT(v2); - const FdMapEntry_t *e = reinterpret_cast< const FdMapEntry_t * >( v1 ); - - return (fd != e->fd); -} - static void Tls_fd_map_add_entry(int fd, int connkey) { - FdMapEntry_t *e = std::make_unique< FdMapEntry_t >().release(); - e->fd = fd; - e->connkey = connkey; - - if (dList_find_custom(fd_map, INT2VOIDP(e->fd), Tls_fd_map_cmp)) { - MSG_ERR("TLS FD ENTRY ALREADY FOUND FOR %d\n", e->fd); + if( fd_map.contains( fd ) ) { + MSG_ERR("TLS FD ENTRY ALREADY FOUND FOR %d\n", fd); assert(0); } - dList_append(fd_map, e); + fd_map[ fd ]= connkey; //MSG("ADD ENTRY %d %s\n", e->fd, URL_STR(sd->url)); } @@ -143,12 +124,9 @@ static void Tls_fd_map_add_entry(int fd, int connkey) */ static void Tls_fd_map_remove_entry(int fd) { - std::unique_ptr< FdMapEntry_t > data { reinterpret_cast< FdMapEntry_t * >( dList_find_custom(fd_map, INT2VOIDP(fd), Tls_fd_map_cmp) ) }; - //MSG("REMOVE ENTRY %d\n", fd); - if (data) { - dList_remove_fast(fd_map, data.get()); - data.reset(); + if (fd_map.contains( fd )) { + fd_map.erase( fd ); } else { MSG("TLS FD ENTRY NOT FOUND FOR %d\n", fd); } @@ -162,11 +140,9 @@ void *a_Tls_openssl_connection(int fd) { Conn_t *conn; - if (fd_map) { - FdMapEntry_t *fme = reinterpret_cast< FdMapEntry_t * >( dList_find_custom(fd_map, INT2VOIDP(fd), - Tls_fd_map_cmp) ); - - if (fme && (conn = reinterpret_cast< Conn_t * >( a_Klist_get_data(conn_list, fme->connkey) ))) + if (fd_map.contains(fd)) { + + if ((conn = reinterpret_cast< Conn_t * >( a_Klist_get_data(conn_list, fd_map.at( fd )) ))) return conn; } return NULL; @@ -324,8 +300,6 @@ void a_Tls_openssl_init(void) SSL_CTX_set_verify(ssl_context, SSL_VERIFY_NONE, NULL); Tls_load_certificates(); - - fd_map = dList_new(20); } /* @@ -1370,33 +1344,8 @@ int a_Tls_openssl_write(void *conn, void *buf, size_t len) void a_Tls_openssl_close_by_fd(int fd) { - FdMapEntry_t *fme = reinterpret_cast< FdMapEntry_t * >( dList_find_custom(fd_map, INT2VOIDP(fd), - Tls_fd_map_cmp) ); - - if (fme) { - Tls_close_by_key(fme->connkey); - } -} - -static void Tls_servers_freeall(void) -{ - int i, n = servers.size(); - - for (i = 0; i < n; i++) { - std::unique_ptr< Server_t > s { std::move( servers.at( i ) ) }; - } - servers.clear(); -} - -static void Tls_fd_map_remove_all(void) -{ - if (fd_map) { - int i, n = dList_length(fd_map); - - for (i = 0; i < n; i++) { - std::unique_ptr< FdMapEntry_t >fme{ reinterpret_cast( dList_nth_data(fd_map, i) ) }; - } - dList_free(fd_map); + if (fd_map.contains( fd )) { + Tls_close_by_key(fd_map.at( fd )); } } @@ -1407,6 +1356,6 @@ void a_Tls_openssl_freeall(void) { if (ssl_context) SSL_CTX_free(ssl_context); - Tls_fd_map_remove_all(); - Tls_servers_freeall(); + fd_map.clear(); + servers.clear(); }