0

I have a weird issue. I have an ESP32 embedded device running an mDNS server which I assign a hostname to. For the record, i've included this code for the ESP32 device below.

When I ping this hostname from my Ubuntu PC on the same local network,this hostname becomes local and it is not actually pinging the device. No matter what I change the hostname to on the esp device, this happens. In this case i've named it audio-server and I ping it as follows :

ping audio-server.local PING audio-server.local (127.0.0.1) 56(84) bytes of data. 64 bytes from localhost (127.0.0.1): icmp_seq=1 ttl=64 time=0.026 ms 64 bytes from localhost (127.0.0.1): icmp_seq=2 ttl=64 time=0.048 ms 64 bytes from localhost (127.0.0.1): icmp_seq=3 ttl=64 time=0.048 ms 64 bytes from localhost (127.0.0.1): icmp_seq=4 ttl=64 time=0.044 ms 

What exactly is going on here?

static void mdns_delegate_hostname(void) { esp_err_t err = ESP_OK; char *delegated_hostname = "audio-server"; esp_netif_t *intf = esp_netif_get_default_netif();; while (intf == NULL) { ESP_LOGE(TAG, "ERROR ! netif is NULL"); intf = esp_netif_get_default_netif(); sleep(2); } mdns_ip_addr_t addr4, addr6; addr4.addr.type = ESP_IPADDR_TYPE_V4; esp_netif_ip_info_t info; esp_netif_get_ip_info(intf, &info); ESP_LOGI(TAG, "IP Address of device is : " IPSTR "\n", IP2STR(&info.ip)); addr4.addr.u_addr.ip4 = info.ip; addr6.addr.type = ESP_IPADDR_TYPE_V6; esp_netif_get_ip6_linklocal(intf, &addr6.addr.u_addr.ip6); addr4.next = &addr6; addr6.next = NULL; ESP_LOGI(TAG, "Setting delegated hostname to %s\n", delegated_hostname); err = mdns_delegate_hostname_add(delegated_hostname, &addr4); if (err != ESP_OK) { ESP_LOGE(TAG, "ERROR Setting delegated hostname to %s\n", delegated_hostname); return; } err = mdns_service_add_for_host(NULL, "_http", "_tcp", delegated_hostname, 80, NULL, 0); if (err != ESP_OK) { ESP_LOGE(TAG, "ERROR adding service for host %s\n", delegated_hostname); } } 

1 Answer 1

0

The code finds the "default network interface" of the ESP32 device based on routing parameters, then uses its IP address and the configured hostname to announce the presence of the device and the availability of its HTTP service over mDNS.

The code to find the "default network interface" is pretty simple, and my guess would be that if there is no default gateway configured on the ESP32 device, the code could accidentally pick the 127.0.0.1 loopback interface as the "default", and the system would end up claiming it's at that address.

So: make sure the ESP32 device actually gets configured with a sensible default gateway value. If the device is in a closed network that has no real route to the internet, use the IP address of the Ubuntu PC as the fake "default gateway" for the ESP32. You don't need to anything special at the Ubuntu PC: the purpose of this is just to ensure the code you pasted sees a "default gateway" associated with the ESP32's physical network interface, and so will pick its IP address to announce over mDNS, rather than 127.0.0.1.

2
  • Thank you for the information. The only problem is that my ESP32 device should be portable. Hence be able to use it across different networks etc. without hard-coding in any network-specific IP addresses etc. Commented Jan 5 at 14:04
  • Then I assume you're using DHCP or similar? To fix the root cause, you should modify the code you quoted so that if it gets IPv4 127.0.0.1 or IPv6 ::1 as the address of the "default" interface, it should skip that interface and try again. You might also check your DHCP server settings and ensure it's not explicitly telling the clients to use 127.0.0.1 as the default gateway address. If it is, that is not correct and even just changing that to some address within the actual network segment might be sufficient to work around the problem for now. Commented Jan 5 at 15:42

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.