ahost.c
Go to the documentation of this file.
1 /* Copyright 1998 by the Massachusetts Institute of Technology.
2  *
3  *
4  * Permission to use, copy, modify, and distribute this
5  * software and its documentation for any purpose and without
6  * fee is hereby granted, provided that the above copyright
7  * notice appear in all copies and that both that copyright
8  * notice and this permission notice appear in supporting
9  * documentation, and that the name of M.I.T. not be used in
10  * advertising or publicity pertaining to distribution of the
11  * software without specific, written prior permission.
12  * M.I.T. makes no representations about the suitability of
13  * this software for any purpose. It is provided "as is"
14  * without express or implied warranty.
15  */
16 
17 #include "ares_setup.h"
18 
19 #if !defined(WIN32) || defined(WATT32)
20 #include <netinet/in.h>
21 #include <arpa/inet.h>
22 #include <netdb.h>
23 #endif
24 
25 #ifdef HAVE_STRINGS_H
26 #include <strings.h>
27 #endif
28 
29 #include "ares.h"
30 #include "ares_dns.h"
31 #include "ares_getopt.h"
32 #include "ares_ipv6.h"
33 #include "ares_nowarn.h"
34 
35 #ifndef HAVE_STRDUP
36 # include "ares_strdup.h"
37 # define strdup(ptr) ares_strdup(ptr)
38 #endif
39 
40 #ifndef HAVE_STRCASECMP
41 # include "ares_strcasecmp.h"
42 # define strcasecmp(p1,p2) ares_strcasecmp(p1,p2)
43 #endif
44 
45 #ifndef HAVE_STRNCASECMP
46 # include "ares_strcasecmp.h"
47 # define strncasecmp(p1,p2,n) ares_strncasecmp(p1,p2,n)
48 #endif
49 
50 static void callback(void *arg, int status, int timeouts, struct hostent *host);
51 static void usage(void);
52 static void print_help_info_ahost(void);
53 
54 int main(int argc, char **argv)
55 {
56  struct ares_options options;
57  int optmask = 0;
59  int status, nfds, c, addr_family = AF_INET;
60  fd_set read_fds, write_fds;
61  struct timeval *tvp, tv;
62  struct in_addr addr4;
63  struct ares_in6_addr addr6;
64 
65 #ifdef USE_WINSOCK
66  WORD wVersionRequested = MAKEWORD(USE_WINSOCK,USE_WINSOCK);
67  WSADATA wsaData;
68  WSAStartup(wVersionRequested, &wsaData);
69 #endif
70 
71  memset(&options, 0, sizeof(options));
72 
74  if (status != ARES_SUCCESS)
75  {
76  fprintf(stderr, "ares_library_init: %s\n", ares_strerror(status));
77  return 1;
78  }
79 
80  while ((c = ares_getopt(argc,argv,"dt:h?s:")) != -1)
81  {
82  switch (c)
83  {
84  case 'd':
85 #ifdef WATT32
86  dbug_init();
87 #endif
88  break;
89  case 's':
90  optmask |= ARES_OPT_DOMAINS;
91  options.ndomains++;
92  options.domains = (char **)realloc(options.domains,
93  options.ndomains * sizeof(char *));
94  options.domains[options.ndomains - 1] = strdup(optarg);
95  break;
96  case 't':
97  if (!strcasecmp(optarg,"a"))
98  addr_family = AF_INET;
99  else if (!strcasecmp(optarg,"aaaa"))
100  addr_family = AF_INET6;
101  else if (!strcasecmp(optarg,"u"))
102  addr_family = AF_UNSPEC;
103  else
104  usage();
105  break;
106  case 'h':
108  break;
109  case '?':
111  break;
112  default:
113  usage();
114  break;
115  }
116  }
117 
118  argc -= optind;
119  argv += optind;
120  if (argc < 1)
121  usage();
122 
123  status = ares_init_options(&channel, &options, optmask);
124  if (status != ARES_SUCCESS)
125  {
126  fprintf(stderr, "ares_init: %s\n", ares_strerror(status));
127  return 1;
128  }
129 
130  /* Initiate the queries, one per command-line argument. */
131  for ( ; *argv; argv++)
132  {
133  if (ares_inet_pton(AF_INET, *argv, &addr4) == 1)
134  {
135  ares_gethostbyaddr(channel, &addr4, sizeof(addr4), AF_INET, callback,
136  *argv);
137  }
138  else if (ares_inet_pton(AF_INET6, *argv, &addr6) == 1)
139  {
141  *argv);
142  }
143  else
144  {
145  ares_gethostbyname(channel, *argv, addr_family, callback, *argv);
146  }
147  }
148 
149  /* Wait for all queries to complete. */
150  for (;;)
151  {
152  int res;
153  FD_ZERO(&read_fds);
154  FD_ZERO(&write_fds);
155  nfds = ares_fds(channel, &read_fds, &write_fds);
156  if (nfds == 0)
157  break;
158  tvp = ares_timeout(channel, NULL, &tv);
159  res = select(nfds, &read_fds, &write_fds, NULL, tvp);
160  if (-1 == res)
161  break;
162  ares_process(channel, &read_fds, &write_fds);
163  }
164 
166 
168 
169 #ifdef USE_WINSOCK
170  WSACleanup();
171 #endif
172 
173  return 0;
174 }
175 
176 static void callback(void *arg, int status, int timeouts, struct hostent *host)
177 {
178  char **p;
179 
180  (void)timeouts;
181 
182  if (status != ARES_SUCCESS)
183  {
184  fprintf(stderr, "%s: %s\n", (char *) arg, ares_strerror(status));
185  return;
186  }
187 
188  for (p = host->h_addr_list; *p; p++)
189  {
190  char addr_buf[46] = "??";
191 
192  ares_inet_ntop(host->h_addrtype, *p, addr_buf, sizeof(addr_buf));
193  printf("%-32s\t%s", host->h_name, addr_buf);
194 #if 0
195  if (host->h_aliases[0])
196  {
197  int i;
198 
199  printf (", Aliases: ");
200  for (i = 0; host->h_aliases[i]; i++)
201  printf("%s ", host->h_aliases[i]);
202  }
203 #endif
204  puts("");
205  }
206 }
207 
208 static void usage(void)
209 {
210  fprintf(stderr, "usage: ahost [-h] [-d] [-s {domain}] [-t {a|aaaa|u}] {host|addr} ...\n");
211  exit(1);
212 }
213 
214 /* Information from the man page. Formatting taken from man -h */
215 static void print_help_info_ahost(void) {
216  printf("ahost, version %s \n\n", ARES_VERSION_STR);
217  printf("usage: ahost [-h] [-d] [-s {domain}] [-t {a|aaaa|u}] {host|addr} ...\n\n"
218  " d : Print some extra debugging output.\n"
219  " h : Display this help and exit.\n\n"
220  " s domain : Specify the domain to search instead of \n"
221  " using the default values from \n"
222  " /etc/resolv.conf. This option only has an \n"
223  " effect on platforms that use /etc/resolv.conf\n"
224  " for DNS configuration; it has no effect on other\n"
225  " platforms (such as Win32 or Android).\n"
226  " t type : If type is \"a\", print the A record (default).\n"
227  " If type is \"aaaa\", print the AAAA record. If\n"
228  " type is \"u\", look for either AAAA or A record\n"
229  " (in that order).\n\n");
230  exit(0);
231 }
optarg
#define optarg
Definition: ares_getopt.h:42
ares_inet_ntop
const CARES_EXTERN char * ares_inet_ntop(int af, const void *src, char *dst, ares_socklen_t size)
Definition: inet_ntop.c:56
ares_inet_pton
CARES_EXTERN int ares_inet_pton(int af, const char *src, void *dst)
Definition: inet_net_pton.c:418
AF_INET6
#define AF_INET6
Definition: ares_setup.h:208
ares_options
Definition: ares.h:259
ares_process
CARES_EXTERN void ares_process(ares_channel channel, fd_set *read_fds, fd_set *write_fds)
Definition: ares_process.c:134
ares_library_init
CARES_EXTERN int ares_library_init(int flags)
Definition: ares_library_init.c:133
memset
return memset(p, 0, total)
ares.h
options
double_dict options[]
Definition: capstone_test.c:55
printf
_Use_decl_annotations_ int __cdecl printf(const char *_Format,...)
Definition: cs_driver.c:91
print_help_info_ahost
static void print_help_info_ahost(void)
Definition: ahost.c:215
ares_fds
CARES_EXTERN int ares_fds(ares_channel channel, fd_set *read_fds, fd_set *write_fds)
Definition: ares_fds.c:23
ares_dns.h
status
absl::Status status
Definition: rls.cc:251
ARES_LIB_INIT_ALL
#define ARES_LIB_INIT_ALL
Definition: ares.h:217
ares_strdup.h
ARES_OPT_DOMAINS
#define ARES_OPT_DOMAINS
Definition: ares.h:160
xds_manager.p
p
Definition: xds_manager.py:60
ares_getopt.h
python_utils.port_server.stderr
stderr
Definition: port_server.py:51
usage
static void usage(void)
Definition: ahost.c:208
c
void c(T a)
Definition: miscompile_with_no_unique_address_test.cc:40
channel
wrapped_grpc_channel * channel
Definition: src/php/ext/grpc/call.h:33
main
int main(int argc, char **argv)
Definition: ahost.c:54
ares_strcasecmp.h
optind
#define optind
Definition: ares_getopt.h:43
strdup
#define strdup(ptr)
Definition: ahost.c:37
addr6
static struct sockaddr_in6 addr6
Definition: test-getnameinfo.c:34
arg
Definition: cmdline.cc:40
strcasecmp
#define strcasecmp(p1, p2)
Definition: ahost.c:42
ARES_SUCCESS
#define ARES_SUCCESS
Definition: ares.h:98
ares_strerror
const CARES_EXTERN char * ares_strerror(int code)
Definition: ares_strerror.c:21
ares_setup.h
ARES_VERSION_STR
#define ARES_VERSION_STR
Definition: ares_version.h:14
addr4
static struct sockaddr_in addr4
Definition: test-getnameinfo.c:33
ares_channeldata
Definition: ares_private.h:266
ares_ipv6.h
ares_getopt
int ares_getopt(int nargc, char *const nargv[], const char *ostr)
Definition: ares_getopt.c:66
ares_in6_addr
Definition: ares.h:514
timeval
Definition: setup_once.h:113
ares_library_cleanup
CARES_EXTERN void ares_library_cleanup(void)
Definition: ares_library_init.c:171
ares_destroy
CARES_EXTERN void ares_destroy(ares_channel channel)
Definition: ares_destroy.c:43
ares_timeout
CARES_EXTERN struct timeval * ares_timeout(ares_channel channel, struct timeval *maxtv, struct timeval *tv)
Definition: ares_timeout.c:38
callback
static void callback(void *arg, int status, int timeouts, struct hostent *host)
Definition: ahost.c:176
ares_gethostbyaddr
CARES_EXTERN void ares_gethostbyaddr(ares_channel channel, const void *addr, int addrlen, int family, ares_host_callback callback, void *arg)
Definition: ares_gethostbyaddr.c:58
ares_init_options
CARES_EXTERN int ares_init_options(ares_channel *channelptr, struct ares_options *options, int optmask)
Definition: ares_init.c:103
ares_nowarn.h
ares_gethostbyname
CARES_EXTERN void ares_gethostbyname(ares_channel channel, const char *name, int family, ares_host_callback callback, void *arg)
Definition: ares_gethostbyname.c:75
i
uint64_t i
Definition: abseil-cpp/absl/container/btree_benchmark.cc:230


grpc
Author(s):
autogenerated on Fri May 16 2025 02:57:40