1 /* Copyright 1998 by the Massachusetts Institute of Technology.
2 *
3 *
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.
15 */
16
17 #include "ares_setup.h"
18
19 #if !defined(WIN32) || defined(WATT32)
20 #include <netinet/in.h>
21 #include <arpa/inet.h>
22 #include <netdb.h>
23 #endif
24
25 #ifdef HAVE_STRINGS_H
26 #include <strings.h>
27 #endif
28
29 #include "ares.h"
30 #include "ares_dns.h"
31 #include "ares_getopt.h"
32 #include "ares_ipv6.h"
33 #include "ares_nowarn.h"
34
35 #ifndef HAVE_STRDUP
36 # include "ares_strdup.h"
37 # define strdup(ptr) ares_strdup(ptr)
38 #endif
39
40 #ifndef HAVE_STRCASECMP
41 # include "ares_strcasecmp.h"
42 # define strcasecmp(p1,p2) ares_strcasecmp(p1,p2)
43 #endif
44
45 #ifndef HAVE_STRNCASECMP
46 # include "ares_strcasecmp.h"
47 # define strncasecmp(p1,p2,n) ares_strncasecmp(p1,p2,n)
48 #endif
49
50 static void callback(void *arg, int status, int timeouts, struct hostent *host);
51 static void usage(void);
52
main(int argc,char ** argv)53 int main(int argc, char **argv)
54 {
55 struct ares_options options;
56 int optmask = 0;
57 ares_channel channel;
58 int status, nfds, c, addr_family = AF_INET;
59 fd_set read_fds, write_fds;
60 struct timeval *tvp, tv;
61 struct in_addr addr4;
62 struct ares_in6_addr addr6;
63
64 #ifdef USE_WINSOCK
65 WORD wVersionRequested = MAKEWORD(USE_WINSOCK,USE_WINSOCK);
66 WSADATA wsaData;
67 WSAStartup(wVersionRequested, &wsaData);
68 #endif
69
70 memset(&options, 0, sizeof(options));
71
72 status = ares_library_init(ARES_LIB_INIT_ALL);
73 if (status != ARES_SUCCESS)
74 {
75 fprintf(stderr, "ares_library_init: %s\n", ares_strerror(status));
76 return 1;
77 }
78
79 while ((c = ares_getopt(argc,argv,"dt:hs:")) != -1)
80 {
81 switch (c)
82 {
83 case 'd':
84 #ifdef WATT32
85 dbug_init();
86 #endif
87 break;
88 case 's':
89 optmask |= ARES_OPT_DOMAINS;
90 options.ndomains++;
91 options.domains = (char **)realloc(options.domains,
92 options.ndomains * sizeof(char *));
93 options.domains[options.ndomains - 1] = strdup(optarg);
94 break;
95 case 't':
96 if (!strcasecmp(optarg,"a"))
97 addr_family = AF_INET;
98 else if (!strcasecmp(optarg,"aaaa"))
99 addr_family = AF_INET6;
100 else if (!strcasecmp(optarg,"u"))
101 addr_family = AF_UNSPEC;
102 else
103 usage();
104 break;
105 case 'h':
106 default:
107 usage();
108 break;
109 }
110 }
111
112 argc -= optind;
113 argv += optind;
114 if (argc < 1)
115 usage();
116
117 status = ares_init_options(&channel, &options, optmask);
118 if (status != ARES_SUCCESS)
119 {
120 fprintf(stderr, "ares_init: %s\n", ares_strerror(status));
121 return 1;
122 }
123
124 /* Initiate the queries, one per command-line argument. */
125 for ( ; *argv; argv++)
126 {
127 if (ares_inet_pton(AF_INET, *argv, &addr4) == 1)
128 {
129 ares_gethostbyaddr(channel, &addr4, sizeof(addr4), AF_INET, callback,
130 *argv);
131 }
132 else if (ares_inet_pton(AF_INET6, *argv, &addr6) == 1)
133 {
134 ares_gethostbyaddr(channel, &addr6, sizeof(addr6), AF_INET6, callback,
135 *argv);
136 }
137 else
138 {
139 ares_gethostbyname(channel, *argv, addr_family, callback, *argv);
140 }
141 }
142
143 /* Wait for all queries to complete. */
144 for (;;)
145 {
146 int res;
147 FD_ZERO(&read_fds);
148 FD_ZERO(&write_fds);
149 nfds = ares_fds(channel, &read_fds, &write_fds);
150 if (nfds == 0)
151 break;
152 tvp = ares_timeout(channel, NULL, &tv);
153 res = select(nfds, &read_fds, &write_fds, NULL, tvp);
154 if (-1 == res)
155 break;
156 ares_process(channel, &read_fds, &write_fds);
157 }
158
159 ares_destroy(channel);
160
161 ares_library_cleanup();
162
163 #ifdef USE_WINSOCK
164 WSACleanup();
165 #endif
166
167 return 0;
168 }
169
callback(void * arg,int status,int timeouts,struct hostent * host)170 static void callback(void *arg, int status, int timeouts, struct hostent *host)
171 {
172 char **p;
173
174 (void)timeouts;
175
176 if (status != ARES_SUCCESS)
177 {
178 fprintf(stderr, "%s: %s\n", (char *) arg, ares_strerror(status));
179 return;
180 }
181
182 for (p = host->h_addr_list; *p; p++)
183 {
184 char addr_buf[46] = "??";
185
186 ares_inet_ntop(host->h_addrtype, *p, addr_buf, sizeof(addr_buf));
187 printf("%-32s\t%s", host->h_name, addr_buf);
188 #if 0
189 if (host->h_aliases[0])
190 {
191 int i;
192
193 printf (", Aliases: ");
194 for (i = 0; host->h_aliases[i]; i++)
195 printf("%s ", host->h_aliases[i]);
196 }
197 #endif
198 puts("");
199 }
200 }
201
usage(void)202 static void usage(void)
203 {
204 fprintf(stderr, "usage: ahost [-h] [-d] [-s {domain}] [-t {a|aaaa|u}] {host|addr} ...\n");
205 exit(1);
206 }
207