44
55#include < arpa/inet.h>
66#include < netinet/in.h>
7+ #include < netdb.h>
78#include < pthread.h>
89#include < string>
910#include < sys/socket.h>
@@ -24,7 +25,6 @@ extern "C" {
2425#include " osd.h"
2526}
2627
27- #define SERVER_IP " 127.0.0.1"
2828#define BUFFER_SIZE 10 * 1024
2929
3030int wfb_thread_signal = 0 ;
@@ -289,44 +289,61 @@ void handle_server_connection(int sock) {
289289 }
290290}
291291
292- int reconnect_to_server (int port) {
293- while (!wfb_thread_signal) {
294- SPDLOG_DEBUG (" Attempting to connect to WFB API server..." );
295-
296- int sock = socket (AF_INET, SOCK_STREAM, 0 );
297- if (sock < 0 ) {
298- perror (" Socket creation failed" );
299- } else {
300- struct sockaddr_in server_address;
301- server_address.sin_family = AF_INET;
302- server_address.sin_port = htons (port);
303-
304- if (inet_pton (AF_INET, SERVER_IP, &server_address.sin_addr ) > 0 ) {
305- if (connect (sock, (struct sockaddr *)&server_address, sizeof (server_address)) == 0 ) {
306- SPDLOG_DEBUG (" Successfully connected to WFB API server." );
307- return sock;
308- } else {
309- SPDLOG_ERROR (" Connection failed" );
310- }
311- } else {
312- SPDLOG_ERROR (" Invalid address/Address not supported" );
313- }
314-
315- close (sock); // Clean up the socket if connection fails
316- }
317-
318- SPDLOG_WARN (" Reconnection failed. Retrying in 1 second" );
319- std::this_thread::sleep_for (std::chrono::seconds (1 ));
320- }
321- return -1 ;
292+ int reconnect_to_server (const char *host, int port) {
293+ struct addrinfo hints, *res = nullptr , *p;
294+ int status, sock;
295+ // Clear hints structure
296+ memset (&hints, 0 , sizeof (hints));
297+ hints.ai_family = AF_UNSPEC; // IPv4 or IPv6
298+ hints.ai_socktype = SOCK_STREAM; // TCP
299+
300+ while (!wfb_thread_signal) {
301+ SPDLOG_DEBUG (" wfb-cli attempting to connect to API server..." );
302+ // Get address info
303+ if ((status = getaddrinfo (host, std::to_string (port).c_str (), &hints, &res)) != 0 ) {
304+ SPDLOG_ERROR (" wfb-cli incorrect host {} or port {}. Error: {}" ,
305+ host, port, gai_strerror (status));
306+ std::this_thread::sleep_for (std::chrono::seconds (1 ));
307+ continue ;
308+ }
309+ // Loop through results and try to connect
310+ for (p = res; p != nullptr ; p = p->ai_next ) {
311+ sock = socket (p->ai_family , p->ai_socktype , p->ai_protocol );
312+ if (sock < 0 ) {
313+ SPDLOG_ERROR (" wfb-cli socket creation failed" );
314+ continue ;
315+ }
316+ if (connect (sock, p->ai_addr , p->ai_addrlen ) < 0 ) {
317+ char ip_str[INET6_ADDRSTRLEN];
318+ if (p->ai_family == AF_INET) {
319+ struct sockaddr_in * ipv4 = (struct sockaddr_in *)p->ai_addr ;
320+ inet_ntop (AF_INET, &ipv4->sin_addr , ip_str, sizeof (ip_str));
321+ } else {
322+ struct sockaddr_in6 * ipv6 = (struct sockaddr_in6 *)p->ai_addr ;
323+ inet_ntop (AF_INET6, &ipv6->sin6_addr , ip_str, sizeof (ip_str));
324+ }
325+ SPDLOG_ERROR (" wfb-cli connection to {}:{} ({}) failed" , host, port, ip_str);
326+ close (sock); // Clean up the socket if connection fails
327+ continue ;
328+ }
329+ SPDLOG_DEBUG (" wfb-cli successfully connected to API server." );
330+ freeaddrinfo (res);
331+ return sock; // success
332+ }
333+ freeaddrinfo (res);
334+ res = nullptr ;
335+ SPDLOG_WARN (" Reconnection failed. Retrying in 1 second" );
336+ std::this_thread::sleep_for (std::chrono::seconds (1 ));
337+ }
338+ return -1 ;
322339}
323340
324341void *__WFB_CLI_THREAD__ (void *param) {
325342 wfb_thread_params *p = (wfb_thread_params *)param;
326343 pthread_setname_np (pthread_self (), " __WFB_CLI" );
327344
328345 while (!wfb_thread_signal) {
329- int sock = reconnect_to_server (p->port );
346+ int sock = reconnect_to_server (p->host , p-> port );
330347 handle_server_connection (sock);
331348 // If we return from handle_server_connection, the server is disconnected
332349 }
0 commit comments