message.c 31.6 KB
Newer Older
Michael Graff's avatar
Michael Graff committed
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
/*
 * Copyright (C) 1999  Internet Software Consortium.
 * 
 * Permission to use, copy, modify, and distribute this software for any
 * purpose with or without fee is hereby granted, provided that the above
 * copyright notice and this permission notice appear in all copies.
 * 
 * THE SOFTWARE IS PROVIDED "AS IS" AND INTERNET SOFTWARE CONSORTIUM DISCLAIMS
 * ALL WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES
 * OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL INTERNET SOFTWARE
 * CONSORTIUM BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL
 * DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR
 * PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS
 * ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS
 * SOFTWARE.
 */

/***
 *** Imports
 ***/

#include <config.h>

#include <stddef.h>
#include <string.h>

#include <isc/assertions.h>
Michael Graff's avatar
Michael Graff committed
28
29
#include <isc/boolean.h>
#include <isc/region.h>
30
#include <isc/types.h>
Michael Graff's avatar
Michael Graff committed
31
32
33

#include <dns/message.h>
#include <dns/rdataset.h>
Michael Graff's avatar
Michael Graff committed
34
35
36
37
38
#include <dns/rdata.h>
#include <dns/rdataclass.h>
#include <dns/rdatatype.h>
#include <dns/rdatalist.h>
#include <dns/compress.h>
Michael Graff's avatar
Michael Graff committed
39
40

#define MESSAGE_MAGIC		0x4d534740U	/* MSG@ */
Michael Graff's avatar
Michael Graff committed
41
42
43
44
45
46
47
48
49
50
51
#define VALID_MESSAGE(msg)	(((msg)->magic) == MESSAGE_MAGIC)

#define VALID_NAMED_SECTION(s)  (((s) > DNS_SECTION_ANY) \
				 && ((s) < DNS_SECTION_MAX))
#define VALID_SECTION(s)	(((s) >= DNS_SECTION_ANY) \
				 && ((s) < DNS_SECTION_MAX))

/*
 * This is the size of each individual scratchpad buffer, and the numbers
 * of various block allocations used within the server.
 */
Michael Graff's avatar
Michael Graff committed
52
#define SCRATCHPAD_SIZE		512
Michael Graff's avatar
Michael Graff committed
53
54
#define NAME_COUNT		 16
#define RDATA_COUNT		 32
Michael Graff's avatar
Michael Graff committed
55
56
#define RDATALIST_COUNT		 32 /* should match RDATASET_COUNT */
#define RDATASET_COUNT		 32
Michael Graff's avatar
Michael Graff committed
57
58
59
60
61
62
63
64
65
66
67

/*
 * "helper" type, which consists of a block of some type, and is linkable.
 * For it to work, sizeof(dns_msgblock_t) must be a multiple of the pointer
 * size, or the allocated elements will not be alligned correctly.
 */
struct dns_msgblock {
	unsigned int			length;
	unsigned int			remaining;
	ISC_LINK(dns_msgblock_t)	link;
}; /* dynamically sized */
Michael Graff's avatar
Michael Graff committed
68
69

static inline void
Michael Graff's avatar
Michael Graff committed
70
msgblock_free(isc_mem_t *, dns_msgblock_t *);
71

Michael Graff's avatar
Michael Graff committed
72
#define msgblock_get(block, type) \
Michael Graff's avatar
Michael Graff committed
73
	((type *)msgblock_internalget(block, sizeof(type)))
Michael Graff's avatar
Michael Graff committed
74
75

static inline void *
Michael Graff's avatar
Michael Graff committed
76
77
78
79
msgblock_internalget(dns_msgblock_t *, unsigned int);

static inline void
msgblock_reset(dns_msgblock_t *, unsigned int);
Michael Graff's avatar
Michael Graff committed
80
81

static inline dns_msgblock_t *
Michael Graff's avatar
Michael Graff committed
82
msgblock_allocate(isc_mem_t *, unsigned int, unsigned int);
Michael Graff's avatar
Michael Graff committed
83
84
85
86
87
88

/*
 * Allocate a new dns_msgblock_t, and return a pointer to it.  If no memory
 * is free, return NULL.
 */
static inline dns_msgblock_t *
Michael Graff's avatar
Michael Graff committed
89
90
msgblock_allocate(isc_mem_t *mctx, unsigned int sizeof_type,
		  unsigned int count)
Michael Graff's avatar
Michael Graff committed
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
{
	dns_msgblock_t *block;
	unsigned int length;

	length = sizeof(dns_msgblock_t) + (sizeof_type * count);

	block = isc_mem_get(mctx, length);
	if (block == NULL)
		return NULL;

	block->length = length;
	block->remaining = count;

	ISC_LINK_INIT(block, link);

	return (block);
}

/*
 * Return an element from the msgblock.  If no more are available, return
 * NULL.
 */
static inline void *
Michael Graff's avatar
Michael Graff committed
114
msgblock_internalget(dns_msgblock_t *block, unsigned int sizeof_type)
Michael Graff's avatar
Michael Graff committed
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
{
	void *ptr;

	if (block->remaining == 0)
		return (NULL);

	block->remaining--;

	ptr = (((unsigned char *)block)
	       + sizeof(dns_msgblock_t)
	       + (sizeof_type * block->remaining));

	return (ptr);
}

Michael Graff's avatar
Michael Graff committed
130
131
132
133
134
135
static inline void
msgblock_reset(dns_msgblock_t *block, unsigned int count)
{
	block->remaining = count;
}

Michael Graff's avatar
Michael Graff committed
136
137
138
139
/*
 * Release memory associated with a message block.
 */
static inline void
Michael Graff's avatar
Michael Graff committed
140
msgblock_free(isc_mem_t *mctx, dns_msgblock_t *block)
Michael Graff's avatar
Michael Graff committed
141
142
143
{
	isc_mem_put(mctx, block, block->length);
}
Michael Graff's avatar
Michael Graff committed
144

Michael Graff's avatar
Michael Graff committed
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
/*
 * Allocate a new dynamic buffer, and attach it to this message as the
 * "current" buffer.  (which is always the last on the list, for our
 * uses)
 */
static inline dns_result_t
newbuffer(dns_message_t *msg)
{
	isc_result_t result;
	isc_dynbuffer_t *dynbuf;

	dynbuf = NULL;
	result = isc_dynbuffer_allocate(msg->mctx, &dynbuf, SCRATCHPAD_SIZE,
					ISC_BUFFERTYPE_BINARY);
	if (result != ISC_R_SUCCESS)
		return (DNS_R_NOMEMORY);

	ISC_LIST_APPEND(msg->scratchpad, dynbuf, link);
	return (DNS_R_SUCCESS);
}

static inline isc_buffer_t *
currentbuffer(dns_message_t *msg)
{
	isc_dynbuffer_t *dynbuf;

	dynbuf = ISC_LIST_TAIL(msg->scratchpad);

	return (&dynbuf->buffer);
}

static inline void
releasename(dns_message_t *msg, dns_name_t *name)
{
	msg->nextname = name;
}

static inline dns_name_t *
newname(dns_message_t *msg)
{
	dns_msgblock_t *msgblock;
	dns_name_t *name;

	if (msg->nextname != NULL) {
		name = msg->nextname;
		msg->nextname = NULL;
Michael Graff's avatar
Michael Graff committed
191
		dns_name_init(name, NULL);
Michael Graff's avatar
Michael Graff committed
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
		return (name);
	}

	msgblock = ISC_LIST_HEAD(msg->names);
	name = msgblock_get(msgblock, dns_name_t);
	if (name == NULL) {
		msgblock = msgblock_allocate(msg->mctx, sizeof(dns_name_t),
					     NAME_COUNT);
		if (msgblock == NULL)
			return (NULL);

		ISC_LIST_APPEND(msg->names, msgblock, link);

		name = msgblock_get(msgblock, dns_name_t);
	}

Michael Graff's avatar
Michael Graff committed
208
	dns_name_init(name, NULL);
Michael Graff's avatar
Michael Graff committed
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
	return (name);
}

static inline void
releaserdata(dns_message_t *msg, dns_rdata_t *rdata)
{
	msg->nextrdata = rdata;
}

static inline dns_rdata_t *
newrdata(dns_message_t *msg)
{
	dns_msgblock_t *msgblock;
	dns_rdata_t *rdata;

	if (msg->nextrdata != NULL) {
		rdata = msg->nextrdata;
		msg->nextrdata = NULL;
		return (rdata);
	}

	msgblock = ISC_LIST_HEAD(msg->rdatas);
	rdata = msgblock_get(msgblock, dns_rdata_t);
	if (rdata == NULL) {
		msgblock = msgblock_allocate(msg->mctx, sizeof(dns_rdata_t),
					     RDATA_COUNT);
		if (msgblock == NULL)
			return (NULL);

		ISC_LIST_APPEND(msg->rdatas, msgblock, link);

		rdata = msgblock_get(msgblock, dns_rdata_t);
	}

	return (rdata);
}

static inline void
releaserdatalist(dns_message_t *msg, dns_rdatalist_t *rdatalist)
{
	msg->nextrdatalist = rdatalist;
}

static inline dns_rdatalist_t *
newrdatalist(dns_message_t *msg)
{
	dns_msgblock_t *msgblock;
	dns_rdatalist_t *rdatalist;

	if (msg->nextrdatalist != NULL) {
		rdatalist = msg->nextrdatalist;
		msg->nextrdatalist = NULL;
		return (rdatalist);
	}

	msgblock = ISC_LIST_HEAD(msg->rdatalists);
	rdatalist = msgblock_get(msgblock, dns_rdatalist_t);
	if (rdatalist == NULL) {
		msgblock = msgblock_allocate(msg->mctx,
					     sizeof(dns_rdatalist_t),
					     RDATALIST_COUNT);
		if (msgblock == NULL)
			return (NULL);

		ISC_LIST_APPEND(msg->rdatalists, msgblock, link);

		rdatalist = msgblock_get(msgblock, dns_rdatalist_t);
	}

	return (rdatalist);
}

static inline void
releaserdataset(dns_message_t *msg, dns_rdataset_t *rdataset)
{
	msg->nextrdataset = rdataset;
}

static inline dns_rdataset_t *
newrdataset(dns_message_t *msg)
{
	dns_msgblock_t *msgblock;
	dns_rdataset_t *rdataset;

	if (msg->nextrdataset != NULL) {
		rdataset = msg->nextrdataset;
		msg->nextrdataset = NULL;
		return (rdataset);
	}

	msgblock = ISC_LIST_HEAD(msg->rdatasets);
	rdataset = msgblock_get(msgblock, dns_rdataset_t);
	if (rdataset == NULL) {
		msgblock = msgblock_allocate(msg->mctx, sizeof(dns_rdataset_t),
					     RDATASET_COUNT);
		if (msgblock == NULL)
			return (NULL);

		ISC_LIST_APPEND(msg->rdatasets, msgblock, link);

		rdataset = msgblock_get(msgblock, dns_rdataset_t);
	}

	return (rdataset);
}

Michael Graff's avatar
Michael Graff committed
315
316
317
318
319
320
321
/*
 * Init elements to default state.  Used both when allocating a new element
 * and when resetting one.
 */
static inline void
msginit(dns_message_t *m)
{
Michael Graff's avatar
Michael Graff committed
322
323
	unsigned int i;

Michael Graff's avatar
Michael Graff committed
324
325
326
327
	m->id = 0;
	m->flags = 0;
	m->rcode = 0;
	m->opcode = 0;
Michael Graff's avatar
Michael Graff committed
328
	m->rdclass = 0;
Michael Graff's avatar
Michael Graff committed
329

Michael Graff's avatar
Michael Graff committed
330
	for (i = 0 ; i < DNS_SECTION_MAX ; i++) {
Michael Graff's avatar
Michael Graff committed
331
		m->cursors[i] = NULL;
Michael Graff's avatar
Michael Graff committed
332
		m->counts[i] = 0;
Michael Graff's avatar
Michael Graff committed
333
	}
Michael Graff's avatar
Michael Graff committed
334

Michael Graff's avatar
Michael Graff committed
335
	m->state = DNS_SECTION_ANY;  /* indicate nothing parsed or rendered */
Michael Graff's avatar
Michael Graff committed
336
337
338
339
340

	m->nextname = NULL;
	m->nextrdata = NULL;
	m->nextrdataset = NULL;
	m->nextrdatalist = NULL;
341
342

	m->buffer = NULL;
Michael Graff's avatar
Michael Graff committed
343
	m->need_cctx_cleanup = ISC_FALSE;
Michael Graff's avatar
Michael Graff committed
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
}

/*
 * Free all but one (or everything) for this message.  This is used by
 * both dns_message_reset() and dns_message_parse().
 */
static void
msgreset(dns_message_t *msg, isc_boolean_t everything)
{
	dns_msgblock_t *msgblock, *next_msgblock;
	isc_dynbuffer_t *dynbuf, *next_dynbuf;
	dns_rdataset_t *rds, *next_rds;
	dns_name_t *name, *next_name;
	unsigned int i;

	/*
	 * Clean up name lists by calling the rdataset disassociate function.
	 */
	for (i = 0 ; i < DNS_SECTION_MAX ; i++) {
		name = ISC_LIST_HEAD(msg->sections[i]);
		while (name != NULL) {
			next_name = ISC_LIST_NEXT(name, link);
			ISC_LIST_UNLINK(msg->sections[i], name, link);

			rds = ISC_LIST_HEAD(name->list);
			while (rds != NULL) {
				next_rds = ISC_LIST_NEXT(rds, link);
				ISC_LIST_UNLINK(name->list, rds, link);

				dns_rdataset_disassociate(rds);
				rds = next_rds;
			}
376
			name = next_name;
Michael Graff's avatar
Michael Graff committed
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
411
412
413
414
415
416
417
418
419
420
421
422
		}
	}

	/*
	 * Clean up linked lists.
	 */

	dynbuf = ISC_LIST_HEAD(msg->scratchpad);
	INSIST(dynbuf != NULL);
	if (everything == ISC_FALSE) {
		isc_dynbuffer_reset(dynbuf);
		dynbuf = ISC_LIST_NEXT(dynbuf, link);
	}
	while (dynbuf != NULL) {
		next_dynbuf = ISC_LIST_NEXT(dynbuf, link);
		ISC_LIST_UNLINK(msg->scratchpad, dynbuf, link);
		isc_dynbuffer_free(msg->mctx, &dynbuf);
		dynbuf = next_dynbuf;
	}

	msgblock = ISC_LIST_HEAD(msg->names);
	INSIST(msgblock != NULL);
	if (everything == ISC_FALSE) {
		msgblock_reset(msgblock, NAME_COUNT);
		msgblock = ISC_LIST_NEXT(msgblock, link);
	}
	while (msgblock != NULL) {
		next_msgblock = ISC_LIST_NEXT(msgblock, link);
		ISC_LIST_UNLINK(msg->names, msgblock, link);
		msgblock_free(msg->mctx, msgblock);
		msgblock = next_msgblock;
	}

	msgblock = ISC_LIST_HEAD(msg->rdatas);
	INSIST(msgblock != NULL);
	if (everything == ISC_FALSE) {
		msgblock_reset(msgblock, RDATA_COUNT);
		msgblock = ISC_LIST_NEXT(msgblock, link);
	}
	while (msgblock != NULL) {
		next_msgblock = ISC_LIST_NEXT(msgblock, link);
		ISC_LIST_UNLINK(msg->rdatas, msgblock, link);
		msgblock_free(msg->mctx, msgblock);
		msgblock = next_msgblock;
	}

Michael Graff's avatar
Michael Graff committed
423
424
425
426
427
428
429
430
431
432
433
434
435
	msgblock = ISC_LIST_HEAD(msg->rdatasets);
	INSIST(msgblock != NULL);
	if (everything == ISC_FALSE) {
		msgblock_reset(msgblock, RDATASET_COUNT);
		msgblock = ISC_LIST_NEXT(msgblock, link);
	}
	while (msgblock != NULL) {
		next_msgblock = ISC_LIST_NEXT(msgblock, link);
		ISC_LIST_UNLINK(msg->rdatasets, msgblock, link);
		msgblock_free(msg->mctx, msgblock);
		msgblock = next_msgblock;
	}

Michael Graff's avatar
Michael Graff committed
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
	/*
	 * "rdatalists" is special, since it is possible (but not recommended)
	 * that the message could be switched from parse into render mode.
	 *
	 * Note that the reverse is not possible -- switching to parse after
	 * render will not work, and should not.
	 */

	msgblock = ISC_LIST_HEAD(msg->rdatalists);
	if (everything == ISC_FALSE) {
		msgblock_reset(msgblock, RDATALIST_COUNT);
		msgblock = ISC_LIST_NEXT(msgblock, link);
	}
	while (msgblock != NULL) {
		next_msgblock = ISC_LIST_NEXT(msgblock, link);
		ISC_LIST_UNLINK(msg->rdatalists, msgblock, link);
		msgblock_free(msg->mctx, msgblock);
		msgblock = next_msgblock;
Michael Graff's avatar
Michael Graff committed
454
455
	}

Michael Graff's avatar
Michael Graff committed
456
457
458
	if (msg->need_cctx_cleanup)
		dns_compress_invalidate(&msg->cctx);

Michael Graff's avatar
Michael Graff committed
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
	/*
	 * Set other bits to normal default values.
	 */
	msginit(msg);
}

dns_result_t
dns_message_create(isc_mem_t *mctx, dns_message_t **msg, unsigned int intent)
{
	dns_message_t *m;
	isc_result_t iresult;
	dns_msgblock_t *msgblock;
	isc_dynbuffer_t *dynbuf;
	unsigned int i;

	REQUIRE(mctx != NULL);
	REQUIRE(msg != NULL);
	REQUIRE(*msg == NULL);
	REQUIRE(intent == DNS_MESSAGE_INTENT_PARSE
		|| intent == DNS_MESSAGE_INTENT_RENDER);

	m = isc_mem_get(mctx, sizeof(dns_message_t));
	if (m == NULL)
		return(DNS_R_NOMEMORY);

	m->magic = MESSAGE_MAGIC;
	m->from_to_wire = intent;
	msginit(m);
	for (i = 0 ; i < DNS_SECTION_MAX ; i++)
		ISC_LIST_INIT(m->sections[i]);
	m->mctx = mctx;
	ISC_LIST_INIT(m->scratchpad);
	ISC_LIST_INIT(m->names);
	ISC_LIST_INIT(m->rdatas);
	ISC_LIST_INIT(m->rdatalists);

	dynbuf = NULL;
	iresult = isc_dynbuffer_allocate(mctx, &dynbuf, SCRATCHPAD_SIZE,
					 ISC_BUFFERTYPE_BINARY);
	if (iresult != ISC_R_SUCCESS)
		goto cleanup1;
	ISC_LIST_APPEND(m->scratchpad, dynbuf, link);

	msgblock = msgblock_allocate(mctx, sizeof(dns_name_t),
				     NAME_COUNT);
	if (msgblock == NULL)
		goto cleanup2;
	ISC_LIST_APPEND(m->names, msgblock, link);

	msgblock = msgblock_allocate(mctx, sizeof(dns_rdata_t),
				     RDATA_COUNT);
	if (msgblock == NULL)
		goto cleanup3;
	ISC_LIST_APPEND(m->rdatas, msgblock, link);

Michael Graff's avatar
Michael Graff committed
514
515
516
517
	msgblock = msgblock_allocate(mctx, sizeof(dns_rdataset_t),
				     RDATASET_COUNT);
	if (msgblock == NULL)
		goto cleanup4;
518
	ISC_LIST_APPEND(m->rdatasets, msgblock, link);
Michael Graff's avatar
Michael Graff committed
519

Michael Graff's avatar
Michael Graff committed
520
521
522
523
	if (intent == DNS_MESSAGE_INTENT_PARSE) {
		msgblock = msgblock_allocate(mctx, sizeof(dns_rdatalist_t),
					     RDATALIST_COUNT);
		if (msgblock == NULL)
Michael Graff's avatar
Michael Graff committed
524
			goto cleanup5;
Michael Graff's avatar
Michael Graff committed
525
526
527
		ISC_LIST_APPEND(m->rdatalists, msgblock, link);
	}

528
	*msg = m;
Michael Graff's avatar
Michael Graff committed
529
530
531
532
533
	return (DNS_R_SUCCESS);

	/*
	 * Cleanup for error returns.
	 */
Michael Graff's avatar
Michael Graff committed
534
535
536
 cleanup5:
	msgblock = ISC_LIST_HEAD(m->rdatasets);
	msgblock_free(mctx, msgblock);
Michael Graff's avatar
Michael Graff committed
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
 cleanup4:
	msgblock = ISC_LIST_HEAD(m->rdatas);
	msgblock_free(mctx, msgblock);
 cleanup3:
	msgblock = ISC_LIST_HEAD(m->names);
	msgblock_free(mctx, msgblock);
 cleanup2:
	dynbuf = ISC_LIST_HEAD(m->scratchpad);
	isc_dynbuffer_free(mctx, &dynbuf);
 cleanup1:
	m->magic = 0;
	isc_mem_put(mctx, m, sizeof(dns_message_t));

	return (DNS_R_NOMEMORY);
}

void
dns_message_reset(dns_message_t *msg)
{
	msgreset(msg, ISC_FALSE);
}

void
dns_message_destroy(dns_message_t **xmsg)
{
	dns_message_t *msg;

	REQUIRE(xmsg != NULL);
	REQUIRE(VALID_MESSAGE(*xmsg));

	msg = *xmsg;
	*xmsg = NULL;

	msgreset(msg, ISC_TRUE);
	msg->magic = 0;
	isc_mem_put(msg->mctx, msg, sizeof(dns_message_t));
}

Michael Graff's avatar
Michael Graff committed
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
static dns_result_t
findname(dns_name_t **foundname, dns_name_t *target, dns_namelist_t *section)
{
	dns_name_t *curr;

	for (curr = ISC_LIST_TAIL(*section) ;
	     curr != NULL ;
	     curr = ISC_LIST_PREV(curr, link)) {
		if (dns_name_compare(curr, target) == 0) {
			if (foundname != NULL)
				*foundname = curr;
			return (DNS_R_SUCCESS);
		}
	}

	return (DNS_R_NOTFOUND);
}

static dns_result_t
findtype(dns_rdataset_t **rdataset, dns_name_t *name, dns_rdatatype_t type)
{
	dns_rdataset_t *curr;

	for (curr = ISC_LIST_TAIL(name->list) ;
	     curr != NULL ;
	     curr = ISC_LIST_PREV(curr, link)) {
		if (curr->type == type) {
			if (rdataset != NULL)
				*rdataset = curr;
			return (DNS_R_SUCCESS);
		}
	}

	return (DNS_R_NOTFOUND);
}

/*
 * Read a name from buffer "source".
 *
 * Assumes dns_name_init() was already called on this name.
 */
static dns_result_t
getname(dns_name_t *name, isc_buffer_t *source, dns_message_t *msg,
	dns_decompress_t *dctx)
{
	isc_buffer_t *scratch;
	dns_result_t result;
	unsigned int tries;

	scratch = currentbuffer(msg);

	if (dns_decompress_edns(dctx) > 1 || !dns_decompress_strict(dctx))
		dns_decompress_setmethods(dctx, DNS_COMPRESS_GLOBAL);
	else
		dns_decompress_setmethods(dctx, DNS_COMPRESS_GLOBAL14);

	tries = 0;
	while (tries < 2) {
		result = dns_name_fromwire(name, source, dctx, ISC_FALSE,
					   scratch);

		if (result == DNS_R_NOSPACE) {
			tries++;

			result = newbuffer(msg);
			if (result != DNS_R_SUCCESS)
				return (result);

			scratch = currentbuffer(msg);
		} else {
			return (result);
		}
	}

	return (DNS_R_UNEXPECTED);  /* should never get here... XXXMLG */
}

652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
static dns_result_t
getrdata(dns_name_t *name, isc_buffer_t *source, dns_message_t *msg,
	 dns_decompress_t *dctx, dns_rdataclass_t rdclass,
	 dns_rdatatype_t rdtype, unsigned int rdatalen, dns_rdata_t *rdata)
{
	isc_buffer_t *scratch;
	dns_result_t result;
	unsigned int tries;

	scratch = currentbuffer(msg);

	isc_buffer_setactive(source, rdatalen);
	dns_decompress_localinit(dctx, name, source);

	tries = 0;
	while (tries < 2) {
		result = dns_rdata_fromwire(rdata, rdclass, rdtype,
					    source, dctx, ISC_FALSE,
					    scratch);

		if (result == DNS_R_NOSPACE) {
			tries++;

			result = newbuffer(msg);
			if (result != DNS_R_SUCCESS)
				return (result);

			scratch = currentbuffer(msg);
		} else {
			return (result);
		}
	}

	return (DNS_R_UNEXPECTED);  /* should never get here... XXXMLG */
}
Michael Graff's avatar
Michael Graff committed
687

Michael Graff's avatar
Michael Graff committed
688
689
690
691
692
693
694
695
696
697
static dns_result_t
getquestions(isc_buffer_t *source, dns_message_t *msg, dns_decompress_t *dctx)
{
	isc_region_t r;
	unsigned int count;
	dns_name_t *name;
	dns_name_t *name2;
	dns_rdataset_t *rdataset;
	dns_rdatalist_t *rdatalist;
	dns_result_t result;
Michael Graff's avatar
Michael Graff committed
698
699
700
	dns_rdatatype_t rdtype;
	dns_rdataclass_t rdclass;
	dns_namelist_t *section;
Michael Graff's avatar
Michael Graff committed
701

Michael Graff's avatar
Michael Graff committed
702
	section = &msg->sections[DNS_SECTION_QUESTION];
Michael Graff's avatar
Michael Graff committed
703

Michael Graff's avatar
Michael Graff committed
704
	for (count = 0 ; count < msg->counts[DNS_SECTION_QUESTION] ; count++) {
Michael Graff's avatar
Michael Graff committed
705
706
707
708
709
710
711
		name = newname(msg);
		if (name == NULL)
			return (DNS_R_NOMEMORY);

		/*
		 * Parse the name out of this packet.
		 */
712
713
		isc_buffer_remaining(source, &r);
		isc_buffer_setactive(source, r.length);
Michael Graff's avatar
Michael Graff committed
714
715
716
717
718
719
720
721
722
723
		result = getname(name, source, msg, dctx);
		if (result != DNS_R_SUCCESS)
			return (result);

		/*
		 * Run through the section, looking to see if this name
		 * is already there.  If it is found, put back the allocated
		 * name since we no longer need it, and set our name pointer
		 * to point to the name we found.
		 */
Michael Graff's avatar
Michael Graff committed
724
		result = findname(&name2, name, section);
Michael Graff's avatar
Michael Graff committed
725
726

		/*
727
728
729
730
		 * If it is the first name in the section, accept it.
		 *
		 * If it is not, but is not the same as the name already
		 * in the question section, append to the section.  Note that
Michael Graff's avatar
Michael Graff committed
731
732
733
734
		 * here in the question section this is illegal, so return
		 * FORMERR.  In the future, check the opcode to see if
		 * this should be legal or not.  In either case we no longer
		 * need this name pointer.
Michael Graff's avatar
Michael Graff committed
735
		 */
736
737
738
739
740
741
742
743
744
		if (result != DNS_R_SUCCESS) {
			if (ISC_LIST_EMPTY(*section)) {
				ISC_LIST_APPEND(*section, name, link);
			} else {
				return (DNS_R_FORMERR);
			}
		} else {
			name = name2;
		}
Michael Graff's avatar
Michael Graff committed
745
746
747
748

		/*
		 * Get type and class.
		 */
Michael Graff's avatar
Michael Graff committed
749
750
751
752
753
		isc_buffer_remaining(source, &r);
		if (r.length < 4)
			return (DNS_R_UNEXPECTEDEND);
		rdtype = isc_buffer_getuint16(source);
		rdclass = isc_buffer_getuint16(source);
Michael Graff's avatar
Michael Graff committed
754
755

		/*
Michael Graff's avatar
Michael Graff committed
756
757
		 * If this class is different than the one we alrady read,
		 * this is an error.
Michael Graff's avatar
Michael Graff committed
758
		 */
Michael Graff's avatar
Michael Graff committed
759
760
761
762
763
764
		if (msg->state == DNS_SECTION_ANY) {
			msg->state = DNS_SECTION_QUESTION;
			msg->rdclass = rdclass;
		} else if (msg->rdclass != rdclass)
			return (DNS_R_FORMERR);
		
Michael Graff's avatar
Michael Graff committed
765
		/*
Michael Graff's avatar
Michael Graff committed
766
		 * Search name for the particular type and class.
Michael Graff's avatar
Michael Graff committed
767
768
		 * If it was found, this is an error, return FORMERR.
		 */
Michael Graff's avatar
Michael Graff committed
769
770
771
772
		result = findtype(NULL, name, rdtype);

		if (result == DNS_R_SUCCESS)
			return (DNS_R_FORMERR);
Michael Graff's avatar
Michael Graff committed
773
774
775
776

		/*
		 * Allocate a new rdatalist.
		 */
Michael Graff's avatar
Michael Graff committed
777
778
		rdatalist = newrdatalist(msg);
		rdataset = newrdataset(msg);
Michael Graff's avatar
Michael Graff committed
779
780
781
782
783

		/*
		 * Convert rdatalist to rdataset, and attach the latter to
		 * the name.
		 */
Michael Graff's avatar
Michael Graff committed
784
785
786
787
788
		rdatalist->type = rdtype;
		rdatalist->rdclass = rdclass;
		rdatalist->ttl = 0;
		ISC_LIST_INIT(rdatalist->rdata);

789
		dns_rdataset_init(rdataset);
Michael Graff's avatar
Michael Graff committed
790
791
792
793
794
		result = dns_rdatalist_tordataset(rdatalist, rdataset);
		if (result != DNS_R_SUCCESS)
			return (result);

		ISC_LIST_APPEND(name->list, rdataset, link);
Michael Graff's avatar
Michael Graff committed
795
796
797
798
799
800
801
	}
	
	return (DNS_R_SUCCESS);
}

static dns_result_t
getsection(isc_buffer_t *source, dns_message_t *msg, dns_decompress_t *dctx,
Michael Graff's avatar
Michael Graff committed
802
	   dns_section_t sectionid)
Michael Graff's avatar
Michael Graff committed
803
{
Michael Graff's avatar
Michael Graff committed
804
805
	isc_region_t r;
	unsigned int count;
Michael Graff's avatar
Michael Graff committed
806
	unsigned int rdatalen;
Michael Graff's avatar
Michael Graff committed
807
808
809
810
811
812
813
	dns_name_t *name;
	dns_name_t *name2;
	dns_rdataset_t *rdataset;
	dns_rdatalist_t *rdatalist;
	dns_result_t result;
	dns_rdatatype_t rdtype;
	dns_rdataclass_t rdclass;
814
	dns_rdata_t *rdata;
Michael Graff's avatar
Michael Graff committed
815
816
817
818
	dns_ttl_t ttl;
	dns_namelist_t *section;

	section = &msg->sections[sectionid];
Michael Graff's avatar
Michael Graff committed
819

Michael Graff's avatar
Michael Graff committed
820
821
822
823
824
825
826
827
	for (count = 0 ; count < msg->counts[sectionid] ; count++) {
		name = newname(msg);
		if (name == NULL)
			return (DNS_R_NOMEMORY);

		/*
		 * Parse the name out of this packet.
		 */
828
829
		isc_buffer_remaining(source, &r);
		isc_buffer_setactive(source, r.length);
Michael Graff's avatar
Michael Graff committed
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
		result = getname(name, source, msg, dctx);
		if (result != DNS_R_SUCCESS)
			return (result);

		/*
		 * Run through the section, looking to see if this name
		 * is already there.  If it is found, put back the allocated
		 * name since we no longer need it, and set our name pointer
		 * to point to the name we found.
		 */
		result = findname(&name2, name, section);

		/*
		 * If it is a new name, append to the section.
		 */
		if (result == DNS_R_SUCCESS) {
			releasename(msg, name);
			name = name2;
848
849
		} else {
			ISC_LIST_APPEND(*section, name, link);
Michael Graff's avatar
Michael Graff committed
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
		}

		/*
		 * Get type, class, ttl, and rdatalen.  Verify that at least
		 * rdatalen bytes remain.  (Some of this is deferred to
		 * later.
		 */
		isc_buffer_remaining(source, &r);
		if (r.length < 10)
			return (DNS_R_UNEXPECTEDEND);
		rdtype = isc_buffer_getuint16(source);
		rdclass = isc_buffer_getuint16(source);

		/*
		 * If this class is different than the one we already read,
		 * this is an error.
		 */
		if (msg->state == DNS_SECTION_ANY) {
			msg->state = sectionid;
			msg->rdclass = rdclass;
		} else if (msg->rdclass != rdclass)
			return (DNS_R_FORMERR);
		
		/*
		 * ... now get ttl and rdatalen, and check buffer.
		 */
		ttl = isc_buffer_getuint32(source);
		rdatalen = isc_buffer_getuint16(source);
		r.length -= 10;
		if (r.length < rdatalen)
			return (DNS_R_UNEXPECTEDEND);

		/*
		 * Search name for the particular type and class.
		 * If it was found, this is an error, return FORMERR.
		 */
		result = findtype(&rdataset, name, rdtype);

		/*
889
890
891
892
893
		 * If we found an rdataset that matches, we need to
		 * append this rdata to that set.  If we did not, we need
		 * to create a new rdatalist, store the important bits there,
		 * convert it to an rdataset, and link the latter to the name.
		 * Yuck.
Michael Graff's avatar
Michael Graff committed
894
895
896
897
898
		 */
		if (result != DNS_R_SUCCESS) {
			rdataset = newrdataset(msg);
			if (rdataset == NULL)
				return (DNS_R_NOMEMORY);
899
900
901
			rdatalist = newrdatalist(msg);
			if (rdatalist == NULL)
				return (DNS_R_NOMEMORY);
Michael Graff's avatar
Michael Graff committed
902

903
904
905
906
			rdatalist->type = rdtype;
			rdatalist->rdclass = rdclass;
			rdatalist->ttl = ttl;
			ISC_LIST_INIT(rdatalist->rdata);
Michael Graff's avatar
Michael Graff committed
907

908
909
910
911
912
			dns_rdataset_init(rdataset);
			dns_rdatalist_tordataset(rdatalist, rdataset);

			ISC_LIST_APPEND(name->list, rdataset, link);
		}
Michael Graff's avatar
Michael Graff committed
913
914

		/*
915
		 * Read the rdata from the wire format.
Michael Graff's avatar
Michael Graff committed
916
		 */
917
918
919
920
921
		rdata = newrdata(msg);
		if (rdata == NULL)
			return (DNS_R_NOMEMORY);
		result = getrdata(name, source, msg, dctx,
				  rdclass, rdtype, rdatalen, rdata);
Michael Graff's avatar
Michael Graff committed
922
923
924
		if (result != DNS_R_SUCCESS)
			return (result);

925
926
927
928
929
930
931
932
933
		/*
		 * XXX Perform a totally ugly hack here to pull
		 * the rdatalist out of the private field in the rdataset,
		 * and append this rdata to the rdatalist's linked list
		 * of rdata.
		 */
		rdatalist = (dns_rdatalist_t *)(rdataset->private1);

		ISC_LIST_APPEND(rdatalist->rdata, rdata, link);
Michael Graff's avatar
Michael Graff committed
934
935
936
	}
	
	return (DNS_R_SUCCESS);
Michael Graff's avatar
Michael Graff committed
937
938
}

Michael Graff's avatar
Michael Graff committed
939
dns_result_t
Michael Graff's avatar
Michael Graff committed
940
dns_message_parse(dns_message_t *msg, isc_buffer_t *source)
Michael Graff's avatar
Michael Graff committed
941
{
Michael Graff's avatar
Michael Graff committed
942
943
944
	isc_region_t r;
	dns_decompress_t dctx;
	dns_result_t ret;
945
	isc_uint16_t tmpflags;
Michael Graff's avatar
Michael Graff committed
946
947

	REQUIRE(VALID_MESSAGE(msg));
Michael Graff's avatar
Michael Graff committed
948
	REQUIRE(source != NULL);
949
	REQUIRE(msg->from_to_wire == DNS_MESSAGE_INTENT_PARSE);
Michael Graff's avatar
Michael Graff committed
950

Michael Graff's avatar
Michael Graff committed
951
	isc_buffer_remaining(source, &r);
952
	if (r.length < DNS_MESSAGE_HEADER_LEN)
Michael Graff's avatar
Michael Graff committed
953
954
955
		return (DNS_R_UNEXPECTEDEND);

	msg->id = isc_buffer_getuint16(source);
956
957
958
959
960
	tmpflags = isc_buffer_getuint16(source);
	msg->opcode = ((tmpflags & DNS_MESSAGE_OPCODE_MASK)
		       >> DNS_MESSAGE_OPCODE_SHIFT);
	msg->rcode = (tmpflags & DNS_MESSAGE_RCODE_MASK);
	msg->flags = (tmpflags & ~DNS_MESSAGE_FLAG_MASK);
Michael Graff's avatar
Michael Graff committed
961
962
963
964
	msg->counts[DNS_SECTION_QUESTION] = isc_buffer_getuint16(source);
	msg->counts[DNS_SECTION_ANSWER] = isc_buffer_getuint16(source);
	msg->counts[DNS_SECTION_AUTHORITY] = isc_buffer_getuint16(source);
	msg->counts[DNS_SECTION_ADDITIONAL] = isc_buffer_getuint16(source);
Michael Graff's avatar
Michael Graff committed
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983

	dns_decompress_init(&dctx, -1, ISC_FALSE);

	ret = getquestions(source, msg, &dctx);
	if (ret != DNS_R_SUCCESS)
		return (ret);

	ret = getsection(source, msg, &dctx, DNS_SECTION_ANSWER);
	if (ret != DNS_R_SUCCESS)
		return (ret);

	ret = getsection(source, msg, &dctx, DNS_SECTION_AUTHORITY);
	if (ret != DNS_R_SUCCESS)
		return (ret);

	ret = getsection(source, msg, &dctx, DNS_SECTION_ADDITIONAL);
	if (ret != DNS_R_SUCCESS)
		return (ret);

984
985
986
987
	isc_buffer_remaining(source, &r);
	if (r.length != 0)
		return (DNS_R_FORMERR);

Michael Graff's avatar
Michael Graff committed
988
989
990
991
992
	/*
	 * XXXMLG Need to check the tsig(s) here...
	 */

	return (DNS_R_SUCCESS);
Michael Graff's avatar
Michael Graff committed
993
994
995
996
997
}

dns_result_t
dns_message_renderbegin(dns_message_t *msg, isc_buffer_t *buffer)
{
998
	isc_region_t r;
Michael Graff's avatar
Michael Graff committed
999
	dns_result_t result;
1000

Michael Graff's avatar
Michael Graff committed
1001
1002
	REQUIRE(VALID_MESSAGE(msg));
	REQUIRE(buffer != NULL);
1003
	REQUIRE(msg->buffer == NULL);
1004
	REQUIRE(msg->from_to_wire == DNS_MESSAGE_INTENT_RENDER);
Michael Graff's avatar
Michael Graff committed
1005

1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
	/*
	 * Erase the contents of this buffer.
	 */
	isc_buffer_clear(buffer);

	/*
	 * Make certain there is enough for at least the header in this
	 * buffer.
	 */
	isc_buffer_available(buffer, &r);
	if (r.length < DNS_MESSAGE_HEADER_LEN)
		return (DNS_R_NOSPACE);

Michael Graff's avatar
Michael Graff committed
1019
1020
1021
1022
1023
	result = dns_compress_init(&msg->cctx, -1, msg->mctx);
	if (result != DNS_R_SUCCESS)
		return (result);
	msg->need_cctx_cleanup = ISC_TRUE;

1024
1025
1026
1027
1028
	/*
	 * Reserve enough space for the header in this buffer.
	 */
	isc_buffer_add(buffer, DNS_MESSAGE_HEADER_LEN);

Michael Graff's avatar
Michael Graff committed
1029
1030
	msg->buffer = buffer;

1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
	return (DNS_R_SUCCESS);
}

dns_result_t
dns_message_renderchangebuffer(dns_message_t *msg, isc_buffer_t *buffer)
{
	isc_region_t r, rn;

	REQUIRE(VALID_MESSAGE(msg));
	REQUIRE(buffer != NULL);
	REQUIRE(msg->buffer != NULL);

	/*
	 * ensure that the new buffer is empty, and has enough space to
	 * hold the current contents.
	 */
	isc_buffer_clear(buffer);

	isc_buffer_available(buffer, &rn);
	isc_buffer_used(msg->buffer, &r);
	if (rn.length < r.length)
		return (DNS_R_NOSPACE);

	/*
	 * Copy the contents from the old to the new buffer.
	 */
	isc_buffer_add(buffer, r.length);
	memcpy(rn.base, r.base, r.length);

	msg->buffer = buffer;

	return (DNS_R_SUCCESS);
Michael Graff's avatar
Michael Graff committed
1063
1064
1065
1066
1067
1068
}

dns_result_t
dns_message_renderrelease(dns_message_t *msg, unsigned int space)
{
	REQUIRE(VALID_MESSAGE(msg));
1069
	REQUIRE(msg->buffer != NULL);
Michael Graff's avatar
Michael Graff committed
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079

	if (msg->reserved < space)
		return (DNS_R_NOSPACE);

	msg->reserved -= space;

	return (DNS_R_SUCCESS);
}

dns_result_t
1080
dns_message_renderreserve(dns_message_t *msg, unsigned int space)
Michael Graff's avatar
Michael Graff committed
1081
1082
1083
1084
{
	isc_region_t r;

	REQUIRE(VALID_MESSAGE(msg));
1085
	REQUIRE(msg->buffer != NULL);
Michael Graff's avatar
Michael Graff committed
1086
1087
1088
1089
1090
1091
1092

	/*
	 * "space" can be positive or negative.  If it is negative we are
	 * removing our reservation of space.  If it is positive, we are
	 * requesting more space to be reserved.
	 */

1093
	isc_buffer_available(msg->buffer, &r);
Michael Graff's avatar
Michael Graff committed
1094
1095
1096
1097
1098
1099
1100
1101
1102
	if (r.length < (space + msg->reserved))
		return (DNS_R_NOSPACE);

	msg->reserved += space;

	return (DNS_R_SUCCESS);
}

dns_result_t
1103
dns_message_rendersection(dns_message_t *msg, dns_section_t sectionid,
1104
			  unsigned int priority, unsigned int flags)
Michael Graff's avatar
Michael Graff committed
1105
{
1106
1107
1108
	isc_region_t r;
	unsigned int used;
	dns_namelist_t *section;
Michael Graff's avatar
Michael Graff committed
1109
1110
1111
1112
1113
	dns_name_t *name, *next_name;
	dns_rdataset_t *rdataset, *next_rdataset;
	unsigned int count, total;
	isc_boolean_t no_render_rdata;
	dns_result_t result;
1114

Michael Graff's avatar
Michael Graff committed
1115
	REQUIRE(VALID_MESSAGE(msg));
1116
	REQUIRE(msg->buffer != NULL);
1117
1118
1119
	REQUIRE(VALID_NAMED_SECTION(sectionid));

	section = &msg->sections[sectionid];
Michael Graff's avatar
Michael Graff committed
1120
1121
1122
1123
1124
1125
1126
1127
1128
	if (sectionid == DNS_SECTION_QUESTION)
		no_render_rdata = ISC_TRUE;
	else
		no_render_rdata = ISC_FALSE;

	name = ISC_LIST_HEAD(*section);
	if (name == NULL)
		return (ISC_R_SUCCESS);
	/*
1129
	 * Shrink the space in the buffer by the reserved amount.
Michael Graff's avatar
Michael Graff committed
1130
	 */
1131
1132
	msg->buffer->length -= msg->reserved;

Michael Graff's avatar
Michael Graff committed
1133
1134
	printf("---Start rendering section %u, count %u\n",
	       sectionid, msg->counts[sectionid]);
Michael Graff's avatar
Michael Graff committed
1135

1136

Michael Graff's avatar
Michael Graff committed
1137
1138
	total = 0;
	while (name != NULL) {
Michael Graff's avatar
Michael Graff committed
1139
1140
1141
1142
1143
		next_name = ISC_LIST_NEXT(name, link);

		rdataset = ISC_LIST_HEAD(name->list);
		while (rdataset != NULL) {
			next_rdataset = ISC_LIST_NEXT(rdataset, link);
1144
			used = msg->buffer->used;
Michael Graff's avatar
Michael Graff committed
1145
1146

			printf("Rendering rdataset, used = %u\n", used);
Michael Graff's avatar
Michael Graff committed
1147

Michael Graff's avatar
Michael Graff committed
1148
			count = 0;
Michael Graff's avatar
Michael Graff committed
1149
			result = dns_rdataset_towire(rdataset, name,
1150
1151
						     &msg->cctx,
						     no_render_rdata,
1152
						     msg->buffer, &count);
Michael Graff's avatar
Michael Graff committed
1153

Michael Graff's avatar
Michael Graff committed
1154
1155
1156
			total += count;
			printf("Rendered %u rdata, total %u\n", count, total);

Michael Graff's avatar
Michael Graff committed
1157
1158
1159
1160
			/*
			 * If out of space, record stats on what we rendered
			 * so far, and return that status.
			 */
1161
			if (result != DNS_R_SUCCESS) {
1162
				msg->buffer->length += msg->reserved;
Michael Graff's avatar
Michael Graff committed
1163
1164
				msg->counts[sectionid] += total;
				return (result);
1165
			}
Michael Graff's avatar
Michael Graff committed
1166
1167

			ISC_LIST_UNLINK(name->list, rdataset, link);
Michael Graff's avatar
Michael Graff committed
1168
1169
1170

			dns_rdataset_disassociate(rdataset);

Michael Graff's avatar
Michael Graff committed
1171
1172
1173
1174
1175
1176
			rdataset = next_rdataset;
		}

		ISC_LIST_UNLINK(*section, name, link);
		name = next_name;
	}
Michael Graff's avatar
Michael Graff committed
1177

1178
	msg->buffer->length += msg->reserved;
Michael Graff's avatar
Michael Graff committed
1179
	msg->counts[sectionid] += total;
Michael Graff's avatar
Michael Graff committed
1180
1181
1182

	printf("Done with section %u, count %u, total rendered %u\n",
	       sectionid, msg->counts[sectionid], total);
Michael Graff's avatar
Michael Graff committed
1183

1184
	return (ISC_R_SUCCESS);
Michael Graff's avatar
Michael Graff committed
1185
1186
1187
}

dns_result_t
1188
dns_message_renderend(dns_message_t *msg)
Michael Graff's avatar
Michael Graff committed
1189
{
Michael Graff's avatar
Michael Graff committed
1190
1191
1192
1193
	isc_buffer_t tmpbuf;
	isc_region_t r;
	isc_uint16_t tmpflags;

Michael Graff's avatar
Michael Graff committed
1194
	REQUIRE(VALID_MESSAGE(msg));
1195
1196
	REQUIRE(msg->buffer != NULL);

Michael Graff's avatar
Michael Graff committed
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
	isc_buffer_used(msg->buffer, &r);
	isc_buffer_init(&tmpbuf, r.base, r.length, ISC_BUFFERTYPE_BINARY);

	isc_buffer_putuint16(&tmpbuf, msg->id);

	tmpflags = ((msg->opcode << DNS_MESSAGE_OPCODE_SHIFT)
		    & DNS_MESSAGE_OPCODE_MASK);
	tmpflags |= (msg->rcode & DNS_MESSAGE_RCODE_MASK);  /* XXX edns? */
	tmpflags |= (msg->flags & DNS_MESSAGE_FLAG_MASK);

	isc_buffer_putuint16(&tmpbuf, tmpflags);
	isc_buffer_putuint16(&tmpbuf, msg->counts[DNS_SECTION_QUESTION]);
	isc_buffer_putuint16(&tmpbuf, msg->counts[DNS_SECTION_ANSWER]);
	isc_buffer_putuint16(&tmpbuf, msg->counts[DNS_SECTION_AUTHORITY]);
	isc_buffer_putuint16(&tmpbuf, msg->counts[DNS_SECTION_ADDITIONAL]);

1213
	msg->buffer = NULL;  /* forget about this buffer only on success XXX */
Michael Graff's avatar
Michael Graff committed
1214

Michael Graff's avatar
Michael Graff committed
1215
1216
1217
	dns_compress_invalidate(&msg->cctx);
	msg->need_cctx_cleanup = ISC_FALSE;

Michael Graff's avatar
Michael Graff committed
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
	/* XXX implement */
	return (ISC_R_NOTIMPLEMENTED);
}

dns_result_t
dns_message_firstname(dns_message_t *msg, dns_section_t section)
{
	REQUIRE(VALID_MESSAGE(msg));
	REQUIRE(VALID_NAMED_SECTION(section));

	msg->cursors[section] = ISC_LIST_HEAD(msg->sections[section]);

	if (msg->cursors[section] == NULL)
		return (DNS_R_NOMORE);

	return (ISC_R_SUCCESS);
}

dns_result_t
dns_message_nextname(dns_message_t *msg, dns_section_t section)
{
	REQUIRE(VALID_MESSAGE(msg));
	REQUIRE(VALID_NAMED_SECTION(section));
	REQUIRE(msg->cursors[section] != NULL);
	
	msg->cursors[section] = ISC_LIST_NEXT(msg->cursors[section], link);

	if (msg->cursors[section] == NULL)
		return (DNS_R_NOMORE);

	return (ISC_R_SUCCESS);
}

void
dns_message_currentname(dns_message_t *msg, dns_section_t section,
			dns_name_t **name)
{
	REQUIRE(VALID_MESSAGE(msg));
	REQUIRE(VALID_NAMED_SECTION(section));
1257
	REQUIRE(name != NULL && *name == NULL);
Michael Graff's avatar
Michael Graff committed
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
	REQUIRE(msg->cursors[section] != NULL);

	*name = msg->cursors[section];
}

dns_result_t
dns_message_findname(dns_message_t *msg, dns_section_t section,
		     dns_name_t *target, dns_rdatatype_t type,
		     dns_name_t **name, dns_rdataset_t **rdataset)
{
1268
1269
	dns_name_t *foundname;
	dns_result_t result;
Michael Graff's avatar
Michael Graff committed
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288

	/*
	 * XXX These requirements are probably too intensive, especially
	 * where things can be NULL, but as they are they ensure that if
	 * something is NON-NULL, indicating that the caller expects it
	 * to be filled in, that we can in fact fill it in.
	 */
	REQUIRE(msg != NULL);
	REQUIRE(VALID_SECTION(section));
	REQUIRE(target != NULL);
	if (name != NULL)
		REQUIRE(*name == NULL);
	if (type == dns_rdatatype_any) {
		REQUIRE(rdataset == NULL);
	} else {
		if (rdataset != NULL)
			REQUIRE(*rdataset == NULL);
	}

1289
1290
1291
1292
	/*
	 * Search through, looking for the name.
	 */
	result = findname(&foundname, target, &msg->sections[section]);
1293
1294
1295
	if (result == DNS_R_NOTFOUND)
		return (DNS_R_NXDOMAIN);
	else if (result != DNS_R_SUCCESS)
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
		return (result);

	if (name != NULL)
		*name = foundname;

	/*
	 * And now look for the type.
	 */
	if (rdataset == NULL)
		return (DNS_R_SUCCESS);

	result = findtype(rdataset, foundname, type);
1308
1309
1310
	if (result == DNS_R_NOTFOUND)
		return (DNS_R_NXRDATASET);

1311
	return (result);
Michael Graff's avatar
Michael Graff committed
1312
1313
1314
1315
1316
1317
1318
1319
}

void
dns_message_movename(dns_message_t *msg, dns_name_t *name,
		     dns_section_t fromsection,
		     dns_section_t tosection)
{
	REQUIRE(msg != NULL);
1320
	REQUIRE(msg->from_to_wire == DNS_MESSAGE_INTENT_RENDER);
Michael Graff's avatar
Michael Graff committed
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
	REQUIRE(name != NULL);
	REQUIRE(VALID_NAMED_SECTION(fromsection));
	REQUIRE(VALID_NAMED_SECTION(tosection));
	REQUIRE(fromsection != tosection);

	/*
	 * Unlink the name from the old section
	 */
	ISC_LIST_UNLINK(msg->sections[fromsection], name, link);
	ISC_LIST_APPEND(msg->sections[tosection], name, link);
}

void
dns_message_addname(dns_message_t *msg, dns_name_t *name,
		    dns_section_t section)
{
	REQUIRE(msg != NULL);
1338
	REQUIRE(msg->from_to_wire == DNS_MESSAGE_INTENT_RENDER);
Michael Graff's avatar
Michael Graff committed
1339
1340
1341
1342
1343
	REQUIRE(name != NULL);
	REQUIRE(VALID_NAMED_SECTION(section));

	ISC_LIST_APPEND(msg->sections[section], name, link);
}