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