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