1 /*
2  * packet.h
3  *
4  * DNS packet definitions
5  *
6  * a Net::DNS like library for C
7  *
8  * (c) NLnet Labs, 2005-2006
9  *
10  * See the file LICENSE for the license
11  */
12 
13 /**
14  * \file
15  *
16  * Contains the definition of ldns_pkt and its parts, as well
17  * as functions to manipulate those.
18  */
19 
20 
21 #ifndef LDNS_PACKET_H
22 #define LDNS_PACKET_H
23 
24 #define LDNS_MAX_PACKETLEN         65535
25 
26 /* allow flags to be given to ldns_pkt_query_new */
27 #define LDNS_QR		1       /* Query Response flag */
28 #define LDNS_AA		2       /* Authoritative Answer - server flag */
29 #define LDNS_TC		4       /* TrunCated - server flag */
30 #define LDNS_RD		8       /* Recursion Desired - query flag */
31 #define LDNS_CD		16      /* Checking Disabled - query flag */
32 #define LDNS_RA		32      /* Recursion Available - server flag */
33 #define LDNS_AD		64      /* Authenticated Data - server flag */
34 
35 #include <ldns/error.h>
36 #include <ldns/common.h>
37 #include <ldns/rr.h>
38 #include <sys/time.h>
39 
40 #ifdef __cplusplus
41 extern "C" {
42 #endif
43 
44 /* opcodes for pkt's */
45 enum ldns_enum_pkt_opcode {
46 	LDNS_PACKET_QUERY = 0,
47 	LDNS_PACKET_IQUERY = 1,
48 	LDNS_PACKET_STATUS = 2, /* there is no 3?? DNS is weird */
49 	LDNS_PACKET_NOTIFY = 4,
50 	LDNS_PACKET_UPDATE = 5
51 };
52 typedef enum ldns_enum_pkt_opcode ldns_pkt_opcode;
53 
54 /* rcodes for pkts */
55 enum ldns_enum_pkt_rcode {
56 	LDNS_RCODE_NOERROR = 0,
57 	LDNS_RCODE_FORMERR = 1,
58 	LDNS_RCODE_SERVFAIL = 2,
59 	LDNS_RCODE_NXDOMAIN = 3,
60 	LDNS_RCODE_NOTIMPL = 4,
61 	LDNS_RCODE_REFUSED = 5,
62 	LDNS_RCODE_YXDOMAIN = 6,
63 	LDNS_RCODE_YXRRSET = 7,
64 	LDNS_RCODE_NXRRSET = 8,
65 	LDNS_RCODE_NOTAUTH = 9,
66 	LDNS_RCODE_NOTZONE = 10
67 };
68 typedef enum ldns_enum_pkt_rcode ldns_pkt_rcode;
69 
70 /**
71  *  Header of a dns packet
72  *
73  * Contains the information about the packet itself, as specified in RFC1035
74 <pre>
75 4.1.1. Header section format
76 
77 The header contains the following fields:
78 
79                                     1  1  1  1  1  1
80       0  1  2  3  4  5  6  7  8  9  0  1  2  3  4  5
81     +--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+
82     |                      ID                       |
83     +--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+
84     |QR|   Opcode  |AA|TC|RD|RA|   Z    |   RCODE   |
85     +--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+
86     |                    QDCOUNT                    |
87     +--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+
88     |                    ANCOUNT                    |
89     +--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+
90     |                    NSCOUNT                    |
91     +--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+
92     |                    ARCOUNT                    |
93     +--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+
94 
95 where:
96 
97 ID              A 16 bit identifier assigned by the program that
98                 generates any kind of query.  This identifier is copied
99                 the corresponding reply and can be used by the requester
100                 to match up replies to outstanding queries.
101 
102 QR              A one bit field that specifies whether this message is a
103                 query (0), or a response (1).
104 
105 OPCODE          A four bit field that specifies kind of query in this
106                 message.  This value is set by the originator of a query
107                 and copied into the response.  The values are:
108 
109                 0               a standard query (QUERY)
110 
111                 1               an inverse query (IQUERY)
112 
113                 2               a server status request (STATUS)
114 
115                 3-15            reserved for future use
116 
117 AA              Authoritative Answer - this bit is valid in responses,
118                 and specifies that the responding name server is an
119                 authority for the domain name in question section.
120 
121                 Note that the contents of the answer section may have
122                 multiple owner names because of aliases.  The AA bit
123 
124                 corresponds to the name which matches the query name, or
125                 the first owner name in the answer section.
126 
127 TC              TrunCation - specifies that this message was truncated
128                 due to length greater than that permitted on the
129                 transmission channel.
130 
131 RD              Recursion Desired - this bit may be set in a query and
132                 is copied into the response.  If RD is set, it directs
133                 the name server to pursue the query recursively.
134                 Recursive query support is optional.
135 
136 RA              Recursion Available - this be is set or cleared in a
137                 response, and denotes whether recursive query support is
138                 available in the name server.
139 
140 Z               Reserved for future use.  Must be zero in all queries
141                 and responses.
142 
143 RCODE           Response code - this 4 bit field is set as part of
144                 responses.  The values have the following
145                 interpretation:
146 
147                 0               No error condition
148 
149                 1               Format error - The name server was
150                                 unable to interpret the query.
151 
152                 2               Server failure - The name server was
153                                 unable to process this query due to a
154                                 problem with the name server.
155 
156                 3               Name Error - Meaningful only for
157                                 responses from an authoritative name
158                                 server, this code signifies that the
159                                 domain name referenced in the query does
160                                 not exist.
161 
162                 4               Not Implemented - The name server does
163                                 not support the requested kind of query.
164 
165                 5               Refused - The name server refuses to
166                                 perform the specified operation for
167                                 policy reasons.  For example, a name
168                                 server may not wish to provide the
169                                 information to the particular requester,
170                                 or a name server may not wish to perform
171                                 a particular operation (e.g., zone
172 
173                                 transfer) for particular data.
174 
175                 6-15            Reserved for future use.
176 
177 QDCOUNT         an unsigned 16 bit integer specifying the number of
178                 entries in the question section.
179 
180 ANCOUNT         an unsigned 16 bit integer specifying the number of
181                 resource records in the answer section.
182 
183 NSCOUNT         an unsigned 16 bit integer specifying the number of name
184                 server resource records in the authority records
185                 section.
186 
187 ARCOUNT         an unsigned 16 bit integer specifying the number of
188                 resource records in the additional records section.
189 
190 </pre>
191  */
192 struct ldns_struct_hdr
193 {
194 	/**  Id of a packet */
195 	uint16_t _id;
196 	/**  Query bit (0=query, 1=answer) */
197 	bool _qr;
198 	/**  Authoritative answer */
199 	bool _aa;
200 	/**  Packet truncated */
201 	bool _tc;
202 	/**  Recursion desired */
203 	bool _rd;
204 	/**  Checking disabled */
205 	bool _cd;
206 	/**  Recursion available */
207 	bool _ra;
208 	/**  Authentic data */
209 	bool _ad;
210 	/**  Query type */
211 	ldns_pkt_opcode _opcode;	 /* XXX 8 bits? */
212 	/**  Response code */
213 	uint8_t _rcode;
214 	/**  question sec */
215 	uint16_t _qdcount;
216 	/**  answer sec */
217 	uint16_t _ancount;
218 	/**  auth sec */
219 	uint16_t _nscount;
220 	/**  add sec */
221 	uint16_t _arcount;
222 };
223 typedef struct ldns_struct_hdr ldns_hdr;
224 
225 /**
226  * DNS packet
227  *
228  * This structure contains a complete DNS packet (either a query or an answer)
229  *
230  * It is the complete representation of what you actually send to a
231  * nameserver, and what it sends back (assuming you are the client here).
232  */
233 struct ldns_struct_pkt
234 {
235 	/** Header section */
236 	ldns_hdr *_header;
237 	/* extra items needed in a packet */
238 	/** an rdf (A or AAAA) with the IP address of the server it is from */
239 	ldns_rdf *_answerfrom;
240         /** Timestamp of the time the packet was sent or created */
241 	struct timeval timestamp;
242 	/** The duration of the query this packet is an answer to */
243 	uint32_t _querytime;
244 	/** The size of the wire format of the packet in octets */
245 	size_t _size;
246 	/** Optional tsig rr */
247 	ldns_rr *_tsig_rr;
248 	/** EDNS0 available buffer size, see RFC2671 */
249 	uint16_t _edns_udp_size;
250 	/** EDNS0 Extended rcode */
251 	uint8_t _edns_extended_rcode;
252 	/** EDNS Version */
253 	uint8_t _edns_version;
254         /* OPT pseudo-RR presence flag */
255         uint8_t _edns_present;
256 	/** Reserved EDNS data bits */
257 	uint16_t _edns_z;
258 	/** Arbitrary EDNS rdata */
259 	ldns_rdf *_edns_data;
260 	/**  Question section */
261 	ldns_rr_list	*_question;
262 	/**  Answer section */
263 	ldns_rr_list	*_answer;
264 	/**  Authority section */
265 	ldns_rr_list	*_authority;
266 	/**  Additional section */
267 	ldns_rr_list	*_additional;
268 };
269 typedef struct ldns_struct_pkt ldns_pkt;
270 
271 /**
272  * The sections of a packet
273  */
274 enum ldns_enum_pkt_section {
275 	LDNS_SECTION_QUESTION = 0,
276 	LDNS_SECTION_ANSWER = 1,
277 	LDNS_SECTION_AUTHORITY = 2,
278 	LDNS_SECTION_ADDITIONAL = 3,
279 	/** bogus section, if not interested */
280 	LDNS_SECTION_ANY = 4,
281 	/** used to get all non-question rrs from a packet */
282 	LDNS_SECTION_ANY_NOQUESTION = 5
283 };
284 typedef enum ldns_enum_pkt_section ldns_pkt_section;
285 
286 /**
287  * The different types of packets
288  */
289 enum ldns_enum_pkt_type {
290 	LDNS_PACKET_QUESTION,
291 	LDNS_PACKET_REFERRAL,
292 	LDNS_PACKET_ANSWER,
293 	LDNS_PACKET_NXDOMAIN,
294 	LDNS_PACKET_NODATA,
295 	LDNS_PACKET_UNKNOWN
296 };
297 typedef enum ldns_enum_pkt_type ldns_pkt_type;
298 
299 /* prototypes */
300 
301 /* read */
302 
303 /**
304  * Read the packet id
305  * \param[in] p the packet
306  * \return the packet id
307  */
308 uint16_t ldns_pkt_id(const ldns_pkt *p);
309 /**
310  * Read the packet's qr bit
311  * \param[in] p the packet
312  * \return value of the bit
313  */
314 bool ldns_pkt_qr(const ldns_pkt *p);
315 /**
316  * Read the packet's aa bit
317  * \param[in] p the packet
318  * \return value of the bit
319  */
320 bool ldns_pkt_aa(const ldns_pkt *p);
321 /**
322  * Read the packet's tc bit
323  * \param[in] p the packet
324  * \return value of the bit
325  */
326 bool ldns_pkt_tc(const ldns_pkt *p);
327 /**
328  * Read the packet's rd bit
329  * \param[in] p the packet
330  * \return value of the bit
331  */
332 bool ldns_pkt_rd(const ldns_pkt *p);
333 /**
334  * Read the packet's cd bit
335  * \param[in] p the packet
336  * \return value of the bit
337  */
338 bool ldns_pkt_cd(const ldns_pkt *p);
339 /**
340  * Read the packet's ra bit
341  * \param[in] p the packet
342  * \return value of the bit
343  */
344 bool ldns_pkt_ra(const ldns_pkt *p);
345 /**
346  * Read the packet's ad bit
347  * \param[in] p the packet
348  * \return value of the bit
349  */
350 bool ldns_pkt_ad(const ldns_pkt *p);
351 /**
352  * Read the packet's code
353  * \param[in] p the packet
354  * \return the opcode
355  */
356 ldns_pkt_opcode ldns_pkt_get_opcode(const ldns_pkt *p);
357 /**
358  * Return the packet's response code
359  * \param[in] p the packet
360  * \return the response code
361  */
362 ldns_pkt_rcode ldns_pkt_get_rcode(const ldns_pkt *p);
363 /**
364  * Return the packet's qd count
365  * \param[in] p the packet
366  * \return the qd count
367  */
368 uint16_t ldns_pkt_qdcount(const ldns_pkt *p);
369 /**
370  * Return the packet's an count
371  * \param[in] p the packet
372  * \return the an count
373  */
374 uint16_t ldns_pkt_ancount(const ldns_pkt *p);
375 /**
376  * Return the packet's ns count
377  * \param[in] p the packet
378  * \return the ns count
379  */
380 uint16_t ldns_pkt_nscount(const ldns_pkt *p);
381 /**
382  * Return the packet's ar count
383  * \param[in] p the packet
384  * \return the ar count
385  */
386 uint16_t ldns_pkt_arcount(const ldns_pkt *p);
387 
388 /**
389  * Return the packet's answerfrom
390  * \param[in] p packet
391  * \return the name of the server
392  */
393 ldns_rdf *ldns_pkt_answerfrom(const ldns_pkt *p);
394 
395 /**
396  * Return the packet's timestamp
397  * \param[in] p the packet
398  * \return the timestamp
399  */
400 struct timeval ldns_pkt_timestamp(const ldns_pkt *p);
401 /**
402  * Return the packet's querytime
403  * \param[in] p the packet
404  * \return the querytime
405  */
406 uint32_t ldns_pkt_querytime(const ldns_pkt *p);
407 
408 /**
409  * Return the packet's size in bytes
410  * \param[in] p the packet
411  * \return the size
412  */
413 size_t ldns_pkt_size(const ldns_pkt *p);
414 
415 /**
416  * Return the number of RRs in the given section.
417  * Returns the sum of all RRs when LDNS_SECTION_ANY is given.
418  * Returns the sum of all non-question RRs when LDNS_SECTION_ANY_NOQUESTION
419  * is given.
420  * \param[in] p the packet
421  * \param[in] s the section
422  * \return the number of RRs in the given section
423  */
424 uint16_t ldns_pkt_section_count(const ldns_pkt *p, ldns_pkt_section s);
425 
426 /**
427  * Return the packet's tsig pseudo rr's
428  * \param[in] p the packet
429  * \return the tsig rr
430  */
431 ldns_rr *ldns_pkt_tsig(const ldns_pkt *p);
432 
433 /**
434  * Return the packet's question section
435  * \param[in] p the packet
436  * \return the section
437  */
438 ldns_rr_list *ldns_pkt_question(const ldns_pkt *p);
439 /**
440  * Return the packet's answer section
441  * \param[in] p the packet
442  * \return the section
443  */
444 ldns_rr_list *ldns_pkt_answer(const ldns_pkt *p);
445 /**
446  * Return the packet's authority section
447  * \param[in] p the packet
448  * \return the section
449  */
450 ldns_rr_list *ldns_pkt_authority(const ldns_pkt *p);
451 /**
452  * Return the packet's additional section
453  * \param[in] p the packet
454  * \return the section
455  */
456 ldns_rr_list *ldns_pkt_additional(const ldns_pkt *p);
457 /**
458  * Return the packet's question, answer, authority and additional sections
459  * concatenated, in a new rr_list clone.
460  * \param[in] p the packet
461  * \return the rrs
462  */
463 ldns_rr_list *ldns_pkt_all(const ldns_pkt *p);
464 /**
465  * Return the packet's answer, authority and additional sections concatenated,
466  * in a new rr_list clone.  Like ldns_pkt_all but without the questions.
467  * \param[in] p the packet
468  * \return the rrs except the question rrs
469  */
470 ldns_rr_list *ldns_pkt_all_noquestion(const ldns_pkt *p);
471 
472 /**
473  * return all the rr_list's in the packet. Clone the lists, instead
474  * of returning pointers.
475  * \param[in] p the packet to look in
476  * \param[in] s what section(s) to return
477  * \return ldns_rr_list with the rr's or NULL if none were found
478  */
479 ldns_rr_list *ldns_pkt_get_section_clone(const ldns_pkt *p, ldns_pkt_section s);
480 
481 /**
482  * return all the rr with a specific name from a packet. Optionally
483  * specify from which section in the packet
484  * \param[in] p the packet
485  * \param[in] r the name
486  * \param[in] s the packet's section
487  * \return a list with the rr's or NULL if none were found
488  */
489 ldns_rr_list *ldns_pkt_rr_list_by_name(const ldns_pkt *p, const ldns_rdf *r, ldns_pkt_section s);
490 /**
491  * return all the rr with a specific type from a packet. Optionally
492  * specify from which section in the packet
493  * \param[in] p the packet
494  * \param[in] t the type
495  * \param[in] s the packet's section
496  * \return a list with the rr's or NULL if none were found
497  */
498 ldns_rr_list *ldns_pkt_rr_list_by_type(const ldns_pkt *p, ldns_rr_type t, ldns_pkt_section s);
499 /**
500  * return all the rr with a specific type and type from a packet. Optionally
501  * specify from which section in the packet
502  * \param[in] packet the packet
503  * \param[in] ownername the name
504  * \param[in] type the type
505  * \param[in] sec the packet's section
506  * \return a list with the rr's or NULL if none were found
507  */
508 ldns_rr_list *ldns_pkt_rr_list_by_name_and_type(const ldns_pkt *packet, const ldns_rdf *ownername, ldns_rr_type type, ldns_pkt_section sec);
509 
510 
511 /**
512  * check to see if an rr exist in the packet
513  * \param[in] pkt the packet to examine
514  * \param[in] sec in which section to look
515  * \param[in] rr the rr to look for
516  */
517 bool ldns_pkt_rr(const ldns_pkt *pkt, ldns_pkt_section sec, const ldns_rr *rr);
518 
519 
520 /**
521  * sets the flags in a packet.
522  * \param[in] pkt the packet to operate on
523  * \param[in] flags ORed values: LDNS_QR| LDNS_AR for instance
524  * \return true on success otherwise false
525  */
526 bool ldns_pkt_set_flags(ldns_pkt *pkt, uint16_t flags);
527 
528 /**
529  * Set the packet's id
530  * \param[in] p the packet
531  * \param[in] id the id to set
532  */
533 void ldns_pkt_set_id(ldns_pkt *p, uint16_t id);
534 /**
535  * Set the packet's id to a random value
536  * \param[in] p the packet
537  */
538 void ldns_pkt_set_random_id(ldns_pkt *p);
539 /**
540  * Set the packet's qr bit
541  * \param[in] p the packet
542  * \param[in] b the value to set (boolean)
543  */
544 void ldns_pkt_set_qr(ldns_pkt *p, bool b);
545 /**
546  * Set the packet's aa bit
547  * \param[in] p the packet
548  * \param[in] b the value to set (boolean)
549  */
550 void ldns_pkt_set_aa(ldns_pkt *p, bool b);
551 /**
552  * Set the packet's tc bit
553  * \param[in] p the packet
554  * \param[in] b the value to set (boolean)
555  */
556 void ldns_pkt_set_tc(ldns_pkt *p, bool b);
557 /**
558  * Set the packet's rd bit
559  * \param[in] p the packet
560  * \param[in] b the value to set (boolean)
561  */
562 void ldns_pkt_set_rd(ldns_pkt *p, bool b);
563 /**
564  * Set the packet's cd bit
565  * \param[in] p the packet
566  * \param[in] b the value to set (boolean)
567  */
568 void ldns_pkt_set_cd(ldns_pkt *p, bool b);
569 /**
570  * Set the packet's ra bit
571  * \param[in] p the packet
572  * \param[in] b the value to set (boolean)
573  */
574 void ldns_pkt_set_ra(ldns_pkt *p, bool b);
575 /**
576  * Set the packet's ad bit
577  * \param[in] p the packet
578  * \param[in] b the value to set (boolean)
579  */
580 void ldns_pkt_set_ad(ldns_pkt *p, bool b);
581 
582 /**
583  * Set the packet's opcode
584  * \param[in] p the packet
585  * \param[in] c the opcode
586  */
587 void ldns_pkt_set_opcode(ldns_pkt *p, ldns_pkt_opcode c);
588 /**
589  * Set the packet's response code
590  * \param[in] p the packet
591  * \param[in] c the rcode
592  */
593 void ldns_pkt_set_rcode(ldns_pkt *p, uint8_t c);
594 /**
595  * Set the packet's qd count
596  * \param[in] p the packet
597  * \param[in] c the count
598  */
599 void ldns_pkt_set_qdcount(ldns_pkt *p, uint16_t c);
600 /**
601  * Set the packet's an count
602  * \param[in] p the packet
603  * \param[in] c the count
604  */
605 void ldns_pkt_set_ancount(ldns_pkt *p, uint16_t c);
606 /**
607  * Set the packet's ns count
608  * \param[in] p the packet
609  * \param[in] c the count
610  */
611 void ldns_pkt_set_nscount(ldns_pkt *p, uint16_t c);
612 /**
613  * Set the packet's arcount
614  * \param[in] p the packet
615  * \param[in] c the count
616  */
617 void ldns_pkt_set_arcount(ldns_pkt *p, uint16_t c);
618 /**
619  * Set the packet's answering server
620  * \param[in] p the packet
621  * \param[in] r the address
622  */
623 void ldns_pkt_set_answerfrom(ldns_pkt *p, ldns_rdf *r);
624 /**
625  * Set the packet's query time
626  * \param[in] p the packet
627  * \param[in] t the querytime in msec
628  */
629 void ldns_pkt_set_querytime(ldns_pkt *p, uint32_t t);
630 /**
631  * Set the packet's size
632  * \param[in] p the packet
633  * \param[in] s the size
634  */
635 void ldns_pkt_set_size(ldns_pkt *p, size_t s);
636 
637 /**
638  * Set the packet's timestamp
639  * \param[in] p the packet
640  * \param[in] timeval the timestamp
641  */
642 void ldns_pkt_set_timestamp(ldns_pkt *p, struct timeval timeval);
643 /**
644  * Set a packet's section count to x
645  * \param[in] p the packet
646  * \param[in] s the section
647  * \param[in] x the section count
648  */
649 void ldns_pkt_set_section_count(ldns_pkt *p, ldns_pkt_section s, uint16_t x);
650 /**
651  * Set the packet's tsig rr
652  * \param[in] p the packet
653  * \param[in] t the tsig rr
654  */
655 void ldns_pkt_set_tsig(ldns_pkt *p, ldns_rr *t);
656 
657 /**
658  * looks inside the packet to determine
659  * what kind of packet it is, AUTH, NXDOMAIN, REFERRAL, etc.
660  * \param[in] p the packet to examine
661  * \return the type of packet
662  */
663 ldns_pkt_type ldns_pkt_reply_type(const ldns_pkt *p);
664 
665 /**
666  * return the packet's edns udp size
667  * \param[in] packet the packet
668  * \return the size
669  */
670 uint16_t ldns_pkt_edns_udp_size(const ldns_pkt *packet);
671 /**
672  * return the packet's edns extended rcode
673  * \param[in] packet the packet
674  * \return the rcode
675  */
676 uint8_t ldns_pkt_edns_extended_rcode(const ldns_pkt *packet);
677 /**
678  * return the packet's edns version
679  * \param[in] packet the packet
680  * \return the version
681  */
682 uint8_t ldns_pkt_edns_version(const ldns_pkt *packet);
683 /**
684  * return the packet's edns z value
685  * \param[in] packet the packet
686  * \return the z value
687  */
688 uint16_t ldns_pkt_edns_z(const ldns_pkt *packet);
689 /**
690  * return the packet's edns data
691  * \param[in] packet the packet
692  * \return the data
693  */
694 ldns_rdf *ldns_pkt_edns_data(const ldns_pkt *packet);
695 
696 /**
697  * return the packet's edns do bit
698  * \param[in] packet the packet
699  * \return the bit's value
700  */
701 bool ldns_pkt_edns_do(const ldns_pkt *packet);
702 /**
703  * Set the packet's edns do bit
704  * \param[in] packet the packet
705  * \param[in] value the bit's new value
706  */
707 void ldns_pkt_set_edns_do(ldns_pkt *packet, bool value);
708 
709 /**
710  * return the packet's EDNS header bits that are unassigned.
711  */
712 uint16_t ldns_pkt_edns_unassigned(const ldns_pkt *packet);
713 
714 /**
715  * Set the packet's EDNS header bits that are unassigned.
716  * \param[in] packet the packet
717  * \param[in] value the value
718  */
719 void ldns_pkt_set_edns_unassigned(ldns_pkt *packet, uint16_t value);
720 
721 /**
722  * returns true if this packet needs and EDNS rr to be sent.
723  * At the moment the only reason is an expected packet
724  * size larger than 512 bytes, but for instance dnssec would
725  * be a good reason too.
726  *
727  * \param[in] packet the packet to check
728  * \return true if packet needs edns rr
729  */
730 bool ldns_pkt_edns(const ldns_pkt *packet);
731 
732 /**
733  * Set the packet's edns udp size
734  * \param[in] packet the packet
735  * \param[in] s the size
736  */
737 void ldns_pkt_set_edns_udp_size(ldns_pkt *packet, uint16_t s);
738 /**
739  * Set the packet's edns extended rcode
740  * \param[in] packet the packet
741  * \param[in] c the code
742  */
743 void ldns_pkt_set_edns_extended_rcode(ldns_pkt *packet, uint8_t c);
744 /**
745  * Set the packet's edns version
746  * \param[in] packet the packet
747  * \param[in] v the version
748  */
749 void ldns_pkt_set_edns_version(ldns_pkt *packet, uint8_t v);
750 /**
751  * Set the packet's edns z value
752  * \param[in] packet the packet
753  * \param[in] z the value
754  */
755 void ldns_pkt_set_edns_z(ldns_pkt *packet, uint16_t z);
756 /**
757  * Set the packet's edns data
758  * \param[in] packet the packet
759  * \param[in] data the data
760  */
761 void ldns_pkt_set_edns_data(ldns_pkt *packet, ldns_rdf *data);
762 
763 /**
764  * allocates and initializes a ldns_pkt structure.
765  * \return pointer to the new packet
766  */
767 ldns_pkt *ldns_pkt_new(void);
768 
769 /**
770  * frees the packet structure and all data that it contains.
771  * \param[in] packet The packet structure to free
772  * \return void
773  */
774 void ldns_pkt_free(ldns_pkt *packet);
775 
776 /**
777  * creates a query packet for the given name, type, class.
778  * \param[out] p the packet to be returned
779  * \param[in] rr_name the name to query for (as string)
780  * \param[in] rr_type the type to query for
781  * \param[in] rr_class the class to query for
782  * \param[in] flags packet flags
783  * \return LDNS_STATUS_OK or a ldns_status mesg with the error
784  */
785 ldns_status ldns_pkt_query_new_frm_str(ldns_pkt **p, const char *rr_name, ldns_rr_type rr_type, ldns_rr_class rr_class , uint16_t flags);
786 
787 /**
788  * creates an IXFR request packet for the given name, class.
789  * adds the SOA record to the authority section.
790  * \param[out] p the packet to be returned
791  * \param[in] rr_name the name to query for (as string)
792  * \param[in] rr_class the class to query for
793  * \param[in] flags packet flags
794  * \param[in] soa soa record to be added to the authority section (not copied).
795  * \return LDNS_STATUS_OK or a ldns_status mesg with the error
796  */
797 ldns_status ldns_pkt_ixfr_request_new_frm_str(ldns_pkt **p, const char *rr_name, ldns_rr_class rr_class, uint16_t flags, ldns_rr* soa);
798 
799 /**
800  * creates a packet with a query in it for the given name, type and class.
801  * \param[in] rr_name the name to query for (not copied).
802  *            The returned packet will take ownership of rr_name, so the caller should not free it.
803  * \param[in] rr_type the type to query for
804  * \param[in] rr_class the class to query for
805  * \param[in] flags packet flags
806  * \return ldns_pkt* a pointer to the new pkt
807  */
808 ldns_pkt *ldns_pkt_query_new(ldns_rdf *rr_name, ldns_rr_type rr_type, ldns_rr_class rr_class, uint16_t flags);
809 
810 /**
811  * creates an IXFR request packet for the given name, type and class.
812  * adds the SOA record to the authority section.
813  * \param[in] rr_name the name to query for (not copied).
814  *            The returned packet will take ownership of rr_name, so the caller should not free it.
815  * \param[in] rr_class the class to query for
816  * \param[in] flags packet flags
817  * \param[in] soa soa record to be added to the authority section (not copied).
818  * \return ldns_pkt* a pointer to the new pkt
819  */
820 ldns_pkt *ldns_pkt_ixfr_request_new(ldns_rdf *rr_name, ldns_rr_class rr_class, uint16_t flags, ldns_rr* soa);
821 
822 /**
823  * clones the given packet, creating a fully allocated copy
824  *
825  * \param[in] pkt the packet to clone
826  * \return ldns_pkt* pointer to the new packet
827  */
828 ldns_pkt *ldns_pkt_clone(const ldns_pkt *pkt);
829 
830 /**
831  * directly set the additional section
832  * \param[in] p packet to operate on
833  * \param[in] rr rrlist to set
834  */
835 void ldns_pkt_set_additional(ldns_pkt *p, ldns_rr_list *rr);
836 
837 /**
838  * directly set the answer section
839  * \param[in] p packet to operate on
840  * \param[in] rr rrlist to set
841  */
842 void ldns_pkt_set_answer(ldns_pkt *p, ldns_rr_list *rr);
843 
844 /**
845  * directly set the question section
846  * \param[in] p packet to operate on
847  * \param[in] rr rrlist to set
848  */
849 void ldns_pkt_set_question(ldns_pkt *p, ldns_rr_list *rr);
850 
851 /**
852  * directly set the authority section
853  * \param[in] p packet to operate on
854  * \param[in] rr rrlist to set
855  */
856 void ldns_pkt_set_authority(ldns_pkt *p, ldns_rr_list *rr);
857 
858 /**
859  * push an rr on a packet
860  * \param[in] packet packet to operate on
861  * \param[in] section where to put it
862  * \param[in] rr rr to push
863  * \return a boolean which is true when the rr was added
864  */
865 bool ldns_pkt_push_rr(ldns_pkt *packet, ldns_pkt_section section, ldns_rr *rr);
866 
867 /**
868  * push an rr on a packet, provided the RR is not there.
869  * \param[in] pkt packet to operate on
870  * \param[in] sec where to put it
871  * \param[in] rr rr to push
872  * \return a boolean which is true when the rr was added
873  */
874 bool ldns_pkt_safe_push_rr(ldns_pkt *pkt, ldns_pkt_section sec, ldns_rr *rr);
875 
876 /**
877  * push a rr_list on a packet
878  * \param[in] packet packet to operate on
879  * \param[in] section where to put it
880  * \param[in] list the rr_list to push
881  * \return a boolean which is true when the rr was added
882  */
883 bool ldns_pkt_push_rr_list(ldns_pkt *packet, ldns_pkt_section section, ldns_rr_list *list);
884 
885 /**
886  * push an rr_list to a packet, provided the RRs are not already there.
887  * \param[in] pkt packet to operate on
888  * \param[in] sec where to put it
889  * \param[in] list the rr_list to push
890  * \return a boolean which is true when the rr was added
891  */
892 bool ldns_pkt_safe_push_rr_list(ldns_pkt *pkt, ldns_pkt_section sec, ldns_rr_list *list);
893 
894 /**
895  * check if a packet is empty
896  * \param[in] p packet
897  * \return true: empty, false: not empty
898  */
899 bool ldns_pkt_empty(ldns_pkt *p);
900 
901 #ifdef __cplusplus
902 }
903 #endif
904 
905 #endif  /* LDNS_PACKET_H */
906