Page 1 of 1
get ip using gethostbyname
Posted: Mon Jun 26, 2017 1:50 pm
by preetam
Hi All,
i am using two esp32's . In the first one i have set the host name as below in wifi event handler below SYSTEM_EVENT_STA_START
ESP_ERROR_CHECK(tcpip_adapter_set_hostname(TCPIP_ADAPTER_IF_STA, "testing32"));
I am able to ping this using
ping testing32 on my windows pc and i am getting reply from the router.
However when i am using the gethostbyname i am not able to retrieve the ip.
here is the snippet
Code: Select all
struct hostent *hp;
struct in_addr **addr;
hp = gethostbyname("testing32");
if(hp == NULL){
ESP_LOGI(TAG,"Cannot resolve host ");
}
i am always getting cannot resolve host.
Could anyone let me know other ways to resolve this .
Re: get ip using gethostbyname
Posted: Mon Jun 26, 2017 3:21 pm
by f.h-f.s.
Have you tried setting your router as DNS in the esp32 that queries the router? (just guessing here)
Re: get ip using gethostbyname
Posted: Mon Jun 26, 2017 5:33 pm
by kolban
DNS doesn't have the ability to resolve dynamically added hosts. Think of DNS like a database that maps host names and the domains they live within to IP addresses. When you configure DNS on the ESP32, you point to a DNS server that will perform the database resolution for you. When you create a new WiFi station (an ESP32) or a network interface on a PC and "declare its hostname", that doesn't cause any external DNS databases to be updated.
The gethostbyname() uses DNS exclusively (belief).
A related but distinct technology to DNS is called mDNS which does support dynamic host names that can come and go on a network. Unlike a database (DNS), mDNS allows hosts on a local network (WiFi / LAN) to resolve each other by name. All the devices listen for broadcast messages and when machine "A" wishes to find the address of machine "B", then machine "A" will broadcast that it is looking for machine "B". This will be heard by all machines on the LAN ... including machine "B". The machine "B" knowing that it is being sought, will broadcast its response ("Hello ... I am machine B and my IP is 1.2.3.4"). This will be heard by machine "A" and the resolution will have been completed. (A side effect (I believe) is that all the other machines listened in on the transaction and can choose to record the question/answer pair for future resolution without having to ask the question themselves). Again, all this is done with mDNS ... which is not the same as DNS and (I believe) not part of gethostbyname().
On larger machines (eg. Linux/Windows), the address resolution technology can be "richer" meaning that both DNS and mDNS may be tried to resolve a request ... for example ... "ping" may first try to resolve using DNS and if that fails, try to resolve by mDNS. Don't assume that "ping" resolving a name is the same as "ping exclusively used gethostbyname".
Re: get ip using gethostbyname
Posted: Tue Jun 27, 2017 7:02 am
by preetam
Hi Kolban,
Thank you for the information.
I think that gethostbyname does not work properly on esp32 when resolving local hostnames. I did try to resolve the esp32 hostname as mentioned in the above post in the android framework and was able to resolve the same without any changes to the router.
Thank you,
Paul
Re: get ip using gethostbyname
Posted: Fri Jun 30, 2017 2:10 pm
by martinayotte
As Kolban said gethostbyname() is strickly relying to your external DNS provided by your router.
Most of router's DNS are NOT mDNS aware, so they won't resolve it.
This is not a ESP32 issue.
What you can do is having a dedicated PC server to provide both DHCP and DNS configured together and switch off the one in the router.
Re: get ip using gethostbyname
Posted: Mon Apr 09, 2018 7:05 am
by MickPF
Hello,
Excuse me if I get involved here, but I've got a similar problem: I try to use the call "gethostbyname" of the esp-idf to get access to a NTP server in the internet. It returns always NULL. I don't really know, what to do to get the names resolved. My WiFi-AP/-router contains a DNS service.
Thanks in advance,
Michael
Re: get ip using gethostbyname
Posted: Tue Apr 10, 2018 3:54 pm
by chegewara
This is how it works for me:
https://github.com/chegewara/esp32-ota- ... #L108-L141
That while loop is crucial because resolving DNS takes some time, thats why you are getting NULL value each time.