910df98b0efcbe8380b952887f4071051cc39a25Michael Graff/*
0c27b3fe77ac1d5094ba3521e8142d9e7973133fMark Andrews * Copyright (C) 1999-2001, 2004, 2005, 2007, 2009, 2011-2013, 2015, 2016 Internet Systems Consortium, Inc. ("ISC")
40f53fa8d9c6a4fc38c0014495e7a42b08f52481David Lawrence *
0c27b3fe77ac1d5094ba3521e8142d9e7973133fMark Andrews * This Source Code Form is subject to the terms of the Mozilla Public
0c27b3fe77ac1d5094ba3521e8142d9e7973133fMark Andrews * License, v. 2.0. If a copy of the MPL was not distributed with this
0c27b3fe77ac1d5094ba3521e8142d9e7973133fMark Andrews * file, You can obtain one at http://mozilla.org/MPL/2.0/.
910df98b0efcbe8380b952887f4071051cc39a25Michael Graff */
910df98b0efcbe8380b952887f4071051cc39a25Michael Graff
4e68c7c87c30c28490c92937adc112dca5b5d5aeAutomatic Updater/* $Id: adb_test.c,v 1.73 2011/08/30 23:46:51 tbox Exp $ */
ab023a65562e62b85a824509d829b6fad87e00b1Rob Austein
ab023a65562e62b85a824509d829b6fad87e00b1Rob Austein/*! \file */
9c3531d72aeaad6c5f01efe6a1c82023e1379e4dDavid Lawrence
910df98b0efcbe8380b952887f4071051cc39a25Michael Graff#include <config.h>
910df98b0efcbe8380b952887f4071051cc39a25Michael Graff
910df98b0efcbe8380b952887f4071051cc39a25Michael Graff#include <stdlib.h>
142784f574e0b63e8bbcccb762eb8727ac7c76feBrian Wellington#include <string.h>
910df98b0efcbe8380b952887f4071051cc39a25Michael Graff#include <unistd.h>
910df98b0efcbe8380b952887f4071051cc39a25Michael Graff
86944a4c8002e80ae9b6eb5a5e29b797879be45fMichael Graff#include <isc/app.h>
910df98b0efcbe8380b952887f4071051cc39a25Michael Graff#include <isc/buffer.h>
9297259c7abecc78470fdeca173c101137e4b5bbMark Andrews#include <isc/entropy.h>
9297259c7abecc78470fdeca173c101137e4b5bbMark Andrews#include <isc/hash.h>
3759f10fc543747668b1ca4b4671f35b0dea8445Francis Dupont#include <isc/print.h>
9297259c7abecc78470fdeca173c101137e4b5bbMark Andrews#include <isc/socket.h>
910df98b0efcbe8380b952887f4071051cc39a25Michael Graff#include <isc/task.h>
efc8a09e19dfcfafa92fd2ad113073a4f5295e9bMichael Graff#include <isc/timer.h>
364a82f7c25b62967678027043425201a5e5171aBob Halley#include <isc/util.h>
910df98b0efcbe8380b952887f4071051cc39a25Michael Graff
e45d323a2a0f4ca08d4b139546e60a5fa7bd3f0cMichael Graff#include <dns/adb.h>
fd15c8e32ed0c1cfd3ed737858a81966e7fbaeacAndreas Gustafsson#include <dns/cache.h>
558ab0f6a8046499bfe3e39ea4789036313b72b3Michael Graff#include <dns/dispatch.h>
efc8a09e19dfcfafa92fd2ad113073a4f5295e9bMichael Graff#include <dns/db.h>
41ac1e406f1be4ec2eba2d8bae673e762d54ea74Andreas Gustafsson#include <dns/log.h>
efa4ebbff3c9f6f38ab8b55540fb696243c1172cAndreas Gustafsson#include <dns/rootns.h>
c90f5e8d1edbd5c277f2ee320167a12a30ba7c7bMichael Graff#include <dns/result.h>
910df98b0efcbe8380b952887f4071051cc39a25Michael Graff
970cccf46e0f949a1a9edbcd8302dd2a112b43c2Michael Grafftypedef struct client client_t;
970cccf46e0f949a1a9edbcd8302dd2a112b43c2Michael Graffstruct client {
970cccf46e0f949a1a9edbcd8302dd2a112b43c2Michael Graff dns_name_t name;
49df2ea9870b89427c847864ba18599f4bfa467dAndreas Gustafsson const char *target;
970cccf46e0f949a1a9edbcd8302dd2a112b43c2Michael Graff ISC_LINK(client_t) link;
c803787146cadcb2d7e10cbf4491f3be513dfa1aMichael Graff dns_adbfind_t *find;
970cccf46e0f949a1a9edbcd8302dd2a112b43c2Michael Graff};
970cccf46e0f949a1a9edbcd8302dd2a112b43c2Michael Graff
9297259c7abecc78470fdeca173c101137e4b5bbMark Andrewsstatic isc_mem_t *mctx = NULL;
9297259c7abecc78470fdeca173c101137e4b5bbMark Andrewsstatic isc_entropy_t *ectx = NULL;
9297259c7abecc78470fdeca173c101137e4b5bbMark Andrewsstatic isc_mempool_t *cmp;
9297259c7abecc78470fdeca173c101137e4b5bbMark Andrewsstatic isc_log_t *lctx;
9297259c7abecc78470fdeca173c101137e4b5bbMark Andrewsstatic isc_logconfig_t *lcfg;
9297259c7abecc78470fdeca173c101137e4b5bbMark Andrewsstatic isc_taskmgr_t *taskmgr;
9297259c7abecc78470fdeca173c101137e4b5bbMark Andrewsstatic isc_socketmgr_t *socketmgr;
9297259c7abecc78470fdeca173c101137e4b5bbMark Andrewsstatic isc_timermgr_t *timermgr;
9297259c7abecc78470fdeca173c101137e4b5bbMark Andrewsstatic dns_dispatchmgr_t *dispatchmgr;
9297259c7abecc78470fdeca173c101137e4b5bbMark Andrewsstatic isc_task_t *t1, *t2;
9297259c7abecc78470fdeca173c101137e4b5bbMark Andrewsstatic dns_view_t *view;
9297259c7abecc78470fdeca173c101137e4b5bbMark Andrewsstatic dns_db_t *rootdb;
9297259c7abecc78470fdeca173c101137e4b5bbMark Andrewsstatic ISC_LIST(client_t) clients;
9297259c7abecc78470fdeca173c101137e4b5bbMark Andrewsstatic isc_mutex_t client_lock;
9297259c7abecc78470fdeca173c101137e4b5bbMark Andrewsstatic isc_stdtime_t now;
9297259c7abecc78470fdeca173c101137e4b5bbMark Andrewsstatic dns_adb_t *adb;
970cccf46e0f949a1a9edbcd8302dd2a112b43c2Michael Graff
76c8294c81fb48b1da6e1fc5b83322a4cedb8e58Andreas Gustafssonstatic void
76c8294c81fb48b1da6e1fc5b83322a4cedb8e58Andreas Gustafssoncheck_result(isc_result_t result, const char *format, ...)
76c8294c81fb48b1da6e1fc5b83322a4cedb8e58Andreas Gustafsson ISC_FORMAT_PRINTF(2, 3);
76c8294c81fb48b1da6e1fc5b83322a4cedb8e58Andreas Gustafsson
910df98b0efcbe8380b952887f4071051cc39a25Michael Graffstatic void
1b6d529cb5ee0ad44f8518e1b8c2cbca54bbdf18David Lawrencecheck_result(isc_result_t result, const char *format, ...) {
910df98b0efcbe8380b952887f4071051cc39a25Michael Graff va_list args;
910df98b0efcbe8380b952887f4071051cc39a25Michael Graff
970cccf46e0f949a1a9edbcd8302dd2a112b43c2Michael Graff if (result == ISC_R_SUCCESS)
970cccf46e0f949a1a9edbcd8302dd2a112b43c2Michael Graff return;
970cccf46e0f949a1a9edbcd8302dd2a112b43c2Michael Graff
40f53fa8d9c6a4fc38c0014495e7a42b08f52481David Lawrence va_start(args, format);
910df98b0efcbe8380b952887f4071051cc39a25Michael Graff vfprintf(stderr, format, args);
910df98b0efcbe8380b952887f4071051cc39a25Michael Graff va_end(args);
306a93530536f05edfb477cac1c2667d90129a8fMichael Graff fprintf(stderr, ": %s\n", isc_result_totext(result));
910df98b0efcbe8380b952887f4071051cc39a25Michael Graff exit(1);
910df98b0efcbe8380b952887f4071051cc39a25Michael Graff}
910df98b0efcbe8380b952887f4071051cc39a25Michael Graff
1a69a1a78cfaa86f3b68bbc965232b7876d4da2aDavid Lawrencestatic client_t *
1a69a1a78cfaa86f3b68bbc965232b7876d4da2aDavid Lawrencenew_client(void) {
970cccf46e0f949a1a9edbcd8302dd2a112b43c2Michael Graff client_t *client;
970cccf46e0f949a1a9edbcd8302dd2a112b43c2Michael Graff
2bcb48cfcae36398454c98e40c563e2cde748e07Michael Graff client = isc_mempool_get(cmp);
970cccf46e0f949a1a9edbcd8302dd2a112b43c2Michael Graff INSIST(client != NULL);
970cccf46e0f949a1a9edbcd8302dd2a112b43c2Michael Graff dns_name_init(&client->name, NULL);
970cccf46e0f949a1a9edbcd8302dd2a112b43c2Michael Graff ISC_LINK_INIT(client, link);
c803787146cadcb2d7e10cbf4491f3be513dfa1aMichael Graff client->find = NULL;
970cccf46e0f949a1a9edbcd8302dd2a112b43c2Michael Graff
970cccf46e0f949a1a9edbcd8302dd2a112b43c2Michael Graff return (client);
970cccf46e0f949a1a9edbcd8302dd2a112b43c2Michael Graff}
970cccf46e0f949a1a9edbcd8302dd2a112b43c2Michael Graff
1a69a1a78cfaa86f3b68bbc965232b7876d4da2aDavid Lawrencestatic void
1a69a1a78cfaa86f3b68bbc965232b7876d4da2aDavid Lawrencefree_client(client_t **c) {
970cccf46e0f949a1a9edbcd8302dd2a112b43c2Michael Graff client_t *client;
970cccf46e0f949a1a9edbcd8302dd2a112b43c2Michael Graff
970cccf46e0f949a1a9edbcd8302dd2a112b43c2Michael Graff INSIST(c != NULL);
970cccf46e0f949a1a9edbcd8302dd2a112b43c2Michael Graff client = *c;
970cccf46e0f949a1a9edbcd8302dd2a112b43c2Michael Graff *c = NULL;
970cccf46e0f949a1a9edbcd8302dd2a112b43c2Michael Graff INSIST(client != NULL);
970cccf46e0f949a1a9edbcd8302dd2a112b43c2Michael Graff dns_name_free(&client->name, mctx);
970cccf46e0f949a1a9edbcd8302dd2a112b43c2Michael Graff INSIST(!ISC_LINK_LINKED(client, link));
c803787146cadcb2d7e10cbf4491f3be513dfa1aMichael Graff INSIST(client->find == NULL);
970cccf46e0f949a1a9edbcd8302dd2a112b43c2Michael Graff
2bcb48cfcae36398454c98e40c563e2cde748e07Michael Graff isc_mempool_put(cmp, client);
970cccf46e0f949a1a9edbcd8302dd2a112b43c2Michael Graff}
970cccf46e0f949a1a9edbcd8302dd2a112b43c2Michael Graff
970cccf46e0f949a1a9edbcd8302dd2a112b43c2Michael Graffstatic inline void
1a69a1a78cfaa86f3b68bbc965232b7876d4da2aDavid LawrenceCLOCK(void) {
970cccf46e0f949a1a9edbcd8302dd2a112b43c2Michael Graff RUNTIME_CHECK(isc_mutex_lock(&client_lock) == ISC_R_SUCCESS);
970cccf46e0f949a1a9edbcd8302dd2a112b43c2Michael Graff}
970cccf46e0f949a1a9edbcd8302dd2a112b43c2Michael Graff
970cccf46e0f949a1a9edbcd8302dd2a112b43c2Michael Graffstatic inline void
1a69a1a78cfaa86f3b68bbc965232b7876d4da2aDavid LawrenceCUNLOCK(void) {
970cccf46e0f949a1a9edbcd8302dd2a112b43c2Michael Graff RUNTIME_CHECK(isc_mutex_unlock(&client_lock) == ISC_R_SUCCESS);
970cccf46e0f949a1a9edbcd8302dd2a112b43c2Michael Graff}
efc8a09e19dfcfafa92fd2ad113073a4f5295e9bMichael Graff
86944a4c8002e80ae9b6eb5a5e29b797879be45fMichael Graffstatic void
1a69a1a78cfaa86f3b68bbc965232b7876d4da2aDavid Lawrencelookup_callback(isc_task_t *task, isc_event_t *ev) {
2bcb48cfcae36398454c98e40c563e2cde748e07Michael Graff client_t *client;
86944a4c8002e80ae9b6eb5a5e29b797879be45fMichael Graff
e44487bfc23599b6b240e09d83d1c862fecfcc82Michael Graff client = ev->ev_arg;
e44487bfc23599b6b240e09d83d1c862fecfcc82Michael Graff INSIST(client->find == ev->ev_sender);
86944a4c8002e80ae9b6eb5a5e29b797879be45fMichael Graff
306a93530536f05edfb477cac1c2667d90129a8fMichael Graff printf("NAME %s:\n\tTask %p got event %p type %08x from %p, client %p\n\terr4: %s err6: %s\n",
306a93530536f05edfb477cac1c2667d90129a8fMichael Graff client->target,
306a93530536f05edfb477cac1c2667d90129a8fMichael Graff task, ev, ev->ev_type, client->find, client,
306a93530536f05edfb477cac1c2667d90129a8fMichael Graff isc_result_totext(client->find->result_v4),
306a93530536f05edfb477cac1c2667d90129a8fMichael Graff isc_result_totext(client->find->result_v6));
897c9ddb4d745b2bfecf98b17e5487bb6656299aMichael Graff
897c9ddb4d745b2bfecf98b17e5487bb6656299aMichael Graff isc_event_free(&ev);
306a93530536f05edfb477cac1c2667d90129a8fMichael Graff ev = NULL;
2bcb48cfcae36398454c98e40c563e2cde748e07Michael Graff
970cccf46e0f949a1a9edbcd8302dd2a112b43c2Michael Graff CLOCK();
970cccf46e0f949a1a9edbcd8302dd2a112b43c2Michael Graff
c803787146cadcb2d7e10cbf4491f3be513dfa1aMichael Graff dns_adb_dumpfind(client->find, stderr);
c803787146cadcb2d7e10cbf4491f3be513dfa1aMichael Graff dns_adb_destroyfind(&client->find);
2bcb48cfcae36398454c98e40c563e2cde748e07Michael Graff
2bcb48cfcae36398454c98e40c563e2cde748e07Michael Graff ISC_LIST_UNLINK(clients, client, link);
2bcb48cfcae36398454c98e40c563e2cde748e07Michael Graff free_client(&client);
970cccf46e0f949a1a9edbcd8302dd2a112b43c2Michael Graff
970cccf46e0f949a1a9edbcd8302dd2a112b43c2Michael Graff CUNLOCK();
86944a4c8002e80ae9b6eb5a5e29b797879be45fMichael Graff}
86944a4c8002e80ae9b6eb5a5e29b797879be45fMichael Graff
1a69a1a78cfaa86f3b68bbc965232b7876d4da2aDavid Lawrencestatic void
1a69a1a78cfaa86f3b68bbc965232b7876d4da2aDavid Lawrencecreate_managers(void) {
efc8a09e19dfcfafa92fd2ad113073a4f5295e9bMichael Graff isc_result_t result;
efc8a09e19dfcfafa92fd2ad113073a4f5295e9bMichael Graff
3ac63b472022ff92691d1fe69ac715a729671965Michael Graff taskmgr = NULL;
306a93530536f05edfb477cac1c2667d90129a8fMichael Graff result = isc_taskmgr_create(mctx, 5, 0, &taskmgr);
efc8a09e19dfcfafa92fd2ad113073a4f5295e9bMichael Graff check_result(result, "isc_taskmgr_create");
efc8a09e19dfcfafa92fd2ad113073a4f5295e9bMichael Graff
efc8a09e19dfcfafa92fd2ad113073a4f5295e9bMichael Graff timermgr = NULL;
efc8a09e19dfcfafa92fd2ad113073a4f5295e9bMichael Graff result = isc_timermgr_create(mctx, &timermgr);
efc8a09e19dfcfafa92fd2ad113073a4f5295e9bMichael Graff check_result(result, "isc_timermgr_create");
efc8a09e19dfcfafa92fd2ad113073a4f5295e9bMichael Graff
efc8a09e19dfcfafa92fd2ad113073a4f5295e9bMichael Graff socketmgr = NULL;
efc8a09e19dfcfafa92fd2ad113073a4f5295e9bMichael Graff result = isc_socketmgr_create(mctx, &socketmgr);
efc8a09e19dfcfafa92fd2ad113073a4f5295e9bMichael Graff check_result(result, "isc_socketmgr_create");
558ab0f6a8046499bfe3e39ea4789036313b72b3Michael Graff
558ab0f6a8046499bfe3e39ea4789036313b72b3Michael Graff dispatchmgr = NULL;
4e21e54a0394d0f87798620a95c1f675c0b0d09cMichael Graff result = dns_dispatchmgr_create(mctx, NULL, &dispatchmgr);
558ab0f6a8046499bfe3e39ea4789036313b72b3Michael Graff check_result(result, "dns_dispatchmgr_create");
efc8a09e19dfcfafa92fd2ad113073a4f5295e9bMichael Graff}
efc8a09e19dfcfafa92fd2ad113073a4f5295e9bMichael Graff
1a69a1a78cfaa86f3b68bbc965232b7876d4da2aDavid Lawrencestatic void
1a69a1a78cfaa86f3b68bbc965232b7876d4da2aDavid Lawrencecreate_view(void) {
fd15c8e32ed0c1cfd3ed737858a81966e7fbaeacAndreas Gustafsson dns_cache_t *cache;
efc8a09e19dfcfafa92fd2ad113073a4f5295e9bMichael Graff isc_result_t result;
efc8a09e19dfcfafa92fd2ad113073a4f5295e9bMichael Graff
efc8a09e19dfcfafa92fd2ad113073a4f5295e9bMichael Graff /*
efc8a09e19dfcfafa92fd2ad113073a4f5295e9bMichael Graff * View.
efc8a09e19dfcfafa92fd2ad113073a4f5295e9bMichael Graff */
efc8a09e19dfcfafa92fd2ad113073a4f5295e9bMichael Graff view = NULL;
efc8a09e19dfcfafa92fd2ad113073a4f5295e9bMichael Graff result = dns_view_create(mctx, dns_rdataclass_in, "_default", &view);
efc8a09e19dfcfafa92fd2ad113073a4f5295e9bMichael Graff check_result(result, "dns_view_create");
efc8a09e19dfcfafa92fd2ad113073a4f5295e9bMichael Graff
efc8a09e19dfcfafa92fd2ad113073a4f5295e9bMichael Graff /*
efc8a09e19dfcfafa92fd2ad113073a4f5295e9bMichael Graff * Cache.
efc8a09e19dfcfafa92fd2ad113073a4f5295e9bMichael Graff */
fd15c8e32ed0c1cfd3ed737858a81966e7fbaeacAndreas Gustafsson cache = NULL;
fd15c8e32ed0c1cfd3ed737858a81966e7fbaeacAndreas Gustafsson result = dns_cache_create(mctx, taskmgr, timermgr, dns_rdataclass_in,
fd15c8e32ed0c1cfd3ed737858a81966e7fbaeacAndreas Gustafsson "rbt", 0, NULL, &cache);
fd15c8e32ed0c1cfd3ed737858a81966e7fbaeacAndreas Gustafsson check_result(result, "dns_cache_create");
fd15c8e32ed0c1cfd3ed737858a81966e7fbaeacAndreas Gustafsson dns_view_setcache(view, cache);
fd15c8e32ed0c1cfd3ed737858a81966e7fbaeacAndreas Gustafsson dns_cache_detach(&cache);
efc8a09e19dfcfafa92fd2ad113073a4f5295e9bMichael Graff
d98c74e2ec5b96bd22aa4ed6d893e8993787493bMichael Graff {
d98c74e2ec5b96bd22aa4ed6d893e8993787493bMichael Graff unsigned int attrs;
d98c74e2ec5b96bd22aa4ed6d893e8993787493bMichael Graff isc_sockaddr_t any4, any6;
d98c74e2ec5b96bd22aa4ed6d893e8993787493bMichael Graff dns_dispatch_t *disp4 = NULL;
40f53fa8d9c6a4fc38c0014495e7a42b08f52481David Lawrence dns_dispatch_t *disp6 = NULL;
d98c74e2ec5b96bd22aa4ed6d893e8993787493bMichael Graff
d98c74e2ec5b96bd22aa4ed6d893e8993787493bMichael Graff isc_sockaddr_any(&any4);
d98c74e2ec5b96bd22aa4ed6d893e8993787493bMichael Graff isc_sockaddr_any6(&any6);
d98c74e2ec5b96bd22aa4ed6d893e8993787493bMichael Graff
d98c74e2ec5b96bd22aa4ed6d893e8993787493bMichael Graff attrs = DNS_DISPATCHATTR_IPV4 | DNS_DISPATCHATTR_UDP;
d98c74e2ec5b96bd22aa4ed6d893e8993787493bMichael Graff RUNTIME_CHECK(dns_dispatch_getudp(dispatchmgr, socketmgr,
67adc03ef81fb610f8df093b17f55275ee816754Evan Hunt taskmgr, &any4,
67adc03ef81fb610f8df093b17f55275ee816754Evan Hunt 512, 6, 1024, 17, 19,
67adc03ef81fb610f8df093b17f55275ee816754Evan Hunt attrs, attrs, &disp4)
d98c74e2ec5b96bd22aa4ed6d893e8993787493bMichael Graff == ISC_R_SUCCESS);
d98c74e2ec5b96bd22aa4ed6d893e8993787493bMichael Graff INSIST(disp4 != NULL);
d98c74e2ec5b96bd22aa4ed6d893e8993787493bMichael Graff
d98c74e2ec5b96bd22aa4ed6d893e8993787493bMichael Graff attrs = DNS_DISPATCHATTR_IPV6 | DNS_DISPATCHATTR_UDP;
d98c74e2ec5b96bd22aa4ed6d893e8993787493bMichael Graff RUNTIME_CHECK(dns_dispatch_getudp(dispatchmgr, socketmgr,
67adc03ef81fb610f8df093b17f55275ee816754Evan Hunt taskmgr, &any6,
67adc03ef81fb610f8df093b17f55275ee816754Evan Hunt 512, 6, 1024, 17, 19,
67adc03ef81fb610f8df093b17f55275ee816754Evan Hunt attrs, attrs, &disp6)
d98c74e2ec5b96bd22aa4ed6d893e8993787493bMichael Graff == ISC_R_SUCCESS);
d98c74e2ec5b96bd22aa4ed6d893e8993787493bMichael Graff INSIST(disp6 != NULL);
d98c74e2ec5b96bd22aa4ed6d893e8993787493bMichael Graff
4e8fe357a619ae2b789b15df7e3d6abf782b4a71Evan Hunt RUNTIME_CHECK(dns_view_createresolver(view, taskmgr, 10, 1,
d98c74e2ec5b96bd22aa4ed6d893e8993787493bMichael Graff socketmgr,
d98c74e2ec5b96bd22aa4ed6d893e8993787493bMichael Graff timermgr, 0,
d98c74e2ec5b96bd22aa4ed6d893e8993787493bMichael Graff dispatchmgr,
d98c74e2ec5b96bd22aa4ed6d893e8993787493bMichael Graff disp4, disp6) ==
d98c74e2ec5b96bd22aa4ed6d893e8993787493bMichael Graff ISC_R_SUCCESS);
d98c74e2ec5b96bd22aa4ed6d893e8993787493bMichael Graff }
efc8a09e19dfcfafa92fd2ad113073a4f5295e9bMichael Graff
efa4ebbff3c9f6f38ab8b55540fb696243c1172cAndreas Gustafsson rootdb = NULL;
e592dd7c344052ee51eb707cd744b48b34f4c74eBob Halley result = dns_rootns_create(mctx, dns_rdataclass_in, NULL, &rootdb);
efa4ebbff3c9f6f38ab8b55540fb696243c1172cAndreas Gustafsson check_result(result, "dns_rootns_create()");
970cccf46e0f949a1a9edbcd8302dd2a112b43c2Michael Graff dns_view_sethints(view, rootdb);
36ca83769dbba29a3d8670eef9acd95c7a71a7f6Michael Graff dns_db_detach(&rootdb);
efc8a09e19dfcfafa92fd2ad113073a4f5295e9bMichael Graff
efc8a09e19dfcfafa92fd2ad113073a4f5295e9bMichael Graff dns_view_freeze(view);
efc8a09e19dfcfafa92fd2ad113073a4f5295e9bMichael Graff}
efc8a09e19dfcfafa92fd2ad113073a4f5295e9bMichael Graff
1a69a1a78cfaa86f3b68bbc965232b7876d4da2aDavid Lawrencestatic void
1b6d529cb5ee0ad44f8518e1b8c2cbca54bbdf18David Lawrencelookup(const char *target) {
970cccf46e0f949a1a9edbcd8302dd2a112b43c2Michael Graff dns_name_t name;
970cccf46e0f949a1a9edbcd8302dd2a112b43c2Michael Graff unsigned char namedata[256];
970cccf46e0f949a1a9edbcd8302dd2a112b43c2Michael Graff client_t *client;
910df98b0efcbe8380b952887f4071051cc39a25Michael Graff isc_buffer_t t, namebuf;
970cccf46e0f949a1a9edbcd8302dd2a112b43c2Michael Graff isc_result_t result;
528829aa8ad69238e674cd81078bc14d4199691bMichael Graff unsigned int options;
970cccf46e0f949a1a9edbcd8302dd2a112b43c2Michael Graff
970cccf46e0f949a1a9edbcd8302dd2a112b43c2Michael Graff INSIST(target != NULL);
970cccf46e0f949a1a9edbcd8302dd2a112b43c2Michael Graff
970cccf46e0f949a1a9edbcd8302dd2a112b43c2Michael Graff client = new_client();
ecf5a60f1e7d8886804becb5fafc765d18175edcMark Andrews isc_buffer_constinit(&t, target, strlen(target));
970cccf46e0f949a1a9edbcd8302dd2a112b43c2Michael Graff isc_buffer_add(&t, strlen(target));
6e49e91bd08778d7eae45a2229dcf41ed97cc636David Lawrence isc_buffer_init(&namebuf, namedata, sizeof(namedata));
970cccf46e0f949a1a9edbcd8302dd2a112b43c2Michael Graff dns_name_init(&name, NULL);
307d2084502eddc7ce921e5ce439aec3531d90e0Tatuya JINMEI 神明達哉 result = dns_name_fromtext(&name, &t, dns_rootname, 0, &namebuf);
970cccf46e0f949a1a9edbcd8302dd2a112b43c2Michael Graff check_result(result, "dns_name_fromtext %s", target);
970cccf46e0f949a1a9edbcd8302dd2a112b43c2Michael Graff
970cccf46e0f949a1a9edbcd8302dd2a112b43c2Michael Graff result = dns_name_dup(&name, mctx, &client->name);
970cccf46e0f949a1a9edbcd8302dd2a112b43c2Michael Graff check_result(result, "dns_name_dup %s", target);
970cccf46e0f949a1a9edbcd8302dd2a112b43c2Michael Graff
cebd4498636d3d480f6f2a7aa2eb72bd2ed64010Michael Graff options = 0;
cebd4498636d3d480f6f2a7aa2eb72bd2ed64010Michael Graff options |= DNS_ADBFIND_INET;
cebd4498636d3d480f6f2a7aa2eb72bd2ed64010Michael Graff options |= DNS_ADBFIND_INET6;
cebd4498636d3d480f6f2a7aa2eb72bd2ed64010Michael Graff options |= DNS_ADBFIND_WANTEVENT;
1a0e33bc2044e1902493111db14cbf793083ac47Michael Graff options |= DNS_ADBFIND_HINTOK;
1a0e33bc2044e1902493111db14cbf793083ac47Michael Graff options |= DNS_ADBFIND_GLUEOK;
897c9ddb4d745b2bfecf98b17e5487bb6656299aMichael Graff result = dns_adb_createfind(adb, t2, lookup_callback, client,
9297259c7abecc78470fdeca173c101137e4b5bbMark Andrews &client->name, dns_rootname, 0, options,
d98c74e2ec5b96bd22aa4ed6d893e8993787493bMichael Graff now, NULL, view->dstport, &client->find);
837633db56089ad135a75282d796f0f0e92a4fadMark Andrews if (result != ISC_R_SUCCESS)
837633db56089ad135a75282d796f0f0e92a4fadMark Andrews printf("DNS_ADB_CREATEFIND -> %s\n", dns_result_totext(result));
c803787146cadcb2d7e10cbf4491f3be513dfa1aMichael Graff dns_adb_dumpfind(client->find, stderr);
970cccf46e0f949a1a9edbcd8302dd2a112b43c2Michael Graff
306a93530536f05edfb477cac1c2667d90129a8fMichael Graff if ((client->find->options & DNS_ADBFIND_WANTEVENT) != 0) {
306a93530536f05edfb477cac1c2667d90129a8fMichael Graff client->target = target;
2bcb48cfcae36398454c98e40c563e2cde748e07Michael Graff ISC_LIST_APPEND(clients, client, link);
306a93530536f05edfb477cac1c2667d90129a8fMichael Graff } else {
306a93530536f05edfb477cac1c2667d90129a8fMichael Graff printf("NAME %s: err4 %s, err6 %s\n",
306a93530536f05edfb477cac1c2667d90129a8fMichael Graff target, isc_result_totext(client->find->result_v4),
306a93530536f05edfb477cac1c2667d90129a8fMichael Graff isc_result_totext(client->find->result_v6));
306a93530536f05edfb477cac1c2667d90129a8fMichael Graff
c803787146cadcb2d7e10cbf4491f3be513dfa1aMichael Graff dns_adb_destroyfind(&client->find);
2bcb48cfcae36398454c98e40c563e2cde748e07Michael Graff free_client(&client);
970cccf46e0f949a1a9edbcd8302dd2a112b43c2Michael Graff }
970cccf46e0f949a1a9edbcd8302dd2a112b43c2Michael Graff}
970cccf46e0f949a1a9edbcd8302dd2a112b43c2Michael Graff
970cccf46e0f949a1a9edbcd8302dd2a112b43c2Michael Graffint
1a69a1a78cfaa86f3b68bbc965232b7876d4da2aDavid Lawrencemain(int argc, char **argv) {
970cccf46e0f949a1a9edbcd8302dd2a112b43c2Michael Graff isc_result_t result;
ff9bb3fc5453bbf310b67c560fbf04a5c0fb60daMichael Graff isc_logdestination_t destination;
910df98b0efcbe8380b952887f4071051cc39a25Michael Graff
1a69a1a78cfaa86f3b68bbc965232b7876d4da2aDavid Lawrence UNUSED(argc);
1a69a1a78cfaa86f3b68bbc965232b7876d4da2aDavid Lawrence UNUSED(argv);
da547174e2b7beb6d6119d58197ad0bc85b91179Michael Graff
910df98b0efcbe8380b952887f4071051cc39a25Michael Graff dns_result_register();
86944a4c8002e80ae9b6eb5a5e29b797879be45fMichael Graff result = isc_app_start();
86944a4c8002e80ae9b6eb5a5e29b797879be45fMichael Graff check_result(result, "isc_app_start()");
910df98b0efcbe8380b952887f4071051cc39a25Michael Graff
58aaab3687aac838542ee4ef65a9c094a5d34ab0Michael Graff isc_stdtime_get(&now);
f00d96a15cdd11e764437f9359e67328631caaeaMichael Graff
970cccf46e0f949a1a9edbcd8302dd2a112b43c2Michael Graff result = isc_mutex_init(&client_lock);
970cccf46e0f949a1a9edbcd8302dd2a112b43c2Michael Graff check_result(result, "isc_mutex_init(&client_lock)");
970cccf46e0f949a1a9edbcd8302dd2a112b43c2Michael Graff ISC_LIST_INIT(clients);
970cccf46e0f949a1a9edbcd8302dd2a112b43c2Michael Graff
910df98b0efcbe8380b952887f4071051cc39a25Michael Graff /*
910df98b0efcbe8380b952887f4071051cc39a25Michael Graff * EVERYTHING needs a memory context.
910df98b0efcbe8380b952887f4071051cc39a25Michael Graff */
910df98b0efcbe8380b952887f4071051cc39a25Michael Graff RUNTIME_CHECK(isc_mem_create(0, 0, &mctx) == ISC_R_SUCCESS);
910df98b0efcbe8380b952887f4071051cc39a25Michael Graff
2bcb48cfcae36398454c98e40c563e2cde748e07Michael Graff cmp = NULL;
2bcb48cfcae36398454c98e40c563e2cde748e07Michael Graff RUNTIME_CHECK(isc_mempool_create(mctx, sizeof(client_t), &cmp)
2bcb48cfcae36398454c98e40c563e2cde748e07Michael Graff == ISC_R_SUCCESS);
2bcb48cfcae36398454c98e40c563e2cde748e07Michael Graff isc_mempool_setname(cmp, "adb test clients");
2bcb48cfcae36398454c98e40c563e2cde748e07Michael Graff
9297259c7abecc78470fdeca173c101137e4b5bbMark Andrews result = isc_entropy_create(mctx, &ectx);
9297259c7abecc78470fdeca173c101137e4b5bbMark Andrews check_result(result, "isc_entropy_create()");
9297259c7abecc78470fdeca173c101137e4b5bbMark Andrews result = isc_hash_create(mctx, ectx, DNS_NAME_MAXWIRE);
9297259c7abecc78470fdeca173c101137e4b5bbMark Andrews check_result(result, "isc_hash_create()");
9297259c7abecc78470fdeca173c101137e4b5bbMark Andrews
e198cb953c1a5bc189ae21dc3f8d622f5a08bc34Bob Halley result = isc_log_create(mctx, &lctx, &lcfg);
2bcb48cfcae36398454c98e40c563e2cde748e07Michael Graff check_result(result, "isc_log_create()");
c90f5e8d1edbd5c277f2ee320167a12a30ba7c7bMichael Graff isc_log_setcontext(lctx);
e198cb953c1a5bc189ae21dc3f8d622f5a08bc34Bob Halley dns_log_init(lctx);
c90f5e8d1edbd5c277f2ee320167a12a30ba7c7bMichael Graff dns_log_setcontext(lctx);
2bcb48cfcae36398454c98e40c563e2cde748e07Michael Graff
ff9bb3fc5453bbf310b67c560fbf04a5c0fb60daMichael Graff /*
ff9bb3fc5453bbf310b67c560fbf04a5c0fb60daMichael Graff * Create and install the default channel.
ff9bb3fc5453bbf310b67c560fbf04a5c0fb60daMichael Graff */
ff9bb3fc5453bbf310b67c560fbf04a5c0fb60daMichael Graff destination.file.stream = stderr;
ff9bb3fc5453bbf310b67c560fbf04a5c0fb60daMichael Graff destination.file.name = NULL;
ff9bb3fc5453bbf310b67c560fbf04a5c0fb60daMichael Graff destination.file.versions = ISC_LOG_ROLLNEVER;
ff9bb3fc5453bbf310b67c560fbf04a5c0fb60daMichael Graff destination.file.maximum_size = 0;
e198cb953c1a5bc189ae21dc3f8d622f5a08bc34Bob Halley result = isc_log_createchannel(lcfg, "_default",
ff9bb3fc5453bbf310b67c560fbf04a5c0fb60daMichael Graff ISC_LOG_TOFILEDESC,
ff9bb3fc5453bbf310b67c560fbf04a5c0fb60daMichael Graff ISC_LOG_DYNAMIC,
ff9bb3fc5453bbf310b67c560fbf04a5c0fb60daMichael Graff &destination, ISC_LOG_PRINTTIME);
ff9bb3fc5453bbf310b67c560fbf04a5c0fb60daMichael Graff check_result(result, "isc_log_createchannel()");
e198cb953c1a5bc189ae21dc3f8d622f5a08bc34Bob Halley result = isc_log_usechannel(lcfg, "_default", NULL, NULL);
ff9bb3fc5453bbf310b67c560fbf04a5c0fb60daMichael Graff check_result(result, "isc_log_usechannel()");
ff9bb3fc5453bbf310b67c560fbf04a5c0fb60daMichael Graff
ff9bb3fc5453bbf310b67c560fbf04a5c0fb60daMichael Graff /*
ff9bb3fc5453bbf310b67c560fbf04a5c0fb60daMichael Graff * Set the initial debug level.
ff9bb3fc5453bbf310b67c560fbf04a5c0fb60daMichael Graff */
d98c74e2ec5b96bd22aa4ed6d893e8993787493bMichael Graff isc_log_setdebuglevel(lctx, 2);
ff9bb3fc5453bbf310b67c560fbf04a5c0fb60daMichael Graff
efc8a09e19dfcfafa92fd2ad113073a4f5295e9bMichael Graff create_managers();
910df98b0efcbe8380b952887f4071051cc39a25Michael Graff
910df98b0efcbe8380b952887f4071051cc39a25Michael Graff t1 = NULL;
738b9aa3ded1ef724922d6695cb04ec2e721bdd1Bob Halley result = isc_task_create(taskmgr, 0, &t1);
910df98b0efcbe8380b952887f4071051cc39a25Michael Graff check_result(result, "isc_task_create t1");
910df98b0efcbe8380b952887f4071051cc39a25Michael Graff t2 = NULL;
738b9aa3ded1ef724922d6695cb04ec2e721bdd1Bob Halley result = isc_task_create(taskmgr, 0, &t2);
910df98b0efcbe8380b952887f4071051cc39a25Michael Graff check_result(result, "isc_task_create t2");
910df98b0efcbe8380b952887f4071051cc39a25Michael Graff
910df98b0efcbe8380b952887f4071051cc39a25Michael Graff printf("task 1 = %p\n", t1);
910df98b0efcbe8380b952887f4071051cc39a25Michael Graff printf("task 2 = %p\n", t2);
910df98b0efcbe8380b952887f4071051cc39a25Michael Graff
efc8a09e19dfcfafa92fd2ad113073a4f5295e9bMichael Graff create_view();
29f28fe573d4b3b318b3b026d567c1eb86738015Michael Graff
ff9bb3fc5453bbf310b67c560fbf04a5c0fb60daMichael Graff adb = view->adb;
910df98b0efcbe8380b952887f4071051cc39a25Michael Graff
29f28fe573d4b3b318b3b026d567c1eb86738015Michael Graff /*
970cccf46e0f949a1a9edbcd8302dd2a112b43c2Michael Graff * Lock the entire client list here. This will cause all events
970cccf46e0f949a1a9edbcd8302dd2a112b43c2Michael Graff * for found names to block as well.
29f28fe573d4b3b318b3b026d567c1eb86738015Michael Graff */
970cccf46e0f949a1a9edbcd8302dd2a112b43c2Michael Graff CLOCK();
cebd4498636d3d480f6f2a7aa2eb72bd2ed64010Michael Graff lookup("f.root-servers.net."); /* Should be in hints */
cebd4498636d3d480f6f2a7aa2eb72bd2ed64010Michael Graff lookup("www.iengines.com"); /* should fetch */
cebd4498636d3d480f6f2a7aa2eb72bd2ed64010Michael Graff lookup("www.isc.org"); /* should fetch */
cebd4498636d3d480f6f2a7aa2eb72bd2ed64010Michael Graff lookup("www.flame.org"); /* should fetch */
1c3bc66ada38236cc81c41b7174a9f0a872c9ab6Michael Graff lookup("kechara.flame.org."); /* should fetch */
1c3bc66ada38236cc81c41b7174a9f0a872c9ab6Michael Graff lookup("moghedien.flame.org."); /* should fetch */
1c3bc66ada38236cc81c41b7174a9f0a872c9ab6Michael Graff lookup("mailrelay.flame.org."); /* should fetch */
528829aa8ad69238e674cd81078bc14d4199691bMichael Graff lookup("ipv4v6.flame.org."); /* should fetch */
f942258b6380ba1f2c2f057a79ffc37bc3436488Michael Graff lookup("nonexistant.flame.org."); /* should fail to be found */
f942258b6380ba1f2c2f057a79ffc37bc3436488Michael Graff lookup("foobar.badns.flame.org."); /* should fail utterly (NS) */
f942258b6380ba1f2c2f057a79ffc37bc3436488Michael Graff lookup("i.root-servers.net."); /* Should be in hints */
306a93530536f05edfb477cac1c2667d90129a8fMichael Graff lookup("www.firstcard.com.");
306a93530536f05edfb477cac1c2667d90129a8fMichael Graff lookup("dns04.flame.org.");
f942258b6380ba1f2c2f057a79ffc37bc3436488Michael Graff CUNLOCK();
f942258b6380ba1f2c2f057a79ffc37bc3436488Michael Graff
1a0e33bc2044e1902493111db14cbf793083ac47Michael Graff sleep(10);
f942258b6380ba1f2c2f057a79ffc37bc3436488Michael Graff
f942258b6380ba1f2c2f057a79ffc37bc3436488Michael Graff dns_adb_dump(adb, stderr);
f942258b6380ba1f2c2f057a79ffc37bc3436488Michael Graff
1a0e33bc2044e1902493111db14cbf793083ac47Michael Graff sleep(10);
f942258b6380ba1f2c2f057a79ffc37bc3436488Michael Graff
f942258b6380ba1f2c2f057a79ffc37bc3436488Michael Graff CLOCK();
f942258b6380ba1f2c2f057a79ffc37bc3436488Michael Graff lookup("f.root-servers.net."); /* Should be in hints */
f942258b6380ba1f2c2f057a79ffc37bc3436488Michael Graff lookup("www.iengines.com"); /* should fetch */
f942258b6380ba1f2c2f057a79ffc37bc3436488Michael Graff lookup("www.isc.org"); /* should fetch */
f942258b6380ba1f2c2f057a79ffc37bc3436488Michael Graff lookup("www.flame.org"); /* should fetch */
f942258b6380ba1f2c2f057a79ffc37bc3436488Michael Graff lookup("kechara.flame.org."); /* should fetch */
f942258b6380ba1f2c2f057a79ffc37bc3436488Michael Graff lookup("moghedien.flame.org."); /* should fetch */
f942258b6380ba1f2c2f057a79ffc37bc3436488Michael Graff lookup("mailrelay.flame.org."); /* should fetch */
f942258b6380ba1f2c2f057a79ffc37bc3436488Michael Graff lookup("ipv4v6.flame.org."); /* should fetch */
f942258b6380ba1f2c2f057a79ffc37bc3436488Michael Graff lookup("nonexistant.flame.org."); /* should fail to be found */
f942258b6380ba1f2c2f057a79ffc37bc3436488Michael Graff lookup("foobar.badns.flame.org."); /* should fail utterly (NS) */
78854e02c127f31ab90f56da0531542004b45377Michael Graff lookup("i.root-servers.net."); /* Should be in hints */
970cccf46e0f949a1a9edbcd8302dd2a112b43c2Michael Graff CUNLOCK();
29f28fe573d4b3b318b3b026d567c1eb86738015Michael Graff
306a93530536f05edfb477cac1c2667d90129a8fMichael Graff sleep(20);
306a93530536f05edfb477cac1c2667d90129a8fMichael Graff
29f28fe573d4b3b318b3b026d567c1eb86738015Michael Graff dns_adb_dump(adb, stderr);
29f28fe573d4b3b318b3b026d567c1eb86738015Michael Graff
86944a4c8002e80ae9b6eb5a5e29b797879be45fMichael Graff isc_task_detach(&t1);
86944a4c8002e80ae9b6eb5a5e29b797879be45fMichael Graff isc_task_detach(&t2);
86944a4c8002e80ae9b6eb5a5e29b797879be45fMichael Graff
910df98b0efcbe8380b952887f4071051cc39a25Michael Graff isc_mem_stats(mctx, stdout);
30251e07d1705d1a85b0e1d5a969496e1aed612eMichael Graff dns_adb_dump(adb, stderr);
910df98b0efcbe8380b952887f4071051cc39a25Michael Graff
30251e07d1705d1a85b0e1d5a969496e1aed612eMichael Graff isc_app_run();
30251e07d1705d1a85b0e1d5a969496e1aed612eMichael Graff
2bcb48cfcae36398454c98e40c563e2cde748e07Michael Graff dns_adb_dump(adb, stderr);
1c3bc66ada38236cc81c41b7174a9f0a872c9ab6Michael Graff
d4d2a13916a114879763562db6a19b70b1444ec1Michael Graff dns_view_detach(&view);
ff9bb3fc5453bbf310b67c560fbf04a5c0fb60daMichael Graff adb = NULL;
efc8a09e19dfcfafa92fd2ad113073a4f5295e9bMichael Graff
837633db56089ad135a75282d796f0f0e92a4fadMark Andrews fprintf(stderr, "Destroying socket manager\n");
efc8a09e19dfcfafa92fd2ad113073a4f5295e9bMichael Graff isc_socketmgr_destroy(&socketmgr);
837633db56089ad135a75282d796f0f0e92a4fadMark Andrews fprintf(stderr, "Destroying timer manager\n");
efc8a09e19dfcfafa92fd2ad113073a4f5295e9bMichael Graff isc_timermgr_destroy(&timermgr);
a9ece9973c35d4d780338e89e288fb6a59575324Michael Graff
910df98b0efcbe8380b952887f4071051cc39a25Michael Graff fprintf(stderr, "Destroying task manager\n");
3ac63b472022ff92691d1fe69ac715a729671965Michael Graff isc_taskmgr_destroy(&taskmgr);
910df98b0efcbe8380b952887f4071051cc39a25Michael Graff
2bcb48cfcae36398454c98e40c563e2cde748e07Michael Graff isc_log_destroy(&lctx);
2bcb48cfcae36398454c98e40c563e2cde748e07Michael Graff
9297259c7abecc78470fdeca173c101137e4b5bbMark Andrews isc_hash_destroy();
9297259c7abecc78470fdeca173c101137e4b5bbMark Andrews isc_entropy_detach(&ectx);
9297259c7abecc78470fdeca173c101137e4b5bbMark Andrews
2bcb48cfcae36398454c98e40c563e2cde748e07Michael Graff isc_mempool_destroy(&cmp);
910df98b0efcbe8380b952887f4071051cc39a25Michael Graff isc_mem_stats(mctx, stdout);
910df98b0efcbe8380b952887f4071051cc39a25Michael Graff isc_mem_destroy(&mctx);
910df98b0efcbe8380b952887f4071051cc39a25Michael Graff
86944a4c8002e80ae9b6eb5a5e29b797879be45fMichael Graff isc_app_finish();
86944a4c8002e80ae9b6eb5a5e29b797879be45fMichael Graff
910df98b0efcbe8380b952887f4071051cc39a25Michael Graff return (0);
910df98b0efcbe8380b952887f4071051cc39a25Michael Graff}