message.h 33.7 KB
Newer Older
1
/*
Mark Andrews's avatar
Mark Andrews committed
2 3
 * Copyright (C) 2004  Internet Systems Consortium, Inc. ("ISC")
 * Copyright (C) 1999-2003  Internet Software Consortium.
4
 *
5 6 7
 * 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.
8
 *
Mark Andrews's avatar
Mark Andrews committed
9 10 11 12 13 14 15
 * THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES WITH
 * REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
 * AND FITNESS.  IN NO EVENT SHALL ISC 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.
16 17
 */

Mark Andrews's avatar
Mark Andrews committed
18
/* $Id: message.h,v 1.114 2004/03/05 05:09:44 marka Exp $ */
David Lawrence's avatar
David Lawrence committed
19

20 21
#ifndef DNS_MESSAGE_H
#define DNS_MESSAGE_H 1
22 23 24 25 26

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

27 28
#include <isc/lang.h>
#include <isc/magic.h>
29

Michael Graff's avatar
Michael Graff committed
30
#include <dns/compress.h>
31
#include <dns/masterdump.h>
32
#include <dns/types.h>
33

34 35
#include <dst/dst.h>

36 37 38
/*
 * How this beast works:
 *
39
 * When a dns message is received in a buffer, dns_message_fromwire() is called
40 41 42 43 44
 * on the memory region.  Various items are checked including the format
 * of the message (if counts are right, if counts consume the entire sections,
 * and if sections consume the entire message) and known pseudo-RRs in the
 * additional data section are analyzed and removed.
 *
Brian Wellington's avatar
Brian Wellington committed
45 46
 * TSIG checking is also done at this layer, and any DNSSEC transaction
 * signatures should also be checked here.
47 48 49 50
 *
 * Notes on using the gettemp*() and puttemp*() functions:
 *
 * These functions return items (names, rdatasets, etc) allocated from some
51 52 53 54
 * internal state of the dns_message_t.
 *
 * Names and rdatasets must be put back into the dns_message_t in
 * one of two ways.  Assume a name was allocated via
55 56 57 58 59 60
 * dns_message_gettempname():
 *
 *	(1) insert it into a section, using dns_message_addname().
 *
 *	(2) return it to the message using dns_message_puttempname().
 *
61 62
 * The same applies to rdatasets.
 *
63
 * On the other hand, offsets, rdatalists and rdatas allocated using
64 65
 * dns_message_gettemp*() will always be freed automatically
 * when the message is reset or destroyed; calling dns_message_puttemp*()
66 67 68
 * on rdatalists and rdatas is optional and serves only to enable the item
 * to be reused multiple times during the lifetime of the message; offsets
 * cannot be reused.
69 70 71 72 73 74 75 76
 *
 * Buffers allocated using isc_buffer_allocate() can be automatically freed
 * as well by giving the buffer to the message using dns_message_takebuffer().
 * Doing this will cause the buffer to be freed using isc_buffer_free()
 * when the section lists are cleared, such as in a reset or in a destroy.
 * Since the buffer itself exists until the message is destroyed, this sort
 * of code can be written:
 *
77
 *	buffer = isc_buffer_allocate(mctx, 512);
78 79 80 81 82 83 84 85 86 87
 *	name = NULL;
 *	name = dns_message_gettempname(message, &name);
 *	dns_name_init(name, NULL);
 *	result = dns_name_fromtext(name, &source, dns_rootname, ISC_FALSE,
 *				   buffer);
 *	dns_message_takebuffer(message, &buffer);
 *
 *
 * TODO:
 *
Brian Wellington's avatar
Brian Wellington committed
88 89
 * XXX Needed:  ways to set and retrieve EDNS information, add rdata to a
 * section, move rdata from one section to another, remove rdata, etc.
90 91
 */

92 93 94 95 96
#define DNS_MESSAGEFLAG_QR		0x8000U
#define DNS_MESSAGEFLAG_AA		0x0400U
#define DNS_MESSAGEFLAG_TC		0x0200U
#define DNS_MESSAGEFLAG_RD		0x0100U
#define DNS_MESSAGEFLAG_RA		0x0080U
Bob Halley's avatar
Bob Halley committed
97 98
#define DNS_MESSAGEFLAG_AD		0x0020U
#define DNS_MESSAGEFLAG_CD		0x0010U
99

100 101
#define DNS_MESSAGEEXTFLAG_DO		0x8000U

102
#define DNS_MESSAGE_REPLYPRESERVE	(DNS_MESSAGEFLAG_RD|DNS_MESSAGEFLAG_CD)
103
#define DNS_MESSAGEEXTFLAG_REPLYPRESERVE (DNS_MESSAGEEXTFLAG_DO)
104

Bob Halley's avatar
Bob Halley committed
105 106
#define DNS_MESSAGE_HEADERLEN		12 /* 6 isc_uint16_t's */

107
#define DNS_MESSAGE_MAGIC		ISC_MAGIC('M','S','G','@')
Michael Graff's avatar
Michael Graff committed
108
#define DNS_MESSAGE_VALID(msg)		ISC_MAGIC_VALID(msg, DNS_MESSAGE_MAGIC)
Michael Graff's avatar
Michael Graff committed
109

Michael Graff's avatar
Michael Graff committed
110 111 112 113
/*
 * Ordering here matters.  DNS_SECTION_ANY must be the lowest and negative,
 * and DNS_SECTION_MAX must be one greater than the last used section.
 */
Michael Graff's avatar
Michael Graff committed
114
typedef int dns_section_t;
Michael Graff's avatar
Michael Graff committed
115
#define DNS_SECTION_ANY			(-1)
Michael Graff's avatar
Michael Graff committed
116 117 118 119
#define DNS_SECTION_QUESTION		0
#define DNS_SECTION_ANSWER		1
#define DNS_SECTION_AUTHORITY		2
#define DNS_SECTION_ADDITIONAL		3
120
#define DNS_SECTION_MAX			4
121

122 123 124 125 126 127 128
typedef int dns_pseudosection_t;
#define DNS_PSEUDOSECTION_ANY		(-1)
#define DNS_PSEUDOSECTION_OPT           0
#define DNS_PSEUDOSECTION_TSIG          1
#define DNS_PSEUDOSECTION_SIG0          2
#define DNS_PSEUDOSECTION_MAX           3

129 130 131
typedef int dns_messagetextflag_t;
#define DNS_MESSAGETEXTFLAG_NOCOMMENTS	0x0001
#define DNS_MESSAGETEXTFLAG_NOHEADERS	0x0002
132

133
/*
Andreas Gustafsson's avatar
Andreas Gustafsson committed
134
 * Dynamic update names for these sections.
135 136 137 138
 */
#define DNS_SECTION_ZONE		DNS_SECTION_QUESTION
#define DNS_SECTION_PREREQUISITE	DNS_SECTION_ANSWER
#define DNS_SECTION_UPDATE		DNS_SECTION_AUTHORITY
Michael Graff's avatar
Michael Graff committed
139 140

/*
Michael Graff's avatar
Michael Graff committed
141
 * These tell the message library how the created dns_message_t will be used.
Michael Graff's avatar
Michael Graff committed
142
 */
Michael Graff's avatar
Michael Graff committed
143 144 145
#define DNS_MESSAGE_INTENTUNKNOWN	0 /* internal use only */
#define DNS_MESSAGE_INTENTPARSE		1 /* parsing messages */
#define DNS_MESSAGE_INTENTRENDER	2 /* rendering */
Michael Graff's avatar
Michael Graff committed
146

147 148 149 150 151 152 153
/*
 * Control behavior of parsing
 */
#define DNS_MESSAGEPARSE_PRESERVEORDER	0x0001	/* preserve rdata order */
#define DNS_MESSAGEPARSE_BESTEFFORT	0x0002	/* return a message if a
						   recoverable parse error
						   occurs */
154 155
#define DNS_MESSAGEPARSE_CLONEBUFFER	0x0004	/* save a copy of the
						   source buffer */
156 157
#define DNS_MESSAGEPARSE_IGNORETRUNCATION 0x0008 /* trucation errors are
						  * not fatal. */
158

Michael Graff's avatar
Michael Graff committed
159 160 161 162
/*
 * Control behavior of rendering
 */
#define DNS_MESSAGERENDER_ORDERED	0x0001	/* don't change order */
163
#define DNS_MESSAGERENDER_PARTIAL	0x0002	/* allow a partial rdataset */
164
#define DNS_MESSAGERENDER_OMITDNSSEC	0x0004	/* omit DNSSEC records */
165 166 167 168
#define DNS_MESSAGERENDER_PREFER_A	0x0008	/* prefer A records in
						 * additional section. */
#define DNS_MESSAGERENDER_PREFER_AAAA	0x0010	/* prefer AAAA records in
						 * additional section. */
Michael Graff's avatar
Michael Graff committed
169

Michael Graff's avatar
Michael Graff committed
170 171
typedef struct dns_msgblock dns_msgblock_t;

Bob Halley's avatar
Bob Halley committed
172
struct dns_message {
Michael Graff's avatar
Michael Graff committed
173
	/* public from here down */
Michael Graff's avatar
Michael Graff committed
174
	unsigned int			magic;
175

176
	dns_messageid_t			id;
Michael Graff's avatar
Michael Graff committed
177
	unsigned int			flags;
178
	dns_rcode_t			rcode;
Michael Graff's avatar
Michael Graff committed
179
	unsigned int			opcode;
Michael Graff's avatar
Michael Graff committed
180
	dns_rdataclass_t		rdclass;
Michael Graff's avatar
Michael Graff committed
181

182
	/* 4 real, 1 pseudo */
Michael Graff's avatar
Michael Graff committed
183
	unsigned int			counts[DNS_SECTION_MAX];
Michael Graff's avatar
Michael Graff committed
184 185

	/* private from here down */
Michael Graff's avatar
Michael Graff committed
186 187
	dns_namelist_t			sections[DNS_SECTION_MAX];
	dns_name_t		       *cursors[DNS_SECTION_MAX];
Bob Halley's avatar
Bob Halley committed
188
	dns_rdataset_t		       *opt;
189
	dns_rdataset_t		       *sig0;
190
	dns_rdataset_t		       *tsig;
191

Michael Graff's avatar
Michael Graff committed
192
	int				state;
Michael Graff's avatar
Michael Graff committed
193
	unsigned int			from_to_wire : 2;
Bob Halley's avatar
Bob Halley committed
194 195
	unsigned int			header_ok : 1;
	unsigned int			question_ok : 1;
196
	unsigned int			tcp_continuation : 1;
197 198
	unsigned int			verified_sig : 1;
	unsigned int			verify_attempted : 1;
199 200
	unsigned int			free_query : 1;
	unsigned int			free_saved : 1;
201

202
	unsigned int			opt_reserved;
203
	unsigned int			sig_reserved;
204
	unsigned int			reserved; /* reserved space (render) */
205

206
	isc_buffer_t		       *buffer;
207
	dns_compress_t		       *cctx;
208

Michael Graff's avatar
Michael Graff committed
209
	isc_mem_t		       *mctx;
210 211 212
	isc_mempool_t		       *namepool;
	isc_mempool_t		       *rdspool;

213
	isc_bufferlist_t		scratchpad;
214 215
	isc_bufferlist_t		cleanup;

Michael Graff's avatar
Michael Graff committed
216 217
	ISC_LIST(dns_msgblock_t)	rdatas;
	ISC_LIST(dns_msgblock_t)	rdatalists;
218
	ISC_LIST(dns_msgblock_t)	offsets;
219 220 221

	ISC_LIST(dns_rdata_t)		freerdata;
	ISC_LIST(dns_rdatalist_t)	freerdatalist;
222 223 224

	dns_rcode_t			tsigstatus;
	dns_rcode_t			querytsigstatus;
Andreas Gustafsson's avatar
Andreas Gustafsson committed
225
	dns_name_t		       *tsigname; /* Owner name of TSIG, if any */
226
	dns_rdataset_t		       *querytsig;
227
	dns_tsigkey_t		       *tsigkey;
Brian Wellington's avatar
Brian Wellington committed
228
	dst_context_t		       *tsigctx;
229
	int				sigstart;
230
	int				timeadjust;
231

Andreas Gustafsson's avatar
Andreas Gustafsson committed
232
	dns_name_t		       *sig0name; /* Owner name of SIG0, if any */
233 234
	dst_key_t		       *sig0key;
	dns_rcode_t			sig0status;
235 236
	isc_region_t			query;
	isc_region_t			saved;
237 238 239

	dns_rdatasetorderfunc_t		order;
	void *				order_arg;
Bob Halley's avatar
Bob Halley committed
240
};
241

242 243 244 245 246 247
/***
 *** Functions
 ***/

ISC_LANG_BEGINDECLS

248
isc_result_t
249
dns_message_create(isc_mem_t *mctx, unsigned int intent, dns_message_t **msgp);
250

251
/*
Michael Graff's avatar
Michael Graff committed
252
 * Create msg structure.
253
 *
Michael Graff's avatar
Michael Graff committed
254
 * This function will allocate some internal blocks of memory that are
Michael Graff's avatar
Michael Graff committed
255
 * expected to be needed for parsing or rendering nearly any type of message.
Michael Graff's avatar
Michael Graff committed
256 257 258
 *
 * Requires:
 *	'mctx' be a valid memory context.
259
 *
Michael Graff's avatar
Michael Graff committed
260
 *	'msgp' be non-null and '*msg' be NULL.
Michael Graff's avatar
Michael Graff committed
261
 *
Andreas Gustafsson's avatar
typo  
Andreas Gustafsson committed
262 263
 *	'intent' must be one of DNS_MESSAGE_INTENTPARSE or
 *	DNS_MESSAGE_INTENTRENDER.
Michael Graff's avatar
Michael Graff committed
264 265
 *
 * Ensures:
Michael Graff's avatar
Michael Graff committed
266
 *	The data in "*msg" is set to indicate an unused and empty msg
Michael Graff's avatar
Michael Graff committed
267 268 269
 *	structure.
 *
 * Returns:
270 271
 *	ISC_R_NOMEMORY		-- out of memory
 *	ISC_R_SUCCESS		-- success
272 273
 */

Michael Graff's avatar
Michael Graff committed
274
void
Bob Halley's avatar
Bob Halley committed
275
dns_message_reset(dns_message_t *msg, unsigned int intent);
Michael Graff's avatar
Michael Graff committed
276 277 278 279 280 281
/*
 * Reset a message structure to default state.  All internal lists are freed
 * or reset to a default state as well.  This is simply a more efficient
 * way to call dns_message_destroy() followed by dns_message_allocate(),
 * since it avoid many memory allocations.
 *
Michael Graff's avatar
Michael Graff committed
282 283 284
 * If any data loanouts (buffers, names, rdatas, etc) were requested,
 * the caller must no longer use them after this call.
 *
Bob Halley's avatar
Bob Halley committed
285 286
 * The intended next use of the message will be 'intent'.
 *
Michael Graff's avatar
Michael Graff committed
287 288 289
 * Requires:
 *
 *	'msg' be valid.
Bob Halley's avatar
Bob Halley committed
290 291
 *
 *	'intent' is DNS_MESSAGE_INTENTPARSE or DNS_MESSAGE_INTENTRENDER
Michael Graff's avatar
Michael Graff committed
292
 */
Michael Graff's avatar
Michael Graff committed
293

Michael Graff's avatar
Michael Graff committed
294
void
Michael Graff's avatar
Michael Graff committed
295
dns_message_destroy(dns_message_t **msgp);
Michael Graff's avatar
Michael Graff committed
296
/*
Michael Graff's avatar
Michael Graff committed
297 298 299 300
 * Destroy all state in the message.
 *
 * Requires:
 *
Michael Graff's avatar
Michael Graff committed
301
 *	'msgp' be valid.
Michael Graff's avatar
Michael Graff committed
302 303
 *
 * Ensures:
Michael Graff's avatar
Michael Graff committed
304
 *	'*msgp' == NULL
Michael Graff's avatar
Michael Graff committed
305 306
 */

307 308
isc_result_t
dns_message_sectiontotext(dns_message_t *msg, dns_section_t section,
309
			  const dns_master_style_t *style,
310
			  dns_messagetextflag_t flags,
311
			  isc_buffer_t *target);
312 313 314 315

isc_result_t
dns_message_pseudosectiontotext(dns_message_t *msg,
				dns_pseudosection_t section,
316
				const dns_master_style_t *style,
317
				dns_messagetextflag_t flags,
318
				isc_buffer_t *target);
319
/*
320
 * Convert section 'section' or 'pseudosection' of message 'msg' to
321
 * a cleartext representation
322 323
 *
 * Notes:
324
 *      See dns_message_totext for meanings of flags.
325 326 327 328 329
 *
 * Requires:
 *
 *	'msg' is a valid message.
 *
330 331
 *	'style' is a valid master dump style.
 *
332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347
 *	'target' is a valid buffer.
 *
 *	'section' is a valid section label.
 *
 * Ensures:
 *
 *	If the result is success:
 *
 *		The used space in 'target' is updated.
 *
 * Returns:
 *
 *	ISC_R_SUCCESS
 *	ISC_R_NOSPACE
 *	ISC_R_NOMORE
 *
348
 *	Note: On error return, *target may be partially filled with data.
349 350 351
*/

isc_result_t
352 353
dns_message_totext(dns_message_t *msg, const dns_master_style_t *style,
		   dns_messagetextflag_t flags, isc_buffer_t *target);
354 355 356 357
/*
 * Convert all sections of message 'msg' to a cleartext representation
 *
 * Notes:
358
 *      In flags, If DNS_MESSAGETEXTFLAG_OMITDOT is set, then the
359
 *      final '.' in absolute names will not be emitted.  If
360
 *      DNS_MESSAGETEXTFLAG_NOCOMMENTS is cleared, lines beginning
361 362
 *      with ";;" will be emitted indicating section name.  If
 *      DNS_MESSAGETEXTFLAG_NOHEADERS is cleared, header lines will
363
 *      be emitted.
364 365 366 367 368
 *
 * Requires:
 *
 *	'msg' is a valid message.
 *
369 370
 *	'style' is a valid master dump style.
 *
371 372 373 374 375 376 377 378 379 380 381 382 383 384
 *	'target' is a valid buffer.
 *
 * Ensures:
 *
 *	If the result is success:
 *
 *		The used space in 'target' is updated.
 *
 * Returns:
 *
 *	ISC_R_SUCCESS
 *	ISC_R_NOSPACE
 *	ISC_R_NOMORE
 *
385 386
 *	Note: On error return, *target may be partially filled with data.
 */
387

388
isc_result_t
389
dns_message_parse(dns_message_t *msg, isc_buffer_t *source,
390
		  unsigned int options);
Michael Graff's avatar
Michael Graff committed
391
/*
392
 * Parse raw wire data in 'source' as a DNS message.
Michael Graff's avatar
Michael Graff committed
393 394
 *
 * OPT records are detected and stored in the pseudo-section "opt".
Brian Wellington's avatar
Brian Wellington committed
395
 * TSIGs are detected and stored in the pseudo-section "tsig".
Michael Graff's avatar
Michael Graff committed
396
 *
397 398 399
 * If DNS_MESSAGEPARSE_PRESERVEORDER is set, or if the opcode of the message
 * is UPDATE, a separate dns_name_t object will be created for each RR in the
 * message.  Each such dns_name_t will have a single rdataset containing the
Brian Wellington's avatar
Brian Wellington committed
400
 * single RR, and the order of the RRs in the message is preserved.
401 402
 * Otherwise, only one dns_name_t object will be created for each unique
 * owner name in the section, and each such dns_name_t will have a list
403 404
 * of rdatasets.  To access the names and their data, use
 * dns_message_firstname() and dns_message_nextname().
405
 *
406 407 408 409
 * If DNS_MESSAGEPARSE_BESTEFFORT is set, errors in message content will
 * not be considered FORMERRs.  If the entire message can be parsed, it
 * will be returned and DNS_R_RECOVERABLE will be returned.
 *
410 411 412
 * If DNS_MESSAGEPARSE_IGNORETRUNCATION is set then return as many complete
 * RR's as possible, DNS_R_RECOVERABLE will be returned.
 *
413 414 415
 * OPT and TSIG records are always handled specially, regardless of the
 * 'preserve_order' setting.
 *
Michael Graff's avatar
Michael Graff committed
416 417 418
 * Requires:
 *	"msg" be valid.
 *
419
 *	"buffer" be a wire format buffer.
Michael Graff's avatar
Michael Graff committed
420 421 422 423
 *
 * Ensures:
 *	The buffer's data format is correct.
 *
Michael Graff's avatar
Michael Graff committed
424 425
 *	The buffer's contents verify as correct regarding header bits, buffer
 * 	and rdata sizes, etc.
Michael Graff's avatar
Michael Graff committed
426 427
 *
 * Returns:
428 429
 *	ISC_R_SUCCESS		-- all is well
 *	ISC_R_NOMEMORY		-- no memory
430 431
 *	DNS_R_RECOVERABLE	-- the message parsed properly, but contained
 *				   errors.
Michael Graff's avatar
Michael Graff committed
432
 *	Many other errors possible XXXMLG
Michael Graff's avatar
Michael Graff committed
433 434
 */

435
isc_result_t
436 437
dns_message_renderbegin(dns_message_t *msg, dns_compress_t *cctx,
			isc_buffer_t *buffer);
Michael Graff's avatar
Michael Graff committed
438
/*
Michael Graff's avatar
Michael Graff committed
439 440
 * Begin rendering on a message.  Only one call can be made to this function
 * per message.
Michael Graff's avatar
Michael Graff committed
441
 *
442 443 444
 * The compression context is "owned" by the message library until
 * dns_message_renderend() is called.  It must be invalidated by the caller.
 *
Andreas Gustafsson's avatar
Andreas Gustafsson committed
445
 * The buffer is "owned" by the message library until dns_message_renderend()
Michael Graff's avatar
Michael Graff committed
446 447
 * is called.
 *
Michael Graff's avatar
Michael Graff committed
448
 * Requires:
Michael Graff's avatar
Michael Graff committed
449 450
 *
 *	'msg' be valid.
451
 *
452 453
 *	'cctx' be valid.
 *
454
 *	'buffer' is a valid buffer.
Michael Graff's avatar
Michael Graff committed
455 456 457 458
 *
 * Side Effects:
 *
 *	The buffer is cleared before it is used.
459
 *
Michael Graff's avatar
Michael Graff committed
460
 * Returns:
461 462
 *	ISC_R_SUCCESS		-- all is well
 *	ISC_R_NOSPACE		-- output buffer is too small
Michael Graff's avatar
Michael Graff committed
463 464
 */

465
isc_result_t
466 467 468
dns_message_renderchangebuffer(dns_message_t *msg, isc_buffer_t *buffer);
/*
 * Reset the buffer.  This can be used after growing the old buffer
469
 * on a ISC_R_NOSPACE return from most of the render functions.
470
 *
Michael Graff's avatar
Michael Graff committed
471 472 473 474
 * On successful completion, the old buffer is no longer used by the
 * library.  The new buffer is owned by the library until
 * dns_message_renderend() is called.
 *
475 476 477 478 479 480 481 482 483
 * Requires:
 *
 *	'msg' be valid.
 *
 *	dns_message_renderbegin() was called.
 *
 *	buffer != NULL.
 *
 * Returns:
484 485
 *	ISC_R_NOSPACE		-- new buffer is too small
 *	ISC_R_SUCCESS		-- all is well.
486 487
 */

488
isc_result_t
489
dns_message_renderreserve(dns_message_t *msg, unsigned int space);
Michael Graff's avatar
Michael Graff committed
490
/*
Michael Graff's avatar
Michael Graff committed
491 492 493
 * XXXMLG should use size_t rather than unsigned int once the buffer
 * API is cleaned up
 *
Michael Graff's avatar
Michael Graff committed
494 495 496 497 498
 * Reserve "space" bytes in the given buffer.
 *
 * Requires:
 *
 *	'msg' be valid.
Michael Graff's avatar
Michael Graff committed
499
 *
500 501
 *	dns_message_renderbegin() was called.
 *
Michael Graff's avatar
Michael Graff committed
502
 * Returns:
503 504
 *	ISC_R_SUCCESS		-- all is well.
 *	ISC_R_NOSPACE		-- not enough free space in the buffer.
Michael Graff's avatar
Michael Graff committed
505 506
 */

507
void
Michael Graff's avatar
Michael Graff committed
508 509
dns_message_renderrelease(dns_message_t *msg, unsigned int space);
/*
Michael Graff's avatar
Michael Graff committed
510 511 512
 * XXXMLG should use size_t rather than unsigned int once the buffer
 * API is cleaned up
 *
Michael Graff's avatar
Michael Graff committed
513 514 515 516 517 518
 * Release "space" bytes in the given buffer that was previously reserved.
 *
 * Requires:
 *
 *	'msg' be valid.
 *
519 520
 *	'space' is less than or equal to the total amount of space reserved
 *	via prior calls to dns_message_renderreserve().
521
 *
522
 *	dns_message_renderbegin() was called.
Michael Graff's avatar
Michael Graff committed
523 524
 */

525
isc_result_t
526
dns_message_rendersection(dns_message_t *msg, dns_section_t section,
Michael Graff's avatar
Michael Graff committed
527
			  unsigned int options);
Michael Graff's avatar
Michael Graff committed
528
/*
Michael Graff's avatar
Michael Graff committed
529 530
 * Render all names, rdatalists, etc from the given section at the
 * specified priority or higher.
Michael Graff's avatar
Michael Graff committed
531 532
 *
 * Requires:
Michael Graff's avatar
Michael Graff committed
533 534 535
 *	'msg' be valid.
 *
 *	'section' be a valid section.
Michael Graff's avatar
Michael Graff committed
536
 *
537 538
 *	dns_message_renderbegin() was called.
 *
Michael Graff's avatar
Michael Graff committed
539
 * Returns:
540
 *	ISC_R_SUCCESS		-- all records were written, and there are
Michael Graff's avatar
Michael Graff committed
541
 *				   no more records for this section.
542
 *	ISC_R_NOSPACE		-- Not enough room in the buffer to write
Michael Graff's avatar
Michael Graff committed
543 544 545
 *				   all records requested.
 *	DNS_R_MOREDATA		-- All requested records written, and there
 *				   are records remaining for this section.
Michael Graff's avatar
Michael Graff committed
546 547
 */

548 549 550 551 552 553 554 555 556 557 558 559 560 561 562
void
dns_message_renderheader(dns_message_t *msg, isc_buffer_t *target);
/*
 * Render the message header.  This is implicitly called by
 * dns_message_renderend().
 *
 * Requires:
 *
 *	'msg' be a valid message.
 *
 *	dns_message_renderbegin() was called.
 *
 *	'target' is a valid buffer with enough space to hold a message header
 */

563
isc_result_t
564
dns_message_renderend(dns_message_t *msg);
Michael Graff's avatar
Michael Graff committed
565
/*
Michael Graff's avatar
Michael Graff committed
566 567 568 569 570 571 572 573
 * Finish rendering to the buffer.  Note that more data can be in the
 * 'msg' structure.  Destroying the structure will free this, or in a multi-
 * part EDNS1 message this data can be rendered to another buffer later.
 *
 * Requires:
 *
 *	'msg' be a valid message.
 *
574 575
 *	dns_message_renderbegin() was called.
 *
Michael Graff's avatar
Michael Graff committed
576
 * Returns:
577
 *	ISC_R_SUCCESS		-- all is well.
Michael Graff's avatar
Michael Graff committed
578
 */
579

Bob Halley's avatar
Bob Halley committed
580 581 582 583 584 585 586 587 588 589 590 591 592 593
void
dns_message_renderreset(dns_message_t *msg);
/*
 * Reset the message so that it may be rendered again.
 *
 * Notes:
 *
 *	If dns_message_renderbegin() has been called, dns_message_renderend()
 *	must be called before calling this function.
 *
 * Requires:
 *
 *	'msg' be a valid message with rendering intent.
 */
Michael Graff's avatar
Michael Graff committed
594

595
isc_result_t
Michael Graff's avatar
Michael Graff committed
596 597 598 599
dns_message_firstname(dns_message_t *msg, dns_section_t section);
/*
 * Set internal per-section name pointer to the beginning of the section.
 *
Michael Graff's avatar
Michael Graff committed
600
 * The functions dns_message_firstname() and dns_message_nextname() may
601
 * be used for iterating over the owner names in a section.
Michael Graff's avatar
Michael Graff committed
602
 *
Michael Graff's avatar
Michael Graff committed
603 604 605 606 607 608 609
 * Requires:
 *
 *   	'msg' be valid.
 *
 *	'section' be a valid section.
 *
 * Returns:
610 611
 *	ISC_R_SUCCESS		-- All is well.
 *	ISC_R_NOMORE		-- No names on given section.
Michael Graff's avatar
Michael Graff committed
612 613
 */

614
isc_result_t
Michael Graff's avatar
Michael Graff committed
615 616 617 618 619 620 621 622 623 624 625
dns_message_nextname(dns_message_t *msg, dns_section_t section);
/*
 * Sets the internal per-section name pointer to point to the next name
 * in that section.
 *
 * Requires:
 *
 *   	'msg' be valid.
 *
 *	'section' be a valid section.
 *
Michael Graff's avatar
Michael Graff committed
626
 *	dns_message_firstname() must have been called on this section,
627
 *	and the result was ISC_R_SUCCESS.
Michael Graff's avatar
Michael Graff committed
628
 *
Michael Graff's avatar
Michael Graff committed
629
 * Returns:
630
 *	ISC_R_SUCCESS		-- All is well.
Brian Wellington's avatar
Brian Wellington committed
631
 *	ISC_R_NOMORE		-- No more names in given section.
Michael Graff's avatar
Michael Graff committed
632 633
 */

Michael Graff's avatar
Michael Graff committed
634
void
Michael Graff's avatar
Michael Graff committed
635 636 637 638 639 640
dns_message_currentname(dns_message_t *msg, dns_section_t section,
			dns_name_t **name);
/*
 * Sets 'name' to point to the name where the per-section internal name
 * pointer is currently set.
 *
Michael Graff's avatar
Michael Graff committed
641 642 643
 * This function returns the name in the database, so any data associated
 * with it (via the name's "list" member) contains the actual rdatasets.
 *
Michael Graff's avatar
Michael Graff committed
644 645 646 647 648 649 650 651
 * Requires:
 *
 *	'msg' be valid.
 *
 *	'name' be non-NULL, and *name be NULL.
 *
 *	'section' be a valid section.
 *
Michael Graff's avatar
Michael Graff committed
652 653
 *	dns_message_firstname() must have been called on this section,
 *	and the result of it and any dns_message_nextname() calls was
654
 *	ISC_R_SUCCESS.
Michael Graff's avatar
Michael Graff committed
655 656
 */

657
isc_result_t
Michael Graff's avatar
Michael Graff committed
658 659
dns_message_findname(dns_message_t *msg, dns_section_t section,
		     dns_name_t *target, dns_rdatatype_t type,
Bob Halley's avatar
Bob Halley committed
660 661
		     dns_rdatatype_t covers, dns_name_t **foundname,
		     dns_rdataset_t **rdataset);
Michael Graff's avatar
Michael Graff committed
662
/*
Michael Graff's avatar
Michael Graff committed
663 664
 * Search for a name in the specified section.  If it is found, *name is
 * set to point to the name, and *rdataset is set to point to the found
Bob Halley's avatar
Bob Halley committed
665
 * rdataset (if type is specified as other than dns_rdatatype_any).
Michael Graff's avatar
Michael Graff committed
666 667 668 669 670 671
 *
 * Requires:
 *	'msg' be valid.
 *
 *	'section' be a valid section.
 *
Michael Graff's avatar
Michael Graff committed
672 673
 *	If a pointer to the name is desired, 'foundname' should be non-NULL.
 *	If it is non-NULL, '*foundname' MUST be NULL.
Michael Graff's avatar
Michael Graff committed
674 675 676
 *
 *	If a type other than dns_datatype_any is searched for, 'rdataset'
 *	may be non-NULL, '*rdataset' be NULL, and will point at the found
Michael Graff's avatar
Michael Graff committed
677
 *	rdataset.  If the type is dns_datatype_any, 'rdataset' must be NULL.
Michael Graff's avatar
Michael Graff committed
678 679 680 681 682
 *
 *	'target' be a valid name.
 *
 *	'type' be a valid type.
 *
683
 *	If 'type' is dns_rdatatype_rrsig, 'covers' must be a valid type.
684 685
 *	Otherwise it should be 0.
 *
Michael Graff's avatar
Michael Graff committed
686
 * Returns:
687
 *	ISC_R_SUCCESS		-- all is well.
Michael Graff's avatar
Michael Graff committed
688
 *	DNS_R_NXDOMAIN		-- name does not exist in that section.
689
 *	DNS_R_NXRRSET		-- The name does exist, but the desired
Michael Graff's avatar
Michael Graff committed
690
 *				   type does not.
Michael Graff's avatar
Michael Graff committed
691 692
 */

693
isc_result_t
694 695 696 697 698 699 700 701 702 703 704
dns_message_findtype(dns_name_t *name, dns_rdatatype_t type,
		     dns_rdatatype_t covers, dns_rdataset_t **rdataset);
/*
 * Search the name for the specified type.  If it is found, *rdataset is
 * filled in with a pointer to that rdataset.
 *
 * Requires:
 *	if '**rdataset' is non-NULL, *rdataset needs to be NULL.
 *
 *	'type' be a valid type, and NOT dns_rdatatype_any.
 *
705
 *	If 'type' is dns_rdatatype_rrsig, 'covers' must be a valid type.
706 707
 *	Otherwise it should be 0.
 *
708
 * Returns:
709 710
 *	ISC_R_SUCCESS		-- all is well.
 *	ISC_R_NOTFOUND		-- the desired type does not exist.
711 712
 */

Michael Graff's avatar
Michael Graff committed
713 714 715 716
void
dns_message_movename(dns_message_t *msg, dns_name_t *name,
		     dns_section_t fromsection,
		     dns_section_t tosection);
Michael Graff's avatar
Michael Graff committed
717 718
/*
 * Move a name from one section to another.
Michael Graff's avatar
Michael Graff committed
719 720 721 722 723
 *
 * Requires:
 *
 *	'msg' be valid.
 *
Michael Graff's avatar
Michael Graff committed
724
 *	'name' must be a name already in 'fromsection'.
Michael Graff's avatar
Michael Graff committed
725 726 727
 *
 *	'fromsection' must be a valid section.
 *
Michael Graff's avatar
Michael Graff committed
728
 *	'tosection' must be a valid section.
Michael Graff's avatar
Michael Graff committed
729 730
 */

Michael Graff's avatar
Michael Graff committed
731 732 733
void
dns_message_addname(dns_message_t *msg, dns_name_t *name,
		    dns_section_t section);
Michael Graff's avatar
Michael Graff committed
734 735 736
/*
 * Adds the name to the given section.
 *
Michael Graff's avatar
Michael Graff committed
737 738
 * It is the caller's responsibility to enforce any unique name requirements
 * in a section.
Michael Graff's avatar
Michael Graff committed
739 740 741 742 743
 *
 * Requires:
 *
 *	'msg' be valid, and be a renderable message.
 *
Mark Andrews's avatar
Mark Andrews committed
744
 *	'name' be a valid absolute name.
Michael Graff's avatar
Michael Graff committed
745 746
 *
 *	'section' be a named section.
Michael Graff's avatar
Michael Graff committed
747
 */
748

749 750 751 752 753 754 755 756
/*
 * LOANOUT FUNCTIONS
 *
 * Each of these functions loan a particular type of data to the caller.
 * The storage for these will vanish when the message is destroyed or
 * reset, and must NOT be used after these operations.
 */

757
isc_result_t
758 759 760
dns_message_gettempname(dns_message_t *msg, dns_name_t **item);
/*
 * Return a name that can be used for any temporary purpose, including
761 762 763
 * inserting into the message's linked lists.  The name must be returned
 * to the message code using dns_message_puttempname() or inserted into
 * one of the message's sections before the message is destroyed.
764 765 766 767 768 769 770 771 772
 *
 * It is the caller's responsibility to initialize this name.
 *
 * Requires:
 *	msg be a valid message
 *
 *	item != NULL && *item == NULL
 *
 * Returns:
773 774
 *	ISC_R_SUCCESS		-- All is well.
 *	ISC_R_NOMEMORY		-- No item can be allocated.
775 776
 */

777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793
isc_result_t
dns_message_gettempoffsets(dns_message_t *msg, dns_offsets_t **item);
/*
 * Return an offsets array that can be used for any temporary purpose,
 * such as attaching to a temporary name.  The offsets will be freed
 * when the message is destroyed or reset.
 *
 * Requires:
 *	msg be a valid message
 *
 *	item != NULL && *item == NULL
 *
 * Returns:
 *	ISC_R_SUCCESS		-- All is well.
 *	ISC_R_NOMEMORY		-- No item can be allocated.
 */

794
isc_result_t
795 796 797
dns_message_gettemprdata(dns_message_t *msg, dns_rdata_t **item);
/*
 * Return a rdata that can be used for any temporary purpose, including
798 799
 * inserting into the message's linked lists.  The rdata will be freed
 * when the message is destroyed or reset.
800 801 802 803 804 805 806
 *
 * Requires:
 *	msg be a valid message
 *
 *	item != NULL && *item == NULL
 *
 * Returns:
807 808
 *	ISC_R_SUCCESS		-- All is well.
 *	ISC_R_NOMEMORY		-- No item can be allocated.
809 810
 */

811
isc_result_t
812 813 814
dns_message_gettemprdataset(dns_message_t *msg, dns_rdataset_t **item);
/*
 * Return a rdataset that can be used for any temporary purpose, including
815 816 817
 * inserting into the message's linked lists. The name must be returned
 * to the message code using dns_message_puttempname() or inserted into
 * one of the message's sections before the message is destroyed.
818 819 820 821 822 823 824
 *
 * Requires:
 *	msg be a valid message
 *
 *	item != NULL && *item == NULL
 *
 * Returns:
825 826
 *	ISC_R_SUCCESS		-- All is well.
 *	ISC_R_NOMEMORY		-- No item can be allocated.
827 828
 */

829
isc_result_t
830 831 832
dns_message_gettemprdatalist(dns_message_t *msg, dns_rdatalist_t **item);
/*
 * Return a rdatalist that can be used for any temporary purpose, including
833 834
 * inserting into the message's linked lists.  The rdatalist will be
 * destroyed when the message is destroyed or reset.
835 836 837 838 839 840 841
 *
 * Requires:
 *	msg be a valid message
 *
 *	item != NULL && *item == NULL
 *
 * Returns:
842 843
 *	ISC_R_SUCCESS		-- All is well.
 *	ISC_R_NOMEMORY		-- No item can be allocated.
844 845 846 847 848 849 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 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905
 */

void
dns_message_puttempname(dns_message_t *msg, dns_name_t **item);
/*
 * Return a borrowed name to the message's name free list.
 *
 * Requires:
 *	msg be a valid message
 *
 *	item != NULL && *item point to a name returned by
 *	dns_message_gettempname()
 *
 * Ensures:
 *	*item == NULL
 */

void
dns_message_puttemprdata(dns_message_t *msg, dns_rdata_t **item);
/*
 * Return a borrowed rdata to the message's rdata free list.
 *
 * Requires:
 *	msg be a valid message
 *
 *	item != NULL && *item point to a rdata returned by
 *	dns_message_gettemprdata()
 *
 * Ensures:
 *	*item == NULL
 */

void
dns_message_puttemprdataset(dns_message_t *msg, dns_rdataset_t **item);
/*
 * Return a borrowed rdataset to the message's rdataset free list.
 *
 * Requires:
 *	msg be a valid message
 *
 *	item != NULL && *item point to a rdataset returned by
 *	dns_message_gettemprdataset()
 *
 * Ensures:
 *	*item == NULL
 */

void
dns_message_puttemprdatalist(dns_message_t *msg, dns_rdatalist_t **item);
/*
 * Return a borrowed rdatalist to the message's rdatalist free list.
 *
 * Requires:
 *	msg be a valid message
 *
 *	item != NULL && *item point to a rdatalist returned by
 *	dns_message_gettemprdatalist()
 *
 * Ensures:
 *	*item == NULL
 */

906
isc_result_t
Michael Graff's avatar
Michael Graff committed
907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924
dns_message_peekheader(isc_buffer_t *source, dns_messageid_t *idp,
		       unsigned int *flagsp);
/*
 * Assume the remaining region of "source" is a DNS message.  Peek into
 * it and fill in "*idp" with the message id, and "*flagsp" with the flags.
 *
 * Requires:
 *
 *	source != NULL
 *
 * Ensures:
 *
 *	if (idp != NULL) *idp == message id.
 *
 *	if (flagsp != NULL) *flagsp == message flags.
 *
 * Returns:
 *
925
 *	ISC_R_SUCCESS		-- all is well.
Michael Graff's avatar
Michael Graff committed
926
 *
927
 *	ISC_R_UNEXPECTEDEND	-- buffer doesn't contain enough for a header.
Michael Graff's avatar
Michael Graff committed
928 929
 */

930
isc_result_t
Bob Halley's avatar
Bob Halley committed
931 932 933 934 935 936
dns_message_reply(dns_message_t *msg, isc_boolean_t want_question_section);
/*
 * Start formatting a reply to the query in 'msg'.
 *
 * Requires:
 *
Bob Halley's avatar
EDNS0  
Bob Halley committed
937
 *	'msg' is a valid message with parsing intent, and contains a query.
938
 *
Bob Halley's avatar
Bob Halley committed
939 940 941
 * Ensures:
 *
 *	The message will have a rendering intent.  If 'want_question_section'
942 943 944 945 946
 *	is true, the message opcode is query or notify, and the question
 *	section is present and properly formatted, then the question section
 *	will be included in the reply.  All other sections will be cleared.
 *	The QR flag will be set, the RD flag will be preserved, and all other
 *	flags will be cleared.
Bob Halley's avatar
Bob Halley committed
947 948 949
 *
 * Returns:
 *
950
 *	ISC_R_SUCCESS		-- all is well.
Bob Halley's avatar
Bob Halley committed
951
 *
Bob Halley's avatar
Bob Halley committed
952 953 954 955 956 957 958
 *	DNS_R_FORMERR		-- the header or question section of the
 *				   message is invalid, replying is impossible.
 *				   If DNS_R_FORMERR is returned when
 *				   want_question_section is ISC_FALSE, then
 *				   it's the header section that's bad;
 *				   otherwise either of the header or question
 *				   sections may be bad.
Bob Halley's avatar
Bob Halley committed
959 960
 */

Bob Halley's avatar
EDNS0  
Bob Halley committed
961 962 963 964 965 966 967 968 969 970 971 972 973 974
dns_rdataset_t *
dns_message_getopt(dns_message_t *msg);
/*
 * Get the OPT record for 'msg'.
 *
 * Requires:
 *
 *	'msg' is a valid message.
 *
 * Returns:
 *
 *	The OPT rdataset of 'msg', or NULL if there isn't one.
 */

975
isc_result_t
Bob Halley's avatar
EDNS0  
Bob Halley committed
976 977 978 979 980 981
dns_message_setopt(dns_message_t *msg, dns_rdataset_t *opt);
/*
 * Set the OPT record for 'msg'.
 *
 * Requires:
 *
982 983
 *	'msg' is a valid message with rendering intent
 *	and no sections have been rendered.
Bob Halley's avatar
EDNS0  
Bob Halley committed
984 985 986 987 988
 *
 *	'opt' is a valid OPT record.
 *
 * Ensures:
 *
989 990 991 992 993
 *	The OPT record has either been freed or ownership of it has
 *	been transferred to the message.
 *
 *	If ISC_R_SUCCESS was returned, the OPT record will be rendered 
 *	when dns_message_renderend() is called.
Bob Halley's avatar
EDNS0  
Bob Halley committed
994 995 996
 *
 * Returns:
 *
997
 *	ISC_R_SUCCESS		-- all is well.
Bob Halley's avatar
EDNS0  
Bob Halley committed
998
 *
999
 *	ISC_R_NOSPACE		-- there is no space for the OPT record.
Bob Halley's avatar
EDNS0  
Bob Halley committed
1000 1001
 */

1002 1003 1004 1005 1006 1007 1008 1009
dns_rdataset_t *
dns_message_gettsig(dns_message_t *msg, dns_name_t **owner);
/*
 * Get the TSIG record and owner for 'msg'.
 *
 * Requires:
 *
 *	'msg' is a valid message.
1010
 *	'owner' is NULL or *owner is NULL.
1011 1012 1013 1014
 *
 * Returns:
 *
 *	The TSIG rdataset of 'msg', or NULL if there isn't one.
1015 1016 1017 1018
 *
 * Ensures:
 *
 * 	If 'owner' is not NULL, it will point to the owner name.
1019 1020
 */

1021
isc_result_t
1022 1023 1024 1025 1026 1027 1028 1029
dns_message_settsigkey(dns_message_t *msg, dns_tsigkey_t *key);
/*
 * Set the tsig key for 'msg'.  This is only necessary for when rendering a
 * query or parsing a response.  The key (if non-NULL) is attached to, and
 * will be detached when the message is destroyed.
 *
 * Requires:
 *
1030 1031 1032
 *	'msg' is a valid message with rendering intent,
 *	dns_message_renderbegin() has been called, and no sections have been
 *	rendered.
1033
 *	'key' is a valid tsig key or NULL.
1034 1035 1036 1037 1038 1039
 *
 * Returns:
 *
 *	ISC_R_SUCCESS		-- all is well.
 *
 *	ISC_R_NOSPACE		-- there is no space for the TSIG record.
1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051
 */

dns_tsigkey_t *
dns_message_gettsigkey(dns_message_t *msg);
/*
 * Gets the tsig key for 'msg'.
 *
 * Requires:
 *
 *	'msg' is a valid message
 */

1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095
isc_result_t
dns_message_setquerytsig(dns_message_t *msg, isc_buffer_t *querytsig);
/*
 * Indicates that 'querytsig' is the TSIG from the signed query for which
 * 'msg' is the response.  This is also used for chained TSIGs in TCP
 * responses.
 *
 * Requires:
 *
 *	'querytsig' is a valid buffer as returned by dns_message_getquerytsig()
 *	or NULL
 *
 *	'msg' is a valid message
 *
 * Returns:
 *
 *	ISC_R_SUCCESS
 *	ISC_R_NOMEMORY
 */

isc_result_t
dns_message_getquerytsig(dns_message_t *msg, isc_mem_t *mctx,
			 isc_buffer_t **querytsig);
/*
 * Gets the tsig from the TSIG from the signed query 'msg'.  This is also used
 * for chained TSIGs in TCP responses.  Unlike dns_message_gettsig, this makes
 * a copy of the data, so can be used if the message is destroyed.
 *
 * Requires:
 *
 *	'msg' is a valid signed message
 *	'mctx' is a valid memory context
 *	querytsig != NULL && *querytsig == NULL
 *
 * Returns:
 *
 *	ISC_R_SUCCESS
 *	ISC_R_NOMEMORY
 *
 * Ensures:
 * 	'tsig' points to NULL or an allocated buffer which must be freed
 * 	by the caller.
 */