get ip using gethostbyname

preetam
Posts: 49
Joined: Thu Jan 26, 2017 2:31 pm
Location: Germany

get ip using gethostbyname

Postby preetam » Mon Jun 26, 2017 1:50 pm

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 .

f.h-f.s.
Posts: 214
Joined: Thu Dec 08, 2016 2:53 pm

Re: get ip using gethostbyname

Postby f.h-f.s. » Mon Jun 26, 2017 3:21 pm

Have you tried setting your router as DNS in the esp32 that queries the router? (just guessing here)

User avatar
kolban
Posts: 1683
Joined: Mon Nov 16, 2015 4:43 pm
Location: Texas, USA

Re: get ip using gethostbyname

Postby kolban » Mon Jun 26, 2017 5:33 pm

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".
Free book on ESP32 available here: https://leanpub.com/kolban-ESP32

preetam
Posts: 49
Joined: Thu Jan 26, 2017 2:31 pm
Location: Germany

Re: get ip using gethostbyname

Postby preetam » Tue Jun 27, 2017 7:02 am

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

User avatar
martinayotte
Posts: 141
Joined: Fri Nov 13, 2015 4:27 pm

Re: get ip using gethostbyname

Postby martinayotte » Fri Jun 30, 2017 2:10 pm

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.

User avatar
MickPF
Posts: 45
Joined: Tue Apr 03, 2018 8:47 pm

Re: get ip using gethostbyname

Postby MickPF » Mon Apr 09, 2018 7:05 am

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
Nobody is perfect!
My name is NOT Nobody...


Nobody lives forever to correct his mistakes someday in the future, if not right now!
Covid-19 is not responsible for any human failures!

chegewara
Posts: 2230
Joined: Wed Jun 14, 2017 9:00 pm

Re: get ip using gethostbyname

Postby chegewara » Tue Apr 10, 2018 3:54 pm

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.

Who is online

Users browsing this forum: No registered users and 139 guests