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.
16 static const char rcsid[] = "$Id: ahost.c,v 1.4 2001/04/02 17:39:42 ghudson Exp $";
18 #include <sys/types.h>
20 #include <sys/socket.h>
21 #include <netinet/in.h>
22 #include <arpa/inet.h>
32 #define INADDR_NONE 0xffffffff
35 static void callback(void *arg, int status, struct hostent *host);
36 static void usage(void);
38 int main(int argc, char **argv)
42 fd_set read_fds, write_fds;
43 struct timeval *tvp, tv;
50 status = ares_init(&channel);
51 if (status != ARES_SUCCESS)
53 fprintf(stderr, "ares_init: %s\n", ares_strerror(status, &errmem));
54 ares_free_errmem(errmem);
58 /* Initiate the queries, one per command-line argument. */
59 for (argv++; *argv; argv++)
61 addr.s_addr = inet_addr(*argv);
62 if (addr.s_addr == INADDR_NONE)
63 ares_gethostbyname(channel, *argv, AF_INET, callback, *argv);
66 ares_gethostbyaddr(channel, &addr, sizeof(addr), AF_INET, callback,
71 /* Wait for all queries to complete. */
76 nfds = ares_fds(channel, &read_fds, &write_fds);
79 tvp = ares_timeout(channel, NULL, &tv);
80 select(nfds, &read_fds, &write_fds, NULL, tvp);
81 ares_process(channel, &read_fds, &write_fds);
84 ares_destroy(channel);
88 static void callback(void *arg, int status, struct hostent *host)
93 if (status != ARES_SUCCESS)
95 fprintf(stderr, "%s: %s\n", (char *) arg, ares_strerror(status, &mem));
96 ares_free_errmem(mem);
100 for (p = host->h_addr_list; *p; p++)
102 memcpy(&addr, *p, sizeof(struct in_addr));
103 printf("%-32s\t%s\n", host->h_name, inet_ntoa(addr));
107 static void usage(void)
109 fprintf(stderr, "usage: ahost {host|addr} ...\n");