tun-rs 2.8.3

Cross-platform TUN and TAP library
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
/*!

# Interruptible I/O Module

This module provides support for interruptible I/O operations on Unix platforms.

## Overview

Interruptible I/O allows you to cancel blocking read/write operations on a TUN/TAP device
from another thread. This is useful for graceful shutdown, implementing timeouts, or
responding to signals.

## Availability

This module is only available when the `interruptible` feature flag is enabled:

```toml
[dependencies]
tun-rs = { version = "2", features = ["interruptible"] }
```

## How It Works

The implementation uses a pipe-based signaling mechanism:
- An `InterruptEvent` creates a pipe internally
- When triggered, it writes to the pipe
- I/O operations use `poll()` (or `select()` on macOS) to wait on both the device fd and the pipe
- If the pipe becomes readable, the I/O operation returns with `ErrorKind::Interrupted`

## Usage

```no_run
# #[cfg(all(unix, feature = "interruptible"))]
# {
use tun_rs::{DeviceBuilder, InterruptEvent};
use std::sync::Arc;
use std::thread;

let dev = DeviceBuilder::new()
    .ipv4("10.0.0.1", 24, None)
    .build_sync()?;

let event = Arc::new(InterruptEvent::new()?);
let event_clone = event.clone();

// Spawn a thread that will read from the device
let handle = thread::spawn(move || {
    let mut buf = vec![0u8; 1500];
    match dev.recv_intr(&mut buf, &event_clone) {
        Ok(n) => println!("Received {} bytes", n),
        Err(e) if e.kind() == std::io::ErrorKind::Interrupted => {
            println!("Read was interrupted");
        }
        Err(e) => eprintln!("Error: {}", e),
    }
});

// From the main thread, trigger the interrupt
thread::sleep(std::time::Duration::from_secs(1));
event.trigger()?;

handle.join().unwrap();
# }
# Ok::<(), std::io::Error>(())
```

## Performance Considerations

- Interruptible I/O has slightly more overhead than regular I/O due to the additional poll() fd
- The pipe is created once and reused across all operations
- Non-blocking mode is set on the pipe fds to prevent deadlocks

## Platform Support

- **Linux**: Uses `poll()` with two file descriptors
- **macOS**: Uses `select()` with fd_set
- **FreeBSD/OpenBSD/NetBSD**: Uses `poll()` like Linux
- **Windows**: Not supported (would need IOCP or overlapped I/O)

## Thread Safety

`InterruptEvent` is thread-safe and can be shared across threads using `Arc`.
*/

use crate::platform::unix::Fd;
use std::io;
use std::io::{IoSlice, IoSliceMut};
use std::os::fd::AsRawFd;
use std::sync::Mutex;

impl Fd {
    pub(crate) fn read_interruptible(
        &self,
        buf: &mut [u8],
        event: &InterruptEvent,
        timeout: Option<std::time::Duration>,
    ) -> io::Result<usize> {
        loop {
            self.wait_readable_interruptible(event, timeout)?;
            return match self.read(buf) {
                Err(ref e) if e.kind() == io::ErrorKind::WouldBlock => {
                    continue;
                }
                rs => rs,
            };
        }
    }
    pub(crate) fn readv_interruptible(
        &self,
        bufs: &mut [IoSliceMut<'_>],
        event: &InterruptEvent,
        timeout: Option<std::time::Duration>,
    ) -> io::Result<usize> {
        loop {
            self.wait_readable_interruptible(event, timeout)?;
            return match self.readv(bufs) {
                Err(ref e) if e.kind() == io::ErrorKind::WouldBlock => {
                    continue;
                }

                rs => rs,
            };
        }
    }
    pub(crate) fn write_interruptible(
        &self,
        buf: &[u8],
        event: &InterruptEvent,
    ) -> io::Result<usize> {
        loop {
            self.wait_writable_interruptible(event)?;
            return match self.write(buf) {
                Err(ref e) if e.kind() == io::ErrorKind::WouldBlock => {
                    continue;
                }
                rs => rs,
            };
        }
    }
    pub fn writev_interruptible(
        &self,
        bufs: &[IoSlice<'_>],
        event: &InterruptEvent,
    ) -> io::Result<usize> {
        loop {
            self.wait_writable_interruptible(event)?;
            return match self.writev(bufs) {
                Err(ref e) if e.kind() == io::ErrorKind::WouldBlock => {
                    continue;
                }
                rs => rs,
            };
        }
    }
    pub fn wait_readable_interruptible(
        &self,
        interrupted_event: &InterruptEvent,
        timeout: Option<std::time::Duration>,
    ) -> io::Result<()> {
        let fd = self.as_raw_fd() as libc::c_int;
        let event_fd = interrupted_event.as_event_fd();

        let mut fds = [
            libc::pollfd {
                fd,
                events: libc::POLLIN,
                revents: 0,
            },
            libc::pollfd {
                fd: event_fd,
                events: libc::POLLIN,
                revents: 0,
            },
        ];

        let result = unsafe {
            libc::poll(
                fds.as_mut_ptr(),
                fds.len() as libc::nfds_t,
                timeout
                    .map(|t| t.as_millis().min(i32::MAX as _) as _)
                    .unwrap_or(-1),
            )
        };

        if result == -1 {
            return Err(io::Error::last_os_error());
        }
        if result == 0 {
            return Err(io::Error::from(io::ErrorKind::TimedOut));
        }
        if fds[0].revents & libc::POLLIN != 0 {
            return Ok(());
        }

        if fds[1].revents & libc::POLLIN != 0 {
            return Err(io::Error::new(
                io::ErrorKind::Interrupted,
                "trigger interrupt",
            ));
        }

        Err(io::Error::other("fd error"))
    }
    pub fn wait_writable_interruptible(
        &self,
        interrupted_event: &InterruptEvent,
    ) -> io::Result<()> {
        let fd = self.as_raw_fd() as libc::c_int;
        let event_fd = interrupted_event.as_event_fd();

        let mut fds = [
            libc::pollfd {
                fd,
                events: libc::POLLOUT,
                revents: 0,
            },
            libc::pollfd {
                fd: event_fd,
                events: libc::POLLIN,
                revents: 0,
            },
        ];

        let result = unsafe { libc::poll(fds.as_mut_ptr(), fds.len() as libc::nfds_t, -1) };

        if result == -1 {
            return Err(io::Error::last_os_error());
        }
        if fds[0].revents & libc::POLLOUT != 0 {
            return Ok(());
        }

        if fds[1].revents & libc::POLLIN != 0 {
            return Err(io::Error::new(
                io::ErrorKind::Interrupted,
                "trigger interrupt",
            ));
        }

        Err(io::Error::other("fd error"))
    }
}

#[cfg(target_os = "macos")]
impl Fd {
    pub fn wait_writable(
        &self,
        interrupt_event: Option<&InterruptEvent>,
        timeout: Option<std::time::Duration>,
    ) -> io::Result<()> {
        let readfds: libc::fd_set = unsafe { std::mem::zeroed() };
        let mut writefds: libc::fd_set = unsafe { std::mem::zeroed() };
        let fd = self.as_raw_fd();
        unsafe {
            libc::FD_SET(fd, &mut writefds);
        }
        self.wait(readfds, Some(writefds), interrupt_event, timeout)
    }
    pub fn wait_readable(
        &self,
        interrupt_event: Option<&InterruptEvent>,
        timeout: Option<std::time::Duration>,
    ) -> io::Result<()> {
        let mut readfds: libc::fd_set = unsafe { std::mem::zeroed() };
        let fd = self.as_raw_fd();
        unsafe {
            libc::FD_SET(fd, &mut readfds);
        }
        self.wait(readfds, None, interrupt_event, timeout)
    }
    fn wait(
        &self,
        mut readfds: libc::fd_set,
        mut writefds: Option<libc::fd_set>,
        interrupt_event: Option<&InterruptEvent>,
        timeout: Option<std::time::Duration>,
    ) -> io::Result<()> {
        let fd = self.as_raw_fd();
        let mut errorfds: libc::fd_set = unsafe { std::mem::zeroed() };
        let mut nfds = fd;

        if let Some(interrupt_event) = interrupt_event {
            unsafe {
                libc::FD_SET(interrupt_event.as_event_fd(), &mut readfds);
            }
            nfds = nfds.max(interrupt_event.as_event_fd());
        }
        let mut tv = libc::timeval {
            tv_sec: 0,
            tv_usec: 0,
        };
        let tv_ptr = if let Some(timeout) = timeout {
            let secs = timeout.as_secs().min(libc::time_t::MAX as u64) as libc::time_t;
            let usecs = (timeout.subsec_micros()) as libc::suseconds_t;
            tv.tv_sec = secs;
            tv.tv_usec = usecs;
            &mut tv as *mut libc::timeval
        } else {
            std::ptr::null_mut()
        };

        let result = unsafe {
            libc::select(
                nfds + 1,
                &mut readfds,
                writefds
                    .as_mut()
                    .map(|p| p as *mut _)
                    .unwrap_or_else(std::ptr::null_mut),
                &mut errorfds,
                tv_ptr,
            )
        };
        if result < 0 {
            return Err(io::Error::last_os_error());
        }
        if result == 0 {
            return Err(io::Error::from(io::ErrorKind::TimedOut));
        }
        unsafe {
            if let Some(cancel_event) = interrupt_event {
                if libc::FD_ISSET(cancel_event.as_event_fd(), &readfds) {
                    return Err(io::Error::new(
                        io::ErrorKind::Interrupted,
                        "trigger interrupt",
                    ));
                }
            }
        }
        Ok(())
    }
}
/// Event object for interrupting blocking I/O operations.
///
/// `InterruptEvent` provides a mechanism to cancel blocking read/write operations
/// from another thread. It uses a pipe-based signaling mechanism internally.
///
/// # Thread Safety
///
/// This type is thread-safe and can be shared across threads using `Arc<InterruptEvent>`.
///
/// # Examples
///
/// Basic usage with interruptible read:
///
/// ```no_run
/// # #[cfg(all(unix, feature = "interruptible"))]
/// # {
/// use std::sync::Arc;
/// use std::thread;
/// use std::time::Duration;
/// use tun_rs::{DeviceBuilder, InterruptEvent};
///
/// let device = DeviceBuilder::new()
///     .ipv4("10.0.0.1", 24, None)
///     .build_sync()?;
///
/// let event = Arc::new(InterruptEvent::new()?);
/// let event_clone = event.clone();
///
/// let reader = thread::spawn(move || {
///     let mut buf = vec![0u8; 1500];
///     device.recv_intr(&mut buf, &event_clone)
/// });
///
/// // Give the reader time to start blocking
/// thread::sleep(Duration::from_millis(100));
///
/// // Trigger the interrupt
/// event.trigger()?;
///
/// match reader.join().unwrap() {
///     Ok(n) => println!("Read {} bytes", n),
///     Err(e) if e.kind() == std::io::ErrorKind::Interrupted => {
///         println!("Successfully interrupted!");
///     }
///     Err(e) => eprintln!("Error: {}", e),
/// }
/// # }
/// # Ok::<(), std::io::Error>(())
/// ```
///
/// Using with a timeout:
///
/// ```no_run
/// # #[cfg(all(unix, feature = "interruptible"))]
/// # {
/// use std::time::Duration;
/// use tun_rs::{DeviceBuilder, InterruptEvent};
///
/// let device = DeviceBuilder::new()
///     .ipv4("10.0.0.1", 24, None)
///     .build_sync()?;
///
/// let event = InterruptEvent::new()?;
/// let mut buf = vec![0u8; 1500];
///
/// // Will return an error if no data arrives within 5 seconds
/// match device.recv_intr_timeout(&mut buf, &event, Some(Duration::from_secs(5))) {
///     Ok(n) => println!("Read {} bytes", n),
///     Err(e) if e.kind() == std::io::ErrorKind::TimedOut => {
///         println!("Timed out waiting for data");
///     }
///     Err(e) if e.kind() == std::io::ErrorKind::Interrupted => {
///         println!("Operation was interrupted");
///     }
///     Err(e) => eprintln!("Error: {}", e),
/// }
/// # }
/// # Ok::<(), std::io::Error>(())
/// ```
///
/// # Implementation Details
///
/// - Uses a Unix pipe for signaling
/// - Both read and write ends are set to non-blocking mode
/// - State is protected by a mutex to prevent race conditions
/// - Once triggered, the event remains triggered until reset
pub struct InterruptEvent {
    state: Mutex<i32>,
    read_fd: Fd,
    write_fd: Fd,
}
impl InterruptEvent {
    /// Creates a new `InterruptEvent`.
    ///
    /// This allocates a Unix pipe for signaling and sets both ends to non-blocking mode.
    ///
    /// # Errors
    ///
    /// Returns an error if:
    /// - The pipe creation fails (e.g., out of file descriptors)
    /// - Setting non-blocking mode fails
    ///
    /// # Example
    ///
    /// ```no_run
    /// # #[cfg(all(unix, feature = "interruptible"))]
    /// # {
    /// use tun_rs::InterruptEvent;
    ///
    /// let event = InterruptEvent::new()?;
    /// # }
    /// # Ok::<(), std::io::Error>(())
    /// ```
    pub fn new() -> io::Result<Self> {
        let mut fds: [libc::c_int; 2] = [0; 2];

        unsafe {
            if libc::pipe(fds.as_mut_ptr()) == -1 {
                return Err(io::Error::last_os_error());
            }
            let read_fd = Fd::new_unchecked(fds[0]);
            let write_fd = Fd::new_unchecked(fds[1]);
            write_fd.set_nonblocking(true)?;
            read_fd.set_nonblocking(true)?;
            Ok(Self {
                state: Mutex::new(0),
                read_fd,
                write_fd,
            })
        }
    }

    /// Triggers the interrupt event with value 1.
    ///
    /// This will cause any blocking I/O operations waiting on this event to return
    /// with `ErrorKind::Interrupted`.
    ///
    /// Calling `trigger()` multiple times before `reset()` has no additional effect -
    /// the event remains triggered.
    ///
    /// # Errors
    ///
    /// Returns an error if writing to the internal pipe fails, though this is rare
    /// in practice (pipe write errors are usually `WouldBlock`, which is handled).
    ///
    /// # Example
    ///
    /// ```no_run
    /// # #[cfg(all(unix, feature = "interruptible"))]
    /// # {
    /// use std::sync::Arc;
    /// use std::thread;
    /// use tun_rs::{DeviceBuilder, InterruptEvent};
    ///
    /// let event = Arc::new(InterruptEvent::new()?);
    /// let event_clone = event.clone();
    ///
    /// thread::spawn(move || {
    ///     // ... blocking I/O with event_clone ...
    /// });
    ///
    /// // Interrupt the operation
    /// event.trigger()?;
    /// # }
    /// # Ok::<(), std::io::Error>(())
    /// ```
    pub fn trigger(&self) -> io::Result<()> {
        self.trigger_value(1)
    }

    /// Triggers the interrupt event with a specific value.
    ///
    /// Similar to [`trigger()`](Self::trigger), but allows specifying a custom value
    /// that can be retrieved with [`value()`](Self::value).
    ///
    /// # Arguments
    ///
    /// * `val` - The value to store (must be non-zero)
    ///
    /// # Errors
    ///
    /// - Returns `ErrorKind::InvalidInput` if `val` is 0
    /// - Returns an error if writing to the pipe fails
    ///
    /// # Example
    ///
    /// ```no_run
    /// # #[cfg(all(unix, feature = "interruptible"))]
    /// # {
    /// use tun_rs::InterruptEvent;
    ///
    /// let event = InterruptEvent::new()?;
    ///
    /// // Trigger with a custom value (e.g., signal number)
    /// event.trigger_value(15)?; // SIGTERM
    ///
    /// // Later, check what value was used
    /// assert_eq!(event.value(), 15);
    /// # }
    /// # Ok::<(), std::io::Error>(())
    /// ```
    pub fn trigger_value(&self, val: i32) -> io::Result<()> {
        if val == 0 {
            return Err(io::Error::new(
                io::ErrorKind::InvalidInput,
                "value cannot be 0",
            ));
        }
        let mut guard = self.state.lock().unwrap();
        if *guard != 0 {
            return Ok(());
        }
        *guard = val;
        let buf: [u8; 8] = 1u64.to_ne_bytes();
        let res = unsafe {
            libc::write(
                self.write_fd.as_raw_fd(),
                buf.as_ptr() as *const _,
                buf.len(),
            )
        };
        if res == -1 {
            let e = io::Error::last_os_error();
            if e.kind() == io::ErrorKind::WouldBlock {
                return Ok(());
            }
            Err(e)
        } else {
            Ok(())
        }
    }
    /// Checks if the event has been triggered.
    ///
    /// Returns `true` if the event is currently in the triggered state (value is non-zero).
    ///
    /// # Example
    ///
    /// ```no_run
    /// # #[cfg(all(unix, feature = "interruptible"))]
    /// # {
    /// use tun_rs::InterruptEvent;
    ///
    /// let event = InterruptEvent::new()?;
    /// assert!(!event.is_trigger());
    ///
    /// event.trigger()?;
    /// assert!(event.is_trigger());
    ///
    /// event.reset()?;
    /// assert!(!event.is_trigger());
    /// # }
    /// # Ok::<(), std::io::Error>(())
    /// ```
    pub fn is_trigger(&self) -> bool {
        *self.state.lock().unwrap() != 0
    }

    /// Returns the current trigger value.
    ///
    /// Returns 0 if the event is not triggered, or the value passed to
    /// [`trigger_value()`](Self::trigger_value) if triggered.
    ///
    /// # Example
    ///
    /// ```no_run
    /// # #[cfg(all(unix, feature = "interruptible"))]
    /// # {
    /// use tun_rs::InterruptEvent;
    ///
    /// let event = InterruptEvent::new()?;
    /// assert_eq!(event.value(), 0);
    ///
    /// event.trigger_value(42)?;
    /// assert_eq!(event.value(), 42);
    /// # }
    /// # Ok::<(), std::io::Error>(())
    /// ```
    pub fn value(&self) -> i32 {
        *self.state.lock().unwrap()
    }

    /// Resets the event to the non-triggered state.
    ///
    /// This drains any pending data from the internal pipe and sets the state back to 0.
    /// After calling `reset()`, the event can be triggered again.
    ///
    /// # Errors
    ///
    /// Returns an error if reading from the pipe fails (other than `WouldBlock`).
    ///
    /// # Example
    ///
    /// ```no_run
    /// # #[cfg(all(unix, feature = "interruptible"))]
    /// # {
    /// use tun_rs::InterruptEvent;
    ///
    /// let event = InterruptEvent::new()?;
    ///
    /// event.trigger()?;
    /// assert!(event.is_trigger());
    ///
    /// event.reset()?;
    /// assert!(!event.is_trigger());
    ///
    /// // Can trigger again after reset
    /// event.trigger()?;
    /// assert!(event.is_trigger());
    /// # }
    /// # Ok::<(), std::io::Error>(())
    /// ```
    pub fn reset(&self) -> io::Result<()> {
        let mut buf = [0; 8];
        let mut guard = self.state.lock().unwrap();
        *guard = 0;
        loop {
            unsafe {
                let res = libc::read(
                    self.read_fd.as_raw_fd(),
                    buf.as_mut_ptr() as *mut _,
                    buf.len(),
                );
                if res == -1 {
                    let error = io::Error::last_os_error();
                    return if error.kind() == io::ErrorKind::WouldBlock {
                        Ok(())
                    } else {
                        Err(error)
                    };
                }
            }
        }
    }
    fn as_event_fd(&self) -> libc::c_int {
        self.read_fd.as_raw_fd()
    }
}