message.h 20.7 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
/*
 * 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.
 */

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

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

#include <isc/mem.h>
Michael Graff's avatar
Michael Graff committed
26
#include <isc/buffer.h>
27
#include <isc/bufferlist.h>
28 29 30 31 32

#include <dns/types.h>
#include <dns/result.h>
#include <dns/name.h>
#include <dns/rdataset.h>
33
#include <dns/rdatastruct.h>
Michael Graff's avatar
Michael Graff committed
34
#include <dns/compress.h>
35 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 45 46 47
 * 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.
 *
48
 * If dns_message_fromwire() returns DNS_R_MOREDATA additional
49 50
 * message packets are required.  This implies an EDNS message.
 *
51
 * When going from structure to wire, dns_message_towire() will return
Michael Graff's avatar
typo  
Michael Graff committed
52
 * DNS_R_MOREDATA if there is more data left in the output buffer that
53 54 55 56 57 58 59 60 61
 * could not be rendered into the exisiting buffer.
 *
 * 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.
 */

ISC_LANG_BEGINDECLS

62 63 64 65 66
#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
67

Bob Halley's avatar
Bob Halley committed
68
#define DNS_MESSAGE_REPLYPRESERVE	(DNS_MESSAGEFLAG_RD)
69

Bob Halley's avatar
Bob Halley committed
70 71 72 73
#define DNS_MESSAGE_HEADERLEN		12 /* 6 isc_uint16_t's */

#define DNS_MESSAGE_MAGIC		0x4d534740U	/* MSG@ */
#define DNS_MESSAGE_VALID(msg)		(((msg)->magic) == DNS_MESSAGE_MAGIC)
Michael Graff's avatar
Michael Graff committed
74

Michael Graff's avatar
Michael Graff committed
75 76 77 78
/*
 * 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
79
typedef int dns_section_t;
Michael Graff's avatar
Michael Graff committed
80
#define DNS_SECTION_ANY			(-1)
Michael Graff's avatar
Michael Graff committed
81 82 83 84
#define DNS_SECTION_QUESTION		0
#define DNS_SECTION_ANSWER		1
#define DNS_SECTION_AUTHORITY		2
#define DNS_SECTION_ADDITIONAL		3
85 86 87 88 89 90 91 92 93
#define DNS_SECTION_TSIG		4 /* pseudo-section */
#define DNS_SECTION_MAX			5

/*
 * Dynamic update named for these sections.
 */
#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
94 95

/*
Michael Graff's avatar
Michael Graff committed
96
 * These tell the message library how the created dns_message_t will be used.
Michael Graff's avatar
Michael Graff committed
97
 */
Michael Graff's avatar
Michael Graff committed
98 99 100
#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
101

Michael Graff's avatar
Michael Graff committed
102 103
typedef struct dns_msgblock dns_msgblock_t;

Bob Halley's avatar
Bob Halley committed
104
struct dns_message {
Michael Graff's avatar
Michael Graff committed
105
	/* public from here down */
Michael Graff's avatar
Michael Graff committed
106
	unsigned int			magic;
107

108
	dns_messageid_t			id;
Michael Graff's avatar
Michael Graff committed
109 110 111
	unsigned int			flags;
	unsigned int			rcode;
	unsigned int			opcode;
Michael Graff's avatar
Michael Graff committed
112
	dns_rdataclass_t		rdclass;
Michael Graff's avatar
Michael Graff committed
113

114
	/* 4 real, 1 pseudo */
Michael Graff's avatar
Michael Graff committed
115
	unsigned int			counts[DNS_SECTION_MAX];
Michael Graff's avatar
Michael Graff committed
116 117

	/* private from here down */
Michael Graff's avatar
Michael Graff committed
118 119
	dns_namelist_t			sections[DNS_SECTION_MAX];
	dns_name_t		       *cursors[DNS_SECTION_MAX];
Bob Halley's avatar
Bob Halley committed
120
	dns_rdataset_t		       *opt;
121

Michael Graff's avatar
Michael Graff committed
122
	int				state;
Michael Graff's avatar
Michael Graff committed
123
	unsigned int			from_to_wire : 2;
124
	unsigned int			need_cctx_cleanup : 1;
Bob Halley's avatar
Bob Halley committed
125 126
	unsigned int			header_ok : 1;
	unsigned int			question_ok : 1;
127 128

	unsigned int			reserved; /* reserved space (render) */
129

130
	isc_buffer_t		       *buffer;
Michael Graff's avatar
Michael Graff committed
131
	dns_compress_t			cctx;
132

Michael Graff's avatar
Michael Graff committed
133
	isc_mem_t		       *mctx;
134
	isc_bufferlist_t		scratchpad;
Michael Graff's avatar
Michael Graff committed
135 136
	ISC_LIST(dns_msgblock_t)	names;
	ISC_LIST(dns_msgblock_t)	rdatas;
Michael Graff's avatar
Michael Graff committed
137
	ISC_LIST(dns_msgblock_t)	rdatasets;
Michael Graff's avatar
Michael Graff committed
138
	ISC_LIST(dns_msgblock_t)	rdatalists;
139 140 141 142 143

	ISC_LIST(dns_name_t)		freename;
	ISC_LIST(dns_rdata_t)		freerdata;
	ISC_LIST(dns_rdataset_t)	freerdataset;
	ISC_LIST(dns_rdatalist_t)	freerdatalist;
144 145 146 147 148 149 150

	dns_rcode_t			tsigstatus;
	dns_rcode_t			querytsigstatus;
	dns_rdata_any_tsig_t	       *tsig;
	dns_rdata_any_tsig_t	       *querytsig;
	dns_tsig_key_t		       *tsigkey;
	int				tsigstart;
Bob Halley's avatar
Bob Halley committed
151
};
152

Michael Graff's avatar
Michael Graff committed
153
dns_result_t
Bob Halley's avatar
Bob Halley committed
154 155 156
dns_message_create(isc_mem_t *mctx, unsigned int intent,
		   dns_message_t **msgp);
		   
157
/*
Michael Graff's avatar
Michael Graff committed
158
 * Create msg structure.
159
 *
Michael Graff's avatar
Michael Graff committed
160
 * This function will allocate some internal blocks of memory that are
Michael Graff's avatar
Michael Graff committed
161
 * expected to be needed for parsing or rendering nearly any type of message.
Michael Graff's avatar
Michael Graff committed
162 163 164
 *
 * Requires:
 *	'mctx' be a valid memory context.
165
 *
Michael Graff's avatar
Michael Graff committed
166
 *	'msgp' be non-null and '*msg' be NULL.
Michael Graff's avatar
Michael Graff committed
167
 *
Andreas Gustafsson's avatar
typo  
Andreas Gustafsson committed
168 169
 *	'intent' must be one of DNS_MESSAGE_INTENTPARSE or
 *	DNS_MESSAGE_INTENTRENDER.
Michael Graff's avatar
Michael Graff committed
170 171
 *
 * Ensures:
Michael Graff's avatar
Michael Graff committed
172
 *	The data in "*msg" is set to indicate an unused and empty msg
Michael Graff's avatar
Michael Graff committed
173 174 175
 *	structure.
 *
 * Returns:
176 177
 *	DNS_R_NOMEMORY		-- out of memory
 *	DNS_R_SUCCESS		-- success
178 179
 */

Michael Graff's avatar
Michael Graff committed
180
void
Bob Halley's avatar
Bob Halley committed
181
dns_message_reset(dns_message_t *msg, unsigned int intent);
Michael Graff's avatar
Michael Graff committed
182 183 184 185 186 187
/*
 * 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
188 189 190
 * 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
191 192
 * The intended next use of the message will be 'intent'.
 *
Michael Graff's avatar
Michael Graff committed
193 194 195
 * Requires:
 *
 *	'msg' be valid.
Bob Halley's avatar
Bob Halley committed
196 197
 *
 *	'intent' is DNS_MESSAGE_INTENTPARSE or DNS_MESSAGE_INTENTRENDER
Michael Graff's avatar
Michael Graff committed
198
 */
Michael Graff's avatar
Michael Graff committed
199

Michael Graff's avatar
Michael Graff committed
200
void
Michael Graff's avatar
Michael Graff committed
201
dns_message_destroy(dns_message_t **msgp);
Michael Graff's avatar
Michael Graff committed
202
/*
Michael Graff's avatar
Michael Graff committed
203 204 205 206
 * Destroy all state in the message.
 *
 * Requires:
 *
Michael Graff's avatar
Michael Graff committed
207
 *	'msgp' be valid.
Michael Graff's avatar
Michael Graff committed
208 209
 *
 * Ensures:
Michael Graff's avatar
Michael Graff committed
210
 *	'*msgp' == NULL
Michael Graff's avatar
Michael Graff committed
211 212
 */

Michael Graff's avatar
Michael Graff committed
213
dns_result_t
214 215
dns_message_parse(dns_message_t *msg, isc_buffer_t *source,
		  isc_boolean_t preserve_order);
Michael Graff's avatar
Michael Graff committed
216
/*
Michael Graff's avatar
Michael Graff committed
217 218 219 220 221 222 223
 * 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".
 * TSIGs are detected and stored in the pseudo-section "tsig".  At detection
 * time, the TSIG is verified (XXX) and the message fails if the TSIG fails
 * to verify.
Michael Graff's avatar
Michael Graff committed
224
 *
225 226 227 228 229 230 231 232 233 234 235 236
 * 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
237 238 239 240 241 242 243 244
 * 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,
 * this function should be repeated with all input buffers until DNS_R_SUCCESS
 * (or an error) is returned.
 *
 * Requires:
 *	"msg" be valid.
 *
Michael Graff's avatar
Michael Graff committed
245
 *	"buffer" be a wire format binary buffer.
Michael Graff's avatar
Michael Graff committed
246 247 248 249
 *
 * Ensures:
 *	The buffer's data format is correct.
 *
Michael Graff's avatar
Michael Graff committed
250 251
 *	The buffer's contents verify as correct regarding header bits, buffer
 * 	and rdata sizes, etc.
Michael Graff's avatar
Michael Graff committed
252 253 254
 *
 * Returns:
 *	DNS_R_SUCCESS		-- all is well
Michael Graff's avatar
Michael Graff committed
255
 *	DNS_R_NOMEMORY		-- no memory
Michael Graff's avatar
Michael Graff committed
256
 *	DNS_R_MOREDATA		-- more packets needed for complete message
Michael Graff's avatar
Michael Graff committed
257 258
 *	DNS_R_???		-- bad signature (XXXMLG need more of these)
 *	Many other errors possible XXXMLG
Michael Graff's avatar
Michael Graff committed
259 260
 */

Michael Graff's avatar
Michael Graff committed
261 262
dns_result_t
dns_message_renderbegin(dns_message_t *msg, isc_buffer_t *buffer);
Michael Graff's avatar
Michael Graff committed
263
/*
Michael Graff's avatar
Michael Graff committed
264 265
 * Begin rendering on a message.  Only one call can be made to this function
 * per message.
Michael Graff's avatar
Michael Graff committed
266
 *
Michael Graff's avatar
Michael Graff committed
267 268 269
 * The buffer is "owned" buy the message library until dns_message_renderend()
 * is called.
 *
Michael Graff's avatar
Michael Graff committed
270
 * Requires:
Michael Graff's avatar
Michael Graff committed
271 272
 *
 *	'msg' be valid.
273
 *
Michael Graff's avatar
Michael Graff committed
274 275 276 277 278
 *	buffer is a valid binary buffer.
 *
 * Side Effects:
 *
 *	The buffer is cleared before it is used.
279
 *
Michael Graff's avatar
Michael Graff committed
280 281 282 283
 * Returns:
 *	DNS_R_SUCCESS		-- all is well
 *	DNS_R_NOSPACE		-- output buffer is too small
 *	Anything that dns_compress_init() can return.
Michael Graff's avatar
Michael Graff committed
284 285 286
 */

dns_result_t
287 288 289 290 291
dns_message_renderchangebuffer(dns_message_t *msg, isc_buffer_t *buffer);
/*
 * Reset the buffer.  This can be used after growing the old buffer
 * on a DNS_R_NOSPACE return from most of the render functions.
 *
Michael Graff's avatar
Michael Graff committed
292 293 294 295
 * 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.
 *
296 297 298 299 300 301 302 303 304
 * Requires:
 *
 *	'msg' be valid.
 *
 *	dns_message_renderbegin() was called.
 *
 *	buffer != NULL.
 *
 * Returns:
305 306
 *	DNS_R_NOSPACE		-- new buffer is too small
 *	DNS_R_SUCCESS		-- all is well.
307 308 309 310
 */

dns_result_t
dns_message_renderreserve(dns_message_t *msg, unsigned int space);
Michael Graff's avatar
Michael Graff committed
311
/*
Michael Graff's avatar
Michael Graff committed
312 313 314
 * XXXMLG should use size_t rather than unsigned int once the buffer
 * API is cleaned up
 *
Michael Graff's avatar
Michael Graff committed
315 316 317 318 319
 * Reserve "space" bytes in the given buffer.
 *
 * Requires:
 *
 *	'msg' be valid.
Michael Graff's avatar
Michael Graff committed
320
 *
321 322
 *	dns_message_renderbegin() was called.
 *
Michael Graff's avatar
Michael Graff committed
323
 * Returns:
Michael Graff's avatar
Michael Graff committed
324 325
 *	DNS_R_SUCCESS		-- all is well.
 *	DNS_R_NOSPACE		-- not enough free space in the buffer.
Michael Graff's avatar
Michael Graff committed
326 327
 */

Michael Graff's avatar
Michael Graff committed
328 329 330
dns_result_t
dns_message_renderrelease(dns_message_t *msg, unsigned int space);
/*
Michael Graff's avatar
Michael Graff committed
331 332 333
 * XXXMLG should use size_t rather than unsigned int once the buffer
 * API is cleaned up
 *
Michael Graff's avatar
Michael Graff committed
334 335 336 337 338 339
 * Release "space" bytes in the given buffer that was previously reserved.
 *
 * Requires:
 *
 *	'msg' be valid.
 *
340 341
 *	dns_message_renderbegin() was called.
 *
Michael Graff's avatar
Michael Graff committed
342 343 344 345 346
 * Returns:
 *	DNS_R_SUCCESS		-- all is well.
 *	DNS_R_NOSPACE		-- trying to release more than was reserved.
 */

Michael Graff's avatar
Michael Graff committed
347
dns_result_t
348
dns_message_rendersection(dns_message_t *msg, dns_section_t section,
Michael Graff's avatar
Michael Graff committed
349
			  unsigned int priority, unsigned int options);
Michael Graff's avatar
Michael Graff committed
350
/*
Michael Graff's avatar
Michael Graff committed
351 352
 * Render all names, rdatalists, etc from the given section at the
 * specified priority or higher.
Michael Graff's avatar
Michael Graff committed
353 354
 *
 * Requires:
Michael Graff's avatar
Michael Graff committed
355 356 357
 *	'msg' be valid.
 *
 *	'section' be a valid section.
Michael Graff's avatar
Michael Graff committed
358
 *
359 360
 *	dns_message_renderbegin() was called.
 *
Michael Graff's avatar
Michael Graff committed
361
 * Returns:
Michael Graff's avatar
Michael Graff committed
362 363 364 365 366 367
 *	DNS_R_SUCCESS		-- all records were written, and there are
 *				   no more records for this section.
 *	DNS_R_NOSPACE		-- Not enough room in the buffer to write
 *				   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
368 369
 */

370 371 372 373 374 375 376 377 378 379 380 381 382 383 384
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
 */

Michael Graff's avatar
Michael Graff committed
385
dns_result_t
386
dns_message_renderend(dns_message_t *msg);
Michael Graff's avatar
Michael Graff committed
387
/*
Michael Graff's avatar
Michael Graff committed
388 389 390 391 392 393 394 395
 * 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.
 *
396 397
 *	dns_message_renderbegin() was called.
 *
Michael Graff's avatar
Michael Graff committed
398 399 400 401 402 403 404 405 406 407
 * Returns:
 *	DNS_R_SUCCESS		-- all is well.
 */
		      

dns_result_t
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
408
 * The functions dns_message_firstname() and dns_message_nextname() may
409
 * be used for iterating over the owner names in a section. 
Michael Graff's avatar
Michael Graff committed
410
 *
Michael Graff's avatar
Michael Graff committed
411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433
 * Requires:
 *
 *   	'msg' be valid.
 *
 *	'section' be a valid section.
 *
 * Returns:
 *	DNS_R_SUCCESS		-- All is well.
 *	DNS_R_NOMORE		-- No names on given section.
 */

dns_result_t
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
434 435 436
 *	dns_message_firstname() must have been called on this section,
 *	and the result was DNS_R_SUCCESS.
 *
Michael Graff's avatar
Michael Graff committed
437 438 439 440 441
 * Returns:
 *	DNS_R_SUCCESS		-- All is well.
 *	DNS_R_NOMORE		-- No names in given section.
 */

Michael Graff's avatar
Michael Graff committed
442
void
Michael Graff's avatar
Michael Graff committed
443 444 445 446 447 448
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
449 450 451
 * 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
452 453 454 455 456 457 458 459
 * Requires:
 *
 *	'msg' be valid.
 *
 *	'name' be non-NULL, and *name be NULL.
 *
 *	'section' be a valid section.
 *
Michael Graff's avatar
Michael Graff committed
460 461 462
 *	dns_message_firstname() must have been called on this section,
 *	and the result of it and any dns_message_nextname() calls was
 *	DNS_R_SUCCESS.
Michael Graff's avatar
Michael Graff committed
463 464
 */

Michael Graff's avatar
Michael Graff committed
465 466 467
dns_result_t
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
468 469
		     dns_rdatatype_t covers, dns_name_t **foundname,
		     dns_rdataset_t **rdataset);
Michael Graff's avatar
Michael Graff committed
470
/*
Michael Graff's avatar
Michael Graff committed
471 472
 * 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
473
 * rdataset (if type is specified as other than dns_rdatatype_any).
Michael Graff's avatar
Michael Graff committed
474 475 476 477 478 479
 *
 * Requires:
 *	'msg' be valid.
 *
 *	'section' be a valid section.
 *
Michael Graff's avatar
Michael Graff committed
480 481
 *	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
482 483 484
 *
 *	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
485
 *	rdataset.  If the type is dns_datatype_any, 'rdataset' must be NULL.
Michael Graff's avatar
Michael Graff committed
486 487 488 489 490 491 492 493 494 495
 *
 *	'target' be a valid name.
 *
 *	'type' be a valid type.
 *
 * Returns:
 *	DNS_R_SUCCESS		-- all is well.
 *	DNS_R_NXDOMAIN		-- name does not exist in that section.
 *	DNS_R_NXRDATASET	-- The name does exist, but the desired
 *				   type does not.
Michael Graff's avatar
Michael Graff committed
496 497
 */

498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514
dns_result_t
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:
 *	DNS_R_SUCCESS		-- all is well.
 *	DNS_R_NOTFOUND		-- the desired type does not exist.
 */

Michael Graff's avatar
Michael Graff committed
515 516 517 518
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
519 520
/*
 * Move a name from one section to another.
Michael Graff's avatar
Michael Graff committed
521 522 523 524 525
 *
 * Requires:
 *
 *	'msg' be valid.
 *
Michael Graff's avatar
Michael Graff committed
526
 *	'name' must be a name already in 'fromsection'.
Michael Graff's avatar
Michael Graff committed
527 528 529
 *
 *	'fromsection' must be a valid section.
 *
Michael Graff's avatar
Michael Graff committed
530
 *	'tosection' must be a valid section.
Michael Graff's avatar
Michael Graff committed
531 532
 */

Michael Graff's avatar
Michael Graff committed
533 534 535
void
dns_message_addname(dns_message_t *msg, dns_name_t *name,
		    dns_section_t section);
Michael Graff's avatar
Michael Graff committed
536 537 538
/*
 * Adds the name to the given section.
 *
Michael Graff's avatar
Michael Graff committed
539 540
 * It is the caller's responsibility to enforce any unique name requirements
 * in a section.
Michael Graff's avatar
Michael Graff committed
541 542 543 544 545 546 547 548
 *
 * 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
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 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 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 687 688
/*
 * 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.
 */

dns_result_t
dns_message_gettempname(dns_message_t *msg, dns_name_t **item);
/*
 * Return a name that can be used for any temporary purpose, including
 * inserting into the message's linked lists.  The storage associated with
 * this name will be destroyed when the message is destroyed or reset.
 *
 * It is the caller's responsibility to initialize this name.
 *
 * Requires:
 *	msg be a valid message
 *
 *	item != NULL && *item == NULL
 *
 * Returns:
 *	DNS_R_SUCCESS		-- All is well.
 *	DNS_R_NOMEMORY		-- No item can be allocated.
 */

dns_result_t
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:
 *	DNS_R_SUCCESS		-- All is well.
 *	DNS_R_NOMEMORY		-- No item can be allocated.
 */

dns_result_t
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:
 *	DNS_R_SUCCESS		-- All is well.
 *	DNS_R_NOMEMORY		-- No item can be allocated.
 */

dns_result_t
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:
 *	DNS_R_SUCCESS		-- All is well.
 *	DNS_R_NOMEMORY		-- No item can be allocated.
 */

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

Michael Graff's avatar
Michael Graff committed
689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712
dns_result_t
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:
 *
 *	DNS_R_SUCCESS		-- all is well.
 *
 *	DNS_R_UNEXPECTEDEND	-- buffer doesn't contain enough for a header.
 */

Bob Halley's avatar
Bob Halley committed
713 714 715 716 717 718 719
dns_result_t
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
720
 *	'msg' is a valid message with parsing intent, and contains a query.
Bob Halley's avatar
Bob Halley committed
721 722 723 724
 * 
 * Ensures:
 *
 *	The message will have a rendering intent.  If 'want_question_section'
Bob Halley's avatar
Bob Halley committed
725 726 727 728 729
 *	is true, the message opcode is query, 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
730 731 732 733 734
 *
 * Returns:
 *
 *	DNS_R_SUCCESS		-- all is well.
 *
Bob Halley's avatar
Bob Halley committed
735 736 737 738 739 740 741
 *	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
742 743
 */

Bob Halley's avatar
EDNS0  
Bob Halley committed
744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782
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.
 */

dns_result_t
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:
 *
 *	DNS_R_SUCCESS		-- all is well.
 *
 *	DNS_R_NOSPACE		-- there is no space for the OPT record.
 */

783 784 785
ISC_LANG_ENDDECLS

#endif	/* DNS_DNS_H */