xref: /freebsd/contrib/ntp/sntp/libevent/test/regress_testutils.c (revision d93a896ef95946b0bf1219866fcb324b78543444)
1 /*
2  * Copyright (c) 2010-2012 Niels Provos and Nick Mathewson
3  *
4  * Redistribution and use in source and binary forms, with or without
5  * modification, are permitted provided that the following conditions
6  * are met:
7  * 1. Redistributions of source code must retain the above copyright
8  *    notice, this list of conditions and the following disclaimer.
9  * 2. Redistributions in binary form must reproduce the above copyright
10  *    notice, this list of conditions and the following disclaimer in the
11  *    documentation and/or other materials provided with the distribution.
12  * 3. The name of the author may not be used to endorse or promote products
13  *    derived from this software without specific prior written permission.
14  *
15  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
16  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
17  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
18  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
19  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
20  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
21  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
22  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
23  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
24  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
25  */
26 #include "../util-internal.h"
27 
28 #ifdef _WIN32
29 #include <winsock2.h>
30 #include <windows.h>
31 #include <ws2tcpip.h>
32 #endif
33 
34 #include "event2/event-config.h"
35 
36 #include <sys/types.h>
37 #include <sys/stat.h>
38 #ifdef EVENT__HAVE_SYS_TIME_H
39 #include <sys/time.h>
40 #endif
41 #include <sys/queue.h>
42 #ifndef _WIN32
43 #include <sys/socket.h>
44 #include <signal.h>
45 #include <netinet/in.h>
46 #include <arpa/inet.h>
47 #include <unistd.h>
48 #endif
49 #ifdef EVENT__HAVE_NETINET_IN6_H
50 #include <netinet/in6.h>
51 #endif
52 #ifdef HAVE_NETDB_H
53 #include <netdb.h>
54 #endif
55 #include <fcntl.h>
56 #include <stdlib.h>
57 #include <stdio.h>
58 #include <string.h>
59 #include <errno.h>
60 
61 #include "event2/dns.h"
62 #include "event2/dns_struct.h"
63 #include "event2/event.h"
64 #include "event2/event_compat.h"
65 #include "event2/util.h"
66 #include "event2/listener.h"
67 #include "event2/bufferevent.h"
68 #include "log-internal.h"
69 #include "regress.h"
70 #include "regress_testutils.h"
71 
72 /* globals */
73 static struct evdns_server_port *dns_port;
74 evutil_socket_t dns_sock = -1;
75 
76 /* Helper: return the port that a socket is bound on, in host order. */
77 int
78 regress_get_socket_port(evutil_socket_t fd)
79 {
80 	struct sockaddr_storage ss;
81 	ev_socklen_t socklen = sizeof(ss);
82 	if (getsockname(fd, (struct sockaddr*)&ss, &socklen) != 0)
83 		return -1;
84 	if (ss.ss_family == AF_INET)
85 		return ntohs( ((struct sockaddr_in*)&ss)->sin_port);
86 	else if (ss.ss_family == AF_INET6)
87 		return ntohs( ((struct sockaddr_in6*)&ss)->sin6_port);
88 	else
89 		return -1;
90 }
91 
92 struct evdns_server_port *
93 regress_get_dnsserver(struct event_base *base,
94     ev_uint16_t *portnum,
95     evutil_socket_t *psock,
96     evdns_request_callback_fn_type cb,
97     void *arg)
98 {
99 	struct evdns_server_port *port = NULL;
100 	evutil_socket_t sock;
101 	struct sockaddr_in my_addr;
102 
103 	sock = socket(AF_INET, SOCK_DGRAM, 0);
104 	if (sock < 0) {
105 		tt_abort_perror("socket");
106 	}
107 
108 	evutil_make_socket_nonblocking(sock);
109 
110 	memset(&my_addr, 0, sizeof(my_addr));
111 	my_addr.sin_family = AF_INET;
112 	my_addr.sin_port = htons(*portnum);
113 	my_addr.sin_addr.s_addr = htonl(0x7f000001UL);
114 	if (bind(sock, (struct sockaddr*)&my_addr, sizeof(my_addr)) < 0) {
115 		evutil_closesocket(sock);
116 		tt_abort_perror("bind");
117 	}
118 	port = evdns_add_server_port_with_base(base, sock, 0, cb, arg);
119 	if (!*portnum)
120 		*portnum = regress_get_socket_port(sock);
121 	if (psock)
122 		*psock = sock;
123 
124 	return port;
125 end:
126 	return NULL;
127 }
128 
129 void
130 regress_clean_dnsserver(void)
131 {
132 	if (dns_port)
133 		evdns_close_server_port(dns_port);
134 	if (dns_sock >= 0)
135 		evutil_closesocket(dns_sock);
136 }
137 
138 void
139 regress_dns_server_cb(struct evdns_server_request *req, void *data)
140 {
141 	struct regress_dns_server_table *tab = data;
142 	const char *question;
143 
144 	if (req->nquestions != 1)
145 		TT_DIE(("Only handling one question at a time; got %d",
146 			req->nquestions));
147 
148 	question = req->questions[0]->name;
149 
150 	while (tab->q && evutil_ascii_strcasecmp(question, tab->q) &&
151 	    strcmp("*", tab->q))
152 		++tab;
153 	if (tab->q == NULL)
154 		TT_DIE(("Unexpected question: '%s'", question));
155 
156 	++tab->seen;
157 
158 	if (!strcmp(tab->anstype, "err")) {
159 		int err = atoi(tab->ans);
160 		tt_assert(! evdns_server_request_respond(req, err));
161 		return;
162 	} else if (!strcmp(tab->anstype, "errsoa")) {
163 		int err = atoi(tab->ans);
164 		char soa_record[] =
165 			"\x04" "dns1" "\x05" "icann" "\x03" "org" "\0"
166 			"\x0a" "hostmaster" "\x05" "icann" "\x03" "org" "\0"
167 			"\x77\xde\x5e\xba" /* serial */
168 			"\x00\x00\x1c\x20" /* refreshtime = 2h */
169 			"\x00\x00\x0e\x10" /* retry = 1h */
170 			"\x00\x12\x75\x00" /* expiration = 14d */
171 			"\x00\x00\x0e\x10" /* min.ttl = 1h */
172 			;
173 		evdns_server_request_add_reply(
174 			req, EVDNS_AUTHORITY_SECTION,
175 			"example.com", EVDNS_TYPE_SOA, EVDNS_CLASS_INET,
176 			42, sizeof(soa_record) - 1, 0, soa_record);
177 		tt_assert(! evdns_server_request_respond(req, err));
178 		return;
179 	} else if (!strcmp(tab->anstype, "A")) {
180 		struct in_addr in;
181 		if (!evutil_inet_pton(AF_INET, tab->ans, &in)) {
182 			TT_DIE(("Bad A value %s in table", tab->ans));
183 		}
184 		evdns_server_request_add_a_reply(req, question, 1, &in.s_addr,
185 		    100);
186 	} else if (!strcmp(tab->anstype, "AAAA")) {
187 		struct in6_addr in6;
188 		if (!evutil_inet_pton(AF_INET6, tab->ans, &in6)) {
189 			TT_DIE(("Bad AAAA value %s in table", tab->ans));
190 		}
191 		evdns_server_request_add_aaaa_reply(req,
192 		    question, 1, &in6.s6_addr, 100);
193 	} else {
194 		TT_DIE(("Weird table entry with type '%s'", tab->anstype));
195 	}
196 	tt_assert(! evdns_server_request_respond(req, 0))
197 	return;
198 end:
199 	tt_want(! evdns_server_request_drop(req));
200 }
201 
202 int
203 regress_dnsserver(struct event_base *base, ev_uint16_t *port,
204     struct regress_dns_server_table *search_table)
205 {
206 	dns_port = regress_get_dnsserver(base, port, &dns_sock,
207 	    regress_dns_server_cb, search_table);
208 	return dns_port != NULL;
209 }
210 
211 int
212 regress_get_listener_addr(struct evconnlistener *lev,
213     struct sockaddr *sa, ev_socklen_t *socklen)
214 {
215 	evutil_socket_t s = evconnlistener_get_fd(lev);
216 	if (s <= 0)
217 		return -1;
218 	return getsockname(s, sa, socklen);
219 }
220