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