openntpd-rs-ctl 0.9.2

ntpctl control client binary for openntpd-rs
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
//! # openntpd-rs-ctl
//!
//! Clean-room, blackbox forensic Rust reconstruction of OpenNTPD's
//! `ntpctl` control client.  CLI: ntpctl(8).
//!
//! Provides control-socket communication over the imsg protocol,
//! response parsing, and output formatting matching real `ntpctl -s`.
//!
//! ## Architecture
//!
//! ```text
//! [ntpctl CLI] → ControlClient → Unix socket → ntpd daemon
//! ```
//!
//! The [`ControlClient`] struct manages the connection lifecycle.
//! Use [`parse_target`] for CLI input processing, then call
//! [`ControlClient::query`] and format with the `print_*` functions.

use std::io::{Read, Write};
use std::os::unix::net::UnixStream;
use std::path::Path;
use std::time::Duration;

use openntpd_rs_core::control::{
    ControlRequest, NtpdStatus, PeerInfo, SensorInfo, SyncState, CTL_REQ_ALL, CTL_REQ_PEERS,
    CTL_REQ_SENSORS, CTL_REQ_STATUS,
};
use openntpd_rs_io::imsg::{Imsg, ImsgHeader, IMSG_CTL_REQ};

// ---------------------------------------------------------------------------
// Constants
// ---------------------------------------------------------------------------

/// Default control socket path matching OpenNTPD convention.
pub const DEFAULT_CONTROL_SOCKET: &str = "/var/run/ntpd.sock";

/// imsg header size: 12 bytes (type + peer_id + length).
pub const IMSG_HEADER_SIZE: usize = 12;

/// Control socket read timeout in seconds.
pub const CTL_SOCKET_TIMEOUT_SECS: u64 = 5;

/// Maximum control socket payload we'll accept (1 MB).
pub const MAX_PAYLOAD: usize = 1_048_576;

/// Valid status query targets matching ntpctl(8).
pub const VALID_TARGETS: &[&str] = &["status", "peers", "Sensors", "all"];

/// Exit codes matching OpenNTPD conventions.
pub const EXIT_ERROR: u8 = 1;

// ---------------------------------------------------------------------------
// Re-exports
// ---------------------------------------------------------------------------

pub use openntpd_rs_core as core;
pub use openntpd_rs_io as io;
pub use openntpd_rs_io::imsg::IMSG_CTL_RESP;

// ---------------------------------------------------------------------------
// ControlClient — main API
// ---------------------------------------------------------------------------

/// A connected ntpctl control client.
///
/// Wraps a `UnixStream` connected to the ntpd control socket and
/// provides methods for the full request/response lifecycle.
pub struct ControlClient {
    stream: UnixStream,
}

impl ControlClient {
    /// Connect to the ntpd control socket at the given path.
    ///
    /// # Errors
    ///
    /// Returns `Err` if the socket cannot be opened or the read
    /// timeout cannot be set.
    pub fn connect<P: AsRef<Path>>(path: P) -> Result<Self, String> {
        let path = path.as_ref();
        let stream = UnixStream::connect(path)
            .map_err(|e| format!("cannot connect to {}: {e}", path.display()))?;
        stream
            .set_read_timeout(Some(Duration::from_secs(CTL_SOCKET_TIMEOUT_SECS)))
            .map_err(|e| format!("set read timeout: {e}"))?;
        Ok(Self { stream })
    }

    /// Send a control request and receive the response.
    ///
    /// # Errors
    ///
    /// Returns `Err` on I/O failure or invalid response header.
    pub fn query(&mut self, action: u32) -> Result<Imsg, String> {
        self.send_request(action)?;
        self.recv_response()
    }

    /// Send an imsg control request.
    fn send_request(&mut self, action: u32) -> Result<(), String> {
        let req = ControlRequest::new(action);
        let payload = req.encode().to_vec();
        let imsg = Imsg::new(IMSG_CTL_REQ, payload);
        let wire = imsg.to_bytes();
        self.stream
            .write_all(&wire)
            .map_err(|e| format!("write imsg: {e}"))?;
        self.stream.flush().map_err(|e| format!("flush: {e}"))?;
        Ok(())
    }

    /// Receive an imsg response.
    fn recv_response(&mut self) -> Result<Imsg, String> {
        let mut header_buf = [0u8; IMSG_HEADER_SIZE];
        read_exact(&mut self.stream, &mut header_buf)
            .map_err(|e| format!("read imsg header: {e}"))?;

        let header = ImsgHeader::from_bytes(&header_buf);
        header
            .validate()
            .map_err(|e| format!("invalid imsg header: {e}"))?;

        let payload_len = header.length as usize;
        if payload_len > MAX_PAYLOAD {
            return Err(format!(
                "imsg payload too large: {payload_len} > {MAX_PAYLOAD}"
            ));
        }

        let mut payload = vec![0u8; payload_len];
        if payload_len > 0 {
            read_exact(&mut self.stream, &mut payload)
                .map_err(|e| format!("read imsg payload ({payload_len} bytes): {e}"))?;
        }

        Ok(Imsg { header, payload })
    }
}

// ---------------------------------------------------------------------------
// Low-level I/O
// ---------------------------------------------------------------------------

/// Read exactly `buf.len()` bytes from the stream, handling partial reads.
fn read_exact(stream: &mut UnixStream, mut buf: &mut [u8]) -> Result<(), String> {
    while !buf.is_empty() {
        match stream.read(buf) {
            Ok(0) => return Err("connection closed by peer".into()),
            Ok(n) => buf = &mut buf[n..],
            Err(e) if e.kind() == std::io::ErrorKind::WouldBlock => {
                return Err("timed out waiting for response".into());
            }
            Err(e) => return Err(format!("read error: {e}")),
        }
    }
    Ok(())
}

// ---------------------------------------------------------------------------
// CLI argument parsing
// ---------------------------------------------------------------------------

/// Check if `input` is a case-insensitive prefix of `target`.
#[must_use]
pub fn is_prefix_of(input: &str, target: &str) -> bool {
    target.to_lowercase().starts_with(&input.to_lowercase())
}

/// Find all valid targets matching the given prefix.
#[must_use]
pub fn matching_targets(input: &str) -> Vec<&'static str> {
    VALID_TARGETS
        .iter()
        .copied()
        .filter(|t| is_prefix_of(input, t))
        .collect()
}

/// Result of parsing an ntpctl CLI invocation.
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum CliResult {
    /// A valid target was resolved.
    Target(&'static str),
    /// No target matched the prefix.
    Unknown(String),
    /// Multiple targets matched the prefix.
    Ambiguous(String, Vec<&'static str>),
    /// Missing or empty `-s` argument.
    MissingTarget,
}

/// Parse the `-s <target>` argument from raw CLI args.
///
/// Returns the first matched [`CliResult`]. The caller is responsible
/// for the top-level `-s` flag parsing.
#[must_use]
pub fn parse_target(what: &str) -> CliResult {
    if what.is_empty() {
        return CliResult::MissingTarget;
    }
    let matches = matching_targets(what);
    match matches.as_slice() {
        [t] => CliResult::Target(t),
        [] => CliResult::Unknown(what.to_string()),
        _ => CliResult::Ambiguous(what.to_string(), matches),
    }
}

/// Map a target name to its `CTL_REQ_*` constant.
#[must_use]
pub fn target_to_action(target: &str) -> u32 {
    match target.to_lowercase().as_str() {
        "status" => CTL_REQ_STATUS,
        "peers" => CTL_REQ_PEERS,
        "sensors" => CTL_REQ_SENSORS,
        "all" => CTL_REQ_ALL,
        _ => CTL_REQ_STATUS,
    }
}

// ---------------------------------------------------------------------------
// Response parsing
// ---------------------------------------------------------------------------

/// Parse a status response payload (32-byte fixed format without type tag).
///
/// # Errors
///
/// Returns `Err` if the payload is too short.
pub fn parse_status(data: &[u8]) -> Result<NtpdStatus, String> {
    NtpdStatus::from_bytes(data).ok_or_else(|| {
        format!(
            "invalid status response: expected >=32 bytes, got {}",
            data.len()
        )
    })
}

/// Parse a peers response payload: u32 count followed by entries.
pub fn parse_peers(data: &[u8]) -> Result<Vec<PeerInfo>, String> {
    if data.len() < 4 {
        return Ok(Vec::new());
    }
    let count = u32::from_be_bytes([data[0], data[1], data[2], data[3]]) as usize;
    let mut peers = Vec::with_capacity(count);
    let mut offset = 4;
    for _ in 0..count {
        match PeerInfo::from_entry_bytes(&data[offset..]) {
            Some((peer, consumed)) => {
                peers.push(peer);
                offset += consumed;
            }
            None => break,
        }
    }
    Ok(peers)
}

/// Parse a sensors response payload: u32 count followed by entries.
pub fn parse_sensors(data: &[u8]) -> Result<Vec<SensorInfo>, String> {
    if data.len() < 4 {
        return Ok(Vec::new());
    }
    let count = u32::from_be_bytes([data[0], data[1], data[2], data[3]]) as usize;
    let mut sensors = Vec::with_capacity(count);
    let mut offset = 4;
    for _ in 0..count {
        match SensorInfo::from_entry_bytes(&data[offset..]) {
            Some((sensor, consumed)) => {
                sensors.push(sensor);
                offset += consumed;
            }
            None => break,
        }
    }
    Ok(sensors)
}

// ---------------------------------------------------------------------------
// Output formatting
// ---------------------------------------------------------------------------

/// Format and print the daemon synchronization status.
pub fn print_status(status: &NtpdStatus) {
    println!("status:");
    match status.sync_state {
        SyncState::Synced => println!("\tsynchronized: YES"),
        SyncState::Unsynchronized => println!("\tsynchronized: NO"),
        SyncState::Constrained => println!("\tsynchronized: CONSTRAINED"),
    }
    println!("\tstratum: {}", status.stratum);
    println!("\toffset: {:.6} seconds", status.offset);
    println!("\tfrequency: {:.3} ppm", status.frequency);
    print_uptime(status.uptime);
}

/// Format uptime seconds into human-readable form.
fn print_uptime(total_secs: u64) {
    let days = total_secs / 86400;
    let hours = (total_secs % 86400) / 3600;
    let minutes = (total_secs % 3600) / 60;
    let secs = total_secs % 60;
    if days > 0 {
        println!("\tuptime: {days}d {hours}h {minutes}m {secs}s");
    } else if hours > 0 {
        println!("\tuptime: {hours}h {minutes}m {secs}s");
    } else if minutes > 0 {
        println!("\tuptime: {minutes}m {secs}s");
    } else {
        println!("\tuptime: {secs}s");
    }
}

/// Format and print peer information, matching real ntpctl output style.
pub fn print_peers(peers: &[PeerInfo]) {
    if peers.is_empty() {
        println!("peers: (none)");
        return;
    }
    println!(
        "{:6} {:8} {:>10} {:>10} {:>10} {:>4} {:>4} {:>4} {:>5}",
        "weight", "stratum", "offset", "delay", "dispersion", "poll", "reach", "flash", "trusted"
    );
    println!("{}", "-".repeat(70));
    for p in peers {
        let poll_sec = 1i64 << p.poll.max(0);
        println!(
            "{:6} {:8} {:>10.6} {:>10.6} {:>10.6} {:>4}s {:>4} 0x{:04x} {:>5}",
            p.weight,
            p.stratum,
            p.offset,
            p.delay,
            p.dispersion,
            poll_sec,
            p.reach,
            p.flash,
            if p.trusted { "trust" } else { "" },
        );
        println!("      {}", p.address);
    }
}

/// Format and print sensor information.
pub fn print_sensors(sensors: &[SensorInfo]) {
    if sensors.is_empty() {
        println!("Sensors: (none)");
        return;
    }
    println!(
        "{:>12} {:>4} {:>6}  device",
        "correction", "stratum", "status",
    );
    println!("{}", "-".repeat(50));
    for s in sensors {
        println!(
            "{:>12} {:>4} {:>6}  {}",
            s.correction, s.stratum, s.status, s.device,
        );
        if !s.refid.is_empty() && s.refid != "HARD" {
            println!("      refid: {}", s.refid);
        }
    }
}

/// Format and print the 'all' response (status + peers + sensors).
pub fn print_all(data: &[u8]) {
    if data.len() >= 32 {
        match parse_status(&data[..32]) {
            Ok(status) => print_status(&status),
            Err(e) => eprintln!("status parse error: {e}"),
        }
        println!();

        if data.len() > 32 {
            match parse_peers(&data[32..]) {
                Ok(peers) => {
                    print_peers(&peers);
                    if !peers.is_empty() {
                        println!();
                    }
                }
                Err(e) => eprintln!("peers parse error: {e}"),
            }
        }
    } else {
        eprintln!("'all' response too short: {} bytes", data.len());
    }
}

// ---------------------------------------------------------------------------
// Tests
// ---------------------------------------------------------------------------

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

    // -- CLI argument parsing --

    #[test]
    fn test_parse_target_exact() {
        assert_eq!(parse_target("status"), CliResult::Target("status"));
        assert_eq!(parse_target("peers"), CliResult::Target("peers"));
        assert_eq!(parse_target("Sensors"), CliResult::Target("Sensors"));
        assert_eq!(parse_target("all"), CliResult::Target("all"));
    }

    #[test]
    fn test_parse_target_prefix() {
        assert_eq!(parse_target("stat"), CliResult::Target("status"));
        assert_eq!(parse_target("peer"), CliResult::Target("peers"));
        assert_eq!(parse_target("Sen"), CliResult::Target("Sensors"));
        assert_eq!(parse_target("a"), CliResult::Target("all"));
    }

    #[test]
    fn test_parse_target_empty_is_missing() {
        assert_eq!(parse_target(""), CliResult::MissingTarget);
    }

    #[test]
    fn test_parse_target_unknown() {
        assert!(matches!(parse_target("nonexistent"), CliResult::Unknown(_)));
    }

    #[test]
    fn test_parse_target_ambiguous() {
        // "s" matches "status" and "Sensors"
        assert!(matches!(parse_target("s"), CliResult::Ambiguous(_, _)));
    }

    #[test]
    fn test_target_to_action() {
        assert_eq!(target_to_action("status"), CTL_REQ_STATUS);
        assert_eq!(target_to_action("peers"), CTL_REQ_PEERS);
        assert_eq!(target_to_action("sensors"), CTL_REQ_SENSORS);
        assert_eq!(target_to_action("all"), CTL_REQ_ALL);
    }

    #[test]
    fn test_is_prefix_of() {
        assert!(is_prefix_of("stat", "status"));
        assert!(is_prefix_of("STAT", "status"));
        assert!(!is_prefix_of("xyz", "status"));
    }

    // -- Response parsing --

    #[test]
    fn test_parse_status_too_short() {
        assert!(parse_status(&[]).is_err());
        assert!(parse_status(&[0u8; 31]).is_err());
    }

    #[test]
    fn test_parse_status_ok() {
        let mut buf = [0u8; 32];
        // sync_state = 1 = Synced (big-endian u32)
        buf[0] = 0;
        buf[1] = 0;
        buf[2] = 0;
        buf[3] = 1;
        // stratum = 3
        buf[4] = 3;
        let status = parse_status(&buf).expect("valid status");
        assert_eq!(status.stratum, 3);
    }

    #[test]
    fn test_parse_peers_empty() {
        let peers = parse_peers(&[0, 0, 0, 0]).expect("empty peers");
        assert!(peers.is_empty());
    }

    #[test]
    fn test_parse_peers_too_short() {
        let peers = parse_peers(&[]).expect("too short -> empty");
        assert!(peers.is_empty());
    }

    #[test]
    fn test_parse_sensors_empty() {
        let sensors = parse_sensors(&[0, 0, 0, 0]).expect("empty sensors");
        assert!(sensors.is_empty());
    }

    // -- Output formatting (smoke tests) --

    #[test]
    fn test_print_status_smoke() {
        let status = NtpdStatus {
            sync_state: SyncState::Synced,
            stratum: 3,
            offset: 0.001,
            frequency: 12.5,
            uptime: 3600,
        };
        // Should not panic
        print_status(&status);
    }

    #[test]
    fn test_print_peers_empty() {
        print_peers(&[]);
    }

    #[test]
    fn test_print_peers_smoke() {
        let peer = PeerInfo {
            weight: 1,
            stratum: 3,
            offset: 0.005,
            delay: 0.050,
            dispersion: 0.010,
            poll: 6,
            reach: 0xFF,
            flash: 0,
            trusted: true,
            address: "192.0.2.1".into(),
        };
        print_peers(&[peer]);
    }

    #[test]
    fn test_print_sensors_empty() {
        print_sensors(&[]);
    }

    #[test]
    fn test_print_sensors_smoke() {
        let sensor = SensorInfo {
            device: "nmea0".into(),
            correction: 12345,
            stratum: 1,
            status: 1,
            weight: 1,
            refid: "GPS".into(),
        };
        print_sensors(&[sensor]);
    }

    #[test]
    fn test_print_all_empty() {
        print_all(&[]);
    }

    #[test]
    fn test_print_all_status_only() {
        let mut buf = [0u8; 32];
        // sync_state = 1 (Synced) as big-endian u32
        buf[3] = 1;
        print_all(&buf);
    }

    // -- read_exact edge cases --

    #[test]
    fn test_read_exact_with_stream() {
        // Smoke test: read_exact doesn't panic with a connected socket.
        let (mut a, mut b) = UnixStream::pair().expect("socket pair");
        b.write_all(b"hello").unwrap();
        drop(b);
        let mut buf = [0u8; 5];
        let result = read_exact(&mut a, &mut buf);
        // Either we read the data or the connection was already closed.
        if result.is_ok() {
            assert_eq!(&buf, b"hello");
        }
    }
}