1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
pub mod dns_parser;
pub mod mac_addr;
pub mod net_utils;

pub use dns_parser::QueryType;
use dns_parser::{Builder, Packet, QueryClass, RData, ResourceRecord};
use smol::channel::{bounded, Receiver};
use smol::net::UdpSocket;
use smol::prelude::*;
use std::net::{IpAddr, Ipv4Addr, Ipv6Addr, SocketAddr};
use std::time::{Duration, Instant};
use thiserror::*;

const MULTICAST_ADDR: Ipv4Addr = Ipv4Addr::new(224, 0, 0, 251);
const MULTICAST_PORT: u16 = 5353;

/// Errors that may occur during resolution/discovery
#[derive(Debug, Error)]
pub enum Error {
    #[error(transparent)]
    Io(#[from] std::io::Error),
    #[error(transparent)]
    ChanRecv(#[from] smol::channel::RecvError),
    #[error(transparent)]
    ChanSend(#[from] smol::channel::SendError<Response>),
    #[error("failed to build DNS packet")]
    DnsPacketBuildError,
    #[error("Timed out")]
    Timeout,
    #[error("The QueryParameters were invalid")]
    InvalidQueryParams,
    #[error("Unable to determine local interface")]
    LocalInterfaceUnknown,
}

pub type Result<T> = std::result::Result<T, Error>;

fn sockaddr(ip: Ipv4Addr, port: u16) -> SocketAddr {
    let addr = std::net::SocketAddrV4::new(ip, port);
    addr.into()
}

async fn create_socket() -> Result<UdpSocket> {
    let socket = socket2::Socket::new(
        socket2::Domain::IPV4,
        socket2::Type::DGRAM,
        Some(socket2::Protocol::UDP),
    )?;
    socket.set_reuse_address(true)?;
    let _ = socket.set_reuse_port(true);

    let addr = sockaddr(Ipv4Addr::UNSPECIFIED, MULTICAST_PORT);
    socket.bind(&addr.into())?;

    let socket = UdpSocket::from(smol::Async::new(socket.into())?);
    socket.set_multicast_loop_v4(false)?;
    socket.join_multicast_v4(MULTICAST_ADDR, Ipv4Addr::UNSPECIFIED)?;
    Ok(socket)
}

/// An mDNS query response
#[derive(Debug)]
pub struct Response {
    pub answers: Vec<Record>,
    pub nameservers: Vec<Record>,
    pub additional: Vec<Record>,
}

/// The resolved information about a host (or rather, a service)
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct Host {
    /// A friendly name for this instance.
    pub name: String,
    /// The mDNS `A` (or `AAAA`) resolvable hostname for this host.
    /// May be different from `name`.
    pub host_name: Option<String>,
    /// The set of addresses
    pub ip_address: Vec<IpAddr>,
    /// The set of addresses with port numbers.
    /// May be empty if no SRV record was resolved
    pub socket_address: Vec<SocketAddr>,
    /// The instant at which this information is no longer valid
    pub expires: Instant,
}

impl Host {
    /// Returns true if the information is still valid (within the
    /// TTL specified by the mDNS response).
    pub fn valid(&self) -> bool {
        Instant::now() < self.expires
    }
}

impl Response {
    fn new(packet: &Packet) -> Self {
        Self {
            answers: packet.answers.iter().map(Record::new).collect(),
            nameservers: packet.nameservers.iter().map(Record::new).collect(),
            additional: packet.additional.iter().map(Record::new).collect(),
        }
    }

    fn all_records(&self) -> impl Iterator<Item = &Record> {
        self.answers
            .iter()
            .chain(self.additional.iter())
            .chain(self.nameservers.iter())
    }

    /// Compose the response as an array of Host structs
    pub fn hosts(&self) -> Vec<Host> {
        let mut result = vec![];

        for ans in &self.answers {
            match &ans.kind {
                RecordKind::A(addr) => {
                    result.push(Host {
                        name: ans.name.clone(),
                        host_name: Some(ans.name.clone()),
                        ip_address: vec![(*addr).into()],
                        socket_address: vec![],
                        expires: Instant::now() + Duration::from_secs(ans.ttl.into()),
                    });
                }
                RecordKind::AAAA(addr) => {
                    result.push(Host {
                        name: ans.name.clone(),
                        host_name: Some(ans.name.clone()),
                        ip_address: vec![(*addr).into()],
                        socket_address: vec![],
                        expires: Instant::now() + Duration::from_secs(ans.ttl.into()),
                    });
                }
                RecordKind::PTR(name) => {
                    let name = name.clone();
                    let mut found_port = None;
                    let mut host_name = None;
                    let mut ip_address = vec![];
                    let mut socket_address = vec![];

                    for r in self.all_records() {
                        if r.name != name {
                            continue;
                        }

                        match &r.kind {
                            RecordKind::SRV { port, target, .. } => {
                                found_port.replace(*port);
                                host_name.replace(target.clone());
                            }
                            _ => {}
                        }
                    }

                    if let Some(host_name) = host_name.as_ref() {
                        for r in self.all_records() {
                            if &r.name != host_name {
                                continue;
                            }

                            match &r.kind {
                                RecordKind::A(addr) => {
                                    ip_address.push(addr.clone().into());
                                }
                                RecordKind::AAAA(addr) => {
                                    ip_address.push(addr.clone().into());
                                }
                                _ => {}
                            }
                        }
                    }

                    if let Some(port) = found_port {
                        for addr in &ip_address {
                            socket_address.push(SocketAddr::new(*addr, port));
                        }
                    }

                    result.push(Host {
                        name,
                        host_name,
                        ip_address,
                        socket_address,
                        expires: Instant::now() + Duration::from_secs(ans.ttl.into()),
                    });
                }
                _ => {}
            }
        }
        result
    }
}

/// mDNS Records compose into a [Response](struct.Record.html)
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct Record {
    pub name: String,
    pub class: dns_parser::Class,
    pub ttl: u32,
    pub kind: RecordKind,
}

impl Record {
    fn new(rr: &ResourceRecord) -> Self {
        Self {
            name: rr.name.to_string(),
            class: rr.cls,
            ttl: rr.ttl,
            kind: RecordKind::new(&rr.data),
        }
    }
}

/// mDNS record data of various kinds
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum RecordKind {
    A(Ipv4Addr),
    AAAA(Ipv6Addr),
    CNAME(String),
    PTR(String),
    NS(String),
    MX {
        preference: u16,
        exchange: String,
    },
    SRV {
        priority: u16,
        weight: u16,
        port: u16,
        target: String,
    },
    SOA {
        primary_ns: String,
        mailbox: String,
        serial: u32,
        refresh: u32,
        retry: u32,
        expire: u32,
        minimum_ttl: u32,
    },
    TXT(Vec<String>),
    Unimplemented {
        kind: dns_parser::Type,
        data: Vec<u8>,
    },
}

impl RecordKind {
    fn new(data: &RData) -> Self {
        match data {
            RData::A(dns_parser::rdata::a::Record(addr)) => Self::A(*addr),
            RData::AAAA(dns_parser::rdata::aaaa::Record(addr)) => Self::AAAA(*addr),
            RData::CNAME(name) => Self::CNAME(name.to_string()),
            RData::NS(name) => Self::NS(name.to_string()),
            RData::PTR(name) => Self::PTR(name.to_string()),
            RData::MX(dns_parser::rdata::mx::Record {
                preference,
                exchange,
            }) => Self::MX {
                preference: *preference,
                exchange: exchange.to_string(),
            },
            RData::SRV(dns_parser::rdata::srv::Record {
                priority,
                weight,
                port,
                target,
            }) => Self::SRV {
                priority: *priority,
                weight: *weight,
                port: *port,
                target: target.to_string(),
            },
            RData::TXT(txt) => Self::TXT(
                txt.iter()
                    .map(|b| String::from_utf8_lossy(b).into_owned())
                    .collect(),
            ),
            RData::SOA(dns_parser::rdata::soa::Record {
                primary_ns,
                mailbox,
                serial,
                refresh,
                retry,
                expire,
                minimum_ttl,
            }) => Self::SOA {
                primary_ns: primary_ns.to_string(),
                mailbox: mailbox.to_string(),
                serial: *serial,
                refresh: *refresh,
                retry: *retry,
                expire: *expire,
                minimum_ttl: *minimum_ttl,
            },
            RData::Unknown(kind, data) => Self::Unimplemented {
                kind: *kind,
                data: data.to_vec(),
            },
        }
    }
}

/// Resolve a single host using an mDNS request.
/// Returns a `Response` if found within the specified timeout,
/// otherwise yields an Error.
pub async fn resolve_one<S: AsRef<str>>(
    service_name: S,
    params: QueryParameters,
) -> Result<Response> {
    let responses = resolve(service_name, params).await?;
    let response = responses.recv().await?;
    Ok(response)
}

/// Controls how to perform the query.
/// You will typically use one of the associated constants
/// [DISCOVERY](#associatedconstant.DISCOVERY),
/// [SERVICE_LOOKUP](#associatedconstant.SERVICE_LOOKUP),
/// [HOST_LOOKUP](#associatedconstant.HOST_LOOKUP)
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub struct QueryParameters {
    pub query_type: QueryType,
    /// If specified, the query will be re-issued after this duration
    pub base_repeat_interval: Option<Duration>,
    /// The maximum interval between retries
    pub max_repeat_interval: Option<Duration>,
    /// If true, repeat interval will be doubled on each iteration
    /// until it reaches the max_repeat_interval.
    /// If false, it will increment by base_repeat_interval on each
    /// iteration until it reaches the max_repeat_interval.
    pub exponential_backoff: bool,
    /// If set, specifies the upper bound on total time spent
    /// processing the request.
    /// Otherwise, the request will keep going forever, subject to
    /// the repeat interval.
    pub timeout_after: Option<Duration>,
}

impl QueryParameters {
    /// Parameters suitable for performing long-running discovery.
    /// Repeatedly performs a PTR lookup with exponential backoff
    /// ranging from 2 seconds up to 5 minutes.
    pub const DISCOVERY: QueryParameters = QueryParameters {
        query_type: QueryType::PTR,
        base_repeat_interval: Some(Duration::from_secs(2)),
        exponential_backoff: true,
        max_repeat_interval: Some(Duration::from_secs(300)),
        timeout_after: None,
    };

    /// Parameters suitable for performing short-lived discovery.
    /// Repeatedly performs a PTR lookup with exponential backoff
    /// ranging from 2 seconds up to 5 minutes.
    pub const SERVICE_LOOKUP: QueryParameters = QueryParameters {
        query_type: QueryType::PTR,
        base_repeat_interval: Some(Duration::from_secs(2)),
        exponential_backoff: true,
        max_repeat_interval: None,
        timeout_after: Some(Duration::from_secs(60)),
    };

    /// Parameters suitable for resolving a single host.
    /// Performs an A lookup with exponential backoff ranging from
    /// 1 second.  The overall lookup will timeout after 1 minutes.
    pub const HOST_LOOKUP: QueryParameters = QueryParameters {
        query_type: QueryType::A,
        base_repeat_interval: Some(Duration::from_secs(1)),
        exponential_backoff: true,
        max_repeat_interval: None,
        timeout_after: Some(Duration::from_secs(60)),
    };

    pub fn with_timeout(mut self, timeout: Duration) -> Self {
        self.timeout_after.replace(timeout);
        self
    }
}

fn make_query(service_name: &str, query_type: QueryType) -> Result<Vec<u8>> {
    let mut builder = Builder::new_query(rand::random(), false);
    let prefer_unicast = false;
    builder.add_question(&service_name, prefer_unicast, query_type, QueryClass::IN);
    Ok(builder.build().map_err(|_| Error::DnsPacketBuildError)?)
}

/// The source UDP port in all Multicast DNS responses MUST
/// be 5353 (the well-known port assigned to mDNS).
/// Multicast DNS implementations MUST silently ignore any
/// Multicast DNS responses they receive where the source
/// UDP port is not 5353.
///
/// Also applies the Source Address Check from section 11 of
/// <https://tools.ietf.org/html/rfc6762>
fn valid_source_address(addr: SocketAddr) -> bool {
    if addr.port() != MULTICAST_PORT {
        false
    } else {
        /// Computes the masked address bits.
        fn masked(addr: &[u8], mask: &[u8]) -> Vec<u8> {
            assert_eq!(addr.len(), mask.len());
            addr.iter().zip(mask.iter()).map(|(a, m)| a & m).collect()
        }

        let ifaces = match crate::net_utils::get_if_addrs() {
            Ok(i) => i,
            Err(err) => {
                log::error!("error while listing local interfaces: {}", err);
                return false;
            }
        };

        for iface in ifaces {
            let matches_iface = match (&iface.addr, addr.ip()) {
                (crate::net_utils::IfAddr::V4(a), IpAddr::V4(source)) => {
                    masked(&a.ip.octets(), &a.netmask.octets())
                        == masked(&source.octets(), &a.netmask.octets())
                }

                (crate::net_utils::IfAddr::V6(a), IpAddr::V6(source)) => {
                    masked(&a.ip.octets(), &a.netmask.octets())
                        == masked(&source.octets(), &a.netmask.octets())
                }
                _ => false,
            };

            if matches_iface {
                return true;
            }
        }

        false
    }
}

/// Resolve records matching the requested service name.
/// Returns a Receiver that will yield successive responses.
/// Once `timeout` passes, the Sender side of the receiver
/// will disconnect and the channel will yield a RecvError.
pub async fn resolve<S: AsRef<str>>(
    service_name: S,
    params: QueryParameters,
) -> Result<Receiver<Response>> {
    if params.base_repeat_interval.is_none() && params.timeout_after.is_none() {
        return Err(Error::InvalidQueryParams);
    }

    let service_name = service_name.as_ref().to_string();
    let deadline = params.timeout_after.map(|d| Instant::now() + d);

    let data = make_query(&service_name, params.query_type)?;

    let socket = create_socket().await?;
    let addr = sockaddr(MULTICAST_ADDR, MULTICAST_PORT);

    socket.send_to(&data, addr).await?;

    let (tx, rx) = bounded(8);

    smol::spawn(async move {
        let mut retry_interval = params.base_repeat_interval;
        let mut last_send = Instant::now();

        loop {
            let now = Instant::now();

            if let Some(deadline) = deadline {
                if now >= deadline {
                    log::trace!("resolve loop completing because {now:?} >= {deadline:?}");
                    break;
                }
            }

            let recv_deadline = match retry_interval {
                Some(retry) => match deadline {
                    Some(overall) => (last_send + retry).min(overall),
                    None => last_send + retry,
                },
                None => match deadline {
                    Some(overall) => overall,
                    None => {
                        // Shouldn't be possible and we should
                        // have caught this in the params validation
                        // at entry to the function.
                        log::error!("resolve loop aborting because params are invalid");
                        return Err(Error::InvalidQueryParams);
                    }
                },
            };

            let mut buf = [0u8; 4096];

            let recv = async {
                let (len, addr) = socket.recv_from(&mut buf).await?;
                Result::Ok(Some((len, addr)))
            };

            let timer = async {
                let timer = smol::Timer::at(recv_deadline);
                timer.await;
                Result::Ok(None)
            };

            if let Some((len, addr)) = recv.or(timer).await? {
                match Packet::parse(&buf[..len]) {
                    Ok(dns) => {
                        let response = Response::new(&dns);
                        if !valid_source_address(addr) {
                            log::warn!(
                                "ignoring response {response:?} from {addr:?} which is not local",
                            );
                        } else {
                            let matched = response
                                .answers
                                .iter()
                                .any(|answer| answer.name == service_name);
                            if matched {
                                tx.send(response).await?;
                            }
                        }
                    }
                    Err(e) => {
                        log::trace!("failed to parse packet: {e:?} received from {addr:?}");
                    }
                }
            } else {
                log::trace!("resolve loop read timeout; send another query");
                // retry_interval exceeded, so send another query
                let data = make_query(&service_name, params.query_type)?;
                socket.send_to(&data, addr).await?;
                last_send = Instant::now();

                // And compute next interval
                match retry_interval.take() {
                    None => {
                        // No retries; we're done!
                        break;
                    }
                    Some(retry) => {
                        let base = params.base_repeat_interval.unwrap();

                        let retry = if params.exponential_backoff {
                            retry + retry
                        } else {
                            retry + base
                        };

                        let retry = params
                            .max_repeat_interval
                            .map(|max| retry.min(max))
                            .unwrap_or(retry);

                        retry_interval.replace(retry);
                    }
                }
                log::trace!("updated retry_interval is now {retry_interval:?}");
            }
        }

        log::trace!("resolve loop completing OK");
        Result::Ok(())
    })
    .detach();

    Ok(rx)
}