2 * pkthdr.h - packet header from wire conversion routines
4 * a Net::DNS like library for C
6 * (c) NLnet Labs, 2005-2006
8 * See the file LICENSE for the license
14 * Contains functions that translate dns data from the wire format (as sent
15 * by servers and clients) to the internal structures for the packet header.
25 /* The length of the header */
26 #define LDNS_HEADER_SIZE 12
28 /* First octet of flags */
29 #define LDNS_RD_MASK 0x01U
30 #define LDNS_RD_SHIFT 0
31 #define LDNS_RD_WIRE(wirebuf) (*(wirebuf+2) & LDNS_RD_MASK)
32 #define LDNS_RD_SET(wirebuf) (*(wirebuf+2) |= LDNS_RD_MASK)
33 #define LDNS_RD_CLR(wirebuf) (*(wirebuf+2) &= ~LDNS_RD_MASK)
35 #define LDNS_TC_MASK 0x02U
36 #define LDNS_TC_SHIFT 1
37 #define LDNS_TC_WIRE(wirebuf) (*(wirebuf+2) & LDNS_TC_MASK)
38 #define LDNS_TC_SET(wirebuf) (*(wirebuf+2) |= LDNS_TC_MASK)
39 #define LDNS_TC_CLR(wirebuf) (*(wirebuf+2) &= ~LDNS_TC_MASK)
41 #define LDNS_AA_MASK 0x04U
42 #define LDNS_AA_SHIFT 2
43 #define LDNS_AA_WIRE(wirebuf) (*(wirebuf+2) & LDNS_AA_MASK)
44 #define LDNS_AA_SET(wirebuf) (*(wirebuf+2) |= LDNS_AA_MASK)
45 #define LDNS_AA_CLR(wirebuf) (*(wirebuf+2) &= ~LDNS_AA_MASK)
47 #define LDNS_OPCODE_MASK 0x78U
48 #define LDNS_OPCODE_SHIFT 3
49 #define LDNS_OPCODE_WIRE(wirebuf) ((*(wirebuf+2) & LDNS_OPCODE_MASK) >> LDNS_OPCODE_SHIFT)
50 #define LDNS_OPCODE_SET(wirebuf, opcode) \
51 (*(wirebuf+2) = ((*(wirebuf+2)) & ~LDNS_OPCODE_MASK) | ((opcode) << LDNS_OPCODE_SHIFT))
53 #define LDNS_QR_MASK 0x80U
54 #define LDNS_QR_SHIFT 7
55 #define LDNS_QR_WIRE(wirebuf) (*(wirebuf+2) & LDNS_QR_MASK)
56 #define LDNS_QR_SET(wirebuf) (*(wirebuf+2) |= LDNS_QR_MASK)
57 #define LDNS_QR_CLR(wirebuf) (*(wirebuf+2) &= ~LDNS_QR_MASK)
59 /* Second octet of flags */
60 #define LDNS_RCODE_MASK 0x0fU
61 #define LDNS_RCODE_SHIFT 0
62 #define LDNS_RCODE_WIRE(wirebuf) (*(wirebuf+3) & LDNS_RCODE_MASK)
63 #define LDNS_RCODE_SET(wirebuf, rcode) \
64 (*(wirebuf+3) = ((*(wirebuf+3)) & ~LDNS_RCODE_MASK) | (rcode))
66 #define LDNS_CD_MASK 0x10U
67 #define LDNS_CD_SHIFT 4
68 #define LDNS_CD_WIRE(wirebuf) (*(wirebuf+3) & LDNS_CD_MASK)
69 #define LDNS_CD_SET(wirebuf) (*(wirebuf+3) |= LDNS_CD_MASK)
70 #define LDNS_CD_CLR(wirebuf) (*(wirebuf+3) &= ~LDNS_CD_MASK)
72 #define LDNS_AD_MASK 0x20U
73 #define LDNS_AD_SHIFT 5
74 #define LDNS_AD_WIRE(wirebuf) (*(wirebuf+3) & LDNS_AD_MASK)
75 #define LDNS_AD_SET(wirebuf) (*(wirebuf+3) |= LDNS_AD_MASK)
76 #define LDNS_AD_CLR(wirebuf) (*(wirebuf+3) &= ~LDNS_AD_MASK)
78 #define LDNS_Z_MASK 0x40U
79 #define LDNS_Z_SHIFT 6
80 #define LDNS_Z_WIRE(wirebuf) (*(wirebuf+3) & LDNS_Z_MASK)
81 #define LDNS_Z_SET(wirebuf) (*(wirebuf+3) |= LDNS_Z_MASK)
82 #define LDNS_Z_CLR(wirebuf) (*(wirebuf+3) &= ~LDNS_Z_MASK)
84 #define LDNS_RA_MASK 0x80U
85 #define LDNS_RA_SHIFT 7
86 #define LDNS_RA_WIRE(wirebuf) (*(wirebuf+3) & LDNS_RA_MASK)
87 #define LDNS_RA_SET(wirebuf) (*(wirebuf+3) |= LDNS_RA_MASK)
88 #define LDNS_RA_CLR(wirebuf) (*(wirebuf+3) &= ~LDNS_RA_MASK)
91 #define LDNS_ID_WIRE(wirebuf) (sldns_read_uint16(wirebuf))
92 #define LDNS_ID_SET(wirebuf, id) (sldns_write_uint16(wirebuf, id))
94 /* Counter of the question section */
95 #define LDNS_QDCOUNT_OFF 4
97 #define QDCOUNT(wirebuf) (ntohs(*(uint16_t *)(wirebuf+QDCOUNT_OFF)))
99 #define LDNS_QDCOUNT(wirebuf) (sldns_read_uint16(wirebuf+LDNS_QDCOUNT_OFF))
101 /* Counter of the answer section */
102 #define LDNS_ANCOUNT_OFF 6
103 #define LDNS_ANCOUNT(wirebuf) (sldns_read_uint16(wirebuf+LDNS_ANCOUNT_OFF))
105 /* Counter of the authority section */
106 #define LDNS_NSCOUNT_OFF 8
107 #define LDNS_NSCOUNT(wirebuf) (sldns_read_uint16(wirebuf+LDNS_NSCOUNT_OFF))
109 /* Counter of the additional section */
110 #define LDNS_ARCOUNT_OFF 10
111 #define LDNS_ARCOUNT(wirebuf) (sldns_read_uint16(wirebuf+LDNS_ARCOUNT_OFF))
114 * The sections of a packet
116 enum sldns_enum_pkt_section
{
117 LDNS_SECTION_QUESTION
= 0,
118 LDNS_SECTION_ANSWER
= 1,
119 LDNS_SECTION_AUTHORITY
= 2,
120 LDNS_SECTION_ADDITIONAL
= 3,
121 /** bogus section, if not interested */
122 LDNS_SECTION_ANY
= 4,
123 /** used to get all non-question rrs from a packet */
124 LDNS_SECTION_ANY_NOQUESTION
= 5
126 typedef enum sldns_enum_pkt_section sldns_pkt_section
;
128 /* opcodes for pkt's */
129 enum sldns_enum_pkt_opcode
{
130 LDNS_PACKET_QUERY
= 0,
131 LDNS_PACKET_IQUERY
= 1,
132 LDNS_PACKET_STATUS
= 2, /* there is no 3?? DNS is weird */
133 LDNS_PACKET_NOTIFY
= 4,
134 LDNS_PACKET_UPDATE
= 5
136 typedef enum sldns_enum_pkt_opcode sldns_pkt_opcode
;
138 /* rcodes for pkts */
139 enum sldns_enum_pkt_rcode
{
140 LDNS_RCODE_NOERROR
= 0,
141 LDNS_RCODE_FORMERR
= 1,
142 LDNS_RCODE_SERVFAIL
= 2,
143 LDNS_RCODE_NXDOMAIN
= 3,
144 LDNS_RCODE_NOTIMPL
= 4,
145 LDNS_RCODE_REFUSED
= 5,
146 LDNS_RCODE_YXDOMAIN
= 6,
147 LDNS_RCODE_YXRRSET
= 7,
148 LDNS_RCODE_NXRRSET
= 8,
149 LDNS_RCODE_NOTAUTH
= 9,
150 LDNS_RCODE_NOTZONE
= 10
152 typedef enum sldns_enum_pkt_rcode sldns_pkt_rcode
;
158 #endif /* LDNS_PKTHDR_H */