tun_rs/lib.rs
1#![cfg_attr(docsrs, feature(doc_cfg))]
2
3/*!
4# Example:
5```no_run
6use tun_rs::DeviceBuilder;
7let dev = DeviceBuilder::new()
8 .name("utun7")
9 .ipv4("10.0.0.12", 24, None)
10 .ipv6("CDCD:910A:2222:5498:8475:1111:3900:2021", 64)
11 .mtu(1400)
12 .build_sync()
13 .unwrap();
14let mut buf = [0;65535];
15loop {
16 let len = dev.recv(&mut buf).unwrap();
17 println!("buf= {:?}",&buf[..len]);
18}
19```
20# Example IOS/Android/... :
21```no_run
22#[cfg(unix)]
23{
24 use tun_rs::SyncDevice;
25 // use PacketTunnelProvider/VpnService create tun fd
26 let fd = 7799;
27 let dev = unsafe{SyncDevice::from_fd(fd).unwrap()};
28 let mut buf = [0;65535];
29 loop {
30 let len = dev.recv(&mut buf).unwrap();
31 println!("buf= {:?}",&buf[..len]);
32 }
33}
34```
35*/
36
37#[cfg(any(
38 target_os = "windows",
39 all(target_os = "linux", not(target_env = "ohos")),
40 target_os = "macos",
41 target_os = "freebsd",
42 target_os = "openbsd",
43 target_os = "netbsd",
44))]
45pub use crate::builder::*;
46pub use crate::platform::*;
47
48#[cfg_attr(docsrs, doc(cfg(any(feature = "async_io", feature = "async_tokio"))))]
49#[cfg(any(feature = "async_io", feature = "async_tokio"))]
50mod async_device;
51
52#[cfg_attr(docsrs, doc(cfg(any(feature = "async_io", feature = "async_tokio"))))]
53#[cfg(any(feature = "async_io", feature = "async_tokio"))]
54pub use async_device::*;
55
56#[cfg(any(
57 target_os = "windows",
58 all(target_os = "linux", not(target_env = "ohos")),
59 target_os = "macos",
60 target_os = "freebsd",
61 target_os = "openbsd",
62 target_os = "netbsd",
63))]
64mod builder;
65mod platform;
66/// Length of the protocol info header
67pub const PACKET_INFORMATION_LENGTH: usize = 4;