From 219415174ed00e3da85cfaa00e89d6a6e9858c84 Mon Sep 17 00:00:00 2001 From: Stroe Andrei Catalin Date: Mon, 1 May 2023 22:44:03 +0300 Subject: [PATCH] [libs] Fix mDNS after a wifi disconnect / reconnect event (#112) * [libs] Fix mDNS not responding when device disconnects / reconnects to wifi * Minor bugfix * Reworked mDNS fix * Update LwIPmDNS.cpp --- cores/beken-72xx/base/config/lwipopts.h | 5 +- .../libraries/common/mDNS/LwIPmDNS.cpp | 95 +++++++++++++++---- cores/realtek-ambz/base/config/lwipopts.h | 3 +- 3 files changed, 82 insertions(+), 21 deletions(-) diff --git a/cores/beken-72xx/base/config/lwipopts.h b/cores/beken-72xx/base/config/lwipopts.h index 13eaa4d..07408f1 100644 --- a/cores/beken-72xx/base/config/lwipopts.h +++ b/cores/beken-72xx/base/config/lwipopts.h @@ -6,8 +6,9 @@ // mDNS support #undef MEMP_NUM_UDP_PCB -#define LWIP_NUM_NETIF_CLIENT_DATA 2 -#define MEMP_NUM_UDP_PCB (MAX_SOCKETS_UDP + 2 + 1) +#define LWIP_NUM_NETIF_CLIENT_DATA 2 +#define LWIP_NETIF_EXT_STATUS_CALLBACK 1 +#define MEMP_NUM_UDP_PCB (MAX_SOCKETS_UDP + 2 + 1) #define ip_addr ip4_addr #define ip_addr_t ip4_addr_t diff --git a/cores/common/arduino/libraries/common/mDNS/LwIPmDNS.cpp b/cores/common/arduino/libraries/common/mDNS/LwIPmDNS.cpp index f590219..40c16ca 100644 --- a/cores/common/arduino/libraries/common/mDNS/LwIPmDNS.cpp +++ b/cores/common/arduino/libraries/common/mDNS/LwIPmDNS.cpp @@ -13,10 +13,17 @@ extern "C" { #include } +static std::vector services_name; static std::vector services; static std::vector protos; +static std::vector ports; static std::vector> records; +static const char *hostName; +#ifdef LWIP_NETIF_EXT_STATUS_CALLBACK +NETIF_DECLARE_EXT_CALLBACK(netif_callback) +#endif + mDNS::mDNS() {} mDNS::~mDNS() {} @@ -37,34 +44,84 @@ static void mdnsStatusCallback(struct netif *netif, uint8_t result) { LT_DM(MDNS, "Status: netif %u, status %u", netif->num, result); } +#ifdef LWIP_NETIF_EXT_STATUS_CALLBACK +static void addServices(struct netif *netif) { + for (uint8_t i = 0; i < services.size(); i++) { + LT_DM( + MDNS, + "Add service: netif %u / %s / %s / %u / %u", + netif->num, + services_name[i], + services[i], + protos[i], + ports[i] + ); + mdns_resp_add_service( + netif, + services_name[i], + services[i], + (mdns_sd_proto)protos[i], + ports[i], + 255, + mdnsTxtCallback, + reinterpret_cast(i) // index of newly added service + ); + } +} +#endif + +static bool enableMDNS(struct netif *netif) { + if (netif_is_up(netif)) { + LT_DM(MDNS, "Starting mDNS on netif %u", netif->num); + if ((netif->flags & NETIF_FLAG_IGMP) == 0) { + netif->flags |= NETIF_FLAG_IGMP; + igmp_start(netif); + LT_DM(MDNS, "Added IGMP to netif %u", netif->num); + } + err_t ret = mdns_resp_add_netif(netif, hostName, 255); + if (ret == ERR_OK) { + LT_DM(MDNS, "mDNS started on netif %u, announcing it to network", netif->num); + mdns_resp_announce(netif); + return true; + } else { + LT_DM(MDNS, "Cannot start mDNS on netif %u; ret=%d, errno=%d", netif->num, ret, errno); + } + } + return false; +} + +#ifdef LWIP_NETIF_EXT_STATUS_CALLBACK +static void +mdns_netif_ext_status_callback(struct netif *netif, netif_nsc_reason_t reason, const netif_ext_callback_args_t *args) { + if (reason & LWIP_NSC_NETIF_REMOVED) { + LT_DM(MDNS, "Netif removed, stopping mDNS on netif %u", netif->num); + mdns_resp_remove_netif(netif); + } else if (reason & LWIP_NSC_STATUS_CHANGED) { + LT_DM(MDNS, "Netif changed, starting mDNS on netif %u", netif->num); + if (enableMDNS(netif) && services.size() > 0) { + LT_DM(MDNS, "Adding services to netif %u", netif->num); + addServices(netif); + } + } +} +#endif + bool mDNS::begin(const char *hostname) { + hostName = strdup(hostname); setInstanceName(hostname); +#ifdef LWIP_NETIF_EXT_STATUS_CALLBACK + netif_add_ext_callback(&netif_callback, mdns_netif_ext_status_callback); +#endif LT_DM(MDNS, "Starting (%s)", hostname); #if LWIP_VERSION_MAJOR >= 2 && LWIP_VERSION_MINOR >= 1 mdns_resp_register_name_result_cb(mdnsStatusCallback); #endif mdns_resp_init(); - uint8_t enabled = 0; - struct netif *netif; for (netif = netif_list; netif != NULL; netif = netif->next) { - if (!netif_is_up(netif)) - continue; - - LT_DM(MDNS, "Adding netif %u", netif->num); - if ((netif->flags & NETIF_FLAG_IGMP) == 0) { - LT_DM(MDNS, "Enabling IGMP"); - netif->flags |= NETIF_FLAG_IGMP; - igmp_start(netif); - } - - err_t ret = mdns_resp_add_netif(netif, hostname, 255); - if (ret == ERR_OK) - enabled++; - else - LT_DM(MDNS, "Cannot add netif %u; ret=%d, errno=%d", netif->num, ret, errno); + enableMDNS(netif); } - return enabled > 0; + return true; } void mDNS::end() { @@ -103,8 +160,10 @@ bool mDNS::addServiceImpl(const char *name, const char *service, uint8_t proto, return false; // add the service to TXT record arrays + services_name.push_back(strdup(name)); services.push_back(strdup(service)); protos.push_back(proto); + ports.push_back(port); records.emplace_back(); return true; diff --git a/cores/realtek-ambz/base/config/lwipopts.h b/cores/realtek-ambz/base/config/lwipopts.h index 7c5a6a9..8c5aa40 100644 --- a/cores/realtek-ambz/base/config/lwipopts.h +++ b/cores/realtek-ambz/base/config/lwipopts.h @@ -5,7 +5,8 @@ #include_next "lwipopts.h" // - 2022-05-23 set LWIP_NUM_NETIF_CLIENT_DATA to 1 -#define LWIP_NUM_NETIF_CLIENT_DATA 1 +#define LWIP_NUM_NETIF_CLIENT_DATA 1 +#define LWIP_NETIF_EXT_STATUS_CALLBACK 1 // - 2022-05-23 set MEMP_NUM_UDP_PCB to 7 #undef MEMP_NUM_UDP_PCB #define MEMP_NUM_UDP_PCB 7