Author: cem
Date: Wed May 11 17:52:06 2016
New Revision: 299473
URL: https://svnweb.freebsd.org/changeset/base/299473

Log:
  whois(1): Pull out async multiple host connection code into a routine
  
  This logic was added to the whois() function in r281959, but could easily be
  its own routine.  In this case, I think the abstraction makes both functions
  easier to reason about.
  
  This precedes some Coverity-suggested cleanup.
  
  Sponsored by: EMC / Isilon Storage Division

Modified:
  head/usr.bin/whois/whois.c

Modified: head/usr.bin/whois/whois.c
==============================================================================
--- head/usr.bin/whois/whois.c  Wed May 11 17:40:51 2016        (r299472)
+++ head/usr.bin/whois/whois.c  Wed May 11 17:52:06 2016        (r299473)
@@ -285,19 +285,15 @@ s_asprintf(char **ret, const char *forma
        va_end(ap);
 }
 
-static void
-whois(const char *query, const char *hostname, int flags)
+static int
+connect_to_any_host(struct addrinfo *hostres)
 {
-       FILE *fp;
-       struct addrinfo *hostres, *res;
-       char *buf, *host, *nhost, *p;
-       int s = -1, f;
+       struct addrinfo *res;
        nfds_t i, j;
-       size_t len, count;
+       size_t count;
        struct pollfd *fds;
-       int timeout = 180;
+       int timeout = 180, s = -1;
 
-       hostres = gethostinfo(hostname, 1);
        for (res = hostres, count = 0; res; res = res->ai_next)
                count++;
        fds = calloc(count, sizeof(*fds));
@@ -401,15 +397,29 @@ whois(const char *query, const char *hos
        s = -1;
        if (count == 0)
                errno = ETIMEDOUT;
-done:
-       if (s == -1)
-               err(EX_OSERR, "connect()");
 
+done:
        /* Close all watched fds except the succeeded one */
        for (j = 0; j < i; j++)
                if (fds[j].fd != s && fds[j].fd != -1)
                        close(fds[j].fd);
        free(fds);
+       return (s);
+}
+
+static void
+whois(const char *query, const char *hostname, int flags)
+{
+       FILE *fp;
+       struct addrinfo *hostres;
+       char *buf, *host, *nhost, *p;
+       int s, f;
+       size_t len, i;
+
+       hostres = gethostinfo(hostname, 1);
+       s = connect_to_any_host(hostres);
+       if (s == -1)
+               err(EX_OSERR, "connect()");
 
        /* Restore default blocking behavior.  */
        if ((f = fcntl(s, F_GETFL)) == -1)
_______________________________________________
svn-src-head@freebsd.org mailing list
https://lists.freebsd.org/mailman/listinfo/svn-src-head
To unsubscribe, send any mail to "svn-src-head-unsubscr...@freebsd.org"

Reply via email to