1 /* $NetBSD: resolv.c,v 1.6 2004/05/23 16:59:11 christos Exp $ */
4 * Copyright (c) 2004 The NetBSD Foundation, Inc.
7 * This code is derived from software contributed to The NetBSD Foundation
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
19 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
20 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
23 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29 * POSSIBILITY OF SUCH DAMAGE.
32 #include <sys/cdefs.h>
33 __RCSID("$NetBSD: resolv.c,v 1.6 2004/05/23 16:59:11 christos Exp $");
35 #include <sys/types.h>
36 #include <sys/socket.h>
44 #include <stringlist.h>
56 static StringList *hosts = NULL;
58 static enum method method = METHOD_GETADDRINFO;
59 static int reverse = 0;
60 static int *ask = NULL;
61 static int *got = NULL;
63 static void usage(void) __attribute__((__noreturn__));
64 static void load(const char *);
65 static void resolvone(int);
66 static void *resolvloop(void *);
67 static void run(int *);
69 static pthread_mutex_t stats = PTHREAD_MUTEX_INITIALIZER;
75 "Usage: %s [-AdHIr] [-h <nhosts>] [-n <nthreads>] <file> ...\n",
81 load(const char *fname)
87 if ((fp = fopen(fname, "r")) == NULL)
88 err(1, "Cannot open `%s'", fname);
89 while ((line = fgetln(fp, &len)) != NULL) {
93 for (ptr = strtok(line, WS); ptr; ptr = strtok(NULL, WS))
94 sl_add(hosts, strdup(ptr));
102 resolv_getaddrinfo(pthread_t self, char *host, int port)
104 char portstr[6], buf[1024], hbuf[NI_MAXHOST], pbuf[NI_MAXSERV];
105 struct addrinfo hints, *res;
108 snprintf(portstr, sizeof(portstr), "%d", port);
109 memset(&hints, 0, sizeof(hints));
110 hints.ai_family = AF_UNSPEC;
111 hints.ai_flags = AI_PASSIVE;
112 hints.ai_socktype = SOCK_STREAM;
113 error = getaddrinfo(host, portstr, &hints, &res);
115 len = snprintf(buf, sizeof(buf), "%p: host %s %s\n",
116 self, host, error ? "not found" : "ok");
117 (void)write(STDOUT_FILENO, buf, len);
119 if (error == 0 && reverse) {
120 memset(hbuf, 0, sizeof(hbuf));
121 memset(pbuf, 0, sizeof(pbuf));
122 getnameinfo(res->ai_addr, res->ai_addrlen, hbuf, sizeof(hbuf),
123 pbuf, sizeof(pbuf), 0);
125 len = snprintf(buf, sizeof(buf),
126 "%p: reverse %s %s\n", self, hbuf, pbuf);
127 (void)write(STDOUT_FILENO, buf, len);
136 resolv_gethostby(pthread_t self, char *host)
139 struct hostent *hp, *hp2;
142 hp = gethostbyname(host);
144 len = snprintf(buf, sizeof(buf), "%p: host %s %s\n",
145 self, host, (hp == NULL) ? "not found" : "ok");
146 (void)write(STDOUT_FILENO, buf, len);
149 memcpy(buf, hp->h_addr, hp->h_length);
150 hp2 = gethostbyaddr(buf, hp->h_length, hp->h_addrtype);
152 len = snprintf(buf, sizeof(buf),
153 "%p: reverse %s\n", self, hp2->h_name);
154 (void)write(STDOUT_FILENO, buf, len);
161 resolv_getipnodeby(pthread_t self, char *host)
164 struct hostent *hp, *hp2;
167 hp = getipnodebyname(host, AF_INET, 0, &h_error);
169 len = snprintf(buf, sizeof(buf), "%p: host %s %s\n",
170 self, host, (hp == NULL) ? "not found" : "ok");
171 (void)write(STDOUT_FILENO, buf, len);
174 memcpy(buf, hp->h_addr, hp->h_length);
175 hp2 = getipnodebyaddr(buf, hp->h_length, hp->h_addrtype,
178 len = snprintf(buf, sizeof(buf),
179 "%p: reverse %s\n", self, hp2->h_name);
180 (void)write(STDOUT_FILENO, buf, len);
194 pthread_t self = pthread_self();
195 size_t i = (random() & 0x0fffffff) % hosts->sl_cur;
196 char *host = hosts->sl_str[i];
197 struct addrinfo hints, *res;
201 len = snprintf(buf, sizeof(buf), "%p: %d resolving %s %d\n",
202 self, n, host, (int)i);
203 (void)write(STDOUT_FILENO, buf, len);
206 case METHOD_GETADDRINFO:
207 error = resolv_getaddrinfo(self, host, i);
209 case METHOD_GETHOSTBY:
210 error = resolv_gethostby(self, host);
212 case METHOD_GETIPNODEBY:
213 error = resolv_getipnodeby(self, host);
218 pthread_mutex_lock(&stats);
220 got[i] += error == 0;
221 pthread_mutex_unlock(&stats);
227 int *nhosts = (int *)p;
239 pthread_t self = pthread_self();
240 if (pthread_create(&self, NULL, resolvloop, nhosts) != 0)
241 err(1, "pthread_create");
245 main(int argc, char *argv[])
247 int nthreads = NTHREADS;
249 int i, c, done, *nleft;
254 while ((c = getopt(argc, argv, "Adh:HIn:r")) != -1)
257 method = METHOD_GETADDRINFO;
263 nhosts = atoi(optarg);
266 method = METHOD_GETHOSTBY;
269 method = METHOD_GETIPNODEBY;
272 nthreads = atoi(optarg);
281 for (i = optind; i < argc; i++)
284 if (hosts->sl_cur == 0)
287 if ((nleft = malloc(nthreads * sizeof(int))) == NULL)
289 if ((ask = calloc(hosts->sl_cur, sizeof(int))) == NULL)
291 if ((got = calloc(hosts->sl_cur, sizeof(int))) == NULL)
295 for (i = 0; i < nthreads; i++) {
300 for (done = 0; !done;) {
302 for (i = 0; i < nthreads; i++) {
311 for (i = 0; i < hosts->sl_cur; i++) {
312 if (ask[i] != got[i] && got[i] != 0) {
313 warnx("Error: host %s ask %d got %d\n",
314 hosts->sl_str[i], ask[i], got[i]);