From 894e7d9aa5e9d50a2c714ad3eb38dc0056d0f6ae Mon Sep 17 00:00:00 2001 From: ubq323 Date: Thu, 13 Jan 2022 17:06:40 +0000 Subject: walk the list returned by getaddrinfo like you are meant to --- thing.c | 23 +++++++++++++++-------- 1 file changed, 15 insertions(+), 8 deletions(-) (limited to 'thing.c') diff --git a/thing.c b/thing.c index 11c095e..e7c0f53 100644 --- a/thing.c +++ b/thing.c @@ -1,9 +1,11 @@ #ifdef LOCAL #define HOST "0.0.0.0" #define PORT "6971" +#define SOCKETNAME "bees.sock" #else #define HOST "ubq323.website" #define PORT "6667" +#define SOCKETNAME "/srv/apiobot/bees.sock" #endif #ifdef DEBUG @@ -17,7 +19,6 @@ #define REALNAME ",flappy fly bird man?" #define CHANNEL "#a" -#define SOCKETNAME "/srv/apiobot/bees.sock" #include #include @@ -67,14 +68,20 @@ int irc_connect() { return -1; } - if ((sockfd = socket(res->ai_family, res->ai_socktype, res->ai_protocol)) == -1) { - perror("socket"); - return -1; - } - if (connect(sockfd, res->ai_addr, res->ai_addrlen) != 0) { - perror("connect"); - return -1; + struct addrinfo *cur; + for (cur = res; cur != NULL; cur = cur->ai_next) { + if ((sockfd = socket(cur->ai_family, cur->ai_socktype, cur->ai_protocol)) == -1) { + perror("socket"); + continue; + } + if (connect(sockfd, cur->ai_addr, cur->ai_addrlen) != 0) { + perror("connect"); + continue; + } + goto success; } + return -1; + success: return sockfd; } -- cgit v1.2.3