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