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) {
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;
}