removed tabs and trailing whitespace from source
[platform/upstream/c-ares.git] / ahost.c
1 /* Copyright 1998 by the Massachusetts Institute of Technology.
2  *
3  * Permission to use, copy, modify, and distribute this
4  * software and its documentation for any purpose and without
5  * fee is hereby granted, provided that the above copyright
6  * notice appear in all copies and that both that copyright
7  * notice and this permission notice appear in supporting
8  * documentation, and that the name of M.I.T. not be used in
9  * advertising or publicity pertaining to distribution of the
10  * software without specific, written prior permission.
11  * M.I.T. makes no representations about the suitability of
12  * this software for any purpose.  It is provided "as is"
13  * without express or implied warranty.
14  */
15
16 #include "setup.h"
17 #include <sys/types.h>
18
19 #ifdef WIN32
20 #else
21 #include <sys/time.h>
22 #include <sys/socket.h>
23 #include <netinet/in.h>
24 #include <arpa/inet.h>
25 #include <netdb.h>
26 #include <unistd.h>
27 #endif
28
29 #include <stdio.h>
30 #include <stdlib.h>
31 #include <string.h>
32 #include "ares.h"
33 #include "ares_dns.h"
34
35 #ifndef INADDR_NONE
36 #define INADDR_NONE 0xffffffff
37 #endif
38
39 static void callback(void *arg, int status, struct hostent *host);
40 static void usage(void);
41
42 int main(int argc, char **argv)
43 {
44   ares_channel channel;
45   int status, nfds;
46   fd_set read_fds, write_fds;
47   struct timeval *tvp, tv;
48   struct in_addr addr;
49
50 #ifdef WIN32
51   WORD wVersionRequested = MAKEWORD(1,1);
52   WSADATA wsaData;
53   WSAStartup(wVersionRequested, &wsaData);
54 #endif
55
56   if (argc <= 1)
57     usage();
58
59   status = ares_init(&channel);
60   if (status != ARES_SUCCESS)
61     {
62       fprintf(stderr, "ares_init: %s\n", ares_strerror(status));
63       return 1;
64     }
65
66   /* Initiate the queries, one per command-line argument. */
67   for (argv++; *argv; argv++)
68     {
69       addr.s_addr = inet_addr(*argv);
70       if (addr.s_addr == INADDR_NONE)
71         ares_gethostbyname(channel, *argv, AF_INET, callback, *argv);
72       else
73         {
74           ares_gethostbyaddr(channel, &addr, sizeof(addr), AF_INET, callback,
75                              *argv);
76         }
77     }
78
79   /* Wait for all queries to complete. */
80   while (1)
81     {
82       FD_ZERO(&read_fds);
83       FD_ZERO(&write_fds);
84       nfds = ares_fds(channel, &read_fds, &write_fds);
85       if (nfds == 0)
86         break;
87       tvp = ares_timeout(channel, NULL, &tv);
88       select(nfds, &read_fds, &write_fds, NULL, tvp);
89       ares_process(channel, &read_fds, &write_fds);
90     }
91
92   ares_destroy(channel);
93   return 0;
94 }
95
96 static void callback(void *arg, int status, struct hostent *host)
97 {
98   struct in_addr addr;
99   char **p;
100
101   if (status != ARES_SUCCESS)
102     {
103       fprintf(stderr, "%s: %s\n", (char *) arg, ares_strerror(status));
104       return;
105     }
106
107   for (p = host->h_addr_list; *p; p++)
108     {
109       memcpy(&addr, *p, sizeof(struct in_addr));
110       printf("%-32s\t%s\n", host->h_name, inet_ntoa(addr));
111     }
112 }
113
114 static void usage(void)
115 {
116   fprintf(stderr, "usage: ahost {host|addr} ...\n");
117   exit(1);
118 }