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