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