summaryrefslogtreecommitdiff
path: root/sockets.c
diff options
context:
space:
mode:
authorLuke Bratch <luke@bratch.co.uk>2019-06-12 20:31:39 +0100
committerLuke Bratch <luke@bratch.co.uk>2019-06-12 21:28:34 +0100
commit0ea06b9c632af2fe09cdea8be0baa9ae6e538aeb (patch)
tree8d7fdc93aa0a0f5c64b0441f93e30d12702580b1 /sockets.c
parent511e258e901e5248e1706609ba1099507fd750ae (diff)
Handle failing to connect to the server on startup.
Diffstat (limited to 'sockets.c')
-rw-r--r--sockets.c21
1 files changed, 12 insertions, 9 deletions
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;
}