• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 static void print_help_info_ahost(void);
53 
main(int argc,char ** argv)54 int main(int argc, char **argv)
55 {
56   struct ares_options options;
57   int optmask = 0;
58   ares_channel channel;
59   int status, nfds, c, addr_family = AF_INET;
60   fd_set read_fds, write_fds;
61   struct timeval *tvp, tv;
62   struct in_addr addr4;
63   struct ares_in6_addr addr6;
64 
65 #ifdef USE_WINSOCK
66   WORD wVersionRequested = MAKEWORD(USE_WINSOCK,USE_WINSOCK);
67   WSADATA wsaData;
68   WSAStartup(wVersionRequested, &wsaData);
69 #endif
70 
71   memset(&options, 0, sizeof(options));
72 
73   status = ares_library_init(ARES_LIB_INIT_ALL);
74   if (status != ARES_SUCCESS)
75     {
76       fprintf(stderr, "ares_library_init: %s\n", ares_strerror(status));
77       return 1;
78     }
79 
80   while ((c = ares_getopt(argc,argv,"dt:h?s:")) != -1)
81     {
82       switch (c)
83         {
84         case 'd':
85 #ifdef WATT32
86           dbug_init();
87 #endif
88           break;
89         case 's':
90           optmask |= ARES_OPT_DOMAINS;
91           options.ndomains++;
92           options.domains = (char **)realloc(options.domains,
93                                              options.ndomains * sizeof(char *));
94           options.domains[options.ndomains - 1] = strdup(optarg);
95           break;
96         case 't':
97           if (!strcasecmp(optarg,"a"))
98             addr_family = AF_INET;
99           else if (!strcasecmp(optarg,"aaaa"))
100             addr_family = AF_INET6;
101           else if (!strcasecmp(optarg,"u"))
102             addr_family = AF_UNSPEC;
103           else
104             usage();
105           break;
106         case 'h':
107           print_help_info_ahost();
108           break;
109         case '?':
110           print_help_info_ahost();
111           break;
112         default:
113           usage();
114           break;
115         }
116     }
117 
118   argc -= optind;
119   argv += optind;
120   if (argc < 1)
121     usage();
122 
123   status = ares_init_options(&channel, &options, optmask);
124   if (status != ARES_SUCCESS)
125     {
126       fprintf(stderr, "ares_init: %s\n", ares_strerror(status));
127       return 1;
128     }
129 
130   /* Initiate the queries, one per command-line argument. */
131   for ( ; *argv; argv++)
132     {
133       if (ares_inet_pton(AF_INET, *argv, &addr4) == 1)
134         {
135           ares_gethostbyaddr(channel, &addr4, sizeof(addr4), AF_INET, callback,
136                              *argv);
137         }
138       else if (ares_inet_pton(AF_INET6, *argv, &addr6) == 1)
139         {
140           ares_gethostbyaddr(channel, &addr6, sizeof(addr6), AF_INET6, callback,
141                              *argv);
142         }
143       else
144         {
145           ares_gethostbyname(channel, *argv, addr_family, callback, *argv);
146         }
147     }
148 
149   /* Wait for all queries to complete. */
150   for (;;)
151     {
152       int res;
153       FD_ZERO(&read_fds);
154       FD_ZERO(&write_fds);
155       nfds = ares_fds(channel, &read_fds, &write_fds);
156       if (nfds == 0)
157         break;
158       tvp = ares_timeout(channel, NULL, &tv);
159       res = select(nfds, &read_fds, &write_fds, NULL, tvp);
160       if (-1 == res)
161         break;
162       ares_process(channel, &read_fds, &write_fds);
163     }
164 
165   ares_destroy(channel);
166 
167   ares_library_cleanup();
168 
169 #ifdef USE_WINSOCK
170   WSACleanup();
171 #endif
172 
173   return 0;
174 }
175 
callback(void * arg,int status,int timeouts,struct hostent * host)176 static void callback(void *arg, int status, int timeouts, struct hostent *host)
177 {
178   char **p;
179 
180   (void)timeouts;
181 
182   if (status != ARES_SUCCESS)
183     {
184       fprintf(stderr, "%s: %s\n", (char *) arg, ares_strerror(status));
185       return;
186     }
187 
188   for (p = host->h_addr_list; *p; p++)
189     {
190       char addr_buf[46] = "??";
191 
192       ares_inet_ntop(host->h_addrtype, *p, addr_buf, sizeof(addr_buf));
193       printf("%-32s\t%s", host->h_name, addr_buf);
194 #if 0
195       if (host->h_aliases[0])
196         {
197            int i;
198 
199            printf (", Aliases: ");
200            for (i = 0; host->h_aliases[i]; i++)
201                printf("%s ", host->h_aliases[i]);
202         }
203 #endif
204       puts("");
205     }
206 }
207 
usage(void)208 static void usage(void)
209 {
210   fprintf(stderr, "usage: ahost [-h] [-d] [-s {domain}] [-t {a|aaaa|u}] {host|addr} ...\n");
211   exit(1);
212 }
213 
214 /* Information from the man page. Formatting taken from man -h */
print_help_info_ahost(void)215 static void print_help_info_ahost(void) {
216     printf("ahost, version %s\n\n", ARES_VERSION_STR);
217     printf("usage: ahost [-h] [-d] [[-s domain] ...] [-t a|aaaa|u] host|addr ...\n\n"
218     "  h : Display this help and exit.\n"
219     "  d : Print some extra debugging output.\n\n"
220     "  s domain : Specify the domain to search instead of using the default values\n"
221     "               from /etc/resolv.conf. This option only has an effect on\n"
222     "               platforms that use /etc/resolv.conf for DNS configuration;\n"
223     "               it has no effect on other platforms (such as Win32 or Android).\n\n"
224     "  t type   : If type is \"a\", print the A record (default).\n"
225     "               If type is \"aaaa\", print the AAAA record.\n"
226     "               If type is \"u\", look for either AAAA or A record (in that order).\n\n");
227     exit(0);
228 }
229