"Fossies" - the Fresh Open Source Software Archive

Member "bind-9.12.3-P1/bin/tests/optional/adb_test.c" (7 Dec 2018, 10792 Bytes) of package /linux/misc/dns/bind9/9.12.3-P1/bind-9.12.3-P1.tar.gz:


As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) C and C++ source code syntax highlighting (style: standard) with prefixed line numbers and code folding option. Alternatively you can here view or download the uninterpreted source code file. See also the last Fossies "Diffs" side-by-side code changes report for "adb_test.c": 9.13.4_vs_9.13.5.

    1 /*
    2  * Copyright (C) Internet Systems Consortium, Inc. ("ISC")
    3  *
    4  * This Source Code Form is subject to the terms of the Mozilla Public
    5  * License, v. 2.0. If a copy of the MPL was not distributed with this
    6  * file, You can obtain one at http://mozilla.org/MPL/2.0/.
    7  *
    8  * See the COPYRIGHT file distributed with this work for additional
    9  * information regarding copyright ownership.
   10  */
   11 
   12 /*! \file */
   13 
   14 #include <config.h>
   15 
   16 #include <stdlib.h>
   17 #include <string.h>
   18 #include <unistd.h>
   19 
   20 #include <isc/app.h>
   21 #include <isc/buffer.h>
   22 #include <isc/entropy.h>
   23 #include <isc/hash.h>
   24 #include <isc/print.h>
   25 #include <isc/socket.h>
   26 #include <isc/task.h>
   27 #include <isc/timer.h>
   28 #include <isc/util.h>
   29 
   30 #include <dns/adb.h>
   31 #include <dns/cache.h>
   32 #include <dns/dispatch.h>
   33 #include <dns/db.h>
   34 #include <dns/log.h>
   35 #include <dns/rootns.h>
   36 #include <dns/result.h>
   37 
   38 typedef struct client client_t;
   39 struct client {
   40     dns_name_t      name;
   41     const char         *target;
   42     ISC_LINK(client_t)  link;
   43     dns_adbfind_t          *find;
   44 };
   45 
   46 static isc_mem_t *mctx = NULL;
   47 static isc_entropy_t *ectx = NULL;
   48 static isc_mempool_t *cmp;
   49 static isc_log_t *lctx;
   50 static isc_logconfig_t *lcfg;
   51 static isc_taskmgr_t *taskmgr;
   52 static isc_socketmgr_t *socketmgr;
   53 static isc_timermgr_t *timermgr;
   54 static dns_dispatchmgr_t *dispatchmgr;
   55 static isc_task_t *t1, *t2;
   56 static dns_view_t *view;
   57 static dns_db_t *rootdb;
   58 static ISC_LIST(client_t) clients;
   59 static isc_mutex_t client_lock;
   60 static isc_stdtime_t now;
   61 static dns_adb_t *adb;
   62 
   63 static void
   64 check_result(isc_result_t result, const char *format, ...)
   65      ISC_FORMAT_PRINTF(2, 3);
   66 
   67 static void
   68 check_result(isc_result_t result, const char *format, ...) {
   69     va_list args;
   70 
   71     if (result == ISC_R_SUCCESS)
   72         return;
   73 
   74     va_start(args, format);
   75     vfprintf(stderr, format, args);
   76     va_end(args);
   77     fprintf(stderr, ": %s\n", isc_result_totext(result));
   78     exit(1);
   79 }
   80 
   81 static client_t *
   82 new_client(void) {
   83     client_t *client;
   84 
   85     client = isc_mempool_get(cmp);
   86     INSIST(client != NULL);
   87     dns_name_init(&client->name, NULL);
   88     ISC_LINK_INIT(client, link);
   89     client->find = NULL;
   90 
   91     return (client);
   92 }
   93 
   94 static void
   95 free_client(client_t **c) {
   96     client_t *client;
   97 
   98     INSIST(c != NULL);
   99     client = *c;
  100     *c = NULL;
  101     INSIST(client != NULL);
  102     dns_name_free(&client->name, mctx);
  103     INSIST(!ISC_LINK_LINKED(client, link));
  104     INSIST(client->find == NULL);
  105 
  106     isc_mempool_put(cmp, client);
  107 }
  108 
  109 static inline void
  110 CLOCK(void) {
  111     RUNTIME_CHECK(isc_mutex_lock(&client_lock) == ISC_R_SUCCESS);
  112 }
  113 
  114 static inline void
  115 CUNLOCK(void) {
  116     RUNTIME_CHECK(isc_mutex_unlock(&client_lock) == ISC_R_SUCCESS);
  117 }
  118 
  119 static void
  120 lookup_callback(isc_task_t *task, isc_event_t *ev) {
  121     client_t *client;
  122 
  123     client = ev->ev_arg;
  124     INSIST(client->find == ev->ev_sender);
  125 
  126     printf("NAME %s:\n\tTask %p got event %p type %08x from %p, client %p\n\terr4: %s  err6: %s\n",
  127            client->target,
  128            task, ev, ev->ev_type, client->find, client,
  129            isc_result_totext(client->find->result_v4),
  130            isc_result_totext(client->find->result_v6));
  131 
  132     isc_event_free(&ev);
  133     ev = NULL;
  134 
  135     CLOCK();
  136 
  137     dns_adb_dumpfind(client->find, stderr);
  138     dns_adb_destroyfind(&client->find);
  139 
  140     ISC_LIST_UNLINK(clients, client, link);
  141     free_client(&client);
  142 
  143     CUNLOCK();
  144 }
  145 
  146 static void
  147 create_managers(void) {
  148     isc_result_t result;
  149 
  150     taskmgr = NULL;
  151     result = isc_taskmgr_create(mctx, 5, 0, &taskmgr);
  152     check_result(result, "isc_taskmgr_create");
  153 
  154     timermgr = NULL;
  155     result = isc_timermgr_create(mctx, &timermgr);
  156     check_result(result, "isc_timermgr_create");
  157 
  158     socketmgr = NULL;
  159     result = isc_socketmgr_create(mctx, &socketmgr);
  160     check_result(result, "isc_socketmgr_create");
  161 
  162     dispatchmgr = NULL;
  163     result = dns_dispatchmgr_create(mctx, NULL, &dispatchmgr);
  164     check_result(result, "dns_dispatchmgr_create");
  165 }
  166 
  167 static void
  168 create_view(void) {
  169     dns_cache_t *cache;
  170     isc_result_t result;
  171 
  172     /*
  173      * View.
  174      */
  175     view = NULL;
  176     result = dns_view_create(mctx, dns_rdataclass_in, "_default", &view);
  177     check_result(result, "dns_view_create");
  178 
  179     /*
  180      * Cache.
  181      */
  182     cache = NULL;
  183     result = dns_cache_create(mctx, taskmgr, timermgr, dns_rdataclass_in,
  184                   "rbt", 0, NULL, &cache);
  185     check_result(result, "dns_cache_create");
  186     dns_view_setcache(view, cache);
  187     dns_cache_detach(&cache);
  188 
  189     {
  190         unsigned int attrs;
  191         isc_sockaddr_t any4, any6;
  192         dns_dispatch_t *disp4 = NULL;
  193         dns_dispatch_t *disp6 = NULL;
  194 
  195         isc_sockaddr_any(&any4);
  196         isc_sockaddr_any6(&any6);
  197 
  198         attrs = DNS_DISPATCHATTR_IPV4 | DNS_DISPATCHATTR_UDP;
  199         RUNTIME_CHECK(dns_dispatch_getudp(dispatchmgr, socketmgr,
  200                           taskmgr, &any4,
  201                           512, 6, 1024, 17, 19,
  202                           attrs, attrs, &disp4)
  203                   == ISC_R_SUCCESS);
  204         INSIST(disp4 != NULL);
  205 
  206         attrs = DNS_DISPATCHATTR_IPV6 | DNS_DISPATCHATTR_UDP;
  207         RUNTIME_CHECK(dns_dispatch_getudp(dispatchmgr, socketmgr,
  208                           taskmgr, &any6,
  209                           512, 6, 1024, 17, 19,
  210                           attrs, attrs, &disp6)
  211                   == ISC_R_SUCCESS);
  212         INSIST(disp6 != NULL);
  213 
  214         RUNTIME_CHECK(dns_view_createresolver(view, taskmgr, 10, 1,
  215                               socketmgr,
  216                               timermgr, 0,
  217                               dispatchmgr,
  218                               disp4, disp6) ==
  219               ISC_R_SUCCESS);
  220     }
  221 
  222     rootdb = NULL;
  223     result = dns_rootns_create(mctx, dns_rdataclass_in, NULL, &rootdb);
  224     check_result(result, "dns_rootns_create()");
  225     dns_view_sethints(view, rootdb);
  226     dns_db_detach(&rootdb);
  227 
  228     dns_view_freeze(view);
  229 }
  230 
  231 static void
  232 lookup(const char *target) {
  233     dns_name_t name;
  234     unsigned char namedata[256];
  235     client_t *client;
  236     isc_buffer_t t, namebuf;
  237     isc_result_t result;
  238     unsigned int options;
  239 
  240     INSIST(target != NULL);
  241 
  242     client = new_client();
  243     isc_buffer_constinit(&t, target, strlen(target));
  244     isc_buffer_add(&t, strlen(target));
  245     isc_buffer_init(&namebuf, namedata, sizeof(namedata));
  246     dns_name_init(&name, NULL);
  247     result = dns_name_fromtext(&name, &t, dns_rootname, 0, &namebuf);
  248     check_result(result, "dns_name_fromtext %s", target);
  249 
  250     result = dns_name_dup(&name, mctx, &client->name);
  251     check_result(result, "dns_name_dup %s", target);
  252 
  253     options = 0;
  254     options |= DNS_ADBFIND_INET;
  255     options |= DNS_ADBFIND_INET6;
  256     options |= DNS_ADBFIND_WANTEVENT;
  257     options |= DNS_ADBFIND_HINTOK;
  258     options |= DNS_ADBFIND_GLUEOK;
  259     result = dns_adb_createfind(adb, t2, lookup_callback, client,
  260                     &client->name, dns_rootname, 0, options,
  261                     now, NULL, view->dstport, &client->find);
  262     if (result != ISC_R_SUCCESS)
  263         printf("DNS_ADB_CREATEFIND -> %s\n", dns_result_totext(result));
  264     dns_adb_dumpfind(client->find, stderr);
  265 
  266     if ((client->find->options & DNS_ADBFIND_WANTEVENT) != 0) {
  267         client->target = target;
  268         ISC_LIST_APPEND(clients, client, link);
  269     } else {
  270         printf("NAME %s:  err4 %s, err6 %s\n",
  271                target, isc_result_totext(client->find->result_v4),
  272                isc_result_totext(client->find->result_v6));
  273 
  274         dns_adb_destroyfind(&client->find);
  275         free_client(&client);
  276     }
  277 }
  278 
  279 int
  280 main(int argc, char **argv) {
  281     isc_result_t result;
  282     isc_logdestination_t destination;
  283 
  284     UNUSED(argc);
  285     UNUSED(argv);
  286 
  287     dns_result_register();
  288     result = isc_app_start();
  289     check_result(result, "isc_app_start()");
  290 
  291     isc_stdtime_get(&now);
  292 
  293     result = isc_mutex_init(&client_lock);
  294     check_result(result, "isc_mutex_init(&client_lock)");
  295     ISC_LIST_INIT(clients);
  296 
  297     /*
  298      * EVERYTHING needs a memory context.
  299      */
  300     RUNTIME_CHECK(isc_mem_create(0, 0, &mctx) == ISC_R_SUCCESS);
  301 
  302     cmp = NULL;
  303     RUNTIME_CHECK(isc_mempool_create(mctx, sizeof(client_t), &cmp)
  304               == ISC_R_SUCCESS);
  305     isc_mempool_setname(cmp, "adb test clients");
  306 
  307     result = isc_entropy_create(mctx, &ectx);
  308     check_result(result, "isc_entropy_create()");
  309     result = isc_hash_create(mctx, ectx, DNS_NAME_MAXWIRE);
  310     check_result(result, "isc_hash_create()");
  311 
  312     result = isc_log_create(mctx, &lctx, &lcfg);
  313     check_result(result, "isc_log_create()");
  314     isc_log_setcontext(lctx);
  315     dns_log_init(lctx);
  316     dns_log_setcontext(lctx);
  317 
  318     /*
  319      * Create and install the default channel.
  320      */
  321     destination.file.stream = stderr;
  322     destination.file.name = NULL;
  323     destination.file.versions = ISC_LOG_ROLLNEVER;
  324     destination.file.maximum_size = 0;
  325     result = isc_log_createchannel(lcfg, "_default",
  326                        ISC_LOG_TOFILEDESC,
  327                        ISC_LOG_DYNAMIC,
  328                        &destination, ISC_LOG_PRINTTIME);
  329     check_result(result, "isc_log_createchannel()");
  330     result = isc_log_usechannel(lcfg, "_default", NULL, NULL);
  331     check_result(result, "isc_log_usechannel()");
  332 
  333     /*
  334      * Set the initial debug level.
  335      */
  336     isc_log_setdebuglevel(lctx, 2);
  337 
  338     create_managers();
  339 
  340     t1 = NULL;
  341     result = isc_task_create(taskmgr, 0, &t1);
  342     check_result(result, "isc_task_create t1");
  343     t2 = NULL;
  344     result = isc_task_create(taskmgr, 0, &t2);
  345     check_result(result, "isc_task_create t2");
  346 
  347     printf("task 1 = %p\n", t1);
  348     printf("task 2 = %p\n", t2);
  349 
  350     create_view();
  351 
  352     adb = view->adb;
  353 
  354     /*
  355      * Lock the entire client list here.  This will cause all events
  356      * for found names to block as well.
  357      */
  358     CLOCK();
  359     lookup("f.root-servers.net.");      /* Should be in hints */
  360     lookup("www.iengines.com");     /* should fetch */
  361     lookup("www.isc.org");          /* should fetch */
  362     lookup("www.flame.org");        /* should fetch */
  363     lookup("kechara.flame.org.");       /* should fetch */
  364     lookup("moghedien.flame.org.");     /* should fetch */
  365     lookup("mailrelay.flame.org.");     /* should fetch */
  366     lookup("ipv4v6.flame.org.");        /* should fetch */
  367     lookup("nonexistant.flame.org.");   /* should fail to be found */
  368     lookup("foobar.badns.flame.org.");  /* should fail utterly (NS) */
  369     lookup("i.root-servers.net.");      /* Should be in hints */
  370     lookup("www.firstcard.com.");
  371     lookup("dns04.flame.org.");
  372     CUNLOCK();
  373 
  374     sleep(10);
  375 
  376     dns_adb_dump(adb, stderr);
  377 
  378     sleep(10);
  379 
  380     CLOCK();
  381     lookup("f.root-servers.net.");      /* Should be in hints */
  382     lookup("www.iengines.com");     /* should fetch */
  383     lookup("www.isc.org");          /* should fetch */
  384     lookup("www.flame.org");        /* should fetch */
  385     lookup("kechara.flame.org.");       /* should fetch */
  386     lookup("moghedien.flame.org.");     /* should fetch */
  387     lookup("mailrelay.flame.org.");     /* should fetch */
  388     lookup("ipv4v6.flame.org.");        /* should fetch */
  389     lookup("nonexistant.flame.org.");   /* should fail to be found */
  390     lookup("foobar.badns.flame.org.");  /* should fail utterly (NS) */
  391     lookup("i.root-servers.net.");      /* Should be in hints */
  392     CUNLOCK();
  393 
  394     sleep(20);
  395 
  396     dns_adb_dump(adb, stderr);
  397 
  398     isc_task_detach(&t1);
  399     isc_task_detach(&t2);
  400 
  401     isc_mem_stats(mctx, stdout);
  402     dns_adb_dump(adb, stderr);
  403 
  404     isc_app_run();
  405 
  406     dns_adb_dump(adb, stderr);
  407 
  408     dns_view_detach(&view);
  409     adb = NULL;
  410 
  411     fprintf(stderr, "Destroying socket manager\n");
  412     isc_socketmgr_destroy(&socketmgr);
  413     fprintf(stderr, "Destroying timer manager\n");
  414     isc_timermgr_destroy(&timermgr);
  415 
  416     fprintf(stderr, "Destroying task manager\n");
  417     isc_taskmgr_destroy(&taskmgr);
  418 
  419     isc_log_destroy(&lctx);
  420 
  421     isc_hash_destroy();
  422     isc_entropy_detach(&ectx);
  423 
  424     isc_mempool_destroy(&cmp);
  425     isc_mem_stats(mctx, stdout);
  426     isc_mem_destroy(&mctx);
  427 
  428     isc_app_finish();
  429 
  430     return (0);
  431 }