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 "ares_getopt.h"
32 #include "ares_ipv6.h"
33 #include "ares_nowarn.h"
36 # include "ares_strdup.h"
37 # define strdup(ptr) ares_strdup(ptr)
40 #ifndef HAVE_STRCASECMP
41 # include "ares_strcasecmp.h"
42 # define strcasecmp(p1,p2) ares_strcasecmp(p1,p2)
45 #ifndef HAVE_STRNCASECMP
46 # include "ares_strcasecmp.h"
47 # define strncasecmp(p1,p2,n) ares_strncasecmp(p1,p2,n)
50 static void callback(void *arg, int status, int timeouts, struct hostent *host);
51 static void usage(void);
53 int main(int argc, char **argv)
56 int status, nfds, c, addr_family = AF_INET;
57 fd_set read_fds, write_fds;
58 struct timeval *tvp, tv;
60 struct ares_in6_addr addr6;
63 WORD wVersionRequested = MAKEWORD(USE_WINSOCK,USE_WINSOCK);
65 WSAStartup(wVersionRequested, &wsaData);
68 status = ares_library_init(ARES_LIB_INIT_ALL);
69 if (status != ARES_SUCCESS)
71 fprintf(stderr, "ares_library_init: %s\n", ares_strerror(status));
75 while ((c = ares_getopt(argc,argv,"dt:h")) != -1)
85 if (!strcasecmp(optarg,"a"))
86 addr_family = AF_INET;
87 else if (!strcasecmp(optarg,"aaaa"))
88 addr_family = AF_INET6;
104 status = ares_init(&channel);
105 if (status != ARES_SUCCESS)
107 fprintf(stderr, "ares_init: %s\n", ares_strerror(status));
111 /* Initiate the queries, one per command-line argument. */
112 for ( ; *argv; argv++)
114 if (ares_inet_pton(AF_INET, *argv, &addr4) == 1)
116 ares_gethostbyaddr(channel, &addr4, sizeof(addr4), AF_INET, callback,
119 else if (ares_inet_pton(AF_INET6, *argv, &addr6) == 1)
121 ares_gethostbyaddr(channel, &addr6, sizeof(addr6), AF_INET6, callback,
126 ares_gethostbyname(channel, *argv, addr_family, callback, *argv);
130 /* Wait for all queries to complete. */
135 nfds = ares_fds(channel, &read_fds, &write_fds);
138 tvp = ares_timeout(channel, NULL, &tv);
139 select(nfds, &read_fds, &write_fds, NULL, tvp);
140 ares_process(channel, &read_fds, &write_fds);
143 ares_destroy(channel);
145 ares_library_cleanup();
154 static void callback(void *arg, int status, int timeouts, struct hostent *host)
160 if (status != ARES_SUCCESS)
162 fprintf(stderr, "%s: %s\n", (char *) arg, ares_strerror(status));
166 for (p = host->h_addr_list; *p; p++)
168 char addr_buf[46] = "??";
170 ares_inet_ntop(host->h_addrtype, *p, addr_buf, sizeof(addr_buf));
171 printf("%-32s\t%s", host->h_name, addr_buf);
173 if (host->h_aliases[0])
177 printf (", Aliases: ");
178 for (i = 0; host->h_aliases[i]; i++)
179 printf("%s ", host->h_aliases[i]);
186 static void usage(void)
188 fprintf(stderr, "usage: ahost [-t {a|aaaa}] {host|addr} ...\n");