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>
43 #include <stringlist.h>
57 static StringList *hosts = NULL;
58 static enum method method = METHOD_GETADDRINFO;
59 static int *ask = NULL;
60 static int *got = NULL;
62 static void load(const char *);
63 static void resolvone(int);
64 static void *resolvloop(void *);
65 static void run(int *);
67 static pthread_mutex_t stats = PTHREAD_MUTEX_INITIALIZER;
70 load(const char *fname)
76 if ((fp = fopen(fname, "r")) == NULL)
77 ATF_REQUIRE(fp != NULL);
78 while ((line = fgetln(fp, &len)) != NULL) {
79 char c = line[len - 1];
82 for (ptr = strtok(line, WS); ptr; ptr = strtok(NULL, WS)) {
83 if (ptr == '\0' || ptr[0] == '#')
85 sl_add(hosts, strdup(ptr));
94 resolv_getaddrinfo(pthread_t self, char *host, int port)
96 char portstr[6], buf[1024], hbuf[NI_MAXHOST], pbuf[NI_MAXSERV];
97 struct addrinfo hints, *res;
100 snprintf(portstr, sizeof(portstr), "%d", port);
101 memset(&hints, 0, sizeof(hints));
102 hints.ai_family = AF_UNSPEC;
103 hints.ai_flags = AI_PASSIVE;
104 hints.ai_socktype = SOCK_STREAM;
105 error = getaddrinfo(host, portstr, &hints, &res);
106 len = snprintf(buf, sizeof(buf), "%p: host %s %s\n",
107 self, host, error ? "not found" : "ok");
108 (void)write(STDOUT_FILENO, buf, len);
110 memset(hbuf, 0, sizeof(hbuf));
111 memset(pbuf, 0, sizeof(pbuf));
112 getnameinfo(res->ai_addr, res->ai_addrlen, hbuf, sizeof(hbuf),
113 pbuf, sizeof(pbuf), 0);
114 len = snprintf(buf, sizeof(buf),
115 "%p: reverse %s %s\n", self, hbuf, pbuf);
116 (void)write(STDOUT_FILENO, buf, len);
124 resolv_gethostby(pthread_t self, char *host)
127 struct hostent *hp, *hp2;
130 hp = gethostbyname(host);
131 len = snprintf(buf, sizeof(buf), "%p: host %s %s\n",
132 self, host, (hp == NULL) ? "not found" : "ok");
133 (void)write(STDOUT_FILENO, buf, len);
135 memcpy(buf, hp->h_addr, hp->h_length);
136 hp2 = gethostbyaddr(buf, hp->h_length, hp->h_addrtype);
138 len = snprintf(buf, sizeof(buf),
139 "%p: reverse %s\n", self, hp2->h_name);
140 (void)write(STDOUT_FILENO, buf, len);
147 resolv_getipnodeby(pthread_t self, char *host)
150 struct hostent *hp, *hp2;
153 hp = getipnodebyname(host, AF_INET, 0, &h_error);
154 len = snprintf(buf, sizeof(buf), "%p: host %s %s\n",
155 self, host, (hp == NULL) ? "not found" : "ok");
156 (void)write(STDOUT_FILENO, buf, len);
158 memcpy(buf, hp->h_addr, hp->h_length);
159 hp2 = getipnodebyaddr(buf, hp->h_length, hp->h_addrtype,
162 len = snprintf(buf, sizeof(buf),
163 "%p: reverse %s\n", self, hp2->h_name);
164 (void)write(STDOUT_FILENO, buf, len);
178 pthread_t self = pthread_self();
179 size_t i = (random() & 0x0fffffff) % hosts->sl_cur;
180 char *host = hosts->sl_str[i];
181 struct addrinfo hints, *res;
184 len = snprintf(buf, sizeof(buf), "%p: %d resolving %s %d\n",
185 self, n, host, (int)i);
186 (void)write(STDOUT_FILENO, buf, len);
188 case METHOD_GETADDRINFO:
189 error = resolv_getaddrinfo(self, host, i);
191 case METHOD_GETHOSTBY:
192 error = resolv_gethostby(self, host);
194 case METHOD_GETIPNODEBY:
195 error = resolv_getipnodeby(self, host);
200 pthread_mutex_lock(&stats);
202 got[i] += error == 0;
203 pthread_mutex_unlock(&stats);
209 int *nhosts = (int *)p;
224 self = pthread_self();
225 rc = pthread_create(&self, NULL, resolvloop, nhosts);
226 ATF_REQUIRE_MSG(rc == 0, "pthread_create failed: %s", strerror(rc));
230 run_tests(const char *hostlist_file, enum method method)
232 int nthreads = NTHREADS;
234 int i, c, done, *nleft;
241 ATF_REQUIRE_MSG(0 < hosts->sl_cur, "0 hosts in %s", hostlist_file);
243 nleft = malloc(nthreads * sizeof(int));
244 ATF_REQUIRE(nleft != NULL);
246 ask = calloc(hosts->sl_cur, sizeof(int));
247 ATF_REQUIRE(ask != NULL);
249 got = calloc(hosts->sl_cur, sizeof(int));
250 ATF_REQUIRE(got != NULL);
252 for (i = 0; i < nthreads; i++) {
257 for (done = 0; !done;) {
259 for (i = 0; i < nthreads; i++) {
268 for (i = 0; i < hosts->sl_cur; i++) {
269 if (ask[i] != got[i] && got[i] != 0) {
270 printf("Error: host %s ask %d got %d\n",
271 hosts->sl_str[i], ask[i], got[i]);
282 #define HOSTLIST_FILE "mach"
284 #define RUN_TESTS(tc, method) \
286 char *_hostlist_file; \
287 ATF_REQUIRE(0 < asprintf(&_hostlist_file, "%s/%s", \
288 atf_tc_get_config_var(tc, "srcdir"), HOSTLIST_FILE)); \
289 ATF_REQUIRE(run_tests(_hostlist_file, method) == 0); \
292 ATF_TC(getaddrinfo_test);
293 ATF_TC_HEAD(getaddrinfo_test, tc) {
294 atf_tc_set_md_var(tc, "timeout", "1200");
296 ATF_TC_BODY(getaddrinfo_test, tc)
299 RUN_TESTS(tc, METHOD_GETADDRINFO);
302 ATF_TC(gethostby_test);
303 ATF_TC_HEAD(gethostby_test, tc) {
304 atf_tc_set_md_var(tc, "timeout", "1200");
306 ATF_TC_BODY(gethostby_test, tc)
309 RUN_TESTS(tc, METHOD_GETHOSTBY);
312 ATF_TC(getipnodeby_test);
313 ATF_TC_HEAD(getipnodeby_test, tc) {
315 atf_tc_set_md_var(tc, "timeout", "1200");
317 ATF_TC_BODY(getipnodeby_test, tc)
320 RUN_TESTS(tc, METHOD_GETIPNODEBY);
326 ATF_TP_ADD_TC(tp, getaddrinfo_test);
327 ATF_TP_ADD_TC(tp, gethostby_test);
328 ATF_TP_ADD_TC(tp, getipnodeby_test);
330 return (atf_no_error());