Refactor: dns_captive als eigenständige ESP-IDF-Komponente ausgelagert
- dns_captive.c/.h nach components/dns_captive verschoben - Neues CMakeLists.txt für Komponente ergänzt - src/CMakeLists.txt angepasst (komponentenbasiert, REQUIRES dns_captive) - Projektstruktur bereinigt und klar modularisiert
This commit is contained in:
+16
-18
@@ -1,19 +1,17 @@
|
||||
set(app_sources
|
||||
"main.c"
|
||||
"http_portal.c"
|
||||
"lv_fs_port.c"
|
||||
"net_influx.c"
|
||||
"net_mdns.c"
|
||||
"sen5x_app.c"
|
||||
"storage.c"
|
||||
"sys_status.c"
|
||||
"wifi_mgr.c"
|
||||
)
|
||||
|
||||
idf_component_register(
|
||||
SRCS
|
||||
"main.c"
|
||||
"lv_fs_port.c"
|
||||
"wifi_mgr.c"
|
||||
"http_portal.c"
|
||||
"storage.c"
|
||||
"dns_captive.c"
|
||||
"net_mdns.c"
|
||||
"net_influx.c"
|
||||
"sys_status.c"
|
||||
"sen5x_app.c"
|
||||
INCLUDE_DIRS
|
||||
"."
|
||||
REQUIRES
|
||||
driver
|
||||
freertos
|
||||
log
|
||||
)
|
||||
SRCS ${app_sources}
|
||||
INCLUDE_DIRS "."
|
||||
REQUIRES dns_captive
|
||||
)
|
||||
|
||||
@@ -1,139 +0,0 @@
|
||||
// File: dns_captive.c
|
||||
#include <string.h>
|
||||
#include <stdio.h>
|
||||
#include <inttypes.h>
|
||||
|
||||
#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");
|
||||
}
|
||||
}
|
||||
@@ -1,29 +0,0 @@
|
||||
#pragma once
|
||||
|
||||
#include "esp_err.h"
|
||||
#include "esp_netif.h"
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
/**
|
||||
* Setzt das AP-Netif, aus dem die eigene IP für DNS-Antworten gelesen wird.
|
||||
* Darf NULL sein; dann wird 192.168.4.1 als Fallback genutzt.
|
||||
*/
|
||||
void dns_captive_set_ap_netif(esp_netif_t *netif_ap);
|
||||
|
||||
/**
|
||||
* Startet den minimalistischen DNS-Server (falls noch nicht aktiv).
|
||||
* Gibt ESP_OK zurück, wenn der Task läuft oder erfolgreich gestartet wurde.
|
||||
*/
|
||||
esp_err_t dns_captive_start(void);
|
||||
|
||||
/**
|
||||
* Stoppt den DNS-Server-Task (idempotent).
|
||||
*/
|
||||
void dns_captive_stop(void);
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
@@ -19,10 +19,9 @@
|
||||
|
||||
#include "http_portal.h"
|
||||
#include "wifi_mgr.h"
|
||||
#include "storage.h" // NVS-Zugriffe
|
||||
#include "storage.h"
|
||||
#include "net_mdns.h"
|
||||
#include "sys_status.h"
|
||||
|
||||
#include "dns_captive.h"
|
||||
|
||||
// ---------- Konfig ----------
|
||||
|
||||
Reference in New Issue
Block a user