net: Replace the double loop of lookup_address() by a single loop.
[paraslash.git] / net.c
diff --git a/net.c b/net.c
index d00895f50342a528f9e6008e886c766a8808820e..94610a84e2a3aa6b42ad9d5c11dbc03bc69cb306 100644 (file)
--- a/net.c
+++ b/net.c
@@ -422,59 +422,42 @@ static int lookup_address(unsigned l4type, bool passive, const char *host,
 static int makesock_addrinfo(unsigned l4type, bool passive, struct addrinfo *ai,
                struct flowopts *fo)
 {
-       struct addrinfo *local, *remote, *src, *dst;
        int ret = -E_MAKESOCK, on = 1, sockfd = -1;
 
-       if (passive) {
-               local = ai;
-               remote = NULL;
-       } else {
-               local = NULL;
-               remote = ai;
-       }
-
-       /* Iterate over all src/dst combination, exhausting dst first */
-       for (src = local, dst = remote; src != NULL || dst != NULL; /* no op */ ) {
-               ret = socket(src ? src->ai_family : dst->ai_family,
-                       sock_type(l4type), l4type);
+       for (; ai; ai = ai->ai_next) {
+               ret = socket(ai->ai_family, sock_type(l4type), l4type);
                if (ret < 0)
-                       goto get_next_dst;
+                       continue;
                sockfd = ret;
-               /*
-                * Reuse the address on passive sockets to avoid failure on
-                * restart (protocols using listen()) and when creating
-                * multiple listener instances (UDP multicast).
-                */
-               if (passive && setsockopt(sockfd, SOL_SOCKET, SO_REUSEADDR,
-                                                 &on, sizeof(on)) == -1) {
-                       ret = -ERRNO_TO_PARA_ERROR(errno);
-                       close(sockfd);
-                       PARA_ERROR_LOG("can not set SO_REUSEADDR: %s\n",
-                               para_strerror(-ret));
-                       break;
-               }
                flowopt_setopts(sockfd, fo);
-
-               if (src) {
-                       if (bind(sockfd, src->ai_addr, src->ai_addrlen) < 0) {
+               if (passive) {
+                       /*
+                        * Reuse the address on passive sockets to avoid
+                        * failure on restart (protocols using listen()) and
+                        * when creating multiple listener instances (UDP
+                        * multicast).
+                        */
+                       if (setsockopt(sockfd, SOL_SOCKET, SO_REUSEADDR, &on,
+                                       sizeof(on)) == -1) {
+                               ret = -ERRNO_TO_PARA_ERROR(errno);
                                close(sockfd);
-                               goto get_next_src;
-                       }
-                       if (!dst) /* bind-only completed successfully */
+                               PARA_ERROR_LOG("can not set SO_REUSEADDR: %s\n",
+                                       para_strerror(-ret));
                                break;
+                       }
+                       if (bind(sockfd, ai->ai_addr, ai->ai_addrlen) < 0) {
+                               close(sockfd);
+                               continue;
+                       }
+                       /* bind completed successfully */
+                       break;
+               } else {
+                       if (connect(sockfd, ai->ai_addr, ai->ai_addrlen) == 0)
+                               break; /* connection completed successfully */
                }
-
-               if (dst && connect(sockfd, dst->ai_addr, dst->ai_addrlen) == 0)
-                       break; /* connection completed successfully */
                close(sockfd);
-get_next_dst:
-               if (dst && (dst = dst->ai_next))
-                       continue;
-get_next_src:
-               if (src && (src = src->ai_next)) /* restart inner loop */
-                       dst = remote;
        }
-       if (src == NULL && dst == NULL)
+       if (!ai)
                return ret < 0? ret : -E_MAKESOCK;
        return sockfd;
 }