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 * NOTE TO INTEGRATORS: 21 * 22 * This header is made public due to legacy projects relying on it. 23 * Please do not use the macros within this header, or include this 24 * header in your project as it may be removed in the future. 25 */ 26 27 28 /* 29 * Macro DNS__16BIT reads a network short (16 bit) given in network 30 * byte order, and returns its value as an unsigned short. 31 */ 32 #define DNS__16BIT(p) ((unsigned short)((unsigned int) 0xffff & \ 33 (((unsigned int)((unsigned char)(p)[0]) << 8U) | \ 34 ((unsigned int)((unsigned char)(p)[1]))))) 35 36 /* 37 * Macro DNS__32BIT reads a network long (32 bit) given in network 38 * byte order, and returns its value as an unsigned int. 39 */ 40 #define DNS__32BIT(p) ((unsigned int) \ 41 (((unsigned int)((unsigned char)(p)[0]) << 24U) | \ 42 ((unsigned int)((unsigned char)(p)[1]) << 16U) | \ 43 ((unsigned int)((unsigned char)(p)[2]) << 8U) | \ 44 ((unsigned int)((unsigned char)(p)[3])))) 45 46 #define DNS__SET16BIT(p, v) (((p)[0] = (unsigned char)(((v) >> 8) & 0xff)), \ 47 ((p)[1] = (unsigned char)((v) & 0xff))) 48 #define DNS__SET32BIT(p, v) (((p)[0] = (unsigned char)(((v) >> 24) & 0xff)), \ 49 ((p)[1] = (unsigned char)(((v) >> 16) & 0xff)), \ 50 ((p)[2] = (unsigned char)(((v) >> 8) & 0xff)), \ 51 ((p)[3] = (unsigned char)((v) & 0xff))) 52 53 #if 0 54 /* we cannot use this approach on systems where we can't access 16/32 bit 55 data on un-aligned addresses */ 56 #define DNS__16BIT(p) ntohs(*(unsigned short*)(p)) 57 #define DNS__32BIT(p) ntohl(*(unsigned long*)(p)) 58 #define DNS__SET16BIT(p, v) *(unsigned short*)(p) = htons(v) 59 #define DNS__SET32BIT(p, v) *(unsigned long*)(p) = htonl(v) 60 #endif 61 62 /* Macros for parsing a DNS header */ 63 #define DNS_HEADER_QID(h) DNS__16BIT(h) 64 #define DNS_HEADER_QR(h) (((h)[2] >> 7) & 0x1) 65 #define DNS_HEADER_OPCODE(h) (((h)[2] >> 3) & 0xf) 66 #define DNS_HEADER_AA(h) (((h)[2] >> 2) & 0x1) 67 #define DNS_HEADER_TC(h) (((h)[2] >> 1) & 0x1) 68 #define DNS_HEADER_RD(h) ((h)[2] & 0x1) 69 #define DNS_HEADER_RA(h) (((h)[3] >> 7) & 0x1) 70 #define DNS_HEADER_Z(h) (((h)[3] >> 4) & 0x7) 71 #define DNS_HEADER_RCODE(h) ((h)[3] & 0xf) 72 #define DNS_HEADER_QDCOUNT(h) DNS__16BIT((h) + 4) 73 #define DNS_HEADER_ANCOUNT(h) DNS__16BIT((h) + 6) 74 #define DNS_HEADER_NSCOUNT(h) DNS__16BIT((h) + 8) 75 #define DNS_HEADER_ARCOUNT(h) DNS__16BIT((h) + 10) 76 77 /* Macros for constructing a DNS header */ 78 #define DNS_HEADER_SET_QID(h, v) DNS__SET16BIT(h, v) 79 #define DNS_HEADER_SET_QR(h, v) ((h)[2] |= (unsigned char)(((v) & 0x1) << 7)) 80 #define DNS_HEADER_SET_OPCODE(h, v) ((h)[2] |= (unsigned char)(((v) & 0xf) << 3)) 81 #define DNS_HEADER_SET_AA(h, v) ((h)[2] |= (unsigned char)(((v) & 0x1) << 2)) 82 #define DNS_HEADER_SET_TC(h, v) ((h)[2] |= (unsigned char)(((v) & 0x1) << 1)) 83 #define DNS_HEADER_SET_RD(h, v) ((h)[2] |= (unsigned char)((v) & 0x1)) 84 #define DNS_HEADER_SET_RA(h, v) ((h)[3] |= (unsigned char)(((v) & 0x1) << 7)) 85 #define DNS_HEADER_SET_Z(h, v) ((h)[3] |= (unsigned char)(((v) & 0x7) << 4)) 86 #define DNS_HEADER_SET_RCODE(h, v) ((h)[3] |= (unsigned char)((v) & 0xf)) 87 #define DNS_HEADER_SET_QDCOUNT(h, v) DNS__SET16BIT((h) + 4, v) 88 #define DNS_HEADER_SET_ANCOUNT(h, v) DNS__SET16BIT((h) + 6, v) 89 #define DNS_HEADER_SET_NSCOUNT(h, v) DNS__SET16BIT((h) + 8, v) 90 #define DNS_HEADER_SET_ARCOUNT(h, v) DNS__SET16BIT((h) + 10, v) 91 92 /* Macros for parsing the fixed part of a DNS question */ 93 #define DNS_QUESTION_TYPE(q) DNS__16BIT(q) 94 #define DNS_QUESTION_CLASS(q) DNS__16BIT((q) + 2) 95 96 /* Macros for constructing the fixed part of a DNS question */ 97 #define DNS_QUESTION_SET_TYPE(q, v) DNS__SET16BIT(q, v) 98 #define DNS_QUESTION_SET_CLASS(q, v) DNS__SET16BIT((q) + 2, v) 99 100 /* Macros for parsing the fixed part of a DNS resource record */ 101 #define DNS_RR_TYPE(r) DNS__16BIT(r) 102 #define DNS_RR_CLASS(r) DNS__16BIT((r) + 2) 103 #define DNS_RR_TTL(r) DNS__32BIT((r) + 4) 104 #define DNS_RR_LEN(r) DNS__16BIT((r) + 8) 105 106 /* Macros for constructing the fixed part of a DNS resource record */ 107 #define DNS_RR_SET_TYPE(r, v) DNS__SET16BIT(r, v) 108 #define DNS_RR_SET_CLASS(r, v) DNS__SET16BIT((r) + 2, v) 109 #define DNS_RR_SET_TTL(r, v) DNS__SET32BIT((r) + 4, v) 110 #define DNS_RR_SET_LEN(r, v) DNS__SET16BIT((r) + 8, v) 111 112 #endif /* HEADER_CARES_DNS_H */ 113