ant-quic 0.26.2

QUIC transport protocol with advanced NAT traversal for P2P networks
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
// Copyright 2024 Saorsa Labs Ltd.
//
// This Saorsa Network Software is licensed under the General Public License (GPL), version 3.
// Please see the file LICENSE-GPL, or visit <http://www.gnu.org/licenses/> for the full text.
//
// Full details available at https://saorsalabs.com/licenses

use std::{cmp, net::SocketAddr};

use tracing::trace;

use super::{
    mtud::MtuDiscovery,
    pacing::Pacer,
    spaces::{PacketSpace, SentPacket},
};
use crate::{Duration, Instant, TIMER_GRANULARITY, TransportConfig, congestion, packet::SpaceId};

#[cfg(feature = "__qlog")]
use qlog::events::quic::MetricsUpdated;

/// Description of a particular network path
pub(super) struct PathData {
    pub(super) remote: SocketAddr,
    pub(super) rtt: RttEstimator,
    /// Whether we're enabling ECN on outgoing packets
    pub(super) sending_ecn: bool,
    /// Congestion controller state
    pub(super) congestion: Box<dyn congestion::Controller>,
    /// Pacing state
    pub(super) pacing: Pacer,
    pub(super) challenge: Option<u64>,
    pub(super) challenge_pending: bool,
    /// Whether we're certain the peer can both send and receive on this address
    ///
    /// Initially equal to `use_stateless_retry` for servers, and becomes false again on every
    /// migration. Always true for clients.
    pub(super) validated: bool,
    /// Total size of all UDP datagrams sent on this path
    pub(super) total_sent: u64,
    /// Total size of all UDP datagrams received on this path
    pub(super) total_recvd: u64,
    /// The state of the MTU discovery process
    pub(super) mtud: MtuDiscovery,
    /// Packet number of the first packet sent after an RTT sample was collected on this path
    ///
    /// Used in persistent congestion determination.
    pub(super) first_packet_after_rtt_sample: Option<(SpaceId, u64)>,
    pub(super) in_flight: InFlight,
    /// Number of the first packet sent on this path
    ///
    /// Used to determine whether a packet was sent on an earlier path. Insufficient to determine if
    /// a packet was sent on a later path.
    first_packet: Option<u64>,

    /// Snapshot of the qlog recovery metrics
    #[cfg(feature = "__qlog")]
    congestion_metrics: CongestionMetrics,

    /// Address discovery information for this path
    pub(super) address_info: PathAddressInfo,
    /// Rate limiter for OBSERVED_ADDRESS frames on this path
    pub(super) observation_rate_limiter: PathObservationRateLimiter,
}

impl PathData {
    pub(super) fn new(
        remote: SocketAddr,
        allow_mtud: bool,
        peer_max_udp_payload_size: Option<u16>,
        now: Instant,
        config: &TransportConfig,
    ) -> Self {
        let congestion = config.congestion_controller_factory.new_controller(
            config.get_initial_mtu() as u64,
            16 * 1024 * 1024,
            now,
        );
        Self {
            remote,
            rtt: RttEstimator::new(config.initial_rtt),
            sending_ecn: true,
            pacing: Pacer::new(
                config.initial_rtt,
                congestion.initial_window(),
                config.get_initial_mtu(),
                now,
            ),
            congestion,
            challenge: None,
            challenge_pending: false,
            validated: false,
            total_sent: 0,
            total_recvd: 0,
            mtud: config
                .mtu_discovery_config
                .as_ref()
                .filter(|_| allow_mtud)
                .map_or(
                    MtuDiscovery::disabled(config.get_initial_mtu(), config.min_mtu),
                    |mtud_config| {
                        MtuDiscovery::new(
                            config.get_initial_mtu(),
                            config.min_mtu,
                            peer_max_udp_payload_size,
                            mtud_config.clone(),
                        )
                    },
                ),
            first_packet_after_rtt_sample: None,
            in_flight: InFlight::new(),
            first_packet: None,
            #[cfg(feature = "__qlog")]
            congestion_metrics: CongestionMetrics::default(),
            address_info: PathAddressInfo::new(),
            observation_rate_limiter: PathObservationRateLimiter::new(10, now), // Default rate of 10
        }
    }

    pub(super) fn from_previous(remote: SocketAddr, prev: &Self, now: Instant) -> Self {
        let congestion = prev.congestion.clone_box();
        let smoothed_rtt = prev.rtt.get();
        Self {
            remote,
            rtt: prev.rtt,
            pacing: Pacer::new(smoothed_rtt, congestion.window(), prev.current_mtu(), now),
            sending_ecn: true,
            congestion,
            challenge: None,
            challenge_pending: false,
            validated: false,
            total_sent: 0,
            total_recvd: 0,
            mtud: prev.mtud.clone(),
            first_packet_after_rtt_sample: prev.first_packet_after_rtt_sample,
            in_flight: InFlight::new(),
            first_packet: None,
            #[cfg(feature = "__qlog")]
            congestion_metrics: prev.congestion_metrics.clone(),
            address_info: PathAddressInfo::new(), // Reset for new path
            observation_rate_limiter: PathObservationRateLimiter::new(
                prev.observation_rate_limiter.rate as u8,
                now,
            ), // Fresh limiter with same rate
        }
    }

    /// Resets RTT, congestion control and MTU states.
    ///
    /// This is useful when it is known the underlying path has changed.
    pub(super) fn reset(&mut self, now: Instant, config: &TransportConfig) {
        self.rtt = RttEstimator::new(config.initial_rtt);
        self.congestion = config.congestion_controller_factory.new_controller(
            config.get_initial_mtu() as u64,
            16 * 1024 * 1024,
            now,
        );
        self.mtud.reset(config.get_initial_mtu(), config.min_mtu);
        self.address_info = PathAddressInfo::new(); // Reset address info
        // Reset tokens but preserve rate
        let rate = self.observation_rate_limiter.rate as u8;
        self.observation_rate_limiter = PathObservationRateLimiter::new(rate, now);
    }

    /// Update the observed address for this path
    pub(super) fn update_observed_address(&mut self, address: SocketAddr, now: Instant) {
        self.address_info.update_observed_address(address, now);
    }

    /// Check if the observed address has changed from the expected remote address
    #[allow(dead_code)]
    pub(super) fn has_address_changed(&self) -> bool {
        self.address_info.has_address_changed(&self.remote)
    }

    /// Mark that we've notified the application about the current address
    #[allow(dead_code)]
    pub(super) fn mark_address_notified(&mut self) {
        self.address_info.mark_notified();
    }

    /// Check if we can send an observation on this path
    #[allow(dead_code)]
    pub(super) fn can_send_observation(&mut self, now: Instant) -> bool {
        self.observation_rate_limiter.can_send(now)
    }

    /// Consume a token for sending an observation
    #[allow(dead_code)]
    pub(super) fn consume_observation_token(&mut self, now: Instant) {
        self.observation_rate_limiter.consume_token(now)
    }

    /// Update observation tokens based on elapsed time
    #[allow(dead_code)]
    pub(super) fn update_observation_tokens(&mut self, now: Instant) {
        self.observation_rate_limiter.update_tokens(now)
    }

    /// Set the observation rate for this path
    pub(super) fn set_observation_rate(&mut self, rate: u8) {
        self.observation_rate_limiter.set_rate(rate)
    }

    /// Indicates whether we're a server that hasn't validated the peer's address and hasn't
    /// received enough data from the peer to permit sending `bytes_to_send` additional bytes
    pub(super) fn anti_amplification_blocked(&self, bytes_to_send: u64) -> bool {
        !self.validated && self.total_recvd * 3 < self.total_sent + bytes_to_send
    }

    /// Returns the path's current MTU
    pub(super) fn current_mtu(&self) -> u16 {
        self.mtud.current_mtu()
    }

    /// Account for transmission of `packet` with number `pn` in `space`
    pub(super) fn sent(&mut self, pn: u64, packet: SentPacket, space: &mut PacketSpace) {
        self.in_flight.insert(&packet);
        if self.first_packet.is_none() {
            self.first_packet = Some(pn);
        }
        self.in_flight.bytes -= space.sent(pn, packet);
    }

    /// Remove `packet` with number `pn` from this path's congestion control counters, or return
    /// `false` if `pn` was sent before this path was established.
    pub(super) fn remove_in_flight(&mut self, pn: u64, packet: &SentPacket) -> bool {
        if self.first_packet.is_none_or(|first| first > pn) {
            return false;
        }
        self.in_flight.remove(packet);
        true
    }

    #[cfg(feature = "__qlog")]
    #[allow(dead_code)]
    pub(super) fn qlog_congestion_metrics(&mut self, pto_count: u32) -> Option<MetricsUpdated> {
        let controller_metrics = self.congestion.metrics();

        let metrics = CongestionMetrics {
            min_rtt: Some(self.rtt.min),
            smoothed_rtt: Some(self.rtt.get()),
            latest_rtt: Some(self.rtt.latest),
            rtt_variance: Some(self.rtt.var),
            pto_count: Some(pto_count),
            bytes_in_flight: Some(self.in_flight.bytes),
            packets_in_flight: Some(self.in_flight.ack_eliciting),

            congestion_window: Some(controller_metrics.congestion_window),
            ssthresh: controller_metrics.ssthresh,
            pacing_rate: controller_metrics.pacing_rate,
        };

        let event = metrics.to_qlog_event(&self.congestion_metrics);
        self.congestion_metrics = metrics;
        event
    }
}

/// Congestion metrics as described in [`recovery_metrics_updated`].
///
/// [`recovery_metrics_updated`]: https://datatracker.ietf.org/doc/html/draft-ietf-quic-qlog-quic-events.html#name-recovery_metrics_updated
#[cfg(feature = "__qlog")]
#[derive(Default, Clone, PartialEq)]
#[non_exhaustive]
struct CongestionMetrics {
    pub min_rtt: Option<Duration>,
    pub smoothed_rtt: Option<Duration>,
    pub latest_rtt: Option<Duration>,
    pub rtt_variance: Option<Duration>,
    pub pto_count: Option<u32>,
    pub bytes_in_flight: Option<u64>,
    pub packets_in_flight: Option<u64>,
    pub congestion_window: Option<u64>,
    pub ssthresh: Option<u64>,
    pub pacing_rate: Option<u64>,
}

#[cfg(feature = "__qlog")]
impl CongestionMetrics {
    /// Retain only values that have been updated since the last snapshot.
    #[allow(dead_code)]
    fn retain_updated(&self, previous: &Self) -> Self {
        macro_rules! keep_if_changed {
            ($name:ident) => {
                if previous.$name == self.$name {
                    None
                } else {
                    self.$name
                }
            };
        }

        Self {
            min_rtt: keep_if_changed!(min_rtt),
            smoothed_rtt: keep_if_changed!(smoothed_rtt),
            latest_rtt: keep_if_changed!(latest_rtt),
            rtt_variance: keep_if_changed!(rtt_variance),
            pto_count: keep_if_changed!(pto_count),
            bytes_in_flight: keep_if_changed!(bytes_in_flight),
            packets_in_flight: keep_if_changed!(packets_in_flight),
            congestion_window: keep_if_changed!(congestion_window),
            ssthresh: keep_if_changed!(ssthresh),
            pacing_rate: keep_if_changed!(pacing_rate),
        }
    }

    /// Emit a `MetricsUpdated` event containing only updated values
    #[allow(dead_code)]
    fn to_qlog_event(&self, previous: &Self) -> Option<MetricsUpdated> {
        let updated = self.retain_updated(previous);

        if updated == Self::default() {
            return None;
        }

        Some(MetricsUpdated {
            min_rtt: updated.min_rtt.map(|rtt| rtt.as_secs_f32()),
            smoothed_rtt: updated.smoothed_rtt.map(|rtt| rtt.as_secs_f32()),
            latest_rtt: updated.latest_rtt.map(|rtt| rtt.as_secs_f32()),
            rtt_variance: updated.rtt_variance.map(|rtt| rtt.as_secs_f32()),
            pto_count: updated
                .pto_count
                .map(|count| count.try_into().unwrap_or(u16::MAX)),
            bytes_in_flight: updated.bytes_in_flight,
            packets_in_flight: updated.packets_in_flight,
            congestion_window: updated.congestion_window,
            ssthresh: updated.ssthresh,
            pacing_rate: updated.pacing_rate,
        })
    }
}

/// RTT estimation for a particular network path
#[derive(Copy, Clone)]
pub struct RttEstimator {
    /// The most recent RTT measurement made when receiving an ack for a previously unacked packet
    latest: Duration,
    /// The smoothed RTT of the connection, computed as described in RFC6298
    smoothed: Option<Duration>,
    /// The RTT variance, computed as described in RFC6298
    var: Duration,
    /// The minimum RTT seen in the connection, ignoring ack delay.
    min: Duration,
}

impl RttEstimator {
    fn new(initial_rtt: Duration) -> Self {
        Self {
            latest: initial_rtt,
            smoothed: None,
            var: initial_rtt / 2,
            min: initial_rtt,
        }
    }

    /// The current best RTT estimation.
    pub fn get(&self) -> Duration {
        self.smoothed.unwrap_or(self.latest)
    }

    /// Conservative estimate of RTT
    ///
    /// Takes the maximum of smoothed and latest RTT, as recommended
    /// in 6.1.2 of the recovery spec (draft 29).
    pub fn conservative(&self) -> Duration {
        self.get().max(self.latest)
    }

    /// Minimum RTT registered so far for this estimator.
    pub fn min(&self) -> Duration {
        self.min
    }

    // PTO computed as described in RFC9002#6.2.1
    pub(crate) fn pto_base(&self) -> Duration {
        self.get() + cmp::max(4 * self.var, TIMER_GRANULARITY)
    }

    pub(crate) fn update(&mut self, ack_delay: Duration, rtt: Duration) {
        self.latest = rtt;
        // min_rtt ignores ack delay.
        self.min = cmp::min(self.min, self.latest);
        // Based on RFC6298.
        if let Some(smoothed) = self.smoothed {
            let adjusted_rtt = if self.min + ack_delay <= self.latest {
                self.latest - ack_delay
            } else {
                self.latest
            };
            let var_sample = smoothed.abs_diff(adjusted_rtt);
            self.var = (3 * self.var + var_sample) / 4;
            self.smoothed = Some((7 * smoothed + adjusted_rtt) / 8);
        } else {
            self.smoothed = Some(self.latest);
            self.var = self.latest / 2;
            self.min = self.latest;
        }
    }
}

#[derive(Default)]
pub(crate) struct PathResponses {
    pending: Vec<PathResponse>,
}

impl PathResponses {
    pub(crate) fn push(&mut self, packet: u64, token: u64, remote: SocketAddr) {
        /// Arbitrary permissive limit to prevent abuse
        const MAX_PATH_RESPONSES: usize = 16;
        let response = PathResponse {
            packet,
            token,
            remote,
        };
        let existing = self.pending.iter_mut().find(|x| x.remote == remote);
        if let Some(existing) = existing {
            // Update a queued response
            if existing.packet <= packet {
                *existing = response;
            }
            return;
        }
        if self.pending.len() < MAX_PATH_RESPONSES {
            self.pending.push(response);
        } else {
            // We don't expect to ever hit this with well-behaved peers, so we don't bother dropping
            // older challenges.
            trace!("ignoring excessive PATH_CHALLENGE");
        }
    }

    pub(crate) fn pop_off_path(&mut self, remote: SocketAddr) -> Option<(u64, SocketAddr)> {
        let response = *self.pending.last()?;
        if response.remote == remote {
            // We don't bother searching further because we expect that the on-path response will
            // get drained in the immediate future by a call to `pop_on_path`
            return None;
        }
        self.pending.pop();
        Some((response.token, response.remote))
    }

    pub(crate) fn pop_on_path(&mut self, remote: SocketAddr) -> Option<u64> {
        let response = *self.pending.last()?;
        if response.remote != remote {
            // We don't bother searching further because we expect that the off-path response will
            // get drained in the immediate future by a call to `pop_off_path`
            return None;
        }
        self.pending.pop();
        Some(response.token)
    }

    pub(crate) fn is_empty(&self) -> bool {
        self.pending.is_empty()
    }
}

#[derive(Copy, Clone)]
struct PathResponse {
    /// The packet number the corresponding PATH_CHALLENGE was received in
    packet: u64,
    token: u64,
    /// The address the corresponding PATH_CHALLENGE was received from
    remote: SocketAddr,
}

/// Summary statistics of packets that have been sent on a particular path, but which have not yet
/// been acked or deemed lost
pub(super) struct InFlight {
    /// Sum of the sizes of all sent packets considered "in flight" by congestion control
    ///
    /// The size does not include IP or UDP overhead. Packets only containing ACK frames do not
    /// count towards this to ensure congestion control does not impede congestion feedback.
    pub(super) bytes: u64,
    /// Number of packets in flight containing frames other than ACK and PADDING
    ///
    /// This can be 0 even when bytes is not 0 because PADDING frames cause a packet to be
    /// considered "in flight" by congestion control. However, if this is nonzero, bytes will always
    /// also be nonzero.
    pub(super) ack_eliciting: u64,
}

impl InFlight {
    fn new() -> Self {
        Self {
            bytes: 0,
            ack_eliciting: 0,
        }
    }

    fn insert(&mut self, packet: &SentPacket) {
        self.bytes += u64::from(packet.size);
        self.ack_eliciting += u64::from(packet.ack_eliciting);
    }

    /// Update counters to account for a packet becoming acknowledged, lost, or abandoned
    fn remove(&mut self, packet: &SentPacket) {
        self.bytes -= u64::from(packet.size);
        self.ack_eliciting -= u64::from(packet.ack_eliciting);
    }
}

/// Information about addresses observed for a specific path
#[derive(Debug, Clone, PartialEq, Eq)]
pub(super) struct PathAddressInfo {
    /// The most recently observed address for this path
    pub(super) observed_address: Option<SocketAddr>,
    /// When the address was last observed
    pub(super) last_observed: Option<Instant>,
    /// Number of times the address has been observed
    pub(super) observation_count: u64,
    /// Whether we've notified the application about this address
    pub(super) notified: bool,
}

/// Rate limiter for OBSERVED_ADDRESS frames per path
#[derive(Debug, Clone)]
pub(super) struct PathObservationRateLimiter {
    /// Tokens available for sending observations
    pub(super) tokens: f64,
    /// Maximum tokens (burst capacity)
    pub(super) max_tokens: f64,
    /// Rate of token replenishment (tokens per second)
    pub(super) rate: f64,
    /// Last time tokens were updated
    pub(super) last_update: Instant,
}

impl PathObservationRateLimiter {
    /// Create a new rate limiter with the given rate
    pub(super) fn new(rate: u8, now: Instant) -> Self {
        let rate_f64 = rate as f64;
        Self {
            tokens: rate_f64,
            max_tokens: rate_f64,
            rate: rate_f64,
            last_update: now,
        }
    }

    /// Update tokens based on elapsed time
    pub(super) fn update_tokens(&mut self, now: Instant) {
        let elapsed = now
            .saturating_duration_since(self.last_update)
            .as_secs_f64();
        self.tokens = (self.tokens + elapsed * self.rate).min(self.max_tokens);
        self.last_update = now;
    }

    /// Check if we can send an observation
    pub(super) fn can_send(&mut self, now: Instant) -> bool {
        self.update_tokens(now);
        self.tokens >= 1.0
    }

    /// Consume a token for sending an observation
    pub(super) fn consume_token(&mut self, now: Instant) {
        self.update_tokens(now);
        if self.tokens >= 1.0 {
            self.tokens -= 1.0;
        }
    }

    /// Update the rate
    pub(super) fn set_rate(&mut self, rate: u8) {
        let rate_f64 = rate as f64;
        self.rate = rate_f64;
        self.max_tokens = rate_f64;
        // Don't change current tokens, just cap at new max
        self.tokens = self.tokens.min(self.max_tokens);
    }
}

impl PathAddressInfo {
    pub(super) fn new() -> Self {
        Self {
            observed_address: None,
            last_observed: None,
            observation_count: 0,
            notified: false,
        }
    }

    /// Update with a newly observed address
    pub(super) fn update_observed_address(&mut self, address: SocketAddr, now: Instant) {
        if self.observed_address == Some(address) {
            // Same address observed again - preserve notification status
            self.observation_count += 1;
        } else {
            // New address observed
            self.observed_address = Some(address);
            self.observation_count = 1;
            self.notified = false; // Reset notification flag for new address
        }
        self.last_observed = Some(now);
    }

    /// Check if the observed address has changed from the expected address
    pub(super) fn has_address_changed(&self, expected: &SocketAddr) -> bool {
        match self.observed_address {
            Some(observed) => observed != *expected,
            None => false,
        }
    }

    /// Mark that we've notified the application about this address
    pub(super) fn mark_notified(&mut self) {
        self.notified = true;
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use std::net::{IpAddr, Ipv4Addr, Ipv6Addr};

    #[test]
    fn path_address_info_new() {
        let info = PathAddressInfo::new();
        assert_eq!(info.observed_address, None);
        assert_eq!(info.last_observed, None);
        assert_eq!(info.observation_count, 0);
        assert!(!info.notified);
    }

    #[test]
    fn path_address_info_update_new_address() {
        let mut info = PathAddressInfo::new();
        let addr = SocketAddr::new(IpAddr::V4(Ipv4Addr::new(192, 168, 1, 1)), 8080);
        let now = Instant::now();

        info.update_observed_address(addr, now);

        assert_eq!(info.observed_address, Some(addr));
        assert_eq!(info.last_observed, Some(now));
        assert_eq!(info.observation_count, 1);
        assert!(!info.notified);
    }

    #[test]
    fn path_address_info_update_same_address() {
        let mut info = PathAddressInfo::new();
        let addr = SocketAddr::new(IpAddr::V4(Ipv4Addr::new(10, 0, 0, 1)), 443);
        let now1 = Instant::now();

        info.update_observed_address(addr, now1);
        assert_eq!(info.observation_count, 1);

        let now2 = now1 + Duration::from_secs(1);
        info.update_observed_address(addr, now2);

        assert_eq!(info.observed_address, Some(addr));
        assert_eq!(info.last_observed, Some(now2));
        assert_eq!(info.observation_count, 2);
    }

    #[test]
    fn path_address_info_update_different_address() {
        let mut info = PathAddressInfo::new();
        let addr1 = SocketAddr::new(IpAddr::V4(Ipv4Addr::new(192, 168, 1, 1)), 8080);
        let addr2 = SocketAddr::new(IpAddr::V4(Ipv4Addr::new(10, 0, 0, 1)), 443);
        let now1 = Instant::now();

        info.update_observed_address(addr1, now1);
        info.mark_notified();
        assert!(info.notified);

        let now2 = now1 + Duration::from_secs(1);
        info.update_observed_address(addr2, now2);

        assert_eq!(info.observed_address, Some(addr2));
        assert_eq!(info.last_observed, Some(now2));
        assert_eq!(info.observation_count, 1);
        assert!(!info.notified); // Reset when address changes
    }

    #[test]
    fn path_address_info_has_address_changed() {
        let mut info = PathAddressInfo::new();
        let expected = SocketAddr::new(IpAddr::V4(Ipv4Addr::new(192, 168, 1, 1)), 8080);
        let observed = SocketAddr::new(IpAddr::V4(Ipv4Addr::new(10, 0, 0, 1)), 443);

        // No observed address yet
        assert!(!info.has_address_changed(&expected));

        // Same as expected
        info.update_observed_address(expected, Instant::now());
        assert!(!info.has_address_changed(&expected));

        // Different from expected
        info.update_observed_address(observed, Instant::now());
        assert!(info.has_address_changed(&expected));
    }

    #[test]
    fn path_address_info_ipv6() {
        let mut info = PathAddressInfo::new();
        let addr = SocketAddr::new(
            IpAddr::V6(Ipv6Addr::new(0x2001, 0xdb8, 0, 0, 0, 0, 0, 1)),
            8080,
        );
        let now = Instant::now();

        info.update_observed_address(addr, now);

        assert_eq!(info.observed_address, Some(addr));
        assert_eq!(info.observation_count, 1);
    }

    #[test]
    fn path_address_info_notification_tracking() {
        let mut info = PathAddressInfo::new();
        assert!(!info.notified);

        // First observe an address
        let addr = SocketAddr::new(IpAddr::V4(Ipv4Addr::new(127, 0, 0, 1)), 80);
        info.update_observed_address(addr, Instant::now());
        assert!(!info.notified);

        // Mark as notified
        info.mark_notified();
        assert!(info.notified);

        // Notification flag persists when observing same address
        info.update_observed_address(addr, Instant::now());
        assert!(info.notified); // Still true for same address

        // But resets on address change
        let new_addr = SocketAddr::new(IpAddr::V4(Ipv4Addr::new(127, 0, 0, 2)), 80);
        info.update_observed_address(new_addr, Instant::now());
        assert!(!info.notified);
    }

    // Tests for PathData with address discovery integration
    #[test]
    fn path_data_with_address_info() {
        let remote = SocketAddr::new(IpAddr::V4(Ipv4Addr::new(192, 168, 1, 1)), 8080);
        let config = TransportConfig::default();
        let now = Instant::now();

        let path = PathData::new(remote, false, None, now, &config);

        // Should have address_info field
        assert!(path.address_info.observed_address.is_none());
        assert!(path.address_info.last_observed.is_none());
        assert_eq!(path.address_info.observation_count, 0);
        assert!(!path.address_info.notified);
    }

    #[test]
    fn path_data_update_observed_address() {
        let remote = SocketAddr::new(IpAddr::V4(Ipv4Addr::new(192, 168, 1, 1)), 8080);
        let config = TransportConfig::default();
        let now = Instant::now();

        let mut path = PathData::new(remote, false, None, now, &config);

        // Update observed address
        let observed = SocketAddr::new(IpAddr::V4(Ipv4Addr::new(10, 0, 0, 1)), 443);
        path.update_observed_address(observed, now);

        assert_eq!(path.address_info.observed_address, Some(observed));
        assert_eq!(path.address_info.last_observed, Some(now));
        assert_eq!(path.address_info.observation_count, 1);
        assert!(!path.address_info.notified);
    }

    #[test]
    fn path_data_has_address_changed() {
        let remote = SocketAddr::new(IpAddr::V4(Ipv4Addr::new(192, 168, 1, 1)), 8080);
        let config = TransportConfig::default();
        let now = Instant::now();

        let mut path = PathData::new(remote, false, None, now, &config);

        // No change when no observed address
        assert!(!path.has_address_changed());

        // Update with same as remote
        path.update_observed_address(remote, now);
        assert!(!path.has_address_changed());

        // Update with different address
        let different = SocketAddr::new(IpAddr::V4(Ipv4Addr::new(10, 0, 0, 1)), 443);
        path.update_observed_address(different, now);
        assert!(path.has_address_changed());
    }

    #[test]
    fn path_data_mark_address_notified() {
        let remote = SocketAddr::new(IpAddr::V4(Ipv4Addr::new(192, 168, 1, 1)), 8080);
        let config = TransportConfig::default();
        let now = Instant::now();

        let mut path = PathData::new(remote, false, None, now, &config);

        // Update and mark as notified
        let observed = SocketAddr::new(IpAddr::V4(Ipv4Addr::new(10, 0, 0, 1)), 443);
        path.update_observed_address(observed, now);
        assert!(!path.address_info.notified);

        path.mark_address_notified();
        assert!(path.address_info.notified);
    }

    #[test]
    fn path_data_from_previous_preserves_address_info() {
        let remote1 = SocketAddr::new(IpAddr::V4(Ipv4Addr::new(192, 168, 1, 1)), 8080);
        let remote2 = SocketAddr::new(IpAddr::V4(Ipv4Addr::new(10, 0, 0, 1)), 443);
        let config = TransportConfig::default();
        let now = Instant::now();

        let mut path1 = PathData::new(remote1, false, None, now, &config);

        // Set up address info
        let observed = SocketAddr::new(IpAddr::V4(Ipv4Addr::new(1, 2, 3, 4)), 5678);
        path1.update_observed_address(observed, now);
        path1.mark_address_notified();

        // Create new path from previous
        let path2 = PathData::from_previous(remote2, &path1, now);

        // Address info should be reset for new path
        assert!(path2.address_info.observed_address.is_none());
        assert!(path2.address_info.last_observed.is_none());
        assert_eq!(path2.address_info.observation_count, 0);
        assert!(!path2.address_info.notified);
    }

    #[test]
    fn path_data_reset_clears_address_info() {
        let remote = SocketAddr::new(IpAddr::V4(Ipv4Addr::new(192, 168, 1, 1)), 8080);
        let config = TransportConfig::default();
        let now = Instant::now();

        let mut path = PathData::new(remote, false, None, now, &config);

        // Set up address info
        let observed = SocketAddr::new(IpAddr::V4(Ipv4Addr::new(10, 0, 0, 1)), 443);
        path.update_observed_address(observed, now);
        path.mark_address_notified();

        // Reset should clear address info
        path.reset(now, &config);

        assert!(path.address_info.observed_address.is_none());
        assert!(path.address_info.last_observed.is_none());
        assert_eq!(path.address_info.observation_count, 0);
        assert!(!path.address_info.notified);
    }

    // Tests for per-path rate limiting
    #[test]
    fn path_data_with_rate_limiter() {
        let remote = SocketAddr::new(IpAddr::V4(Ipv4Addr::new(192, 168, 1, 1)), 8080);
        let config = TransportConfig::default();
        let now = Instant::now();

        let path = PathData::new(remote, false, None, now, &config);

        // Should have a rate limiter
        assert!(path.observation_rate_limiter.tokens > 0.0);
        assert_eq!(path.observation_rate_limiter.rate, 10.0); // Default rate
        assert_eq!(path.observation_rate_limiter.max_tokens, 10.0);
        assert_eq!(path.observation_rate_limiter.last_update, now);
    }

    #[test]
    fn path_data_can_send_observation() {
        let remote = SocketAddr::new(IpAddr::V4(Ipv4Addr::new(192, 168, 1, 1)), 8080);
        let config = TransportConfig::default();
        let now = Instant::now();

        let mut path = PathData::new(remote, false, None, now, &config);

        // Should be able to send initially (has tokens)
        assert!(path.can_send_observation(now));

        // Consume a token
        path.consume_observation_token(now);

        // Should still have tokens available
        assert!(path.can_send_observation(now));

        // Consume all tokens
        for _ in 0..9 {
            path.consume_observation_token(now);
        }

        // Should be out of tokens
        assert!(!path.can_send_observation(now));

        // Wait for token replenishment
        let later = now + Duration::from_millis(200); // 0.2 seconds = 2 tokens
        assert!(path.can_send_observation(later));
    }

    #[test]
    fn path_data_rate_limiter_replenishment() {
        let remote = SocketAddr::new(IpAddr::V4(Ipv4Addr::new(192, 168, 1, 1)), 8080);
        let config = TransportConfig::default();
        let now = Instant::now();

        let mut path = PathData::new(remote, false, None, now, &config);

        // Consume all tokens
        for _ in 0..10 {
            path.consume_observation_token(now);
        }
        assert_eq!(path.observation_rate_limiter.tokens, 0.0);

        // Check replenishment after 1 second
        let later = now + Duration::from_secs(1);
        path.update_observation_tokens(later);

        // Should have replenished to max (rate = 10/sec)
        assert_eq!(path.observation_rate_limiter.tokens, 10.0);
    }

    #[test]
    fn path_data_rate_limiter_custom_rate() {
        let remote = SocketAddr::new(IpAddr::V4(Ipv4Addr::new(192, 168, 1, 1)), 8080);
        let config = TransportConfig::default();
        let now = Instant::now();

        let mut path = PathData::new(remote, false, None, now, &config);

        // Update with custom rate
        path.set_observation_rate(5); // 5 per second
        assert_eq!(path.observation_rate_limiter.rate, 5.0);
        assert_eq!(path.observation_rate_limiter.max_tokens, 5.0);

        // Consume all tokens
        for _ in 0..5 {
            path.consume_observation_token(now);
        }
        assert!(!path.can_send_observation(now));

        // Check replenishment with new rate
        let later = now + Duration::from_millis(400); // 0.4 seconds = 2 tokens at rate 5
        path.update_observation_tokens(later);
        assert_eq!(path.observation_rate_limiter.tokens, 2.0);
    }

    #[test]
    fn path_data_rate_limiter_from_previous() {
        let remote1 = SocketAddr::new(IpAddr::V4(Ipv4Addr::new(192, 168, 1, 1)), 8080);
        let remote2 = SocketAddr::new(IpAddr::V4(Ipv4Addr::new(10, 0, 0, 1)), 443);
        let config = TransportConfig::default();
        let now = Instant::now();

        let mut path1 = PathData::new(remote1, false, None, now, &config);

        // Set custom rate and consume some tokens
        path1.set_observation_rate(20);
        for _ in 0..5 {
            path1.consume_observation_token(now);
        }

        // Create new path from previous
        let path2 = PathData::from_previous(remote2, &path1, now);

        // New path should have fresh rate limiter with same rate
        assert_eq!(path2.observation_rate_limiter.rate, 20.0);
        assert_eq!(path2.observation_rate_limiter.max_tokens, 20.0);
        assert_eq!(path2.observation_rate_limiter.tokens, 20.0); // Full tokens
    }

    #[test]
    fn path_data_reset_preserves_rate() {
        let remote = SocketAddr::new(IpAddr::V4(Ipv4Addr::new(192, 168, 1, 1)), 8080);
        let config = TransportConfig::default();
        let now = Instant::now();

        let mut path = PathData::new(remote, false, None, now, &config);

        // Set custom rate
        path.set_observation_rate(15);

        // Consume some tokens
        for _ in 0..3 {
            path.consume_observation_token(now);
        }

        // Reset the path
        path.reset(now, &config);

        // Rate should be preserved, tokens should be reset
        assert_eq!(path.observation_rate_limiter.rate, 15.0);
        assert_eq!(path.observation_rate_limiter.tokens, 15.0); // Full tokens after reset
    }
}