change dnsName

jumjum123
Posts: 199
Joined: Mon Oct 17, 2016 3:11 pm

change dnsName

Postby jumjum123 » Fri Mar 10, 2017 9:23 am

In my router I see my esp32 under the name espressif.
Is there anything in esp-idf to change this default name ?
My esp32 should have the same name in each network without having to rename in router.

MalteJ
Posts: 62
Joined: Wed Sep 21, 2016 10:26 pm

Re: change dnsName

Postby MalteJ » Fri Mar 10, 2017 2:03 pm


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

Re: change dnsName

Postby kolban » Fri Mar 10, 2017 2:24 pm

I think the API you are looking for is the tcpip_adapter_set_hostname() function call. If you search for that keyword in the docs you will find the usage instructions.
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: change dnsName

Postby preetam » Mon Mar 13, 2017 5:12 pm

Hi,

I tried setting the hostname as below with different options.

Code: Select all

tcpip_adapter_set_hostname(TCPIP_ADAPTER_IF_STA, "testing");
1. After initializing the tcp adapter.
2.Before initializing the tcp adapter.
3. After starting the wifi and before starting the wifi.

I am still seeing espressif in my router.

Is there another way to set the hostname.

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

Re: change dnsName

Postby kolban » Wed Mar 15, 2017 5:19 am

Preetam,
I tested the function and, just as you said, when I look at my WiFi access point's definitions of attached devices, I still see "espressif" after having made a call to tcpip_adapter_set_hostname(). The one possible wild guess I have is that the access point "remembers" the first name for a given MAC address that it finds. My hunch on this is that I use a NetGear access point and surfing through its settings I found a page on access control where I can allow/disallow devices. Looking there, I seem to see a list of all the previous devices that have ever connected including their MAC addresses and "Device Name". It is in the realms of possibility that if we rebooted our access points and THEN tried a connection with a new host name, then it might work.

Another puzzle (to me) is the purpose of this interface host name. This thread begins with the title of "changing dnsName" ... however I don't believe we are going anywhere near any "Domain Name Servers" ...

I'm not sure what effect "supplying" a name here actually has. Dynamic lookup of names might be performed through mDNS. I don't believe that if we were to say "ping espressif" it would resolve. I don't think access points run DNS servers.
Free book on ESP32 available here: https://leanpub.com/kolban-ESP32

ESP_Angus
Posts: 2344
Joined: Sun May 08, 2016 4:11 am

Re: change dnsName

Postby ESP_Angus » Wed Mar 15, 2017 6:55 am

kolban wrote: I tested the function and, just as you said, when I look at my WiFi access point's definitions of attached devices, I still see "espressif" after having made a call to tcpip_adapter_set_hostname(). The one possible wild guess I have is that the access point "remembers" the first name for a given MAC address that it finds.
I think kolban's guess is correct.

The ESP32 appends its hostname to DHCP request, discover, renew and bind requests. So initially it will have been appended 'espressif' and later it would be the new value. You could use a packet capture to confirm this.

Something to also verify is that you set the hostname before WiFi connects and these DHCP requests go out.

However, even if the correct name is sent on the newer DHCP packets, when the router grants the DHCP lease it probably saves the hostname is saw in the original request. It may not bother updating this if the hostname for the MAC changed later. You may have to wait for the DHCP lease to expire (or change the MAC of the ESP32 with esp_wifi_set_mac(), so it looks like a brand new device to the router.)

For further details on the intricacies of DHCP, the Wikipedia page is pretty good: https://en.wikipedia.org/wiki/Dynamic_H ... n_Protocol - and it links to the RFCs with even more detail if needed.

In IDF, the dhcp client implementation can be found in components/lwip/core/ipv4/dhcp.c (function dhcp_option_hostname adds the hostname to the DHCP request packet.)

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

Re: change dnsName

Postby preetam » Wed Mar 15, 2017 10:05 am

Thank you for the information.

I did follow an example from https://github.com/espressif/arduino-es ... v6.ino#L78

and changed the same in the event handler

Code: Select all

 switch(event->event_id) {
    case SYSTEM_EVENT_STA_START:
        ESP_ERROR_CHECK(tcpip_adapter_set_hostname(TCPIP_ADAPTER_IF_STA, "testing"));	
        esp_wifi_connect();
Now i can ping with testing and the reply comes from the router (testing.<routername>.com). As there are no ping echo's in my code.

I still need to test what happens if there is an wifi disconnect and connects later. whether the interface Hostname will be retained or do i need to stop the interface and start again.

As with regards to dynamic look up with mDNS . i am skeptical about the amount of time needed to search for the service or host. If in my router there are around 20 sta's connected and if i need to find a service or host of one sta. Then the present mdns_query api may not search in 1 second and i need to increase the search time to more than one second. This may affect the other tasks as they will be blocked.

MalteJ
Posts: 62
Joined: Wed Sep 21, 2016 10:26 pm

Re: change dnsName

Postby MalteJ » Wed Mar 15, 2017 11:34 am

Usually you use something like the avahi daemon, which caches mDNS announcements and returns the IP of the requested device from its cache.
So that would be quite fast.

Best,
Malte

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

Re: change dnsName

Postby preetam » Wed Mar 15, 2017 11:38 am

yes using avahi it would be fast. but on esp32 it would demand time using mdns_query api

Thank you
Paul

MalteJ
Posts: 62
Joined: Wed Sep 21, 2016 10:26 pm

Re: change dnsName

Postby MalteJ » Wed Mar 15, 2017 12:01 pm

You could extend the esp-idf mdns component by a caching feature. So if desired the component would listen on all mDNS broadcasts and caches the information.

Who is online

Users browsing this forum: asargent, Gaston1980, Google [Bot] and 162 guests