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>
45 #include <stringlist.h>
59 static StringList *hosts = NULL;
60 static int *ask = NULL;
61 static int *got = NULL;
63 static void load(const char *);
64 static void resolvone(int, enum method);
65 static void *resolvloop(void *);
66 static void run(int *, enum method);
68 static pthread_mutex_t stats = PTHREAD_MUTEX_INITIALIZER;
71 load(const char *fname)
77 fp = fopen(fname, "r");
78 ATF_REQUIRE(fp != NULL);
81 while (getline(&line, &linecap, fp) >= 0) {
84 for (ptr = strtok(line, WS); ptr; ptr = strtok(NULL, WS)) {
87 sl_add(hosts, strdup(ptr));
96 resolv_getaddrinfo(pthread_t self, char *host, int port)
98 char portstr[6], buf[1024], hbuf[NI_MAXHOST], pbuf[NI_MAXSERV];
99 struct addrinfo hints, *res;
102 snprintf(portstr, sizeof(portstr), "%d", port);
103 memset(&hints, 0, sizeof(hints));
104 hints.ai_family = AF_UNSPEC;
105 hints.ai_flags = AI_PASSIVE;
106 hints.ai_socktype = SOCK_STREAM;
107 error = getaddrinfo(host, portstr, &hints, &res);
108 len = snprintf(buf, sizeof(buf), "%p: host %s %s\n",
109 self, host, error ? "not found" : "ok");
110 (void)write(STDOUT_FILENO, buf, len);
112 memset(hbuf, 0, sizeof(hbuf));
113 memset(pbuf, 0, sizeof(pbuf));
114 getnameinfo(res->ai_addr, res->ai_addrlen, hbuf, sizeof(hbuf),
115 pbuf, sizeof(pbuf), 0);
116 len = snprintf(buf, sizeof(buf),
117 "%p: reverse %s %s\n", self, hbuf, pbuf);
118 (void)write(STDOUT_FILENO, buf, len);
126 resolv_gethostby(pthread_t self, char *host)
129 struct hostent *hp, *hp2;
132 hp = gethostbyname(host);
133 len = snprintf(buf, sizeof(buf), "%p: host %s %s\n",
134 self, host, (hp == NULL) ? "not found" : "ok");
135 (void)write(STDOUT_FILENO, buf, len);
137 memcpy(buf, hp->h_addr, hp->h_length);
138 hp2 = gethostbyaddr(buf, hp->h_length, hp->h_addrtype);
140 len = snprintf(buf, sizeof(buf),
141 "%p: reverse %s\n", self, hp2->h_name);
142 (void)write(STDOUT_FILENO, buf, len);
149 resolv_getipnodeby(pthread_t self, char *host)
152 struct hostent *hp, *hp2;
155 hp = getipnodebyname(host, AF_INET, 0, &h_error);
156 len = snprintf(buf, sizeof(buf), "%p: host %s %s\n",
157 self, host, (hp == NULL) ? "not found" : "ok");
158 (void)write(STDOUT_FILENO, buf, len);
160 memcpy(buf, hp->h_addr, hp->h_length);
161 hp2 = getipnodebyaddr(buf, hp->h_length, hp->h_addrtype,
164 len = snprintf(buf, sizeof(buf),
165 "%p: reverse %s\n", self, hp2->h_name);
166 (void)write(STDOUT_FILENO, buf, len);
177 resolvone(int n, enum method method)
180 pthread_t self = pthread_self();
181 size_t i = (random() & 0x0fffffff) % hosts->sl_cur;
182 char *host = hosts->sl_str[i];
185 len = snprintf(buf, sizeof(buf), "%p: %d resolving %s %d\n",
186 self, n, host, (int)i);
187 (void)write(STDOUT_FILENO, buf, len);
190 case METHOD_GETADDRINFO:
191 error = resolv_getaddrinfo(self, host, i);
193 case METHOD_GETHOSTBY:
194 error = resolv_gethostby(self, host);
196 case METHOD_GETIPNODEBY:
197 error = resolv_getipnodeby(self, host);
201 /* XXX Needs an __assert_unreachable() for userland. */
202 assert(0 && "Unreachable segment reached");
206 pthread_mutex_lock(&stats);
208 got[i] += error == 0;
209 pthread_mutex_unlock(&stats);
212 struct resolvloop_args {
220 struct resolvloop_args *args = p;
222 if (*args->nhosts == 0) {
228 resolvone(*args->nhosts, args->method);
229 while (--(*args->nhosts));
235 run(int *nhosts, enum method method)
239 struct resolvloop_args *args;
241 /* Created thread is responsible for free(). */
242 args = malloc(sizeof(*args));
243 ATF_REQUIRE(args != NULL);
245 args->nhosts = nhosts;
246 args->method = method;
247 self = pthread_self();
248 rc = pthread_create(&self, NULL, resolvloop, args);
249 ATF_REQUIRE_MSG(rc == 0, "pthread_create failed: %s", strerror(rc));
253 run_tests(const char *hostlist_file, enum method method)
255 size_t nthreads = NTHREADS;
256 size_t nhosts = NHOSTS;
265 ATF_REQUIRE_MSG(0 < hosts->sl_cur, "0 hosts in %s", hostlist_file);
267 nleft = malloc(nthreads * sizeof(int));
268 ATF_REQUIRE(nleft != NULL);
270 ask = calloc(hosts->sl_cur, sizeof(int));
271 ATF_REQUIRE(ask != NULL);
273 got = calloc(hosts->sl_cur, sizeof(int));
274 ATF_REQUIRE(got != NULL);
276 for (i = 0; i < nthreads; i++) {
278 run(&nleft[i], method);
281 for (done = 0; !done;) {
283 for (i = 0; i < nthreads; i++) {
292 for (i = 0; i < hosts->sl_cur; i++) {
293 if (ask[i] != got[i] && got[i] != 0) {
294 printf("Error: host %s ask %d got %d\n",
295 hosts->sl_str[i], ask[i], got[i]);
306 #define HOSTLIST_FILE "mach"
308 #define RUN_TESTS(tc, method) \
310 char *_hostlist_file; \
311 ATF_REQUIRE(0 < asprintf(&_hostlist_file, "%s/%s", \
312 atf_tc_get_config_var(tc, "srcdir"), HOSTLIST_FILE)); \
313 ATF_REQUIRE(run_tests(_hostlist_file, method) == 0); \
316 ATF_TC(getaddrinfo_test);
317 ATF_TC_HEAD(getaddrinfo_test, tc) {
318 atf_tc_set_md_var(tc, "timeout", "1200");
320 ATF_TC_BODY(getaddrinfo_test, tc)
323 RUN_TESTS(tc, METHOD_GETADDRINFO);
326 ATF_TC(gethostby_test);
327 ATF_TC_HEAD(gethostby_test, tc) {
328 atf_tc_set_md_var(tc, "timeout", "1200");
330 ATF_TC_BODY(gethostby_test, tc)
333 RUN_TESTS(tc, METHOD_GETHOSTBY);
336 ATF_TC(getipnodeby_test);
337 ATF_TC_HEAD(getipnodeby_test, tc) {
339 atf_tc_set_md_var(tc, "timeout", "1200");
341 ATF_TC_BODY(getipnodeby_test, tc)
344 RUN_TESTS(tc, METHOD_GETIPNODEBY);
350 ATF_TP_ADD_TC(tp, getaddrinfo_test);
351 ATF_TP_ADD_TC(tp, gethostby_test);
352 ATF_TP_ADD_TC(tp, getipnodeby_test);
354 return (atf_no_error());