[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
This commit is contained in:
Stroe Andrei Catalin
2023-05-01 22:44:03 +03:00
committed by GitHub
parent 8999cb9091
commit 219415174e
3 changed files with 82 additions and 21 deletions

View File

@@ -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

View File

@@ -13,10 +13,17 @@ extern "C" {
#include <lwip/netif.h>
}
static std::vector<char *> services_name;
static std::vector<char *> services;
static std::vector<uint8_t> protos;
static std::vector<uint16_t> ports;
static std::vector<std::vector<char *>> 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<void *>(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;

View File

@@ -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