1 /* Copyright 1998 by the Massachusetts Institute of Technology.
4 * Permission to use, copy, modify, and distribute this
5 * software and its documentation for any purpose and without
6 * fee is hereby granted, provided that the above copyright
7 * notice appear in all copies and that both that copyright
8 * notice and this permission notice appear in supporting
9 * documentation, and that the name of M.I.T. not be used in
10 * advertising or publicity pertaining to distribution of the
11 * software without specific, written prior permission.
12 * M.I.T. makes no representations about the suitability of
13 * this software for any purpose. It is provided "as is"
14 * without express or implied warranty.
17 #include "ares_setup.h"
19 #if !defined(WIN32) || defined(WATT32)
20 #include <netinet/in.h>
21 #include <arpa/inet.h>
31 #include "inet_ntop.h"
32 #include "inet_net_pton.h"
33 #include "ares_getopt.h"
34 #include "ares_ipv6.h"
35 #include "ares_nowarn.h"
38 # include "ares_strdup.h"
39 # define strdup(ptr) ares_strdup(ptr)
42 #ifndef HAVE_STRCASECMP
43 # include "ares_strcasecmp.h"
44 # define strcasecmp(p1,p2) ares_strcasecmp(p1,p2)
47 #ifndef HAVE_STRNCASECMP
48 # include "ares_strcasecmp.h"
49 # define strncasecmp(p1,p2,n) ares_strncasecmp(p1,p2,n)
52 static void callback(void *arg, int status, int timeouts, struct hostent *host);
53 static void usage(void);
55 int main(int argc, char **argv)
58 int status, nfds, c, addr_family = AF_INET;
59 fd_set read_fds, write_fds;
60 struct timeval *tvp, tv;
62 struct ares_in6_addr addr6;
65 WORD wVersionRequested = MAKEWORD(USE_WINSOCK,USE_WINSOCK);
67 WSAStartup(wVersionRequested, &wsaData);
70 status = ares_library_init(ARES_LIB_INIT_ALL);
71 if (status != ARES_SUCCESS)
73 fprintf(stderr, "ares_library_init: %s\n", ares_strerror(status));
77 while ((c = ares_getopt(argc,argv,"dt:h")) != -1)
87 if (!strcasecmp(optarg,"a"))
88 addr_family = AF_INET;
89 else if (!strcasecmp(optarg,"aaaa"))
90 addr_family = AF_INET6;
106 status = ares_init(&channel);
107 if (status != ARES_SUCCESS)
109 fprintf(stderr, "ares_init: %s\n", ares_strerror(status));
113 /* Initiate the queries, one per command-line argument. */
114 for ( ; *argv; argv++)
116 if (ares_inet_pton(AF_INET, *argv, &addr4) == 1)
118 ares_gethostbyaddr(channel, &addr4, sizeof(addr4), AF_INET, callback,
121 else if (ares_inet_pton(AF_INET6, *argv, &addr6) == 1)
123 ares_gethostbyaddr(channel, &addr6, sizeof(addr6), AF_INET6, callback,
128 ares_gethostbyname(channel, *argv, addr_family, callback, *argv);
132 /* Wait for all queries to complete. */
137 nfds = ares_fds(channel, &read_fds, &write_fds);
140 tvp = ares_timeout(channel, NULL, &tv);
141 select(nfds, &read_fds, &write_fds, NULL, tvp);
142 ares_process(channel, &read_fds, &write_fds);
145 ares_destroy(channel);
147 ares_library_cleanup();
156 static void callback(void *arg, int status, int timeouts, struct hostent *host)
162 if (status != ARES_SUCCESS)
164 fprintf(stderr, "%s: %s\n", (char *) arg, ares_strerror(status));
168 for (p = host->h_addr_list; *p; p++)
170 char addr_buf[46] = "??";
172 ares_inet_ntop(host->h_addrtype, *p, addr_buf, sizeof(addr_buf));
173 printf("%-32s\t%s", host->h_name, addr_buf);
175 if (host->h_aliases[0])
179 printf (", Aliases: ");
180 for (i = 0; host->h_aliases[i]; i++)
181 printf("%s ", host->h_aliases[i]);
188 static void usage(void)
190 fprintf(stderr, "usage: ahost [-t {a|aaaa}] {host|addr} ...\n");