Files
MimiCon/main/dns_server.c
T
dark98 69ebdb4331 Initial commit: MimiCon v1.0.0 - Universal Gamepad Mimic & Console Bridge
- Dual-Core Architecture: Dedicated 1000Hz USB gaming pipeline on Core 1 and BLE/Wi-Fi/Web telemetry on Core 0.
- Multi-Input BLE Host: Parsers for Xbox Wireless, Google Stadia, 8BitDo (Ultimate/Pro2/SN30), and Generic HOGP gamepads.
- Multi-Console USB Emulation: Nintendo Switch, PC DirectInput, PS3, PS4, Xbox 360 (libxsm3 cryptographic challenge-response), Original Xbox, and Couch Mouse/Keyboard.
- Onboard Web Portal & Captive Portal: Real-time SVG gamepad visualizer, stick deadzone calibration, button combo remapping, and Wi-Fi manager.
- Low-Latency Radio Management: Interactive SoftAP toggle to drop to 0-latency gaming mode.
- Dual OTA Firmware Flashing: Browser-based wireless firmware updater and automated Python OTA script.
- Strict Build Quality: Clean build under -Wall -Wextra with zero warnings or errors.
- Versioning & Licensing: Semantic Versioning v1.0.0 and GNU GPLv3 with Commercial Restrictions.
2026-10-17 20:21:09 +01:00

152 lines
4.5 KiB
C

#include "dns_server.h"
#include <string.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include "esp_log.h"
#include "freertos/FreeRTOS.h"
#include "freertos/task.h"
static const char *TAG = "DNS_SERVER";
#define DNS_PORT 53
#define DNS_MAX_LEN 512
static int s_dns_sock = -1;
static TaskHandle_t s_dns_task_handle = NULL;
static volatile bool s_dns_running = false;
static void dns_server_task(void *pvParameters)
{
uint8_t rx_buffer[DNS_MAX_LEN];
uint8_t tx_buffer[DNS_MAX_LEN];
struct sockaddr_in client_addr;
socklen_t client_addr_len = sizeof(client_addr);
ESP_LOGI(TAG, "DNS Captive Portal Server listening on UDP port %d", DNS_PORT);
while (s_dns_running && s_dns_sock >= 0) {
int len = recvfrom(s_dns_sock, rx_buffer, sizeof(rx_buffer), 0,
(struct sockaddr *)&client_addr, &client_addr_len);
if (len < 12) {
continue;
}
/* Check if it's a standard query (QR=0, Opcode=0) */
uint16_t flags = (rx_buffer[2] << 8) | rx_buffer[3];
if ((flags & 0x8000) != 0) {
continue; /* Not a query */
}
/* Construct response */
memcpy(tx_buffer, rx_buffer, len);
/* Set flags: Response, Authoritative, Recursion Available */
tx_buffer[2] = 0x81;
tx_buffer[3] = 0x80;
/* Answers count = 1 */
tx_buffer[6] = 0x00;
tx_buffer[7] = 0x01;
/* Authority RRs = 0, Additional RRs = 0 */
tx_buffer[8] = 0x00;
tx_buffer[9] = 0x00;
tx_buffer[10] = 0x00;
tx_buffer[11] = 0x00;
/* Append Answer record right after the query section */
int resp_len = len;
if (resp_len + 16 <= DNS_MAX_LEN) {
/* Name pointer to offset 12 (first label) */
tx_buffer[resp_len++] = 0xC0;
tx_buffer[resp_len++] = 0x0C;
/* Type A (0x0001) */
tx_buffer[resp_len++] = 0x00;
tx_buffer[resp_len++] = 0x01;
/* Class IN (0x0001) */
tx_buffer[resp_len++] = 0x00;
tx_buffer[resp_len++] = 0x01;
/* TTL: 60 seconds (0x0000003C) */
tx_buffer[resp_len++] = 0x00;
tx_buffer[resp_len++] = 0x00;
tx_buffer[resp_len++] = 0x00;
tx_buffer[resp_len++] = 0x3C;
/* Data length: 4 */
tx_buffer[resp_len++] = 0x00;
tx_buffer[resp_len++] = 0x04;
/* IP: 192.168.4.1 */
tx_buffer[resp_len++] = 192;
tx_buffer[resp_len++] = 168;
tx_buffer[resp_len++] = 4;
tx_buffer[resp_len++] = 1;
sendto(s_dns_sock, tx_buffer, resp_len, 0,
(struct sockaddr *)&client_addr, client_addr_len);
}
}
if (s_dns_sock >= 0) {
close(s_dns_sock);
s_dns_sock = -1;
}
s_dns_task_handle = NULL;
vTaskDelete(NULL);
}
esp_err_t dns_server_start(void)
{
if (s_dns_running) {
return ESP_OK;
}
struct sockaddr_in serv_addr;
memset(&serv_addr, 0, sizeof(serv_addr));
serv_addr.sin_family = AF_INET;
serv_addr.sin_addr.s_addr = htonl(INADDR_ANY);
serv_addr.sin_port = htons(DNS_PORT);
s_dns_sock = socket(AF_INET, SOCK_DGRAM, IPPROTO_IP);
if (s_dns_sock < 0) {
ESP_LOGE(TAG, "Unable to create DNS socket: errno %d", errno);
return ESP_FAIL;
}
/* Set socket receive timeout so task can exit cleanly */
struct timeval timeout = {
.tv_sec = 1,
.tv_usec = 0,
};
setsockopt(s_dns_sock, SOL_SOCKET, SO_RCVTIMEO, &timeout, sizeof(timeout));
int err = bind(s_dns_sock, (struct sockaddr *)&serv_addr, sizeof(serv_addr));
if (err < 0) {
ESP_LOGE(TAG, "DNS socket unable to bind: errno %d", errno);
close(s_dns_sock);
s_dns_sock = -1;
return ESP_FAIL;
}
/* Pin captive portal DNS responder to Core 0 (alongside network stack) */
BaseType_t ret = xTaskCreatePinnedToCore(dns_server_task, "dns_server_task", 3072, NULL, 5, &s_dns_task_handle, 0);
if (ret != pdPASS) {
ESP_LOGE(TAG, "Failed to create DNS task");
close(s_dns_sock);
s_dns_sock = -1;
s_dns_running = false;
return ESP_FAIL;
}
return ESP_OK;
}
void dns_server_stop(void)
{
if (!s_dns_running) {
return;
}
ESP_LOGI(TAG, "Stopping DNS Captive Portal server...");
s_dns_running = false;
/* Socket timeout will trigger task to exit and close socket */
}