From 8d47a8cea663cc9c1c01cdba23b0531caa5c8d9c Mon Sep 17 00:00:00 2001 From: Andre Noll Date: Sun, 8 Sep 2013 04:57:50 +0000 Subject: [PATCH 1/1] net: Replace the double loop of lookup_address() by a single loop. --- net.c | 31 +++++++------------------------ 1 file changed, 7 insertions(+), 24 deletions(-) diff --git a/net.c b/net.c index dfbbc1ee..94610a84 100644 --- 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; } -- 2.39.2