net: Replace the double loop of lookup_address() by a single loop.
authorAndre Noll <maan@systemlinux.org>
Sun, 8 Sep 2013 04:57:50 +0000 (04:57 +0000)
committerAndre Noll <maan@systemlinux.org>
Wed, 1 Jan 2014 17:40:15 +0000 (17:40 +0000)
net.c

diff --git a/net.c b/net.c
index dfbbc1e..94610a8 100644 (file)
--- a/net.c
+++ b/net.c
@@ -422,23 +422,12 @@ 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;
                flowopt_setopts(sockfd, fo);
                if (passive) {
@@ -456,25 +445,19 @@ static int makesock_addrinfo(unsigned l4type, bool passive, struct addrinfo *ai,
                                        para_strerror(-ret));
                                break;
                        }
-                       if (bind(sockfd, src->ai_addr, src->ai_addrlen) < 0) {
+                       if (bind(sockfd, ai->ai_addr, ai->ai_addrlen) < 0) {
                                close(sockfd);
-                               goto get_next_src;
+                               continue;
                        }
                        /* bind completed successfully */
                        break;
                } else {
-                       if (connect(sockfd, dst->ai_addr, dst->ai_addrlen) == 0)
+                       if (connect(sockfd, ai->ai_addr, ai->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;
 }