]> asedeno.scripts.mit.edu Git - 1ts-debian.git/blob - libares/ahost.c
Initial revision
[1ts-debian.git] / libares / 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 static const char rcsid[] = "$Id: ahost.c,v 1.3 1999/10/23 19:28:13 danw Exp $";
17
18 #include <sys/types.h>
19 #include <sys/time.h>
20 #include <sys/socket.h>
21 #include <netinet/in.h>
22 #include <arpa/inet.h>
23 #include <stdio.h>
24 #include <string.h>
25 #include <unistd.h>
26 #include <netdb.h>
27 #include "ares.h"
28 #include "ares_dns.h"
29
30 #ifndef INADDR_NONE
31 #define INADDR_NONE 0xffffffff
32 #endif
33
34 static void callback(void *arg, int status, struct hostent *host);
35 static void usage(void);
36
37 int main(int argc, char **argv)
38 {
39   ares_channel channel;
40   int status, nfds;
41   fd_set read_fds, write_fds;
42   struct timeval *tvp, tv;
43   char *errmem;
44   struct in_addr addr;
45
46   if (argc == 0)
47     usage();
48
49   status = ares_init(&channel);
50   if (status != ARES_SUCCESS)
51     {
52       fprintf(stderr, "ares_init: %s\n", ares_strerror(status, &errmem));
53       ares_free_errmem(errmem);
54       return 1;
55     }
56
57   /* Initiate the queries, one per command-line argument. */
58   for (argv++; *argv; argv++)
59     {
60       addr.s_addr = inet_addr(*argv);
61       if (addr.s_addr == INADDR_NONE)
62         ares_gethostbyname(channel, *argv, AF_INET, callback, *argv);
63       else
64         {
65           ares_gethostbyaddr(channel, &addr, sizeof(addr), AF_INET, callback,
66                              *argv);
67         }
68     }
69
70   /* Wait for all queries to complete. */
71   while (1)
72     {
73       FD_ZERO(&read_fds);
74       FD_ZERO(&write_fds);
75       nfds = ares_fds(channel, &read_fds, &write_fds);
76       if (nfds == 0)
77         break;
78       tvp = ares_timeout(channel, NULL, &tv);
79       select(nfds, &read_fds, &write_fds, NULL, tvp);
80       ares_process(channel, &read_fds, &write_fds);
81     }
82
83   ares_destroy(channel);
84   return 0;
85 }
86
87 static void callback(void *arg, int status, struct hostent *host)
88 {
89   struct in_addr addr;
90   char *mem, **p;
91
92   if (status != ARES_SUCCESS)
93     {
94       fprintf(stderr, "%s: %s\n", (char *) arg, ares_strerror(status, &mem));
95       ares_free_errmem(mem);
96       return;
97     }
98
99   for (p = host->h_addr_list; *p; p++)
100     {
101       memcpy(&addr, *p, sizeof(struct in_addr));
102       printf("%-32s\t%s\n", host->h_name, inet_ntoa(addr));
103     }
104 }
105
106 static void usage(void)
107 {
108   fprintf(stderr, "usage: ahost {host|addr} ...\n");
109   exit(1);
110 }