rns-net 0.5.5

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
//! Event types for the driver loop — generic over the writer type.

use std::fmt;
use std::net::IpAddr;
use std::sync::mpsc;
use std::time::Duration;

use rns_core::announce::ValidatedAnnounce;
use rns_core::transport::announce_verify_queue::AnnounceVerifyKey;
use rns_core::transport::types::{InterfaceId, InterfaceInfo};

/// Policy for handling incoming direct-connect proposals.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum HolePunchPolicy {
    /// Reject all proposals.
    Reject,
    /// Accept all proposals automatically.
    AcceptAll,
    /// Ask the application callback.
    AskApp,
}

impl Default for HolePunchPolicy {
    fn default() -> Self {
        HolePunchPolicy::AcceptAll
    }
}

/// Scalar runtime-config value.
#[derive(Debug, Clone, PartialEq)]
pub enum RuntimeConfigValue {
    Int(i64),
    Float(f64),
    Bool(bool),
    String(String),
    Null,
}

/// Source of a runtime-config value.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum RuntimeConfigSource {
    Startup,
    RuntimeOverride,
}

/// How a runtime-config change applies.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum RuntimeConfigApplyMode {
    Immediate,
    NewConnectionsOnly,
    NextReconnect,
    RestartRequired,
}

/// A runtime-config entry exposed by the daemon.
#[derive(Debug, Clone, PartialEq)]
pub struct RuntimeConfigEntry {
    pub key: String,
    pub value: RuntimeConfigValue,
    pub default: RuntimeConfigValue,
    pub source: RuntimeConfigSource,
    pub apply_mode: RuntimeConfigApplyMode,
    pub description: Option<String>,
}

/// Runtime-config mutation error.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct RuntimeConfigError {
    pub code: RuntimeConfigErrorCode,
    pub message: String,
}

/// Category of runtime-config mutation error.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum RuntimeConfigErrorCode {
    UnknownKey,
    InvalidType,
    InvalidValue,
    Unsupported,
    NotFound,
    ApplyFailed,
}

#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum LifecycleState {
    Active,
    Draining,
    Stopping,
    Stopped,
}

#[derive(Debug, Clone, PartialEq)]
pub struct DrainStatus {
    pub state: LifecycleState,
    pub drain_age_seconds: Option<f64>,
    pub deadline_remaining_seconds: Option<f64>,
    pub drain_complete: bool,
    pub interface_writer_queued_frames: usize,
    pub provider_backlog_events: usize,
    pub provider_consumer_queued_events: usize,
    pub detail: Option<String>,
}

/// Events sent to the driver thread.
///
/// `W` is the writer type (e.g. `Box<dyn Writer>` for sync,
/// or a channel sender for async).
pub enum Event<W: Send> {
    /// A decoded frame arrived from an interface.
    Frame {
        interface_id: InterfaceId,
        data: Vec<u8>,
    },
    /// (Internal) An announce was verified off-thread and is ready for driver-side processing.
    AnnounceVerified {
        key: AnnounceVerifyKey,
        validated: ValidatedAnnounce,
        sig_cache_key: [u8; 32],
    },
    /// (Internal) An announce failed off-thread verification.
    AnnounceVerifyFailed {
        key: AnnounceVerifyKey,
        sig_cache_key: [u8; 32],
    },
    /// An interface came online after (re)connecting.
    /// Carries a new writer if the connection was re-established.
    /// Carries InterfaceInfo if this is a new dynamic interface (e.g. TCP server client).
    InterfaceUp(InterfaceId, Option<W>, Option<InterfaceInfo>),
    /// An interface went offline (socket closed, error).
    InterfaceDown(InterfaceId),
    /// Periodic maintenance tick (1s interval).
    Tick,
    /// Enter drain mode and stop admitting new work.
    BeginDrain { timeout: Duration },
    /// Shut down the driver loop.
    Shutdown,
    /// Send an outbound packet.
    SendOutbound {
        raw: Vec<u8>,
        dest_type: u8,
        attached_interface: Option<InterfaceId>,
    },
    /// Register a local destination.
    RegisterDestination { dest_hash: [u8; 16], dest_type: u8 },
    /// Remember the latest explicit SINGLE announce for shared-client replay.
    StoreSharedAnnounce {
        dest_hash: [u8; 16],
        name_hash: [u8; 10],
        identity_prv_key: [u8; 64],
        app_data: Option<Vec<u8>>,
    },
    /// Deregister a local destination.
    DeregisterDestination { dest_hash: [u8; 16] },
    /// Deregister a link destination (stop accepting incoming links).
    DeregisterLinkDestination { dest_hash: [u8; 16] },
    /// Query driver state. Response is sent via the provided channel.
    Query(QueryRequest, mpsc::Sender<QueryResponse>),
    /// Register a link destination (accepts incoming LINKREQUEST).
    RegisterLinkDestination {
        dest_hash: [u8; 16],
        sig_prv_bytes: [u8; 32],
        sig_pub_bytes: [u8; 32],
        resource_strategy: u8,
    },
    /// Register a request handler for a path on established links.
    RegisterRequestHandler {
        path: String,
        allowed_list: Option<Vec<[u8; 16]>>,
        handler: Box<
            dyn Fn([u8; 16], &str, &[u8], Option<&([u8; 16], [u8; 64])>) -> Option<Vec<u8>> + Send,
        >,
    },
    /// Create an outbound link. Response sends (link_id) back.
    CreateLink {
        dest_hash: [u8; 16],
        dest_sig_pub_bytes: [u8; 32],
        response_tx: mpsc::Sender<[u8; 16]>,
    },
    /// Send a request on an established link.
    SendRequest {
        link_id: [u8; 16],
        path: String,
        data: Vec<u8>,
    },
    /// Identify on a link (send identity to remote peer).
    IdentifyOnLink {
        link_id: [u8; 16],
        identity_prv_key: [u8; 64],
    },
    /// Tear down a link.
    TeardownLink { link_id: [u8; 16] },
    /// Send a resource on a link.
    SendResource {
        link_id: [u8; 16],
        data: Vec<u8>,
        metadata: Option<Vec<u8>>,
    },
    /// Set the resource acceptance strategy for a link.
    SetResourceStrategy { link_id: [u8; 16], strategy: u8 },
    /// Accept or reject a pending resource (for AcceptApp strategy).
    AcceptResource {
        link_id: [u8; 16],
        resource_hash: Vec<u8>,
        accept: bool,
    },
    /// Send a channel message on a link.
    SendChannelMessage {
        link_id: [u8; 16],
        msgtype: u16,
        payload: Vec<u8>,
        response_tx: mpsc::Sender<Result<(), String>>,
    },
    /// Send generic data on a link with a given context.
    SendOnLink {
        link_id: [u8; 16],
        data: Vec<u8>,
        context: u8,
    },
    /// Request a path to a destination from the network.
    RequestPath { dest_hash: [u8; 16] },
    /// Register a proof strategy for a destination.
    RegisterProofStrategy {
        dest_hash: [u8; 16],
        strategy: rns_core::types::ProofStrategy,
        /// Full identity private key (64 bytes) for signing proofs.
        signing_key: Option<[u8; 64]>,
    },
    /// Propose a direct connection to a peer via hole punching.
    ProposeDirectConnect { link_id: [u8; 16] },
    /// Set the direct-connect policy.
    SetDirectConnectPolicy { policy: HolePunchPolicy },
    /// (Internal) Probe result arrived from a worker thread.
    HolePunchProbeResult {
        link_id: [u8; 16],
        session_id: [u8; 16],
        observed_addr: std::net::SocketAddr,
        socket: std::net::UdpSocket,
        /// The probe server that responded successfully.
        probe_server: std::net::SocketAddr,
    },
    /// (Internal) Probe failed.
    HolePunchProbeFailed {
        link_id: [u8; 16],
        session_id: [u8; 16],
    },
    /// An interface's configuration changed (placeholder for future use).
    InterfaceConfigChanged(InterfaceId),
    /// A backbone server accepted a new inbound peer connection.
    BackbonePeerConnected {
        server_interface_id: InterfaceId,
        peer_interface_id: InterfaceId,
        peer_ip: IpAddr,
        peer_port: u16,
    },
    /// A backbone peer connection closed for any reason.
    BackbonePeerDisconnected {
        server_interface_id: InterfaceId,
        peer_interface_id: InterfaceId,
        peer_ip: IpAddr,
        peer_port: u16,
        connected_for: Duration,
        had_received_data: bool,
    },
    /// A backbone peer was disconnected for idling without sending data.
    BackbonePeerIdleTimeout {
        server_interface_id: InterfaceId,
        peer_interface_id: InterfaceId,
        peer_ip: IpAddr,
        peer_port: u16,
        connected_for: Duration,
    },
    /// A backbone peer was disconnected because its writer stalled.
    BackbonePeerWriteStall {
        server_interface_id: InterfaceId,
        peer_interface_id: InterfaceId,
        peer_ip: IpAddr,
        peer_port: u16,
        connected_for: Duration,
    },
    /// A backbone peer IP was penalized due to abusive behavior.
    BackbonePeerPenalty {
        server_interface_id: InterfaceId,
        peer_ip: IpAddr,
        penalty_level: u8,
        blacklist_for: Duration,
    },
    /// Load a WASM hook at runtime.
    LoadHook {
        name: String,
        wasm_bytes: Vec<u8>,
        attach_point: String,
        priority: i32,
        response_tx: mpsc::Sender<Result<(), String>>,
    },
    /// Unload a WASM hook at runtime.
    UnloadHook {
        name: String,
        attach_point: String,
        response_tx: mpsc::Sender<Result<(), String>>,
    },
    /// Reload a WASM hook at runtime (detach + recompile + reattach with same priority).
    ReloadHook {
        name: String,
        attach_point: String,
        wasm_bytes: Vec<u8>,
        response_tx: mpsc::Sender<Result<(), String>>,
    },
    /// Enable or disable a loaded WASM hook at runtime.
    SetHookEnabled {
        name: String,
        attach_point: String,
        enabled: bool,
        response_tx: mpsc::Sender<Result<(), String>>,
    },
    /// Update the priority of a loaded WASM hook at runtime.
    SetHookPriority {
        name: String,
        attach_point: String,
        priority: i32,
        response_tx: mpsc::Sender<Result<(), String>>,
    },
    /// List all loaded hooks.
    ListHooks {
        response_tx: mpsc::Sender<Vec<HookInfo>>,
    },
}

/// Information about a loaded hook program.
#[derive(Debug, Clone)]
pub struct HookInfo {
    pub name: String,
    pub attach_point: String,
    pub priority: i32,
    pub enabled: bool,
    pub consecutive_traps: u32,
}

/// Live behavioral state for a backbone peer IP.
#[derive(Debug, Clone, PartialEq)]
pub struct BackbonePeerStateEntry {
    pub interface_name: String,
    pub peer_ip: IpAddr,
    pub connected_count: usize,
    pub blacklisted_remaining_secs: Option<f64>,
    pub blacklist_reason: Option<String>,
    pub reject_count: u64,
}

/// Hook-visible snapshot of a backbone peer lifecycle event.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct BackbonePeerHookEvent {
    pub server_interface_id: InterfaceId,
    pub peer_interface_id: Option<InterfaceId>,
    pub peer_ip: IpAddr,
    pub peer_port: u16,
    pub connected_for: Duration,
    pub had_received_data: bool,
    pub penalty_level: u8,
    pub blacklist_for: Duration,
}

#[derive(Debug, Clone, PartialEq, Eq)]
pub struct BackboneInterfaceEntry {
    pub interface_id: InterfaceId,
    pub interface_name: String,
}

#[derive(Debug, Clone, PartialEq, Eq)]
pub struct ProviderBridgeConsumerStats {
    pub id: u64,
    pub connected: bool,
    pub queue_len: usize,
    pub queued_bytes: usize,
    pub dropped_pending: u64,
    pub dropped_total: u64,
    pub queue_max_events: usize,
    pub queue_max_bytes: usize,
}

#[derive(Debug, Clone, PartialEq, Eq)]
pub struct ProviderBridgeStats {
    pub connected: bool,
    pub consumer_count: usize,
    pub queue_max_events: usize,
    pub queue_max_bytes: usize,
    pub backlog_len: usize,
    pub backlog_bytes: usize,
    pub backlog_dropped_pending: u64,
    pub backlog_dropped_total: u64,
    pub total_disconnect_count: u64,
    pub consumers: Vec<ProviderBridgeConsumerStats>,
}

/// Queries that can be sent to the driver.
#[derive(Debug)]
pub enum QueryRequest {
    /// Get interface statistics and transport info.
    InterfaceStats,
    /// Get path table entries, optionally filtered by max hops.
    PathTable { max_hops: Option<u8> },
    /// Get rate table entries.
    RateTable,
    /// Look up the next hop for a destination.
    NextHop { dest_hash: [u8; 16] },
    /// Look up the next hop interface name for a destination.
    NextHopIfName { dest_hash: [u8; 16] },
    /// Get link table entry count.
    LinkCount,
    /// Drop a specific path.
    DropPath { dest_hash: [u8; 16] },
    /// Drop all paths that route via a given transport hash.
    DropAllVia { transport_hash: [u8; 16] },
    /// Drop all announce queues.
    DropAnnounceQueues,
    /// Get the transport identity hash.
    TransportIdentity,
    /// Get all blackholed identities.
    GetBlackholed,
    /// Add an identity to the blackhole list.
    BlackholeIdentity {
        identity_hash: [u8; 16],
        duration_hours: Option<f64>,
        reason: Option<String>,
    },
    /// Remove an identity from the blackhole list.
    UnblackholeIdentity { identity_hash: [u8; 16] },
    /// Check if a path exists to a destination.
    HasPath { dest_hash: [u8; 16] },
    /// Get hop count to a destination.
    HopsTo { dest_hash: [u8; 16] },
    /// Recall identity info for a destination.
    RecallIdentity { dest_hash: [u8; 16] },
    /// Get locally registered destinations.
    LocalDestinations,
    /// Get active links.
    Links,
    /// Get active resource transfers.
    Resources,
    /// Inject a path entry into the path table.
    InjectPath {
        dest_hash: [u8; 16],
        next_hop: [u8; 16],
        hops: u8,
        expires: f64,
        interface_name: String,
        packet_hash: [u8; 32],
    },
    /// Inject an identity into the known destinations cache.
    InjectIdentity {
        dest_hash: [u8; 16],
        identity_hash: [u8; 16],
        public_key: [u8; 64],
        app_data: Option<Vec<u8>>,
        hops: u8,
        received_at: f64,
    },
    /// Get discovered interfaces.
    DiscoveredInterfaces {
        only_available: bool,
        only_transport: bool,
    },
    /// Send a probe packet to a destination and return (packet_hash, hops).
    SendProbe {
        dest_hash: [u8; 16],
        payload_size: usize,
    },
    /// Check if a proof was received for a probe packet.
    CheckProof { packet_hash: [u8; 32] },
    /// List runtime-config entries currently supported by the daemon.
    ListRuntimeConfig,
    /// Get a single runtime-config entry by key.
    GetRuntimeConfig { key: String },
    /// Set a runtime-config value by key.
    SetRuntimeConfig {
        key: String,
        value: RuntimeConfigValue,
    },
    /// Reset a runtime-config value to its startup/default value.
    ResetRuntimeConfig { key: String },
    /// List live backbone peer state, optionally filtered to one interface.
    BackbonePeerState { interface_name: Option<String> },
    /// List registered backbone server interfaces.
    BackboneInterfaces,
    /// Report live provider-bridge queue/drop state.
    ProviderBridgeStats,
    /// Report current lifecycle/drain status.
    DrainStatus,
    /// Clear live backbone peer state for one interface/IP pair.
    ClearBackbonePeerState {
        interface_name: String,
        peer_ip: IpAddr,
    },
    /// Blacklist a backbone peer IP for a duration.
    BlacklistBackbonePeer {
        interface_name: String,
        peer_ip: IpAddr,
        duration: Duration,
        reason: String,
        penalty_level: u8,
    },
}

/// Responses to queries.
#[derive(Debug)]
pub enum QueryResponse {
    InterfaceStats(InterfaceStatsResponse),
    PathTable(Vec<PathTableEntry>),
    RateTable(Vec<RateTableEntry>),
    NextHop(Option<NextHopResponse>),
    NextHopIfName(Option<String>),
    LinkCount(usize),
    DropPath(bool),
    DropAllVia(usize),
    DropAnnounceQueues,
    TransportIdentity(Option<[u8; 16]>),
    Blackholed(Vec<BlackholeInfo>),
    BlackholeResult(bool),
    UnblackholeResult(bool),
    HasPath(bool),
    HopsTo(Option<u8>),
    RecallIdentity(Option<crate::common::destination::AnnouncedIdentity>),
    LocalDestinations(Vec<LocalDestinationEntry>),
    Links(Vec<LinkInfoEntry>),
    Resources(Vec<ResourceInfoEntry>),
    InjectPath(bool),
    InjectIdentity(bool),
    /// List of discovered interfaces.
    DiscoveredInterfaces(Vec<crate::common::discovery::DiscoveredInterface>),
    /// Probe sent: (packet_hash, hops) or None if identity unknown.
    SendProbe(Option<([u8; 32], u8)>),
    /// Proof check: RTT if received, None if still pending.
    CheckProof(Option<f64>),
    /// Runtime-config entries currently supported by the daemon.
    RuntimeConfigList(Vec<RuntimeConfigEntry>),
    /// A specific runtime-config entry, or None if the key is unknown.
    RuntimeConfigEntry(Option<RuntimeConfigEntry>),
    /// Result of setting a runtime-config value.
    RuntimeConfigSet(Result<RuntimeConfigEntry, RuntimeConfigError>),
    /// Result of resetting a runtime-config value.
    RuntimeConfigReset(Result<RuntimeConfigEntry, RuntimeConfigError>),
    /// Live backbone peer state entries.
    BackbonePeerState(Vec<BackbonePeerStateEntry>),
    /// Registered backbone server interfaces.
    BackboneInterfaces(Vec<BackboneInterfaceEntry>),
    /// Live provider-bridge queue/drop state if enabled.
    ProviderBridgeStats(Option<ProviderBridgeStats>),
    /// Current lifecycle/drain status.
    DrainStatus(DrainStatus),
    /// Result of clearing one backbone peer state entry.
    ClearBackbonePeerState(bool),
    /// Result of blacklisting a backbone peer.
    BlacklistBackbonePeer(bool),
}

/// Interface statistics response.
#[derive(Debug, Clone)]
pub struct InterfaceStatsResponse {
    pub interfaces: Vec<SingleInterfaceStat>,
    pub transport_id: Option<[u8; 16]>,
    pub transport_enabled: bool,
    pub transport_uptime: f64,
    /// Total received bytes across all interfaces.
    pub total_rxb: u64,
    /// Total transmitted bytes across all interfaces.
    pub total_txb: u64,
    /// Probe responder destination hash (if enabled).
    pub probe_responder: Option<[u8; 16]>,
}

/// Statistics for a single interface.
#[derive(Debug, Clone)]
pub struct SingleInterfaceStat {
    pub id: u64,
    pub name: String,
    pub status: bool,
    pub mode: u8,
    pub rxb: u64,
    pub txb: u64,
    pub rx_packets: u64,
    pub tx_packets: u64,
    pub bitrate: Option<u64>,
    pub ifac_size: Option<usize>,
    pub started: f64,
    /// Incoming announce frequency (per second).
    pub ia_freq: f64,
    /// Outgoing announce frequency (per second).
    pub oa_freq: f64,
    /// Human-readable interface type string (e.g. "TCPClientInterface").
    pub interface_type: String,
}

/// A locally registered destination.
#[derive(Debug, Clone)]
pub struct LocalDestinationEntry {
    pub hash: [u8; 16],
    pub dest_type: u8,
}

/// Information about an active link.
#[derive(Debug, Clone)]
pub struct LinkInfoEntry {
    pub link_id: [u8; 16],
    pub state: String,
    pub is_initiator: bool,
    pub dest_hash: [u8; 16],
    pub remote_identity: Option<[u8; 16]>,
    pub rtt: Option<f64>,
    pub channel_window: Option<u16>,
    pub channel_outstanding: Option<usize>,
    pub pending_channel_packets: usize,
    pub channel_send_ok: u64,
    pub channel_send_not_ready: u64,
    pub channel_send_too_big: u64,
    pub channel_send_other_error: u64,
    pub channel_messages_received: u64,
    pub channel_proofs_sent: u64,
    pub channel_proofs_received: u64,
}

/// Information about an active resource transfer.
#[derive(Debug, Clone)]
pub struct ResourceInfoEntry {
    pub link_id: [u8; 16],
    pub direction: String,
    pub total_parts: usize,
    pub transferred_parts: usize,
    pub complete: bool,
}

/// A single path table entry for query responses.
#[derive(Debug, Clone)]
pub struct PathTableEntry {
    pub hash: [u8; 16],
    pub timestamp: f64,
    pub via: [u8; 16],
    pub hops: u8,
    pub expires: f64,
    pub interface: InterfaceId,
    pub interface_name: String,
}

/// A single rate table entry for query responses.
#[derive(Debug, Clone)]
pub struct RateTableEntry {
    pub hash: [u8; 16],
    pub last: f64,
    pub rate_violations: u32,
    pub blocked_until: f64,
    pub timestamps: Vec<f64>,
}

/// A blackholed identity for query responses.
#[derive(Debug, Clone)]
pub struct BlackholeInfo {
    pub identity_hash: [u8; 16],
    pub created: f64,
    pub expires: f64,
    pub reason: Option<String>,
}

/// Next hop lookup result.
#[derive(Debug, Clone)]
pub struct NextHopResponse {
    pub next_hop: [u8; 16],
    pub hops: u8,
    pub interface: InterfaceId,
}

impl<W: Send> fmt::Debug for Event<W> {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        match self {
            Event::Frame { interface_id, data } => f
                .debug_struct("Frame")
                .field("interface_id", interface_id)
                .field("data_len", &data.len())
                .finish(),
            Event::AnnounceVerified { key, .. } => f
                .debug_struct("AnnounceVerified")
                .field("destination_hash", &key.destination_hash)
                .field("received_from", &key.received_from)
                .finish(),
            Event::AnnounceVerifyFailed { key, .. } => f
                .debug_struct("AnnounceVerifyFailed")
                .field("destination_hash", &key.destination_hash)
                .field("received_from", &key.received_from)
                .finish(),
            Event::InterfaceUp(id, writer, info) => f
                .debug_tuple("InterfaceUp")
                .field(id)
                .field(&writer.is_some())
                .field(&info.is_some())
                .finish(),
            Event::InterfaceDown(id) => f.debug_tuple("InterfaceDown").field(id).finish(),
            Event::Tick => write!(f, "Tick"),
            Event::BeginDrain { timeout } => f
                .debug_struct("BeginDrain")
                .field("timeout", timeout)
                .finish(),
            Event::Shutdown => write!(f, "Shutdown"),
            Event::SendOutbound { raw, dest_type, .. } => f
                .debug_struct("SendOutbound")
                .field("raw_len", &raw.len())
                .field("dest_type", dest_type)
                .finish(),
            Event::RegisterDestination {
                dest_hash,
                dest_type,
            } => f
                .debug_struct("RegisterDestination")
                .field("dest_hash", dest_hash)
                .field("dest_type", dest_type)
                .finish(),
            Event::StoreSharedAnnounce {
                dest_hash,
                name_hash,
                app_data,
                ..
            } => f
                .debug_struct("StoreSharedAnnounce")
                .field("dest_hash", dest_hash)
                .field("name_hash", name_hash)
                .field("app_data_len", &app_data.as_ref().map(|d| d.len()))
                .finish(),
            Event::DeregisterDestination { dest_hash } => f
                .debug_struct("DeregisterDestination")
                .field("dest_hash", dest_hash)
                .finish(),
            Event::DeregisterLinkDestination { dest_hash } => f
                .debug_struct("DeregisterLinkDestination")
                .field("dest_hash", dest_hash)
                .finish(),
            Event::Query(req, _) => f.debug_tuple("Query").field(req).finish(),
            Event::RegisterLinkDestination { dest_hash, .. } => f
                .debug_struct("RegisterLinkDestination")
                .field("dest_hash", dest_hash)
                .finish(),
            Event::RegisterRequestHandler { path, .. } => f
                .debug_struct("RegisterRequestHandler")
                .field("path", path)
                .finish(),
            Event::CreateLink { dest_hash, .. } => f
                .debug_struct("CreateLink")
                .field("dest_hash", dest_hash)
                .finish(),
            Event::SendRequest { link_id, path, .. } => f
                .debug_struct("SendRequest")
                .field("link_id", link_id)
                .field("path", path)
                .finish(),
            Event::IdentifyOnLink { link_id, .. } => f
                .debug_struct("IdentifyOnLink")
                .field("link_id", link_id)
                .finish(),
            Event::TeardownLink { link_id } => f
                .debug_struct("TeardownLink")
                .field("link_id", link_id)
                .finish(),
            Event::SendResource { link_id, data, .. } => f
                .debug_struct("SendResource")
                .field("link_id", link_id)
                .field("data_len", &data.len())
                .finish(),
            Event::SetResourceStrategy { link_id, strategy } => f
                .debug_struct("SetResourceStrategy")
                .field("link_id", link_id)
                .field("strategy", strategy)
                .finish(),
            Event::AcceptResource {
                link_id, accept, ..
            } => f
                .debug_struct("AcceptResource")
                .field("link_id", link_id)
                .field("accept", accept)
                .finish(),
            Event::SendChannelMessage {
                link_id,
                msgtype,
                payload,
                ..
            } => f
                .debug_struct("SendChannelMessage")
                .field("link_id", link_id)
                .field("msgtype", msgtype)
                .field("payload_len", &payload.len())
                .finish(),
            Event::SendOnLink {
                link_id,
                data,
                context,
            } => f
                .debug_struct("SendOnLink")
                .field("link_id", link_id)
                .field("data_len", &data.len())
                .field("context", context)
                .finish(),
            Event::RequestPath { dest_hash } => f
                .debug_struct("RequestPath")
                .field("dest_hash", dest_hash)
                .finish(),
            Event::RegisterProofStrategy {
                dest_hash,
                strategy,
                ..
            } => f
                .debug_struct("RegisterProofStrategy")
                .field("dest_hash", dest_hash)
                .field("strategy", strategy)
                .finish(),
            Event::ProposeDirectConnect { link_id } => f
                .debug_struct("ProposeDirectConnect")
                .field("link_id", link_id)
                .finish(),
            Event::SetDirectConnectPolicy { .. } => {
                write!(f, "SetDirectConnectPolicy")
            }
            Event::HolePunchProbeResult {
                link_id,
                session_id,
                observed_addr,
                probe_server,
                ..
            } => f
                .debug_struct("HolePunchProbeResult")
                .field("link_id", link_id)
                .field("session_id", session_id)
                .field("observed_addr", observed_addr)
                .field("probe_server", probe_server)
                .finish(),
            Event::HolePunchProbeFailed {
                link_id,
                session_id,
            } => f
                .debug_struct("HolePunchProbeFailed")
                .field("link_id", link_id)
                .field("session_id", session_id)
                .finish(),
            Event::InterfaceConfigChanged(id) => {
                f.debug_tuple("InterfaceConfigChanged").field(id).finish()
            }
            Event::BackbonePeerConnected {
                server_interface_id,
                peer_interface_id,
                peer_ip,
                peer_port,
            } => f
                .debug_struct("BackbonePeerConnected")
                .field("server_interface_id", server_interface_id)
                .field("peer_interface_id", peer_interface_id)
                .field("peer_ip", peer_ip)
                .field("peer_port", peer_port)
                .finish(),
            Event::BackbonePeerDisconnected {
                server_interface_id,
                peer_interface_id,
                peer_ip,
                peer_port,
                connected_for,
                had_received_data,
            } => f
                .debug_struct("BackbonePeerDisconnected")
                .field("server_interface_id", server_interface_id)
                .field("peer_interface_id", peer_interface_id)
                .field("peer_ip", peer_ip)
                .field("peer_port", peer_port)
                .field("connected_for", connected_for)
                .field("had_received_data", had_received_data)
                .finish(),
            Event::BackbonePeerIdleTimeout {
                server_interface_id,
                peer_interface_id,
                peer_ip,
                peer_port,
                connected_for,
            } => f
                .debug_struct("BackbonePeerIdleTimeout")
                .field("server_interface_id", server_interface_id)
                .field("peer_interface_id", peer_interface_id)
                .field("peer_ip", peer_ip)
                .field("peer_port", peer_port)
                .field("connected_for", connected_for)
                .finish(),
            Event::BackbonePeerWriteStall {
                server_interface_id,
                peer_interface_id,
                peer_ip,
                peer_port,
                connected_for,
            } => f
                .debug_struct("BackbonePeerWriteStall")
                .field("server_interface_id", server_interface_id)
                .field("peer_interface_id", peer_interface_id)
                .field("peer_ip", peer_ip)
                .field("peer_port", peer_port)
                .field("connected_for", connected_for)
                .finish(),
            Event::BackbonePeerPenalty {
                server_interface_id,
                peer_ip,
                penalty_level,
                blacklist_for,
            } => f
                .debug_struct("BackbonePeerPenalty")
                .field("server_interface_id", server_interface_id)
                .field("peer_ip", peer_ip)
                .field("penalty_level", penalty_level)
                .field("blacklist_for", blacklist_for)
                .finish(),
            Event::LoadHook {
                name,
                attach_point,
                priority,
                ..
            } => f
                .debug_struct("LoadHook")
                .field("name", name)
                .field("attach_point", attach_point)
                .field("priority", priority)
                .finish(),
            Event::UnloadHook {
                name, attach_point, ..
            } => f
                .debug_struct("UnloadHook")
                .field("name", name)
                .field("attach_point", attach_point)
                .finish(),
            Event::ReloadHook {
                name, attach_point, ..
            } => f
                .debug_struct("ReloadHook")
                .field("name", name)
                .field("attach_point", attach_point)
                .finish(),
            Event::SetHookEnabled {
                name,
                attach_point,
                enabled,
                ..
            } => f
                .debug_struct("SetHookEnabled")
                .field("name", name)
                .field("attach_point", attach_point)
                .field("enabled", enabled)
                .finish(),
            Event::SetHookPriority {
                name,
                attach_point,
                priority,
                ..
            } => f
                .debug_struct("SetHookPriority")
                .field("name", name)
                .field("attach_point", attach_point)
                .field("priority", priority)
                .finish(),
            Event::ListHooks { .. } => write!(f, "ListHooks"),
        }
    }
}