dns_server.c 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  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.c
  19. @author Tony Pottier
  20. @brief Defines an extremely basic DNS server for captive portal functionality.
  21. It's basically a DNS hijack that replies to the esp's address no matter which
  22. request is sent to it.
  23. Contains the freeRTOS task for the DNS server that processes the requests.
  24. @see https://idyl.io
  25. @see https://github.com/tonyp7/esp32-wifi-manager
  26. */
  27. #include "dns_server.h"
  28. #include <lwip/sockets.h>
  29. #include <string.h>
  30. #include <freertos/FreeRTOS.h>
  31. #include <freertos/task.h>
  32. #include <freertos/event_groups.h>
  33. #include <esp_system.h>
  34. #include <esp_wifi.h>
  35. #include <esp_event.h>
  36. #include <esp_log.h>
  37. #include <esp_err.h>
  38. #include <nvs_flash.h>
  39. #include <lwip/err.h>
  40. #include <lwip/sockets.h>
  41. #include <lwip/sys.h>
  42. #include <lwip/netdb.h>
  43. #include <lwip/dns.h>
  44. #include <byteswap.h>
  45. #include "squeezelite-ota.h"
  46. #include "wifi_manager.h"
  47. static const char TAG[] = "dns_server";
  48. static TaskHandle_t task_dns_server = NULL;
  49. int socket_fd;
  50. void dns_server_start() {
  51. xTaskCreate(&dns_server, "dns_server", 3072, NULL, WIFI_MANAGER_TASK_PRIORITY-1, &task_dns_server);
  52. }
  53. void dns_server_stop(){
  54. if(task_dns_server){
  55. vTaskDelete(task_dns_server);
  56. close(socket_fd);
  57. task_dns_server = NULL;
  58. }
  59. }
  60. void dns_server(void *pvParameters) {
  61. struct sockaddr_in sa, ra;
  62. /* Set redirection DNS hijack to the access point IP */
  63. ip4_addr_t ip_resolved;
  64. inet_pton(AF_INET, DEFAULT_AP_IP, &ip_resolved);
  65. /* Create UDP socket */
  66. socket_fd = socket(AF_INET, SOCK_DGRAM, 0);
  67. if (socket_fd < 0){
  68. ESP_LOGE(TAG, "Failed to create socket");
  69. exit(0);
  70. }
  71. memset(&sa, 0, sizeof(struct sockaddr_in));
  72. /* Bind to port 53 (typical DNS Server port) */
  73. tcpip_adapter_ip_info_t ip;
  74. tcpip_adapter_get_ip_info(TCPIP_ADAPTER_IF_STA, &ip);
  75. ra.sin_family = AF_INET;
  76. ra.sin_addr.s_addr = ip.ip.addr;
  77. ra.sin_port = htons(53);
  78. if (bind(socket_fd, (struct sockaddr *)&ra, sizeof(struct sockaddr_in)) == -1) {
  79. ESP_LOGE(TAG, "Failed to bind to 53/udp");
  80. close(socket_fd);
  81. exit(1);
  82. }
  83. struct sockaddr_in client;
  84. socklen_t client_len;
  85. client_len = sizeof(client);
  86. int length;
  87. uint8_t data[DNS_QUERY_MAX_SIZE]; /* dns query buffer */
  88. uint8_t response[DNS_ANSWER_MAX_SIZE]; /* dns response buffer */
  89. char ip_address[INET_ADDRSTRLEN]; /* buffer to store IPs as text. This is only used for debug and serves no other purpose */
  90. char *domain; /* This is only used for debug and serves no other purpose */
  91. int err;
  92. ESP_LOGI(TAG, "DNS Server listening on 53/udp");
  93. /* Start loop to process DNS requests */
  94. for(;;) {
  95. memset(data, 0x00, sizeof(data)); /* reset buffer */
  96. length = recvfrom(socket_fd, data, sizeof(data), 0, (struct sockaddr *)&client, &client_len); /* read udp request */
  97. /*if the query is bigger than the buffer size we simply ignore it. This case should only happen in case of multiple
  98. * queries within the same DNS packet and is not supported by this simple DNS hijack. */
  99. if ( length > 0 && ((length + sizeof(dns_answer_t)-1) < DNS_ANSWER_MAX_SIZE) ) {
  100. data[length] = '\0'; /*in case there's a bogus domain name that isn't null terminated */
  101. /* Generate header message */
  102. memcpy(response, data, sizeof(dns_header_t));
  103. dns_header_t *dns_header = (dns_header_t*)response;
  104. dns_header->QR = 1; /*response bit */
  105. dns_header->OPCode = DNS_OPCODE_QUERY; /* no support for other type of response */
  106. dns_header->AA = 1; /*authoritative answer */
  107. dns_header->RCode = DNS_REPLY_CODE_NO_ERROR; /* no error */
  108. dns_header->TC = 0; /*no truncation */
  109. dns_header->RD = 0; /*no recursion */
  110. dns_header->ANCount = dns_header->QDCount; /* set answer count = question count -- duhh! */
  111. dns_header->NSCount = 0x0000; /* name server resource records = 0 */
  112. dns_header->ARCount = 0x0000; /* resource records = 0 */
  113. /* copy the rest of the query in the response */
  114. memcpy(response + sizeof(dns_header_t), data + sizeof(dns_header_t), length - sizeof(dns_header_t));
  115. /* extract domain name and request IP for debug */
  116. inet_ntop(AF_INET, &(client.sin_addr), ip_address, INET_ADDRSTRLEN);
  117. domain = (char*) &data[sizeof(dns_header_t) + 1];
  118. for(char* c=domain; *c != '\0'; c++){
  119. if(*c < ' ' || *c > 'z') *c = '.'; /* technically we should test if the first two bits are 00 (e.g. if( (*c & 0xC0) == 0x00) *c = '.') but this makes the code a lot more readable */
  120. }
  121. ESP_LOGI(TAG, "Replying to DNS request for %s from %s", domain, ip_address);
  122. /* create DNS answer at the end of the query*/
  123. dns_answer_t *dns_answer = (dns_answer_t*)&response[length];
  124. dns_answer->NAME = __bswap_16(0xC00C); /* This is a pointer to the beginning of the question. As per DNS standard, first two bits must be set to 11 for some odd reason hence 0xC0 */
  125. dns_answer->TYPE = __bswap_16(DNS_ANSWER_TYPE_A);
  126. dns_answer->CLASS = __bswap_16(DNS_ANSWER_CLASS_IN);
  127. dns_answer->TTL = (uint32_t)0x00000000; /* no caching. Avoids DNS poisoning since this is a DNS hijack */
  128. dns_answer->RDLENGTH = __bswap_16(0x0004); /* 4 byte => size of an ipv4 address */
  129. dns_answer->RDATA = ip_resolved.addr;
  130. err = sendto(socket_fd, response, length+sizeof(dns_answer_t), 0, (struct sockaddr *)&client, client_len);
  131. if (err < 0) {
  132. ESP_LOGE(TAG, "UDP sendto failed: %d", err);
  133. }
  134. }
  135. taskYIELD(); /* allows the freeRTOS scheduler to take over if needed. DNS daemon should not be taxing on the system */
  136. }
  137. close(socket_fd);
  138. vTaskDelete ( NULL );
  139. }