message.h 12.1 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 28 29 30 31 32 33 34 35

#include <dns/types.h>
#include <dns/result.h>
#include <dns/name.h>
#include <dns/rdataset.h>

/*
 * How this beast works:
 *
36
 * When a dns message is received in a buffer, dns_message_fromwire() is called
37 38 39 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.
 *
 * TSIG checking is also done at this layer, and any DNSSEC information should
 * also be performed at this time.
 *
45
 * If dns_message_fromwire() returns DNS_R_MOREDATA additional
46 47
 * message packets are required.  This implies an EDNS message.
 *
48
 * When going from structure to wire, dns_message_towire() will return
Michael Graff's avatar
typo  
Michael Graff committed
49
 * DNS_R_MOREDATA if there is more data left in the output buffer that
50 51 52 53 54 55 56 57 58
 * 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

59 60 61 62 63
#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
64

65 66 67
#define DNS_MESSAGE_OPCODE_MASK		0x7000U
#define DNS_MESSAGE_OPCODE_SHIFT	    11
#define DNS_MESSAGE_RCODE_MASK		0x000fU
68
#define DNS_MESSAGE_FLAG_MASK		0x1ff0U
69

Michael Graff's avatar
Michael Graff committed
70 71
#define DNS_MESSAGE_HEADER_LEN		    12 /* 6 u_int16_t's */

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

/*
Michael Graff's avatar
Michael Graff committed
87
 * These tell the message library how the created dns_message_t will be used.
Michael Graff's avatar
Michael Graff committed
88
 */
Michael Graff's avatar
Michael Graff committed
89 90 91 92
#define DNS_MESSAGE_INTENT_UNKNOWN	0 /* internal use only */
#define DNS_MESSAGE_INTENT_PARSE	1 /* parsing messages */
#define DNS_MESSAGE_INTENT_RENDER	2 /* rendering */

Michael Graff's avatar
Michael Graff committed
93 94
typedef struct dns_msgblock dns_msgblock_t;

95
typedef struct {
Michael Graff's avatar
Michael Graff committed
96
	unsigned int			magic;
97

Michael Graff's avatar
Michael Graff committed
98
	unsigned int			id;
Michael Graff's avatar
Michael Graff committed
99 100 101
	unsigned int			flags;
	unsigned int			rcode;
	unsigned int			opcode;
Michael Graff's avatar
Michael Graff committed
102
	dns_rdataclass_t		rdclass;
Michael Graff's avatar
Michael Graff committed
103

Michael Graff's avatar
Michael Graff committed
104
	/* 4 real, 2 pseudo */
Michael Graff's avatar
Michael Graff committed
105
	unsigned int			counts[DNS_SECTION_MAX];
Michael Graff's avatar
Michael Graff committed
106 107
	dns_namelist_t			sections[DNS_SECTION_MAX];
	dns_name_t		       *cursors[DNS_SECTION_MAX];
108

Michael Graff's avatar
Michael Graff committed
109
	int				state;
Michael Graff's avatar
Michael Graff committed
110
	unsigned int			from_to_wire : 2;
Michael Graff's avatar
Michael Graff committed
111
	unsigned int			reserved;
112

113 114
	isc_buffer_t		       *buffer;

Michael Graff's avatar
Michael Graff committed
115
	isc_mem_t		       *mctx;
Michael Graff's avatar
Michael Graff committed
116
	ISC_LIST(isc_dynbuffer_t)	scratchpad;
Michael Graff's avatar
Michael Graff committed
117 118
	ISC_LIST(dns_msgblock_t)	names;
	ISC_LIST(dns_msgblock_t)	rdatas;
Michael Graff's avatar
Michael Graff committed
119
	ISC_LIST(dns_msgblock_t)	rdatasets;
Michael Graff's avatar
Michael Graff committed
120
	ISC_LIST(dns_msgblock_t)	rdatalists;
Michael Graff's avatar
Michael Graff committed
121 122 123 124
	dns_name_t		       *nextname;
	dns_rdata_t		       *nextrdata;
	dns_rdataset_t		       *nextrdataset;
	dns_rdatalist_t		       *nextrdatalist;
Michael Graff's avatar
Michael Graff committed
125
} dns_message_t;
126

Michael Graff's avatar
Michael Graff committed
127
dns_result_t
Michael Graff's avatar
Michael Graff committed
128
dns_message_create(isc_mem_t *mctx, dns_message_t **msg, unsigned int intent);
129
/*
Michael Graff's avatar
Michael Graff committed
130
 * Initialize msg structure.  Must be called on a new (or reused) structure.
131
 *
Michael Graff's avatar
Michael Graff committed
132 133 134 135 136
 * This function will allocate some internal blocks of memory that are
 * exptected to be needed for parsing or rendering nearly any type of message.
 *
 * Requires:
 *	'mctx' be a valid memory context.
137
 *
Michael Graff's avatar
Michael Graff committed
138 139 140 141
 *	'msg' be non-null and '*msg' be NULL.
 *
 *	'intent' must be one of DNS_MESSAGE_INTENT_PARSE or
 *	DNS_MESSAGE_INTENT_RENDER.
Michael Graff's avatar
Michael Graff committed
142 143
 *
 * Ensures:
Michael Graff's avatar
Michael Graff committed
144
 *	The data in "*msg" is set to indicate an unused and empty msg
Michael Graff's avatar
Michael Graff committed
145 146 147
 *	structure.
 *
 * Returns:
148 149
 *	DNS_R_NOMEMORY		-- out of memory
 *	DNS_R_SUCCESS		-- success
150 151
 */

Michael Graff's avatar
Michael Graff committed
152 153 154 155 156 157 158 159 160 161 162 163 164 165 166
void
dns_message_reset(dns_message_t *msg);
/*
 * 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.
 *
 * Requires:
 *
 *	'msg' be valid.
 *
 *	If any data loanouts (buffers, names, rdatas, etc) were requested,
 *	the caller must no longer use them after this call.
 */
Michael Graff's avatar
Michael Graff committed
167

Michael Graff's avatar
Michael Graff committed
168
void
Michael Graff's avatar
Michael Graff committed
169
dns_message_destroy(dns_message_t **msg);
Michael Graff's avatar
Michael Graff committed
170
/*
Michael Graff's avatar
Michael Graff committed
171 172 173 174 175 176 177
 * Destroy all state in the message.
 *
 * Requires:
 *
 *	'msg' be valid.
 *
 *	'msg' be "empty" with no message elements on the internal lists.
Michael Graff's avatar
Michael Graff committed
178 179
 *
 * Ensures:
Michael Graff's avatar
Michael Graff committed
180
 *	'msg' can be reused via re-initialization with dns_message_init()
Michael Graff's avatar
Michael Graff committed
181 182
 */

Michael Graff's avatar
Michael Graff committed
183
dns_result_t
Michael Graff's avatar
Michael Graff committed
184
dns_message_parse(dns_message_t *msg, isc_buffer_t *source);
Michael Graff's avatar
Michael Graff committed
185
/*
Michael Graff's avatar
Michael Graff committed
186 187 188 189 190 191 192
 * 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
193 194 195 196 197 198 199 200 201 202 203 204 205 206
 *
 * 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.
 *
 *	"buffer" have "sane" contents.
 *
 * Ensures:
 *	The buffer's data format is correct.
 *
Michael Graff's avatar
Michael Graff committed
207 208
 *	The buffer's contents verify as correct regarding header bits, buffer
 * 	and rdata sizes, etc.
Michael Graff's avatar
Michael Graff committed
209 210 211
 *
 * Returns:
 *	DNS_R_SUCCESS		-- all is well
Michael Graff's avatar
Michael Graff committed
212
 *	DNS_R_NOMEMORY		-- no memory
Michael Graff's avatar
Michael Graff committed
213 214 215 216
 *	DNS_R_MOREDATA		-- more packets needed for complete message
 *	DNS_R_???		-- bad signature (XXX need more of these)
 */

Michael Graff's avatar
Michael Graff committed
217 218
dns_result_t
dns_message_renderbegin(dns_message_t *msg, isc_buffer_t *buffer);
Michael Graff's avatar
Michael Graff committed
219
/*
Michael Graff's avatar
Michael Graff committed
220 221
 * Begin rendering on a message.  Only one call can be made to this function
 * per message.
Michael Graff's avatar
Michael Graff committed
222 223
 *
 * Requires:
Michael Graff's avatar
Michael Graff committed
224 225
 *
 *	'msg' be valid.
226 227 228 229
 *
 *	buffer != NULL.
 *
 *	buffer is empty.
Michael Graff's avatar
Michael Graff committed
230 231 232
 */

dns_result_t
233 234 235 236 237 238 239 240 241 242 243 244 245 246 247
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.
 *
 * Requires:
 *
 *	'msg' be valid.
 *
 *	dns_message_renderbegin() was called.
 *
 *	buffer != NULL.
 *
 * Returns:
 *
248 249
 *	DNS_R_NOSPACE		-- new buffer is too small
 *	DNS_R_SUCCESS		-- all is well.
250 251 252 253
 */

dns_result_t
dns_message_renderreserve(dns_message_t *msg, unsigned int space);
Michael Graff's avatar
Michael Graff committed
254 255 256 257 258 259
/*
 * Reserve "space" bytes in the given buffer.
 *
 * Requires:
 *
 *	'msg' be valid.
Michael Graff's avatar
Michael Graff committed
260
 *
261 262
 *	dns_message_renderbegin() was called.
 *
Michael Graff's avatar
Michael Graff committed
263
 * Returns:
Michael Graff's avatar
Michael Graff committed
264 265 266
 *
 *	DNS_R_SUCCESS		-- all is well.
 *	DNS_R_NOSPACE		-- not enough free space in the buffer.
Michael Graff's avatar
Michael Graff committed
267 268
 */

Michael Graff's avatar
Michael Graff committed
269 270 271 272 273 274 275 276 277
dns_result_t
dns_message_renderrelease(dns_message_t *msg, unsigned int space);
/*
 * Release "space" bytes in the given buffer that was previously reserved.
 *
 * Requires:
 *
 *	'msg' be valid.
 *
278 279
 *	dns_message_renderbegin() was called.
 *
Michael Graff's avatar
Michael Graff committed
280 281 282 283 284 285
 * Returns:
 *
 *	DNS_R_SUCCESS		-- all is well.
 *	DNS_R_NOSPACE		-- trying to release more than was reserved.
 */

Michael Graff's avatar
Michael Graff committed
286
dns_result_t
287 288
dns_message_rendersection(dns_message_t *msg, dns_section_t section,
			  unsigned int priority, unsigned int flags);
Michael Graff's avatar
Michael Graff committed
289
/*
Michael Graff's avatar
Michael Graff committed
290 291
 * Render all names, rdatalists, etc from the given section at the
 * specified priority or higher.
Michael Graff's avatar
Michael Graff committed
292 293
 *
 * Requires:
Michael Graff's avatar
Michael Graff committed
294 295 296
 *	'msg' be valid.
 *
 *	'section' be a valid section.
Michael Graff's avatar
Michael Graff committed
297
 *
Michael Graff's avatar
Michael Graff committed
298 299
 *	'buffer' be non-NULL and be initialized to point to a valid memory
 *	block.
Michael Graff's avatar
Michael Graff committed
300
 *
301 302
 *	dns_message_renderbegin() was called.
 *
Michael Graff's avatar
Michael Graff committed
303
 * Returns:
Michael Graff's avatar
Michael Graff committed
304 305 306 307 308 309
 *	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
310 311
 */

Michael Graff's avatar
Michael Graff committed
312
dns_result_t
313
dns_message_renderend(dns_message_t *msg);
Michael Graff's avatar
Michael Graff committed
314
/*
Michael Graff's avatar
Michael Graff committed
315 316 317 318 319 320 321 322
 * 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.
 *
323 324
 *	dns_message_renderbegin() was called.
 *
Michael Graff's avatar
Michael Graff committed
325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359
 * 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.
 *
 * 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
360 361 362
 *	dns_message_firstname() must have been called on this section,
 *	and the result was DNS_R_SUCCESS.
 *
Michael Graff's avatar
Michael Graff committed
363 364 365 366 367 368
 * Returns:
 *
 *	DNS_R_SUCCESS		-- All is well.
 *	DNS_R_NOMORE		-- No names in given section.
 */

Michael Graff's avatar
Michael Graff committed
369
void
Michael Graff's avatar
Michael Graff committed
370 371 372 373 374 375 376 377 378 379 380 381 382 383
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.
 *
 * Requires:
 *
 *	'msg' be valid.
 *
 *	'name' be non-NULL, and *name be NULL.
 *
 *	'section' be a valid section.
 *
Michael Graff's avatar
Michael Graff committed
384 385 386
 *	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
387 388
 */

Michael Graff's avatar
Michael Graff committed
389 390 391 392
dns_result_t
dns_message_findname(dns_message_t *msg, dns_section_t section,
		     dns_name_t *target, dns_rdatatype_t type,
		     dns_name_t **name, dns_rdataset_t **rdataset);
Michael Graff's avatar
Michael Graff committed
393
/*
Michael Graff's avatar
Michael Graff committed
394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419
 * 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
 * rdataset (if type is specified as other than dns_rdatatype_any.)
 *
 * Requires:
 *	'msg' be valid.
 *
 *	'section' be a valid section.
 *
 *	If a pointer to the name is desired, 'name' should be non-NULL.
 *	If it is non-NULL, '*name' MUST be NULL.
 *
 *	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
 *	rdataset.
 *
 *	'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
420 421
 */

Michael Graff's avatar
Michael Graff committed
422 423 424 425
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
426 427
/*
 * Move a name from one section to another.
Michael Graff's avatar
Michael Graff committed
428 429 430 431 432
 *
 * Requires:
 *
 *	'msg' be valid.
 *
Michael Graff's avatar
Michael Graff committed
433
 *	'name' must be in 'fromsection'.
Michael Graff's avatar
Michael Graff committed
434 435 436 437
 *
 *	'fromsection' must be a valid section.
 *
 *	'tosection' must be a valid section, and be renderable.
Michael Graff's avatar
Michael Graff committed
438 439
 *
 *	'fromsection' and 'tosection' cannot be the same section.
Michael Graff's avatar
Michael Graff committed
440 441
 */

Michael Graff's avatar
Michael Graff committed
442 443 444
void
dns_message_addname(dns_message_t *msg, dns_name_t *name,
		    dns_section_t section);
Michael Graff's avatar
Michael Graff committed
445 446 447
/*
 * Adds the name to the given section.
 *
Michael Graff's avatar
Michael Graff committed
448 449
 * Caller must ensure that the name does not already exist.  This condition
 * is NOT checked for by this function.
Michael Graff's avatar
Michael Graff committed
450 451 452 453 454 455 456 457
 *
 * 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
458
 */
459 460 461 462

ISC_LANG_ENDDECLS

#endif	/* DNS_DNS_H */