Skip to content

Commit 13f52c9

Browse files
authored
[libs] Fix mDNS double netif initialization (#309)
* Track mDNS initialization state on each netif num LWIP assertion clearly shows that calling mdns_resp_add_netif twice for the same interface is a no-no. So to prevent this we need to be able to easily check if this was already called. This PR adds a simple map that keeps track of the initialization state per each interface (identified via netif->num). * Code formatting
1 parent fcbe5be commit 13f52c9

File tree

1 file changed

+33
-5
lines changed

1 file changed

+33
-5
lines changed

cores/common/arduino/libraries/common/mDNS/LwIPmDNS.cpp

Lines changed: 33 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
#if LT_HAS_LWIP2
44

55
#include "mDNS.h"
6+
#include <map>
67
#include <vector>
78

89
extern "C" {
@@ -65,6 +66,14 @@ static const char *hostName;
6566
NETIF_DECLARE_EXT_CALLBACK(netif_callback)
6667
#endif
6768

69+
enum StateForNetIf {
70+
DISABLED = 0, // not yet enabled
71+
ENABLED, // enabled, but no services added
72+
CONFIGURED // enabled and services added
73+
};
74+
75+
static std::map<uint8_t, StateForNetIf> sNetIfState; // netif->num --> state
76+
6877
mDNS::mDNS() {}
6978

7079
mDNS::~mDNS() {
@@ -171,12 +180,27 @@ static void mdns_netif_ext_status_callback(
171180
netif_nsc_reason_t reason,
172181
const netif_ext_callback_args_t *args
173182
) {
183+
auto stateIter = sNetIfState.find(netif->num);
184+
if (stateIter == sNetIfState.end()) {
185+
stateIter = sNetIfState.insert(std::make_pair(netif->num, StateForNetIf::DISABLED)).first;
186+
}
187+
auto currentState = sNetIfState[netif->num];
174188
if (reason & LWIP_NSC_NETIF_REMOVED) {
175-
LT_DM(MDNS, "Netif removed, stopping mDNS on netif %u", netif->num);
176-
mdns_resp_remove_netif(netif);
189+
if (StateForNetIf::DISABLED != stateIter->second) {
190+
LT_DM(MDNS, "Netif removed, stopping mDNS on netif %u", netif->num);
191+
mdns_resp_remove_netif(netif);
192+
sNetIfState.erase(stateIter);
193+
}
177194
} else if ((reason & LWIP_NSC_STATUS_CHANGED) || (reason & LWIP_NSC_NETIF_ADDED)) {
178-
LT_DM(MDNS, "Netif changed/added, starting mDNS on netif %u", netif->num);
179-
if (enableMDNS(netif) && sCachedServices.size() > 0) {
195+
if (StateForNetIf::DISABLED == stateIter->second) {
196+
LT_DM(MDNS, "Starting mDNS on netif %u", netif->num);
197+
if (enableMDNS(netif)) {
198+
stateIter->second = StateForNetIf::ENABLED;
199+
}
200+
}
201+
202+
if ((StateForNetIf::ENABLED == stateIter->second) && (sCachedServices.size() > 0)) {
203+
stateIter->second = StateForNetIf::CONFIGURED;
180204
LT_DM(MDNS, "Adding services to netif %u", netif->num);
181205
addServices(netif);
182206
}
@@ -195,9 +219,13 @@ bool mDNS::begin(const char *hostname) {
195219
mdns_resp_register_name_result_cb(mdnsStatusCallback);
196220
#endif
197221
mdns_resp_init();
222+
198223
struct netif *netif;
199224
for (netif = netif_list; netif != NULL; netif = netif->next) {
200-
enableMDNS(netif);
225+
sNetIfState[netif->num] = StateForNetIf::DISABLED;
226+
if (enableMDNS(netif)) {
227+
sNetIfState[netif->num] = StateForNetIf::ENABLED;
228+
}
201229
}
202230
return true;
203231
}

0 commit comments

Comments
 (0)