1275970Scy/* 2275970Scy * Copyright (c) 2003-2007 Niels Provos <provos@citi.umich.edu> 3275970Scy * Copyright (c) 2007-2012 Niels Provos and Nick Mathewson 4275970Scy * 5275970Scy * Redistribution and use in source and binary forms, with or without 6275970Scy * modification, are permitted provided that the following conditions 7275970Scy * are met: 8275970Scy * 1. Redistributions of source code must retain the above copyright 9275970Scy * notice, this list of conditions and the following disclaimer. 10275970Scy * 2. Redistributions in binary form must reproduce the above copyright 11275970Scy * notice, this list of conditions and the following disclaimer in the 12275970Scy * documentation and/or other materials provided with the distribution. 13275970Scy * 3. The name of the author may not be used to endorse or promote products 14275970Scy * derived from this software without specific prior written permission. 15275970Scy * 16275970Scy * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR 17275970Scy * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 18275970Scy * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 19275970Scy * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, 20275970Scy * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 21275970Scy * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 22275970Scy * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 23275970Scy * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 24275970Scy * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 25275970Scy * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 26275970Scy */ 27275970Scy#include "../util-internal.h" 28275970Scy 29275970Scy#ifdef _WIN32 30275970Scy#include <winsock2.h> 31275970Scy#include <windows.h> 32275970Scy#include <ws2tcpip.h> 33275970Scy#endif 34275970Scy 35275970Scy#include "event2/event-config.h" 36275970Scy 37275970Scy#include <sys/types.h> 38275970Scy#include <sys/stat.h> 39275970Scy#ifdef EVENT__HAVE_SYS_TIME_H 40275970Scy#include <sys/time.h> 41275970Scy#endif 42275970Scy#include <sys/queue.h> 43275970Scy#ifndef _WIN32 44275970Scy#include <sys/socket.h> 45275970Scy#include <signal.h> 46275970Scy#include <netinet/in.h> 47275970Scy#include <arpa/inet.h> 48275970Scy#include <unistd.h> 49275970Scy#endif 50275970Scy#ifdef EVENT__HAVE_NETINET_IN6_H 51275970Scy#include <netinet/in6.h> 52275970Scy#endif 53275970Scy#ifdef HAVE_NETDB_H 54275970Scy#include <netdb.h> 55275970Scy#endif 56275970Scy#include <fcntl.h> 57275970Scy#include <stdlib.h> 58275970Scy#include <stdio.h> 59275970Scy#include <string.h> 60275970Scy#include <errno.h> 61275970Scy 62275970Scy#include "event2/dns.h" 63275970Scy#include "event2/dns_compat.h" 64275970Scy#include "event2/dns_struct.h" 65275970Scy#include "event2/event.h" 66275970Scy#include "event2/event_compat.h" 67275970Scy#include "event2/event_struct.h" 68275970Scy#include "event2/util.h" 69275970Scy#include "event2/listener.h" 70275970Scy#include "event2/bufferevent.h" 71275970Scy#include "log-internal.h" 72275970Scy#include "regress.h" 73275970Scy#include "regress_testutils.h" 74275970Scy 75275970Scystatic int dns_ok = 0; 76275970Scystatic int dns_got_cancel = 0; 77275970Scystatic int dns_err = 0; 78275970Scy 79275970Scy 80275970Scystatic void 81275970Scydns_gethostbyname_cb(int result, char type, int count, int ttl, 82275970Scy void *addresses, void *arg) 83275970Scy{ 84275970Scy dns_ok = dns_err = 0; 85275970Scy 86275970Scy if (result == DNS_ERR_TIMEOUT) { 87275970Scy printf("[Timed out] "); 88275970Scy dns_err = result; 89275970Scy goto out; 90275970Scy } 91275970Scy 92275970Scy if (result != DNS_ERR_NONE) { 93275970Scy printf("[Error code %d] ", result); 94275970Scy goto out; 95275970Scy } 96275970Scy 97275970Scy TT_BLATHER(("type: %d, count: %d, ttl: %d: ", type, count, ttl)); 98275970Scy 99275970Scy switch (type) { 100275970Scy case DNS_IPv6_AAAA: { 101275970Scy#if defined(EVENT__HAVE_STRUCT_IN6_ADDR) && defined(EVENT__HAVE_INET_NTOP) && defined(INET6_ADDRSTRLEN) 102275970Scy struct in6_addr *in6_addrs = addresses; 103275970Scy char buf[INET6_ADDRSTRLEN+1]; 104275970Scy int i; 105275970Scy /* a resolution that's not valid does not help */ 106275970Scy if (ttl < 0) 107275970Scy goto out; 108275970Scy for (i = 0; i < count; ++i) { 109275970Scy const char *b = evutil_inet_ntop(AF_INET6, &in6_addrs[i], buf,sizeof(buf)); 110275970Scy if (b) 111275970Scy TT_BLATHER(("%s ", b)); 112275970Scy else 113275970Scy TT_BLATHER(("%s ", strerror(errno))); 114275970Scy } 115275970Scy#endif 116275970Scy break; 117275970Scy } 118275970Scy case DNS_IPv4_A: { 119275970Scy struct in_addr *in_addrs = addresses; 120275970Scy int i; 121275970Scy /* a resolution that's not valid does not help */ 122275970Scy if (ttl < 0) 123275970Scy goto out; 124275970Scy for (i = 0; i < count; ++i) 125275970Scy TT_BLATHER(("%s ", inet_ntoa(in_addrs[i]))); 126275970Scy break; 127275970Scy } 128275970Scy case DNS_PTR: 129275970Scy /* may get at most one PTR */ 130275970Scy if (count != 1) 131275970Scy goto out; 132275970Scy 133275970Scy TT_BLATHER(("%s ", *(char **)addresses)); 134275970Scy break; 135275970Scy default: 136275970Scy goto out; 137275970Scy } 138275970Scy 139275970Scy dns_ok = type; 140275970Scy 141275970Scyout: 142275970Scy if (arg == NULL) 143275970Scy event_loopexit(NULL); 144275970Scy else 145275970Scy event_base_loopexit((struct event_base *)arg, NULL); 146275970Scy} 147275970Scy 148275970Scystatic void 149275970Scydns_gethostbyname(void) 150275970Scy{ 151275970Scy dns_ok = 0; 152275970Scy evdns_resolve_ipv4("www.monkey.org", 0, dns_gethostbyname_cb, NULL); 153275970Scy event_dispatch(); 154275970Scy 155275970Scy tt_int_op(dns_ok, ==, DNS_IPv4_A); 156275970Scy test_ok = dns_ok; 157275970Scyend: 158275970Scy ; 159275970Scy} 160275970Scy 161275970Scystatic void 162275970Scydns_gethostbyname6(void) 163275970Scy{ 164275970Scy dns_ok = 0; 165275970Scy evdns_resolve_ipv6("www.ietf.org", 0, dns_gethostbyname_cb, NULL); 166275970Scy event_dispatch(); 167275970Scy 168275970Scy if (!dns_ok && dns_err == DNS_ERR_TIMEOUT) { 169275970Scy tt_skip(); 170275970Scy } 171275970Scy 172275970Scy tt_int_op(dns_ok, ==, DNS_IPv6_AAAA); 173275970Scy test_ok = 1; 174275970Scyend: 175275970Scy ; 176275970Scy} 177275970Scy 178275970Scystatic void 179275970Scydns_gethostbyaddr(void) 180275970Scy{ 181275970Scy struct in_addr in; 182275970Scy in.s_addr = htonl(0x7f000001ul); /* 127.0.0.1 */ 183275970Scy dns_ok = 0; 184275970Scy evdns_resolve_reverse(&in, 0, dns_gethostbyname_cb, NULL); 185275970Scy event_dispatch(); 186275970Scy 187275970Scy tt_int_op(dns_ok, ==, DNS_PTR); 188275970Scy test_ok = dns_ok; 189275970Scyend: 190275970Scy ; 191275970Scy} 192275970Scy 193275970Scystatic void 194275970Scydns_resolve_reverse(void *ptr) 195275970Scy{ 196275970Scy struct in_addr in; 197275970Scy struct event_base *base = event_base_new(); 198275970Scy struct evdns_base *dns = evdns_base_new(base, 1/* init name servers */); 199275970Scy struct evdns_request *req = NULL; 200275970Scy 201275970Scy tt_assert(base); 202275970Scy tt_assert(dns); 203275970Scy in.s_addr = htonl(0x7f000001ul); /* 127.0.0.1 */ 204275970Scy dns_ok = 0; 205275970Scy 206275970Scy req = evdns_base_resolve_reverse( 207275970Scy dns, &in, 0, dns_gethostbyname_cb, base); 208275970Scy tt_assert(req); 209275970Scy 210275970Scy event_base_dispatch(base); 211275970Scy 212275970Scy tt_int_op(dns_ok, ==, DNS_PTR); 213275970Scy 214275970Scyend: 215275970Scy if (dns) 216275970Scy evdns_base_free(dns, 0); 217275970Scy if (base) 218275970Scy event_base_free(base); 219275970Scy} 220275970Scy 221275970Scystatic int n_server_responses = 0; 222275970Scy 223275970Scystatic void 224275970Scydns_server_request_cb(struct evdns_server_request *req, void *data) 225275970Scy{ 226275970Scy int i, r; 227275970Scy const char TEST_ARPA[] = "11.11.168.192.in-addr.arpa"; 228275970Scy const char TEST_IN6[] = 229275970Scy "f.e.f.e." "0.0.0.0." "0.0.0.0." "1.1.1.1." 230275970Scy "a.a.a.a." "0.0.0.0." "0.0.0.0." "0.f.f.f.ip6.arpa"; 231275970Scy 232275970Scy for (i = 0; i < req->nquestions; ++i) { 233275970Scy const int qtype = req->questions[i]->type; 234275970Scy const int qclass = req->questions[i]->dns_question_class; 235275970Scy const char *qname = req->questions[i]->name; 236275970Scy 237275970Scy struct in_addr ans; 238275970Scy ans.s_addr = htonl(0xc0a80b0bUL); /* 192.168.11.11 */ 239275970Scy if (qtype == EVDNS_TYPE_A && 240275970Scy qclass == EVDNS_CLASS_INET && 241275970Scy !evutil_ascii_strcasecmp(qname, "zz.example.com")) { 242275970Scy r = evdns_server_request_add_a_reply(req, qname, 243275970Scy 1, &ans.s_addr, 12345); 244275970Scy if (r<0) 245275970Scy dns_ok = 0; 246275970Scy } else if (qtype == EVDNS_TYPE_AAAA && 247275970Scy qclass == EVDNS_CLASS_INET && 248275970Scy !evutil_ascii_strcasecmp(qname, "zz.example.com")) { 249275970Scy char addr6[17] = "abcdefghijklmnop"; 250275970Scy r = evdns_server_request_add_aaaa_reply(req, 251275970Scy qname, 1, addr6, 123); 252275970Scy if (r<0) 253275970Scy dns_ok = 0; 254275970Scy } else if (qtype == EVDNS_TYPE_PTR && 255275970Scy qclass == EVDNS_CLASS_INET && 256275970Scy !evutil_ascii_strcasecmp(qname, TEST_ARPA)) { 257275970Scy r = evdns_server_request_add_ptr_reply(req, NULL, 258275970Scy qname, "ZZ.EXAMPLE.COM", 54321); 259275970Scy if (r<0) 260275970Scy dns_ok = 0; 261275970Scy } else if (qtype == EVDNS_TYPE_PTR && 262275970Scy qclass == EVDNS_CLASS_INET && 263275970Scy !evutil_ascii_strcasecmp(qname, TEST_IN6)){ 264275970Scy r = evdns_server_request_add_ptr_reply(req, NULL, 265275970Scy qname, 266275970Scy "ZZ-INET6.EXAMPLE.COM", 54322); 267275970Scy if (r<0) 268275970Scy dns_ok = 0; 269275970Scy } else if (qtype == EVDNS_TYPE_A && 270275970Scy qclass == EVDNS_CLASS_INET && 271275970Scy !evutil_ascii_strcasecmp(qname, "drop.example.com")) { 272275970Scy if (evdns_server_request_drop(req)<0) 273275970Scy dns_ok = 0; 274275970Scy return; 275275970Scy } else { 276275970Scy printf("Unexpected question %d %d \"%s\" ", 277275970Scy qtype, qclass, qname); 278275970Scy dns_ok = 0; 279275970Scy } 280275970Scy } 281275970Scy r = evdns_server_request_respond(req, 0); 282275970Scy if (r<0) { 283275970Scy printf("Couldn't send reply. "); 284275970Scy dns_ok = 0; 285275970Scy } 286275970Scy} 287275970Scy 288275970Scystatic void 289275970Scydns_server_gethostbyname_cb(int result, char type, int count, int ttl, 290275970Scy void *addresses, void *arg) 291275970Scy{ 292275970Scy if (result == DNS_ERR_CANCEL) { 293275970Scy if (arg != (void*)(char*)90909) { 294275970Scy printf("Unexpected cancelation"); 295275970Scy dns_ok = 0; 296275970Scy } 297275970Scy dns_got_cancel = 1; 298275970Scy goto out; 299275970Scy } 300275970Scy if (result != DNS_ERR_NONE) { 301275970Scy printf("Unexpected result %d. ", result); 302275970Scy dns_ok = 0; 303275970Scy goto out; 304275970Scy } 305275970Scy if (count != 1) { 306275970Scy printf("Unexpected answer count %d. ", count); 307275970Scy dns_ok = 0; 308275970Scy goto out; 309275970Scy } 310275970Scy switch (type) { 311275970Scy case DNS_IPv4_A: { 312275970Scy struct in_addr *in_addrs = addresses; 313275970Scy if (in_addrs[0].s_addr != htonl(0xc0a80b0bUL) || ttl != 12345) { 314275970Scy printf("Bad IPv4 response \"%s\" %d. ", 315275970Scy inet_ntoa(in_addrs[0]), ttl); 316275970Scy dns_ok = 0; 317275970Scy goto out; 318275970Scy } 319275970Scy break; 320275970Scy } 321275970Scy case DNS_IPv6_AAAA: { 322275970Scy#if defined (EVENT__HAVE_STRUCT_IN6_ADDR) && defined(EVENT__HAVE_INET_NTOP) && defined(INET6_ADDRSTRLEN) 323275970Scy struct in6_addr *in6_addrs = addresses; 324275970Scy char buf[INET6_ADDRSTRLEN+1]; 325275970Scy if (memcmp(&in6_addrs[0].s6_addr, "abcdefghijklmnop", 16) 326275970Scy || ttl != 123) { 327275970Scy const char *b = evutil_inet_ntop(AF_INET6, &in6_addrs[0],buf,sizeof(buf)); 328275970Scy printf("Bad IPv6 response \"%s\" %d. ", b, ttl); 329275970Scy dns_ok = 0; 330275970Scy goto out; 331275970Scy } 332275970Scy#endif 333275970Scy break; 334275970Scy } 335275970Scy case DNS_PTR: { 336275970Scy char **addrs = addresses; 337275970Scy if (arg != (void*)6) { 338275970Scy if (strcmp(addrs[0], "ZZ.EXAMPLE.COM") || 339275970Scy ttl != 54321) { 340275970Scy printf("Bad PTR response \"%s\" %d. ", 341275970Scy addrs[0], ttl); 342275970Scy dns_ok = 0; 343275970Scy goto out; 344275970Scy } 345275970Scy } else { 346275970Scy if (strcmp(addrs[0], "ZZ-INET6.EXAMPLE.COM") || 347275970Scy ttl != 54322) { 348275970Scy printf("Bad ipv6 PTR response \"%s\" %d. ", 349275970Scy addrs[0], ttl); 350275970Scy dns_ok = 0; 351275970Scy goto out; 352275970Scy } 353275970Scy } 354275970Scy break; 355275970Scy } 356275970Scy default: 357275970Scy printf("Bad response type %d. ", type); 358275970Scy dns_ok = 0; 359275970Scy } 360275970Scy out: 361275970Scy if (++n_server_responses == 3) { 362275970Scy event_loopexit(NULL); 363275970Scy } 364275970Scy} 365275970Scy 366275970Scystatic void 367275970Scydns_server(void) 368275970Scy{ 369275970Scy evutil_socket_t sock=-1; 370275970Scy struct sockaddr_in my_addr; 371275970Scy struct sockaddr_storage ss; 372275970Scy ev_socklen_t slen; 373275970Scy struct evdns_server_port *port=NULL; 374275970Scy struct in_addr resolve_addr; 375275970Scy struct in6_addr resolve_addr6; 376275970Scy struct evdns_base *base=NULL; 377275970Scy struct evdns_request *req=NULL; 378275970Scy 379275970Scy dns_ok = 1; 380275970Scy 381275970Scy base = evdns_base_new(NULL, 0); 382275970Scy 383275970Scy /* Now configure a nameserver port. */ 384275970Scy sock = socket(AF_INET, SOCK_DGRAM, 0); 385275970Scy if (sock<0) { 386275970Scy tt_abort_perror("socket"); 387275970Scy } 388275970Scy 389275970Scy evutil_make_socket_nonblocking(sock); 390275970Scy 391275970Scy memset(&my_addr, 0, sizeof(my_addr)); 392275970Scy my_addr.sin_family = AF_INET; 393275970Scy my_addr.sin_port = 0; /* kernel picks */ 394275970Scy my_addr.sin_addr.s_addr = htonl(0x7f000001UL); 395275970Scy if (bind(sock, (struct sockaddr*)&my_addr, sizeof(my_addr)) < 0) { 396275970Scy tt_abort_perror("bind"); 397275970Scy } 398275970Scy slen = sizeof(ss); 399275970Scy if (getsockname(sock, (struct sockaddr*)&ss, &slen) < 0) { 400275970Scy tt_abort_perror("getsockname"); 401275970Scy } 402275970Scy 403275970Scy port = evdns_add_server_port(sock, 0, dns_server_request_cb, NULL); 404275970Scy 405275970Scy /* Add ourself as the only nameserver, and make sure we really are 406275970Scy * the only nameserver. */ 407275970Scy evdns_base_nameserver_sockaddr_add(base, (struct sockaddr*)&ss, slen, 0); 408275970Scy tt_int_op(evdns_base_count_nameservers(base), ==, 1); 409290000Sglebius { 410290000Sglebius struct sockaddr_storage ss2; 411290000Sglebius int slen2; 412275970Scy 413290000Sglebius memset(&ss2, 0, sizeof(ss2)); 414290000Sglebius 415290000Sglebius slen2 = evdns_base_get_nameserver_addr(base, 0, (struct sockaddr *)&ss2, 3); 416290000Sglebius tt_int_op(slen2, ==, slen); 417290000Sglebius tt_int_op(ss2.ss_family, ==, 0); 418290000Sglebius slen2 = evdns_base_get_nameserver_addr(base, 0, (struct sockaddr *)&ss2, sizeof(ss2)); 419290000Sglebius tt_int_op(slen2, ==, slen); 420290000Sglebius tt_mem_op(&ss2, ==, &ss, slen); 421290000Sglebius 422290000Sglebius slen2 = evdns_base_get_nameserver_addr(base, 1, (struct sockaddr *)&ss2, sizeof(ss2)); 423290000Sglebius tt_int_op(-1, ==, slen2); 424290000Sglebius } 425290000Sglebius 426275970Scy /* Send some queries. */ 427275970Scy evdns_base_resolve_ipv4(base, "zz.example.com", DNS_QUERY_NO_SEARCH, 428275970Scy dns_server_gethostbyname_cb, NULL); 429275970Scy evdns_base_resolve_ipv6(base, "zz.example.com", DNS_QUERY_NO_SEARCH, 430275970Scy dns_server_gethostbyname_cb, NULL); 431275970Scy resolve_addr.s_addr = htonl(0xc0a80b0bUL); /* 192.168.11.11 */ 432275970Scy evdns_base_resolve_reverse(base, &resolve_addr, 0, 433275970Scy dns_server_gethostbyname_cb, NULL); 434275970Scy memcpy(resolve_addr6.s6_addr, 435275970Scy "\xff\xf0\x00\x00\x00\x00\xaa\xaa" 436275970Scy "\x11\x11\x00\x00\x00\x00\xef\xef", 16); 437275970Scy evdns_base_resolve_reverse_ipv6(base, &resolve_addr6, 0, 438275970Scy dns_server_gethostbyname_cb, (void*)6); 439275970Scy 440275970Scy req = evdns_base_resolve_ipv4(base, 441275970Scy "drop.example.com", DNS_QUERY_NO_SEARCH, 442275970Scy dns_server_gethostbyname_cb, (void*)(char*)90909); 443275970Scy 444275970Scy evdns_cancel_request(base, req); 445275970Scy 446275970Scy event_dispatch(); 447275970Scy 448275970Scy tt_assert(dns_got_cancel); 449275970Scy test_ok = dns_ok; 450275970Scy 451275970Scyend: 452275970Scy if (port) 453275970Scy evdns_close_server_port(port); 454275970Scy if (sock >= 0) 455275970Scy evutil_closesocket(sock); 456275970Scy if (base) 457275970Scy evdns_base_free(base, 0); 458275970Scy} 459275970Scy 460275970Scystatic int n_replies_left; 461275970Scystatic struct event_base *exit_base; 462290000Sglebiusstatic struct evdns_server_port *exit_port; 463275970Scy 464275970Scystruct generic_dns_callback_result { 465275970Scy int result; 466275970Scy char type; 467275970Scy int count; 468275970Scy int ttl; 469275970Scy size_t addrs_len; 470275970Scy void *addrs; 471275970Scy char addrs_buf[256]; 472275970Scy}; 473275970Scy 474275970Scystatic void 475275970Scygeneric_dns_callback(int result, char type, int count, int ttl, void *addresses, 476275970Scy void *arg) 477275970Scy{ 478275970Scy size_t len; 479275970Scy struct generic_dns_callback_result *res = arg; 480275970Scy res->result = result; 481275970Scy res->type = type; 482275970Scy res->count = count; 483275970Scy res->ttl = ttl; 484275970Scy 485275970Scy if (type == DNS_IPv4_A) 486275970Scy len = count * 4; 487275970Scy else if (type == DNS_IPv6_AAAA) 488275970Scy len = count * 16; 489275970Scy else if (type == DNS_PTR) 490275970Scy len = strlen(addresses)+1; 491275970Scy else { 492275970Scy res->addrs_len = len = 0; 493275970Scy res->addrs = NULL; 494275970Scy } 495275970Scy if (len) { 496275970Scy res->addrs_len = len; 497275970Scy if (len > 256) 498275970Scy len = 256; 499275970Scy memcpy(res->addrs_buf, addresses, len); 500275970Scy res->addrs = res->addrs_buf; 501275970Scy } 502275970Scy 503290000Sglebius --n_replies_left; 504290000Sglebius if (n_replies_left == 0) { 505290000Sglebius if (exit_port) { 506290000Sglebius evdns_close_server_port(exit_port); 507290000Sglebius exit_port = NULL; 508290000Sglebius } else 509290000Sglebius event_base_loopexit(exit_base, NULL); 510290000Sglebius } 511275970Scy} 512275970Scy 513275970Scystatic struct regress_dns_server_table search_table[] = { 514275970Scy { "host.a.example.com", "err", "3", 0 }, 515275970Scy { "host.b.example.com", "err", "3", 0 }, 516275970Scy { "host.c.example.com", "A", "11.22.33.44", 0 }, 517275970Scy { "host2.a.example.com", "err", "3", 0 }, 518275970Scy { "host2.b.example.com", "A", "200.100.0.100", 0 }, 519275970Scy { "host2.c.example.com", "err", "3", 0 }, 520275970Scy { "hostn.a.example.com", "errsoa", "0", 0 }, 521275970Scy { "hostn.b.example.com", "errsoa", "3", 0 }, 522275970Scy { "hostn.c.example.com", "err", "0", 0 }, 523275970Scy 524275970Scy { "host", "err", "3", 0 }, 525275970Scy { "host2", "err", "3", 0 }, 526275970Scy { "*", "err", "3", 0 }, 527275970Scy { NULL, NULL, NULL, 0 } 528275970Scy}; 529275970Scy 530275970Scystatic void 531275970Scydns_search_test(void *arg) 532275970Scy{ 533275970Scy struct basic_test_data *data = arg; 534275970Scy struct event_base *base = data->base; 535275970Scy struct evdns_base *dns = NULL; 536275970Scy ev_uint16_t portnum = 0; 537275970Scy char buf[64]; 538275970Scy 539275970Scy struct generic_dns_callback_result r[8]; 540275970Scy 541275970Scy tt_assert(regress_dnsserver(base, &portnum, search_table)); 542275970Scy evutil_snprintf(buf, sizeof(buf), "127.0.0.1:%d", (int)portnum); 543275970Scy 544275970Scy dns = evdns_base_new(base, 0); 545275970Scy tt_assert(!evdns_base_nameserver_ip_add(dns, buf)); 546275970Scy 547275970Scy evdns_base_search_add(dns, "a.example.com"); 548275970Scy evdns_base_search_add(dns, "b.example.com"); 549275970Scy evdns_base_search_add(dns, "c.example.com"); 550275970Scy 551275970Scy n_replies_left = sizeof(r)/sizeof(r[0]); 552275970Scy exit_base = base; 553275970Scy 554275970Scy evdns_base_resolve_ipv4(dns, "host", 0, generic_dns_callback, &r[0]); 555275970Scy evdns_base_resolve_ipv4(dns, "host2", 0, generic_dns_callback, &r[1]); 556275970Scy evdns_base_resolve_ipv4(dns, "host", DNS_NO_SEARCH, generic_dns_callback, &r[2]); 557275970Scy evdns_base_resolve_ipv4(dns, "host2", DNS_NO_SEARCH, generic_dns_callback, &r[3]); 558275970Scy evdns_base_resolve_ipv4(dns, "host3", 0, generic_dns_callback, &r[4]); 559275970Scy evdns_base_resolve_ipv4(dns, "hostn.a.example.com", DNS_NO_SEARCH, generic_dns_callback, &r[5]); 560275970Scy evdns_base_resolve_ipv4(dns, "hostn.b.example.com", DNS_NO_SEARCH, generic_dns_callback, &r[6]); 561275970Scy evdns_base_resolve_ipv4(dns, "hostn.c.example.com", DNS_NO_SEARCH, generic_dns_callback, &r[7]); 562275970Scy 563275970Scy event_base_dispatch(base); 564275970Scy 565275970Scy tt_int_op(r[0].type, ==, DNS_IPv4_A); 566275970Scy tt_int_op(r[0].count, ==, 1); 567275970Scy tt_int_op(((ev_uint32_t*)r[0].addrs)[0], ==, htonl(0x0b16212c)); 568275970Scy tt_int_op(r[1].type, ==, DNS_IPv4_A); 569275970Scy tt_int_op(r[1].count, ==, 1); 570275970Scy tt_int_op(((ev_uint32_t*)r[1].addrs)[0], ==, htonl(0xc8640064)); 571275970Scy tt_int_op(r[2].result, ==, DNS_ERR_NOTEXIST); 572275970Scy tt_int_op(r[3].result, ==, DNS_ERR_NOTEXIST); 573275970Scy tt_int_op(r[4].result, ==, DNS_ERR_NOTEXIST); 574275970Scy tt_int_op(r[5].result, ==, DNS_ERR_NODATA); 575275970Scy tt_int_op(r[5].ttl, ==, 42); 576275970Scy tt_int_op(r[6].result, ==, DNS_ERR_NOTEXIST); 577275970Scy tt_int_op(r[6].ttl, ==, 42); 578275970Scy tt_int_op(r[7].result, ==, DNS_ERR_NODATA); 579275970Scy tt_int_op(r[7].ttl, ==, 0); 580275970Scy 581275970Scyend: 582275970Scy if (dns) 583275970Scy evdns_base_free(dns, 0); 584275970Scy 585275970Scy regress_clean_dnsserver(); 586275970Scy} 587275970Scy 588275970Scystatic int request_count = 0; 589275970Scystatic struct evdns_request *current_req = NULL; 590275970Scy 591275970Scystatic void 592275970Scysearch_cancel_server_cb(struct evdns_server_request *req, void *data) 593275970Scy{ 594275970Scy const char *question; 595275970Scy 596275970Scy if (req->nquestions != 1) 597275970Scy TT_DIE(("Only handling one question at a time; got %d", 598275970Scy req->nquestions)); 599275970Scy 600275970Scy question = req->questions[0]->name; 601275970Scy 602275970Scy TT_BLATHER(("got question, %s", question)); 603275970Scy 604275970Scy tt_assert(request_count > 0); 605275970Scy tt_assert(!evdns_server_request_respond(req, 3)); 606275970Scy 607275970Scy if (!--request_count) 608275970Scy evdns_cancel_request(NULL, current_req); 609275970Scy 610275970Scyend: 611275970Scy ; 612275970Scy} 613275970Scy 614275970Scystatic void 615275970Scydns_search_cancel_test(void *arg) 616275970Scy{ 617275970Scy struct basic_test_data *data = arg; 618275970Scy struct event_base *base = data->base; 619275970Scy struct evdns_base *dns = NULL; 620275970Scy struct evdns_server_port *port = NULL; 621275970Scy ev_uint16_t portnum = 0; 622275970Scy struct generic_dns_callback_result r1; 623275970Scy char buf[64]; 624275970Scy 625275970Scy port = regress_get_dnsserver(base, &portnum, NULL, 626275970Scy search_cancel_server_cb, NULL); 627275970Scy tt_assert(port); 628275970Scy evutil_snprintf(buf, sizeof(buf), "127.0.0.1:%d", (int)portnum); 629275970Scy 630275970Scy dns = evdns_base_new(base, 0); 631275970Scy tt_assert(!evdns_base_nameserver_ip_add(dns, buf)); 632275970Scy 633275970Scy evdns_base_search_add(dns, "a.example.com"); 634275970Scy evdns_base_search_add(dns, "b.example.com"); 635275970Scy evdns_base_search_add(dns, "c.example.com"); 636275970Scy evdns_base_search_add(dns, "d.example.com"); 637275970Scy 638275970Scy exit_base = base; 639275970Scy request_count = 3; 640275970Scy n_replies_left = 1; 641275970Scy 642275970Scy current_req = evdns_base_resolve_ipv4(dns, "host", 0, 643275970Scy generic_dns_callback, &r1); 644275970Scy event_base_dispatch(base); 645275970Scy 646275970Scy tt_int_op(r1.result, ==, DNS_ERR_CANCEL); 647275970Scy 648275970Scyend: 649275970Scy if (port) 650275970Scy evdns_close_server_port(port); 651275970Scy if (dns) 652275970Scy evdns_base_free(dns, 0); 653275970Scy} 654275970Scy 655275970Scystatic void 656275970Scyfail_server_cb(struct evdns_server_request *req, void *data) 657275970Scy{ 658275970Scy const char *question; 659275970Scy int *count = data; 660275970Scy struct in_addr in; 661275970Scy 662275970Scy /* Drop the first N requests that we get. */ 663275970Scy if (*count > 0) { 664275970Scy --*count; 665275970Scy tt_want(! evdns_server_request_drop(req)); 666275970Scy return; 667275970Scy } 668275970Scy 669275970Scy if (req->nquestions != 1) 670275970Scy TT_DIE(("Only handling one question at a time; got %d", 671275970Scy req->nquestions)); 672275970Scy 673275970Scy question = req->questions[0]->name; 674275970Scy 675275970Scy if (!evutil_ascii_strcasecmp(question, "google.com")) { 676275970Scy /* Detect a probe, and get out of the loop. */ 677275970Scy event_base_loopexit(exit_base, NULL); 678275970Scy } 679275970Scy 680275970Scy tt_assert(evutil_inet_pton(AF_INET, "16.32.64.128", &in)); 681275970Scy evdns_server_request_add_a_reply(req, question, 1, &in.s_addr, 682275970Scy 100); 683275970Scy tt_assert(! evdns_server_request_respond(req, 0)) 684275970Scy return; 685275970Scyend: 686275970Scy tt_want(! evdns_server_request_drop(req)); 687275970Scy} 688275970Scy 689275970Scystatic void 690290000Sglebiusdns_retry_test_impl(void *arg, int flags) 691275970Scy{ 692275970Scy struct basic_test_data *data = arg; 693275970Scy struct event_base *base = data->base; 694275970Scy struct evdns_server_port *port = NULL; 695275970Scy struct evdns_base *dns = NULL; 696275970Scy int drop_count = 2; 697275970Scy ev_uint16_t portnum = 0; 698275970Scy char buf[64]; 699275970Scy 700275970Scy struct generic_dns_callback_result r1; 701275970Scy 702275970Scy port = regress_get_dnsserver(base, &portnum, NULL, 703275970Scy fail_server_cb, &drop_count); 704275970Scy tt_assert(port); 705275970Scy evutil_snprintf(buf, sizeof(buf), "127.0.0.1:%d", (int)portnum); 706275970Scy 707290000Sglebius dns = evdns_base_new(base, flags); 708275970Scy tt_assert(!evdns_base_nameserver_ip_add(dns, buf)); 709275970Scy tt_assert(! evdns_base_set_option(dns, "timeout", "0.2")); 710275970Scy tt_assert(! evdns_base_set_option(dns, "max-timeouts:", "10")); 711275970Scy tt_assert(! evdns_base_set_option(dns, "initial-probe-timeout", "0.1")); 712275970Scy 713275970Scy evdns_base_resolve_ipv4(dns, "host.example.com", 0, 714275970Scy generic_dns_callback, &r1); 715275970Scy 716275970Scy n_replies_left = 1; 717275970Scy exit_base = base; 718275970Scy 719275970Scy event_base_dispatch(base); 720275970Scy 721275970Scy tt_int_op(drop_count, ==, 0); 722275970Scy 723275970Scy tt_int_op(r1.type, ==, DNS_IPv4_A); 724275970Scy tt_int_op(r1.count, ==, 1); 725275970Scy tt_int_op(((ev_uint32_t*)r1.addrs)[0], ==, htonl(0x10204080)); 726275970Scy 727275970Scy /* Now try again, but this time have the server get treated as 728275970Scy * failed, so we can send it a test probe. */ 729275970Scy drop_count = 4; 730275970Scy tt_assert(! evdns_base_set_option(dns, "max-timeouts:", "2")); 731275970Scy tt_assert(! evdns_base_set_option(dns, "attempts:", "3")); 732275970Scy memset(&r1, 0, sizeof(r1)); 733275970Scy 734275970Scy evdns_base_resolve_ipv4(dns, "host.example.com", 0, 735275970Scy generic_dns_callback, &r1); 736275970Scy 737275970Scy n_replies_left = 2; 738275970Scy 739275970Scy /* This will run until it answers the "google.com" probe request. */ 740275970Scy event_base_dispatch(base); 741275970Scy 742275970Scy /* We'll treat the server as failed here. */ 743275970Scy tt_int_op(r1.result, ==, DNS_ERR_TIMEOUT); 744275970Scy 745275970Scy /* It should work this time. */ 746275970Scy tt_int_op(drop_count, ==, 0); 747275970Scy evdns_base_resolve_ipv4(dns, "host.example.com", 0, 748275970Scy generic_dns_callback, &r1); 749275970Scy 750275970Scy event_base_dispatch(base); 751275970Scy tt_int_op(r1.result, ==, DNS_ERR_NONE); 752275970Scy tt_int_op(r1.type, ==, DNS_IPv4_A); 753275970Scy tt_int_op(r1.count, ==, 1); 754275970Scy tt_int_op(((ev_uint32_t*)r1.addrs)[0], ==, htonl(0x10204080)); 755275970Scy 756275970Scyend: 757275970Scy if (dns) 758275970Scy evdns_base_free(dns, 0); 759275970Scy if (port) 760275970Scy evdns_close_server_port(port); 761275970Scy} 762290000Sglebiusstatic void 763290000Sglebiusdns_retry_test(void *arg) 764290000Sglebius{ 765290000Sglebius dns_retry_test_impl(arg, 0); 766290000Sglebius} 767290000Sglebiusstatic void 768290000Sglebiusdns_retry_disable_when_inactive_test(void *arg) 769290000Sglebius{ 770290000Sglebius dns_retry_test_impl(arg, EVDNS_BASE_DISABLE_WHEN_INACTIVE); 771290000Sglebius} 772275970Scy 773275970Scystatic struct regress_dns_server_table internal_error_table[] = { 774275970Scy /* Error 4 (NOTIMPL) makes us reissue the request to another server 775275970Scy if we can. 776275970Scy 777275970Scy XXXX we should reissue under a much wider set of circumstances! 778275970Scy */ 779275970Scy { "foof.example.com", "err", "4", 0 }, 780275970Scy { NULL, NULL, NULL, 0 } 781275970Scy}; 782275970Scy 783275970Scystatic struct regress_dns_server_table reissue_table[] = { 784275970Scy { "foof.example.com", "A", "240.15.240.15", 0 }, 785275970Scy { NULL, NULL, NULL, 0 } 786275970Scy}; 787275970Scy 788275970Scystatic void 789290000Sglebiusdns_reissue_test_impl(void *arg, int flags) 790275970Scy{ 791275970Scy struct basic_test_data *data = arg; 792275970Scy struct event_base *base = data->base; 793275970Scy struct evdns_server_port *port1 = NULL, *port2 = NULL; 794275970Scy struct evdns_base *dns = NULL; 795275970Scy struct generic_dns_callback_result r1; 796275970Scy ev_uint16_t portnum1 = 0, portnum2=0; 797275970Scy char buf1[64], buf2[64]; 798275970Scy 799275970Scy port1 = regress_get_dnsserver(base, &portnum1, NULL, 800275970Scy regress_dns_server_cb, internal_error_table); 801275970Scy tt_assert(port1); 802275970Scy port2 = regress_get_dnsserver(base, &portnum2, NULL, 803275970Scy regress_dns_server_cb, reissue_table); 804275970Scy tt_assert(port2); 805275970Scy evutil_snprintf(buf1, sizeof(buf1), "127.0.0.1:%d", (int)portnum1); 806275970Scy evutil_snprintf(buf2, sizeof(buf2), "127.0.0.1:%d", (int)portnum2); 807275970Scy 808290000Sglebius dns = evdns_base_new(base, flags); 809275970Scy tt_assert(!evdns_base_nameserver_ip_add(dns, buf1)); 810275970Scy tt_assert(! evdns_base_set_option(dns, "timeout:", "0.3")); 811275970Scy tt_assert(! evdns_base_set_option(dns, "max-timeouts:", "2")); 812275970Scy tt_assert(! evdns_base_set_option(dns, "attempts:", "5")); 813275970Scy 814275970Scy memset(&r1, 0, sizeof(r1)); 815275970Scy evdns_base_resolve_ipv4(dns, "foof.example.com", 0, 816275970Scy generic_dns_callback, &r1); 817275970Scy 818275970Scy /* Add this after, so that we are sure to get a reissue. */ 819275970Scy tt_assert(!evdns_base_nameserver_ip_add(dns, buf2)); 820275970Scy 821275970Scy n_replies_left = 1; 822275970Scy exit_base = base; 823275970Scy 824275970Scy event_base_dispatch(base); 825275970Scy tt_int_op(r1.result, ==, DNS_ERR_NONE); 826275970Scy tt_int_op(r1.type, ==, DNS_IPv4_A); 827275970Scy tt_int_op(r1.count, ==, 1); 828275970Scy tt_int_op(((ev_uint32_t*)r1.addrs)[0], ==, htonl(0xf00ff00f)); 829275970Scy 830275970Scy /* Make sure we dropped at least once. */ 831275970Scy tt_int_op(internal_error_table[0].seen, >, 0); 832275970Scy 833275970Scyend: 834275970Scy if (dns) 835275970Scy evdns_base_free(dns, 0); 836275970Scy if (port1) 837275970Scy evdns_close_server_port(port1); 838275970Scy if (port2) 839275970Scy evdns_close_server_port(port2); 840275970Scy} 841290000Sglebiusstatic void 842290000Sglebiusdns_reissue_test(void *arg) 843290000Sglebius{ 844290000Sglebius dns_reissue_test_impl(arg, 0); 845290000Sglebius} 846290000Sglebiusstatic void 847290000Sglebiusdns_reissue_disable_when_inactive_test(void *arg) 848290000Sglebius{ 849290000Sglebius dns_reissue_test_impl(arg, EVDNS_BASE_DISABLE_WHEN_INACTIVE); 850290000Sglebius} 851275970Scy 852275970Scy#if 0 853275970Scystatic void 854275970Scydumb_bytes_fn(char *p, size_t n) 855275970Scy{ 856275970Scy unsigned i; 857275970Scy /* This gets us 6 bits of entropy per transaction ID, which means we 858275970Scy * will have probably have collisions and need to pick again. */ 859275970Scy for (i=0;i<n;++i) 860275970Scy p[i] = (char)(rand() & 7); 861275970Scy} 862275970Scy#endif 863275970Scy 864275970Scystatic void 865290000Sglebiusdns_inflight_test_impl(void *arg, int flags) 866275970Scy{ 867275970Scy struct basic_test_data *data = arg; 868275970Scy struct event_base *base = data->base; 869275970Scy struct evdns_base *dns = NULL; 870290000Sglebius struct evdns_server_port *dns_port = NULL; 871275970Scy ev_uint16_t portnum = 0; 872275970Scy char buf[64]; 873290000Sglebius int disable_when_inactive = flags & EVDNS_BASE_DISABLE_WHEN_INACTIVE; 874275970Scy 875275970Scy struct generic_dns_callback_result r[20]; 876275970Scy int i; 877275970Scy 878290000Sglebius dns_port = regress_get_dnsserver(base, &portnum, NULL, 879290000Sglebius regress_dns_server_cb, reissue_table); 880290000Sglebius tt_assert(dns_port); 881290000Sglebius if (disable_when_inactive) { 882290000Sglebius exit_port = dns_port; 883290000Sglebius } 884290000Sglebius 885275970Scy evutil_snprintf(buf, sizeof(buf), "127.0.0.1:%d", (int)portnum); 886275970Scy 887290000Sglebius dns = evdns_base_new(base, flags); 888275970Scy tt_assert(!evdns_base_nameserver_ip_add(dns, buf)); 889275970Scy tt_assert(! evdns_base_set_option(dns, "max-inflight:", "3")); 890275970Scy tt_assert(! evdns_base_set_option(dns, "randomize-case:", "0")); 891275970Scy 892275970Scy for (i=0;i<20;++i) 893275970Scy evdns_base_resolve_ipv4(dns, "foof.example.com", 0, generic_dns_callback, &r[i]); 894275970Scy 895275970Scy n_replies_left = 20; 896275970Scy exit_base = base; 897275970Scy 898275970Scy event_base_dispatch(base); 899275970Scy 900275970Scy for (i=0;i<20;++i) { 901275970Scy tt_int_op(r[i].type, ==, DNS_IPv4_A); 902275970Scy tt_int_op(r[i].count, ==, 1); 903275970Scy tt_int_op(((ev_uint32_t*)r[i].addrs)[0], ==, htonl(0xf00ff00f)); 904275970Scy } 905275970Scy 906275970Scyend: 907275970Scy if (dns) 908275970Scy evdns_base_free(dns, 0); 909290000Sglebius if (exit_port) { 910290000Sglebius evdns_close_server_port(exit_port); 911290000Sglebius exit_port = NULL; 912290000Sglebius } else if (! disable_when_inactive) { 913290000Sglebius evdns_close_server_port(dns_port); 914290000Sglebius } 915290000Sglebius} 916290000Sglebius 917290000Sglebiusstatic void 918290000Sglebiusdns_inflight_test(void *arg) 919290000Sglebius{ 920290000Sglebius dns_inflight_test_impl(arg, 0); 921290000Sglebius} 922290000Sglebius 923290000Sglebiusstatic void 924290000Sglebiusdns_disable_when_inactive_test(void *arg) 925290000Sglebius{ 926290000Sglebius dns_inflight_test_impl(arg, EVDNS_BASE_DISABLE_WHEN_INACTIVE); 927290000Sglebius} 928290000Sglebius 929290000Sglebiusstatic void 930290000Sglebiusdns_disable_when_inactive_no_ns_test(void *arg) 931290000Sglebius{ 932290000Sglebius struct basic_test_data *data = arg; 933290000Sglebius struct event_base *base = data->base, *inactive_base; 934290000Sglebius struct evdns_base *dns = NULL; 935290000Sglebius ev_uint16_t portnum = 0; 936290000Sglebius char buf[64]; 937290000Sglebius struct generic_dns_callback_result r; 938290000Sglebius 939290000Sglebius inactive_base = event_base_new(); 940290000Sglebius tt_assert(inactive_base); 941290000Sglebius 942290000Sglebius /** Create dns server with inactive base, to avoid replying to clients */ 943290000Sglebius tt_assert(regress_dnsserver(inactive_base, &portnum, search_table)); 944290000Sglebius evutil_snprintf(buf, sizeof(buf), "127.0.0.1:%d", (int)portnum); 945290000Sglebius 946290000Sglebius dns = evdns_base_new(base, EVDNS_BASE_DISABLE_WHEN_INACTIVE); 947290000Sglebius tt_assert(!evdns_base_nameserver_ip_add(dns, buf)); 948290000Sglebius tt_assert(! evdns_base_set_option(dns, "timeout:", "0.1")); 949290000Sglebius 950290000Sglebius evdns_base_resolve_ipv4(dns, "foof.example.com", 0, generic_dns_callback, &r); 951290000Sglebius n_replies_left = 1; 952290000Sglebius exit_base = base; 953290000Sglebius 954290000Sglebius event_base_dispatch(base); 955290000Sglebius 956290000Sglebius tt_int_op(n_replies_left, ==, 0); 957290000Sglebius 958290000Sglebius tt_int_op(r.result, ==, DNS_ERR_TIMEOUT); 959290000Sglebius tt_int_op(r.count, ==, 0); 960290000Sglebius tt_ptr_op(r.addrs, ==, NULL); 961290000Sglebius 962290000Sglebiusend: 963290000Sglebius if (dns) 964290000Sglebius evdns_base_free(dns, 0); 965275970Scy regress_clean_dnsserver(); 966290000Sglebius if (inactive_base) 967290000Sglebius event_base_free(inactive_base); 968275970Scy} 969275970Scy 970275970Scy/* === Test for bufferevent_socket_connect_hostname */ 971275970Scy 972275970Scystatic int total_connected_or_failed = 0; 973275970Scystatic int total_n_accepted = 0; 974275970Scystatic struct event_base *be_connect_hostname_base = NULL; 975275970Scy 976275970Scy/* Implements a DNS server for the connect_hostname test and the 977275970Scy * getaddrinfo_async test */ 978275970Scystatic void 979275970Scybe_getaddrinfo_server_cb(struct evdns_server_request *req, void *data) 980275970Scy{ 981275970Scy int i; 982275970Scy int *n_got_p=data; 983275970Scy int added_any=0; 984275970Scy ++*n_got_p; 985275970Scy 986275970Scy for (i=0;i<req->nquestions;++i) { 987275970Scy const int qtype = req->questions[i]->type; 988275970Scy const int qclass = req->questions[i]->dns_question_class; 989275970Scy const char *qname = req->questions[i]->name; 990275970Scy struct in_addr ans; 991275970Scy struct in6_addr ans6; 992275970Scy memset(&ans6, 0, sizeof(ans6)); 993275970Scy 994275970Scy TT_BLATHER(("Got question about %s, type=%d", qname, qtype)); 995275970Scy 996275970Scy if (qtype == EVDNS_TYPE_A && 997275970Scy qclass == EVDNS_CLASS_INET && 998275970Scy !evutil_ascii_strcasecmp(qname, "nobodaddy.example.com")) { 999275970Scy ans.s_addr = htonl(0x7f000001); 1000275970Scy evdns_server_request_add_a_reply(req, qname, 1001275970Scy 1, &ans.s_addr, 2000); 1002275970Scy added_any = 1; 1003275970Scy } else if (!evutil_ascii_strcasecmp(qname, 1004275970Scy "nosuchplace.example.com")) { 1005275970Scy /* ok, just say notfound. */ 1006275970Scy } else if (!evutil_ascii_strcasecmp(qname, 1007275970Scy "both.example.com")) { 1008275970Scy if (qtype == EVDNS_TYPE_A) { 1009275970Scy ans.s_addr = htonl(0x50502020); 1010275970Scy evdns_server_request_add_a_reply(req, qname, 1011275970Scy 1, &ans.s_addr, 2000); 1012275970Scy added_any = 1; 1013275970Scy } else if (qtype == EVDNS_TYPE_AAAA) { 1014275970Scy ans6.s6_addr[0] = 0x80; 1015275970Scy ans6.s6_addr[1] = 0xff; 1016275970Scy ans6.s6_addr[14] = 0xbb; 1017275970Scy ans6.s6_addr[15] = 0xbb; 1018275970Scy evdns_server_request_add_aaaa_reply(req, qname, 1019275970Scy 1, &ans6.s6_addr, 2000); 1020275970Scy added_any = 1; 1021275970Scy } 1022275970Scy evdns_server_request_add_cname_reply(req, qname, 1023275970Scy "both-canonical.example.com", 1000); 1024275970Scy } else if (!evutil_ascii_strcasecmp(qname, 1025275970Scy "v4only.example.com") || 1026275970Scy !evutil_ascii_strcasecmp(qname, "v4assert.example.com")) { 1027275970Scy if (qtype == EVDNS_TYPE_A) { 1028275970Scy ans.s_addr = htonl(0x12345678); 1029275970Scy evdns_server_request_add_a_reply(req, qname, 1030275970Scy 1, &ans.s_addr, 2000); 1031275970Scy added_any = 1; 1032275970Scy } else if (!evutil_ascii_strcasecmp(qname, 1033275970Scy "v4assert.example.com")) { 1034275970Scy TT_FAIL(("Got an AAAA request for v4assert")); 1035275970Scy } 1036275970Scy } else if (!evutil_ascii_strcasecmp(qname, 1037275970Scy "v6only.example.com") || 1038275970Scy !evutil_ascii_strcasecmp(qname, "v6assert.example.com")) { 1039275970Scy if (qtype == EVDNS_TYPE_AAAA) { 1040275970Scy ans6.s6_addr[0] = 0x0b; 1041275970Scy ans6.s6_addr[1] = 0x0b; 1042275970Scy ans6.s6_addr[14] = 0xf0; 1043275970Scy ans6.s6_addr[15] = 0x0d; 1044275970Scy evdns_server_request_add_aaaa_reply(req, qname, 1045275970Scy 1, &ans6.s6_addr, 2000); 1046275970Scy added_any = 1; 1047275970Scy } else if (!evutil_ascii_strcasecmp(qname, 1048275970Scy "v6assert.example.com")) { 1049275970Scy TT_FAIL(("Got a A request for v6assert")); 1050275970Scy } 1051275970Scy } else if (!evutil_ascii_strcasecmp(qname, 1052275970Scy "v6timeout.example.com")) { 1053275970Scy if (qtype == EVDNS_TYPE_A) { 1054275970Scy ans.s_addr = htonl(0xabcdef01); 1055275970Scy evdns_server_request_add_a_reply(req, qname, 1056275970Scy 1, &ans.s_addr, 2000); 1057275970Scy added_any = 1; 1058275970Scy } else if (qtype == EVDNS_TYPE_AAAA) { 1059275970Scy /* Let the v6 request time out.*/ 1060275970Scy evdns_server_request_drop(req); 1061275970Scy return; 1062275970Scy } 1063275970Scy } else if (!evutil_ascii_strcasecmp(qname, 1064275970Scy "v4timeout.example.com")) { 1065275970Scy if (qtype == EVDNS_TYPE_AAAA) { 1066275970Scy ans6.s6_addr[0] = 0x0a; 1067275970Scy ans6.s6_addr[1] = 0x0a; 1068275970Scy ans6.s6_addr[14] = 0xff; 1069275970Scy ans6.s6_addr[15] = 0x01; 1070275970Scy evdns_server_request_add_aaaa_reply(req, qname, 1071275970Scy 1, &ans6.s6_addr, 2000); 1072275970Scy added_any = 1; 1073275970Scy } else if (qtype == EVDNS_TYPE_A) { 1074275970Scy /* Let the v4 request time out.*/ 1075275970Scy evdns_server_request_drop(req); 1076275970Scy return; 1077275970Scy } 1078275970Scy } else if (!evutil_ascii_strcasecmp(qname, 1079275970Scy "v6timeout-nonexist.example.com")) { 1080275970Scy if (qtype == EVDNS_TYPE_A) { 1081275970Scy /* Fall through, give an nexist. */ 1082275970Scy } else if (qtype == EVDNS_TYPE_AAAA) { 1083275970Scy /* Let the v6 request time out.*/ 1084275970Scy evdns_server_request_drop(req); 1085275970Scy return; 1086275970Scy } 1087275970Scy } else if (!evutil_ascii_strcasecmp(qname, 1088275970Scy "all-timeout.example.com")) { 1089275970Scy /* drop all requests */ 1090275970Scy evdns_server_request_drop(req); 1091275970Scy return; 1092275970Scy } else { 1093275970Scy TT_GRIPE(("Got weird request for %s",qname)); 1094275970Scy } 1095275970Scy } 1096275970Scy if (added_any) { 1097275970Scy TT_BLATHER(("answering")); 1098275970Scy evdns_server_request_respond(req, 0); 1099275970Scy } else { 1100275970Scy TT_BLATHER(("saying nexist.")); 1101275970Scy evdns_server_request_respond(req, 3); 1102275970Scy } 1103275970Scy} 1104275970Scy 1105275970Scy/* Implements a listener for connect_hostname test. */ 1106275970Scystatic void 1107275970Scynil_accept_cb(struct evconnlistener *l, evutil_socket_t fd, struct sockaddr *s, 1108275970Scy int socklen, void *arg) 1109275970Scy{ 1110275970Scy int *p = arg; 1111275970Scy (*p)++; 1112275970Scy ++total_n_accepted; 1113275970Scy /* don't do anything with the socket; let it close when we exit() */ 1114275970Scy if (total_n_accepted >= 3 && total_connected_or_failed >= 5) 1115275970Scy event_base_loopexit(be_connect_hostname_base, 1116275970Scy NULL); 1117275970Scy} 1118275970Scy 1119275970Scystruct be_conn_hostname_result { 1120275970Scy int dnserr; 1121275970Scy int what; 1122275970Scy}; 1123275970Scy 1124275970Scy/* Bufferevent event callback for the connect_hostname test: remembers what 1125275970Scy * event we got. */ 1126275970Scystatic void 1127275970Scybe_connect_hostname_event_cb(struct bufferevent *bev, short what, void *ctx) 1128275970Scy{ 1129275970Scy struct be_conn_hostname_result *got = ctx; 1130275970Scy if (!got->what) { 1131275970Scy TT_BLATHER(("Got a bufferevent event %d", what)); 1132275970Scy got->what = what; 1133275970Scy 1134275970Scy if ((what & BEV_EVENT_CONNECTED) || (what & BEV_EVENT_ERROR)) { 1135275970Scy int r; 1136275970Scy if ((r = bufferevent_socket_get_dns_error(bev))) { 1137275970Scy got->dnserr = r; 1138275970Scy TT_BLATHER(("DNS error %d: %s", r, 1139275970Scy evutil_gai_strerror(r))); 1140275970Scy } ++total_connected_or_failed; 1141275970Scy TT_BLATHER(("Got %d connections or errors.", total_connected_or_failed)); 1142275970Scy 1143275970Scy if (total_n_accepted >= 3 && total_connected_or_failed >= 5) 1144275970Scy event_base_loopexit(be_connect_hostname_base, 1145275970Scy NULL); 1146275970Scy } 1147275970Scy } else { 1148275970Scy TT_FAIL(("Two events on one bufferevent. %d,%d", 1149275970Scy got->what, (int)what)); 1150275970Scy } 1151275970Scy} 1152275970Scy 1153275970Scystatic void 1154275970Scytest_bufferevent_connect_hostname(void *arg) 1155275970Scy{ 1156275970Scy struct basic_test_data *data = arg; 1157275970Scy struct evconnlistener *listener = NULL; 1158275970Scy struct bufferevent *be1=NULL, *be2=NULL, *be3=NULL, *be4=NULL, *be5=NULL; 1159275970Scy struct be_conn_hostname_result be1_outcome={0,0}, be2_outcome={0,0}, 1160275970Scy be3_outcome={0,0}, be4_outcome={0,0}, be5_outcome={0,0}; 1161275970Scy int expect_err5; 1162275970Scy struct evdns_base *dns=NULL; 1163275970Scy struct evdns_server_port *port=NULL; 1164275970Scy struct sockaddr_in sin; 1165275970Scy int listener_port=-1; 1166275970Scy ev_uint16_t dns_port=0; 1167275970Scy int n_accept=0, n_dns=0; 1168275970Scy char buf[128]; 1169275970Scy 1170275970Scy be_connect_hostname_base = data->base; 1171275970Scy 1172275970Scy /* Bind an address and figure out what port it's on. */ 1173275970Scy memset(&sin, 0, sizeof(sin)); 1174275970Scy sin.sin_family = AF_INET; 1175275970Scy sin.sin_addr.s_addr = htonl(0x7f000001); /* 127.0.0.1 */ 1176275970Scy sin.sin_port = 0; 1177275970Scy listener = evconnlistener_new_bind(data->base, nil_accept_cb, 1178275970Scy &n_accept, 1179275970Scy LEV_OPT_REUSEABLE|LEV_OPT_CLOSE_ON_EXEC, 1180275970Scy -1, (struct sockaddr *)&sin, sizeof(sin)); 1181275970Scy tt_assert(listener); 1182275970Scy listener_port = regress_get_socket_port( 1183275970Scy evconnlistener_get_fd(listener)); 1184275970Scy 1185275970Scy port = regress_get_dnsserver(data->base, &dns_port, NULL, 1186275970Scy be_getaddrinfo_server_cb, &n_dns); 1187275970Scy tt_assert(port); 1188275970Scy tt_int_op(dns_port, >=, 0); 1189275970Scy 1190275970Scy /* Start an evdns_base that uses the server as its resolver. */ 1191275970Scy dns = evdns_base_new(data->base, 0); 1192275970Scy evutil_snprintf(buf, sizeof(buf), "127.0.0.1:%d", (int)dns_port); 1193275970Scy evdns_base_nameserver_ip_add(dns, buf); 1194275970Scy 1195275970Scy /* Now, finally, at long last, launch the bufferevents. One should do 1196275970Scy * a failing lookup IP, one should do a successful lookup by IP, 1197275970Scy * and one should do a successful lookup by hostname. */ 1198275970Scy be1 = bufferevent_socket_new(data->base, -1, BEV_OPT_CLOSE_ON_FREE); 1199275970Scy be2 = bufferevent_socket_new(data->base, -1, BEV_OPT_CLOSE_ON_FREE); 1200275970Scy be3 = bufferevent_socket_new(data->base, -1, BEV_OPT_CLOSE_ON_FREE); 1201275970Scy be4 = bufferevent_socket_new(data->base, -1, BEV_OPT_CLOSE_ON_FREE); 1202275970Scy be5 = bufferevent_socket_new(data->base, -1, BEV_OPT_CLOSE_ON_FREE); 1203275970Scy 1204275970Scy bufferevent_setcb(be1, NULL, NULL, be_connect_hostname_event_cb, 1205275970Scy &be1_outcome); 1206275970Scy bufferevent_setcb(be2, NULL, NULL, be_connect_hostname_event_cb, 1207275970Scy &be2_outcome); 1208275970Scy bufferevent_setcb(be3, NULL, NULL, be_connect_hostname_event_cb, 1209275970Scy &be3_outcome); 1210275970Scy bufferevent_setcb(be4, NULL, NULL, be_connect_hostname_event_cb, 1211275970Scy &be4_outcome); 1212275970Scy bufferevent_setcb(be5, NULL, NULL, be_connect_hostname_event_cb, 1213275970Scy &be5_outcome); 1214275970Scy 1215275970Scy /* Launch an async resolve that will fail. */ 1216275970Scy tt_assert(!bufferevent_socket_connect_hostname(be1, dns, AF_INET, 1217275970Scy "nosuchplace.example.com", listener_port)); 1218275970Scy /* Connect to the IP without resolving. */ 1219275970Scy tt_assert(!bufferevent_socket_connect_hostname(be2, dns, AF_INET, 1220275970Scy "127.0.0.1", listener_port)); 1221275970Scy /* Launch an async resolve that will succeed. */ 1222275970Scy tt_assert(!bufferevent_socket_connect_hostname(be3, dns, AF_INET, 1223275970Scy "nobodaddy.example.com", listener_port)); 1224275970Scy /* Use the blocking resolver. This one will fail if your resolver 1225275970Scy * can't resolve localhost to 127.0.0.1 */ 1226275970Scy tt_assert(!bufferevent_socket_connect_hostname(be4, NULL, AF_INET, 1227275970Scy "localhost", listener_port)); 1228275970Scy /* Use the blocking resolver with a nonexistent hostname. */ 1229275970Scy tt_assert(!bufferevent_socket_connect_hostname(be5, NULL, AF_INET, 1230275970Scy "nonesuch.nowhere.example.com", 80)); 1231275970Scy { 1232275970Scy /* The blocking resolver will use the system nameserver, which 1233275970Scy * might tell us anything. (Yes, some twits even pretend that 1234275970Scy * example.com is real.) Let's see what answer to expect. */ 1235275970Scy struct evutil_addrinfo hints, *ai = NULL; 1236275970Scy memset(&hints, 0, sizeof(hints)); 1237275970Scy hints.ai_family = AF_INET; 1238275970Scy hints.ai_socktype = SOCK_STREAM; 1239275970Scy hints.ai_protocol = IPPROTO_TCP; 1240275970Scy expect_err5 = evutil_getaddrinfo( 1241275970Scy "nonesuch.nowhere.example.com", "80", &hints, &ai); 1242275970Scy } 1243275970Scy 1244275970Scy event_base_dispatch(data->base); 1245275970Scy 1246275970Scy tt_int_op(be1_outcome.what, ==, BEV_EVENT_ERROR); 1247275970Scy tt_int_op(be1_outcome.dnserr, ==, EVUTIL_EAI_NONAME); 1248275970Scy tt_int_op(be2_outcome.what, ==, BEV_EVENT_CONNECTED); 1249275970Scy tt_int_op(be2_outcome.dnserr, ==, 0); 1250275970Scy tt_int_op(be3_outcome.what, ==, BEV_EVENT_CONNECTED); 1251275970Scy tt_int_op(be3_outcome.dnserr, ==, 0); 1252275970Scy tt_int_op(be4_outcome.what, ==, BEV_EVENT_CONNECTED); 1253275970Scy tt_int_op(be4_outcome.dnserr, ==, 0); 1254275970Scy if (expect_err5) { 1255275970Scy tt_int_op(be5_outcome.what, ==, BEV_EVENT_ERROR); 1256275970Scy tt_int_op(be5_outcome.dnserr, ==, expect_err5); 1257275970Scy } 1258275970Scy 1259275970Scy tt_int_op(n_accept, ==, 3); 1260275970Scy tt_int_op(n_dns, ==, 2); 1261275970Scy 1262275970Scyend: 1263275970Scy if (listener) 1264275970Scy evconnlistener_free(listener); 1265275970Scy if (port) 1266275970Scy evdns_close_server_port(port); 1267275970Scy if (dns) 1268275970Scy evdns_base_free(dns, 0); 1269275970Scy if (be1) 1270275970Scy bufferevent_free(be1); 1271275970Scy if (be2) 1272275970Scy bufferevent_free(be2); 1273275970Scy if (be3) 1274275970Scy bufferevent_free(be3); 1275275970Scy if (be4) 1276275970Scy bufferevent_free(be4); 1277275970Scy if (be5) 1278275970Scy bufferevent_free(be5); 1279275970Scy} 1280275970Scy 1281275970Scy 1282275970Scystruct gai_outcome { 1283275970Scy int err; 1284275970Scy struct evutil_addrinfo *ai; 1285275970Scy}; 1286275970Scy 1287275970Scystatic int n_gai_results_pending = 0; 1288275970Scystatic struct event_base *exit_base_on_no_pending_results = NULL; 1289275970Scy 1290275970Scystatic void 1291275970Scygai_cb(int err, struct evutil_addrinfo *res, void *ptr) 1292275970Scy{ 1293275970Scy struct gai_outcome *go = ptr; 1294275970Scy go->err = err; 1295275970Scy go->ai = res; 1296275970Scy if (--n_gai_results_pending <= 0 && exit_base_on_no_pending_results) 1297275970Scy event_base_loopexit(exit_base_on_no_pending_results, NULL); 1298275970Scy if (n_gai_results_pending < 900) 1299275970Scy TT_BLATHER(("Got an answer; expecting %d more.", 1300275970Scy n_gai_results_pending)); 1301275970Scy} 1302275970Scy 1303275970Scystatic void 1304275970Scycancel_gai_cb(evutil_socket_t fd, short what, void *ptr) 1305275970Scy{ 1306275970Scy struct evdns_getaddrinfo_request *r = ptr; 1307275970Scy evdns_getaddrinfo_cancel(r); 1308275970Scy} 1309275970Scy 1310275970Scystatic void 1311275970Scytest_getaddrinfo_async(void *arg) 1312275970Scy{ 1313275970Scy struct basic_test_data *data = arg; 1314275970Scy struct evutil_addrinfo hints, *a; 1315275970Scy struct gai_outcome local_outcome; 1316275970Scy struct gai_outcome a_out[12]; 1317275970Scy int i; 1318275970Scy struct evdns_getaddrinfo_request *r; 1319275970Scy char buf[128]; 1320275970Scy struct evdns_server_port *port = NULL; 1321275970Scy ev_uint16_t dns_port = 0; 1322275970Scy int n_dns_questions = 0; 1323275970Scy struct evdns_base *dns_base; 1324275970Scy 1325290000Sglebius memset(&a_out, 0, sizeof(a_out)); 1326275970Scy memset(&local_outcome, 0, sizeof(local_outcome)); 1327275970Scy 1328275970Scy dns_base = evdns_base_new(data->base, 0); 1329275970Scy tt_assert(dns_base); 1330275970Scy 1331275970Scy /* for localhost */ 1332275970Scy evdns_base_load_hosts(dns_base, NULL); 1333275970Scy 1334275970Scy tt_assert(! evdns_base_set_option(dns_base, "timeout", "0.3")); 1335275970Scy tt_assert(! evdns_base_set_option(dns_base, "getaddrinfo-allow-skew", "0.2")); 1336275970Scy 1337275970Scy n_gai_results_pending = 10000; /* don't think about exiting yet. */ 1338275970Scy 1339275970Scy /* 1. Try some cases that will never hit the asynchronous resolver. */ 1340275970Scy /* 1a. Simple case with a symbolic service name */ 1341275970Scy memset(&hints, 0, sizeof(hints)); 1342275970Scy hints.ai_family = PF_UNSPEC; 1343275970Scy hints.ai_socktype = SOCK_STREAM; 1344275970Scy memset(&local_outcome, 0, sizeof(local_outcome)); 1345275970Scy r = evdns_getaddrinfo(dns_base, "1.2.3.4", "http", 1346275970Scy &hints, gai_cb, &local_outcome); 1347275970Scy tt_assert(! r); 1348275970Scy if (!local_outcome.err) { 1349275970Scy tt_ptr_op(local_outcome.ai,!=,NULL); 1350275970Scy test_ai_eq(local_outcome.ai, "1.2.3.4:80", SOCK_STREAM, IPPROTO_TCP); 1351275970Scy evutil_freeaddrinfo(local_outcome.ai); 1352275970Scy local_outcome.ai = NULL; 1353275970Scy } else { 1354275970Scy TT_BLATHER(("Apparently we have no getservbyname.")); 1355275970Scy } 1356275970Scy 1357275970Scy /* 1b. EVUTIL_AI_NUMERICHOST is set */ 1358275970Scy memset(&hints, 0, sizeof(hints)); 1359275970Scy hints.ai_family = PF_UNSPEC; 1360275970Scy hints.ai_flags = EVUTIL_AI_NUMERICHOST; 1361275970Scy memset(&local_outcome, 0, sizeof(local_outcome)); 1362275970Scy r = evdns_getaddrinfo(dns_base, "www.google.com", "80", 1363275970Scy &hints, gai_cb, &local_outcome); 1364275970Scy tt_ptr_op(r,==,NULL); 1365275970Scy tt_int_op(local_outcome.err,==,EVUTIL_EAI_NONAME); 1366275970Scy tt_ptr_op(local_outcome.ai,==,NULL); 1367275970Scy 1368275970Scy /* 1c. We give a numeric address (ipv6) */ 1369275970Scy memset(&hints, 0, sizeof(hints)); 1370275970Scy memset(&local_outcome, 0, sizeof(local_outcome)); 1371275970Scy hints.ai_family = PF_UNSPEC; 1372275970Scy hints.ai_protocol = IPPROTO_TCP; 1373275970Scy r = evdns_getaddrinfo(dns_base, "f::f", "8008", 1374275970Scy &hints, gai_cb, &local_outcome); 1375275970Scy tt_assert(!r); 1376275970Scy tt_int_op(local_outcome.err,==,0); 1377275970Scy tt_assert(local_outcome.ai); 1378275970Scy tt_ptr_op(local_outcome.ai->ai_next,==,NULL); 1379275970Scy test_ai_eq(local_outcome.ai, "[f::f]:8008", SOCK_STREAM, IPPROTO_TCP); 1380275970Scy evutil_freeaddrinfo(local_outcome.ai); 1381275970Scy local_outcome.ai = NULL; 1382275970Scy 1383275970Scy /* 1d. We give a numeric address (ipv4) */ 1384275970Scy memset(&hints, 0, sizeof(hints)); 1385275970Scy memset(&local_outcome, 0, sizeof(local_outcome)); 1386275970Scy hints.ai_family = PF_UNSPEC; 1387275970Scy r = evdns_getaddrinfo(dns_base, "5.6.7.8", NULL, 1388275970Scy &hints, gai_cb, &local_outcome); 1389275970Scy tt_assert(!r); 1390275970Scy tt_int_op(local_outcome.err,==,0); 1391275970Scy tt_assert(local_outcome.ai); 1392275970Scy a = ai_find_by_protocol(local_outcome.ai, IPPROTO_TCP); 1393275970Scy tt_assert(a); 1394275970Scy test_ai_eq(a, "5.6.7.8", SOCK_STREAM, IPPROTO_TCP); 1395275970Scy a = ai_find_by_protocol(local_outcome.ai, IPPROTO_UDP); 1396275970Scy tt_assert(a); 1397275970Scy test_ai_eq(a, "5.6.7.8", SOCK_DGRAM, IPPROTO_UDP); 1398275970Scy evutil_freeaddrinfo(local_outcome.ai); 1399275970Scy local_outcome.ai = NULL; 1400275970Scy 1401275970Scy /* 1e. nodename is NULL (bind) */ 1402275970Scy memset(&hints, 0, sizeof(hints)); 1403275970Scy memset(&local_outcome, 0, sizeof(local_outcome)); 1404275970Scy hints.ai_family = PF_UNSPEC; 1405275970Scy hints.ai_socktype = SOCK_DGRAM; 1406275970Scy hints.ai_flags = EVUTIL_AI_PASSIVE; 1407275970Scy r = evdns_getaddrinfo(dns_base, NULL, "9090", 1408275970Scy &hints, gai_cb, &local_outcome); 1409275970Scy tt_assert(!r); 1410275970Scy tt_int_op(local_outcome.err,==,0); 1411275970Scy tt_assert(local_outcome.ai); 1412275970Scy /* we should get a v4 address of 0.0.0.0... */ 1413275970Scy a = ai_find_by_family(local_outcome.ai, PF_INET); 1414275970Scy tt_assert(a); 1415275970Scy test_ai_eq(a, "0.0.0.0:9090", SOCK_DGRAM, IPPROTO_UDP); 1416275970Scy /* ... and a v6 address of ::0 */ 1417275970Scy a = ai_find_by_family(local_outcome.ai, PF_INET6); 1418275970Scy tt_assert(a); 1419275970Scy test_ai_eq(a, "[::]:9090", SOCK_DGRAM, IPPROTO_UDP); 1420275970Scy evutil_freeaddrinfo(local_outcome.ai); 1421275970Scy local_outcome.ai = NULL; 1422275970Scy 1423275970Scy /* 1f. nodename is NULL (connect) */ 1424275970Scy memset(&hints, 0, sizeof(hints)); 1425275970Scy memset(&local_outcome, 0, sizeof(local_outcome)); 1426275970Scy hints.ai_family = PF_UNSPEC; 1427275970Scy hints.ai_socktype = SOCK_STREAM; 1428275970Scy r = evdns_getaddrinfo(dns_base, NULL, "2", 1429275970Scy &hints, gai_cb, &local_outcome); 1430275970Scy tt_assert(!r); 1431275970Scy tt_int_op(local_outcome.err,==,0); 1432275970Scy tt_assert(local_outcome.ai); 1433275970Scy /* we should get a v4 address of 127.0.0.1 .... */ 1434275970Scy a = ai_find_by_family(local_outcome.ai, PF_INET); 1435275970Scy tt_assert(a); 1436275970Scy test_ai_eq(a, "127.0.0.1:2", SOCK_STREAM, IPPROTO_TCP); 1437275970Scy /* ... and a v6 address of ::1 */ 1438275970Scy a = ai_find_by_family(local_outcome.ai, PF_INET6); 1439275970Scy tt_assert(a); 1440275970Scy test_ai_eq(a, "[::1]:2", SOCK_STREAM, IPPROTO_TCP); 1441275970Scy evutil_freeaddrinfo(local_outcome.ai); 1442275970Scy local_outcome.ai = NULL; 1443275970Scy 1444275970Scy /* 1g. We find localhost immediately. (pf_unspec) */ 1445275970Scy memset(&hints, 0, sizeof(hints)); 1446275970Scy memset(&local_outcome, 0, sizeof(local_outcome)); 1447275970Scy hints.ai_family = PF_UNSPEC; 1448275970Scy hints.ai_socktype = SOCK_STREAM; 1449275970Scy r = evdns_getaddrinfo(dns_base, "LOCALHOST", "80", 1450275970Scy &hints, gai_cb, &local_outcome); 1451275970Scy tt_assert(!r); 1452275970Scy tt_int_op(local_outcome.err,==,0); 1453275970Scy tt_assert(local_outcome.ai); 1454275970Scy /* we should get a v4 address of 127.0.0.1 .... */ 1455275970Scy a = ai_find_by_family(local_outcome.ai, PF_INET); 1456275970Scy tt_assert(a); 1457275970Scy test_ai_eq(a, "127.0.0.1:80", SOCK_STREAM, IPPROTO_TCP); 1458275970Scy /* ... and a v6 address of ::1 */ 1459275970Scy a = ai_find_by_family(local_outcome.ai, PF_INET6); 1460275970Scy tt_assert(a); 1461275970Scy test_ai_eq(a, "[::1]:80", SOCK_STREAM, IPPROTO_TCP); 1462275970Scy evutil_freeaddrinfo(local_outcome.ai); 1463275970Scy local_outcome.ai = NULL; 1464275970Scy 1465275970Scy /* 1g. We find localhost immediately. (pf_inet6) */ 1466275970Scy memset(&hints, 0, sizeof(hints)); 1467275970Scy memset(&local_outcome, 0, sizeof(local_outcome)); 1468275970Scy hints.ai_family = PF_INET6; 1469275970Scy hints.ai_socktype = SOCK_STREAM; 1470275970Scy r = evdns_getaddrinfo(dns_base, "LOCALHOST", "9999", 1471275970Scy &hints, gai_cb, &local_outcome); 1472275970Scy tt_assert(! r); 1473275970Scy tt_int_op(local_outcome.err,==,0); 1474275970Scy tt_assert(local_outcome.ai); 1475275970Scy a = local_outcome.ai; 1476275970Scy test_ai_eq(a, "[::1]:9999", SOCK_STREAM, IPPROTO_TCP); 1477275970Scy tt_ptr_op(a->ai_next, ==, NULL); 1478275970Scy evutil_freeaddrinfo(local_outcome.ai); 1479275970Scy local_outcome.ai = NULL; 1480275970Scy 1481275970Scy /* 2. Okay, now we can actually test the asynchronous resolver. */ 1482275970Scy /* Start a dummy local dns server... */ 1483275970Scy port = regress_get_dnsserver(data->base, &dns_port, NULL, 1484275970Scy be_getaddrinfo_server_cb, &n_dns_questions); 1485275970Scy tt_assert(port); 1486275970Scy tt_int_op(dns_port, >=, 0); 1487275970Scy /* ... and tell the evdns_base about it. */ 1488275970Scy evutil_snprintf(buf, sizeof(buf), "127.0.0.1:%d", dns_port); 1489275970Scy evdns_base_nameserver_ip_add(dns_base, buf); 1490275970Scy 1491275970Scy memset(&hints, 0, sizeof(hints)); 1492275970Scy hints.ai_family = PF_UNSPEC; 1493275970Scy hints.ai_socktype = SOCK_STREAM; 1494275970Scy hints.ai_flags = EVUTIL_AI_CANONNAME; 1495275970Scy /* 0: Request for both.example.com should return both addresses. */ 1496275970Scy r = evdns_getaddrinfo(dns_base, "both.example.com", "8000", 1497275970Scy &hints, gai_cb, &a_out[0]); 1498275970Scy tt_assert(r); 1499275970Scy 1500275970Scy /* 1: Request for v4only.example.com should return one address. */ 1501275970Scy r = evdns_getaddrinfo(dns_base, "v4only.example.com", "8001", 1502275970Scy &hints, gai_cb, &a_out[1]); 1503275970Scy tt_assert(r); 1504275970Scy 1505275970Scy /* 2: Request for v6only.example.com should return one address. */ 1506275970Scy hints.ai_flags = 0; 1507275970Scy r = evdns_getaddrinfo(dns_base, "v6only.example.com", "8002", 1508275970Scy &hints, gai_cb, &a_out[2]); 1509275970Scy tt_assert(r); 1510275970Scy 1511275970Scy /* 3: PF_INET request for v4assert.example.com should not generate a 1512275970Scy * v6 request. The server will fail the test if it does. */ 1513275970Scy hints.ai_family = PF_INET; 1514275970Scy r = evdns_getaddrinfo(dns_base, "v4assert.example.com", "8003", 1515275970Scy &hints, gai_cb, &a_out[3]); 1516275970Scy tt_assert(r); 1517275970Scy 1518275970Scy /* 4: PF_INET6 request for v6assert.example.com should not generate a 1519275970Scy * v4 request. The server will fail the test if it does. */ 1520275970Scy hints.ai_family = PF_INET6; 1521275970Scy r = evdns_getaddrinfo(dns_base, "v6assert.example.com", "8004", 1522275970Scy &hints, gai_cb, &a_out[4]); 1523275970Scy tt_assert(r); 1524275970Scy 1525275970Scy /* 5: PF_INET request for nosuchplace.example.com should give NEXIST. */ 1526275970Scy hints.ai_family = PF_INET; 1527275970Scy r = evdns_getaddrinfo(dns_base, "nosuchplace.example.com", "8005", 1528275970Scy &hints, gai_cb, &a_out[5]); 1529275970Scy tt_assert(r); 1530275970Scy 1531275970Scy /* 6: PF_UNSPEC request for nosuchplace.example.com should give NEXIST. 1532275970Scy */ 1533275970Scy hints.ai_family = PF_UNSPEC; 1534275970Scy r = evdns_getaddrinfo(dns_base, "nosuchplace.example.com", "8006", 1535275970Scy &hints, gai_cb, &a_out[6]); 1536275970Scy tt_assert(r); 1537275970Scy 1538275970Scy /* 7: PF_UNSPEC request for v6timeout.example.com should give an ipv4 1539275970Scy * address only. */ 1540275970Scy hints.ai_family = PF_UNSPEC; 1541275970Scy r = evdns_getaddrinfo(dns_base, "v6timeout.example.com", "8007", 1542275970Scy &hints, gai_cb, &a_out[7]); 1543275970Scy tt_assert(r); 1544275970Scy 1545275970Scy /* 8: PF_UNSPEC request for v6timeout-nonexist.example.com should give 1546275970Scy * a NEXIST */ 1547275970Scy hints.ai_family = PF_UNSPEC; 1548275970Scy r = evdns_getaddrinfo(dns_base, "v6timeout-nonexist.example.com", 1549275970Scy "8008", &hints, gai_cb, &a_out[8]); 1550275970Scy tt_assert(r); 1551275970Scy 1552275970Scy /* 9: AI_ADDRCONFIG should at least not crash. Can't test it more 1553275970Scy * without knowing what kind of internet we have. */ 1554275970Scy hints.ai_flags |= EVUTIL_AI_ADDRCONFIG; 1555275970Scy r = evdns_getaddrinfo(dns_base, "both.example.com", 1556275970Scy "8009", &hints, gai_cb, &a_out[9]); 1557275970Scy tt_assert(r); 1558275970Scy 1559275970Scy /* 10: PF_UNSPEC for v4timeout.example.com should give an ipv6 address 1560275970Scy * only. */ 1561275970Scy hints.ai_family = PF_UNSPEC; 1562275970Scy hints.ai_flags = 0; 1563275970Scy r = evdns_getaddrinfo(dns_base, "v4timeout.example.com", "8010", 1564275970Scy &hints, gai_cb, &a_out[10]); 1565275970Scy tt_assert(r); 1566275970Scy 1567275970Scy /* 11: timeout.example.com: cancel it after 100 msec. */ 1568275970Scy r = evdns_getaddrinfo(dns_base, "all-timeout.example.com", "8011", 1569275970Scy &hints, gai_cb, &a_out[11]); 1570275970Scy tt_assert(r); 1571275970Scy { 1572275970Scy struct timeval tv; 1573275970Scy tv.tv_sec = 0; 1574275970Scy tv.tv_usec = 100*1000; /* 100 msec */ 1575275970Scy event_base_once(data->base, -1, EV_TIMEOUT, cancel_gai_cb, 1576275970Scy r, &tv); 1577275970Scy } 1578275970Scy 1579275970Scy /* XXXXX There are more tests we could do, including: 1580275970Scy 1581275970Scy - A test to elicit NODATA. 1582275970Scy 1583275970Scy */ 1584275970Scy 1585275970Scy n_gai_results_pending = 12; 1586275970Scy exit_base_on_no_pending_results = data->base; 1587275970Scy 1588275970Scy event_base_dispatch(data->base); 1589275970Scy 1590275970Scy /* 0: both.example.com */ 1591275970Scy tt_int_op(a_out[0].err, ==, 0); 1592275970Scy tt_assert(a_out[0].ai); 1593275970Scy tt_assert(a_out[0].ai->ai_next); 1594275970Scy tt_assert(!a_out[0].ai->ai_next->ai_next); 1595275970Scy a = ai_find_by_family(a_out[0].ai, PF_INET); 1596275970Scy tt_assert(a); 1597275970Scy test_ai_eq(a, "80.80.32.32:8000", SOCK_STREAM, IPPROTO_TCP); 1598275970Scy a = ai_find_by_family(a_out[0].ai, PF_INET6); 1599275970Scy tt_assert(a); 1600275970Scy test_ai_eq(a, "[80ff::bbbb]:8000", SOCK_STREAM, IPPROTO_TCP); 1601275970Scy tt_assert(a_out[0].ai->ai_canonname); 1602275970Scy tt_str_op(a_out[0].ai->ai_canonname, ==, "both-canonical.example.com"); 1603275970Scy 1604275970Scy /* 1: v4only.example.com */ 1605275970Scy tt_int_op(a_out[1].err, ==, 0); 1606275970Scy tt_assert(a_out[1].ai); 1607275970Scy tt_assert(! a_out[1].ai->ai_next); 1608275970Scy test_ai_eq(a_out[1].ai, "18.52.86.120:8001", SOCK_STREAM, IPPROTO_TCP); 1609275970Scy tt_assert(a_out[1].ai->ai_canonname == NULL); 1610275970Scy 1611275970Scy 1612275970Scy /* 2: v6only.example.com */ 1613275970Scy tt_int_op(a_out[2].err, ==, 0); 1614275970Scy tt_assert(a_out[2].ai); 1615275970Scy tt_assert(! a_out[2].ai->ai_next); 1616275970Scy test_ai_eq(a_out[2].ai, "[b0b::f00d]:8002", SOCK_STREAM, IPPROTO_TCP); 1617275970Scy 1618275970Scy /* 3: v4assert.example.com */ 1619275970Scy tt_int_op(a_out[3].err, ==, 0); 1620275970Scy tt_assert(a_out[3].ai); 1621275970Scy tt_assert(! a_out[3].ai->ai_next); 1622275970Scy test_ai_eq(a_out[3].ai, "18.52.86.120:8003", SOCK_STREAM, IPPROTO_TCP); 1623275970Scy 1624275970Scy /* 4: v6assert.example.com */ 1625275970Scy tt_int_op(a_out[4].err, ==, 0); 1626275970Scy tt_assert(a_out[4].ai); 1627275970Scy tt_assert(! a_out[4].ai->ai_next); 1628275970Scy test_ai_eq(a_out[4].ai, "[b0b::f00d]:8004", SOCK_STREAM, IPPROTO_TCP); 1629275970Scy 1630275970Scy /* 5: nosuchplace.example.com (inet) */ 1631275970Scy tt_int_op(a_out[5].err, ==, EVUTIL_EAI_NONAME); 1632275970Scy tt_assert(! a_out[5].ai); 1633275970Scy 1634275970Scy /* 6: nosuchplace.example.com (unspec) */ 1635275970Scy tt_int_op(a_out[6].err, ==, EVUTIL_EAI_NONAME); 1636275970Scy tt_assert(! a_out[6].ai); 1637275970Scy 1638275970Scy /* 7: v6timeout.example.com */ 1639275970Scy tt_int_op(a_out[7].err, ==, 0); 1640275970Scy tt_assert(a_out[7].ai); 1641275970Scy tt_assert(! a_out[7].ai->ai_next); 1642275970Scy test_ai_eq(a_out[7].ai, "171.205.239.1:8007", SOCK_STREAM, IPPROTO_TCP); 1643275970Scy 1644275970Scy /* 8: v6timeout-nonexist.example.com */ 1645275970Scy tt_int_op(a_out[8].err, ==, EVUTIL_EAI_NONAME); 1646275970Scy tt_assert(! a_out[8].ai); 1647275970Scy 1648275970Scy /* 9: both (ADDRCONFIG) */ 1649275970Scy tt_int_op(a_out[9].err, ==, 0); 1650275970Scy tt_assert(a_out[9].ai); 1651275970Scy a = ai_find_by_family(a_out[9].ai, PF_INET); 1652275970Scy if (a) 1653275970Scy test_ai_eq(a, "80.80.32.32:8009", SOCK_STREAM, IPPROTO_TCP); 1654275970Scy else 1655275970Scy tt_assert(ai_find_by_family(a_out[9].ai, PF_INET6)); 1656275970Scy a = ai_find_by_family(a_out[9].ai, PF_INET6); 1657275970Scy if (a) 1658275970Scy test_ai_eq(a, "[80ff::bbbb]:8009", SOCK_STREAM, IPPROTO_TCP); 1659275970Scy else 1660275970Scy tt_assert(ai_find_by_family(a_out[9].ai, PF_INET)); 1661275970Scy 1662275970Scy /* 10: v4timeout.example.com */ 1663275970Scy tt_int_op(a_out[10].err, ==, 0); 1664275970Scy tt_assert(a_out[10].ai); 1665275970Scy tt_assert(! a_out[10].ai->ai_next); 1666275970Scy test_ai_eq(a_out[10].ai, "[a0a::ff01]:8010", SOCK_STREAM, IPPROTO_TCP); 1667275970Scy 1668275970Scy /* 11: cancelled request. */ 1669275970Scy tt_int_op(a_out[11].err, ==, EVUTIL_EAI_CANCEL); 1670275970Scy tt_assert(a_out[11].ai == NULL); 1671275970Scy 1672275970Scyend: 1673275970Scy if (local_outcome.ai) 1674275970Scy evutil_freeaddrinfo(local_outcome.ai); 1675290000Sglebius#define ARRAY_SIZE(a) (sizeof(a) / sizeof(a[0])) 1676290000Sglebius for (i=0;i<(int)ARRAY_SIZE(a_out);++i) { 1677275970Scy if (a_out[i].ai) 1678275970Scy evutil_freeaddrinfo(a_out[i].ai); 1679275970Scy } 1680275970Scy if (port) 1681275970Scy evdns_close_server_port(port); 1682275970Scy if (dns_base) 1683275970Scy evdns_base_free(dns_base, 0); 1684275970Scy} 1685275970Scy 1686275970Scystruct gaic_request_status { 1687275970Scy int magic; 1688275970Scy struct event_base *base; 1689275970Scy struct evdns_base *dns_base; 1690275970Scy struct evdns_getaddrinfo_request *request; 1691275970Scy struct event cancel_event; 1692275970Scy int canceled; 1693275970Scy}; 1694275970Scy 1695275970Scy#define GAIC_MAGIC 0x1234abcd 1696275970Scy 1697275970Scystatic int pending = 0; 1698275970Scy 1699275970Scystatic void 1700275970Scygaic_cancel_request_cb(evutil_socket_t fd, short what, void *arg) 1701275970Scy{ 1702275970Scy struct gaic_request_status *status = arg; 1703275970Scy 1704275970Scy tt_assert(status->magic == GAIC_MAGIC); 1705275970Scy status->canceled = 1; 1706275970Scy evdns_getaddrinfo_cancel(status->request); 1707275970Scy return; 1708275970Scyend: 1709275970Scy event_base_loopexit(status->base, NULL); 1710275970Scy} 1711275970Scy 1712275970Scystatic void 1713275970Scygaic_server_cb(struct evdns_server_request *req, void *arg) 1714275970Scy{ 1715275970Scy ev_uint32_t answer = 0x7f000001; 1716275970Scy tt_assert(req->nquestions); 1717275970Scy evdns_server_request_add_a_reply(req, req->questions[0]->name, 1, 1718275970Scy &answer, 100); 1719275970Scy evdns_server_request_respond(req, 0); 1720275970Scy return; 1721275970Scyend: 1722275970Scy evdns_server_request_respond(req, DNS_ERR_REFUSED); 1723275970Scy} 1724275970Scy 1725275970Scy 1726275970Scystatic void 1727275970Scygaic_getaddrinfo_cb(int result, struct evutil_addrinfo *res, void *arg) 1728275970Scy{ 1729275970Scy struct gaic_request_status *status = arg; 1730275970Scy struct event_base *base = status->base; 1731275970Scy tt_assert(status->magic == GAIC_MAGIC); 1732275970Scy 1733275970Scy if (result == EVUTIL_EAI_CANCEL) { 1734275970Scy tt_assert(status->canceled); 1735275970Scy } 1736275970Scy event_del(&status->cancel_event); 1737275970Scy 1738275970Scy memset(status, 0xf0, sizeof(*status)); 1739275970Scy free(status); 1740275970Scy 1741275970Scyend: 1742275970Scy if (--pending <= 0) 1743275970Scy event_base_loopexit(base, NULL); 1744275970Scy} 1745275970Scy 1746275970Scystatic void 1747275970Scygaic_launch(struct event_base *base, struct evdns_base *dns_base) 1748275970Scy{ 1749290000Sglebius struct gaic_request_status *status = calloc(1, sizeof(*status)); 1750290000Sglebius tt_assert(status); 1751275970Scy struct timeval tv = { 0, 10000 }; 1752275970Scy status->magic = GAIC_MAGIC; 1753275970Scy status->base = base; 1754275970Scy status->dns_base = dns_base; 1755275970Scy event_assign(&status->cancel_event, base, -1, 0, gaic_cancel_request_cb, 1756275970Scy status); 1757275970Scy status->request = evdns_getaddrinfo(dns_base, 1758275970Scy "foobar.bazquux.example.com", "80", NULL, gaic_getaddrinfo_cb, 1759275970Scy status); 1760275970Scy event_add(&status->cancel_event, &tv); 1761275970Scy ++pending; 1762275970Scy} 1763275970Scy 1764275970Scy#ifdef EVENT_SET_MEM_FUNCTIONS_IMPLEMENTED 1765275970Scy/* FIXME: We should move this to regress_main.c if anything else needs it.*/ 1766275970Scy 1767275970Scy/* Trivial replacements for malloc/free/realloc to check for memory leaks. 1768275970Scy * Not threadsafe. */ 1769275970Scystatic int allocated_chunks = 0; 1770275970Scy 1771275970Scystatic void * 1772275970Scycnt_malloc(size_t sz) 1773275970Scy{ 1774275970Scy allocated_chunks += 1; 1775275970Scy return malloc(sz); 1776275970Scy} 1777275970Scy 1778275970Scystatic void * 1779275970Scycnt_realloc(void *old, size_t sz) 1780275970Scy{ 1781275970Scy if (!old) 1782275970Scy allocated_chunks += 1; 1783275970Scy if (!sz) 1784275970Scy allocated_chunks -= 1; 1785275970Scy return realloc(old, sz); 1786275970Scy} 1787275970Scy 1788275970Scystatic void 1789275970Scycnt_free(void *ptr) 1790275970Scy{ 1791275970Scy allocated_chunks -= 1; 1792275970Scy free(ptr); 1793275970Scy} 1794275970Scy 1795275970Scystruct testleak_env_t { 1796275970Scy struct event_base *base; 1797275970Scy struct evdns_base *dns_base; 1798275970Scy struct evdns_request *req; 1799275970Scy struct generic_dns_callback_result r; 1800275970Scy}; 1801275970Scy 1802275970Scystatic void * 1803275970Scytestleak_setup(const struct testcase_t *testcase) 1804275970Scy{ 1805275970Scy struct testleak_env_t *env; 1806275970Scy 1807275970Scy allocated_chunks = 0; 1808290000Sglebius 1809290000Sglebius /* Reset allocation counter, to start allocations from the very beginning. 1810290000Sglebius * (this will avoid false-positive negative numbers for allocated_chunks) 1811290000Sglebius */ 1812290000Sglebius libevent_global_shutdown(); 1813290000Sglebius 1814275970Scy event_set_mem_functions(cnt_malloc, cnt_realloc, cnt_free); 1815275970Scy 1816290000Sglebius event_enable_debug_mode(); 1817290000Sglebius 1818275970Scy /* not mm_calloc: we don't want to mess with the count. */ 1819275970Scy env = calloc(1, sizeof(struct testleak_env_t)); 1820275970Scy env->base = event_base_new(); 1821275970Scy env->dns_base = evdns_base_new(env->base, 0); 1822275970Scy env->req = evdns_base_resolve_ipv4( 1823275970Scy env->dns_base, "example.com", DNS_QUERY_NO_SEARCH, 1824275970Scy generic_dns_callback, &env->r); 1825275970Scy return env; 1826275970Scy} 1827275970Scy 1828275970Scystatic int 1829275970Scytestleak_cleanup(const struct testcase_t *testcase, void *env_) 1830275970Scy{ 1831275970Scy int ok = 0; 1832275970Scy struct testleak_env_t *env = env_; 1833275970Scy tt_assert(env); 1834275970Scy#ifdef EVENT__DISABLE_DEBUG_MODE 1835275970Scy tt_int_op(allocated_chunks, ==, 0); 1836275970Scy#else 1837290000Sglebius libevent_global_shutdown(); 1838290000Sglebius tt_int_op(allocated_chunks, ==, 0); 1839275970Scy#endif 1840275970Scy ok = 1; 1841275970Scyend: 1842275970Scy if (env) { 1843275970Scy if (env->dns_base) 1844275970Scy evdns_base_free(env->dns_base, 0); 1845275970Scy if (env->base) 1846275970Scy event_base_free(env->base); 1847275970Scy free(env); 1848275970Scy } 1849275970Scy return ok; 1850275970Scy} 1851275970Scy 1852275970Scystatic struct testcase_setup_t testleak_funcs = { 1853275970Scy testleak_setup, testleak_cleanup 1854275970Scy}; 1855275970Scy 1856275970Scystatic void 1857275970Scytest_dbg_leak_cancel(void *env_) 1858275970Scy{ 1859275970Scy /* cancel, loop, free/dns, free/base */ 1860275970Scy struct testleak_env_t *env = env_; 1861275970Scy int send_err_shutdown = 1; 1862275970Scy evdns_cancel_request(env->dns_base, env->req); 1863275970Scy env->req = 0; 1864275970Scy 1865275970Scy /* `req` is freed in callback, that's why one loop is required. */ 1866275970Scy event_base_loop(env->base, EVLOOP_NONBLOCK); 1867275970Scy 1868275970Scy /* send_err_shutdown means nothing as soon as our request is 1869275970Scy * already canceled */ 1870275970Scy evdns_base_free(env->dns_base, send_err_shutdown); 1871275970Scy env->dns_base = 0; 1872275970Scy event_base_free(env->base); 1873275970Scy env->base = 0; 1874275970Scy} 1875275970Scy 1876275970Scystatic void 1877275970Scydbg_leak_resume(void *env_, int cancel, int send_err_shutdown) 1878275970Scy{ 1879275970Scy /* cancel, loop, free/dns, free/base */ 1880275970Scy struct testleak_env_t *env = env_; 1881275970Scy if (cancel) { 1882275970Scy evdns_cancel_request(env->dns_base, env->req); 1883275970Scy tt_assert(!evdns_base_resume(env->dns_base)); 1884275970Scy } else { 1885275970Scy /* TODO: No nameservers, request can't be processed, must be errored */ 1886275970Scy tt_assert(!evdns_base_resume(env->dns_base)); 1887275970Scy } 1888275970Scy 1889275970Scy /** 1890275970Scy * Because we don't cancel request, 1891275970Scy * and want our callback to recieve DNS_ERR_SHUTDOWN, 1892275970Scy * we use deferred callback, and there was 1893275970Scy * - one extra malloc(), 1894275970Scy * @see reply_schedule_callback() 1895275970Scy * - and one missing free 1896275970Scy * @see request_finished() (req->handle->pending_cb = 1) 1897275970Scy * than we don't need to count in testleak_cleanup() 1898275970Scy * 1899275970Scy * So just decrement allocated_chunks to 2, 1900275970Scy * like we already take care about it. 1901275970Scy */ 1902275970Scy if (!cancel && send_err_shutdown) { 1903275970Scy allocated_chunks -= 2; 1904275970Scy } 1905275970Scy 1906275970Scy event_base_loop(env->base, EVLOOP_NONBLOCK); 1907275970Scy 1908275970Scyend: 1909275970Scy evdns_base_free(env->dns_base, send_err_shutdown); 1910275970Scy env->dns_base = 0; 1911290000Sglebius 1912275970Scy event_base_free(env->base); 1913275970Scy env->base = 0; 1914275970Scy} 1915275970Scy 1916275970Scy#define IMPL_DBG_LEAK_RESUME(name, cancel, send_err_shutdown) \ 1917275970Scy static void \ 1918275970Scy test_dbg_leak_##name##_(void *env_) \ 1919275970Scy { \ 1920275970Scy dbg_leak_resume(env_, cancel, send_err_shutdown); \ 1921275970Scy } 1922275970ScyIMPL_DBG_LEAK_RESUME(resume, 0, 0) 1923275970ScyIMPL_DBG_LEAK_RESUME(cancel_and_resume, 1, 0) 1924275970ScyIMPL_DBG_LEAK_RESUME(resume_send_err, 0, 1) 1925275970ScyIMPL_DBG_LEAK_RESUME(cancel_and_resume_send_err, 1, 1) 1926275970Scy 1927275970Scystatic void 1928275970Scytest_dbg_leak_shutdown(void *env_) 1929275970Scy{ 1930275970Scy /* free/dns, loop, free/base */ 1931275970Scy struct testleak_env_t *env = env_; 1932275970Scy int send_err_shutdown = 1; 1933275970Scy 1934275970Scy /* `req` is freed both with `send_err_shutdown` and without it, 1935275970Scy * the only difference is `evdns_callback` call */ 1936275970Scy env->req = 0; 1937275970Scy 1938275970Scy evdns_base_free(env->dns_base, send_err_shutdown); 1939275970Scy env->dns_base = 0; 1940275970Scy 1941275970Scy /* `req` is freed in callback, that's why one loop is required */ 1942275970Scy event_base_loop(env->base, EVLOOP_NONBLOCK); 1943275970Scy event_base_free(env->base); 1944275970Scy env->base = 0; 1945275970Scy} 1946275970Scy#endif 1947275970Scy 1948275970Scystatic void 1949275970Scytest_getaddrinfo_async_cancel_stress(void *ptr) 1950275970Scy{ 1951275970Scy struct event_base *base; 1952275970Scy struct evdns_base *dns_base = NULL; 1953275970Scy struct evdns_server_port *server = NULL; 1954275970Scy evutil_socket_t fd = -1; 1955275970Scy struct sockaddr_in sin; 1956275970Scy struct sockaddr_storage ss; 1957275970Scy ev_socklen_t slen; 1958275970Scy int i; 1959275970Scy 1960275970Scy base = event_base_new(); 1961275970Scy dns_base = evdns_base_new(base, 0); 1962275970Scy 1963275970Scy memset(&sin, 0, sizeof(sin)); 1964275970Scy sin.sin_family = AF_INET; 1965275970Scy sin.sin_port = 0; 1966275970Scy sin.sin_addr.s_addr = htonl(0x7f000001); 1967275970Scy if ((fd = socket(AF_INET, SOCK_DGRAM, 0)) < 0) { 1968275970Scy tt_abort_perror("socket"); 1969275970Scy } 1970275970Scy evutil_make_socket_nonblocking(fd); 1971275970Scy if (bind(fd, (struct sockaddr*)&sin, sizeof(sin))<0) { 1972275970Scy tt_abort_perror("bind"); 1973275970Scy } 1974275970Scy server = evdns_add_server_port_with_base(base, fd, 0, gaic_server_cb, 1975275970Scy base); 1976275970Scy 1977275970Scy memset(&ss, 0, sizeof(ss)); 1978275970Scy slen = sizeof(ss); 1979275970Scy if (getsockname(fd, (struct sockaddr*)&ss, &slen)<0) { 1980275970Scy tt_abort_perror("getsockname"); 1981275970Scy } 1982275970Scy evdns_base_nameserver_sockaddr_add(dns_base, 1983275970Scy (struct sockaddr*)&ss, slen, 0); 1984275970Scy 1985275970Scy for (i = 0; i < 1000; ++i) { 1986275970Scy gaic_launch(base, dns_base); 1987275970Scy } 1988275970Scy 1989275970Scy event_base_dispatch(base); 1990275970Scy 1991275970Scyend: 1992275970Scy if (dns_base) 1993275970Scy evdns_base_free(dns_base, 1); 1994275970Scy if (server) 1995275970Scy evdns_close_server_port(server); 1996290000Sglebius if (base) 1997290000Sglebius event_base_free(base); 1998275970Scy if (fd >= 0) 1999275970Scy evutil_closesocket(fd); 2000275970Scy} 2001275970Scy 2002275970Scy 2003275970Scy#define DNS_LEGACY(name, flags) \ 2004275970Scy { #name, run_legacy_test_fn, flags|TT_LEGACY, &legacy_setup, \ 2005275970Scy dns_##name } 2006275970Scy 2007275970Scystruct testcase_t dns_testcases[] = { 2008275970Scy DNS_LEGACY(server, TT_FORK|TT_NEED_BASE), 2009275970Scy DNS_LEGACY(gethostbyname, TT_FORK|TT_NEED_BASE|TT_NEED_DNS|TT_OFF_BY_DEFAULT), 2010275970Scy DNS_LEGACY(gethostbyname6, TT_FORK|TT_NEED_BASE|TT_NEED_DNS|TT_OFF_BY_DEFAULT), 2011275970Scy DNS_LEGACY(gethostbyaddr, TT_FORK|TT_NEED_BASE|TT_NEED_DNS|TT_OFF_BY_DEFAULT), 2012275970Scy { "resolve_reverse", dns_resolve_reverse, TT_FORK|TT_OFF_BY_DEFAULT, NULL, NULL }, 2013275970Scy { "search", dns_search_test, TT_FORK|TT_NEED_BASE, &basic_setup, NULL }, 2014275970Scy { "search_cancel", dns_search_cancel_test, 2015275970Scy TT_FORK|TT_NEED_BASE, &basic_setup, NULL }, 2016275970Scy { "retry", dns_retry_test, TT_FORK|TT_NEED_BASE|TT_NO_LOGS, &basic_setup, NULL }, 2017290000Sglebius { "retry_disable_when_inactive", dns_retry_disable_when_inactive_test, 2018290000Sglebius TT_FORK|TT_NEED_BASE|TT_NO_LOGS, &basic_setup, NULL }, 2019275970Scy { "reissue", dns_reissue_test, TT_FORK|TT_NEED_BASE|TT_NO_LOGS, &basic_setup, NULL }, 2020290000Sglebius { "reissue_disable_when_inactive", dns_reissue_disable_when_inactive_test, 2021290000Sglebius TT_FORK|TT_NEED_BASE|TT_NO_LOGS, &basic_setup, NULL }, 2022275970Scy { "inflight", dns_inflight_test, TT_FORK|TT_NEED_BASE, &basic_setup, NULL }, 2023275970Scy { "bufferevent_connect_hostname", test_bufferevent_connect_hostname, 2024275970Scy TT_FORK|TT_NEED_BASE, &basic_setup, NULL }, 2025290000Sglebius { "disable_when_inactive", dns_disable_when_inactive_test, 2026290000Sglebius TT_FORK|TT_NEED_BASE, &basic_setup, NULL }, 2027290000Sglebius { "disable_when_inactive_no_ns", dns_disable_when_inactive_no_ns_test, 2028290000Sglebius TT_FORK|TT_NEED_BASE, &basic_setup, NULL }, 2029275970Scy 2030275970Scy { "getaddrinfo_async", test_getaddrinfo_async, 2031275970Scy TT_FORK|TT_NEED_BASE, &basic_setup, (char*)"" }, 2032275970Scy { "getaddrinfo_cancel_stress", test_getaddrinfo_async_cancel_stress, 2033275970Scy TT_FORK, NULL, NULL }, 2034275970Scy 2035275970Scy#ifdef EVENT_SET_MEM_FUNCTIONS_IMPLEMENTED 2036275970Scy { "leak_shutdown", test_dbg_leak_shutdown, TT_FORK, &testleak_funcs, NULL }, 2037275970Scy { "leak_cancel", test_dbg_leak_cancel, TT_FORK, &testleak_funcs, NULL }, 2038275970Scy 2039275970Scy { "leak_resume", test_dbg_leak_resume_, TT_FORK, &testleak_funcs, NULL }, 2040275970Scy { "leak_cancel_and_resume", test_dbg_leak_cancel_and_resume_, 2041275970Scy TT_FORK, &testleak_funcs, NULL }, 2042275970Scy { "leak_resume_send_err", test_dbg_leak_resume_send_err_, 2043275970Scy TT_FORK, &testleak_funcs, NULL }, 2044275970Scy { "leak_cancel_and_resume_send_err", test_dbg_leak_cancel_and_resume_send_err_, 2045275970Scy TT_FORK, &testleak_funcs, NULL }, 2046275970Scy#endif 2047275970Scy 2048275970Scy END_OF_TESTCASES 2049275970Scy}; 2050275970Scy 2051