12b15cb3dSCy Schubert /*
22b15cb3dSCy Schubert * Copyright (c) 2010-2012 Niels Provos and Nick Mathewson
32b15cb3dSCy Schubert *
42b15cb3dSCy Schubert * Redistribution and use in source and binary forms, with or without
52b15cb3dSCy Schubert * modification, are permitted provided that the following conditions
62b15cb3dSCy Schubert * are met:
72b15cb3dSCy Schubert * 1. Redistributions of source code must retain the above copyright
82b15cb3dSCy Schubert * notice, this list of conditions and the following disclaimer.
92b15cb3dSCy Schubert * 2. Redistributions in binary form must reproduce the above copyright
102b15cb3dSCy Schubert * notice, this list of conditions and the following disclaimer in the
112b15cb3dSCy Schubert * documentation and/or other materials provided with the distribution.
122b15cb3dSCy Schubert * 3. The name of the author may not be used to endorse or promote products
132b15cb3dSCy Schubert * derived from this software without specific prior written permission.
142b15cb3dSCy Schubert *
152b15cb3dSCy Schubert * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
162b15cb3dSCy Schubert * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
172b15cb3dSCy Schubert * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
182b15cb3dSCy Schubert * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
192b15cb3dSCy Schubert * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
202b15cb3dSCy Schubert * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
212b15cb3dSCy Schubert * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
222b15cb3dSCy Schubert * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
232b15cb3dSCy Schubert * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
242b15cb3dSCy Schubert * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
252b15cb3dSCy Schubert */
262b15cb3dSCy Schubert #include "../util-internal.h"
272b15cb3dSCy Schubert
282b15cb3dSCy Schubert #ifdef _WIN32
292b15cb3dSCy Schubert #include <winsock2.h>
302b15cb3dSCy Schubert #include <windows.h>
312b15cb3dSCy Schubert #include <ws2tcpip.h>
322b15cb3dSCy Schubert #endif
332b15cb3dSCy Schubert
342b15cb3dSCy Schubert #include "event2/event-config.h"
352b15cb3dSCy Schubert
362b15cb3dSCy Schubert #include <sys/types.h>
372b15cb3dSCy Schubert #include <sys/stat.h>
382b15cb3dSCy Schubert #ifdef EVENT__HAVE_SYS_TIME_H
392b15cb3dSCy Schubert #include <sys/time.h>
402b15cb3dSCy Schubert #endif
412b15cb3dSCy Schubert #include <sys/queue.h>
422b15cb3dSCy Schubert #ifndef _WIN32
432b15cb3dSCy Schubert #include <sys/socket.h>
442b15cb3dSCy Schubert #include <signal.h>
452b15cb3dSCy Schubert #include <netinet/in.h>
462b15cb3dSCy Schubert #include <arpa/inet.h>
472b15cb3dSCy Schubert #include <unistd.h>
482b15cb3dSCy Schubert #endif
492b15cb3dSCy Schubert #ifdef EVENT__HAVE_NETINET_IN6_H
502b15cb3dSCy Schubert #include <netinet/in6.h>
512b15cb3dSCy Schubert #endif
522b15cb3dSCy Schubert #ifdef HAVE_NETDB_H
532b15cb3dSCy Schubert #include <netdb.h>
542b15cb3dSCy Schubert #endif
552b15cb3dSCy Schubert #include <fcntl.h>
562b15cb3dSCy Schubert #include <stdlib.h>
572b15cb3dSCy Schubert #include <stdio.h>
582b15cb3dSCy Schubert #include <string.h>
592b15cb3dSCy Schubert #include <errno.h>
602b15cb3dSCy Schubert
612b15cb3dSCy Schubert #include "event2/dns.h"
622b15cb3dSCy Schubert #include "event2/dns_struct.h"
632b15cb3dSCy Schubert #include "event2/event.h"
642b15cb3dSCy Schubert #include "event2/event_compat.h"
652b15cb3dSCy Schubert #include "event2/util.h"
662b15cb3dSCy Schubert #include "event2/listener.h"
672b15cb3dSCy Schubert #include "event2/bufferevent.h"
682b15cb3dSCy Schubert #include "log-internal.h"
692b15cb3dSCy Schubert #include "regress.h"
702b15cb3dSCy Schubert #include "regress_testutils.h"
712b15cb3dSCy Schubert
722b15cb3dSCy Schubert /* globals */
732b15cb3dSCy Schubert static struct evdns_server_port *dns_port;
742b15cb3dSCy Schubert evutil_socket_t dns_sock = -1;
752b15cb3dSCy Schubert
762b15cb3dSCy Schubert /* Helper: return the port that a socket is bound on, in host order. */
772b15cb3dSCy Schubert int
regress_get_socket_port(evutil_socket_t fd)782b15cb3dSCy Schubert regress_get_socket_port(evutil_socket_t fd)
792b15cb3dSCy Schubert {
802b15cb3dSCy Schubert struct sockaddr_storage ss;
812b15cb3dSCy Schubert ev_socklen_t socklen = sizeof(ss);
822b15cb3dSCy Schubert if (getsockname(fd, (struct sockaddr*)&ss, &socklen) != 0)
832b15cb3dSCy Schubert return -1;
842b15cb3dSCy Schubert if (ss.ss_family == AF_INET)
852b15cb3dSCy Schubert return ntohs( ((struct sockaddr_in*)&ss)->sin_port);
862b15cb3dSCy Schubert else if (ss.ss_family == AF_INET6)
872b15cb3dSCy Schubert return ntohs( ((struct sockaddr_in6*)&ss)->sin6_port);
882b15cb3dSCy Schubert else
892b15cb3dSCy Schubert return -1;
902b15cb3dSCy Schubert }
912b15cb3dSCy Schubert
922b15cb3dSCy Schubert 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)932b15cb3dSCy Schubert regress_get_dnsserver(struct event_base *base,
942b15cb3dSCy Schubert ev_uint16_t *portnum,
952b15cb3dSCy Schubert evutil_socket_t *psock,
962b15cb3dSCy Schubert evdns_request_callback_fn_type cb,
972b15cb3dSCy Schubert void *arg)
982b15cb3dSCy Schubert {
992b15cb3dSCy Schubert struct evdns_server_port *port = NULL;
1002b15cb3dSCy Schubert evutil_socket_t sock;
1012b15cb3dSCy Schubert struct sockaddr_in my_addr;
1022b15cb3dSCy Schubert
1032b15cb3dSCy Schubert sock = socket(AF_INET, SOCK_DGRAM, 0);
1042b15cb3dSCy Schubert if (sock < 0) {
1052b15cb3dSCy Schubert tt_abort_perror("socket");
1062b15cb3dSCy Schubert }
1072b15cb3dSCy Schubert
1082b15cb3dSCy Schubert evutil_make_socket_nonblocking(sock);
1092b15cb3dSCy Schubert
1102b15cb3dSCy Schubert memset(&my_addr, 0, sizeof(my_addr));
1112b15cb3dSCy Schubert my_addr.sin_family = AF_INET;
1122b15cb3dSCy Schubert my_addr.sin_port = htons(*portnum);
113*a466cc55SCy Schubert my_addr.sin_addr.s_addr = inet_addr("127.0.0.1");
1142b15cb3dSCy Schubert if (bind(sock, (struct sockaddr*)&my_addr, sizeof(my_addr)) < 0) {
1152b15cb3dSCy Schubert evutil_closesocket(sock);
1162b15cb3dSCy Schubert tt_abort_perror("bind");
1172b15cb3dSCy Schubert }
1182b15cb3dSCy Schubert port = evdns_add_server_port_with_base(base, sock, 0, cb, arg);
1192b15cb3dSCy Schubert if (!*portnum)
1202b15cb3dSCy Schubert *portnum = regress_get_socket_port(sock);
1212b15cb3dSCy Schubert if (psock)
1222b15cb3dSCy Schubert *psock = sock;
1232b15cb3dSCy Schubert
1242b15cb3dSCy Schubert return port;
1252b15cb3dSCy Schubert end:
1262b15cb3dSCy Schubert return NULL;
1272b15cb3dSCy Schubert }
1282b15cb3dSCy Schubert
1292b15cb3dSCy Schubert void
regress_clean_dnsserver(void)1302b15cb3dSCy Schubert regress_clean_dnsserver(void)
1312b15cb3dSCy Schubert {
132*a466cc55SCy Schubert if (dns_port) {
1332b15cb3dSCy Schubert evdns_close_server_port(dns_port);
134*a466cc55SCy Schubert dns_port = NULL;
135*a466cc55SCy Schubert }
136*a466cc55SCy Schubert if (dns_sock >= 0) {
1372b15cb3dSCy Schubert evutil_closesocket(dns_sock);
138*a466cc55SCy Schubert dns_sock = -1;
139*a466cc55SCy Schubert }
1402b15cb3dSCy Schubert }
1412b15cb3dSCy Schubert
strtolower(char * s)142*a466cc55SCy Schubert static void strtolower(char *s)
143*a466cc55SCy Schubert {
144*a466cc55SCy Schubert while (*s) {
145*a466cc55SCy Schubert *s = EVUTIL_TOLOWER_(*s);
146*a466cc55SCy Schubert ++s;
147*a466cc55SCy Schubert }
148*a466cc55SCy Schubert }
1492b15cb3dSCy Schubert void
regress_dns_server_cb(struct evdns_server_request * req,void * data)1502b15cb3dSCy Schubert regress_dns_server_cb(struct evdns_server_request *req, void *data)
1512b15cb3dSCy Schubert {
1522b15cb3dSCy Schubert struct regress_dns_server_table *tab = data;
153*a466cc55SCy Schubert char *question;
1542b15cb3dSCy Schubert
1552b15cb3dSCy Schubert if (req->nquestions != 1)
1562b15cb3dSCy Schubert TT_DIE(("Only handling one question at a time; got %d",
1572b15cb3dSCy Schubert req->nquestions));
1582b15cb3dSCy Schubert
1592b15cb3dSCy Schubert question = req->questions[0]->name;
1602b15cb3dSCy Schubert
1612b15cb3dSCy Schubert while (tab->q && evutil_ascii_strcasecmp(question, tab->q) &&
1622b15cb3dSCy Schubert strcmp("*", tab->q))
1632b15cb3dSCy Schubert ++tab;
1642b15cb3dSCy Schubert if (tab->q == NULL)
1652b15cb3dSCy Schubert TT_DIE(("Unexpected question: '%s'", question));
1662b15cb3dSCy Schubert
1672b15cb3dSCy Schubert ++tab->seen;
1682b15cb3dSCy Schubert
169*a466cc55SCy Schubert if (tab->lower)
170*a466cc55SCy Schubert strtolower(question);
171*a466cc55SCy Schubert
1722b15cb3dSCy Schubert if (!strcmp(tab->anstype, "err")) {
1732b15cb3dSCy Schubert int err = atoi(tab->ans);
1742b15cb3dSCy Schubert tt_assert(! evdns_server_request_respond(req, err));
1752b15cb3dSCy Schubert return;
1762b15cb3dSCy Schubert } else if (!strcmp(tab->anstype, "errsoa")) {
1772b15cb3dSCy Schubert int err = atoi(tab->ans);
1782b15cb3dSCy Schubert char soa_record[] =
1792b15cb3dSCy Schubert "\x04" "dns1" "\x05" "icann" "\x03" "org" "\0"
1802b15cb3dSCy Schubert "\x0a" "hostmaster" "\x05" "icann" "\x03" "org" "\0"
1812b15cb3dSCy Schubert "\x77\xde\x5e\xba" /* serial */
1822b15cb3dSCy Schubert "\x00\x00\x1c\x20" /* refreshtime = 2h */
1832b15cb3dSCy Schubert "\x00\x00\x0e\x10" /* retry = 1h */
1842b15cb3dSCy Schubert "\x00\x12\x75\x00" /* expiration = 14d */
1852b15cb3dSCy Schubert "\x00\x00\x0e\x10" /* min.ttl = 1h */
1862b15cb3dSCy Schubert ;
1872b15cb3dSCy Schubert evdns_server_request_add_reply(
1882b15cb3dSCy Schubert req, EVDNS_AUTHORITY_SECTION,
1892b15cb3dSCy Schubert "example.com", EVDNS_TYPE_SOA, EVDNS_CLASS_INET,
1902b15cb3dSCy Schubert 42, sizeof(soa_record) - 1, 0, soa_record);
1912b15cb3dSCy Schubert tt_assert(! evdns_server_request_respond(req, err));
1922b15cb3dSCy Schubert return;
1932b15cb3dSCy Schubert } else if (!strcmp(tab->anstype, "A")) {
1942b15cb3dSCy Schubert struct in_addr in;
1952b15cb3dSCy Schubert if (!evutil_inet_pton(AF_INET, tab->ans, &in)) {
1962b15cb3dSCy Schubert TT_DIE(("Bad A value %s in table", tab->ans));
1972b15cb3dSCy Schubert }
1982b15cb3dSCy Schubert evdns_server_request_add_a_reply(req, question, 1, &in.s_addr,
1992b15cb3dSCy Schubert 100);
2002b15cb3dSCy Schubert } else if (!strcmp(tab->anstype, "AAAA")) {
2012b15cb3dSCy Schubert struct in6_addr in6;
2022b15cb3dSCy Schubert if (!evutil_inet_pton(AF_INET6, tab->ans, &in6)) {
2032b15cb3dSCy Schubert TT_DIE(("Bad AAAA value %s in table", tab->ans));
2042b15cb3dSCy Schubert }
2052b15cb3dSCy Schubert evdns_server_request_add_aaaa_reply(req,
2062b15cb3dSCy Schubert question, 1, &in6.s6_addr, 100);
2072b15cb3dSCy Schubert } else {
2082b15cb3dSCy Schubert TT_DIE(("Weird table entry with type '%s'", tab->anstype));
2092b15cb3dSCy Schubert }
2102b15cb3dSCy Schubert tt_assert(! evdns_server_request_respond(req, 0))
2112b15cb3dSCy Schubert return;
2122b15cb3dSCy Schubert end:
2132b15cb3dSCy Schubert tt_want(! evdns_server_request_drop(req));
2142b15cb3dSCy Schubert }
2152b15cb3dSCy Schubert
2162b15cb3dSCy Schubert int
regress_dnsserver(struct event_base * base,ev_uint16_t * port,struct regress_dns_server_table * search_table)2172b15cb3dSCy Schubert regress_dnsserver(struct event_base *base, ev_uint16_t *port,
2182b15cb3dSCy Schubert struct regress_dns_server_table *search_table)
2192b15cb3dSCy Schubert {
2202b15cb3dSCy Schubert dns_port = regress_get_dnsserver(base, port, &dns_sock,
2212b15cb3dSCy Schubert regress_dns_server_cb, search_table);
2222b15cb3dSCy Schubert return dns_port != NULL;
2232b15cb3dSCy Schubert }
2242b15cb3dSCy Schubert
2252b15cb3dSCy Schubert int
regress_get_listener_addr(struct evconnlistener * lev,struct sockaddr * sa,ev_socklen_t * socklen)2262b15cb3dSCy Schubert regress_get_listener_addr(struct evconnlistener *lev,
2272b15cb3dSCy Schubert struct sockaddr *sa, ev_socklen_t *socklen)
2282b15cb3dSCy Schubert {
2292b15cb3dSCy Schubert evutil_socket_t s = evconnlistener_get_fd(lev);
2302b15cb3dSCy Schubert if (s <= 0)
2312b15cb3dSCy Schubert return -1;
2322b15cb3dSCy Schubert return getsockname(s, sa, socklen);
2332b15cb3dSCy Schubert }
234