1*57718be8SEnji Cooper /* $NetBSD: h_resolv.c,v 1.2 2010/11/03 16:10:22 christos Exp $ */ 2*57718be8SEnji Cooper 3*57718be8SEnji Cooper /*- 4*57718be8SEnji Cooper * Copyright (c) 2004, 2008 The NetBSD Foundation, Inc. 5*57718be8SEnji Cooper * All rights reserved. 6*57718be8SEnji Cooper * 7*57718be8SEnji Cooper * This code is derived from software contributed to The NetBSD Foundation 8*57718be8SEnji Cooper * by Christos Zoulas. 9*57718be8SEnji Cooper * 10*57718be8SEnji Cooper * Redistribution and use in source and binary forms, with or without 11*57718be8SEnji Cooper * modification, are permitted provided that the following conditions 12*57718be8SEnji Cooper * are met: 13*57718be8SEnji Cooper * 1. Redistributions of source code must retain the above copyright 14*57718be8SEnji Cooper * notice, this list of conditions and the following disclaimer. 15*57718be8SEnji Cooper * 2. Redistributions in binary form must reproduce the above copyright 16*57718be8SEnji Cooper * notice, this list of conditions and the following disclaimer in the 17*57718be8SEnji Cooper * documentation and/or other materials provided with the distribution. 18*57718be8SEnji Cooper * 19*57718be8SEnji Cooper * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS 20*57718be8SEnji Cooper * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED 21*57718be8SEnji Cooper * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 22*57718be8SEnji Cooper * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS 23*57718be8SEnji Cooper * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 24*57718be8SEnji Cooper * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 25*57718be8SEnji Cooper * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 26*57718be8SEnji Cooper * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 27*57718be8SEnji Cooper * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 28*57718be8SEnji Cooper * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 29*57718be8SEnji Cooper * POSSIBILITY OF SUCH DAMAGE. 30*57718be8SEnji Cooper */ 31*57718be8SEnji Cooper 32*57718be8SEnji Cooper #include <sys/cdefs.h> 33*57718be8SEnji Cooper __COPYRIGHT("@(#) Copyright (c) 2008\ 34*57718be8SEnji Cooper The NetBSD Foundation, inc. All rights reserved."); 35*57718be8SEnji Cooper __RCSID("$NetBSD: h_resolv.c,v 1.2 2010/11/03 16:10:22 christos Exp $"); 36*57718be8SEnji Cooper 37*57718be8SEnji Cooper #include <pthread.h> 38*57718be8SEnji Cooper #include <stdio.h> 39*57718be8SEnji Cooper #include <netdb.h> 40*57718be8SEnji Cooper #include <stdlib.h> 41*57718be8SEnji Cooper #include <unistd.h> 42*57718be8SEnji Cooper #include <err.h> 43*57718be8SEnji Cooper #include <string.h> 44*57718be8SEnji Cooper #include <stringlist.h> 45*57718be8SEnji Cooper 46*57718be8SEnji Cooper #define NTHREADS 10 47*57718be8SEnji Cooper #define NHOSTS 100 48*57718be8SEnji Cooper #define WS " \t\n\r" 49*57718be8SEnji Cooper 50*57718be8SEnji Cooper static StringList *hosts = NULL; 51*57718be8SEnji Cooper static int debug = 0; 52*57718be8SEnji Cooper static int *ask = NULL; 53*57718be8SEnji Cooper static int *got = NULL; 54*57718be8SEnji Cooper 55*57718be8SEnji Cooper static void usage(void) __attribute__((__noreturn__)); 56*57718be8SEnji Cooper static void load(const char *); 57*57718be8SEnji Cooper static void resolvone(int); 58*57718be8SEnji Cooper static void *resolvloop(void *); 59*57718be8SEnji Cooper static void run(int *); 60*57718be8SEnji Cooper 61*57718be8SEnji Cooper static pthread_mutex_t stats = PTHREAD_MUTEX_INITIALIZER; 62*57718be8SEnji Cooper 63*57718be8SEnji Cooper static void 64*57718be8SEnji Cooper usage(void) 65*57718be8SEnji Cooper { 66*57718be8SEnji Cooper (void)fprintf(stderr, 67*57718be8SEnji Cooper "Usage: %s [-d] [-h <nhosts>] [-n <nthreads>] <file> ...\n", 68*57718be8SEnji Cooper getprogname()); 69*57718be8SEnji Cooper exit(1); 70*57718be8SEnji Cooper } 71*57718be8SEnji Cooper 72*57718be8SEnji Cooper static void 73*57718be8SEnji Cooper load(const char *fname) 74*57718be8SEnji Cooper { 75*57718be8SEnji Cooper FILE *fp; 76*57718be8SEnji Cooper size_t len; 77*57718be8SEnji Cooper char *line; 78*57718be8SEnji Cooper 79*57718be8SEnji Cooper if ((fp = fopen(fname, "r")) == NULL) 80*57718be8SEnji Cooper err(1, "Cannot open `%s'", fname); 81*57718be8SEnji Cooper while ((line = fgetln(fp, &len)) != NULL) { 82*57718be8SEnji Cooper char c = line[len]; 83*57718be8SEnji Cooper char *ptr; 84*57718be8SEnji Cooper line[len] = '\0'; 85*57718be8SEnji Cooper for (ptr = strtok(line, WS); ptr; ptr = strtok(NULL, WS)) 86*57718be8SEnji Cooper sl_add(hosts, strdup(ptr)); 87*57718be8SEnji Cooper line[len] = c; 88*57718be8SEnji Cooper } 89*57718be8SEnji Cooper 90*57718be8SEnji Cooper (void)fclose(fp); 91*57718be8SEnji Cooper } 92*57718be8SEnji Cooper 93*57718be8SEnji Cooper static void 94*57718be8SEnji Cooper resolvone(int n) 95*57718be8SEnji Cooper { 96*57718be8SEnji Cooper char buf[1024]; 97*57718be8SEnji Cooper pthread_t self = pthread_self(); 98*57718be8SEnji Cooper size_t i = (random() & 0x0fffffff) % hosts->sl_cur; 99*57718be8SEnji Cooper char *host = hosts->sl_str[i]; 100*57718be8SEnji Cooper struct addrinfo *res; 101*57718be8SEnji Cooper int error, len; 102*57718be8SEnji Cooper if (debug) { 103*57718be8SEnji Cooper len = snprintf(buf, sizeof(buf), "%p: %d resolving %s %d\n", 104*57718be8SEnji Cooper self, n, host, (int)i); 105*57718be8SEnji Cooper (void)write(STDOUT_FILENO, buf, len); 106*57718be8SEnji Cooper } 107*57718be8SEnji Cooper error = getaddrinfo(host, NULL, NULL, &res); 108*57718be8SEnji Cooper if (debug) { 109*57718be8SEnji Cooper len = snprintf(buf, sizeof(buf), "%p: host %s %s\n", 110*57718be8SEnji Cooper self, host, error ? "not found" : "ok"); 111*57718be8SEnji Cooper (void)write(STDOUT_FILENO, buf, len); 112*57718be8SEnji Cooper } 113*57718be8SEnji Cooper pthread_mutex_lock(&stats); 114*57718be8SEnji Cooper ask[i]++; 115*57718be8SEnji Cooper got[i] += error == 0; 116*57718be8SEnji Cooper pthread_mutex_unlock(&stats); 117*57718be8SEnji Cooper if (error == 0) 118*57718be8SEnji Cooper freeaddrinfo(res); 119*57718be8SEnji Cooper } 120*57718be8SEnji Cooper 121*57718be8SEnji Cooper static void * 122*57718be8SEnji Cooper resolvloop(void *p) 123*57718be8SEnji Cooper { 124*57718be8SEnji Cooper int *nhosts = (int *)p; 125*57718be8SEnji Cooper if (*nhosts == 0) 126*57718be8SEnji Cooper return NULL; 127*57718be8SEnji Cooper do 128*57718be8SEnji Cooper resolvone(*nhosts); 129*57718be8SEnji Cooper while (--(*nhosts)); 130*57718be8SEnji Cooper return NULL; 131*57718be8SEnji Cooper } 132*57718be8SEnji Cooper 133*57718be8SEnji Cooper static void 134*57718be8SEnji Cooper run(int *nhosts) 135*57718be8SEnji Cooper { 136*57718be8SEnji Cooper pthread_t self = pthread_self(); 137*57718be8SEnji Cooper if (pthread_create(&self, NULL, resolvloop, nhosts) != 0) 138*57718be8SEnji Cooper err(1, "pthread_create"); 139*57718be8SEnji Cooper } 140*57718be8SEnji Cooper 141*57718be8SEnji Cooper int 142*57718be8SEnji Cooper main(int argc, char *argv[]) 143*57718be8SEnji Cooper { 144*57718be8SEnji Cooper int nthreads = NTHREADS; 145*57718be8SEnji Cooper int nhosts = NHOSTS; 146*57718be8SEnji Cooper int i, c, done, *nleft; 147*57718be8SEnji Cooper hosts = sl_init(); 148*57718be8SEnji Cooper 149*57718be8SEnji Cooper srandom(1234); 150*57718be8SEnji Cooper 151*57718be8SEnji Cooper while ((c = getopt(argc, argv, "dh:n:")) != -1) 152*57718be8SEnji Cooper switch (c) { 153*57718be8SEnji Cooper case 'd': 154*57718be8SEnji Cooper debug++; 155*57718be8SEnji Cooper break; 156*57718be8SEnji Cooper case 'h': 157*57718be8SEnji Cooper nhosts = atoi(optarg); 158*57718be8SEnji Cooper break; 159*57718be8SEnji Cooper case 'n': 160*57718be8SEnji Cooper nthreads = atoi(optarg); 161*57718be8SEnji Cooper break; 162*57718be8SEnji Cooper default: 163*57718be8SEnji Cooper usage(); 164*57718be8SEnji Cooper } 165*57718be8SEnji Cooper 166*57718be8SEnji Cooper for (i = optind; i < argc; i++) 167*57718be8SEnji Cooper load(argv[i]); 168*57718be8SEnji Cooper 169*57718be8SEnji Cooper if (hosts->sl_cur == 0) 170*57718be8SEnji Cooper usage(); 171*57718be8SEnji Cooper 172*57718be8SEnji Cooper if ((nleft = malloc(nthreads * sizeof(int))) == NULL) 173*57718be8SEnji Cooper err(1, "malloc"); 174*57718be8SEnji Cooper if ((ask = calloc(hosts->sl_cur, sizeof(int))) == NULL) 175*57718be8SEnji Cooper err(1, "calloc"); 176*57718be8SEnji Cooper if ((got = calloc(hosts->sl_cur, sizeof(int))) == NULL) 177*57718be8SEnji Cooper err(1, "calloc"); 178*57718be8SEnji Cooper 179*57718be8SEnji Cooper 180*57718be8SEnji Cooper for (i = 0; i < nthreads; i++) { 181*57718be8SEnji Cooper nleft[i] = nhosts; 182*57718be8SEnji Cooper run(&nleft[i]); 183*57718be8SEnji Cooper } 184*57718be8SEnji Cooper 185*57718be8SEnji Cooper for (done = 0; !done;) { 186*57718be8SEnji Cooper done = 1; 187*57718be8SEnji Cooper for (i = 0; i < nthreads; i++) { 188*57718be8SEnji Cooper if (nleft[i] != 0) { 189*57718be8SEnji Cooper done = 0; 190*57718be8SEnji Cooper break; 191*57718be8SEnji Cooper } 192*57718be8SEnji Cooper } 193*57718be8SEnji Cooper sleep(1); 194*57718be8SEnji Cooper } 195*57718be8SEnji Cooper c = 0; 196*57718be8SEnji Cooper for (i = 0; i < (int)hosts->sl_cur; i++) { 197*57718be8SEnji Cooper if (ask[i] != got[i] && got[i] != 0) { 198*57718be8SEnji Cooper warnx("Error: host %s ask %d got %d\n", 199*57718be8SEnji Cooper hosts->sl_str[i], ask[i], got[i]); 200*57718be8SEnji Cooper c++; 201*57718be8SEnji Cooper } 202*57718be8SEnji Cooper } 203*57718be8SEnji Cooper free(nleft); 204*57718be8SEnji Cooper free(ask); 205*57718be8SEnji Cooper free(got); 206*57718be8SEnji Cooper sl_free(hosts, 1); 207*57718be8SEnji Cooper return c; 208*57718be8SEnji Cooper } 209