SOLVED: DNS getaddrinfo returns error 202 but packet trace shows result was returned

MikeMyhre
Posts: 54
Joined: Sat Nov 05, 2022 3:32 am

SOLVED: DNS getaddrinfo returns error 202 but packet trace shows result was returned

Postby MikeMyhre » Mon May 06, 2024 10:26 pm

I am using IDF version 5.2 and trying to do a DNS query.
This is the code I am using and DNS is set to 8.8.8.8
I am using a static IP and the request is going out the static IP address through an ethernet chip. WIFI is not in use at the moment. Packet response time is about 10 mS round trip. It takes about 4 seconds for getaddrinfo function to return with the error code (about 3 seconds after the packet was received on my local network.

Code: Select all

bool net_find_server(char *host, char *ip, int ip_size)
{
	struct addrinfo* result = NULL;
	//struct addrinfo* res;
	struct addrinfo hint;
	int error;

	ESP_LOGI("net_find_server","Searching for host %s",host);

	/* resolve the domain name into a list of addresses */
	memset(&hint,0,sizeof(hint));
	error = getaddrinfo(host, NULL, &hint, &result);
	if (error != 0) {
		ESP_LOGI("net_find_server","error in getaddrinfo: %d - %s", error, strerror(error));
		return false;
	}
	ip_addr_t target_addr;
	memset(&target_addr, 0, sizeof(target_addr));
	struct in_addr addr4 = ((struct sockaddr_in *) (result->ai_addr))->sin_addr;
	inet_addr_to_ip4addr(ip_2_ip4(&target_addr), &addr4);
	strncpy(ip,inet_ntoa(addr4),ip_size);

	//ESP_LOGI("net_find_server","hostname: %s, IP: %s", host,ip);
	freeaddrinfo(result);

	return true;
}
What could the problem be? Clearly the packet trace shows that the DNS request goes out and the correct result is returned by the server.

UPDATE:
I just found out that this is related to a static IP address. Both use the same DNS server. When I use DHCP mode, the getaddrinfo() queries return just fine.

Code: Select all

	// setting DNS to 8.8.8.8
		esp_netif_dns_info_t dns_info;
		dns_info.ip.u_addr.ip4.addr = swap32bytes(sysConf.eth_dns);
		esp_netif_set_dns_info(my_eth_netif,ESP_NETIF_DNS_MAIN, &dns_info);
	    	ESP_LOGI("net","Using Static IP");
Update2: I figured it out. When setting the DNS, the structure needs to be initialized to zero before setting the IP address. Like this:

Code: Select all

	// setting DNS to 8.8.8.8
		esp_netif_dns_info_t dns_info;
[b]		memset(&dns_info,0,sizeof(dns_info));[/b]
		dns_info.ip.u_addr.ip4.addr = swap32bytes(sysConf.eth_dns);
		esp_netif_set_dns_info(my_eth_netif,ESP_NETIF_DNS_MAIN, &dns_info);
	    	ESP_LOGI("net","Using Static IP");

Who is online

Users browsing this forum: No registered users and 94 guests