68a8db1efb0cd13e0e3d59362ab9db1a4a8bc496Andreas Gustafsson/*
68a8db1efb0cd13e0e3d59362ab9db1a4a8bc496Andreas Gustafsson * Copyright (C) 2000, 2001 Nominum, Inc.
68a8db1efb0cd13e0e3d59362ab9db1a4a8bc496Andreas Gustafsson *
68a8db1efb0cd13e0e3d59362ab9db1a4a8bc496Andreas Gustafsson * Permission to use, copy, modify, and distribute this software for any
68a8db1efb0cd13e0e3d59362ab9db1a4a8bc496Andreas Gustafsson * purpose with or without fee is hereby granted, provided that the above
68a8db1efb0cd13e0e3d59362ab9db1a4a8bc496Andreas Gustafsson * copyright notice and this permission notice appear in all copies.
68a8db1efb0cd13e0e3d59362ab9db1a4a8bc496Andreas Gustafsson *
68a8db1efb0cd13e0e3d59362ab9db1a4a8bc496Andreas Gustafsson * THE SOFTWARE IS PROVIDED "AS IS" AND INTERNET SOFTWARE CONSORTIUM
68a8db1efb0cd13e0e3d59362ab9db1a4a8bc496Andreas Gustafsson * DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL
68a8db1efb0cd13e0e3d59362ab9db1a4a8bc496Andreas Gustafsson * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL
68a8db1efb0cd13e0e3d59362ab9db1a4a8bc496Andreas Gustafsson * INTERNET SOFTWARE CONSORTIUM BE LIABLE FOR ANY SPECIAL, DIRECT,
68a8db1efb0cd13e0e3d59362ab9db1a4a8bc496Andreas Gustafsson * INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING
68a8db1efb0cd13e0e3d59362ab9db1a4a8bc496Andreas Gustafsson * FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT,
68a8db1efb0cd13e0e3d59362ab9db1a4a8bc496Andreas Gustafsson * NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION
68a8db1efb0cd13e0e3d59362ab9db1a4a8bc496Andreas Gustafsson * WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
68a8db1efb0cd13e0e3d59362ab9db1a4a8bc496Andreas Gustafsson */
68a8db1efb0cd13e0e3d59362ab9db1a4a8bc496Andreas Gustafsson
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafsson/***
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafsson *** DNS Query Performance Testing Tool (queryperf.c)
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafsson ***
9db88ce113701a6fb600a67aaa65764d800e25b0Mark Andrews *** Version $Id: queryperf.c,v 1.12 2007/09/05 07:36:04 marka Exp $
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafsson ***
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafsson *** Stephen Jacob <sj@nominum.com>
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafsson ***/
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafsson
66f51d8678c8fcc7256354b599317f67455120fdMark Andrews#define BIND_8_COMPAT /* Pull in <arpa/nameser_compat.h> */
66f51d8678c8fcc7256354b599317f67455120fdMark Andrews
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafsson#include <sys/time.h>
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafsson#include <sys/types.h>
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafsson#include <sys/socket.h>
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafsson#include <stdio.h>
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafsson#include <stdlib.h>
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafsson#include <string.h>
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafsson#include <limits.h>
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafsson#include <time.h>
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafsson#include <unistd.h>
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafsson#include <netdb.h>
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafsson#include <netinet/in.h>
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafsson#include <arpa/nameser.h>
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafsson#include <resolv.h>
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafsson#include <math.h>
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafsson#include <errno.h>
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafsson
9db88ce113701a6fb600a67aaa65764d800e25b0Mark Andrews#ifdef HAVE_CONFIG_H
9db88ce113701a6fb600a67aaa65764d800e25b0Mark Andrews#include "config.h"
f620c5e527746a2ec3d90a11d21abd8a114746dfTatuya JINMEI 神明達哉#ifndef HAVE_GETADDRINFO
f620c5e527746a2ec3d90a11d21abd8a114746dfTatuya JINMEI 神明達哉#include "missing/addrinfo.h"
f620c5e527746a2ec3d90a11d21abd8a114746dfTatuya JINMEI 神明達哉#endif
9db88ce113701a6fb600a67aaa65764d800e25b0Mark Andrews#endif
f620c5e527746a2ec3d90a11d21abd8a114746dfTatuya JINMEI 神明達哉
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafsson/*
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafsson * Configuration defaults
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafsson */
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafsson
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafsson#define DEF_MAX_QUERIES_OUTSTANDING 20
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafsson#define DEF_QUERY_TIMEOUT 5 /* in seconds */
f620c5e527746a2ec3d90a11d21abd8a114746dfTatuya JINMEI 神明達哉#define DEF_SERVER_TO_QUERY "127.0.0.1"
f620c5e527746a2ec3d90a11d21abd8a114746dfTatuya JINMEI 神明達哉#define DEF_SERVER_PORT "53"
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafsson#define DEF_BUFFER_SIZE 32 /* in k */
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafsson
b89095ba15e9163f2bacc46239d8f69d1a2b9ad3Tatuya JINMEI 神明達哉#define DEF_RTTARRAY_SIZE 50000
b89095ba15e9163f2bacc46239d8f69d1a2b9ad3Tatuya JINMEI 神明達哉#define DEF_RTTARRAY_UNIT 100 /* in usec */
b89095ba15e9163f2bacc46239d8f69d1a2b9ad3Tatuya JINMEI 神明達哉
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafsson/*
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafsson * Other constants / definitions
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafsson */
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafsson
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafsson#define COMMENT_CHAR ';'
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafsson#define CONFIG_CHAR '#'
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafsson#define MAX_PORT 65535
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafsson#define MAX_INPUT_LEN 512
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafsson#define MAX_DOMAIN_LEN 255
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafsson#define MAX_BUFFER_LEN 8192 /* in bytes */
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafsson#define HARD_TIMEOUT_EXTRA 5 /* in seconds */
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafsson#define RESPONSE_BLOCKING_WAIT_TIME 0.1 /* in seconds */
8ac1acc30d0f405222ffa7b2b93131d9d4e18599Mark Andrews#define EDNSLEN 11
384fef003441df4067b773688237700513390f9eEvan Hunt#define DNS_HEADERLEN 12
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafsson
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafsson#define FALSE 0
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafsson#define TRUE 1
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafsson
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafsson#define WHITESPACE " \t\n"
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafsson
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafssonenum directives_enum { V_SERVER, V_PORT, V_MAXQUERIES, V_MAXWAIT };
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafsson#define DIRECTIVES { "server", "port", "maxqueries", "maxwait" }
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafsson#define DIR_VALUES { V_SERVER, V_PORT, V_MAXQUERIES, V_MAXWAIT }
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafsson
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafsson#define QTYPE_STRINGS { \
3192f9981331ae89c91a03f1c0f1e3b843fd4f21Evan Hunt "A", "NS", "MD", "MF", "CNAME", "SOA", "MB", "MG", "MR", \
3192f9981331ae89c91a03f1c0f1e3b843fd4f21Evan Hunt "NULL", "WKS", "PTR", "HINFO", "MINFO", "MX", "TXT", "RP", \
3192f9981331ae89c91a03f1c0f1e3b843fd4f21Evan Hunt "AFSDB", "X25", "ISDN", "RT", "NSAP", "NSAP-PTR", "SIG", \
3192f9981331ae89c91a03f1c0f1e3b843fd4f21Evan Hunt "KEY", "PX", "GPOS", "AAAA", "LOC", "NXT", "EID", "NIMLOC", \
3192f9981331ae89c91a03f1c0f1e3b843fd4f21Evan Hunt "SRV", "ATMA", "NAPTR", "KX", "CERT", "A6", "DNAME", "SINK", \
3192f9981331ae89c91a03f1c0f1e3b843fd4f21Evan Hunt "OPT", "APL", "DS", "SSHFP", "IPSECKEY", "RRSIG", "NSEC", \
3192f9981331ae89c91a03f1c0f1e3b843fd4f21Evan Hunt "DNSKEY", "DHCID", "NSEC3", "NSEC3PARAM", "TLSA", "HIP", \
3192f9981331ae89c91a03f1c0f1e3b843fd4f21Evan Hunt "NINFO", "RKEY", "TALINK", "CDS", "SPF", "UINFO", "UID", \
3192f9981331ae89c91a03f1c0f1e3b843fd4f21Evan Hunt "GID", "UNSPEC", "NID", "L32", "L64", "LP", "TKEY", "TSIG", \
3192f9981331ae89c91a03f1c0f1e3b843fd4f21Evan Hunt "IXFR", "AXFR", "MAILB", "MAILA", "URI", "CAA", "*", "ANY", \
3192f9981331ae89c91a03f1c0f1e3b843fd4f21Evan Hunt "TA", "DLV" \
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafsson}
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafsson
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafsson#define QTYPE_CODES { \
3192f9981331ae89c91a03f1c0f1e3b843fd4f21Evan Hunt 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, \
3192f9981331ae89c91a03f1c0f1e3b843fd4f21Evan Hunt 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, \
3192f9981331ae89c91a03f1c0f1e3b843fd4f21Evan Hunt 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, \
3192f9981331ae89c91a03f1c0f1e3b843fd4f21Evan Hunt 49, 50, 51, 52, 55, 56, 57, 58, 59, 99, 100, 101, 102, 103, \
3192f9981331ae89c91a03f1c0f1e3b843fd4f21Evan Hunt 104, 105, 106, 107, 249, 250, 251, 252, 253, 254, 255, 255, \
3192f9981331ae89c91a03f1c0f1e3b843fd4f21Evan Hunt 256, 257, 32768, 32769 \
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafsson}
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafsson
e2cf0e8ff9ff37121518af5b34b9e4de7abbb47cMark Andrews#define RCODE_STRINGS { \
e2cf0e8ff9ff37121518af5b34b9e4de7abbb47cMark Andrews "NOERROR", "FORMERR", "SERVFAIL", "NXDOMAIN", \
e2cf0e8ff9ff37121518af5b34b9e4de7abbb47cMark Andrews "NOTIMP", "REFUSED", "YXDOMAIN", "YXRRSET", \
e2cf0e8ff9ff37121518af5b34b9e4de7abbb47cMark Andrews "NXRRSET", "NOTAUTH", "NOTZONE", "rcode11", \
e2cf0e8ff9ff37121518af5b34b9e4de7abbb47cMark Andrews "rcode12", "rcode13", "rcode14", "rcode15" \
e2cf0e8ff9ff37121518af5b34b9e4de7abbb47cMark Andrews}
e2cf0e8ff9ff37121518af5b34b9e4de7abbb47cMark Andrews
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafsson/*
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafsson * Data type definitions
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafsson */
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafsson
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafsson#define QUERY_STATUS_MAGIC 0x51535441U /* QSTA */
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafsson#define VALID_QUERY_STATUS(q) ((q) != NULL && \
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafsson (q)->magic == QUERY_STATUS_MAGIC)
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafsson
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafssonstruct query_status {
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafsson unsigned int magic;
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafsson int in_use;
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafsson unsigned short int id;
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafsson struct timeval sent_timestamp;
81e805888bcd4bd2a6d5c986fade6ffdf7d88a54Andreas Gustafsson char *desc;
384fef003441df4067b773688237700513390f9eEvan Hunt int qtype;
384fef003441df4067b773688237700513390f9eEvan Hunt char qname[MAX_DOMAIN_LEN + 1];
384fef003441df4067b773688237700513390f9eEvan Hunt};
384fef003441df4067b773688237700513390f9eEvan Hunt
384fef003441df4067b773688237700513390f9eEvan Huntstruct query_mininfo { /* minimum info for timeout queries */
384fef003441df4067b773688237700513390f9eEvan Hunt int qtype; /* use -1 if N/A */
384fef003441df4067b773688237700513390f9eEvan Hunt struct timeval sent_timestamp;
384fef003441df4067b773688237700513390f9eEvan Hunt char qname[MAX_DOMAIN_LEN + 1];
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafsson};
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafsson
66f51d8678c8fcc7256354b599317f67455120fdMark Andrews/*
66f51d8678c8fcc7256354b599317f67455120fdMark Andrews * Forward declarations.
66f51d8678c8fcc7256354b599317f67455120fdMark Andrews */
66f51d8678c8fcc7256354b599317f67455120fdMark Andrewsint is_uint(char *test_int, unsigned int *result);
66f51d8678c8fcc7256354b599317f67455120fdMark Andrews
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafsson/*
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafsson * Configuration options (global)
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafsson */
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafsson
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafssonunsigned int max_queries_outstanding; /* init 0 */
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafssonunsigned int query_timeout = DEF_QUERY_TIMEOUT;
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafssonint ignore_config_changes = FALSE;
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafssonunsigned int socket_bufsize = DEF_BUFFER_SIZE;
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafsson
f620c5e527746a2ec3d90a11d21abd8a114746dfTatuya JINMEI 神明達哉int family = AF_UNSPEC;
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafssonint use_stdin = TRUE;
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafssonchar *datafile_name; /* init NULL */
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafsson
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafssonchar *server_to_query; /* init NULL */
f620c5e527746a2ec3d90a11d21abd8a114746dfTatuya JINMEI 神明達哉char *server_port; /* init NULL */
f620c5e527746a2ec3d90a11d21abd8a114746dfTatuya JINMEI 神明達哉struct addrinfo *server_ai; /* init NULL */
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafsson
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafssonint run_only_once = FALSE;
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafssonint use_timelimit = FALSE;
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafssonunsigned int run_timelimit; /* init 0 */
b89095ba15e9163f2bacc46239d8f69d1a2b9ad3Tatuya JINMEI 神明達哉unsigned int print_interval; /* init 0 */
b89095ba15e9163f2bacc46239d8f69d1a2b9ad3Tatuya JINMEI 神明達哉
b89095ba15e9163f2bacc46239d8f69d1a2b9ad3Tatuya JINMEI 神明達哉unsigned int target_qps; /* init 0 */
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafsson
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafssonint serverset = FALSE, portset = FALSE;
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafssonint queriesset = FALSE, timeoutset = FALSE;
8ac1acc30d0f405222ffa7b2b93131d9d4e18599Mark Andrewsint edns = FALSE, dnssec = FALSE;
e2cf0e8ff9ff37121518af5b34b9e4de7abbb47cMark Andrewsint countrcodes = FALSE;
e2cf0e8ff9ff37121518af5b34b9e4de7abbb47cMark Andrewsint rcodecounts[16] = {0};
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafsson
81e805888bcd4bd2a6d5c986fade6ffdf7d88a54Andreas Gustafssonint verbose = FALSE;
66f51d8678c8fcc7256354b599317f67455120fdMark Andrewsint recurse = 1;
81e805888bcd4bd2a6d5c986fade6ffdf7d88a54Andreas Gustafsson
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafsson/*
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafsson * Other global stuff
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafsson */
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafsson
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafssonint setup_phase = TRUE;
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafsson
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas GustafssonFILE *datafile_ptr; /* init NULL */
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafssonunsigned int runs_through_file; /* init 0 */
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafsson
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafssonunsigned int num_queries_sent; /* init 0 */
b89095ba15e9163f2bacc46239d8f69d1a2b9ad3Tatuya JINMEI 神明達哉unsigned int num_queries_sent_interval;
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafssonunsigned int num_queries_outstanding; /* init 0 */
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafssonunsigned int num_queries_timed_out; /* init 0 */
b89095ba15e9163f2bacc46239d8f69d1a2b9ad3Tatuya JINMEI 神明達哉unsigned int num_queries_possiblydelayed; /* init 0 */
b89095ba15e9163f2bacc46239d8f69d1a2b9ad3Tatuya JINMEI 神明達哉unsigned int num_queries_timed_out_interval;
b89095ba15e9163f2bacc46239d8f69d1a2b9ad3Tatuya JINMEI 神明達哉unsigned int num_queries_possiblydelayed_interval;
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafsson
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafssonstruct timeval time_of_program_start;
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafssonstruct timeval time_of_first_query;
b89095ba15e9163f2bacc46239d8f69d1a2b9ad3Tatuya JINMEI 神明達哉double time_of_first_query_sec;
b89095ba15e9163f2bacc46239d8f69d1a2b9ad3Tatuya JINMEI 神明達哉struct timeval time_of_first_query_interval;
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafssonstruct timeval time_of_end_of_run;
b89095ba15e9163f2bacc46239d8f69d1a2b9ad3Tatuya JINMEI 神明達哉struct timeval time_of_stop_sending;
b89095ba15e9163f2bacc46239d8f69d1a2b9ad3Tatuya JINMEI 神明達哉
b89095ba15e9163f2bacc46239d8f69d1a2b9ad3Tatuya JINMEI 神明達哉struct timeval time_of_queryset_start;
b89095ba15e9163f2bacc46239d8f69d1a2b9ad3Tatuya JINMEI 神明達哉double query_interval;
b89095ba15e9163f2bacc46239d8f69d1a2b9ad3Tatuya JINMEI 神明達哉struct timeval time_of_next_queryset;
b89095ba15e9163f2bacc46239d8f69d1a2b9ad3Tatuya JINMEI 神明達哉
b89095ba15e9163f2bacc46239d8f69d1a2b9ad3Tatuya JINMEI 神明達哉double rtt_max = -1;
b89095ba15e9163f2bacc46239d8f69d1a2b9ad3Tatuya JINMEI 神明達哉double rtt_max_interval = -1;
b89095ba15e9163f2bacc46239d8f69d1a2b9ad3Tatuya JINMEI 神明達哉double rtt_min = -1;
b89095ba15e9163f2bacc46239d8f69d1a2b9ad3Tatuya JINMEI 神明達哉double rtt_min_interval = -1;
b89095ba15e9163f2bacc46239d8f69d1a2b9ad3Tatuya JINMEI 神明達哉double rtt_total;
b89095ba15e9163f2bacc46239d8f69d1a2b9ad3Tatuya JINMEI 神明達哉double rtt_total_interval;
b89095ba15e9163f2bacc46239d8f69d1a2b9ad3Tatuya JINMEI 神明達哉int rttarray_size = DEF_RTTARRAY_SIZE;
b89095ba15e9163f2bacc46239d8f69d1a2b9ad3Tatuya JINMEI 神明達哉int rttarray_unit = DEF_RTTARRAY_UNIT;
b89095ba15e9163f2bacc46239d8f69d1a2b9ad3Tatuya JINMEI 神明達哉unsigned int *rttarray = NULL;
b89095ba15e9163f2bacc46239d8f69d1a2b9ad3Tatuya JINMEI 神明達哉unsigned int *rttarray_interval = NULL;
b89095ba15e9163f2bacc46239d8f69d1a2b9ad3Tatuya JINMEI 神明達哉unsigned int rtt_overflows;
b89095ba15e9163f2bacc46239d8f69d1a2b9ad3Tatuya JINMEI 神明達哉unsigned int rtt_overflows_interval;
384fef003441df4067b773688237700513390f9eEvan Huntunsigned int rtt_counted;
384fef003441df4067b773688237700513390f9eEvan Huntunsigned int rtt_counted_interval;
b89095ba15e9163f2bacc46239d8f69d1a2b9ad3Tatuya JINMEI 神明達哉char *rtt_histogram_file = NULL;
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafsson
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafssonstruct query_status *status; /* init NULL */
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafssonunsigned int query_status_allocated; /* init 0 */
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafsson
f620c5e527746a2ec3d90a11d21abd8a114746dfTatuya JINMEI 神明達哉int query_socket = -1;
f620c5e527746a2ec3d90a11d21abd8a114746dfTatuya JINMEI 神明達哉int socket4 = -1, socket6 = -1;
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafsson
e2cf0e8ff9ff37121518af5b34b9e4de7abbb47cMark Andrewsstatic char *rcode_strings[] = RCODE_STRINGS;
e2cf0e8ff9ff37121518af5b34b9e4de7abbb47cMark Andrews
384fef003441df4067b773688237700513390f9eEvan Huntstatic struct query_mininfo *timeout_queries;
384fef003441df4067b773688237700513390f9eEvan Hunt
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafsson/*
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafsson * get_uint16:
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafsson * Get an unsigned short integer from a buffer (in network order)
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafsson */
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafssonstatic unsigned short
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafssonget_uint16(unsigned char *buf) {
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafsson unsigned short ret;
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafsson
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafsson ret = buf[0] * 256 + buf[1];
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafsson
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafsson return (ret);
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafsson}
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafsson
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafsson/*
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafsson * show_startup_info:
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafsson * Show name/version
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafsson */
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafssonvoid
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafssonshow_startup_info(void) {
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafsson printf("\n"
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafsson"DNS Query Performance Testing Tool\n"
9db88ce113701a6fb600a67aaa65764d800e25b0Mark Andrews"Version: $Id: queryperf.c,v 1.12 2007/09/05 07:36:04 marka Exp $\n"
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafsson"\n");
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafsson}
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafsson
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafsson/*
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafsson * show_usage:
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafsson * Print out usage/syntax information
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafsson */
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafssonvoid
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafssonshow_usage(void) {
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafsson fprintf(stderr,
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafsson"\n"
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafsson"Usage: queryperf [-d datafile] [-s server_addr] [-p port] [-q num_queries]\n"
f620c5e527746a2ec3d90a11d21abd8a114746dfTatuya JINMEI 神明達哉" [-b bufsize] [-t timeout] [-n] [-l limit] [-f family] [-1]\n"
b89095ba15e9163f2bacc46239d8f69d1a2b9ad3Tatuya JINMEI 神明達哉" [-i interval] [-r arraysize] [-u unit] [-H histfile]\n"
66f51d8678c8fcc7256354b599317f67455120fdMark Andrews" [-T qps] [-e] [-D] [-R] [-c] [-v] [-h]\n"
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafsson" -d specifies the input data file (default: stdin)\n"
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafsson" -s sets the server to query (default: %s)\n"
f620c5e527746a2ec3d90a11d21abd8a114746dfTatuya JINMEI 神明達哉" -p sets the port on which to query the server (default: %s)\n"
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafsson" -q specifies the maximum number of queries outstanding (default: %d)\n"
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafsson" -t specifies the timeout for query completion in seconds (default: %d)\n"
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafsson" -n causes configuration changes to be ignored\n"
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafsson" -l specifies how a limit for how long to run tests in seconds (no default)\n"
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafsson" -1 run through input only once (default: multiple iff limit given)\n"
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafsson" -b set input/output buffer size in kilobytes (default: %d k)\n"
b89095ba15e9163f2bacc46239d8f69d1a2b9ad3Tatuya JINMEI 神明達哉" -i specifies interval of intermediate outputs in seconds (default: 0=none)\n"
f620c5e527746a2ec3d90a11d21abd8a114746dfTatuya JINMEI 神明達哉" -f specify address family of DNS transport, inet or inet6 (default: any)\n"
b89095ba15e9163f2bacc46239d8f69d1a2b9ad3Tatuya JINMEI 神明達哉" -r set RTT statistics array size (default: %d)\n"
b89095ba15e9163f2bacc46239d8f69d1a2b9ad3Tatuya JINMEI 神明達哉" -u set RTT statistics time unit in usec (default: %d)\n"
b89095ba15e9163f2bacc46239d8f69d1a2b9ad3Tatuya JINMEI 神明達哉" -H specifies RTT histogram data file (default: none)\n"
b89095ba15e9163f2bacc46239d8f69d1a2b9ad3Tatuya JINMEI 神明達哉" -T specify the target qps (default: 0=unspecified)\n"
8ac1acc30d0f405222ffa7b2b93131d9d4e18599Mark Andrews" -e enable EDNS 0\n"
8ac1acc30d0f405222ffa7b2b93131d9d4e18599Mark Andrews" -D set the DNSSEC OK bit (implies EDNS)\n"
66f51d8678c8fcc7256354b599317f67455120fdMark Andrews" -R disable recursion\n"
e2cf0e8ff9ff37121518af5b34b9e4de7abbb47cMark Andrews" -c print the number of packets with each rcode\n"
81e805888bcd4bd2a6d5c986fade6ffdf7d88a54Andreas Gustafsson" -v verbose: report the RCODE of each response on stdout\n"
bd3946db298667e769804b2e978df2d6dcd85e29Mark Andrews" -h print this usage\n"
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafsson"\n",
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafsson DEF_SERVER_TO_QUERY, DEF_SERVER_PORT,
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafsson DEF_MAX_QUERIES_OUTSTANDING, DEF_QUERY_TIMEOUT,
b89095ba15e9163f2bacc46239d8f69d1a2b9ad3Tatuya JINMEI 神明達哉 DEF_BUFFER_SIZE, DEF_RTTARRAY_SIZE, DEF_RTTARRAY_UNIT);
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafsson}
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafsson
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafsson/*
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafsson * set_datafile:
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafsson * Set the datafile to read
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafsson *
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafsson * Return -1 on failure
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafsson * Return a non-negative integer otherwise
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafsson */
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafssonint
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafssonset_datafile(char *new_file) {
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafsson char *dfname_tmp;
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafsson
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafsson if ((new_file == NULL) || (new_file[0] == '\0')) {
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafsson fprintf(stderr, "Error: null datafile name\n");
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafsson return (-1);
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafsson }
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafsson
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafsson if ((dfname_tmp = malloc(strlen(new_file) + 1)) == NULL) {
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafsson fprintf(stderr, "Error allocating memory for datafile name: "
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafsson "%s\n", new_file);
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafsson return (-1);
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafsson }
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafsson
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafsson free(datafile_name);
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafsson datafile_name = dfname_tmp;
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafsson
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafsson strcpy(datafile_name, new_file);
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafsson use_stdin = FALSE;
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafsson
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafsson return (0);
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafsson}
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafsson
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafsson/*
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafsson * set_input_stdin:
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafsson * Set the input to be stdin (instead of a datafile)
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafsson */
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafssonvoid
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafssonset_input_stdin(void) {
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafsson use_stdin = TRUE;
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafsson free(datafile_name);
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafsson datafile_name = NULL;
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafsson}
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafsson
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafsson/*
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafsson * set_server:
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafsson * Set the server to be queried
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafsson *
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafsson * Return -1 on failure
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafsson * Return a non-negative integer otherwise
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafsson */
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafssonint
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafssonset_server(char *new_name) {
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafsson static struct hostent *server_he;
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafsson
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafsson /* If no change in server name, don't do anything... */
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafsson if ((server_to_query != NULL) && (new_name != NULL))
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafsson if (strcmp(new_name, server_to_query) == 0)
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafsson return (0);
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafsson
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafsson if ((new_name == NULL) || (new_name[0] == '\0')) {
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafsson fprintf(stderr, "Error: null server name\n");
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafsson return (-1);
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafsson }
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafsson
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafsson free(server_to_query);
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafsson server_to_query = NULL;
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafsson
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafsson if ((server_to_query = malloc(strlen(new_name) + 1)) == NULL) {
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafsson fprintf(stderr, "Error allocating memory for server name: "
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafsson "%s\n", new_name);
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafsson return (-1);
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafsson }
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafsson
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafsson strcpy(server_to_query, new_name);
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafsson
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafsson return (0);
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafsson}
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafsson
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafsson/*
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafsson * set_server_port:
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafsson * Set the port on which to contact the server
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafsson *
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafsson * Return -1 if port is invalid
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafsson * Return a non-negative integer otherwise
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafsson */
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafssonint
f620c5e527746a2ec3d90a11d21abd8a114746dfTatuya JINMEI 神明達哉set_server_port(char *new_port) {
f620c5e527746a2ec3d90a11d21abd8a114746dfTatuya JINMEI 神明達哉 unsigned int uint_val;
f620c5e527746a2ec3d90a11d21abd8a114746dfTatuya JINMEI 神明達哉
f620c5e527746a2ec3d90a11d21abd8a114746dfTatuya JINMEI 神明達哉 if ((is_uint(new_port, &uint_val)) != TRUE)
f620c5e527746a2ec3d90a11d21abd8a114746dfTatuya JINMEI 神明達哉 return (-1);
f620c5e527746a2ec3d90a11d21abd8a114746dfTatuya JINMEI 神明達哉
f620c5e527746a2ec3d90a11d21abd8a114746dfTatuya JINMEI 神明達哉 if (uint_val && uint_val > MAX_PORT)
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafsson return (-1);
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafsson else {
f620c5e527746a2ec3d90a11d21abd8a114746dfTatuya JINMEI 神明達哉 if (server_port != NULL && new_port != NULL &&
f620c5e527746a2ec3d90a11d21abd8a114746dfTatuya JINMEI 神明達哉 strcmp(server_port, new_port) == 0)
f620c5e527746a2ec3d90a11d21abd8a114746dfTatuya JINMEI 神明達哉 return (0);
f620c5e527746a2ec3d90a11d21abd8a114746dfTatuya JINMEI 神明達哉
f620c5e527746a2ec3d90a11d21abd8a114746dfTatuya JINMEI 神明達哉 free(server_port);
f620c5e527746a2ec3d90a11d21abd8a114746dfTatuya JINMEI 神明達哉 server_port = NULL;
f620c5e527746a2ec3d90a11d21abd8a114746dfTatuya JINMEI 神明達哉
f620c5e527746a2ec3d90a11d21abd8a114746dfTatuya JINMEI 神明達哉 if ((server_port = malloc(strlen(new_port) + 1)) == NULL) {
f620c5e527746a2ec3d90a11d21abd8a114746dfTatuya JINMEI 神明達哉 fprintf(stderr,
f620c5e527746a2ec3d90a11d21abd8a114746dfTatuya JINMEI 神明達哉 "Error allocating memory for server port: "
f620c5e527746a2ec3d90a11d21abd8a114746dfTatuya JINMEI 神明達哉 "%s\n", new_port);
f620c5e527746a2ec3d90a11d21abd8a114746dfTatuya JINMEI 神明達哉 return (-1);
f620c5e527746a2ec3d90a11d21abd8a114746dfTatuya JINMEI 神明達哉 }
f620c5e527746a2ec3d90a11d21abd8a114746dfTatuya JINMEI 神明達哉
f620c5e527746a2ec3d90a11d21abd8a114746dfTatuya JINMEI 神明達哉 strcpy(server_port, new_port);
f620c5e527746a2ec3d90a11d21abd8a114746dfTatuya JINMEI 神明達哉
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafsson return (0);
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafsson }
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafsson}
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafsson
f620c5e527746a2ec3d90a11d21abd8a114746dfTatuya JINMEI 神明達哉int
f620c5e527746a2ec3d90a11d21abd8a114746dfTatuya JINMEI 神明達哉set_server_sa(void) {
f620c5e527746a2ec3d90a11d21abd8a114746dfTatuya JINMEI 神明達哉 struct addrinfo hints, *res;
f620c5e527746a2ec3d90a11d21abd8a114746dfTatuya JINMEI 神明達哉 static struct protoent *proto;
f620c5e527746a2ec3d90a11d21abd8a114746dfTatuya JINMEI 神明達哉 int error;
f620c5e527746a2ec3d90a11d21abd8a114746dfTatuya JINMEI 神明達哉
f620c5e527746a2ec3d90a11d21abd8a114746dfTatuya JINMEI 神明達哉 if (proto == NULL && (proto = getprotobyname("udp")) == NULL) {
f620c5e527746a2ec3d90a11d21abd8a114746dfTatuya JINMEI 神明達哉 fprintf(stderr, "Error: getprotobyname call failed");
f620c5e527746a2ec3d90a11d21abd8a114746dfTatuya JINMEI 神明達哉 return (-1);
f620c5e527746a2ec3d90a11d21abd8a114746dfTatuya JINMEI 神明達哉 }
f620c5e527746a2ec3d90a11d21abd8a114746dfTatuya JINMEI 神明達哉
f620c5e527746a2ec3d90a11d21abd8a114746dfTatuya JINMEI 神明達哉 memset(&hints, 0, sizeof(hints));
f620c5e527746a2ec3d90a11d21abd8a114746dfTatuya JINMEI 神明達哉 hints.ai_family = family;
f620c5e527746a2ec3d90a11d21abd8a114746dfTatuya JINMEI 神明達哉 hints.ai_socktype = SOCK_DGRAM;
f620c5e527746a2ec3d90a11d21abd8a114746dfTatuya JINMEI 神明達哉 hints.ai_protocol = proto->p_proto;
f620c5e527746a2ec3d90a11d21abd8a114746dfTatuya JINMEI 神明達哉 if ((error = getaddrinfo(server_to_query, server_port,
f620c5e527746a2ec3d90a11d21abd8a114746dfTatuya JINMEI 神明達哉 &hints, &res)) != 0) {
f620c5e527746a2ec3d90a11d21abd8a114746dfTatuya JINMEI 神明達哉 fprintf(stderr, "Error: getaddrinfo(%s, %s) failed\n",
f620c5e527746a2ec3d90a11d21abd8a114746dfTatuya JINMEI 神明達哉 server_to_query, server_port);
f620c5e527746a2ec3d90a11d21abd8a114746dfTatuya JINMEI 神明達哉 return (-1);
f620c5e527746a2ec3d90a11d21abd8a114746dfTatuya JINMEI 神明達哉 }
f620c5e527746a2ec3d90a11d21abd8a114746dfTatuya JINMEI 神明達哉
f620c5e527746a2ec3d90a11d21abd8a114746dfTatuya JINMEI 神明達哉 /* replace the server's addrinfo */
f620c5e527746a2ec3d90a11d21abd8a114746dfTatuya JINMEI 神明達哉 if (server_ai != NULL)
f620c5e527746a2ec3d90a11d21abd8a114746dfTatuya JINMEI 神明達哉 freeaddrinfo(server_ai);
f620c5e527746a2ec3d90a11d21abd8a114746dfTatuya JINMEI 神明達哉 server_ai = res;
f620c5e527746a2ec3d90a11d21abd8a114746dfTatuya JINMEI 神明達哉 return (0);
f620c5e527746a2ec3d90a11d21abd8a114746dfTatuya JINMEI 神明達哉}
f620c5e527746a2ec3d90a11d21abd8a114746dfTatuya JINMEI 神明達哉
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafsson/*
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafsson * is_digit:
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafsson * Tests if a character is a digit
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafsson *
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafsson * Return TRUE if it is
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafsson * Return FALSE if it is not
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafsson */
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafssonint
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafssonis_digit(char d) {
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafsson if (d < '0' || d > '9')
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafsson return (FALSE);
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafsson else
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafsson return (TRUE);
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafsson}
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafsson
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafsson/*
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafsson * is_uint:
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafsson * Tests if a string, test_int, is a valid unsigned integer
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafsson *
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafsson * Sets *result to be the unsigned integer if it is valid
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafsson *
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafsson * Return TRUE if it is
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafsson * Return FALSE if it is not
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafsson */
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafssonint
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafssonis_uint(char *test_int, unsigned int *result) {
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafsson unsigned long int value;
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafsson char *end;
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafsson
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafsson if (test_int == NULL)
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafsson return (FALSE);
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafsson
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafsson if (is_digit(test_int[0]) == FALSE)
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafsson return (FALSE);
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafsson
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafsson value = strtoul(test_int, &end, 10);
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafsson
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafsson if ((errno == ERANGE) || (*end != '\0') || (value > UINT_MAX))
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafsson return (FALSE);
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafsson
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafsson *result = (unsigned int)value;
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafsson return (TRUE);
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafsson}
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafsson
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafsson/*
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafsson * set_max_queries:
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafsson * Set the maximum number of outstanding queries
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafsson *
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafsson * Returns -1 on failure
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafsson * Returns a non-negative integer otherwise
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafsson */
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafssonint
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafssonset_max_queries(unsigned int new_max) {
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafsson static unsigned int size_qs = sizeof(struct query_status);
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafsson struct query_status *temp_stat;
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafsson unsigned int count;
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafsson
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafsson if (new_max > query_status_allocated) {
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafsson temp_stat = realloc(status, new_max * size_qs);
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafsson
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafsson if (temp_stat == NULL) {
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafsson fprintf(stderr, "Error resizing query_status\n");
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafsson return (-1);
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafsson } else {
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafsson status = temp_stat;
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafsson
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafsson /*
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafsson * Be careful to only initialise between above
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafsson * the previously allocated space. Note that the
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafsson * allocation may be larger than the current
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafsson * max_queries_outstanding. We don't want to
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafsson * "forget" any outstanding queries! We might
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafsson * still have some above the bounds of the max.
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafsson */
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafsson count = query_status_allocated;
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafsson for (; count < new_max; count++) {
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafsson status[count].in_use = FALSE;
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafsson status[count].magic = QUERY_STATUS_MAGIC;
81e805888bcd4bd2a6d5c986fade6ffdf7d88a54Andreas Gustafsson status[count].desc = NULL;
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafsson }
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafsson
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafsson query_status_allocated = new_max;
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafsson }
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafsson }
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafsson
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafsson max_queries_outstanding = new_max;
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafsson
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafsson return (0);
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafsson}
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafsson
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafsson/*
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafsson * parse_args:
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafsson * Parse program arguments and set configuration options
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafsson *
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafsson * Return -1 on failure
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafsson * Return a non-negative integer otherwise
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafsson */
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafssonint
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafssonparse_args(int argc, char **argv) {
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafsson int c;
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafsson unsigned int uint_arg_val;
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafsson
b89095ba15e9163f2bacc46239d8f69d1a2b9ad3Tatuya JINMEI 神明達哉 while ((c = getopt(argc, argv,
7d8bfa8f8ea4c222b9403fe97dba0523d768f667Mark Andrews "f:q:t:i:nd:s:p:1l:b:eDcvr:RT:u:H:h")) != -1) {
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafsson switch (c) {
f620c5e527746a2ec3d90a11d21abd8a114746dfTatuya JINMEI 神明達哉 case 'f':
f620c5e527746a2ec3d90a11d21abd8a114746dfTatuya JINMEI 神明達哉 if (strcmp(optarg, "inet") == 0)
f620c5e527746a2ec3d90a11d21abd8a114746dfTatuya JINMEI 神明達哉 family = AF_INET;
f620c5e527746a2ec3d90a11d21abd8a114746dfTatuya JINMEI 神明達哉#ifdef AF_INET6
f620c5e527746a2ec3d90a11d21abd8a114746dfTatuya JINMEI 神明達哉 else if (strcmp(optarg, "inet6") == 0)
f620c5e527746a2ec3d90a11d21abd8a114746dfTatuya JINMEI 神明達哉 family = AF_INET6;
f620c5e527746a2ec3d90a11d21abd8a114746dfTatuya JINMEI 神明達哉#endif
f620c5e527746a2ec3d90a11d21abd8a114746dfTatuya JINMEI 神明達哉 else if (strcmp(optarg, "any") == 0)
f620c5e527746a2ec3d90a11d21abd8a114746dfTatuya JINMEI 神明達哉 family = AF_UNSPEC;
f620c5e527746a2ec3d90a11d21abd8a114746dfTatuya JINMEI 神明達哉 else {
f620c5e527746a2ec3d90a11d21abd8a114746dfTatuya JINMEI 神明達哉 fprintf(stderr, "Invalid address family: %s\n",
f620c5e527746a2ec3d90a11d21abd8a114746dfTatuya JINMEI 神明達哉 optarg);
f620c5e527746a2ec3d90a11d21abd8a114746dfTatuya JINMEI 神明達哉 return (-1);
f620c5e527746a2ec3d90a11d21abd8a114746dfTatuya JINMEI 神明達哉 }
f620c5e527746a2ec3d90a11d21abd8a114746dfTatuya JINMEI 神明達哉 break;
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafsson case 'q':
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafsson if (is_uint(optarg, &uint_arg_val) == TRUE) {
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafsson set_max_queries(uint_arg_val);
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafsson queriesset = TRUE;
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafsson } else {
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafsson fprintf(stderr, "Option requires a positive "
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafsson "integer value: -%c %s\n",
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafsson c, optarg);
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafsson return (-1);
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafsson }
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafsson break;
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafsson
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafsson case 't':
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafsson if (is_uint(optarg, &uint_arg_val) == TRUE) {
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafsson query_timeout = uint_arg_val;
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafsson timeoutset = TRUE;
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafsson } else {
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafsson fprintf(stderr, "Option requires a positive "
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafsson "integer value: -%c %s\n",
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafsson c, optarg);
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafsson return (-1);
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafsson }
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafsson break;
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafsson
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafsson case 'n':
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafsson ignore_config_changes = TRUE;
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafsson break;
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafsson
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafsson case 'd':
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafsson if (set_datafile(optarg) == -1) {
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafsson fprintf(stderr, "Error setting datafile "
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafsson "name: %s\n", optarg);
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafsson return (-1);
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafsson }
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafsson break;
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafsson
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafsson case 's':
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafsson if (set_server(optarg) == -1) {
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafsson fprintf(stderr, "Error setting server "
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafsson "name: %s\n", optarg);
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafsson return (-1);
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafsson }
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafsson serverset = TRUE;
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafsson break;
b89095ba15e9163f2bacc46239d8f69d1a2b9ad3Tatuya JINMEI 神明達哉
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafsson case 'p':
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafsson if (is_uint(optarg, &uint_arg_val) == TRUE &&
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafsson uint_arg_val < MAX_PORT)
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafsson {
f620c5e527746a2ec3d90a11d21abd8a114746dfTatuya JINMEI 神明達哉 set_server_port(optarg);
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafsson portset = TRUE;
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafsson } else {
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafsson fprintf(stderr, "Option requires a positive "
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafsson "integer between 0 and %d: -%c %s\n",
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafsson MAX_PORT - 1, c, optarg);
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafsson return (-1);
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafsson }
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafsson break;
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafsson
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafsson case '1':
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafsson run_only_once = TRUE;
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafsson break;
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafsson
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafsson case 'l':
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafsson if (is_uint(optarg, &uint_arg_val) == TRUE) {
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafsson use_timelimit = TRUE;
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafsson run_timelimit = uint_arg_val;
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafsson } else {
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafsson fprintf(stderr, "Option requires a positive "
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafsson "integer: -%c %s\n",
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafsson c, optarg);
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafsson return (-1);
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafsson }
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafsson break;
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafsson
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafsson case 'b':
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafsson if (is_uint(optarg, &uint_arg_val) == TRUE) {
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafsson socket_bufsize = uint_arg_val;
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafsson } else {
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafsson fprintf(stderr, "Option requires a positive "
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafsson "integer: -%c %s\n",
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafsson c, optarg);
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafsson return (-1);
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafsson }
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafsson break;
8ac1acc30d0f405222ffa7b2b93131d9d4e18599Mark Andrews case 'e':
8ac1acc30d0f405222ffa7b2b93131d9d4e18599Mark Andrews edns = TRUE;
8ac1acc30d0f405222ffa7b2b93131d9d4e18599Mark Andrews break;
8ac1acc30d0f405222ffa7b2b93131d9d4e18599Mark Andrews case 'D':
8ac1acc30d0f405222ffa7b2b93131d9d4e18599Mark Andrews dnssec = TRUE;
8ac1acc30d0f405222ffa7b2b93131d9d4e18599Mark Andrews edns = TRUE;
8ac1acc30d0f405222ffa7b2b93131d9d4e18599Mark Andrews break;
e2cf0e8ff9ff37121518af5b34b9e4de7abbb47cMark Andrews case 'c':
e2cf0e8ff9ff37121518af5b34b9e4de7abbb47cMark Andrews countrcodes = TRUE;
e2cf0e8ff9ff37121518af5b34b9e4de7abbb47cMark Andrews break;
81e805888bcd4bd2a6d5c986fade6ffdf7d88a54Andreas Gustafsson case 'v':
81e805888bcd4bd2a6d5c986fade6ffdf7d88a54Andreas Gustafsson verbose = 1;
81e805888bcd4bd2a6d5c986fade6ffdf7d88a54Andreas Gustafsson break;
b89095ba15e9163f2bacc46239d8f69d1a2b9ad3Tatuya JINMEI 神明達哉 case 'i':
b89095ba15e9163f2bacc46239d8f69d1a2b9ad3Tatuya JINMEI 神明達哉 if (is_uint(optarg, &uint_arg_val) == TRUE)
b89095ba15e9163f2bacc46239d8f69d1a2b9ad3Tatuya JINMEI 神明達哉 print_interval = uint_arg_val;
b89095ba15e9163f2bacc46239d8f69d1a2b9ad3Tatuya JINMEI 神明達哉 else {
b89095ba15e9163f2bacc46239d8f69d1a2b9ad3Tatuya JINMEI 神明達哉 fprintf(stderr, "Invalid interval: %s\n",
b89095ba15e9163f2bacc46239d8f69d1a2b9ad3Tatuya JINMEI 神明達哉 optarg);
b89095ba15e9163f2bacc46239d8f69d1a2b9ad3Tatuya JINMEI 神明達哉 return (-1);
b89095ba15e9163f2bacc46239d8f69d1a2b9ad3Tatuya JINMEI 神明達哉 }
b89095ba15e9163f2bacc46239d8f69d1a2b9ad3Tatuya JINMEI 神明達哉 break;
66f51d8678c8fcc7256354b599317f67455120fdMark Andrews case 'R':
66f51d8678c8fcc7256354b599317f67455120fdMark Andrews recurse = 0;
66f51d8678c8fcc7256354b599317f67455120fdMark Andrews break;
b89095ba15e9163f2bacc46239d8f69d1a2b9ad3Tatuya JINMEI 神明達哉 case 'r':
b89095ba15e9163f2bacc46239d8f69d1a2b9ad3Tatuya JINMEI 神明達哉 if (is_uint(optarg, &uint_arg_val) == TRUE)
b89095ba15e9163f2bacc46239d8f69d1a2b9ad3Tatuya JINMEI 神明達哉 rttarray_size = uint_arg_val;
b89095ba15e9163f2bacc46239d8f69d1a2b9ad3Tatuya JINMEI 神明達哉 else {
b89095ba15e9163f2bacc46239d8f69d1a2b9ad3Tatuya JINMEI 神明達哉 fprintf(stderr, "Invalid RTT array size: %s\n",
b89095ba15e9163f2bacc46239d8f69d1a2b9ad3Tatuya JINMEI 神明達哉 optarg);
b89095ba15e9163f2bacc46239d8f69d1a2b9ad3Tatuya JINMEI 神明達哉 return (-1);
b89095ba15e9163f2bacc46239d8f69d1a2b9ad3Tatuya JINMEI 神明達哉 }
b89095ba15e9163f2bacc46239d8f69d1a2b9ad3Tatuya JINMEI 神明達哉 break;
b89095ba15e9163f2bacc46239d8f69d1a2b9ad3Tatuya JINMEI 神明達哉 case 'u':
b89095ba15e9163f2bacc46239d8f69d1a2b9ad3Tatuya JINMEI 神明達哉 if (is_uint(optarg, &uint_arg_val) == TRUE)
b89095ba15e9163f2bacc46239d8f69d1a2b9ad3Tatuya JINMEI 神明達哉 rttarray_unit = uint_arg_val;
b89095ba15e9163f2bacc46239d8f69d1a2b9ad3Tatuya JINMEI 神明達哉 else {
b89095ba15e9163f2bacc46239d8f69d1a2b9ad3Tatuya JINMEI 神明達哉 fprintf(stderr, "Invalid RTT unit: %s\n",
b89095ba15e9163f2bacc46239d8f69d1a2b9ad3Tatuya JINMEI 神明達哉 optarg);
b89095ba15e9163f2bacc46239d8f69d1a2b9ad3Tatuya JINMEI 神明達哉 return (-1);
b89095ba15e9163f2bacc46239d8f69d1a2b9ad3Tatuya JINMEI 神明達哉 }
b89095ba15e9163f2bacc46239d8f69d1a2b9ad3Tatuya JINMEI 神明達哉 break;
b89095ba15e9163f2bacc46239d8f69d1a2b9ad3Tatuya JINMEI 神明達哉 case 'H':
b89095ba15e9163f2bacc46239d8f69d1a2b9ad3Tatuya JINMEI 神明達哉 rtt_histogram_file = optarg;
b89095ba15e9163f2bacc46239d8f69d1a2b9ad3Tatuya JINMEI 神明達哉 break;
b89095ba15e9163f2bacc46239d8f69d1a2b9ad3Tatuya JINMEI 神明達哉 case 'T':
b89095ba15e9163f2bacc46239d8f69d1a2b9ad3Tatuya JINMEI 神明達哉 if (is_uint(optarg, &uint_arg_val) == TRUE)
b89095ba15e9163f2bacc46239d8f69d1a2b9ad3Tatuya JINMEI 神明達哉 target_qps = uint_arg_val;
b89095ba15e9163f2bacc46239d8f69d1a2b9ad3Tatuya JINMEI 神明達哉 else {
b89095ba15e9163f2bacc46239d8f69d1a2b9ad3Tatuya JINMEI 神明達哉 fprintf(stderr, "Invalid target qps: %s\n",
b89095ba15e9163f2bacc46239d8f69d1a2b9ad3Tatuya JINMEI 神明達哉 optarg);
b89095ba15e9163f2bacc46239d8f69d1a2b9ad3Tatuya JINMEI 神明達哉 return (-1);
b89095ba15e9163f2bacc46239d8f69d1a2b9ad3Tatuya JINMEI 神明達哉 }
b89095ba15e9163f2bacc46239d8f69d1a2b9ad3Tatuya JINMEI 神明達哉 break;
bd3946db298667e769804b2e978df2d6dcd85e29Mark Andrews case 'h':
bd3946db298667e769804b2e978df2d6dcd85e29Mark Andrews return (-1);
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafsson default:
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafsson fprintf(stderr, "Invalid option: -%c\n", optopt);
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafsson return (-1);
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafsson }
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafsson }
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafsson
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafsson if (run_only_once == FALSE && use_timelimit == FALSE)
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafsson run_only_once = TRUE;
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafsson
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafsson return (0);
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafsson}
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafsson
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafsson/*
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafsson * open_datafile:
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafsson * Open the data file ready for reading
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafsson *
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafsson * Return -1 on failure
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafsson * Return non-negative integer on success
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafsson */
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafssonint
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafssonopen_datafile(void) {
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafsson if (use_stdin == TRUE) {
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafsson datafile_ptr = stdin;
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafsson return (0);
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafsson } else {
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafsson if ((datafile_ptr = fopen(datafile_name, "r")) == NULL) {
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafsson fprintf(stderr, "Error: unable to open datafile: %s\n",
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafsson datafile_name);
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafsson return (-1);
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafsson } else
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafsson return (0);
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafsson }
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafsson}
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafsson
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafsson/*
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafsson * close_datafile:
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafsson * Close the data file if any is open
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafsson *
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafsson * Return -1 on failure
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafsson * Return non-negative integer on success, including if not needed
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafsson */
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafssonint
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafssonclose_datafile(void) {
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafsson if ((use_stdin == FALSE) && (datafile_ptr != NULL)) {
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafsson if (fclose(datafile_ptr) != 0) {
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafsson fprintf(stderr, "Error: unable to close datafile\n");
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafsson return (-1);
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafsson }
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafsson }
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafsson
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafsson return (0);
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafsson}
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafsson
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafsson/*
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafsson * open_socket:
f620c5e527746a2ec3d90a11d21abd8a114746dfTatuya JINMEI 神明達哉 * Open a socket for the queries. When we have an active socket already,
f620c5e527746a2ec3d90a11d21abd8a114746dfTatuya JINMEI 神明達哉 * close it and open a new one.
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafsson *
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafsson * Return -1 on failure
f620c5e527746a2ec3d90a11d21abd8a114746dfTatuya JINMEI 神明達哉 * Return the socket identifier
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafsson */
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafssonint
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafssonopen_socket(void) {
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafsson int sock;
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafsson int ret;
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafsson int bufsize;
f620c5e527746a2ec3d90a11d21abd8a114746dfTatuya JINMEI 神明達哉 struct addrinfo hints, *res;
f620c5e527746a2ec3d90a11d21abd8a114746dfTatuya JINMEI 神明達哉
f620c5e527746a2ec3d90a11d21abd8a114746dfTatuya JINMEI 神明達哉 memset(&hints, 0, sizeof(hints));
f620c5e527746a2ec3d90a11d21abd8a114746dfTatuya JINMEI 神明達哉 hints.ai_family = server_ai->ai_family;
f620c5e527746a2ec3d90a11d21abd8a114746dfTatuya JINMEI 神明達哉 hints.ai_socktype = server_ai->ai_socktype;
f620c5e527746a2ec3d90a11d21abd8a114746dfTatuya JINMEI 神明達哉 hints.ai_protocol = server_ai->ai_protocol;
f620c5e527746a2ec3d90a11d21abd8a114746dfTatuya JINMEI 神明達哉 hints.ai_flags = AI_PASSIVE;
f620c5e527746a2ec3d90a11d21abd8a114746dfTatuya JINMEI 神明達哉
f620c5e527746a2ec3d90a11d21abd8a114746dfTatuya JINMEI 神明達哉 if ((ret = getaddrinfo(NULL, "0", &hints, &res)) != 0) {
f620c5e527746a2ec3d90a11d21abd8a114746dfTatuya JINMEI 神明達哉 fprintf(stderr,
f620c5e527746a2ec3d90a11d21abd8a114746dfTatuya JINMEI 神明達哉 "Error: getaddrinfo for bind socket failed: %s\n",
f620c5e527746a2ec3d90a11d21abd8a114746dfTatuya JINMEI 神明達哉 gai_strerror(ret));
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafsson return (-1);
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafsson }
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafsson
f620c5e527746a2ec3d90a11d21abd8a114746dfTatuya JINMEI 神明達哉 if ((sock = socket(res->ai_family, SOCK_DGRAM,
f620c5e527746a2ec3d90a11d21abd8a114746dfTatuya JINMEI 神明達哉 res->ai_protocol)) == -1) {
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafsson fprintf(stderr, "Error: socket call failed");
f620c5e527746a2ec3d90a11d21abd8a114746dfTatuya JINMEI 神明達哉 goto fail;
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafsson }
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafsson
f620c5e527746a2ec3d90a11d21abd8a114746dfTatuya JINMEI 神明達哉#if defined(AF_INET6) && defined(IPV6_V6ONLY)
f620c5e527746a2ec3d90a11d21abd8a114746dfTatuya JINMEI 神明達哉 if (res->ai_family == AF_INET6) {
f620c5e527746a2ec3d90a11d21abd8a114746dfTatuya JINMEI 神明達哉 int on = 1;
f620c5e527746a2ec3d90a11d21abd8a114746dfTatuya JINMEI 神明達哉
f620c5e527746a2ec3d90a11d21abd8a114746dfTatuya JINMEI 神明達哉 if (setsockopt(sock, IPPROTO_IPV6, IPV6_V6ONLY,
f620c5e527746a2ec3d90a11d21abd8a114746dfTatuya JINMEI 神明達哉 &on, sizeof(on)) == -1) {
f620c5e527746a2ec3d90a11d21abd8a114746dfTatuya JINMEI 神明達哉 fprintf(stderr,
f620c5e527746a2ec3d90a11d21abd8a114746dfTatuya JINMEI 神明達哉 "Warning: setsockopt(IPV6_V6ONLY) failed\n");
f620c5e527746a2ec3d90a11d21abd8a114746dfTatuya JINMEI 神明達哉 }
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafsson }
f620c5e527746a2ec3d90a11d21abd8a114746dfTatuya JINMEI 神明達哉#endif
f620c5e527746a2ec3d90a11d21abd8a114746dfTatuya JINMEI 神明達哉
f620c5e527746a2ec3d90a11d21abd8a114746dfTatuya JINMEI 神明達哉 if (bind(sock, res->ai_addr, res->ai_addrlen) == -1)
f620c5e527746a2ec3d90a11d21abd8a114746dfTatuya JINMEI 神明達哉 fprintf(stderr, "Error: bind call failed");
f620c5e527746a2ec3d90a11d21abd8a114746dfTatuya JINMEI 神明達哉
f620c5e527746a2ec3d90a11d21abd8a114746dfTatuya JINMEI 神明達哉 freeaddrinfo(res);
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafsson
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafsson bufsize = 1024 * socket_bufsize;
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafsson
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafsson ret = setsockopt(sock, SOL_SOCKET, SO_RCVBUF,
1123872a47604644f6d74d7d8929472201db96f3Andreas Gustafsson (char *) &bufsize, sizeof(bufsize));
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafsson if (ret < 0)
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafsson fprintf(stderr, "Warning: setsockbuf(SO_RCVBUF) failed\n");
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafsson
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafsson ret = setsockopt(sock, SOL_SOCKET, SO_SNDBUF,
1123872a47604644f6d74d7d8929472201db96f3Andreas Gustafsson (char *) &bufsize, sizeof(bufsize));
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafsson if (ret < 0)
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafsson fprintf(stderr, "Warning: setsockbuf(SO_SNDBUF) failed\n");
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafsson
f620c5e527746a2ec3d90a11d21abd8a114746dfTatuya JINMEI 神明達哉 return (sock);
f620c5e527746a2ec3d90a11d21abd8a114746dfTatuya JINMEI 神明達哉
f620c5e527746a2ec3d90a11d21abd8a114746dfTatuya JINMEI 神明達哉 fail:
f620c5e527746a2ec3d90a11d21abd8a114746dfTatuya JINMEI 神明達哉 if (res)
f620c5e527746a2ec3d90a11d21abd8a114746dfTatuya JINMEI 神明達哉 freeaddrinfo(res);
f620c5e527746a2ec3d90a11d21abd8a114746dfTatuya JINMEI 神明達哉 return (-1);
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafsson}
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafsson
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafsson/*
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafsson * close_socket:
f620c5e527746a2ec3d90a11d21abd8a114746dfTatuya JINMEI 神明達哉 * Close the query socket(s)
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafsson *
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafsson * Return -1 on failure
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafsson * Return a non-negative integer otherwise
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafsson */
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafssonint
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafssonclose_socket(void) {
f620c5e527746a2ec3d90a11d21abd8a114746dfTatuya JINMEI 神明達哉 if (socket4 != -1) {
f620c5e527746a2ec3d90a11d21abd8a114746dfTatuya JINMEI 神明達哉 if (close(socket4) != 0) {
f620c5e527746a2ec3d90a11d21abd8a114746dfTatuya JINMEI 神明達哉 fprintf(stderr,
f620c5e527746a2ec3d90a11d21abd8a114746dfTatuya JINMEI 神明達哉 "Error: unable to close IPv4 socket\n");
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafsson return (-1);
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafsson }
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafsson }
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafsson
f620c5e527746a2ec3d90a11d21abd8a114746dfTatuya JINMEI 神明達哉 if (socket6 != -1) {
f620c5e527746a2ec3d90a11d21abd8a114746dfTatuya JINMEI 神明達哉 if (close(socket6) != 0) {
f620c5e527746a2ec3d90a11d21abd8a114746dfTatuya JINMEI 神明達哉 fprintf(stderr,
f620c5e527746a2ec3d90a11d21abd8a114746dfTatuya JINMEI 神明達哉 "Error: unable to close IPv6 socket\n");
f620c5e527746a2ec3d90a11d21abd8a114746dfTatuya JINMEI 神明達哉 return (-1);
f620c5e527746a2ec3d90a11d21abd8a114746dfTatuya JINMEI 神明達哉 }
f620c5e527746a2ec3d90a11d21abd8a114746dfTatuya JINMEI 神明達哉 }
f620c5e527746a2ec3d90a11d21abd8a114746dfTatuya JINMEI 神明達哉
f620c5e527746a2ec3d90a11d21abd8a114746dfTatuya JINMEI 神明達哉 query_socket = -1;
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafsson
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafsson return (0);
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafsson}
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafsson
f620c5e527746a2ec3d90a11d21abd8a114746dfTatuya JINMEI 神明達哉/*
f620c5e527746a2ec3d90a11d21abd8a114746dfTatuya JINMEI 神明達哉 * change_socket:
f620c5e527746a2ec3d90a11d21abd8a114746dfTatuya JINMEI 神明達哉 * Choose an appropriate socket according to the address family of the
f620c5e527746a2ec3d90a11d21abd8a114746dfTatuya JINMEI 神明達哉 * current server. Open a new socket if necessary.
f620c5e527746a2ec3d90a11d21abd8a114746dfTatuya JINMEI 神明達哉 *
f620c5e527746a2ec3d90a11d21abd8a114746dfTatuya JINMEI 神明達哉 * Return -1 on failure
f620c5e527746a2ec3d90a11d21abd8a114746dfTatuya JINMEI 神明達哉 * Return the socket identifier
f620c5e527746a2ec3d90a11d21abd8a114746dfTatuya JINMEI 神明達哉 */
f620c5e527746a2ec3d90a11d21abd8a114746dfTatuya JINMEI 神明達哉int
f620c5e527746a2ec3d90a11d21abd8a114746dfTatuya JINMEI 神明達哉change_socket(void) {
f620c5e527746a2ec3d90a11d21abd8a114746dfTatuya JINMEI 神明達哉 int s, *sockp;
f620c5e527746a2ec3d90a11d21abd8a114746dfTatuya JINMEI 神明達哉
f620c5e527746a2ec3d90a11d21abd8a114746dfTatuya JINMEI 神明達哉 switch (server_ai->ai_family) {
f620c5e527746a2ec3d90a11d21abd8a114746dfTatuya JINMEI 神明達哉 case AF_INET:
f620c5e527746a2ec3d90a11d21abd8a114746dfTatuya JINMEI 神明達哉 sockp = &socket4;
f620c5e527746a2ec3d90a11d21abd8a114746dfTatuya JINMEI 神明達哉 break;
f620c5e527746a2ec3d90a11d21abd8a114746dfTatuya JINMEI 神明達哉#ifdef AF_INET6
f620c5e527746a2ec3d90a11d21abd8a114746dfTatuya JINMEI 神明達哉 case AF_INET6:
f620c5e527746a2ec3d90a11d21abd8a114746dfTatuya JINMEI 神明達哉 sockp = &socket6;
f620c5e527746a2ec3d90a11d21abd8a114746dfTatuya JINMEI 神明達哉 break;
f620c5e527746a2ec3d90a11d21abd8a114746dfTatuya JINMEI 神明達哉#endif
f620c5e527746a2ec3d90a11d21abd8a114746dfTatuya JINMEI 神明達哉 default:
f620c5e527746a2ec3d90a11d21abd8a114746dfTatuya JINMEI 神明達哉 fprintf(stderr, "unexpected address family: %d\n",
f620c5e527746a2ec3d90a11d21abd8a114746dfTatuya JINMEI 神明達哉 server_ai->ai_family);
f620c5e527746a2ec3d90a11d21abd8a114746dfTatuya JINMEI 神明達哉 exit(1);
f620c5e527746a2ec3d90a11d21abd8a114746dfTatuya JINMEI 神明達哉 }
f620c5e527746a2ec3d90a11d21abd8a114746dfTatuya JINMEI 神明達哉
f620c5e527746a2ec3d90a11d21abd8a114746dfTatuya JINMEI 神明達哉 if (*sockp == -1) {
f620c5e527746a2ec3d90a11d21abd8a114746dfTatuya JINMEI 神明達哉 if ((s = open_socket()) == -1)
f620c5e527746a2ec3d90a11d21abd8a114746dfTatuya JINMEI 神明達哉 return (-1);
f620c5e527746a2ec3d90a11d21abd8a114746dfTatuya JINMEI 神明達哉 *sockp = s;
f620c5e527746a2ec3d90a11d21abd8a114746dfTatuya JINMEI 神明達哉 }
f620c5e527746a2ec3d90a11d21abd8a114746dfTatuya JINMEI 神明達哉
f620c5e527746a2ec3d90a11d21abd8a114746dfTatuya JINMEI 神明達哉 return (*sockp);
f620c5e527746a2ec3d90a11d21abd8a114746dfTatuya JINMEI 神明達哉}
f620c5e527746a2ec3d90a11d21abd8a114746dfTatuya JINMEI 神明達哉
b89095ba15e9163f2bacc46239d8f69d1a2b9ad3Tatuya JINMEI 神明達哉/*
b89095ba15e9163f2bacc46239d8f69d1a2b9ad3Tatuya JINMEI 神明達哉 * reset_rttarray:
b89095ba15e9163f2bacc46239d8f69d1a2b9ad3Tatuya JINMEI 神明達哉 * (re)allocate RTT array and zero-clear the whole buffer.
b89095ba15e9163f2bacc46239d8f69d1a2b9ad3Tatuya JINMEI 神明達哉 * if array is being used, it is freed.
b89095ba15e9163f2bacc46239d8f69d1a2b9ad3Tatuya JINMEI 神明達哉 * Returns -1 on failure
b89095ba15e9163f2bacc46239d8f69d1a2b9ad3Tatuya JINMEI 神明達哉 * Returns a non-negative integer otherwise
b89095ba15e9163f2bacc46239d8f69d1a2b9ad3Tatuya JINMEI 神明達哉 */
b89095ba15e9163f2bacc46239d8f69d1a2b9ad3Tatuya JINMEI 神明達哉int
b89095ba15e9163f2bacc46239d8f69d1a2b9ad3Tatuya JINMEI 神明達哉reset_rttarray(int size) {
b89095ba15e9163f2bacc46239d8f69d1a2b9ad3Tatuya JINMEI 神明達哉 if (rttarray != NULL)
b89095ba15e9163f2bacc46239d8f69d1a2b9ad3Tatuya JINMEI 神明達哉 free(rttarray);
b89095ba15e9163f2bacc46239d8f69d1a2b9ad3Tatuya JINMEI 神明達哉 if (rttarray_interval != NULL)
b89095ba15e9163f2bacc46239d8f69d1a2b9ad3Tatuya JINMEI 神明達哉 free(rttarray_interval);
b89095ba15e9163f2bacc46239d8f69d1a2b9ad3Tatuya JINMEI 神明達哉
b89095ba15e9163f2bacc46239d8f69d1a2b9ad3Tatuya JINMEI 神明達哉 rttarray = NULL;
b89095ba15e9163f2bacc46239d8f69d1a2b9ad3Tatuya JINMEI 神明達哉 rttarray_interval = NULL;
b89095ba15e9163f2bacc46239d8f69d1a2b9ad3Tatuya JINMEI 神明達哉 rtt_max = -1;
b89095ba15e9163f2bacc46239d8f69d1a2b9ad3Tatuya JINMEI 神明達哉 rtt_min = -1;
b89095ba15e9163f2bacc46239d8f69d1a2b9ad3Tatuya JINMEI 神明達哉
b89095ba15e9163f2bacc46239d8f69d1a2b9ad3Tatuya JINMEI 神明達哉 if (size > 0) {
b89095ba15e9163f2bacc46239d8f69d1a2b9ad3Tatuya JINMEI 神明達哉 rttarray = malloc(size * sizeof(rttarray[0]));
b89095ba15e9163f2bacc46239d8f69d1a2b9ad3Tatuya JINMEI 神明達哉 if (rttarray == NULL) {
b89095ba15e9163f2bacc46239d8f69d1a2b9ad3Tatuya JINMEI 神明達哉 fprintf(stderr,
b89095ba15e9163f2bacc46239d8f69d1a2b9ad3Tatuya JINMEI 神明達哉 "Error: allocating memory for RTT array\n");
b89095ba15e9163f2bacc46239d8f69d1a2b9ad3Tatuya JINMEI 神明達哉 return (-1);
b89095ba15e9163f2bacc46239d8f69d1a2b9ad3Tatuya JINMEI 神明達哉 }
b89095ba15e9163f2bacc46239d8f69d1a2b9ad3Tatuya JINMEI 神明達哉 memset(rttarray, 0, size * sizeof(rttarray[0]));
b89095ba15e9163f2bacc46239d8f69d1a2b9ad3Tatuya JINMEI 神明達哉
b89095ba15e9163f2bacc46239d8f69d1a2b9ad3Tatuya JINMEI 神明達哉 rttarray_interval = malloc(size *
b89095ba15e9163f2bacc46239d8f69d1a2b9ad3Tatuya JINMEI 神明達哉 sizeof(rttarray_interval[0]));
b89095ba15e9163f2bacc46239d8f69d1a2b9ad3Tatuya JINMEI 神明達哉 if (rttarray_interval == NULL) {
b89095ba15e9163f2bacc46239d8f69d1a2b9ad3Tatuya JINMEI 神明達哉 fprintf(stderr,
b89095ba15e9163f2bacc46239d8f69d1a2b9ad3Tatuya JINMEI 神明達哉 "Error: allocating memory for RTT array\n");
b89095ba15e9163f2bacc46239d8f69d1a2b9ad3Tatuya JINMEI 神明達哉 return (-1);
b89095ba15e9163f2bacc46239d8f69d1a2b9ad3Tatuya JINMEI 神明達哉 }
b89095ba15e9163f2bacc46239d8f69d1a2b9ad3Tatuya JINMEI 神明達哉
b89095ba15e9163f2bacc46239d8f69d1a2b9ad3Tatuya JINMEI 神明達哉 memset(rttarray_interval, 0,
b89095ba15e9163f2bacc46239d8f69d1a2b9ad3Tatuya JINMEI 神明達哉 size * sizeof(rttarray_interval[0]));
b89095ba15e9163f2bacc46239d8f69d1a2b9ad3Tatuya JINMEI 神明達哉 }
b89095ba15e9163f2bacc46239d8f69d1a2b9ad3Tatuya JINMEI 神明達哉
b89095ba15e9163f2bacc46239d8f69d1a2b9ad3Tatuya JINMEI 神明達哉 return (0);
b89095ba15e9163f2bacc46239d8f69d1a2b9ad3Tatuya JINMEI 神明達哉}
b89095ba15e9163f2bacc46239d8f69d1a2b9ad3Tatuya JINMEI 神明達哉
b89095ba15e9163f2bacc46239d8f69d1a2b9ad3Tatuya JINMEI 神明達哉/*
b89095ba15e9163f2bacc46239d8f69d1a2b9ad3Tatuya JINMEI 神明達哉 * set_query_interval:
b89095ba15e9163f2bacc46239d8f69d1a2b9ad3Tatuya JINMEI 神明達哉 * set the interval of consecutive queries if the target qps are specified.
b89095ba15e9163f2bacc46239d8f69d1a2b9ad3Tatuya JINMEI 神明達哉 * Returns -1 on failure
b89095ba15e9163f2bacc46239d8f69d1a2b9ad3Tatuya JINMEI 神明達哉 * Returns a non-negative integer otherwise
b89095ba15e9163f2bacc46239d8f69d1a2b9ad3Tatuya JINMEI 神明達哉 */
b89095ba15e9163f2bacc46239d8f69d1a2b9ad3Tatuya JINMEI 神明達哉int
b89095ba15e9163f2bacc46239d8f69d1a2b9ad3Tatuya JINMEI 神明達哉set_query_interval(unsigned int qps) {
b89095ba15e9163f2bacc46239d8f69d1a2b9ad3Tatuya JINMEI 神明達哉 if (qps == 0)
b89095ba15e9163f2bacc46239d8f69d1a2b9ad3Tatuya JINMEI 神明達哉 return (0);
b89095ba15e9163f2bacc46239d8f69d1a2b9ad3Tatuya JINMEI 神明達哉
b89095ba15e9163f2bacc46239d8f69d1a2b9ad3Tatuya JINMEI 神明達哉 query_interval = (1.0 / (double)qps);
b89095ba15e9163f2bacc46239d8f69d1a2b9ad3Tatuya JINMEI 神明達哉
b89095ba15e9163f2bacc46239d8f69d1a2b9ad3Tatuya JINMEI 神明達哉 return (0);
b89095ba15e9163f2bacc46239d8f69d1a2b9ad3Tatuya JINMEI 神明達哉}
b89095ba15e9163f2bacc46239d8f69d1a2b9ad3Tatuya JINMEI 神明達哉
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafsson/*
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafsson * setup:
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafsson * Set configuration options from command line arguments
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafsson * Open datafile ready for reading
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafsson *
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafsson * Return -1 on failure
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafsson * Return non-negative integer on success
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafsson */
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafssonint
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafssonsetup(int argc, char **argv) {
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafsson set_input_stdin();
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafsson
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafsson if (set_max_queries(DEF_MAX_QUERIES_OUTSTANDING) == -1) {
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafsson fprintf(stderr, "%s: Unable to set default max outstanding "
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafsson "queries\n", argv[0]);
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafsson return (-1);
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafsson }
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafsson
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafsson if (set_server(DEF_SERVER_TO_QUERY) == -1) {
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafsson fprintf(stderr, "%s: Error setting default server name\n",
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafsson argv[0]);
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafsson return (-1);
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafsson }
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafsson
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafsson if (set_server_port(DEF_SERVER_PORT) == -1) {
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafsson fprintf(stderr, "%s: Error setting default server port\n",
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafsson argv[0]);
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafsson return (-1);
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafsson }
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafsson
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafsson if (parse_args(argc, argv) == -1) {
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafsson show_usage();
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafsson return (-1);
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafsson }
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafsson
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafsson if (open_datafile() == -1)
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafsson return (-1);
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafsson
f620c5e527746a2ec3d90a11d21abd8a114746dfTatuya JINMEI 神明達哉 if (set_server_sa() == -1)
f620c5e527746a2ec3d90a11d21abd8a114746dfTatuya JINMEI 神明達哉 return (-1);
f620c5e527746a2ec3d90a11d21abd8a114746dfTatuya JINMEI 神明達哉
f620c5e527746a2ec3d90a11d21abd8a114746dfTatuya JINMEI 神明達哉 if ((query_socket = change_socket()) == -1)
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafsson return (-1);
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafsson
b89095ba15e9163f2bacc46239d8f69d1a2b9ad3Tatuya JINMEI 神明達哉 if (reset_rttarray(rttarray_size) == -1)
b89095ba15e9163f2bacc46239d8f69d1a2b9ad3Tatuya JINMEI 神明達哉 return (-1);
b89095ba15e9163f2bacc46239d8f69d1a2b9ad3Tatuya JINMEI 神明達哉
b89095ba15e9163f2bacc46239d8f69d1a2b9ad3Tatuya JINMEI 神明達哉 if (set_query_interval(target_qps) == -1)
b89095ba15e9163f2bacc46239d8f69d1a2b9ad3Tatuya JINMEI 神明達哉 return (-1);
b89095ba15e9163f2bacc46239d8f69d1a2b9ad3Tatuya JINMEI 神明達哉
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafsson return (0);
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafsson}
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafsson
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafsson/*
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafsson * set_timenow:
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafsson * Set a timeval struct to indicate the current time
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafsson */
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafssonvoid
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafssonset_timenow(struct timeval *tv) {
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafsson if (gettimeofday(tv, NULL) == -1) {
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafsson fprintf(stderr, "Error in gettimeofday(). Using inaccurate "
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafsson "time() instead\n");
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafsson tv->tv_sec = time(NULL);
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafsson tv->tv_usec = 0;
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafsson }
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafsson}
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafsson
b89095ba15e9163f2bacc46239d8f69d1a2b9ad3Tatuya JINMEI 神明達哉/*
b89095ba15e9163f2bacc46239d8f69d1a2b9ad3Tatuya JINMEI 神明達哉 * addtv:
b89095ba15e9163f2bacc46239d8f69d1a2b9ad3Tatuya JINMEI 神明達哉 * add tv1 and tv2, store the result in tv_result.
b89095ba15e9163f2bacc46239d8f69d1a2b9ad3Tatuya JINMEI 神明達哉 */
b89095ba15e9163f2bacc46239d8f69d1a2b9ad3Tatuya JINMEI 神明達哉void
b89095ba15e9163f2bacc46239d8f69d1a2b9ad3Tatuya JINMEI 神明達哉addtv(struct timeval *tv1, struct timeval *tv2, struct timeval *tv_result) {
b89095ba15e9163f2bacc46239d8f69d1a2b9ad3Tatuya JINMEI 神明達哉 tv_result->tv_sec = tv1->tv_sec + tv2->tv_sec;
b89095ba15e9163f2bacc46239d8f69d1a2b9ad3Tatuya JINMEI 神明達哉 tv_result->tv_usec = tv1->tv_usec + tv2->tv_usec;
b89095ba15e9163f2bacc46239d8f69d1a2b9ad3Tatuya JINMEI 神明達哉 if (tv_result->tv_usec > 1000000) {
b89095ba15e9163f2bacc46239d8f69d1a2b9ad3Tatuya JINMEI 神明達哉 tv_result->tv_sec++;
b89095ba15e9163f2bacc46239d8f69d1a2b9ad3Tatuya JINMEI 神明達哉 tv_result->tv_usec -= 1000000;
b89095ba15e9163f2bacc46239d8f69d1a2b9ad3Tatuya JINMEI 神明達哉 }
b89095ba15e9163f2bacc46239d8f69d1a2b9ad3Tatuya JINMEI 神明達哉}
b89095ba15e9163f2bacc46239d8f69d1a2b9ad3Tatuya JINMEI 神明達哉
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafsson/*
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafsson * difftv:
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafsson * Find the difference in seconds between two timeval structs.
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafsson *
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafsson * Return the difference between tv1 and tv2 in seconds in a double.
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafsson */
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafssondouble
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafssondifftv(struct timeval tv1, struct timeval tv2) {
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafsson long diff_sec, diff_usec;
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafsson double diff;
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafsson
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafsson diff_sec = tv1.tv_sec - tv2.tv_sec;
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafsson diff_usec = tv1.tv_usec - tv2.tv_usec;
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafsson
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafsson diff = (double)diff_sec + ((double)diff_usec / 1000000.0);
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafsson
f620c5e527746a2ec3d90a11d21abd8a114746dfTatuya JINMEI 神明達哉 return (diff);
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafsson}
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafsson
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafsson/*
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafsson * timelimit_reached:
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafsson * Have we reached the time limit (if any)?
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafsson *
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafsson * Returns FALSE if there is no time limit or if we have not reached it
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafsson * Returns TRUE otherwise
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafsson */
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafssonint
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafssontimelimit_reached(void) {
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafsson struct timeval time_now;
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafsson
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafsson set_timenow(&time_now);
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafsson
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafsson if (use_timelimit == FALSE)
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafsson return (FALSE);
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafsson
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafsson if (setup_phase == TRUE) {
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafsson if (difftv(time_now, time_of_program_start)
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafsson < (double)(run_timelimit + HARD_TIMEOUT_EXTRA))
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafsson return (FALSE);
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafsson else
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafsson return (TRUE);
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafsson } else {
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafsson if (difftv(time_now, time_of_first_query)
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafsson < (double)run_timelimit)
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafsson return (FALSE);
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafsson else
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafsson return (TRUE);
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafsson }
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafsson}
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafsson
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafsson/*
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafsson * keep_sending:
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafsson * Should we keep sending queries or stop here?
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafsson *
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafsson * Return TRUE if we should keep on sending queries
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafsson * Return FALSE if we should stop
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafsson *
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafsson * Side effects:
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafsson * Rewinds the input and clears reached_end_input if we have reached the
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafsson * end of the input, but we are meant to run through it multiple times
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafsson * and have not hit the time limit yet (if any is set).
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafsson */
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafssonint
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafssonkeep_sending(int *reached_end_input) {
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafsson static int stop = FALSE;
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafsson
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafsson if (stop == TRUE)
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafsson return (FALSE);
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafsson
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafsson if ((*reached_end_input == FALSE) && (timelimit_reached() == FALSE))
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafsson return (TRUE);
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafsson else if ((*reached_end_input == TRUE) && (run_only_once == FALSE)
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafsson && (timelimit_reached() == FALSE)) {
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafsson rewind(datafile_ptr);
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafsson *reached_end_input = FALSE;
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafsson runs_through_file++;
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafsson return (TRUE);
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafsson } else {
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafsson if (*reached_end_input == TRUE)
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafsson runs_through_file++;
b89095ba15e9163f2bacc46239d8f69d1a2b9ad3Tatuya JINMEI 神明達哉 set_timenow(&time_of_stop_sending);
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafsson stop = TRUE;
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafsson return (FALSE);
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafsson }
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafsson}
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafsson
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafsson/*
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafsson * queries_outstanding:
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafsson * How many queries are outstanding?
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafsson *
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafsson * Returns the number of outstanding queries
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafsson */
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafssonunsigned int
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafssonqueries_outstanding(void) {
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafsson return (num_queries_outstanding);
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafsson}
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafsson
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafsson/*
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafsson * next_input_line:
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafsson * Get the next non-comment line from the input file
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafsson *
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafsson * Put text in line, up to max of n chars. Skip comment lines.
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafsson * Skip empty lines.
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafsson *
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafsson * Return line length on success
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafsson * Return 0 if cannot read a non-comment line (EOF or error)
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafsson */
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafssonint
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafssonnext_input_line(char *line, int n) {
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafsson char *result;
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafsson
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafsson do {
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafsson result = fgets(line, n, datafile_ptr);
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafsson } while ((result != NULL) &&
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafsson ((line[0] == COMMENT_CHAR) || (line[0] == '\n')));
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafsson
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafsson if (result == NULL)
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafsson return (0);
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafsson else
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafsson return (strlen(line));
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafsson}
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafsson
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafsson/*
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafsson * identify_directive:
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafsson * Gives us a numerical value equivelant for a directive string
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafsson *
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafsson * Returns the value for the directive
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafsson * Returns -1 if not a valid directive
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafsson */
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafssonint
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafssonidentify_directive(char *dir) {
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafsson static char *directives[] = DIRECTIVES;
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafsson static int dir_values[] = DIR_VALUES;
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafsson unsigned int index, num_directives;
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafsson
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafsson num_directives = sizeof(directives) / sizeof(directives[0]);
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafsson
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafsson if (num_directives > (sizeof(dir_values) / sizeof(int)))
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafsson num_directives = sizeof(dir_values) / sizeof(int);
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafsson
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafsson for (index = 0; index < num_directives; index++) {
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafsson if (strcmp(dir, directives[index]) == 0)
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafsson return (dir_values[index]);
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafsson }
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafsson
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafsson return (-1);
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafsson}
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafsson
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafsson/*
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafsson * update_config:
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafsson * Update configuration options from a line from the input file
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafsson */
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafssonvoid
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafssonupdate_config(char *config_change_desc) {
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafsson char *directive, *config_value, *trailing_garbage;
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafsson char conf_copy[MAX_INPUT_LEN + 1];
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafsson unsigned int uint_val;
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafsson int directive_number;
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafsson int check;
f620c5e527746a2ec3d90a11d21abd8a114746dfTatuya JINMEI 神明達哉 int old_af;
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafsson
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafsson if (ignore_config_changes == TRUE) {
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafsson fprintf(stderr, "Ignoring configuration change: %s",
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafsson config_change_desc);
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafsson return;
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafsson }
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafsson
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafsson strcpy(conf_copy, config_change_desc);
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafsson
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafsson ++config_change_desc;
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafsson
81e805888bcd4bd2a6d5c986fade6ffdf7d88a54Andreas Gustafsson if (*config_change_desc == '\0') {
81e805888bcd4bd2a6d5c986fade6ffdf7d88a54Andreas Gustafsson fprintf(stderr, "Invalid config: No directive present: %s\n",
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafsson conf_copy);
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafsson return;
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafsson }
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafsson
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafsson if (index(WHITESPACE, *config_change_desc) != NULL) {
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafsson fprintf(stderr, "Invalid config: Space before directive or "
81e805888bcd4bd2a6d5c986fade6ffdf7d88a54Andreas Gustafsson "no directive present: %s\n", conf_copy);
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafsson return;
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafsson }
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafsson
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafsson directive = strtok(config_change_desc, WHITESPACE);
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafsson config_value = strtok(NULL, WHITESPACE);
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafsson trailing_garbage = strtok(NULL, WHITESPACE);
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafsson
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafsson if ((directive_number = identify_directive(directive)) == -1) {
81e805888bcd4bd2a6d5c986fade6ffdf7d88a54Andreas Gustafsson fprintf(stderr, "Invalid config: Bad directive: %s\n",
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafsson conf_copy);
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafsson return;
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafsson }
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafsson
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafsson if (config_value == NULL) {
81e805888bcd4bd2a6d5c986fade6ffdf7d88a54Andreas Gustafsson fprintf(stderr, "Invalid config: No value present: %s\n",
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafsson conf_copy);
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafsson return;
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafsson }
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafsson
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafsson if (trailing_garbage != NULL) {
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafsson fprintf(stderr, "Config warning: "
81e805888bcd4bd2a6d5c986fade6ffdf7d88a54Andreas Gustafsson "trailing garbage: %s\n", conf_copy);
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafsson }
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafsson
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafsson switch(directive_number) {
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafsson
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafsson case V_SERVER:
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafsson if (serverset && (setup_phase == TRUE)) {
6d7939a85025621aa11e9cfe933629113bd0963aMark Andrews fprintf(stderr, "Config change overridden by command "
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafsson "line: %s\n", directive);
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafsson return;
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafsson }
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafsson
f620c5e527746a2ec3d90a11d21abd8a114746dfTatuya JINMEI 神明達哉 if (set_server(config_value) == -1) {
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafsson fprintf(stderr, "Set server error: unable to change "
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafsson "the server name to '%s'\n", config_value);
f620c5e527746a2ec3d90a11d21abd8a114746dfTatuya JINMEI 神明達哉 return;
f620c5e527746a2ec3d90a11d21abd8a114746dfTatuya JINMEI 神明達哉 }
f620c5e527746a2ec3d90a11d21abd8a114746dfTatuya JINMEI 神明達哉
f620c5e527746a2ec3d90a11d21abd8a114746dfTatuya JINMEI 神明達哉 old_af = server_ai->ai_family;
f620c5e527746a2ec3d90a11d21abd8a114746dfTatuya JINMEI 神明達哉 if (set_server_sa() == -1) {
f620c5e527746a2ec3d90a11d21abd8a114746dfTatuya JINMEI 神明達哉 fprintf(stderr, "Set server error: unable to resolve "
f620c5e527746a2ec3d90a11d21abd8a114746dfTatuya JINMEI 神明達哉 "a new server '%s'\n",
f620c5e527746a2ec3d90a11d21abd8a114746dfTatuya JINMEI 神明達哉 config_value);
f620c5e527746a2ec3d90a11d21abd8a114746dfTatuya JINMEI 神明達哉 return;
f620c5e527746a2ec3d90a11d21abd8a114746dfTatuya JINMEI 神明達哉 }
f620c5e527746a2ec3d90a11d21abd8a114746dfTatuya JINMEI 神明達哉 if (old_af != server_ai->ai_family) {
f620c5e527746a2ec3d90a11d21abd8a114746dfTatuya JINMEI 神明達哉 if ((query_socket = change_socket()) == -1) {
f620c5e527746a2ec3d90a11d21abd8a114746dfTatuya JINMEI 神明達哉 /* XXX: this is fatal */
f620c5e527746a2ec3d90a11d21abd8a114746dfTatuya JINMEI 神明達哉 fprintf(stderr, "Set server error: "
f620c5e527746a2ec3d90a11d21abd8a114746dfTatuya JINMEI 神明達哉 "unable to open a new socket "
f620c5e527746a2ec3d90a11d21abd8a114746dfTatuya JINMEI 神明達哉 "for '%s'\n", config_value);
f620c5e527746a2ec3d90a11d21abd8a114746dfTatuya JINMEI 神明達哉 exit(1);
f620c5e527746a2ec3d90a11d21abd8a114746dfTatuya JINMEI 神明達哉 }
f620c5e527746a2ec3d90a11d21abd8a114746dfTatuya JINMEI 神明達哉 }
f620c5e527746a2ec3d90a11d21abd8a114746dfTatuya JINMEI 神明達哉
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafsson break;
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafsson
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafsson case V_PORT:
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafsson if (portset && (setup_phase == TRUE)) {
6d7939a85025621aa11e9cfe933629113bd0963aMark Andrews fprintf(stderr, "Config change overridden by command "
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafsson "line: %s\n", directive);
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafsson return;
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafsson }
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafsson
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafsson check = is_uint(config_value, &uint_val);
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafsson
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafsson if ((check == TRUE) && (uint_val > 0)) {
f620c5e527746a2ec3d90a11d21abd8a114746dfTatuya JINMEI 神明達哉 if (set_server_port(config_value) == -1) {
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafsson fprintf(stderr, "Invalid config: Bad value for"
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafsson " %s: %s\n", directive, config_value);
f620c5e527746a2ec3d90a11d21abd8a114746dfTatuya JINMEI 神明達哉 } else {
f620c5e527746a2ec3d90a11d21abd8a114746dfTatuya JINMEI 神明達哉 if (set_server_sa() == -1) {
f620c5e527746a2ec3d90a11d21abd8a114746dfTatuya JINMEI 神明達哉 fprintf(stderr,
f620c5e527746a2ec3d90a11d21abd8a114746dfTatuya JINMEI 神明達哉 "Failed to set a new port\n");
f620c5e527746a2ec3d90a11d21abd8a114746dfTatuya JINMEI 神明達哉 return;
f620c5e527746a2ec3d90a11d21abd8a114746dfTatuya JINMEI 神明達哉 }
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafsson }
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafsson } else
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafsson fprintf(stderr, "Invalid config: Bad value for "
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafsson "%s: %s\n", directive, config_value);
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafsson break;
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafsson
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafsson case V_MAXQUERIES:
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafsson if (queriesset && (setup_phase == TRUE)) {
6d7939a85025621aa11e9cfe933629113bd0963aMark Andrews fprintf(stderr, "Config change overridden by command "
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafsson "line: %s\n", directive);
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafsson return;
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafsson }
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafsson
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafsson check = is_uint(config_value, &uint_val);
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafsson
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafsson if ((check == TRUE) && (uint_val > 0)) {
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafsson set_max_queries(uint_val);
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafsson } else
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafsson fprintf(stderr, "Invalid config: Bad value for "
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafsson "%s: %s\n", directive, config_value);
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafsson break;
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafsson
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafsson case V_MAXWAIT:
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafsson if (timeoutset && (setup_phase == TRUE)) {
6d7939a85025621aa11e9cfe933629113bd0963aMark Andrews fprintf(stderr, "Config change overridden by command "
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafsson "line: %s\n", directive);
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafsson return;
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafsson }
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafsson
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafsson check = is_uint(config_value, &uint_val);
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafsson
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafsson if ((check == TRUE) && (uint_val > 0)) {
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafsson query_timeout = uint_val;
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafsson } else
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafsson fprintf(stderr, "Invalid config: Bad value for "
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafsson "%s: %s\n", directive, config_value);
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafsson break;
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafsson
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafsson default:
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafsson fprintf(stderr, "Invalid config: Bad directive: %s\n",
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafsson directive);
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafsson break;
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafsson }
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafsson}
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafsson
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafsson/*
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafsson * parse_query:
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafsson * Parse a query line from the input file
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafsson *
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafsson * Set qname to be the domain to query (up to a max of qnlen chars)
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafsson * Set qtype to be the type of the query
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafsson *
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafsson * Return -1 on failure
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafsson * Return a non-negative integer otherwise
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafsson */
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafssonint
384fef003441df4067b773688237700513390f9eEvan Huntparse_query(char *input, char *qname, unsigned int qnlen, int *qtype) {
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafsson static char *qtype_strings[] = QTYPE_STRINGS;
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafsson static int qtype_codes[] = QTYPE_CODES;
384fef003441df4067b773688237700513390f9eEvan Hunt unsigned int num_types, index;
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafsson int found = FALSE;
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafsson char incopy[MAX_INPUT_LEN + 1];
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafsson char *domain_str, *type_str;
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafsson
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafsson num_types = sizeof(qtype_strings) / sizeof(qtype_strings[0]);
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafsson if (num_types > (sizeof(qtype_codes) / sizeof(int)))
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafsson num_types = sizeof(qtype_codes) / sizeof(int);
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafsson
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafsson strcpy(incopy, input);
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafsson
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafsson domain_str = strtok(incopy, WHITESPACE);
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafsson type_str = strtok(NULL, WHITESPACE);
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafsson
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafsson if ((domain_str == NULL) || (type_str == NULL)) {
8ac1acc30d0f405222ffa7b2b93131d9d4e18599Mark Andrews fprintf(stderr, "Invalid query input format: %s\n", input);
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafsson return (-1);
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafsson }
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafsson
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafsson if (strlen(domain_str) > qnlen) {
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafsson fprintf(stderr, "Query domain too long: %s\n", domain_str);
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafsson return (-1);
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafsson }
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafsson
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafsson for (index = 0; (index < num_types) && (found == FALSE); index++) {
8ac1acc30d0f405222ffa7b2b93131d9d4e18599Mark Andrews if (strcasecmp(type_str, qtype_strings[index]) == 0) {
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafsson *qtype = qtype_codes[index];
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafsson found = TRUE;
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafsson }
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafsson }
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafsson
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafsson if (found == FALSE) {
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafsson fprintf(stderr, "Query type not understood: %s\n", type_str);
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafsson return (-1);
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafsson }
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafsson
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafsson strcpy(qname, domain_str);
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafsson
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafsson return (0);
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafsson}
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafsson
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafsson/*
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafsson * dispatch_query:
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafsson * Send the query packet for the entry
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafsson *
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafsson * Return -1 on failure
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafsson * Return a non-negative integer otherwise
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafsson */
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafssonint
384fef003441df4067b773688237700513390f9eEvan Huntdispatch_query(unsigned short int id, char *dom, int qt, u_char **pktp,
384fef003441df4067b773688237700513390f9eEvan Hunt int *pktlenp)
384fef003441df4067b773688237700513390f9eEvan Hunt{
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafsson static u_char packet_buffer[PACKETSZ + 1];
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafsson int buffer_len = PACKETSZ;
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafsson int bytes_sent;
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafsson unsigned short int net_id = htons(id);
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafsson char *id_ptr = (char *)&net_id;
66f51d8678c8fcc7256354b599317f67455120fdMark Andrews HEADER *hp = (HEADER *)packet_buffer;
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafsson
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafsson buffer_len = res_mkquery(QUERY, dom, C_IN, qt, NULL, 0,
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafsson NULL, packet_buffer, PACKETSZ);
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafsson if (buffer_len == -1) {
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafsson fprintf(stderr, "Failed to create query packet: %s %d\n",
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafsson dom, qt);
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafsson return (-1);
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafsson }
66f51d8678c8fcc7256354b599317f67455120fdMark Andrews hp->rd = recurse;
8ac1acc30d0f405222ffa7b2b93131d9d4e18599Mark Andrews if (edns) {
8ac1acc30d0f405222ffa7b2b93131d9d4e18599Mark Andrews unsigned char *p;
8ac1acc30d0f405222ffa7b2b93131d9d4e18599Mark Andrews if (buffer_len + EDNSLEN >= PACKETSZ) {
8ac1acc30d0f405222ffa7b2b93131d9d4e18599Mark Andrews fprintf(stderr, "Failed to add OPT to query packet\n");
8ac1acc30d0f405222ffa7b2b93131d9d4e18599Mark Andrews return (-1);
8ac1acc30d0f405222ffa7b2b93131d9d4e18599Mark Andrews }
8ac1acc30d0f405222ffa7b2b93131d9d4e18599Mark Andrews packet_buffer[11] = 1;
8ac1acc30d0f405222ffa7b2b93131d9d4e18599Mark Andrews p = &packet_buffer[buffer_len];
8ac1acc30d0f405222ffa7b2b93131d9d4e18599Mark Andrews *p++ = 0; /* root name */
8ac1acc30d0f405222ffa7b2b93131d9d4e18599Mark Andrews *p++ = 0;
8ac1acc30d0f405222ffa7b2b93131d9d4e18599Mark Andrews *p++ = 41; /* OPT */
8ac1acc30d0f405222ffa7b2b93131d9d4e18599Mark Andrews *p++ = 16;
8ac1acc30d0f405222ffa7b2b93131d9d4e18599Mark Andrews *p++ = 0; /* UDP payload size (4K) */
8ac1acc30d0f405222ffa7b2b93131d9d4e18599Mark Andrews *p++ = 0; /* extended rcode */
8ac1acc30d0f405222ffa7b2b93131d9d4e18599Mark Andrews *p++ = 0; /* version */
8ac1acc30d0f405222ffa7b2b93131d9d4e18599Mark Andrews if (dnssec)
8ac1acc30d0f405222ffa7b2b93131d9d4e18599Mark Andrews *p++ = 0x80; /* upper flag bits - DO set */
8ac1acc30d0f405222ffa7b2b93131d9d4e18599Mark Andrews else
8ac1acc30d0f405222ffa7b2b93131d9d4e18599Mark Andrews *p++ = 0; /* upper flag bits */
8ac1acc30d0f405222ffa7b2b93131d9d4e18599Mark Andrews *p++ = 0; /* lower flag bit */
8ac1acc30d0f405222ffa7b2b93131d9d4e18599Mark Andrews *p++ = 0;
8ac1acc30d0f405222ffa7b2b93131d9d4e18599Mark Andrews *p++ = 0; /* rdlen == 0 */
8ac1acc30d0f405222ffa7b2b93131d9d4e18599Mark Andrews buffer_len += EDNSLEN;
8ac1acc30d0f405222ffa7b2b93131d9d4e18599Mark Andrews }
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafsson
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafsson packet_buffer[0] = id_ptr[0];
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafsson packet_buffer[1] = id_ptr[1];
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafsson
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafsson bytes_sent = sendto(query_socket, packet_buffer, buffer_len, 0,
f620c5e527746a2ec3d90a11d21abd8a114746dfTatuya JINMEI 神明達哉 server_ai->ai_addr, server_ai->ai_addrlen);
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafsson if (bytes_sent == -1) {
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafsson fprintf(stderr, "Failed to send query packet: %s %d\n",
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafsson dom, qt);
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafsson return (-1);
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafsson }
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafsson
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafsson if (bytes_sent != buffer_len)
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafsson fprintf(stderr, "Warning: incomplete packet sent: %s %d\n",
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafsson dom, qt);
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafsson
384fef003441df4067b773688237700513390f9eEvan Hunt *pktp = packet_buffer;
384fef003441df4067b773688237700513390f9eEvan Hunt *pktlenp = buffer_len;
384fef003441df4067b773688237700513390f9eEvan Hunt
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafsson return (0);
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafsson}
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafsson
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafsson/*
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafsson * send_query:
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafsson * Send a query based on a line of input
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafsson */
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafssonvoid
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafssonsend_query(char *query_desc) {
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafsson static unsigned short int use_query_id = 0;
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafsson static int qname_len = MAX_DOMAIN_LEN;
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafsson static char domain[MAX_DOMAIN_LEN + 1];
384fef003441df4067b773688237700513390f9eEvan Hunt u_char *qpkt;
f620c5e527746a2ec3d90a11d21abd8a114746dfTatuya JINMEI 神明達哉 char serveraddr[NI_MAXHOST];
384fef003441df4067b773688237700513390f9eEvan Hunt int query_type, qpkt_len;
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafsson unsigned int count;
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafsson
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafsson use_query_id++;
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafsson
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafsson if (parse_query(query_desc, domain, qname_len, &query_type) == -1) {
81e805888bcd4bd2a6d5c986fade6ffdf7d88a54Andreas Gustafsson fprintf(stderr, "Error parsing query: %s\n", query_desc);
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafsson return;
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafsson }
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafsson
384fef003441df4067b773688237700513390f9eEvan Hunt if (dispatch_query(use_query_id, domain, query_type,
384fef003441df4067b773688237700513390f9eEvan Hunt &qpkt, &qpkt_len) == -1) {
f620c5e527746a2ec3d90a11d21abd8a114746dfTatuya JINMEI 神明達哉 char *addrstr;
f620c5e527746a2ec3d90a11d21abd8a114746dfTatuya JINMEI 神明達哉
f620c5e527746a2ec3d90a11d21abd8a114746dfTatuya JINMEI 神明達哉 if (getnameinfo(server_ai->ai_addr, server_ai->ai_addrlen,
f620c5e527746a2ec3d90a11d21abd8a114746dfTatuya JINMEI 神明達哉 serveraddr, sizeof(serveraddr), NULL, 0,
f620c5e527746a2ec3d90a11d21abd8a114746dfTatuya JINMEI 神明達哉 NI_NUMERICHOST) == 0) {
f620c5e527746a2ec3d90a11d21abd8a114746dfTatuya JINMEI 神明達哉 addrstr = serveraddr;
f620c5e527746a2ec3d90a11d21abd8a114746dfTatuya JINMEI 神明達哉 } else
f620c5e527746a2ec3d90a11d21abd8a114746dfTatuya JINMEI 神明達哉 addrstr = "???"; /* XXX: this should not happen */
f620c5e527746a2ec3d90a11d21abd8a114746dfTatuya JINMEI 神明達哉 fprintf(stderr, "Error sending query to %s: %s\n",
f620c5e527746a2ec3d90a11d21abd8a114746dfTatuya JINMEI 神明達哉 addrstr, query_desc);
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafsson return;
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafsson }
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafsson
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafsson if (setup_phase == TRUE) {
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafsson set_timenow(&time_of_first_query);
b89095ba15e9163f2bacc46239d8f69d1a2b9ad3Tatuya JINMEI 神明達哉 time_of_first_query_sec = (double)time_of_first_query.tv_sec +
b89095ba15e9163f2bacc46239d8f69d1a2b9ad3Tatuya JINMEI 神明達哉 ((double)time_of_first_query.tv_usec / 1000000.0);
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafsson setup_phase = FALSE;
f620c5e527746a2ec3d90a11d21abd8a114746dfTatuya JINMEI 神明達哉 if (getnameinfo(server_ai->ai_addr, server_ai->ai_addrlen,
f620c5e527746a2ec3d90a11d21abd8a114746dfTatuya JINMEI 神明達哉 serveraddr, sizeof(serveraddr), NULL, 0,
f620c5e527746a2ec3d90a11d21abd8a114746dfTatuya JINMEI 神明達哉 NI_NUMERICHOST) != 0) {
f620c5e527746a2ec3d90a11d21abd8a114746dfTatuya JINMEI 神明達哉 fprintf(stderr, "Error printing server address\n");
f620c5e527746a2ec3d90a11d21abd8a114746dfTatuya JINMEI 神明達哉 return;
f620c5e527746a2ec3d90a11d21abd8a114746dfTatuya JINMEI 神明達哉 }
f620c5e527746a2ec3d90a11d21abd8a114746dfTatuya JINMEI 神明達哉 printf("[Status] Sending queries (beginning with %s)\n",
f620c5e527746a2ec3d90a11d21abd8a114746dfTatuya JINMEI 神明達哉 serveraddr);
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafsson }
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafsson
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafsson /* Find the first slot in status[] that is not in use */
b89095ba15e9163f2bacc46239d8f69d1a2b9ad3Tatuya JINMEI 神明達哉 for (count = 0; (status[count].in_use == TRUE)
b89095ba15e9163f2bacc46239d8f69d1a2b9ad3Tatuya JINMEI 神明達哉 && (count < max_queries_outstanding); count++);
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafsson
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafsson if (status[count].in_use == TRUE) {
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafsson fprintf(stderr, "Unexpected error: We have run out of "
b89095ba15e9163f2bacc46239d8f69d1a2b9ad3Tatuya JINMEI 神明達哉 "status[] space!\n");
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafsson return;
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafsson }
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafsson
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafsson /* Register the query in status[] */
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafsson status[count].id = use_query_id;
81e805888bcd4bd2a6d5c986fade6ffdf7d88a54Andreas Gustafsson if (verbose)
81e805888bcd4bd2a6d5c986fade6ffdf7d88a54Andreas Gustafsson status[count].desc = strdup(query_desc);
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafsson set_timenow(&status[count].sent_timestamp);
384fef003441df4067b773688237700513390f9eEvan Hunt status[count].qtype = query_type;
384fef003441df4067b773688237700513390f9eEvan Hunt if (dn_expand(qpkt, qpkt + qpkt_len, qpkt + DNS_HEADERLEN,
384fef003441df4067b773688237700513390f9eEvan Hunt status[count].qname, MAX_DOMAIN_LEN) == -1) {
384fef003441df4067b773688237700513390f9eEvan Hunt fprintf(stderr, "Unexpected error: "
384fef003441df4067b773688237700513390f9eEvan Hunt "query message doesn't have qname?\n");
384fef003441df4067b773688237700513390f9eEvan Hunt return;
384fef003441df4067b773688237700513390f9eEvan Hunt }
384fef003441df4067b773688237700513390f9eEvan Hunt status[count].in_use = TRUE;
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafsson
b89095ba15e9163f2bacc46239d8f69d1a2b9ad3Tatuya JINMEI 神明達哉 if (num_queries_sent_interval == 0)
b89095ba15e9163f2bacc46239d8f69d1a2b9ad3Tatuya JINMEI 神明達哉 set_timenow(&time_of_first_query_interval);
b89095ba15e9163f2bacc46239d8f69d1a2b9ad3Tatuya JINMEI 神明達哉
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafsson num_queries_sent++;
b89095ba15e9163f2bacc46239d8f69d1a2b9ad3Tatuya JINMEI 神明達哉 num_queries_sent_interval++;
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafsson num_queries_outstanding++;
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafsson}
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafsson
b89095ba15e9163f2bacc46239d8f69d1a2b9ad3Tatuya JINMEI 神明達哉void
384fef003441df4067b773688237700513390f9eEvan Huntregister_rtt(struct timeval *timestamp, char *qname, int qtype,
384fef003441df4067b773688237700513390f9eEvan Hunt unsigned int rcode)
384fef003441df4067b773688237700513390f9eEvan Hunt{
b89095ba15e9163f2bacc46239d8f69d1a2b9ad3Tatuya JINMEI 神明達哉 int i;
b89095ba15e9163f2bacc46239d8f69d1a2b9ad3Tatuya JINMEI 神明達哉 int oldquery = FALSE;
b89095ba15e9163f2bacc46239d8f69d1a2b9ad3Tatuya JINMEI 神明達哉 struct timeval now;
b89095ba15e9163f2bacc46239d8f69d1a2b9ad3Tatuya JINMEI 神明達哉 double rtt;
b89095ba15e9163f2bacc46239d8f69d1a2b9ad3Tatuya JINMEI 神明達哉
b89095ba15e9163f2bacc46239d8f69d1a2b9ad3Tatuya JINMEI 神明達哉 set_timenow(&now);
b89095ba15e9163f2bacc46239d8f69d1a2b9ad3Tatuya JINMEI 神明達哉 rtt = difftv(now, *timestamp);
b89095ba15e9163f2bacc46239d8f69d1a2b9ad3Tatuya JINMEI 神明達哉
b89095ba15e9163f2bacc46239d8f69d1a2b9ad3Tatuya JINMEI 神明達哉 if (difftv(*timestamp, time_of_first_query_interval) < 0)
b89095ba15e9163f2bacc46239d8f69d1a2b9ad3Tatuya JINMEI 神明達哉 oldquery = TRUE;
b89095ba15e9163f2bacc46239d8f69d1a2b9ad3Tatuya JINMEI 神明達哉
b89095ba15e9163f2bacc46239d8f69d1a2b9ad3Tatuya JINMEI 神明達哉 if (rtt_max < 0 || rtt_max < rtt)
b89095ba15e9163f2bacc46239d8f69d1a2b9ad3Tatuya JINMEI 神明達哉 rtt_max = rtt;
b89095ba15e9163f2bacc46239d8f69d1a2b9ad3Tatuya JINMEI 神明達哉
b89095ba15e9163f2bacc46239d8f69d1a2b9ad3Tatuya JINMEI 神明達哉 if (rtt_min < 0 || rtt_min > rtt)
b89095ba15e9163f2bacc46239d8f69d1a2b9ad3Tatuya JINMEI 神明達哉 rtt_min = rtt;
b89095ba15e9163f2bacc46239d8f69d1a2b9ad3Tatuya JINMEI 神明達哉
b89095ba15e9163f2bacc46239d8f69d1a2b9ad3Tatuya JINMEI 神明達哉 rtt_total += rtt;
384fef003441df4067b773688237700513390f9eEvan Hunt rtt_counted++;
b89095ba15e9163f2bacc46239d8f69d1a2b9ad3Tatuya JINMEI 神明達哉
b89095ba15e9163f2bacc46239d8f69d1a2b9ad3Tatuya JINMEI 神明達哉 if (!oldquery) {
b89095ba15e9163f2bacc46239d8f69d1a2b9ad3Tatuya JINMEI 神明達哉 if (rtt_max_interval < 0 || rtt_max_interval < rtt)
b89095ba15e9163f2bacc46239d8f69d1a2b9ad3Tatuya JINMEI 神明達哉 rtt_max_interval = rtt;
b89095ba15e9163f2bacc46239d8f69d1a2b9ad3Tatuya JINMEI 神明達哉
b89095ba15e9163f2bacc46239d8f69d1a2b9ad3Tatuya JINMEI 神明達哉 if (rtt_min_interval < 0 || rtt_min_interval > rtt)
b89095ba15e9163f2bacc46239d8f69d1a2b9ad3Tatuya JINMEI 神明達哉 rtt_min_interval = rtt;
b89095ba15e9163f2bacc46239d8f69d1a2b9ad3Tatuya JINMEI 神明達哉
b89095ba15e9163f2bacc46239d8f69d1a2b9ad3Tatuya JINMEI 神明達哉 rtt_total_interval += rtt;
384fef003441df4067b773688237700513390f9eEvan Hunt rtt_counted_interval++;
b89095ba15e9163f2bacc46239d8f69d1a2b9ad3Tatuya JINMEI 神明達哉 }
b89095ba15e9163f2bacc46239d8f69d1a2b9ad3Tatuya JINMEI 神明達哉
b89095ba15e9163f2bacc46239d8f69d1a2b9ad3Tatuya JINMEI 神明達哉 if (rttarray == NULL)
b89095ba15e9163f2bacc46239d8f69d1a2b9ad3Tatuya JINMEI 神明達哉 return;
b89095ba15e9163f2bacc46239d8f69d1a2b9ad3Tatuya JINMEI 神明達哉
b89095ba15e9163f2bacc46239d8f69d1a2b9ad3Tatuya JINMEI 神明達哉 i = (int)(rtt * (1000000.0 / rttarray_unit));
b89095ba15e9163f2bacc46239d8f69d1a2b9ad3Tatuya JINMEI 神明達哉 if (i < rttarray_size) {
b89095ba15e9163f2bacc46239d8f69d1a2b9ad3Tatuya JINMEI 神明達哉 rttarray[i]++;
b89095ba15e9163f2bacc46239d8f69d1a2b9ad3Tatuya JINMEI 神明達哉 if (!oldquery)
b89095ba15e9163f2bacc46239d8f69d1a2b9ad3Tatuya JINMEI 神明達哉 rttarray_interval[i]++;
b89095ba15e9163f2bacc46239d8f69d1a2b9ad3Tatuya JINMEI 神明達哉 } else {
384fef003441df4067b773688237700513390f9eEvan Hunt fprintf(stderr, "Warning: RTT is out of range: %.6lf "
384fef003441df4067b773688237700513390f9eEvan Hunt "[query=%s/%d, rcode=%u]\n", rtt, qname, qtype, rcode);
b89095ba15e9163f2bacc46239d8f69d1a2b9ad3Tatuya JINMEI 神明達哉 rtt_overflows++;
b89095ba15e9163f2bacc46239d8f69d1a2b9ad3Tatuya JINMEI 神明達哉 if (!oldquery)
b89095ba15e9163f2bacc46239d8f69d1a2b9ad3Tatuya JINMEI 神明達哉 rtt_overflows_interval++;
b89095ba15e9163f2bacc46239d8f69d1a2b9ad3Tatuya JINMEI 神明達哉 }
b89095ba15e9163f2bacc46239d8f69d1a2b9ad3Tatuya JINMEI 神明達哉}
b89095ba15e9163f2bacc46239d8f69d1a2b9ad3Tatuya JINMEI 神明達哉
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafsson/*
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafsson * register_response:
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafsson * Register receipt of a query
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafsson *
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafsson * Removes (sets in_use = FALSE) the record for the given query id in
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafsson * status[] if any exists.
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafsson */
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafssonvoid
384fef003441df4067b773688237700513390f9eEvan Huntregister_response(unsigned short int id, unsigned int rcode, char *qname,
384fef003441df4067b773688237700513390f9eEvan Hunt int qtype)
384fef003441df4067b773688237700513390f9eEvan Hunt{
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafsson unsigned int ct = 0;
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafsson int found = FALSE;
b89095ba15e9163f2bacc46239d8f69d1a2b9ad3Tatuya JINMEI 神明達哉 struct timeval now;
b89095ba15e9163f2bacc46239d8f69d1a2b9ad3Tatuya JINMEI 神明達哉 double rtt;
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafsson
384fef003441df4067b773688237700513390f9eEvan Hunt if (timeout_queries != NULL) {
384fef003441df4067b773688237700513390f9eEvan Hunt struct query_mininfo *qi = &timeout_queries[id];
384fef003441df4067b773688237700513390f9eEvan Hunt
384fef003441df4067b773688237700513390f9eEvan Hunt if (qi->qtype == qtype && strcasecmp(qi->qname, qname) == 0) {
384fef003441df4067b773688237700513390f9eEvan Hunt register_rtt(&qi->sent_timestamp, qname, qtype, rcode);
384fef003441df4067b773688237700513390f9eEvan Hunt qi->qtype = -1;
384fef003441df4067b773688237700513390f9eEvan Hunt found = TRUE;
384fef003441df4067b773688237700513390f9eEvan Hunt }
384fef003441df4067b773688237700513390f9eEvan Hunt }
384fef003441df4067b773688237700513390f9eEvan Hunt
b89095ba15e9163f2bacc46239d8f69d1a2b9ad3Tatuya JINMEI 神明達哉 for (; (ct < query_status_allocated) && (found == FALSE); ct++) {
384fef003441df4067b773688237700513390f9eEvan Hunt if (status[ct].in_use == TRUE && status[ct].id == id &&
384fef003441df4067b773688237700513390f9eEvan Hunt status[ct].qtype == qtype &&
384fef003441df4067b773688237700513390f9eEvan Hunt strcasecmp(status[ct].qname, qname) == 0) {
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafsson status[ct].in_use = FALSE;
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafsson num_queries_outstanding--;
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafsson found = TRUE;
b89095ba15e9163f2bacc46239d8f69d1a2b9ad3Tatuya JINMEI 神明達哉
384fef003441df4067b773688237700513390f9eEvan Hunt register_rtt(&status[ct].sent_timestamp, qname, qtype,
384fef003441df4067b773688237700513390f9eEvan Hunt rcode);
b89095ba15e9163f2bacc46239d8f69d1a2b9ad3Tatuya JINMEI 神明達哉
81e805888bcd4bd2a6d5c986fade6ffdf7d88a54Andreas Gustafsson if (status[ct].desc) {
e2cf0e8ff9ff37121518af5b34b9e4de7abbb47cMark Andrews printf("> %s %s\n", rcode_strings[rcode],
e2cf0e8ff9ff37121518af5b34b9e4de7abbb47cMark Andrews status[ct].desc);
81e805888bcd4bd2a6d5c986fade6ffdf7d88a54Andreas Gustafsson free(status[ct].desc);
81e805888bcd4bd2a6d5c986fade6ffdf7d88a54Andreas Gustafsson }
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafsson }
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafsson }
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafsson
384fef003441df4067b773688237700513390f9eEvan Hunt if (countrcodes && (found == TRUE || target_qps > 0))
384fef003441df4067b773688237700513390f9eEvan Hunt rcodecounts[rcode]++;
384fef003441df4067b773688237700513390f9eEvan Hunt
b89095ba15e9163f2bacc46239d8f69d1a2b9ad3Tatuya JINMEI 神明達哉 if (found == FALSE) {
b89095ba15e9163f2bacc46239d8f69d1a2b9ad3Tatuya JINMEI 神明達哉 if (target_qps > 0) {
b89095ba15e9163f2bacc46239d8f69d1a2b9ad3Tatuya JINMEI 神明達哉 num_queries_possiblydelayed++;
b89095ba15e9163f2bacc46239d8f69d1a2b9ad3Tatuya JINMEI 神明達哉 num_queries_possiblydelayed_interval++;
b89095ba15e9163f2bacc46239d8f69d1a2b9ad3Tatuya JINMEI 神明達哉 } else {
384fef003441df4067b773688237700513390f9eEvan Hunt fprintf(stderr,
384fef003441df4067b773688237700513390f9eEvan Hunt "Warning: Received a response with an "
b89095ba15e9163f2bacc46239d8f69d1a2b9ad3Tatuya JINMEI 神明達哉 "unexpected (maybe timed out) id: %u\n", id);
b89095ba15e9163f2bacc46239d8f69d1a2b9ad3Tatuya JINMEI 神明達哉 }
b89095ba15e9163f2bacc46239d8f69d1a2b9ad3Tatuya JINMEI 神明達哉 }
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafsson}
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafsson
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafsson/*
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafsson * process_single_response:
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafsson * Receive from the given socket & process an invididual response packet.
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafsson * Remove it from the list of open queries (status[]) and decrement the
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafsson * number of outstanding queries if it matches an open query.
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafsson */
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafssonvoid
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafssonprocess_single_response(int sockfd) {
f620c5e527746a2ec3d90a11d21abd8a114746dfTatuya JINMEI 神明達哉 struct sockaddr_storage from_addr_ss;
f620c5e527746a2ec3d90a11d21abd8a114746dfTatuya JINMEI 神明達哉 struct sockaddr *from_addr;
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafsson static unsigned char in_buf[MAX_BUFFER_LEN];
384fef003441df4067b773688237700513390f9eEvan Hunt char qname[MAX_DOMAIN_LEN + 1];
384fef003441df4067b773688237700513390f9eEvan Hunt int numbytes, addr_len, resp_id, qnamelen;
384fef003441df4067b773688237700513390f9eEvan Hunt int qtype, flags;
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafsson
f620c5e527746a2ec3d90a11d21abd8a114746dfTatuya JINMEI 神明達哉 memset(&from_addr_ss, 0, sizeof(from_addr_ss));
f620c5e527746a2ec3d90a11d21abd8a114746dfTatuya JINMEI 神明達哉 from_addr = (struct sockaddr *)&from_addr_ss;
f620c5e527746a2ec3d90a11d21abd8a114746dfTatuya JINMEI 神明達哉 addr_len = sizeof(from_addr_ss);
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafsson
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafsson if ((numbytes = recvfrom(sockfd, in_buf, MAX_BUFFER_LEN,
f620c5e527746a2ec3d90a11d21abd8a114746dfTatuya JINMEI 神明達哉 0, from_addr, &addr_len)) == -1) {
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafsson fprintf(stderr, "Error receiving datagram\n");
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafsson return;
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafsson }
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafsson
384fef003441df4067b773688237700513390f9eEvan Hunt if (numbytes < DNS_HEADERLEN) {
384fef003441df4067b773688237700513390f9eEvan Hunt if (verbose)
384fef003441df4067b773688237700513390f9eEvan Hunt fprintf(stderr, "Malformed response\n");
384fef003441df4067b773688237700513390f9eEvan Hunt return;
384fef003441df4067b773688237700513390f9eEvan Hunt }
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafsson resp_id = get_uint16(in_buf);
81e805888bcd4bd2a6d5c986fade6ffdf7d88a54Andreas Gustafsson flags = get_uint16(in_buf + 2);
384fef003441df4067b773688237700513390f9eEvan Hunt qnamelen = dn_expand(in_buf, in_buf + numbytes, in_buf + DNS_HEADERLEN,
384fef003441df4067b773688237700513390f9eEvan Hunt qname, MAX_DOMAIN_LEN);
384fef003441df4067b773688237700513390f9eEvan Hunt if (qnamelen == -1) {
384fef003441df4067b773688237700513390f9eEvan Hunt if (verbose)
384fef003441df4067b773688237700513390f9eEvan Hunt fprintf(stderr,
384fef003441df4067b773688237700513390f9eEvan Hunt "Failed to retrieve qname from response\n");
384fef003441df4067b773688237700513390f9eEvan Hunt return;
384fef003441df4067b773688237700513390f9eEvan Hunt }
384fef003441df4067b773688237700513390f9eEvan Hunt if (numbytes < DNS_HEADERLEN + qnamelen + 2) {
384fef003441df4067b773688237700513390f9eEvan Hunt if (verbose)
384fef003441df4067b773688237700513390f9eEvan Hunt fprintf(stderr, "Malformed response\n");
384fef003441df4067b773688237700513390f9eEvan Hunt return;
384fef003441df4067b773688237700513390f9eEvan Hunt }
384fef003441df4067b773688237700513390f9eEvan Hunt qtype = get_uint16(in_buf + DNS_HEADERLEN + qnamelen);
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafsson
384fef003441df4067b773688237700513390f9eEvan Hunt register_response(resp_id, flags & 0xF, qname, qtype);
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafsson}
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafsson
f620c5e527746a2ec3d90a11d21abd8a114746dfTatuya JINMEI 神明達哉/*
f620c5e527746a2ec3d90a11d21abd8a114746dfTatuya JINMEI 神明達哉 * data_available:
f620c5e527746a2ec3d90a11d21abd8a114746dfTatuya JINMEI 神明達哉 * Is there data available on the given file descriptor?
f620c5e527746a2ec3d90a11d21abd8a114746dfTatuya JINMEI 神明達哉 *
f620c5e527746a2ec3d90a11d21abd8a114746dfTatuya JINMEI 神明達哉 * Return TRUE if there is
f620c5e527746a2ec3d90a11d21abd8a114746dfTatuya JINMEI 神明達哉 * Return FALSE otherwise
f620c5e527746a2ec3d90a11d21abd8a114746dfTatuya JINMEI 神明達哉 */
f620c5e527746a2ec3d90a11d21abd8a114746dfTatuya JINMEI 神明達哉int
f620c5e527746a2ec3d90a11d21abd8a114746dfTatuya JINMEI 神明達哉data_available(double wait) {
f620c5e527746a2ec3d90a11d21abd8a114746dfTatuya JINMEI 神明達哉 fd_set read_fds;
f620c5e527746a2ec3d90a11d21abd8a114746dfTatuya JINMEI 神明達哉 struct timeval tv;
f620c5e527746a2ec3d90a11d21abd8a114746dfTatuya JINMEI 神明達哉 int retval;
f620c5e527746a2ec3d90a11d21abd8a114746dfTatuya JINMEI 神明達哉 int available = FALSE;
f620c5e527746a2ec3d90a11d21abd8a114746dfTatuya JINMEI 神明達哉 int maxfd = -1;
f620c5e527746a2ec3d90a11d21abd8a114746dfTatuya JINMEI 神明達哉
f620c5e527746a2ec3d90a11d21abd8a114746dfTatuya JINMEI 神明達哉 /* Set list of file descriptors */
f620c5e527746a2ec3d90a11d21abd8a114746dfTatuya JINMEI 神明達哉 FD_ZERO(&read_fds);
f620c5e527746a2ec3d90a11d21abd8a114746dfTatuya JINMEI 神明達哉 if (socket4 != -1) {
f620c5e527746a2ec3d90a11d21abd8a114746dfTatuya JINMEI 神明達哉 FD_SET(socket4, &read_fds);
f620c5e527746a2ec3d90a11d21abd8a114746dfTatuya JINMEI 神明達哉 maxfd = socket4;
f620c5e527746a2ec3d90a11d21abd8a114746dfTatuya JINMEI 神明達哉 }
f620c5e527746a2ec3d90a11d21abd8a114746dfTatuya JINMEI 神明達哉 if (socket6 != -1) {
f620c5e527746a2ec3d90a11d21abd8a114746dfTatuya JINMEI 神明達哉 FD_SET(socket6, &read_fds);
f620c5e527746a2ec3d90a11d21abd8a114746dfTatuya JINMEI 神明達哉 if (maxfd == -1 || maxfd < socket6)
f620c5e527746a2ec3d90a11d21abd8a114746dfTatuya JINMEI 神明達哉 maxfd = socket6;
f620c5e527746a2ec3d90a11d21abd8a114746dfTatuya JINMEI 神明達哉 }
f620c5e527746a2ec3d90a11d21abd8a114746dfTatuya JINMEI 神明達哉
f620c5e527746a2ec3d90a11d21abd8a114746dfTatuya JINMEI 神明達哉 if ((wait > 0.0) && (wait < (double)LONG_MAX)) {
f620c5e527746a2ec3d90a11d21abd8a114746dfTatuya JINMEI 神明達哉 tv.tv_sec = (long)floor(wait);
f620c5e527746a2ec3d90a11d21abd8a114746dfTatuya JINMEI 神明達哉 tv.tv_usec = (long)(1000000.0 * (wait - floor(wait)));
f620c5e527746a2ec3d90a11d21abd8a114746dfTatuya JINMEI 神明達哉 } else {
f620c5e527746a2ec3d90a11d21abd8a114746dfTatuya JINMEI 神明達哉 tv.tv_sec = 0;
f620c5e527746a2ec3d90a11d21abd8a114746dfTatuya JINMEI 神明達哉 tv.tv_usec = 0;
f620c5e527746a2ec3d90a11d21abd8a114746dfTatuya JINMEI 神明達哉 }
f620c5e527746a2ec3d90a11d21abd8a114746dfTatuya JINMEI 神明達哉
f620c5e527746a2ec3d90a11d21abd8a114746dfTatuya JINMEI 神明達哉 retval = select(maxfd + 1, &read_fds, NULL, NULL, &tv);
f620c5e527746a2ec3d90a11d21abd8a114746dfTatuya JINMEI 神明達哉
f620c5e527746a2ec3d90a11d21abd8a114746dfTatuya JINMEI 神明達哉 if (socket4 != -1 && FD_ISSET(socket4, &read_fds)) {
f620c5e527746a2ec3d90a11d21abd8a114746dfTatuya JINMEI 神明達哉 available = TRUE;
f620c5e527746a2ec3d90a11d21abd8a114746dfTatuya JINMEI 神明達哉 process_single_response(socket4);
f620c5e527746a2ec3d90a11d21abd8a114746dfTatuya JINMEI 神明達哉 }
f620c5e527746a2ec3d90a11d21abd8a114746dfTatuya JINMEI 神明達哉 if (socket6 != -1 && FD_ISSET(socket6, &read_fds)) {
f620c5e527746a2ec3d90a11d21abd8a114746dfTatuya JINMEI 神明達哉 available = TRUE;
f620c5e527746a2ec3d90a11d21abd8a114746dfTatuya JINMEI 神明達哉 process_single_response(socket6);
f620c5e527746a2ec3d90a11d21abd8a114746dfTatuya JINMEI 神明達哉 }
f620c5e527746a2ec3d90a11d21abd8a114746dfTatuya JINMEI 神明達哉
f620c5e527746a2ec3d90a11d21abd8a114746dfTatuya JINMEI 神明達哉 return (available);
f620c5e527746a2ec3d90a11d21abd8a114746dfTatuya JINMEI 神明達哉}
f620c5e527746a2ec3d90a11d21abd8a114746dfTatuya JINMEI 神明達哉
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafsson/*
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafsson * process_responses:
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafsson * Go through any/all received responses and remove them from the list of
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafsson * open queries (set in_use = FALSE for their entry in status[]), also
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafsson * decrementing the number of outstanding queries.
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafsson */
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafssonvoid
b89095ba15e9163f2bacc46239d8f69d1a2b9ad3Tatuya JINMEI 神明達哉process_responses(int adjust_rate) {
b89095ba15e9163f2bacc46239d8f69d1a2b9ad3Tatuya JINMEI 神明達哉 double wait;
b89095ba15e9163f2bacc46239d8f69d1a2b9ad3Tatuya JINMEI 神明達哉 struct timeval now, waituntil;
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafsson double first_packet_wait = RESPONSE_BLOCKING_WAIT_TIME;
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafsson unsigned int outstanding = queries_outstanding();
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafsson
b89095ba15e9163f2bacc46239d8f69d1a2b9ad3Tatuya JINMEI 神明達哉 if (adjust_rate == TRUE) {
b89095ba15e9163f2bacc46239d8f69d1a2b9ad3Tatuya JINMEI 神明達哉 double u;
b89095ba15e9163f2bacc46239d8f69d1a2b9ad3Tatuya JINMEI 神明達哉
b89095ba15e9163f2bacc46239d8f69d1a2b9ad3Tatuya JINMEI 神明達哉 u = time_of_first_query_sec +
b89095ba15e9163f2bacc46239d8f69d1a2b9ad3Tatuya JINMEI 神明達哉 query_interval * num_queries_sent;
b89095ba15e9163f2bacc46239d8f69d1a2b9ad3Tatuya JINMEI 神明達哉 waituntil.tv_sec = (long)floor(u);
b89095ba15e9163f2bacc46239d8f69d1a2b9ad3Tatuya JINMEI 神明達哉 waituntil.tv_usec = (long)(1000000.0 * (u - waituntil.tv_sec));
b89095ba15e9163f2bacc46239d8f69d1a2b9ad3Tatuya JINMEI 神明達哉
b89095ba15e9163f2bacc46239d8f69d1a2b9ad3Tatuya JINMEI 神明達哉 /*
b89095ba15e9163f2bacc46239d8f69d1a2b9ad3Tatuya JINMEI 神明達哉 * Wait until a response arrives or the specified limit is
b89095ba15e9163f2bacc46239d8f69d1a2b9ad3Tatuya JINMEI 神明達哉 * reached.
b89095ba15e9163f2bacc46239d8f69d1a2b9ad3Tatuya JINMEI 神明達哉 */
b89095ba15e9163f2bacc46239d8f69d1a2b9ad3Tatuya JINMEI 神明達哉 while (1) {
b89095ba15e9163f2bacc46239d8f69d1a2b9ad3Tatuya JINMEI 神明達哉 set_timenow(&now);
b89095ba15e9163f2bacc46239d8f69d1a2b9ad3Tatuya JINMEI 神明達哉 wait = difftv(waituntil, now);
b89095ba15e9163f2bacc46239d8f69d1a2b9ad3Tatuya JINMEI 神明達哉 if (wait <= 0)
b89095ba15e9163f2bacc46239d8f69d1a2b9ad3Tatuya JINMEI 神明達哉 wait = 0.0;
b89095ba15e9163f2bacc46239d8f69d1a2b9ad3Tatuya JINMEI 神明達哉 if (data_available(wait) != TRUE)
b89095ba15e9163f2bacc46239d8f69d1a2b9ad3Tatuya JINMEI 神明達哉 break;
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafsson
b89095ba15e9163f2bacc46239d8f69d1a2b9ad3Tatuya JINMEI 神明達哉 /*
b89095ba15e9163f2bacc46239d8f69d1a2b9ad3Tatuya JINMEI 神明達哉 * We have reached the limit. Read as many responses
b89095ba15e9163f2bacc46239d8f69d1a2b9ad3Tatuya JINMEI 神明達哉 * as possible without waiting, and exit.
b89095ba15e9163f2bacc46239d8f69d1a2b9ad3Tatuya JINMEI 神明達哉 */
b89095ba15e9163f2bacc46239d8f69d1a2b9ad3Tatuya JINMEI 神明達哉 if (wait == 0) {
b89095ba15e9163f2bacc46239d8f69d1a2b9ad3Tatuya JINMEI 神明達哉 while (data_available(0.0) == TRUE)
b89095ba15e9163f2bacc46239d8f69d1a2b9ad3Tatuya JINMEI 神明達哉 ;
b89095ba15e9163f2bacc46239d8f69d1a2b9ad3Tatuya JINMEI 神明達哉 break;
b89095ba15e9163f2bacc46239d8f69d1a2b9ad3Tatuya JINMEI 神明達哉 }
b89095ba15e9163f2bacc46239d8f69d1a2b9ad3Tatuya JINMEI 神明達哉 }
b89095ba15e9163f2bacc46239d8f69d1a2b9ad3Tatuya JINMEI 神明達哉 } else {
b89095ba15e9163f2bacc46239d8f69d1a2b9ad3Tatuya JINMEI 神明達哉 /*
b89095ba15e9163f2bacc46239d8f69d1a2b9ad3Tatuya JINMEI 神明達哉 * Don't block waiting for packets at all if we aren't
b89095ba15e9163f2bacc46239d8f69d1a2b9ad3Tatuya JINMEI 神明達哉 * looking for any responses or if we are now able to send new
b89095ba15e9163f2bacc46239d8f69d1a2b9ad3Tatuya JINMEI 神明達哉 * queries.
b89095ba15e9163f2bacc46239d8f69d1a2b9ad3Tatuya JINMEI 神明達哉 */
b89095ba15e9163f2bacc46239d8f69d1a2b9ad3Tatuya JINMEI 神明達哉 if ((outstanding == 0) ||
b89095ba15e9163f2bacc46239d8f69d1a2b9ad3Tatuya JINMEI 神明達哉 (outstanding < max_queries_outstanding)) {
b89095ba15e9163f2bacc46239d8f69d1a2b9ad3Tatuya JINMEI 神明達哉 first_packet_wait = 0.0;
b89095ba15e9163f2bacc46239d8f69d1a2b9ad3Tatuya JINMEI 神明達哉 }
b89095ba15e9163f2bacc46239d8f69d1a2b9ad3Tatuya JINMEI 神明達哉
b89095ba15e9163f2bacc46239d8f69d1a2b9ad3Tatuya JINMEI 神明達哉 if (data_available(first_packet_wait) == TRUE) {
b89095ba15e9163f2bacc46239d8f69d1a2b9ad3Tatuya JINMEI 神明達哉 while (data_available(0.0) == TRUE)
b89095ba15e9163f2bacc46239d8f69d1a2b9ad3Tatuya JINMEI 神明達哉 ;
b89095ba15e9163f2bacc46239d8f69d1a2b9ad3Tatuya JINMEI 神明達哉 }
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafsson }
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafsson}
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafsson
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafsson/*
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafsson * retire_old_queries:
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafsson * Go through the list of open queries (status[]) and remove any queries
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafsson * (i.e. set in_use = FALSE) which are older than the timeout, decrementing
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafsson * the number of queries outstanding for each one removed.
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafsson */
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafssonvoid
b89095ba15e9163f2bacc46239d8f69d1a2b9ad3Tatuya JINMEI 神明達哉retire_old_queries(int sending) {
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafsson unsigned int count = 0;
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafsson struct timeval curr_time;
b89095ba15e9163f2bacc46239d8f69d1a2b9ad3Tatuya JINMEI 神明達哉 double timeout = query_timeout;
b89095ba15e9163f2bacc46239d8f69d1a2b9ad3Tatuya JINMEI 神明達哉 int timeout_reduced = FALSE;
b89095ba15e9163f2bacc46239d8f69d1a2b9ad3Tatuya JINMEI 神明達哉
b89095ba15e9163f2bacc46239d8f69d1a2b9ad3Tatuya JINMEI 神明達哉 /*
b89095ba15e9163f2bacc46239d8f69d1a2b9ad3Tatuya JINMEI 神明達哉 * If we have target qps and would not be able to send any packets
b89095ba15e9163f2bacc46239d8f69d1a2b9ad3Tatuya JINMEI 神明達哉 * due to buffer full, check whether we are behind the schedule.
b89095ba15e9163f2bacc46239d8f69d1a2b9ad3Tatuya JINMEI 神明達哉 * If we are, purge some queries more aggressively.
b89095ba15e9163f2bacc46239d8f69d1a2b9ad3Tatuya JINMEI 神明達哉 */
b89095ba15e9163f2bacc46239d8f69d1a2b9ad3Tatuya JINMEI 神明達哉 if (target_qps > 0 && sending == TRUE && count == 0 &&
b89095ba15e9163f2bacc46239d8f69d1a2b9ad3Tatuya JINMEI 神明達哉 queries_outstanding() == max_queries_outstanding) {
b89095ba15e9163f2bacc46239d8f69d1a2b9ad3Tatuya JINMEI 神明達哉 struct timeval next, now;
b89095ba15e9163f2bacc46239d8f69d1a2b9ad3Tatuya JINMEI 神明達哉 double n;
b89095ba15e9163f2bacc46239d8f69d1a2b9ad3Tatuya JINMEI 神明達哉
b89095ba15e9163f2bacc46239d8f69d1a2b9ad3Tatuya JINMEI 神明達哉 n = time_of_first_query_sec +
b89095ba15e9163f2bacc46239d8f69d1a2b9ad3Tatuya JINMEI 神明達哉 query_interval * num_queries_sent;
b89095ba15e9163f2bacc46239d8f69d1a2b9ad3Tatuya JINMEI 神明達哉 next.tv_sec = (long)floor(n);
b89095ba15e9163f2bacc46239d8f69d1a2b9ad3Tatuya JINMEI 神明達哉 next.tv_usec = (long)(1000000.0 * (n - next.tv_sec));
b89095ba15e9163f2bacc46239d8f69d1a2b9ad3Tatuya JINMEI 神明達哉
b89095ba15e9163f2bacc46239d8f69d1a2b9ad3Tatuya JINMEI 神明達哉 set_timenow(&now);
b89095ba15e9163f2bacc46239d8f69d1a2b9ad3Tatuya JINMEI 神明達哉 if (difftv(next, now) <= 0) {
b89095ba15e9163f2bacc46239d8f69d1a2b9ad3Tatuya JINMEI 神明達哉 timeout_reduced = TRUE;
b89095ba15e9163f2bacc46239d8f69d1a2b9ad3Tatuya JINMEI 神明達哉 timeout = 0.001; /* XXX: ad-hoc value */
b89095ba15e9163f2bacc46239d8f69d1a2b9ad3Tatuya JINMEI 神明達哉 }
b89095ba15e9163f2bacc46239d8f69d1a2b9ad3Tatuya JINMEI 神明達哉 }
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafsson
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafsson set_timenow(&curr_time);
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafsson
b89095ba15e9163f2bacc46239d8f69d1a2b9ad3Tatuya JINMEI 神明達哉 for (; count < query_status_allocated; count++) {
384fef003441df4067b773688237700513390f9eEvan Hunt if ((status[count].in_use == TRUE) &&
384fef003441df4067b773688237700513390f9eEvan Hunt (difftv(curr_time,
384fef003441df4067b773688237700513390f9eEvan Hunt status[count].sent_timestamp) >= (double)timeout))
384fef003441df4067b773688237700513390f9eEvan Hunt {
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafsson status[count].in_use = FALSE;
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafsson num_queries_outstanding--;
384fef003441df4067b773688237700513390f9eEvan Hunt
384fef003441df4067b773688237700513390f9eEvan Hunt if (timeout_queries != NULL) {
384fef003441df4067b773688237700513390f9eEvan Hunt struct query_mininfo *qi;
384fef003441df4067b773688237700513390f9eEvan Hunt
384fef003441df4067b773688237700513390f9eEvan Hunt qi = &timeout_queries[status[count].id];
384fef003441df4067b773688237700513390f9eEvan Hunt if (qi->qtype != -1) {
384fef003441df4067b773688237700513390f9eEvan Hunt /* now really retire this query */
384fef003441df4067b773688237700513390f9eEvan Hunt num_queries_timed_out++;
384fef003441df4067b773688237700513390f9eEvan Hunt num_queries_timed_out_interval++;
384fef003441df4067b773688237700513390f9eEvan Hunt }
384fef003441df4067b773688237700513390f9eEvan Hunt qi->qtype = status[count].qtype;
384fef003441df4067b773688237700513390f9eEvan Hunt qi->sent_timestamp =
384fef003441df4067b773688237700513390f9eEvan Hunt status[count].sent_timestamp;
384fef003441df4067b773688237700513390f9eEvan Hunt strcpy(qi->qname, status[count].qname);
384fef003441df4067b773688237700513390f9eEvan Hunt } else {
384fef003441df4067b773688237700513390f9eEvan Hunt num_queries_timed_out++;
384fef003441df4067b773688237700513390f9eEvan Hunt num_queries_timed_out_interval++;
384fef003441df4067b773688237700513390f9eEvan Hunt }
b89095ba15e9163f2bacc46239d8f69d1a2b9ad3Tatuya JINMEI 神明達哉
b89095ba15e9163f2bacc46239d8f69d1a2b9ad3Tatuya JINMEI 神明達哉 if (timeout_reduced == FALSE) {
b89095ba15e9163f2bacc46239d8f69d1a2b9ad3Tatuya JINMEI 神明達哉 if (status[count].desc) {
b89095ba15e9163f2bacc46239d8f69d1a2b9ad3Tatuya JINMEI 神明達哉 printf("> T %s\n", status[count].desc);
b89095ba15e9163f2bacc46239d8f69d1a2b9ad3Tatuya JINMEI 神明達哉 free(status[count].desc);
b89095ba15e9163f2bacc46239d8f69d1a2b9ad3Tatuya JINMEI 神明達哉 } else {
b89095ba15e9163f2bacc46239d8f69d1a2b9ad3Tatuya JINMEI 神明達哉 printf("[Timeout] Query timed out: "
b89095ba15e9163f2bacc46239d8f69d1a2b9ad3Tatuya JINMEI 神明達哉 "msg id %u\n",
b89095ba15e9163f2bacc46239d8f69d1a2b9ad3Tatuya JINMEI 神明達哉 status[count].id);
b89095ba15e9163f2bacc46239d8f69d1a2b9ad3Tatuya JINMEI 神明達哉 }
81e805888bcd4bd2a6d5c986fade6ffdf7d88a54Andreas Gustafsson }
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafsson }
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafsson }
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafsson}
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafsson
b89095ba15e9163f2bacc46239d8f69d1a2b9ad3Tatuya JINMEI 神明達哉/*
b89095ba15e9163f2bacc46239d8f69d1a2b9ad3Tatuya JINMEI 神明達哉 * print_histogram
b89095ba15e9163f2bacc46239d8f69d1a2b9ad3Tatuya JINMEI 神明達哉 * Print RTT histogram to the specified file in the gnuplot format
b89095ba15e9163f2bacc46239d8f69d1a2b9ad3Tatuya JINMEI 神明達哉 */
b89095ba15e9163f2bacc46239d8f69d1a2b9ad3Tatuya JINMEI 神明達哉void
b89095ba15e9163f2bacc46239d8f69d1a2b9ad3Tatuya JINMEI 神明達哉print_histogram(unsigned int total) {
b89095ba15e9163f2bacc46239d8f69d1a2b9ad3Tatuya JINMEI 神明達哉 int i;
b89095ba15e9163f2bacc46239d8f69d1a2b9ad3Tatuya JINMEI 神明達哉 double ratio;
b89095ba15e9163f2bacc46239d8f69d1a2b9ad3Tatuya JINMEI 神明達哉 FILE *fp;
b89095ba15e9163f2bacc46239d8f69d1a2b9ad3Tatuya JINMEI 神明達哉
b89095ba15e9163f2bacc46239d8f69d1a2b9ad3Tatuya JINMEI 神明達哉 if (rtt_histogram_file == NULL || rttarray == NULL)
b89095ba15e9163f2bacc46239d8f69d1a2b9ad3Tatuya JINMEI 神明達哉 return;
b89095ba15e9163f2bacc46239d8f69d1a2b9ad3Tatuya JINMEI 神明達哉
b89095ba15e9163f2bacc46239d8f69d1a2b9ad3Tatuya JINMEI 神明達哉 fp = fopen((const char *)rtt_histogram_file, "w+");
b89095ba15e9163f2bacc46239d8f69d1a2b9ad3Tatuya JINMEI 神明達哉 if (fp == NULL) {
b89095ba15e9163f2bacc46239d8f69d1a2b9ad3Tatuya JINMEI 神明達哉 fprintf(stderr, "Error opening RTT histogram file: %s\n",
b89095ba15e9163f2bacc46239d8f69d1a2b9ad3Tatuya JINMEI 神明達哉 rtt_histogram_file);
b89095ba15e9163f2bacc46239d8f69d1a2b9ad3Tatuya JINMEI 神明達哉 return;
b89095ba15e9163f2bacc46239d8f69d1a2b9ad3Tatuya JINMEI 神明達哉 }
b89095ba15e9163f2bacc46239d8f69d1a2b9ad3Tatuya JINMEI 神明達哉
b89095ba15e9163f2bacc46239d8f69d1a2b9ad3Tatuya JINMEI 神明達哉 for (i = 0; i < rttarray_size; i++) {
b89095ba15e9163f2bacc46239d8f69d1a2b9ad3Tatuya JINMEI 神明達哉 ratio = ((double)rttarray[i] / (double)total) * 100;
b89095ba15e9163f2bacc46239d8f69d1a2b9ad3Tatuya JINMEI 神明達哉 fprintf(fp, "%.6lf %.3lf\n",
b89095ba15e9163f2bacc46239d8f69d1a2b9ad3Tatuya JINMEI 神明達哉 (double)(i * rttarray_unit) +
b89095ba15e9163f2bacc46239d8f69d1a2b9ad3Tatuya JINMEI 神明達哉 (double)rttarray_unit / 2,
b89095ba15e9163f2bacc46239d8f69d1a2b9ad3Tatuya JINMEI 神明達哉 ratio);
b89095ba15e9163f2bacc46239d8f69d1a2b9ad3Tatuya JINMEI 神明達哉 }
b89095ba15e9163f2bacc46239d8f69d1a2b9ad3Tatuya JINMEI 神明達哉
b89095ba15e9163f2bacc46239d8f69d1a2b9ad3Tatuya JINMEI 神明達哉 (void)fclose(fp);
b89095ba15e9163f2bacc46239d8f69d1a2b9ad3Tatuya JINMEI 神明達哉}
b89095ba15e9163f2bacc46239d8f69d1a2b9ad3Tatuya JINMEI 神明達哉
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafsson/*
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafsson * print_statistics:
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafsson * Print out statistics based on the results of the test
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafsson */
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafssonvoid
b89095ba15e9163f2bacc46239d8f69d1a2b9ad3Tatuya JINMEI 神明達哉print_statistics(int intermediate, unsigned int sent, unsigned int timed_out,
b89095ba15e9163f2bacc46239d8f69d1a2b9ad3Tatuya JINMEI 神明達哉 unsigned int possibly_delayed,
b89095ba15e9163f2bacc46239d8f69d1a2b9ad3Tatuya JINMEI 神明達哉 struct timeval *first_query,
b89095ba15e9163f2bacc46239d8f69d1a2b9ad3Tatuya JINMEI 神明達哉 struct timeval *program_start,
b89095ba15e9163f2bacc46239d8f69d1a2b9ad3Tatuya JINMEI 神明達哉 struct timeval *end_perf, struct timeval *end_query,
384fef003441df4067b773688237700513390f9eEvan Hunt unsigned int rcounted, double rmax, double rmin, double rtotal,
b89095ba15e9163f2bacc46239d8f69d1a2b9ad3Tatuya JINMEI 神明達哉 unsigned int roverflows, unsigned int *rarray)
b89095ba15e9163f2bacc46239d8f69d1a2b9ad3Tatuya JINMEI 神明達哉{
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafsson unsigned int num_queries_completed;
b89095ba15e9163f2bacc46239d8f69d1a2b9ad3Tatuya JINMEI 神明達哉 double per_lost, per_completed, per_lost2, per_completed2;
b89095ba15e9163f2bacc46239d8f69d1a2b9ad3Tatuya JINMEI 神明達哉 double run_time, queries_per_sec, queries_per_sec2;
b89095ba15e9163f2bacc46239d8f69d1a2b9ad3Tatuya JINMEI 神明達哉 double queries_per_sec_total;
b89095ba15e9163f2bacc46239d8f69d1a2b9ad3Tatuya JINMEI 神明達哉 double rtt_average, rtt_stddev;
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafsson struct timeval start_time;
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafsson
b89095ba15e9163f2bacc46239d8f69d1a2b9ad3Tatuya JINMEI 神明達哉 num_queries_completed = sent - timed_out;
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafsson
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafsson if (num_queries_completed == 0) {
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafsson per_lost = 0.0;
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafsson per_completed = 0.0;
b89095ba15e9163f2bacc46239d8f69d1a2b9ad3Tatuya JINMEI 神明達哉
b89095ba15e9163f2bacc46239d8f69d1a2b9ad3Tatuya JINMEI 神明達哉 per_lost2 = 0.0;
b89095ba15e9163f2bacc46239d8f69d1a2b9ad3Tatuya JINMEI 神明達哉 per_completed2 = 0.0;
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafsson } else {
b89095ba15e9163f2bacc46239d8f69d1a2b9ad3Tatuya JINMEI 神明達哉 per_lost = (100.0 * (double)timed_out) / (double)sent;
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafsson per_completed = 100.0 - per_lost;
b89095ba15e9163f2bacc46239d8f69d1a2b9ad3Tatuya JINMEI 神明達哉
b89095ba15e9163f2bacc46239d8f69d1a2b9ad3Tatuya JINMEI 神明達哉 per_lost2 = (100.0 * (double)(timed_out - possibly_delayed))
b89095ba15e9163f2bacc46239d8f69d1a2b9ad3Tatuya JINMEI 神明達哉 / (double)sent;
b89095ba15e9163f2bacc46239d8f69d1a2b9ad3Tatuya JINMEI 神明達哉 per_completed2 = 100 - per_lost2;
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafsson }
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafsson
b89095ba15e9163f2bacc46239d8f69d1a2b9ad3Tatuya JINMEI 神明達哉 if (sent == 0) {
b89095ba15e9163f2bacc46239d8f69d1a2b9ad3Tatuya JINMEI 神明達哉 start_time.tv_sec = program_start->tv_sec;
b89095ba15e9163f2bacc46239d8f69d1a2b9ad3Tatuya JINMEI 神明達哉 start_time.tv_usec = program_start->tv_usec;
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafsson run_time = 0.0;
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafsson queries_per_sec = 0.0;
b89095ba15e9163f2bacc46239d8f69d1a2b9ad3Tatuya JINMEI 神明達哉 queries_per_sec2 = 0.0;
b89095ba15e9163f2bacc46239d8f69d1a2b9ad3Tatuya JINMEI 神明達哉 queries_per_sec_total = 0.0;
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafsson } else {
b89095ba15e9163f2bacc46239d8f69d1a2b9ad3Tatuya JINMEI 神明達哉 start_time.tv_sec = first_query->tv_sec;
b89095ba15e9163f2bacc46239d8f69d1a2b9ad3Tatuya JINMEI 神明達哉 start_time.tv_usec = first_query->tv_usec;
b89095ba15e9163f2bacc46239d8f69d1a2b9ad3Tatuya JINMEI 神明達哉 run_time = difftv(*end_perf, *first_query);
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafsson queries_per_sec = (double)num_queries_completed / run_time;
b89095ba15e9163f2bacc46239d8f69d1a2b9ad3Tatuya JINMEI 神明達哉 queries_per_sec2 = (double)(num_queries_completed +
b89095ba15e9163f2bacc46239d8f69d1a2b9ad3Tatuya JINMEI 神明達哉 possibly_delayed) / run_time;
b89095ba15e9163f2bacc46239d8f69d1a2b9ad3Tatuya JINMEI 神明達哉
b89095ba15e9163f2bacc46239d8f69d1a2b9ad3Tatuya JINMEI 神明達哉 queries_per_sec_total = (double)sent /
b89095ba15e9163f2bacc46239d8f69d1a2b9ad3Tatuya JINMEI 神明達哉 difftv(*end_query, *first_query);
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafsson }
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafsson
384fef003441df4067b773688237700513390f9eEvan Hunt if (rcounted > 0) {
b89095ba15e9163f2bacc46239d8f69d1a2b9ad3Tatuya JINMEI 神明達哉 int i;
b89095ba15e9163f2bacc46239d8f69d1a2b9ad3Tatuya JINMEI 神明達哉 double sum = 0;
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafsson
384fef003441df4067b773688237700513390f9eEvan Hunt rtt_average = rtotal / (double)rcounted;
b89095ba15e9163f2bacc46239d8f69d1a2b9ad3Tatuya JINMEI 神明達哉 for (i = 0; i < rttarray_size; i++) {
b89095ba15e9163f2bacc46239d8f69d1a2b9ad3Tatuya JINMEI 神明達哉 if (rarray[i] != 0) {
b89095ba15e9163f2bacc46239d8f69d1a2b9ad3Tatuya JINMEI 神明達哉 double mean, diff;
b89095ba15e9163f2bacc46239d8f69d1a2b9ad3Tatuya JINMEI 神明達哉
b89095ba15e9163f2bacc46239d8f69d1a2b9ad3Tatuya JINMEI 神明達哉 mean = (double)(i * rttarray_unit) +
b89095ba15e9163f2bacc46239d8f69d1a2b9ad3Tatuya JINMEI 神明達哉 (double)rttarray_unit / 2;
b89095ba15e9163f2bacc46239d8f69d1a2b9ad3Tatuya JINMEI 神明達哉 diff = rtt_average - (mean / 1000000.0);
b89095ba15e9163f2bacc46239d8f69d1a2b9ad3Tatuya JINMEI 神明達哉 sum += (diff * diff) * rarray[i];
b89095ba15e9163f2bacc46239d8f69d1a2b9ad3Tatuya JINMEI 神明達哉 }
b89095ba15e9163f2bacc46239d8f69d1a2b9ad3Tatuya JINMEI 神明達哉 }
384fef003441df4067b773688237700513390f9eEvan Hunt rtt_stddev = sqrt(sum / (double)rcounted);
b89095ba15e9163f2bacc46239d8f69d1a2b9ad3Tatuya JINMEI 神明達哉 } else {
b89095ba15e9163f2bacc46239d8f69d1a2b9ad3Tatuya JINMEI 神明達哉 rtt_average = 0.0;
b89095ba15e9163f2bacc46239d8f69d1a2b9ad3Tatuya JINMEI 神明達哉 rtt_stddev = 0.0;
b89095ba15e9163f2bacc46239d8f69d1a2b9ad3Tatuya JINMEI 神明達哉 }
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafsson
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafsson printf("\n");
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafsson
b89095ba15e9163f2bacc46239d8f69d1a2b9ad3Tatuya JINMEI 神明達哉 printf("%sStatistics:\n", intermediate ? "Intermediate " : "");
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafsson
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafsson printf("\n");
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafsson
b89095ba15e9163f2bacc46239d8f69d1a2b9ad3Tatuya JINMEI 神明達哉 if (!intermediate) {
b89095ba15e9163f2bacc46239d8f69d1a2b9ad3Tatuya JINMEI 神明達哉 printf(" Parse input file: %s\n",
b89095ba15e9163f2bacc46239d8f69d1a2b9ad3Tatuya JINMEI 神明達哉 ((run_only_once == TRUE) ? "once" : "multiple times"));
b89095ba15e9163f2bacc46239d8f69d1a2b9ad3Tatuya JINMEI 神明達哉 if (use_timelimit)
b89095ba15e9163f2bacc46239d8f69d1a2b9ad3Tatuya JINMEI 神明達哉 printf(" Run time limit: %u seconds\n",
b89095ba15e9163f2bacc46239d8f69d1a2b9ad3Tatuya JINMEI 神明達哉 run_timelimit);
b89095ba15e9163f2bacc46239d8f69d1a2b9ad3Tatuya JINMEI 神明達哉 if (run_only_once == FALSE)
b89095ba15e9163f2bacc46239d8f69d1a2b9ad3Tatuya JINMEI 神明達哉 printf(" Ran through file: %u times\n",
b89095ba15e9163f2bacc46239d8f69d1a2b9ad3Tatuya JINMEI 神明達哉 runs_through_file);
b89095ba15e9163f2bacc46239d8f69d1a2b9ad3Tatuya JINMEI 神明達哉 else
b89095ba15e9163f2bacc46239d8f69d1a2b9ad3Tatuya JINMEI 神明達哉 printf(" Ended due to: reaching %s\n",
b89095ba15e9163f2bacc46239d8f69d1a2b9ad3Tatuya JINMEI 神明達哉 ((runs_through_file == 0) ? "time limit"
b89095ba15e9163f2bacc46239d8f69d1a2b9ad3Tatuya JINMEI 神明達哉 : "end of file"));
b89095ba15e9163f2bacc46239d8f69d1a2b9ad3Tatuya JINMEI 神明達哉
b89095ba15e9163f2bacc46239d8f69d1a2b9ad3Tatuya JINMEI 神明達哉 printf("\n");
b89095ba15e9163f2bacc46239d8f69d1a2b9ad3Tatuya JINMEI 神明達哉 }
b89095ba15e9163f2bacc46239d8f69d1a2b9ad3Tatuya JINMEI 神明達哉
b89095ba15e9163f2bacc46239d8f69d1a2b9ad3Tatuya JINMEI 神明達哉 printf(" Queries sent: %u queries\n", sent);
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafsson printf(" Queries completed: %u queries\n", num_queries_completed);
b89095ba15e9163f2bacc46239d8f69d1a2b9ad3Tatuya JINMEI 神明達哉 printf(" Queries lost: %u queries\n", timed_out);
b89095ba15e9163f2bacc46239d8f69d1a2b9ad3Tatuya JINMEI 神明達哉 printf(" Queries delayed(?): %u queries\n", possibly_delayed);
b89095ba15e9163f2bacc46239d8f69d1a2b9ad3Tatuya JINMEI 神明達哉
b89095ba15e9163f2bacc46239d8f69d1a2b9ad3Tatuya JINMEI 神明達哉 printf("\n");
b89095ba15e9163f2bacc46239d8f69d1a2b9ad3Tatuya JINMEI 神明達哉
b89095ba15e9163f2bacc46239d8f69d1a2b9ad3Tatuya JINMEI 神明達哉 printf(" RTT max: %3.6lf sec\n", rmax);
b89095ba15e9163f2bacc46239d8f69d1a2b9ad3Tatuya JINMEI 神明達哉 printf(" RTT min: %3.6lf sec\n", rmin);
b89095ba15e9163f2bacc46239d8f69d1a2b9ad3Tatuya JINMEI 神明達哉 printf(" RTT average: %3.6lf sec\n", rtt_average);
b89095ba15e9163f2bacc46239d8f69d1a2b9ad3Tatuya JINMEI 神明達哉 printf(" RTT std deviation: %3.6lf sec\n", rtt_stddev);
b89095ba15e9163f2bacc46239d8f69d1a2b9ad3Tatuya JINMEI 神明達哉 printf(" RTT out of range: %u queries\n", roverflows);
b89095ba15e9163f2bacc46239d8f69d1a2b9ad3Tatuya JINMEI 神明達哉
b89095ba15e9163f2bacc46239d8f69d1a2b9ad3Tatuya JINMEI 神明達哉 if (!intermediate) /* XXX should we print this case also? */
b89095ba15e9163f2bacc46239d8f69d1a2b9ad3Tatuya JINMEI 神明達哉 print_histogram(num_queries_completed);
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafsson
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafsson printf("\n");
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafsson
e2cf0e8ff9ff37121518af5b34b9e4de7abbb47cMark Andrews if (countrcodes) {
e2cf0e8ff9ff37121518af5b34b9e4de7abbb47cMark Andrews unsigned int i;
e2cf0e8ff9ff37121518af5b34b9e4de7abbb47cMark Andrews
e2cf0e8ff9ff37121518af5b34b9e4de7abbb47cMark Andrews for (i = 0; i < 16; i++) {
e2cf0e8ff9ff37121518af5b34b9e4de7abbb47cMark Andrews if (rcodecounts[i] == 0)
e2cf0e8ff9ff37121518af5b34b9e4de7abbb47cMark Andrews continue;
e2cf0e8ff9ff37121518af5b34b9e4de7abbb47cMark Andrews printf(" Returned %8s: %u queries\n",
e2cf0e8ff9ff37121518af5b34b9e4de7abbb47cMark Andrews rcode_strings[i], rcodecounts[i]);
e2cf0e8ff9ff37121518af5b34b9e4de7abbb47cMark Andrews }
e2cf0e8ff9ff37121518af5b34b9e4de7abbb47cMark Andrews printf("\n");
e2cf0e8ff9ff37121518af5b34b9e4de7abbb47cMark Andrews }
e2cf0e8ff9ff37121518af5b34b9e4de7abbb47cMark Andrews
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafsson printf(" Percentage completed: %6.2lf%%\n", per_completed);
b89095ba15e9163f2bacc46239d8f69d1a2b9ad3Tatuya JINMEI 神明達哉 if (possibly_delayed > 0)
b89095ba15e9163f2bacc46239d8f69d1a2b9ad3Tatuya JINMEI 神明達哉 printf(" (w/ delayed qrys): %6.2lf%%\n", per_completed2);
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafsson printf(" Percentage lost: %6.2lf%%\n", per_lost);
b89095ba15e9163f2bacc46239d8f69d1a2b9ad3Tatuya JINMEI 神明達哉 if (possibly_delayed > 0)
b89095ba15e9163f2bacc46239d8f69d1a2b9ad3Tatuya JINMEI 神明達哉 printf(" (w/o delayed qrys): %6.2lf%%\n", per_lost2);
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafsson
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafsson printf("\n");
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafsson
f620c5e527746a2ec3d90a11d21abd8a114746dfTatuya JINMEI 神明達哉 printf(" Started at: %s",
f620c5e527746a2ec3d90a11d21abd8a114746dfTatuya JINMEI 神明達哉 ctime((const time_t *)&start_time.tv_sec));
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafsson printf(" Finished at: %s",
b89095ba15e9163f2bacc46239d8f69d1a2b9ad3Tatuya JINMEI 神明達哉 ctime((const time_t *)&end_perf->tv_sec));
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafsson printf(" Ran for: %.6lf seconds\n", run_time);
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafsson
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafsson printf("\n");
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafsson
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafsson printf(" Queries per second: %.6lf qps\n", queries_per_sec);
b89095ba15e9163f2bacc46239d8f69d1a2b9ad3Tatuya JINMEI 神明達哉 if (possibly_delayed > 0) {
b89095ba15e9163f2bacc46239d8f69d1a2b9ad3Tatuya JINMEI 神明達哉 printf(" (w/ delayed qrys): %.6lf qps\n",
b89095ba15e9163f2bacc46239d8f69d1a2b9ad3Tatuya JINMEI 神明達哉 queries_per_sec2);
b89095ba15e9163f2bacc46239d8f69d1a2b9ad3Tatuya JINMEI 神明達哉 }
b89095ba15e9163f2bacc46239d8f69d1a2b9ad3Tatuya JINMEI 神明達哉 if (target_qps > 0) {
b89095ba15e9163f2bacc46239d8f69d1a2b9ad3Tatuya JINMEI 神明達哉 printf(" Total QPS/target: %.6lf/%d qps\n",
b89095ba15e9163f2bacc46239d8f69d1a2b9ad3Tatuya JINMEI 神明達哉 queries_per_sec_total, target_qps);
b89095ba15e9163f2bacc46239d8f69d1a2b9ad3Tatuya JINMEI 神明達哉 }
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafsson
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafsson printf("\n");
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafsson}
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafsson
b89095ba15e9163f2bacc46239d8f69d1a2b9ad3Tatuya JINMEI 神明達哉void
b89095ba15e9163f2bacc46239d8f69d1a2b9ad3Tatuya JINMEI 神明達哉print_interval_statistics() {
b89095ba15e9163f2bacc46239d8f69d1a2b9ad3Tatuya JINMEI 神明達哉 struct timeval time_now;
b89095ba15e9163f2bacc46239d8f69d1a2b9ad3Tatuya JINMEI 神明達哉
b89095ba15e9163f2bacc46239d8f69d1a2b9ad3Tatuya JINMEI 神明達哉 if (use_timelimit == FALSE)
b89095ba15e9163f2bacc46239d8f69d1a2b9ad3Tatuya JINMEI 神明達哉 return;
b89095ba15e9163f2bacc46239d8f69d1a2b9ad3Tatuya JINMEI 神明達哉
b89095ba15e9163f2bacc46239d8f69d1a2b9ad3Tatuya JINMEI 神明達哉 if (setup_phase == TRUE)
b89095ba15e9163f2bacc46239d8f69d1a2b9ad3Tatuya JINMEI 神明達哉 return;
b89095ba15e9163f2bacc46239d8f69d1a2b9ad3Tatuya JINMEI 神明達哉
b89095ba15e9163f2bacc46239d8f69d1a2b9ad3Tatuya JINMEI 神明達哉 if (print_interval == 0)
b89095ba15e9163f2bacc46239d8f69d1a2b9ad3Tatuya JINMEI 神明達哉 return;
b89095ba15e9163f2bacc46239d8f69d1a2b9ad3Tatuya JINMEI 神明達哉
b89095ba15e9163f2bacc46239d8f69d1a2b9ad3Tatuya JINMEI 神明達哉 if (timelimit_reached() == TRUE)
b89095ba15e9163f2bacc46239d8f69d1a2b9ad3Tatuya JINMEI 神明達哉 return;
b89095ba15e9163f2bacc46239d8f69d1a2b9ad3Tatuya JINMEI 神明達哉
b89095ba15e9163f2bacc46239d8f69d1a2b9ad3Tatuya JINMEI 神明達哉 set_timenow(&time_now);
b89095ba15e9163f2bacc46239d8f69d1a2b9ad3Tatuya JINMEI 神明達哉 if (difftv(time_now, time_of_first_query_interval)
b89095ba15e9163f2bacc46239d8f69d1a2b9ad3Tatuya JINMEI 神明達哉 <= (double)print_interval)
b89095ba15e9163f2bacc46239d8f69d1a2b9ad3Tatuya JINMEI 神明達哉 return;
b89095ba15e9163f2bacc46239d8f69d1a2b9ad3Tatuya JINMEI 神明達哉
b89095ba15e9163f2bacc46239d8f69d1a2b9ad3Tatuya JINMEI 神明達哉 /* Don't count currently outstanding queries */
b89095ba15e9163f2bacc46239d8f69d1a2b9ad3Tatuya JINMEI 神明達哉 num_queries_sent_interval -= queries_outstanding();
b89095ba15e9163f2bacc46239d8f69d1a2b9ad3Tatuya JINMEI 神明達哉 print_statistics(TRUE, num_queries_sent_interval,
b89095ba15e9163f2bacc46239d8f69d1a2b9ad3Tatuya JINMEI 神明達哉 num_queries_timed_out_interval,
b89095ba15e9163f2bacc46239d8f69d1a2b9ad3Tatuya JINMEI 神明達哉 num_queries_possiblydelayed_interval,
b89095ba15e9163f2bacc46239d8f69d1a2b9ad3Tatuya JINMEI 神明達哉 &time_of_first_query_interval,
b89095ba15e9163f2bacc46239d8f69d1a2b9ad3Tatuya JINMEI 神明達哉 &time_of_first_query_interval, &time_now, &time_now,
384fef003441df4067b773688237700513390f9eEvan Hunt rtt_counted_interval, rtt_max_interval,
384fef003441df4067b773688237700513390f9eEvan Hunt rtt_min_interval, rtt_total_interval,
384fef003441df4067b773688237700513390f9eEvan Hunt rtt_overflows_interval, rttarray_interval);
b89095ba15e9163f2bacc46239d8f69d1a2b9ad3Tatuya JINMEI 神明達哉
b89095ba15e9163f2bacc46239d8f69d1a2b9ad3Tatuya JINMEI 神明達哉 /* Reset intermediate counters */
b89095ba15e9163f2bacc46239d8f69d1a2b9ad3Tatuya JINMEI 神明達哉 num_queries_sent_interval = 0;
b89095ba15e9163f2bacc46239d8f69d1a2b9ad3Tatuya JINMEI 神明達哉 num_queries_timed_out_interval = 0;
b89095ba15e9163f2bacc46239d8f69d1a2b9ad3Tatuya JINMEI 神明達哉 num_queries_possiblydelayed_interval = 0;
b89095ba15e9163f2bacc46239d8f69d1a2b9ad3Tatuya JINMEI 神明達哉 rtt_max_interval = -1;
b89095ba15e9163f2bacc46239d8f69d1a2b9ad3Tatuya JINMEI 神明達哉 rtt_min_interval = -1;
b89095ba15e9163f2bacc46239d8f69d1a2b9ad3Tatuya JINMEI 神明達哉 rtt_total_interval = 0.0;
384fef003441df4067b773688237700513390f9eEvan Hunt rtt_counted_interval = 0.0;
b89095ba15e9163f2bacc46239d8f69d1a2b9ad3Tatuya JINMEI 神明達哉 rtt_overflows_interval = 0;
b89095ba15e9163f2bacc46239d8f69d1a2b9ad3Tatuya JINMEI 神明達哉 if (rttarray_interval != NULL) {
b89095ba15e9163f2bacc46239d8f69d1a2b9ad3Tatuya JINMEI 神明達哉 memset(rttarray_interval, 0,
b89095ba15e9163f2bacc46239d8f69d1a2b9ad3Tatuya JINMEI 神明達哉 sizeof(rttarray_interval[0]) * rttarray_size);
b89095ba15e9163f2bacc46239d8f69d1a2b9ad3Tatuya JINMEI 神明達哉 }
b89095ba15e9163f2bacc46239d8f69d1a2b9ad3Tatuya JINMEI 神明達哉}
b89095ba15e9163f2bacc46239d8f69d1a2b9ad3Tatuya JINMEI 神明達哉
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafsson/*
b89095ba15e9163f2bacc46239d8f69d1a2b9ad3Tatuya JINMEI 神明達哉 * queryperf Program Mainline
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafsson */
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafssonint
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafssonmain(int argc, char **argv) {
b89095ba15e9163f2bacc46239d8f69d1a2b9ad3Tatuya JINMEI 神明達哉 int adjust_rate;
b89095ba15e9163f2bacc46239d8f69d1a2b9ad3Tatuya JINMEI 神明達哉 int sending = FALSE;
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafsson int got_eof = FALSE;
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafsson int input_length = MAX_INPUT_LEN;
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafsson char input_line[MAX_INPUT_LEN + 1];
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafsson
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafsson set_timenow(&time_of_program_start);
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafsson time_of_first_query.tv_sec = 0;
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafsson time_of_first_query.tv_usec = 0;
b89095ba15e9163f2bacc46239d8f69d1a2b9ad3Tatuya JINMEI 神明達哉 time_of_first_query_interval.tv_sec = 0;
b89095ba15e9163f2bacc46239d8f69d1a2b9ad3Tatuya JINMEI 神明達哉 time_of_first_query_interval.tv_usec = 0;
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafsson time_of_end_of_run.tv_sec = 0;
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafsson time_of_end_of_run.tv_usec = 0;
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafsson
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafsson input_line[0] = '\0';
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafsson
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafsson show_startup_info();
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafsson
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafsson if (setup(argc, argv) == -1)
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafsson return (-1);
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafsson
384fef003441df4067b773688237700513390f9eEvan Hunt /* XXX: move this to setup: */
384fef003441df4067b773688237700513390f9eEvan Hunt timeout_queries = malloc(sizeof(struct query_mininfo) * 65536);
384fef003441df4067b773688237700513390f9eEvan Hunt if (timeout_queries == NULL) {
384fef003441df4067b773688237700513390f9eEvan Hunt fprintf(stderr,
384fef003441df4067b773688237700513390f9eEvan Hunt "failed to allocate memory for timeout queries\n");
384fef003441df4067b773688237700513390f9eEvan Hunt return (-1);
384fef003441df4067b773688237700513390f9eEvan Hunt } else {
384fef003441df4067b773688237700513390f9eEvan Hunt int i;
384fef003441df4067b773688237700513390f9eEvan Hunt for (i = 0; i < 65536; i++)
384fef003441df4067b773688237700513390f9eEvan Hunt timeout_queries[i].qtype = -1;
384fef003441df4067b773688237700513390f9eEvan Hunt }
384fef003441df4067b773688237700513390f9eEvan Hunt
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafsson printf("[Status] Processing input data\n");
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafsson
b89095ba15e9163f2bacc46239d8f69d1a2b9ad3Tatuya JINMEI 神明達哉 while ((sending = keep_sending(&got_eof)) == TRUE ||
a18fc12ba3d48b66bea298c80f3e3f09f3c91527Evan Hunt queries_outstanding() > 0)
a18fc12ba3d48b66bea298c80f3e3f09f3c91527Evan Hunt {
a18fc12ba3d48b66bea298c80f3e3f09f3c91527Evan Hunt if (num_queries_sent_interval > 0){
a18fc12ba3d48b66bea298c80f3e3f09f3c91527Evan Hunt /*
a18fc12ba3d48b66bea298c80f3e3f09f3c91527Evan Hunt * After statistics are printed, send_query()
a18fc12ba3d48b66bea298c80f3e3f09f3c91527Evan Hunt * needs to be called at least once so that
a18fc12ba3d48b66bea298c80f3e3f09f3c91527Evan Hunt * time_of_first_query_interval is reset
a18fc12ba3d48b66bea298c80f3e3f09f3c91527Evan Hunt */
a18fc12ba3d48b66bea298c80f3e3f09f3c91527Evan Hunt print_interval_statistics();
a18fc12ba3d48b66bea298c80f3e3f09f3c91527Evan Hunt }
b89095ba15e9163f2bacc46239d8f69d1a2b9ad3Tatuya JINMEI 神明達哉 adjust_rate = FALSE;
b89095ba15e9163f2bacc46239d8f69d1a2b9ad3Tatuya JINMEI 神明達哉
b89095ba15e9163f2bacc46239d8f69d1a2b9ad3Tatuya JINMEI 神明達哉 while ((sending = keep_sending(&got_eof)) == TRUE &&
a18fc12ba3d48b66bea298c80f3e3f09f3c91527Evan Hunt queries_outstanding() < max_queries_outstanding)
a18fc12ba3d48b66bea298c80f3e3f09f3c91527Evan Hunt {
81e805888bcd4bd2a6d5c986fade6ffdf7d88a54Andreas Gustafsson int len = next_input_line(input_line, input_length);
81e805888bcd4bd2a6d5c986fade6ffdf7d88a54Andreas Gustafsson if (len == 0) {
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafsson got_eof = TRUE;
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafsson } else {
81e805888bcd4bd2a6d5c986fade6ffdf7d88a54Andreas Gustafsson /* Zap the trailing newline */
81e805888bcd4bd2a6d5c986fade6ffdf7d88a54Andreas Gustafsson if (input_line[len - 1] == '\n')
81e805888bcd4bd2a6d5c986fade6ffdf7d88a54Andreas Gustafsson input_line[len - 1] = '\0';
81e805888bcd4bd2a6d5c986fade6ffdf7d88a54Andreas Gustafsson
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafsson /*
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafsson * TODO: Should test if we got a whole line
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafsson * and flush to the next \n in input if not
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafsson * here... Add this later. Only do the next
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafsson * few lines if we got a whole line, else
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafsson * print a warning. Alternative: Make the
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafsson * max line size really big. BAD! :)
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafsson */
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafsson
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafsson if (input_line[0] == CONFIG_CHAR)
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafsson update_config(input_line);
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafsson else {
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafsson send_query(input_line);
b89095ba15e9163f2bacc46239d8f69d1a2b9ad3Tatuya JINMEI 神明達哉 if (target_qps > 0 &&
b89095ba15e9163f2bacc46239d8f69d1a2b9ad3Tatuya JINMEI 神明達哉 (num_queries_sent %
b89095ba15e9163f2bacc46239d8f69d1a2b9ad3Tatuya JINMEI 神明達哉 max_queries_outstanding) == 0) {
b89095ba15e9163f2bacc46239d8f69d1a2b9ad3Tatuya JINMEI 神明達哉 adjust_rate = TRUE;
b89095ba15e9163f2bacc46239d8f69d1a2b9ad3Tatuya JINMEI 神明達哉 }
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafsson }
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafsson }
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafsson }
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafsson
b89095ba15e9163f2bacc46239d8f69d1a2b9ad3Tatuya JINMEI 神明達哉 process_responses(adjust_rate);
b89095ba15e9163f2bacc46239d8f69d1a2b9ad3Tatuya JINMEI 神明達哉 retire_old_queries(sending);
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafsson }
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafsson
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafsson set_timenow(&time_of_end_of_run);
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafsson
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafsson printf("[Status] Testing complete\n");
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafsson
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafsson close_socket();
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafsson close_datafile();
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafsson
b89095ba15e9163f2bacc46239d8f69d1a2b9ad3Tatuya JINMEI 神明達哉 print_statistics(FALSE, num_queries_sent, num_queries_timed_out,
b89095ba15e9163f2bacc46239d8f69d1a2b9ad3Tatuya JINMEI 神明達哉 num_queries_possiblydelayed,
b89095ba15e9163f2bacc46239d8f69d1a2b9ad3Tatuya JINMEI 神明達哉 &time_of_first_query, &time_of_program_start,
b89095ba15e9163f2bacc46239d8f69d1a2b9ad3Tatuya JINMEI 神明達哉 &time_of_end_of_run, &time_of_stop_sending,
384fef003441df4067b773688237700513390f9eEvan Hunt rtt_counted, rtt_max, rtt_min, rtt_total,
384fef003441df4067b773688237700513390f9eEvan Hunt rtt_overflows, rttarray);
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafsson
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafsson return (0);
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafsson}