19ada6f33SEnji Cooper /* $NetBSD: resolv.c,v 1.6 2004/05/23 16:59:11 christos Exp $ */
29ada6f33SEnji Cooper
39ada6f33SEnji Cooper /*-
49ada6f33SEnji Cooper * Copyright (c) 2004 The NetBSD Foundation, Inc.
59ada6f33SEnji Cooper * All rights reserved.
69ada6f33SEnji Cooper *
79ada6f33SEnji Cooper * This code is derived from software contributed to The NetBSD Foundation
89ada6f33SEnji Cooper * by Christos Zoulas.
99ada6f33SEnji Cooper *
109ada6f33SEnji Cooper * Redistribution and use in source and binary forms, with or without
119ada6f33SEnji Cooper * modification, are permitted provided that the following conditions
129ada6f33SEnji Cooper * are met:
139ada6f33SEnji Cooper * 1. Redistributions of source code must retain the above copyright
149ada6f33SEnji Cooper * notice, this list of conditions and the following disclaimer.
159ada6f33SEnji Cooper * 2. Redistributions in binary form must reproduce the above copyright
169ada6f33SEnji Cooper * notice, this list of conditions and the following disclaimer in the
179ada6f33SEnji Cooper * documentation and/or other materials provided with the distribution.
189ada6f33SEnji Cooper *
199ada6f33SEnji Cooper * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
209ada6f33SEnji Cooper * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
219ada6f33SEnji Cooper * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
229ada6f33SEnji Cooper * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
239ada6f33SEnji Cooper * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
249ada6f33SEnji Cooper * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
259ada6f33SEnji Cooper * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
269ada6f33SEnji Cooper * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
279ada6f33SEnji Cooper * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
289ada6f33SEnji Cooper * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
299ada6f33SEnji Cooper * POSSIBILITY OF SUCH DAMAGE.
309ada6f33SEnji Cooper */
319ada6f33SEnji Cooper #include <sys/cdefs.h>
329ada6f33SEnji Cooper __RCSID("$NetBSD: resolv.c,v 1.6 2004/05/23 16:59:11 christos Exp $");
339ada6f33SEnji Cooper
349ada6f33SEnji Cooper #include <sys/types.h>
359ada6f33SEnji Cooper #include <sys/socket.h>
365593499dSKyle Evans #include <assert.h>
375593499dSKyle Evans #include <errno.h>
389ada6f33SEnji Cooper #include <pthread.h>
399ada6f33SEnji Cooper #include <stdio.h>
40*85425bdcSAlex Richardson #include <stdatomic.h>
419ada6f33SEnji Cooper #include <netdb.h>
429ada6f33SEnji Cooper #include <stdlib.h>
439ada6f33SEnji Cooper #include <unistd.h>
449ada6f33SEnji Cooper #include <string.h>
459ada6f33SEnji Cooper #include <stringlist.h>
469ada6f33SEnji Cooper
479ada6f33SEnji Cooper #include <atf-c.h>
489ada6f33SEnji Cooper
499ada6f33SEnji Cooper #define NTHREADS 10
509ada6f33SEnji Cooper #define NHOSTS 100
519ada6f33SEnji Cooper #define WS " \t\n\r"
529ada6f33SEnji Cooper
539ada6f33SEnji Cooper enum method {
549ada6f33SEnji Cooper METHOD_GETADDRINFO,
559ada6f33SEnji Cooper METHOD_GETHOSTBY,
569ada6f33SEnji Cooper METHOD_GETIPNODEBY
579ada6f33SEnji Cooper };
589ada6f33SEnji Cooper
599ada6f33SEnji Cooper static StringList *hosts = NULL;
60*85425bdcSAlex Richardson static _Atomic(int) *ask = NULL;
61*85425bdcSAlex Richardson static _Atomic(int) *got = NULL;
62*85425bdcSAlex Richardson static bool debug_output = 0;
639ada6f33SEnji Cooper
649ada6f33SEnji Cooper static void load(const char *);
65*85425bdcSAlex Richardson static void resolvone(long, int, enum method);
669ada6f33SEnji Cooper static void *resolvloop(void *);
67*85425bdcSAlex Richardson static pthread_t run(int, enum method, long);
689ada6f33SEnji Cooper
69*85425bdcSAlex Richardson #define DBG(...) do { \
70*85425bdcSAlex Richardson if (debug_output) \
71*85425bdcSAlex Richardson dprintf(STDOUT_FILENO, __VA_ARGS__); \
72*85425bdcSAlex Richardson } while (0)
739ada6f33SEnji Cooper
749ada6f33SEnji Cooper static void
load(const char * fname)759ada6f33SEnji Cooper load(const char *fname)
769ada6f33SEnji Cooper {
779ada6f33SEnji Cooper FILE *fp;
782afeaad3SJohn Baldwin size_t linecap;
799ada6f33SEnji Cooper char *line;
809ada6f33SEnji Cooper
812afeaad3SJohn Baldwin fp = fopen(fname, "r");
829ada6f33SEnji Cooper ATF_REQUIRE(fp != NULL);
832afeaad3SJohn Baldwin line = NULL;
842afeaad3SJohn Baldwin linecap = 0;
852afeaad3SJohn Baldwin while (getline(&line, &linecap, fp) >= 0) {
869ada6f33SEnji Cooper char *ptr;
872afeaad3SJohn Baldwin
889ada6f33SEnji Cooper for (ptr = strtok(line, WS); ptr; ptr = strtok(NULL, WS)) {
892afeaad3SJohn Baldwin if (ptr[0] == '#')
902afeaad3SJohn Baldwin break;
919ada6f33SEnji Cooper sl_add(hosts, strdup(ptr));
929ada6f33SEnji Cooper }
939ada6f33SEnji Cooper }
942afeaad3SJohn Baldwin free(line);
959ada6f33SEnji Cooper
969ada6f33SEnji Cooper (void)fclose(fp);
979ada6f33SEnji Cooper }
989ada6f33SEnji Cooper
999ada6f33SEnji Cooper static int
resolv_getaddrinfo(long threadnum,char * host,int port,const char ** errstr)100*85425bdcSAlex Richardson resolv_getaddrinfo(long threadnum, char *host, int port, const char **errstr)
1019ada6f33SEnji Cooper {
102*85425bdcSAlex Richardson char portstr[6], hbuf[NI_MAXHOST], pbuf[NI_MAXSERV];
1039ada6f33SEnji Cooper struct addrinfo hints, *res;
104*85425bdcSAlex Richardson int error;
1059ada6f33SEnji Cooper
1069ada6f33SEnji Cooper snprintf(portstr, sizeof(portstr), "%d", port);
1079ada6f33SEnji Cooper memset(&hints, 0, sizeof(hints));
1089ada6f33SEnji Cooper hints.ai_family = AF_UNSPEC;
1099ada6f33SEnji Cooper hints.ai_flags = AI_PASSIVE;
1109ada6f33SEnji Cooper hints.ai_socktype = SOCK_STREAM;
1119ada6f33SEnji Cooper error = getaddrinfo(host, portstr, &hints, &res);
1129ada6f33SEnji Cooper if (error == 0) {
113*85425bdcSAlex Richardson DBG("T%ld: host %s ok\n", threadnum, host);
1149ada6f33SEnji Cooper memset(hbuf, 0, sizeof(hbuf));
1159ada6f33SEnji Cooper memset(pbuf, 0, sizeof(pbuf));
1169ada6f33SEnji Cooper getnameinfo(res->ai_addr, res->ai_addrlen, hbuf, sizeof(hbuf),
1179ada6f33SEnji Cooper pbuf, sizeof(pbuf), 0);
118*85425bdcSAlex Richardson DBG("T%ld: reverse %s %s\n", threadnum, hbuf, pbuf);
1199ada6f33SEnji Cooper freeaddrinfo(res);
120*85425bdcSAlex Richardson } else {
121*85425bdcSAlex Richardson *errstr = gai_strerror(error);
122*85425bdcSAlex Richardson DBG("T%ld: host %s not found: %s\n", threadnum, host, *errstr);
123*85425bdcSAlex Richardson }
1249ada6f33SEnji Cooper return error;
1259ada6f33SEnji Cooper }
1269ada6f33SEnji Cooper
1279ada6f33SEnji Cooper static int
resolv_gethostby(long threadnum,char * host,const char ** errstr)128*85425bdcSAlex Richardson resolv_gethostby(long threadnum, char *host, const char **errstr)
1299ada6f33SEnji Cooper {
1309ada6f33SEnji Cooper char buf[1024];
1319ada6f33SEnji Cooper struct hostent *hp, *hp2;
1329ada6f33SEnji Cooper
1339ada6f33SEnji Cooper hp = gethostbyname(host);
1349ada6f33SEnji Cooper if (hp) {
135*85425bdcSAlex Richardson DBG("T%ld: host %s ok\n", threadnum, host);
1369ada6f33SEnji Cooper memcpy(buf, hp->h_addr, hp->h_length);
1379ada6f33SEnji Cooper hp2 = gethostbyaddr(buf, hp->h_length, hp->h_addrtype);
1389ada6f33SEnji Cooper if (hp2) {
139*85425bdcSAlex Richardson DBG("T%ld: reverse %s\n", threadnum, hp2->h_name);
1409ada6f33SEnji Cooper }
141*85425bdcSAlex Richardson } else {
142*85425bdcSAlex Richardson *errstr = hstrerror(h_errno);
143*85425bdcSAlex Richardson DBG("T%ld: host %s not found: %s\n", threadnum, host, *errstr);
1449ada6f33SEnji Cooper }
145*85425bdcSAlex Richardson return hp ? 0 : h_errno;
1469ada6f33SEnji Cooper }
1479ada6f33SEnji Cooper
1489ada6f33SEnji Cooper static int
resolv_getipnodeby(long threadnum,char * host,const char ** errstr)149*85425bdcSAlex Richardson resolv_getipnodeby(long threadnum, char *host, const char **errstr)
1509ada6f33SEnji Cooper {
1519ada6f33SEnji Cooper char buf[1024];
1529ada6f33SEnji Cooper struct hostent *hp, *hp2;
153*85425bdcSAlex Richardson int error = 0;
1549ada6f33SEnji Cooper
155*85425bdcSAlex Richardson hp = getipnodebyname(host, AF_INET, 0, &error);
1569ada6f33SEnji Cooper if (hp) {
157*85425bdcSAlex Richardson DBG("T%ld: host %s ok\n", threadnum, host);
1589ada6f33SEnji Cooper memcpy(buf, hp->h_addr, hp->h_length);
1599ada6f33SEnji Cooper hp2 = getipnodebyaddr(buf, hp->h_length, hp->h_addrtype,
160*85425bdcSAlex Richardson &error);
1619ada6f33SEnji Cooper if (hp2) {
162*85425bdcSAlex Richardson DBG("T%ld: reverse %s\n", threadnum, hp2->h_name);
1639ada6f33SEnji Cooper freehostent(hp2);
1649ada6f33SEnji Cooper }
1659ada6f33SEnji Cooper freehostent(hp);
166*85425bdcSAlex Richardson } else {
167*85425bdcSAlex Richardson *errstr = hstrerror(error);
168*85425bdcSAlex Richardson DBG("T%ld: host %s not found: %s\n", threadnum, host, *errstr);
169*85425bdcSAlex Richardson }
170*85425bdcSAlex Richardson return hp ? 0 : error;
1719ada6f33SEnji Cooper }
1729ada6f33SEnji Cooper
1739ada6f33SEnji Cooper static void
resolvone(long threadnum,int n,enum method method)174*85425bdcSAlex Richardson resolvone(long threadnum, int n, enum method method)
1759ada6f33SEnji Cooper {
176*85425bdcSAlex Richardson const char* errstr = NULL;
1779ada6f33SEnji Cooper size_t i = (random() & 0x0fffffff) % hosts->sl_cur;
1789ada6f33SEnji Cooper char *host = hosts->sl_str[i];
179*85425bdcSAlex Richardson int error;
1809ada6f33SEnji Cooper
181*85425bdcSAlex Richardson DBG("T%ld: %d resolving %s %zd\n", threadnum, n, host, i);
1829ada6f33SEnji Cooper switch (method) {
1839ada6f33SEnji Cooper case METHOD_GETADDRINFO:
184*85425bdcSAlex Richardson error = resolv_getaddrinfo(threadnum, host, i, &errstr);
1859ada6f33SEnji Cooper break;
1869ada6f33SEnji Cooper case METHOD_GETHOSTBY:
187*85425bdcSAlex Richardson error = resolv_gethostby(threadnum, host, &errstr);
1889ada6f33SEnji Cooper break;
1899ada6f33SEnji Cooper case METHOD_GETIPNODEBY:
190*85425bdcSAlex Richardson error = resolv_getipnodeby(threadnum, host, &errstr);
1919ada6f33SEnji Cooper break;
1929ada6f33SEnji Cooper default:
1935593499dSKyle Evans /* UNREACHABLE */
1945593499dSKyle Evans /* XXX Needs an __assert_unreachable() for userland. */
1955593499dSKyle Evans assert(0 && "Unreachable segment reached");
1965593499dSKyle Evans abort();
1979ada6f33SEnji Cooper break;
1989ada6f33SEnji Cooper }
199*85425bdcSAlex Richardson atomic_fetch_add_explicit(&ask[i], 1, memory_order_relaxed);
200*85425bdcSAlex Richardson if (error == 0)
201*85425bdcSAlex Richardson atomic_fetch_add_explicit(&got[i], 1, memory_order_relaxed);
202*85425bdcSAlex Richardson else if (got[i] != 0)
203*85425bdcSAlex Richardson fprintf(stderr,
204*85425bdcSAlex Richardson "T%ld ERROR after previous success for %s: %d (%s)\n",
205*85425bdcSAlex Richardson threadnum, hosts->sl_str[i], error, errstr);
2069ada6f33SEnji Cooper }
2079ada6f33SEnji Cooper
2085593499dSKyle Evans struct resolvloop_args {
209*85425bdcSAlex Richardson int nhosts;
2105593499dSKyle Evans enum method method;
211*85425bdcSAlex Richardson long threadnum;
2125593499dSKyle Evans };
2135593499dSKyle Evans
2149ada6f33SEnji Cooper static void *
resolvloop(void * p)2159ada6f33SEnji Cooper resolvloop(void *p)
2169ada6f33SEnji Cooper {
2175593499dSKyle Evans struct resolvloop_args *args = p;
218*85425bdcSAlex Richardson int nhosts = args->nhosts;
2195593499dSKyle Evans
220*85425bdcSAlex Richardson if (nhosts == 0) {
2215593499dSKyle Evans free(args);
2229ada6f33SEnji Cooper return NULL;
2235593499dSKyle Evans }
2245593499dSKyle Evans
225*85425bdcSAlex Richardson do {
226*85425bdcSAlex Richardson resolvone(args->threadnum, nhosts, args->method);
227*85425bdcSAlex Richardson } while (--nhosts);
2285593499dSKyle Evans free(args);
229*85425bdcSAlex Richardson return (void *)(uintptr_t)nhosts;
2309ada6f33SEnji Cooper }
2319ada6f33SEnji Cooper
232*85425bdcSAlex Richardson static pthread_t
run(int nhosts,enum method method,long i)233*85425bdcSAlex Richardson run(int nhosts, enum method method, long i)
2349ada6f33SEnji Cooper {
235*85425bdcSAlex Richardson pthread_t t;
2369ada6f33SEnji Cooper int rc;
2375593499dSKyle Evans struct resolvloop_args *args;
2389ada6f33SEnji Cooper
2395593499dSKyle Evans /* Created thread is responsible for free(). */
2405593499dSKyle Evans args = malloc(sizeof(*args));
2415593499dSKyle Evans ATF_REQUIRE(args != NULL);
2425593499dSKyle Evans
2435593499dSKyle Evans args->nhosts = nhosts;
2445593499dSKyle Evans args->method = method;
245*85425bdcSAlex Richardson args->threadnum = i + 1;
246*85425bdcSAlex Richardson rc = pthread_create(&t, NULL, resolvloop, args);
2479ada6f33SEnji Cooper ATF_REQUIRE_MSG(rc == 0, "pthread_create failed: %s", strerror(rc));
248*85425bdcSAlex Richardson return t;
2499ada6f33SEnji Cooper }
2509ada6f33SEnji Cooper
2519ada6f33SEnji Cooper static int
run_tests(const char * hostlist_file,enum method method)2529ada6f33SEnji Cooper run_tests(const char *hostlist_file, enum method method)
2539ada6f33SEnji Cooper {
2545593499dSKyle Evans size_t nthreads = NTHREADS;
255*85425bdcSAlex Richardson pthread_t *threads;
2565593499dSKyle Evans size_t nhosts = NHOSTS;
2575593499dSKyle Evans size_t i;
258*85425bdcSAlex Richardson int c;
2599ada6f33SEnji Cooper hosts = sl_init();
2609ada6f33SEnji Cooper
2619ada6f33SEnji Cooper srandom(1234);
262*85425bdcSAlex Richardson debug_output = getenv("DEBUG_OUTPUT") != NULL;
2639ada6f33SEnji Cooper
2649ada6f33SEnji Cooper load(hostlist_file);
2659ada6f33SEnji Cooper
2669ada6f33SEnji Cooper ATF_REQUIRE_MSG(0 < hosts->sl_cur, "0 hosts in %s", hostlist_file);
2679ada6f33SEnji Cooper
2689ada6f33SEnji Cooper ask = calloc(hosts->sl_cur, sizeof(int));
2699ada6f33SEnji Cooper ATF_REQUIRE(ask != NULL);
2709ada6f33SEnji Cooper
2719ada6f33SEnji Cooper got = calloc(hosts->sl_cur, sizeof(int));
2729ada6f33SEnji Cooper ATF_REQUIRE(got != NULL);
2739ada6f33SEnji Cooper
274*85425bdcSAlex Richardson threads = calloc(nthreads, sizeof(pthread_t));
275*85425bdcSAlex Richardson ATF_REQUIRE(threads != NULL);
276*85425bdcSAlex Richardson
2779ada6f33SEnji Cooper for (i = 0; i < nthreads; i++) {
278*85425bdcSAlex Richardson threads[i] = run(nhosts, method, i);
279*85425bdcSAlex Richardson }
280*85425bdcSAlex Richardson /* Wait for all threads to join and check that they checked all hosts */
281*85425bdcSAlex Richardson for (i = 0; i < nthreads; i++) {
282*85425bdcSAlex Richardson size_t remaining;
283*85425bdcSAlex Richardson
284*85425bdcSAlex Richardson remaining = (uintptr_t)pthread_join(threads[i], NULL);
285*85425bdcSAlex Richardson ATF_CHECK_EQ_MSG(0, remaining,
286*85425bdcSAlex Richardson "Thread %zd still had %zd hosts to check!", i, remaining);
2879ada6f33SEnji Cooper }
2889ada6f33SEnji Cooper
2899ada6f33SEnji Cooper c = 0;
2909ada6f33SEnji Cooper for (i = 0; i < hosts->sl_cur; i++) {
291*85425bdcSAlex Richardson if (got[i] != 0) {
292*85425bdcSAlex Richardson ATF_CHECK_EQ_MSG(ask[i], got[i],
293*85425bdcSAlex Richardson "Error: host %s ask %d got %d", hosts->sl_str[i],
294*85425bdcSAlex Richardson ask[i], got[i]);
295*85425bdcSAlex Richardson c += ask[i] != got[i];
2969ada6f33SEnji Cooper }
2979ada6f33SEnji Cooper }
298*85425bdcSAlex Richardson free(threads);
2999ada6f33SEnji Cooper free(ask);
3009ada6f33SEnji Cooper free(got);
3019ada6f33SEnji Cooper sl_free(hosts, 1);
3029ada6f33SEnji Cooper return c;
3039ada6f33SEnji Cooper }
3049ada6f33SEnji Cooper
3059ada6f33SEnji Cooper #define HOSTLIST_FILE "mach"
3069ada6f33SEnji Cooper
3079ada6f33SEnji Cooper #define RUN_TESTS(tc, method) \
3089ada6f33SEnji Cooper do { \
3099ada6f33SEnji Cooper char *_hostlist_file; \
3109ada6f33SEnji Cooper ATF_REQUIRE(0 < asprintf(&_hostlist_file, "%s/%s", \
3119ada6f33SEnji Cooper atf_tc_get_config_var(tc, "srcdir"), HOSTLIST_FILE)); \
3129ada6f33SEnji Cooper ATF_REQUIRE(run_tests(_hostlist_file, method) == 0); \
3139ada6f33SEnji Cooper } while(0)
3149ada6f33SEnji Cooper
315159a783fSEnji Cooper ATF_TC(getaddrinfo_test);
ATF_TC_HEAD(getaddrinfo_test,tc)316159a783fSEnji Cooper ATF_TC_HEAD(getaddrinfo_test, tc) {
31704ee829fSEnji Cooper atf_tc_set_md_var(tc, "timeout", "1200");
318159a783fSEnji Cooper }
ATF_TC_BODY(getaddrinfo_test,tc)3199ada6f33SEnji Cooper ATF_TC_BODY(getaddrinfo_test, tc)
3209ada6f33SEnji Cooper {
3219ada6f33SEnji Cooper
3229ada6f33SEnji Cooper RUN_TESTS(tc, METHOD_GETADDRINFO);
3239ada6f33SEnji Cooper }
3249ada6f33SEnji Cooper
325159a783fSEnji Cooper ATF_TC(gethostby_test);
ATF_TC_HEAD(gethostby_test,tc)326159a783fSEnji Cooper ATF_TC_HEAD(gethostby_test, tc) {
32704ee829fSEnji Cooper atf_tc_set_md_var(tc, "timeout", "1200");
328159a783fSEnji Cooper }
ATF_TC_BODY(gethostby_test,tc)3299ada6f33SEnji Cooper ATF_TC_BODY(gethostby_test, tc)
3309ada6f33SEnji Cooper {
3319ada6f33SEnji Cooper
3329ada6f33SEnji Cooper RUN_TESTS(tc, METHOD_GETHOSTBY);
3339ada6f33SEnji Cooper }
3349ada6f33SEnji Cooper
335159a783fSEnji Cooper ATF_TC(getipnodeby_test);
ATF_TC_HEAD(getipnodeby_test,tc)336159a783fSEnji Cooper ATF_TC_HEAD(getipnodeby_test, tc) {
337159a783fSEnji Cooper
33804ee829fSEnji Cooper atf_tc_set_md_var(tc, "timeout", "1200");
339159a783fSEnji Cooper }
ATF_TC_BODY(getipnodeby_test,tc)3409ada6f33SEnji Cooper ATF_TC_BODY(getipnodeby_test, tc)
3419ada6f33SEnji Cooper {
3429ada6f33SEnji Cooper
3439ada6f33SEnji Cooper RUN_TESTS(tc, METHOD_GETIPNODEBY);
3449ada6f33SEnji Cooper }
3459ada6f33SEnji Cooper
ATF_TP_ADD_TCS(tp)3469ada6f33SEnji Cooper ATF_TP_ADD_TCS(tp)
3479ada6f33SEnji Cooper {
3489ada6f33SEnji Cooper
3499ada6f33SEnji Cooper ATF_TP_ADD_TC(tp, getaddrinfo_test);
3509ada6f33SEnji Cooper ATF_TP_ADD_TC(tp, gethostby_test);
3519ada6f33SEnji Cooper ATF_TP_ADD_TC(tp, getipnodeby_test);
3529ada6f33SEnji Cooper
3539ada6f33SEnji Cooper return (atf_no_error());
3549ada6f33SEnji Cooper }
355