Home | History | Annotate | Line # | Download | only in testcode
streamtcp.c revision 1.1.1.4
      1      1.1  christos /*
      2      1.1  christos  * testcode/streamtcp.c - debug program perform multiple DNS queries on tcp.
      3      1.1  christos  *
      4      1.1  christos  * Copyright (c) 2008, NLnet Labs. All rights reserved.
      5      1.1  christos  *
      6      1.1  christos  * This software is open source.
      7      1.1  christos  *
      8      1.1  christos  * Redistribution and use in source and binary forms, with or without
      9      1.1  christos  * modification, are permitted provided that the following conditions
     10      1.1  christos  * are met:
     11      1.1  christos  *
     12      1.1  christos  * Redistributions of source code must retain the above copyright notice,
     13      1.1  christos  * this list of conditions and the following disclaimer.
     14      1.1  christos  *
     15      1.1  christos  * Redistributions in binary form must reproduce the above copyright notice,
     16      1.1  christos  * this list of conditions and the following disclaimer in the documentation
     17      1.1  christos  * and/or other materials provided with the distribution.
     18      1.1  christos  *
     19      1.1  christos  * Neither the name of the NLNET LABS nor the names of its contributors may
     20      1.1  christos  * be used to endorse or promote products derived from this software without
     21      1.1  christos  * specific prior written permission.
     22      1.1  christos  *
     23      1.1  christos  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
     24      1.1  christos  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
     25      1.1  christos  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
     26      1.1  christos  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
     27      1.1  christos  * HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
     28      1.1  christos  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
     29      1.1  christos  * TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
     30      1.1  christos  * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
     31      1.1  christos  * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
     32      1.1  christos  * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
     33      1.1  christos  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     34      1.1  christos  */
     35      1.1  christos 
     36      1.1  christos /**
     37      1.1  christos  * \file
     38      1.1  christos  *
     39      1.1  christos  * This program performs multiple DNS queries on a TCP stream.
     40      1.1  christos  */
     41      1.1  christos 
     42      1.1  christos #include "config.h"
     43      1.1  christos #ifdef HAVE_GETOPT_H
     44      1.1  christos #include <getopt.h>
     45      1.1  christos #endif
     46      1.1  christos #include <signal.h>
     47  1.1.1.4  christos #include <stdlib.h>
     48  1.1.1.4  christos #include <unistd.h>
     49      1.1  christos #include "util/locks.h"
     50      1.1  christos #include "util/log.h"
     51      1.1  christos #include "util/net_help.h"
     52      1.1  christos #include "util/data/msgencode.h"
     53      1.1  christos #include "util/data/msgparse.h"
     54      1.1  christos #include "util/data/msgreply.h"
     55      1.1  christos #include "util/data/dname.h"
     56      1.1  christos #include "sldns/sbuffer.h"
     57      1.1  christos #include "sldns/str2wire.h"
     58      1.1  christos #include "sldns/wire2str.h"
     59      1.1  christos #include <openssl/ssl.h>
     60      1.1  christos #include <openssl/rand.h>
     61      1.1  christos #include <openssl/err.h>
     62      1.1  christos 
     63      1.1  christos #ifndef PF_INET6
     64      1.1  christos /** define in case streamtcp is compiled on legacy systems */
     65      1.1  christos #define PF_INET6 10
     66      1.1  christos #endif
     67      1.1  christos 
     68      1.1  christos /** usage information for streamtcp */
     69      1.1  christos static void usage(char* argv[])
     70      1.1  christos {
     71      1.1  christos 	printf("usage: %s [options] name type class ...\n", argv[0]);
     72      1.1  christos 	printf("	sends the name-type-class queries over TCP.\n");
     73      1.1  christos 	printf("-f server	what ipaddr@portnr to send the queries to\n");
     74      1.1  christos 	printf("-u 		use UDP. No retries are attempted.\n");
     75      1.1  christos 	printf("-n 		do not wait for an answer.\n");
     76  1.1.1.4  christos 	printf("-a 		print answers as they arrive.\n");
     77  1.1.1.4  christos 	printf("-d secs		delay after connection before sending query\n");
     78      1.1  christos 	printf("-s		use ssl\n");
     79      1.1  christos 	printf("-h 		this help text\n");
     80      1.1  christos 	exit(1);
     81      1.1  christos }
     82      1.1  christos 
     83      1.1  christos /** open TCP socket to svr */
     84      1.1  christos static int
     85      1.1  christos open_svr(const char* svr, int udp)
     86      1.1  christos {
     87      1.1  christos 	struct sockaddr_storage addr;
     88      1.1  christos 	socklen_t addrlen;
     89      1.1  christos 	int fd = -1;
     90      1.1  christos 	/* svr can be ip@port */
     91      1.1  christos 	memset(&addr, 0, sizeof(addr));
     92      1.1  christos 	if(!extstrtoaddr(svr, &addr, &addrlen)) {
     93      1.1  christos 		printf("fatal: bad server specs '%s'\n", svr);
     94      1.1  christos 		exit(1);
     95      1.1  christos 	}
     96      1.1  christos 	fd = socket(addr_is_ip6(&addr, addrlen)?PF_INET6:PF_INET,
     97      1.1  christos 		udp?SOCK_DGRAM:SOCK_STREAM, 0);
     98      1.1  christos 	if(fd == -1) {
     99      1.1  christos #ifndef USE_WINSOCK
    100      1.1  christos 		perror("socket() error");
    101      1.1  christos #else
    102      1.1  christos 		printf("socket: %s\n", wsa_strerror(WSAGetLastError()));
    103      1.1  christos #endif
    104      1.1  christos 		exit(1);
    105      1.1  christos 	}
    106      1.1  christos 	if(connect(fd, (struct sockaddr*)&addr, addrlen) < 0) {
    107      1.1  christos #ifndef USE_WINSOCK
    108      1.1  christos 		perror("connect() error");
    109      1.1  christos #else
    110      1.1  christos 		printf("connect: %s\n", wsa_strerror(WSAGetLastError()));
    111      1.1  christos #endif
    112      1.1  christos 		exit(1);
    113      1.1  christos 	}
    114      1.1  christos 	return fd;
    115      1.1  christos }
    116      1.1  christos 
    117      1.1  christos /** write a query over the TCP fd */
    118      1.1  christos static void
    119      1.1  christos write_q(int fd, int udp, SSL* ssl, sldns_buffer* buf, uint16_t id,
    120      1.1  christos 	const char* strname, const char* strtype, const char* strclass)
    121      1.1  christos {
    122      1.1  christos 	struct query_info qinfo;
    123      1.1  christos 	uint16_t len;
    124      1.1  christos 	/* qname */
    125      1.1  christos 	qinfo.qname = sldns_str2wire_dname(strname, &qinfo.qname_len);
    126      1.1  christos 	if(!qinfo.qname) {
    127      1.1  christos 		printf("cannot parse query name: '%s'\n", strname);
    128      1.1  christos 		exit(1);
    129      1.1  christos 	}
    130      1.1  christos 
    131      1.1  christos 	/* qtype and qclass */
    132      1.1  christos 	qinfo.qtype = sldns_get_rr_type_by_name(strtype);
    133      1.1  christos 	qinfo.qclass = sldns_get_rr_class_by_name(strclass);
    134      1.1  christos 
    135  1.1.1.2  christos 	/* clear local alias */
    136  1.1.1.2  christos 	qinfo.local_alias = NULL;
    137  1.1.1.2  christos 
    138      1.1  christos 	/* make query */
    139      1.1  christos 	qinfo_query_encode(buf, &qinfo);
    140      1.1  christos 	sldns_buffer_write_u16_at(buf, 0, id);
    141      1.1  christos 	sldns_buffer_write_u16_at(buf, 2, BIT_RD);
    142      1.1  christos 
    143      1.1  christos 	if(1) {
    144      1.1  christos 		/* add EDNS DO */
    145      1.1  christos 		struct edns_data edns;
    146      1.1  christos 		memset(&edns, 0, sizeof(edns));
    147      1.1  christos 		edns.edns_present = 1;
    148      1.1  christos 		edns.bits = EDNS_DO;
    149      1.1  christos 		edns.udp_size = 4096;
    150  1.1.1.2  christos 		if(sldns_buffer_capacity(buf) >=
    151  1.1.1.2  christos 			sldns_buffer_limit(buf)+calc_edns_field_size(&edns))
    152  1.1.1.2  christos 			attach_edns_record(buf, &edns);
    153      1.1  christos 	}
    154      1.1  christos 
    155      1.1  christos 	/* send it */
    156      1.1  christos 	if(!udp) {
    157      1.1  christos 		len = (uint16_t)sldns_buffer_limit(buf);
    158      1.1  christos 		len = htons(len);
    159      1.1  christos 		if(ssl) {
    160      1.1  christos 			if(SSL_write(ssl, (void*)&len, (int)sizeof(len)) <= 0) {
    161      1.1  christos 				log_crypto_err("cannot SSL_write");
    162      1.1  christos 				exit(1);
    163      1.1  christos 			}
    164      1.1  christos 		} else {
    165      1.1  christos 			if(send(fd, (void*)&len, sizeof(len), 0) <
    166      1.1  christos 				(ssize_t)sizeof(len)){
    167      1.1  christos #ifndef USE_WINSOCK
    168      1.1  christos 				perror("send() len failed");
    169      1.1  christos #else
    170      1.1  christos 				printf("send len: %s\n",
    171      1.1  christos 					wsa_strerror(WSAGetLastError()));
    172      1.1  christos #endif
    173      1.1  christos 				exit(1);
    174      1.1  christos 			}
    175      1.1  christos 		}
    176      1.1  christos 	}
    177      1.1  christos 	if(ssl) {
    178      1.1  christos 		if(SSL_write(ssl, (void*)sldns_buffer_begin(buf),
    179      1.1  christos 			(int)sldns_buffer_limit(buf)) <= 0) {
    180      1.1  christos 			log_crypto_err("cannot SSL_write");
    181      1.1  christos 			exit(1);
    182      1.1  christos 		}
    183      1.1  christos 	} else {
    184      1.1  christos 		if(send(fd, (void*)sldns_buffer_begin(buf),
    185      1.1  christos 			sldns_buffer_limit(buf), 0) <
    186      1.1  christos 			(ssize_t)sldns_buffer_limit(buf)) {
    187      1.1  christos #ifndef USE_WINSOCK
    188      1.1  christos 			perror("send() data failed");
    189      1.1  christos #else
    190      1.1  christos 			printf("send data: %s\n", wsa_strerror(WSAGetLastError()));
    191      1.1  christos #endif
    192      1.1  christos 			exit(1);
    193      1.1  christos 		}
    194      1.1  christos 	}
    195      1.1  christos 
    196      1.1  christos 	free(qinfo.qname);
    197      1.1  christos }
    198      1.1  christos 
    199      1.1  christos /** receive DNS datagram over TCP and print it */
    200      1.1  christos static void
    201      1.1  christos recv_one(int fd, int udp, SSL* ssl, sldns_buffer* buf)
    202      1.1  christos {
    203      1.1  christos 	char* pktstr;
    204      1.1  christos 	uint16_t len;
    205      1.1  christos 	if(!udp) {
    206      1.1  christos 		if(ssl) {
    207  1.1.1.4  christos 			int sr = SSL_read(ssl, (void*)&len, (int)sizeof(len));
    208  1.1.1.4  christos 			if(sr == 0) {
    209  1.1.1.4  christos 				printf("ssl: stream closed\n");
    210  1.1.1.4  christos 				exit(1);
    211  1.1.1.4  christos 			}
    212  1.1.1.4  christos 			if(sr < 0) {
    213      1.1  christos 				log_crypto_err("could not SSL_read");
    214      1.1  christos 				exit(1);
    215      1.1  christos 			}
    216      1.1  christos 		} else {
    217  1.1.1.4  christos 			ssize_t r = recv(fd, (void*)&len, sizeof(len), 0);
    218  1.1.1.4  christos 			if(r == 0) {
    219  1.1.1.4  christos 				printf("recv: stream closed\n");
    220  1.1.1.4  christos 				exit(1);
    221  1.1.1.4  christos 			}
    222  1.1.1.4  christos 			if(r < (ssize_t)sizeof(len)) {
    223      1.1  christos #ifndef USE_WINSOCK
    224      1.1  christos 				perror("read() len failed");
    225      1.1  christos #else
    226      1.1  christos 				printf("read len: %s\n",
    227      1.1  christos 					wsa_strerror(WSAGetLastError()));
    228      1.1  christos #endif
    229      1.1  christos 				exit(1);
    230      1.1  christos 			}
    231      1.1  christos 		}
    232      1.1  christos 		len = ntohs(len);
    233      1.1  christos 		sldns_buffer_clear(buf);
    234      1.1  christos 		sldns_buffer_set_limit(buf, len);
    235      1.1  christos 		if(ssl) {
    236      1.1  christos 			int r = SSL_read(ssl, (void*)sldns_buffer_begin(buf),
    237      1.1  christos 				(int)len);
    238      1.1  christos 			if(r <= 0) {
    239      1.1  christos 				log_crypto_err("could not SSL_read");
    240      1.1  christos 				exit(1);
    241      1.1  christos 			}
    242      1.1  christos 			if(r != (int)len)
    243      1.1  christos 				fatal_exit("ssl_read %d of %d", r, len);
    244      1.1  christos 		} else {
    245      1.1  christos 			if(recv(fd, (void*)sldns_buffer_begin(buf), len, 0) <
    246      1.1  christos 				(ssize_t)len) {
    247      1.1  christos #ifndef USE_WINSOCK
    248      1.1  christos 				perror("read() data failed");
    249      1.1  christos #else
    250      1.1  christos 				printf("read data: %s\n",
    251      1.1  christos 					wsa_strerror(WSAGetLastError()));
    252      1.1  christos #endif
    253      1.1  christos 				exit(1);
    254      1.1  christos 			}
    255      1.1  christos 		}
    256      1.1  christos 	} else {
    257      1.1  christos 		ssize_t l;
    258      1.1  christos 		sldns_buffer_clear(buf);
    259      1.1  christos 		if((l=recv(fd, (void*)sldns_buffer_begin(buf),
    260      1.1  christos 			sldns_buffer_capacity(buf), 0)) < 0) {
    261      1.1  christos #ifndef USE_WINSOCK
    262      1.1  christos 			perror("read() data failed");
    263      1.1  christos #else
    264      1.1  christos 			printf("read data: %s\n",
    265      1.1  christos 				wsa_strerror(WSAGetLastError()));
    266      1.1  christos #endif
    267      1.1  christos 			exit(1);
    268      1.1  christos 		}
    269      1.1  christos 		sldns_buffer_set_limit(buf, (size_t)l);
    270      1.1  christos 		len = (size_t)l;
    271      1.1  christos 	}
    272      1.1  christos 	printf("\nnext received packet\n");
    273      1.1  christos 	log_buf(0, "data", buf);
    274      1.1  christos 
    275      1.1  christos 	pktstr = sldns_wire2str_pkt(sldns_buffer_begin(buf), len);
    276      1.1  christos 	printf("%s", pktstr);
    277      1.1  christos 	free(pktstr);
    278      1.1  christos }
    279      1.1  christos 
    280  1.1.1.4  christos /** see if we can receive any results */
    281  1.1.1.4  christos static void
    282  1.1.1.4  christos print_any_answers(int fd, int udp, SSL* ssl, sldns_buffer* buf,
    283  1.1.1.4  christos 	int* num_answers, int wait_all)
    284  1.1.1.4  christos {
    285  1.1.1.4  christos 	/* see if the fd can read, if so, print one answer, repeat */
    286  1.1.1.4  christos 	int ret;
    287  1.1.1.4  christos 	struct timeval tv, *waittv;
    288  1.1.1.4  christos 	fd_set rfd;
    289  1.1.1.4  christos 	while(*num_answers > 0) {
    290  1.1.1.4  christos 		memset(&rfd, 0, sizeof(rfd));
    291  1.1.1.4  christos 		memset(&tv, 0, sizeof(tv));
    292  1.1.1.4  christos 		FD_ZERO(&rfd);
    293  1.1.1.4  christos 		FD_SET(fd, &rfd);
    294  1.1.1.4  christos 		if(wait_all) waittv = NULL;
    295  1.1.1.4  christos 		else waittv = &tv;
    296  1.1.1.4  christos 		ret = select(fd+1, &rfd, NULL, NULL, waittv);
    297  1.1.1.4  christos 		if(ret < 0) {
    298  1.1.1.4  christos 			if(errno == EINTR || errno == EAGAIN) continue;
    299  1.1.1.4  christos 			perror("select() failed");
    300  1.1.1.4  christos 			exit(1);
    301  1.1.1.4  christos 		}
    302  1.1.1.4  christos 		if(ret == 0) {
    303  1.1.1.4  christos 			if(wait_all) continue;
    304  1.1.1.4  christos 			return;
    305  1.1.1.4  christos 		}
    306  1.1.1.4  christos 		(*num_answers) -= 1;
    307  1.1.1.4  christos 		recv_one(fd, udp, ssl, buf);
    308  1.1.1.4  christos 	}
    309  1.1.1.4  christos }
    310  1.1.1.4  christos 
    311      1.1  christos static int get_random(void)
    312      1.1  christos {
    313      1.1  christos 	int r;
    314      1.1  christos 	if (RAND_bytes((unsigned char*)&r, (int)sizeof(r)) == 1) {
    315      1.1  christos 		return r;
    316      1.1  christos 	}
    317      1.1  christos 	return arc4random();
    318      1.1  christos }
    319      1.1  christos 
    320      1.1  christos /** send the TCP queries and print answers */
    321      1.1  christos static void
    322  1.1.1.4  christos send_em(const char* svr, int udp, int usessl, int noanswer, int onarrival,
    323  1.1.1.4  christos 	int delay, int num, char** qs)
    324      1.1  christos {
    325      1.1  christos 	sldns_buffer* buf = sldns_buffer_new(65553);
    326      1.1  christos 	int fd = open_svr(svr, udp);
    327  1.1.1.4  christos 	int i, wait_results = 0;
    328      1.1  christos 	SSL_CTX* ctx = NULL;
    329      1.1  christos 	SSL* ssl = NULL;
    330      1.1  christos 	if(!buf) fatal_exit("out of memory");
    331      1.1  christos 	if(usessl) {
    332  1.1.1.3  christos 		ctx = connect_sslctx_create(NULL, NULL, NULL, 0);
    333      1.1  christos 		if(!ctx) fatal_exit("cannot create ssl ctx");
    334      1.1  christos 		ssl = outgoing_ssl_fd(ctx, fd);
    335      1.1  christos 		if(!ssl) fatal_exit("cannot create ssl");
    336      1.1  christos 		while(1) {
    337      1.1  christos 			int r;
    338      1.1  christos 			ERR_clear_error();
    339      1.1  christos 			if( (r=SSL_do_handshake(ssl)) == 1)
    340      1.1  christos 				break;
    341      1.1  christos 			r = SSL_get_error(ssl, r);
    342      1.1  christos 			if(r != SSL_ERROR_WANT_READ &&
    343      1.1  christos 				r != SSL_ERROR_WANT_WRITE) {
    344      1.1  christos 				log_crypto_err("could not ssl_handshake");
    345      1.1  christos 				exit(1);
    346      1.1  christos 			}
    347      1.1  christos 		}
    348      1.1  christos 		if(1) {
    349      1.1  christos 			X509* x = SSL_get_peer_certificate(ssl);
    350      1.1  christos 			if(!x) printf("SSL: no peer certificate\n");
    351      1.1  christos 			else {
    352      1.1  christos 				X509_print_fp(stdout, x);
    353      1.1  christos 				X509_free(x);
    354      1.1  christos 			}
    355      1.1  christos 		}
    356      1.1  christos 	}
    357      1.1  christos 	for(i=0; i<num; i+=3) {
    358  1.1.1.4  christos 		if (delay != 0) {
    359  1.1.1.4  christos #ifdef HAVE_SLEEP
    360  1.1.1.4  christos 			sleep((unsigned)delay);
    361  1.1.1.4  christos #else
    362  1.1.1.4  christos 			Sleep(delay*1000);
    363  1.1.1.4  christos #endif
    364  1.1.1.4  christos 		}
    365      1.1  christos 		printf("\nNext query is %s %s %s\n", qs[i], qs[i+1], qs[i+2]);
    366      1.1  christos 		write_q(fd, udp, ssl, buf, (uint16_t)get_random(), qs[i],
    367      1.1  christos 			qs[i+1], qs[i+2]);
    368      1.1  christos 		/* print at least one result */
    369  1.1.1.4  christos 		if(onarrival) {
    370  1.1.1.4  christos 			wait_results += 1; /* one more answer to fetch */
    371  1.1.1.4  christos 			print_any_answers(fd, udp, ssl, buf, &wait_results, 0);
    372  1.1.1.4  christos 		} else if(!noanswer) {
    373      1.1  christos 			recv_one(fd, udp, ssl, buf);
    374  1.1.1.4  christos 		}
    375      1.1  christos 	}
    376  1.1.1.4  christos 	if(onarrival)
    377  1.1.1.4  christos 		print_any_answers(fd, udp, ssl, buf, &wait_results, 1);
    378      1.1  christos 
    379      1.1  christos 	if(usessl) {
    380      1.1  christos 		SSL_shutdown(ssl);
    381      1.1  christos 		SSL_free(ssl);
    382      1.1  christos 		SSL_CTX_free(ctx);
    383      1.1  christos 	}
    384      1.1  christos #ifndef USE_WINSOCK
    385      1.1  christos 	close(fd);
    386      1.1  christos #else
    387      1.1  christos 	closesocket(fd);
    388      1.1  christos #endif
    389      1.1  christos 	sldns_buffer_free(buf);
    390      1.1  christos 	printf("orderly exit\n");
    391      1.1  christos }
    392      1.1  christos 
    393      1.1  christos #ifdef SIGPIPE
    394      1.1  christos /** SIGPIPE handler */
    395      1.1  christos static RETSIGTYPE sigh(int sig)
    396      1.1  christos {
    397      1.1  christos 	if(sig == SIGPIPE) {
    398      1.1  christos 		printf("got SIGPIPE, remote connection gone\n");
    399      1.1  christos 		exit(1);
    400      1.1  christos 	}
    401      1.1  christos 	printf("Got unhandled signal %d\n", sig);
    402      1.1  christos 	exit(1);
    403      1.1  christos }
    404      1.1  christos #endif /* SIGPIPE */
    405      1.1  christos 
    406      1.1  christos /** getopt global, in case header files fail to declare it. */
    407      1.1  christos extern int optind;
    408      1.1  christos /** getopt global, in case header files fail to declare it. */
    409      1.1  christos extern char* optarg;
    410      1.1  christos 
    411      1.1  christos /** main program for streamtcp */
    412      1.1  christos int main(int argc, char** argv)
    413      1.1  christos {
    414      1.1  christos 	int c;
    415      1.1  christos 	const char* svr = "127.0.0.1";
    416      1.1  christos 	int udp = 0;
    417      1.1  christos 	int noanswer = 0;
    418  1.1.1.4  christos 	int onarrival = 0;
    419      1.1  christos 	int usessl = 0;
    420  1.1.1.4  christos 	int delay = 0;
    421      1.1  christos 
    422      1.1  christos #ifdef USE_WINSOCK
    423      1.1  christos 	WSADATA wsa_data;
    424      1.1  christos 	if(WSAStartup(MAKEWORD(2,2), &wsa_data) != 0) {
    425      1.1  christos 		printf("WSAStartup failed\n");
    426      1.1  christos 		return 1;
    427      1.1  christos 	}
    428      1.1  christos #endif
    429      1.1  christos 
    430      1.1  christos 	/* lock debug start (if any) */
    431      1.1  christos 	log_init(0, 0, 0);
    432      1.1  christos 	checklock_start();
    433      1.1  christos 
    434      1.1  christos #ifdef SIGPIPE
    435      1.1  christos 	if(signal(SIGPIPE, &sigh) == SIG_ERR) {
    436      1.1  christos 		perror("could not install signal handler");
    437      1.1  christos 		return 1;
    438      1.1  christos 	}
    439      1.1  christos #endif
    440      1.1  christos 
    441      1.1  christos 	/* command line options */
    442      1.1  christos 	if(argc == 1) {
    443      1.1  christos 		usage(argv);
    444      1.1  christos 	}
    445  1.1.1.4  christos 	while( (c=getopt(argc, argv, "af:hnsud:")) != -1) {
    446      1.1  christos 		switch(c) {
    447      1.1  christos 			case 'f':
    448      1.1  christos 				svr = optarg;
    449      1.1  christos 				break;
    450  1.1.1.4  christos 			case 'a':
    451  1.1.1.4  christos 				onarrival = 1;
    452  1.1.1.4  christos 				break;
    453      1.1  christos 			case 'n':
    454      1.1  christos 				noanswer = 1;
    455      1.1  christos 				break;
    456      1.1  christos 			case 'u':
    457      1.1  christos 				udp = 1;
    458      1.1  christos 				break;
    459      1.1  christos 			case 's':
    460      1.1  christos 				usessl = 1;
    461      1.1  christos 				break;
    462  1.1.1.4  christos 			case 'd':
    463  1.1.1.4  christos 				if(atoi(optarg)==0 && strcmp(optarg,"0")!=0) {
    464  1.1.1.4  christos 					printf("error parsing delay, "
    465  1.1.1.4  christos 					    "number expected: %s\n", optarg);
    466  1.1.1.4  christos 					return 1;
    467  1.1.1.4  christos 				}
    468  1.1.1.4  christos 				delay = atoi(optarg);
    469  1.1.1.4  christos 				break;
    470      1.1  christos 			case 'h':
    471      1.1  christos 			case '?':
    472      1.1  christos 			default:
    473      1.1  christos 				usage(argv);
    474      1.1  christos 		}
    475      1.1  christos 	}
    476      1.1  christos 	argc -= optind;
    477      1.1  christos 	argv += optind;
    478      1.1  christos 
    479      1.1  christos 	if(argc % 3 != 0) {
    480      1.1  christos 		printf("queries must be multiples of name,type,class\n");
    481      1.1  christos 		return 1;
    482      1.1  christos 	}
    483      1.1  christos 	if(usessl) {
    484  1.1.1.2  christos #if OPENSSL_VERSION_NUMBER < 0x10100000 || !defined(HAVE_OPENSSL_INIT_SSL)
    485      1.1  christos 		ERR_load_SSL_strings();
    486  1.1.1.2  christos #endif
    487  1.1.1.2  christos #if OPENSSL_VERSION_NUMBER < 0x10100000 || !defined(HAVE_OPENSSL_INIT_CRYPTO)
    488      1.1  christos 		OpenSSL_add_all_algorithms();
    489  1.1.1.2  christos #else
    490  1.1.1.2  christos 		OPENSSL_init_crypto(OPENSSL_INIT_ADD_ALL_CIPHERS
    491  1.1.1.2  christos 			| OPENSSL_INIT_ADD_ALL_DIGESTS
    492  1.1.1.2  christos 			| OPENSSL_INIT_LOAD_CRYPTO_STRINGS, NULL);
    493  1.1.1.2  christos #endif
    494  1.1.1.2  christos #if OPENSSL_VERSION_NUMBER < 0x10100000 || !defined(HAVE_OPENSSL_INIT_SSL)
    495  1.1.1.2  christos 		(void)SSL_library_init();
    496  1.1.1.2  christos #else
    497  1.1.1.2  christos 		(void)OPENSSL_init_ssl(OPENSSL_INIT_LOAD_SSL_STRINGS, NULL);
    498  1.1.1.2  christos #endif
    499      1.1  christos 	}
    500  1.1.1.4  christos 	send_em(svr, udp, usessl, noanswer, onarrival, delay, argc, argv);
    501      1.1  christos 	checklock_stop();
    502      1.1  christos #ifdef USE_WINSOCK
    503      1.1  christos 	WSACleanup();
    504      1.1  christos #endif
    505      1.1  christos 	return 0;
    506      1.1  christos }
    507