message.h 26.9 KB
Newer Older
1
/*
Bob Halley's avatar
Bob Halley committed
2
 * Copyright (C) 1999, 2000  Internet Software Consortium.
3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
 * 
 * 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.
 */

18 19
#ifndef DNS_MESSAGE_H
#define DNS_MESSAGE_H 1
20 21 22 23 24

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

25 26
#include <isc/lang.h>
#include <isc/magic.h>
27

Michael Graff's avatar
Michael Graff committed
28
#include <dns/compress.h>
29 30
#include <dns/rdatastruct.h>
#include <dns/types.h>
31

32 33
#include <dst/dst.h>

34 35 36
/*
 * How this beast works:
 *
37
 * When a dns message is received in a buffer, dns_message_fromwire() is called
38 39 40 41 42 43 44 45
 * 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.
 *
 * TSIG checking is also done at this layer, and any DNSSEC information should
 * also be performed at this time.
 *
46
 * If dns_message_fromwire() returns DNS_R_MOREDATA additional
47 48
 * message packets are required.  This implies an EDNS message.
 *
49
 * When going from structure to wire, dns_message_towire() will return
Michael Graff's avatar
typo  
Michael Graff committed
50
 * DNS_R_MOREDATA if there is more data left in the output buffer that
51 52
 * could not be rendered into the exisiting buffer.
 *
53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74
 *
 * Notes on using the gettemp*() and puttemp*() functions:
 *
 * These functions return items (names, rdatasets, etc) allocated from some
 * internal state of the dns_message_t.  These items must be put back into
 * the dns_message_t in one of two ways.  Assume a name was allocated via
 * dns_message_gettempname():
 *
 *	(1) insert it into a section, using dns_message_addname().
 *
 *	(2) return it to the message using dns_message_puttempname().
 *
 * The same applies to rdata, rdatasets, and rdatalists which were
 * allocated using this group of functions.
 *
 * 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:
 *
75
 *	buffer = isc_buffer_allocate(mctx, 512);
76 77 78 79 80 81 82 83 84 85
 *	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:
 *
86 87 88 89 90
 * XXX Needed:  ways to handle TSIG and DNSSEC, supply TSIG and DNSSEC
 * keys, set and retrieve EDNS information, add rdata to a section,
 * move rdata from one section to another, remove rdata, etc.
 */

91 92 93 94 95
#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
96 97
#define DNS_MESSAGEFLAG_AD		0x0020U
#define DNS_MESSAGEFLAG_CD		0x0010U
98

Bob Halley's avatar
Bob Halley committed
99
#define DNS_MESSAGE_REPLYPRESERVE	(DNS_MESSAGEFLAG_RD)
100

Bob Halley's avatar
Bob Halley committed
101 102 103
#define DNS_MESSAGE_HEADERLEN		12 /* 6 isc_uint16_t's */

#define DNS_MESSAGE_MAGIC		0x4d534740U	/* MSG@ */
Michael Graff's avatar
Michael Graff committed
104
#define DNS_MESSAGE_VALID(msg)		ISC_MAGIC_VALID(msg, DNS_MESSAGE_MAGIC)
Michael Graff's avatar
Michael Graff committed
105

Michael Graff's avatar
Michael Graff committed
106 107 108 109
/*
 * 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
110
typedef int dns_section_t;
Michael Graff's avatar
Michael Graff committed
111
#define DNS_SECTION_ANY			(-1)
Michael Graff's avatar
Michael Graff committed
112 113 114 115
#define DNS_SECTION_QUESTION		0
#define DNS_SECTION_ANSWER		1
#define DNS_SECTION_AUTHORITY		2
#define DNS_SECTION_ADDITIONAL		3
116
#define DNS_SECTION_MAX			4
117 118

/*
Andreas Gustafsson's avatar
Andreas Gustafsson committed
119
 * Dynamic update names for these sections.
120 121 122 123
 */
#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
124 125

/*
Michael Graff's avatar
Michael Graff committed
126
 * These tell the message library how the created dns_message_t will be used.
Michael Graff's avatar
Michael Graff committed
127
 */
Michael Graff's avatar
Michael Graff committed
128 129 130
#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
131

Michael Graff's avatar
Michael Graff committed
132 133 134 135 136
/*
 * Control behavior of rendering
 */
#define DNS_MESSAGERENDER_ORDERED	0x0001	/* don't change order */

Michael Graff's avatar
Michael Graff committed
137 138
typedef struct dns_msgblock dns_msgblock_t;

Bob Halley's avatar
Bob Halley committed
139
struct dns_message {
Michael Graff's avatar
Michael Graff committed
140
	/* public from here down */
Michael Graff's avatar
Michael Graff committed
141
	unsigned int			magic;
142

143
	dns_messageid_t			id;
Michael Graff's avatar
Michael Graff committed
144 145 146
	unsigned int			flags;
	unsigned int			rcode;
	unsigned int			opcode;
Michael Graff's avatar
Michael Graff committed
147
	dns_rdataclass_t		rdclass;
Michael Graff's avatar
Michael Graff committed
148

149
	/* 4 real, 1 pseudo */
Michael Graff's avatar
Michael Graff committed
150
	unsigned int			counts[DNS_SECTION_MAX];
Michael Graff's avatar
Michael Graff committed
151 152

	/* private from here down */
Michael Graff's avatar
Michael Graff committed
153 154
	dns_namelist_t			sections[DNS_SECTION_MAX];
	dns_name_t		       *cursors[DNS_SECTION_MAX];
Bob Halley's avatar
Bob Halley committed
155
	dns_rdataset_t		       *opt;
156 157
	dns_rdataset_t		       *sig0;
	dns_rdataset_t		       *tsigset;
158

Michael Graff's avatar
Michael Graff committed
159
	int				state;
Michael Graff's avatar
Michael Graff committed
160
	unsigned int			from_to_wire : 2;
161
	unsigned int			need_cctx_cleanup : 1;
Bob Halley's avatar
Bob Halley committed
162 163
	unsigned int			header_ok : 1;
	unsigned int			question_ok : 1;
164
	unsigned int			tcp_continuation : 1;
165 166
	unsigned int			verified_sig : 1;
	unsigned int			verify_attempted : 1;
167

168
	unsigned int			opt_reserved;
169
	unsigned int			reserved; /* reserved space (render) */
170

171
	isc_buffer_t		       *buffer;
Michael Graff's avatar
Michael Graff committed
172
	dns_compress_t			cctx;
173

Michael Graff's avatar
Michael Graff committed
174
	isc_mem_t		       *mctx;
175 176 177
	isc_mempool_t		       *namepool;
	isc_mempool_t		       *rdspool;

178
	isc_bufferlist_t		scratchpad;
179 180
	isc_bufferlist_t		cleanup;

Michael Graff's avatar
Michael Graff committed
181 182
	ISC_LIST(dns_msgblock_t)	rdatas;
	ISC_LIST(dns_msgblock_t)	rdatalists;
183 184 185

	ISC_LIST(dns_rdata_t)		freerdata;
	ISC_LIST(dns_rdatalist_t)	freerdatalist;
186 187 188

	dns_rcode_t			tsigstatus;
	dns_rcode_t			querytsigstatus;
189
	dns_name_t		       *tsigname;
190 191
	dns_rdata_any_tsig_t	       *tsig;
	dns_rdata_any_tsig_t	       *querytsig;
192
	dns_tsigkey_t		       *tsigkey;
193
	void			       *tsigctx;
194 195 196 197 198
	int				sigstart;

	dst_key_t		       *sig0key;
	dns_rcode_t			sig0status;
	isc_region_t		       *query;
Brian Wellington's avatar
Brian Wellington committed
199
	isc_region_t		       *saved;
Bob Halley's avatar
Bob Halley committed
200
};
201

202 203 204 205 206 207
/***
 *** Functions
 ***/

ISC_LANG_BEGINDECLS

208
isc_result_t
209
dns_message_create(isc_mem_t *mctx, unsigned int intent, dns_message_t **msgp);
Bob Halley's avatar
Bob Halley committed
210
		   
211
/*
Michael Graff's avatar
Michael Graff committed
212
 * Create msg structure.
213
 *
Michael Graff's avatar
Michael Graff committed
214
 * This function will allocate some internal blocks of memory that are
Michael Graff's avatar
Michael Graff committed
215
 * expected to be needed for parsing or rendering nearly any type of message.
Michael Graff's avatar
Michael Graff committed
216 217 218
 *
 * Requires:
 *	'mctx' be a valid memory context.
219
 *
Michael Graff's avatar
Michael Graff committed
220
 *	'msgp' be non-null and '*msg' be NULL.
Michael Graff's avatar
Michael Graff committed
221
 *
Andreas Gustafsson's avatar
typo  
Andreas Gustafsson committed
222 223
 *	'intent' must be one of DNS_MESSAGE_INTENTPARSE or
 *	DNS_MESSAGE_INTENTRENDER.
Michael Graff's avatar
Michael Graff committed
224 225
 *
 * Ensures:
Michael Graff's avatar
Michael Graff committed
226
 *	The data in "*msg" is set to indicate an unused and empty msg
Michael Graff's avatar
Michael Graff committed
227 228 229
 *	structure.
 *
 * Returns:
230 231
 *	ISC_R_NOMEMORY		-- out of memory
 *	ISC_R_SUCCESS		-- success
232 233
 */

Michael Graff's avatar
Michael Graff committed
234
void
Bob Halley's avatar
Bob Halley committed
235
dns_message_reset(dns_message_t *msg, unsigned int intent);
Michael Graff's avatar
Michael Graff committed
236 237 238 239 240 241
/*
 * 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
242 243 244
 * 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
245 246
 * The intended next use of the message will be 'intent'.
 *
Michael Graff's avatar
Michael Graff committed
247 248 249
 * Requires:
 *
 *	'msg' be valid.
Bob Halley's avatar
Bob Halley committed
250 251
 *
 *	'intent' is DNS_MESSAGE_INTENTPARSE or DNS_MESSAGE_INTENTRENDER
Michael Graff's avatar
Michael Graff committed
252
 */
Michael Graff's avatar
Michael Graff committed
253

Michael Graff's avatar
Michael Graff committed
254
void
Michael Graff's avatar
Michael Graff committed
255
dns_message_destroy(dns_message_t **msgp);
Michael Graff's avatar
Michael Graff committed
256
/*
Michael Graff's avatar
Michael Graff committed
257 258 259 260
 * Destroy all state in the message.
 *
 * Requires:
 *
Michael Graff's avatar
Michael Graff committed
261
 *	'msgp' be valid.
Michael Graff's avatar
Michael Graff committed
262 263
 *
 * Ensures:
Michael Graff's avatar
Michael Graff committed
264
 *	'*msgp' == NULL
Michael Graff's avatar
Michael Graff committed
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
isc_result_t
dns_message_sectiontotext(dns_message_t *msg, dns_section_t section,
			  isc_boolean_t comments,
			  isc_boolean_t omit_final_dot,
			  isc_buffer_t *target);
/*
 * Convert section 'section' of message 'msg' to a cleartext representation
 *
 * Notes:
 *	If 'omit_final_dot' is true, then the final '.' in absolute names
 *	will not be emitted.
 *	If 'no_rdata_or_tt;' is true, omit rdata and ttl fields.
 *	If 'comments' is true, lines beginning with ";;" will be emitted
 *	indicating section name.
 *
 * Requires:
 *
 *	'msg' is a valid message.
 *
 *	'target' is a valid buffer.
 *
 *	'section' is a valid section label.
 *
 * Ensures:
 *
 *	If the result is success:
 *
 *		Any bitstring labels are in canonical form.
 *
 *		The used space in 'target' is updated.
 *
 * Returns:
 *
 *	ISC_R_SUCCESS
 *	ISC_R_NOSPACE
 *	ISC_R_NOMORE
 *
 *	Note: On error return, *target may be partially filled with data. 
*/

isc_result_t
dns_message_totext(dns_message_t *msg, isc_boolean_t comments,
		   isc_boolean_t headers, isc_boolean_t omit_final_dot,
		   isc_buffer_t *target);
/*
 * Convert all sections of message 'msg' to a cleartext representation
 *
 * Notes:
 *	If 'omit_final_dot' is true, then the final '.' in absolute names
 *	will not be emitted.
 *	If 'no_rdata_or_tt;' is true, omit rdata and ttl fields.
 *	If 'comments' is true, lines beginning with ";;" will be emitted
 *	indicating section name.
 *
 * Requires:
 *
 *	'msg' is a valid message.
 *
 *	'target' is a valid buffer.
 *
 * Ensures:
 *
 *	If the result is success:
 *
 *		Any bitstring labels are in canonical form.
 *
 *		The used space in 'target' is updated.
 *
 * Returns:
 *
 *	ISC_R_SUCCESS
 *	ISC_R_NOSPACE
 *	ISC_R_NOMORE
 *
 *	Note: On error return, *target may be partially filled with data. 
*/

344
isc_result_t
345 346
dns_message_parse(dns_message_t *msg, isc_buffer_t *source,
		  isc_boolean_t preserve_order);
Michael Graff's avatar
Michael Graff committed
347
/*
Michael Graff's avatar
Michael Graff committed
348 349 350 351
 * Parse raw wire data pointed to by "buffer" and bounded by "buflen" as a
 * DNS message.
 *
 * OPT records are detected and stored in the pseudo-section "opt".
Brian Wellington's avatar
Brian Wellington committed
352
 * TSIGs are detected and stored in the pseudo-section "tsig".
Michael Graff's avatar
Michael Graff committed
353
 *
354 355 356 357 358 359 360 361 362 363 364 365
 * If 'preserve_order' is true, 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 single RR,
 * and the order of the RRs in the message is preserved.
 * 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
 * of rdatasets.  To access the names and their data, use 
 * dns_message_firstname() and dns_message_nextname(). 
 *
 * OPT and TSIG records are always handled specially, regardless of the
 * 'preserve_order' setting.
 *
Michael Graff's avatar
Michael Graff committed
366 367
 * If this is a multi-packet message (edns) and more data is required to
 * build the full message state, DNS_R_MOREDATA is returned.  In this case,
368
 * this function should be repeated with all input buffers until ISC_R_SUCCESS
Michael Graff's avatar
Michael Graff committed
369 370 371 372 373
 * (or an error) is returned.
 *
 * Requires:
 *	"msg" be valid.
 *
Michael Graff's avatar
Michael Graff committed
374
 *	"buffer" be a wire format binary buffer.
Michael Graff's avatar
Michael Graff committed
375 376 377 378
 *
 * Ensures:
 *	The buffer's data format is correct.
 *
Michael Graff's avatar
Michael Graff committed
379 380
 *	The buffer's contents verify as correct regarding header bits, buffer
 * 	and rdata sizes, etc.
Michael Graff's avatar
Michael Graff committed
381 382
 *
 * Returns:
383 384
 *	ISC_R_SUCCESS		-- all is well
 *	ISC_R_NOMEMORY		-- no memory
Michael Graff's avatar
Michael Graff committed
385
 *	DNS_R_MOREDATA		-- more packets needed for complete message
Michael Graff's avatar
Michael Graff committed
386 387
 *	DNS_R_???		-- bad signature (XXXMLG need more of these)
 *	Many other errors possible XXXMLG
Michael Graff's avatar
Michael Graff committed
388 389
 */

390
isc_result_t
Michael Graff's avatar
Michael Graff committed
391
dns_message_renderbegin(dns_message_t *msg, isc_buffer_t *buffer);
Michael Graff's avatar
Michael Graff committed
392
/*
Michael Graff's avatar
Michael Graff committed
393 394
 * Begin rendering on a message.  Only one call can be made to this function
 * per message.
Michael Graff's avatar
Michael Graff committed
395
 *
Andreas Gustafsson's avatar
Andreas Gustafsson committed
396
 * The buffer is "owned" by the message library until dns_message_renderend()
Michael Graff's avatar
Michael Graff committed
397 398
 * is called.
 *
Michael Graff's avatar
Michael Graff committed
399
 * Requires:
Michael Graff's avatar
Michael Graff committed
400 401
 *
 *	'msg' be valid.
402
 *
Michael Graff's avatar
Michael Graff committed
403 404 405 406 407
 *	buffer is a valid binary buffer.
 *
 * Side Effects:
 *
 *	The buffer is cleared before it is used.
408
 *
Michael Graff's avatar
Michael Graff committed
409
 * Returns:
410 411
 *	ISC_R_SUCCESS		-- all is well
 *	ISC_R_NOSPACE		-- output buffer is too small
Michael Graff's avatar
Michael Graff committed
412
 *	Anything that dns_compress_init() can return.
Michael Graff's avatar
Michael Graff committed
413 414
 */

415
isc_result_t
416 417 418
dns_message_renderchangebuffer(dns_message_t *msg, isc_buffer_t *buffer);
/*
 * Reset the buffer.  This can be used after growing the old buffer
419
 * on a ISC_R_NOSPACE return from most of the render functions.
420
 *
Michael Graff's avatar
Michael Graff committed
421 422 423 424
 * 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.
 *
425 426 427 428 429 430 431 432 433
 * Requires:
 *
 *	'msg' be valid.
 *
 *	dns_message_renderbegin() was called.
 *
 *	buffer != NULL.
 *
 * Returns:
434 435
 *	ISC_R_NOSPACE		-- new buffer is too small
 *	ISC_R_SUCCESS		-- all is well.
436 437
 */

438
isc_result_t
439
dns_message_renderreserve(dns_message_t *msg, unsigned int space);
Michael Graff's avatar
Michael Graff committed
440
/*
Michael Graff's avatar
Michael Graff committed
441 442 443
 * XXXMLG should use size_t rather than unsigned int once the buffer
 * API is cleaned up
 *
Michael Graff's avatar
Michael Graff committed
444 445 446 447 448
 * Reserve "space" bytes in the given buffer.
 *
 * Requires:
 *
 *	'msg' be valid.
Michael Graff's avatar
Michael Graff committed
449
 *
450 451
 *	dns_message_renderbegin() was called.
 *
Michael Graff's avatar
Michael Graff committed
452
 * Returns:
453 454
 *	ISC_R_SUCCESS		-- all is well.
 *	ISC_R_NOSPACE		-- not enough free space in the buffer.
Michael Graff's avatar
Michael Graff committed
455 456
 */

457
void
Michael Graff's avatar
Michael Graff committed
458 459
dns_message_renderrelease(dns_message_t *msg, unsigned int space);
/*
Michael Graff's avatar
Michael Graff committed
460 461 462
 * XXXMLG should use size_t rather than unsigned int once the buffer
 * API is cleaned up
 *
Michael Graff's avatar
Michael Graff committed
463 464 465 466 467 468
 * Release "space" bytes in the given buffer that was previously reserved.
 *
 * Requires:
 *
 *	'msg' be valid.
 *
469 470
 *	'space' is less than or equal to the total amount of space reserved
 *	via prior calls to dns_message_renderreserve().
471
 *
472
 *	dns_message_renderbegin() was called.
Michael Graff's avatar
Michael Graff committed
473 474
 */

475
isc_result_t
476
dns_message_rendersection(dns_message_t *msg, dns_section_t section,
Michael Graff's avatar
Michael Graff committed
477
			  unsigned int options);
Michael Graff's avatar
Michael Graff committed
478
/*
Michael Graff's avatar
Michael Graff committed
479 480
 * Render all names, rdatalists, etc from the given section at the
 * specified priority or higher.
Michael Graff's avatar
Michael Graff committed
481 482
 *
 * Requires:
Michael Graff's avatar
Michael Graff committed
483 484 485
 *	'msg' be valid.
 *
 *	'section' be a valid section.
Michael Graff's avatar
Michael Graff committed
486
 *
487 488
 *	dns_message_renderbegin() was called.
 *
Michael Graff's avatar
Michael Graff committed
489
 * Returns:
490
 *	ISC_R_SUCCESS		-- all records were written, and there are
Michael Graff's avatar
Michael Graff committed
491
 *				   no more records for this section.
492
 *	ISC_R_NOSPACE		-- Not enough room in the buffer to write
Michael Graff's avatar
Michael Graff committed
493 494 495
 *				   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
496 497
 */

498 499 500 501 502 503 504 505 506 507 508 509 510 511 512
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
 */

513
isc_result_t
514
dns_message_renderend(dns_message_t *msg);
Michael Graff's avatar
Michael Graff committed
515
/*
Michael Graff's avatar
Michael Graff committed
516 517 518 519 520 521 522 523
 * 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.
 *
524 525
 *	dns_message_renderbegin() was called.
 *
Michael Graff's avatar
Michael Graff committed
526
 * Returns:
527
 *	ISC_R_SUCCESS		-- all is well.
Michael Graff's avatar
Michael Graff committed
528 529
 */
		      
Bob Halley's avatar
Bob Halley committed
530 531 532 533 534 535 536 537 538 539 540 541 542 543
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
544

545
isc_result_t
Michael Graff's avatar
Michael Graff committed
546 547 548 549
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
550
 * The functions dns_message_firstname() and dns_message_nextname() may
551
 * be used for iterating over the owner names in a section. 
Michael Graff's avatar
Michael Graff committed
552
 *
Michael Graff's avatar
Michael Graff committed
553 554 555 556 557 558 559
 * Requires:
 *
 *   	'msg' be valid.
 *
 *	'section' be a valid section.
 *
 * Returns:
560 561
 *	ISC_R_SUCCESS		-- All is well.
 *	ISC_R_NOMORE		-- No names on given section.
Michael Graff's avatar
Michael Graff committed
562 563
 */

564
isc_result_t
Michael Graff's avatar
Michael Graff committed
565 566 567 568 569 570 571 572 573 574 575
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
576
 *	dns_message_firstname() must have been called on this section,
577
 *	and the result was ISC_R_SUCCESS.
Michael Graff's avatar
Michael Graff committed
578
 *
Michael Graff's avatar
Michael Graff committed
579
 * Returns:
580 581
 *	ISC_R_SUCCESS		-- All is well.
 *	ISC_R_NOMORE		-- No names in given section.
Michael Graff's avatar
Michael Graff committed
582 583
 */

Michael Graff's avatar
Michael Graff committed
584
void
Michael Graff's avatar
Michael Graff committed
585 586 587 588 589 590
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
591 592 593
 * 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
594 595 596 597 598 599 600 601
 * Requires:
 *
 *	'msg' be valid.
 *
 *	'name' be non-NULL, and *name be NULL.
 *
 *	'section' be a valid section.
 *
Michael Graff's avatar
Michael Graff committed
602 603
 *	dns_message_firstname() must have been called on this section,
 *	and the result of it and any dns_message_nextname() calls was
604
 *	ISC_R_SUCCESS.
Michael Graff's avatar
Michael Graff committed
605 606
 */

607
isc_result_t
Michael Graff's avatar
Michael Graff committed
608 609
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
610 611
		     dns_rdatatype_t covers, dns_name_t **foundname,
		     dns_rdataset_t **rdataset);
Michael Graff's avatar
Michael Graff committed
612
/*
Michael Graff's avatar
Michael Graff committed
613 614
 * 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
615
 * rdataset (if type is specified as other than dns_rdatatype_any).
Michael Graff's avatar
Michael Graff committed
616 617 618 619 620 621
 *
 * Requires:
 *	'msg' be valid.
 *
 *	'section' be a valid section.
 *
Michael Graff's avatar
Michael Graff committed
622 623
 *	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
624 625 626
 *
 *	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
627
 *	rdataset.  If the type is dns_datatype_any, 'rdataset' must be NULL.
Michael Graff's avatar
Michael Graff committed
628 629 630 631 632 633
 *
 *	'target' be a valid name.
 *
 *	'type' be a valid type.
 *
 * Returns:
634
 *	ISC_R_SUCCESS		-- all is well.
Michael Graff's avatar
Michael Graff committed
635
 *	DNS_R_NXDOMAIN		-- name does not exist in that section.
636
 *	DNS_R_NXRRSET		-- The name does exist, but the desired
Michael Graff's avatar
Michael Graff committed
637
 *				   type does not.
Michael Graff's avatar
Michael Graff committed
638 639
 */

640
isc_result_t
641 642 643 644 645 646 647 648 649 650 651 652
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.
 *
 * Returns:
653 654
 *	ISC_R_SUCCESS		-- all is well.
 *	ISC_R_NOTFOUND		-- the desired type does not exist.
655 656
 */

Michael Graff's avatar
Michael Graff committed
657 658 659 660
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
661 662
/*
 * Move a name from one section to another.
Michael Graff's avatar
Michael Graff committed
663 664 665 666 667
 *
 * Requires:
 *
 *	'msg' be valid.
 *
Michael Graff's avatar
Michael Graff committed
668
 *	'name' must be a name already in 'fromsection'.
Michael Graff's avatar
Michael Graff committed
669 670 671
 *
 *	'fromsection' must be a valid section.
 *
Michael Graff's avatar
Michael Graff committed
672
 *	'tosection' must be a valid section.
Michael Graff's avatar
Michael Graff committed
673 674
 */

Michael Graff's avatar
Michael Graff committed
675 676 677
void
dns_message_addname(dns_message_t *msg, dns_name_t *name,
		    dns_section_t section);
Michael Graff's avatar
Michael Graff committed
678 679 680
/*
 * Adds the name to the given section.
 *
Michael Graff's avatar
Michael Graff committed
681 682
 * It is the caller's responsibility to enforce any unique name requirements
 * in a section.
Michael Graff's avatar
Michael Graff committed
683 684 685 686 687 688 689 690
 *
 * Requires:
 *
 *	'msg' be valid, and be a renderable message.
 *
 *	'name' be a valid name.
 *
 *	'section' be a named section.
Michael Graff's avatar
Michael Graff committed
691
 */
692

693 694 695 696 697 698 699 700
/*
 * 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.
 */

701
isc_result_t
702 703 704
dns_message_gettempname(dns_message_t *msg, dns_name_t **item);
/*
 * Return a name that can be used for any temporary purpose, including
705 706 707
 * 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.
708 709 710 711 712 713 714 715 716
 *
 * It is the caller's responsibility to initialize this name.
 *
 * Requires:
 *	msg be a valid message
 *
 *	item != NULL && *item == NULL
 *
 * Returns:
717 718
 *	ISC_R_SUCCESS		-- All is well.
 *	ISC_R_NOMEMORY		-- No item can be allocated.
719 720
 */

721
isc_result_t
722 723 724 725 726 727 728 729 730 731 732 733
dns_message_gettemprdata(dns_message_t *msg, dns_rdata_t **item);
/*
 * Return a rdata that can be used for any temporary purpose, including
 * inserting into the message's linked lists.  The storage associated with
 * this rdata will be destroyed when the message is destroyed or reset.
 *
 * Requires:
 *	msg be a valid message
 *
 *	item != NULL && *item == NULL
 *
 * Returns:
734 735
 *	ISC_R_SUCCESS		-- All is well.
 *	ISC_R_NOMEMORY		-- No item can be allocated.
736 737
 */

738
isc_result_t
739 740 741 742 743 744 745 746 747 748 749 750
dns_message_gettemprdataset(dns_message_t *msg, dns_rdataset_t **item);
/*
 * Return a rdataset that can be used for any temporary purpose, including
 * inserting into the message's linked lists.  The storage associated with
 * this rdataset will be destroyed when the message is destroyed or reset.
 *
 * Requires:
 *	msg be a valid message
 *
 *	item != NULL && *item == NULL
 *
 * Returns:
751 752
 *	ISC_R_SUCCESS		-- All is well.
 *	ISC_R_NOMEMORY		-- No item can be allocated.
753 754
 */

755
isc_result_t
756 757 758 759 760 761 762 763 764 765 766 767
dns_message_gettemprdatalist(dns_message_t *msg, dns_rdatalist_t **item);
/*
 * Return a rdatalist that can be used for any temporary purpose, including
 * inserting into the message's linked lists.  The storage associated with
 * this rdatalist will be destroyed when the message is destroyed or reset.
 *
 * Requires:
 *	msg be a valid message
 *
 *	item != NULL && *item == NULL
 *
 * Returns:
768 769
 *	ISC_R_SUCCESS		-- All is well.
 *	ISC_R_NOMEMORY		-- No item can be allocated.
770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831
 */

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
 */

832
isc_result_t
Michael Graff's avatar
Michael Graff committed
833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850
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:
 *
851
 *	ISC_R_SUCCESS		-- all is well.
Michael Graff's avatar
Michael Graff committed
852
 *
853
 *	ISC_R_UNEXPECTEDEND	-- buffer doesn't contain enough for a header.
Michael Graff's avatar
Michael Graff committed
854 855
 */

856
isc_result_t
Bob Halley's avatar
Bob Halley committed
857 858 859 860 861 862
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
863
 *	'msg' is a valid message with parsing intent, and contains a query.
Bob Halley's avatar
Bob Halley committed
864 865 866 867
 * 
 * Ensures:
 *
 *	The message will have a rendering intent.  If 'want_question_section'
868 869 870 871 872
 *	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
873 874 875
 *
 * Returns:
 *
876
 *	ISC_R_SUCCESS		-- all is well.
Bob Halley's avatar
Bob Halley committed
877
 *
Bob Halley's avatar
Bob Halley committed
878 879 880 881 882 883 884
 *	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
885 886
 */

Bob Halley's avatar
EDNS0  
Bob Halley committed
887 888 889 890 891 892 893 894 895 896 897 898 899 900
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.
 */

901
isc_result_t
Bob Halley's avatar
EDNS0  
Bob Halley committed
902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920
dns_message_setopt(dns_message_t *msg, dns_rdataset_t *opt);
/*
 * Set the OPT record for 'msg'.
 *
 * Requires:
 *
 *	'msg' is a valid message with rendering intent,
 *	dns_message_renderbegin() has been called, and no sections have been
 *	rendered.
 *
 *	'opt' is a valid OPT record.
 *
 * Ensures:
 *
 *	The OPT record will be rendered when dns_message_renderend() is
 *	called.
 *
 * Returns:
 *
921
 *	ISC_R_SUCCESS		-- all is well.
Bob Halley's avatar
EDNS0  
Bob Halley committed
922
 *
923
 *	ISC_R_NOSPACE		-- there is no space for the OPT record.
Bob Halley's avatar
EDNS0  
Bob Halley committed
924 925
 */

926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954
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.
 *	'owner' is not NULL, and *owner is NULL.  Contains the owner on return.
 *
 * Returns:
 *
 *	The TSIG rdataset of 'msg', or NULL if there isn't one.
 */

dns_rdataset_t *
dns_message_getsig0(dns_message_t *msg);
/*
 * Get the SIG(0) record for 'msg'.
 *
 * Requires:
 *
 *	'msg' is a valid message.
 *
 * Returns:
 *
 *	The SIG(0) rdataset of 'msg', or NULL if there isn't one.
 */

955 956 957 958 959 960 961 962 963 964 965 966 967 968 969
void
dns_message_takebuffer(dns_message_t *msg, isc_buffer_t **buffer);
/*
 * Give the *buffer to the message code to clean up when it is no
 * longer needed.  This is usually when the message is reset or
 * destroyed.
 *
 * Requires:
 *
 *	msg be a valid message.
 *
 *	buffer != NULL && *buffer is a valid isc_buffer_t, which was
 *	dynamincally allocated via isc_buffer_allocate().
 */

Brian Wellington's avatar
Brian Wellington committed
970
isc_result_t
971
dns_message_signer(dns_message_t *msg, dns_name_t *signer);
Brian Wellington's avatar
Brian Wellington committed
972
/*
973
 * If this message was signed, return the identity of the signer.
Brian Wellington's avatar
Brian Wellington committed
974 975
 * Unless ISC_R_NOTFOUND is returned, signer will reflect the name of the
 * key that signed the message.
Brian Wellington's avatar
Brian Wellington committed
976 977 978
 *
 * Requires:
 *
979 980
 *	msg is a valid parsed message.
 *	signer is a valid name
Brian Wellington's avatar
Brian Wellington committed
981 982 983 984 985 986
 *
 * Returns:
 *
 *	ISC_R_SUCCESS		- the message was signed, and *signer
 *				  contains the signing identity
 *
987
 *	ISC_R_NOTFOUND		- no TSIG or SIG(0) record is present in the
Brian Wellington's avatar
Brian Wellington committed
988 989
 *				  message
 *
990 991 992 993 994 995 996 997 998 999
 *	DNS_R_TSIGVERIFYFAILURE	- the message was signed by a TSIG, but the
 *				  signature failed to verify
 *
 *	DNS_R_TSIGERRORSET	- the message was signed by a TSIG and
 *				  verified, but the query was rejected by
 *				  the server
 *
 *	DNS_R_NOIDENTITY	- the message was signed by a TSIG and
 *				  verified, but the key has no identity since
 *				  it was generated by an unsigned TKEY process
Brian Wellington's avatar
Brian Wellington committed
1000
 *
1001 1002
 *	DNS_R_SIGINVALID	- the message was signed by a SIG(0), but
 *				  the signature failed to verify
Brian Wellington's avatar
Brian Wellington committed
1003
 *
1004 1005
 *	DNS_R_SIGNOTVERIFIEDYET	- the message was signed by a SIG(0), but
 *				  the signature has not been verified yet
Brian Wellington's avatar
Brian Wellington committed
1006 1007
 */

Brian Wellington's avatar
Brian Wellington committed
1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027
isc_result_t
dns_message_checksig(dns_message_t *msg, dns_view_t *view);
/*
 * If this message was signed, verify the signature.
 *
 * Requires:
 *
 *	msg is a valid parsed message.
 *	view is a valid view
 *
 * Returns:
 *
 *	ISC_R_SUCCESS		- the message was unsigned, or the message
 *				  was signed correctly.
 *
 *	DNS_R_EXPECTEDTSIG	- A TSIG was expected, but not seen
 *	DNS_R_UNEXPECTEDTSIG	- A TSIG was seen but not expected
 *	DNS_R_TSIGVERIFYFAILURE - The TSIG failed to verify
 */

1028 1029
ISC_LANG_ENDDECLS

1030
#endif /* DNS_MESSAGE_H */