DNS setting with 'IPAddress4/5' not persisted (#23426)

This commit is contained in:
s-hadinger 2025-05-15 23:25:32 +02:00 committed by GitHub
parent aff2157356
commit e86af24056
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 55 additions and 27 deletions

View File

@ -18,6 +18,7 @@ All notable changes to this project will be documented in this file.
### Fixed
- Haspmota `haspmota.parse()` page parsing (#23403)
- ESP32-S3 display stability regression from #23397 (#23404)
- DNS setting with `IPAddress4/5` not persisted
### Removed

View File

@ -27,8 +27,8 @@ enum LoggingLevels {LOG_LEVEL_NONE, LOG_LEVEL_ERROR, LOG_LEVEL_INFO, LOG_LEVEL_D
#ifdef USE_IPV6
ip_addr_t dns_save4[DNS_MAX_SERVERS] = {}; // IPv4 DNS servers
ip_addr_t dns_save6[DNS_MAX_SERVERS] = {}; // IPv6 DNS servers
ip_addr_t dns_save4[2] = {}; // IPv4 DNS servers
ip_addr_t dns_save6[2] = {}; // IPv6 DNS servers
#endif // USE_IPV6
#include "tasmota_options.h"
@ -76,48 +76,72 @@ extern bool WifiHasIPv6(void);
extern bool EthernetHasIPv6(void);
void WiFiHelper::scrubDNS(void) {
// AddLog(LOG_LEVEL_DEBUG, "IP>1: dns_save4 %s %s dns_save6 %s %s",
// IPAddress(&dns_save4[0]).toString().c_str(),IPAddress(&dns_save4[1]).toString().c_str(),
// IPAddress(&dns_save6[0]).toString().c_str(),IPAddress(&dns_save6[1]).toString().c_str());
// String dns_entry0 = IPAddress(dns_getserver(0)).toString();
// String dns_entry1 = IPAddress(dns_getserver(1)).toString();
// scan DNS entries
bool has_v4 = WifiHasIPv4() || EthernetHasIPv4();
bool has_v6 = false;
#ifdef USE_IPV6
has_v6 = WifiHasIPv6() || EthernetHasIPv6();
#endif
// AddLog(LOG_LEVEL_DEBUG, "IP>1: DNS: (%s %s) has4/6:%i-%i", dns_entry0.c_str(), dns_entry1.c_str(), has_v4, has_v6);
// First pass, save values
for (uint32_t i=0; i<DNS_MAX_SERVERS; i++) {
#ifdef USE_IPV6
// First pass, save values
for (uint32_t i=0; i<2; i++) {
const IPAddress ip_dns = IPAddress(dns_getserver(i));
// Step 1. save valid values from DNS
if (!ip_addr_isany_val((const ip_addr_t &)ip_dns)) {
if (ip_dns.type() == IPv4 && has_v4) {
ip_dns.to_ip_addr_t(&dns_save4[i]);
// dns_save4[i] = (ip_addr_t) ip_dns; // dns entry is populated, save it in v4 slot
if (ip_dns.type() == IPv4 && (has_v4 || !has_v6)) {
ip_dns.to_ip_addr_t(&dns_save4[i]); // dns entry is populated, save it in v4 slot
} else if (has_v6) {
ip_dns.to_ip_addr_t(&dns_save6[i]);
// dns_save6[i] = (ip_addr_t) ip_dns; // dns entry is populated, save it in v6 slot
ip_dns.to_ip_addr_t(&dns_save6[i]); // dns entry is populated, save it in v6 slot
}
}
// Step 2. scrub addresses not supported
if (!has_v4) { dns_save4[i] = *IP4_ADDR_ANY; }
if (!has_v6) { dns_save6[i] = *IP_ADDR_ANY; }
// Step 3. restore saved value
if (has_v4 && has_v6) { // if both IPv4 and IPv6 are active, prefer IPv4
if (!ip_addr_isany_val(dns_save4[i])) { dns_setserver(i, &dns_save4[i]); }
else { dns_setserver(i, &dns_save6[i]); }
} else if (has_v4) {
dns_setserver(i, &dns_save4[i]);
} else if (has_v6) {
dns_setserver(i, &dns_save6[i]);
} else {
dns_setserver(i, IP4_ADDR_ANY);
}
#endif // USE_IPV6
}
// AddLog(LOG_LEVEL_DEBUG, "IP>: DNS: from(%s %s) to (%s %s) has4/6:%i-%i", dns_entry0.c_str(), dns_entry1.c_str(), IPAddress(dns_getserver(0)).toString().c_str(), IPAddress(dns_getserver(1)).toString().c_str(), has_v4, has_v6);
// Step 2. scrub addresses not supported
if (!has_v4 && has_v6) { // v6 only
dns_save4[0] = *IP4_ADDR_ANY;
dns_save4[1] = *IP4_ADDR_ANY;
}
if (!has_v6) {
dns_save6[0] = *IP_ADDR_ANY;
dns_save6[1] = *IP_ADDR_ANY;
}
// Step 3. restore saved value
if (has_v4 && has_v6) { // if both IPv4 and IPv6 are active, prefer IPv4 for first and IPv6 for second
if (!ip_addr_isany_val(dns_save4[0])) {
// First DNS IPv4
dns_setserver(0, &dns_save4[0]);
if (!ip_addr_isany_val(dns_save6[0])) {
dns_setserver(1, &dns_save6[0]); // take first IPv6 as second DNS
} else {
dns_setserver(1, &dns_save4[1]); // or revert to second IPv4
}
} else {
// If no DNS IPv4, use IPv6
dns_setserver(0, &dns_save6[0]);
dns_setserver(1, &dns_save6[1]);
}
} else if (has_v6) { // v6 and no v4
dns_setserver(0, &dns_save6[0]);
dns_setserver(1, &dns_save6[1]);
} else { // no v6, we use v4 even if not connected
dns_setserver(0, &dns_save4[0]);
dns_setserver(1, &dns_save4[1]);
}
#endif // USE_IPV6
// AddLog(LOG_LEVEL_DEBUG, "IP>2: DNS: from(%s %s) to (%s %s) has4/6:%i-%i", dns_entry0.c_str(), dns_entry1.c_str(), IPAddress(dns_getserver(0)).toString().c_str(), IPAddress(dns_getserver(1)).toString().c_str(), has_v4, has_v6);
// AddLog(LOG_LEVEL_DEBUG, "IP>2: dns_save4 %s %s dns_save6 %s %s",
// IPAddress(&dns_save4[0]).toString().c_str(),IPAddress(&dns_save4[1]).toString().c_str(),
// IPAddress(&dns_save6[0]).toString().c_str(),IPAddress(&dns_save6[1]).toString().c_str());
}

View File

@ -244,6 +244,7 @@ void WifiBegin(uint8_t flag, uint8_t channel) {
Settings->sta_active ^= 1; // Skip empty SSID
}
if (Settings->ipv4_address[0]) {
// AddLog(LOG_LEVEL_INFO, ">>>1: Wifi Config DNS %_I %_I", Settings->ipv4_address[3], Settings->ipv4_address[4]);
WiFi.config(Settings->ipv4_address[0], Settings->ipv4_address[1], Settings->ipv4_address[2], Settings->ipv4_address[3], Settings->ipv4_address[4]); // Set static IP
}
WiFi.hostname(TasmotaGlobal.hostname); // ESP8266 needs this here (after WiFi.mode)
@ -842,10 +843,12 @@ void WifiCheckIp(void) {
if (Wifi.status != WL_CONNECTED) {
AddLog(LOG_LEVEL_INFO, PSTR(D_LOG_WIFI D_CONNECTED));
// AddLog(LOG_LEVEL_INFO, PSTR("Wifi: Set IP addresses"));
// AddLog(LOG_LEVEL_INFO, ">>>1: Before DNS %_I %_I", Settings->ipv4_address[3], Settings->ipv4_address[4]);
Settings->ipv4_address[1] = (uint32_t)WiFi.gatewayIP();
Settings->ipv4_address[2] = (uint32_t)WiFi.subnetMask();
Settings->ipv4_address[3] = (uint32_t)WiFi.dnsIP();
Settings->ipv4_address[4] = (uint32_t)WiFi.dnsIP(1);
// AddLog(LOG_LEVEL_INFO, ">>>1: After DNS %_I %_I", Settings->ipv4_address[3], Settings->ipv4_address[4]);
// Save current AP parameters for quick reconnect
Settings->wifi_channel = WiFi.channel();