dns_server.h 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. /*
  2. Copyright (c) 2019 Tony Pottier
  3. Permission is hereby granted, free of charge, to any person obtaining a copy
  4. of this software and associated documentation files (the "Software"), to deal
  5. in the Software without restriction, including without limitation the rights
  6. to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  7. copies of the Software, and to permit persons to whom the Software is
  8. furnished to do so, subject to the following conditions:
  9. The above copyright notice and this permission notice shall be included in all
  10. copies or substantial portions of the Software.
  11. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  12. IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  13. FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  14. AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  15. LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  16. OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  17. SOFTWARE.
  18. @file dns_server.h
  19. @author Tony Pottier
  20. @brief Defines an extremly basic DNS server for captive portal functionality.
  21. Contains the freeRTOS task for the DNS server that processes the requests.
  22. @see https://idyl.io
  23. @see https://github.com/tonyp7/esp32-wifi-manager
  24. @see http://www.zytrax.com/books/dns/ch15
  25. */
  26. #ifndef MAIN_DNS_SERVER_H_
  27. #define MAIN_DNS_SERVER_H_
  28. #include <esp_system.h>
  29. #include <stdbool.h>
  30. #include "squeezelite-ota.h"
  31. #include "esp_netif.h"
  32. #ifdef __cplusplus
  33. extern "C" {
  34. #endif
  35. /** 12 byte header, 64 byte domain name, 4 byte qtype/qclass. This NOT compliant with the RFC, but it's good enough for a captive portal
  36. * if a DNS query is too big it just wont be processed. */
  37. #define DNS_QUERY_MAX_SIZE 80
  38. /** Query + 2 byte ptr, 2 byte type, 2 byte class, 4 byte TTL, 2 byte len, 4 byte data */
  39. #define DNS_ANSWER_MAX_SIZE (DNS_QUERY_MAX_SIZE+16)
  40. /**
  41. * @brief RCODE values used in a DNS header message
  42. */
  43. typedef enum dns_reply_code_t {
  44. DNS_REPLY_CODE_NO_ERROR = 0,
  45. DNS_REPLY_CODE_FORM_ERROR = 1,
  46. DNS_REPLY_CODE_SERVER_FAILURE = 2,
  47. DNS_REPLY_CODE_NON_EXISTANT_DOMAIN = 3,
  48. DNS_REPLY_CODE_NOT_IMPLEMENTED = 4,
  49. DNS_REPLY_CODE_REFUSED = 5,
  50. DNS_REPLY_CODE_YXDOMAIN = 6,
  51. DNS_REPLY_CODE_YXRRSET = 7,
  52. DNS_REPLY_CODE_NXRRSET = 8
  53. }dns_reply_code_t;
  54. /**
  55. * @brief OPCODE values used in a DNS header message
  56. */
  57. typedef enum dns_opcode_code_t {
  58. DNS_OPCODE_QUERY = 0,
  59. DNS_OPCODE_IQUERY = 1,
  60. DNS_OPCODE_STATUS = 2
  61. }dns_opcode_code_t;
  62. /**
  63. * @brief Represents a 12 byte DNS header.
  64. * __packed__ is needed to prevent potential unwanted memory alignments
  65. */
  66. typedef struct __attribute__((__packed__)) dns_header_t{
  67. uint16_t ID; // identification number
  68. uint8_t RD : 1; // recursion desired
  69. uint8_t TC : 1; // truncated message
  70. uint8_t AA : 1; // authoritive answer
  71. uint8_t OPCode : 4; // message_type
  72. uint8_t QR : 1; // query/response flag
  73. uint8_t RCode : 4; // response code
  74. uint8_t Z : 3; // its z! reserved
  75. uint8_t RA : 1; // recursion available
  76. uint16_t QDCount; // number of question entries
  77. uint16_t ANCount; // number of answer entries
  78. uint16_t NSCount; // number of authority entries
  79. uint16_t ARCount; // number of resource entries
  80. }dns_header_t;
  81. typedef enum dns_answer_type_t {
  82. DNS_ANSWER_TYPE_A = 1,
  83. DNS_ANSWER_TYPE_NS = 2,
  84. DNS_ANSWER_TYPE_CNAME = 5,
  85. DNS_ANSWER_TYPE_SOA = 6,
  86. DNS_ANSWER_TYPE_WKS = 11,
  87. DNS_ANSWER_TYPE_PTR = 12,
  88. DNS_ANSWER_TYPE_MX = 15,
  89. DNS_ANSWER_TYPE_SRV = 33,
  90. DNS_ANSWER_TYPE_AAAA = 28
  91. }dns_answer_type_t;
  92. typedef enum dns_answer_class_t {
  93. DNS_ANSWER_CLASS_IN = 1
  94. }dns_answer_class_t;
  95. typedef struct __attribute__((__packed__)) dns_answer_t{
  96. uint16_t NAME; /* for the sake of simplicity only 16 bit pointers are supported */
  97. uint16_t TYPE; /* Unsigned 16 bit value. The resource record types - determines the content of the RDATA field. */
  98. uint16_t CLASS; /* Class of response. */
  99. uint32_t TTL; /* The time in seconds that the record may be cached. A value of 0 indicates the record should not be cached. */
  100. uint16_t RDLENGTH; /* Unsigned 16-bit value that defines the length in bytes of the RDATA record. */
  101. uint32_t RDATA; /* For the sake of simplicity only ipv4 is supported, and as such it's a unsigned 32 bit */
  102. }dns_answer_t;
  103. void dns_server(void *pvParameters);
  104. void dns_server_start(esp_netif_t * netif);
  105. void dns_server_stop();
  106. #ifdef __cplusplus
  107. }
  108. #endif
  109. #endif /* MAIN_DNS_SERVER_H_ */