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>
41 #include <stdatomic.h>
46 #include <stringlist.h>
60 static StringList *hosts = NULL;
61 static _Atomic(int) *ask = NULL;
62 static _Atomic(int) *got = NULL;
63 static bool debug_output = 0;
65 static void load(const char *);
66 static void resolvone(long, int, enum method);
67 static void *resolvloop(void *);
68 static pthread_t run(int, enum method, long);
70 #define DBG(...) do { \
72 dprintf(STDOUT_FILENO, __VA_ARGS__); \
76 load(const char *fname)
82 fp = fopen(fname, "r");
83 ATF_REQUIRE(fp != NULL);
86 while (getline(&line, &linecap, fp) >= 0) {
89 for (ptr = strtok(line, WS); ptr; ptr = strtok(NULL, WS)) {
92 sl_add(hosts, strdup(ptr));
101 resolv_getaddrinfo(long threadnum, char *host, int port, const char **errstr)
103 char portstr[6], hbuf[NI_MAXHOST], pbuf[NI_MAXSERV];
104 struct addrinfo hints, *res;
107 snprintf(portstr, sizeof(portstr), "%d", port);
108 memset(&hints, 0, sizeof(hints));
109 hints.ai_family = AF_UNSPEC;
110 hints.ai_flags = AI_PASSIVE;
111 hints.ai_socktype = SOCK_STREAM;
112 error = getaddrinfo(host, portstr, &hints, &res);
114 DBG("T%ld: host %s ok\n", threadnum, host);
115 memset(hbuf, 0, sizeof(hbuf));
116 memset(pbuf, 0, sizeof(pbuf));
117 getnameinfo(res->ai_addr, res->ai_addrlen, hbuf, sizeof(hbuf),
118 pbuf, sizeof(pbuf), 0);
119 DBG("T%ld: reverse %s %s\n", threadnum, hbuf, pbuf);
122 *errstr = gai_strerror(error);
123 DBG("T%ld: host %s not found: %s\n", threadnum, host, *errstr);
129 resolv_gethostby(long threadnum, char *host, const char **errstr)
132 struct hostent *hp, *hp2;
134 hp = gethostbyname(host);
136 DBG("T%ld: host %s ok\n", threadnum, host);
137 memcpy(buf, hp->h_addr, hp->h_length);
138 hp2 = gethostbyaddr(buf, hp->h_length, hp->h_addrtype);
140 DBG("T%ld: reverse %s\n", threadnum, hp2->h_name);
143 *errstr = hstrerror(h_errno);
144 DBG("T%ld: host %s not found: %s\n", threadnum, host, *errstr);
146 return hp ? 0 : h_errno;
150 resolv_getipnodeby(long threadnum, char *host, const char **errstr)
153 struct hostent *hp, *hp2;
156 hp = getipnodebyname(host, AF_INET, 0, &error);
158 DBG("T%ld: host %s ok\n", threadnum, host);
159 memcpy(buf, hp->h_addr, hp->h_length);
160 hp2 = getipnodebyaddr(buf, hp->h_length, hp->h_addrtype,
163 DBG("T%ld: reverse %s\n", threadnum, hp2->h_name);
168 *errstr = hstrerror(error);
169 DBG("T%ld: host %s not found: %s\n", threadnum, host, *errstr);
171 return hp ? 0 : error;
175 resolvone(long threadnum, int n, enum method method)
177 const char* errstr = NULL;
178 size_t i = (random() & 0x0fffffff) % hosts->sl_cur;
179 char *host = hosts->sl_str[i];
182 DBG("T%ld: %d resolving %s %zd\n", threadnum, n, host, i);
184 case METHOD_GETADDRINFO:
185 error = resolv_getaddrinfo(threadnum, host, i, &errstr);
187 case METHOD_GETHOSTBY:
188 error = resolv_gethostby(threadnum, host, &errstr);
190 case METHOD_GETIPNODEBY:
191 error = resolv_getipnodeby(threadnum, host, &errstr);
195 /* XXX Needs an __assert_unreachable() for userland. */
196 assert(0 && "Unreachable segment reached");
200 atomic_fetch_add_explicit(&ask[i], 1, memory_order_relaxed);
202 atomic_fetch_add_explicit(&got[i], 1, memory_order_relaxed);
203 else if (got[i] != 0)
205 "T%ld ERROR after previous success for %s: %d (%s)\n",
206 threadnum, hosts->sl_str[i], error, errstr);
209 struct resolvloop_args {
218 struct resolvloop_args *args = p;
219 int nhosts = args->nhosts;
227 resolvone(args->threadnum, nhosts, args->method);
230 return (void *)(uintptr_t)nhosts;
234 run(int nhosts, enum method method, long i)
238 struct resolvloop_args *args;
240 /* Created thread is responsible for free(). */
241 args = malloc(sizeof(*args));
242 ATF_REQUIRE(args != NULL);
244 args->nhosts = nhosts;
245 args->method = method;
246 args->threadnum = i + 1;
247 rc = pthread_create(&t, NULL, resolvloop, args);
248 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;
257 size_t nhosts = NHOSTS;
263 debug_output = getenv("DEBUG_OUTPUT") != NULL;
267 ATF_REQUIRE_MSG(0 < hosts->sl_cur, "0 hosts in %s", hostlist_file);
269 ask = calloc(hosts->sl_cur, sizeof(int));
270 ATF_REQUIRE(ask != NULL);
272 got = calloc(hosts->sl_cur, sizeof(int));
273 ATF_REQUIRE(got != NULL);
275 threads = calloc(nthreads, sizeof(pthread_t));
276 ATF_REQUIRE(threads != NULL);
278 for (i = 0; i < nthreads; i++) {
279 threads[i] = run(nhosts, method, i);
281 /* Wait for all threads to join and check that they checked all hosts */
282 for (i = 0; i < nthreads; i++) {
285 remaining = (uintptr_t)pthread_join(threads[i], NULL);
286 ATF_CHECK_EQ_MSG(0, remaining,
287 "Thread %zd still had %zd hosts to check!", i, remaining);
291 for (i = 0; i < hosts->sl_cur; i++) {
293 ATF_CHECK_EQ_MSG(ask[i], got[i],
294 "Error: host %s ask %d got %d", hosts->sl_str[i],
296 c += 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());