DNS Server

The DNS server provides a simple DNS responder that can be used to implement captive portal functionality. It responds to all DNS queries with a configured IP address, which is useful for redirecting clients to a local web server during WiFi provisioning or configuration.

The server uses UDP socket for receiving DNS queries and can be easily integrated with WiFi AP mode to create a captive portal experience.

API Reference

Header File

Classes

class DnsServer : public espp::BaseComponent

Simple DNS server for captive portal support.

This component implements a minimal DNS server that responds to all DNS queries with a configured IP address. This is useful for captive portals where you want all DNS requests to resolve to the ESP32’s IP address.

The server listens on UDP port 53 (standard DNS port) and responds to A record queries with the configured IP address.

DNS Server Example


Public Functions

explicit DnsServer(const Config &config)

Construct a new DNS Server.

Parameters:

config – Configuration for the DNS server

~DnsServer()

Destroy the DNS Server.

bool start(std::error_code &ec)

Start the DNS server.

Parameters:

ec – Error code set if start fails

Returns:

true if server started successfully, false otherwise

void stop()

Stop the DNS server.

bool is_running() const

Check if the server is running.

Returns:

true if running, false otherwise

inline const std::string &get_name() const

Get the name of the component

Note

This is the tag of the logger

Returns:

A const reference to the name of the component

inline void set_log_tag(const std::string_view &tag)

Set the tag for the logger

Parameters:

tag – The tag to use for the logger

inline espp::Logger::Verbosity get_log_level() const

Get the log level for the logger

Returns:

The verbosity level of the logger

inline void set_log_level(espp::Logger::Verbosity level)

Set the log level for the logger

Parameters:

level – The verbosity level to use for the logger

inline void set_log_verbosity(espp::Logger::Verbosity level)

Set the log verbosity for the logger

See also

set_log_level

Note

This is a convenience method that calls set_log_level

Parameters:

level – The verbosity level to use for the logger

inline espp::Logger::Verbosity get_log_verbosity() const

Get the log verbosity for the logger

See also

get_log_level

Note

This is a convenience method that calls get_log_level

Returns:

The verbosity level of the logger

inline void set_log_rate_limit(std::chrono::duration<float> rate_limit)

Set the rate limit for the logger

Note

Only calls to the logger that have _rate_limit suffix will be rate limited

Parameters:

rate_limit – The rate limit to use for the logger

struct Config

Configuration for the DNS server.

Public Members

std::string ip_address

IP address to respond with for all DNS queries

espp::Logger::Verbosity log_level = espp::Logger::Verbosity::WARN

Log verbosity