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) 2003-2007 Niels Provos <provos@citi.umich.edu> |
michael@0 | 3 | * Copyright (c) 2007-2012 Niels Provos and Nick Mathewson |
michael@0 | 4 | * |
michael@0 | 5 | * Redistribution and use in source and binary forms, with or without |
michael@0 | 6 | * modification, are permitted provided that the following conditions |
michael@0 | 7 | * are met: |
michael@0 | 8 | * 1. Redistributions of source code must retain the above copyright |
michael@0 | 9 | * notice, this list of conditions and the following disclaimer. |
michael@0 | 10 | * 2. Redistributions in binary form must reproduce the above copyright |
michael@0 | 11 | * notice, this list of conditions and the following disclaimer in the |
michael@0 | 12 | * documentation and/or other materials provided with the distribution. |
michael@0 | 13 | * 3. The name of the author may not be used to endorse or promote products |
michael@0 | 14 | * derived from this software without specific prior written permission. |
michael@0 | 15 | * |
michael@0 | 16 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR |
michael@0 | 17 | * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES |
michael@0 | 18 | * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. |
michael@0 | 19 | * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, |
michael@0 | 20 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT |
michael@0 | 21 | * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
michael@0 | 22 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
michael@0 | 23 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
michael@0 | 24 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF |
michael@0 | 25 | * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
michael@0 | 26 | */ |
michael@0 | 27 | |
michael@0 | 28 | #ifdef WIN32 |
michael@0 | 29 | #include <winsock2.h> |
michael@0 | 30 | #include <windows.h> |
michael@0 | 31 | #include <ws2tcpip.h> |
michael@0 | 32 | #endif |
michael@0 | 33 | |
michael@0 | 34 | #include "event2/event-config.h" |
michael@0 | 35 | |
michael@0 | 36 | #include <sys/types.h> |
michael@0 | 37 | #include <sys/stat.h> |
michael@0 | 38 | #ifdef _EVENT_HAVE_SYS_TIME_H |
michael@0 | 39 | #include <sys/time.h> |
michael@0 | 40 | #endif |
michael@0 | 41 | #include <sys/queue.h> |
michael@0 | 42 | #ifndef WIN32 |
michael@0 | 43 | #include <sys/socket.h> |
michael@0 | 44 | #include <signal.h> |
michael@0 | 45 | #include <netinet/in.h> |
michael@0 | 46 | #include <arpa/inet.h> |
michael@0 | 47 | #include <unistd.h> |
michael@0 | 48 | #endif |
michael@0 | 49 | #ifdef _EVENT_HAVE_NETINET_IN6_H |
michael@0 | 50 | #include <netinet/in6.h> |
michael@0 | 51 | #endif |
michael@0 | 52 | #ifdef HAVE_NETDB_H |
michael@0 | 53 | #include <netdb.h> |
michael@0 | 54 | #endif |
michael@0 | 55 | #include <fcntl.h> |
michael@0 | 56 | #include <stdlib.h> |
michael@0 | 57 | #include <stdio.h> |
michael@0 | 58 | #include <string.h> |
michael@0 | 59 | #include <errno.h> |
michael@0 | 60 | |
michael@0 | 61 | #include "event2/dns.h" |
michael@0 | 62 | #include "event2/dns_compat.h" |
michael@0 | 63 | #include "event2/dns_struct.h" |
michael@0 | 64 | #include "event2/event.h" |
michael@0 | 65 | #include "event2/event_compat.h" |
michael@0 | 66 | #include "event2/event_struct.h" |
michael@0 | 67 | #include "event2/util.h" |
michael@0 | 68 | #include "event2/listener.h" |
michael@0 | 69 | #include "event2/bufferevent.h" |
michael@0 | 70 | #include "log-internal.h" |
michael@0 | 71 | #include "regress.h" |
michael@0 | 72 | #include "regress_testutils.h" |
michael@0 | 73 | |
michael@0 | 74 | #include "../util-internal.h" |
michael@0 | 75 | |
michael@0 | 76 | static int dns_ok = 0; |
michael@0 | 77 | static int dns_got_cancel = 0; |
michael@0 | 78 | static int dns_err = 0; |
michael@0 | 79 | |
michael@0 | 80 | |
michael@0 | 81 | static void |
michael@0 | 82 | dns_gethostbyname_cb(int result, char type, int count, int ttl, |
michael@0 | 83 | void *addresses, void *arg) |
michael@0 | 84 | { |
michael@0 | 85 | dns_ok = dns_err = 0; |
michael@0 | 86 | |
michael@0 | 87 | if (result == DNS_ERR_TIMEOUT) { |
michael@0 | 88 | printf("[Timed out] "); |
michael@0 | 89 | dns_err = result; |
michael@0 | 90 | goto out; |
michael@0 | 91 | } |
michael@0 | 92 | |
michael@0 | 93 | if (result != DNS_ERR_NONE) { |
michael@0 | 94 | printf("[Error code %d] ", result); |
michael@0 | 95 | goto out; |
michael@0 | 96 | } |
michael@0 | 97 | |
michael@0 | 98 | TT_BLATHER(("type: %d, count: %d, ttl: %d: ", type, count, ttl)); |
michael@0 | 99 | |
michael@0 | 100 | switch (type) { |
michael@0 | 101 | case DNS_IPv6_AAAA: { |
michael@0 | 102 | #if defined(_EVENT_HAVE_STRUCT_IN6_ADDR) && defined(_EVENT_HAVE_INET_NTOP) && defined(INET6_ADDRSTRLEN) |
michael@0 | 103 | struct in6_addr *in6_addrs = addresses; |
michael@0 | 104 | char buf[INET6_ADDRSTRLEN+1]; |
michael@0 | 105 | int i; |
michael@0 | 106 | /* a resolution that's not valid does not help */ |
michael@0 | 107 | if (ttl < 0) |
michael@0 | 108 | goto out; |
michael@0 | 109 | for (i = 0; i < count; ++i) { |
michael@0 | 110 | const char *b = evutil_inet_ntop(AF_INET6, &in6_addrs[i], buf,sizeof(buf)); |
michael@0 | 111 | if (b) |
michael@0 | 112 | TT_BLATHER(("%s ", b)); |
michael@0 | 113 | else |
michael@0 | 114 | TT_BLATHER(("%s ", strerror(errno))); |
michael@0 | 115 | } |
michael@0 | 116 | #endif |
michael@0 | 117 | break; |
michael@0 | 118 | } |
michael@0 | 119 | case DNS_IPv4_A: { |
michael@0 | 120 | struct in_addr *in_addrs = addresses; |
michael@0 | 121 | int i; |
michael@0 | 122 | /* a resolution that's not valid does not help */ |
michael@0 | 123 | if (ttl < 0) |
michael@0 | 124 | goto out; |
michael@0 | 125 | for (i = 0; i < count; ++i) |
michael@0 | 126 | TT_BLATHER(("%s ", inet_ntoa(in_addrs[i]))); |
michael@0 | 127 | break; |
michael@0 | 128 | } |
michael@0 | 129 | case DNS_PTR: |
michael@0 | 130 | /* may get at most one PTR */ |
michael@0 | 131 | if (count != 1) |
michael@0 | 132 | goto out; |
michael@0 | 133 | |
michael@0 | 134 | TT_BLATHER(("%s ", *(char **)addresses)); |
michael@0 | 135 | break; |
michael@0 | 136 | default: |
michael@0 | 137 | goto out; |
michael@0 | 138 | } |
michael@0 | 139 | |
michael@0 | 140 | dns_ok = type; |
michael@0 | 141 | |
michael@0 | 142 | out: |
michael@0 | 143 | if (arg == NULL) |
michael@0 | 144 | event_loopexit(NULL); |
michael@0 | 145 | else |
michael@0 | 146 | event_base_loopexit((struct event_base *)arg, NULL); |
michael@0 | 147 | } |
michael@0 | 148 | |
michael@0 | 149 | static void |
michael@0 | 150 | dns_gethostbyname(void) |
michael@0 | 151 | { |
michael@0 | 152 | dns_ok = 0; |
michael@0 | 153 | evdns_resolve_ipv4("www.monkey.org", 0, dns_gethostbyname_cb, NULL); |
michael@0 | 154 | event_dispatch(); |
michael@0 | 155 | |
michael@0 | 156 | tt_int_op(dns_ok, ==, DNS_IPv4_A); |
michael@0 | 157 | test_ok = dns_ok; |
michael@0 | 158 | end: |
michael@0 | 159 | ; |
michael@0 | 160 | } |
michael@0 | 161 | |
michael@0 | 162 | static void |
michael@0 | 163 | dns_gethostbyname6(void) |
michael@0 | 164 | { |
michael@0 | 165 | dns_ok = 0; |
michael@0 | 166 | evdns_resolve_ipv6("www.ietf.org", 0, dns_gethostbyname_cb, NULL); |
michael@0 | 167 | event_dispatch(); |
michael@0 | 168 | |
michael@0 | 169 | if (!dns_ok && dns_err == DNS_ERR_TIMEOUT) { |
michael@0 | 170 | tt_skip(); |
michael@0 | 171 | } |
michael@0 | 172 | |
michael@0 | 173 | tt_int_op(dns_ok, ==, DNS_IPv6_AAAA); |
michael@0 | 174 | test_ok = 1; |
michael@0 | 175 | end: |
michael@0 | 176 | ; |
michael@0 | 177 | } |
michael@0 | 178 | |
michael@0 | 179 | static void |
michael@0 | 180 | dns_gethostbyaddr(void) |
michael@0 | 181 | { |
michael@0 | 182 | struct in_addr in; |
michael@0 | 183 | in.s_addr = htonl(0x7f000001ul); /* 127.0.0.1 */ |
michael@0 | 184 | dns_ok = 0; |
michael@0 | 185 | evdns_resolve_reverse(&in, 0, dns_gethostbyname_cb, NULL); |
michael@0 | 186 | event_dispatch(); |
michael@0 | 187 | |
michael@0 | 188 | tt_int_op(dns_ok, ==, DNS_PTR); |
michael@0 | 189 | test_ok = dns_ok; |
michael@0 | 190 | end: |
michael@0 | 191 | ; |
michael@0 | 192 | } |
michael@0 | 193 | |
michael@0 | 194 | static void |
michael@0 | 195 | dns_resolve_reverse(void *ptr) |
michael@0 | 196 | { |
michael@0 | 197 | struct in_addr in; |
michael@0 | 198 | struct event_base *base = event_base_new(); |
michael@0 | 199 | struct evdns_base *dns = evdns_base_new(base, 1/* init name servers */); |
michael@0 | 200 | struct evdns_request *req = NULL; |
michael@0 | 201 | |
michael@0 | 202 | tt_assert(base); |
michael@0 | 203 | tt_assert(dns); |
michael@0 | 204 | in.s_addr = htonl(0x7f000001ul); /* 127.0.0.1 */ |
michael@0 | 205 | dns_ok = 0; |
michael@0 | 206 | |
michael@0 | 207 | req = evdns_base_resolve_reverse( |
michael@0 | 208 | dns, &in, 0, dns_gethostbyname_cb, base); |
michael@0 | 209 | tt_assert(req); |
michael@0 | 210 | |
michael@0 | 211 | event_base_dispatch(base); |
michael@0 | 212 | |
michael@0 | 213 | tt_int_op(dns_ok, ==, DNS_PTR); |
michael@0 | 214 | |
michael@0 | 215 | end: |
michael@0 | 216 | if (dns) |
michael@0 | 217 | evdns_base_free(dns, 0); |
michael@0 | 218 | if (base) |
michael@0 | 219 | event_base_free(base); |
michael@0 | 220 | } |
michael@0 | 221 | |
michael@0 | 222 | static int n_server_responses = 0; |
michael@0 | 223 | |
michael@0 | 224 | static void |
michael@0 | 225 | dns_server_request_cb(struct evdns_server_request *req, void *data) |
michael@0 | 226 | { |
michael@0 | 227 | int i, r; |
michael@0 | 228 | const char TEST_ARPA[] = "11.11.168.192.in-addr.arpa"; |
michael@0 | 229 | const char TEST_IN6[] = |
michael@0 | 230 | "f.e.f.e." "0.0.0.0." "0.0.0.0." "1.1.1.1." |
michael@0 | 231 | "a.a.a.a." "0.0.0.0." "0.0.0.0." "0.f.f.f.ip6.arpa"; |
michael@0 | 232 | |
michael@0 | 233 | for (i = 0; i < req->nquestions; ++i) { |
michael@0 | 234 | const int qtype = req->questions[i]->type; |
michael@0 | 235 | const int qclass = req->questions[i]->dns_question_class; |
michael@0 | 236 | const char *qname = req->questions[i]->name; |
michael@0 | 237 | |
michael@0 | 238 | struct in_addr ans; |
michael@0 | 239 | ans.s_addr = htonl(0xc0a80b0bUL); /* 192.168.11.11 */ |
michael@0 | 240 | if (qtype == EVDNS_TYPE_A && |
michael@0 | 241 | qclass == EVDNS_CLASS_INET && |
michael@0 | 242 | !evutil_ascii_strcasecmp(qname, "zz.example.com")) { |
michael@0 | 243 | r = evdns_server_request_add_a_reply(req, qname, |
michael@0 | 244 | 1, &ans.s_addr, 12345); |
michael@0 | 245 | if (r<0) |
michael@0 | 246 | dns_ok = 0; |
michael@0 | 247 | } else if (qtype == EVDNS_TYPE_AAAA && |
michael@0 | 248 | qclass == EVDNS_CLASS_INET && |
michael@0 | 249 | !evutil_ascii_strcasecmp(qname, "zz.example.com")) { |
michael@0 | 250 | char addr6[17] = "abcdefghijklmnop"; |
michael@0 | 251 | r = evdns_server_request_add_aaaa_reply(req, |
michael@0 | 252 | qname, 1, addr6, 123); |
michael@0 | 253 | if (r<0) |
michael@0 | 254 | dns_ok = 0; |
michael@0 | 255 | } else if (qtype == EVDNS_TYPE_PTR && |
michael@0 | 256 | qclass == EVDNS_CLASS_INET && |
michael@0 | 257 | !evutil_ascii_strcasecmp(qname, TEST_ARPA)) { |
michael@0 | 258 | r = evdns_server_request_add_ptr_reply(req, NULL, |
michael@0 | 259 | qname, "ZZ.EXAMPLE.COM", 54321); |
michael@0 | 260 | if (r<0) |
michael@0 | 261 | dns_ok = 0; |
michael@0 | 262 | } else if (qtype == EVDNS_TYPE_PTR && |
michael@0 | 263 | qclass == EVDNS_CLASS_INET && |
michael@0 | 264 | !evutil_ascii_strcasecmp(qname, TEST_IN6)){ |
michael@0 | 265 | r = evdns_server_request_add_ptr_reply(req, NULL, |
michael@0 | 266 | qname, |
michael@0 | 267 | "ZZ-INET6.EXAMPLE.COM", 54322); |
michael@0 | 268 | if (r<0) |
michael@0 | 269 | dns_ok = 0; |
michael@0 | 270 | } else if (qtype == EVDNS_TYPE_A && |
michael@0 | 271 | qclass == EVDNS_CLASS_INET && |
michael@0 | 272 | !evutil_ascii_strcasecmp(qname, "drop.example.com")) { |
michael@0 | 273 | if (evdns_server_request_drop(req)<0) |
michael@0 | 274 | dns_ok = 0; |
michael@0 | 275 | return; |
michael@0 | 276 | } else { |
michael@0 | 277 | printf("Unexpected question %d %d \"%s\" ", |
michael@0 | 278 | qtype, qclass, qname); |
michael@0 | 279 | dns_ok = 0; |
michael@0 | 280 | } |
michael@0 | 281 | } |
michael@0 | 282 | r = evdns_server_request_respond(req, 0); |
michael@0 | 283 | if (r<0) { |
michael@0 | 284 | printf("Couldn't send reply. "); |
michael@0 | 285 | dns_ok = 0; |
michael@0 | 286 | } |
michael@0 | 287 | } |
michael@0 | 288 | |
michael@0 | 289 | static void |
michael@0 | 290 | dns_server_gethostbyname_cb(int result, char type, int count, int ttl, |
michael@0 | 291 | void *addresses, void *arg) |
michael@0 | 292 | { |
michael@0 | 293 | if (result == DNS_ERR_CANCEL) { |
michael@0 | 294 | if (arg != (void*)(char*)90909) { |
michael@0 | 295 | printf("Unexpected cancelation"); |
michael@0 | 296 | dns_ok = 0; |
michael@0 | 297 | } |
michael@0 | 298 | dns_got_cancel = 1; |
michael@0 | 299 | goto out; |
michael@0 | 300 | } |
michael@0 | 301 | if (result != DNS_ERR_NONE) { |
michael@0 | 302 | printf("Unexpected result %d. ", result); |
michael@0 | 303 | dns_ok = 0; |
michael@0 | 304 | goto out; |
michael@0 | 305 | } |
michael@0 | 306 | if (count != 1) { |
michael@0 | 307 | printf("Unexpected answer count %d. ", count); |
michael@0 | 308 | dns_ok = 0; |
michael@0 | 309 | goto out; |
michael@0 | 310 | } |
michael@0 | 311 | switch (type) { |
michael@0 | 312 | case DNS_IPv4_A: { |
michael@0 | 313 | struct in_addr *in_addrs = addresses; |
michael@0 | 314 | if (in_addrs[0].s_addr != htonl(0xc0a80b0bUL) || ttl != 12345) { |
michael@0 | 315 | printf("Bad IPv4 response \"%s\" %d. ", |
michael@0 | 316 | inet_ntoa(in_addrs[0]), ttl); |
michael@0 | 317 | dns_ok = 0; |
michael@0 | 318 | goto out; |
michael@0 | 319 | } |
michael@0 | 320 | break; |
michael@0 | 321 | } |
michael@0 | 322 | case DNS_IPv6_AAAA: { |
michael@0 | 323 | #if defined (_EVENT_HAVE_STRUCT_IN6_ADDR) && defined(_EVENT_HAVE_INET_NTOP) && defined(INET6_ADDRSTRLEN) |
michael@0 | 324 | struct in6_addr *in6_addrs = addresses; |
michael@0 | 325 | char buf[INET6_ADDRSTRLEN+1]; |
michael@0 | 326 | if (memcmp(&in6_addrs[0].s6_addr, "abcdefghijklmnop", 16) |
michael@0 | 327 | || ttl != 123) { |
michael@0 | 328 | const char *b = evutil_inet_ntop(AF_INET6, &in6_addrs[0],buf,sizeof(buf)); |
michael@0 | 329 | printf("Bad IPv6 response \"%s\" %d. ", b, ttl); |
michael@0 | 330 | dns_ok = 0; |
michael@0 | 331 | goto out; |
michael@0 | 332 | } |
michael@0 | 333 | #endif |
michael@0 | 334 | break; |
michael@0 | 335 | } |
michael@0 | 336 | case DNS_PTR: { |
michael@0 | 337 | char **addrs = addresses; |
michael@0 | 338 | if (arg != (void*)6) { |
michael@0 | 339 | if (strcmp(addrs[0], "ZZ.EXAMPLE.COM") || |
michael@0 | 340 | ttl != 54321) { |
michael@0 | 341 | printf("Bad PTR response \"%s\" %d. ", |
michael@0 | 342 | addrs[0], ttl); |
michael@0 | 343 | dns_ok = 0; |
michael@0 | 344 | goto out; |
michael@0 | 345 | } |
michael@0 | 346 | } else { |
michael@0 | 347 | if (strcmp(addrs[0], "ZZ-INET6.EXAMPLE.COM") || |
michael@0 | 348 | ttl != 54322) { |
michael@0 | 349 | printf("Bad ipv6 PTR response \"%s\" %d. ", |
michael@0 | 350 | addrs[0], ttl); |
michael@0 | 351 | dns_ok = 0; |
michael@0 | 352 | goto out; |
michael@0 | 353 | } |
michael@0 | 354 | } |
michael@0 | 355 | break; |
michael@0 | 356 | } |
michael@0 | 357 | default: |
michael@0 | 358 | printf("Bad response type %d. ", type); |
michael@0 | 359 | dns_ok = 0; |
michael@0 | 360 | } |
michael@0 | 361 | out: |
michael@0 | 362 | if (++n_server_responses == 3) { |
michael@0 | 363 | event_loopexit(NULL); |
michael@0 | 364 | } |
michael@0 | 365 | } |
michael@0 | 366 | |
michael@0 | 367 | static void |
michael@0 | 368 | dns_server(void) |
michael@0 | 369 | { |
michael@0 | 370 | evutil_socket_t sock=-1; |
michael@0 | 371 | struct sockaddr_in my_addr; |
michael@0 | 372 | struct sockaddr_storage ss; |
michael@0 | 373 | ev_socklen_t slen; |
michael@0 | 374 | struct evdns_server_port *port=NULL; |
michael@0 | 375 | struct in_addr resolve_addr; |
michael@0 | 376 | struct in6_addr resolve_addr6; |
michael@0 | 377 | struct evdns_base *base=NULL; |
michael@0 | 378 | struct evdns_request *req=NULL; |
michael@0 | 379 | |
michael@0 | 380 | dns_ok = 1; |
michael@0 | 381 | |
michael@0 | 382 | base = evdns_base_new(NULL, 0); |
michael@0 | 383 | |
michael@0 | 384 | /* Now configure a nameserver port. */ |
michael@0 | 385 | sock = socket(AF_INET, SOCK_DGRAM, 0); |
michael@0 | 386 | if (sock<0) { |
michael@0 | 387 | tt_abort_perror("socket"); |
michael@0 | 388 | } |
michael@0 | 389 | |
michael@0 | 390 | evutil_make_socket_nonblocking(sock); |
michael@0 | 391 | |
michael@0 | 392 | memset(&my_addr, 0, sizeof(my_addr)); |
michael@0 | 393 | my_addr.sin_family = AF_INET; |
michael@0 | 394 | my_addr.sin_port = 0; /* kernel picks */ |
michael@0 | 395 | my_addr.sin_addr.s_addr = htonl(0x7f000001UL); |
michael@0 | 396 | if (bind(sock, (struct sockaddr*)&my_addr, sizeof(my_addr)) < 0) { |
michael@0 | 397 | tt_abort_perror("bind"); |
michael@0 | 398 | } |
michael@0 | 399 | slen = sizeof(ss); |
michael@0 | 400 | if (getsockname(sock, (struct sockaddr*)&ss, &slen) < 0) { |
michael@0 | 401 | tt_abort_perror("getsockname"); |
michael@0 | 402 | } |
michael@0 | 403 | |
michael@0 | 404 | port = evdns_add_server_port(sock, 0, dns_server_request_cb, NULL); |
michael@0 | 405 | |
michael@0 | 406 | /* Add ourself as the only nameserver, and make sure we really are |
michael@0 | 407 | * the only nameserver. */ |
michael@0 | 408 | evdns_base_nameserver_sockaddr_add(base, (struct sockaddr*)&ss, slen, 0); |
michael@0 | 409 | tt_int_op(evdns_base_count_nameservers(base), ==, 1); |
michael@0 | 410 | |
michael@0 | 411 | /* Send some queries. */ |
michael@0 | 412 | evdns_base_resolve_ipv4(base, "zz.example.com", DNS_QUERY_NO_SEARCH, |
michael@0 | 413 | dns_server_gethostbyname_cb, NULL); |
michael@0 | 414 | evdns_base_resolve_ipv6(base, "zz.example.com", DNS_QUERY_NO_SEARCH, |
michael@0 | 415 | dns_server_gethostbyname_cb, NULL); |
michael@0 | 416 | resolve_addr.s_addr = htonl(0xc0a80b0bUL); /* 192.168.11.11 */ |
michael@0 | 417 | evdns_base_resolve_reverse(base, &resolve_addr, 0, |
michael@0 | 418 | dns_server_gethostbyname_cb, NULL); |
michael@0 | 419 | memcpy(resolve_addr6.s6_addr, |
michael@0 | 420 | "\xff\xf0\x00\x00\x00\x00\xaa\xaa" |
michael@0 | 421 | "\x11\x11\x00\x00\x00\x00\xef\xef", 16); |
michael@0 | 422 | evdns_base_resolve_reverse_ipv6(base, &resolve_addr6, 0, |
michael@0 | 423 | dns_server_gethostbyname_cb, (void*)6); |
michael@0 | 424 | |
michael@0 | 425 | req = evdns_base_resolve_ipv4(base, |
michael@0 | 426 | "drop.example.com", DNS_QUERY_NO_SEARCH, |
michael@0 | 427 | dns_server_gethostbyname_cb, (void*)(char*)90909); |
michael@0 | 428 | |
michael@0 | 429 | evdns_cancel_request(base, req); |
michael@0 | 430 | |
michael@0 | 431 | event_dispatch(); |
michael@0 | 432 | |
michael@0 | 433 | tt_assert(dns_got_cancel); |
michael@0 | 434 | test_ok = dns_ok; |
michael@0 | 435 | |
michael@0 | 436 | end: |
michael@0 | 437 | if (port) |
michael@0 | 438 | evdns_close_server_port(port); |
michael@0 | 439 | if (sock >= 0) |
michael@0 | 440 | evutil_closesocket(sock); |
michael@0 | 441 | if (base) |
michael@0 | 442 | evdns_base_free(base, 0); |
michael@0 | 443 | } |
michael@0 | 444 | |
michael@0 | 445 | static int n_replies_left; |
michael@0 | 446 | static struct event_base *exit_base; |
michael@0 | 447 | |
michael@0 | 448 | struct generic_dns_callback_result { |
michael@0 | 449 | int result; |
michael@0 | 450 | char type; |
michael@0 | 451 | int count; |
michael@0 | 452 | int ttl; |
michael@0 | 453 | size_t addrs_len; |
michael@0 | 454 | void *addrs; |
michael@0 | 455 | char addrs_buf[256]; |
michael@0 | 456 | }; |
michael@0 | 457 | |
michael@0 | 458 | static void |
michael@0 | 459 | generic_dns_callback(int result, char type, int count, int ttl, void *addresses, |
michael@0 | 460 | void *arg) |
michael@0 | 461 | { |
michael@0 | 462 | size_t len; |
michael@0 | 463 | struct generic_dns_callback_result *res = arg; |
michael@0 | 464 | res->result = result; |
michael@0 | 465 | res->type = type; |
michael@0 | 466 | res->count = count; |
michael@0 | 467 | res->ttl = ttl; |
michael@0 | 468 | |
michael@0 | 469 | if (type == DNS_IPv4_A) |
michael@0 | 470 | len = count * 4; |
michael@0 | 471 | else if (type == DNS_IPv6_AAAA) |
michael@0 | 472 | len = count * 16; |
michael@0 | 473 | else if (type == DNS_PTR) |
michael@0 | 474 | len = strlen(addresses)+1; |
michael@0 | 475 | else { |
michael@0 | 476 | res->addrs_len = len = 0; |
michael@0 | 477 | res->addrs = NULL; |
michael@0 | 478 | } |
michael@0 | 479 | if (len) { |
michael@0 | 480 | res->addrs_len = len; |
michael@0 | 481 | if (len > 256) |
michael@0 | 482 | len = 256; |
michael@0 | 483 | memcpy(res->addrs_buf, addresses, len); |
michael@0 | 484 | res->addrs = res->addrs_buf; |
michael@0 | 485 | } |
michael@0 | 486 | |
michael@0 | 487 | if (--n_replies_left == 0) |
michael@0 | 488 | event_base_loopexit(exit_base, NULL); |
michael@0 | 489 | } |
michael@0 | 490 | |
michael@0 | 491 | static struct regress_dns_server_table search_table[] = { |
michael@0 | 492 | { "host.a.example.com", "err", "3", 0 }, |
michael@0 | 493 | { "host.b.example.com", "err", "3", 0 }, |
michael@0 | 494 | { "host.c.example.com", "A", "11.22.33.44", 0 }, |
michael@0 | 495 | { "host2.a.example.com", "err", "3", 0 }, |
michael@0 | 496 | { "host2.b.example.com", "A", "200.100.0.100", 0 }, |
michael@0 | 497 | { "host2.c.example.com", "err", "3", 0 }, |
michael@0 | 498 | { "hostn.a.example.com", "errsoa", "0", 0 }, |
michael@0 | 499 | { "hostn.b.example.com", "errsoa", "3", 0 }, |
michael@0 | 500 | { "hostn.c.example.com", "err", "0", 0 }, |
michael@0 | 501 | |
michael@0 | 502 | { "host", "err", "3", 0 }, |
michael@0 | 503 | { "host2", "err", "3", 0 }, |
michael@0 | 504 | { "*", "err", "3", 0 }, |
michael@0 | 505 | { NULL, NULL, NULL, 0 } |
michael@0 | 506 | }; |
michael@0 | 507 | |
michael@0 | 508 | static void |
michael@0 | 509 | dns_search_test(void *arg) |
michael@0 | 510 | { |
michael@0 | 511 | struct basic_test_data *data = arg; |
michael@0 | 512 | struct event_base *base = data->base; |
michael@0 | 513 | struct evdns_base *dns = NULL; |
michael@0 | 514 | ev_uint16_t portnum = 0; |
michael@0 | 515 | char buf[64]; |
michael@0 | 516 | |
michael@0 | 517 | struct generic_dns_callback_result r[8]; |
michael@0 | 518 | |
michael@0 | 519 | tt_assert(regress_dnsserver(base, &portnum, search_table)); |
michael@0 | 520 | evutil_snprintf(buf, sizeof(buf), "127.0.0.1:%d", (int)portnum); |
michael@0 | 521 | |
michael@0 | 522 | dns = evdns_base_new(base, 0); |
michael@0 | 523 | tt_assert(!evdns_base_nameserver_ip_add(dns, buf)); |
michael@0 | 524 | |
michael@0 | 525 | evdns_base_search_add(dns, "a.example.com"); |
michael@0 | 526 | evdns_base_search_add(dns, "b.example.com"); |
michael@0 | 527 | evdns_base_search_add(dns, "c.example.com"); |
michael@0 | 528 | |
michael@0 | 529 | n_replies_left = sizeof(r)/sizeof(r[0]); |
michael@0 | 530 | exit_base = base; |
michael@0 | 531 | |
michael@0 | 532 | evdns_base_resolve_ipv4(dns, "host", 0, generic_dns_callback, &r[0]); |
michael@0 | 533 | evdns_base_resolve_ipv4(dns, "host2", 0, generic_dns_callback, &r[1]); |
michael@0 | 534 | evdns_base_resolve_ipv4(dns, "host", DNS_NO_SEARCH, generic_dns_callback, &r[2]); |
michael@0 | 535 | evdns_base_resolve_ipv4(dns, "host2", DNS_NO_SEARCH, generic_dns_callback, &r[3]); |
michael@0 | 536 | evdns_base_resolve_ipv4(dns, "host3", 0, generic_dns_callback, &r[4]); |
michael@0 | 537 | evdns_base_resolve_ipv4(dns, "hostn.a.example.com", DNS_NO_SEARCH, generic_dns_callback, &r[5]); |
michael@0 | 538 | evdns_base_resolve_ipv4(dns, "hostn.b.example.com", DNS_NO_SEARCH, generic_dns_callback, &r[6]); |
michael@0 | 539 | evdns_base_resolve_ipv4(dns, "hostn.c.example.com", DNS_NO_SEARCH, generic_dns_callback, &r[7]); |
michael@0 | 540 | |
michael@0 | 541 | event_base_dispatch(base); |
michael@0 | 542 | |
michael@0 | 543 | tt_int_op(r[0].type, ==, DNS_IPv4_A); |
michael@0 | 544 | tt_int_op(r[0].count, ==, 1); |
michael@0 | 545 | tt_int_op(((ev_uint32_t*)r[0].addrs)[0], ==, htonl(0x0b16212c)); |
michael@0 | 546 | tt_int_op(r[1].type, ==, DNS_IPv4_A); |
michael@0 | 547 | tt_int_op(r[1].count, ==, 1); |
michael@0 | 548 | tt_int_op(((ev_uint32_t*)r[1].addrs)[0], ==, htonl(0xc8640064)); |
michael@0 | 549 | tt_int_op(r[2].result, ==, DNS_ERR_NOTEXIST); |
michael@0 | 550 | tt_int_op(r[3].result, ==, DNS_ERR_NOTEXIST); |
michael@0 | 551 | tt_int_op(r[4].result, ==, DNS_ERR_NOTEXIST); |
michael@0 | 552 | tt_int_op(r[5].result, ==, DNS_ERR_NODATA); |
michael@0 | 553 | tt_int_op(r[5].ttl, ==, 42); |
michael@0 | 554 | tt_int_op(r[6].result, ==, DNS_ERR_NOTEXIST); |
michael@0 | 555 | tt_int_op(r[6].ttl, ==, 42); |
michael@0 | 556 | tt_int_op(r[7].result, ==, DNS_ERR_NODATA); |
michael@0 | 557 | tt_int_op(r[7].ttl, ==, 0); |
michael@0 | 558 | |
michael@0 | 559 | end: |
michael@0 | 560 | if (dns) |
michael@0 | 561 | evdns_base_free(dns, 0); |
michael@0 | 562 | |
michael@0 | 563 | regress_clean_dnsserver(); |
michael@0 | 564 | } |
michael@0 | 565 | |
michael@0 | 566 | static int request_count = 0; |
michael@0 | 567 | static struct evdns_request *current_req = NULL; |
michael@0 | 568 | |
michael@0 | 569 | static void |
michael@0 | 570 | search_cancel_server_cb(struct evdns_server_request *req, void *data) |
michael@0 | 571 | { |
michael@0 | 572 | const char *question; |
michael@0 | 573 | |
michael@0 | 574 | if (req->nquestions != 1) |
michael@0 | 575 | TT_DIE(("Only handling one question at a time; got %d", |
michael@0 | 576 | req->nquestions)); |
michael@0 | 577 | |
michael@0 | 578 | question = req->questions[0]->name; |
michael@0 | 579 | |
michael@0 | 580 | TT_BLATHER(("got question, %s", question)); |
michael@0 | 581 | |
michael@0 | 582 | tt_assert(request_count > 0); |
michael@0 | 583 | tt_assert(!evdns_server_request_respond(req, 3)); |
michael@0 | 584 | |
michael@0 | 585 | if (!--request_count) |
michael@0 | 586 | evdns_cancel_request(NULL, current_req); |
michael@0 | 587 | |
michael@0 | 588 | end: |
michael@0 | 589 | ; |
michael@0 | 590 | } |
michael@0 | 591 | |
michael@0 | 592 | static void |
michael@0 | 593 | dns_search_cancel_test(void *arg) |
michael@0 | 594 | { |
michael@0 | 595 | struct basic_test_data *data = arg; |
michael@0 | 596 | struct event_base *base = data->base; |
michael@0 | 597 | struct evdns_base *dns = NULL; |
michael@0 | 598 | struct evdns_server_port *port = NULL; |
michael@0 | 599 | ev_uint16_t portnum = 0; |
michael@0 | 600 | struct generic_dns_callback_result r1; |
michael@0 | 601 | char buf[64]; |
michael@0 | 602 | |
michael@0 | 603 | port = regress_get_dnsserver(base, &portnum, NULL, |
michael@0 | 604 | search_cancel_server_cb, NULL); |
michael@0 | 605 | tt_assert(port); |
michael@0 | 606 | evutil_snprintf(buf, sizeof(buf), "127.0.0.1:%d", (int)portnum); |
michael@0 | 607 | |
michael@0 | 608 | dns = evdns_base_new(base, 0); |
michael@0 | 609 | tt_assert(!evdns_base_nameserver_ip_add(dns, buf)); |
michael@0 | 610 | |
michael@0 | 611 | evdns_base_search_add(dns, "a.example.com"); |
michael@0 | 612 | evdns_base_search_add(dns, "b.example.com"); |
michael@0 | 613 | evdns_base_search_add(dns, "c.example.com"); |
michael@0 | 614 | evdns_base_search_add(dns, "d.example.com"); |
michael@0 | 615 | |
michael@0 | 616 | exit_base = base; |
michael@0 | 617 | request_count = 3; |
michael@0 | 618 | n_replies_left = 1; |
michael@0 | 619 | |
michael@0 | 620 | current_req = evdns_base_resolve_ipv4(dns, "host", 0, |
michael@0 | 621 | generic_dns_callback, &r1); |
michael@0 | 622 | event_base_dispatch(base); |
michael@0 | 623 | |
michael@0 | 624 | tt_int_op(r1.result, ==, DNS_ERR_CANCEL); |
michael@0 | 625 | |
michael@0 | 626 | end: |
michael@0 | 627 | if (port) |
michael@0 | 628 | evdns_close_server_port(port); |
michael@0 | 629 | if (dns) |
michael@0 | 630 | evdns_base_free(dns, 0); |
michael@0 | 631 | } |
michael@0 | 632 | |
michael@0 | 633 | static void |
michael@0 | 634 | fail_server_cb(struct evdns_server_request *req, void *data) |
michael@0 | 635 | { |
michael@0 | 636 | const char *question; |
michael@0 | 637 | int *count = data; |
michael@0 | 638 | struct in_addr in; |
michael@0 | 639 | |
michael@0 | 640 | /* Drop the first N requests that we get. */ |
michael@0 | 641 | if (*count > 0) { |
michael@0 | 642 | --*count; |
michael@0 | 643 | tt_want(! evdns_server_request_drop(req)); |
michael@0 | 644 | return; |
michael@0 | 645 | } |
michael@0 | 646 | |
michael@0 | 647 | if (req->nquestions != 1) |
michael@0 | 648 | TT_DIE(("Only handling one question at a time; got %d", |
michael@0 | 649 | req->nquestions)); |
michael@0 | 650 | |
michael@0 | 651 | question = req->questions[0]->name; |
michael@0 | 652 | |
michael@0 | 653 | if (!evutil_ascii_strcasecmp(question, "google.com")) { |
michael@0 | 654 | /* Detect a probe, and get out of the loop. */ |
michael@0 | 655 | event_base_loopexit(exit_base, NULL); |
michael@0 | 656 | } |
michael@0 | 657 | |
michael@0 | 658 | evutil_inet_pton(AF_INET, "16.32.64.128", &in); |
michael@0 | 659 | evdns_server_request_add_a_reply(req, question, 1, &in.s_addr, |
michael@0 | 660 | 100); |
michael@0 | 661 | tt_assert(! evdns_server_request_respond(req, 0)) |
michael@0 | 662 | return; |
michael@0 | 663 | end: |
michael@0 | 664 | tt_want(! evdns_server_request_drop(req)); |
michael@0 | 665 | } |
michael@0 | 666 | |
michael@0 | 667 | static void |
michael@0 | 668 | dns_retry_test(void *arg) |
michael@0 | 669 | { |
michael@0 | 670 | struct basic_test_data *data = arg; |
michael@0 | 671 | struct event_base *base = data->base; |
michael@0 | 672 | struct evdns_server_port *port = NULL; |
michael@0 | 673 | struct evdns_base *dns = NULL; |
michael@0 | 674 | int drop_count = 2; |
michael@0 | 675 | ev_uint16_t portnum = 0; |
michael@0 | 676 | char buf[64]; |
michael@0 | 677 | |
michael@0 | 678 | struct generic_dns_callback_result r1; |
michael@0 | 679 | |
michael@0 | 680 | port = regress_get_dnsserver(base, &portnum, NULL, |
michael@0 | 681 | fail_server_cb, &drop_count); |
michael@0 | 682 | tt_assert(port); |
michael@0 | 683 | evutil_snprintf(buf, sizeof(buf), "127.0.0.1:%d", (int)portnum); |
michael@0 | 684 | |
michael@0 | 685 | dns = evdns_base_new(base, 0); |
michael@0 | 686 | tt_assert(!evdns_base_nameserver_ip_add(dns, buf)); |
michael@0 | 687 | tt_assert(! evdns_base_set_option(dns, "timeout", "0.3")); |
michael@0 | 688 | tt_assert(! evdns_base_set_option(dns, "max-timeouts:", "10")); |
michael@0 | 689 | tt_assert(! evdns_base_set_option(dns, "initial-probe-timeout", "0.5")); |
michael@0 | 690 | |
michael@0 | 691 | evdns_base_resolve_ipv4(dns, "host.example.com", 0, |
michael@0 | 692 | generic_dns_callback, &r1); |
michael@0 | 693 | |
michael@0 | 694 | n_replies_left = 1; |
michael@0 | 695 | exit_base = base; |
michael@0 | 696 | |
michael@0 | 697 | event_base_dispatch(base); |
michael@0 | 698 | |
michael@0 | 699 | tt_int_op(drop_count, ==, 0); |
michael@0 | 700 | |
michael@0 | 701 | tt_int_op(r1.type, ==, DNS_IPv4_A); |
michael@0 | 702 | tt_int_op(r1.count, ==, 1); |
michael@0 | 703 | tt_int_op(((ev_uint32_t*)r1.addrs)[0], ==, htonl(0x10204080)); |
michael@0 | 704 | |
michael@0 | 705 | /* Now try again, but this time have the server get treated as |
michael@0 | 706 | * failed, so we can send it a test probe. */ |
michael@0 | 707 | drop_count = 4; |
michael@0 | 708 | tt_assert(! evdns_base_set_option(dns, "max-timeouts:", "3")); |
michael@0 | 709 | tt_assert(! evdns_base_set_option(dns, "attempts:", "4")); |
michael@0 | 710 | memset(&r1, 0, sizeof(r1)); |
michael@0 | 711 | |
michael@0 | 712 | evdns_base_resolve_ipv4(dns, "host.example.com", 0, |
michael@0 | 713 | generic_dns_callback, &r1); |
michael@0 | 714 | |
michael@0 | 715 | n_replies_left = 2; |
michael@0 | 716 | |
michael@0 | 717 | /* This will run until it answers the "google.com" probe request. */ |
michael@0 | 718 | event_base_dispatch(base); |
michael@0 | 719 | |
michael@0 | 720 | /* We'll treat the server as failed here. */ |
michael@0 | 721 | tt_int_op(r1.result, ==, DNS_ERR_TIMEOUT); |
michael@0 | 722 | |
michael@0 | 723 | /* It should work this time. */ |
michael@0 | 724 | tt_int_op(drop_count, ==, 0); |
michael@0 | 725 | evdns_base_resolve_ipv4(dns, "host.example.com", 0, |
michael@0 | 726 | generic_dns_callback, &r1); |
michael@0 | 727 | |
michael@0 | 728 | event_base_dispatch(base); |
michael@0 | 729 | tt_int_op(r1.result, ==, DNS_ERR_NONE); |
michael@0 | 730 | tt_int_op(r1.type, ==, DNS_IPv4_A); |
michael@0 | 731 | tt_int_op(r1.count, ==, 1); |
michael@0 | 732 | tt_int_op(((ev_uint32_t*)r1.addrs)[0], ==, htonl(0x10204080)); |
michael@0 | 733 | |
michael@0 | 734 | end: |
michael@0 | 735 | if (dns) |
michael@0 | 736 | evdns_base_free(dns, 0); |
michael@0 | 737 | if (port) |
michael@0 | 738 | evdns_close_server_port(port); |
michael@0 | 739 | } |
michael@0 | 740 | |
michael@0 | 741 | static struct regress_dns_server_table internal_error_table[] = { |
michael@0 | 742 | /* Error 4 (NOTIMPL) makes us reissue the request to another server |
michael@0 | 743 | if we can. |
michael@0 | 744 | |
michael@0 | 745 | XXXX we should reissue under a much wider set of circumstances! |
michael@0 | 746 | */ |
michael@0 | 747 | { "foof.example.com", "err", "4", 0 }, |
michael@0 | 748 | { NULL, NULL, NULL, 0 } |
michael@0 | 749 | }; |
michael@0 | 750 | |
michael@0 | 751 | static struct regress_dns_server_table reissue_table[] = { |
michael@0 | 752 | { "foof.example.com", "A", "240.15.240.15", 0 }, |
michael@0 | 753 | { NULL, NULL, NULL, 0 } |
michael@0 | 754 | }; |
michael@0 | 755 | |
michael@0 | 756 | static void |
michael@0 | 757 | dns_reissue_test(void *arg) |
michael@0 | 758 | { |
michael@0 | 759 | struct basic_test_data *data = arg; |
michael@0 | 760 | struct event_base *base = data->base; |
michael@0 | 761 | struct evdns_server_port *port1 = NULL, *port2 = NULL; |
michael@0 | 762 | struct evdns_base *dns = NULL; |
michael@0 | 763 | struct generic_dns_callback_result r1; |
michael@0 | 764 | ev_uint16_t portnum1 = 0, portnum2=0; |
michael@0 | 765 | char buf1[64], buf2[64]; |
michael@0 | 766 | |
michael@0 | 767 | port1 = regress_get_dnsserver(base, &portnum1, NULL, |
michael@0 | 768 | regress_dns_server_cb, internal_error_table); |
michael@0 | 769 | tt_assert(port1); |
michael@0 | 770 | port2 = regress_get_dnsserver(base, &portnum2, NULL, |
michael@0 | 771 | regress_dns_server_cb, reissue_table); |
michael@0 | 772 | tt_assert(port2); |
michael@0 | 773 | evutil_snprintf(buf1, sizeof(buf1), "127.0.0.1:%d", (int)portnum1); |
michael@0 | 774 | evutil_snprintf(buf2, sizeof(buf2), "127.0.0.1:%d", (int)portnum2); |
michael@0 | 775 | |
michael@0 | 776 | dns = evdns_base_new(base, 0); |
michael@0 | 777 | tt_assert(!evdns_base_nameserver_ip_add(dns, buf1)); |
michael@0 | 778 | tt_assert(! evdns_base_set_option(dns, "timeout:", "0.3")); |
michael@0 | 779 | tt_assert(! evdns_base_set_option(dns, "max-timeouts:", "2")); |
michael@0 | 780 | tt_assert(! evdns_base_set_option(dns, "attempts:", "5")); |
michael@0 | 781 | |
michael@0 | 782 | memset(&r1, 0, sizeof(r1)); |
michael@0 | 783 | evdns_base_resolve_ipv4(dns, "foof.example.com", 0, |
michael@0 | 784 | generic_dns_callback, &r1); |
michael@0 | 785 | |
michael@0 | 786 | /* Add this after, so that we are sure to get a reissue. */ |
michael@0 | 787 | tt_assert(!evdns_base_nameserver_ip_add(dns, buf2)); |
michael@0 | 788 | |
michael@0 | 789 | n_replies_left = 1; |
michael@0 | 790 | exit_base = base; |
michael@0 | 791 | |
michael@0 | 792 | event_base_dispatch(base); |
michael@0 | 793 | tt_int_op(r1.result, ==, DNS_ERR_NONE); |
michael@0 | 794 | tt_int_op(r1.type, ==, DNS_IPv4_A); |
michael@0 | 795 | tt_int_op(r1.count, ==, 1); |
michael@0 | 796 | tt_int_op(((ev_uint32_t*)r1.addrs)[0], ==, htonl(0xf00ff00f)); |
michael@0 | 797 | |
michael@0 | 798 | /* Make sure we dropped at least once. */ |
michael@0 | 799 | tt_int_op(internal_error_table[0].seen, >, 0); |
michael@0 | 800 | |
michael@0 | 801 | end: |
michael@0 | 802 | if (dns) |
michael@0 | 803 | evdns_base_free(dns, 0); |
michael@0 | 804 | if (port1) |
michael@0 | 805 | evdns_close_server_port(port1); |
michael@0 | 806 | if (port2) |
michael@0 | 807 | evdns_close_server_port(port2); |
michael@0 | 808 | } |
michael@0 | 809 | |
michael@0 | 810 | #if 0 |
michael@0 | 811 | static void |
michael@0 | 812 | dumb_bytes_fn(char *p, size_t n) |
michael@0 | 813 | { |
michael@0 | 814 | unsigned i; |
michael@0 | 815 | /* This gets us 6 bits of entropy per transaction ID, which means we |
michael@0 | 816 | * will have probably have collisions and need to pick again. */ |
michael@0 | 817 | for (i=0;i<n;++i) |
michael@0 | 818 | p[i] = (char)(rand() & 7); |
michael@0 | 819 | } |
michael@0 | 820 | #endif |
michael@0 | 821 | |
michael@0 | 822 | static void |
michael@0 | 823 | dns_inflight_test(void *arg) |
michael@0 | 824 | { |
michael@0 | 825 | struct basic_test_data *data = arg; |
michael@0 | 826 | struct event_base *base = data->base; |
michael@0 | 827 | struct evdns_base *dns = NULL; |
michael@0 | 828 | ev_uint16_t portnum = 0; |
michael@0 | 829 | char buf[64]; |
michael@0 | 830 | |
michael@0 | 831 | struct generic_dns_callback_result r[20]; |
michael@0 | 832 | int i; |
michael@0 | 833 | |
michael@0 | 834 | tt_assert(regress_dnsserver(base, &portnum, reissue_table)); |
michael@0 | 835 | evutil_snprintf(buf, sizeof(buf), "127.0.0.1:%d", (int)portnum); |
michael@0 | 836 | |
michael@0 | 837 | dns = evdns_base_new(base, 0); |
michael@0 | 838 | tt_assert(!evdns_base_nameserver_ip_add(dns, buf)); |
michael@0 | 839 | tt_assert(! evdns_base_set_option(dns, "max-inflight:", "3")); |
michael@0 | 840 | tt_assert(! evdns_base_set_option(dns, "randomize-case:", "0")); |
michael@0 | 841 | |
michael@0 | 842 | for (i=0;i<20;++i) |
michael@0 | 843 | evdns_base_resolve_ipv4(dns, "foof.example.com", 0, generic_dns_callback, &r[i]); |
michael@0 | 844 | |
michael@0 | 845 | n_replies_left = 20; |
michael@0 | 846 | exit_base = base; |
michael@0 | 847 | |
michael@0 | 848 | event_base_dispatch(base); |
michael@0 | 849 | |
michael@0 | 850 | for (i=0;i<20;++i) { |
michael@0 | 851 | tt_int_op(r[i].type, ==, DNS_IPv4_A); |
michael@0 | 852 | tt_int_op(r[i].count, ==, 1); |
michael@0 | 853 | tt_int_op(((ev_uint32_t*)r[i].addrs)[0], ==, htonl(0xf00ff00f)); |
michael@0 | 854 | } |
michael@0 | 855 | |
michael@0 | 856 | end: |
michael@0 | 857 | if (dns) |
michael@0 | 858 | evdns_base_free(dns, 0); |
michael@0 | 859 | regress_clean_dnsserver(); |
michael@0 | 860 | } |
michael@0 | 861 | |
michael@0 | 862 | /* === Test for bufferevent_socket_connect_hostname */ |
michael@0 | 863 | |
michael@0 | 864 | static int total_connected_or_failed = 0; |
michael@0 | 865 | static int total_n_accepted = 0; |
michael@0 | 866 | static struct event_base *be_connect_hostname_base = NULL; |
michael@0 | 867 | |
michael@0 | 868 | /* Implements a DNS server for the connect_hostname test and the |
michael@0 | 869 | * getaddrinfo_async test */ |
michael@0 | 870 | static void |
michael@0 | 871 | be_getaddrinfo_server_cb(struct evdns_server_request *req, void *data) |
michael@0 | 872 | { |
michael@0 | 873 | int i; |
michael@0 | 874 | int *n_got_p=data; |
michael@0 | 875 | int added_any=0; |
michael@0 | 876 | ++*n_got_p; |
michael@0 | 877 | |
michael@0 | 878 | for (i=0;i<req->nquestions;++i) { |
michael@0 | 879 | const int qtype = req->questions[i]->type; |
michael@0 | 880 | const int qclass = req->questions[i]->dns_question_class; |
michael@0 | 881 | const char *qname = req->questions[i]->name; |
michael@0 | 882 | struct in_addr ans; |
michael@0 | 883 | struct in6_addr ans6; |
michael@0 | 884 | memset(&ans6, 0, sizeof(ans6)); |
michael@0 | 885 | |
michael@0 | 886 | if (qtype == EVDNS_TYPE_A && |
michael@0 | 887 | qclass == EVDNS_CLASS_INET && |
michael@0 | 888 | !evutil_ascii_strcasecmp(qname, "nobodaddy.example.com")) { |
michael@0 | 889 | ans.s_addr = htonl(0x7f000001); |
michael@0 | 890 | evdns_server_request_add_a_reply(req, qname, |
michael@0 | 891 | 1, &ans.s_addr, 2000); |
michael@0 | 892 | added_any = 1; |
michael@0 | 893 | } else if (!evutil_ascii_strcasecmp(qname, |
michael@0 | 894 | "nosuchplace.example.com")) { |
michael@0 | 895 | /* ok, just say notfound. */ |
michael@0 | 896 | } else if (!evutil_ascii_strcasecmp(qname, |
michael@0 | 897 | "both.example.com")) { |
michael@0 | 898 | if (qtype == EVDNS_TYPE_A) { |
michael@0 | 899 | ans.s_addr = htonl(0x50502020); |
michael@0 | 900 | evdns_server_request_add_a_reply(req, qname, |
michael@0 | 901 | 1, &ans.s_addr, 2000); |
michael@0 | 902 | added_any = 1; |
michael@0 | 903 | } else if (qtype == EVDNS_TYPE_AAAA) { |
michael@0 | 904 | ans6.s6_addr[0] = 0x80; |
michael@0 | 905 | ans6.s6_addr[1] = 0xff; |
michael@0 | 906 | ans6.s6_addr[14] = 0xbb; |
michael@0 | 907 | ans6.s6_addr[15] = 0xbb; |
michael@0 | 908 | evdns_server_request_add_aaaa_reply(req, qname, |
michael@0 | 909 | 1, &ans6.s6_addr, 2000); |
michael@0 | 910 | added_any = 1; |
michael@0 | 911 | } |
michael@0 | 912 | evdns_server_request_add_cname_reply(req, qname, |
michael@0 | 913 | "both-canonical.example.com", 1000); |
michael@0 | 914 | } else if (!evutil_ascii_strcasecmp(qname, |
michael@0 | 915 | "v4only.example.com") || |
michael@0 | 916 | !evutil_ascii_strcasecmp(qname, "v4assert.example.com")) { |
michael@0 | 917 | if (qtype == EVDNS_TYPE_A) { |
michael@0 | 918 | ans.s_addr = htonl(0x12345678); |
michael@0 | 919 | evdns_server_request_add_a_reply(req, qname, |
michael@0 | 920 | 1, &ans.s_addr, 2000); |
michael@0 | 921 | added_any = 1; |
michael@0 | 922 | } else if (!evutil_ascii_strcasecmp(qname, |
michael@0 | 923 | "v4assert.example.com")) { |
michael@0 | 924 | TT_FAIL(("Got an AAAA request for v4assert")); |
michael@0 | 925 | } |
michael@0 | 926 | } else if (!evutil_ascii_strcasecmp(qname, |
michael@0 | 927 | "v6only.example.com") || |
michael@0 | 928 | !evutil_ascii_strcasecmp(qname, "v6assert.example.com")) { |
michael@0 | 929 | if (qtype == EVDNS_TYPE_AAAA) { |
michael@0 | 930 | ans6.s6_addr[0] = 0x0b; |
michael@0 | 931 | ans6.s6_addr[1] = 0x0b; |
michael@0 | 932 | ans6.s6_addr[14] = 0xf0; |
michael@0 | 933 | ans6.s6_addr[15] = 0x0d; |
michael@0 | 934 | evdns_server_request_add_aaaa_reply(req, qname, |
michael@0 | 935 | 1, &ans6.s6_addr, 2000); |
michael@0 | 936 | added_any = 1; |
michael@0 | 937 | } else if (!evutil_ascii_strcasecmp(qname, |
michael@0 | 938 | "v6assert.example.com")) { |
michael@0 | 939 | TT_FAIL(("Got a A request for v6assert")); |
michael@0 | 940 | } |
michael@0 | 941 | } else if (!evutil_ascii_strcasecmp(qname, |
michael@0 | 942 | "v6timeout.example.com")) { |
michael@0 | 943 | if (qtype == EVDNS_TYPE_A) { |
michael@0 | 944 | ans.s_addr = htonl(0xabcdef01); |
michael@0 | 945 | evdns_server_request_add_a_reply(req, qname, |
michael@0 | 946 | 1, &ans.s_addr, 2000); |
michael@0 | 947 | added_any = 1; |
michael@0 | 948 | } else if (qtype == EVDNS_TYPE_AAAA) { |
michael@0 | 949 | /* Let the v6 request time out.*/ |
michael@0 | 950 | evdns_server_request_drop(req); |
michael@0 | 951 | return; |
michael@0 | 952 | } |
michael@0 | 953 | } else if (!evutil_ascii_strcasecmp(qname, |
michael@0 | 954 | "v4timeout.example.com")) { |
michael@0 | 955 | if (qtype == EVDNS_TYPE_AAAA) { |
michael@0 | 956 | ans6.s6_addr[0] = 0x0a; |
michael@0 | 957 | ans6.s6_addr[1] = 0x0a; |
michael@0 | 958 | ans6.s6_addr[14] = 0xff; |
michael@0 | 959 | ans6.s6_addr[15] = 0x01; |
michael@0 | 960 | evdns_server_request_add_aaaa_reply(req, qname, |
michael@0 | 961 | 1, &ans6.s6_addr, 2000); |
michael@0 | 962 | added_any = 1; |
michael@0 | 963 | } else if (qtype == EVDNS_TYPE_A) { |
michael@0 | 964 | /* Let the v4 request time out.*/ |
michael@0 | 965 | evdns_server_request_drop(req); |
michael@0 | 966 | return; |
michael@0 | 967 | } |
michael@0 | 968 | } else if (!evutil_ascii_strcasecmp(qname, |
michael@0 | 969 | "v6timeout-nonexist.example.com")) { |
michael@0 | 970 | if (qtype == EVDNS_TYPE_A) { |
michael@0 | 971 | /* Fall through, give an nexist. */ |
michael@0 | 972 | } else if (qtype == EVDNS_TYPE_AAAA) { |
michael@0 | 973 | /* Let the v6 request time out.*/ |
michael@0 | 974 | evdns_server_request_drop(req); |
michael@0 | 975 | return; |
michael@0 | 976 | } |
michael@0 | 977 | } else if (!evutil_ascii_strcasecmp(qname, |
michael@0 | 978 | "all-timeout.example.com")) { |
michael@0 | 979 | /* drop all requests */ |
michael@0 | 980 | evdns_server_request_drop(req); |
michael@0 | 981 | return; |
michael@0 | 982 | } else { |
michael@0 | 983 | TT_GRIPE(("Got weird request for %s",qname)); |
michael@0 | 984 | } |
michael@0 | 985 | } |
michael@0 | 986 | if (added_any) |
michael@0 | 987 | evdns_server_request_respond(req, 0); |
michael@0 | 988 | else |
michael@0 | 989 | evdns_server_request_respond(req, 3); |
michael@0 | 990 | } |
michael@0 | 991 | |
michael@0 | 992 | /* Implements a listener for connect_hostname test. */ |
michael@0 | 993 | static void |
michael@0 | 994 | nil_accept_cb(struct evconnlistener *l, evutil_socket_t fd, struct sockaddr *s, |
michael@0 | 995 | int socklen, void *arg) |
michael@0 | 996 | { |
michael@0 | 997 | int *p = arg; |
michael@0 | 998 | (*p)++; |
michael@0 | 999 | ++total_n_accepted; |
michael@0 | 1000 | /* don't do anything with the socket; let it close when we exit() */ |
michael@0 | 1001 | if (total_n_accepted >= 3 && total_connected_or_failed >= 5) |
michael@0 | 1002 | event_base_loopexit(be_connect_hostname_base, |
michael@0 | 1003 | NULL); |
michael@0 | 1004 | } |
michael@0 | 1005 | |
michael@0 | 1006 | struct be_conn_hostname_result { |
michael@0 | 1007 | int dnserr; |
michael@0 | 1008 | int what; |
michael@0 | 1009 | }; |
michael@0 | 1010 | |
michael@0 | 1011 | /* Bufferevent event callback for the connect_hostname test: remembers what |
michael@0 | 1012 | * event we got. */ |
michael@0 | 1013 | static void |
michael@0 | 1014 | be_connect_hostname_event_cb(struct bufferevent *bev, short what, void *ctx) |
michael@0 | 1015 | { |
michael@0 | 1016 | struct be_conn_hostname_result *got = ctx; |
michael@0 | 1017 | if (!got->what) { |
michael@0 | 1018 | TT_BLATHER(("Got a bufferevent event %d", what)); |
michael@0 | 1019 | got->what = what; |
michael@0 | 1020 | |
michael@0 | 1021 | if ((what & BEV_EVENT_CONNECTED) || (what & BEV_EVENT_ERROR)) { |
michael@0 | 1022 | int r; |
michael@0 | 1023 | if ((r = bufferevent_socket_get_dns_error(bev))) { |
michael@0 | 1024 | got->dnserr = r; |
michael@0 | 1025 | TT_BLATHER(("DNS error %d: %s", r, |
michael@0 | 1026 | evutil_gai_strerror(r))); |
michael@0 | 1027 | } ++total_connected_or_failed; |
michael@0 | 1028 | TT_BLATHER(("Got %d connections or errors.", total_connected_or_failed)); |
michael@0 | 1029 | |
michael@0 | 1030 | if (total_n_accepted >= 3 && total_connected_or_failed >= 5) |
michael@0 | 1031 | event_base_loopexit(be_connect_hostname_base, |
michael@0 | 1032 | NULL); |
michael@0 | 1033 | } |
michael@0 | 1034 | } else { |
michael@0 | 1035 | TT_FAIL(("Two events on one bufferevent. %d,%d", |
michael@0 | 1036 | got->what, (int)what)); |
michael@0 | 1037 | } |
michael@0 | 1038 | } |
michael@0 | 1039 | |
michael@0 | 1040 | static void |
michael@0 | 1041 | test_bufferevent_connect_hostname(void *arg) |
michael@0 | 1042 | { |
michael@0 | 1043 | struct basic_test_data *data = arg; |
michael@0 | 1044 | struct evconnlistener *listener = NULL; |
michael@0 | 1045 | struct bufferevent *be1=NULL, *be2=NULL, *be3=NULL, *be4=NULL, *be5=NULL; |
michael@0 | 1046 | struct be_conn_hostname_result be1_outcome={0,0}, be2_outcome={0,0}, |
michael@0 | 1047 | be3_outcome={0,0}, be4_outcome={0,0}, be5_outcome={0,0}; |
michael@0 | 1048 | int expect_err5; |
michael@0 | 1049 | struct evdns_base *dns=NULL; |
michael@0 | 1050 | struct evdns_server_port *port=NULL; |
michael@0 | 1051 | struct sockaddr_in sin; |
michael@0 | 1052 | int listener_port=-1; |
michael@0 | 1053 | ev_uint16_t dns_port=0; |
michael@0 | 1054 | int n_accept=0, n_dns=0; |
michael@0 | 1055 | char buf[128]; |
michael@0 | 1056 | |
michael@0 | 1057 | be_connect_hostname_base = data->base; |
michael@0 | 1058 | |
michael@0 | 1059 | /* Bind an address and figure out what port it's on. */ |
michael@0 | 1060 | memset(&sin, 0, sizeof(sin)); |
michael@0 | 1061 | sin.sin_family = AF_INET; |
michael@0 | 1062 | sin.sin_addr.s_addr = htonl(0x7f000001); /* 127.0.0.1 */ |
michael@0 | 1063 | sin.sin_port = 0; |
michael@0 | 1064 | listener = evconnlistener_new_bind(data->base, nil_accept_cb, |
michael@0 | 1065 | &n_accept, |
michael@0 | 1066 | LEV_OPT_REUSEABLE|LEV_OPT_CLOSE_ON_EXEC, |
michael@0 | 1067 | -1, (struct sockaddr *)&sin, sizeof(sin)); |
michael@0 | 1068 | tt_assert(listener); |
michael@0 | 1069 | listener_port = regress_get_socket_port( |
michael@0 | 1070 | evconnlistener_get_fd(listener)); |
michael@0 | 1071 | |
michael@0 | 1072 | port = regress_get_dnsserver(data->base, &dns_port, NULL, |
michael@0 | 1073 | be_getaddrinfo_server_cb, &n_dns); |
michael@0 | 1074 | tt_assert(port); |
michael@0 | 1075 | tt_int_op(dns_port, >=, 0); |
michael@0 | 1076 | |
michael@0 | 1077 | /* Start an evdns_base that uses the server as its resolver. */ |
michael@0 | 1078 | dns = evdns_base_new(data->base, 0); |
michael@0 | 1079 | evutil_snprintf(buf, sizeof(buf), "127.0.0.1:%d", (int)dns_port); |
michael@0 | 1080 | evdns_base_nameserver_ip_add(dns, buf); |
michael@0 | 1081 | |
michael@0 | 1082 | /* Now, finally, at long last, launch the bufferevents. One should do |
michael@0 | 1083 | * a failing lookup IP, one should do a successful lookup by IP, |
michael@0 | 1084 | * and one should do a successful lookup by hostname. */ |
michael@0 | 1085 | be1 = bufferevent_socket_new(data->base, -1, BEV_OPT_CLOSE_ON_FREE); |
michael@0 | 1086 | be2 = bufferevent_socket_new(data->base, -1, BEV_OPT_CLOSE_ON_FREE); |
michael@0 | 1087 | be3 = bufferevent_socket_new(data->base, -1, BEV_OPT_CLOSE_ON_FREE); |
michael@0 | 1088 | be4 = bufferevent_socket_new(data->base, -1, BEV_OPT_CLOSE_ON_FREE); |
michael@0 | 1089 | be5 = bufferevent_socket_new(data->base, -1, BEV_OPT_CLOSE_ON_FREE); |
michael@0 | 1090 | |
michael@0 | 1091 | bufferevent_setcb(be1, NULL, NULL, be_connect_hostname_event_cb, |
michael@0 | 1092 | &be1_outcome); |
michael@0 | 1093 | bufferevent_setcb(be2, NULL, NULL, be_connect_hostname_event_cb, |
michael@0 | 1094 | &be2_outcome); |
michael@0 | 1095 | bufferevent_setcb(be3, NULL, NULL, be_connect_hostname_event_cb, |
michael@0 | 1096 | &be3_outcome); |
michael@0 | 1097 | bufferevent_setcb(be4, NULL, NULL, be_connect_hostname_event_cb, |
michael@0 | 1098 | &be4_outcome); |
michael@0 | 1099 | bufferevent_setcb(be5, NULL, NULL, be_connect_hostname_event_cb, |
michael@0 | 1100 | &be5_outcome); |
michael@0 | 1101 | |
michael@0 | 1102 | /* Launch an async resolve that will fail. */ |
michael@0 | 1103 | tt_assert(!bufferevent_socket_connect_hostname(be1, dns, AF_INET, |
michael@0 | 1104 | "nosuchplace.example.com", listener_port)); |
michael@0 | 1105 | /* Connect to the IP without resolving. */ |
michael@0 | 1106 | tt_assert(!bufferevent_socket_connect_hostname(be2, dns, AF_INET, |
michael@0 | 1107 | "127.0.0.1", listener_port)); |
michael@0 | 1108 | /* Launch an async resolve that will succeed. */ |
michael@0 | 1109 | tt_assert(!bufferevent_socket_connect_hostname(be3, dns, AF_INET, |
michael@0 | 1110 | "nobodaddy.example.com", listener_port)); |
michael@0 | 1111 | /* Use the blocking resolver. This one will fail if your resolver |
michael@0 | 1112 | * can't resolve localhost to 127.0.0.1 */ |
michael@0 | 1113 | tt_assert(!bufferevent_socket_connect_hostname(be4, NULL, AF_INET, |
michael@0 | 1114 | "localhost", listener_port)); |
michael@0 | 1115 | /* Use the blocking resolver with a nonexistent hostname. */ |
michael@0 | 1116 | tt_assert(!bufferevent_socket_connect_hostname(be5, NULL, AF_INET, |
michael@0 | 1117 | "nonesuch.nowhere.example.com", 80)); |
michael@0 | 1118 | { |
michael@0 | 1119 | /* The blocking resolver will use the system nameserver, which |
michael@0 | 1120 | * might tell us anything. (Yes, some twits even pretend that |
michael@0 | 1121 | * example.com is real.) Let's see what answer to expect. */ |
michael@0 | 1122 | struct evutil_addrinfo hints, *ai = NULL; |
michael@0 | 1123 | memset(&hints, 0, sizeof(hints)); |
michael@0 | 1124 | hints.ai_family = AF_INET; |
michael@0 | 1125 | hints.ai_socktype = SOCK_STREAM; |
michael@0 | 1126 | hints.ai_protocol = IPPROTO_TCP; |
michael@0 | 1127 | expect_err5 = evutil_getaddrinfo( |
michael@0 | 1128 | "nonesuch.nowhere.example.com", "80", &hints, &ai); |
michael@0 | 1129 | } |
michael@0 | 1130 | |
michael@0 | 1131 | event_base_dispatch(data->base); |
michael@0 | 1132 | |
michael@0 | 1133 | tt_int_op(be1_outcome.what, ==, BEV_EVENT_ERROR); |
michael@0 | 1134 | tt_int_op(be1_outcome.dnserr, ==, EVUTIL_EAI_NONAME); |
michael@0 | 1135 | tt_int_op(be2_outcome.what, ==, BEV_EVENT_CONNECTED); |
michael@0 | 1136 | tt_int_op(be2_outcome.dnserr, ==, 0); |
michael@0 | 1137 | tt_int_op(be3_outcome.what, ==, BEV_EVENT_CONNECTED); |
michael@0 | 1138 | tt_int_op(be3_outcome.dnserr, ==, 0); |
michael@0 | 1139 | tt_int_op(be4_outcome.what, ==, BEV_EVENT_CONNECTED); |
michael@0 | 1140 | tt_int_op(be4_outcome.dnserr, ==, 0); |
michael@0 | 1141 | if (expect_err5) { |
michael@0 | 1142 | tt_int_op(be5_outcome.what, ==, BEV_EVENT_ERROR); |
michael@0 | 1143 | tt_int_op(be5_outcome.dnserr, ==, expect_err5); |
michael@0 | 1144 | } |
michael@0 | 1145 | |
michael@0 | 1146 | tt_int_op(n_accept, ==, 3); |
michael@0 | 1147 | tt_int_op(n_dns, ==, 2); |
michael@0 | 1148 | |
michael@0 | 1149 | end: |
michael@0 | 1150 | if (listener) |
michael@0 | 1151 | evconnlistener_free(listener); |
michael@0 | 1152 | if (port) |
michael@0 | 1153 | evdns_close_server_port(port); |
michael@0 | 1154 | if (dns) |
michael@0 | 1155 | evdns_base_free(dns, 0); |
michael@0 | 1156 | if (be1) |
michael@0 | 1157 | bufferevent_free(be1); |
michael@0 | 1158 | if (be2) |
michael@0 | 1159 | bufferevent_free(be2); |
michael@0 | 1160 | if (be3) |
michael@0 | 1161 | bufferevent_free(be3); |
michael@0 | 1162 | if (be4) |
michael@0 | 1163 | bufferevent_free(be4); |
michael@0 | 1164 | if (be5) |
michael@0 | 1165 | bufferevent_free(be5); |
michael@0 | 1166 | } |
michael@0 | 1167 | |
michael@0 | 1168 | |
michael@0 | 1169 | struct gai_outcome { |
michael@0 | 1170 | int err; |
michael@0 | 1171 | struct evutil_addrinfo *ai; |
michael@0 | 1172 | }; |
michael@0 | 1173 | |
michael@0 | 1174 | static int n_gai_results_pending = 0; |
michael@0 | 1175 | static struct event_base *exit_base_on_no_pending_results = NULL; |
michael@0 | 1176 | |
michael@0 | 1177 | static void |
michael@0 | 1178 | gai_cb(int err, struct evutil_addrinfo *res, void *ptr) |
michael@0 | 1179 | { |
michael@0 | 1180 | struct gai_outcome *go = ptr; |
michael@0 | 1181 | go->err = err; |
michael@0 | 1182 | go->ai = res; |
michael@0 | 1183 | if (--n_gai_results_pending <= 0 && exit_base_on_no_pending_results) |
michael@0 | 1184 | event_base_loopexit(exit_base_on_no_pending_results, NULL); |
michael@0 | 1185 | if (n_gai_results_pending < 900) |
michael@0 | 1186 | TT_BLATHER(("Got an answer; expecting %d more.", |
michael@0 | 1187 | n_gai_results_pending)); |
michael@0 | 1188 | } |
michael@0 | 1189 | |
michael@0 | 1190 | static void |
michael@0 | 1191 | cancel_gai_cb(evutil_socket_t fd, short what, void *ptr) |
michael@0 | 1192 | { |
michael@0 | 1193 | struct evdns_getaddrinfo_request *r = ptr; |
michael@0 | 1194 | evdns_getaddrinfo_cancel(r); |
michael@0 | 1195 | } |
michael@0 | 1196 | |
michael@0 | 1197 | static void |
michael@0 | 1198 | test_getaddrinfo_async(void *arg) |
michael@0 | 1199 | { |
michael@0 | 1200 | struct basic_test_data *data = arg; |
michael@0 | 1201 | struct evutil_addrinfo hints, *a; |
michael@0 | 1202 | struct gai_outcome local_outcome; |
michael@0 | 1203 | struct gai_outcome a_out[12]; |
michael@0 | 1204 | int i; |
michael@0 | 1205 | struct evdns_getaddrinfo_request *r; |
michael@0 | 1206 | char buf[128]; |
michael@0 | 1207 | struct evdns_server_port *port = NULL; |
michael@0 | 1208 | ev_uint16_t dns_port = 0; |
michael@0 | 1209 | int n_dns_questions = 0; |
michael@0 | 1210 | |
michael@0 | 1211 | struct evdns_base *dns_base = evdns_base_new(data->base, 0); |
michael@0 | 1212 | tt_assert(dns_base); |
michael@0 | 1213 | |
michael@0 | 1214 | /* for localhost */ |
michael@0 | 1215 | evdns_base_load_hosts(dns_base, NULL); |
michael@0 | 1216 | |
michael@0 | 1217 | memset(a_out, 0, sizeof(a_out)); |
michael@0 | 1218 | memset(&local_outcome, 0, sizeof(local_outcome)); |
michael@0 | 1219 | |
michael@0 | 1220 | n_gai_results_pending = 10000; /* don't think about exiting yet. */ |
michael@0 | 1221 | |
michael@0 | 1222 | /* 1. Try some cases that will never hit the asynchronous resolver. */ |
michael@0 | 1223 | /* 1a. Simple case with a symbolic service name */ |
michael@0 | 1224 | memset(&hints, 0, sizeof(hints)); |
michael@0 | 1225 | hints.ai_family = PF_UNSPEC; |
michael@0 | 1226 | hints.ai_socktype = SOCK_STREAM; |
michael@0 | 1227 | memset(&local_outcome, 0, sizeof(local_outcome)); |
michael@0 | 1228 | r = evdns_getaddrinfo(dns_base, "1.2.3.4", "http", |
michael@0 | 1229 | &hints, gai_cb, &local_outcome); |
michael@0 | 1230 | tt_assert(! r); |
michael@0 | 1231 | if (!local_outcome.err) { |
michael@0 | 1232 | tt_ptr_op(local_outcome.ai,!=,NULL); |
michael@0 | 1233 | test_ai_eq(local_outcome.ai, "1.2.3.4:80", SOCK_STREAM, IPPROTO_TCP); |
michael@0 | 1234 | evutil_freeaddrinfo(local_outcome.ai); |
michael@0 | 1235 | local_outcome.ai = NULL; |
michael@0 | 1236 | } else { |
michael@0 | 1237 | TT_BLATHER(("Apparently we have no getservbyname.")); |
michael@0 | 1238 | } |
michael@0 | 1239 | |
michael@0 | 1240 | /* 1b. EVUTIL_AI_NUMERICHOST is set */ |
michael@0 | 1241 | memset(&hints, 0, sizeof(hints)); |
michael@0 | 1242 | hints.ai_family = PF_UNSPEC; |
michael@0 | 1243 | hints.ai_flags = EVUTIL_AI_NUMERICHOST; |
michael@0 | 1244 | memset(&local_outcome, 0, sizeof(local_outcome)); |
michael@0 | 1245 | r = evdns_getaddrinfo(dns_base, "www.google.com", "80", |
michael@0 | 1246 | &hints, gai_cb, &local_outcome); |
michael@0 | 1247 | tt_ptr_op(r,==,NULL); |
michael@0 | 1248 | tt_int_op(local_outcome.err,==,EVUTIL_EAI_NONAME); |
michael@0 | 1249 | tt_ptr_op(local_outcome.ai,==,NULL); |
michael@0 | 1250 | |
michael@0 | 1251 | /* 1c. We give a numeric address (ipv6) */ |
michael@0 | 1252 | memset(&hints, 0, sizeof(hints)); |
michael@0 | 1253 | memset(&local_outcome, 0, sizeof(local_outcome)); |
michael@0 | 1254 | hints.ai_family = PF_UNSPEC; |
michael@0 | 1255 | hints.ai_protocol = IPPROTO_TCP; |
michael@0 | 1256 | r = evdns_getaddrinfo(dns_base, "f::f", "8008", |
michael@0 | 1257 | &hints, gai_cb, &local_outcome); |
michael@0 | 1258 | tt_assert(!r); |
michael@0 | 1259 | tt_int_op(local_outcome.err,==,0); |
michael@0 | 1260 | tt_assert(local_outcome.ai); |
michael@0 | 1261 | tt_ptr_op(local_outcome.ai->ai_next,==,NULL); |
michael@0 | 1262 | test_ai_eq(local_outcome.ai, "[f::f]:8008", SOCK_STREAM, IPPROTO_TCP); |
michael@0 | 1263 | evutil_freeaddrinfo(local_outcome.ai); |
michael@0 | 1264 | local_outcome.ai = NULL; |
michael@0 | 1265 | |
michael@0 | 1266 | /* 1d. We give a numeric address (ipv4) */ |
michael@0 | 1267 | memset(&hints, 0, sizeof(hints)); |
michael@0 | 1268 | memset(&local_outcome, 0, sizeof(local_outcome)); |
michael@0 | 1269 | hints.ai_family = PF_UNSPEC; |
michael@0 | 1270 | r = evdns_getaddrinfo(dns_base, "5.6.7.8", NULL, |
michael@0 | 1271 | &hints, gai_cb, &local_outcome); |
michael@0 | 1272 | tt_assert(!r); |
michael@0 | 1273 | tt_int_op(local_outcome.err,==,0); |
michael@0 | 1274 | tt_assert(local_outcome.ai); |
michael@0 | 1275 | a = ai_find_by_protocol(local_outcome.ai, IPPROTO_TCP); |
michael@0 | 1276 | tt_assert(a); |
michael@0 | 1277 | test_ai_eq(a, "5.6.7.8", SOCK_STREAM, IPPROTO_TCP); |
michael@0 | 1278 | a = ai_find_by_protocol(local_outcome.ai, IPPROTO_UDP); |
michael@0 | 1279 | tt_assert(a); |
michael@0 | 1280 | test_ai_eq(a, "5.6.7.8", SOCK_DGRAM, IPPROTO_UDP); |
michael@0 | 1281 | evutil_freeaddrinfo(local_outcome.ai); |
michael@0 | 1282 | local_outcome.ai = NULL; |
michael@0 | 1283 | |
michael@0 | 1284 | /* 1e. nodename is NULL (bind) */ |
michael@0 | 1285 | memset(&hints, 0, sizeof(hints)); |
michael@0 | 1286 | memset(&local_outcome, 0, sizeof(local_outcome)); |
michael@0 | 1287 | hints.ai_family = PF_UNSPEC; |
michael@0 | 1288 | hints.ai_socktype = SOCK_DGRAM; |
michael@0 | 1289 | hints.ai_flags = EVUTIL_AI_PASSIVE; |
michael@0 | 1290 | r = evdns_getaddrinfo(dns_base, NULL, "9090", |
michael@0 | 1291 | &hints, gai_cb, &local_outcome); |
michael@0 | 1292 | tt_assert(!r); |
michael@0 | 1293 | tt_int_op(local_outcome.err,==,0); |
michael@0 | 1294 | tt_assert(local_outcome.ai); |
michael@0 | 1295 | /* we should get a v4 address of 0.0.0.0... */ |
michael@0 | 1296 | a = ai_find_by_family(local_outcome.ai, PF_INET); |
michael@0 | 1297 | tt_assert(a); |
michael@0 | 1298 | test_ai_eq(a, "0.0.0.0:9090", SOCK_DGRAM, IPPROTO_UDP); |
michael@0 | 1299 | /* ... and a v6 address of ::0 */ |
michael@0 | 1300 | a = ai_find_by_family(local_outcome.ai, PF_INET6); |
michael@0 | 1301 | tt_assert(a); |
michael@0 | 1302 | test_ai_eq(a, "[::]:9090", SOCK_DGRAM, IPPROTO_UDP); |
michael@0 | 1303 | evutil_freeaddrinfo(local_outcome.ai); |
michael@0 | 1304 | local_outcome.ai = NULL; |
michael@0 | 1305 | |
michael@0 | 1306 | /* 1f. nodename is NULL (connect) */ |
michael@0 | 1307 | memset(&hints, 0, sizeof(hints)); |
michael@0 | 1308 | memset(&local_outcome, 0, sizeof(local_outcome)); |
michael@0 | 1309 | hints.ai_family = PF_UNSPEC; |
michael@0 | 1310 | hints.ai_socktype = SOCK_STREAM; |
michael@0 | 1311 | r = evdns_getaddrinfo(dns_base, NULL, "2", |
michael@0 | 1312 | &hints, gai_cb, &local_outcome); |
michael@0 | 1313 | tt_assert(!r); |
michael@0 | 1314 | tt_int_op(local_outcome.err,==,0); |
michael@0 | 1315 | tt_assert(local_outcome.ai); |
michael@0 | 1316 | /* we should get a v4 address of 127.0.0.1 .... */ |
michael@0 | 1317 | a = ai_find_by_family(local_outcome.ai, PF_INET); |
michael@0 | 1318 | tt_assert(a); |
michael@0 | 1319 | test_ai_eq(a, "127.0.0.1:2", SOCK_STREAM, IPPROTO_TCP); |
michael@0 | 1320 | /* ... and a v6 address of ::1 */ |
michael@0 | 1321 | a = ai_find_by_family(local_outcome.ai, PF_INET6); |
michael@0 | 1322 | tt_assert(a); |
michael@0 | 1323 | test_ai_eq(a, "[::1]:2", SOCK_STREAM, IPPROTO_TCP); |
michael@0 | 1324 | evutil_freeaddrinfo(local_outcome.ai); |
michael@0 | 1325 | local_outcome.ai = NULL; |
michael@0 | 1326 | |
michael@0 | 1327 | /* 1g. We find localhost immediately. (pf_unspec) */ |
michael@0 | 1328 | memset(&hints, 0, sizeof(hints)); |
michael@0 | 1329 | memset(&local_outcome, 0, sizeof(local_outcome)); |
michael@0 | 1330 | hints.ai_family = PF_UNSPEC; |
michael@0 | 1331 | hints.ai_socktype = SOCK_STREAM; |
michael@0 | 1332 | r = evdns_getaddrinfo(dns_base, "LOCALHOST", "80", |
michael@0 | 1333 | &hints, gai_cb, &local_outcome); |
michael@0 | 1334 | tt_assert(!r); |
michael@0 | 1335 | tt_int_op(local_outcome.err,==,0); |
michael@0 | 1336 | tt_assert(local_outcome.ai); |
michael@0 | 1337 | /* we should get a v4 address of 127.0.0.1 .... */ |
michael@0 | 1338 | a = ai_find_by_family(local_outcome.ai, PF_INET); |
michael@0 | 1339 | tt_assert(a); |
michael@0 | 1340 | test_ai_eq(a, "127.0.0.1:80", SOCK_STREAM, IPPROTO_TCP); |
michael@0 | 1341 | /* ... and a v6 address of ::1 */ |
michael@0 | 1342 | a = ai_find_by_family(local_outcome.ai, PF_INET6); |
michael@0 | 1343 | tt_assert(a); |
michael@0 | 1344 | test_ai_eq(a, "[::1]:80", SOCK_STREAM, IPPROTO_TCP); |
michael@0 | 1345 | evutil_freeaddrinfo(local_outcome.ai); |
michael@0 | 1346 | local_outcome.ai = NULL; |
michael@0 | 1347 | |
michael@0 | 1348 | /* 1g. We find localhost immediately. (pf_inet6) */ |
michael@0 | 1349 | memset(&hints, 0, sizeof(hints)); |
michael@0 | 1350 | memset(&local_outcome, 0, sizeof(local_outcome)); |
michael@0 | 1351 | hints.ai_family = PF_INET6; |
michael@0 | 1352 | hints.ai_socktype = SOCK_STREAM; |
michael@0 | 1353 | r = evdns_getaddrinfo(dns_base, "LOCALHOST", "9999", |
michael@0 | 1354 | &hints, gai_cb, &local_outcome); |
michael@0 | 1355 | tt_assert(! r); |
michael@0 | 1356 | tt_int_op(local_outcome.err,==,0); |
michael@0 | 1357 | tt_assert(local_outcome.ai); |
michael@0 | 1358 | a = local_outcome.ai; |
michael@0 | 1359 | test_ai_eq(a, "[::1]:9999", SOCK_STREAM, IPPROTO_TCP); |
michael@0 | 1360 | tt_ptr_op(a->ai_next, ==, NULL); |
michael@0 | 1361 | evutil_freeaddrinfo(local_outcome.ai); |
michael@0 | 1362 | local_outcome.ai = NULL; |
michael@0 | 1363 | |
michael@0 | 1364 | /* 2. Okay, now we can actually test the asynchronous resolver. */ |
michael@0 | 1365 | /* Start a dummy local dns server... */ |
michael@0 | 1366 | port = regress_get_dnsserver(data->base, &dns_port, NULL, |
michael@0 | 1367 | be_getaddrinfo_server_cb, &n_dns_questions); |
michael@0 | 1368 | tt_assert(port); |
michael@0 | 1369 | tt_int_op(dns_port, >=, 0); |
michael@0 | 1370 | /* ... and tell the evdns_base about it. */ |
michael@0 | 1371 | evutil_snprintf(buf, sizeof(buf), "127.0.0.1:%d", dns_port); |
michael@0 | 1372 | evdns_base_nameserver_ip_add(dns_base, buf); |
michael@0 | 1373 | |
michael@0 | 1374 | memset(&hints, 0, sizeof(hints)); |
michael@0 | 1375 | hints.ai_family = PF_UNSPEC; |
michael@0 | 1376 | hints.ai_socktype = SOCK_STREAM; |
michael@0 | 1377 | hints.ai_flags = EVUTIL_AI_CANONNAME; |
michael@0 | 1378 | /* 0: Request for both.example.com should return both addresses. */ |
michael@0 | 1379 | r = evdns_getaddrinfo(dns_base, "both.example.com", "8000", |
michael@0 | 1380 | &hints, gai_cb, &a_out[0]); |
michael@0 | 1381 | tt_assert(r); |
michael@0 | 1382 | |
michael@0 | 1383 | /* 1: Request for v4only.example.com should return one address. */ |
michael@0 | 1384 | r = evdns_getaddrinfo(dns_base, "v4only.example.com", "8001", |
michael@0 | 1385 | &hints, gai_cb, &a_out[1]); |
michael@0 | 1386 | tt_assert(r); |
michael@0 | 1387 | |
michael@0 | 1388 | /* 2: Request for v6only.example.com should return one address. */ |
michael@0 | 1389 | hints.ai_flags = 0; |
michael@0 | 1390 | r = evdns_getaddrinfo(dns_base, "v6only.example.com", "8002", |
michael@0 | 1391 | &hints, gai_cb, &a_out[2]); |
michael@0 | 1392 | tt_assert(r); |
michael@0 | 1393 | |
michael@0 | 1394 | /* 3: PF_INET request for v4assert.example.com should not generate a |
michael@0 | 1395 | * v6 request. The server will fail the test if it does. */ |
michael@0 | 1396 | hints.ai_family = PF_INET; |
michael@0 | 1397 | r = evdns_getaddrinfo(dns_base, "v4assert.example.com", "8003", |
michael@0 | 1398 | &hints, gai_cb, &a_out[3]); |
michael@0 | 1399 | tt_assert(r); |
michael@0 | 1400 | |
michael@0 | 1401 | /* 4: PF_INET6 request for v6assert.example.com should not generate a |
michael@0 | 1402 | * v4 request. The server will fail the test if it does. */ |
michael@0 | 1403 | hints.ai_family = PF_INET6; |
michael@0 | 1404 | r = evdns_getaddrinfo(dns_base, "v6assert.example.com", "8004", |
michael@0 | 1405 | &hints, gai_cb, &a_out[4]); |
michael@0 | 1406 | tt_assert(r); |
michael@0 | 1407 | |
michael@0 | 1408 | /* 5: PF_INET request for nosuchplace.example.com should give NEXIST. */ |
michael@0 | 1409 | hints.ai_family = PF_INET; |
michael@0 | 1410 | r = evdns_getaddrinfo(dns_base, "nosuchplace.example.com", "8005", |
michael@0 | 1411 | &hints, gai_cb, &a_out[5]); |
michael@0 | 1412 | tt_assert(r); |
michael@0 | 1413 | |
michael@0 | 1414 | /* 6: PF_UNSPEC request for nosuchplace.example.com should give NEXIST. |
michael@0 | 1415 | */ |
michael@0 | 1416 | hints.ai_family = PF_UNSPEC; |
michael@0 | 1417 | r = evdns_getaddrinfo(dns_base, "nosuchplace.example.com", "8006", |
michael@0 | 1418 | &hints, gai_cb, &a_out[6]); |
michael@0 | 1419 | tt_assert(r); |
michael@0 | 1420 | |
michael@0 | 1421 | /* 7: PF_UNSPEC request for v6timeout.example.com should give an ipv4 |
michael@0 | 1422 | * address only. */ |
michael@0 | 1423 | hints.ai_family = PF_UNSPEC; |
michael@0 | 1424 | r = evdns_getaddrinfo(dns_base, "v6timeout.example.com", "8007", |
michael@0 | 1425 | &hints, gai_cb, &a_out[7]); |
michael@0 | 1426 | tt_assert(r); |
michael@0 | 1427 | |
michael@0 | 1428 | /* 8: PF_UNSPEC request for v6timeout-nonexist.example.com should give |
michael@0 | 1429 | * a NEXIST */ |
michael@0 | 1430 | hints.ai_family = PF_UNSPEC; |
michael@0 | 1431 | r = evdns_getaddrinfo(dns_base, "v6timeout-nonexist.example.com", |
michael@0 | 1432 | "8008", &hints, gai_cb, &a_out[8]); |
michael@0 | 1433 | tt_assert(r); |
michael@0 | 1434 | |
michael@0 | 1435 | /* 9: AI_ADDRCONFIG should at least not crash. Can't test it more |
michael@0 | 1436 | * without knowing what kind of internet we have. */ |
michael@0 | 1437 | hints.ai_flags |= EVUTIL_AI_ADDRCONFIG; |
michael@0 | 1438 | r = evdns_getaddrinfo(dns_base, "both.example.com", |
michael@0 | 1439 | "8009", &hints, gai_cb, &a_out[9]); |
michael@0 | 1440 | tt_assert(r); |
michael@0 | 1441 | |
michael@0 | 1442 | /* 10: PF_UNSPEC for v4timeout.example.com should give an ipv6 address |
michael@0 | 1443 | * only. */ |
michael@0 | 1444 | hints.ai_family = PF_UNSPEC; |
michael@0 | 1445 | hints.ai_flags = 0; |
michael@0 | 1446 | r = evdns_getaddrinfo(dns_base, "v4timeout.example.com", "8010", |
michael@0 | 1447 | &hints, gai_cb, &a_out[10]); |
michael@0 | 1448 | tt_assert(r); |
michael@0 | 1449 | |
michael@0 | 1450 | /* 11: timeout.example.com: cancel it after 100 msec. */ |
michael@0 | 1451 | r = evdns_getaddrinfo(dns_base, "all-timeout.example.com", "8011", |
michael@0 | 1452 | &hints, gai_cb, &a_out[11]); |
michael@0 | 1453 | tt_assert(r); |
michael@0 | 1454 | { |
michael@0 | 1455 | struct timeval tv; |
michael@0 | 1456 | tv.tv_sec = 0; |
michael@0 | 1457 | tv.tv_usec = 100*1000; /* 100 msec */ |
michael@0 | 1458 | event_base_once(data->base, -1, EV_TIMEOUT, cancel_gai_cb, |
michael@0 | 1459 | r, &tv); |
michael@0 | 1460 | } |
michael@0 | 1461 | |
michael@0 | 1462 | /* XXXXX There are more tests we could do, including: |
michael@0 | 1463 | |
michael@0 | 1464 | - A test to elicit NODATA. |
michael@0 | 1465 | |
michael@0 | 1466 | */ |
michael@0 | 1467 | |
michael@0 | 1468 | n_gai_results_pending = 12; |
michael@0 | 1469 | exit_base_on_no_pending_results = data->base; |
michael@0 | 1470 | |
michael@0 | 1471 | event_base_dispatch(data->base); |
michael@0 | 1472 | |
michael@0 | 1473 | /* 0: both.example.com */ |
michael@0 | 1474 | tt_int_op(a_out[0].err, ==, 0); |
michael@0 | 1475 | tt_assert(a_out[0].ai); |
michael@0 | 1476 | tt_assert(a_out[0].ai->ai_next); |
michael@0 | 1477 | tt_assert(!a_out[0].ai->ai_next->ai_next); |
michael@0 | 1478 | a = ai_find_by_family(a_out[0].ai, PF_INET); |
michael@0 | 1479 | tt_assert(a); |
michael@0 | 1480 | test_ai_eq(a, "80.80.32.32:8000", SOCK_STREAM, IPPROTO_TCP); |
michael@0 | 1481 | a = ai_find_by_family(a_out[0].ai, PF_INET6); |
michael@0 | 1482 | tt_assert(a); |
michael@0 | 1483 | test_ai_eq(a, "[80ff::bbbb]:8000", SOCK_STREAM, IPPROTO_TCP); |
michael@0 | 1484 | tt_assert(a_out[0].ai->ai_canonname); |
michael@0 | 1485 | tt_str_op(a_out[0].ai->ai_canonname, ==, "both-canonical.example.com"); |
michael@0 | 1486 | |
michael@0 | 1487 | /* 1: v4only.example.com */ |
michael@0 | 1488 | tt_int_op(a_out[1].err, ==, 0); |
michael@0 | 1489 | tt_assert(a_out[1].ai); |
michael@0 | 1490 | tt_assert(! a_out[1].ai->ai_next); |
michael@0 | 1491 | test_ai_eq(a_out[1].ai, "18.52.86.120:8001", SOCK_STREAM, IPPROTO_TCP); |
michael@0 | 1492 | tt_assert(a_out[1].ai->ai_canonname == NULL); |
michael@0 | 1493 | |
michael@0 | 1494 | |
michael@0 | 1495 | /* 2: v6only.example.com */ |
michael@0 | 1496 | tt_int_op(a_out[2].err, ==, 0); |
michael@0 | 1497 | tt_assert(a_out[2].ai); |
michael@0 | 1498 | tt_assert(! a_out[2].ai->ai_next); |
michael@0 | 1499 | test_ai_eq(a_out[2].ai, "[b0b::f00d]:8002", SOCK_STREAM, IPPROTO_TCP); |
michael@0 | 1500 | |
michael@0 | 1501 | /* 3: v4assert.example.com */ |
michael@0 | 1502 | tt_int_op(a_out[3].err, ==, 0); |
michael@0 | 1503 | tt_assert(a_out[3].ai); |
michael@0 | 1504 | tt_assert(! a_out[3].ai->ai_next); |
michael@0 | 1505 | test_ai_eq(a_out[3].ai, "18.52.86.120:8003", SOCK_STREAM, IPPROTO_TCP); |
michael@0 | 1506 | |
michael@0 | 1507 | /* 4: v6assert.example.com */ |
michael@0 | 1508 | tt_int_op(a_out[4].err, ==, 0); |
michael@0 | 1509 | tt_assert(a_out[4].ai); |
michael@0 | 1510 | tt_assert(! a_out[4].ai->ai_next); |
michael@0 | 1511 | test_ai_eq(a_out[4].ai, "[b0b::f00d]:8004", SOCK_STREAM, IPPROTO_TCP); |
michael@0 | 1512 | |
michael@0 | 1513 | /* 5: nosuchplace.example.com (inet) */ |
michael@0 | 1514 | tt_int_op(a_out[5].err, ==, EVUTIL_EAI_NONAME); |
michael@0 | 1515 | tt_assert(! a_out[5].ai); |
michael@0 | 1516 | |
michael@0 | 1517 | /* 6: nosuchplace.example.com (unspec) */ |
michael@0 | 1518 | tt_int_op(a_out[6].err, ==, EVUTIL_EAI_NONAME); |
michael@0 | 1519 | tt_assert(! a_out[6].ai); |
michael@0 | 1520 | |
michael@0 | 1521 | /* 7: v6timeout.example.com */ |
michael@0 | 1522 | tt_int_op(a_out[7].err, ==, 0); |
michael@0 | 1523 | tt_assert(a_out[7].ai); |
michael@0 | 1524 | tt_assert(! a_out[7].ai->ai_next); |
michael@0 | 1525 | test_ai_eq(a_out[7].ai, "171.205.239.1:8007", SOCK_STREAM, IPPROTO_TCP); |
michael@0 | 1526 | |
michael@0 | 1527 | /* 8: v6timeout-nonexist.example.com */ |
michael@0 | 1528 | tt_int_op(a_out[8].err, ==, EVUTIL_EAI_NONAME); |
michael@0 | 1529 | tt_assert(! a_out[8].ai); |
michael@0 | 1530 | |
michael@0 | 1531 | /* 9: both (ADDRCONFIG) */ |
michael@0 | 1532 | tt_int_op(a_out[9].err, ==, 0); |
michael@0 | 1533 | tt_assert(a_out[9].ai); |
michael@0 | 1534 | a = ai_find_by_family(a_out[9].ai, PF_INET); |
michael@0 | 1535 | if (a) |
michael@0 | 1536 | test_ai_eq(a, "80.80.32.32:8009", SOCK_STREAM, IPPROTO_TCP); |
michael@0 | 1537 | else |
michael@0 | 1538 | tt_assert(ai_find_by_family(a_out[9].ai, PF_INET6)); |
michael@0 | 1539 | a = ai_find_by_family(a_out[9].ai, PF_INET6); |
michael@0 | 1540 | if (a) |
michael@0 | 1541 | test_ai_eq(a, "[80ff::bbbb]:8009", SOCK_STREAM, IPPROTO_TCP); |
michael@0 | 1542 | else |
michael@0 | 1543 | tt_assert(ai_find_by_family(a_out[9].ai, PF_INET)); |
michael@0 | 1544 | |
michael@0 | 1545 | /* 10: v4timeout.example.com */ |
michael@0 | 1546 | tt_int_op(a_out[10].err, ==, 0); |
michael@0 | 1547 | tt_assert(a_out[10].ai); |
michael@0 | 1548 | tt_assert(! a_out[10].ai->ai_next); |
michael@0 | 1549 | test_ai_eq(a_out[10].ai, "[a0a::ff01]:8010", SOCK_STREAM, IPPROTO_TCP); |
michael@0 | 1550 | |
michael@0 | 1551 | /* 11: cancelled request. */ |
michael@0 | 1552 | tt_int_op(a_out[11].err, ==, EVUTIL_EAI_CANCEL); |
michael@0 | 1553 | tt_assert(a_out[11].ai == NULL); |
michael@0 | 1554 | |
michael@0 | 1555 | end: |
michael@0 | 1556 | if (local_outcome.ai) |
michael@0 | 1557 | evutil_freeaddrinfo(local_outcome.ai); |
michael@0 | 1558 | for (i=0;i<10;++i) { |
michael@0 | 1559 | if (a_out[i].ai) |
michael@0 | 1560 | evutil_freeaddrinfo(a_out[i].ai); |
michael@0 | 1561 | } |
michael@0 | 1562 | if (port) |
michael@0 | 1563 | evdns_close_server_port(port); |
michael@0 | 1564 | if (dns_base) |
michael@0 | 1565 | evdns_base_free(dns_base, 0); |
michael@0 | 1566 | } |
michael@0 | 1567 | |
michael@0 | 1568 | struct gaic_request_status { |
michael@0 | 1569 | int magic; |
michael@0 | 1570 | struct event_base *base; |
michael@0 | 1571 | struct evdns_base *dns_base; |
michael@0 | 1572 | struct evdns_getaddrinfo_request *request; |
michael@0 | 1573 | struct event cancel_event; |
michael@0 | 1574 | int canceled; |
michael@0 | 1575 | }; |
michael@0 | 1576 | |
michael@0 | 1577 | #define GAIC_MAGIC 0x1234abcd |
michael@0 | 1578 | |
michael@0 | 1579 | static int pending = 0; |
michael@0 | 1580 | |
michael@0 | 1581 | static void |
michael@0 | 1582 | gaic_cancel_request_cb(evutil_socket_t fd, short what, void *arg) |
michael@0 | 1583 | { |
michael@0 | 1584 | struct gaic_request_status *status = arg; |
michael@0 | 1585 | |
michael@0 | 1586 | tt_assert(status->magic == GAIC_MAGIC); |
michael@0 | 1587 | status->canceled = 1; |
michael@0 | 1588 | evdns_getaddrinfo_cancel(status->request); |
michael@0 | 1589 | return; |
michael@0 | 1590 | end: |
michael@0 | 1591 | event_base_loopexit(status->base, NULL); |
michael@0 | 1592 | } |
michael@0 | 1593 | |
michael@0 | 1594 | static void |
michael@0 | 1595 | gaic_server_cb(struct evdns_server_request *req, void *arg) |
michael@0 | 1596 | { |
michael@0 | 1597 | ev_uint32_t answer = 0x7f000001; |
michael@0 | 1598 | tt_assert(req->nquestions); |
michael@0 | 1599 | evdns_server_request_add_a_reply(req, req->questions[0]->name, 1, |
michael@0 | 1600 | &answer, 100); |
michael@0 | 1601 | evdns_server_request_respond(req, 0); |
michael@0 | 1602 | return; |
michael@0 | 1603 | end: |
michael@0 | 1604 | evdns_server_request_respond(req, DNS_ERR_REFUSED); |
michael@0 | 1605 | } |
michael@0 | 1606 | |
michael@0 | 1607 | |
michael@0 | 1608 | static void |
michael@0 | 1609 | gaic_getaddrinfo_cb(int result, struct evutil_addrinfo *res, void *arg) |
michael@0 | 1610 | { |
michael@0 | 1611 | struct gaic_request_status *status = arg; |
michael@0 | 1612 | struct event_base *base = status->base; |
michael@0 | 1613 | tt_assert(status->magic == GAIC_MAGIC); |
michael@0 | 1614 | |
michael@0 | 1615 | if (result == EVUTIL_EAI_CANCEL) { |
michael@0 | 1616 | tt_assert(status->canceled); |
michael@0 | 1617 | } |
michael@0 | 1618 | event_del(&status->cancel_event); |
michael@0 | 1619 | |
michael@0 | 1620 | memset(status, 0xf0, sizeof(*status)); |
michael@0 | 1621 | free(status); |
michael@0 | 1622 | |
michael@0 | 1623 | end: |
michael@0 | 1624 | if (--pending <= 0) |
michael@0 | 1625 | event_base_loopexit(base, NULL); |
michael@0 | 1626 | } |
michael@0 | 1627 | |
michael@0 | 1628 | static void |
michael@0 | 1629 | gaic_launch(struct event_base *base, struct evdns_base *dns_base) |
michael@0 | 1630 | { |
michael@0 | 1631 | struct gaic_request_status *status = calloc(1,sizeof(*status)); |
michael@0 | 1632 | struct timeval tv = { 0, 10000 }; |
michael@0 | 1633 | status->magic = GAIC_MAGIC; |
michael@0 | 1634 | status->base = base; |
michael@0 | 1635 | status->dns_base = dns_base; |
michael@0 | 1636 | event_assign(&status->cancel_event, base, -1, 0, gaic_cancel_request_cb, |
michael@0 | 1637 | status); |
michael@0 | 1638 | status->request = evdns_getaddrinfo(dns_base, |
michael@0 | 1639 | "foobar.bazquux.example.com", "80", NULL, gaic_getaddrinfo_cb, |
michael@0 | 1640 | status); |
michael@0 | 1641 | event_add(&status->cancel_event, &tv); |
michael@0 | 1642 | ++pending; |
michael@0 | 1643 | } |
michael@0 | 1644 | |
michael@0 | 1645 | #ifdef EVENT_SET_MEM_FUNCTIONS_IMPLEMENTED |
michael@0 | 1646 | /* FIXME: We should move this to regress_main.c if anything else needs it.*/ |
michael@0 | 1647 | |
michael@0 | 1648 | /* Trivial replacements for malloc/free/realloc to check for memory leaks. |
michael@0 | 1649 | * Not threadsafe. */ |
michael@0 | 1650 | static int allocated_chunks = 0; |
michael@0 | 1651 | |
michael@0 | 1652 | static void * |
michael@0 | 1653 | cnt_malloc(size_t sz) |
michael@0 | 1654 | { |
michael@0 | 1655 | allocated_chunks += 1; |
michael@0 | 1656 | return malloc(sz); |
michael@0 | 1657 | } |
michael@0 | 1658 | |
michael@0 | 1659 | static void * |
michael@0 | 1660 | cnt_realloc(void *old, size_t sz) |
michael@0 | 1661 | { |
michael@0 | 1662 | if (!old) |
michael@0 | 1663 | allocated_chunks += 1; |
michael@0 | 1664 | if (!sz) |
michael@0 | 1665 | allocated_chunks -= 1; |
michael@0 | 1666 | return realloc(old, sz); |
michael@0 | 1667 | } |
michael@0 | 1668 | |
michael@0 | 1669 | static void |
michael@0 | 1670 | cnt_free(void *ptr) |
michael@0 | 1671 | { |
michael@0 | 1672 | allocated_chunks -= 1; |
michael@0 | 1673 | free(ptr); |
michael@0 | 1674 | } |
michael@0 | 1675 | |
michael@0 | 1676 | struct testleak_env_t { |
michael@0 | 1677 | struct event_base *base; |
michael@0 | 1678 | struct evdns_base *dns_base; |
michael@0 | 1679 | struct evdns_request *req; |
michael@0 | 1680 | struct generic_dns_callback_result r; |
michael@0 | 1681 | }; |
michael@0 | 1682 | |
michael@0 | 1683 | static void * |
michael@0 | 1684 | testleak_setup(const struct testcase_t *testcase) |
michael@0 | 1685 | { |
michael@0 | 1686 | struct testleak_env_t *env; |
michael@0 | 1687 | |
michael@0 | 1688 | allocated_chunks = 0; |
michael@0 | 1689 | event_set_mem_functions(cnt_malloc, cnt_realloc, cnt_free); |
michael@0 | 1690 | event_enable_debug_mode(); |
michael@0 | 1691 | |
michael@0 | 1692 | /* not mm_calloc: we don't want to mess with the count. */ |
michael@0 | 1693 | env = calloc(1, sizeof(struct testleak_env_t)); |
michael@0 | 1694 | env->base = event_base_new(); |
michael@0 | 1695 | env->dns_base = evdns_base_new(env->base, 0); |
michael@0 | 1696 | env->req = evdns_base_resolve_ipv4( |
michael@0 | 1697 | env->dns_base, "example.com", DNS_QUERY_NO_SEARCH, |
michael@0 | 1698 | generic_dns_callback, &env->r); |
michael@0 | 1699 | return env; |
michael@0 | 1700 | } |
michael@0 | 1701 | |
michael@0 | 1702 | static int |
michael@0 | 1703 | testleak_cleanup(const struct testcase_t *testcase, void *env_) |
michael@0 | 1704 | { |
michael@0 | 1705 | int ok = 0; |
michael@0 | 1706 | struct testleak_env_t *env = env_; |
michael@0 | 1707 | tt_assert(env); |
michael@0 | 1708 | #ifdef _EVENT_DISABLE_DEBUG_MODE |
michael@0 | 1709 | tt_int_op(allocated_chunks, ==, 0); |
michael@0 | 1710 | #else |
michael@0 | 1711 | /* FIXME: that's `1' because of event_debug_map_HT_GROW */ |
michael@0 | 1712 | tt_int_op(allocated_chunks, ==, 1); |
michael@0 | 1713 | #endif |
michael@0 | 1714 | ok = 1; |
michael@0 | 1715 | end: |
michael@0 | 1716 | if (env) { |
michael@0 | 1717 | if (env->dns_base) |
michael@0 | 1718 | evdns_base_free(env->dns_base, 0); |
michael@0 | 1719 | if (env->base) |
michael@0 | 1720 | event_base_free(env->base); |
michael@0 | 1721 | free(env); |
michael@0 | 1722 | } |
michael@0 | 1723 | return ok; |
michael@0 | 1724 | } |
michael@0 | 1725 | |
michael@0 | 1726 | static struct testcase_setup_t testleak_funcs = { |
michael@0 | 1727 | testleak_setup, testleak_cleanup |
michael@0 | 1728 | }; |
michael@0 | 1729 | |
michael@0 | 1730 | static void |
michael@0 | 1731 | test_dbg_leak_cancel(void *env_) |
michael@0 | 1732 | { |
michael@0 | 1733 | /* cancel, loop, free/dns, free/base */ |
michael@0 | 1734 | struct testleak_env_t *env = env_; |
michael@0 | 1735 | int send_err_shutdown = 1; |
michael@0 | 1736 | evdns_cancel_request(env->dns_base, env->req); |
michael@0 | 1737 | env->req = 0; |
michael@0 | 1738 | |
michael@0 | 1739 | /* `req` is freed in callback, that's why one loop is required. */ |
michael@0 | 1740 | event_base_loop(env->base, EVLOOP_NONBLOCK); |
michael@0 | 1741 | |
michael@0 | 1742 | /* send_err_shutdown means nothing as soon as our request is |
michael@0 | 1743 | * already canceled */ |
michael@0 | 1744 | evdns_base_free(env->dns_base, send_err_shutdown); |
michael@0 | 1745 | env->dns_base = 0; |
michael@0 | 1746 | event_base_free(env->base); |
michael@0 | 1747 | env->base = 0; |
michael@0 | 1748 | } |
michael@0 | 1749 | |
michael@0 | 1750 | static void |
michael@0 | 1751 | test_dbg_leak_shutdown(void *env_) |
michael@0 | 1752 | { |
michael@0 | 1753 | /* free/dns, loop, free/base */ |
michael@0 | 1754 | struct testleak_env_t *env = env_; |
michael@0 | 1755 | int send_err_shutdown = 1; |
michael@0 | 1756 | |
michael@0 | 1757 | /* `req` is freed both with `send_err_shutdown` and without it, |
michael@0 | 1758 | * the only difference is `evdns_callback` call */ |
michael@0 | 1759 | env->req = 0; |
michael@0 | 1760 | |
michael@0 | 1761 | evdns_base_free(env->dns_base, send_err_shutdown); |
michael@0 | 1762 | env->dns_base = 0; |
michael@0 | 1763 | |
michael@0 | 1764 | /* `req` is freed in callback, that's why one loop is required */ |
michael@0 | 1765 | event_base_loop(env->base, EVLOOP_NONBLOCK); |
michael@0 | 1766 | event_base_free(env->base); |
michael@0 | 1767 | env->base = 0; |
michael@0 | 1768 | } |
michael@0 | 1769 | #endif |
michael@0 | 1770 | |
michael@0 | 1771 | static void |
michael@0 | 1772 | test_getaddrinfo_async_cancel_stress(void *ptr) |
michael@0 | 1773 | { |
michael@0 | 1774 | struct event_base *base; |
michael@0 | 1775 | struct evdns_base *dns_base = NULL; |
michael@0 | 1776 | struct evdns_server_port *server = NULL; |
michael@0 | 1777 | evutil_socket_t fd = -1; |
michael@0 | 1778 | struct sockaddr_in sin; |
michael@0 | 1779 | struct sockaddr_storage ss; |
michael@0 | 1780 | ev_socklen_t slen; |
michael@0 | 1781 | int i; |
michael@0 | 1782 | |
michael@0 | 1783 | base = event_base_new(); |
michael@0 | 1784 | dns_base = evdns_base_new(base, 0); |
michael@0 | 1785 | |
michael@0 | 1786 | memset(&sin, 0, sizeof(sin)); |
michael@0 | 1787 | sin.sin_family = AF_INET; |
michael@0 | 1788 | sin.sin_port = 0; |
michael@0 | 1789 | sin.sin_addr.s_addr = htonl(0x7f000001); |
michael@0 | 1790 | if ((fd = socket(AF_INET, SOCK_DGRAM, 0)) < 0) { |
michael@0 | 1791 | tt_abort_perror("socket"); |
michael@0 | 1792 | } |
michael@0 | 1793 | evutil_make_socket_nonblocking(fd); |
michael@0 | 1794 | if (bind(fd, (struct sockaddr*)&sin, sizeof(sin))<0) { |
michael@0 | 1795 | tt_abort_perror("bind"); |
michael@0 | 1796 | } |
michael@0 | 1797 | server = evdns_add_server_port_with_base(base, fd, 0, gaic_server_cb, |
michael@0 | 1798 | base); |
michael@0 | 1799 | |
michael@0 | 1800 | memset(&ss, 0, sizeof(ss)); |
michael@0 | 1801 | slen = sizeof(ss); |
michael@0 | 1802 | if (getsockname(fd, (struct sockaddr*)&ss, &slen)<0) { |
michael@0 | 1803 | tt_abort_perror("getsockname"); |
michael@0 | 1804 | } |
michael@0 | 1805 | evdns_base_nameserver_sockaddr_add(dns_base, |
michael@0 | 1806 | (struct sockaddr*)&ss, slen, 0); |
michael@0 | 1807 | |
michael@0 | 1808 | for (i = 0; i < 1000; ++i) { |
michael@0 | 1809 | gaic_launch(base, dns_base); |
michael@0 | 1810 | } |
michael@0 | 1811 | |
michael@0 | 1812 | event_base_dispatch(base); |
michael@0 | 1813 | |
michael@0 | 1814 | end: |
michael@0 | 1815 | if (dns_base) |
michael@0 | 1816 | evdns_base_free(dns_base, 1); |
michael@0 | 1817 | if (server) |
michael@0 | 1818 | evdns_close_server_port(server); |
michael@0 | 1819 | if (fd >= 0) |
michael@0 | 1820 | evutil_closesocket(fd); |
michael@0 | 1821 | } |
michael@0 | 1822 | |
michael@0 | 1823 | |
michael@0 | 1824 | #define DNS_LEGACY(name, flags) \ |
michael@0 | 1825 | { #name, run_legacy_test_fn, flags|TT_LEGACY, &legacy_setup, \ |
michael@0 | 1826 | dns_##name } |
michael@0 | 1827 | |
michael@0 | 1828 | struct testcase_t dns_testcases[] = { |
michael@0 | 1829 | DNS_LEGACY(server, TT_FORK|TT_NEED_BASE), |
michael@0 | 1830 | DNS_LEGACY(gethostbyname, TT_FORK|TT_NEED_BASE|TT_NEED_DNS), |
michael@0 | 1831 | DNS_LEGACY(gethostbyname6, TT_FORK|TT_NEED_BASE|TT_NEED_DNS), |
michael@0 | 1832 | DNS_LEGACY(gethostbyaddr, TT_FORK|TT_NEED_BASE|TT_NEED_DNS), |
michael@0 | 1833 | { "resolve_reverse", dns_resolve_reverse, TT_FORK, NULL, NULL }, |
michael@0 | 1834 | { "search", dns_search_test, TT_FORK|TT_NEED_BASE, &basic_setup, NULL }, |
michael@0 | 1835 | { "search_cancel", dns_search_cancel_test, |
michael@0 | 1836 | TT_FORK|TT_NEED_BASE, &basic_setup, NULL }, |
michael@0 | 1837 | { "retry", dns_retry_test, TT_FORK|TT_NEED_BASE, &basic_setup, NULL }, |
michael@0 | 1838 | { "reissue", dns_reissue_test, TT_FORK|TT_NEED_BASE, &basic_setup, NULL }, |
michael@0 | 1839 | { "inflight", dns_inflight_test, TT_FORK|TT_NEED_BASE, &basic_setup, NULL }, |
michael@0 | 1840 | { "bufferevent_connect_hostname", test_bufferevent_connect_hostname, |
michael@0 | 1841 | TT_FORK|TT_NEED_BASE, &basic_setup, NULL }, |
michael@0 | 1842 | |
michael@0 | 1843 | { "getaddrinfo_async", test_getaddrinfo_async, |
michael@0 | 1844 | TT_FORK|TT_NEED_BASE, &basic_setup, (char*)"" }, |
michael@0 | 1845 | { "getaddrinfo_cancel_stress", test_getaddrinfo_async_cancel_stress, |
michael@0 | 1846 | TT_FORK, NULL, NULL }, |
michael@0 | 1847 | |
michael@0 | 1848 | #ifdef EVENT_SET_MEM_FUNCTIONS_IMPLEMENTED |
michael@0 | 1849 | { "leak_shutdown", test_dbg_leak_shutdown, TT_FORK, &testleak_funcs, NULL }, |
michael@0 | 1850 | { "leak_cancel", test_dbg_leak_cancel, TT_FORK, &testleak_funcs, NULL }, |
michael@0 | 1851 | #endif |
michael@0 | 1852 | |
michael@0 | 1853 | END_OF_TESTCASES |
michael@0 | 1854 | }; |
michael@0 | 1855 |