dns_server.h 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  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. #ifdef __cplusplus
  32. extern "C" {
  33. #endif
  34. /** 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
  35. * if a DNS query is too big it just wont be processed. */
  36. #define DNS_QUERY_MAX_SIZE 80
  37. /** Query + 2 byte ptr, 2 byte type, 2 byte class, 4 byte TTL, 2 byte len, 4 byte data */
  38. #define DNS_ANSWER_MAX_SIZE (DNS_QUERY_MAX_SIZE+16)
  39. /**
  40. * @brief RCODE values used in a DNS header message
  41. */
  42. typedef enum dns_reply_code_t {
  43. DNS_REPLY_CODE_NO_ERROR = 0,
  44. DNS_REPLY_CODE_FORM_ERROR = 1,
  45. DNS_REPLY_CODE_SERVER_FAILURE = 2,
  46. DNS_REPLY_CODE_NON_EXISTANT_DOMAIN = 3,
  47. DNS_REPLY_CODE_NOT_IMPLEMENTED = 4,
  48. DNS_REPLY_CODE_REFUSED = 5,
  49. DNS_REPLY_CODE_YXDOMAIN = 6,
  50. DNS_REPLY_CODE_YXRRSET = 7,
  51. DNS_REPLY_CODE_NXRRSET = 8
  52. }dns_reply_code_t;
  53. /**
  54. * @brief OPCODE values used in a DNS header message
  55. */
  56. typedef enum dns_opcode_code_t {
  57. DNS_OPCODE_QUERY = 0,
  58. DNS_OPCODE_IQUERY = 1,
  59. DNS_OPCODE_STATUS = 2
  60. }dns_opcode_code_t;
  61. /**
  62. * @brief Represents a 12 byte DNS header.
  63. * __packed__ is needed to prevent potential unwanted memory alignments
  64. */
  65. typedef struct __attribute__((__packed__)) dns_header_t{
  66. uint16_t ID; // identification number
  67. uint8_t RD : 1; // recursion desired
  68. uint8_t TC : 1; // truncated message
  69. uint8_t AA : 1; // authoritive answer
  70. uint8_t OPCode : 4; // message_type
  71. uint8_t QR : 1; // query/response flag
  72. uint8_t RCode : 4; // response code
  73. uint8_t Z : 3; // its z! reserved
  74. uint8_t RA : 1; // recursion available
  75. uint16_t QDCount; // number of question entries
  76. uint16_t ANCount; // number of answer entries
  77. uint16_t NSCount; // number of authority entries
  78. uint16_t ARCount; // number of resource entries
  79. }dns_header_t;
  80. typedef enum dns_answer_type_t {
  81. DNS_ANSWER_TYPE_A = 1,
  82. DNS_ANSWER_TYPE_NS = 2,
  83. DNS_ANSWER_TYPE_CNAME = 5,
  84. DNS_ANSWER_TYPE_SOA = 6,
  85. DNS_ANSWER_TYPE_WKS = 11,
  86. DNS_ANSWER_TYPE_PTR = 12,
  87. DNS_ANSWER_TYPE_MX = 15,
  88. DNS_ANSWER_TYPE_SRV = 33,
  89. DNS_ANSWER_TYPE_AAAA = 28
  90. }dns_answer_type_t;
  91. typedef enum dns_answer_class_t {
  92. DNS_ANSWER_CLASS_IN = 1
  93. }dns_answer_class_t;
  94. typedef struct __attribute__((__packed__)) dns_answer_t{
  95. uint16_t NAME; /* for the sake of simplicity only 16 bit pointers are supported */
  96. uint16_t TYPE; /* Unsigned 16 bit value. The resource record types - determines the content of the RDATA field. */
  97. uint16_t CLASS; /* Class of response. */
  98. uint32_t TTL; /* The time in seconds that the record may be cached. A value of 0 indicates the record should not be cached. */
  99. uint16_t RDLENGTH; /* Unsigned 16-bit value that defines the length in bytes of the RDATA record. */
  100. uint32_t RDATA; /* For the sake of simplicity only ipv4 is supported, and as such it's a unsigned 32 bit */
  101. }dns_answer_t;
  102. void dns_server(void *pvParameters);
  103. void dns_server_start();
  104. void dns_server_stop();
  105. #ifdef __cplusplus
  106. }
  107. #endif
  108. #endif /* MAIN_DNS_SERVER_H_ */