Skip to content

Commit 9003b02

Browse files
authored
MDNS MultiInterface (#7636)
* MDNS MultiInterface * Move strlcat & strlcpy to __cplusplus * Add LwipIntfCB.cpp to Makefile
1 parent 1c624dd commit 9003b02

File tree

8 files changed

+189
-177
lines changed

8 files changed

+189
-177
lines changed

libraries/ESP8266mDNS/src/LEAmDNS.cpp

+107-107
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,8 @@
2727

2828
#include "ESP8266mDNS.h"
2929
#include "LEAmDNS_Priv.h"
30-
30+
#include <LwipIntf.h> // LwipIntf::stateUpCB()
31+
#include "lwip/igmp.h"
3132

3233
namespace esp8266
3334
{
@@ -61,13 +62,7 @@ MDNSResponder::MDNSResponder(void)
6162
m_pUDPContext(0),
6263
m_pcHostname(0),
6364
m_pServiceQueries(0),
64-
m_fnServiceTxtCallback(0),
65-
#ifdef ENABLE_ESP_MDNS_RESPONDER_PASSIV_MODE
66-
m_bPassivModeEnabled(true),
67-
#else
68-
m_bPassivModeEnabled(false),
69-
#endif
70-
m_netif(nullptr)
65+
m_fnServiceTxtCallback(0)
7166
{
7267
}
7368

@@ -93,104 +88,29 @@ MDNSResponder::~MDNSResponder(void)
9388
Finally the responder is (re)started
9489
9590
*/
96-
bool MDNSResponder::begin(const char* p_pcHostname, const IPAddress& p_IPAddress, uint32_t p_u32TTL)
91+
bool MDNSResponder::begin(const char* p_pcHostname, const IPAddress& /*p_IPAddress*/, uint32_t /*p_u32TTL*/)
9792
{
98-
99-
(void)p_u32TTL; // ignored
10093
bool bResult = false;
10194

102-
if (0 == m_pUDPContext)
103-
{
104-
if (_setHostname(p_pcHostname))
105-
{
106-
107-
//// select interface
108-
109-
m_netif = nullptr;
110-
IPAddress ipAddress = p_IPAddress;
111-
112-
if (!ipAddress.isSet())
113-
{
114-
115-
IPAddress sta = WiFi.localIP();
116-
IPAddress ap = WiFi.softAPIP();
117-
118-
if (sta.isSet())
119-
{
120-
DEBUG_EX_INFO(DEBUG_OUTPUT.printf_P(PSTR("[MDNSResponder] STA interface selected\n")));
121-
ipAddress = sta;
122-
}
123-
else if (ap.isSet())
124-
{
125-
DEBUG_EX_INFO(DEBUG_OUTPUT.printf_P(PSTR("[MDNSResponder] AP interface selected\n")));
126-
ipAddress = ap;
127-
}
128-
else
129-
{
130-
DEBUG_EX_INFO(DEBUG_OUTPUT.printf_P(PSTR("[MDNSResponder] standard interfaces are not up, please specify one in ::begin()\n")));
131-
return false;
132-
}
133-
134-
// continue to ensure interface is UP
135-
}
136-
137-
// check existence of this IP address in the interface list
138-
bool found = false;
139-
m_netif = nullptr;
140-
for (auto a : addrList)
141-
if (ipAddress == a.addr())
142-
{
143-
if (a.ifUp())
144-
{
145-
found = true;
146-
m_netif = a.interface();
147-
break;
148-
}
149-
DEBUG_EX_INFO(DEBUG_OUTPUT.printf_P(PSTR("[MDNSResponder] found interface for IP '%s' but it is not UP\n"), ipAddress.toString().c_str()););
150-
}
151-
if (!found)
152-
{
153-
DEBUG_EX_INFO(DEBUG_OUTPUT.printf_P(PSTR("[MDNSResponder] interface defined by IP '%s' not found\n"), ipAddress.toString().c_str()););
154-
return false;
155-
}
156-
157-
//// done selecting the interface
158-
159-
if (m_netif->num == STATION_IF)
160-
{
161-
162-
m_GotIPHandler = WiFi.onStationModeGotIP([this](const WiFiEventStationModeGotIP & pEvent)
163-
{
164-
(void) pEvent;
165-
// Ensure that _restart() runs in USER context
166-
schedule_function([this]()
167-
{
168-
MDNSResponder::_restart();
169-
});
170-
});
171-
172-
m_DisconnectedHandler = WiFi.onStationModeDisconnected([this](const WiFiEventStationModeDisconnected & pEvent)
173-
{
174-
(void) pEvent;
175-
// Ensure that _restart() runs in USER context
176-
schedule_function([this]()
177-
{
178-
MDNSResponder::_restart();
179-
});
180-
});
181-
}
182-
183-
bResult = _restart();
184-
}
185-
DEBUG_EX_ERR(if (!bResult)
95+
if (_setHostname(p_pcHostname))
18696
{
187-
DEBUG_OUTPUT.printf_P(PSTR("[MDNSResponder] begin: FAILED for '%s'!\n"), (p_pcHostname ? : "-"));
188-
});
97+
bResult = _restart();
18998
}
190-
else
99+
100+
LwipIntf::stateUpCB
101+
(
102+
[this](netif * intf)
191103
{
192-
DEBUG_EX_INFO(DEBUG_OUTPUT.printf_P(PSTR("[MDNSResponder] begin: Ignoring multiple calls to begin (Ignored host domain: '%s')!\n"), (p_pcHostname ? : "-")););
104+
(void)intf;
105+
DEBUG_EX_INFO(DEBUG_OUTPUT.printf_P(PSTR("[MDNSResponder] new Interface '%c%c' is UP! restarting\n"), intf->name[0], intf->name[1]));
106+
_restart();
193107
}
108+
);
109+
DEBUG_EX_ERR(if (!bResult)
110+
{
111+
DEBUG_OUTPUT.printf_P(PSTR("[MDNSResponder] begin: FAILED for '%s'!\n"), (p_pcHostname ? : "-"));
112+
});
113+
194114
return bResult;
195115
}
196116

@@ -207,9 +127,6 @@ bool MDNSResponder::close(void)
207127

208128
if (0 != m_pUDPContext)
209129
{
210-
m_GotIPHandler.reset(); // reset WiFi event callbacks.
211-
m_DisconnectedHandler.reset();
212-
213130
_announce(false, true);
214131
_resetProbeStatus(false); // Stop probing
215132
_releaseServiceQueries();
@@ -1329,11 +1246,6 @@ bool MDNSResponder::notifyAPChange(void)
13291246
*/
13301247
bool MDNSResponder::update(void)
13311248
{
1332-
1333-
if (m_bPassivModeEnabled)
1334-
{
1335-
m_bPassivModeEnabled = false;
1336-
}
13371249
return _process(true);
13381250
}
13391251

@@ -1374,6 +1286,94 @@ MDNSResponder::hMDNSService MDNSResponder::enableArduino(uint16_t p_u16Port,
13741286
return hService;
13751287
}
13761288

1289+
/*
1290+
1291+
MULTICAST GROUPS
1292+
1293+
*/
1294+
1295+
/*
1296+
MDNSResponder::_joinMulticastGroups
1297+
*/
1298+
bool MDNSResponder::_joinMulticastGroups(void)
1299+
{
1300+
bool bResult = false;
1301+
1302+
// Join multicast group(s)
1303+
for (netif* pNetIf = netif_list; pNetIf; pNetIf = pNetIf->next)
1304+
{
1305+
if (netif_is_up(pNetIf))
1306+
{
1307+
#ifdef MDNS_IPV4_SUPPORT
1308+
ip_addr_t multicast_addr_V4 = DNS_MQUERY_IPV4_GROUP_INIT;
1309+
if (!(pNetIf->flags & NETIF_FLAG_IGMP))
1310+
{
1311+
DEBUG_EX_ERR(DEBUG_OUTPUT.printf_P(PSTR("[MDNSResponder] _createHost: Setting flag: flags & NETIF_FLAG_IGMP\n")););
1312+
pNetIf->flags |= NETIF_FLAG_IGMP;
1313+
1314+
if (ERR_OK != igmp_start(pNetIf))
1315+
{
1316+
DEBUG_EX_ERR(DEBUG_OUTPUT.printf_P(PSTR("[MDNSResponder] _createHost: igmp_start FAILED!\n")););
1317+
}
1318+
}
1319+
1320+
if ((ERR_OK == igmp_joingroup_netif(pNetIf, ip_2_ip4(&multicast_addr_V4))))
1321+
{
1322+
bResult = true;
1323+
}
1324+
else
1325+
{
1326+
DEBUG_EX_ERR(DEBUG_OUTPUT.printf_P(PSTR("[MDNSResponder] _createHost: igmp_joingroup_netif(" NETIFID_STR ": %s) FAILED!\n"),
1327+
NETIFID_VAL(pNetIf), IPAddress(multicast_addr_V4).toString().c_str()););
1328+
}
1329+
#endif
1330+
1331+
#ifdef MDNS_IPV6_SUPPORT
1332+
ip_addr_t multicast_addr_V6 = DNS_MQUERY_IPV6_GROUP_INIT;
1333+
bResult = ((bResult) &&
1334+
(ERR_OK == mld6_joingroup_netif(pNetIf, ip_2_ip6(&multicast_addr_V6))));
1335+
DEBUG_EX_ERR_IF(!bResult, DEBUG_OUTPUT.printf_P(PSTR("[MDNSResponder] _createHost: mld6_joingroup_netif (" NETIFID_STR ") FAILED!\n"),
1336+
NETIFID_VAL(pNetIf)));
1337+
#endif
1338+
}
1339+
}
1340+
return bResult;
1341+
}
1342+
1343+
/*
1344+
clsLEAmDNS2_Host::_leaveMulticastGroups
1345+
*/
1346+
bool MDNSResponder::_leaveMulticastGroups()
1347+
{
1348+
bool bResult = false;
1349+
1350+
for (netif* pNetIf = netif_list; pNetIf; pNetIf = pNetIf->next)
1351+
{
1352+
if (netif_is_up(pNetIf))
1353+
{
1354+
bResult = true;
1355+
1356+
// Leave multicast group(s)
1357+
#ifdef MDNS_IPV4_SUPPORT
1358+
ip_addr_t multicast_addr_V4 = DNS_MQUERY_IPV4_GROUP_INIT;
1359+
if (ERR_OK != igmp_leavegroup_netif(pNetIf, ip_2_ip4(&multicast_addr_V4)))
1360+
{
1361+
DEBUG_EX_ERR(DEBUG_OUTPUT.printf_P(PSTR("\n")););
1362+
}
1363+
#endif
1364+
#ifdef MDNS_IPV6_SUPPORT
1365+
ip_addr_t multicast_addr_V6 = DNS_MQUERY_IPV6_GROUP_INIT;
1366+
if (ERR_OK != mld6_leavegroup_netif(pNetIf, ip_2_ip6(&multicast_addr_V6)/*&(multicast_addr_V6.u_addr.ip6)*/))
1367+
{
1368+
DEBUG_EX_ERR(DEBUG_OUTPUT.printf_P(PSTR("\n")););
1369+
}
1370+
#endif
1371+
}
1372+
}
1373+
return bResult;
1374+
}
1375+
1376+
13771377

13781378
} //namespace MDNSImplementation
13791379

libraries/ESP8266mDNS/src/LEAmDNS.h

+7-9
Original file line numberDiff line numberDiff line change
@@ -164,6 +164,10 @@ namespace MDNSImplementation
164164
*/
165165
#define MDNS_QUERYSERVICES_WAIT_TIME 1000
166166

167+
/*
168+
Timeout for udpContext->sendtimeout()
169+
*/
170+
#define MDNS_UDPCONTEXT_TIMEOUT 50
167171

168172
/**
169173
MDNSResponder
@@ -185,6 +189,8 @@ class MDNSResponder
185189
{
186190
return begin(p_strHostname.c_str(), p_IPAddress, p_u32TTL);
187191
}
192+
bool _joinMulticastGroups(void);
193+
bool _leaveMulticastGroups(void);
188194

189195
// Finish MDNS processing
190196
bool close(void);
@@ -1184,6 +1190,7 @@ class MDNSResponder
11841190
~stcMDNSSendParameter(void);
11851191

11861192
bool clear(void);
1193+
bool clearCachedNames(void);
11871194

11881195
bool shiftOffset(uint16_t p_u16Shift);
11891196

@@ -1199,12 +1206,8 @@ class MDNSResponder
11991206
UdpContext* m_pUDPContext;
12001207
char* m_pcHostname;
12011208
stcMDNSServiceQuery* m_pServiceQueries;
1202-
WiFiEventHandler m_DisconnectedHandler;
1203-
WiFiEventHandler m_GotIPHandler;
12041209
MDNSDynamicServiceTxtCallbackFunc m_fnServiceTxtCallback;
1205-
bool m_bPassivModeEnabled;
12061210
stcProbeInformation m_HostProbeInformation;
1207-
const netif* m_netif; // network interface to run on
12081211

12091212
/** CONTROL **/
12101213
/* MAINTENANCE */
@@ -1259,11 +1262,6 @@ class MDNSResponder
12591262
uint16_t p_u16QueryType,
12601263
stcMDNSServiceQuery::stcAnswer* p_pKnownAnswers = 0);
12611264

1262-
const IPAddress _getResponseMulticastInterface() const
1263-
{
1264-
return IPAddress(m_netif->ip_addr);
1265-
}
1266-
12671265
uint8_t _replyMaskForHost(const stcMDNS_RRHeader& p_RRHeader,
12681266
bool* p_pbFullNameMatch = 0) const;
12691267
uint8_t _replyMaskForService(const stcMDNS_RRHeader& p_RRHeader,

0 commit comments

Comments
 (0)