tailfeather 0.0.1

Allocation-free, transport-agnostic building blocks for a Tailscale-compatible mesh node
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
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
//! The transport-agnostic, cooperative node state machine.
//!
//! [`Node`] owns protocol state, while callers own sockets, streams, storage,
//! clocks, and executors. Each input produces at most one explicit action.

use core::net::SocketAddr;

use heapless::{String, Vec};
use zeroize::Zeroize;

use crate::keys::{DiscoPrivate, Identity, MachinePrivate, NodePrivate, NodePublic};
use crate::netmap::{Netmap, Peer};
use crate::tunnel::{self, Action as TunnelAction, Device, Instant, PeerId, Tai64n};

/// Largest packet carried by a node action.
pub const MAX_ACTION_PACKET: usize = tunnel::MAX_DATAGRAM_LEN;
/// Largest bounded control request body.
pub const MAX_CONTROL_BODY: usize = 2048;

/// A DNS name and TCP port. DNS resolution belongs to the transport adapter.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct Endpoint {
    pub host: String<128>,
    pub port: u16,
}

impl Endpoint {
    pub fn new(host: &str, port: u16) -> Result<Self, NodeError> {
        Ok(Self {
            host: String::try_from(host).map_err(|_| NodeError::Capacity)?,
            port,
        })
    }
}

/// An owned bounded value which clears its initialized bytes on drop and never
/// exposes them through `Debug`.
pub struct SecretBuffer<const N: usize>(Vec<u8, N>);

impl<const N: usize> SecretBuffer<N> {
    pub fn try_from_slice(bytes: &[u8]) -> Result<Self, NodeError> {
        let mut value = Vec::new();
        value
            .extend_from_slice(bytes)
            .map_err(|_| NodeError::Capacity)?;
        Ok(Self(value))
    }

    pub fn as_slice(&self) -> &[u8] {
        self.0.as_slice()
    }
}

impl<const N: usize> Drop for SecretBuffer<N> {
    fn drop(&mut self) {
        self.0.as_mut_slice().zeroize();
    }
}

impl<const N: usize> core::fmt::Debug for SecretBuffer<N> {
    fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
        f.write_str("SecretBuffer(redacted)")
    }
}

/// Work for the caller's control-plane task.
#[derive(Debug)]
#[allow(
    clippy::large_enum_variant,
    reason = "bounded owned commands replace heap indirection"
)]
pub enum ControlCommand {
    Connect {
        endpoint: Endpoint,
    },
    Register {
        body: SecretBuffer<MAX_CONTROL_BODY>,
    },
    RequestMap,
    Disconnect,
}

/// Work for the caller's DERP task. Payload storage is bounded and zeroized.
#[derive(Debug)]
#[allow(
    clippy::large_enum_variant,
    reason = "bounded owned commands replace heap indirection"
)]
pub enum DerpCommand {
    Connect {
        region: u16,
        endpoint: Endpoint,
    },
    Send {
        region: u16,
        peer: NodePublic,
        packet: SecretBuffer<MAX_ACTION_PACKET>,
    },
    Disconnect {
        region: u16,
    },
}

#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum RegistrationState {
    WaitingForPersistence,
    Disconnected,
    Connecting,
    Registering,
    Registered,
}

#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum NodeStatus {
    PersistenceRequired,
    Offline,
    Connecting,
    Registered,
    MapChanged,
}

#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum NodeError {
    Capacity,
    InvalidState,
    Persistence,
    StaleGeneration,
    Tunnel(tunnel::Error),
}

impl From<tunnel::Error> for NodeError {
    fn from(value: tunnel::Error) -> Self {
        Self::Tunnel(value)
    }
}

impl core::fmt::Display for NodeError {
    fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
        f.write_str(match self {
            Self::Capacity => "a bounded node value is full",
            Self::InvalidState => "event is invalid in the current node state",
            Self::Persistence => "persisted state is corrupt or unknown",
            Self::StaleGeneration => "persistence acknowledgement is stale",
            Self::Tunnel(_) => "tunnel protocol error",
        })
    }
}

impl core::error::Error for NodeError {}

/// Versioned caller-owned persistent state.
///
/// Auth keys, netmaps, and tunnel session keys are intentionally absent.
pub struct PersistState {
    generation: u64,
    trusted_time_floor: u64,
    blob: [u8; Self::BLOB_LEN],
}

impl PersistState {
    const MAGIC: [u8; 4] = *b"TFST";
    const VERSION: u8 = 1;
    const PAYLOAD_LEN: usize = 4 + 1 + 8 + 8 + 32 * 3;
    const CHECKSUM_LEN: usize = 16;
    pub const BLOB_LEN: usize = Self::PAYLOAD_LEN + Self::CHECKSUM_LEN;

    fn encode(identity: &Identity, generation: u64, trusted_time_floor: u64) -> Self {
        let mut blob = [0; Self::BLOB_LEN];
        blob[..4].copy_from_slice(&Self::MAGIC);
        blob[4] = Self::VERSION;
        blob[5..13].copy_from_slice(&generation.to_le_bytes());
        blob[13..21].copy_from_slice(&trusted_time_floor.to_le_bytes());
        blob[21..53].copy_from_slice(&identity.machine.to_bytes());
        blob[53..85].copy_from_slice(&identity.node.to_bytes());
        blob[85..117].copy_from_slice(&identity.disco.to_bytes());
        let checksum = tunnel::crypto::hash(&[&blob[..Self::PAYLOAD_LEN]]);
        blob[Self::PAYLOAD_LEN..].copy_from_slice(&checksum[..Self::CHECKSUM_LEN]);
        Self {
            generation,
            trusted_time_floor,
            blob,
        }
    }

    pub fn from_bytes(bytes: &[u8]) -> Result<Self, NodeError> {
        if bytes.len() != Self::BLOB_LEN || bytes[..4] != Self::MAGIC || bytes[4] != Self::VERSION {
            return Err(NodeError::Persistence);
        }
        let checksum = tunnel::crypto::hash(&[&bytes[..Self::PAYLOAD_LEN]]);
        if !tunnel::crypto::ct_eq(&checksum[..Self::CHECKSUM_LEN], &bytes[Self::PAYLOAD_LEN..]) {
            return Err(NodeError::Persistence);
        }
        let mut blob = [0; Self::BLOB_LEN];
        blob.copy_from_slice(bytes);
        let generation = u64::from_le_bytes(blob[5..13].try_into().unwrap());
        let trusted_time_floor = u64::from_le_bytes(blob[13..21].try_into().unwrap());
        if generation == 0 {
            return Err(NodeError::Persistence);
        }
        Ok(Self {
            generation,
            trusted_time_floor,
            blob,
        })
    }

    pub fn as_bytes(&self) -> &[u8] {
        &self.blob
    }
    pub fn generation(&self) -> u64 {
        self.generation
    }
    pub fn trusted_time_floor(&self) -> u64 {
        self.trusted_time_floor
    }

    fn identity(&self) -> Identity {
        let mut key = [0; 32];
        key.copy_from_slice(&self.blob[21..53]);
        let machine = MachinePrivate::from_bytes(key);
        key.copy_from_slice(&self.blob[53..85]);
        let node = NodePrivate::from_bytes(key);
        key.copy_from_slice(&self.blob[85..117]);
        let disco = DiscoPrivate::from_bytes(key);
        key.zeroize();
        Identity {
            machine,
            node,
            disco,
        }
    }
}

impl Drop for PersistState {
    fn drop(&mut self) {
        self.blob.zeroize();
    }
}

impl core::fmt::Debug for PersistState {
    fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
        f.debug_struct("PersistState")
            .field("generation", &self.generation)
            .field("trusted_time_floor", &self.trusted_time_floor)
            .field("blob", &"redacted")
            .finish()
    }
}

#[derive(Debug)]
#[allow(
    clippy::large_enum_variant,
    reason = "bounded owned actions replace heap indirection"
)]
pub enum NodeAction {
    DirectTransmit {
        peer: NodePublic,
        endpoint: SocketAddr,
        packet: SecretBuffer<MAX_ACTION_PACKET>,
    },
    Control(ControlCommand),
    Derp(DerpCommand),
    DeliveredIpv4 {
        peer: NodePublic,
        packet: SecretBuffer<MAX_ACTION_PACKET>,
    },
    PersistState(PersistState),
    StatusChanged(NodeStatus),
}

/// Control events contain decoded protocol-level data, never stream handles.
pub enum ControlEvent<'a, const PEERS: usize = 32> {
    Connected,
    Registered,
    Disconnected,
    /// `lease` is the exact caller-owned staging map. On success it is swapped
    /// with the live map; on error it is returned untouched.
    MapFinished {
        lease: &'a mut Netmap<PEERS>,
        result: Result<(), crate::netmap::Error>,
    },
}

/// DERP framing is consumed by the session task; the node only sees peers.
pub enum DerpEvent<'a> {
    Connected {
        region: u16,
    },
    Disconnected {
        region: u16,
    },
    Packet {
        region: u16,
        peer: NodePublic,
        payload: &'a [u8],
    },
}

#[derive(Clone, Copy)]
struct PeerPath {
    key: NodePublic,
    /// Endpoint last advertised by the control plane. This is tracked
    /// separately from `direct`, which may have been learned from authenticated
    /// traffic after NAT translation.
    advertised: Option<SocketAddr>,
    direct: Option<SocketAddr>,
    direct_healthy: bool,
    /// Try a newly advertised endpoint once before falling back to DERP. A
    /// direct-only peer keeps using its only candidate for all traffic.
    direct_probe_pending: bool,
    home_derp: u16,
}

pub struct Node<const PEERS: usize = 32> {
    identity: Identity,
    netmap: Netmap<PEERS>,
    tunnel: Device<PEERS>,
    paths: Vec<PeerPath, PEERS>,
    registration: RegistrationState,
    generation: u64,
    trusted_time_floor: u64,
    persist_in_flight: bool,
    control_disconnect_pending: bool,
    control_endpoint: Option<Endpoint>,
    registration_body: Option<SecretBuffer<MAX_CONTROL_BODY>>,
    connected_derp: Vec<u16, { crate::netmap::MAX_DERP_REGIONS }>,
    derp_connecting: Option<u16>,
    derp_disconnecting: Vec<u16, { crate::netmap::MAX_DERP_REGIONS }>,
    reconnect_at_ms: u64,
    reconnect_backoff_s: u8,
}

impl<const PEERS: usize> Node<PEERS> {
    /// Create a replacement identity only for an explicit factory reset.
    pub fn factory_reset(identity: Identity, generation: u64) -> Self {
        let generation = generation.max(1);
        let tunnel = Device::new(identity.node.to_bytes());
        Self {
            identity,
            netmap: Netmap::new(),
            tunnel,
            paths: Vec::new(),
            registration: RegistrationState::WaitingForPersistence,
            generation,
            trusted_time_floor: 0,
            persist_in_flight: false,
            control_disconnect_pending: false,
            control_endpoint: None,
            registration_body: None,
            connected_derp: Vec::new(),
            derp_connecting: None,
            derp_disconnecting: Vec::new(),
            reconnect_at_ms: 0,
            reconnect_backoff_s: 1,
        }
    }

    /// Restore an identity. Unknown or corrupt state fails closed.
    pub fn restore(state: &PersistState) -> Result<Self, NodeError> {
        let identity = state.identity();
        let mut node = Self::factory_reset(identity, state.generation);
        node.trusted_time_floor = state.trusted_time_floor;
        node.registration = RegistrationState::Disconnected;
        Ok(node)
    }

    pub fn identity(&self) -> &Identity {
        &self.identity
    }
    pub fn netmap(&self) -> &Netmap<PEERS> {
        &self.netmap
    }
    pub fn registration_state(&self) -> RegistrationState {
        self.registration
    }
    pub fn requires_persistence(&self) -> bool {
        self.registration == RegistrationState::WaitingForPersistence
    }

    pub fn set_control_endpoint(&mut self, endpoint: Endpoint) {
        self.control_endpoint = Some(endpoint);
    }

    /// Supply the next encoded registration request. It remains caller-owned
    /// until moved into a command and is zeroized after the control task drops
    /// it.
    pub fn set_registration_body(&mut self, body: &[u8]) -> Result<(), NodeError> {
        self.registration_body = Some(SecretBuffer::try_from_slice(body)?);
        Ok(())
    }

    /// Set an authenticated, nondecreasing wall-clock floor. The new floor must
    /// be atomically persisted before control traffic resumes.
    pub fn advance_trusted_time(&mut self, unix_seconds: u64) -> Result<(), NodeError> {
        if unix_seconds < self.trusted_time_floor {
            return Err(NodeError::Persistence);
        }
        if unix_seconds > self.trusted_time_floor {
            let generation = self
                .generation
                .checked_add(1)
                .ok_or(NodeError::Persistence)?;
            self.trusted_time_floor = unix_seconds;
            self.generation = generation;
            self.control_disconnect_pending = matches!(
                self.registration,
                RegistrationState::Connecting
                    | RegistrationState::Registering
                    | RegistrationState::Registered
            );
            self.registration = RegistrationState::WaitingForPersistence;
            self.persist_in_flight = false;
        }
        Ok(())
    }

    /// Poll persistence and reconnect deadlines.
    pub fn poll(&mut self, now_ms: u64) -> Result<Option<NodeAction>, NodeError> {
        if self.control_disconnect_pending {
            self.control_disconnect_pending = false;
            return Ok(Some(NodeAction::Control(ControlCommand::Disconnect)));
        }
        if self.registration == RegistrationState::WaitingForPersistence {
            if self.persist_in_flight {
                return Ok(None);
            }
            self.persist_in_flight = true;
            return Ok(Some(NodeAction::PersistState(PersistState::encode(
                &self.identity,
                self.generation,
                self.trusted_time_floor,
            ))));
        }
        if self.registration == RegistrationState::Disconnected && now_ms >= self.reconnect_at_ms {
            let endpoint = self
                .control_endpoint
                .clone()
                .ok_or(NodeError::InvalidState)?;
            self.registration = RegistrationState::Connecting;
            return Ok(Some(NodeAction::Control(ControlCommand::Connect {
                endpoint,
            })));
        }
        Ok(None)
    }

    /// A failed atomic save never unlocks control traffic.
    pub fn persistence_result(
        &mut self,
        generation: u64,
        saved: bool,
    ) -> Result<Option<NodeAction>, NodeError> {
        if generation != self.generation {
            return Err(NodeError::StaleGeneration);
        }
        if self.registration != RegistrationState::WaitingForPersistence || !self.persist_in_flight
        {
            return Err(NodeError::InvalidState);
        }
        self.persist_in_flight = false;
        if !saved {
            return Ok(Some(NodeAction::StatusChanged(
                NodeStatus::PersistenceRequired,
            )));
        }
        self.registration = RegistrationState::Disconnected;
        Ok(Some(NodeAction::StatusChanged(NodeStatus::Offline)))
    }

    pub fn control_failed(&mut self, now_ms: u64, jitter_ms: u16) {
        let seconds = self.reconnect_backoff_s.clamp(1, 60);
        self.reconnect_at_ms =
            now_ms.saturating_add(u64::from(seconds) * 1000 + u64::from(jitter_ms));
        self.reconnect_backoff_s = seconds.saturating_mul(2).min(60);
        self.control_disconnect_pending = false;
        if self.registration != RegistrationState::WaitingForPersistence {
            self.registration = RegistrationState::Disconnected;
        }
    }

    pub fn handle_control_event(
        &mut self,
        event: ControlEvent<'_, PEERS>,
    ) -> Result<Option<NodeAction>, NodeError> {
        match event {
            ControlEvent::Connected if self.registration == RegistrationState::Connecting => {
                let body = self
                    .registration_body
                    .take()
                    .ok_or(NodeError::InvalidState)?;
                self.registration = RegistrationState::Registering;
                Ok(Some(NodeAction::Control(ControlCommand::Register { body })))
            }
            ControlEvent::Registered if self.registration == RegistrationState::Registering => {
                self.registration = RegistrationState::Registered;
                self.reconnect_backoff_s = 1;
                Ok(Some(NodeAction::Control(ControlCommand::RequestMap)))
            }
            ControlEvent::Disconnected
                if self.registration == RegistrationState::WaitingForPersistence =>
            {
                self.control_disconnect_pending = false;
                Ok(Some(NodeAction::StatusChanged(
                    NodeStatus::PersistenceRequired,
                )))
            }
            ControlEvent::Disconnected => {
                self.registration = RegistrationState::Disconnected;
                Ok(Some(NodeAction::StatusChanged(NodeStatus::Offline)))
            }
            ControlEvent::MapFinished {
                lease,
                result: Ok(()),
            } if self.registration != RegistrationState::WaitingForPersistence => {
                core::mem::swap(&mut self.netmap, lease);
                self.reconcile_peers()?;
                Ok(Some(NodeAction::StatusChanged(NodeStatus::MapChanged)))
            }
            ControlEvent::MapFinished { result: Err(_), .. } => Ok(None),
            _ => Err(NodeError::InvalidState),
        }
    }

    /// Seed the one caller-owned staging lease before a streaming update.
    pub fn seed_staging(&self, lease: &mut Netmap<PEERS>) {
        lease.clone_from(&self.netmap);
    }

    fn reconcile_peers(&mut self) -> Result<(), NodeError> {
        self.tunnel.retain_peers(|key| {
            self.netmap
                .peers
                .iter()
                .any(|peer| peer.node_key.as_bytes() == key)
        });
        let mut next_paths = Vec::new();
        for peer in self.netmap.peers.iter() {
            if self
                .tunnel
                .peer_by_public_key(peer.node_key.as_bytes())
                .is_none()
            {
                let id = self.tunnel.add_peer(*peer.node_key.as_bytes(), None)?;
                self.tunnel.set_initiating(id, true)?;
            }
            let mut path = Self::path(peer);
            if let Some(previous) = self.paths.iter().find(|previous| previous.key == path.key) {
                // Preserve an authenticated, possibly NAT-translated source only
                // while the control plane continues to advertise the same path.
                // A changed or removed endpoint invalidates the old health bit.
                if previous.advertised == path.advertised {
                    path.direct = previous.direct;
                    path.direct_healthy = previous.direct_healthy;
                    path.direct_probe_pending = previous.direct_probe_pending;
                }
            }
            next_paths.push(path).map_err(|_| NodeError::Capacity)?;
        }
        self.paths = next_paths;
        Ok(())
    }

    fn path(peer: &Peer) -> PeerPath {
        let advertised = peer.direct_endpoint().map(SocketAddr::V4);
        PeerPath {
            key: peer.node_key,
            advertised,
            direct: advertised,
            direct_healthy: false,
            direct_probe_pending: advertised.is_some(),
            home_derp: peer.home_derp,
        }
    }

    fn route_packet(&mut self, id: PeerId, packet: &[u8]) -> Result<NodeAction, NodeError> {
        let key = NodePublic::from_bytes(
            self.tunnel
                .peer_public_key(id)
                .ok_or(NodeError::InvalidState)?,
        );
        let path = self
            .paths
            .iter_mut()
            .find(|p| p.key == key)
            .ok_or(NodeError::InvalidState)?;
        let handshake = matches!(
            packet.first().copied(),
            Some(tunnel::messages::TYPE_INITIATION | tunnel::messages::TYPE_RESPONSE)
        );
        let probe_direct = handshake
            && path.direct.is_some()
            && (path.direct_probe_pending || path.home_derp == 0);
        if probe_direct {
            path.direct_probe_pending = false;
        }
        if let Some(endpoint) = path
            .direct
            .filter(|_| path.direct_healthy || probe_direct || path.home_derp == 0)
        {
            return Ok(NodeAction::DirectTransmit {
                peer: key,
                endpoint,
                packet: SecretBuffer::try_from_slice(packet)?,
            });
        }
        if path.home_derp == 0 {
            return Err(NodeError::InvalidState);
        }
        Ok(NodeAction::Derp(DerpCommand::Send {
            region: path.home_derp,
            peer: key,
            packet: SecretBuffer::try_from_slice(packet)?,
        }))
    }

    fn observe_direct(&mut self, id: PeerId, source: SocketAddr) -> Result<(), NodeError> {
        let key = NodePublic::from_bytes(
            self.tunnel
                .peer_public_key(id)
                .ok_or(NodeError::InvalidState)?,
        );
        let path = self
            .paths
            .iter_mut()
            .find(|path| path.key == key)
            .ok_or(NodeError::InvalidState)?;
        path.direct = Some(source);
        path.direct_healthy = true;
        path.direct_probe_pending = false;
        Ok(())
    }

    /// Mark a formerly working direct path unhealthy so subsequent packets use
    /// the peer's home DERP, when one is advertised, until authenticated direct
    /// traffic is seen again.
    pub fn mark_direct_unhealthy(&mut self, peer: &NodePublic) -> Result<(), NodeError> {
        let path = self
            .paths
            .iter_mut()
            .find(|path| &path.key == peer)
            .ok_or(NodeError::InvalidState)?;
        path.direct_healthy = false;
        path.direct_probe_pending = false;
        Ok(())
    }

    /// Return one due DERP connection command. DNS and TCP remain the adapter's
    /// responsibility.
    pub fn poll_derp(&mut self) -> Result<Option<NodeAction>, NodeError> {
        if let Some(region) = self.connected_derp.iter().copied().find(|region| {
            !self.derp_region_required(*region) && !self.derp_disconnecting.contains(region)
        }) {
            self.derp_disconnecting
                .push(region)
                .map_err(|_| NodeError::Capacity)?;
            return Ok(Some(NodeAction::Derp(DerpCommand::Disconnect { region })));
        }
        if let Some(region) = self
            .derp_connecting
            .filter(|region| !self.derp_region_required(*region))
        {
            self.derp_connecting = None;
            if !self.derp_disconnecting.contains(&region) {
                self.derp_disconnecting
                    .push(region)
                    .map_err(|_| NodeError::Capacity)?;
            }
            return Ok(Some(NodeAction::Derp(DerpCommand::Disconnect { region })));
        }
        if !self.derp_disconnecting.is_empty() {
            return Ok(None);
        }
        let Some(region) = self.paths.iter().map(|path| path.home_derp).find(|region| {
            *region != 0
                && !self.connected_derp.contains(region)
                && self.derp_connecting != Some(*region)
        }) else {
            return Ok(None);
        };
        let relay = self
            .netmap
            .derp
            .region(region)
            .and_then(|region| region.nodes.first())
            .ok_or(NodeError::InvalidState)?;
        let endpoint = Endpoint::new(relay.host_name.as_str(), relay.port)?;
        self.derp_connecting = Some(region);
        Ok(Some(NodeAction::Derp(DerpCommand::Connect {
            region,
            endpoint,
        })))
    }

    fn derp_region_required(&self, region: u16) -> bool {
        region != 0 && self.paths.iter().any(|path| path.home_derp == region)
    }

    pub fn handle_derp_event(
        &mut self,
        event: DerpEvent<'_>,
        now_ms: u64,
        rng: &mut impl tunnel::Rng,
    ) -> Result<Option<NodeAction>, NodeError> {
        match event {
            DerpEvent::Connected { region } => {
                if self.derp_connecting == Some(region) {
                    self.derp_connecting = None;
                }
                if self.derp_disconnecting.contains(&region) || !self.derp_region_required(region) {
                    if !self.derp_disconnecting.contains(&region) {
                        self.derp_disconnecting
                            .push(region)
                            .map_err(|_| NodeError::Capacity)?;
                    }
                    return Ok(Some(NodeAction::Derp(DerpCommand::Disconnect { region })));
                }
                if !self.connected_derp.contains(&region) {
                    self.connected_derp
                        .push(region)
                        .map_err(|_| NodeError::Capacity)?;
                }
                Ok(None)
            }
            DerpEvent::Disconnected { region } => {
                if let Some(index) = self.connected_derp.iter().position(|item| *item == region) {
                    self.connected_derp.swap_remove(index);
                }
                if self.derp_connecting == Some(region) {
                    self.derp_connecting = None;
                }
                if let Some(index) = self
                    .derp_disconnecting
                    .iter()
                    .position(|item| *item == region)
                {
                    self.derp_disconnecting.swap_remove(index);
                }
                Ok(None)
            }
            DerpEvent::Packet {
                region,
                peer,
                payload,
            } => {
                let expected = self
                    .tunnel
                    .peer_by_public_key(peer.as_bytes())
                    .ok_or(NodeError::InvalidState)?;
                let mut out = [0; MAX_ACTION_PACKET];
                match self.tunnel.handle_udp_from_peer(
                    expected,
                    payload,
                    Instant(now_ms),
                    rng,
                    &mut out,
                )? {
                    TunnelAction::None => Ok(None),
                    TunnelAction::Authenticated { .. } => Ok(None),
                    TunnelAction::Send { data, .. } => {
                        Ok(Some(NodeAction::Derp(DerpCommand::Send {
                            region,
                            peer,
                            packet: SecretBuffer::try_from_slice(data)?,
                        })))
                    }
                    TunnelAction::Receive { packet, .. } => Ok(Some(NodeAction::DeliveredIpv4 {
                        peer,
                        packet: SecretBuffer::try_from_slice(packet)?,
                    })),
                }
            }
        }
    }

    pub fn handle_direct(
        &mut self,
        datagram: &[u8],
        now_ms: u64,
        rng: &mut impl tunnel::Rng,
    ) -> Result<Option<NodeAction>, NodeError> {
        let mut out = [0; MAX_ACTION_PACKET];
        match self
            .tunnel
            .handle_udp(datagram, Instant(now_ms), rng, &mut out)?
        {
            TunnelAction::None => Ok(None),
            TunnelAction::Authenticated { .. } => Ok(None),
            TunnelAction::Send { peer, data } => self.route_packet(peer, data).map(Some),
            TunnelAction::Receive { peer, packet } => {
                let key = NodePublic::from_bytes(
                    self.tunnel
                        .peer_public_key(peer)
                        .ok_or(NodeError::InvalidState)?,
                );
                Ok(Some(NodeAction::DeliveredIpv4 {
                    peer: key,
                    packet: SecretBuffer::try_from_slice(packet)?,
                }))
            }
        }
    }

    /// Process a direct datagram and mark its authenticated peer's source as a
    /// healthy direct path before routing any response.
    pub fn handle_direct_from(
        &mut self,
        datagram: &[u8],
        source: SocketAddr,
        now_ms: u64,
        rng: &mut impl tunnel::Rng,
    ) -> Result<Option<NodeAction>, NodeError> {
        let mut out = [0; MAX_ACTION_PACKET];
        match self
            .tunnel
            .handle_udp(datagram, Instant(now_ms), rng, &mut out)?
        {
            TunnelAction::None => Ok(None),
            TunnelAction::Authenticated { peer } => {
                self.observe_direct(peer, source)?;
                Ok(None)
            }
            TunnelAction::Send { peer, data } => {
                self.observe_direct(peer, source)?;
                self.route_packet(peer, data).map(Some)
            }
            TunnelAction::Receive { peer, packet } => {
                self.observe_direct(peer, source)?;
                let key = NodePublic::from_bytes(
                    self.tunnel
                        .peer_public_key(peer)
                        .ok_or(NodeError::InvalidState)?,
                );
                Ok(Some(NodeAction::DeliveredIpv4 {
                    peer: key,
                    packet: SecretBuffer::try_from_slice(packet)?,
                }))
            }
        }
    }

    /// Encrypt an inner IPv4 packet for a configured peer.
    pub fn encapsulate_ipv4(
        &mut self,
        peer: &NodePublic,
        packet: &[u8],
        now_ms: u64,
    ) -> Result<Option<NodeAction>, NodeError> {
        let id = self
            .tunnel
            .peer_by_public_key(peer.as_bytes())
            .ok_or(NodeError::InvalidState)?;
        let mut out = [0; MAX_ACTION_PACKET];
        match self
            .tunnel
            .encapsulate(id, packet, Instant(now_ms), &mut out)?
        {
            TunnelAction::None => Ok(None),
            TunnelAction::Authenticated { .. } => Err(NodeError::InvalidState),
            TunnelAction::Send { peer, data } => self.route_packet(peer, data).map(Some),
            TunnelAction::Receive { .. } => Err(NodeError::InvalidState),
        }
    }

    /// Poll WireGuard keepalive and session-expiry timers.
    pub fn poll_tunnel_timers(&mut self, now_ms: u64) -> Result<Option<NodeAction>, NodeError> {
        let mut out = [0; MAX_ACTION_PACKET];
        match self.tunnel.poll_timers(Instant(now_ms), &mut out) {
            TunnelAction::None => Ok(None),
            TunnelAction::Authenticated { .. } => Err(NodeError::InvalidState),
            TunnelAction::Send { peer, data } => self.route_packet(peer, data).map(Some),
            TunnelAction::Receive { .. } => Err(NodeError::InvalidState),
        }
    }

    pub fn poll_tunnel(
        &mut self,
        now_ms: u64,
        tai64n: &Tai64n,
        rng: &mut impl tunnel::Rng,
    ) -> Result<Option<NodeAction>, NodeError> {
        let mut out = [0; MAX_ACTION_PACKET];
        match self
            .tunnel
            .poll_handshakes(Instant(now_ms), tai64n, rng, &mut out)
        {
            TunnelAction::None => Ok(None),
            TunnelAction::Authenticated { .. } => Err(NodeError::InvalidState),
            TunnelAction::Send { peer, data } => self.route_packet(peer, data).map(Some),
            TunnelAction::Receive { .. } => Err(NodeError::InvalidState),
        }
    }
}

/// Static-dispatch adapter for control and DERP connections.
#[allow(async_fn_in_trait)]
pub trait Transport {
    type Error;
    type Stream<'a>: embedded_io_async::Read + embedded_io_async::Write
    where
        Self: 'a;

    async fn connect<'a>(
        &'a mut self,
        endpoint: &Endpoint,
    ) -> Result<Self::Stream<'a>, Self::Error>;
}

/// A session owns its stream and has no reference to a [`Node`].
pub struct ControlSession<S> {
    stream: S,
}

impl<S> ControlSession<S> {
    pub async fn connect<'a, T: Transport>(
        transport: &'a mut T,
        endpoint: &Endpoint,
    ) -> Result<ControlSession<T::Stream<'a>>, T::Error> {
        Ok(ControlSession {
            stream: transport.connect(endpoint).await?,
        })
    }
    pub fn stream(&mut self) -> &mut S {
        &mut self.stream
    }
}

/// A DERP session has the same ownership boundary as a control session.
pub struct DerpSession<S> {
    stream: S,
}

impl<S> DerpSession<S> {
    pub async fn connect<'a, T: Transport>(
        transport: &'a mut T,
        endpoint: &Endpoint,
    ) -> Result<DerpSession<T::Stream<'a>>, T::Error> {
        Ok(DerpSession {
            stream: transport.connect(endpoint).await?,
        })
    }
    pub fn stream(&mut self) -> &mut S {
        &mut self.stream
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    struct Counter(u8);
    impl crate::keys::Rng for Counter {
        fn fill_bytes(&mut self, out: &mut [u8]) {
            for byte in out {
                *byte = self.0;
                self.0 = self.0.wrapping_add(1);
            }
        }
    }
    impl tunnel::Rng for Counter {
        fn fill_bytes(&mut self, out: &mut [u8]) {
            crate::keys::Rng::fill_bytes(self, out);
        }
    }

    #[test]
    fn persistence_must_be_acknowledged_before_connecting() {
        let mut node = Node::<4>::factory_reset(Identity::generate(&mut Counter(1)), 1);
        node.set_control_endpoint(Endpoint::new("control.example", 443).unwrap());
        let state = match node.poll(0).unwrap().unwrap() {
            NodeAction::PersistState(state) => state,
            _ => panic!("persistence must be first"),
        };
        assert!(node.poll(0).unwrap().is_none());
        node.persistence_result(state.generation(), true).unwrap();
        assert!(matches!(
            node.poll(0).unwrap(),
            Some(NodeAction::Control(ControlCommand::Connect { .. }))
        ));
    }

    #[test]
    fn control_failures_cannot_bypass_required_persistence() {
        let mut node = Node::<4>::factory_reset(Identity::generate(&mut Counter(1)), 1);
        node.set_control_endpoint(Endpoint::new("control.example", 443).unwrap());
        let state = match node.poll(0).unwrap().unwrap() {
            NodeAction::PersistState(state) => state,
            _ => unreachable!(),
        };

        node.control_failed(100, 7);
        assert!(node.requires_persistence());
        assert!(matches!(
            node.handle_control_event(ControlEvent::Disconnected)
                .unwrap(),
            Some(NodeAction::StatusChanged(NodeStatus::PersistenceRequired))
        ));
        assert!(node.requires_persistence());
        assert!(node.poll(2_000).unwrap().is_none());

        node.persistence_result(state.generation(), true).unwrap();
        assert!(matches!(
            node.poll(2_000).unwrap(),
            Some(NodeAction::Control(ControlCommand::Connect { .. }))
        ));
    }

    #[test]
    fn corrupt_state_fails_closed() {
        assert!(PersistState::from_bytes(&[0; PersistState::BLOB_LEN]).is_err());

        let identity = Identity::generate(&mut Counter(9));
        let state = PersistState::encode(&identity, 1, 0);
        let mut corrupt = [0; PersistState::BLOB_LEN];
        corrupt.copy_from_slice(state.as_bytes());
        corrupt[42] ^= 1;
        assert!(PersistState::from_bytes(&corrupt).is_err());
    }

    #[test]
    fn default_node_fits_device_budget() {
        assert!(core::mem::size_of::<Node<32>>() <= 64 * 1024);
        assert!(core::mem::size_of::<Netmap<32>>() <= 32 * 1024);
    }

    #[test]
    fn registration_and_map_lease_lifecycle_is_explicit() {
        let mut node = Node::<2>::factory_reset(Identity::generate(&mut Counter(1)), 1);
        node.set_control_endpoint(Endpoint::new("control.example", 443).unwrap());
        node.set_registration_body(b"synthetic registration")
            .unwrap();

        let generation = match node.poll(0).unwrap().unwrap() {
            NodeAction::PersistState(state) => state.generation(),
            _ => unreachable!(),
        };
        node.persistence_result(generation, true).unwrap();
        assert!(matches!(
            node.poll(0).unwrap(),
            Some(NodeAction::Control(ControlCommand::Connect { .. }))
        ));
        let register = node
            .handle_control_event(ControlEvent::Connected)
            .unwrap()
            .unwrap();
        match register {
            NodeAction::Control(ControlCommand::Register { body }) => {
                assert_eq!(body.as_slice(), b"synthetic registration");
            }
            _ => unreachable!(),
        }
        assert!(matches!(
            node.handle_control_event(ControlEvent::Registered).unwrap(),
            Some(NodeAction::Control(ControlCommand::RequestMap))
        ));

        let mut lease = Netmap::<2>::new();
        lease
            .peers
            .upsert(Peer::new(7, NodePrivate::from_bytes([77; 32]).public()))
            .unwrap();
        node.handle_control_event(ControlEvent::MapFinished {
            lease: &mut lease,
            result: Err(crate::netmap::Error::Json),
        })
        .unwrap();
        assert!(node.netmap().peers.is_empty());
        assert_eq!(lease.peers.len(), 1);

        node.handle_control_event(ControlEvent::MapFinished {
            lease: &mut lease,
            result: Ok(()),
        })
        .unwrap();
        assert_eq!(node.netmap().peers.len(), 1);
        assert!(
            lease.peers.is_empty(),
            "the same lease now holds the old map"
        );
    }

    #[test]
    fn trusted_time_disconnects_and_blocks_maps_until_persisted() {
        let mut node = Node::<2>::factory_reset(Identity::generate(&mut Counter(1)), 1);
        node.set_control_endpoint(Endpoint::new("control.example", 443).unwrap());
        node.set_registration_body(b"{}").unwrap();
        let generation = match node.poll(0).unwrap().unwrap() {
            NodeAction::PersistState(state) => state.generation(),
            _ => unreachable!(),
        };
        node.persistence_result(generation, true).unwrap();
        node.poll(0).unwrap();
        node.handle_control_event(ControlEvent::Connected).unwrap();
        node.handle_control_event(ControlEvent::Registered).unwrap();

        node.advance_trusted_time(1_000).unwrap();
        let mut lease = Netmap::new();
        assert_eq!(
            node.handle_control_event(ControlEvent::MapFinished {
                lease: &mut lease,
                result: Ok(()),
            })
            .unwrap_err(),
            NodeError::InvalidState
        );
        assert!(matches!(
            node.poll(0).unwrap(),
            Some(NodeAction::Control(ControlCommand::Disconnect))
        ));
        assert!(matches!(
            node.poll(0).unwrap(),
            Some(NodeAction::PersistState(_))
        ));
    }

    #[test]
    fn a_new_direct_endpoint_is_probed_before_derp_fallback() {
        let mut node = Node::<2>::factory_reset(Identity::generate(&mut Counter(1)), 1);
        let generation = match node.poll(0).unwrap().unwrap() {
            NodeAction::PersistState(state) => state.generation(),
            _ => unreachable!(),
        };
        node.persistence_result(generation, true).unwrap();

        let endpoint: SocketAddr = "192.0.2.10:41641".parse().unwrap();
        let mut lease = Netmap::<2>::new();
        let mut peer = Peer::new(7, NodePrivate::from_bytes([77; 32]).public());
        peer.home_derp = 9;
        peer.endpoints.push(endpoint).unwrap();
        lease.peers.upsert(peer).unwrap();
        node.handle_control_event(ControlEvent::MapFinished {
            lease: &mut lease,
            result: Ok(()),
        })
        .unwrap();

        assert!(matches!(
            node.poll_tunnel(0, &[0; 12], &mut Counter(1)).unwrap(),
            Some(NodeAction::DirectTransmit { endpoint: sent, .. }) if sent == endpoint
        ));
        assert!(matches!(
            node.poll_tunnel(tunnel::timers::REKEY_TIMEOUT_MS, &[1; 12], &mut Counter(2),)
                .unwrap(),
            Some(NodeAction::Derp(DerpCommand::Send { region: 9, .. }))
        ));
    }

    #[test]
    fn a_direct_only_peer_keeps_retrying_its_advertised_endpoint() {
        let mut node = Node::<2>::factory_reset(Identity::generate(&mut Counter(1)), 1);
        let generation = match node.poll(0).unwrap().unwrap() {
            NodeAction::PersistState(state) => state.generation(),
            _ => unreachable!(),
        };
        node.persistence_result(generation, true).unwrap();

        let endpoint: SocketAddr = "192.0.2.10:41641".parse().unwrap();
        let mut lease = Netmap::<2>::new();
        let mut peer = Peer::new(7, NodePrivate::from_bytes([77; 32]).public());
        peer.endpoints.push(endpoint).unwrap();
        lease.peers.upsert(peer).unwrap();
        node.handle_control_event(ControlEvent::MapFinished {
            lease: &mut lease,
            result: Ok(()),
        })
        .unwrap();

        for (now, timestamp) in [(0, [0; 12]), (tunnel::timers::REKEY_TIMEOUT_MS, [1; 12])] {
            assert!(matches!(
                node.poll_tunnel(now, &timestamp, &mut Counter(1)).unwrap(),
                Some(NodeAction::DirectTransmit { endpoint: sent, .. }) if sent == endpoint
            ));
        }
    }

    #[test]
    fn an_unhealthy_direct_only_peer_keeps_sending_data_directly() {
        let mut node = Node::<2>::factory_reset(Identity::generate(&mut Counter(1)), 1);
        let generation = match node.poll(0).unwrap().unwrap() {
            NodeAction::PersistState(state) => state.generation(),
            _ => unreachable!(),
        };
        node.persistence_result(generation, true).unwrap();

        let key = NodePrivate::from_bytes([77; 32]).public();
        let endpoint: SocketAddr = "192.0.2.10:41641".parse().unwrap();
        let mut lease = Netmap::<2>::new();
        let mut peer = Peer::new(7, key);
        peer.endpoints.push(endpoint).unwrap();
        lease.peers.upsert(peer).unwrap();
        node.handle_control_event(ControlEvent::MapFinished {
            lease: &mut lease,
            result: Ok(()),
        })
        .unwrap();

        node.paths[0].direct_healthy = true;
        node.mark_direct_unhealthy(&key).unwrap();
        let id = node.tunnel.peer_by_public_key(key.as_bytes()).unwrap();

        assert!(matches!(
            node.route_packet(id, &[4]).unwrap(),
            NodeAction::DirectTransmit { endpoint: sent, .. } if sent == endpoint
        ));
    }

    #[test]
    fn a_changed_advertised_endpoint_invalidates_the_old_direct_path() {
        let mut node = Node::<2>::factory_reset(Identity::generate(&mut Counter(1)), 1);
        let generation = match node.poll(0).unwrap().unwrap() {
            NodeAction::PersistState(state) => state.generation(),
            _ => unreachable!(),
        };
        node.persistence_result(generation, true).unwrap();

        let key = NodePrivate::from_bytes([77; 32]).public();
        let first: SocketAddr = "192.0.2.10:41641".parse().unwrap();
        let learned: SocketAddr = "198.51.100.10:50123".parse().unwrap();
        let replacement: SocketAddr = "192.0.2.11:41641".parse().unwrap();
        let mut lease = Netmap::<2>::new();
        let mut peer = Peer::new(7, key);
        peer.endpoints.push(first).unwrap();
        lease.peers.upsert(peer).unwrap();
        node.handle_control_event(ControlEvent::MapFinished {
            lease: &mut lease,
            result: Ok(()),
        })
        .unwrap();

        let id = node.tunnel.peer_by_public_key(key.as_bytes()).unwrap();
        node.observe_direct(id, learned).unwrap();
        assert!(node.paths[0].direct_healthy);

        node.seed_staging(&mut lease);
        let peer = lease.peers.get_mut(7).unwrap();
        peer.endpoints.clear();
        peer.endpoints.push(replacement).unwrap();
        node.handle_control_event(ControlEvent::MapFinished {
            lease: &mut lease,
            result: Ok(()),
        })
        .unwrap();

        assert_eq!(node.paths[0].advertised, Some(replacement));
        assert_eq!(node.paths[0].direct, Some(replacement));
        assert!(!node.paths[0].direct_healthy);
    }

    #[test]
    fn obsolete_derp_connections_are_closed_before_new_regions_connect() {
        fn region(id: u16) -> crate::netmap::DerpRegion {
            let mut nodes = heapless::Vec::new();
            nodes
                .push(crate::netmap::DerpNode {
                    host_name: heapless::String::try_from("derp.example").unwrap(),
                    port: 443,
                    stun_port: 3478,
                })
                .unwrap();
            crate::netmap::DerpRegion {
                id,
                code: heapless::String::try_from("test").unwrap(),
                nodes,
            }
        }

        let mut node = Node::<16>::factory_reset(Identity::generate(&mut Counter(1)), 1);
        let generation = match node.poll(0).unwrap().unwrap() {
            NodeAction::PersistState(state) => state.generation(),
            _ => unreachable!(),
        };
        node.persistence_result(generation, true).unwrap();

        let mut initial = Netmap::<16>::new();
        for region_id in 1..=crate::netmap::MAX_DERP_REGIONS as u16 {
            let mut peer = Peer::new(
                u64::from(region_id),
                NodePrivate::from_bytes([region_id as u8; 32]).public(),
            );
            peer.home_derp = region_id;
            initial.peers.upsert(peer).unwrap();
            initial.derp.insert(region(region_id)).unwrap();
        }
        node.handle_control_event(ControlEvent::MapFinished {
            lease: &mut initial,
            result: Ok(()),
        })
        .unwrap();
        for region_id in 1..=crate::netmap::MAX_DERP_REGIONS as u16 {
            node.handle_derp_event(
                DerpEvent::Connected { region: region_id },
                0,
                &mut Counter(1),
            )
            .unwrap();
        }
        assert_eq!(node.connected_derp.len(), crate::netmap::MAX_DERP_REGIONS);

        let replacement_id = 999;
        let mut replacement = Netmap::<16>::new();
        let mut peer = Peer::new(999, NodePrivate::from_bytes([99; 32]).public());
        peer.home_derp = replacement_id;
        replacement.peers.upsert(peer).unwrap();
        replacement.derp.insert(region(replacement_id)).unwrap();
        node.handle_control_event(ControlEvent::MapFinished {
            lease: &mut replacement,
            result: Ok(()),
        })
        .unwrap();

        let mut disconnected = heapless::Vec::<u16, { crate::netmap::MAX_DERP_REGIONS }>::new();
        for _ in 0..crate::netmap::MAX_DERP_REGIONS {
            let action = node.poll_derp().unwrap().unwrap();
            let NodeAction::Derp(DerpCommand::Disconnect { region }) = action else {
                panic!("obsolete regions must disconnect before a replacement connects");
            };
            disconnected.push(region).unwrap();
            node.handle_derp_event(DerpEvent::Disconnected { region }, 0, &mut Counter(1))
                .unwrap();
        }
        for region_id in 1..=crate::netmap::MAX_DERP_REGIONS as u16 {
            assert!(disconnected.contains(&region_id));
        }

        assert!(matches!(
            node.poll_derp().unwrap(),
            Some(NodeAction::Derp(DerpCommand::Connect {
                region,
                ..
            })) if region == replacement_id
        ));
    }
}