Skip to content

Commit 04494f0

Browse files
authored
Generic netif status callback and mDNS (#8705)
* sprinkle IPAddress(...).isSet() across our loops to avoid polling on a stopped interface. status callback and netif_is_up **does not guarantee and we could use the interface**! * register *one* status callback per instance, e.g. when begin() is called multiple times (also notice a subtle issue with schedule function when instance is delete'ed) * consistent LwipIntf callback signature. no need for rvalue, just pass stdfunc as-is and let the compiler figure it out
1 parent 78444a5 commit 04494f0

File tree

6 files changed

+71
-41
lines changed

6 files changed

+71
-41
lines changed

Diff for: cores/esp8266/LwipIntf.h

+5-4
Original file line numberDiff line numberDiff line change
@@ -34,8 +34,6 @@ class LwipIntf
3434
public:
3535
using CBType = std::function<void(netif*)>;
3636

37-
static bool stateUpCB(LwipIntf::CBType&& cb);
38-
3937
// reorder WiFi.config() parameters for a esp8266/official Arduino dual-compatibility API
4038
// args | esp order arduino order
4139
// ---- + --------- -------------
@@ -67,8 +65,11 @@ class LwipIntf
6765
// ESP32 API compatibility
6866
const char* getHostname();
6967

70-
protected:
71-
static bool stateChangeSysCB(LwipIntf::CBType&& cb);
68+
// whenever netif status callback is called
69+
static bool statusChangeCB(LwipIntf::CBType);
70+
71+
static bool stateUpCB(LwipIntf::CBType);
72+
static bool stateDownCB(LwipIntf::CBType);
7273
};
7374

7475
#endif // _LWIPINTF_H

Diff for: cores/esp8266/LwipIntfCB.cpp

+32-22
Original file line numberDiff line numberDiff line change
@@ -25,44 +25,54 @@
2525
#include <Schedule.h>
2626
#include <debug.h>
2727

28-
#define NETIF_STATUS_CB_SIZE 3
28+
static constexpr size_t LwipIntfCallbacks = 3;
2929

30-
static int netifStatusChangeListLength = 0;
31-
LwipIntf::CBType netifStatusChangeList[NETIF_STATUS_CB_SIZE];
30+
static LwipIntf::CBType callbacks[LwipIntfCallbacks];
31+
static size_t size = 0;
3232

33+
// override empty weak function from glue-lwip
3334
extern "C" void netif_status_changed(struct netif* netif)
3435
{
35-
// override the default empty weak function
36-
for (int i = 0; i < netifStatusChangeListLength; i++)
36+
for (size_t index = 0; index < size; ++index)
3737
{
38-
netifStatusChangeList[i](netif);
38+
callbacks[index](netif);
3939
}
4040
}
4141

42-
bool LwipIntf::stateChangeSysCB(LwipIntf::CBType&& cb)
42+
bool LwipIntf::statusChangeCB(LwipIntf::CBType cb)
4343
{
44-
if (netifStatusChangeListLength >= NETIF_STATUS_CB_SIZE)
44+
if (size < LwipIntfCallbacks)
4545
{
46+
callbacks[size++] = std::move(cb);
47+
return true;
48+
}
4649
#if defined(DEBUG_ESP_CORE)
47-
DEBUGV("NETIF_STATUS_CB_SIZE is too low\n");
50+
DEBUGV("LwipIntf::CB %zu/%zu, cannot add more!\n", size, size);
4851
#endif
49-
return false;
50-
}
5152

52-
netifStatusChangeList[netifStatusChangeListLength++] = cb;
53-
return true;
53+
return false;
54+
}
55+
56+
bool LwipIntf::stateUpCB(LwipIntf::CBType cb)
57+
{
58+
return statusChangeCB(
59+
[cb](netif* interface)
60+
{
61+
if (netif_is_up(interface))
62+
{
63+
cb(interface);
64+
}
65+
});
5466
}
5567

56-
bool LwipIntf::stateUpCB(LwipIntf::CBType&& cb)
68+
bool LwipIntf::stateDownCB(LwipIntf::CBType cb)
5769
{
58-
return stateChangeSysCB(
59-
[cb](netif* nif)
70+
return statusChangeCB(
71+
[cb](netif* interface)
6072
{
61-
if (netif_is_up(nif))
62-
schedule_function(
63-
[cb, nif]()
64-
{
65-
cb(nif);
66-
});
73+
if (!netif_is_up(interface))
74+
{
75+
cb(interface);
76+
}
6777
});
6878
}

Diff for: libraries/ESP8266mDNS/src/LEAmDNS.cpp

+30-13
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@
2727

2828
#include "ESP8266mDNS.h"
2929
#include "LEAmDNS_Priv.h"
30-
#include <LwipIntf.h> // LwipIntf::stateUpCB()
30+
#include <LwipIntf.h>
3131
#include <lwip/igmp.h>
3232
#include <lwip/prot/dns.h>
3333

@@ -54,7 +54,7 @@ namespace MDNSImplementation
5454
*/
5555
MDNSResponder::MDNSResponder(void) :
5656
m_pServices(0), m_pUDPContext(0), m_pcHostname(0), m_pServiceQueries(0),
57-
m_fnServiceTxtCallback(0)
57+
m_fnServiceTxtCallback(0), m_bLwipCb(false), m_bRestarting(false)
5858
{
5959
}
6060

@@ -89,17 +89,34 @@ namespace MDNSImplementation
8989
bResult = _restart();
9090
}
9191

92-
LwipIntf::stateUpCB(
93-
[this](netif* intf)
94-
{
95-
if (IPAddress(intf->ip_addr).isSet())
92+
if (bResult && !m_bLwipCb)
93+
{
94+
bool bCallback = LwipIntf::statusChangeCB(
95+
[this](netif*)
9696
{
97-
DEBUG_EX_INFO(DEBUG_OUTPUT.printf_P(
98-
PSTR("[MDNSResponder] new Interface '%c%c' is UP! restarting\n"),
99-
intf->name[0], intf->name[1]));
100-
_restart();
101-
}
97+
if (m_bRestarting)
98+
{
99+
return;
100+
}
101+
102+
m_bRestarting = true;
103+
schedule_function(
104+
[this]()
105+
{
106+
DEBUG_EX_INFO(
107+
DEBUG_OUTPUT.printf_P(PSTR("[MDNSResponder] begin: restarting "
108+
"after interface status changed\n")););
109+
_restart();
110+
m_bRestarting = false;
111+
});
112+
});
113+
DEBUG_EX_ERR(if (!bCallback) {
114+
DEBUG_OUTPUT.printf_P(
115+
PSTR("[MDNSResponder] begin: FAILED LwipIntf::statusChangeCB!\n"));
102116
});
117+
m_bLwipCb = bCallback;
118+
}
119+
103120
DEBUG_EX_ERR(if (!bResult) {
104121
DEBUG_OUTPUT.printf_P(PSTR("[MDNSResponder] begin: FAILED for '%s'!\n"),
105122
(p_pcHostname ?: "-"));
@@ -1281,7 +1298,7 @@ namespace MDNSImplementation
12811298
// Join multicast group(s)
12821299
for (netif* pNetIf = netif_list; pNetIf; pNetIf = pNetIf->next)
12831300
{
1284-
if (netif_is_up(pNetIf))
1301+
if (netif_is_up(pNetIf) && IPAddress(pNetIf->ip_addr).isSet())
12851302
{
12861303
#ifdef MDNS_IP4_SUPPORT
12871304
ip_addr_t multicast_addr_V4 = DNS_MQUERY_IPV4_GROUP_INIT;
@@ -1337,7 +1354,7 @@ namespace MDNSImplementation
13371354

13381355
for (netif* pNetIf = netif_list; pNetIf; pNetIf = pNetIf->next)
13391356
{
1340-
if (netif_is_up(pNetIf))
1357+
if (netif_is_up(pNetIf) && IPAddress(pNetIf->ip_addr).isSet())
13411358
{
13421359
bResult = true;
13431360

Diff for: libraries/ESP8266mDNS/src/LEAmDNS.h

+2
Original file line numberDiff line numberDiff line change
@@ -1174,6 +1174,8 @@ namespace MDNSImplementation
11741174
stcMDNSServiceQuery* m_pServiceQueries;
11751175
MDNSDynamicServiceTxtCallbackFunc m_fnServiceTxtCallback;
11761176
stcProbeInformation m_HostProbeInformation;
1177+
bool m_bLwipCb;
1178+
bool m_bRestarting;
11771179

11781180
/** CONTROL **/
11791181
/* MAINTENANCE */

Diff for: libraries/ESP8266mDNS/src/LEAmDNS_Control.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -2228,7 +2228,7 @@ namespace MDNSImplementation
22282228
stcMDNS_RRDomain reverseIP4Domain;
22292229
for (netif* pNetIf = netif_list; pNetIf; pNetIf = pNetIf->next)
22302230
{
2231-
if (netif_is_up(pNetIf))
2231+
if (netif_is_up(pNetIf) && IPAddress(pNetIf->ip_addr).isSet())
22322232
{
22332233
if ((_buildDomainForReverseIP4(pNetIf->ip_addr, reverseIP4Domain))
22342234
&& (p_RRHeader.m_Domain == reverseIP4Domain))

Diff for: libraries/ESP8266mDNS/src/LEAmDNS_Transfer.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -122,7 +122,7 @@ namespace MDNSImplementation
122122

123123
for (netif* pNetIf = netif_list; pNetIf; pNetIf = pNetIf->next)
124124
{
125-
if (netif_is_up(pNetIf))
125+
if (netif_is_up(pNetIf) && IPAddress(pNetIf->ip_addr).isSet())
126126
{
127127
IPAddress fromIPAddress;
128128
// fromIPAddress = _getResponseMulticastInterface();

0 commit comments

Comments
 (0)