DeterministicESPAsyncWebServer v6.27.1
Zero-allocation, bounded-execution async HTTP server for ESP32
Loading...
Searching...
No Matches
dns_server.h File Reference

Authoritative DNS server (UDP/53) - resolve local names on an offline LAN. More...

#include <stddef.h>
#include <stdint.h>

Go to the source code of this file.

Typedefs

typedef uint32_t(* DnsResolveFn) (const char *name)
 Resolve a queried name to an IPv4 address.
 

Functions

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).
 
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.
 
bool dns_server_begin ()
 Start answering DNS queries on UDP/53 from the built-in table.
 

Detailed Description

Authoritative DNS server (UDP/53) - resolve local names on an offline LAN.

A tiny name server for a network with no path to a real DNS: register name -> IPv4 A records with dns_server_add() and the device answers matching A/IN queries from that fixed table (NXDOMAIN for anything else). Devices can then use printer.lan instead of 192.168.1.5, a companion to the NTP server for self-hosted, offline infrastructure. Zero heap; gated by DETWS_ENABLE_DNS_SERVER.

The response builder (dns_server_build_response) is pure - it parses the query and, via a resolver callback, writes the reply - so the wire format is host-tested with no lwIP. dns_server_begin() binds UDP/53 through the transport UDP service and serves the built-in table (dns_server_lookup). It is a general resolver, distinct from the provisioning captive-portal DNS (which points every name at the softAP); do not enable both.

Author
Douglas Quigg (dstroy0)
Date
2026

Definition in file dns_server.h.

Typedef Documentation

◆ DnsResolveFn

typedef uint32_t(* DnsResolveFn) (const char *name)

Resolve a queried name to an IPv4 address.

Parameters
namethe queried name, lower/upper case as sent (match case-insensitively).
Returns
the address in host byte order (0xC0A80105 == 192.168.1.5), or 0 for "not found".

Definition at line 35 of file dns_server.h.

Function Documentation

◆ dns_server_build_response()

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.

Parses the first question; for an A/IN query it looks the name up via resolve and, on a hit, appends a single A answer (compressed name pointer, ttl, the address). A miss on an A query returns NXDOMAIN; a non-A query returns no answer with RCODE 0. The query id and the recursion-desired bit are preserved and AA (authoritative) is set.

Parameters
querythe received query bytes.
qlenlength of query (must be >= 12, the DNS header).
ttlTTL (seconds) to advertise on the answer.
resolvethe name -> IPv4 resolver.
outoutput buffer.
out_capcapacity of out.
Returns
response length, or 0 on a malformed query or insufficient capacity.

◆ dns_server_add()

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).

Returns
true if stored, false if the name is invalid or the table is full.

◆ dns_server_lookup()

uint32_t dns_server_lookup ( const char *  name)

Look name up in the built-in table.

Returns
host-order IPv4, or 0 if absent.

◆ dns_server_clear()

void dns_server_clear ( )

Remove every record from the built-in table.

◆ dns_server_begin()

bool dns_server_begin ( )

Start answering DNS queries on UDP/53 from the built-in table.

Returns
true if the UDP listener bound; false on a host build or if the port is taken.