Support Watt-32 under Win32.
[platform/upstream/c-ares.git] / ahost.c
1 /* Copyright 1998 by the Massachusetts Institute of Technology.
2  *
3  * $Id$
4  *
5  * Permission to use, copy, modify, and distribute this
6  * software and its documentation for any purpose and without
7  * fee is hereby granted, provided that the above copyright
8  * notice appear in all copies and that both that copyright
9  * notice and this permission notice appear in supporting
10  * documentation, and that the name of M.I.T. not be used in
11  * advertising or publicity pertaining to distribution of the
12  * software without specific, written prior permission.
13  * M.I.T. makes no representations about the suitability of
14  * this software for any purpose.  It is provided "as is"
15  * without express or implied warranty.
16  */
17
18 #include "setup.h"
19
20 #if !defined(WIN32) || defined(WATT32)
21 #ifdef HAVE_SYS_TIME_H
22 #include <sys/time.h>
23 #endif
24 #include <sys/socket.h>
25 #include <netinet/in.h>
26 #include <arpa/inet.h>
27 #include <netdb.h>
28 #ifdef HAVE_UNISTD_H
29 #include <unistd.h>
30 #endif
31 #endif
32 #ifdef HAVE_STRINGS_H
33 #include <strings.h>
34 #endif
35
36 #include <stdio.h>
37 #include <stdlib.h>
38 #include <string.h>
39
40 #include "ares.h"
41 #include "ares_dns.h"
42 #include "inet_ntop.h"
43 #include "inet_net_pton.h"
44 #include "ares_getopt.h"
45
46 static void callback(void *arg, int status, int timeouts, struct hostent *host);
47 static void usage(void);
48
49 int main(int argc, char **argv)
50 {
51   ares_channel channel;
52   int status, nfds, c, addr_family = AF_INET;
53   fd_set read_fds, write_fds;
54   struct timeval *tvp, tv;
55   struct in_addr addr4;
56   struct in6_addr addr6;
57
58 #ifdef USE_WINSOCK
59   WORD wVersionRequested = MAKEWORD(USE_WINSOCK,USE_WINSOCK);
60   WSADATA wsaData;
61   WSAStartup(wVersionRequested, &wsaData);
62 #endif
63
64   while ((c = ares_getopt(argc,argv,"dt:h")) != -1)
65     {
66       switch (c)
67         {
68         case 'd':
69 #ifdef WATT32
70           dbug_init();
71 #endif
72           break;
73         case 't':
74           if (!strcasecmp(optarg,"a"))
75             addr_family = AF_INET;
76           else if (!strcasecmp(optarg,"aaaa"))
77             addr_family = AF_INET6;
78           else
79             usage();
80           break;
81         case 'h':
82         default:
83           usage();
84           break;
85         }
86     }
87
88   argc -= optind;
89   argv += optind;
90   if (argc < 1)
91     usage();
92
93   status = ares_init(&channel);
94   if (status != ARES_SUCCESS)
95     {
96       fprintf(stderr, "ares_init: %s\n", ares_strerror(status));
97       return 1;
98     }
99
100   /* Initiate the queries, one per command-line argument. */
101   for ( ; *argv; argv++)
102     {
103       if (ares_inet_pton(AF_INET, *argv, &addr4) == 1)
104         {
105           ares_gethostbyaddr(channel, &addr4, sizeof(addr4), AF_INET, callback,
106                              *argv);
107         }
108       else if (ares_inet_pton(AF_INET6, *argv, &addr6) == 1)
109         {
110           ares_gethostbyaddr(channel, &addr6, sizeof(addr6), AF_INET6, callback,
111                              *argv);
112         }
113       else
114         {
115           ares_gethostbyname(channel, *argv, addr_family, callback, *argv);
116         }
117     }
118
119   /* Wait for all queries to complete. */
120   while (1)
121     {
122       FD_ZERO(&read_fds);
123       FD_ZERO(&write_fds);
124       nfds = ares_fds(channel, &read_fds, &write_fds);
125       if (nfds == 0)
126         break;
127       tvp = ares_timeout(channel, NULL, &tv);
128       select(nfds, &read_fds, &write_fds, NULL, tvp);
129       ares_process(channel, &read_fds, &write_fds);
130     }
131
132   ares_destroy(channel);
133
134 #ifdef USE_WINSOCK
135   WSACleanup();
136 #endif
137
138   return 0;
139 }
140
141 static void callback(void *arg, int status, int timeouts, struct hostent *host)
142 {
143   char **p;
144
145   (void)timeouts;
146
147   if (status != ARES_SUCCESS)
148     {
149       fprintf(stderr, "%s: %s\n", (char *) arg, ares_strerror(status));
150       return;
151     }
152
153   for (p = host->h_addr_list; *p; p++)
154     {
155       char addr_buf[46] = "??";
156
157       ares_inet_ntop(host->h_addrtype, *p, addr_buf, sizeof(addr_buf));
158       printf("%-32s\t%s", host->h_name, addr_buf);
159 #if 0
160       if (host->h_aliases[0])
161         {
162            int i;
163
164            printf (", Aliases: ");
165            for (i = 0; host->h_aliases[i]; i++)
166                printf("%s ", host->h_aliases[i]);
167         }
168 #endif
169       puts("");
170     }
171 }
172
173 static void usage(void)
174 {
175   fprintf(stderr, "usage: ahost [-t {a|aaaa}] {host|addr} ...\n");
176   exit(1);
177 }