ares_dns.h 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. #ifndef HEADER_CARES_DNS_H
  2. #define HEADER_CARES_DNS_H
  3. /* Copyright 1998, 2011 by the Massachusetts Institute of Technology.
  4. *
  5. * Permission to use, copy, modify, and distribute this
  6. * software and its documentation for any purpose and without
  7. * fee is hereby granted, provided that the above copyright
  8. * notice appear in all copies and that both that copyright
  9. * notice and this permission notice appear in supporting
  10. * documentation, and that the name of M.I.T. not be used in
  11. * advertising or publicity pertaining to distribution of the
  12. * software without specific, written prior permission.
  13. * M.I.T. makes no representations about the suitability of
  14. * this software for any purpose. It is provided "as is"
  15. * without express or implied warranty.
  16. */
  17. /*
  18. * NOTE TO INTEGRATORS:
  19. *
  20. * This header is made public due to legacy projects relying on it.
  21. * Please do not use the macros within this header, or include this
  22. * header in your project as it may be removed in the future.
  23. */
  24. /*
  25. * Macro DNS__16BIT reads a network short (16 bit) given in network
  26. * byte order, and returns its value as an unsigned short.
  27. */
  28. #define DNS__16BIT(p) ((unsigned short)((unsigned int) 0xffff & \
  29. (((unsigned int)((unsigned char)(p)[0]) << 8U) | \
  30. ((unsigned int)((unsigned char)(p)[1])))))
  31. /*
  32. * Macro DNS__32BIT reads a network long (32 bit) given in network
  33. * byte order, and returns its value as an unsigned int.
  34. */
  35. #define DNS__32BIT(p) ((unsigned int) \
  36. (((unsigned int)((unsigned char)(p)[0]) << 24U) | \
  37. ((unsigned int)((unsigned char)(p)[1]) << 16U) | \
  38. ((unsigned int)((unsigned char)(p)[2]) << 8U) | \
  39. ((unsigned int)((unsigned char)(p)[3]))))
  40. #define DNS__SET16BIT(p, v) (((p)[0] = (unsigned char)(((v) >> 8) & 0xff)), \
  41. ((p)[1] = (unsigned char)((v) & 0xff)))
  42. #define DNS__SET32BIT(p, v) (((p)[0] = (unsigned char)(((v) >> 24) & 0xff)), \
  43. ((p)[1] = (unsigned char)(((v) >> 16) & 0xff)), \
  44. ((p)[2] = (unsigned char)(((v) >> 8) & 0xff)), \
  45. ((p)[3] = (unsigned char)((v) & 0xff)))
  46. #if 0
  47. /* we cannot use this approach on systems where we can't access 16/32 bit
  48. data on un-aligned addresses */
  49. #define DNS__16BIT(p) ntohs(*(unsigned short*)(p))
  50. #define DNS__32BIT(p) ntohl(*(unsigned long*)(p))
  51. #define DNS__SET16BIT(p, v) *(unsigned short*)(p) = htons(v)
  52. #define DNS__SET32BIT(p, v) *(unsigned long*)(p) = htonl(v)
  53. #endif
  54. /* Macros for parsing a DNS header */
  55. #define DNS_HEADER_QID(h) DNS__16BIT(h)
  56. #define DNS_HEADER_QR(h) (((h)[2] >> 7) & 0x1)
  57. #define DNS_HEADER_OPCODE(h) (((h)[2] >> 3) & 0xf)
  58. #define DNS_HEADER_AA(h) (((h)[2] >> 2) & 0x1)
  59. #define DNS_HEADER_TC(h) (((h)[2] >> 1) & 0x1)
  60. #define DNS_HEADER_RD(h) ((h)[2] & 0x1)
  61. #define DNS_HEADER_RA(h) (((h)[3] >> 7) & 0x1)
  62. #define DNS_HEADER_Z(h) (((h)[3] >> 4) & 0x7)
  63. #define DNS_HEADER_RCODE(h) ((h)[3] & 0xf)
  64. #define DNS_HEADER_QDCOUNT(h) DNS__16BIT((h) + 4)
  65. #define DNS_HEADER_ANCOUNT(h) DNS__16BIT((h) + 6)
  66. #define DNS_HEADER_NSCOUNT(h) DNS__16BIT((h) + 8)
  67. #define DNS_HEADER_ARCOUNT(h) DNS__16BIT((h) + 10)
  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. /* Macros for parsing the fixed part of a DNS question */
  83. #define DNS_QUESTION_TYPE(q) DNS__16BIT(q)
  84. #define DNS_QUESTION_CLASS(q) DNS__16BIT((q) + 2)
  85. /* Macros for constructing the fixed part of a DNS question */
  86. #define DNS_QUESTION_SET_TYPE(q, v) DNS__SET16BIT(q, v)
  87. #define DNS_QUESTION_SET_CLASS(q, v) DNS__SET16BIT((q) + 2, v)
  88. /* Macros for parsing the fixed part of a DNS resource record */
  89. #define DNS_RR_TYPE(r) DNS__16BIT(r)
  90. #define DNS_RR_CLASS(r) DNS__16BIT((r) + 2)
  91. #define DNS_RR_TTL(r) DNS__32BIT((r) + 4)
  92. #define DNS_RR_LEN(r) DNS__16BIT((r) + 8)
  93. /* Macros for constructing the fixed part of a DNS resource record */
  94. #define DNS_RR_SET_TYPE(r, v) DNS__SET16BIT(r, v)
  95. #define DNS_RR_SET_CLASS(r, v) DNS__SET16BIT((r) + 2, v)
  96. #define DNS_RR_SET_TTL(r, v) DNS__SET32BIT((r) + 4, v)
  97. #define DNS_RR_SET_LEN(r, v) DNS__SET16BIT((r) + 8, v)
  98. #endif /* HEADER_CARES_DNS_H */