ares_dns.h 4.6 KB

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