2003-10-07 17:54:04 -04:00
|
|
|
/* Copyright 1998 by the Massachusetts Institute of Technology.
|
|
|
|
*
|
|
|
|
* Permission to use, copy, modify, and distribute this
|
|
|
|
* software and its documentation for any purpose and without
|
|
|
|
* fee is hereby granted, provided that the above copyright
|
|
|
|
* notice appear in all copies and that both that copyright
|
|
|
|
* notice and this permission notice appear in supporting
|
|
|
|
* documentation, and that the name of M.I.T. not be used in
|
|
|
|
* advertising or publicity pertaining to distribution of the
|
|
|
|
* software without specific, written prior permission.
|
|
|
|
* M.I.T. makes no representations about the suitability of
|
|
|
|
* this software for any purpose. It is provided "as is"
|
|
|
|
* without express or implied warranty.
|
|
|
|
*/
|
|
|
|
|
2004-07-22 18:18:45 -04:00
|
|
|
#include "setup.h"
|
2003-10-07 17:54:04 -04:00
|
|
|
#include <sys/types.h>
|
|
|
|
|
|
|
|
#ifdef WIN32
|
|
|
|
#else
|
|
|
|
#include <sys/time.h>
|
|
|
|
#include <sys/socket.h>
|
|
|
|
#include <netinet/in.h>
|
|
|
|
#include <arpa/inet.h>
|
|
|
|
#include <netdb.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
2005-11-10 23:28:23 -05:00
|
|
|
|
2003-10-07 17:54:04 -04:00
|
|
|
#include "ares.h"
|
|
|
|
#include "ares_dns.h"
|
2005-11-10 23:28:23 -05:00
|
|
|
#include "inet_ntop.h"
|
|
|
|
#include "inet_net_pton.h"
|
2003-10-07 17:54:04 -04:00
|
|
|
|
|
|
|
#ifndef INADDR_NONE
|
2004-10-06 03:50:18 -04:00
|
|
|
#define INADDR_NONE 0xffffffff
|
2003-10-07 17:54:04 -04:00
|
|
|
#endif
|
|
|
|
|
2005-11-10 23:28:23 -05:00
|
|
|
#ifndef HAVE_STRUCT_IN6_ADDR
|
|
|
|
struct in6_addr
|
|
|
|
{
|
|
|
|
unsigned char s6_addr[16];
|
|
|
|
};
|
|
|
|
#endif
|
|
|
|
|
2003-10-07 17:54:04 -04:00
|
|
|
static void callback(void *arg, int status, struct hostent *host);
|
|
|
|
static void usage(void);
|
|
|
|
|
|
|
|
int main(int argc, char **argv)
|
|
|
|
{
|
|
|
|
ares_channel channel;
|
|
|
|
int status, nfds;
|
|
|
|
fd_set read_fds, write_fds;
|
|
|
|
struct timeval *tvp, tv;
|
2005-11-10 23:28:23 -05:00
|
|
|
struct in_addr addr4;
|
|
|
|
struct in6_addr addr6;
|
2003-10-07 17:54:04 -04:00
|
|
|
|
|
|
|
#ifdef WIN32
|
|
|
|
WORD wVersionRequested = MAKEWORD(1,1);
|
|
|
|
WSADATA wsaData;
|
|
|
|
WSAStartup(wVersionRequested, &wsaData);
|
2004-10-06 03:50:18 -04:00
|
|
|
#endif
|
2003-10-07 17:54:04 -04:00
|
|
|
|
2004-03-09 04:43:30 -05:00
|
|
|
if (argc <= 1)
|
2003-10-07 17:54:04 -04:00
|
|
|
usage();
|
|
|
|
|
|
|
|
status = ares_init(&channel);
|
|
|
|
if (status != ARES_SUCCESS)
|
|
|
|
{
|
2004-02-02 11:15:58 -05:00
|
|
|
fprintf(stderr, "ares_init: %s\n", ares_strerror(status));
|
2003-10-07 17:54:04 -04:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Initiate the queries, one per command-line argument. */
|
|
|
|
for (argv++; *argv; argv++)
|
|
|
|
{
|
2005-11-10 23:28:23 -05:00
|
|
|
if (ares_inet_pton(AF_INET, *argv, &addr4) == 1)
|
|
|
|
{
|
|
|
|
ares_gethostbyaddr(channel, &addr4, sizeof(addr4), AF_INET, callback,
|
|
|
|
*argv);
|
|
|
|
}
|
|
|
|
else if (ares_inet_pton(AF_INET6, *argv, &addr6) == 1)
|
2004-10-06 03:50:18 -04:00
|
|
|
{
|
2005-11-10 23:28:23 -05:00
|
|
|
ares_gethostbyaddr(channel, &addr6, sizeof(addr6), AF_INET6, callback,
|
2004-10-06 03:50:18 -04:00
|
|
|
*argv);
|
|
|
|
}
|
2005-11-10 23:28:23 -05:00
|
|
|
else
|
|
|
|
{
|
|
|
|
/* assume user wants A-records */
|
|
|
|
ares_gethostbyname(channel, *argv, AF_INET, callback, *argv);
|
|
|
|
}
|
2003-10-07 17:54:04 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Wait for all queries to complete. */
|
|
|
|
while (1)
|
|
|
|
{
|
|
|
|
FD_ZERO(&read_fds);
|
|
|
|
FD_ZERO(&write_fds);
|
|
|
|
nfds = ares_fds(channel, &read_fds, &write_fds);
|
|
|
|
if (nfds == 0)
|
2004-10-06 03:50:18 -04:00
|
|
|
break;
|
2003-10-07 17:54:04 -04:00
|
|
|
tvp = ares_timeout(channel, NULL, &tv);
|
|
|
|
select(nfds, &read_fds, &write_fds, NULL, tvp);
|
|
|
|
ares_process(channel, &read_fds, &write_fds);
|
|
|
|
}
|
|
|
|
|
|
|
|
ares_destroy(channel);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void callback(void *arg, int status, struct hostent *host)
|
|
|
|
{
|
2004-02-13 07:28:27 -05:00
|
|
|
char **p;
|
2003-10-07 17:54:04 -04:00
|
|
|
|
|
|
|
if (status != ARES_SUCCESS)
|
|
|
|
{
|
2004-02-02 11:15:58 -05:00
|
|
|
fprintf(stderr, "%s: %s\n", (char *) arg, ares_strerror(status));
|
2003-10-07 17:54:04 -04:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
for (p = host->h_addr_list; *p; p++)
|
|
|
|
{
|
2005-11-10 23:28:23 -05:00
|
|
|
char addr_buf[46] = "??";
|
|
|
|
|
|
|
|
ares_inet_ntop(host->h_addrtype, *p, addr_buf, sizeof(addr_buf));
|
|
|
|
printf("%-32s\t%s", host->h_name, addr_buf);
|
|
|
|
#if 0
|
|
|
|
if (host->h_aliases[0])
|
|
|
|
{
|
|
|
|
int i;
|
|
|
|
|
|
|
|
printf (", Aliases: ");
|
|
|
|
for (i = 0; host->h_aliases[i]; i++)
|
|
|
|
printf("%s ", host->h_aliases[i]);
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
puts("");
|
2003-10-07 17:54:04 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void usage(void)
|
|
|
|
{
|
|
|
|
fprintf(stderr, "usage: ahost {host|addr} ...\n");
|
|
|
|
exit(1);
|
|
|
|
}
|