DeterministicESPAsyncWebServer v6.27.1
Zero-allocation, bounded-execution async HTTP server for ESP32
Loading...
Searching...
No Matches
dns_server.h
Go to the documentation of this file.
1// Copyright (C) 2026 Douglas Quigg (dstroy0) <dquigg123@gmail.com>
2// SPDX-License-Identifier: AGPL-3.0-or-later
3
4/**
5 * @file dns_server.h
6 * @brief Authoritative DNS server (UDP/53) - resolve local names on an offline LAN.
7 *
8 * A tiny name server for a network with no path to a real DNS: register `name -> IPv4` A
9 * records with dns_server_add() and the device answers matching A/IN queries from that fixed
10 * table (NXDOMAIN for anything else). Devices can then use `printer.lan` instead of
11 * `192.168.1.5`, a companion to the NTP server for self-hosted, offline infrastructure. Zero
12 * heap; gated by DETWS_ENABLE_DNS_SERVER.
13 *
14 * The response builder (dns_server_build_response) is pure - it parses the query and, via a
15 * resolver callback, writes the reply - so the wire format is host-tested with no lwIP.
16 * dns_server_begin() binds UDP/53 through the transport UDP service and serves the built-in
17 * table (dns_server_lookup). It is a general resolver, distinct from the provisioning
18 * captive-portal DNS (which points every name at the softAP); do not enable both.
19 *
20 * @author Douglas Quigg (dstroy0)
21 * @date 2026
22 */
23
24#ifndef DETERMINISTICESPASYNCWEBSERVER_DNS_SERVER_H
25#define DETERMINISTICESPASYNCWEBSERVER_DNS_SERVER_H
26
27#include <stddef.h>
28#include <stdint.h>
29
30/**
31 * @brief Resolve a queried name to an IPv4 address.
32 * @param name the queried name, lower/upper case as sent (match case-insensitively).
33 * @return the address in host byte order (0xC0A80105 == 192.168.1.5), or 0 for "not found".
34 */
35typedef uint32_t (*DnsResolveFn)(const char *name);
36
37/**
38 * @brief Build a DNS reply to @p query. Pure - no clock, no I/O.
39 *
40 * Parses the first question; for an A/IN query it looks the name up via @p resolve and, on a
41 * hit, appends a single A answer (compressed name pointer, @p ttl, the address). A miss on an
42 * A query returns NXDOMAIN; a non-A query returns no answer with RCODE 0. The query id and the
43 * recursion-desired bit are preserved and AA (authoritative) is set.
44 *
45 * @param query the received query bytes.
46 * @param qlen length of @p query (must be >= 12, the DNS header).
47 * @param ttl TTL (seconds) to advertise on the answer.
48 * @param resolve the name -> IPv4 resolver.
49 * @param out output buffer.
50 * @param out_cap capacity of @p out.
51 * @return response length, or 0 on a malformed query or insufficient capacity.
52 */
53size_t dns_server_build_response(const uint8_t *query, size_t qlen, uint32_t ttl, DnsResolveFn resolve, uint8_t *out,
54 size_t out_cap);
55
56/**
57 * @brief Add an A record to the built-in table (case-insensitive name).
58 * @return true if stored, false if the name is invalid or the table is full.
59 */
60bool dns_server_add(const char *name, uint8_t a, uint8_t b, uint8_t c, uint8_t d);
61
62/** @brief Look @p name up in the built-in table. @return host-order IPv4, or 0 if absent. */
63uint32_t dns_server_lookup(const char *name);
64
65/** @brief Remove every record from the built-in table. */
67
68/**
69 * @brief Start answering DNS queries on UDP/53 from the built-in table.
70 * @return true if the UDP listener bound; false on a host build or if the port is taken.
71 */
73
74#endif // DETERMINISTICESPASYNCWEBSERVER_DNS_SERVER_H
uint32_t(* DnsResolveFn)(const char *name)
Resolve a queried name to an IPv4 address.
Definition dns_server.h:35
size_t dns_server_build_response(const uint8_t *query, size_t qlen, uint32_t ttl, DnsResolveFn resolve, uint8_t *out, size_t out_cap)
Build a DNS reply to query. Pure - no clock, no I/O.
bool dns_server_add(const char *name, uint8_t a, uint8_t b, uint8_t c, uint8_t d)
Add an A record to the built-in table (case-insensitive name).
bool dns_server_begin()
Start answering DNS queries on UDP/53 from the built-in table.
uint32_t dns_server_lookup(const char *name)
Look name up in the built-in table.
void dns_server_clear()
Remove every record from the built-in table.