4 * DNS packet definitions
6 * a Net::DNS like library for C
8 * (c) NLnet Labs, 2005-2006
10 * See the file LICENSE for the license
16 * Contains the definition of ldns_pkt and its parts, as well
17 * as functions to manipulate those.
24 #define LDNS_MAX_PACKETLEN 65535
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 */
35 #include <ldns/error.h>
36 #include <ldns/common.h>
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
48 typedef enum ldns_enum_pkt_opcode ldns_pkt_opcode;
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
64 typedef enum ldns_enum_pkt_rcode ldns_pkt_rcode;
67 * Header of a dns packet
69 * Contains the information about the packet itself, as specified in RFC1035
71 4.1.1. Header section format
73 The header contains the following fields:
76 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5
77 +--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+
79 +--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+
80 |QR| Opcode |AA|TC|RD|RA| Z | RCODE |
81 +--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+
83 +--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+
85 +--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+
87 +--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+
89 +--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+
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.
98 QR A one bit field that specifies whether this message is a
99 query (0), or a response (1).
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:
105 0 a standard query (QUERY)
107 1 an inverse query (IQUERY)
109 2 a server status request (STATUS)
111 3-15 reserved for future use
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.
117 Note that the contents of the answer section may have
118 multiple owner names because of aliases. The AA bit
120 corresponds to the name which matches the query name, or
121 the first owner name in the answer section.
123 TC TrunCation - specifies that this message was truncated
124 due to length greater than that permitted on the
125 transmission channel.
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.
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.
136 Z Reserved for future use. Must be zero in all queries
139 RCODE Response code - this 4 bit field is set as part of
140 responses. The values have the following
145 1 Format error - The name server was
146 unable to interpret the query.
148 2 Server failure - The name server was
149 unable to process this query due to a
150 problem with the name server.
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
158 4 Not Implemented - The name server does
159 not support the requested kind of query.
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
169 transfer) for particular data.
171 6-15 Reserved for future use.
173 QDCOUNT an unsigned 16 bit integer specifying the number of
174 entries in the question section.
176 ANCOUNT an unsigned 16 bit integer specifying the number of
177 resource records in the answer section.
179 NSCOUNT an unsigned 16 bit integer specifying the number of name
180 server resource records in the authority records
183 ARCOUNT an unsigned 16 bit integer specifying the number of
184 resource records in the additional records section.
188 struct ldns_struct_hdr
190 /** Id of a packet */
192 /** Query bit (0=query, 1=answer) */
194 /** Authoritative answer */
196 /** Packet truncated */
198 /** Recursion desired */
200 /** Checking disabled */
202 /** Recursion available */
204 /** Authentic data */
207 ldns_pkt_opcode _opcode; /* XXX 8 bits? */
219 typedef struct ldns_struct_hdr ldns_hdr;
224 * This structure contains a complete DNS packet (either a query or an answer)
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).
229 struct ldns_struct_pkt
231 /** Header section */
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 */
240 /** The size of the wire format of the packet in octets */
242 /** Optional 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;
249 uint8_t _edns_version;
250 /** Reserved EDNS data bits */
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;
263 typedef struct ldns_struct_pkt ldns_pkt;
266 * The sections of a packet
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
278 typedef enum ldns_enum_pkt_section ldns_pkt_section;
281 * The different types of packets
283 enum ldns_enum_pkt_type {
284 LDNS_PACKET_QUESTION,
285 LDNS_PACKET_REFERRAL,
287 LDNS_PACKET_NXDOMAIN,
291 typedef enum ldns_enum_pkt_type ldns_pkt_type;
299 * \param[in] p the packet
300 * \return the packet id
302 uint16_t ldns_pkt_id(const ldns_pkt *p);
304 * Read the packet's qr bit
305 * \param[in] p the packet
306 * \return value of the bit
308 bool ldns_pkt_qr(const ldns_pkt *p);
310 * Read the packet's aa bit
311 * \param[in] p the packet
312 * \return value of the bit
314 bool ldns_pkt_aa(const ldns_pkt *p);
316 * Read the packet's tc bit
317 * \param[in] p the packet
318 * \return value of the bit
320 bool ldns_pkt_tc(const ldns_pkt *p);
322 * Read the packet's rd bit
323 * \param[in] p the packet
324 * \return value of the bit
326 bool ldns_pkt_rd(const ldns_pkt *p);
328 * Read the packet's cd bit
329 * \param[in] p the packet
330 * \return value of the bit
332 bool ldns_pkt_cd(const ldns_pkt *p);
334 * Read the packet's ra bit
335 * \param[in] p the packet
336 * \return value of the bit
338 bool ldns_pkt_ra(const ldns_pkt *p);
340 * Read the packet's ad bit
341 * \param[in] p the packet
342 * \return value of the bit
344 bool ldns_pkt_ad(const ldns_pkt *p);
346 * Read the packet's code
347 * \param[in] p the packet
350 ldns_pkt_opcode ldns_pkt_get_opcode(const ldns_pkt *p);
352 * Return the packet's respons code
353 * \param[in] p the packet
354 * \return the respons code
356 ldns_pkt_rcode ldns_pkt_get_rcode(const ldns_pkt *p);
358 * Return the packet's qd count
359 * \param[in] p the packet
360 * \return the qd count
362 uint16_t ldns_pkt_qdcount(const ldns_pkt *p);
364 * Return the packet's an count
365 * \param[in] p the packet
366 * \return the an count
368 uint16_t ldns_pkt_ancount(const ldns_pkt *p);
370 * Return the packet's ns count
371 * \param[in] p the packet
372 * \return the ns count
374 uint16_t ldns_pkt_nscount(const ldns_pkt *p);
376 * Return the packet's ar count
377 * \param[in] p the packet
378 * \return the ar count
380 uint16_t ldns_pkt_arcount(const ldns_pkt *p);
383 * Return the packet's answerfrom
384 * \param[in] p packet
385 * \return the name of the server
387 ldns_rdf *ldns_pkt_answerfrom(const ldns_pkt *p);
390 * Return the packet's timestamp
391 * \param[in] p the packet
392 * \return the timestamp
394 struct timeval ldns_pkt_timestamp(const ldns_pkt *p);
396 * Return the packet's querytime
397 * \param[in] p the packet
398 * \return the querytime
400 uint32_t ldns_pkt_querytime(const ldns_pkt *p);
403 * Return the packet's size in bytes
404 * \param[in] p the packet
407 size_t ldns_pkt_size(const ldns_pkt *p);
410 * Return the packet's tsig pseudo rr's
411 * \param[in] p the packet
412 * \return the tsig rr
414 ldns_rr *ldns_pkt_tsig(const ldns_pkt *p);
417 * Return the packet's question section
418 * \param[in] p the packet
419 * \return the section
421 ldns_rr_list *ldns_pkt_question(const ldns_pkt *p);
423 * Return the packet's answer section
424 * \param[in] p the packet
425 * \return the section
427 ldns_rr_list *ldns_pkt_answer(const ldns_pkt *p);
429 * Return the packet's authority section
430 * \param[in] p the packet
431 * \return the section
433 ldns_rr_list *ldns_pkt_authority(const ldns_pkt *p);
435 * Return the packet's additional section
436 * \param[in] p the packet
437 * \return the section
439 ldns_rr_list *ldns_pkt_additional(const ldns_pkt *p);
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
446 ldns_rr_list *ldns_pkt_all(const ldns_pkt *p);
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
453 ldns_rr_list *ldns_pkt_all_noquestion(const ldns_pkt *p);
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
462 ldns_rr_list *ldns_pkt_get_section_clone(const ldns_pkt *p, ldns_pkt_section s);
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
472 ldns_rr_list *ldns_pkt_rr_list_by_name(ldns_pkt *p, ldns_rdf *r, ldns_pkt_section s);
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
481 ldns_rr_list *ldns_pkt_rr_list_by_type(const ldns_pkt *p, ldns_rr_type t, ldns_pkt_section s);
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
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);
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
500 bool ldns_pkt_rr(ldns_pkt *pkt, ldns_pkt_section sec, ldns_rr *rr);
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
509 bool ldns_pkt_set_flags(ldns_pkt *pkt, uint16_t flags);
512 * Set the packet's id
513 * \param[in] p the packet
514 * \param[in] id the id to set
516 void ldns_pkt_set_id(ldns_pkt *p, uint16_t id);
518 * Set the packet's id to a random value
519 * \param[in] p the packet
521 void ldns_pkt_set_random_id(ldns_pkt *p);
523 * Set the packet's qr bit
524 * \param[in] p the packet
525 * \param[in] b the value to set (boolean)
527 void ldns_pkt_set_qr(ldns_pkt *p, bool b);
529 * Set the packet's aa bit
530 * \param[in] p the packet
531 * \param[in] b the value to set (boolean)
533 void ldns_pkt_set_aa(ldns_pkt *p, bool b);
535 * Set the packet's tc bit
536 * \param[in] p the packet
537 * \param[in] b the value to set (boolean)
539 void ldns_pkt_set_tc(ldns_pkt *p, bool b);
541 * Set the packet's rd bit
542 * \param[in] p the packet
543 * \param[in] b the value to set (boolean)
545 void ldns_pkt_set_rd(ldns_pkt *p, bool b);
547 * Set the packet's cd bit
548 * \param[in] p the packet
549 * \param[in] b the value to set (boolean)
551 void ldns_pkt_set_cd(ldns_pkt *p, bool b);
553 * Set the packet's ra bit
554 * \param[in] p the packet
555 * \param[in] b the value to set (boolean)
557 void ldns_pkt_set_ra(ldns_pkt *p, bool b);
559 * Set the packet's ad bit
560 * \param[in] p the packet
561 * \param[in] b the value to set (boolean)
563 void ldns_pkt_set_ad(ldns_pkt *p, bool b);
566 * Set the packet's opcode
567 * \param[in] p the packet
568 * \param[in] c the opcode
570 void ldns_pkt_set_opcode(ldns_pkt *p, ldns_pkt_opcode c);
572 * Set the packet's respons code
573 * \param[in] p the packet
574 * \param[in] c the rcode
576 void ldns_pkt_set_rcode(ldns_pkt *p, uint8_t c);
578 * Set the packet's qd count
579 * \param[in] p the packet
580 * \param[in] c the count
582 void ldns_pkt_set_qdcount(ldns_pkt *p, uint16_t c);
584 * Set the packet's an count
585 * \param[in] p the packet
586 * \param[in] c the count
588 void ldns_pkt_set_ancount(ldns_pkt *p, uint16_t c);
590 * Set the packet's ns count
591 * \param[in] p the packet
592 * \param[in] c the count
594 void ldns_pkt_set_nscount(ldns_pkt *p, uint16_t c);
596 * Set the packet's arcount
597 * \param[in] p the packet
598 * \param[in] c the count
600 void ldns_pkt_set_arcount(ldns_pkt *p, uint16_t c);
602 * Set the packet's answering server
603 * \param[in] p the packet
604 * \param[in] r the address
606 void ldns_pkt_set_answerfrom(ldns_pkt *p, ldns_rdf *r);
608 * Set the packet's query time
609 * \param[in] p the packet
610 * \param[in] t the querytime in msec
612 void ldns_pkt_set_querytime(ldns_pkt *p, uint32_t t);
614 * Set the packet's size
615 * \param[in] p the packet
616 * \param[in] s the size
618 void ldns_pkt_set_size(ldns_pkt *p, size_t s);
621 * Set the packet's timestamp
622 * \param[in] p the packet
623 * \param[in] timeval the timestamp
625 void ldns_pkt_set_timestamp(ldns_pkt *p, struct timeval);
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
632 void ldns_pkt_set_section_count(ldns_pkt *p, ldns_pkt_section s, uint16_t x);
634 * Set the packet's tsig rr
635 * \param[in] p the packet
636 * \param[in] t the tsig rr
638 void ldns_pkt_set_tsig(ldns_pkt *p, ldns_rr *t);
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
646 ldns_pkt_type ldns_pkt_reply_type(ldns_pkt *p);
649 * return the packet's edns udp size
650 * \param[in] packet the packet
653 uint16_t ldns_pkt_edns_udp_size(const ldns_pkt *packet);
655 * return the packet's edns extended rcode
656 * \param[in] packet the packet
659 uint8_t ldns_pkt_edns_extended_rcode(const ldns_pkt *packet);
661 * return the packet's edns version
662 * \param[in] packet the packet
663 * \return the version
665 uint8_t ldns_pkt_edns_version(const ldns_pkt *packet);
667 * return the packet's edns z value
668 * \param[in] packet the packet
669 * \return the z value
671 uint16_t ldns_pkt_edns_z(const ldns_pkt *packet);
673 * return the packet's edns data
674 * \param[in] packet the packet
677 ldns_rdf *ldns_pkt_edns_data(const ldns_pkt *packet);
680 * return the packet's edns do bit
681 * \param[in] packet the packet
682 * \return the bit's value
684 bool ldns_pkt_edns_do(const ldns_pkt *packet);
686 * Set the packet's edns do bit
687 * \param[in] packet the packet
688 * \param[in] value the bit's new value
690 void ldns_pkt_set_edns_do(ldns_pkt *packet, bool value);
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.
698 * \param[in] packet the packet to check
699 * \return true if packet needs edns rr
701 bool ldns_pkt_edns(const ldns_pkt *packet);
704 * Set the packet's edns udp size
705 * \param[in] packet the packet
706 * \param[in] s the size
708 void ldns_pkt_set_edns_udp_size(ldns_pkt *packet, uint16_t s);
710 * Set the packet's edns extended rcode
711 * \param[in] packet the packet
712 * \param[in] c the code
714 void ldns_pkt_set_edns_extended_rcode(ldns_pkt *packet, uint8_t c);
716 * Set the packet's edns version
717 * \param[in] packet the packet
718 * \param[in] v the version
720 void ldns_pkt_set_edns_version(ldns_pkt *packet, uint8_t v);
722 * Set the packet's edns z value
723 * \param[in] packet the packet
724 * \param[in] z the value
726 void ldns_pkt_set_edns_z(ldns_pkt *packet, uint16_t z);
728 * Set the packet's edns data
729 * \param[in] packet the packet
730 * \param[in] data the data
732 void ldns_pkt_set_edns_data(ldns_pkt *packet, ldns_rdf *data);
735 * allocates and initializes a ldns_pkt structure.
736 * \return pointer to the new packet
738 ldns_pkt *ldns_pkt_new();
741 * frees the packet structure and all data that it contains.
742 * \param[in] packet The packet structure to free
745 void ldns_pkt_free(ldns_pkt *packet);
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
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);
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
766 ldns_pkt *ldns_pkt_query_new(ldns_rdf *rr_name, ldns_rr_type rr_type, ldns_rr_class rr_class, uint16_t flags);
769 * clones the given packet, creating a fully allocated copy
771 * \param[in] pkt the packet to clone
772 * \return ldns_pkt* pointer to the new packet
774 ldns_pkt *ldns_pkt_clone(ldns_pkt *pkt);
777 * directly set the additional section
778 * \param[in] p packet to operate on
779 * \param[in] rr rrlist to set
781 void ldns_pkt_set_additional(ldns_pkt *p, ldns_rr_list *rr);
784 * directly set the answer section
785 * \param[in] p packet to operate on
786 * \param[in] rr rrlist to set
788 void ldns_pkt_set_answer(ldns_pkt *p, ldns_rr_list *rr);
791 * directly set the question section
792 * \param[in] p packet to operate on
793 * \param[in] rr rrlist to set
795 void ldns_pkt_set_question(ldns_pkt *p, ldns_rr_list *rr);
798 * directly set the auhority section
799 * \param[in] p packet to operate on
800 * \param[in] rr rrlist to set
802 void ldns_pkt_set_authority(ldns_pkt *p, ldns_rr_list *rr);
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
811 bool ldns_pkt_push_rr(ldns_pkt *packet, ldns_pkt_section section, ldns_rr *rr);
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
820 bool ldns_pkt_safe_push_rr(ldns_pkt *pkt, ldns_pkt_section sec, ldns_rr *rr);
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
829 bool ldns_pkt_push_rr_list(ldns_pkt *packet, ldns_pkt_section section, ldns_rr_list *list);
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
838 bool ldns_pkt_safe_push_rr_list(ldns_pkt *pkt, ldns_pkt_section sec, ldns_rr_list *list);
841 * check if a packet is empty
842 * \param[in] p packet
843 * \return true: empty, false: empty
845 bool ldns_pkt_empty(ldns_pkt *p);
847 #endif /* LDNS_PACKET_H */