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