1
0
mirror of https://github.com/esp8266/Arduino.git synced 2025-07-14 13:41:23 +03:00

MDNS: fix random crash on startup (#6261)

* mDNS debug option + AP address is used by default when STA is also present

* mDNS: store network interface, checking it is up

* igmp: force on selected interface (avoid crash *sometimes*)

* fix for all lwip2 ipv4 ipv6 & lwip1

* mdns: IPAddress is not needed to reference associated interface

* mdns: debug: fix print warnings

* emulation: add ets_strncpy

* emulation: truly emulate AddrList (remove fake one)
This commit is contained in:
david gauchard
2019-09-05 05:10:47 +02:00
committed by Earle F. Philhower, III
parent 273f4000f0
commit 5ca0bde200
12 changed files with 288 additions and 315 deletions

View File

@ -23,6 +23,7 @@
*/
#include <Schedule.h>
#include <AddrList.h>
#include "LEAmDNS_Priv.h"
@ -59,11 +60,11 @@ MDNSResponder::MDNSResponder(void)
m_pServiceQueries(0),
m_fnServiceTxtCallback(0),
#ifdef ENABLE_ESP_MDNS_RESPONDER_PASSIV_MODE
m_bPassivModeEnabled(true) {
m_bPassivModeEnabled(true),
#else
m_bPassivModeEnabled(false) {
m_bPassivModeEnabled(false),
#endif
m_netif(nullptr) {
}
/*
@ -95,20 +96,74 @@ bool MDNSResponder::begin(const char* p_pcHostname, const IPAddress& p_IPAddress
if (0 == m_pUDPContext) {
if (_setHostname(p_pcHostname)) {
m_IPAddress = p_IPAddress;
//// select interface
m_GotIPHandler = WiFi.onStationModeGotIP([this](const WiFiEventStationModeGotIP& pEvent) {
(void) pEvent;
// Ensure that _restart() runs in USER context
schedule_function([this]() { MDNSResponder::_restart(); });
});
m_netif = nullptr;
IPAddress ipAddress = p_IPAddress;
m_DisconnectedHandler = WiFi.onStationModeDisconnected([this](const WiFiEventStationModeDisconnected& pEvent) {
(void) pEvent;
// Ensure that _restart() runs in USER context
schedule_function([this]() { MDNSResponder::_restart(); });
if (!ipAddress.isSet()) {
IPAddress sta = WiFi.localIP();
IPAddress ap = WiFi.softAPIP();
if (!sta.isSet() && !ap.isSet()) {
DEBUG_EX_INFO(DEBUG_OUTPUT.printf_P(PSTR("[MDNSResponder] internal interfaces (STA, AP) are not set (none was specified)\n")));
return false;
}
if (ap.isSet()) {
if (sta.isSet())
DEBUG_EX_INFO(DEBUG_OUTPUT.printf_P(PSTR("[MDNSResponder] default interface AP selected over STA (none was specified)\n")));
else
DEBUG_EX_INFO(DEBUG_OUTPUT.printf_P(PSTR("[MDNSResponder] default interface AP selected\n")));
ipAddress = ap;
} else {
DEBUG_EX_INFO(DEBUG_OUTPUT.printf_P(PSTR("[MDNSResponder] default interface STA selected (none was specified)\n")));
ipAddress = sta;
}
// continue to ensure interface is UP
}
// check existence of this IP address in the interface list
bool found = false;
m_netif = nullptr;
for (auto a: addrList)
if (ipAddress == a.addr()) {
if (a.ifUp()) {
found = true;
m_netif = a.interface();
break;
}
DEBUG_EX_INFO(DEBUG_OUTPUT.printf_P(PSTR("[MDNSResponder] found interface for IP '%s' but it is not UP\n"), ipAddress.toString().c_str()););
}
if (!found) {
DEBUG_EX_INFO(DEBUG_OUTPUT.printf_P(PSTR("[MDNSResponder] interface defined by IP '%s' not found\n"), ipAddress.toString().c_str()););
return false;
}
//// done selecting the interface
if (m_netif->num == STATION_IF) {
m_GotIPHandler = WiFi.onStationModeGotIP([this](const WiFiEventStationModeGotIP& pEvent) {
(void) pEvent;
// Ensure that _restart() runs in USER context
schedule_function([this]() { MDNSResponder::_restart(); });
});
m_DisconnectedHandler = WiFi.onStationModeDisconnected([this](const WiFiEventStationModeDisconnected& pEvent) {
(void) pEvent;
// Ensure that _restart() runs in USER context
schedule_function([this]() { MDNSResponder::_restart(); });
});
}
bResult = _restart();
}
DEBUG_EX_ERR(if (!bResult) { DEBUG_OUTPUT.printf_P(PSTR("[MDNSResponder] begin: FAILED for '%s'!\n"), (p_pcHostname ?: "-")); } );
@ -642,7 +697,7 @@ uint32_t MDNSResponder::queryService(const char* p_pcService,
}
}
else {
DEBUG_EX_ERR(DEBUG_OUTPUT.printf_P(PSTR("[MDNSResponder] queryService: INVALID input data!\n"), p_pcService, p_pcProtocol););
DEBUG_EX_ERR(DEBUG_OUTPUT.printf_P(PSTR("[MDNSResponder] queryService: INVALID input data!\n")););
}
return u32Result;
}