nlink 0.13.0

Async netlink library for Linux network configuration
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
//! Core types for socket diagnostics.
//!
//! This module provides strongly-typed representations of socket states,
//! address families, protocols, and diagnostic information.

use std::fmt;

use serde::{Deserialize, Serialize};

/// Socket address family.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Serialize, Deserialize)]
#[repr(u8)]
pub enum AddressFamily {
    /// Unix domain sockets.
    Unix = libc::AF_UNIX as u8,
    /// IPv4.
    Inet = libc::AF_INET as u8,
    /// IPv6.
    Inet6 = libc::AF_INET6 as u8,
    /// Netlink.
    Netlink = libc::AF_NETLINK as u8,
    /// Packet (raw).
    Packet = libc::AF_PACKET as u8,
    /// VSOCK (virtual machine sockets).
    Vsock = 40, // AF_VSOCK
    /// TIPC.
    Tipc = 30, // AF_TIPC
    /// XDP (eXpress Data Path).
    Xdp = 44, // AF_XDP
}

impl AddressFamily {
    /// Parse from a raw u8 value.
    pub fn from_u8(value: u8) -> Option<Self> {
        match value as i32 {
            libc::AF_UNIX => Some(Self::Unix),
            libc::AF_INET => Some(Self::Inet),
            libc::AF_INET6 => Some(Self::Inet6),
            libc::AF_NETLINK => Some(Self::Netlink),
            libc::AF_PACKET => Some(Self::Packet),
            40 => Some(Self::Vsock),
            30 => Some(Self::Tipc),
            44 => Some(Self::Xdp),
            _ => None,
        }
    }

    /// Get the netid string (used by ss).
    pub fn netid(&self) -> &'static str {
        match self {
            Self::Unix => "u_str",
            Self::Inet => "tcp",
            Self::Inet6 => "tcp6",
            Self::Netlink => "nl",
            Self::Packet => "p_raw",
            Self::Vsock => "v_str",
            Self::Tipc => "tipc",
            Self::Xdp => "xdp",
        }
    }
}

/// Transport protocol.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Serialize, Deserialize)]
pub enum Protocol {
    /// TCP.
    Tcp,
    /// UDP.
    Udp,
    /// SCTP.
    Sctp,
    /// DCCP.
    Dccp,
    /// MPTCP.
    Mptcp,
    /// Raw IP.
    Raw,
}

impl Protocol {
    /// Get the protocol number.
    pub fn number(&self) -> u8 {
        match self {
            Self::Tcp => libc::IPPROTO_TCP as u8,
            Self::Udp => libc::IPPROTO_UDP as u8,
            Self::Sctp => 132,
            Self::Dccp => 33,
            Self::Mptcp => 6, // Uses TCP protocol number in inet_diag
            Self::Raw => libc::IPPROTO_RAW as u8,
        }
    }

    /// Parse from a raw u8 value.
    pub fn from_u8(value: u8) -> Option<Self> {
        match value as i32 {
            libc::IPPROTO_TCP => Some(Self::Tcp),
            libc::IPPROTO_UDP => Some(Self::Udp),
            132 => Some(Self::Sctp),
            33 => Some(Self::Dccp),
            libc::IPPROTO_RAW => Some(Self::Raw),
            _ => None,
        }
    }

    /// Get the protocol name.
    pub fn name(&self) -> &'static str {
        match self {
            Self::Tcp => "tcp",
            Self::Udp => "udp",
            Self::Sctp => "sctp",
            Self::Dccp => "dccp",
            Self::Mptcp => "mptcp",
            Self::Raw => "raw",
        }
    }
}

/// TCP socket states.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Serialize, Deserialize)]
#[repr(u8)]
pub enum TcpState {
    /// Unknown state.
    Unknown = 0,
    /// Connection established.
    Established = 1,
    /// SYN sent, waiting for matching SYN.
    SynSent = 2,
    /// SYN received, waiting for ACK.
    SynRecv = 3,
    /// FIN sent, waiting for FIN or FIN-ACK.
    FinWait1 = 4,
    /// FIN received, waiting for FIN.
    FinWait2 = 5,
    /// In TIME-WAIT state.
    TimeWait = 6,
    /// Socket is closed.
    Close = 7,
    /// FIN received, close pending.
    CloseWait = 8,
    /// Close wait acknowledged, waiting for FIN.
    LastAck = 9,
    /// Socket is listening.
    Listen = 10,
    /// Both sides sent FIN simultaneously.
    Closing = 11,
    /// New SYN received (kernel only).
    NewSynRecv = 12,
    /// Bound but inactive (MPTCP).
    BoundInactive = 13,
}

impl TcpState {
    /// Parse from a raw u8 value.
    pub fn from_u8(value: u8) -> Self {
        match value {
            1 => Self::Established,
            2 => Self::SynSent,
            3 => Self::SynRecv,
            4 => Self::FinWait1,
            5 => Self::FinWait2,
            6 => Self::TimeWait,
            7 => Self::Close,
            8 => Self::CloseWait,
            9 => Self::LastAck,
            10 => Self::Listen,
            11 => Self::Closing,
            12 => Self::NewSynRecv,
            13 => Self::BoundInactive,
            _ => Self::Unknown,
        }
    }

    /// Get the state name.
    pub fn name(&self) -> &'static str {
        match self {
            Self::Unknown => "UNKNOWN",
            Self::Established => "ESTAB",
            Self::SynSent => "SYN-SENT",
            Self::SynRecv => "SYN-RECV",
            Self::FinWait1 => "FIN-WAIT-1",
            Self::FinWait2 => "FIN-WAIT-2",
            Self::TimeWait => "TIME-WAIT",
            Self::Close => "UNCONN",
            Self::CloseWait => "CLOSE-WAIT",
            Self::LastAck => "LAST-ACK",
            Self::Listen => "LISTEN",
            Self::Closing => "CLOSING",
            Self::NewSynRecv => "NEW-SYN-RECV",
            Self::BoundInactive => "BOUND-INACTIVE",
        }
    }

    /// Create a bitmask for this state.
    pub fn mask(&self) -> u32 {
        1 << (*self as u32)
    }

    /// All connection states (excludes LISTEN, CLOSE, TIME-WAIT, SYN-RECV).
    pub fn connected_mask() -> u32 {
        Self::all_mask()
            & !(Self::Listen.mask()
                | Self::Close.mask()
                | Self::TimeWait.mask()
                | Self::SynRecv.mask())
    }

    /// All states mask.
    pub fn all_mask() -> u32 {
        (1 << 14) - 1
    }
}

/// Generic socket state (for non-TCP sockets).
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Serialize, Deserialize)]
pub enum SocketState {
    /// TCP state (for TCP/MPTCP/DCCP sockets).
    Tcp(TcpState),
    /// Closed (for UDP, Unix, etc.).
    Close,
    /// Established (for UDP, Unix stream).
    Established,
    /// Listening (for Unix stream/seqpacket).
    Listen,
}

impl SocketState {
    /// Get the state name.
    pub fn name(&self) -> &'static str {
        match self {
            Self::Tcp(state) => state.name(),
            Self::Close => "UNCONN",
            Self::Established => "ESTAB",
            Self::Listen => "LISTEN",
        }
    }
}

/// Extensions to request in inet_diag query.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
#[repr(u8)]
pub enum InetExtension {
    /// Memory info (idiag_rmem, idiag_wmem, etc.).
    MemInfo = 1,
    /// TCP info structure.
    Info = 2,
    /// Vegas congestion info.
    VegasInfo = 3,
    /// Congestion algorithm name.
    Cong = 4,
    /// Type of service.
    Tos = 5,
    /// Traffic class (IPv6).
    TClass = 6,
    /// Socket memory info array.
    SkMemInfo = 7,
    /// Shutdown state.
    Shutdown = 8,
}

impl InetExtension {
    /// Get the bitmask for this extension.
    pub fn mask(&self) -> u8 {
        1 << (*self as u8)
    }
}

/// What to show in Unix socket queries.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
#[repr(u32)]
pub enum UnixShow {
    /// Show socket name.
    Name = 0x00000001,
    /// Show VFS inode info.
    Vfs = 0x00000002,
    /// Show peer socket info.
    Peer = 0x00000004,
    /// Show pending connections.
    Icons = 0x00000008,
    /// Show receive queue length.
    RqLen = 0x00000010,
    /// Show memory info.
    MemInfo = 0x00000020,
    /// Show socket UID.
    Uid = 0x00000040,
}

impl UnixShow {
    /// Get the bitmask for this show option.
    pub fn mask(&self) -> u32 {
        *self as u32
    }

    /// Combine multiple show options into a bitmask.
    pub fn combine(options: &[UnixShow]) -> u32 {
        options.iter().fold(0, |acc, opt| acc | opt.mask())
    }
}

/// TCP information structure (from tcp_info).
#[derive(Debug, Clone, Default, Serialize, Deserialize)]
pub struct TcpInfo {
    /// State.
    pub state: u8,
    /// CA state.
    pub ca_state: u8,
    /// Retransmits.
    pub retransmits: u8,
    /// Probes.
    pub probes: u8,
    /// Backoff.
    pub backoff: u8,
    /// Options.
    pub options: u8,
    /// Send/receive window scale.
    pub wscale: u8,
    /// Delivery rate app limited flag.
    pub delivery_rate_app_limited: bool,

    /// Retransmit timeout (usec).
    pub rto: u32,
    /// Estimated RTT (usec).
    pub rtt: u32,
    /// RTT variance (usec).
    pub rttvar: u32,
    /// Send MSS.
    pub snd_mss: u32,
    /// Receive MSS.
    pub rcv_mss: u32,

    /// Unacked packets.
    pub unacked: u32,
    /// Sacked packets.
    pub sacked: u32,
    /// Lost packets.
    pub lost: u32,
    /// Retransmitted packets.
    pub retrans: u32,
    /// Forward acknowledged packets.
    pub fackets: u32,

    /// Last data sent timestamp.
    pub last_data_sent: u32,
    /// Last ACK sent timestamp.
    pub last_ack_sent: u32,
    /// Last data received timestamp.
    pub last_data_recv: u32,
    /// Last ACK received timestamp.
    pub last_ack_recv: u32,

    /// Path MTU.
    pub pmtu: u32,
    /// Receive SSTHRESH.
    pub rcv_ssthresh: u32,
    /// Send SSTHRESH.
    pub snd_ssthresh: u32,
    /// Send CWND.
    pub snd_cwnd: u32,
    /// Advertised MSS.
    pub advmss: u32,
    /// Reordering.
    pub reordering: u32,

    /// Receive RTT (usec).
    pub rcv_rtt: u32,
    /// Receive space.
    pub rcv_space: u32,

    /// Total retransmits.
    pub total_retrans: u32,

    /// Pacing rate (bytes/sec).
    pub pacing_rate: u64,
    /// Max pacing rate (bytes/sec).
    pub max_pacing_rate: u64,
    /// Bytes ACKed.
    pub bytes_acked: u64,
    /// Bytes received.
    pub bytes_received: u64,
    /// Segments out.
    pub segs_out: u32,
    /// Segments in.
    pub segs_in: u32,

    /// Not sent bytes.
    pub notsent_bytes: u32,
    /// Minimum RTT (usec).
    pub min_rtt: u32,
    /// Data segments in.
    pub data_segs_in: u32,
    /// Data segments out.
    pub data_segs_out: u32,

    /// Delivery rate (bytes/sec).
    pub delivery_rate: u64,

    /// Busy time (usec).
    pub busy_time: u64,
    /// RWnd limited time (usec).
    pub rwnd_limited: u64,
    /// Sndbuf limited time (usec).
    pub sndbuf_limited: u64,

    /// Delivered packets.
    pub delivered: u32,
    /// Delivered with CE mark.
    pub delivered_ce: u32,

    /// Bytes sent.
    pub bytes_sent: u64,
    /// Bytes retransmitted.
    pub bytes_retrans: u64,
    /// Duplicate SACKs received.
    pub dsack_dups: u32,
    /// Reordering seen.
    pub reord_seen: u32,

    /// Receive out-of-order rate.
    pub rcv_ooopack: u32,
    /// Send window.
    pub snd_wnd: u32,
}

impl TcpInfo {
    /// Format as ss-style string with all non-zero metrics.
    ///
    /// This produces output similar to `ss -i`:
    /// ```text
    /// rtt:0.123/0.050 ato:40 mss:1448 cwnd:10 ssthresh:7 bytes_acked:12345
    /// ```
    pub fn format_ss(&self) -> String {
        let mut parts = Vec::new();

        // RTT (in milliseconds with 3 decimal places)
        if self.rtt > 0 {
            let rtt_ms = self.rtt as f64 / 1000.0;
            let rttvar_ms = self.rttvar as f64 / 1000.0;
            parts.push(format!("rtt:{:.3}/{:.3}", rtt_ms, rttvar_ms));
        }

        // RTO
        if self.rto > 0 && self.rto != 200_000 {
            // Skip default RTO
            parts.push(format!("rto:{}", self.rto / 1000));
        }

        // MSS
        if self.snd_mss > 0 {
            parts.push(format!("mss:{}", self.snd_mss));
        }

        // Congestion window
        if self.snd_cwnd > 0 {
            parts.push(format!("cwnd:{}", self.snd_cwnd));
        }

        // Slow-start threshold
        if self.snd_ssthresh > 0 && self.snd_ssthresh < u32::MAX {
            parts.push(format!("ssthresh:{}", self.snd_ssthresh));
        }

        // Bytes metrics
        if self.bytes_acked > 0 {
            parts.push(format!("bytes_acked:{}", self.bytes_acked));
        }
        if self.bytes_received > 0 {
            parts.push(format!("bytes_received:{}", self.bytes_received));
        }

        // Segments
        if self.segs_out > 0 {
            parts.push(format!("segs_out:{}", self.segs_out));
        }
        if self.segs_in > 0 {
            parts.push(format!("segs_in:{}", self.segs_in));
        }

        // Retransmissions
        if self.retrans > 0 {
            parts.push(format!("retrans:{}/{}", self.retrans, self.total_retrans));
        } else if self.total_retrans > 0 {
            parts.push(format!("retrans:0/{}", self.total_retrans));
        }

        // Delivery rate
        if self.delivery_rate > 0 {
            parts.push(format!(
                "delivery_rate:{}{}",
                format_rate_bps(self.delivery_rate * 8),
                if self.delivery_rate_app_limited {
                    "app_limited"
                } else {
                    ""
                }
            ));
        }

        // Pacing rate
        if self.pacing_rate > 0 && self.pacing_rate < u64::MAX {
            parts.push(format!(
                "pacing_rate:{}",
                format_rate_bps(self.pacing_rate * 8)
            ));
        }

        // Minimum RTT
        if self.min_rtt > 0 {
            parts.push(format!("minrtt:{:.3}", self.min_rtt as f64 / 1000.0));
        }

        // Receive window
        if self.rcv_space > 0 {
            parts.push(format!("rcv_space:{}", self.rcv_space));
        }

        parts.join(" ")
    }

    /// Format RTT as "rtt/rttvar" in milliseconds.
    pub fn rtt_str(&self) -> String {
        if self.rtt > 0 {
            format!(
                "{:.3}/{:.3}",
                self.rtt as f64 / 1000.0,
                self.rttvar as f64 / 1000.0
            )
        } else {
            String::new()
        }
    }

    /// Format congestion window.
    pub fn cwnd_str(&self) -> String {
        if self.snd_cwnd > 0 {
            format!("{}", self.snd_cwnd)
        } else {
            String::new()
        }
    }

    /// Format delivery rate as human-readable string.
    pub fn delivery_rate_str(&self) -> String {
        if self.delivery_rate > 0 {
            format_rate_bps(self.delivery_rate * 8)
        } else {
            String::new()
        }
    }

    /// Format pacing rate as human-readable string.
    pub fn pacing_rate_str(&self) -> String {
        if self.pacing_rate > 0 && self.pacing_rate < u64::MAX {
            format_rate_bps(self.pacing_rate * 8)
        } else {
            String::new()
        }
    }

    /// Get the send window scale (high nibble of wscale).
    pub fn snd_wscale(&self) -> u8 {
        self.wscale >> 4
    }

    /// Get the receive window scale (low nibble of wscale).
    pub fn rcv_wscale(&self) -> u8 {
        self.wscale & 0x0f
    }
}

/// Format bits per second as human-readable string.
fn format_rate_bps(bps: u64) -> String {
    if bps >= 1_000_000_000 {
        format!("{:.1}Gbps", bps as f64 / 1_000_000_000.0)
    } else if bps >= 1_000_000 {
        format!("{:.1}Mbps", bps as f64 / 1_000_000.0)
    } else if bps >= 1_000 {
        format!("{:.1}Kbps", bps as f64 / 1_000.0)
    } else {
        format!("{}bps", bps)
    }
}

/// Socket memory information.
#[derive(Debug, Clone, Default, Serialize, Deserialize)]
pub struct MemInfo {
    /// Receive memory allocated.
    pub rmem_alloc: u32,
    /// Receive buffer size.
    pub rcvbuf: u32,
    /// Write memory allocated.
    pub wmem_alloc: u32,
    /// Send buffer size.
    pub sndbuf: u32,
    /// Forward alloc.
    pub fwd_alloc: u32,
    /// Write memory queued.
    pub wmem_queued: u32,
    /// Option memory.
    pub optmem: u32,
    /// Backlog.
    pub backlog: u32,
    /// Drops.
    pub drops: u32,
}

impl MemInfo {
    /// Format as skmem style string.
    ///
    /// This produces output like ss's skmem() format:
    /// ```text
    /// skmem:(r0,rb131072,t0,tb16384,f0,w0,o0,bl0,d0)
    /// ```
    pub fn format_skmem(&self) -> String {
        format!(
            "skmem:(r{},rb{},t{},tb{},f{},w{},o{},bl{},d{})",
            self.rmem_alloc,
            self.rcvbuf,
            self.wmem_alloc,
            self.sndbuf,
            self.fwd_alloc,
            self.wmem_queued,
            self.optmem,
            self.backlog,
            self.drops
        )
    }

    /// Format as compact skmem string (only non-zero values).
    pub fn format_skmem_compact(&self) -> String {
        let mut parts = Vec::new();

        if self.rmem_alloc > 0 {
            parts.push(format!("r{}", self.rmem_alloc));
        }
        if self.rcvbuf > 0 {
            parts.push(format!("rb{}", self.rcvbuf));
        }
        if self.wmem_alloc > 0 {
            parts.push(format!("t{}", self.wmem_alloc));
        }
        if self.sndbuf > 0 {
            parts.push(format!("tb{}", self.sndbuf));
        }
        if self.fwd_alloc > 0 {
            parts.push(format!("f{}", self.fwd_alloc));
        }
        if self.wmem_queued > 0 {
            parts.push(format!("w{}", self.wmem_queued));
        }
        if self.optmem > 0 {
            parts.push(format!("o{}", self.optmem));
        }
        if self.backlog > 0 {
            parts.push(format!("bl{}", self.backlog));
        }
        if self.drops > 0 {
            parts.push(format!("d{}", self.drops));
        }

        if parts.is_empty() {
            "skmem:()".to_string()
        } else {
            format!("skmem:({})", parts.join(","))
        }
    }

    /// Check if there are any drops.
    pub fn has_drops(&self) -> bool {
        self.drops > 0
    }

    /// Total memory in use (rmem + wmem).
    pub fn total_mem(&self) -> u64 {
        self.rmem_alloc as u64 + self.wmem_alloc as u64
    }
}

/// Timer information.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
pub enum Timer {
    /// No timer active.
    Off,
    /// Retransmit timer.
    On { expires_ms: u32, retrans: u8 },
    /// Keepalive timer.
    Keepalive { expires_ms: u32, probes: u8 },
    /// TIME-WAIT timer.
    TimeWait { expires_ms: u32 },
    /// Zero window probe timer.
    Probe { expires_ms: u32, retrans: u8 },
}

impl Timer {
    /// Parse timer info from idiag_timer, idiag_expires, idiag_retrans.
    pub fn from_raw(timer: u8, expires: u32, retrans: u8) -> Self {
        match timer {
            0 => Self::Off,
            1 => Self::On {
                expires_ms: expires,
                retrans,
            },
            2 => Self::Keepalive {
                expires_ms: expires,
                probes: retrans,
            },
            3 => Self::TimeWait {
                expires_ms: expires,
            },
            4 => Self::Probe {
                expires_ms: expires,
                retrans,
            },
            _ => Self::Off,
        }
    }
}

/// Aggregated socket statistics across all families.
///
/// # Example
///
/// ```ignore
/// use nlink::netlink::{Connection, SockDiag};
///
/// let conn = Connection::<SockDiag>::new()?;
/// let summary = conn.socket_summary().await?;
/// println!("{}", summary);
/// ```
#[derive(Debug, Clone, Default, Serialize, Deserialize)]
pub struct SocketSummary {
    /// TCP socket summary.
    pub tcp: TcpSummary,
    /// Total UDP sockets.
    pub udp: u32,
    /// Total raw sockets.
    pub raw: u32,
    /// Total Unix domain sockets.
    pub unix: u32,
}

impl fmt::Display for SocketSummary {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        let total = self.tcp.total + self.udp + self.raw + self.unix;
        writeln!(f, "Total: {total}")?;
        writeln!(
            f,
            "TCP:   {} (estab {}, closed {}, orphaned 0, timewait {})",
            self.tcp.total, self.tcp.established, self.tcp.close, self.tcp.time_wait
        )?;
        writeln!(f, "UDP:   {}", self.udp)?;
        writeln!(f, "RAW:   {}", self.raw)?;
        write!(f, "UNIX:  {}", self.unix)
    }
}

/// Result of a batch socket destruction operation.
///
/// Contains the count of successfully destroyed sockets and any errors.
///
/// # Example
///
/// ```ignore
/// let result = conn.destroy_matching(&filter).await?;
/// println!("Destroyed {} sockets", result.destroyed);
/// for err in &result.errors {
///     eprintln!("Failed to destroy {:?}: {}", err.socket, err.error);
/// }
/// ```
#[derive(Debug)]
pub struct DestroyResult {
    /// Number of sockets successfully destroyed.
    pub destroyed: u32,
    /// Errors encountered during destruction.
    pub errors: Vec<DestroyError>,
}

impl DestroyResult {
    /// True if all sockets were destroyed successfully.
    pub fn all_ok(&self) -> bool {
        self.errors.is_empty()
    }
}

/// Error from destroying a specific socket.
#[derive(Debug)]
pub struct DestroyError {
    /// The socket that failed to be destroyed (local -> remote).
    pub socket: std::net::SocketAddr,
    /// The error that occurred.
    pub error: crate::netlink::Error,
}

/// TCP socket statistics broken down by state.
#[derive(Debug, Clone, Default, Serialize, Deserialize)]
pub struct TcpSummary {
    /// Total TCP sockets.
    pub total: u32,
    /// ESTABLISHED state.
    pub established: u32,
    /// SYN-SENT state.
    pub syn_sent: u32,
    /// SYN-RECV state.
    pub syn_recv: u32,
    /// FIN-WAIT-1 state.
    pub fin_wait1: u32,
    /// FIN-WAIT-2 state.
    pub fin_wait2: u32,
    /// TIME-WAIT state.
    pub time_wait: u32,
    /// CLOSE state.
    pub close: u32,
    /// CLOSE-WAIT state.
    pub close_wait: u32,
    /// LAST-ACK state.
    pub last_ack: u32,
    /// LISTEN state.
    pub listen: u32,
    /// CLOSING state.
    pub closing: u32,
}

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

    #[test]
    fn default_socket_summary_is_all_zeros() {
        let summary = SocketSummary::default();
        assert_eq!(summary.tcp.total, 0);
        assert_eq!(summary.tcp.established, 0);
        assert_eq!(summary.tcp.syn_sent, 0);
        assert_eq!(summary.tcp.syn_recv, 0);
        assert_eq!(summary.tcp.fin_wait1, 0);
        assert_eq!(summary.tcp.fin_wait2, 0);
        assert_eq!(summary.tcp.time_wait, 0);
        assert_eq!(summary.tcp.close, 0);
        assert_eq!(summary.tcp.close_wait, 0);
        assert_eq!(summary.tcp.last_ack, 0);
        assert_eq!(summary.tcp.listen, 0);
        assert_eq!(summary.tcp.closing, 0);
        assert_eq!(summary.udp, 0);
        assert_eq!(summary.raw, 0);
        assert_eq!(summary.unix, 0);
    }

    #[test]
    fn display_format_matches_expected_output() {
        let summary = SocketSummary {
            tcp: TcpSummary {
                total: 15,
                established: 8,
                syn_sent: 1,
                syn_recv: 0,
                fin_wait1: 0,
                fin_wait2: 1,
                time_wait: 3,
                close: 2,
                close_wait: 0,
                last_ack: 0,
                listen: 0,
                closing: 0,
            },
            udp: 5,
            raw: 1,
            unix: 10,
        };

        let output = format!("{}", summary);
        let lines: Vec<&str> = output.lines().collect();

        assert_eq!(lines.len(), 5);
        assert_eq!(lines[0], "Total: 31");
        assert_eq!(
            lines[1],
            "TCP:   15 (estab 8, closed 2, orphaned 0, timewait 3)"
        );
        assert_eq!(lines[2], "UDP:   5");
        assert_eq!(lines[3], "RAW:   1");
        assert_eq!(lines[4], "UNIX:  10");
    }

    #[test]
    fn display_total_is_sum_of_all_socket_types() {
        let summary = SocketSummary {
            tcp: TcpSummary {
                total: 10,
                ..Default::default()
            },
            udp: 20,
            raw: 3,
            unix: 7,
        };

        let output = format!("{}", summary);
        let first_line = output.lines().next().unwrap();
        assert_eq!(first_line, "Total: 40");
    }
}