aboutsummaryrefslogtreecommitdiff
path: root/lib
diff options
context:
space:
mode:
authorRob Landley <rob@landley.net>2015-08-05 21:20:27 -0500
committerRob Landley <rob@landley.net>2015-08-05 21:20:27 -0500
commit31ff1f23dedbfe6e2dccf9d3c7c294a1cd761266 (patch)
treea50c230a6fe9b8c99bdacce741887f01d0ee2005 /lib
parent35dafc7b17ce23da62dcce2195bed9b370680e65 (diff)
downloadtoybox-31ff1f23dedbfe6e2dccf9d3c7c294a1cd761266.tar.gz
Switch nbd_client to xconnect() and make xconnect() try all returned addresses
before failing.
Diffstat (limited to 'lib')
-rw-r--r--lib/net.c15
1 files changed, 10 insertions, 5 deletions
diff --git a/lib/net.c b/lib/net.c
index 7be66b67..48d0a5fe 100644
--- a/lib/net.c
+++ b/lib/net.c
@@ -16,7 +16,7 @@ void xsetsockopt(int fd, int level, int opt, void *val, socklen_t len)
int xconnect(char *host, char *port, int family, int socktype, int protocol,
int flags)
{
- struct addrinfo info, *ai;
+ struct addrinfo info, *ai, *ai2;
int fd;
memset(&info, 0, sizeof(struct addrinfo));
@@ -26,14 +26,19 @@ int xconnect(char *host, char *port, int family, int socktype, int protocol,
info.ai_flags = flags;
fd = getaddrinfo(host, port, &info, &ai);
-
if (fd || !ai)
error_exit("Connect '%s%s%s': %s", host, port ? ":" : "", port ? port : "",
fd ? gai_strerror(fd) : "not found");
- fd = xsocket(ai->ai_family, ai->ai_socktype, ai->ai_protocol);
- if (connect(fd, ai->ai_addr, ai->ai_addrlen)) perror_exit("connect");
- freeaddrinfo(ai);
+ // Try all the returned addresses. Report errors if last entry can't connect.
+ for (ai2 = ai; ai; ai = ai->ai_next) {
+ fd = (ai->ai_next ? socket : xsocket)(ai->ai_family, ai->ai_socktype,
+ ai->ai_protocol);
+ if (!connect(fd, ai->ai_addr, ai->ai_addrlen)) break;
+ else if (!ai2->ai_next) perror_exit("connect");
+ close(fd);
+ }
+ freeaddrinfo(ai2);
return fd;
}