michael@0: /* michael@0: * Copyright (c) 2010-2012 Niels Provos and Nick Mathewson michael@0: * michael@0: * Redistribution and use in source and binary forms, with or without michael@0: * modification, are permitted provided that the following conditions michael@0: * are met: michael@0: * 1. Redistributions of source code must retain the above copyright michael@0: * notice, this list of conditions and the following disclaimer. michael@0: * 2. Redistributions in binary form must reproduce the above copyright michael@0: * notice, this list of conditions and the following disclaimer in the michael@0: * documentation and/or other materials provided with the distribution. michael@0: * 3. The name of the author may not be used to endorse or promote products michael@0: * derived from this software without specific prior written permission. michael@0: * michael@0: * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR michael@0: * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES michael@0: * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. michael@0: * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, michael@0: * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT michael@0: * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, michael@0: * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY michael@0: * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT michael@0: * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF michael@0: * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. michael@0: */ michael@0: michael@0: #ifdef WIN32 michael@0: #include michael@0: #include michael@0: #include michael@0: #endif michael@0: michael@0: #include "event2/event-config.h" michael@0: michael@0: #include michael@0: #include michael@0: #ifdef _EVENT_HAVE_SYS_TIME_H michael@0: #include michael@0: #endif michael@0: #include michael@0: #ifndef WIN32 michael@0: #include michael@0: #include michael@0: #include michael@0: #include michael@0: #include michael@0: #endif michael@0: #ifdef _EVENT_HAVE_NETINET_IN6_H michael@0: #include michael@0: #endif michael@0: #ifdef HAVE_NETDB_H michael@0: #include michael@0: #endif michael@0: #include michael@0: #include michael@0: #include michael@0: #include michael@0: #include michael@0: michael@0: #include "event2/dns.h" michael@0: #include "event2/dns_struct.h" michael@0: #include "event2/event.h" michael@0: #include "event2/event_compat.h" michael@0: #include "event2/util.h" michael@0: #include "event2/listener.h" michael@0: #include "event2/bufferevent.h" michael@0: #include "log-internal.h" michael@0: #include "regress.h" michael@0: #include "regress_testutils.h" michael@0: michael@0: #include "../util-internal.h" michael@0: michael@0: /* globals */ michael@0: static struct evdns_server_port *dns_port; michael@0: evutil_socket_t dns_sock = -1; michael@0: michael@0: /* Helper: return the port that a socket is bound on, in host order. */ michael@0: int michael@0: regress_get_socket_port(evutil_socket_t fd) michael@0: { michael@0: struct sockaddr_storage ss; michael@0: ev_socklen_t socklen = sizeof(ss); michael@0: if (getsockname(fd, (struct sockaddr*)&ss, &socklen) != 0) michael@0: return -1; michael@0: if (ss.ss_family == AF_INET) michael@0: return ntohs( ((struct sockaddr_in*)&ss)->sin_port); michael@0: else if (ss.ss_family == AF_INET6) michael@0: return ntohs( ((struct sockaddr_in6*)&ss)->sin6_port); michael@0: else michael@0: return -1; michael@0: } michael@0: michael@0: struct evdns_server_port * michael@0: regress_get_dnsserver(struct event_base *base, michael@0: ev_uint16_t *portnum, michael@0: evutil_socket_t *psock, michael@0: evdns_request_callback_fn_type cb, michael@0: void *arg) michael@0: { michael@0: struct evdns_server_port *port = NULL; michael@0: evutil_socket_t sock; michael@0: struct sockaddr_in my_addr; michael@0: michael@0: sock = socket(AF_INET, SOCK_DGRAM, 0); michael@0: if (sock < 0) { michael@0: tt_abort_perror("socket"); michael@0: } michael@0: michael@0: evutil_make_socket_nonblocking(sock); michael@0: michael@0: memset(&my_addr, 0, sizeof(my_addr)); michael@0: my_addr.sin_family = AF_INET; michael@0: my_addr.sin_port = htons(*portnum); michael@0: my_addr.sin_addr.s_addr = htonl(0x7f000001UL); michael@0: if (bind(sock, (struct sockaddr*)&my_addr, sizeof(my_addr)) < 0) { michael@0: evutil_closesocket(sock); michael@0: tt_abort_perror("bind"); michael@0: } michael@0: port = evdns_add_server_port_with_base(base, sock, 0, cb, arg); michael@0: if (!*portnum) michael@0: *portnum = regress_get_socket_port(sock); michael@0: if (psock) michael@0: *psock = sock; michael@0: michael@0: return port; michael@0: end: michael@0: return NULL; michael@0: } michael@0: michael@0: void michael@0: regress_clean_dnsserver(void) michael@0: { michael@0: if (dns_port) michael@0: evdns_close_server_port(dns_port); michael@0: if (dns_sock >= 0) michael@0: evutil_closesocket(dns_sock); michael@0: } michael@0: michael@0: void michael@0: regress_dns_server_cb(struct evdns_server_request *req, void *data) michael@0: { michael@0: struct regress_dns_server_table *tab = data; michael@0: const char *question; michael@0: michael@0: if (req->nquestions != 1) michael@0: TT_DIE(("Only handling one question at a time; got %d", michael@0: req->nquestions)); michael@0: michael@0: question = req->questions[0]->name; michael@0: michael@0: while (tab->q && evutil_ascii_strcasecmp(question, tab->q) && michael@0: strcmp("*", tab->q)) michael@0: ++tab; michael@0: if (tab->q == NULL) michael@0: TT_DIE(("Unexpected question: '%s'", question)); michael@0: michael@0: ++tab->seen; michael@0: michael@0: if (!strcmp(tab->anstype, "err")) { michael@0: int err = atoi(tab->ans); michael@0: tt_assert(! evdns_server_request_respond(req, err)); michael@0: return; michael@0: } else if (!strcmp(tab->anstype, "errsoa")) { michael@0: int err = atoi(tab->ans); michael@0: char soa_record[] = michael@0: "\x04" "dns1" "\x05" "icann" "\x03" "org" "\0" michael@0: "\x0a" "hostmaster" "\x05" "icann" "\x03" "org" "\0" michael@0: "\x77\xde\x5e\xba" /* serial */ michael@0: "\x00\x00\x1c\x20" /* refreshtime = 2h */ michael@0: "\x00\x00\x0e\x10" /* retry = 1h */ michael@0: "\x00\x12\x75\x00" /* expiration = 14d */ michael@0: "\x00\x00\x0e\x10" /* min.ttl = 1h */ michael@0: ; michael@0: evdns_server_request_add_reply( michael@0: req, EVDNS_AUTHORITY_SECTION, michael@0: "example.com", EVDNS_TYPE_SOA, EVDNS_CLASS_INET, michael@0: 42, sizeof(soa_record) - 1, 0, soa_record); michael@0: tt_assert(! evdns_server_request_respond(req, err)); michael@0: return; michael@0: } else if (!strcmp(tab->anstype, "A")) { michael@0: struct in_addr in; michael@0: if (!evutil_inet_pton(AF_INET, tab->ans, &in)) { michael@0: TT_DIE(("Bad A value %s in table", tab->ans)); michael@0: } michael@0: evdns_server_request_add_a_reply(req, question, 1, &in.s_addr, michael@0: 100); michael@0: } else if (!strcmp(tab->anstype, "AAAA")) { michael@0: struct in6_addr in6; michael@0: if (!evutil_inet_pton(AF_INET6, tab->ans, &in6)) { michael@0: TT_DIE(("Bad AAAA value %s in table", tab->ans)); michael@0: } michael@0: evdns_server_request_add_aaaa_reply(req, michael@0: question, 1, &in6.s6_addr, 100); michael@0: } else { michael@0: TT_DIE(("Weird table entry with type '%s'", tab->anstype)); michael@0: } michael@0: tt_assert(! evdns_server_request_respond(req, 0)) michael@0: return; michael@0: end: michael@0: tt_want(! evdns_server_request_drop(req)); michael@0: } michael@0: michael@0: int michael@0: regress_dnsserver(struct event_base *base, ev_uint16_t *port, michael@0: struct regress_dns_server_table *search_table) michael@0: { michael@0: dns_port = regress_get_dnsserver(base, port, &dns_sock, michael@0: regress_dns_server_cb, search_table); michael@0: return dns_port != NULL; michael@0: } michael@0: michael@0: int michael@0: regress_get_listener_addr(struct evconnlistener *lev, michael@0: struct sockaddr *sa, ev_socklen_t *socklen) michael@0: { michael@0: evutil_socket_t s = evconnlistener_get_fd(lev); michael@0: if (s <= 0) michael@0: return -1; michael@0: return getsockname(s, sa, socklen); michael@0: }