nd300 3.7.0

Cross-platform network diagnostic tool
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
use serde::Serialize;

#[derive(Debug, Clone, Serialize)]
pub struct ProtocolStatistics {
    pub tcp: TcpStats,
    pub udp: UdpStats,
    pub icmp: IcmpStats,
}

#[derive(Debug, Clone, Serialize)]
pub struct TcpStats {
    pub active_opens: u64,
    pub passive_opens: u64,
    pub failed_connections: u64,
    pub reset_connections: u64,
    pub current_connections: u64,
    pub segments_received: u64,
    pub segments_sent: u64,
    pub segments_retransmitted: u64,
}

#[derive(Debug, Clone, Serialize)]
pub struct UdpStats {
    pub datagrams_received: u64,
    pub datagrams_sent: u64,
    pub receive_errors: u64,
    pub no_port_errors: u64,
}

#[derive(Debug, Clone, Serialize)]
pub struct IcmpStats {
    pub messages_received: u64,
    pub messages_sent: u64,
    pub errors_received: u64,
    pub errors_sent: u64,
}

pub async fn collect() -> Option<ProtocolStatistics> {
    #[cfg(windows)]
    {
        collect_windows().await
    }

    #[cfg(target_os = "macos")]
    {
        collect_macos().await
    }

    #[cfg(target_os = "linux")]
    {
        collect_linux().await
    }
}

#[cfg(windows)]
async fn collect_windows() -> Option<ProtocolStatistics> {
    let mut cmd = tokio::process::Command::new("netstat");
    cmd.args(["-s"]);
    let output = super::util::run_with_timeout(cmd, super::util::QUICK).await?;

    let text = String::from_utf8_lossy(&output.stdout);
    let mut tcp = TcpStats {
        active_opens: 0,
        passive_opens: 0,
        failed_connections: 0,
        reset_connections: 0,
        current_connections: 0,
        segments_received: 0,
        segments_sent: 0,
        segments_retransmitted: 0,
    };
    let mut udp = UdpStats {
        datagrams_received: 0,
        datagrams_sent: 0,
        receive_errors: 0,
        no_port_errors: 0,
    };
    let mut icmp = IcmpStats {
        messages_received: 0,
        messages_sent: 0,
        errors_received: 0,
        errors_sent: 0,
    };

    let mut section = "";
    for line in text.lines() {
        let line = line.trim();
        if line.contains("TCP Statistics") {
            section = "tcp";
            continue;
        }
        if line.contains("UDP Statistics") {
            section = "udp";
            continue;
        }
        if line.contains("ICMPv4 Statistics") || line.contains("ICMP Statistics") {
            section = "icmp";
            continue;
        }
        if line.contains("IPv4 Statistics") || line.contains("IPv6 Statistics") {
            section = "";
            continue;
        }

        let val = extract_stat_value(line).unwrap_or(0);

        match section {
            "tcp" => {
                if line.contains("Active Opens") {
                    tcp.active_opens = val;
                } else if line.contains("Passive Opens") {
                    tcp.passive_opens = val;
                } else if line.contains("Failed") {
                    tcp.failed_connections = val;
                } else if line.contains("Reset") && line.contains("Connection") {
                    tcp.reset_connections = val;
                } else if line.contains("Current") {
                    tcp.current_connections = val;
                } else if line.contains("Segments Received") {
                    tcp.segments_received = val;
                } else if line.contains("Segments Sent") && !line.contains("Re") {
                    tcp.segments_sent = val;
                } else if line.contains("Retransmit") {
                    tcp.segments_retransmitted = val;
                }
            }
            "udp" => {
                if line.contains("Datagrams Received") {
                    udp.datagrams_received = val;
                } else if line.contains("No Ports") {
                    udp.no_port_errors = val;
                } else if line.contains("Receive Errors") {
                    udp.receive_errors = val;
                } else if line.contains("Datagrams Sent") {
                    udp.datagrams_sent = val;
                }
            }
            "icmp" => {
                if line.contains("Messages") && line.contains("Received") {
                    icmp.messages_received = val;
                } else if line.contains("Messages") && line.contains("Sent") {
                    icmp.messages_sent = val;
                } else if line.contains("Errors") && line.contains("Received") {
                    icmp.errors_received = val;
                } else if line.contains("Errors") && line.contains("Sent") {
                    icmp.errors_sent = val;
                }
            }
            _ => {}
        }
    }

    Some(ProtocolStatistics { tcp, udp, icmp })
}

#[cfg(target_os = "macos")]
async fn collect_macos() -> Option<ProtocolStatistics> {
    let mut cmd = tokio::process::Command::new("netstat");
    cmd.args(["-s"]);
    let output = super::util::run_with_timeout(cmd, super::util::QUICK).await?;

    let text = String::from_utf8_lossy(&output.stdout);

    let mut stats = parse_macos_protocol_stats(&text)?;
    let mut connections_cmd = tokio::process::Command::new("netstat");
    connections_cmd.args(["-anW", "-p", "tcp"]);
    let connections = super::util::run_with_timeout(connections_cmd, super::util::QUICK).await?;
    stats.tcp.current_connections = String::from_utf8_lossy(&connections.stdout)
        .lines()
        .filter(|line| line.split_whitespace().last() == Some("ESTABLISHED"))
        .count() as u64;
    Some(stats)
}

#[cfg(any(target_os = "macos", test))]
fn parse_macos_protocol_stats(text: &str) -> Option<ProtocolStatistics> {
    let mut tcp = TcpStats {
        active_opens: 0,
        passive_opens: 0,
        failed_connections: 0,
        reset_connections: 0,
        current_connections: 0,
        segments_received: 0,
        segments_sent: 0,
        segments_retransmitted: 0,
    };
    let mut udp = UdpStats {
        datagrams_received: 0,
        datagrams_sent: 0,
        receive_errors: 0,
        no_port_errors: 0,
    };
    let mut icmp = IcmpStats {
        messages_received: 0,
        messages_sent: 0,
        errors_received: 0,
        errors_sent: 0,
    };

    let mut section = "";
    let mut icmp_input_histogram = false;
    let mut icmp_input_total = 0u64;
    for line in text.lines() {
        let trimmed = line.trim();
        if trimmed == "tcp:" {
            section = "tcp";
            continue;
        }
        if trimmed == "udp:" {
            section = "udp";
            continue;
        }
        if trimmed == "icmp:" {
            section = "icmp";
            icmp_input_histogram = false;
            continue;
        }
        if trimmed.ends_with(':') && !trimmed.contains(' ') {
            section = "";
            continue;
        }

        let val = extract_leading_number(trimmed).unwrap_or(0);

        match section {
            "tcp" => {
                if trimmed.contains("connection request") {
                    tcp.active_opens = val;
                } else if trimmed.contains("connection accept") {
                    tcp.passive_opens = val;
                } else if trimmed.contains("bad connection") {
                    tcp.failed_connections = val;
                } else if trimmed.ends_with("bad reset") {
                    tcp.reset_connections = val;
                } else if trimmed.contains("data packet") && trimmed.contains("retransmitted") {
                    tcp.segments_retransmitted = val;
                } else if trimmed.ends_with("packet sent") || trimmed.ends_with("packets sent") {
                    tcp.segments_sent = val;
                } else if trimmed.ends_with("packet received")
                    || trimmed.ends_with("packets received")
                {
                    tcp.segments_received = val;
                }
            }
            "udp" => {
                if trimmed.contains("datagram") && trimmed.contains("received") {
                    udp.datagrams_received = val;
                } else if trimmed.contains("datagram") && trimmed.contains("output") {
                    udp.datagrams_sent = val;
                } else if trimmed.contains("dropped due to no socket") {
                    udp.no_port_errors = val;
                } else if trimmed.contains("incomplete header")
                    || trimmed.contains("bad data length")
                    || trimmed.contains("bad checksum")
                    || trimmed.contains("full socket buffers")
                {
                    udp.receive_errors = udp.receive_errors.saturating_add(val);
                }
            }
            "icmp" => {
                if trimmed == "Input histogram:" {
                    icmp_input_histogram = true;
                } else if trimmed.ends_with("calls to icmp_error") {
                    icmp.errors_sent = val;
                    icmp.messages_sent = icmp.messages_sent.saturating_add(val);
                } else if trimmed.ends_with("message response generated") {
                    icmp.messages_sent = icmp.messages_sent.saturating_add(val);
                } else if icmp_input_histogram && trimmed.contains(':') {
                    let histogram_value = trimmed
                        .rsplit_once(':')
                        .and_then(|(_, value)| value.trim().parse::<u64>().ok())
                        .unwrap_or(0);
                    icmp_input_total = icmp_input_total.saturating_add(histogram_value);
                } else if trimmed.contains("bad code")
                    || trimmed.contains("bad checksum")
                    || trimmed.contains("bad length")
                    || trimmed.contains("minimum length")
                {
                    icmp.errors_received = icmp.errors_received.saturating_add(val);
                }
            }
            _ => {}
        }
    }
    icmp.messages_received = icmp_input_total;

    // Current macOS releases can expose a populated TCP connection table while
    // `netstat -s` returns an all-zero TCP block. The public schema uses plain
    // integers, so serializing that block would claim measured zeroes. Omit
    // this optional technician section until the kernel supplies counters.
    let tcp_available = tcp.active_opens > 0
        || tcp.passive_opens > 0
        || tcp.failed_connections > 0
        || tcp.reset_connections > 0
        || tcp.current_connections > 0
        || tcp.segments_received > 0
        || tcp.segments_sent > 0
        || tcp.segments_retransmitted > 0;
    tcp_available.then_some(ProtocolStatistics { tcp, udp, icmp })
}

#[cfg(target_os = "linux")]
async fn collect_linux() -> Option<ProtocolStatistics> {
    let mut tcp = TcpStats {
        active_opens: 0,
        passive_opens: 0,
        failed_connections: 0,
        reset_connections: 0,
        current_connections: 0,
        segments_received: 0,
        segments_sent: 0,
        segments_retransmitted: 0,
    };
    let mut udp = UdpStats {
        datagrams_received: 0,
        datagrams_sent: 0,
        receive_errors: 0,
        no_port_errors: 0,
    };
    let mut icmp = IcmpStats {
        messages_received: 0,
        messages_sent: 0,
        errors_received: 0,
        errors_sent: 0,
    };

    // Read /proc/net/snmp
    if let Ok(content) = tokio::fs::read_to_string("/proc/net/snmp").await {
        let lines: Vec<&str> = content.lines().collect();
        for i in (0..lines.len()).step_by(2) {
            if i + 1 >= lines.len() {
                break;
            }
            let headers: Vec<&str> = lines[i].split_whitespace().collect();
            let values: Vec<&str> = lines[i + 1].split_whitespace().collect();

            if headers.first() == Some(&"Tcp:") && headers.len() == values.len() {
                for (j, header) in headers.iter().enumerate() {
                    let val: u64 = values.get(j).and_then(|s| s.parse().ok()).unwrap_or(0);
                    match *header {
                        "ActiveOpens" => tcp.active_opens = val,
                        "PassiveOpens" => tcp.passive_opens = val,
                        "AttemptFails" => tcp.failed_connections = val,
                        "EstabResets" => tcp.reset_connections = val,
                        "CurrEstab" => tcp.current_connections = val,
                        "InSegs" => tcp.segments_received = val,
                        "OutSegs" => tcp.segments_sent = val,
                        "RetransSegs" => tcp.segments_retransmitted = val,
                        _ => {}
                    }
                }
            } else if headers.first() == Some(&"Udp:") && headers.len() == values.len() {
                for (j, header) in headers.iter().enumerate() {
                    let val: u64 = values.get(j).and_then(|s| s.parse().ok()).unwrap_or(0);
                    match *header {
                        "InDatagrams" => udp.datagrams_received = val,
                        "OutDatagrams" => udp.datagrams_sent = val,
                        "InErrors" => udp.receive_errors = val,
                        "NoPorts" => udp.no_port_errors = val,
                        _ => {}
                    }
                }
            } else if headers.first() == Some(&"Icmp:") && headers.len() == values.len() {
                for (j, header) in headers.iter().enumerate() {
                    let val: u64 = values.get(j).and_then(|s| s.parse().ok()).unwrap_or(0);
                    match *header {
                        "InMsgs" => icmp.messages_received = val,
                        "OutMsgs" => icmp.messages_sent = val,
                        "InErrors" => icmp.errors_received = val,
                        "OutErrors" => icmp.errors_sent = val,
                        _ => {}
                    }
                }
            }
        }
    }

    Some(ProtocolStatistics { tcp, udp, icmp })
}

#[cfg(windows)]
fn extract_stat_value(line: &str) -> Option<u64> {
    // "  Active Opens              = 12345"
    if let Some(pos) = line.find('=') {
        let after = line[pos + 1..].trim();
        return after.parse().ok();
    }
    None
}

#[cfg(any(target_os = "macos", test))]
fn extract_leading_number(line: &str) -> Option<u64> {
    let num_str: String = line.chars().take_while(|c| c.is_ascii_digit()).collect();
    num_str.parse().ok()
}

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

    #[test]
    fn current_macos_udp_wording_and_icmp_histogram_parse() {
        let fixture = "tcp:\n\t12 packets sent\n\t3 data packets retransmitted\n\t15 packets received\n\t2 connection requests\n\t1 connection accept\nudp:\n\t409241 datagrams received\n\t\t7 dropped due to no socket\n\t\t3 dropped due to full socket buffers\n\t104267 datagrams output\nicmp:\n\t5 calls to icmp_error\n\tInput histogram:\n\t\techo reply: 8\n\t\tdestination unreachable: 2\n\t1 message response generated\n";
        let stats = parse_macos_protocol_stats(fixture).unwrap();
        assert_eq!(stats.tcp.segments_sent, 12);
        assert_eq!(stats.tcp.segments_retransmitted, 3);
        assert_eq!(stats.udp.datagrams_sent, 104267);
        assert_eq!(stats.udp.no_port_errors, 7);
        assert_eq!(stats.udp.receive_errors, 3);
        assert_eq!(stats.icmp.messages_received, 10);
        assert_eq!(stats.icmp.messages_sent, 6);
    }

    #[test]
    fn suppressed_macos_tcp_counters_omit_optional_section() {
        let fixture = "tcp:\n\t0 packet sent\n\t0 packet received\n\t0 connection request\n\t0 connection accept\nudp:\n\t12 datagrams received\n\t9 datagrams output\n";
        assert!(parse_macos_protocol_stats(fixture).is_none());
    }
}