Update using ares_inet_pton() and ares_inet_ntop().
[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
33 #include "ares.h"
34 #include "ares_dns.h"
35 #include "inet_ntop.h"
36 #include "inet_net_pton.h"
37
38 #ifndef INADDR_NONE
39 #define INADDR_NONE 0xffffffff
40 #endif
41
42 #ifndef HAVE_STRUCT_IN6_ADDR
43 struct in6_addr
44 {
45   unsigned char s6_addr[16];
46 };
47 #endif
48
49 static void callback(void *arg, int status, struct hostent *host);
50 static void usage(void);
51
52 int main(int argc, char **argv)
53 {
54   ares_channel channel;
55   int status, nfds;
56   fd_set read_fds, write_fds;
57   struct timeval *tvp, tv;
58   struct in_addr addr4;
59   struct in6_addr addr6;
60
61 #ifdef WIN32
62   WORD wVersionRequested = MAKEWORD(1,1);
63   WSADATA wsaData;
64   WSAStartup(wVersionRequested, &wsaData);
65 #endif
66
67   if (argc <= 1)
68     usage();
69
70   status = ares_init(&channel);
71   if (status != ARES_SUCCESS)
72     {
73       fprintf(stderr, "ares_init: %s\n", ares_strerror(status));
74       return 1;
75     }
76
77   /* Initiate the queries, one per command-line argument. */
78   for (argv++; *argv; argv++)
79     {
80       if (ares_inet_pton(AF_INET, *argv, &addr4) == 1)
81         {
82           ares_gethostbyaddr(channel, &addr4, sizeof(addr4), AF_INET, callback,
83                              *argv);
84         }
85       else if (ares_inet_pton(AF_INET6, *argv, &addr6) == 1)
86         {
87           ares_gethostbyaddr(channel, &addr6, sizeof(addr6), AF_INET6, callback,
88                              *argv);
89         }
90       else
91         {
92           /* assume user wants A-records */
93           ares_gethostbyname(channel, *argv, AF_INET, callback, *argv);
94         }
95     }
96
97   /* Wait for all queries to complete. */
98   while (1)
99     {
100       FD_ZERO(&read_fds);
101       FD_ZERO(&write_fds);
102       nfds = ares_fds(channel, &read_fds, &write_fds);
103       if (nfds == 0)
104         break;
105       tvp = ares_timeout(channel, NULL, &tv);
106       select(nfds, &read_fds, &write_fds, NULL, tvp);
107       ares_process(channel, &read_fds, &write_fds);
108     }
109
110   ares_destroy(channel);
111   return 0;
112 }
113
114 static void callback(void *arg, int status, struct hostent *host)
115 {
116   char **p;
117
118   if (status != ARES_SUCCESS)
119     {
120       fprintf(stderr, "%s: %s\n", (char *) arg, ares_strerror(status));
121       return;
122     }
123
124   for (p = host->h_addr_list; *p; p++)
125     {
126       char addr_buf[46] = "??";
127
128       ares_inet_ntop(host->h_addrtype, *p, addr_buf, sizeof(addr_buf));
129       printf("%-32s\t%s", host->h_name, addr_buf);
130 #if 0
131       if (host->h_aliases[0])
132         {
133            int i;
134
135            printf (", Aliases: ");
136            for (i = 0; host->h_aliases[i]; i++)
137                printf("%s ", host->h_aliases[i]);
138         }
139 #endif
140       puts("");
141     }
142 }
143
144 static void usage(void)
145 {
146   fprintf(stderr, "usage: ahost {host|addr} ...\n");
147   exit(1);
148 }