adb_test.c revision 910df98b0efcbe8380b952887f4071051cc39a25
1e107b3d7b54de5022c3328423164e533afcc15eMark Andrews/*
7a272c6b0de3b8c0ad018b9896e287da19c43befAutomatic Updater * Copyright (C) 1998, 1999 Internet Software Consortium.
1e107b3d7b54de5022c3328423164e533afcc15eMark Andrews *
1e107b3d7b54de5022c3328423164e533afcc15eMark Andrews * Permission to use, copy, modify, and distribute this software for any
ec5347e2c775f027573ce5648b910361aa926c01Automatic Updater * purpose with or without fee is hereby granted, provided that the above
1e107b3d7b54de5022c3328423164e533afcc15eMark Andrews * copyright notice and this permission notice appear in all copies.
1e107b3d7b54de5022c3328423164e533afcc15eMark Andrews *
1e107b3d7b54de5022c3328423164e533afcc15eMark Andrews * THE SOFTWARE IS PROVIDED "AS IS" AND INTERNET SOFTWARE CONSORTIUM DISCLAIMS
dafcb997e390efa4423883dafd100c975c4095d6Mark Andrews * ALL WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES
dafcb997e390efa4423883dafd100c975c4095d6Mark Andrews * OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL INTERNET SOFTWARE
dafcb997e390efa4423883dafd100c975c4095d6Mark Andrews * CONSORTIUM BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL
dafcb997e390efa4423883dafd100c975c4095d6Mark Andrews * DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR
dafcb997e390efa4423883dafd100c975c4095d6Mark Andrews * PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS
dafcb997e390efa4423883dafd100c975c4095d6Mark Andrews * ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS
dafcb997e390efa4423883dafd100c975c4095d6Mark Andrews * SOFTWARE.
1e107b3d7b54de5022c3328423164e533afcc15eMark Andrews */
1e107b3d7b54de5022c3328423164e533afcc15eMark Andrews
7a272c6b0de3b8c0ad018b9896e287da19c43befAutomatic Updater#include <config.h>
1e107b3d7b54de5022c3328423164e533afcc15eMark Andrews
ab023a65562e62b85a824509d829b6fad87e00b1Rob Austein#include <stdio.h>
1e107b3d7b54de5022c3328423164e533afcc15eMark Andrews#include <stdlib.h>
7a272c6b0de3b8c0ad018b9896e287da19c43befAutomatic Updater#include <unistd.h>
ab023a65562e62b85a824509d829b6fad87e00b1Rob Austein#include <string.h>
1e107b3d7b54de5022c3328423164e533afcc15eMark Andrews
1e107b3d7b54de5022c3328423164e533afcc15eMark Andrews#include <isc/assertions.h>
1e107b3d7b54de5022c3328423164e533afcc15eMark Andrews#include <isc/buffer.h>
1e107b3d7b54de5022c3328423164e533afcc15eMark Andrews#include <isc/error.h>
1e107b3d7b54de5022c3328423164e533afcc15eMark Andrews#include <isc/mem.h>
1e107b3d7b54de5022c3328423164e533afcc15eMark Andrews#include <isc/task.h>
1e107b3d7b54de5022c3328423164e533afcc15eMark Andrews#include <isc/thread.h>
1e107b3d7b54de5022c3328423164e533afcc15eMark Andrews#include <isc/result.h>
1e107b3d7b54de5022c3328423164e533afcc15eMark Andrews#include <isc/sockaddr.h>
1e107b3d7b54de5022c3328423164e533afcc15eMark Andrews#include <isc/net.h>
1e107b3d7b54de5022c3328423164e533afcc15eMark Andrews
1e107b3d7b54de5022c3328423164e533afcc15eMark Andrews#include <dns/name.h>
1e107b3d7b54de5022c3328423164e533afcc15eMark Andrews#include <dns/address.h>
1e107b3d7b54de5022c3328423164e533afcc15eMark Andrews
1e107b3d7b54de5022c3328423164e533afcc15eMark Andrewsisc_mem_t *mctx;
1e107b3d7b54de5022c3328423164e533afcc15eMark Andrewsisc_taskmgr_t *manager;
1e107b3d7b54de5022c3328423164e533afcc15eMark Andrews
1e107b3d7b54de5022c3328423164e533afcc15eMark Andrewsstatic void
1e107b3d7b54de5022c3328423164e533afcc15eMark Andrewsfatal(char *format, ...)
1e107b3d7b54de5022c3328423164e533afcc15eMark Andrews{
1e107b3d7b54de5022c3328423164e533afcc15eMark Andrews va_list args;
1e107b3d7b54de5022c3328423164e533afcc15eMark Andrews
1e107b3d7b54de5022c3328423164e533afcc15eMark Andrews va_start(args, format);
1e107b3d7b54de5022c3328423164e533afcc15eMark Andrews vfprintf(stderr, format, args);
1e107b3d7b54de5022c3328423164e533afcc15eMark Andrews va_end(args);
1e107b3d7b54de5022c3328423164e533afcc15eMark Andrews fprintf(stderr, "\n");
1e107b3d7b54de5022c3328423164e533afcc15eMark Andrews exit(1);
1e107b3d7b54de5022c3328423164e533afcc15eMark Andrews}
1e107b3d7b54de5022c3328423164e533afcc15eMark Andrews
1e107b3d7b54de5022c3328423164e533afcc15eMark Andrewsstatic inline void
1e107b3d7b54de5022c3328423164e533afcc15eMark Andrewscheck_result(isc_result_t result, char *msg)
1e107b3d7b54de5022c3328423164e533afcc15eMark Andrews{
1e107b3d7b54de5022c3328423164e533afcc15eMark Andrews if (result != ISC_R_SUCCESS)
1e107b3d7b54de5022c3328423164e533afcc15eMark Andrews fatal("%s: %s", msg, isc_result_totext(result));
1e107b3d7b54de5022c3328423164e533afcc15eMark Andrews}
a8f061d5c61bbdbe922cbb0adb70ae81770b52cbMark Andrews
a8f061d5c61bbdbe922cbb0adb70ae81770b52cbMark Andrewsint
1e107b3d7b54de5022c3328423164e533afcc15eMark Andrewsmain(int argc, char *argv[])
1e107b3d7b54de5022c3328423164e533afcc15eMark Andrews{
1e107b3d7b54de5022c3328423164e533afcc15eMark Andrews isc_task_t *t1, *t2;
1e107b3d7b54de5022c3328423164e533afcc15eMark Andrews isc_sockaddr_t sockaddr;
1e107b3d7b54de5022c3328423164e533afcc15eMark Andrews struct in_addr ina;
1e107b3d7b54de5022c3328423164e533afcc15eMark Andrews isc_result_t result;
1e107b3d7b54de5022c3328423164e533afcc15eMark Andrews dns_name_t name;
1e107b3d7b54de5022c3328423164e533afcc15eMark Andrews isc_buffer_t t, namebuf;
d19fc9d988171a1a7ff87d200b86c9aa657aa3beMark Andrews unsigned char namestorage[512];
1e107b3d7b54de5022c3328423164e533afcc15eMark Andrews dns_adb_t *adb;
1e107b3d7b54de5022c3328423164e533afcc15eMark Andrews
1e107b3d7b54de5022c3328423164e533afcc15eMark Andrews dns_result_register();
1e107b3d7b54de5022c3328423164e533afcc15eMark Andrews
1e107b3d7b54de5022c3328423164e533afcc15eMark Andrews /*
ab023a65562e62b85a824509d829b6fad87e00b1Rob Austein * EVERYTHING needs a memory context.
1e107b3d7b54de5022c3328423164e533afcc15eMark Andrews */
1e107b3d7b54de5022c3328423164e533afcc15eMark Andrews mctx = NULL;
1e107b3d7b54de5022c3328423164e533afcc15eMark Andrews RUNTIME_CHECK(isc_mem_create(0, 0, &mctx) == ISC_R_SUCCESS);
1e107b3d7b54de5022c3328423164e533afcc15eMark Andrews
ab023a65562e62b85a824509d829b6fad87e00b1Rob Austein /*
ab023a65562e62b85a824509d829b6fad87e00b1Rob Austein * The task manager is independent (other than memory context)
1e107b3d7b54de5022c3328423164e533afcc15eMark Andrews */
1e107b3d7b54de5022c3328423164e533afcc15eMark Andrews manager = NULL;
1e107b3d7b54de5022c3328423164e533afcc15eMark Andrews result = isc_taskmgr_create(mctx, 2, 0, &manager);
1e107b3d7b54de5022c3328423164e533afcc15eMark Andrews check_result(result, "isc_taskmgr_create");
1e107b3d7b54de5022c3328423164e533afcc15eMark Andrews
ab023a65562e62b85a824509d829b6fad87e00b1Rob Austein t1 = NULL;
1e107b3d7b54de5022c3328423164e533afcc15eMark Andrews result = isc_task_create(manager, NULL, 0, &t1);
ab023a65562e62b85a824509d829b6fad87e00b1Rob Austein check_result(result, "isc_task_create t1");
1e107b3d7b54de5022c3328423164e533afcc15eMark Andrews t2 = NULL;
1e107b3d7b54de5022c3328423164e533afcc15eMark Andrews result = isc_task_create(manager, NULL, 0, &t2);
1e107b3d7b54de5022c3328423164e533afcc15eMark Andrews check_result(result, "isc_task_create t2");
1e107b3d7b54de5022c3328423164e533afcc15eMark Andrews
1e107b3d7b54de5022c3328423164e533afcc15eMark Andrews printf("task 1 = %p\n", t1);
1e107b3d7b54de5022c3328423164e533afcc15eMark Andrews printf("task 2 = %p\n", t2);
ab023a65562e62b85a824509d829b6fad87e00b1Rob Austein
ab023a65562e62b85a824509d829b6fad87e00b1Rob Austein /*
ab023a65562e62b85a824509d829b6fad87e00b1Rob Austein * Create the address database.
ab023a65562e62b85a824509d829b6fad87e00b1Rob Austein */
1e107b3d7b54de5022c3328423164e533afcc15eMark Andrews adb = NULL;
1e107b3d7b54de5022c3328423164e533afcc15eMark Andrews result = dns_adb_create(mctx, &adb);
be2c2c29a88db96bd51f11d671ec207f0b6b0d45Mark Andrews check_result(result, "dns_adb_create");
1e107b3d7b54de5022c3328423164e533afcc15eMark Andrews
1e107b3d7b54de5022c3328423164e533afcc15eMark Andrews /*
1e107b3d7b54de5022c3328423164e533afcc15eMark Andrews * Store this address and name in it.
1e107b3d7b54de5022c3328423164e533afcc15eMark Andrews */
1e107b3d7b54de5022c3328423164e533afcc15eMark Andrews ina.s_addr = inet_addr("204.152.184.97");
1e107b3d7b54de5022c3328423164e533afcc15eMark Andrews isc_sockaddr_fromin(&sockaddr, &ina, 53);
1e107b3d7b54de5022c3328423164e533afcc15eMark Andrews
1e107b3d7b54de5022c3328423164e533afcc15eMark Andrews#define NAME1 "kechara.flame.org."
1e107b3d7b54de5022c3328423164e533afcc15eMark Andrews
1e107b3d7b54de5022c3328423164e533afcc15eMark Andrews isc_buffer_init(&t, NAME1, sizeof NAME1 - 1, ISC_BUFFERTYPE_TEXT);
1e107b3d7b54de5022c3328423164e533afcc15eMark Andrews isc_buffer_add(&t, strlen(NAME1));
1e107b3d7b54de5022c3328423164e533afcc15eMark Andrews isc_buffer_init(&namebuf, namestorage, sizeof namestorage,
1e107b3d7b54de5022c3328423164e533afcc15eMark Andrews ISC_BUFFERTYPE_BINARY);
1e107b3d7b54de5022c3328423164e533afcc15eMark Andrews dns_name_init(&name, NULL);
1e107b3d7b54de5022c3328423164e533afcc15eMark Andrews result = dns_name_fromtext(&name, &t, dns_rootname, ISC_FALSE,
1e107b3d7b54de5022c3328423164e533afcc15eMark Andrews &namebuf);
1e107b3d7b54de5022c3328423164e533afcc15eMark Andrews check_result(result, "dns_name_fromtext");
1e107b3d7b54de5022c3328423164e533afcc15eMark Andrews
1e107b3d7b54de5022c3328423164e533afcc15eMark Andrews result = dns_adb_insert(adb, &name, &sockaddr);
1e107b3d7b54de5022c3328423164e533afcc15eMark Andrews check_result(result, "dns_adb_insert");
1e107b3d7b54de5022c3328423164e533afcc15eMark Andrews
1e107b3d7b54de5022c3328423164e533afcc15eMark Andrews isc_task_detach(&t1);
1e107b3d7b54de5022c3328423164e533afcc15eMark Andrews isc_task_detach(&t2);
1e107b3d7b54de5022c3328423164e533afcc15eMark Andrews
1e107b3d7b54de5022c3328423164e533afcc15eMark Andrews dns_adb_dump(adb, stderr);
1e107b3d7b54de5022c3328423164e533afcc15eMark Andrews
1e107b3d7b54de5022c3328423164e533afcc15eMark Andrews result = dns_adb_deletename(adb, &name);
1e107b3d7b54de5022c3328423164e533afcc15eMark Andrews check_result(result, "dns_adb_deletename");
1e107b3d7b54de5022c3328423164e533afcc15eMark Andrews
1e107b3d7b54de5022c3328423164e533afcc15eMark Andrews dns_adb_dump(adb, stderr);
1e107b3d7b54de5022c3328423164e533afcc15eMark Andrews
1e107b3d7b54de5022c3328423164e533afcc15eMark Andrews isc_mem_stats(mctx, stdout);
1e107b3d7b54de5022c3328423164e533afcc15eMark Andrews dns_adb_destroy(&adb);
1e107b3d7b54de5022c3328423164e533afcc15eMark Andrews
1e107b3d7b54de5022c3328423164e533afcc15eMark Andrews fprintf(stderr, "Destroying task manager\n");
1e107b3d7b54de5022c3328423164e533afcc15eMark Andrews isc_taskmgr_destroy(&manager);
1e107b3d7b54de5022c3328423164e533afcc15eMark Andrews
1e107b3d7b54de5022c3328423164e533afcc15eMark Andrews isc_mem_stats(mctx, stdout);
1e107b3d7b54de5022c3328423164e533afcc15eMark Andrews isc_mem_destroy(&mctx);
1e107b3d7b54de5022c3328423164e533afcc15eMark Andrews
1e107b3d7b54de5022c3328423164e533afcc15eMark Andrews return (0);
1e107b3d7b54de5022c3328423164e533afcc15eMark Andrews}
18d0b5e54be891a1aa938c165b6d439859121ec8Mark Andrews