From 24f8e2688acaa519bccbf6df0b7c3752e8f15fe4 Mon Sep 17 00:00:00 2001 From: Barry Kane Date: Mon, 18 Mar 2024 03:09:32 +0000 Subject: [PATCH] Added address iteration and graceful TLS failures. * source/client/main.c (main): - Added iteration through found addresses from getaddrinfo. - Added graceful failures for TLS errors. --- source/client/main.c | 49 +++++++++++++++++++++++++++++++------------- 1 file changed, 35 insertions(+), 14 deletions(-) diff --git a/source/client/main.c b/source/client/main.c index 1fb34d8..91f9e60 100644 --- a/source/client/main.c +++ b/source/client/main.c @@ -21,23 +21,24 @@ #include "client-drawing.h" #include "receiving-thread.h" +static char serverPort[HOST_NAME_MAX] = "5050"; +static char serverHostname[HOST_NAME_MAX] = "127.0.0.1"; +static bool hostSpecified = false, portSpecified = false; + int main (int argc, char ** argv) -{ - static char serverPort[HOST_NAME_MAX] = "5050"; - static char serverHostname[HOST_NAME_MAX] = "127.0.0.1"; - struct addrinfo * serverInformation; - +{ // Print a welcome message: printf("SilverMUD Client - Starting Now.\n" "================================\n"); + struct addrinfo * serverInformation; + // Configure command-line options: static struct option longOptions[] = { {"host", required_argument, 0, 'h' }, {"port", required_argument, 0, 'p' } }; - bool hostSpecified = false, portSpecified = false; // Parse command-line options: int selectedOption = 0, optionIndex = 0; @@ -78,13 +79,22 @@ int main (int argc, char ** argv) exit(EXIT_FAILURE); } - // Connect to the server: - if (connect(serverSocket, serverInformation->ai_addr, serverInformation->ai_addrlen) != 0) + // Connect to the server, iterating through addresses until we get SilverMUD: + struct addrinfo * currentAddress; + for (currentAddress = serverInformation; currentAddress != NULL; currentAddress = currentAddress->ai_next) + { + if (connect(serverSocket, serverInformation->ai_addr, serverInformation->ai_addrlen) != -1) + { + break; + } + } + if (currentAddress == NULL) { printf("Failed to connect to the server. Aborting.\n"); exit(EXIT_FAILURE); } - + freeaddrinfo(serverInformation); + // Set up a GnuTLS session and handshake with the server: gnutls_session_t tlsSession = NULL; if (gnutls_init(&tlsSession, GNUTLS_CLIENT) < 0) @@ -94,20 +104,31 @@ int main (int argc, char ** argv) gnutls_anon_client_credentials_t clientKey = NULL; gnutls_anon_allocate_client_credentials(&clientKey); - gnutls_credentials_set(tlsSession, GNUTLS_CRD_ANON, &clientKey); - gnutls_transport_set_int(tlsSession, serverSocket); - gnutls_priority_set_direct(tlsSession, "PERFORMANCE:+ANON-ECDH:+ANON-DH", NULL); - + gnutls_credentials_set(tlsSession, GNUTLS_CRD_ANON, &clientKey); gnutls_handshake_set_timeout(tlsSession, GNUTLS_DEFAULT_HANDSHAKE_TIMEOUT); + gnutls_priority_set_direct(tlsSession, "PERFORMANCE:+ANON-ECDH:+ANON-DH", NULL); + gnutls_server_name_set(tlsSession, GNUTLS_NAME_DNS, serverHostname, strlen(serverHostname)); - int returnValue = -1; + int returnValue = -1, connectionAttempts = 0; do { returnValue = gnutls_handshake(tlsSession); + connectionAttempts++; + if (connectionAttempts == 50) + { + printf("Failed to establish a TLS session. Aborting.\n"); + exit(EXIT_FAILURE); + } } while (returnValue < 0 && gnutls_error_is_fatal(returnValue) == 0); + if (returnValue < 0) + { + printf("Failed to establish a TLS session. Aborting.\n"); + exit(EXIT_FAILURE); + } + // Initialize ncurses: initscr(); keypad(stdscr, TRUE);