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 #ifdef HAVE_SYS_TIME_H
23 #include <sys/socket.h>
24 #include <netinet/in.h>
25 #include <arpa/inet.h>
41 #include "inet_ntop.h"
42 #include "inet_net_pton.h"
43 #include "ares_getopt.h"
44 #include "ares_ipv6.h"
45 #include "ares_nowarn.h"
48 # include "ares_strdup.h"
49 # define strdup(ptr) ares_strdup(ptr)
52 #ifndef HAVE_STRCASECMP
53 # include "ares_strcasecmp.h"
54 # define strcasecmp(p1,p2) ares_strcasecmp(p1,p2)
57 #ifndef HAVE_STRNCASECMP
58 # include "ares_strcasecmp.h"
59 # define strncasecmp(p1,p2,n) ares_strncasecmp(p1,p2,n)
62 static void callback(void *arg, int status, int timeouts, struct hostent *host);
63 static void usage(void);
65 int main(int argc, char **argv)
68 int status, nfds, c, addr_family = AF_INET;
69 fd_set read_fds, write_fds;
70 struct timeval *tvp, tv;
72 struct ares_in6_addr addr6;
75 WORD wVersionRequested = MAKEWORD(USE_WINSOCK,USE_WINSOCK);
77 WSAStartup(wVersionRequested, &wsaData);
80 status = ares_library_init(ARES_LIB_INIT_ALL);
81 if (status != ARES_SUCCESS)
83 fprintf(stderr, "ares_library_init: %s\n", ares_strerror(status));
87 while ((c = ares_getopt(argc,argv,"dt:h")) != -1)
97 if (!strcasecmp(optarg,"a"))
98 addr_family = AF_INET;
99 else if (!strcasecmp(optarg,"aaaa"))
100 addr_family = AF_INET6;
116 status = ares_init(&channel);
117 if (status != ARES_SUCCESS)
119 fprintf(stderr, "ares_init: %s\n", ares_strerror(status));
123 /* Initiate the queries, one per command-line argument. */
124 for ( ; *argv; argv++)
126 if (ares_inet_pton(AF_INET, *argv, &addr4) == 1)
128 ares_gethostbyaddr(channel, &addr4, sizeof(addr4), AF_INET, callback,
131 else if (ares_inet_pton(AF_INET6, *argv, &addr6) == 1)
133 ares_gethostbyaddr(channel, &addr6, sizeof(addr6), AF_INET6, callback,
138 ares_gethostbyname(channel, *argv, addr_family, callback, *argv);
142 /* Wait for all queries to complete. */
147 nfds = ares_fds(channel, &read_fds, &write_fds);
150 tvp = ares_timeout(channel, NULL, &tv);
151 select(nfds, &read_fds, &write_fds, NULL, tvp);
152 ares_process(channel, &read_fds, &write_fds);
155 ares_destroy(channel);
157 ares_library_cleanup();
166 static void callback(void *arg, int status, int timeouts, struct hostent *host)
172 if (status != ARES_SUCCESS)
174 fprintf(stderr, "%s: %s\n", (char *) arg, ares_strerror(status));
178 for (p = host->h_addr_list; *p; p++)
180 char addr_buf[46] = "??";
182 ares_inet_ntop(host->h_addrtype, *p, addr_buf, sizeof(addr_buf));
183 printf("%-32s\t%s", host->h_name, addr_buf);
185 if (host->h_aliases[0])
189 printf (", Aliases: ");
190 for (i = 0; host->h_aliases[i]; i++)
191 printf("%s ", host->h_aliases[i]);
198 static void usage(void)
200 fprintf(stderr, "usage: ahost [-t {a|aaaa}] {host|addr} ...\n");