How to set main DNS server addr when configuring wifi in Soft-AP mode

User avatar
i_am_mrp
Posts: 27
Joined: Thu Sep 28, 2017 9:14 am
Location: California

How to set main DNS server addr when configuring wifi in Soft-AP mode

Postby i_am_mrp » Mon Nov 27, 2017 8:29 pm

A classic DHCP server can provide DHCP clients with what DNS servers they should use.

Currently I see that in Soft-AP mode the ESP32 has 192.168.4.1 for the gateway. When a client connects to the ESP32, it will successfully do so and pickup the ip/gateway/netmask. However, when I look at what DNS servers have been assigned, I see none.

How do I specify what DNS servers the DHCP clients should get? I see the tcpip_adapter_set_dns_info(..) and it's comment is:
...
2.In soft-AP mode, the DNS Server's main dns server offered to the station is the IP address of soft-AP, if the application don't want to use the IP address of soft-AP, they can set the main dns server.
...
This comment seems to indicate that the IP address of the ESP32 "should" be provided as the main DNS server by default when in AP-mode? This is not happening, as any of the connecting clients never have a DNS server set; is this a bug? I am using the latest ESP-IDF pull as of this writing.

I see the tcpip_adapter_dns_type_t enum says that the backup and fallback DNS server addresses are only for STA mode at this time. However, the TCPIP_ADAPTER_DNS_MAIN enum value does not have this restriction, which corresponds to the previous code comment.

The following code snippet is my attempt to set the main DNS server. The ** Problem ** is I can't figure out how to assign the actual IP address to the tcpip_adapter_dns_info_t. I keep getting compile errors not matter what approach I try. In general the error is related to: 'ip_addr_t {aka struct _ip_addr}' has no member named 'addr', or something related to that.

Code: Select all

tcpip_adapter_dns_info_t dns_info;
const char* dns_server = "192.168.4.1" ;
// int ip4addr_aton(const char *cp, ip4_addr_t *addr);
int v = ip4addr_aton(dns_server, &dns_info.ip) ;  // ** Problem **
ESP_ERROR_CHECK(tcpip_adapter_set_dns_info(
	TCPIP_ADAPTER_IF_AP,
	TCPIP_ADAPTER_DNS_MAIN,
	&dns_info));
1.What is the correct code for doing this?
2. When should this call be made in the set of calls necessary to setup AP-mode?

WiFive
Posts: 3529
Joined: Tue Dec 01, 2015 7:35 am

Re: How to set main DNS server addr when configuring wifi in Soft-AP mode

Postby WiFive » Mon Nov 27, 2017 10:49 pm

ipaddr_aton

User avatar
i_am_mrp
Posts: 27
Joined: Thu Sep 28, 2017 9:14 am
Location: California

Re: How to set main DNS server addr when configuring wifi in Soft-AP mode

Postby i_am_mrp » Tue Nov 28, 2017 12:44 am

Thank you WiFive!

Hopefully someone can answer if the main DNS server not being set in AP-mode by default is a bug/omission.

As for where in the AP-mode setup call sequence you need to make the call, empirically this is what is working:

Code: Select all

tcp_adapter_init();
esp_event_loop_init(..);
    // optional 2 function calls if setting static ip info...
    tcpip_adapter_dhcps_stop(TCPIP_ADAPTER_IF_AP)) ;
    tcpip_adapter_set_ip_info(TCPIP_ADAPTER_IF_AP, pstatic_ip_info) ;
tcpip_adapter_set_dns_info(TCPIP_ADAPTER_IF_AP, TCPIP_ADAPTER_DNS_MAIN, &dns_info) ;
tcpip_adapter_dhcps_start(TCPIP_ADAPTER_IF_AP);
esp_wifi_init(&wifi_init_cfg) ;
... // normal calls to complete the AP-mode setup

phatpaul
Posts: 110
Joined: Fri Aug 24, 2018 1:14 pm

Re: How to set main DNS server addr when configuring wifi in Soft-AP mode

Postby phatpaul » Thu Sep 27, 2018 7:59 pm

The formatting for the DNS is different for some reason. Here's what I figured out:

Code: Select all

    	tcpip_adapter_ip_info_t info = {0};
    	tcpip_adapter_dns_info_t dns_info = {0};
    	IP_ADDR4(&dns_info.ip, 192, 168, 4, 254);
        IP4_ADDR(&info.ip, 192, 168, 4, 1);
        IP4_ADDR(&info.gw, 0, 0, 0, 0);
        IP4_ADDR(&info.netmask, 255, 255, 255, 0);

        ESP_ERROR_CHECK(tcpip_adapter_dhcps_stop(TCPIP_ADAPTER_IF_AP));
        ESP_ERROR_CHECK(tcpip_adapter_set_ip_info(TCPIP_ADAPTER_IF_AP, &info));
        ESP_ERROR_CHECK(tcpip_adapter_set_dns_info(TCPIP_ADAPTER_IF_AP, TCPIP_ADAPTER_DNS_MAIN, &dns_info));
        ESP_ERROR_CHECK(tcpip_adapter_dhcps_start(TCPIP_ADAPTER_IF_AP));
Anyway, even after writing a different IP to that, I still see 192.168.4.1 listed on my Android phone as the DNS server for that connection.

BTW, I found an undocumented option which looks like it should enable/disable providing a DNS server IP via DHCP from the softAP.

Code: Select all

ESP_ERROR_CHECK(tcpip_adapter_dhcps_stop(TCPIP_ADAPTER_IF_AP));
        uint8_t opt_val = 0; // don't supply a dns server via dhcps
        tcpip_adapter_dhcps_option(TCPIP_ADAPTER_OP_SET, TCPIP_ADAPTER_DOMAIN_NAME_SERVER, &opt_val, 1);
        ESP_ERROR_CHECK(tcpip_adapter_dhcps_start(TCPIP_ADAPTER_IF_AP));
Anyway, even after writing '0' to that, I still see 192.168.4.1 listed on my Android phone as the DNS server for that connection.

SergeyMinin
Posts: 3
Joined: Mon Nov 25, 2019 8:36 am

Re: How to set main DNS server addr when configuring wifi in Soft-AP mode

Postby SergeyMinin » Thu Feb 27, 2020 6:00 pm

Managed to replace the DNS server in this way

Code: Select all

    
    tcpip_adapter_ip_info_t info = {0};
    tcpip_adapter_dns_info_t dns_info = {0};
    memset (&dns_info, 8, sizeof(dns_info));
    IP4_ADDR(&info.ip, 192, 168, 4, 1);
    IP4_ADDR(&info.gw, 0, 0, 0, 0);
    IP4_ADDR(&info.netmask, 255, 255, 255, 0);
    IP_ADDR4(&dns_info.ip, 4, 4, 4, 4);
    
    tcpip_adapter_dhcps_stop(TCPIP_ADAPTER_IF_AP);
    tcpip_adapter_set_ip_info(TCPIP_ADAPTER_IF_AP, &info);
    ESP_ERROR_CHECK(tcpip_adapter_set_dns_info(TCPIP_ADAPTER_IF_AP, TCPIP_ADAPTER_DNS_MAIN, &dns_info));
    dhcps_offer_t opt_val = OFFER_DNS; // supply a dns server via dhcps
    tcpip_adapter_dhcps_option(TCPIP_ADAPTER_OP_SET, TCPIP_ADAPTER_DOMAIN_NAME_SERVER, &opt_val, 1);
    tcpip_adapter_dhcps_start(TCPIP_ADAPTER_IF_AP);

Who is online

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