source: trunk/athena/lib/ares/ahost.c @ 15940

Revision 15940, 2.8 KB checked in by ghudson, 24 years ago (diff)
Need stdlib.h for exit().
Line 
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
16static const char rcsid[] = "$Id: ahost.c,v 1.4 2001-04-02 17:39:42 ghudson 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 <stdlib.h>
25#include <string.h>
26#include <unistd.h>
27#include <netdb.h>
28#include "ares.h"
29#include "ares_dns.h"
30
31#ifndef INADDR_NONE
32#define INADDR_NONE 0xffffffff
33#endif
34
35static void callback(void *arg, int status, struct hostent *host);
36static void usage(void);
37
38int main(int argc, char **argv)
39{
40  ares_channel channel;
41  int status, nfds;
42  fd_set read_fds, write_fds;
43  struct timeval *tvp, tv;
44  char *errmem;
45  struct in_addr addr;
46
47  if (argc == 0)
48    usage();
49
50  status = ares_init(&channel);
51  if (status != ARES_SUCCESS)
52    {
53      fprintf(stderr, "ares_init: %s\n", ares_strerror(status, &errmem));
54      ares_free_errmem(errmem);
55      return 1;
56    }
57
58  /* Initiate the queries, one per command-line argument. */
59  for (argv++; *argv; argv++)
60    {
61      addr.s_addr = inet_addr(*argv);
62      if (addr.s_addr == INADDR_NONE)
63        ares_gethostbyname(channel, *argv, AF_INET, callback, *argv);
64      else
65        {
66          ares_gethostbyaddr(channel, &addr, sizeof(addr), AF_INET, callback,
67                             *argv);
68        }
69    }
70
71  /* Wait for all queries to complete. */
72  while (1)
73    {
74      FD_ZERO(&read_fds);
75      FD_ZERO(&write_fds);
76      nfds = ares_fds(channel, &read_fds, &write_fds);
77      if (nfds == 0)
78        break;
79      tvp = ares_timeout(channel, NULL, &tv);
80      select(nfds, &read_fds, &write_fds, NULL, tvp);
81      ares_process(channel, &read_fds, &write_fds);
82    }
83
84  ares_destroy(channel);
85  return 0;
86}
87
88static void callback(void *arg, int status, struct hostent *host)
89{
90  struct in_addr addr;
91  char *mem, **p;
92
93  if (status != ARES_SUCCESS)
94    {
95      fprintf(stderr, "%s: %s\n", (char *) arg, ares_strerror(status, &mem));
96      ares_free_errmem(mem);
97      return;
98    }
99
100  for (p = host->h_addr_list; *p; p++)
101    {
102      memcpy(&addr, *p, sizeof(struct in_addr));
103      printf("%-32s\t%s\n", host->h_name, inet_ntoa(addr));
104    }
105}
106
107static void usage(void)
108{
109  fprintf(stderr, "usage: ahost {host|addr} ...\n");
110  exit(1);
111}
Note: See TracBrowser for help on using the repository browser.