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>
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
52 typedef enum ldns_enum_pkt_opcode ldns_pkt_opcode;
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
68 typedef enum ldns_enum_pkt_rcode ldns_pkt_rcode;
71 * Header of a dns packet
73 * Contains the information about the packet itself, as specified in RFC1035
75 4.1.1. Header section format
77 The header contains the following fields:
80 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5
81 +--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+
83 +--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+
84 |QR| Opcode |AA|TC|RD|RA| Z | RCODE |
85 +--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+
87 +--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+
89 +--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+
91 +--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+
93 +--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+
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.
102 QR A one bit field that specifies whether this message is a
103 query (0), or a response (1).
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:
109 0 a standard query (QUERY)
111 1 an inverse query (IQUERY)
113 2 a server status request (STATUS)
115 3-15 reserved for future use
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.
121 Note that the contents of the answer section may have
122 multiple owner names because of aliases. The AA bit
124 corresponds to the name which matches the query name, or
125 the first owner name in the answer section.
127 TC TrunCation - specifies that this message was truncated
128 due to length greater than that permitted on the
129 transmission channel.
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.
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.
140 Z Reserved for future use. Must be zero in all queries
143 RCODE Response code - this 4 bit field is set as part of
144 responses. The values have the following
149 1 Format error - The name server was
150 unable to interpret the query.
152 2 Server failure - The name server was
153 unable to process this query due to a
154 problem with the name server.
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
162 4 Not Implemented - The name server does
163 not support the requested kind of query.
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
173 transfer) for particular data.
175 6-15 Reserved for future use.
177 QDCOUNT an unsigned 16 bit integer specifying the number of
178 entries in the question section.
180 ANCOUNT an unsigned 16 bit integer specifying the number of
181 resource records in the answer section.
183 NSCOUNT an unsigned 16 bit integer specifying the number of name
184 server resource records in the authority records
187 ARCOUNT an unsigned 16 bit integer specifying the number of
188 resource records in the additional records section.
192 struct ldns_struct_hdr
194 /** Id of a packet */
196 /** Query bit (0=query, 1=answer) */
198 /** Authoritative answer */
200 /** Packet truncated */
202 /** Recursion desired */
204 /** Checking disabled */
206 /** Recursion available */
208 /** Authentic data */
211 ldns_pkt_opcode _opcode; /* XXX 8 bits? */
223 typedef struct ldns_struct_hdr ldns_hdr;
228 * This structure contains a complete DNS packet (either a query or an answer)
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).
233 struct ldns_struct_pkt
235 /** Header section */
237 /* extra items needed in a packet */
238 /** The size of the wire format of the packet in octets */
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 */
244 /** The size of the wire format of the packet in octets */
246 /** Optional 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;
253 uint8_t _edns_version;
254 /** Reserved EDNS data bits */
256 /** Arbitrary EDNS rdata */
257 ldns_rdf *_edns_data;
258 /** Question section */
259 ldns_rr_list *_question;
260 /** Answer section */
261 ldns_rr_list *_answer;
262 /** Authority section */
263 ldns_rr_list *_authority;
264 /** Additional section */
265 ldns_rr_list *_additional;
267 typedef struct ldns_struct_pkt ldns_pkt;
270 * The sections of a packet
272 enum ldns_enum_pkt_section {
273 LDNS_SECTION_QUESTION = 0,
274 LDNS_SECTION_ANSWER = 1,
275 LDNS_SECTION_AUTHORITY = 2,
276 LDNS_SECTION_ADDITIONAL = 3,
277 /** bogus section, if not interested */
278 LDNS_SECTION_ANY = 4,
279 /** used to get all non-question rrs from a packet */
280 LDNS_SECTION_ANY_NOQUESTION = 5
282 typedef enum ldns_enum_pkt_section ldns_pkt_section;
285 * The different types of packets
287 enum ldns_enum_pkt_type {
288 LDNS_PACKET_QUESTION,
289 LDNS_PACKET_REFERRAL,
291 LDNS_PACKET_NXDOMAIN,
295 typedef enum ldns_enum_pkt_type ldns_pkt_type;
303 * \param[in] p the packet
304 * \return the packet id
306 uint16_t ldns_pkt_id(const ldns_pkt *p);
308 * Read the packet's qr bit
309 * \param[in] p the packet
310 * \return value of the bit
312 bool ldns_pkt_qr(const ldns_pkt *p);
314 * Read the packet's aa bit
315 * \param[in] p the packet
316 * \return value of the bit
318 bool ldns_pkt_aa(const ldns_pkt *p);
320 * Read the packet's tc bit
321 * \param[in] p the packet
322 * \return value of the bit
324 bool ldns_pkt_tc(const ldns_pkt *p);
326 * Read the packet's rd bit
327 * \param[in] p the packet
328 * \return value of the bit
330 bool ldns_pkt_rd(const ldns_pkt *p);
332 * Read the packet's cd bit
333 * \param[in] p the packet
334 * \return value of the bit
336 bool ldns_pkt_cd(const ldns_pkt *p);
338 * Read the packet's ra bit
339 * \param[in] p the packet
340 * \return value of the bit
342 bool ldns_pkt_ra(const ldns_pkt *p);
344 * Read the packet's ad bit
345 * \param[in] p the packet
346 * \return value of the bit
348 bool ldns_pkt_ad(const ldns_pkt *p);
350 * Read the packet's code
351 * \param[in] p the packet
354 ldns_pkt_opcode ldns_pkt_get_opcode(const ldns_pkt *p);
356 * Return the packet's respons code
357 * \param[in] p the packet
358 * \return the respons code
360 ldns_pkt_rcode ldns_pkt_get_rcode(const ldns_pkt *p);
362 * Return the packet's qd count
363 * \param[in] p the packet
364 * \return the qd count
366 uint16_t ldns_pkt_qdcount(const ldns_pkt *p);
368 * Return the packet's an count
369 * \param[in] p the packet
370 * \return the an count
372 uint16_t ldns_pkt_ancount(const ldns_pkt *p);
374 * Return the packet's ns count
375 * \param[in] p the packet
376 * \return the ns count
378 uint16_t ldns_pkt_nscount(const ldns_pkt *p);
380 * Return the packet's ar count
381 * \param[in] p the packet
382 * \return the ar count
384 uint16_t ldns_pkt_arcount(const ldns_pkt *p);
387 * Return the packet's answerfrom
388 * \param[in] p packet
389 * \return the name of the server
391 ldns_rdf *ldns_pkt_answerfrom(const ldns_pkt *p);
394 * Return the packet's timestamp
395 * \param[in] p the packet
396 * \return the timestamp
398 struct timeval ldns_pkt_timestamp(const ldns_pkt *p);
400 * Return the packet's querytime
401 * \param[in] p the packet
402 * \return the querytime
404 uint32_t ldns_pkt_querytime(const ldns_pkt *p);
407 * Return the packet's size in bytes
408 * \param[in] p the packet
411 size_t ldns_pkt_size(const ldns_pkt *p);
414 * Return the packet's tsig pseudo rr's
415 * \param[in] p the packet
416 * \return the tsig rr
418 ldns_rr *ldns_pkt_tsig(const ldns_pkt *p);
421 * Return the packet's question section
422 * \param[in] p the packet
423 * \return the section
425 ldns_rr_list *ldns_pkt_question(const ldns_pkt *p);
427 * Return the packet's answer section
428 * \param[in] p the packet
429 * \return the section
431 ldns_rr_list *ldns_pkt_answer(const ldns_pkt *p);
433 * Return the packet's authority section
434 * \param[in] p the packet
435 * \return the section
437 ldns_rr_list *ldns_pkt_authority(const ldns_pkt *p);
439 * Return the packet's additional section
440 * \param[in] p the packet
441 * \return the section
443 ldns_rr_list *ldns_pkt_additional(const ldns_pkt *p);
445 * Return the packet's question, answer, authority and additional sections
446 * concatenated, in a new rr_list clone.
447 * \param[in] p the packet
450 ldns_rr_list *ldns_pkt_all(const ldns_pkt *p);
452 * Return the packet's answer, authority and additional sections concatenated,
453 * in a new rr_list clone. Like ldns_pkt_all but without the questions.
454 * \param[in] p the packet
455 * \return the rrs except the question rrs
457 ldns_rr_list *ldns_pkt_all_noquestion(const ldns_pkt *p);
460 * return all the rr_list's in the packet. Clone the lists, instead
461 * of returning pointers.
462 * \param[in] p the packet to look in
463 * \param[in] s what section(s) to return
464 * \return ldns_rr_list with the rr's or NULL if none were found
466 ldns_rr_list *ldns_pkt_get_section_clone(const ldns_pkt *p, ldns_pkt_section s);
469 * return all the rr with a specific name from a packet. Optionally
470 * specify from which section in the packet
471 * \param[in] p the packet
472 * \param[in] r the name
473 * \param[in] s the packet's section
474 * \return a list with the rr's or NULL if none were found
476 ldns_rr_list *ldns_pkt_rr_list_by_name(ldns_pkt *p, ldns_rdf *r, ldns_pkt_section s);
478 * return all the rr with a specific type from a packet. Optionally
479 * specify from which section in the packet
480 * \param[in] p the packet
481 * \param[in] t the type
482 * \param[in] s the packet's section
483 * \return a list with the rr's or NULL if none were found
485 ldns_rr_list *ldns_pkt_rr_list_by_type(const ldns_pkt *p, ldns_rr_type t, ldns_pkt_section s);
487 * return all the rr with a specific type and type from a packet. Optionally
488 * specify from which section in the packet
489 * \param[in] packet the packet
490 * \param[in] ownername the name
491 * \param[in] type the type
492 * \param[in] sec the packet's section
493 * \return a list with the rr's or NULL if none were found
495 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);
499 * check to see if an rr exist in the packet
500 * \param[in] pkt the packet to examine
501 * \param[in] sec in which section to look
502 * \param[in] rr the rr to look for
504 bool ldns_pkt_rr(ldns_pkt *pkt, ldns_pkt_section sec, ldns_rr *rr);
508 * sets the flags in a packet.
509 * \param[in] pkt the packet to operate on
510 * \param[in] flags ORed values: LDNS_QR| LDNS_AR for instance
511 * \return true on success otherwise false
513 bool ldns_pkt_set_flags(ldns_pkt *pkt, uint16_t flags);
516 * Set the packet's id
517 * \param[in] p the packet
518 * \param[in] id the id to set
520 void ldns_pkt_set_id(ldns_pkt *p, uint16_t id);
522 * Set the packet's id to a random value
523 * \param[in] p the packet
525 void ldns_pkt_set_random_id(ldns_pkt *p);
527 * Set the packet's qr bit
528 * \param[in] p the packet
529 * \param[in] b the value to set (boolean)
531 void ldns_pkt_set_qr(ldns_pkt *p, bool b);
533 * Set the packet's aa bit
534 * \param[in] p the packet
535 * \param[in] b the value to set (boolean)
537 void ldns_pkt_set_aa(ldns_pkt *p, bool b);
539 * Set the packet's tc bit
540 * \param[in] p the packet
541 * \param[in] b the value to set (boolean)
543 void ldns_pkt_set_tc(ldns_pkt *p, bool b);
545 * Set the packet's rd bit
546 * \param[in] p the packet
547 * \param[in] b the value to set (boolean)
549 void ldns_pkt_set_rd(ldns_pkt *p, bool b);
551 * Set the packet's cd bit
552 * \param[in] p the packet
553 * \param[in] b the value to set (boolean)
555 void ldns_pkt_set_cd(ldns_pkt *p, bool b);
557 * Set the packet's ra bit
558 * \param[in] p the packet
559 * \param[in] b the value to set (boolean)
561 void ldns_pkt_set_ra(ldns_pkt *p, bool b);
563 * Set the packet's ad bit
564 * \param[in] p the packet
565 * \param[in] b the value to set (boolean)
567 void ldns_pkt_set_ad(ldns_pkt *p, bool b);
570 * Set the packet's opcode
571 * \param[in] p the packet
572 * \param[in] c the opcode
574 void ldns_pkt_set_opcode(ldns_pkt *p, ldns_pkt_opcode c);
576 * Set the packet's respons code
577 * \param[in] p the packet
578 * \param[in] c the rcode
580 void ldns_pkt_set_rcode(ldns_pkt *p, uint8_t c);
582 * Set the packet's qd count
583 * \param[in] p the packet
584 * \param[in] c the count
586 void ldns_pkt_set_qdcount(ldns_pkt *p, uint16_t c);
588 * Set the packet's an count
589 * \param[in] p the packet
590 * \param[in] c the count
592 void ldns_pkt_set_ancount(ldns_pkt *p, uint16_t c);
594 * Set the packet's ns count
595 * \param[in] p the packet
596 * \param[in] c the count
598 void ldns_pkt_set_nscount(ldns_pkt *p, uint16_t c);
600 * Set the packet's arcount
601 * \param[in] p the packet
602 * \param[in] c the count
604 void ldns_pkt_set_arcount(ldns_pkt *p, uint16_t c);
606 * Set the packet's answering server
607 * \param[in] p the packet
608 * \param[in] r the address
610 void ldns_pkt_set_answerfrom(ldns_pkt *p, ldns_rdf *r);
612 * Set the packet's query time
613 * \param[in] p the packet
614 * \param[in] t the querytime in msec
616 void ldns_pkt_set_querytime(ldns_pkt *p, uint32_t t);
618 * Set the packet's size
619 * \param[in] p the packet
620 * \param[in] s the size
622 void ldns_pkt_set_size(ldns_pkt *p, size_t s);
625 * Set the packet's timestamp
626 * \param[in] p the packet
627 * \param[in] timeval the timestamp
629 void ldns_pkt_set_timestamp(ldns_pkt *p, struct timeval timeval);
631 * Set a packet's section count to x
632 * \param[in] p the packet
633 * \param[in] s the section
634 * \param[in] x the section count
636 void ldns_pkt_set_section_count(ldns_pkt *p, ldns_pkt_section s, uint16_t x);
638 * Set the packet's tsig rr
639 * \param[in] p the packet
640 * \param[in] t the tsig rr
642 void ldns_pkt_set_tsig(ldns_pkt *p, ldns_rr *t);
645 * looks inside the packet to determine
646 * what kind of packet it is, AUTH, NXDOMAIN, REFERRAL, etc.
647 * \param[in] p the packet to examine
648 * \return the type of packet
650 ldns_pkt_type ldns_pkt_reply_type(ldns_pkt *p);
653 * return the packet's edns udp size
654 * \param[in] packet the packet
657 uint16_t ldns_pkt_edns_udp_size(const ldns_pkt *packet);
659 * return the packet's edns extended rcode
660 * \param[in] packet the packet
663 uint8_t ldns_pkt_edns_extended_rcode(const ldns_pkt *packet);
665 * return the packet's edns version
666 * \param[in] packet the packet
667 * \return the version
669 uint8_t ldns_pkt_edns_version(const ldns_pkt *packet);
671 * return the packet's edns z value
672 * \param[in] packet the packet
673 * \return the z value
675 uint16_t ldns_pkt_edns_z(const ldns_pkt *packet);
677 * return the packet's edns data
678 * \param[in] packet the packet
681 ldns_rdf *ldns_pkt_edns_data(const ldns_pkt *packet);
684 * return the packet's edns do bit
685 * \param[in] packet the packet
686 * \return the bit's value
688 bool ldns_pkt_edns_do(const ldns_pkt *packet);
690 * Set the packet's edns do bit
691 * \param[in] packet the packet
692 * \param[in] value the bit's new value
694 void ldns_pkt_set_edns_do(ldns_pkt *packet, bool value);
697 * returns true if this packet needs and EDNS rr to be sent.
698 * At the moment the only reason is an expected packet
699 * size larger than 512 bytes, but for instance dnssec would
700 * be a good reason too.
702 * \param[in] packet the packet to check
703 * \return true if packet needs edns rr
705 bool ldns_pkt_edns(const ldns_pkt *packet);
708 * Set the packet's edns udp size
709 * \param[in] packet the packet
710 * \param[in] s the size
712 void ldns_pkt_set_edns_udp_size(ldns_pkt *packet, uint16_t s);
714 * Set the packet's edns extended rcode
715 * \param[in] packet the packet
716 * \param[in] c the code
718 void ldns_pkt_set_edns_extended_rcode(ldns_pkt *packet, uint8_t c);
720 * Set the packet's edns version
721 * \param[in] packet the packet
722 * \param[in] v the version
724 void ldns_pkt_set_edns_version(ldns_pkt *packet, uint8_t v);
726 * Set the packet's edns z value
727 * \param[in] packet the packet
728 * \param[in] z the value
730 void ldns_pkt_set_edns_z(ldns_pkt *packet, uint16_t z);
732 * Set the packet's edns data
733 * \param[in] packet the packet
734 * \param[in] data the data
736 void ldns_pkt_set_edns_data(ldns_pkt *packet, ldns_rdf *data);
739 * allocates and initializes a ldns_pkt structure.
740 * \return pointer to the new packet
742 ldns_pkt *ldns_pkt_new();
745 * frees the packet structure and all data that it contains.
746 * \param[in] packet The packet structure to free
749 void ldns_pkt_free(ldns_pkt *packet);
752 * creates a query packet for the given name, type, class.
753 * \param[out] p the packet to be returned
754 * \param[in] rr_name the name to query for (as string)
755 * \param[in] rr_type the type to query for
756 * \param[in] rr_class the class to query for
757 * \param[in] flags packet flags
758 * \return LDNS_STATUS_OK or a ldns_status mesg with the error
760 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);
763 * creates a packet with a query in it for the given name, type and class.
764 * \param[in] rr_name the name to query for
765 * \param[in] rr_type the type to query for
766 * \param[in] rr_class the class to query for
767 * \param[in] flags packet flags
768 * \return ldns_pkt* a pointer to the new pkt
770 ldns_pkt *ldns_pkt_query_new(ldns_rdf *rr_name, ldns_rr_type rr_type, ldns_rr_class rr_class, uint16_t flags);
773 * clones the given packet, creating a fully allocated copy
775 * \param[in] pkt the packet to clone
776 * \return ldns_pkt* pointer to the new packet
778 ldns_pkt *ldns_pkt_clone(ldns_pkt *pkt);
781 * directly set the additional section
782 * \param[in] p packet to operate on
783 * \param[in] rr rrlist to set
785 void ldns_pkt_set_additional(ldns_pkt *p, ldns_rr_list *rr);
788 * directly set the answer section
789 * \param[in] p packet to operate on
790 * \param[in] rr rrlist to set
792 void ldns_pkt_set_answer(ldns_pkt *p, ldns_rr_list *rr);
795 * directly set the question section
796 * \param[in] p packet to operate on
797 * \param[in] rr rrlist to set
799 void ldns_pkt_set_question(ldns_pkt *p, ldns_rr_list *rr);
802 * directly set the auhority section
803 * \param[in] p packet to operate on
804 * \param[in] rr rrlist to set
806 void ldns_pkt_set_authority(ldns_pkt *p, ldns_rr_list *rr);
809 * push an rr on a packet
810 * \param[in] packet packet to operate on
811 * \param[in] section where to put it
812 * \param[in] rr rr to push
813 * \return a boolean which is true when the rr was added
815 bool ldns_pkt_push_rr(ldns_pkt *packet, ldns_pkt_section section, ldns_rr *rr);
818 * push an rr on a packet, provided the RR is not there.
819 * \param[in] pkt packet to operate on
820 * \param[in] sec where to put it
821 * \param[in] rr rr to push
822 * \return a boolean which is true when the rr was added
824 bool ldns_pkt_safe_push_rr(ldns_pkt *pkt, ldns_pkt_section sec, ldns_rr *rr);
827 * push a rr_list on a packet
828 * \param[in] packet packet to operate on
829 * \param[in] section where to put it
830 * \param[in] list the rr_list to push
831 * \return a boolean which is true when the rr was added
833 bool ldns_pkt_push_rr_list(ldns_pkt *packet, ldns_pkt_section section, ldns_rr_list *list);
836 * push an rr_list to a packet, provided the RRs are not already there.
837 * \param[in] pkt packet to operate on
838 * \param[in] sec where to put it
839 * \param[in] list the rr_list to push
840 * \return a boolean which is true when the rr was added
842 bool ldns_pkt_safe_push_rr_list(ldns_pkt *pkt, ldns_pkt_section sec, ldns_rr_list *list);
845 * check if a packet is empty
846 * \param[in] p packet
847 * \return true: empty, false: not empty
849 bool ldns_pkt_empty(ldns_pkt *p);
855 #endif /* LDNS_PACKET_H */