ares_dns.h 5.7 KB

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