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
//! Pipe interface: subprocess stdin/stdout with HDLC framing.
//!
//! Matches Python `PipeInterface.py`. Spawns a subprocess, communicates
//! via piped stdin/stdout using HDLC framing for packet boundaries.
//! Auto-respawns subprocess on failure.

use std::collections::HashMap;
use std::io::{self, Read, Write};
use std::process::{Command, Stdio};
use std::sync::{Arc, Mutex};
use std::thread;
use std::time::Duration;

use rns_core::transport::types::{InterfaceId, InterfaceInfo};

use super::{InterfaceConfigData, InterfaceFactory, StartContext, StartResult};
use crate::event::{Event, EventSender};
use crate::hdlc;
use crate::interface::Writer;

/// Configuration for a pipe interface.
#[derive(Debug, Clone)]
pub struct PipeConfig {
    pub name: String,
    pub command: String,
    pub respawn_delay: Duration,
    pub interface_id: InterfaceId,
    pub runtime: Arc<Mutex<PipeRuntime>>,
}

#[derive(Debug, Clone)]
pub struct PipeRuntime {
    pub respawn_delay: Duration,
}

impl PipeRuntime {
    pub fn from_config(config: &PipeConfig) -> Self {
        Self {
            respawn_delay: config.respawn_delay,
        }
    }
}

#[derive(Debug, Clone)]
pub struct PipeRuntimeConfigHandle {
    pub interface_name: String,
    pub runtime: Arc<Mutex<PipeRuntime>>,
    pub startup: PipeRuntime,
}

impl Default for PipeConfig {
    fn default() -> Self {
        let mut config = PipeConfig {
            name: String::new(),
            command: String::new(),
            respawn_delay: Duration::from_secs(5),
            interface_id: InterfaceId(0),
            runtime: Arc::new(Mutex::new(PipeRuntime {
                respawn_delay: Duration::from_secs(5),
            })),
        };
        let startup = PipeRuntime::from_config(&config);
        config.runtime = Arc::new(Mutex::new(startup));
        config
    }
}

/// Writer that sends HDLC-framed data to a subprocess stdin.
struct PipeWriter {
    stdin: std::process::ChildStdin,
}

impl Writer for PipeWriter {
    fn send_frame(&mut self, data: &[u8]) -> io::Result<()> {
        self.stdin.write_all(&hdlc::frame(data))
    }
}

/// Start the pipe interface. Spawns subprocess, returns writer.
pub fn start(config: PipeConfig, tx: EventSender) -> io::Result<Box<dyn Writer>> {
    let id = config.interface_id;
    {
        let startup = PipeRuntime::from_config(&config);
        *config.runtime.lock().unwrap() = startup;
    }

    let mut child = spawn_child(&config.command)?;

    let stdout = child
        .stdout
        .take()
        .ok_or_else(|| io::Error::new(io::ErrorKind::Other, "no stdout from child"))?;
    let stdin = child
        .stdin
        .take()
        .ok_or_else(|| io::Error::new(io::ErrorKind::Other, "no stdin from child"))?;

    log::info!(
        "[{}] pipe interface started: {}",
        config.name,
        config.command
    );

    // Signal interface up
    let _ = tx.send(Event::InterfaceUp(id, None, None));

    // Spawn reader thread
    thread::Builder::new()
        .name(format!("pipe-reader-{}", id.0))
        .spawn(move || {
            reader_loop(stdout, child, id, config, tx);
        })?;

    Ok(Box::new(PipeWriter { stdin }))
}

fn spawn_child(command: &str) -> io::Result<std::process::Child> {
    Command::new("sh")
        .args(["-c", command])
        .stdin(Stdio::piped())
        .stdout(Stdio::piped())
        .stderr(Stdio::null())
        .spawn()
}

/// Reader loop: reads from subprocess stdout, HDLC-decodes, dispatches events.
/// On subprocess exit, attempts respawn.
fn reader_loop(
    mut stdout: std::process::ChildStdout,
    mut child: std::process::Child,
    id: InterfaceId,
    config: PipeConfig,
    tx: EventSender,
) {
    let mut decoder = hdlc::Decoder::new();
    let mut buf = [0u8; 4096];

    loop {
        match stdout.read(&mut buf) {
            Ok(0) => {
                // EOF — subprocess exited
                let _ = child.wait();
                log::warn!("[{}] subprocess terminated", config.name);
                let _ = tx.send(Event::InterfaceDown(id));
                match respawn(&config, &tx) {
                    Some((new_stdout, new_child)) => {
                        stdout = new_stdout;
                        child = new_child;
                        decoder = hdlc::Decoder::new();
                        continue;
                    }
                    None => return,
                }
            }
            Ok(n) => {
                for frame in decoder.feed(&buf[..n]) {
                    if tx
                        .send(Event::Frame {
                            interface_id: id,
                            data: frame,
                        })
                        .is_err()
                    {
                        // Driver shut down
                        let _ = child.kill();
                        return;
                    }
                }
            }
            Err(e) => {
                log::warn!("[{}] pipe read error: {}", config.name, e);
                let _ = child.kill();
                let _ = child.wait();
                let _ = tx.send(Event::InterfaceDown(id));
                match respawn(&config, &tx) {
                    Some((new_stdout, new_child)) => {
                        stdout = new_stdout;
                        child = new_child;
                        decoder = hdlc::Decoder::new();
                        continue;
                    }
                    None => return,
                }
            }
        }
    }
}

/// Attempt to respawn the subprocess after a delay.
fn respawn(
    config: &PipeConfig,
    tx: &EventSender,
) -> Option<(std::process::ChildStdout, std::process::Child)> {
    loop {
        thread::sleep(config.runtime.lock().unwrap().respawn_delay);
        log::info!(
            "[{}] attempting to respawn subprocess: {}",
            config.name,
            config.command
        );

        match spawn_child(&config.command) {
            Ok(mut child) => {
                let stdout = match child.stdout.take() {
                    Some(s) => s,
                    None => {
                        let _ = child.kill();
                        let _ = child.wait();
                        continue;
                    }
                };
                let stdin = match child.stdin.take() {
                    Some(s) => s,
                    None => {
                        let _ = child.kill();
                        let _ = child.wait();
                        continue;
                    }
                };

                let new_writer: Box<dyn Writer> = Box::new(PipeWriter { stdin });
                if tx
                    .send(Event::InterfaceUp(
                        config.interface_id,
                        Some(new_writer),
                        None,
                    ))
                    .is_err()
                {
                    return None; // Driver shut down
                }
                log::info!("[{}] subprocess respawned", config.name);
                return Some((stdout, child));
            }
            Err(e) => {
                log::warn!("[{}] respawn failed: {}", config.name, e);
            }
        }
    }
}

/// Factory for [`PipeInterface`] instances.
pub struct PipeFactory;

impl InterfaceFactory for PipeFactory {
    fn type_name(&self) -> &str {
        "PipeInterface"
    }

    fn parse_config(
        &self,
        name: &str,
        id: InterfaceId,
        params: &HashMap<String, String>,
    ) -> Result<Box<dyn InterfaceConfigData>, String> {
        let command = params
            .get("command")
            .ok_or_else(|| "PipeInterface requires 'command'".to_string())?
            .clone();

        let respawn_delay = match params.get("respawn_delay") {
            Some(v) => {
                let ms: u64 = v
                    .parse()
                    .map_err(|_| format!("invalid respawn_delay: {}", v))?;
                Duration::from_millis(ms)
            }
            None => Duration::from_secs(5),
        };

        Ok(Box::new(PipeConfig {
            name: name.to_string(),
            command,
            respawn_delay,
            interface_id: id,
            runtime: Arc::new(Mutex::new(PipeRuntime { respawn_delay })),
        }))
    }

    fn start(
        &self,
        config: Box<dyn InterfaceConfigData>,
        ctx: StartContext,
    ) -> io::Result<StartResult> {
        let pipe_config = *config
            .into_any()
            .downcast::<PipeConfig>()
            .map_err(|_| io::Error::new(io::ErrorKind::InvalidData, "wrong config type"))?;

        let id = pipe_config.interface_id;
        let info = InterfaceInfo {
            id,
            name: pipe_config.name.clone(),
            mode: ctx.mode,
            out_capable: true,
            in_capable: true,
            bitrate: Some(1_000_000),
            announce_rate_target: None,
            announce_rate_grace: 0,
            announce_rate_penalty: 0.0,
            announce_cap: rns_core::constants::ANNOUNCE_CAP,
            is_local_client: false,
            wants_tunnel: false,
            tunnel_id: None,
            mtu: rns_core::constants::MTU as u32,
            ingress_control: rns_core::transport::types::IngressControlConfig::disabled(),
            ia_freq: 0.0,
            started: crate::time::now(),
        };

        let writer = start(pipe_config, ctx.tx)?;

        Ok(StartResult::Simple {
            id,
            info,
            writer,
            interface_type_name: "PipeInterface".to_string(),
        })
    }
}

pub(crate) fn pipe_runtime_handle_from_config(config: &PipeConfig) -> PipeRuntimeConfigHandle {
    PipeRuntimeConfigHandle {
        interface_name: config.name.clone(),
        runtime: Arc::clone(&config.runtime),
        startup: PipeRuntime::from_config(config),
    }
}

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

    #[test]
    fn pipe_start_and_receive() {
        // Use `cat` as a loopback subprocess
        let (tx, rx) = crate::event::channel();
        let config = PipeConfig {
            name: "test-pipe".into(),
            command: "cat".into(),
            respawn_delay: Duration::from_secs(1),
            interface_id: InterfaceId(100),
            runtime: Arc::new(Mutex::new(PipeRuntime {
                respawn_delay: Duration::from_secs(1),
            })),
        };

        let mut writer = start(config, tx).unwrap();

        // Drain InterfaceUp
        let event = rx.recv_timeout(Duration::from_secs(2)).unwrap();
        assert!(matches!(
            event,
            Event::InterfaceUp(InterfaceId(100), None, None)
        ));

        // Send a packet (>= 19 bytes for HDLC minimum)
        let payload: Vec<u8> = (0..32).collect();
        writer.send_frame(&payload).unwrap();

        // Should receive Frame event (cat echos back the HDLC frame)
        let event = rx.recv_timeout(Duration::from_secs(2)).unwrap();
        match event {
            Event::Frame { interface_id, data } => {
                assert_eq!(interface_id, InterfaceId(100));
                assert_eq!(data, payload);
            }
            other => panic!("expected Frame, got {:?}", other),
        }
    }

    #[test]
    fn pipe_writer_sends() {
        // Verify the writer wraps data in HDLC
        let (tx, rx) = crate::event::channel();
        let config = PipeConfig {
            name: "test-pipe-writer".into(),
            command: "cat".into(),
            respawn_delay: Duration::from_secs(1),
            interface_id: InterfaceId(101),
            runtime: Arc::new(Mutex::new(PipeRuntime {
                respawn_delay: Duration::from_secs(1),
            })),
        };

        let mut writer = start(config, tx).unwrap();
        let _ = rx.recv_timeout(Duration::from_secs(2)).unwrap(); // drain InterfaceUp

        // Write data and verify we get it back as HDLC frame
        let payload: Vec<u8> = (10..42).collect();
        writer.send_frame(&payload).unwrap();

        let event = rx.recv_timeout(Duration::from_secs(2)).unwrap();
        match event {
            Event::Frame { data, .. } => {
                assert_eq!(data, payload);
            }
            other => panic!("expected Frame, got {:?}", other),
        }
    }

    #[test]
    fn pipe_subprocess_exit() {
        // Use a command that exits immediately
        let (tx, rx) = crate::event::channel();
        let config = PipeConfig {
            name: "test-pipe-exit".into(),
            command: "true".into(),                 // exits immediately with 0
            respawn_delay: Duration::from_secs(60), // long delay so we catch InterfaceDown
            interface_id: InterfaceId(102),
            runtime: Arc::new(Mutex::new(PipeRuntime {
                respawn_delay: Duration::from_secs(60),
            })),
        };

        let _writer = start(config, tx).unwrap();

        // Should get InterfaceUp then InterfaceDown
        let mut got_down = false;
        for _ in 0..5 {
            match rx.recv_timeout(Duration::from_secs(2)) {
                Ok(Event::InterfaceDown(InterfaceId(102))) => {
                    got_down = true;
                    break;
                }
                Ok(_) => continue,
                Err(_) => break,
            }
        }
        assert!(
            got_down,
            "should receive InterfaceDown after subprocess exits"
        );
    }

    #[test]
    fn pipe_config_defaults() {
        let config = PipeConfig::default();
        assert_eq!(config.respawn_delay, Duration::from_secs(5));
        assert_eq!(config.interface_id, InterfaceId(0));
        assert!(config.command.is_empty());
    }

    #[test]
    fn pipe_invalid_command() {
        let (tx, _rx) = crate::event::channel();
        let config = PipeConfig {
            name: "test-pipe-bad".into(),
            command: "/nonexistent_rns_test_binary_that_does_not_exist_xyz".into(),
            respawn_delay: Duration::from_secs(60),
            interface_id: InterfaceId(103),
            runtime: Arc::new(Mutex::new(PipeRuntime {
                respawn_delay: Duration::from_secs(60),
            })),
        };

        // sh -c <nonexistent> will start sh successfully but the child exits immediately
        // For a truly invalid binary we need to check that the process fails
        // Actually, sh -c "<nonexistent>" will still spawn sh successfully
        // Let's test with a different approach: verify it doesn't panic
        let result = start(config, tx);
        // sh will spawn successfully even if the inner command fails
        // The real test is that it doesn't panic
        assert!(result.is_ok());
    }
}