From 0ea06b9c632af2fe09cdea8be0baa9ae6e538aeb Mon Sep 17 00:00:00 2001 From: Luke Bratch Date: Wed, 12 Jun 2019 20:31:39 +0100 Subject: Handle failing to connect to the server on startup. --- TODO | 4 +++- blabouncer.c | 6 +++++- sockets.c | 21 ++++++++++++--------- sockets.h | 1 + 4 files changed, 21 insertions(+), 11 deletions(-) diff --git a/TODO b/TODO index b49d7bd..4c106d8 100644 --- a/TODO +++ b/TODO @@ -7,4 +7,6 @@ Add various auto replay options: Might need to #include in blabouncer.c to make some operating systems and/or compilers happy. -Handle connecting to the server failing. +Handle re-connecting to the server failing. + +Allow reloading the configuration file while running (at least for things like replayseconds, replaymode) - BLABOUNCER command and SIGHUP? diff --git a/blabouncer.c b/blabouncer.c index 0db2dca..c416c3e 100644 --- a/blabouncer.c +++ b/blabouncer.c @@ -1053,7 +1053,11 @@ int main(int argc, char *argv[]) { // I will try to keep to the notation of "server" meaning the real IRCd, "bouncer" meaning the bouncer, and "client" meaning the real IRC client // Create server socket - int serversockfd = createserversocket(settings.ircserver, settings.ircserverport); + int serversockfd; + if ((serversockfd = createserversocket(settings.ircserver, settings.ircserverport)) == -1) { + debugprint(DEBUG_CRIT, "main(): Couldn't connect to server, exiting.\n"); + exit(1); + } // Create client socket (after server so we can use its fd number later as fdmax) int clientsockfd = createclientsocket(settings.clientport); diff --git a/sockets.c b/sockets.c index 5f0bf3a..9f3563f 100644 --- a/sockets.c +++ b/sockets.c @@ -27,10 +27,11 @@ void *get_in_addr(struct sockaddr *sa) { } // Create socket to connect to real IRC server +// Returns the socket descriptor on success, or -1 on error int createserversocket(char *host, char *port) { int sockfd; struct addrinfo hints, *servinfo, *p; - int rv;// return value for getaddrinfo (for error message) + int rv; // Return value for getaddrinfo (for error message) char s[INET6_ADDRSTRLEN]; memset(&hints, 0, sizeof hints); @@ -38,20 +39,21 @@ int createserversocket(char *host, char *port) { hints.ai_socktype = SOCK_STREAM; if ((rv = getaddrinfo(host, port, &hints, &servinfo)) != 0) { - fprintf(stderr, "getaddrinfo: %s\n", gai_strerror(rv)); - return 1; // TODO - Should this perhaps return 0 or exit? + debugprint(DEBUG_CRIT, "createserversocket(): getaddrinfo(): %s\n", gai_strerror(rv)); + freeaddrinfo(servinfo); + return -1; } - // loop through all the results and connect to the first we can + // Loop through all the results and connect to the first we can for (p = servinfo; p != NULL; p = p->ai_next) { if ((sockfd = socket(p->ai_family, p->ai_socktype, p->ai_protocol)) == -1) { - perror("bouncer-server: socket"); + debugprint(DEBUG_CRIT, "createserversocket(): socket(): %s\n", strerror(errno)); continue; } if (connect(sockfd, p->ai_addr, p->ai_addrlen) == -1) { close(sockfd); - perror("bouncer-server: connect"); + debugprint(DEBUG_CRIT, "createserversocket(): connect(): %s\n", strerror(errno)); continue; } @@ -59,14 +61,15 @@ int createserversocket(char *host, char *port) { } if (p == NULL) { - fprintf(stderr, "bouncer-server: failed to connect\n"); - return 2; // TODO - Should this perhaps return 0 or exit? + debugprint(DEBUG_CRIT, "createserversocket(): p == NULL, (%s)\n", strerror(errno)); + freeaddrinfo(servinfo); + return -1; } inet_ntop(p->ai_family, get_in_addr((struct sockaddr *)p->ai_addr), s, sizeof s); debugprint(DEBUG_SOME, "bouncer-server: connecting to '%s'\n", s); - freeaddrinfo(servinfo); // all done with this structure + freeaddrinfo(servinfo); // All done with this structure return sockfd; } diff --git a/sockets.h b/sockets.h index 7a88926..c1d74c7 100644 --- a/sockets.h +++ b/sockets.h @@ -44,6 +44,7 @@ void *get_in_addr(struct sockaddr *sa); // Create socket to connect to real IRC server +// Returns the socket descriptor on success, or -1 on error int createserversocket(char *host, char *port); // Create listening socket to listen for bouncer client connections -- cgit v1.2.3