keepsimple1 / mdns-sd

Rust library for mDNS based Service Discovery
Apache License 2.0
104 stars 39 forks source link

dns_cache: address record should only flush on the same network #261

Closed keepsimple1 closed 1 month ago

keepsimple1 commented 1 month ago

When debugging an issue in Discussions #260 , I installed docker on macOS, and the test case integration_success fails intermittently.

With some digging, it seems that: there are IPv6 interfaces that have more than 1 IPv6 address configured. I.e. same interface name same interface index, but different IPv6 address. Because IPv6 sockets joins multicast using their index, not the address, there are two sockets receive packets for both addresses. Depends on the timing, one of the addresses could be flushed by another one and cause the resolved service has less addresses than expected.

The following log shows this happened with two cases, and resolved 16 addr(s) instead of expected 18 addr(s):

Expired addr: DnsAddress { record: DnsRecord { entry: DnsEntry { name: "integration_host.local.", ty: 28, class: 1, cache_flush: true }, ttl: 120, created: 1727984047922, expires: 1727984049923, refresh: 1727984143922 }, address: fd5b:7dca:54f1::2 } at now: 1727984049925
Expired addr: DnsAddress { record: DnsRecord { entry: DnsEntry { name: "integration_host.local.", ty: 28, class: 1, cache_flush: true }, ttl: 120, created: 1727984047922, expires: 1727984049923, refresh: 1727984143922 }, address: fdc9:76a:d895:9ee4:426:ddf3:376f:23d6 } at now: 1727984049925
Expired: integration_host.local. addr {fd5b:7dca:54f1::2, fdc9:76a:d895:9ee4:426:ddf3:376f:23d6}
Resolved a new service: 1727984047917628._mdns-sd-it._udp.local. with 16 addr(s)

This patch adds a check for cache flush: for address records, we only flush if the incoming address and the existing address are on the same network of the interface.

Also, changed a error log into a debug log per the discussions comment.