forked from yaa110/tokio-tun
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathread.rs
48 lines (42 loc) · 1.32 KB
/
read.rs
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
use std::net::Ipv4Addr;
use std::os::unix::io::AsRawFd;
use tokio::io::AsyncReadExt;
use tokio_tun::result::Result;
use tokio_tun::TunBuilder;
#[tokio::main]
async fn main() -> Result<()> {
let tun = TunBuilder::new()
.name("")
.tap(false)
.packet_info(false)
.mtu(1350)
.up()
.address(Ipv4Addr::new(10, 0, 0, 1))
.destination(Ipv4Addr::new(10, 1, 0, 1))
.broadcast(Ipv4Addr::BROADCAST)
.netmask(Ipv4Addr::new(255, 255, 255, 0))
.try_build()?;
println!("-----------");
println!("tun created");
println!("-----------");
println!(
"┌ name: {}\n├ fd: {}\n├ mtu: {}\n├ flags: {}\n├ address: {}\n├ destination: {}\n├ broadcast: {}\n└ netmask: {}",
tun.name(),
tun.as_raw_fd(),
tun.mtu().unwrap(),
tun.flags().unwrap(),
tun.address().unwrap(),
tun.destination().unwrap(),
tun.broadcast().unwrap(),
tun.netmask().unwrap(),
);
println!("---------------------");
println!("ping 10.1.0.2 to test");
println!("---------------------");
let (mut reader, mut _writer) = tokio::io::split(tun);
let mut buf = [0u8; 1024];
loop {
let n = reader.read(&mut buf).await?;
println!("reading {} bytes: {:?}", n, &buf[..n]);
}
}