-
Notifications
You must be signed in to change notification settings - Fork 25
Expand file tree
/
Copy pathdns_server_example.cpp
More file actions
71 lines (56 loc) · 1.98 KB
/
dns_server_example.cpp
File metadata and controls
71 lines (56 loc) · 1.98 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
#include <chrono>
#include <system_error>
#include <vector>
#include "dns_server.hpp"
#include "logger.hpp"
#include "wifi.hpp"
using namespace std::chrono_literals;
extern "C" void app_main(void) {
espp::Logger logger({.tag = "DNS Server Example", .level = espp::Logger::Verbosity::INFO});
logger.info("Starting DNS Server Example");
#if CONFIG_ESP32_WIFI_NVS_ENABLED
// Initialize NVS
esp_err_t ret = nvs_flash_init();
if (ret == ESP_ERR_NVS_NO_FREE_PAGES || ret == ESP_ERR_NVS_NEW_VERSION_FOUND) {
ESP_ERROR_CHECK(nvs_flash_erase());
ret = nvs_flash_init();
}
ESP_ERROR_CHECK(ret);
#endif
// Initialize WiFi in AP mode
std::string ap_ssid = "ESP-DNS-Test";
std::string ap_password = "testpassword";
logger.info("Starting WiFi AP: {}", ap_ssid);
espp::WifiAp ap{espp::WifiAp::Config{
.ssid = ap_ssid,
.password = ap_password,
.channel = 1,
.max_number_of_stations = 4,
}};
logger.info("WiFi AP started successfully");
logger.info("Connect to SSID: {} with password: {}", ap_ssid, ap_password);
// Get the AP IP address
std::string ap_ip = ap.get_ip_address();
logger.info("AP IP Address: {}", ap_ip);
// Create and start DNS server
logger.info("Starting DNS server on {}:53", ap_ip);
espp::DnsServer::Config dns_config{.ip_address = ap_ip,
.log_level = espp::Logger::Verbosity::INFO};
espp::DnsServer dns_server(dns_config);
std::error_code ec;
if (!dns_server.start(ec)) {
logger.error("Failed to start DNS server: {}", ec.message());
return;
}
logger.info("DNS server started successfully");
logger.info("All DNS queries will resolve to: {}", ap_ip);
logger.info("");
logger.info("To test:");
logger.info("1. Connect your device to WiFi network '{}'", ap_ssid);
logger.info("2. Try pinging any domain (e.g., ping google.com)");
logger.info("3. All domains should resolve to {}", ap_ip);
// Run forever
while (true) {
std::this_thread::sleep_for(1s);
}
}