// File: dns_captive.c #include #include #include #include "freertos/FreeRTOS.h" #include "freertos/task.h" #include "esp_log.h" #include "esp_err.h" #include "esp_netif.h" #include "lwip/udp.h" #include "lwip/sockets.h" #include "lwip/netdb.h" #include "dns_captive.h" static const char *TAG = "dns_captive"; // ---------- State ---------- static TaskHandle_t s_dns_task_handle = NULL; static esp_netif_t *s_netif_ap = NULL; // ---------- DNS Paketlayout ---------- typedef struct __attribute__((packed)) { uint16_t id, flags, qdcount, ancount, nscount, arcount; } dns_hdr_t; // ---------- Internals ---------- static void dns_task(void *arg) { (void)arg; int sock = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP); if (sock < 0) { ESP_LOGE(TAG, "socket() fehlgeschlagen"); vTaskDelete(NULL); return; } struct sockaddr_in addr = {0}; addr.sin_family = AF_INET; addr.sin_port = htons(53); addr.sin_addr.s_addr = htonl(INADDR_ANY); if (bind(sock, (struct sockaddr*)&addr, sizeof(addr)) < 0) { ESP_LOGE(TAG, "bind(:53) fehlgeschlagen"); close(sock); vTaskDelete(NULL); return; } ESP_LOGI(TAG, "DNS Server läuft auf :53"); for (;;) { uint8_t buf[512]; struct sockaddr_in from; socklen_t fromlen = sizeof(from); int n = recvfrom(sock, buf, sizeof(buf), 0, (struct sockaddr*)&from, &fromlen); if (n <= (int)sizeof(dns_hdr_t)) continue; dns_hdr_t *h = (dns_hdr_t*)buf; uint16_t id = ntohs(h->id); uint16_t qd = ntohs(h->qdcount); if (qd == 0) continue; int pos = sizeof(dns_hdr_t); for (int qi = 0; qi < qd; ++qi) { while (pos < n && buf[pos] != 0) { uint8_t l = buf[pos]; pos += 1 + l; } pos += 1; // null label pos += 4; // type + class } if (pos + 16 > (int)sizeof(buf)) continue; // Ziel-IP: AP-IP falls vorhanden, sonst 192.168.4.1 ip4_addr_t ip = { .addr = 0 }; if (s_netif_ap) { esp_netif_ip_info_t ipi; if (esp_netif_get_ip_info(s_netif_ap, &ipi) == ESP_OK) { ip.addr = ipi.ip.addr; // already network order } } if (ip.addr == 0) { struct in_addr tmp; inet_aton("192.168.4.1", &tmp); ip.addr = tmp.s_addr; } uint8_t out[512]; memcpy(out, buf, pos); dns_hdr_t *oh = (dns_hdr_t*)out; oh->id = htons(id); oh->flags = htons(0x8180); // Standard response / no error oh->qdcount = htons(qd); oh->ancount = htons(1); oh->nscount = 0; oh->arcount = 0; int opos = pos; // Name-Pointer auf erste Frage (0x000c), A-Record, IN, TTL=30, RDLEN=4 out[opos++] = 0xC0; out[opos++] = 0x0C; out[opos++] = 0x00; out[opos++] = 0x01; out[opos++] = 0x00; out[opos++] = 0x01; out[opos++] = 0x00; out[opos++] = 0x00; out[opos++] = 0x00; out[opos++] = 0x1E; out[opos++] = 0x00; out[opos++] = 0x04; uint32_t ipbe = ip.addr; memcpy(&out[opos], &ipbe, 4); opos += 4; (void)sendto(sock, out, opos, 0, (struct sockaddr*)&from, fromlen); } } // ---------- Public API ---------- void dns_captive_set_ap_netif(esp_netif_t *netif_ap) { s_netif_ap = netif_ap; } esp_err_t dns_captive_start(void) { if (s_dns_task_handle) { return ESP_OK; } BaseType_t ok = xTaskCreatePinnedToCore(dns_task, "dns", 4096, NULL, 4, &s_dns_task_handle, 0); if (ok != pdPASS) { s_dns_task_handle = NULL; return ESP_FAIL; } return ESP_OK; } void dns_captive_stop(void) { if (s_dns_task_handle) { vTaskDelete(s_dns_task_handle); s_dns_task_handle = NULL; ESP_LOGI(TAG, "DNS Server gestoppt"); } }