rns-net 0.7.1

Network interfaces and node driver for the Reticulum Network Stack
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
//! Identity, known destinations, and received ratchet persistence.
//!
//! Identity file format: 64 bytes = 32-byte X25519 private key + 32-byte Ed25519 private key.
//! Same as Python's `Identity.to_file()` / `Identity.from_file()`.
//!
//! Known destinations: msgpack binary with 16-byte keys and tuple values.

use std::collections::{HashMap, HashSet};
use std::fs;
use std::io;
use std::path::{Path, PathBuf};
use std::sync::Mutex;
use std::time::{SystemTime, UNIX_EPOCH};

use rns_crypto::identity::Identity;
use rns_crypto::OsRng;

/// Paths for storage directories.
#[derive(Debug, Clone)]
pub struct StoragePaths {
    pub config_dir: PathBuf,
    pub storage: PathBuf,
    pub cache: PathBuf,
    pub identities: PathBuf,
    pub ratchets: PathBuf,
    /// Directory for discovered interface data: storage/discovery/interfaces
    pub discovered_interfaces: PathBuf,
}

/// A known destination entry.
#[derive(Debug, Clone)]
pub struct KnownDestination {
    pub identity_hash: [u8; 16],
    pub public_key: [u8; 64],
    pub app_data: Option<Vec<u8>>,
    pub hops: u8,
    pub received_at: f64,
    pub receiving_interface: u64,
    pub was_used: bool,
    pub last_used_at: Option<f64>,
    pub retained: bool,
}

#[derive(Debug, Clone, Copy, PartialEq)]
pub struct RatchetEntry {
    pub ratchet: [u8; 32],
    pub received_at: f64,
}

#[derive(Debug, Clone, Copy, Default, PartialEq, Eq)]
pub struct RatchetCleanupStats {
    pub processed: usize,
    pub not_known: usize,
    pub removed: usize,
}

pub trait RatchetStore: Send + Sync {
    fn remember(&self, dest_hash: [u8; 16], entry: RatchetEntry) -> io::Result<()>;
    fn current(
        &self,
        dest_hash: &[u8; 16],
        now: f64,
        expiry_secs: f64,
    ) -> io::Result<Option<RatchetEntry>>;
    fn cleanup(
        &self,
        known_destinations: &HashSet<[u8; 16]>,
        now: f64,
        expiry_secs: f64,
    ) -> io::Result<RatchetCleanupStats>;
}

#[derive(Debug)]
pub struct FsRatchetStore {
    dir: PathBuf,
    cache: Mutex<HashMap<[u8; 16], RatchetEntry>>,
}

impl FsRatchetStore {
    pub fn new(dir: PathBuf) -> Self {
        Self {
            dir,
            cache: Mutex::new(HashMap::new()),
        }
    }

    fn path_for(&self, dest_hash: &[u8; 16]) -> PathBuf {
        self.dir.join(hex_lower(dest_hash))
    }

    fn read_entry(path: &Path) -> io::Result<RatchetEntry> {
        use rns_core::msgpack;

        let data = fs::read(path)?;
        let (value, _) = msgpack::unpack(&data).map_err(|e| {
            io::Error::new(io::ErrorKind::InvalidData, format!("msgpack error: {}", e))
        })?;
        let ratchet = value
            .map_get("ratchet")
            .and_then(|v| v.as_bin())
            .ok_or_else(|| io::Error::new(io::ErrorKind::InvalidData, "missing ratchet"))?;
        if ratchet.len() != 32 {
            return Err(io::Error::new(
                io::ErrorKind::InvalidData,
                format!("ratchet must be 32 bytes, got {}", ratchet.len()),
            ));
        }
        let mut ratchet_bytes = [0u8; 32];
        ratchet_bytes.copy_from_slice(ratchet);
        let received_at = value
            .map_get("received")
            .and_then(|v| v.as_number())
            .ok_or_else(|| io::Error::new(io::ErrorKind::InvalidData, "missing received"))?;

        Ok(RatchetEntry {
            ratchet: ratchet_bytes,
            received_at,
        })
    }

    fn write_entry(&self, path: &Path, entry: RatchetEntry) -> io::Result<()> {
        use rns_core::msgpack::{self, Value};

        fs::create_dir_all(&self.dir)?;
        let value = Value::Map(vec![
            (
                Value::Str("ratchet".into()),
                Value::Bin(entry.ratchet.to_vec()),
            ),
            (
                Value::Str("received".into()),
                Value::Float(entry.received_at),
            ),
        ]);
        let packed = msgpack::pack(&value);
        let tmp = path.with_extension("out");
        fs::write(&tmp, packed)?;
        fs::rename(tmp, path)
    }
}

impl RatchetStore for FsRatchetStore {
    fn remember(&self, dest_hash: [u8; 16], entry: RatchetEntry) -> io::Result<()> {
        if self
            .cache
            .lock()
            .map(|cache| cache.get(&dest_hash).copied() == Some(entry))
            .unwrap_or(false)
        {
            return Ok(());
        }

        let path = self.path_for(&dest_hash);
        self.write_entry(&path, entry)?;
        if let Ok(mut cache) = self.cache.lock() {
            cache.insert(dest_hash, entry);
        }
        Ok(())
    }

    fn current(
        &self,
        dest_hash: &[u8; 16],
        now: f64,
        expiry_secs: f64,
    ) -> io::Result<Option<RatchetEntry>> {
        if let Ok(cache) = self.cache.lock() {
            if let Some(entry) = cache.get(dest_hash).copied() {
                if now <= entry.received_at + expiry_secs {
                    return Ok(Some(entry));
                }
            }
        }

        let path = self.path_for(dest_hash);
        if !path.is_file() {
            return Ok(None);
        }

        let entry = Self::read_entry(&path)?;
        if now > entry.received_at + expiry_secs {
            let _ = fs::remove_file(path);
            if let Ok(mut cache) = self.cache.lock() {
                cache.remove(dest_hash);
            }
            return Ok(None);
        }

        if let Ok(mut cache) = self.cache.lock() {
            cache.insert(*dest_hash, entry);
        }
        Ok(Some(entry))
    }

    fn cleanup(
        &self,
        known_destinations: &HashSet<[u8; 16]>,
        now: f64,
        expiry_secs: f64,
    ) -> io::Result<RatchetCleanupStats> {
        let mut stats = RatchetCleanupStats::default();
        if !self.dir.is_dir() {
            return Ok(stats);
        }

        for entry in fs::read_dir(&self.dir)? {
            let entry = entry?;
            if !entry.file_type()?.is_file() {
                continue;
            }
            stats.processed += 1;
            let path = entry.path();
            let Some(filename) = path.file_name().and_then(|name| name.to_str()) else {
                let _ = fs::remove_file(&path);
                stats.removed += 1;
                continue;
            };

            let Some(dest_hash) = parse_dest_hash_hex(filename) else {
                let _ = fs::remove_file(&path);
                stats.removed += 1;
                continue;
            };

            let unknown = !known_destinations.contains(&dest_hash);
            if unknown {
                stats.not_known += 1;
            }

            let expired_or_corrupt = match Self::read_entry(&path) {
                Ok(entry) => now > entry.received_at + expiry_secs,
                Err(_) => true,
            };

            if unknown || expired_or_corrupt {
                let _ = fs::remove_file(&path);
                stats.removed += 1;
                if let Ok(mut cache) = self.cache.lock() {
                    cache.remove(&dest_hash);
                }
            }
        }

        Ok(stats)
    }
}

/// Ensure all storage directories exist. Creates them if missing.
pub fn ensure_storage_dirs(config_dir: &Path) -> io::Result<StoragePaths> {
    let storage = config_dir.join("storage");
    let cache = config_dir.join("cache");
    let identities = storage.join("identities");
    let ratchets = storage.join("ratchets");
    let announces = cache.join("announces");
    let discovered_interfaces = storage.join("discovery").join("interfaces");

    fs::create_dir_all(&storage)?;
    fs::create_dir_all(&cache)?;
    fs::create_dir_all(&identities)?;
    fs::create_dir_all(&ratchets)?;
    fs::create_dir_all(&announces)?;
    fs::create_dir_all(&discovered_interfaces)?;

    Ok(StoragePaths {
        config_dir: config_dir.to_path_buf(),
        storage,
        cache,
        identities,
        ratchets,
        discovered_interfaces,
    })
}

fn hex_lower(bytes: &[u8]) -> String {
    let mut out = String::with_capacity(bytes.len() * 2);
    for byte in bytes {
        use std::fmt::Write;
        let _ = write!(&mut out, "{:02x}", byte);
    }
    out
}

fn parse_dest_hash_hex(s: &str) -> Option<[u8; 16]> {
    if s.len() != 32 {
        return None;
    }
    let mut out = [0u8; 16];
    for i in 0..16 {
        out[i] = u8::from_str_radix(&s[i * 2..i * 2 + 2], 16).ok()?;
    }
    Some(out)
}

/// Save an identity's private key to a file (64 bytes).
pub fn save_identity(identity: &Identity, path: &Path) -> io::Result<()> {
    let private_key = identity
        .get_private_key()
        .ok_or_else(|| io::Error::new(io::ErrorKind::InvalidData, "Identity has no private key"))?;
    fs::write(path, private_key)
}

/// Load an identity from a private key file (64 bytes).
pub fn load_identity(path: &Path) -> io::Result<Identity> {
    let data = fs::read(path)?;
    if data.len() != 64 {
        return Err(io::Error::new(
            io::ErrorKind::InvalidData,
            format!("Identity file must be 64 bytes, got {}", data.len()),
        ));
    }
    let mut key = [0u8; 64];
    key.copy_from_slice(&data);
    Ok(Identity::from_private_key(&key))
}

/// Save known destinations to a msgpack file.
///
/// Format: `{bytes(16): [received_at, public_key, app_data, identity_hash, hops,
/// receiving_interface, was_used, last_used_at, retained], ...}`
///
/// Legacy 4-element arrays are still accepted on load.
pub fn save_known_destinations(
    destinations: &HashMap<[u8; 16], KnownDestination>,
    path: &Path,
) -> io::Result<()> {
    use rns_core::msgpack::{self, Value};

    let entries: Vec<(Value, Value)> = destinations
        .iter()
        .map(|(hash, dest)| {
            let key = Value::Bin(hash.to_vec());
            let app_data = match &dest.app_data {
                Some(d) => Value::Bin(d.clone()),
                None => Value::Nil,
            };
            let value = Value::Array(vec![
                Value::UInt(dest.received_at as u64),
                Value::Bin(dest.public_key.to_vec()),
                app_data,
                Value::Bin(dest.identity_hash.to_vec()),
                Value::UInt(dest.hops as u64),
                Value::UInt(dest.receiving_interface),
                Value::Bool(dest.was_used),
                match dest.last_used_at {
                    Some(last_used_at) => Value::UInt(last_used_at as u64),
                    None => Value::Nil,
                },
                Value::Bool(dest.retained),
            ]);
            (key, value)
        })
        .collect();

    let packed = msgpack::pack(&Value::Map(entries));
    atomic_write(path, &packed)
}

fn atomic_write(path: &Path, data: &[u8]) -> io::Result<()> {
    let parent = path.parent().unwrap_or_else(|| Path::new("."));
    let file_name = path
        .file_name()
        .and_then(|name| name.to_str())
        .unwrap_or("known_destinations");
    let nonce = SystemTime::now()
        .duration_since(UNIX_EPOCH)
        .map(|duration| duration.as_nanos())
        .unwrap_or_default();
    let temp_path = parent.join(format!(".{file_name}.tmp.{}.{}", std::process::id(), nonce));
    match fs::write(&temp_path, data).and_then(|_| replace_file(&temp_path, path)) {
        Ok(()) => Ok(()),
        Err(err) => {
            let _ = fs::remove_file(&temp_path);
            Err(err)
        }
    }
}

fn replace_file(temp_path: &Path, path: &Path) -> io::Result<()> {
    #[cfg(windows)]
    if path.exists() {
        fs::remove_file(path)?;
    }
    fs::rename(temp_path, path)
}

/// Load known destinations from a msgpack file.
pub fn load_known_destinations(path: &Path) -> io::Result<HashMap<[u8; 16], KnownDestination>> {
    use rns_core::msgpack;

    let data = fs::read(path)?;
    if data.is_empty() {
        return Ok(HashMap::new());
    }

    let (value, _) = msgpack::unpack(&data)
        .map_err(|e| io::Error::new(io::ErrorKind::InvalidData, format!("msgpack error: {}", e)))?;

    let map = value
        .as_map()
        .ok_or_else(|| io::Error::new(io::ErrorKind::InvalidData, "Expected msgpack map"))?;

    let mut result = HashMap::new();

    for (k, v) in map {
        let hash_bytes = k
            .as_bin()
            .ok_or_else(|| io::Error::new(io::ErrorKind::InvalidData, "Expected bin key"))?;

        if hash_bytes.len() != 16 {
            continue; // Skip invalid entries like Python does
        }

        let mut dest_hash = [0u8; 16];
        dest_hash.copy_from_slice(hash_bytes);

        let arr = v
            .as_array()
            .ok_or_else(|| io::Error::new(io::ErrorKind::InvalidData, "Expected array value"))?;

        if arr.len() < 3 {
            continue;
        }

        let received_at = arr[0].as_uint().unwrap_or(0) as f64;

        let pub_key_bytes = arr[1]
            .as_bin()
            .ok_or_else(|| io::Error::new(io::ErrorKind::InvalidData, "Expected bin public_key"))?;
        if pub_key_bytes.len() != 64 {
            continue;
        }
        let mut public_key = [0u8; 64];
        public_key.copy_from_slice(pub_key_bytes);

        let app_data = if arr.len() > 2 {
            arr[2].as_bin().map(|b| b.to_vec())
        } else {
            None
        };

        let identity_hash = if arr.len() > 3 {
            let hash_bytes = arr[3]
                .as_bin()
                .filter(|bytes| bytes.len() == 16)
                .map(|bytes| {
                    let mut hash = [0u8; 16];
                    hash.copy_from_slice(bytes);
                    hash
                });
            hash_bytes.unwrap_or_else(|| {
                let identity = Identity::from_public_key(&public_key);
                *identity.hash()
            })
        } else {
            let identity = Identity::from_public_key(&public_key);
            *identity.hash()
        };
        let hops = arr.get(4).and_then(|value| value.as_uint()).unwrap_or(0) as u8;
        let receiving_interface = arr.get(5).and_then(|value| value.as_uint()).unwrap_or(0);
        let was_used = arr
            .get(6)
            .and_then(|value| value.as_bool())
            .unwrap_or(false);
        let last_used_at = arr
            .get(7)
            .and_then(|value| value.as_uint())
            .map(|value| value as f64);
        let retained = arr
            .get(8)
            .and_then(|value| value.as_bool())
            .unwrap_or(false);

        result.insert(
            dest_hash,
            KnownDestination {
                identity_hash,
                public_key,
                app_data,
                hops,
                received_at,
                receiving_interface,
                was_used,
                last_used_at,
                retained,
            },
        );
    }

    Ok(result)
}

/// Persist packet de-duplication, route, and tunnel state using the upstream
/// Reticulum filenames and wire layouts.
pub fn save_transport_state(
    snapshot: &rns_core::transport::persistence::TransportStateSnapshot,
    storage_dir: &Path,
    low_priority: bool,
) -> io::Result<()> {
    let mut round_started = std::time::Instant::now();
    save_transport_state_with_checkpoint(snapshot, storage_dir, || {
        if low_priority && round_started.elapsed() > std::time::Duration::from_millis(10) {
            std::thread::sleep(std::time::Duration::from_millis(1));
            round_started = std::time::Instant::now();
        }
    })
}

fn save_transport_state_with_checkpoint<F>(
    snapshot: &rns_core::transport::persistence::TransportStateSnapshot,
    storage_dir: &Path,
    mut checkpoint: F,
) -> io::Result<()>
where
    F: FnMut(),
{
    use rns_core::msgpack::{self, Value};

    fs::create_dir_all(storage_dir)?;

    let mut hashes = Vec::with_capacity(snapshot.packet_hashes.len() * 32);
    for hash in &snapshot.packet_hashes {
        checkpoint();
        hashes.extend_from_slice(hash);
    }
    atomic_write(&storage_dir.join("packet_hashlist.raw"), &hashes)?;

    let paths = snapshot
        .paths
        .iter()
        .map(|path| {
            checkpoint();
            encode_persisted_path(path)
        })
        .collect();
    atomic_write(
        &storage_dir.join("destination_table"),
        &msgpack::pack(&Value::Array(paths)),
    )?;

    let tunnels = snapshot
        .tunnels
        .iter()
        .map(|tunnel| {
            checkpoint();
            let paths = tunnel
                .paths
                .iter()
                .map(|path| {
                    checkpoint();
                    encode_persisted_path(path)
                })
                .collect();
            Value::Array(vec![
                Value::Bin(tunnel.tunnel_id.to_vec()),
                optional_hash_value(tunnel.interface_hash),
                Value::Array(paths),
                Value::Float(tunnel.expires),
            ])
        })
        .collect();
    atomic_write(
        &storage_dir.join("tunnels"),
        &msgpack::pack(&Value::Array(tunnels)),
    )
}

fn encode_persisted_path(
    path: &rns_core::transport::persistence::PersistedPath,
) -> rns_core::msgpack::Value {
    use rns_core::msgpack::Value;

    Value::Array(vec![
        Value::Bin(path.destination_hash.to_vec()),
        Value::Float(path.timestamp),
        Value::Bin(path.received_from.to_vec()),
        Value::UInt(path.hops as u64),
        Value::Float(path.expires),
        Value::Array(
            path.random_blobs
                .iter()
                .map(|blob| Value::Bin(blob.to_vec()))
                .collect(),
        ),
        optional_hash_value(path.interface_hash),
        Value::Bin(path.packet_hash.to_vec()),
    ])
}

fn optional_hash_value(hash: Option<[u8; 32]>) -> rns_core::msgpack::Value {
    match hash {
        Some(hash) => rns_core::msgpack::Value::Bin(hash.to_vec()),
        None => rns_core::msgpack::Value::Nil,
    }
}

/// Load transport state. Missing files are treated as empty state; malformed
/// existing files are surfaced so callers can log and continue deliberately.
pub fn load_transport_state(
    storage_dir: &Path,
) -> io::Result<rns_core::transport::persistence::TransportStateSnapshot> {
    use rns_core::transport::persistence::TransportStateSnapshot;

    let packet_hashes = load_packet_hashlist(&storage_dir.join("packet_hashlist.raw"))?;
    let paths = load_path_table(&storage_dir.join("destination_table"))?;
    let tunnels = load_tunnel_table(&storage_dir.join("tunnels"))?;
    Ok(TransportStateSnapshot {
        packet_hashes,
        paths,
        tunnels,
    })
}

fn load_packet_hashlist(path: &Path) -> io::Result<Vec<[u8; 32]>> {
    let data = match fs::read(path) {
        Ok(data) => data,
        Err(err) if err.kind() == io::ErrorKind::NotFound => return Ok(Vec::new()),
        Err(err) => return Err(err),
    };
    Ok(data
        .chunks_exact(32)
        .map(|chunk| {
            let mut hash = [0u8; 32];
            hash.copy_from_slice(chunk);
            hash
        })
        .collect())
}

fn load_path_table(
    path: &Path,
) -> io::Result<Vec<rns_core::transport::persistence::PersistedPath>> {
    let Some(value) = load_optional_msgpack(path)? else {
        return Ok(Vec::new());
    };
    value
        .as_array()
        .ok_or_else(|| invalid_transport_data("destination table must be an array"))?
        .iter()
        .map(decode_persisted_path)
        .collect()
}

fn load_tunnel_table(
    path: &Path,
) -> io::Result<Vec<rns_core::transport::persistence::PersistedTunnel>> {
    use rns_core::transport::persistence::PersistedTunnel;

    let Some(value) = load_optional_msgpack(path)? else {
        return Ok(Vec::new());
    };
    value
        .as_array()
        .ok_or_else(|| invalid_transport_data("tunnel table must be an array"))?
        .iter()
        .map(|value| {
            let fields = value
                .as_array()
                .filter(|fields| fields.len() == 4)
                .ok_or_else(|| invalid_transport_data("tunnel entry must contain 4 fields"))?;
            let tunnel_id = fixed_bin::<32>(&fields[0], "tunnel id")?;
            let interface_hash = optional_fixed_bin::<32>(&fields[1], "tunnel interface hash")?;
            let paths = fields[2]
                .as_array()
                .ok_or_else(|| invalid_transport_data("tunnel paths must be an array"))?
                .iter()
                .map(decode_persisted_path)
                .collect::<io::Result<Vec<_>>>()?;
            let expires = fields[3]
                .as_number()
                .ok_or_else(|| invalid_transport_data("tunnel expiry must be numeric"))?;
            Ok(PersistedTunnel {
                tunnel_id,
                interface_hash,
                paths,
                expires,
            })
        })
        .collect()
}

fn decode_persisted_path(
    value: &rns_core::msgpack::Value,
) -> io::Result<rns_core::transport::persistence::PersistedPath> {
    use rns_core::transport::persistence::PersistedPath;

    let fields = value
        .as_array()
        .filter(|fields| fields.len() == 8)
        .ok_or_else(|| invalid_transport_data("path entry must contain 8 fields"))?;
    let random_blobs = fields[5]
        .as_array()
        .ok_or_else(|| invalid_transport_data("random blobs must be an array"))?
        .iter()
        .map(|value| fixed_bin::<10>(value, "random blob"))
        .collect::<io::Result<Vec<_>>>()?;
    let hops = fields[3]
        .as_uint()
        .and_then(|value| u8::try_from(value).ok())
        .ok_or_else(|| invalid_transport_data("hop count must fit in a byte"))?;
    Ok(PersistedPath {
        destination_hash: fixed_bin::<16>(&fields[0], "destination hash")?,
        timestamp: fields[1]
            .as_number()
            .ok_or_else(|| invalid_transport_data("path timestamp must be numeric"))?,
        received_from: fixed_bin::<16>(&fields[2], "received-from hash")?,
        hops,
        expires: fields[4]
            .as_number()
            .ok_or_else(|| invalid_transport_data("path expiry must be numeric"))?,
        random_blobs,
        interface_hash: optional_fixed_bin::<32>(&fields[6], "interface hash")?,
        packet_hash: fixed_bin::<32>(&fields[7], "packet hash")?,
    })
}

fn load_optional_msgpack(path: &Path) -> io::Result<Option<rns_core::msgpack::Value>> {
    let data = match fs::read(path) {
        Ok(data) => data,
        Err(err) if err.kind() == io::ErrorKind::NotFound => return Ok(None),
        Err(err) => return Err(err),
    };
    rns_core::msgpack::unpack_exact(&data)
        .map(Some)
        .map_err(|err| invalid_transport_data(&format!("msgpack error: {err}")))
}

fn fixed_bin<const N: usize>(value: &rns_core::msgpack::Value, field: &str) -> io::Result<[u8; N]> {
    let bytes = value
        .as_bin()
        .filter(|bytes| bytes.len() == N)
        .ok_or_else(|| invalid_transport_data(&format!("{field} must be {N} bytes")))?;
    let mut result = [0u8; N];
    result.copy_from_slice(bytes);
    Ok(result)
}

fn optional_fixed_bin<const N: usize>(
    value: &rns_core::msgpack::Value,
    field: &str,
) -> io::Result<Option<[u8; N]>> {
    if matches!(value, rns_core::msgpack::Value::Nil) {
        Ok(None)
    } else {
        fixed_bin(value, field).map(Some)
    }
}

fn invalid_transport_data(message: &str) -> io::Error {
    io::Error::new(io::ErrorKind::InvalidData, message)
}

/// Resolve the config directory path.
/// Priority: explicit path > `~/.reticulum/`
pub fn resolve_config_dir(explicit: Option<&Path>) -> PathBuf {
    if let Some(p) = explicit {
        p.to_path_buf()
    } else {
        let home = std::env::var("HOME").unwrap_or_else(|_| "/tmp".into());
        PathBuf::from(home).join(".reticulum")
    }
}

/// Load or create an identity at the standard location.
pub fn load_or_create_identity(identities_dir: &Path) -> io::Result<Identity> {
    let id_path = identities_dir.join("identity");
    if id_path.exists() {
        load_identity(&id_path)
    } else {
        let identity = Identity::new(&mut OsRng);
        save_identity(&identity, &id_path)?;
        Ok(identity)
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    use std::sync::atomic::{AtomicU64, Ordering};
    static TEST_COUNTER: AtomicU64 = AtomicU64::new(0);

    fn temp_dir() -> PathBuf {
        let id = TEST_COUNTER.fetch_add(1, Ordering::Relaxed);
        let dir = std::env::temp_dir().join(format!("rns-test-{}-{}", std::process::id(), id));
        let _ = fs::remove_dir_all(&dir);
        fs::create_dir_all(&dir).unwrap();
        dir
    }

    #[test]
    fn save_load_identity_roundtrip() {
        let dir = temp_dir();
        let path = dir.join("test_identity");

        let identity = Identity::new(&mut OsRng);
        let original_hash = *identity.hash();

        save_identity(&identity, &path).unwrap();
        let loaded = load_identity(&path).unwrap();

        assert_eq!(*loaded.hash(), original_hash);

        let _ = fs::remove_dir_all(&dir);
    }

    #[test]
    fn identity_file_format() {
        let dir = temp_dir();
        let path = dir.join("test_identity_fmt");

        let identity = Identity::new(&mut OsRng);
        save_identity(&identity, &path).unwrap();

        let data = fs::read(&path).unwrap();
        assert_eq!(data.len(), 64, "Identity file must be exactly 64 bytes");

        // First 32 bytes: X25519 private key
        // Next 32 bytes: Ed25519 private key (seed)
        let private_key = identity.get_private_key();
        let private_key = private_key.unwrap();
        assert_eq!(&data[..], &private_key[..]);

        let _ = fs::remove_dir_all(&dir);
    }

    #[test]
    fn save_load_known_destinations_empty() {
        let dir = temp_dir();
        let path = dir.join("known_destinations");

        let empty: HashMap<[u8; 16], KnownDestination> = HashMap::new();
        save_known_destinations(&empty, &path).unwrap();

        let loaded = load_known_destinations(&path).unwrap();
        assert!(loaded.is_empty());

        let _ = fs::remove_dir_all(&dir);
    }

    #[test]
    fn save_load_known_destinations_roundtrip() {
        let dir = temp_dir();
        let path = dir.join("known_destinations");

        let mut dests = HashMap::new();
        dests.insert(
            [0x01u8; 16],
            KnownDestination {
                identity_hash: [0x11u8; 16],
                public_key: [0xABu8; 64],
                app_data: Some(vec![0x01, 0x02, 0x03]),
                hops: 2,
                received_at: 1700000000.0,
                receiving_interface: 7,
                was_used: true,
                last_used_at: Some(1700000010.0),
                retained: true,
            },
        );
        dests.insert(
            [0x02u8; 16],
            KnownDestination {
                identity_hash: [0x22u8; 16],
                public_key: [0xCDu8; 64],
                app_data: None,
                hops: 1,
                received_at: 1700000001.0,
                receiving_interface: 0,
                was_used: false,
                last_used_at: None,
                retained: false,
            },
        );

        save_known_destinations(&dests, &path).unwrap();
        let loaded = load_known_destinations(&path).unwrap();

        assert_eq!(loaded.len(), 2);

        let d1 = &loaded[&[0x01u8; 16]];
        assert_eq!(d1.identity_hash, [0x11u8; 16]);
        assert_eq!(d1.public_key, [0xABu8; 64]);
        assert_eq!(d1.app_data, Some(vec![0x01, 0x02, 0x03]));
        assert_eq!(d1.hops, 2);
        assert_eq!(d1.received_at as u64, 1700000000);
        assert_eq!(d1.receiving_interface, 7);
        assert!(d1.was_used);
        assert_eq!(d1.last_used_at, Some(1700000010.0));
        assert!(d1.retained);

        let d2 = &loaded[&[0x02u8; 16]];
        assert_eq!(d2.app_data, None);
        assert!(!d2.was_used);
        assert_eq!(d2.last_used_at, None);
        assert!(!d2.retained);

        let _ = fs::remove_dir_all(&dir);
    }

    #[test]
    fn save_known_destinations_replaces_existing_file_atomically() {
        let dir = temp_dir();
        let path = dir.join("known_destinations");
        fs::write(&path, b"old").unwrap();

        let mut dests = HashMap::new();
        dests.insert(
            [0x03u8; 16],
            KnownDestination {
                identity_hash: [0x33u8; 16],
                public_key: [0xEFu8; 64],
                app_data: None,
                hops: 3,
                received_at: 1700000002.0,
                receiving_interface: 9,
                was_used: false,
                last_used_at: None,
                retained: false,
            },
        );

        save_known_destinations(&dests, &path).unwrap();

        let loaded = load_known_destinations(&path).unwrap();
        assert_eq!(loaded.len(), 1);
        assert!(loaded.contains_key(&[0x03u8; 16]));
        let leftover_temp = fs::read_dir(&dir)
            .unwrap()
            .filter_map(Result::ok)
            .any(|entry| entry.file_name().to_string_lossy().contains(".tmp."));
        assert!(!leftover_temp);

        let _ = fs::remove_dir_all(&dir);
    }

    #[test]
    fn ratchet_store_roundtrip() {
        let dir = temp_dir();
        let store = FsRatchetStore::new(dir.join("ratchets"));
        let dest = [0xAA; 16];
        let entry = RatchetEntry {
            ratchet: [0xBB; 32],
            received_at: 1700000000.25,
        };

        store.remember(dest, entry).unwrap();
        let loaded = store.current(&dest, 1700000001.0, 60.0).unwrap();

        assert_eq!(loaded, Some(entry));
        assert!(dir.join("ratchets").join(hex_lower(&dest)).exists());

        let _ = fs::remove_dir_all(&dir);
    }

    #[test]
    fn ratchet_cleanup_removes_expired_corrupt_unknown_and_temp() {
        let dir = temp_dir();
        let ratchets = dir.join("ratchets");
        fs::create_dir_all(&ratchets).unwrap();
        let store = FsRatchetStore::new(ratchets.clone());

        let known_live = [0x01; 16];
        let known_expired = [0x02; 16];
        let unknown = [0x03; 16];
        store
            .remember(
                known_live,
                RatchetEntry {
                    ratchet: [0x11; 32],
                    received_at: 1000.0,
                },
            )
            .unwrap();
        store
            .remember(
                known_expired,
                RatchetEntry {
                    ratchet: [0x22; 32],
                    received_at: 100.0,
                },
            )
            .unwrap();
        store
            .remember(
                unknown,
                RatchetEntry {
                    ratchet: [0x33; 32],
                    received_at: 1000.0,
                },
            )
            .unwrap();
        fs::write(ratchets.join(hex_lower(&[0x04; 16])), b"not msgpack").unwrap();
        fs::write(ratchets.join("0102.out"), b"temp").unwrap();

        let known = HashSet::from([known_live, known_expired, [0x04; 16]]);
        let stats = store.cleanup(&known, 1000.0, 300.0).unwrap();

        assert_eq!(stats.processed, 5);
        assert_eq!(stats.not_known, 1);
        assert_eq!(stats.removed, 4);
        assert!(ratchets.join(hex_lower(&known_live)).exists());
        assert!(!ratchets.join(hex_lower(&known_expired)).exists());
        assert!(!ratchets.join(hex_lower(&unknown)).exists());
        assert!(!ratchets.join(hex_lower(&[0x04; 16])).exists());
        assert!(!ratchets.join("0102.out").exists());

        let _ = fs::remove_dir_all(&dir);
    }

    #[test]
    fn ensure_dirs_creates() {
        let dir = temp_dir().join("new_config");
        let _ = fs::remove_dir_all(&dir);

        let paths = ensure_storage_dirs(&dir).unwrap();

        assert!(paths.storage.exists());
        assert!(paths.cache.exists());
        assert!(paths.identities.exists());
        assert!(paths.ratchets.exists());
        assert!(paths.discovered_interfaces.exists());

        let _ = fs::remove_dir_all(&dir);
    }

    #[test]
    fn ensure_dirs_existing() {
        let dir = temp_dir().join("existing_config");
        fs::create_dir_all(dir.join("storage")).unwrap();
        fs::create_dir_all(dir.join("cache")).unwrap();

        let paths = ensure_storage_dirs(&dir).unwrap();
        assert!(paths.storage.exists());
        assert!(paths.identities.exists());

        let _ = fs::remove_dir_all(&dir);
    }

    #[test]
    fn load_or_create_identity_new() {
        let dir = temp_dir().join("load_or_create");
        fs::create_dir_all(&dir).unwrap();

        let identity = load_or_create_identity(&dir).unwrap();
        let id_path = dir.join("identity");
        assert!(id_path.exists());

        // Loading again should give same identity
        let loaded = load_or_create_identity(&dir).unwrap();
        assert_eq!(*identity.hash(), *loaded.hash());

        let _ = fs::remove_dir_all(&dir);
    }

    fn transport_snapshot() -> rns_core::transport::persistence::TransportStateSnapshot {
        use rns_core::transport::persistence::{
            PersistedPath, PersistedTunnel, TransportStateSnapshot,
        };

        let path = PersistedPath {
            destination_hash: [0x11; 16],
            timestamp: 10.25,
            received_from: [0x22; 16],
            hops: 3,
            expires: 20.5,
            random_blobs: vec![[0x33; 10], [0x44; 10]],
            interface_hash: Some([0x55; 32]),
            packet_hash: [0x66; 32],
        };
        TransportStateSnapshot {
            packet_hashes: vec![[1; 32], [2; 32]],
            paths: vec![path.clone()],
            tunnels: vec![PersistedTunnel {
                tunnel_id: [0x77; 32],
                interface_hash: None,
                paths: vec![PersistedPath {
                    destination_hash: [0x88; 16],
                    interface_hash: None,
                    ..path
                }],
                expires: 30.75,
            }],
        }
    }

    #[test]
    fn packet_hashlist_raw_is_fixed_width_fifo_and_ignores_partial_tail() {
        let dir = temp_dir();
        let snapshot = transport_snapshot();
        save_transport_state(&snapshot, &dir, false).unwrap();

        let path = dir.join("packet_hashlist.raw");
        let mut raw = fs::read(&path).unwrap();
        assert_eq!(raw.len(), 64);
        assert_eq!(&raw[..32], &[1; 32]);
        assert_eq!(&raw[32..], &[2; 32]);
        raw.extend_from_slice(&[0xFF; 17]);
        fs::write(&path, raw).unwrap();

        assert_eq!(load_packet_hashlist(&path).unwrap(), vec![[1; 32], [2; 32]]);
        let _ = fs::remove_dir_all(&dir);
    }

    #[test]
    fn path_and_tunnel_tables_round_trip_python_compatible_fields() {
        let dir = temp_dir();
        let expected = transport_snapshot();
        save_transport_state(&expected, &dir, false).unwrap();
        let loaded = load_transport_state(&dir).unwrap();
        assert_eq!(loaded, expected);

        let packed = fs::read(dir.join("destination_table")).unwrap();
        let value = rns_core::msgpack::unpack_exact(&packed).unwrap();
        let entry = &value.as_array().unwrap()[0];
        assert_eq!(entry.as_array().unwrap().len(), 8);
        assert_eq!(entry.as_array().unwrap()[0].as_bin().unwrap().len(), 16);
        assert_eq!(entry.as_array().unwrap()[6].as_bin().unwrap().len(), 32);

        let packed = fs::read(dir.join("tunnels")).unwrap();
        let value = rns_core::msgpack::unpack_exact(&packed).unwrap();
        let tunnel = &value.as_array().unwrap()[0];
        assert_eq!(tunnel.as_array().unwrap().len(), 4);
        assert!(matches!(
            tunnel.as_array().unwrap()[1],
            rns_core::msgpack::Value::Nil
        ));
        let _ = fs::remove_dir_all(&dir);
    }

    #[test]
    fn missing_transport_state_files_load_as_empty_tables() {
        let dir = temp_dir();
        assert_eq!(
            load_transport_state(&dir).unwrap(),
            rns_core::transport::persistence::TransportStateSnapshot::default()
        );
        let _ = fs::remove_dir_all(&dir);
    }

    #[test]
    fn low_priority_save_checkpoints_each_collection_without_temp_leaks() {
        let dir = temp_dir();
        let snapshot = transport_snapshot();
        let mut checkpoints = 0;
        save_transport_state_with_checkpoint(&snapshot, &dir, || checkpoints += 1).unwrap();

        // 2 hashes + 1 route + 1 tunnel + 1 tunnel route.
        assert_eq!(checkpoints, 5);
        assert!(!fs::read_dir(&dir)
            .unwrap()
            .filter_map(Result::ok)
            .any(|entry| entry.file_name().to_string_lossy().contains(".tmp.")));
        let _ = fs::remove_dir_all(&dir);
    }

    #[test]
    fn malformed_transport_table_reports_invalid_data_without_panicking() {
        let dir = temp_dir();
        fs::write(dir.join("destination_table"), b"not-msgpack").unwrap();
        let error = load_transport_state(&dir).unwrap_err();
        assert_eq!(error.kind(), io::ErrorKind::InvalidData);
        let _ = fs::remove_dir_all(&dir);
    }
}