use crate::protocols::error::{PacketError, Result};
use dns_parser::{Packet, RData};
use std::{
collections::{BTreeMap, BTreeSet},
net::IpAddr,
};
use crate::protocols::dns;
const MULTICAST_QUERY_ID: u16 = 0;
pub const PORT: u16 = 5353;
pub fn build_query(name: &str) -> Result<Vec<u8>> {
dns::build_query(
MULTICAST_QUERY_ID,
false,
&[(name, dns::record_type::A), (name, dns::record_type::AAAA)],
)
}
#[non_exhaustive]
#[derive(Debug, Clone, Default, PartialEq, Eq)]
pub struct MdnsHost {
pub hostname: String,
pub ips: BTreeSet<IpAddr>,
}
pub fn device_info_name(hostname: &str) -> Option<String> {
let name = hostname.trim_end_matches('.');
let host = match name.rsplit_once('.') {
Some((host, zone)) if zone.eq_ignore_ascii_case("local") => host,
Some(_) => return None,
None if name.eq_ignore_ascii_case("local") => return None,
None => name,
};
(!host.is_empty()).then(|| format!("{host}._device-info._tcp.local"))
}
pub fn build_reverse_query(ip: IpAddr) -> Result<Vec<u8>> {
dns::build_query(
MULTICAST_QUERY_ID,
false,
&[(&dns::reverse_pointer_name(ip), dns::record_type::PTR)],
)
}
pub fn build_device_info_query(hostname: &str) -> Option<Result<Vec<u8>>> {
let name = device_info_name(hostname)?;
Some(dns::build_query(
MULTICAST_QUERY_ID,
false,
&[(&name, dns::record_type::TXT)],
))
}
pub fn text_records(data: &[u8]) -> Result<Vec<String>> {
let packet =
Packet::parse(data).map_err(|error| PacketError::unreadable("an mDNS message", error))?;
Ok(packet
.answers
.iter()
.chain(packet.additional.iter())
.filter_map(|record| match &record.data {
RData::TXT(txt) => Some(txt.iter()),
_ => None,
})
.flatten()
.filter_map(|chunk| String::from_utf8(chunk.to_vec()).ok())
.filter(|text| !text.is_empty())
.collect())
}
pub fn extract_hosts(data: &[u8]) -> Result<Vec<MdnsHost>> {
let packet =
Packet::parse(data).map_err(|error| PacketError::unreadable("an mDNS message", error))?;
let mut by_hostname: BTreeMap<String, BTreeSet<IpAddr>> = BTreeMap::new();
for record in packet.answers.iter().chain(packet.additional.iter()) {
let (hostname, ip) = match &record.data {
RData::A(a) => (owner_name(&record.name), IpAddr::V4(a.0)),
RData::AAAA(aaaa) => (owner_name(&record.name), IpAddr::V6(aaaa.0)),
RData::PTR(ptr) => match dns::address_from_pointer_name(&record.name.to_string()) {
Some(ip) => (trim_root(&ptr.0.to_string()), ip),
None => continue,
},
_ => continue,
};
by_hostname.entry(hostname).or_default().insert(ip);
}
Ok(by_hostname
.into_iter()
.map(|(hostname, ips)| MdnsHost { hostname, ips })
.collect())
}
fn owner_name(name: &dns_parser::Name<'_>) -> String {
trim_root(&name.to_string())
}
fn trim_root(name: &str) -> String {
name.trim_end_matches('.').to_string()
}
#[cfg(test)]
mod tests {
use super::*;
use dns_parser::QueryType;
#[test]
fn an_address_record_names_its_owner() {
let message = response(&[
record("raspberrypi.local", Rdata::A([203, 0, 113, 150])),
record("raspberrypi.local", Rdata::Aaaa("fe80::1".parse().unwrap())),
]);
assert_eq!(
extract_hosts(&message).unwrap(),
vec![MdnsHost {
hostname: "raspberrypi.local".to_string(),
ips: BTreeSet::from(["203.0.113.150".parse().unwrap(), "fe80::1".parse().unwrap()]),
}]
);
}
#[test]
fn a_service_record_names_no_host() {
let message = response(&[
record(
"_airplay._tcp.local",
Rdata::Ptr("Living Room._airplay._tcp.local"),
),
record(
"_services._dns-sd._udp.local",
Rdata::Ptr("_airplay._tcp.local"),
),
]);
assert_eq!(extract_hosts(&message).unwrap(), Vec::new());
}
#[test]
fn a_reverse_record_names_the_host_at_that_address() {
let message = response(&[record(
"150.113.0.203.in-addr.arpa",
Rdata::Ptr("raspberrypi.local"),
)]);
assert_eq!(
extract_hosts(&message).unwrap(),
vec![MdnsHost {
hostname: "raspberrypi.local".to_string(),
ips: BTreeSet::from(["203.0.113.150".parse().unwrap()]),
}]
);
}
#[test]
fn each_owner_in_a_message_is_a_host_of_its_own() {
let message = response(&[
record("appletv.local", Rdata::A([203, 0, 113, 40])),
record(
"_airplay._tcp.local",
Rdata::Ptr("Living Room._airplay._tcp.local"),
),
record("printer.local", Rdata::A([203, 0, 113, 30])),
]);
assert_eq!(
extract_hosts(&message).unwrap(),
vec![
MdnsHost {
hostname: "appletv.local".to_string(),
ips: BTreeSet::from(["203.0.113.40".parse().unwrap()]),
},
MdnsHost {
hostname: "printer.local".to_string(),
ips: BTreeSet::from(["203.0.113.30".parse().unwrap()]),
},
]
);
}
#[test]
fn bytes_that_are_not_dns_are_rejected() {
assert!(extract_hosts(b"not dns").is_err());
}
#[test]
fn a_forward_query_asks_for_the_name_in_both_families() {
let query = build_query("raspberrypi.local").expect("the query builds");
let packet = Packet::parse(&query).expect("a responder can parse it");
assert!(packet.header.query, "it is a query, not a response");
assert_eq!(packet.header.id, 0, "a multicast query carries the zero ID");
let asked: Vec<_> = packet
.questions
.iter()
.map(|q| (q.qname.to_string(), q.qtype))
.collect();
assert_eq!(
asked,
vec![
("raspberrypi.local".to_string(), QueryType::A),
("raspberrypi.local".to_string(), QueryType::AAAA),
]
);
}
#[test]
fn a_name_with_no_wire_form_is_refused_rather_than_fatal() {
let long_label = format!("{}.local", "a".repeat(64));
assert!(matches!(
build_query(&long_label),
Err(PacketError::UnwritableName { .. })
));
assert!(build_query(&format!("{}.local", "a".repeat(63))).is_ok());
let long_name = std::iter::repeat_n("label", 60)
.collect::<Vec<_>>()
.join(".");
assert!(matches!(
build_query(&long_name),
Err(PacketError::UnwritableName { .. })
));
assert!(matches!(
build_query("printer..local"),
Err(PacketError::UnwritableName { .. })
));
}
#[test]
fn a_trailing_dot_is_stripped_from_the_question() {
let query = build_query("printer.local.").expect("the query builds");
let packet = Packet::parse(&query).expect("parses");
assert_eq!(
packet.questions.first().map(|q| q.qname.to_string()),
Some("printer.local".to_string())
);
}
enum Rdata<'a> {
A([u8; 4]),
Aaaa(std::net::Ipv6Addr),
Ptr(&'a str),
}
struct TestRecord<'a> {
owner: &'a str,
data: Rdata<'a>,
}
fn record<'a>(owner: &'a str, data: Rdata<'a>) -> TestRecord<'a> {
TestRecord { owner, data }
}
fn response(records: &[TestRecord<'_>]) -> Vec<u8> {
let mut bytes = Vec::new();
bytes.extend_from_slice(&0u16.to_be_bytes()); bytes.extend_from_slice(&0x8400u16.to_be_bytes()); bytes.extend_from_slice(&0u16.to_be_bytes()); bytes.extend_from_slice(&(records.len() as u16).to_be_bytes()); bytes.extend_from_slice(&0u16.to_be_bytes()); bytes.extend_from_slice(&0u16.to_be_bytes());
for record in records {
write_name(&mut bytes, record.owner);
let (rtype, rdata) = match &record.data {
Rdata::A(octets) => (1u16, octets.to_vec()),
Rdata::Aaaa(addr) => (28u16, addr.octets().to_vec()),
Rdata::Ptr(target) => {
let mut rdata = Vec::new();
write_name(&mut rdata, target);
(12u16, rdata)
}
};
bytes.extend_from_slice(&rtype.to_be_bytes());
bytes.extend_from_slice(&1u16.to_be_bytes()); bytes.extend_from_slice(&120u32.to_be_bytes()); bytes.extend_from_slice(&(rdata.len() as u16).to_be_bytes());
bytes.extend_from_slice(&rdata);
}
bytes
}
fn write_name(bytes: &mut Vec<u8>, name: &str) {
for label in name.split('.') {
bytes.push(label.len() as u8);
bytes.extend_from_slice(label.as_bytes());
}
bytes.push(0);
}
const DEVICE_INFO: &[u8] = &[
0x00, 0x00, 0x84, 0x00, 0x00, 0x01, 0x00, 0x01, 0x00, 0x00, 0x00, 0x00, 0x03, 0x6d, 0x61,
0x63, 0x0c, 0x5f, 0x64, 0x65, 0x76, 0x69, 0x63, 0x65, 0x2d, 0x69, 0x6e, 0x66, 0x6f, 0x04,
0x5f, 0x74, 0x63, 0x70, 0x05, 0x6c, 0x6f, 0x63, 0x61, 0x6c, 0x00, 0x00, 0x10, 0x80, 0x01,
0xc0, 0x0c, 0x00, 0x10, 0x00, 0x01, 0x00, 0x00, 0x00, 0x0a, 0x00, 0x23, 0x0e, 0x6d, 0x6f,
0x64, 0x65, 0x6c, 0x3d, 0x4d, 0x61, 0x63, 0x31, 0x36, 0x2c, 0x31, 0x30, 0x0a, 0x6f, 0x73,
0x78, 0x76, 0x65, 0x72, 0x73, 0x3d, 0x32, 0x35, 0x08, 0x69, 0x63, 0x6f, 0x6c, 0x6f, 0x72,
0x3d, 0x30,
];
#[test]
fn a_device_info_record_yields_one_string_per_field() {
assert_eq!(
text_records(DEVICE_INFO).expect("a well-formed answer"),
vec!["model=Mac16,10", "osxvers=25", "icolor=0"]
);
}
#[test]
fn the_query_name_is_built_from_the_host_rather_than_browsed_for() {
for name in ["mac", "mac.local", "mac.local."] {
assert_eq!(
device_info_name(name).as_deref(),
Some("mac._device-info._tcp.local"),
"{name}"
);
}
}
#[test]
fn a_fully_qualified_or_capitalised_name_asks_the_same_question() {
assert_eq!(
device_info_name("mac.").as_deref(),
Some("mac._device-info._tcp.local")
);
assert_eq!(
device_info_name("Mac.LOCAL").as_deref(),
Some("Mac._device-info._tcp.local")
);
}
#[test]
fn a_name_outside_local_asks_no_question() {
assert_eq!(device_info_name("x.fritz.box"), None);
assert!(build_device_info_query("x.fritz.box").is_none());
for empty in ["", ".", ".local", "local."] {
assert_eq!(device_info_name(empty), None, "{empty:?}");
}
}
#[test]
fn the_query_asks_for_text_under_that_name() {
let query = build_device_info_query("mac")
.expect("a name with a record under it")
.expect("a name that fits a label");
let packet = Packet::parse(&query).expect("a responder can parse it");
let question = packet.questions.first().expect("one question");
assert_eq!(question.qname.to_string(), "mac._device-info._tcp.local");
assert_eq!(question.qtype, QueryType::TXT);
}
#[test]
fn a_message_carrying_no_text_yields_nothing() {
let query = build_device_info_query("mac")
.expect("a name with a record under it")
.expect("a query");
assert!(
text_records(&query)
.expect("a parseable message")
.is_empty()
);
}
#[test]
fn bytes_that_are_not_a_message_are_refused_rather_than_read() {
assert!(text_records(b"not an mdns message at all").is_err());
}
#[test]
fn the_reverse_query_asks_for_the_name_of_an_address() {
let ip: IpAddr = "203.0.113.160".parse().expect("a literal address");
let query = build_reverse_query(ip).expect("a reverse name fits");
let packet = Packet::parse(&query).expect("a responder can parse it");
let question = packet.questions.first().expect("one question");
assert_eq!(
question.qname.to_string(),
"160.113.0.203.in-addr.arpa",
"the reverse name names the address"
);
assert_eq!(question.qtype, QueryType::PTR);
}
}