• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 #ifndef HEADER_CARES_DNS_H
2 #define HEADER_CARES_DNS_H
3 
4 /* Copyright 1998, 2011 by the Massachusetts Institute of Technology.
5  *
6  * Permission to use, copy, modify, and distribute this
7  * software and its documentation for any purpose and without
8  * fee is hereby granted, provided that the above copyright
9  * notice appear in all copies and that both that copyright
10  * notice and this permission notice appear in supporting
11  * documentation, and that the name of M.I.T. not be used in
12  * advertising or publicity pertaining to distribution of the
13  * software without specific, written prior permission.
14  * M.I.T. makes no representations about the suitability of
15  * this software for any purpose.  It is provided "as is"
16  * without express or implied warranty.
17  */
18 
19 /*
20  * Macro DNS__16BIT reads a network short (16 bit) given in network
21  * byte order, and returns its value as an unsigned short.
22  */
23 #define DNS__16BIT(p)  ((unsigned short)((unsigned int) 0xffff & \
24                          (((unsigned int)((unsigned char)(p)[0]) << 8U) | \
25                           ((unsigned int)((unsigned char)(p)[1])))))
26 
27 /*
28  * Macro DNS__32BIT reads a network long (32 bit) given in network
29  * byte order, and returns its value as an unsigned int.
30  */
31 #define DNS__32BIT(p)  ((unsigned int) \
32                          (((unsigned int)((unsigned char)(p)[0]) << 24U) | \
33                           ((unsigned int)((unsigned char)(p)[1]) << 16U) | \
34                           ((unsigned int)((unsigned char)(p)[2]) <<  8U) | \
35                           ((unsigned int)((unsigned char)(p)[3]))))
36 
37 #define DNS__SET16BIT(p, v)  (((p)[0] = (unsigned char)(((v) >> 8) & 0xff)), \
38                               ((p)[1] = (unsigned char)((v) & 0xff)))
39 #define DNS__SET32BIT(p, v)  (((p)[0] = (unsigned char)(((v) >> 24) & 0xff)), \
40                               ((p)[1] = (unsigned char)(((v) >> 16) & 0xff)), \
41                               ((p)[2] = (unsigned char)(((v) >> 8) & 0xff)), \
42                               ((p)[3] = (unsigned char)((v) & 0xff)))
43 
44 #if 0
45 /* we cannot use this approach on systems where we can't access 16/32 bit
46    data on un-aligned addresses */
47 #define DNS__16BIT(p)                   ntohs(*(unsigned short*)(p))
48 #define DNS__32BIT(p)                   ntohl(*(unsigned long*)(p))
49 #define DNS__SET16BIT(p, v)             *(unsigned short*)(p) = htons(v)
50 #define DNS__SET32BIT(p, v)             *(unsigned long*)(p) = htonl(v)
51 #endif
52 
53 /* Macros for parsing a DNS header */
54 #define DNS_HEADER_QID(h)               DNS__16BIT(h)
55 #define DNS_HEADER_QR(h)                (((h)[2] >> 7) & 0x1)
56 #define DNS_HEADER_OPCODE(h)            (((h)[2] >> 3) & 0xf)
57 #define DNS_HEADER_AA(h)                (((h)[2] >> 2) & 0x1)
58 #define DNS_HEADER_TC(h)                (((h)[2] >> 1) & 0x1)
59 #define DNS_HEADER_RD(h)                ((h)[2] & 0x1)
60 #define DNS_HEADER_RA(h)                (((h)[3] >> 7) & 0x1)
61 #define DNS_HEADER_Z(h)                 (((h)[3] >> 4) & 0x7)
62 #define DNS_HEADER_RCODE(h)             ((h)[3] & 0xf)
63 #define DNS_HEADER_QDCOUNT(h)           DNS__16BIT((h) + 4)
64 #define DNS_HEADER_ANCOUNT(h)           DNS__16BIT((h) + 6)
65 #define DNS_HEADER_NSCOUNT(h)           DNS__16BIT((h) + 8)
66 #define DNS_HEADER_ARCOUNT(h)           DNS__16BIT((h) + 10)
67 
68 /* Macros for constructing a DNS header */
69 #define DNS_HEADER_SET_QID(h, v)      DNS__SET16BIT(h, v)
70 #define DNS_HEADER_SET_QR(h, v)       ((h)[2] |= (unsigned char)(((v) & 0x1) << 7))
71 #define DNS_HEADER_SET_OPCODE(h, v)   ((h)[2] |= (unsigned char)(((v) & 0xf) << 3))
72 #define DNS_HEADER_SET_AA(h, v)       ((h)[2] |= (unsigned char)(((v) & 0x1) << 2))
73 #define DNS_HEADER_SET_TC(h, v)       ((h)[2] |= (unsigned char)(((v) & 0x1) << 1))
74 #define DNS_HEADER_SET_RD(h, v)       ((h)[2] |= (unsigned char)((v) & 0x1))
75 #define DNS_HEADER_SET_RA(h, v)       ((h)[3] |= (unsigned char)(((v) & 0x1) << 7))
76 #define DNS_HEADER_SET_Z(h, v)        ((h)[3] |= (unsigned char)(((v) & 0x7) << 4))
77 #define DNS_HEADER_SET_RCODE(h, v)    ((h)[3] |= (unsigned char)((v) & 0xf))
78 #define DNS_HEADER_SET_QDCOUNT(h, v)  DNS__SET16BIT((h) + 4, v)
79 #define DNS_HEADER_SET_ANCOUNT(h, v)  DNS__SET16BIT((h) + 6, v)
80 #define DNS_HEADER_SET_NSCOUNT(h, v)  DNS__SET16BIT((h) + 8, v)
81 #define DNS_HEADER_SET_ARCOUNT(h, v)  DNS__SET16BIT((h) + 10, v)
82 
83 /* Macros for parsing the fixed part of a DNS question */
84 #define DNS_QUESTION_TYPE(q)            DNS__16BIT(q)
85 #define DNS_QUESTION_CLASS(q)           DNS__16BIT((q) + 2)
86 
87 /* Macros for constructing the fixed part of a DNS question */
88 #define DNS_QUESTION_SET_TYPE(q, v)     DNS__SET16BIT(q, v)
89 #define DNS_QUESTION_SET_CLASS(q, v)    DNS__SET16BIT((q) + 2, v)
90 
91 /* Macros for parsing the fixed part of a DNS resource record */
92 #define DNS_RR_TYPE(r)                  DNS__16BIT(r)
93 #define DNS_RR_CLASS(r)                 DNS__16BIT((r) + 2)
94 #define DNS_RR_TTL(r)                   DNS__32BIT((r) + 4)
95 #define DNS_RR_LEN(r)                   DNS__16BIT((r) + 8)
96 
97 /* Macros for constructing the fixed part of a DNS resource record */
98 #define DNS_RR_SET_TYPE(r, v)           DNS__SET16BIT(r, v)
99 #define DNS_RR_SET_CLASS(r, v)          DNS__SET16BIT((r) + 2, v)
100 #define DNS_RR_SET_TTL(r, v)            DNS__SET32BIT((r) + 4, v)
101 #define DNS_RR_SET_LEN(r, v)            DNS__SET16BIT((r) + 8, v)
102 
103 #endif /* HEADER_CARES_DNS_H */
104