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