reSIProcate
SIP stack, Dialog Usage Manager and related libraries
Loading...
Searching...
No Matches
ahost.c
Go to the documentation of this file.
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
16#ifndef WIN32
17#include <sys/types.h>
18#include <sys/time.h>
19#include <sys/socket.h>
20#include <netinet/in.h>
21#include <arpa/inet.h>
22#include <unistd.h>
23#include <netdb.h>
24#endif
25#include <stdio.h>
26#include <stdlib.h>
27#include <string.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;
43 struct timeval *tvp, tv;
44 struct in_addr addr;
45
46 if (argc == 0)
47 usage();
48
49 status = ares_init(&channel);
50 if (status != ARES_SUCCESS)
51 {
52 fprintf(stderr, "ares_init: %s\n", ares_strerror(status));
53 return 1;
54 }
55
56 /* Initiate the queries, one per command-line argument. */
57 for (argv++; *argv; argv++)
58 {
59 if (inet_pton(AF_INET, *argv, &addr.s_addr) == 1)
60 {
61 ares_gethostbyaddr(channel, &addr, sizeof(addr), AF_INET, callback, *argv);
62 }
63 else
64 {
66 }
67 }
68
69 /* Wait for all queries to complete. */
70 while (1)
71 {
74 nfds = ares_fds(channel, &read_fds, &write_fds);
75 if (nfds == 0)
76 break;
77 tvp = ares_timeout(channel, NULL, &tv);
78 select(nfds, &read_fds, &write_fds, NULL, tvp);
79 ares_process(channel, &read_fds, &write_fds);
80 }
81
82 ares_destroy(channel);
83 return 0;
84}
85
86static void callback(void *arg, int status, struct hostent *host)
87{
88 struct in_addr addr;
89 char **p;
90
91 if (status != ARES_SUCCESS)
92 {
93 fprintf(stderr, "%s: %s\n", (char *) arg, ares_strerror(status));
94 return;
95 }
96
97 for (p = host->h_addr_list; *p; p++)
98 {
100 memcpy(&addr, *p, sizeof(struct in_addr));
101 inet_ntop(AF_INET, &addr, addrBuf, sizeof(addrBuf));
102 printf("%-32s\t%s\n", host->h_name, addrBuf);
103 }
104}
105
106static void usage(void)
107{
108 fprintf(stderr, "usage: ahost {host|addr} ...\n");
109 exit(1);
110}
int main(int argc, char **argv)
Definition ahost.c:38
static void callback(void *arg, int status, struct hostent *host)
Definition ahost.c:86
static void usage(void)
Definition ahost.c:106
#define ARES_SUCCESS
Definition ares.h:79
void ares_gethostbyname(ares_channel channel, const char *name, int family, ares_host_callback callback, void *arg)
int ares_init(ares_channel *channelptr)
Definition ares_init.c:104
struct timeval * ares_timeout(ares_channel channel, struct timeval *maxtv, struct timeval *tv)
void ares_process(ares_channel channel, fd_set *read_fds, fd_set *write_fds)
const char * ares_strerror(int code)
void ares_gethostbyaddr(ares_channel channel, const void *addr, int addrlen, int family, ares_host_callback callback, void *arg)
void ares_destroy(ares_channel channel)
int ares_fds(ares_channel channel, fd_set *read_fds, fd_set *write_fds)
Definition ares_fds.c:22
void list()
Definition userAdmin.cxx:66