2
0
mirror of https://gitlab.isc.org/isc-projects/bind9 synced 2025-08-31 14:35:26 +00:00

create a resolver and a cache, and load hints from the built-in string. Look up f.root-servers.net.

This commit is contained in:
Michael Graff
1999-10-21 23:09:41 +00:00
parent 4d727d86b2
commit efc8a09e19

View File

@@ -29,15 +29,27 @@
#include <isc/mem.h>
#include <isc/task.h>
#include <isc/thread.h>
#include <isc/timer.h>
#include <isc/result.h>
#include <isc/sockaddr.h>
#include <isc/socket.h>
#include <isc/net.h>
#include <dns/name.h>
#include <dns/address.h>
#include <dns/db.h>
#include <dns/master.h>
#include <dns/name.h>
isc_mem_t *mctx;
isc_taskmgr_t *manager;
isc_socketmgr_t *socketmgr;
isc_timermgr_t *timermgr;
unsigned char namestorage1[512];
unsigned char namestorage2[512];
unsigned char namestorage3[512];
unsigned char namestorage4[512];
dns_view_t *view;
dns_db_t *ns_g_rootns;
static void lookup_callback(isc_task_t *, isc_event_t *);
static void fatal(char *, ...);
@@ -62,6 +74,94 @@ check_result(isc_result_t result, char *msg)
fatal("%s: %s", msg, isc_result_totext(result));
}
static char root_ns[] =
";\n"
"; Internet Root Nameservers\n"
";\n"
"; Thu Sep 23 17:57:37 PDT 1999\n"
";\n"
"$TTL 518400\n"
". 518400 IN NS F.ROOT-SERVERS.NET.\n"
". 518400 IN NS B.ROOT-SERVERS.NET.\n"
". 518400 IN NS J.ROOT-SERVERS.NET.\n"
". 518400 IN NS K.ROOT-SERVERS.NET.\n"
". 518400 IN NS L.ROOT-SERVERS.NET.\n"
". 518400 IN NS M.ROOT-SERVERS.NET.\n"
". 518400 IN NS I.ROOT-SERVERS.NET.\n"
". 518400 IN NS E.ROOT-SERVERS.NET.\n"
". 518400 IN NS D.ROOT-SERVERS.NET.\n"
". 518400 IN NS A.ROOT-SERVERS.NET.\n"
". 518400 IN NS H.ROOT-SERVERS.NET.\n"
". 518400 IN NS C.ROOT-SERVERS.NET.\n"
". 518400 IN NS G.ROOT-SERVERS.NET.\n"
"F.ROOT-SERVERS.NET. 3600000 IN A 192.5.5.241\n"
"B.ROOT-SERVERS.NET. 3600000 IN A 128.9.0.107\n"
"J.ROOT-SERVERS.NET. 3600000 IN A 198.41.0.10\n"
"K.ROOT-SERVERS.NET. 3600000 IN A 193.0.14.129\n"
"L.ROOT-SERVERS.NET. 3600000 IN A 198.32.64.12\n"
"M.ROOT-SERVERS.NET. 3600000 IN A 202.12.27.33\n"
"I.ROOT-SERVERS.NET. 3600000 IN A 192.36.148.17\n"
"E.ROOT-SERVERS.NET. 3600000 IN A 192.203.230.10\n"
"D.ROOT-SERVERS.NET. 3600000 IN A 128.8.10.90\n"
"A.ROOT-SERVERS.NET. 3600000 IN A 198.41.0.4\n"
"H.ROOT-SERVERS.NET. 3600000 IN A 128.63.2.53\n"
"C.ROOT-SERVERS.NET. 3600000 IN A 192.33.4.12\n"
"G.ROOT-SERVERS.NET. 3600000 IN A 192.112.36.4\n";
isc_result_t
ns_rootns_init(void)
{
dns_result_t result, eresult;
isc_buffer_t source;
size_t len;
int soacount, nscount;
dns_rdatacallbacks_t callbacks;
REQUIRE(ns_g_rootns == NULL);
result = dns_db_create(mctx, "rbt", dns_rootname, ISC_FALSE,
dns_rdataclass_in, 0, NULL, &ns_g_rootns);
if (result != ISC_R_SUCCESS)
return (result);
dns_rdatacallbacks_init(&callbacks);
len = strlen(root_ns);
isc_buffer_init(&source, root_ns, len, ISC_BUFFERTYPE_TEXT);
isc_buffer_add(&source, len);
result = dns_db_beginload(ns_g_rootns, &callbacks.add,
&callbacks.add_private);
if (result != ISC_R_SUCCESS)
return (result);
result = dns_master_loadbuffer(&source, &ns_g_rootns->origin,
&ns_g_rootns->origin,
ns_g_rootns->rdclass, ISC_FALSE,
&soacount, &nscount, &callbacks,
ns_g_rootns->mctx);
eresult = dns_db_endload(ns_g_rootns, &callbacks.add_private);
if (result == ISC_R_SUCCESS)
result = eresult;
if (result != ISC_R_SUCCESS)
goto db_detach;
return (DNS_R_SUCCESS);
db_detach:
dns_db_detach(&ns_g_rootns);
return (result);
}
void
ns_rootns_destroy(void)
{
REQUIRE(ns_g_rootns != NULL);
dns_db_detach(&ns_g_rootns);
}
static void
lookup_callback(isc_task_t *task, isc_event_t *ev)
{
@@ -78,9 +178,74 @@ lookup_callback(isc_task_t *task, isc_event_t *ev)
isc_app_shutdown();
}
unsigned char namestorage1[512];
unsigned char namestorage2[512];
unsigned char namestorage3[512];
void
create_managers(void)
{
isc_result_t result;
manager = NULL;
result = isc_taskmgr_create(mctx, 2, 0, &manager);
check_result(result, "isc_taskmgr_create");
timermgr = NULL;
result = isc_timermgr_create(mctx, &timermgr);
check_result(result, "isc_timermgr_create");
socketmgr = NULL;
result = isc_socketmgr_create(mctx, &socketmgr);
check_result(result, "isc_socketmgr_create");
}
void
create_view(void)
{
dns_db_t *db;
isc_result_t result;
/*
* View.
*/
view = NULL;
result = dns_view_create(mctx, dns_rdataclass_in, "_default", &view);
check_result(result, "dns_view_create");
/*
* Cache.
*/
db = NULL;
result = dns_db_create(mctx, "rbt", dns_rootname, ISC_TRUE,
dns_rdataclass_in, 0, NULL, &db);
check_result(result, "dns_view_create");
dns_view_setcachedb(view, db);
dns_db_detach(&db);
/*
* Resolver.
*
* XXXRTH hardwired number of tasks. Also, we'll need to
* see if we are dealing with a shared dispatcher in this view.
*/
result = dns_view_createresolver(view, manager, 16, socketmgr,
timermgr, NULL);
check_result(result, "dns_view_createresolver");
result = ns_rootns_init();
check_result(result, "ns_rootns_init()");
/*
* We have default hints for class IN.
*/
dns_view_sethints(view, ns_g_rootns);
dns_view_freeze(view);
}
void
destroy_view(void)
{
dns_view_detach(&view);
ns_rootns_destroy();
}
int
main(int argc, char **argv)
@@ -89,9 +254,8 @@ main(int argc, char **argv)
isc_sockaddr_t sockaddr;
struct in_addr ina;
isc_result_t result;
dns_name_t name1, name2, name3;
dns_name_t name1, name2, name3, name4;
isc_buffer_t t, namebuf;
dns_view_t *view;
dns_adb_t *adb;
dns_adbhandle_t *handle;
dns_adbaddrinfo_t *ai;
@@ -113,12 +277,7 @@ main(int argc, char **argv)
mctx = NULL;
RUNTIME_CHECK(isc_mem_create(0, 0, &mctx) == ISC_R_SUCCESS);
/*
* The task manager is independent (other than memory context)
*/
manager = NULL;
result = isc_taskmgr_create(mctx, 2, 0, &manager);
check_result(result, "isc_taskmgr_create");
create_managers();
t1 = NULL;
result = isc_task_create(manager, NULL, 0, &t1);
@@ -130,11 +289,7 @@ main(int argc, char **argv)
printf("task 1 = %p\n", t1);
printf("task 2 = %p\n", t2);
view = NULL;
result = dns_view_create(mctx, dns_rdataclass_in, "foo", &view);
check_result(result, "dns_view_create");
dns_view_freeze(view);
create_view();
/*
* Create the address database.
@@ -146,6 +301,7 @@ main(int argc, char **argv)
#define NAME1 "kechara.flame.org."
#define NAME2 "moghedien.isc.org."
#define NAME3 "nonexistant.flame.org."
#define NAME4 "f.root-servers.net."
isc_buffer_init(&t, NAME1, sizeof NAME1 - 1, ISC_BUFFERTYPE_TEXT);
isc_buffer_add(&t, strlen(NAME1));
@@ -174,6 +330,15 @@ main(int argc, char **argv)
&namebuf);
check_result(result, "dns_name_fromtext NAME3");
isc_buffer_init(&t, NAME4, sizeof NAME4 - 1, ISC_BUFFERTYPE_TEXT);
isc_buffer_add(&t, strlen(NAME4));
isc_buffer_init(&namebuf, namestorage4, sizeof namestorage4,
ISC_BUFFERTYPE_BINARY);
dns_name_init(&name4, NULL);
result = dns_name_fromtext(&name4, &t, dns_rootname, ISC_FALSE,
&namebuf);
check_result(result, "dns_name_fromtext NAME4");
/*
* Store this address for this name.
*/
@@ -245,7 +410,7 @@ main(int argc, char **argv)
dns_adb_dumphandle(adb, handle, stderr);
/*
* delete one of the names, and kill the adb
* delete one of the names
*/
result = dns_adb_deletename(adb, &name2);
check_result(result, "dns_adb_deletename name2");
@@ -275,6 +440,29 @@ main(int argc, char **argv)
if (handle != NULL)
dns_adb_done(adb, &handle);
/*
* Look up a host that will be in the hints database
*/
result = dns_adb_lookup(adb, t2, lookup_callback, &name4,
&name4, dns_rootname, now, &handle);
if (result == ISC_R_SUCCESS) {
check_result(handle->result, "handle->result");
check_result(result, "dns_adb_lookup name4");
dns_adb_dump(adb, stderr);
dns_adb_dumphandle(adb, handle, stderr);
} else {
fprintf(stderr, "lookup of name4: %s\n",
isc_result_totext(result));
}
dns_adb_dump(adb, stderr);
if (handle != NULL) {
dns_adb_dumphandle(adb, handle, stderr);
dns_adb_done(adb, &handle);
}
isc_task_detach(&t1);
isc_task_detach(&t2);
@@ -284,7 +472,10 @@ main(int argc, char **argv)
isc_app_run();
dns_view_detach(&view);
destroy_view();
isc_socketmgr_destroy(&socketmgr);
isc_timermgr_destroy(&timermgr);
fprintf(stderr, "Destroying task manager\n");
isc_taskmgr_destroy(&manager);