1 /* Copyright 1998 by the Massachusetts Institute of Technology.
5 * Permission to use, copy, modify, and distribute this
6 * software and its documentation for any purpose and without
7 * fee is hereby granted, provided that the above copyright
8 * notice appear in all copies and that both that copyright
9 * notice and this permission notice appear in supporting
10 * documentation, and that the name of M.I.T. not be used in
11 * advertising or publicity pertaining to distribution of the
12 * software without specific, written prior permission.
13 * M.I.T. makes no representations about the suitability of
14 * this software for any purpose. It is provided "as is"
15 * without express or implied warranty.
20 #if !defined(WIN32) || defined(WATT32)
21 #ifdef HAVE_SYS_TIME_H
24 #include <sys/socket.h>
25 #include <netinet/in.h>
26 #include <arpa/inet.h>
42 #include "inet_ntop.h"
43 #include "inet_net_pton.h"
44 #include "ares_getopt.h"
46 static void callback(void *arg, int status, int timeouts, struct hostent *host);
47 static void usage(void);
49 int main(int argc, char **argv)
52 int status, nfds, c, addr_family = AF_INET;
53 fd_set read_fds, write_fds;
54 struct timeval *tvp, tv;
56 struct in6_addr addr6;
59 WORD wVersionRequested = MAKEWORD(USE_WINSOCK,USE_WINSOCK);
61 WSAStartup(wVersionRequested, &wsaData);
64 while ((c = ares_getopt(argc,argv,"dt:h")) != -1)
74 if (!strcasecmp(optarg,"a"))
75 addr_family = AF_INET;
76 else if (!strcasecmp(optarg,"aaaa"))
77 addr_family = AF_INET6;
93 status = ares_init(&channel);
94 if (status != ARES_SUCCESS)
96 fprintf(stderr, "ares_init: %s\n", ares_strerror(status));
100 /* Initiate the queries, one per command-line argument. */
101 for ( ; *argv; argv++)
103 if (ares_inet_pton(AF_INET, *argv, &addr4) == 1)
105 ares_gethostbyaddr(channel, &addr4, sizeof(addr4), AF_INET, callback,
108 else if (ares_inet_pton(AF_INET6, *argv, &addr6) == 1)
110 ares_gethostbyaddr(channel, &addr6, sizeof(addr6), AF_INET6, callback,
115 ares_gethostbyname(channel, *argv, addr_family, callback, *argv);
119 /* Wait for all queries to complete. */
124 nfds = ares_fds(channel, &read_fds, &write_fds);
127 tvp = ares_timeout(channel, NULL, &tv);
128 select(nfds, &read_fds, &write_fds, NULL, tvp);
129 ares_process(channel, &read_fds, &write_fds);
132 ares_destroy(channel);
141 static void callback(void *arg, int status, int timeouts, struct hostent *host)
147 if (status != ARES_SUCCESS)
149 fprintf(stderr, "%s: %s\n", (char *) arg, ares_strerror(status));
153 for (p = host->h_addr_list; *p; p++)
155 char addr_buf[46] = "??";
157 ares_inet_ntop(host->h_addrtype, *p, addr_buf, sizeof(addr_buf));
158 printf("%-32s\t%s", host->h_name, addr_buf);
160 if (host->h_aliases[0])
164 printf (", Aliases: ");
165 for (i = 0; host->h_aliases[i]; i++)
166 printf("%s ", host->h_aliases[i]);
173 static void usage(void)
175 fprintf(stderr, "usage: ahost [-t {a|aaaa}] {host|addr} ...\n");