| 1522 | } |
| 1523 | |
| 1524 | static void HandleIncomingQueries(MDNS* mdns) |
| 1525 | { |
| 1526 | // Minimal RFC 6762 responder loop: |
| 1527 | // parse multicast questions, match them against locally registered |
| 1528 | // authoritative records, and emit the corresponding record set. |
| 1529 | bool incoming_data = false; |
| 1530 | do |
| 1531 | { |
| 1532 | incoming_data = false; |
| 1533 | dmSocket::Selector selector; |
| 1534 | dmSocket::SelectorZero(&selector); |
| 1535 | dmSocket::SelectorSet(&selector, dmSocket::SELECTOR_KIND_READ, mdns->m_Socket); |
| 1536 | |
| 1537 | if (dmSocket::Select(&selector, 0) != dmSocket::RESULT_OK) |
| 1538 | return; |
| 1539 | |
| 1540 | if (!dmSocket::SelectorIsSet(&selector, dmSocket::SELECTOR_KIND_READ, mdns->m_Socket)) |
| 1541 | return; |
| 1542 | |
| 1543 | dmSocket::Address from_address; |
| 1544 | uint16_t from_port = 0; |
| 1545 | int received = 0; |
| 1546 | dmSocket::Result sr = dmSocket::ReceiveFrom(mdns->m_Socket, mdns->m_Buffer, sizeof(mdns->m_Buffer), &received, &from_address, &from_port); |
| 1547 | if (sr != dmSocket::RESULT_OK || received <= 0) |
| 1548 | return; |
| 1549 | |
| 1550 | (void) from_address; |
| 1551 | (void) from_port; |
| 1552 | |
| 1553 | incoming_data = true; |
| 1554 | |
| 1555 | uint32_t size = (uint32_t) received; |
| 1556 | if (size < 12) |
| 1557 | continue; |
| 1558 | |
| 1559 | uint32_t offset = 0; |
| 1560 | uint16_t id = 0; |
| 1561 | uint16_t flags = 0; |
| 1562 | uint16_t qdcount = 0; |
| 1563 | uint16_t ancount = 0; |
| 1564 | uint16_t nscount = 0; |
| 1565 | uint16_t arcount = 0; |
| 1566 | |
| 1567 | if (!ReadU16(mdns->m_Buffer, size, &offset, &id) |
| 1568 | || !ReadU16(mdns->m_Buffer, size, &offset, &flags) |
| 1569 | || !ReadU16(mdns->m_Buffer, size, &offset, &qdcount) |
| 1570 | || !ReadU16(mdns->m_Buffer, size, &offset, &ancount) |
| 1571 | || !ReadU16(mdns->m_Buffer, size, &offset, &nscount) |
| 1572 | || !ReadU16(mdns->m_Buffer, size, &offset, &arcount)) |
| 1573 | { |
| 1574 | continue; |
| 1575 | } |
| 1576 | |
| 1577 | (void) id; |
| 1578 | if ((flags & 0x8000) != 0) |
| 1579 | { |
| 1580 | const uint32_t records = (uint32_t) ancount + (uint32_t) nscount + (uint32_t) arcount; |
| 1581 | HandleIncomingResponseRecords(mdns, mdns->m_Buffer, size, &offset, records); |
no test coverage detected