* If there are multiple IP addresses available for a host, sock_connect() now tries...
authorArt Cancro <ajc@citadel.org>
Thu, 19 Aug 2010 03:26:18 +0000 (03:26 +0000)
committerArt Cancro <ajc@citadel.org>
Thu, 19 Aug 2010 03:26:18 +0000 (03:26 +0000)
citadel/clientsocket.c

index c5d948565f622538bb31ef431e7aad0483743312..0d2da5a5ac87380eede99dbd957864ec71c2965d 100644 (file)
@@ -95,18 +95,22 @@ int sock_connect(char *host, char *service)
                return(-1);
        }
 
-       rc = connect(sock, res->ai_addr, res->ai_addrlen);
-       if (rc < 0) {
-               /*
-                * Note: the res is a linked list of addresses found for server.
-                * If the connect() fails to the first one, subsequent addresses
-                * (if any) in the list could be tried if desired.
-                */
-               CtdlLogPrintf(CTDL_ERR, "connect() failed: %s\n", strerror(errno));
-               return(-1);
+       /*
+        * Try all available addresses until we connect to one or until we run out.
+        */
+       struct addrinfo *ai;
+       for (ai = res; ai != NULL; ai = ai->ai_next) {
+               /* FIXME display the address to which we are trying to connect */
+               rc = connect(sock, res->ai_addr, res->ai_addrlen);
+               if (rc >= 0) {
+                       return(sock);
+               }
+               else {
+                       CtdlLogPrintf(CTDL_ERR, "connect() failed: %s\n", strerror(errno));
+               }
        }
 
-       return (sock);
+       return(-1);
 }