2003-10-07 23:54:04 +02:00
|
|
|
/* Copyright 1998 by the Massachusetts Institute of Technology.
|
2006-07-22 19:31:00 +02:00
|
|
|
*
|
|
|
|
* $Id$
|
2003-10-07 23:54:04 +02:00
|
|
|
*
|
|
|
|
* 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.
|
|
|
|
*/
|
|
|
|
|
2009-11-02 12:55:53 +01:00
|
|
|
#include "ares_setup.h"
|
2003-10-07 23:54:04 +02:00
|
|
|
|
2006-10-31 18:51:54 +01:00
|
|
|
#if !defined(WIN32) || defined(WATT32)
|
2007-02-06 19:56:34 +01:00
|
|
|
#ifdef HAVE_SYS_TIME_H
|
2003-10-07 23:54:04 +02:00
|
|
|
#include <sys/time.h>
|
2007-02-06 19:56:34 +01:00
|
|
|
#endif
|
2003-10-07 23:54:04 +02:00
|
|
|
#include <sys/socket.h>
|
|
|
|
#include <netinet/in.h>
|
|
|
|
#include <arpa/inet.h>
|
|
|
|
#include <netdb.h>
|
2007-02-06 19:56:34 +01:00
|
|
|
#ifdef HAVE_UNISTD_H
|
2003-10-07 23:54:04 +02:00
|
|
|
#include <unistd.h>
|
|
|
|
#endif
|
2007-02-06 19:56:34 +01:00
|
|
|
#endif
|
2008-05-09 00:14:17 +02:00
|
|
|
#ifdef HAVE_STRINGS_H
|
|
|
|
#include <strings.h>
|
|
|
|
#endif
|
2003-10-07 23:54:04 +02:00
|
|
|
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
2005-11-11 05:28:23 +01:00
|
|
|
|
2003-10-07 23:54:04 +02:00
|
|
|
#include "ares.h"
|
|
|
|
#include "ares_dns.h"
|
2005-11-11 05:28:23 +01:00
|
|
|
#include "inet_ntop.h"
|
|
|
|
#include "inet_net_pton.h"
|
2007-04-16 11:01:16 +02:00
|
|
|
#include "ares_getopt.h"
|
2009-04-28 18:47:33 +02:00
|
|
|
#include "ares_ipv6.h"
|
2003-10-07 23:54:04 +02:00
|
|
|
|
2008-09-15 17:28:26 +02:00
|
|
|
#ifndef HAVE_STRDUP
|
|
|
|
# include "ares_strdup.h"
|
|
|
|
# define strdup(ptr) ares_strdup(ptr)
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifndef HAVE_STRCASECMP
|
|
|
|
# include "ares_strcasecmp.h"
|
|
|
|
# define strcasecmp(p1,p2) ares_strcasecmp(p1,p2)
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifndef HAVE_STRNCASECMP
|
|
|
|
# include "ares_strcasecmp.h"
|
|
|
|
# define strncasecmp(p1,p2,n) ares_strncasecmp(p1,p2,n)
|
|
|
|
#endif
|
|
|
|
|
2007-09-28 16:46:51 +02:00
|
|
|
static void callback(void *arg, int status, int timeouts, struct hostent *host);
|
2003-10-07 23:54:04 +02:00
|
|
|
static void usage(void);
|
|
|
|
|
|
|
|
int main(int argc, char **argv)
|
|
|
|
{
|
|
|
|
ares_channel channel;
|
2006-07-22 19:31:00 +02:00
|
|
|
int status, nfds, c, addr_family = AF_INET;
|
2003-10-07 23:54:04 +02:00
|
|
|
fd_set read_fds, write_fds;
|
|
|
|
struct timeval *tvp, tv;
|
2005-11-11 05:28:23 +01:00
|
|
|
struct in_addr addr4;
|
|
|
|
struct in6_addr addr6;
|
2003-10-07 23:54:04 +02:00
|
|
|
|
2006-10-18 23:05:40 +02:00
|
|
|
#ifdef USE_WINSOCK
|
|
|
|
WORD wVersionRequested = MAKEWORD(USE_WINSOCK,USE_WINSOCK);
|
2003-10-07 23:54:04 +02:00
|
|
|
WSADATA wsaData;
|
|
|
|
WSAStartup(wVersionRequested, &wsaData);
|
2004-10-06 09:50:18 +02:00
|
|
|
#endif
|
2003-10-07 23:54:04 +02:00
|
|
|
|
2009-05-17 19:11:28 +02:00
|
|
|
status = ares_library_init(ARES_LIB_INIT_ALL);
|
|
|
|
if (status != ARES_SUCCESS)
|
|
|
|
{
|
|
|
|
fprintf(stderr, "ares_library_init: %s\n", ares_strerror(status));
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2007-04-12 21:01:19 +02:00
|
|
|
while ((c = ares_getopt(argc,argv,"dt:h")) != -1)
|
2006-07-22 19:31:00 +02:00
|
|
|
{
|
|
|
|
switch (c)
|
|
|
|
{
|
2007-02-06 20:12:38 +01:00
|
|
|
case 'd':
|
|
|
|
#ifdef WATT32
|
|
|
|
dbug_init();
|
|
|
|
#endif
|
|
|
|
break;
|
2006-07-22 19:31:00 +02:00
|
|
|
case 't':
|
|
|
|
if (!strcasecmp(optarg,"a"))
|
|
|
|
addr_family = AF_INET;
|
|
|
|
else if (!strcasecmp(optarg,"aaaa"))
|
|
|
|
addr_family = AF_INET6;
|
|
|
|
else
|
|
|
|
usage();
|
|
|
|
break;
|
|
|
|
case 'h':
|
|
|
|
default:
|
|
|
|
usage();
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
argc -= optind;
|
|
|
|
argv += optind;
|
|
|
|
if (argc < 1)
|
2003-10-07 23:54:04 +02:00
|
|
|
usage();
|
|
|
|
|
|
|
|
status = ares_init(&channel);
|
|
|
|
if (status != ARES_SUCCESS)
|
|
|
|
{
|
2004-02-02 17:15:58 +01:00
|
|
|
fprintf(stderr, "ares_init: %s\n", ares_strerror(status));
|
2003-10-07 23:54:04 +02:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Initiate the queries, one per command-line argument. */
|
2006-07-22 19:31:00 +02:00
|
|
|
for ( ; *argv; argv++)
|
2003-10-07 23:54:04 +02:00
|
|
|
{
|
2005-11-11 05:28:23 +01: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 09:50:18 +02:00
|
|
|
{
|
2005-11-11 05:28:23 +01:00
|
|
|
ares_gethostbyaddr(channel, &addr6, sizeof(addr6), AF_INET6, callback,
|
2004-10-06 09:50:18 +02:00
|
|
|
*argv);
|
|
|
|
}
|
2005-11-11 05:28:23 +01:00
|
|
|
else
|
|
|
|
{
|
2006-07-22 19:31:00 +02:00
|
|
|
ares_gethostbyname(channel, *argv, addr_family, callback, *argv);
|
2005-11-11 05:28:23 +01:00
|
|
|
}
|
2003-10-07 23:54:04 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Wait for all queries to complete. */
|
2009-11-10 19:41:03 +01:00
|
|
|
for (;;)
|
2003-10-07 23:54:04 +02:00
|
|
|
{
|
|
|
|
FD_ZERO(&read_fds);
|
|
|
|
FD_ZERO(&write_fds);
|
|
|
|
nfds = ares_fds(channel, &read_fds, &write_fds);
|
|
|
|
if (nfds == 0)
|
2004-10-06 09:50:18 +02:00
|
|
|
break;
|
2003-10-07 23:54:04 +02: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);
|
2007-02-21 16:01:51 +01:00
|
|
|
|
2009-05-17 19:11:28 +02:00
|
|
|
ares_library_cleanup();
|
|
|
|
|
2007-02-21 16:01:51 +01:00
|
|
|
#ifdef USE_WINSOCK
|
|
|
|
WSACleanup();
|
|
|
|
#endif
|
|
|
|
|
2003-10-07 23:54:04 +02:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2007-09-28 16:46:51 +02:00
|
|
|
static void callback(void *arg, int status, int timeouts, struct hostent *host)
|
2003-10-07 23:54:04 +02:00
|
|
|
{
|
2004-02-13 13:28:27 +01:00
|
|
|
char **p;
|
2003-10-07 23:54:04 +02:00
|
|
|
|
2008-05-26 15:52:25 +02:00
|
|
|
(void)timeouts;
|
|
|
|
|
2003-10-07 23:54:04 +02:00
|
|
|
if (status != ARES_SUCCESS)
|
|
|
|
{
|
2004-02-02 17:15:58 +01:00
|
|
|
fprintf(stderr, "%s: %s\n", (char *) arg, ares_strerror(status));
|
2003-10-07 23:54:04 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
for (p = host->h_addr_list; *p; p++)
|
|
|
|
{
|
2005-11-11 05:28:23 +01: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;
|
|
|
|
|
2006-07-22 19:31:00 +02:00
|
|
|
printf (", Aliases: ");
|
|
|
|
for (i = 0; host->h_aliases[i]; i++)
|
2005-11-11 05:28:23 +01:00
|
|
|
printf("%s ", host->h_aliases[i]);
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
puts("");
|
2003-10-07 23:54:04 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void usage(void)
|
|
|
|
{
|
2006-07-22 19:31:00 +02:00
|
|
|
fprintf(stderr, "usage: ahost [-t {a|aaaa}] {host|addr} ...\n");
|
2003-10-07 23:54:04 +02:00
|
|
|
exit(1);
|
|
|
|
}
|