Skip to content

Commit 2450ec6

Browse files
swarrenigrr
authored andcommitted
ESP8266mDNS: support multiple interfaces in query code
Add a loop over all known interfaces to queryService() so that it will find devices attached to all available WiFi networks.
1 parent 4cf7909 commit 2450ec6

File tree

1 file changed

+40
-29
lines changed

1 file changed

+40
-29
lines changed

libraries/ESP8266mDNS/ESP8266mDNS.cpp

Lines changed: 40 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -317,36 +317,47 @@ int MDNSResponder::queryService(char *service, char *proto) {
317317
// Only supports sending one PTR query
318318
uint8_t questionCount = 1;
319319

320-
// Write the header
321-
_conn->flush();
322-
uint8_t head[12] = {
323-
0x00, 0x00, //ID = 0
324-
0x00, 0x00, //Flags = response + authoritative answer
325-
0x00, questionCount, //Question count
326-
0x00, 0x00, //Answer count
327-
0x00, 0x00, //Name server records
328-
0x00, 0x00 //Additional records
329-
};
330-
_conn->append(reinterpret_cast<const char*>(head), 12);
331-
332-
// Only supports sending one PTR query
333-
// Send the Name field (eg. "_http._tcp.local")
334-
_conn->append(reinterpret_cast<const char*>(&serviceNameLen), 1); // lenght of "_" + service
335-
_conn->append(reinterpret_cast<const char*>(serviceName), serviceNameLen); // "_" + service
336-
_conn->append(reinterpret_cast<const char*>(&protoNameLen), 1); // lenght of "_" + proto
337-
_conn->append(reinterpret_cast<const char*>(protoName), protoNameLen); // "_" + proto
338-
_conn->append(reinterpret_cast<const char*>(&localNameLen), 1); // lenght of "local"
339-
_conn->append(reinterpret_cast<const char*>(localName), localNameLen); // "local"
340-
_conn->append(reinterpret_cast<const char*>(&terminator), 1); // terminator
341-
342-
//Send the type and class
343-
uint8_t ptrAttrs[4] = {
344-
0x00, 0x0c, //PTR record query
345-
0x00, 0x01 //Class IN
346-
};
347-
_conn->append(reinterpret_cast<const char*>(ptrAttrs), 4);
348320
_waitingForAnswers = true;
349-
_conn->send();
321+
for (int itfn = 0; itfn < 2; itfn++) {
322+
struct ip_info ip_info;
323+
ip_addr_t ifaddr;
324+
325+
wifi_get_ip_info((!itfn) ? SOFTAP_IF : STATION_IF, &ip_info);
326+
if (!ip_info.ip.addr)
327+
continue;
328+
ifaddr.addr = ip_info.ip.addr;
329+
_conn->setMulticastInterface(ifaddr);
330+
331+
// Write the header
332+
_conn->flush();
333+
uint8_t head[12] = {
334+
0x00, 0x00, //ID = 0
335+
0x00, 0x00, //Flags = response + authoritative answer
336+
0x00, questionCount, //Question count
337+
0x00, 0x00, //Answer count
338+
0x00, 0x00, //Name server records
339+
0x00, 0x00 //Additional records
340+
};
341+
_conn->append(reinterpret_cast<const char*>(head), 12);
342+
343+
// Only supports sending one PTR query
344+
// Send the Name field (eg. "_http._tcp.local")
345+
_conn->append(reinterpret_cast<const char*>(&serviceNameLen), 1); // lenght of "_" + service
346+
_conn->append(reinterpret_cast<const char*>(serviceName), serviceNameLen); // "_" + service
347+
_conn->append(reinterpret_cast<const char*>(&protoNameLen), 1); // lenght of "_" + proto
348+
_conn->append(reinterpret_cast<const char*>(protoName), protoNameLen); // "_" + proto
349+
_conn->append(reinterpret_cast<const char*>(&localNameLen), 1); // lenght of "local"
350+
_conn->append(reinterpret_cast<const char*>(localName), localNameLen); // "local"
351+
_conn->append(reinterpret_cast<const char*>(&terminator), 1); // terminator
352+
353+
//Send the type and class
354+
uint8_t ptrAttrs[4] = {
355+
0x00, 0x0c, //PTR record query
356+
0x00, 0x01 //Class IN
357+
};
358+
_conn->append(reinterpret_cast<const char*>(ptrAttrs), 4);
359+
_conn->send();
360+
}
350361

351362
#ifdef MDNS_DEBUG_TX
352363
Serial.println("Waiting for answers..");

0 commit comments

Comments
 (0)