dns_server.c 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  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 "network_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(esp_netif_t * netif) {
  51. xTaskCreate(&dns_server, "dns_server", 3072, (void *)netif, 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. esp_err_t esp_err = ESP_OK;
  63. esp_netif_t * netif = (esp_netif_t * )pvParameters;
  64. /* Set redirection DNS hijack to the access point IP */
  65. ip4_addr_t ip_resolved;
  66. inet_pton(AF_INET, DEFAULT_AP_IP, &ip_resolved);
  67. /* Create UDP socket */
  68. socket_fd = socket(AF_INET, SOCK_DGRAM, 0);
  69. if (socket_fd < 0){
  70. ESP_LOGE(TAG, "Failed to create socket");
  71. exit(0);
  72. }
  73. memset(&sa, 0, sizeof(struct sockaddr_in));
  74. /* Bind to port 53 (typical DNS Server port) */
  75. esp_netif_ip_info_t ip_info;
  76. esp_err = esp_netif_get_ip_info(netif,&ip_info);
  77. if(esp_err!=ESP_OK) {
  78. ESP_LOGE(TAG, "Failed to get adapter info for udp: %s", esp_err_to_name(esp_err));
  79. exit(1);
  80. }
  81. ra.sin_family = AF_INET;
  82. ra.sin_addr.s_addr = ip_info.ip.addr;
  83. ra.sin_port = htons(53);
  84. if (bind(socket_fd, (struct sockaddr *)&ra, sizeof(struct sockaddr_in)) == -1) {
  85. ESP_LOGE(TAG, "Failed to bind to 53/udp");
  86. close(socket_fd);
  87. exit(1);
  88. }
  89. struct sockaddr_in client;
  90. socklen_t client_len;
  91. client_len = sizeof(client);
  92. int length;
  93. uint8_t data[DNS_QUERY_MAX_SIZE]; /* dns query buffer */
  94. uint8_t response[DNS_ANSWER_MAX_SIZE]; /* dns response buffer */
  95. char ip_address[INET_ADDRSTRLEN]; /* buffer to store IPs as text. This is only used for debug and serves no other purpose */
  96. char *domain; /* This is only used for debug and serves no other purpose */
  97. int err;
  98. ESP_LOGI(TAG, "DNS Server listening on 53/udp");
  99. /* Start loop to process DNS requests */
  100. for(;;) {
  101. memset(data, 0x00, sizeof(data)); /* reset buffer */
  102. length = recvfrom(socket_fd, data, sizeof(data), 0, (struct sockaddr *)&client, &client_len); /* read udp request */
  103. /*if the query is bigger than the buffer size we simply ignore it. This case should only happen in case of multiple
  104. * queries within the same DNS packet and is not supported by this simple DNS hijack. */
  105. if ( length > 0 && ((length + sizeof(dns_answer_t)-1) < DNS_ANSWER_MAX_SIZE) ) {
  106. data[length] = '\0'; /*in case there's a bogus domain name that isn't null terminated */
  107. /* Generate header message */
  108. memcpy(response, data, sizeof(dns_header_t));
  109. dns_header_t *dns_header = (dns_header_t*)response;
  110. dns_header->QR = 1; /*response bit */
  111. dns_header->OPCode = DNS_OPCODE_QUERY; /* no support for other type of response */
  112. dns_header->AA = 1; /*authoritative answer */
  113. dns_header->RCode = DNS_REPLY_CODE_NO_ERROR; /* no error */
  114. dns_header->TC = 0; /*no truncation */
  115. dns_header->RD = 0; /*no recursion */
  116. dns_header->ANCount = dns_header->QDCount; /* set answer count = question count -- duhh! */
  117. dns_header->NSCount = 0x0000; /* name server resource records = 0 */
  118. dns_header->ARCount = 0x0000; /* resource records = 0 */
  119. /* copy the rest of the query in the response */
  120. memcpy(response + sizeof(dns_header_t), data + sizeof(dns_header_t), length - sizeof(dns_header_t));
  121. /* extract domain name and request IP for debug */
  122. inet_ntop(AF_INET, &(client.sin_addr), ip_address, INET_ADDRSTRLEN);
  123. domain = (char*) &data[sizeof(dns_header_t) + 1];
  124. for(char* c=domain; *c != '\0'; c++){
  125. 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 */
  126. }
  127. ESP_LOGD(TAG, "Replying to DNS request for %s from %s", domain, ip_address);
  128. /* create DNS answer at the end of the query*/
  129. dns_answer_t *dns_answer = (dns_answer_t*)&response[length];
  130. 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 */
  131. dns_answer->TYPE = __bswap_16(DNS_ANSWER_TYPE_A);
  132. dns_answer->CLASS = __bswap_16(DNS_ANSWER_CLASS_IN);
  133. dns_answer->TTL = (uint32_t)0x00000000; /* no caching. Avoids DNS poisoning since this is a DNS hijack */
  134. dns_answer->RDLENGTH = __bswap_16(0x0004); /* 4 byte => size of an ipv4 address */
  135. dns_answer->RDATA = ip_resolved.addr;
  136. err = sendto(socket_fd, response, length+sizeof(dns_answer_t), 0, (struct sockaddr *)&client, client_len);
  137. if (err < 0) {
  138. ESP_LOGE(TAG, "UDP sendto failed: %d", err);
  139. }
  140. }
  141. taskYIELD(); /* allows the freeRTOS scheduler to take over if needed. DNS daemon should not be taxing on the system */
  142. }
  143. close(socket_fd);
  144. vTaskDelete ( NULL );
  145. }