Code: Select all
Change IP configuration settings disabling the dhcp client
Code: Select all
Change IP configuration settings disabling the dhcp client
Using the native API, is it possible to set the DNS manually after the DHCP assigns it automatically? In other words to manually override the DNS setting set by the DHCP server?
Oh, ok, ok, I kinda get it now. If you could provide aditional help it would be greatly appreciated, but I get your idea I will give it a try.
Oh my God! You actually tried, thank you so much for your help. I also tried and couldnt set de DNS manually by any mean. I really, really apreciate your help.gtjoseph wrote: ↑Mon Sep 05, 2022 10:41 amWell, I gave it my best shot but there just doesn't seem to be a way to use the DHCP client but set the DNS server manually. The esp_netif_dhcpc_option() API is hard coded to allow only two options (ESP_NETIF_VENDOR_SPECIFIC_INFO and ESP_NETIF_IP_REQUEST_RETRY_TIME) and will return ESP_ERR_ESP_NETIF_INVALID_PARAMS if you try to give it any other parameter and that's not documented anywhere. I'm not sure if this is a bug in the code or the documentation.
LWIP itself does allow you to use your own local dynamic lookup function but it doesn't seem to be exposed through esp_netif so it's probably take a bit of effort to get it to work.
The only other thing I can suggest is that if you have control over the dhcp server in your runtime environment, you could add an option specific to the ESP32 to give it a different dns server than other dhcp clients would get.
PantuFJAR wrote: ↑Wed Sep 14, 2022 9:23 pmOh my God! You actually tried, thank you so much for your help. I also tried and couldnt set de DNS manually by any mean. I really, really apreciate your help.gtjoseph wrote: ↑Mon Sep 05, 2022 10:41 amWell, I gave it my best shot but there just doesn't seem to be a way to use the DHCP client but set the DNS server manually. The esp_netif_dhcpc_option() API is hard coded to allow only two options (ESP_NETIF_VENDOR_SPECIFIC_INFO and ESP_NETIF_IP_REQUEST_RETRY_TIME) and will return ESP_ERR_ESP_NETIF_INVALID_PARAMS if you try to give it any other parameter and that's not documented anywhere. I'm not sure if this is a bug in the code or the documentation.
LWIP itself does allow you to use your own local dynamic lookup function but it doesn't seem to be exposed through esp_netif so it's probably take a bit of effort to get it to work.
The only other thing I can suggest is that if you have control over the dhcp server in your runtime environment, you could add an option specific to the ESP32 to give it a different dns server than other dhcp clients would get.
And no, I dont have access to the DHCP server, sadly.
Code: Select all
case IP_EVENT_STA_GOT_IP:
{
ESP_LOGI("WiFiManager", "IP_EVENT_STA_GOT_IP");
connected = true;
ip_event_got_ip_t *event = (ip_event_got_ip_t *)event_data;
ip = event->ip_info.ip;
ESP_LOGI("WiFiManager-IP_EVENT", "got ip:" IPSTR, IP2STR(&ip));
esp_netif_dns_info_t dns_info;
if (WiFiManager::instance && WiFiManager::instance->netif)
{
if (esp_netif_get_dns_info(WiFiManager::instance->netif, ESP_NETIF_DNS_MAIN, &dns_info) == ESP_OK)
{
ip_addr_t ip_addr;
ip_addr.u_addr.ip4.addr = dns_info.ip.u_addr.ip4.addr; // Copia o endereço IPv4 diretamente
ip_addr.type = IPADDR_TYPE_V4; // Define como IPv4
ESP_LOGI("WiFiManager", "DNS antes da alteração: %s", ipaddr_ntoa(&ip_addr));
}
else
{
ESP_LOGW("WiFiManager", "Falha ao obter o DNS configurado antes.");
}
// Configurar DNS manualmente
ip_addr_t dns_primary, dns_secondary;
IP4_ADDR(&dns_primary.u_addr.ip4, 8, 8, 8, 8); // Google DNS
dns_primary.type = IPADDR_TYPE_V4;
IP4_ADDR(&dns_secondary.u_addr.ip4, 1, 1, 1, 1); // Cloudflare DNS
dns_secondary.type = IPADDR_TYPE_V4;
// Configurar DNS principal
dns_info.ip.u_addr.ip4.addr = dns_primary.u_addr.ip4.addr;
dns_info.ip.type = IPADDR_TYPE_V4;
ESP_ERROR_CHECK(esp_netif_set_dns_info(WiFiManager::instance->netif, ESP_NETIF_DNS_MAIN, &dns_info));
// Configurar DNS secundário
dns_info.ip.u_addr.ip4.addr = dns_secondary.u_addr.ip4.addr;
ESP_ERROR_CHECK(esp_netif_set_dns_info(WiFiManager::instance->netif, ESP_NETIF_DNS_BACKUP, &dns_info));
ESP_LOGI("WiFiManager", "DNS alterado para Google (8.8.8.8) e Cloudflare (1.1.1.1)");
// Ler o DNS atual após a alteração
if (esp_netif_get_dns_info(WiFiManager::instance->netif, ESP_NETIF_DNS_MAIN, &dns_info) == ESP_OK)
{
ip_addr_t ip_addr;
ip_addr.u_addr.ip4.addr = dns_info.ip.u_addr.ip4.addr; // Copia o endereço IPv4 novamente
ip_addr.type = IPADDR_TYPE_V4; // Define como IPv4
ESP_LOGI("WiFiManager", "DNS apos a alteração: %s", ipaddr_ntoa(&ip_addr));
}
else
{
ESP_LOGW("WiFiManager", "Falha ao obter o DNS configurado após.");
}
}
else
{
ESP_LOGE("WiFiManager", "Erro: netif não está inicializado.");
}
}
break;
Users browsing this forum: No registered users and 62 guests