Skip to content

Commit

Permalink
internet: fix occasional connection issues by upgrading from a deprec…
Browse files Browse the repository at this point in the history
…ated internet API
  • Loading branch information
Wertzui123 committed Dec 10, 2024
1 parent d6535fa commit 2280141
Show file tree
Hide file tree
Showing 2 changed files with 70 additions and 30 deletions.
50 changes: 35 additions & 15 deletions thirdparty/cwsc/thirdparty/tlse_adapter.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

Expand Down
50 changes: 35 additions & 15 deletions thirdparty/echttp/thirdparty/tlse_adapter.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

Expand Down

0 comments on commit 2280141

Please sign in to comment.