Page 1 of 1

HOW to read and SET DNS IP in esp32 idf?

Posted: Sat Dec 19, 2020 6:33 am
by ht_main1
Hi

I am trying to set DNS primary and secondary IP of ESP32, but i didn't see any API function for the same.
I am using esp-idf 4.0, I need to know the function to read and set DNS IP.

regards
vaibhav

Re: HOW to read and SET DNS IP in esp32 idf?

Posted: Sat Dec 19, 2020 8:55 am
by felmue
Hello @ht_main1

I don't have esp-idf 4.0 installed, but I've searched a bit through the Espressif github repository. How about dns_setserver() used here https://github.com/espressif/esp-idf/bl ... test_dns.c? And most likely there is a dns_getserver() as well.

Thanks
Felix

Re: HOW to read and SET DNS IP in esp32 idf?

Posted: Sat Dec 19, 2020 9:49 am
by markkuk

Re: HOW to read and SET DNS IP in esp32 idf?

Posted: Sun Dec 20, 2020 7:10 am
by ht_main1
Thanks this helped

Re: HOW to read and SET DNS IP in esp32 idf?

Posted: Sun Dec 20, 2020 7:58 am
by ht_main1
Hi i tried your method, i am setting Static IP, and dns, but when i read back DNS , i get 0.0.0.0

Code: Select all

tcpip_adapter_init();

	if(userData.eth_host_cfg.dhcp_en){
			
		tcpip_adapter_dns_info_t dns_info = {0};
		dhcps_offer_t dhcps_dns_value = OFFER_DNS;
		//dns_info.ip.u_addr.ip4.addr = addr;
		dns_info.ip.type = IPADDR_TYPE_V4;
		
		memset (&dns_info, 8, sizeof(dns_info));
		
		//IP4_ADDR(&dns_info.ip.u_addr.ip4, 8, 8, 8, 8);
		IP_ADDR4(&dns_info.ip, 4, 4, 4, 4);
		
  /*  static esp_err_t set_dhcps_dns(esp_netif_t *netif, uint32_t addr)
{
    esp_netif_dns_info_t dns;
    dns.ip.u_addr.ip4.addr = addr;
    dns.ip.type = IPADDR_TYPE_V4;
    dhcps_offer_t dhcps_dns_value = OFFER_DNS;
    ESP_ERROR_CHECK(esp_netif_dhcps_option(netif, ESP_NETIF_OP_SET, 
				ESP_NETIF_DOMAIN_NAME_SERVER, &dhcps_dns_value, sizeof(dhcps_dns_value)));
    ESP_ERROR_CHECK(esp_netif_set_dns_info(netif, ESP_NETIF_DNS_MAIN, &dns));
    return ESP_OK;
}*/
   // 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);
	
	
		tcpip_adapter_ip_info_t ipInfo;
		memset(&ipInfo, 0x00, sizeof(ipInfo));

		IP4_ADDR(&ipInfo.ip, userData.eth_host_cfg.server.IP[0],userData.eth_host_cfg.server.IP[1],userData.eth_host_cfg.server.IP[2],userData.eth_host_cfg.server.IP[3]);
		IP4_ADDR(&ipInfo.gw, userData.eth_host_cfg.network.GW[0],userData.eth_host_cfg.network.GW[1],userData.eth_host_cfg.network.GW[2],userData.eth_host_cfg.network.GW[3]);
		IP4_ADDR(&ipInfo.netmask , userData.eth_host_cfg.network.SNM[0],userData.eth_host_cfg.network.SNM[1],userData.eth_host_cfg.network.SNM[2],userData.eth_host_cfg.network.SNM[3]);

		printf("[app_main]ETHIP: " IPSTR "\r\n", IP2STR(&ipInfo.ip));
		printf("[app_main]ETHMASK: " IPSTR "\r\n", IP2STR(&ipInfo.netmask));
		printf("[app_main]ETHGW: " IPSTR "\r\n", IP2STR(&ipInfo.gw));
		
		tcpip_adapter_dhcpc_stop(TCPIP_ADAPTER_IF_ETH); // ret=0x5000 -> tcpip_adapter_invalid_params, //
		//very old esp-idf didn't implementated this yet.

		ESP_LOGI(TAG, "dhcp client stop RESULT:");
		ESP_ERROR_CHECK(tcpip_adapter_dhcps_option(TCPIP_ADAPTER_OP_SET , 
				TCPIP_ADAPTER_DOMAIN_NAME_SERVER , &dhcps_dns_value, sizeof(dhcps_dns_value)));
		ESP_ERROR_CHECK(tcpip_adapter_set_dns_info(TCPIP_ADAPTER_IF_ETH, TCPIP_ADAPTER_DNS_MAIN, &dns_info));
		tcpip_adapter_set_ip_info(TCPIP_ADAPTER_IF_ETH, &ipInfo);
		//tcpip_adapter_dhcps_start(TCPIP_ADAPTER_IF_AP);
	}

Code: Select all

void monitoring_task(void *pvParameter)
{
	ST_QUERY vTmp_q = {0};
	uint32_t lu32Cnt = 0;
	uint32_t lu32Index = 0;
	tcpip_adapter_dns_info_t dns_info = {0};
	for(;;){
		ESP_LOGI(TAG, "free heap: %d",esp_get_free_heap_size());
		//tcpip_adapter_get_dns_info(TCPIP_ADAPTER_IF_ETH, TCPIP_ADAPTER_DNS_MAIN, &dns_info);
		tcpip_adapter_get_dns_info(TCPIP_ADAPTER_IF_ETH, 0, &dns_info);
        ESP_LOGI(TAG, "Name Server1: " IPSTR, IP2STR(&dns_info.ip.u_addr.ip4));
        tcpip_adapter_get_dns_info(TCPIP_ADAPTER_IF_ETH, 1, &dns_info);
        ESP_LOGI(TAG, "Name Server2: " IPSTR, IP2STR(&dns_info.ip.u_addr.ip4));
		vTaskDelay( 5000 / portTICK_PERIOD_MS);		// task delay 5s
	}
}
As you can see from following log , static IP is set but dns server are empty

Code: Select all

[app_main]ETHIP: 192.168.2.10

[app_main]ETHMASK: 255.255.255.0

[app_main]ETHGW: 192.168.2.1
[0;32mI (17523) eth_example: free heap: 167480[0m
[0;32mI (17523) eth_example: Name Server1: 0.0.0.0[0m
[0;32mI (17523) eth_example: Name Server2: 0.0.0.0[0m
[0;32mI (22523) eth_example: free heap: 167480[0m
[0;32mI (22523) eth_example: Name Server1: 0.0.0.0[0m
[0;32mI (22523) eth_example: Name Server2: 0.0.0.0[0m