rtci-mdns 0.2.0

Sans-I/O mDNS (RFC 6762) querier and responder, as used for WebRTC mDNS ICE candidates
Documentation
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
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
//! The Sans-I/O mDNS connection.
//!
//! This module is private; [`Mdns`] is re-exported as [`crate::Mdns`], where its
//! documentation and examples live so that rustdoc renders them and their doctests run.
//!
//! The connection is a resolver (`resolver.rs`: query series, the address cache) and a
//! responder for unique host-name records (`responder.rs`: probing, announcing, answering,
//! goodbyes), over the message reader and writer of `wire.rs` and the message budget of
//! `rate.rs`. Every decision is made against the instant the caller supplied.

mod rate;
mod reconfirm;
mod resolver;
mod responder;
pub(crate) mod wire;

use std::collections::VecDeque;
use std::net::{IpAddr, Ipv4Addr, SocketAddr};
use std::time::{Duration, Instant};

use bytes::BytesMut;
use shared::{TaggedBytesMut, TransportContext, TransportMessage, TransportProtocol};

use crate::config::{DEFAULT_QUERY_INTERVAL, MdnsConfig};
use crate::message::RCode;
use rate::Jitter;
use reconfirm::Reconfirm;
use resolver::{Cached, Query};
use responder::{Goodbye, LocalRecord};
use shared::error::{Error, Result};

pub use rate::MdnsRateLimit;

/// The mDNS multicast group address (224.0.0.251).
pub const MDNS_MULTICAST_IPV4: Ipv4Addr = Ipv4Addr::new(224, 0, 0, 251);

/// The standard mDNS port (5353).
pub const MDNS_PORT: u16 = 5353;

/// mDNS multicast destination address (224.0.0.251:5353).
///
/// All mDNS queries and multicast responses are sent to this address.
///
/// # Example
///
/// ```rust
/// use rtci_mdns::MDNS_DEST_ADDR;
///
/// assert_eq!(MDNS_DEST_ADDR.to_string(), "224.0.0.251:5353");
/// ```
pub const MDNS_DEST_ADDR: SocketAddr = SocketAddr::new(IpAddr::V4(MDNS_MULTICAST_IPV4), MDNS_PORT);

/// The most names [`Mdns::add_local_name`] registers: one per local address is the use, and a
/// host has a handful.
pub const MAX_LOCAL_NAMES: usize = 64;

/// The most addresses the resolver's cache holds. Any host on the link can offer records, so
/// the cache is bounded: when it is full, the entry nearest its expiry gives way to one that
/// lives longer.
pub const MAX_CACHED_ADDRESSES: usize = 256;

/// The longest a cached address is kept, whatever TTL it came with: 75 minutes, the TTL
/// RFC 6762 section 10 recommends for records that are not host names.
const CACHE_TTL_CAP: u32 = 75 * 60;

/// Query intervals double up to one hour (RFC 6762 section 5.2).
const MAX_QUERY_INTERVAL: Duration = Duration::from_secs(60 * 60);

/// Packets waiting for `poll_write`. With this many unsent, nothing more is queued until the
/// caller drains them.
const MAX_QUEUED_PACKETS: usize = 256;

/// How long sending is put off when the write queue is full.
const QUEUE_FULL_RETRY: Duration = Duration::from_secs(1);

/// Unique identifier for tracking mDNS queries.
///
/// Each call to [`Mdns::query_now()`] returns a unique ID that can be used to:
/// - Track which query was answered in [`MdnsEvent::QueryAnswered`]
/// - Cancel a pending query with [`Mdns::cancel_query()`]
/// - Check if a query is still pending with [`Mdns::is_query_pending()`]
pub type QueryId = u64;

/// When something is to be done: at an instant, or as soon as the caller next gives the time
/// (what a method that takes no `now` leaves behind).
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub(crate) enum When {
    Asap,
    At(Instant),
}

impl When {
    fn due(self, now: Instant) -> bool {
        matches!(self, When::At(at) if at <= now)
    }
}

/// Events emitted by the mDNS connection.
///
/// Poll for events using [`poll_event()`](sansio::Protocol::poll_event) after
/// calling [`handle_read()`](sansio::Protocol::handle_read) or
/// [`handle_timeout()`](sansio::Protocol::handle_timeout).
///
/// # Example
///
/// ```rust,ignore
/// while let Some(event) = mdns.poll_event() {
///     match event {
///         MdnsEvent::QueryAnswered(query_id, addr) => {
///             println!("Query {} resolved to {}", query_id, addr);
///         }
///         MdnsEvent::QueryTimeout(query_id) => {
///             println!("Query {} timed out", query_id);
///         }
///         _ => {}
///     }
/// }
/// ```
#[derive(Debug)]
#[non_exhaustive]
pub enum MdnsEvent {
    /// A query was successfully answered.
    ///
    /// Contains the query ID and the resolved IP address.
    /// The query is automatically removed from the pending list.
    QueryAnswered(QueryId, IpAddr),

    /// A query ended without an address.
    ///
    /// Emitted when the query's timeout passed
    /// ([`MdnsConfig::query_timeout`](crate::MdnsConfig::query_timeout), 30 seconds unless
    /// changed), when a response gave the name more than one address and
    /// [`MdnsConfig::with_multiple_addresses`](crate::MdnsConfig::with_multiple_addresses) is
    /// off, or when the name's owner said it has no address record. The query is removed from
    /// the pending list when this event is emitted.
    ///
    /// ```rust
    /// use rtci_mdns::MdnsConfig;
    /// use std::time::Duration;
    ///
    /// let config = MdnsConfig::default()
    ///     .with_query_timeout(Duration::from_secs(5));
    /// ```
    QueryTimeout(QueryId),

    /// A name of ours is in use by another host and has been given up (RFC 6762 section 9):
    /// probing for it met a defender. The name is no longer answered; it is given as it was
    /// registered. Register another name in its place.
    LocalNameConflict(String),
}

/// Sans-I/O mDNS Connection.
///
/// This implements a sans-I/O mDNS resolver and host-name responder (RFC 6762, as far as
/// draft-ietf-mmusic-mdns-ice-candidates uses it):
///
/// - **Resolver**: a query asks for a name's A and AAAA records and is retried at doubling
///   intervals until it is answered or times out. Answers are cached for their TTL, so a name
///   seen in an announcement or resolved before answers at once.
/// - **Responder**: each local name owns one address record. A name is probed for unless it is
///   a random `<uuid>.local` name, announced twice, answered (by multicast at most once a
///   second per record, or by unicast where the querier asked), defended against probes, and
///   withdrawn with a goodbye when it is removed or the connection closes.
/// - Every message sent draws on one budget ([`MdnsRateLimit`]).
///
/// # Sans-I/O Pattern
///
/// This struct implements [`sansio::Protocol`], which means it doesn't perform
/// any I/O itself. Instead, the caller is responsible for:
///
/// 1. Calling [`handle_read()`](sansio::Protocol::handle_read) when packets arrive
/// 2. Sending packets from [`poll_write()`](sansio::Protocol::poll_write)
/// 3. Calling [`handle_timeout()`](sansio::Protocol::handle_timeout) on schedule
/// 4. Processing events from [`poll_event()`](sansio::Protocol::poll_event)
///
/// The socket is the application's: bound to port 5353, joined to 224.0.0.251
/// ([`MulticastSocket`](crate::MulticastSocket)). Give `handle_read` each datagram with its
/// source in `peer_addr`. Every datagram is taken to have arrived on the group, unless the
/// application reports destinations
/// ([`MdnsConfig::with_destination_reported`]): then `local_addr` is the datagram's
/// destination address, and one sent to the host by unicast is treated as RFC 6762 treats
/// unicast queries and responses.
///
/// # Example: Complete Event Loop
///
/// ```rust
/// use rtci_mdns::{MdnsConfig, Mdns, MdnsEvent};
/// use sansio::Protocol;
/// use std::time::{Duration, Instant};
///
/// let mut mdns = Mdns::new(Instant::now(), MdnsConfig::default());
///
/// // Start a query
/// let query_id = mdns.query_now(Instant::now(), "device.local");
///
/// // Simulate an event loop iteration
/// let now = Instant::now();
///
/// // 1. Send queued packets (would go to network in real code)
/// while let Some(packet) = mdns.poll_write() {
///     println!("Would send {} bytes to {}", packet.message.len(), packet.transport.peer_addr);
/// }
///
/// // 2. Handle timeout if due
/// if let Some(deadline) = mdns.poll_timeout() {
///     if deadline <= now {
///         mdns.handle_timeout(now).ok();
///     }
/// }
///
/// // 3. Process any events
/// while let Some(event) = mdns.poll_event() {
///     match event {
///         MdnsEvent::QueryAnswered(query_id, addr) => {
///             println!("Query {} answered: {}", query_id, addr);
///         }
///         MdnsEvent::QueryTimeout(id) => {
///             println!("Query {} timed out", id);
///         }
///         // MdnsEvent is #[non_exhaustive]: a wildcard arm is required.
///         _ => {}
///     }
/// }
/// ```
///
/// # Example: Multiple Concurrent Queries
///
/// ```rust
/// # use std::time::Instant;
/// use rtci_mdns::{MdnsConfig, Mdns};
/// use sansio::Protocol;
///
/// let mut mdns = Mdns::new(Instant::now(), MdnsConfig::default());
///
/// // Start multiple queries - each gets a unique ID
/// let id1 = mdns.query_now(Instant::now(), "printer.local");
/// let id2 = mdns.query_now(Instant::now(), "server.local");
/// let id3 = mdns.query_now(Instant::now(), "nas.local");
///
/// assert_eq!(mdns.pending_query_count(), 3);
/// assert!(mdns.is_query_pending(id1));
/// assert!(mdns.is_query_pending(id2));
/// assert!(mdns.is_query_pending(id3));
///
/// // Cancel one query
/// mdns.cancel_query(id2);
/// assert_eq!(mdns.pending_query_count(), 2);
/// assert!(!mdns.is_query_pending(id2));
/// ```
///
/// # Query Lifecycle
///
/// 1. Call [`Mdns::query_now()`] (sent at once) or [`Mdns::schedule_query()`] (sent from the
///    next `handle_timeout`; after a random 20 to 120 ms with
///    [`MdnsConfig::with_first_query_delay`]) with the hostname to resolve
/// 2. Retrieve the query packet from [`poll_write()`](sansio::Protocol::poll_write)
/// 3. Send the packet to the mDNS multicast address
/// 4. When responses arrive, pass them to [`handle_read()`](sansio::Protocol::handle_read)
/// 5. Check [`poll_event()`](sansio::Protocol::poll_event) for [`MdnsEvent::QueryAnswered`]
/// 6. Call [`handle_timeout()`](sansio::Protocol::handle_timeout) when
///    [`poll_timeout()`](sansio::Protocol::poll_timeout) says so: retries go out after 1, 2,
///    4, ... seconds, and the query ends with [`MdnsEvent::QueryTimeout`] at its timeout
///
/// # Name Lifecycle
///
/// A name given in [`MdnsConfig::with_local_names`] or to [`Mdns::add_local_name`] starts at
/// the next `handle_timeout`: three probes 250 ms apart unless the name is exempt
/// ([`MdnsProbePolicy`](crate::MdnsProbePolicy)), then two announcements one second apart. A
/// name that is not probed for is answered from the moment it is registered.
/// [`Mdns::remove_local_name`] and [`close()`](sansio::Protocol::close) send goodbyes;
/// [`Mdns::network_changed`] starts every name over.
pub struct Mdns {
    config: MdnsConfig,

    /// The configured local names with trailing dots, as given.
    local_names: Vec<String>,

    /// The records this connection owns: the configured names (when a local IP is configured)
    /// and those registered with [`Mdns::add_local_name`].
    records: Vec<LocalRecord>,

    /// Goodbyes still to send for removed names. At most [`MAX_LOCAL_NAMES`].
    goodbyes: Vec<Goodbye>,

    /// The times of the latest name conflicts, at most [`responder::CONFLICT_LIMIT`].
    conflicts: VecDeque<Instant>,

    /// Pending queries.
    queries: Vec<Query>,

    /// Addresses learned from responses. At most [`MAX_CACHED_ADDRESSES`].
    cache: Vec<Cached>,

    /// Cached names being reconfirmed (RFC 6762 section 10.4).
    reconfirms: Vec<Reconfirm>,

    /// Next query ID to assign
    next_query_id: QueryId,

    /// The interval between the first two queries of a series.
    query_interval: Duration,

    /// Query timeout (None = no automatic timeout)
    query_timeout: Option<Duration>,

    limiter: MdnsRateLimit,
    jitter: Jitter,

    /// Set by a flush that could not send: when sending can go on.
    blocked_until: Option<Instant>,

    /// Outgoing packet queue
    write_outs: VecDeque<TaggedBytesMut>,

    /// Event queue
    event_outs: VecDeque<MdnsEvent>,

    /// The next instant anything is due.
    next_timeout: Option<Instant>,

    /// Whether anything waits for the caller to give the time.
    asap: bool,

    last_now: Instant,

    /// Whether the connection is closed
    closed: bool,
}

/// `name` with a trailing dot, and that in ASCII lower case: DNS names compare without regard
/// to ASCII case (RFC 1035 section 2.3.3).
fn normalize(name: &str) -> (String, String) {
    let name = if name.ends_with('.') {
        name.to_owned()
    } else {
        format!("{name}.")
    };
    let key = name.to_ascii_lowercase();
    (name, key)
}

impl Mdns {
    /// Feeds a received datagram together with the destination address of its IP header, as
    /// the driver reads it (`IP_PKTINFO`, `IPV6_PKTINFO`, `IP_RECVDSTADDR`). With it the RFC
    /// 6762 rules that turn on the destination apply exactly:
    ///
    /// - section 6: a response that arrived by unicast is taken only for a recent question
    ///   asked with the QU bit; others are ignored;
    /// - section 11: a response that arrived at the mDNS multicast address (224.0.0.251 or
    ///   FF02::FB) "originated on the local link, regardless of source IP address"; one that
    ///   arrived by unicast is held to the on-link test.
    ///
    /// [`handle_read`](sansio::Protocol::handle_read) is the same without a known
    /// destination: then the address scope of the source decides (see
    /// [`MdnsConfig::with_on_link_subnets`]).
    ///
    /// # Errors
    ///
    /// As `handle_read`.
    pub fn handle_datagram(&mut self, msg: TaggedBytesMut, destination: IpAddr) -> Result<()> {
        if self.closed {
            return Err(Error::ErrConnectionClosed);
        }
        self.last_now = msg.now;
        self.process_message(&msg, Some(destination));
        self.flush(msg.now);
        self.update_next_timeout();
        Ok(())
    }

    /// Create a new mDNS connection with the given configuration.
    ///
    /// # Arguments
    ///
    /// * `now` - The current time
    /// * `config` - MdnsConfiguration for the connection
    ///
    /// # Example
    ///
    /// ```rust
    /// use rtci_mdns::{MdnsConfig, Mdns};
    /// use std::time::{Duration, Instant};
    ///
    /// // Client-only configuration
    /// let client = Mdns::new(Instant::now(), MdnsConfig::default());
    ///
    /// // Server configuration
    /// use std::net::{IpAddr, Ipv4Addr};
    /// let server = Mdns::new(
    ///     Instant::now(),
    ///     MdnsConfig::default()
    ///         .with_local_names(vec!["myhost.local".to_string()])
    ///         .with_local_ip(
    ///             IpAddr::V4(Ipv4Addr::new(192, 168, 1, 1)),
    ///         )
    /// );
    /// ```
    pub fn new(now: Instant, config: MdnsConfig) -> Self {
        let query_interval = if config.query_interval == Duration::ZERO {
            DEFAULT_QUERY_INTERVAL
        } else {
            // RFC 6762 section 5.2: at least one second between the first two queries, and
            // no need for more than an hour.
            config
                .query_interval
                .clamp(DEFAULT_QUERY_INTERVAL, MAX_QUERY_INTERVAL)
        };

        let mut mdns = Self {
            local_names: Vec::new(),
            records: Vec::new(),
            goodbyes: Vec::new(),
            conflicts: VecDeque::new(),
            queries: Vec::new(),
            cache: Vec::new(),
            reconfirms: Vec::new(),
            next_query_id: 1,
            query_interval,
            query_timeout: config.query_timeout,
            limiter: config.rate_limit.clone().unwrap_or_default(),
            jitter: Jitter::new(config.jitter_seed),
            blocked_until: None,
            write_outs: VecDeque::new(),
            event_outs: VecDeque::new(),
            next_timeout: None,
            asap: false,
            last_now: now,
            closed: false,
            config,
        };

        if !mdns.config.local_names.is_empty() && mdns.config.local_ip.is_none() {
            log::warn!("mDNS local names are configured without a local IP: they are not answered");
        }
        for name in mdns.config.local_names.clone() {
            let (name, key) = normalize(&name);
            mdns.local_names.push(name.clone());
            if let Some(ip) = mdns.config.local_ip
                && !mdns.records.iter().any(|r| r.key == key)
            {
                match mdns.new_record(name, key, ip, false) {
                    Some(record) => mdns.records.push(record),
                    None => log::warn!("mDNS cannot publish a configured name: not a DNS name"),
                }
            }
        }
        mdns.update_next_timeout();
        mdns
    }

    /// Answers queries for `name` with `ip` from now on: an A record for an IPv4 address, an
    /// AAAA record for an IPv6 one. Unlike [`MdnsConfig::with_local_names`], whose names all
    /// answer with the one [`MdnsConfig::local_ip`], each name registered here has its own
    /// address, so a host can publish one name per interface address (what WebRTC's mDNS host
    /// candidates do).
    ///
    /// The name is announced from the next
    /// [`handle_timeout`](sansio::Protocol::handle_timeout) (RFC 6762 section 8.3). A name that
    /// is not `<uuid>.local` is probed for first
    /// ([`MdnsProbePolicy`](crate::MdnsProbePolicy)) and answered only once probing has met no
    /// defender.
    ///
    /// Returns `false`, registering nothing, when `name` is already registered,
    /// [`MAX_LOCAL_NAMES`] are, or `name` is not a DNS name (an empty label, a label over 63
    /// bytes, over 255 bytes in all).
    ///
    /// # Example
    ///
    /// ```rust
    /// # use std::time::Instant;
    /// use rtci_mdns::{Mdns, MdnsConfig};
    /// use std::net::IpAddr;
    ///
    /// let mut mdns = Mdns::new(Instant::now(), MdnsConfig::default());
    /// assert!(mdns.add_local_name("v4-name.local", "192.168.1.10".parse::<IpAddr>().unwrap()));
    /// assert!(mdns.add_local_name("v6-name.local", "2001:db8::10".parse::<IpAddr>().unwrap()));
    /// ```
    pub fn add_local_name(&mut self, name: &str, ip: IpAddr) -> bool {
        let (name, key) = normalize(name);
        if self.closed
            || self.records.iter().filter(|r| r.registered).count() >= MAX_LOCAL_NAMES
            || self.records.iter().any(|r| r.key == key)
        {
            return false;
        }
        let Some(record) = self.new_record(name, key, ip, true) else {
            return false;
        };
        // The name is back: the goodbye its removal left behind would withdraw it again.
        self.goodbyes
            .retain(|g| !(g.key == record.key && g.ip == record.ip));
        self.records.push(record);
        self.update_next_timeout();
        true
    }

    /// Stops answering for `name` and withdraws it: when the name was ever sent to the link,
    /// a goodbye (its record with TTL 0, RFC 6762 section 10.1) goes out from the next
    /// [`handle_timeout`](sansio::Protocol::handle_timeout), so that neighbours drop it from
    /// their caches.
    ///
    /// Returns `false` when `name` is not one of this connection's names.
    ///
    /// # Example
    ///
    /// ```rust
    /// # use std::time::Instant;
    /// use rtci_mdns::{Mdns, MdnsConfig};
    /// use std::net::IpAddr;
    ///
    /// let mut mdns = Mdns::new(Instant::now(), MdnsConfig::default());
    /// let ip: IpAddr = "192.168.1.10".parse().unwrap();
    /// assert!(mdns.add_local_name("a-name.local", ip));
    /// assert!(mdns.remove_local_name("A-Name.local"));
    /// assert!(!mdns.remove_local_name("a-name.local"));
    /// ```
    pub fn remove_local_name(&mut self, name: &str) -> bool {
        let (_, key) = normalize(name);
        let Some(index) = self.records.iter().position(|r| r.key == key) else {
            return false;
        };
        let record = self.records.remove(index);
        self.withdraw(record);
        self.update_next_timeout();
        true
    }

    /// Tells the connection that the host's network may have changed: it woke from sleep, a
    /// link came up, an address changed.
    ///
    /// What was learned on the old link is dropped (the address cache, RFC 6762 section 10.3),
    /// and every local name starts over as a new one does, from the next
    /// [`handle_timeout`](sansio::Protocol::handle_timeout): probed for unless exempt, then
    /// announced (RFC 6762 sections 8, 10.2). Pending queries carry on.
    ///
    /// Announcements are sent only when a name is registered and when this is called: there
    /// are no periodic ones (RFC 6762 section 8.3).
    pub fn network_changed(&mut self) {
        if self.closed {
            return;
        }
        self.cache.clear();
        self.reconfirms.clear();
        self.restart_records();
        self.update_next_timeout();
    }

    /// Start a new mDNS query for the given name, sending it at once.
    ///
    /// The query asks for the name's A and AAAA records. It is retried after the configured
    /// interval, then at doubling intervals (RFC 6762 section 5.2), until either:
    /// - An answer is received (emits [`MdnsEvent::QueryAnswered`])
    /// - The query times out (emits [`MdnsEvent::QueryTimeout`])
    /// - The query is cancelled with [`cancel_query()`](Self::cancel_query)
    /// - The connection is closed
    ///
    /// A name whose address is in the cache (answered before, or announced by its owner, and
    /// not past its TTL) is answered at once: the event is queued, no packet is, and the query
    /// is never pending.
    ///
    /// # Arguments
    ///
    /// * `now` - The current time
    /// * `name` - The hostname to query (e.g., `"mydevice.local"`)
    ///
    /// # Returns
    ///
    /// A unique [`QueryId`] that can be used to track this query.
    ///
    /// # Example
    ///
    /// ```rust
    /// # use std::time::Instant;
    /// use rtci_mdns::{MdnsConfig, Mdns, MdnsEvent};
    /// use sansio::Protocol;
    ///
    /// let mut mdns = Mdns::new(Instant::now(), MdnsConfig::default());
    ///
    /// // Start a query
    /// let query_id = mdns.query_now(Instant::now(), "printer.local");
    ///
    /// // The query packet is now queued
    /// let packet = mdns.poll_write().expect("query packet should be queued");
    /// assert_eq!(packet.transport.peer_addr.to_string(), "224.0.0.251:5353");
    ///
    /// // Track the query
    /// assert!(mdns.is_query_pending(query_id));
    /// ```
    pub fn query_now(&mut self, now: Instant, name: &str) -> QueryId {
        let id = self.new_query(name, When::At(now), Some(now));
        if !self.closed {
            self.last_now = now;
            self.answer_from_cache(now);
            self.flush(now);
            self.update_next_timeout();
        }
        id
    }

    /// Start a query for `name` from the next
    /// [`handle_timeout`](sansio::Protocol::handle_timeout): its first packet goes out in that
    /// call, or, with [`MdnsConfig::with_first_query_delay`], a random 20 to 120 ms after it
    /// (RFC 6762 section 5.2); not at all when the name's address is in the cache. Otherwise
    /// as [`query_now`](Self::query_now).
    pub fn schedule_query(&mut self, name: &str) -> QueryId {
        let id = self.new_query(name, When::Asap, None);
        self.update_next_timeout();
        id
    }

    /// Cancel a pending query.
    ///
    /// Removes the query from the pending list. No more retry packets will
    /// be sent and no events will be emitted for this query.
    ///
    /// # Arguments
    ///
    /// * `query_id` - The ID returned by [`query_now()`](Self::query_now)
    ///
    /// # Example
    ///
    /// ```rust
    /// # use std::time::Instant;
    /// use rtci_mdns::{MdnsConfig, Mdns};
    ///
    /// let mut mdns = Mdns::new(Instant::now(), MdnsConfig::default());
    /// let query_id = mdns.query_now(Instant::now(), "device.local");
    ///
    /// assert!(mdns.is_query_pending(query_id));
    /// mdns.cancel_query(query_id);
    /// assert!(!mdns.is_query_pending(query_id));
    /// ```
    pub fn cancel_query(&mut self, query_id: QueryId) {
        self.queries.retain(|q| q.id != query_id);
        self.update_next_timeout();
    }

    /// Check if a query is still pending.
    ///
    /// A query is pending until it is answered, times out or is cancelled.
    ///
    /// # Arguments
    ///
    /// * `query_id` - The ID returned by [`query_now()`](Self::query_now)
    ///
    /// # Returns
    ///
    /// `true` if the query is still waiting for an answer, `false` otherwise.
    ///
    /// # Example
    ///
    /// ```rust
    /// # use std::time::Instant;
    /// use rtci_mdns::{MdnsConfig, Mdns};
    ///
    /// let mut mdns = Mdns::new(Instant::now(), MdnsConfig::default());
    /// let query_id = mdns.query_now(Instant::now(), "device.local");
    ///
    /// // Query is pending until answered or cancelled
    /// assert!(mdns.is_query_pending(query_id));
    /// ```
    pub fn is_query_pending(&self, query_id: QueryId) -> bool {
        self.queries.iter().any(|q| q.id == query_id)
    }

    /// Get the number of pending queries.
    ///
    /// # Returns
    ///
    /// The count of queries that are still waiting for answers.
    ///
    /// # Example
    ///
    /// ```rust
    /// # use std::time::Instant;
    /// use rtci_mdns::{MdnsConfig, Mdns};
    ///
    /// let mut mdns = Mdns::new(Instant::now(), MdnsConfig::default());
    /// assert_eq!(mdns.pending_query_count(), 0);
    ///
    /// mdns.query_now(Instant::now(), "device1.local");
    /// mdns.query_now(Instant::now(), "device2.local");
    /// assert_eq!(mdns.pending_query_count(), 2);
    /// ```
    pub fn pending_query_count(&self) -> usize {
        self.queries.len()
    }

    /// Declares, from now on, whether the caller of
    /// [`handle_read`](sansio::Protocol::handle_read) puts each datagram's destination address
    /// in `local_addr` ([`MdnsConfig::with_destination_reported`]).
    pub fn set_destination_reported(&mut self, reported: bool) {
        self.config.destination_reported = reported;
    }

    /// Whether `ip` may be on the local link (RFC 6762 section 11): in one of the configured
    /// subnets, or, with none configured, by the scope of the address.
    fn is_on_link(&self, ip: IpAddr) -> bool {
        if self.config.on_link_subnets.is_empty() {
            return is_on_link_source(ip);
        }
        let ip = ip.to_canonical();
        self.config
            .on_link_subnets
            .iter()
            .any(|&(net, prefix)| in_subnet(ip, net.to_canonical(), prefix))
    }

    /// Whether an answer naming `addr` is believed from `src`: see
    /// [`answer_is_from_its_owner`]. `on_link` overrides the scope test: a response the
    /// driver says arrived at the mDNS multicast address originated on the link (RFC 6762
    /// section 11). An A record must still name its sender (the fork's hardening).
    fn trusts(&self, addr: IpAddr, src: IpAddr, on_link: bool) -> bool {
        let (addr, src) = (addr.to_canonical(), src.to_canonical());
        let on_link = on_link || self.is_on_link(src);
        match addr {
            IpAddr::V4(_) => addr == src && on_link,
            IpAddr::V6(_) => addr == src || on_link,
        }
    }

    /// Whether `ip` is an address this connection publishes: a datagram from it is our own,
    /// looped back, or another responder of this host.
    fn is_own_address(&self, ip: IpAddr) -> bool {
        let ip = ip.to_canonical();
        self.config.local_ip.map(|own| own.to_canonical()) == Some(ip)
            || self.records.iter().any(|r| r.ip.to_canonical() == ip)
    }

    /// Queues `bytes` for `to`, taking one message of the budget. `false`, queuing nothing,
    /// when the budget or the queue has no room; `blocked_until` then says when to try again.
    fn emit(&mut self, now: Instant, from: IpAddr, to: SocketAddr, bytes: &[u8]) -> bool {
        if self.write_outs.len() >= MAX_QUEUED_PACKETS {
            self.blocked_until = Some(now + QUEUE_FULL_RETRY);
            return false;
        }
        if !self.limiter.take(now) {
            self.blocked_until = Some(self.limiter.ready_at(now));
            return false;
        }
        self.push_packet(now, from, to, bytes);
        true
    }

    fn push_packet(&mut self, now: Instant, from: IpAddr, to: SocketAddr, bytes: &[u8]) {
        self.write_outs.push_back(TransportMessage {
            now,
            transport: TransportContext {
                local_addr: SocketAddr::new(from, MDNS_PORT),
                peer_addr: to,
                transport_protocol: TransportProtocol::UDP,
                ecn: None,
            },
            message: BytesMut::from(bytes),
        });
    }

    /// Sends everything that is due and allowed at `now`, in this order: probes, records
    /// (answers and announcements), goodbyes, queries. Stops at the first message the budget
    /// refuses; what was not sent stays due.
    fn flush(&mut self, now: Instant) {
        if self.closed {
            return;
        }
        self.blocked_until = None;
        let _ = self.flush_probes(now)
            && self.flush_records(now)
            && self.flush_goodbyes(now)
            && self.flush_queries(now)
            && self.flush_reconfirms(now);
    }

    fn update_next_timeout(&mut self) {
        let mut next: Option<Instant> = None;
        let mut asap = false;
        let mut consider = |when: When| match when {
            When::Asap => asap = true,
            When::At(at) => next = Some(next.map_or(at, |n: Instant| n.min(at))),
        };
        for record in &self.records {
            record.deadlines(&mut consider);
        }
        for goodbye in &self.goodbyes {
            consider(goodbye.due);
        }
        for query in &self.queries {
            consider(query.next);
            if let (Some(started), Some(timeout)) = (query.started, self.query_timeout)
                && let Some(at) = started.checked_add(timeout)
            {
                consider(When::At(at));
            }
        }
        self.stale_deadlines(&mut consider);
        // What is due but could not be sent waits for the budget.
        if let (Some(at), Some(blocked)) = (next, self.blocked_until) {
            next = Some(at.max(blocked));
        }
        self.next_timeout = next;
        self.asap = asap;
    }

    fn process_message(&mut self, msg: &TaggedBytesMut, destination: Option<IpAddr>) {
        let Some(inbound) = wire::parse(&msg.message) else {
            log::debug!("mDNS ignores a datagram that is not a DNS message");
            return;
        };
        // RFC 6762 sections 18.3 and 18.11: a message with another OPCODE or a non-zero RCODE
        // is silently ignored.
        if inbound.header.op_code != 0 || inbound.header.rcode != RCode::Success {
            return;
        }
        let src = msg.transport.peer_addr;
        // The destination, where the caller reports it: a unicast arrival is held to stricter
        // rules (RFC 6762 sections 5.5, 6, 11).
        // A destination the driver read from the IP header (`handle_datagram`) is believed
        // as given; otherwise the datagram's `local_addr` is, where the configuration says
        // the driver puts the destination there.
        let (unicast_arrival, multicast_arrival) = match destination {
            Some(dst) => (!dst.is_multicast(), dst.is_multicast()),
            // RFC 6762 section 11: "All responses received with a destination address in the
            // IP header that is the mDNS IPv4 link-local multicast address 224.0.0.251 or the
            // mDNS IPv6 link-local multicast address FF02::FB are necessarily deemed to have
            // originated on the local link, regardless of source IP address."
            None if self.config.destination_reported => {
                let dst = msg.transport.local_addr.ip();
                (
                    !(dst.is_unspecified() || dst.is_multicast()),
                    dst.is_multicast(),
                )
            }
            None => (false, false),
        };

        if inbound.header.response {
            // RFC 6762 section 6: a response from another source port is silently ignored.
            // Its questions, if it has any, are too.
            if src.port() == MDNS_PORT {
                self.process_response(&inbound, src, unicast_arrival, multicast_arrival, msg.now);
            }
        } else {
            // The Answer section of a query is its sender's known answers: it resolves
            // nothing and is never cached (RFC 6762 section 7.1).
            self.process_query(&inbound, src, unicast_arrival, msg.now);
        }
    }
}

/// Whether `ip` is in the subnet `net`/`prefix`.
fn in_subnet(ip: IpAddr, net: IpAddr, prefix: u8) -> bool {
    fn bits(ip: IpAddr) -> (u128, u32) {
        match ip {
            IpAddr::V4(v4) => (u128::from(u32::from(v4)), 32),
            IpAddr::V6(v6) => (u128::from(v6), 128),
        }
    }
    let ((ip, width), (net, net_width)) = (bits(ip), bits(net));
    if width != net_width {
        return false;
    }
    let prefix = u32::from(prefix).min(width);
    if prefix == 0 {
        return true;
    }
    (ip ^ net) >> (width - prefix) == 0
}

/// Whether `src` may be on the local link (RFC 6762 §11: mDNS is link-local; responses from
/// elsewhere are not to be trusted). The receiver sees no TTL or interface here, so this is the
/// address's scope: private, shared, link-local and loopback IPv4; link-local, unique-local and
/// loopback IPv6.
pub(crate) fn is_on_link_source(src: IpAddr) -> bool {
    match src.to_canonical() {
        IpAddr::V4(v4) => {
            let [a, b, ..] = v4.octets();
            v4.is_private()
                || v4.is_link_local()
                || v4.is_loopback()
                // 100.64.0.0/10, shared address space (RFC 6598).
                || (a == 100 && (64..128).contains(&b))
        }
        IpAddr::V6(v6) => {
            v6.is_unicast_link_local() || v6.is_loopback() || (v6.segments()[0] & 0xfe00) == 0xfc00
        }
    }
}

/// Whether an answer naming `addr` is believed from `src`: the sender must be on-link, and an
/// A record must name the sender's own address. A host answers for its IPv6 names over the IPv4
/// group too, so an AAAA record is not compared with an IPv4 source (an IPv6 source that names
/// itself is accepted whatever its scope).
pub(crate) fn answer_is_from_its_owner(addr: IpAddr, src: IpAddr) -> bool {
    let (addr, src) = (addr.to_canonical(), src.to_canonical());
    match addr {
        IpAddr::V4(_) => addr == src && is_on_link_source(src),
        IpAddr::V6(_) => addr == src || is_on_link_source(src),
    }
}

impl sansio::Protocol<TaggedBytesMut, (), ()> for Mdns {
    type Rout = ();
    type Wout = TaggedBytesMut;
    type Eout = MdnsEvent;
    type Error = Error;
    type Time = Instant;

    /// Process an incoming mDNS packet.
    ///
    /// Call this method when a UDP packet is received on the mDNS socket (port 5353, joined
    /// to 224.0.0.251).
    ///
    /// The connection will:
    /// - Parse the packet as an mDNS message; one with a non-zero OPCODE or RCODE, or over
    ///   9000 bytes, is ignored
    /// - For a query: answer the questions for its names (at once, or after the delay RFC 6762
    ///   section 6 asks for), by multicast, by unicast when asked, or as a conventional DNS
    ///   reply to a querier that is not on port 5353
    /// - For a response (taken only from source port 5353): resolve pending queries, cache
    ///   the addresses, and check its records against the names it owns (a conflicting record
    ///   sends the name back to probing)
    ///
    /// # Arguments
    ///
    /// * `msg` - The received packet with transport context: the sender in `peer_addr`, and,
    ///   with [`MdnsConfig::with_destination_reported`], the datagram's destination address
    ///   in `local_addr`
    ///
    /// # Errors
    ///
    /// Returns [`Error::ErrConnectionClosed`] if the connection has been closed.
    ///
    /// # Example
    ///
    /// ```rust,ignore
    /// use bytes::BytesMut;
    /// use shared::{TaggedBytesMut, TransportContext, TransportProtocol};
    /// use std::time::Instant;
    ///
    /// // When a packet arrives from the network:
    /// let msg = TaggedBytesMut {
    ///     now: Instant::now(),
    ///     transport: TransportContext {
    ///         local_addr: "224.0.0.251:5353".parse().unwrap(),
    ///         peer_addr: src_addr,
    ///         transport_protocol: TransportProtocol::UDP,
    ///         ecn: None,
    ///     },
    ///     message: BytesMut::from(&packet_data[..]),
    /// };
    /// mdns.handle_read(msg)?;
    ///
    /// // Check for events
    /// while let Some(event) = mdns.poll_event() {
    ///     // handle event
    /// }
    /// ```
    fn handle_read(&mut self, msg: TaggedBytesMut) -> Result<()> {
        if self.closed {
            return Err(Error::ErrConnectionClosed);
        }
        self.last_now = msg.now;
        self.process_message(&msg, None);
        self.flush(msg.now);
        self.update_next_timeout();
        Ok(())
    }

    /// mDNS doesn't produce read outputs.
    ///
    /// Answers to queries are delivered via `poll_event()`
    /// as [`MdnsEvent::QueryAnswered`] events instead.
    ///
    /// # Returns
    ///
    /// Always returns `None`.
    fn poll_read(&mut self) -> Option<Self::Rout> {
        None
    }

    /// Handle write requests (not used).
    ///
    /// Queries are initiated via the [`query_now()`](Mdns::query_now) method instead
    /// of through this interface.
    fn handle_write(&mut self, _msg: ()) -> Result<()> {
        Ok(())
    }

    /// Get the next packet to send.
    ///
    /// Call this method repeatedly until it returns `None` to retrieve all
    /// queued packets. Packets should be sent via UDP, from port 5353, to the address
    /// specified in `packet.transport.peer_addr`: 224.0.0.251:5353, or a querier's own address
    /// for a unicast reply. Responses should leave with IP TTL 255 (RFC 6762 section 11;
    /// [`MulticastSocket`](crate::MulticastSocket) sets it).
    ///
    /// Packets are queued when:
    /// - A query is started with [`query_now()`](Mdns::query_now)
    /// - `handle_timeout()` reaches a query's first packet or retry, a probe, an
    ///   announcement, a delayed response or a goodbye
    /// - A response is generated for a matching question
    /// - The connection is closed while it has published names (their goodbyes)
    ///
    /// # Returns
    ///
    /// The next packet to send, or `None` if the queue is empty.
    ///
    /// # Example
    ///
    /// ```rust
    /// # use std::time::Instant;
    /// use rtci_mdns::{MdnsConfig, Mdns};
    /// use sansio::Protocol;
    ///
    /// let mut mdns = Mdns::new(Instant::now(), MdnsConfig::default());
    /// mdns.query_now(Instant::now(), "device.local");
    ///
    /// // Send all queued packets
    /// while let Some(packet) = mdns.poll_write() {
    ///     // socket.send_to(&packet.message, packet.transport.peer_addr).await?;
    ///     println!("Send to {}", packet.transport.peer_addr);
    /// }
    /// ```
    fn poll_write(&mut self) -> Option<Self::Wout> {
        self.write_outs.pop_front()
    }

    /// Handle external events (not used).
    ///
    /// mDNS does not use external events. This method does nothing.
    fn handle_event(&mut self, _evt: ()) -> Result<()> {
        Ok(())
    }

    /// Get the next event.
    ///
    /// Call this method repeatedly until it returns `None` to process all
    /// queued events. Events are generated when:
    /// - An mDNS answer matches a pending query ([`MdnsEvent::QueryAnswered`])
    /// - A query ends without an address ([`MdnsEvent::QueryTimeout`])
    /// - A local name is lost to another host ([`MdnsEvent::LocalNameConflict`])
    ///
    /// # Returns
    ///
    /// The next event, or `None` if the queue is empty.
    ///
    /// # Example
    ///
    /// ```rust,ignore
    /// while let Some(event) = mdns.poll_event() {
    ///     match event {
    ///         MdnsEvent::QueryAnswered(query_id, addr) => {
    ///             println!("Query {} resolved to {}", query_id, addr);
    ///         }
    ///         MdnsEvent::QueryTimeout(id) => {
    ///             println!("Query {} timed out", id);
    ///         }
    ///         _ => {}
    ///     }
    /// }
    /// ```
    fn poll_event(&mut self) -> Option<Self::Eout> {
        self.event_outs.pop_front()
    }

    /// Handle timeout: send what is due.
    ///
    /// Call this method when the deadline from `poll_timeout()`
    /// is reached. It starts what was registered or scheduled since the last call, ends the
    /// queries whose timeout has passed, and queues the packets that are due: query packets
    /// and retries, probes, announcements, delayed responses, goodbyes. They can be retrieved
    /// with `poll_write()`.
    ///
    /// # Arguments
    ///
    /// * `now` - The current time
    ///
    /// # Errors
    ///
    /// Returns [`Error::ErrConnectionClosed`] if the connection has been closed.
    ///
    /// # Example
    ///
    /// ```rust
    /// use rtci_mdns::{MdnsConfig, Mdns};
    /// use sansio::Protocol;
    /// use std::time::{Duration, Instant};
    ///
    /// let start = Instant::now();
    /// let mut mdns = Mdns::new(start, MdnsConfig::default());
    /// mdns.query_now(start, "device.local");
    ///
    /// // Consume initial packet
    /// mdns.poll_write();
    ///
    /// // The first retry is due one second after the query
    /// assert_eq!(mdns.poll_timeout(), Some(start + Duration::from_secs(1)));
    /// mdns.handle_timeout(start + Duration::from_secs(1)).unwrap();
    ///
    /// // A retry packet should be queued
    /// assert!(mdns.poll_write().is_some());
    /// ```
    fn handle_timeout(&mut self, now: Self::Time) -> Result<()> {
        if self.closed {
            return Err(Error::ErrConnectionClosed);
        }
        self.last_now = now;

        self.start_records(now);
        self.start_queries(now);
        self.expire_queries(now);
        self.cache.retain(|cached| cached.expires > now);
        self.expire_stale(now);

        self.flush(now);
        self.update_next_timeout();
        Ok(())
    }

    /// Get the next timeout deadline.
    ///
    /// Returns the time at which `handle_timeout()` should
    /// be called next. Use this to schedule your event loop's sleep/wait.
    ///
    /// # Returns
    ///
    /// - `Some(instant)` when something is due then: a query's packet or timeout, a probe, an
    ///   announcement, a delayed response, a goodbye. An instant that is not in the future
    ///   means "call `handle_timeout` now".
    /// - `None` if nothing is pending
    ///
    /// # Example
    ///
    /// ```rust
    /// # use std::time::Instant;
    /// use rtci_mdns::{MdnsConfig, Mdns};
    /// use sansio::Protocol;
    ///
    /// let mut mdns = Mdns::new(Instant::now(), MdnsConfig::default());
    ///
    /// // No queries, no timeout
    /// assert!(mdns.poll_timeout().is_none());
    ///
    /// // Start a query
    /// mdns.query_now(Instant::now(), "device.local");
    ///
    /// // Now there's a timeout scheduled
    /// assert!(mdns.poll_timeout().is_some());
    /// ```
    fn poll_timeout(&mut self) -> Option<Self::Time> {
        if self.closed {
            None
        } else if self.asap {
            // Something waits for the time: handle it at once.
            Some(self.last_now)
        } else {
            self.next_timeout
        }
    }

    /// Close the connection.
    ///
    /// This clears all pending queries and queued packets/events, then queues a goodbye (TTL
    /// 0, RFC 6762 section 10.1) for every name that was sent to the link: drain
    /// `poll_write()` once more after closing so that neighbours drop the names at once.
    /// After closing, `handle_read()` and
    /// `handle_timeout()` will return
    /// [`Error::ErrConnectionClosed`].
    ///
    /// # Example
    ///
    /// ```rust
    /// # use std::time::Instant;
    /// use rtci_mdns::{MdnsConfig, Mdns};
    /// use sansio::Protocol;
    ///
    /// let mut mdns = Mdns::new(Instant::now(), MdnsConfig::default());
    /// mdns.query_now(Instant::now(), "device.local");
    ///
    /// assert_eq!(mdns.pending_query_count(), 1);
    ///
    /// mdns.close().unwrap();
    ///
    /// // All state is cleared
    /// assert_eq!(mdns.pending_query_count(), 0);
    /// assert!(mdns.poll_write().is_none());
    /// assert!(mdns.poll_timeout().is_none());
    /// ```
    fn close(&mut self) -> Result<()> {
        if self.closed {
            return Ok(());
        }
        self.closed = true;
        self.queries.clear();
        self.cache.clear();
        self.reconfirms.clear();
        self.write_outs.clear();
        self.event_outs.clear();
        self.queue_closing_goodbyes();
        self.records.clear();
        self.goodbyes.clear();
        self.next_timeout = None;
        self.asap = false;
        Ok(())
    }
}

#[cfg(test)]
mod mdns_answer_trust_test;
#[cfg(test)]
mod mdns_resolver_test;
#[cfg(test)]
mod mdns_responder_test;
#[cfg(test)]
mod mdns_stale_test;
#[cfg(test)]
mod mdns_test;
#[cfg(test)]
mod mdns_timeout_test;
#[cfg(test)]
mod testkit;
#[cfg(test)]
mod wire_test;