Skip to content

Commit

Permalink
Merge pull request project-chip#154 from lucasvr/lucas/mdns-cache-flag
Browse files Browse the repository at this point in the history
mDNS: always set the "cache flush" bit
  • Loading branch information
andy31415 authored Jul 26, 2024
2 parents 40e3820 + 6fc803c commit d03c5f3
Showing 1 changed file with 20 additions and 5 deletions.
25 changes: 20 additions & 5 deletions rs-matter/src/mdns/proto.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,13 @@ use crate::error::{Error, ErrorCode};

use super::Service;

/// Internet DNS class with the "Cache Flush" bit set.
/// See https://datatracker.ietf.org/doc/html/rfc6762#section-10.2 for details.
fn dns_class_with_flush(dns_class: Class) -> Class {
const RESOURCE_RECORD_CACHE_FLUSH_BIT: u16 = 0x8000;
Class::from_int(u16::from(dns_class) | RESOURCE_RECORD_CACHE_FLUSH_BIT)
}

impl From<ShortBuf> for Error {
fn from(_: ShortBuf) -> Self {
Self::new(ErrorCode::NoSpace)
Expand Down Expand Up @@ -417,7 +424,7 @@ impl<'a> Host<'a> {
{
answer.push((
Self::host_fqdn(self.hostname, false).unwrap(),
Class::IN,
dns_class_with_flush(Class::IN),
ttl_sec,
A::from_octets(self.ip[0], self.ip[1], self.ip[2], self.ip[3]),
))
Expand All @@ -431,7 +438,7 @@ impl<'a> Host<'a> {
if let Some(ip) = &self.ipv6 {
answer.push((
Self::host_fqdn(self.hostname, false).unwrap(),
Class::IN,
dns_class_with_flush(Class::IN),
ttl_sec,
Aaaa::new((*ip).into()),
))
Expand Down Expand Up @@ -463,7 +470,7 @@ impl<'a> Service<'a> {
{
answer.push((
self.service_fqdn(false).unwrap(),
Class::IN,
dns_class_with_flush(Class::IN),
ttl_sec,
Srv::new(0, 0, self.port, Host::host_fqdn(hostname, false).unwrap()),
))
Expand All @@ -489,7 +496,7 @@ impl<'a> Service<'a> {
{
answer.push((
Self::dns_sd_fqdn(false).unwrap(),
Class::IN,
dns_class_with_flush(Class::IN),
ttl_sec,
Ptr::new(self.service_type_fqdn(false).unwrap()),
))
Expand Down Expand Up @@ -533,6 +540,9 @@ impl<'a> Service<'a> {
R: RecordSectionBuilder<T>,
T: Composer,
{
// Don't set the flush-bit when sending this PTR record, as we're not the
// authority of dns_sd_fqdn: there may be answers from other devices on
// the network as well.
answer.push((
self.service_subtype_fqdn(service_subtype, false).unwrap(),
Class::IN,
Expand Down Expand Up @@ -582,7 +592,12 @@ impl<'a> Service<'a> {

let txt = Txt::from_octets(&octets).unwrap();

answer.push((self.service_fqdn(false).unwrap(), Class::IN, ttl_sec, txt))
answer.push((
self.service_fqdn(false).unwrap(),
dns_class_with_flush(Class::IN),
ttl_sec,
txt,
))
}
}

Expand Down

0 comments on commit d03c5f3

Please sign in to comment.