Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
75 changes: 75 additions & 0 deletions libraries/ESP8266mDNS/src/LEAmDNS.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -671,6 +671,81 @@ namespace MDNSImplementation
return addDynamicServiceTxt(p_hService, p_pcKey, acBuffer);
}

#ifdef MDNS_IP4_SUPPORT
/*
MDNSResponder::queryHost
Perform a blocking mDNS A query for a host.
*/
IPAddress MDNSResponder::queryHost(const char* p_pcHostname,
const uint32_t p_u32Timeout /*= MDNS_QUERYHOST_WAIT_TIME*/)
{
if (0 == m_pUDPContext)
{
// safeguard against misuse
return IPAddress();
}

DEBUG_EX_INFO(DEBUG_OUTPUT.printf_P(PSTR("[MDNSResponder] queryHost '%s'\n"),
(p_pcHostname ?: "-")););

IPAddress result;

stcMDNSServiceQuery* pServiceQuery = 0;
stcMDNS_RRDomain hostDomain;

if ((p_pcHostname) && (os_strlen(p_pcHostname)) && (p_u32Timeout)
&& (_removeLegacyServiceQuery()) && (_buildDomainForHost(p_pcHostname, hostDomain))
&& ((pServiceQuery = _allocServiceQuery())))
{
pServiceQuery->m_bLegacyQuery = true;

stcMDNSServiceQuery::stcAnswer* pAnswer = new stcMDNSServiceQuery::stcAnswer;

if (pAnswer)
{
pAnswer->m_HostDomain = hostDomain;

if ((pServiceQuery->addAnswer(pAnswer))
&& (_sendMDNSQuery(hostDomain, DNS_RRTYPE_A)))
{
DEBUG_EX_INFO(DEBUG_OUTPUT.printf_P(
PSTR("[MDNSResponder] queryHost: Waiting up to %lu ms for answer...\n"),
p_u32Timeout););

const uint32_t start = millis();

do
{
delay(1);

const stcMDNSServiceQuery::stcAnswer::stcIP4Address* pIP4Address
= pAnswer->IP4AddressAtIndex(0);

if (pIP4Address)
{
result = pIP4Address->m_IPAddress;
break;
}
} while ((millis() - start) < p_u32Timeout);
}
}

pServiceQuery->m_bAwaitingAnswers = false;
_removeServiceQuery(pServiceQuery);
}
else
{
if (pServiceQuery)
_removeServiceQuery(pServiceQuery);

DEBUG_EX_ERR(
DEBUG_OUTPUT.printf_P(PSTR("[MDNSResponder] queryHost: INVALID input data!\n")););
}

return result;
}
#endif

/**
STATIC SERVICE QUERY (LEGACY)
*/
Expand Down
17 changes: 17 additions & 0 deletions libraries/ESP8266mDNS/src/LEAmDNS.h
Original file line number Diff line number Diff line change
Expand Up @@ -167,6 +167,10 @@ namespace MDNSImplementation
Default timeout for static service queries
*/
#define MDNS_QUERYSERVICES_WAIT_TIME 1000
/*
Default timeout for static host queries
*/
#define MDNS_QUERYHOST_WAIT_TIME 2000

/*
Timeout for udpContext->sendtimeout()
Expand Down Expand Up @@ -312,6 +316,19 @@ namespace MDNSImplementation
hMDNSTxt addDynamicServiceTxt(hMDNSService p_hService, const char* p_pcKey,
int8_t p_i8Value);

#ifdef MDNS_IP4_SUPPORT
// Resolve a host name using an mDNS A query.
// The ".local" suffix is appended automatically.
IPAddress queryHost(const char* p_pcHostname,
const uint32_t p_u32Timeout = MDNS_QUERYHOST_WAIT_TIME);

IPAddress queryHost(const String& p_strHostname,
const uint32_t p_u32Timeout = MDNS_QUERYHOST_WAIT_TIME)
{
return queryHost(p_strHostname.c_str(), p_u32Timeout);
}
#endif

// Perform a (static) service query. The function returns after p_u16Timeout milliseconds
// The answers (the number of received answers is returned) can be retrieved by calling
// - answerHostname (or hostname)
Expand Down