You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

dns.h 3.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  1. #ifndef _IPXE_DNS_H
  2. #define _IPXE_DNS_H
  3. /** @file
  4. *
  5. * DNS protocol
  6. *
  7. */
  8. FILE_LICENCE ( GPL2_OR_LATER );
  9. #include <stdint.h>
  10. #include <ipxe/in.h>
  11. /** DNS server port */
  12. #define DNS_PORT 53
  13. /** An RFC1035-encoded DNS name */
  14. struct dns_name {
  15. /** Start of data */
  16. void *data;
  17. /** Offset of name within data */
  18. size_t offset;
  19. /** Total length of data */
  20. size_t len;
  21. };
  22. /**
  23. * Test for a DNS compression pointer
  24. *
  25. * @v byte Initial byte
  26. * @ret is_compressed Is a compression pointer
  27. */
  28. #define DNS_IS_COMPRESSED( byte ) ( (byte) & 0xc0 )
  29. /**
  30. * Extract DNS compression pointer
  31. *
  32. * @v word Initial word
  33. * @ret offset Offset
  34. */
  35. #define DNS_COMPRESSED_OFFSET( word ) ( (word) & ~0xc000 )
  36. /**
  37. * Extract DNS label length
  38. *
  39. * @v byte Initial byte
  40. * @ret len Label length
  41. */
  42. #define DNS_LABEL_LEN( byte ) ( (byte) & ~0xc0 )
  43. /** Maximum length of a single DNS label */
  44. #define DNS_MAX_LABEL_LEN 0x3f
  45. /** Maximum length of a DNS name (mandated by RFC1035 section 2.3.4) */
  46. #define DNS_MAX_NAME_LEN 255
  47. /** Maximum depth of CNAME recursion
  48. *
  49. * This is a policy decision.
  50. */
  51. #define DNS_MAX_CNAME_RECURSION 32
  52. /** A DNS packet header */
  53. struct dns_header {
  54. /** Query identifier */
  55. uint16_t id;
  56. /** Flags */
  57. uint16_t flags;
  58. /** Number of question records */
  59. uint16_t qdcount;
  60. /** Number of answer records */
  61. uint16_t ancount;
  62. /** Number of name server records */
  63. uint16_t nscount;
  64. /** Number of additional records */
  65. uint16_t arcount;
  66. } __attribute__ (( packed ));
  67. /** Recursion desired flag */
  68. #define DNS_FLAG_RD 0x0100
  69. /** A DNS question */
  70. struct dns_question {
  71. /** Query type */
  72. uint16_t qtype;
  73. /** Query class */
  74. uint16_t qclass;
  75. } __attribute__ (( packed ));
  76. /** DNS class "IN" */
  77. #define DNS_CLASS_IN 1
  78. /** A DNS resource record */
  79. struct dns_rr_common {
  80. /** Type */
  81. uint16_t type;
  82. /** Class */
  83. uint16_t class;
  84. /** Time to live */
  85. uint32_t ttl;
  86. /** Resource data length */
  87. uint16_t rdlength;
  88. } __attribute__ (( packed ));
  89. /** Type of a DNS "A" record */
  90. #define DNS_TYPE_A 1
  91. /** A DNS "A" record */
  92. struct dns_rr_a {
  93. /** Common fields */
  94. struct dns_rr_common common;
  95. /** IPv4 address */
  96. struct in_addr in_addr;
  97. } __attribute__ (( packed ));
  98. /** Type of a DNS "AAAA" record */
  99. #define DNS_TYPE_AAAA 28
  100. /** A DNS "AAAA" record */
  101. struct dns_rr_aaaa {
  102. /** Common fields */
  103. struct dns_rr_common common;
  104. /** IPv6 address */
  105. struct in6_addr in6_addr;
  106. } __attribute__ (( packed ));
  107. /** Type of a DNS "NAME" record */
  108. #define DNS_TYPE_CNAME 5
  109. /** A DNS "CNAME" record */
  110. struct dns_rr_cname {
  111. /** Common fields */
  112. struct dns_rr_common common;
  113. } __attribute__ (( packed ));
  114. /** A DNS resource record */
  115. union dns_rr {
  116. /** Common fields */
  117. struct dns_rr_common common;
  118. /** "A" record */
  119. struct dns_rr_a a;
  120. /** "AAAA" record */
  121. struct dns_rr_aaaa aaaa;
  122. /** "CNAME" record */
  123. struct dns_rr_cname cname;
  124. };
  125. extern int dns_encode ( const char *string, struct dns_name *name );
  126. extern int dns_decode ( struct dns_name *name, char *data, size_t len );
  127. extern int dns_compare ( struct dns_name *first, struct dns_name *second );
  128. extern int dns_copy ( struct dns_name *src, struct dns_name *dst );
  129. extern int dns_skip ( struct dns_name *name );
  130. #endif /* _IPXE_DNS_H */