Set DNS with DHCP

PantuFJAR
Posts: 5
Joined: Tue Aug 23, 2022 6:41 pm

Set DNS with DHCP

Postby PantuFJAR » Tue Aug 23, 2022 6:52 pm

Is it possible to set the DNS server for the ESP32 when its using a DHCP server to get its own IP? Im using platformio latest arduino framweork with the board esp32dev, when I take a look to the WiFiSTAClass the only way to configure the DNS is by disabling the dhcp client, quoting from the code itself on the config() method:

Code: Select all

Change IP configuration settings disabling the dhcp client

User avatar
gtjoseph
Posts: 92
Joined: Fri Oct 15, 2021 10:55 pm

Re: Set DNS with DHCP

Postby gtjoseph » Wed Aug 24, 2022 6:35 pm

I'm not sure how the arduino component factors in but when using the native esp-idf, the dhcp client automatically gets the dns server and sets it accordingly.

PantuFJAR
Posts: 5
Joined: Tue Aug 23, 2022 6:41 pm

Re: Set DNS with DHCP

Postby PantuFJAR » Mon Aug 29, 2022 11:07 pm

gtjoseph wrote:
Wed Aug 24, 2022 6:35 pm
I'm not sure how the arduino component factors in but when using the native esp-idf, the dhcp client automatically gets the dns server and sets it accordingly.
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?

User avatar
gtjoseph
Posts: 92
Joined: Fri Oct 15, 2021 10:55 pm

Re: Set DNS with DHCP

Postby gtjoseph » Tue Aug 30, 2022 2:57 pm

Ah,you don't want to use the DHCP provided nameservers at all. Based on the docs, I believe esp_netif_set_dns_info will only set the fallback dns servers if the dhcp client is active and the client was configured to get the nameservers from the server. So I think if wifi was already connected and dhcpc already ran, you'd have to stop the dhcp client, set the dhcpc optons to not include dns, set the nameservers with esp_netif_set_dns_info then start the client again.

PantuFJAR
Posts: 5
Joined: Tue Aug 23, 2022 6:41 pm

Re: Set DNS with DHCP

Postby PantuFJAR » Tue Aug 30, 2022 10:13 pm

Thank you for helping me, let me see if I get this right... Your suggestion is to connect using the DHCP server, get the connection configuration from it (IP,Gateway). Then disconnect, configure the client with the configuration I got from the DHCP server but with the DNS servers address that I want?

User avatar
gtjoseph
Posts: 92
Joined: Fri Oct 15, 2021 10:55 pm

Re: Set DNS with DHCP

Postby gtjoseph » Wed Aug 31, 2022 2:31 pm

You don't want to disconnect, just stop the dhcp client, reconfigure it to not ask for dns servers, set the dns servers manually, then restart the client. Let me see if I can work up an example.

PantuFJAR
Posts: 5
Joined: Tue Aug 23, 2022 6:41 pm

Re: Set DNS with DHCP

Postby PantuFJAR » Wed Aug 31, 2022 6:50 pm

gtjoseph wrote:
Wed Aug 31, 2022 2:31 pm
You don't want to disconnect, just stop the dhcp client, reconfigure it to not ask for dns servers, set the dns servers manually, then restart the client. Let me see if I can work up an example.
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.

User avatar
gtjoseph
Posts: 92
Joined: Fri Oct 15, 2021 10:55 pm

Re: Set DNS with DHCP

Postby gtjoseph » Mon Sep 05, 2022 10:41 am

Well, 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
Posts: 5
Joined: Tue Aug 23, 2022 6:41 pm

Re: Set DNS with DHCP

Postby PantuFJAR » Wed Sep 14, 2022 9:23 pm

gtjoseph wrote:
Mon Sep 05, 2022 10:41 am
Well, 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.
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.

And no, I dont have access to the DHCP server, sadly.

Who is online

Users browsing this forum: Bing [Bot] and 89 guests