1 /* Copyright 1998 by the Massachusetts Institute of Technology.
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.
17 #include <sys/types.h>
22 #include <sys/socket.h>
23 #include <netinet/in.h>
24 #include <arpa/inet.h>
35 #include "inet_ntop.h"
36 #include "inet_net_pton.h"
39 #define INADDR_NONE 0xffffffff
42 #ifndef HAVE_STRUCT_IN6_ADDR
45 unsigned char s6_addr[16];
49 static void callback(void *arg, int status, struct hostent *host);
50 static void usage(void);
52 int main(int argc, char **argv)
56 fd_set read_fds, write_fds;
57 struct timeval *tvp, tv;
59 struct in6_addr addr6;
62 WORD wVersionRequested = MAKEWORD(1,1);
64 WSAStartup(wVersionRequested, &wsaData);
70 status = ares_init(&channel);
71 if (status != ARES_SUCCESS)
73 fprintf(stderr, "ares_init: %s\n", ares_strerror(status));
77 /* Initiate the queries, one per command-line argument. */
78 for (argv++; *argv; argv++)
80 if (ares_inet_pton(AF_INET, *argv, &addr4) == 1)
82 ares_gethostbyaddr(channel, &addr4, sizeof(addr4), AF_INET, callback,
85 else if (ares_inet_pton(AF_INET6, *argv, &addr6) == 1)
87 ares_gethostbyaddr(channel, &addr6, sizeof(addr6), AF_INET6, callback,
92 /* assume user wants A-records */
93 ares_gethostbyname(channel, *argv, AF_INET, callback, *argv);
97 /* Wait for all queries to complete. */
102 nfds = ares_fds(channel, &read_fds, &write_fds);
105 tvp = ares_timeout(channel, NULL, &tv);
106 select(nfds, &read_fds, &write_fds, NULL, tvp);
107 ares_process(channel, &read_fds, &write_fds);
110 ares_destroy(channel);
114 static void callback(void *arg, int status, struct hostent *host)
118 if (status != ARES_SUCCESS)
120 fprintf(stderr, "%s: %s\n", (char *) arg, ares_strerror(status));
124 for (p = host->h_addr_list; *p; p++)
126 char addr_buf[46] = "??";
128 ares_inet_ntop(host->h_addrtype, *p, addr_buf, sizeof(addr_buf));
129 printf("%-32s\t%s", host->h_name, addr_buf);
131 if (host->h_aliases[0])
135 printf (", Aliases: ");
136 for (i = 0; host->h_aliases[i]; i++)
137 printf("%s ", host->h_aliases[i]);
144 static void usage(void)
146 fprintf(stderr, "usage: ahost {host|addr} ...\n");