diff options
author | Luke Bratch <luke@bratch.co.uk> | 2019-06-12 20:31:39 +0100 |
---|---|---|
committer | Luke Bratch <luke@bratch.co.uk> | 2019-06-12 21:28:34 +0100 |
commit | 0ea06b9c632af2fe09cdea8be0baa9ae6e538aeb (patch) | |
tree | 8d7fdc93aa0a0f5c64b0441f93e30d12702580b1 | |
parent | 511e258e901e5248e1706609ba1099507fd750ae (diff) |
Handle failing to connect to the server on startup.
-rw-r--r-- | TODO | 4 | ||||
-rw-r--r-- | blabouncer.c | 6 | ||||
-rw-r--r-- | sockets.c | 21 | ||||
-rw-r--r-- | sockets.h | 1 |
4 files changed, 21 insertions, 11 deletions
@@ -7,4 +7,6 @@ Add various auto replay options: Might need to #include <limits.h> 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); @@ -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; } @@ -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 |