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 if (ptr == '\0' || ptr[0] == '#')
96 sl_add(hosts, strdup(ptr));
105 resolv_getaddrinfo(pthread_t self, char *host, int port)
107 char portstr[6], buf[1024], hbuf[NI_MAXHOST], pbuf[NI_MAXSERV];
108 struct addrinfo hints, *res;
111 snprintf(portstr, sizeof(portstr), "%d", port);
112 memset(&hints, 0, sizeof(hints));
113 hints.ai_family = AF_UNSPEC;
114 hints.ai_flags = AI_PASSIVE;
115 hints.ai_socktype = SOCK_STREAM;
116 error = getaddrinfo(host, portstr, &hints, &res);
118 len = snprintf(buf, sizeof(buf), "%p: host %s %s\n",
119 self, host, error ? "not found" : "ok");
120 (void)write(STDOUT_FILENO, buf, len);
122 if (error == 0 && reverse) {
123 memset(hbuf, 0, sizeof(hbuf));
124 memset(pbuf, 0, sizeof(pbuf));
125 getnameinfo(res->ai_addr, res->ai_addrlen, hbuf, sizeof(hbuf),
126 pbuf, sizeof(pbuf), 0);
128 len = snprintf(buf, sizeof(buf),
129 "%p: reverse %s %s\n", self, hbuf, pbuf);
130 (void)write(STDOUT_FILENO, buf, len);
139 resolv_gethostby(pthread_t self, char *host)
142 struct hostent *hp, *hp2;
145 hp = gethostbyname(host);
147 len = snprintf(buf, sizeof(buf), "%p: host %s %s\n",
148 self, host, (hp == NULL) ? "not found" : "ok");
149 (void)write(STDOUT_FILENO, buf, len);
152 memcpy(buf, hp->h_addr, hp->h_length);
153 hp2 = gethostbyaddr(buf, hp->h_length, hp->h_addrtype);
155 len = snprintf(buf, sizeof(buf),
156 "%p: reverse %s\n", self, hp2->h_name);
157 (void)write(STDOUT_FILENO, buf, len);
164 resolv_getipnodeby(pthread_t self, char *host)
167 struct hostent *hp, *hp2;
170 hp = getipnodebyname(host, AF_INET, 0, &h_error);
172 len = snprintf(buf, sizeof(buf), "%p: host %s %s\n",
173 self, host, (hp == NULL) ? "not found" : "ok");
174 (void)write(STDOUT_FILENO, buf, len);
177 memcpy(buf, hp->h_addr, hp->h_length);
178 hp2 = getipnodebyaddr(buf, hp->h_length, hp->h_addrtype,
181 len = snprintf(buf, sizeof(buf),
182 "%p: reverse %s\n", self, hp2->h_name);
183 (void)write(STDOUT_FILENO, buf, len);
197 pthread_t self = pthread_self();
198 size_t i = (random() & 0x0fffffff) % hosts->sl_cur;
199 char *host = hosts->sl_str[i];
200 struct addrinfo hints, *res;
204 len = snprintf(buf, sizeof(buf), "%p: %d resolving %s %d\n",
205 self, n, host, (int)i);
206 (void)write(STDOUT_FILENO, buf, len);
209 case METHOD_GETADDRINFO:
210 error = resolv_getaddrinfo(self, host, i);
212 case METHOD_GETHOSTBY:
213 error = resolv_gethostby(self, host);
215 case METHOD_GETIPNODEBY:
216 error = resolv_getipnodeby(self, host);
221 pthread_mutex_lock(&stats);
223 got[i] += error == 0;
224 pthread_mutex_unlock(&stats);
230 int *nhosts = (int *)p;
242 pthread_t self = pthread_self();
243 if (pthread_create(&self, NULL, resolvloop, nhosts) != 0)
244 err(1, "pthread_create");
248 main(int argc, char *argv[])
250 int nthreads = NTHREADS;
252 int i, c, done, *nleft;
257 while ((c = getopt(argc, argv, "Adh:HIn:r")) != -1)
260 method = METHOD_GETADDRINFO;
266 nhosts = atoi(optarg);
269 method = METHOD_GETHOSTBY;
272 method = METHOD_GETIPNODEBY;
275 nthreads = atoi(optarg);
284 for (i = optind; i < argc; i++)
287 if (hosts->sl_cur == 0)
290 if ((nleft = malloc(nthreads * sizeof(int))) == NULL)
292 if ((ask = calloc(hosts->sl_cur, sizeof(int))) == NULL)
294 if ((got = calloc(hosts->sl_cur, sizeof(int))) == NULL)
298 for (i = 0; i < nthreads; i++) {
303 for (done = 0; !done;) {
305 for (i = 0; i < nthreads; i++) {
314 for (i = 0; i < hosts->sl_cur; i++) {
315 if (ask[i] != got[i] && got[i] != 0) {
316 warnx("Error: host %s ask %d got %d\n",
317 hosts->sl_str[i], ask[i], got[i]);