jpayne@69
|
1 /* MIT License
|
jpayne@69
|
2 *
|
jpayne@69
|
3 * Copyright (c) Massachusetts Institute of Technology
|
jpayne@69
|
4 * Copyright (c) The c-ares project and its contributors
|
jpayne@69
|
5 *
|
jpayne@69
|
6 * Permission is hereby granted, free of charge, to any person obtaining a copy
|
jpayne@69
|
7 * of this software and associated documentation files (the "Software"), to deal
|
jpayne@69
|
8 * in the Software without restriction, including without limitation the rights
|
jpayne@69
|
9 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
jpayne@69
|
10 * copies of the Software, and to permit persons to whom the Software is
|
jpayne@69
|
11 * furnished to do so, subject to the following conditions:
|
jpayne@69
|
12 *
|
jpayne@69
|
13 * The above copyright notice and this permission notice (including the next
|
jpayne@69
|
14 * paragraph) shall be included in all copies or substantial portions of the
|
jpayne@69
|
15 * Software.
|
jpayne@69
|
16 *
|
jpayne@69
|
17 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
jpayne@69
|
18 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
jpayne@69
|
19 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
jpayne@69
|
20 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
jpayne@69
|
21 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
jpayne@69
|
22 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
jpayne@69
|
23 * SOFTWARE.
|
jpayne@69
|
24 *
|
jpayne@69
|
25 * SPDX-License-Identifier: MIT
|
jpayne@69
|
26 */
|
jpayne@69
|
27 #ifndef HEADER_CARES_DNS_H
|
jpayne@69
|
28 #define HEADER_CARES_DNS_H
|
jpayne@69
|
29
|
jpayne@69
|
30 /*
|
jpayne@69
|
31 * NOTE TO INTEGRATORS:
|
jpayne@69
|
32 *
|
jpayne@69
|
33 * This header is made public due to legacy projects relying on it.
|
jpayne@69
|
34 * Please do not use the macros within this header, or include this
|
jpayne@69
|
35 * header in your project as it may be removed in the future.
|
jpayne@69
|
36 */
|
jpayne@69
|
37
|
jpayne@69
|
38
|
jpayne@69
|
39 /*
|
jpayne@69
|
40 * Macro DNS__16BIT reads a network short (16 bit) given in network
|
jpayne@69
|
41 * byte order, and returns its value as an unsigned short.
|
jpayne@69
|
42 */
|
jpayne@69
|
43 #define DNS__16BIT(p) \
|
jpayne@69
|
44 ((unsigned short)((unsigned int)0xffff & \
|
jpayne@69
|
45 (((unsigned int)((unsigned char)(p)[0]) << 8U) | \
|
jpayne@69
|
46 ((unsigned int)((unsigned char)(p)[1])))))
|
jpayne@69
|
47
|
jpayne@69
|
48 /*
|
jpayne@69
|
49 * Macro DNS__32BIT reads a network long (32 bit) given in network
|
jpayne@69
|
50 * byte order, and returns its value as an unsigned int.
|
jpayne@69
|
51 */
|
jpayne@69
|
52 #define DNS__32BIT(p) \
|
jpayne@69
|
53 ((unsigned int)(((unsigned int)((unsigned char)(p)[0]) << 24U) | \
|
jpayne@69
|
54 ((unsigned int)((unsigned char)(p)[1]) << 16U) | \
|
jpayne@69
|
55 ((unsigned int)((unsigned char)(p)[2]) << 8U) | \
|
jpayne@69
|
56 ((unsigned int)((unsigned char)(p)[3]))))
|
jpayne@69
|
57
|
jpayne@69
|
58 #define DNS__SET16BIT(p, v) \
|
jpayne@69
|
59 (((p)[0] = (unsigned char)(((v) >> 8) & 0xff)), \
|
jpayne@69
|
60 ((p)[1] = (unsigned char)((v) & 0xff)))
|
jpayne@69
|
61 #define DNS__SET32BIT(p, v) \
|
jpayne@69
|
62 (((p)[0] = (unsigned char)(((v) >> 24) & 0xff)), \
|
jpayne@69
|
63 ((p)[1] = (unsigned char)(((v) >> 16) & 0xff)), \
|
jpayne@69
|
64 ((p)[2] = (unsigned char)(((v) >> 8) & 0xff)), \
|
jpayne@69
|
65 ((p)[3] = (unsigned char)((v) & 0xff)))
|
jpayne@69
|
66
|
jpayne@69
|
67 #if 0
|
jpayne@69
|
68 /* we cannot use this approach on systems where we can't access 16/32 bit
|
jpayne@69
|
69 data on un-aligned addresses */
|
jpayne@69
|
70 # define DNS__16BIT(p) ntohs(*(unsigned short *)(p))
|
jpayne@69
|
71 # define DNS__32BIT(p) ntohl(*(unsigned long *)(p))
|
jpayne@69
|
72 # define DNS__SET16BIT(p, v) *(unsigned short *)(p) = htons(v)
|
jpayne@69
|
73 # define DNS__SET32BIT(p, v) *(unsigned long *)(p) = htonl(v)
|
jpayne@69
|
74 #endif
|
jpayne@69
|
75
|
jpayne@69
|
76 /* Macros for parsing a DNS header */
|
jpayne@69
|
77 #define DNS_HEADER_QID(h) DNS__16BIT(h)
|
jpayne@69
|
78 #define DNS_HEADER_QR(h) (((h)[2] >> 7) & 0x1)
|
jpayne@69
|
79 #define DNS_HEADER_OPCODE(h) (((h)[2] >> 3) & 0xf)
|
jpayne@69
|
80 #define DNS_HEADER_AA(h) (((h)[2] >> 2) & 0x1)
|
jpayne@69
|
81 #define DNS_HEADER_TC(h) (((h)[2] >> 1) & 0x1)
|
jpayne@69
|
82 #define DNS_HEADER_RD(h) ((h)[2] & 0x1)
|
jpayne@69
|
83 #define DNS_HEADER_RA(h) (((h)[3] >> 7) & 0x1)
|
jpayne@69
|
84 #define DNS_HEADER_Z(h) (((h)[3] >> 4) & 0x7)
|
jpayne@69
|
85 #define DNS_HEADER_RCODE(h) ((h)[3] & 0xf)
|
jpayne@69
|
86 #define DNS_HEADER_QDCOUNT(h) DNS__16BIT((h) + 4)
|
jpayne@69
|
87 #define DNS_HEADER_ANCOUNT(h) DNS__16BIT((h) + 6)
|
jpayne@69
|
88 #define DNS_HEADER_NSCOUNT(h) DNS__16BIT((h) + 8)
|
jpayne@69
|
89 #define DNS_HEADER_ARCOUNT(h) DNS__16BIT((h) + 10)
|
jpayne@69
|
90
|
jpayne@69
|
91 /* Macros for constructing a DNS header */
|
jpayne@69
|
92 #define DNS_HEADER_SET_QID(h, v) DNS__SET16BIT(h, v)
|
jpayne@69
|
93 #define DNS_HEADER_SET_QR(h, v) ((h)[2] |= (unsigned char)(((v) & 0x1) << 7))
|
jpayne@69
|
94 #define DNS_HEADER_SET_OPCODE(h, v) \
|
jpayne@69
|
95 ((h)[2] |= (unsigned char)(((v) & 0xf) << 3))
|
jpayne@69
|
96 #define DNS_HEADER_SET_AA(h, v) ((h)[2] |= (unsigned char)(((v) & 0x1) << 2))
|
jpayne@69
|
97 #define DNS_HEADER_SET_TC(h, v) ((h)[2] |= (unsigned char)(((v) & 0x1) << 1))
|
jpayne@69
|
98 #define DNS_HEADER_SET_RD(h, v) ((h)[2] |= (unsigned char)((v) & 0x1))
|
jpayne@69
|
99 #define DNS_HEADER_SET_RA(h, v) ((h)[3] |= (unsigned char)(((v) & 0x1) << 7))
|
jpayne@69
|
100 #define DNS_HEADER_SET_Z(h, v) ((h)[3] |= (unsigned char)(((v) & 0x7) << 4))
|
jpayne@69
|
101 #define DNS_HEADER_SET_RCODE(h, v) ((h)[3] |= (unsigned char)((v) & 0xf))
|
jpayne@69
|
102 #define DNS_HEADER_SET_QDCOUNT(h, v) DNS__SET16BIT((h) + 4, v)
|
jpayne@69
|
103 #define DNS_HEADER_SET_ANCOUNT(h, v) DNS__SET16BIT((h) + 6, v)
|
jpayne@69
|
104 #define DNS_HEADER_SET_NSCOUNT(h, v) DNS__SET16BIT((h) + 8, v)
|
jpayne@69
|
105 #define DNS_HEADER_SET_ARCOUNT(h, v) DNS__SET16BIT((h) + 10, v)
|
jpayne@69
|
106
|
jpayne@69
|
107 /* Macros for parsing the fixed part of a DNS question */
|
jpayne@69
|
108 #define DNS_QUESTION_TYPE(q) DNS__16BIT(q)
|
jpayne@69
|
109 #define DNS_QUESTION_CLASS(q) DNS__16BIT((q) + 2)
|
jpayne@69
|
110
|
jpayne@69
|
111 /* Macros for constructing the fixed part of a DNS question */
|
jpayne@69
|
112 #define DNS_QUESTION_SET_TYPE(q, v) DNS__SET16BIT(q, v)
|
jpayne@69
|
113 #define DNS_QUESTION_SET_CLASS(q, v) DNS__SET16BIT((q) + 2, v)
|
jpayne@69
|
114
|
jpayne@69
|
115 /* Macros for parsing the fixed part of a DNS resource record */
|
jpayne@69
|
116 #define DNS_RR_TYPE(r) DNS__16BIT(r)
|
jpayne@69
|
117 #define DNS_RR_CLASS(r) DNS__16BIT((r) + 2)
|
jpayne@69
|
118 #define DNS_RR_TTL(r) DNS__32BIT((r) + 4)
|
jpayne@69
|
119 #define DNS_RR_LEN(r) DNS__16BIT((r) + 8)
|
jpayne@69
|
120
|
jpayne@69
|
121 /* Macros for constructing the fixed part of a DNS resource record */
|
jpayne@69
|
122 #define DNS_RR_SET_TYPE(r, v) DNS__SET16BIT(r, v)
|
jpayne@69
|
123 #define DNS_RR_SET_CLASS(r, v) DNS__SET16BIT((r) + 2, v)
|
jpayne@69
|
124 #define DNS_RR_SET_TTL(r, v) DNS__SET32BIT((r) + 4, v)
|
jpayne@69
|
125 #define DNS_RR_SET_LEN(r, v) DNS__SET16BIT((r) + 8, v)
|
jpayne@69
|
126
|
jpayne@69
|
127 #endif /* HEADER_CARES_DNS_H */
|