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