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 2.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. #ifndef _GPXE_DNS_H
  2. #define _GPXE_DNS_H
  3. /** @file
  4. *
  5. * DNS protocol
  6. *
  7. */
  8. #include <stdint.h>
  9. #include <gpxe/in.h>
  10. #include <gpxe/async.h>
  11. #include <gpxe/retry.h>
  12. /*
  13. * Constants
  14. *
  15. */
  16. #define DNS_TYPE_A 1
  17. #define DNS_TYPE_CNAME 5
  18. #define DNS_TYPE_ANY 255
  19. #define DNS_CLASS_IN 1
  20. #define DNS_CLASS_CS 2
  21. #define DNS_CLASS_CH 3
  22. #define DNS_CLASS_HS 4
  23. #define DNS_FLAG_QUERY ( 0x00 << 15 )
  24. #define DNS_FLAG_RESPONSE ( 0x01 << 15 )
  25. #define DNS_FLAG_QR(flags) ( (flags) & ( 0x01 << 15 ) )
  26. #define DNS_FLAG_OPCODE_QUERY ( 0x00 << 11 )
  27. #define DNS_FLAG_OPCODE_IQUERY ( 0x01 << 11 )
  28. #define DNS_FLAG_OPCODE_STATUS ( 0x02 << 11 )
  29. #define DNS_FLAG_OPCODE(flags) ( (flags) & ( 0x0f << 11 ) )
  30. #define DNS_FLAG_RD ( 0x01 << 8 )
  31. #define DNS_FLAG_RA ( 0x01 << 7 )
  32. #define DNS_FLAG_RCODE_OK ( 0x00 << 0 )
  33. #define DNS_FLAG_RCODE_NX ( 0x03 << 0 )
  34. #define DNS_FLAG_RCODE(flags) ( (flags) & ( 0x0f << 0 ) )
  35. #define DNS_PORT 53
  36. #define DNS_MAX_RETRIES 3
  37. #define DNS_MAX_CNAME_RECURSION 0x30
  38. /*
  39. * DNS protocol structures
  40. *
  41. */
  42. struct dns_header {
  43. uint16_t id;
  44. uint16_t flags;
  45. uint16_t qdcount;
  46. uint16_t ancount;
  47. uint16_t nscount;
  48. uint16_t arcount;
  49. } __attribute__ (( packed ));
  50. struct dns_query_info {
  51. uint16_t qtype;
  52. uint16_t qclass;
  53. } __attribute__ (( packed ));
  54. struct dns_query {
  55. struct dns_header dns;
  56. char payload[ 256 + sizeof ( struct dns_query_info ) ];
  57. } __attribute__ (( packed ));
  58. struct dns_rr_info_common {
  59. uint16_t type;
  60. uint16_t class;
  61. uint32_t ttl;
  62. uint16_t rdlength;
  63. } __attribute__ (( packed ));
  64. struct dns_rr_info_a {
  65. struct dns_rr_info_common common;
  66. struct in_addr in_addr;
  67. } __attribute__ (( packed ));
  68. struct dns_rr_info_cname {
  69. struct dns_rr_info_common common;
  70. char cname[0];
  71. } __attribute__ (( packed ));
  72. union dns_rr_info {
  73. struct dns_rr_info_common common;
  74. struct dns_rr_info_a a;
  75. struct dns_rr_info_cname cname;
  76. };
  77. /** A DNS request */
  78. struct dns_request {
  79. /** Socket address to fill in with resolved address */
  80. struct sockaddr *sa;
  81. /** Current query packet */
  82. struct dns_query query;
  83. /** Length of current query packet */
  84. struct dns_query_info *qinfo;
  85. /** Recursion counter */
  86. unsigned int recursion;
  87. /** Asynchronous operation */
  88. struct async async;
  89. /** UDP connection */
  90. struct udp_connection udp;
  91. /** Retry timer */
  92. struct retry_timer timer;
  93. };
  94. extern int dns_resolv ( const char *name, struct sockaddr *sa,
  95. struct async *parent );
  96. #endif /* _GPXE_DNS_H */