-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathstd.rs
More file actions
42 lines (34 loc) · 1.55 KB
/
std.rs
File metadata and controls
42 lines (34 loc) · 1.55 KB
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
// (c) Copyright 2021 Christian Saide
// SPDX-License-Identifier: MIT
use riptun::{AsyncStdTun, Result};
use std::io::ErrorKind;
const NUM_QUEUES: usize = 5;
#[async_std::main]
pub async fn main() -> Result<()> {
// Create a new async Tun using the AsyncStdTun implementation, named `rip%d` with
// NUM_QUEUES internal queues.
let async_dev = AsyncStdTun::new("rip%d", NUM_QUEUES)?;
// Print out the OS given name of this device.
println!("[INFO] => Created new virtual device: {}", async_dev.name());
// Create a buffer the same size as the MTU as the Tun device, which by default
// is 1500 Bytes.
let mut buffer: [u8; 1500] = [0x00; 1500];
// Loop forever reading packets off the Tun.
loop {
// Calling [AsyncStdTun::recv()] handles selecting an available queue and reading one
// packet off the selected queue. See the documentation for further details on its internal
// selection mechanism.
let read = match async_dev.recv(&mut buffer).await {
Ok(read) => read,
// If its a WouldBlock simply short circuit, and re-poll the AsyncStdQueue.
Err(err) if err.kind() == ErrorKind::WouldBlock => continue,
Err(err) => {
// If we error simply log the issue, and continue on business as usual.
println!("[ERROR] => {}", err);
continue;
}
};
// Print the packet data as a byte slice, noting its size.
println!("[INFO] => Packet data ({}B): {:?}", read, &buffer[..read]);
}
}