From 22801415e4f443d19eda3e073aaabc4b622c7122 Mon Sep 17 00:00:00 2001 From: Wertzui123 <46199283+Wertzui123@users.noreply.github.com> Date: Tue, 10 Dec 2024 20:38:38 +0100 Subject: [PATCH] internet: fix occasional connection issues by upgrading from a deprecated internet API --- thirdparty/cwsc/thirdparty/tlse_adapter.c | 50 ++++++++++++++------- thirdparty/echttp/thirdparty/tlse_adapter.c | 50 ++++++++++++++------- 2 files changed, 70 insertions(+), 30 deletions(-) diff --git a/thirdparty/cwsc/thirdparty/tlse_adapter.c b/thirdparty/cwsc/thirdparty/tlse_adapter.c index f7e4998..9cb18ba 100644 --- a/thirdparty/cwsc/thirdparty/tlse_adapter.c +++ b/thirdparty/cwsc/thirdparty/tlse_adapter.c @@ -63,29 +63,49 @@ int cwsc_tlse_wrapper_validate_certificate(struct TLSContext* context, struct TL int cwsc_tlse_wrapper_connect_socket(const char* host, int port) { int sockfd; - struct sockaddr_in serv_addr; - struct hostent* server; + struct addrinfo hints, * servinfo, * p; + char port_str[6]; + int status; + #ifdef _WIN32 WSADATA wsaData; WSAStartup(MAKEWORD(2, 2), &wsaData); #else signal(SIGPIPE, SIG_IGN); #endif - sockfd = socket(AF_INET, SOCK_STREAM, 0); - if (sockfd < 0) - cwsc_tlse_wrapper_error("ERROR opening socket"); - server = gethostbyname(host); - if (server == NULL) + + snprintf(port_str, sizeof(port_str), "%d", port); + + memset(&hints, 0, sizeof(hints)); + hints.ai_family = AF_UNSPEC; + hints.ai_socktype = SOCK_STREAM; + + if ((status = getaddrinfo(host, port_str, &hints, &servinfo)) != 0) + { + fprintf(stderr, "ERROR, getaddrinfo: %s\n", gai_strerror(status)); + return -1; + } + + for (p = servinfo; p != NULL; p = p->ai_next) { - fprintf(stderr, "ERROR, no such host\n"); - exit(0); + sockfd = socket(p->ai_family, p->ai_socktype, p->ai_protocol); + if (sockfd == -1) + continue; + + if (connect(sockfd, p->ai_addr, p->ai_addrlen) == 0) + break; + + close(sockfd); } - memset((char*)&serv_addr, 0, sizeof(serv_addr)); - serv_addr.sin_family = AF_INET; - memcpy((char*)&serv_addr.sin_addr.s_addr, (char*)server->h_addr, server->h_length); - serv_addr.sin_port = htons(port); - if (connect(sockfd, (struct sockaddr*)&serv_addr, sizeof(serv_addr)) < 0) - cwsc_tlse_wrapper_error("ERROR connecting"); + + if (p == NULL) + { + fprintf(stderr, "ERROR, failed to connect\n"); + freeaddrinfo(servinfo); + return -1; + } + + freeaddrinfo(servinfo); return sockfd; } diff --git a/thirdparty/echttp/thirdparty/tlse_adapter.c b/thirdparty/echttp/thirdparty/tlse_adapter.c index 1436cac..35c71f2 100644 --- a/thirdparty/echttp/thirdparty/tlse_adapter.c +++ b/thirdparty/echttp/thirdparty/tlse_adapter.c @@ -63,29 +63,49 @@ int echttp_tlse_wrapper_validate_certificate(struct TLSContext* context, struct int echttp_tlse_wrapper_connect_socket(const char* host, int port) { int sockfd; - struct sockaddr_in serv_addr; - struct hostent* server; + struct addrinfo hints, * servinfo, * p; + char port_str[6]; + int status; + #ifdef _WIN32 WSADATA wsaData; WSAStartup(MAKEWORD(2, 2), &wsaData); #else signal(SIGPIPE, SIG_IGN); #endif - sockfd = socket(AF_INET, SOCK_STREAM, 0); - if (sockfd < 0) - echttp_tlse_wrapper_error("ERROR opening socket"); - server = gethostbyname(host); - if (server == NULL) + + snprintf(port_str, sizeof(port_str), "%d", port); + + memset(&hints, 0, sizeof(hints)); + hints.ai_family = AF_UNSPEC; + hints.ai_socktype = SOCK_STREAM; + + if ((status = getaddrinfo(host, port_str, &hints, &servinfo)) != 0) + { + fprintf(stderr, "ERROR, getaddrinfo: %s\n", gai_strerror(status)); + return -1; + } + + for (p = servinfo; p != NULL; p = p->ai_next) { - fprintf(stderr, "ERROR, no such host\n"); - exit(0); + sockfd = socket(p->ai_family, p->ai_socktype, p->ai_protocol); + if (sockfd == -1) + continue; + + if (connect(sockfd, p->ai_addr, p->ai_addrlen) == 0) + break; + + close(sockfd); } - memset((char*)&serv_addr, 0, sizeof(serv_addr)); - serv_addr.sin_family = AF_INET; - memcpy((char*)&serv_addr.sin_addr.s_addr, (char*)server->h_addr, server->h_length); - serv_addr.sin_port = htons(port); - if (connect(sockfd, (struct sockaddr*)&serv_addr, sizeof(serv_addr)) < 0) - echttp_tlse_wrapper_error("ERROR connecting"); + + if (p == NULL) + { + fprintf(stderr, "ERROR, failed to connect\n"); + freeaddrinfo(servinfo); + return -1; + } + + freeaddrinfo(servinfo); return sockfd; }