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.3 1999/10/23 19:28:13 danw Exp $";
18 #include <sys/types.h>
20 #include <sys/socket.h>
21 #include <netinet/in.h>
22 #include <arpa/inet.h>
31 #define INADDR_NONE 0xffffffff
34 static void callback(void *arg, int status, struct hostent *host);
35 static void usage(void);
37 int main(int argc, char **argv)
41 fd_set read_fds, write_fds;
42 struct timeval *tvp, tv;
49 status = ares_init(&channel);
50 if (status != ARES_SUCCESS)
52 fprintf(stderr, "ares_init: %s\n", ares_strerror(status, &errmem));
53 ares_free_errmem(errmem);
57 /* Initiate the queries, one per command-line argument. */
58 for (argv++; *argv; argv++)
60 addr.s_addr = inet_addr(*argv);
61 if (addr.s_addr == INADDR_NONE)
62 ares_gethostbyname(channel, *argv, AF_INET, callback, *argv);
65 ares_gethostbyaddr(channel, &addr, sizeof(addr), AF_INET, callback,
70 /* Wait for all queries to complete. */
75 nfds = ares_fds(channel, &read_fds, &write_fds);
78 tvp = ares_timeout(channel, NULL, &tv);
79 select(nfds, &read_fds, &write_fds, NULL, tvp);
80 ares_process(channel, &read_fds, &write_fds);
83 ares_destroy(channel);
87 static void callback(void *arg, int status, struct hostent *host)
92 if (status != ARES_SUCCESS)
94 fprintf(stderr, "%s: %s\n", (char *) arg, ares_strerror(status, &mem));
95 ares_free_errmem(mem);
99 for (p = host->h_addr_list; *p; p++)
101 memcpy(&addr, *p, sizeof(struct in_addr));
102 printf("%-32s\t%s\n", host->h_name, inet_ntoa(addr));
106 static void usage(void)
108 fprintf(stderr, "usage: ahost {host|addr} ...\n");