1968bf19396ad404e89420f5d67900fce13f4186cGilad Arnold/* Copyright 1998 by the Massachusetts Institute of Technology. 2968bf19396ad404e89420f5d67900fce13f4186cGilad Arnold * 3968bf19396ad404e89420f5d67900fce13f4186cGilad Arnold * 4968bf19396ad404e89420f5d67900fce13f4186cGilad Arnold * Permission to use, copy, modify, and distribute this 5968bf19396ad404e89420f5d67900fce13f4186cGilad Arnold * software and its documentation for any purpose and without 6968bf19396ad404e89420f5d67900fce13f4186cGilad Arnold * fee is hereby granted, provided that the above copyright 7968bf19396ad404e89420f5d67900fce13f4186cGilad Arnold * notice appear in all copies and that both that copyright 8968bf19396ad404e89420f5d67900fce13f4186cGilad Arnold * notice and this permission notice appear in supporting 9968bf19396ad404e89420f5d67900fce13f4186cGilad Arnold * documentation, and that the name of M.I.T. not be used in 10968bf19396ad404e89420f5d67900fce13f4186cGilad Arnold * advertising or publicity pertaining to distribution of the 11968bf19396ad404e89420f5d67900fce13f4186cGilad Arnold * software without specific, written prior permission. 12968bf19396ad404e89420f5d67900fce13f4186cGilad Arnold * M.I.T. makes no representations about the suitability of 13968bf19396ad404e89420f5d67900fce13f4186cGilad Arnold * this software for any purpose. It is provided "as is" 14968bf19396ad404e89420f5d67900fce13f4186cGilad Arnold * without express or implied warranty. 15968bf19396ad404e89420f5d67900fce13f4186cGilad Arnold */ 16968bf19396ad404e89420f5d67900fce13f4186cGilad Arnold 17968bf19396ad404e89420f5d67900fce13f4186cGilad Arnold#include "ares_setup.h" 18968bf19396ad404e89420f5d67900fce13f4186cGilad Arnold 19968bf19396ad404e89420f5d67900fce13f4186cGilad Arnold#if !defined(WIN32) || defined(WATT32) 20968bf19396ad404e89420f5d67900fce13f4186cGilad Arnold#ifdef HAVE_SYS_TIME_H 21968bf19396ad404e89420f5d67900fce13f4186cGilad Arnold#include <sys/time.h> 22968bf19396ad404e89420f5d67900fce13f4186cGilad Arnold#endif 23968bf19396ad404e89420f5d67900fce13f4186cGilad Arnold#include <sys/socket.h> 24968bf19396ad404e89420f5d67900fce13f4186cGilad Arnold#include <netinet/in.h> 25968bf19396ad404e89420f5d67900fce13f4186cGilad Arnold#include <arpa/inet.h> 26968bf19396ad404e89420f5d67900fce13f4186cGilad Arnold#include <netdb.h> 27968bf19396ad404e89420f5d67900fce13f4186cGilad Arnold#ifdef HAVE_UNISTD_H 28968bf19396ad404e89420f5d67900fce13f4186cGilad Arnold#include <unistd.h> 29968bf19396ad404e89420f5d67900fce13f4186cGilad Arnold#endif 30968bf19396ad404e89420f5d67900fce13f4186cGilad Arnold#endif 31968bf19396ad404e89420f5d67900fce13f4186cGilad Arnold#ifdef HAVE_STRINGS_H 32968bf19396ad404e89420f5d67900fce13f4186cGilad Arnold#include <strings.h> 33968bf19396ad404e89420f5d67900fce13f4186cGilad Arnold#endif 34968bf19396ad404e89420f5d67900fce13f4186cGilad Arnold 35968bf19396ad404e89420f5d67900fce13f4186cGilad Arnold#include <stdio.h> 36968bf19396ad404e89420f5d67900fce13f4186cGilad Arnold#include <stdlib.h> 37968bf19396ad404e89420f5d67900fce13f4186cGilad Arnold#include <string.h> 38968bf19396ad404e89420f5d67900fce13f4186cGilad Arnold 39968bf19396ad404e89420f5d67900fce13f4186cGilad Arnold#include "ares.h" 40968bf19396ad404e89420f5d67900fce13f4186cGilad Arnold#include "ares_dns.h" 41968bf19396ad404e89420f5d67900fce13f4186cGilad Arnold#include "inet_ntop.h" 42968bf19396ad404e89420f5d67900fce13f4186cGilad Arnold#include "inet_net_pton.h" 43968bf19396ad404e89420f5d67900fce13f4186cGilad Arnold#include "ares_getopt.h" 44968bf19396ad404e89420f5d67900fce13f4186cGilad Arnold#include "ares_ipv6.h" 45968bf19396ad404e89420f5d67900fce13f4186cGilad Arnold#include "ares_nowarn.h" 46968bf19396ad404e89420f5d67900fce13f4186cGilad Arnold 47968bf19396ad404e89420f5d67900fce13f4186cGilad Arnold#ifndef HAVE_STRDUP 48968bf19396ad404e89420f5d67900fce13f4186cGilad Arnold# include "ares_strdup.h" 49968bf19396ad404e89420f5d67900fce13f4186cGilad Arnold# define strdup(ptr) ares_strdup(ptr) 50968bf19396ad404e89420f5d67900fce13f4186cGilad Arnold#endif 51968bf19396ad404e89420f5d67900fce13f4186cGilad Arnold 52968bf19396ad404e89420f5d67900fce13f4186cGilad Arnold#ifndef HAVE_STRCASECMP 53968bf19396ad404e89420f5d67900fce13f4186cGilad Arnold# include "ares_strcasecmp.h" 54968bf19396ad404e89420f5d67900fce13f4186cGilad Arnold# define strcasecmp(p1,p2) ares_strcasecmp(p1,p2) 55968bf19396ad404e89420f5d67900fce13f4186cGilad Arnold#endif 56968bf19396ad404e89420f5d67900fce13f4186cGilad Arnold 57968bf19396ad404e89420f5d67900fce13f4186cGilad Arnold#ifndef HAVE_STRNCASECMP 58968bf19396ad404e89420f5d67900fce13f4186cGilad Arnold# include "ares_strcasecmp.h" 59968bf19396ad404e89420f5d67900fce13f4186cGilad Arnold# define strncasecmp(p1,p2,n) ares_strncasecmp(p1,p2,n) 60968bf19396ad404e89420f5d67900fce13f4186cGilad Arnold#endif 61968bf19396ad404e89420f5d67900fce13f4186cGilad Arnold 62968bf19396ad404e89420f5d67900fce13f4186cGilad Arnoldstatic void callback(void *arg, int status, int timeouts, struct hostent *host); 63968bf19396ad404e89420f5d67900fce13f4186cGilad Arnoldstatic void usage(void); 64968bf19396ad404e89420f5d67900fce13f4186cGilad Arnold 65968bf19396ad404e89420f5d67900fce13f4186cGilad Arnoldint main(int argc, char **argv) 66968bf19396ad404e89420f5d67900fce13f4186cGilad Arnold{ 67968bf19396ad404e89420f5d67900fce13f4186cGilad Arnold ares_channel channel; 68968bf19396ad404e89420f5d67900fce13f4186cGilad Arnold int status, nfds, c, addr_family = AF_INET; 69968bf19396ad404e89420f5d67900fce13f4186cGilad Arnold fd_set read_fds, write_fds; 70968bf19396ad404e89420f5d67900fce13f4186cGilad Arnold struct timeval *tvp, tv; 71968bf19396ad404e89420f5d67900fce13f4186cGilad Arnold struct in_addr addr4; 72968bf19396ad404e89420f5d67900fce13f4186cGilad Arnold struct ares_in6_addr addr6; 73968bf19396ad404e89420f5d67900fce13f4186cGilad Arnold 74968bf19396ad404e89420f5d67900fce13f4186cGilad Arnold#ifdef USE_WINSOCK 75968bf19396ad404e89420f5d67900fce13f4186cGilad Arnold WORD wVersionRequested = MAKEWORD(USE_WINSOCK,USE_WINSOCK); 76968bf19396ad404e89420f5d67900fce13f4186cGilad Arnold WSADATA wsaData; 77968bf19396ad404e89420f5d67900fce13f4186cGilad Arnold WSAStartup(wVersionRequested, &wsaData); 78968bf19396ad404e89420f5d67900fce13f4186cGilad Arnold#endif 79968bf19396ad404e89420f5d67900fce13f4186cGilad Arnold 80968bf19396ad404e89420f5d67900fce13f4186cGilad Arnold status = ares_library_init(ARES_LIB_INIT_ALL); 81968bf19396ad404e89420f5d67900fce13f4186cGilad Arnold if (status != ARES_SUCCESS) 82968bf19396ad404e89420f5d67900fce13f4186cGilad Arnold { 83968bf19396ad404e89420f5d67900fce13f4186cGilad Arnold fprintf(stderr, "ares_library_init: %s\n", ares_strerror(status)); 84968bf19396ad404e89420f5d67900fce13f4186cGilad Arnold return 1; 85968bf19396ad404e89420f5d67900fce13f4186cGilad Arnold } 86968bf19396ad404e89420f5d67900fce13f4186cGilad Arnold 87968bf19396ad404e89420f5d67900fce13f4186cGilad Arnold while ((c = ares_getopt(argc,argv,"dt:h")) != -1) 88968bf19396ad404e89420f5d67900fce13f4186cGilad Arnold { 89968bf19396ad404e89420f5d67900fce13f4186cGilad Arnold switch (c) 90968bf19396ad404e89420f5d67900fce13f4186cGilad Arnold { 91968bf19396ad404e89420f5d67900fce13f4186cGilad Arnold case 'd': 92968bf19396ad404e89420f5d67900fce13f4186cGilad Arnold#ifdef WATT32 93968bf19396ad404e89420f5d67900fce13f4186cGilad Arnold dbug_init(); 94968bf19396ad404e89420f5d67900fce13f4186cGilad Arnold#endif 95968bf19396ad404e89420f5d67900fce13f4186cGilad Arnold break; 96968bf19396ad404e89420f5d67900fce13f4186cGilad Arnold case 't': 97968bf19396ad404e89420f5d67900fce13f4186cGilad Arnold if (!strcasecmp(optarg,"a")) 98968bf19396ad404e89420f5d67900fce13f4186cGilad Arnold addr_family = AF_INET; 99968bf19396ad404e89420f5d67900fce13f4186cGilad Arnold else if (!strcasecmp(optarg,"aaaa")) 100968bf19396ad404e89420f5d67900fce13f4186cGilad Arnold addr_family = AF_INET6; 101968bf19396ad404e89420f5d67900fce13f4186cGilad Arnold else 102968bf19396ad404e89420f5d67900fce13f4186cGilad Arnold usage(); 103968bf19396ad404e89420f5d67900fce13f4186cGilad Arnold break; 104968bf19396ad404e89420f5d67900fce13f4186cGilad Arnold case 'h': 105968bf19396ad404e89420f5d67900fce13f4186cGilad Arnold default: 106968bf19396ad404e89420f5d67900fce13f4186cGilad Arnold usage(); 107968bf19396ad404e89420f5d67900fce13f4186cGilad Arnold break; 108968bf19396ad404e89420f5d67900fce13f4186cGilad Arnold } 109968bf19396ad404e89420f5d67900fce13f4186cGilad Arnold } 110968bf19396ad404e89420f5d67900fce13f4186cGilad Arnold 111968bf19396ad404e89420f5d67900fce13f4186cGilad Arnold argc -= optind; 112968bf19396ad404e89420f5d67900fce13f4186cGilad Arnold argv += optind; 113968bf19396ad404e89420f5d67900fce13f4186cGilad Arnold if (argc < 1) 114968bf19396ad404e89420f5d67900fce13f4186cGilad Arnold usage(); 115968bf19396ad404e89420f5d67900fce13f4186cGilad Arnold 116968bf19396ad404e89420f5d67900fce13f4186cGilad Arnold status = ares_init(&channel); 117968bf19396ad404e89420f5d67900fce13f4186cGilad Arnold if (status != ARES_SUCCESS) 118968bf19396ad404e89420f5d67900fce13f4186cGilad Arnold { 119968bf19396ad404e89420f5d67900fce13f4186cGilad Arnold fprintf(stderr, "ares_init: %s\n", ares_strerror(status)); 120968bf19396ad404e89420f5d67900fce13f4186cGilad Arnold return 1; 121968bf19396ad404e89420f5d67900fce13f4186cGilad Arnold } 122968bf19396ad404e89420f5d67900fce13f4186cGilad Arnold 123968bf19396ad404e89420f5d67900fce13f4186cGilad Arnold /* Initiate the queries, one per command-line argument. */ 124968bf19396ad404e89420f5d67900fce13f4186cGilad Arnold for ( ; *argv; argv++) 125968bf19396ad404e89420f5d67900fce13f4186cGilad Arnold { 126968bf19396ad404e89420f5d67900fce13f4186cGilad Arnold if (ares_inet_pton(AF_INET, *argv, &addr4) == 1) 127968bf19396ad404e89420f5d67900fce13f4186cGilad Arnold { 128968bf19396ad404e89420f5d67900fce13f4186cGilad Arnold ares_gethostbyaddr(channel, &addr4, sizeof(addr4), AF_INET, callback, 129968bf19396ad404e89420f5d67900fce13f4186cGilad Arnold *argv); 130968bf19396ad404e89420f5d67900fce13f4186cGilad Arnold } 131968bf19396ad404e89420f5d67900fce13f4186cGilad Arnold else if (ares_inet_pton(AF_INET6, *argv, &addr6) == 1) 132968bf19396ad404e89420f5d67900fce13f4186cGilad Arnold { 133968bf19396ad404e89420f5d67900fce13f4186cGilad Arnold ares_gethostbyaddr(channel, &addr6, sizeof(addr6), AF_INET6, callback, 134968bf19396ad404e89420f5d67900fce13f4186cGilad Arnold *argv); 135968bf19396ad404e89420f5d67900fce13f4186cGilad Arnold } 136968bf19396ad404e89420f5d67900fce13f4186cGilad Arnold else 137968bf19396ad404e89420f5d67900fce13f4186cGilad Arnold { 138968bf19396ad404e89420f5d67900fce13f4186cGilad Arnold ares_gethostbyname(channel, *argv, addr_family, callback, *argv); 139968bf19396ad404e89420f5d67900fce13f4186cGilad Arnold } 140968bf19396ad404e89420f5d67900fce13f4186cGilad Arnold } 141968bf19396ad404e89420f5d67900fce13f4186cGilad Arnold 142968bf19396ad404e89420f5d67900fce13f4186cGilad Arnold /* Wait for all queries to complete. */ 143968bf19396ad404e89420f5d67900fce13f4186cGilad Arnold for (;;) 144968bf19396ad404e89420f5d67900fce13f4186cGilad Arnold { 145968bf19396ad404e89420f5d67900fce13f4186cGilad Arnold FD_ZERO(&read_fds); 146968bf19396ad404e89420f5d67900fce13f4186cGilad Arnold FD_ZERO(&write_fds); 147968bf19396ad404e89420f5d67900fce13f4186cGilad Arnold nfds = ares_fds(channel, &read_fds, &write_fds); 148968bf19396ad404e89420f5d67900fce13f4186cGilad Arnold if (nfds == 0) 149968bf19396ad404e89420f5d67900fce13f4186cGilad Arnold break; 150968bf19396ad404e89420f5d67900fce13f4186cGilad Arnold tvp = ares_timeout(channel, NULL, &tv); 151968bf19396ad404e89420f5d67900fce13f4186cGilad Arnold select(nfds, &read_fds, &write_fds, NULL, tvp); 152968bf19396ad404e89420f5d67900fce13f4186cGilad Arnold ares_process(channel, &read_fds, &write_fds); 153968bf19396ad404e89420f5d67900fce13f4186cGilad Arnold } 154968bf19396ad404e89420f5d67900fce13f4186cGilad Arnold 155968bf19396ad404e89420f5d67900fce13f4186cGilad Arnold ares_destroy(channel); 156968bf19396ad404e89420f5d67900fce13f4186cGilad Arnold 157968bf19396ad404e89420f5d67900fce13f4186cGilad Arnold ares_library_cleanup(); 158968bf19396ad404e89420f5d67900fce13f4186cGilad Arnold 159968bf19396ad404e89420f5d67900fce13f4186cGilad Arnold#ifdef USE_WINSOCK 160968bf19396ad404e89420f5d67900fce13f4186cGilad Arnold WSACleanup(); 161968bf19396ad404e89420f5d67900fce13f4186cGilad Arnold#endif 162968bf19396ad404e89420f5d67900fce13f4186cGilad Arnold 163968bf19396ad404e89420f5d67900fce13f4186cGilad Arnold return 0; 164968bf19396ad404e89420f5d67900fce13f4186cGilad Arnold} 165968bf19396ad404e89420f5d67900fce13f4186cGilad Arnold 166968bf19396ad404e89420f5d67900fce13f4186cGilad Arnoldstatic void callback(void *arg, int status, int timeouts, struct hostent *host) 167968bf19396ad404e89420f5d67900fce13f4186cGilad Arnold{ 168968bf19396ad404e89420f5d67900fce13f4186cGilad Arnold char **p; 169968bf19396ad404e89420f5d67900fce13f4186cGilad Arnold 170968bf19396ad404e89420f5d67900fce13f4186cGilad Arnold (void)timeouts; 171968bf19396ad404e89420f5d67900fce13f4186cGilad Arnold 172968bf19396ad404e89420f5d67900fce13f4186cGilad Arnold if (status != ARES_SUCCESS) 173968bf19396ad404e89420f5d67900fce13f4186cGilad Arnold { 174968bf19396ad404e89420f5d67900fce13f4186cGilad Arnold fprintf(stderr, "%s: %s\n", (char *) arg, ares_strerror(status)); 175968bf19396ad404e89420f5d67900fce13f4186cGilad Arnold return; 176968bf19396ad404e89420f5d67900fce13f4186cGilad Arnold } 177968bf19396ad404e89420f5d67900fce13f4186cGilad Arnold 178968bf19396ad404e89420f5d67900fce13f4186cGilad Arnold for (p = host->h_addr_list; *p; p++) 179968bf19396ad404e89420f5d67900fce13f4186cGilad Arnold { 180968bf19396ad404e89420f5d67900fce13f4186cGilad Arnold char addr_buf[46] = "??"; 181968bf19396ad404e89420f5d67900fce13f4186cGilad Arnold 182968bf19396ad404e89420f5d67900fce13f4186cGilad Arnold ares_inet_ntop(host->h_addrtype, *p, addr_buf, sizeof(addr_buf)); 183968bf19396ad404e89420f5d67900fce13f4186cGilad Arnold printf("%-32s\t%s", host->h_name, addr_buf); 184968bf19396ad404e89420f5d67900fce13f4186cGilad Arnold#if 0 185968bf19396ad404e89420f5d67900fce13f4186cGilad Arnold if (host->h_aliases[0]) 186968bf19396ad404e89420f5d67900fce13f4186cGilad Arnold { 187968bf19396ad404e89420f5d67900fce13f4186cGilad Arnold int i; 188968bf19396ad404e89420f5d67900fce13f4186cGilad Arnold 189968bf19396ad404e89420f5d67900fce13f4186cGilad Arnold printf (", Aliases: "); 190968bf19396ad404e89420f5d67900fce13f4186cGilad Arnold for (i = 0; host->h_aliases[i]; i++) 191968bf19396ad404e89420f5d67900fce13f4186cGilad Arnold printf("%s ", host->h_aliases[i]); 192968bf19396ad404e89420f5d67900fce13f4186cGilad Arnold } 193968bf19396ad404e89420f5d67900fce13f4186cGilad Arnold#endif 194968bf19396ad404e89420f5d67900fce13f4186cGilad Arnold puts(""); 195968bf19396ad404e89420f5d67900fce13f4186cGilad Arnold } 196968bf19396ad404e89420f5d67900fce13f4186cGilad Arnold} 197968bf19396ad404e89420f5d67900fce13f4186cGilad Arnold 198968bf19396ad404e89420f5d67900fce13f4186cGilad Arnoldstatic void usage(void) 199968bf19396ad404e89420f5d67900fce13f4186cGilad Arnold{ 200968bf19396ad404e89420f5d67900fce13f4186cGilad Arnold fprintf(stderr, "usage: ahost [-t {a|aaaa}] {host|addr} ...\n"); 201968bf19396ad404e89420f5d67900fce13f4186cGilad Arnold exit(1); 202968bf19396ad404e89420f5d67900fce13f4186cGilad Arnold} 203