Switch nbd_client to xconnect() and make xconnect() try all returned addresses
authorRob Landley <rob@landley.net>
Thu, 6 Aug 2015 02:20:27 +0000 (21:20 -0500)
committerRob Landley <rob@landley.net>
Thu, 6 Aug 2015 02:20:27 +0000 (21:20 -0500)
before failing.

lib/net.c
toys/other/nbd_client.c

index 7be66b6..48d0a5f 100644 (file)
--- 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;
 }
index c16585a..a82ff7c 100644 (file)
@@ -40,7 +40,6 @@ void nbd_client_main(void)
 {
   int sock = -1, nbd, flags;
   unsigned long timeout = 0;
-  struct addrinfo *addr, *p;
   char *host=toys.optargs[0], *port=toys.optargs[1], *device=toys.optargs[2];
   uint64_t devsize;
 
@@ -49,23 +48,10 @@ void nbd_client_main(void)
   nbd = xopen(device, O_RDWR);
   for (;;) {
     int temp;
-    struct addrinfo hints;
 
     // Find and connect to server
 
-    memset(&hints, 0, sizeof(hints));
-    hints.ai_family = PF_UNSPEC;
-    hints.ai_socktype = SOCK_STREAM;
-    if (getaddrinfo(host, port, &hints, &addr)) addr = 0;
-    for (p = addr; p; p = p->ai_next) {
-      sock = socket(p->ai_family, p->ai_socktype, p->ai_protocol);
-      if (-1 != connect(sock, p->ai_addr, p->ai_addrlen)) break;
-      close(sock);
-    }
-    freeaddrinfo(addr);
-
-    if (!p) perror_exit("%s:%s", host, port);
-
+    sock = xconnect(host, port, AF_UNSPEC, SOCK_STREAM, 0, 0);
     temp = 1;
     setsockopt(sock, IPPROTO_TCP, TCP_NODELAY, &temp, sizeof(int));