xref: /freebsd/contrib/unbound/sldns/pkthdr.h (revision 924226fba12cc9a228c73b956e1b7fa24c60b055)
1 /*
2  * pkthdr.h - packet header from wire conversion routines
3  *
4  * a Net::DNS like library for C
5  *
6  * (c) NLnet Labs, 2005-2006
7  *
8  * See the file LICENSE for the license
9  */
10 
11 /**
12  * \file
13  *
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.
16  */
17 
18 #ifndef LDNS_PKTHDR_H
19 #define LDNS_PKTHDR_H
20 
21 #ifdef __cplusplus
22 extern "C" {
23 #endif
24 
25 /* The length of the header */
26 #define	LDNS_HEADER_SIZE	12
27 
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)
34 
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)
40 
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)
46 
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))
52 
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)
58 
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))
65 
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)
71 
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)
77 
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)
83 
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)
89 
90 /* Query ID */
91 #define	LDNS_ID_WIRE(wirebuf)		(sldns_read_uint16(wirebuf))
92 #define	LDNS_ID_SET(wirebuf, id)	(sldns_write_uint16(wirebuf, id))
93 
94 /* Counter of the question section */
95 #define LDNS_QDCOUNT_OFF		4
96 /*
97 #define	QDCOUNT(wirebuf)		(ntohs(*(uint16_t *)(wirebuf+QDCOUNT_OFF)))
98 */
99 #define	LDNS_QDCOUNT(wirebuf)		(sldns_read_uint16(wirebuf+LDNS_QDCOUNT_OFF))
100 #define LDNS_QDCOUNT_SET(wirebuf, i)    (sldns_write_uint16(wirebuf+LDNS_QDCOUNT_OFF, i))
101 
102 /* Counter of the answer section */
103 #define LDNS_ANCOUNT_OFF		6
104 #define	LDNS_ANCOUNT(wirebuf)		(sldns_read_uint16(wirebuf+LDNS_ANCOUNT_OFF))
105 #define LDNS_ANCOUNT_SET(wirebuf, i)    (sldns_write_uint16(wirebuf+LDNS_ANCOUNT_OFF, i))
106 
107 /* Counter of the authority section */
108 #define LDNS_NSCOUNT_OFF		8
109 #define	LDNS_NSCOUNT(wirebuf)		(sldns_read_uint16(wirebuf+LDNS_NSCOUNT_OFF))
110 #define LDNS_NSCOUNT_SET(wirebuf, i)    (sldns_write_uint16(wirebuf+LDNS_NSCOUNT_OFF, i))
111 
112 /* Counter of the additional section */
113 #define LDNS_ARCOUNT_OFF		10
114 #define	LDNS_ARCOUNT(wirebuf)		(sldns_read_uint16(wirebuf+LDNS_ARCOUNT_OFF))
115 #define LDNS_ARCOUNT_SET(wirebuf, i)    (sldns_write_uint16(wirebuf+LDNS_ARCOUNT_OFF, i))
116 
117 /**
118  * The sections of a packet
119  */
120 enum sldns_enum_pkt_section {
121         LDNS_SECTION_QUESTION = 0,
122         LDNS_SECTION_ANSWER = 1,
123         LDNS_SECTION_AUTHORITY = 2,
124         LDNS_SECTION_ADDITIONAL = 3,
125         /** bogus section, if not interested */
126         LDNS_SECTION_ANY = 4,
127         /** used to get all non-question rrs from a packet */
128         LDNS_SECTION_ANY_NOQUESTION = 5
129 };
130 typedef enum sldns_enum_pkt_section sldns_pkt_section;
131 
132 /* opcodes for pkt's */
133 enum sldns_enum_pkt_opcode {
134         LDNS_PACKET_QUERY = 0,
135         LDNS_PACKET_IQUERY = 1,
136         LDNS_PACKET_STATUS = 2, /* there is no 3?? DNS is weird */
137         LDNS_PACKET_NOTIFY = 4,
138         LDNS_PACKET_UPDATE = 5
139 };
140 typedef enum sldns_enum_pkt_opcode sldns_pkt_opcode;
141 
142 /* rcodes for pkts */
143 enum sldns_enum_pkt_rcode {
144         LDNS_RCODE_NOERROR = 0,
145         LDNS_RCODE_FORMERR = 1,
146         LDNS_RCODE_SERVFAIL = 2,
147         LDNS_RCODE_NXDOMAIN = 3,
148         LDNS_RCODE_NOTIMPL = 4,
149         LDNS_RCODE_REFUSED = 5,
150         LDNS_RCODE_YXDOMAIN = 6,
151         LDNS_RCODE_YXRRSET = 7,
152         LDNS_RCODE_NXRRSET = 8,
153         LDNS_RCODE_NOTAUTH = 9,
154         LDNS_RCODE_NOTZONE = 10
155 };
156 typedef enum sldns_enum_pkt_rcode sldns_pkt_rcode;
157 
158 #ifdef __cplusplus
159 }
160 #endif
161 
162 #endif /* LDNS_PKTHDR_H */
163