From c807a0e4c6503aa5bb9cfcab13838000724df8e8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?V=C3=A4in=C3=B6=20M=C3=A4kel=C3=A4?= Date: Tue, 15 Sep 2020 18:21:03 +0300 Subject: Add error handling to irc_init --- kirc.c | 37 ++++++++++++++++++++++++++++++++----- 1 file changed, 32 insertions(+), 5 deletions(-) (limited to 'kirc.c') diff --git a/kirc.c b/kirc.c index 91f3c4c..f60590c 100644 --- a/kirc.c +++ b/kirc.c @@ -10,6 +10,7 @@ #include #include #include +#include #define MSG_MAX 512 /* guaranteed max message length */ #define CHA_MAX 200 /* gauranteed max channel length */ @@ -61,19 +62,43 @@ raw(char *fmt, ...) { free(cmd_str); } -static void +static int irc_init() { struct addrinfo *res, hints = { .ai_family = AF_INET, .ai_socktype = SOCK_STREAM }; + int gai_status; + + if ((gai_status = getaddrinfo(host, port, &hints, &res)) != 0) { + fprintf(stderr, "getaddrinfo: %s\n", gai_strerror(gai_status)); + return -1; + } + + struct addrinfo *p; + for (p = res; p != NULL; p = p->ai_next) { + if ((conn = socket(p->ai_family, p->ai_socktype, p->ai_protocol)) == -1) { + perror("socket"); + continue; + } + if (connect(conn, p->ai_addr, p->ai_addrlen) == -1) { + close(conn); + perror("connect"); + continue; + } + break; + } - getaddrinfo(host, port, &hints, &res); - conn = socket(res->ai_family, res->ai_socktype, res->ai_protocol); - connect(conn, res->ai_addr, res->ai_addrlen); freeaddrinfo(res); + + if (p == NULL) { + fputs("Failed to connect\n", stderr); + return -1; + } + fcntl(conn, F_SETFL, O_NONBLOCK); + return 0; } static void @@ -242,7 +267,9 @@ main(int argc, char **argv) { return EXIT_FAILURE; } - irc_init(); + if (irc_init() != 0) { + return EXIT_FAILURE; + } raw("NICK %s\r\n", nick); raw("USER %s - - :%s\r\n", (user ? user : nick), (real ? real : nick)); -- cgit v1.2.3