MDNS transmit failure

brandondbz
Posts: 1
Joined: Thu Sep 25, 2025 11:56 pm

MDNS transmit failure

Postby brandondbz » Wed Oct 29, 2025 11:55 pm

So this is a bit of an odd one. I am using the official MDNS library

Code: Select all

dependencies:
  espressif/mdns: ^1.8.2
I have it setup to initialize on STA IP got (It was more reliable to do it this way than to do it on first init, also used menuconfig to specify STA interface only, though problem has occured with default (STA, AP, ETH) mode). I have also both tried default and bsd sockets, but with same error. And I have also tried both wifi connected via APSTA and via STA modes and still get the error.
So, The error is that mdns will stop working randomly after a long period (hours or sometimes days). Enabling logging and adding a raw socket to look for incoming udp, I have verified
1)the mdns requests to come int
2)the mdns library recognizes the request
3)the mdns library calls

Code: Select all

_mdns_udp_pcb_write(p->tcpip_if, p->ip_protocol, &p->dst, p->port, packet, index);
as the last line (verified by the transmission of the TX debug line)
4)no packet recieved by other devices on the network (verified by wireshark seeing the outgoing question packets but not receiving the esp's answer packets despite the logging saying packets were sent)
5)this is inconsistent (sometimes works fine days where 'TX' means packet gets out) and on some router fails immediately
6)TCP packets still send (so at least it is not a full lwip failure as httpd still works by using ip address instead of the .local)

for reference, here is the mdns init/reinit code

Code: Select all

#define TAG "MDNS"
static int mdns_state=0;


void stop_mdns(void){
    if(mdns_state==0)return;
    mdns_free();
    mdns_state=0;
}
//just help with logging
#define WK_ERROR_CHECK(x) do {                                         \
        esp_err_t err_rc_ = (x);                                        \
        if (unlikely(err_rc_ != ESP_OK)) {                              \
            ESP_LOGE("WK", "%d, %s, %d,%s ,%s", (int)err_rc_, __FILE__, (int)__LINE__,        \
                                    __func__, #x);                 \
        }                                                               \
    } while(0)

//from example, small tweaks (just note that hostname is const truely, not stack bound though I don't think it should matter)    
void initialise_mdns(const char * hostname)
{
    if(mdns_state==1)return;//prevent double init
    // initialize mDNS
    WK_ERROR_CHECK(mdns_init());
    mdns_state=1;
    // set mDNS hostname (required if you want to advertise services)
    WK_ERROR_CHECK(mdns_hostname_set(hostname));
    ESP_LOGI(TAG, "mdns hostname set to: [%s]", hostname);
    // set default mDNS instance name
    WK_ERROR_CHECK(mdns_instance_name_set(hostname));
    // structure with TXT records
    
    //next escalation
    //mdns_register_netif(x);
    // initialize service
    WK_ERROR_CHECK(mdns_service_add(hostname, "_http", "_tcp", 80,NULL,0)); // serviceTxtData, 2));
    WK_ERROR_CHECK(mdns_service_subtype_add_for_host(hostname, "_http", "_tcp", NULL, "_server"));
    // #if CONFIG_MDNS_MULTIPLE_INSTANCE
    //     WK_ERROR_CHECK(mdns_service_add("ESP32-WebServer1", "_http", "_tcp", 80, NULL, 0));
    // #endif
    // add another TXT item
    //WK_ERROR_CHECK(mdns_service_txt_item_set("_http", "_tcp", "path", "/"));
    // change TXT item value
    // WK_ERROR_CHECK(mdns_service_txt_item_set_with_explicit_value_len("_http", "_tcp", "u", "admin", strlen("admin")));
    mdns_state=1;
}
//reinit on new IP prevents long wait times when connecting new network
void lazy_restart_mdns(const char* hostname){
    if(mdns_state){stop_mdns();vTaskDelay(100/portTICK_PERIOD_MS);}
    initialise_mdns(hostname);
}


Who is online

Users browsing this forum: Baidu [Spider], ChatGPT-User and 3 guests