Makefile.am: increment -version-info for 1.10.0 release
[platform/upstream/c-ares.git] / ahost.c
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
53 int main(int argc, char **argv)
54 {
55   ares_channel channel;
56   int status, nfds, c, addr_family = AF_INET;
57   fd_set read_fds, write_fds;
58   struct timeval *tvp, tv;
59   struct in_addr addr4;
60   struct ares_in6_addr addr6;
61
62 #ifdef USE_WINSOCK
63   WORD wVersionRequested = MAKEWORD(USE_WINSOCK,USE_WINSOCK);
64   WSADATA wsaData;
65   WSAStartup(wVersionRequested, &wsaData);
66 #endif
67
68   status = ares_library_init(ARES_LIB_INIT_ALL);
69   if (status != ARES_SUCCESS)
70     {
71       fprintf(stderr, "ares_library_init: %s\n", ares_strerror(status));
72       return 1;
73     }
74
75   while ((c = ares_getopt(argc,argv,"dt:h")) != -1)
76     {
77       switch (c)
78         {
79         case 'd':
80 #ifdef WATT32
81           dbug_init();
82 #endif
83           break;
84         case 't':
85           if (!strcasecmp(optarg,"a"))
86             addr_family = AF_INET;
87           else if (!strcasecmp(optarg,"aaaa"))
88             addr_family = AF_INET6;
89           else
90             usage();
91           break;
92         case 'h':
93         default:
94           usage();
95           break;
96         }
97     }
98
99   argc -= optind;
100   argv += optind;
101   if (argc < 1)
102     usage();
103
104   status = ares_init(&channel);
105   if (status != ARES_SUCCESS)
106     {
107       fprintf(stderr, "ares_init: %s\n", ares_strerror(status));
108       return 1;
109     }
110
111   /* Initiate the queries, one per command-line argument. */
112   for ( ; *argv; argv++)
113     {
114       if (ares_inet_pton(AF_INET, *argv, &addr4) == 1)
115         {
116           ares_gethostbyaddr(channel, &addr4, sizeof(addr4), AF_INET, callback,
117                              *argv);
118         }
119       else if (ares_inet_pton(AF_INET6, *argv, &addr6) == 1)
120         {
121           ares_gethostbyaddr(channel, &addr6, sizeof(addr6), AF_INET6, callback,
122                              *argv);
123         }
124       else
125         {
126           ares_gethostbyname(channel, *argv, addr_family, callback, *argv);
127         }
128     }
129
130   /* Wait for all queries to complete. */
131   for (;;)
132     {
133       FD_ZERO(&read_fds);
134       FD_ZERO(&write_fds);
135       nfds = ares_fds(channel, &read_fds, &write_fds);
136       if (nfds == 0)
137         break;
138       tvp = ares_timeout(channel, NULL, &tv);
139       select(nfds, &read_fds, &write_fds, NULL, tvp);
140       ares_process(channel, &read_fds, &write_fds);
141     }
142
143   ares_destroy(channel);
144
145   ares_library_cleanup();
146
147 #ifdef USE_WINSOCK
148   WSACleanup();
149 #endif
150
151   return 0;
152 }
153
154 static void callback(void *arg, int status, int timeouts, struct hostent *host)
155 {
156   char **p;
157
158   (void)timeouts;
159
160   if (status != ARES_SUCCESS)
161     {
162       fprintf(stderr, "%s: %s\n", (char *) arg, ares_strerror(status));
163       return;
164     }
165
166   for (p = host->h_addr_list; *p; p++)
167     {
168       char addr_buf[46] = "??";
169
170       ares_inet_ntop(host->h_addrtype, *p, addr_buf, sizeof(addr_buf));
171       printf("%-32s\t%s", host->h_name, addr_buf);
172 #if 0
173       if (host->h_aliases[0])
174         {
175            int i;
176
177            printf (", Aliases: ");
178            for (i = 0; host->h_aliases[i]; i++)
179                printf("%s ", host->h_aliases[i]);
180         }
181 #endif
182       puts("");
183     }
184 }
185
186 static void usage(void)
187 {
188   fprintf(stderr, "usage: ahost [-t {a|aaaa}] {host|addr} ...\n");
189   exit(1);
190 }