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
This commit is contained in:
Conrad Meyer 2016-05-11 17:52:06 +00:00
parent 73571281f5
commit 68c47fcfad
Notes: svn2git 2020-12-20 02:59:44 +00:00
svn path=/head/; revision=299473

View file

@ -285,19 +285,15 @@ s_asprintf(char **ret, const char *format, ...)
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 *hostname, int flags)
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)