Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor & clean code #93

Closed
wants to merge 174 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
174 commits
Select commit Hold shift + click to select a range
3c8fb5a
clean code
ssrlive Nov 5, 2023
dde6920
refine macos device
ssrlive Nov 23, 2023
9d6c749
refactor device n queue
ssrlive Nov 30, 2023
11d136f
add ablility to set device GUID for wintun
ssrlive Dec 2, 2023
e944758
add apply_settings switch for Linux configutation
ssrlive Dec 6, 2023
a74731b
refine TunPacketCodec
ssrlive Jan 11, 2024
dbb6351
beginning v2
ssrlive Jan 13, 2024
05df46c
refine packet_information
ssrlive Jan 13, 2024
c32366f
Merge branch 'master' into v2
ssrlive Jan 13, 2024
ee80e99
cargo config.toml
ssrlive Jan 16, 2024
b1d0c56
refine cargo.toml
ssrlive Jan 17, 2024
51e2553
refine code
ssrlive Jan 17, 2024
3061457
eliminate redundantcopy
xmh0511 Jan 17, 2024
885d09e
Merge pull request #4 from xmh0511/v2
xmh0511 Jan 17, 2024
57fc820
Construct TunPacket from certain types that satisfy some trait bounds
xmh0511 Jan 17, 2024
641bd42
Construct TunPacket from certain types that satisfy some trait bounds…
xmh0511 Jan 17, 2024
f1abffe
Merge branch 'ssrlive:v2' into v2
xmh0511 Jan 17, 2024
c024261
fix the mtu of wintun may larger than the provided default capacity o…
xmh0511 Jan 17, 2024
dd57ac0
Merge pull request #6 from xmh0511/v2
xmh0511 Jan 17, 2024
86fd2fd
associate mtu with the capacity of ReadBuf for all platforms
xmh0511 Jan 17, 2024
199f675
change to shared reference
xmh0511 Jan 17, 2024
33c2b12
Merge pull request #7 from xmh0511/v2
xmh0511 Jan 17, 2024
821dc33
remove AsyncQueue
ssrlive Jan 17, 2024
f153bbf
clippy issues
ssrlive Jan 17, 2024
fa6d281
clippy issues
ssrlive Jan 17, 2024
9a5da49
refactor interfaces
ssrlive Jan 18, 2024
a80efc3
Merge pull request #8 from ssrlive/v0.7
ssrlive Jan 18, 2024
c2bb8e1
Bump v0.7.1
ssrlive Jan 18, 2024
6b0bb56
Create dependabot.yml
ssrlive Jan 19, 2024
5a182dd
codec module to public
ssrlive Jan 19, 2024
17b1c58
Merge pull request #9 from ssrlive/codec
ssrlive Jan 19, 2024
4b48324
rename to unix_device
ssrlive Jan 19, 2024
e0e0792
avoid unnecessary copy and fix lfreq not found on macOS
xmh0511 Jan 22, 2024
a891bd9
Merge pull request #11 from xmh0511/v2
xmh0511 Jan 22, 2024
04fb141
bump version
xmh0511 Jan 22, 2024
ec2e08c
Merge pull request #12 from xmh0511/v2
xmh0511 Jan 22, 2024
e9ce3a0
add split for windows platform
xmh0511 Jan 22, 2024
2ddb560
Merge pull request #13 from xmh0511/v2
xmh0511 Jan 22, 2024
a39357f
recover libc version
xmh0511 Jan 22, 2024
336d12d
Merge pull request #14 from xmh0511/v2
xmh0511 Jan 22, 2024
fd74d71
refine platform/windows/device.rs
xmh0511 Jan 22, 2024
d33d3ad
Merge pull request #15 from xmh0511/v2
xmh0511 Jan 22, 2024
13cb7d8
Bump package version
xmh0511 Jan 22, 2024
11c6c2f
Merge pull request #16 from xmh0511/v2
xmh0511 Jan 22, 2024
b2d3b10
fix mtu for windows
xmh0511 Jan 22, 2024
bcdbcbc
Bump version
xmh0511 Jan 22, 2024
f8f6598
Merge pull request #17 from xmh0511/v2
xmh0511 Jan 22, 2024
19b77c4
set PlatformConfig to zero-size type if it is no-op
xmh0511 Jan 23, 2024
8fd2629
revamp encapsulatation
xmh0511 Jan 23, 2024
046e9b2
correct read-async example
xmh0511 Jan 23, 2024
8b5d4c0
recover pub
xmh0511 Jan 23, 2024
e87262e
update dependabot.yml
xmh0511 Jan 23, 2024
4c6e107
chore(deps): bump actions/checkout from 3 to 4
dependabot[bot] Jan 23, 2024
3f47a6d
update example
xmh0511 Jan 23, 2024
8127bc1
update
xmh0511 Jan 23, 2024
701b7e3
update
xmh0511 Jan 23, 2024
169a269
Merge pull request #20 from ssrlive/dependabot/github_actions/actions…
xmh0511 Jan 23, 2024
0bf68e2
refine TunPacketCodec (#19)
ssrlive Jan 23, 2024
236fa4e
MTU issues (#21)
ssrlive Jan 23, 2024
773023a
refine codec.rs
xmh0511 Jan 24, 2024
329e6bf
remove TunPacket struct
ssrlive Jan 24, 2024
82002f3
minor packet_information issues
ssrlive Jan 24, 2024
d0b4f8d
strip Queue and eliminate platform differences in packet information …
xmh0511 Jan 25, 2024
8ca6b01
remove device_io
ssrlive Jan 25, 2024
9d0ff37
Reader (#25)
ssrlive Jan 25, 2024
c2cfaea
rename apply_settings to ask_permission
ssrlive Jan 25, 2024
cfe31a9
writer issues
ssrlive Jan 25, 2024
d1739e7
fix decode in codec (#26)
xmh0511 Jan 26, 2024
80d4e18
move codec to async module (#27)
xmh0511 Jan 26, 2024
ef84fdf
prepare to support Ipv6 (#28)
xmh0511 Jan 26, 2024
c7e1843
examples
ssrlive Jan 26, 2024
f2e89c1
add ctrlc2 to examples
ssrlive Jan 27, 2024
dae1b7d
comment for ask_permission
ssrlive Jan 27, 2024
0dd0cea
prepare for v1.0.0
ssrlive Jan 27, 2024
4fb9c10
ensure_root_privileges
ssrlive Jan 27, 2024
0f60f19
detail packet_information (#29)
xmh0511 Jan 29, 2024
059118b
Update Cargo.toml
xmh0511 Jan 30, 2024
c29132b
Update README.md
xmh0511 Jan 30, 2024
02a813d
Update README.md
ssrlive Jan 30, 2024
f8f32c5
update doc (#31)
xmh0511 Jan 31, 2024
a9c0668
update doc (#32)
xmh0511 Jan 31, 2024
211b0ba
update doc and examples (#33)
xmh0511 Jan 31, 2024
bb7a083
update doc (#34)
xmh0511 Jan 31, 2024
847b641
Update configuration.rs
ssrlive Jan 31, 2024
f34d8fb
change the type of mtu to u16 (#38)
xmh0511 Feb 1, 2024
60c0aee
To fd (#39)
ssrlive Feb 1, 2024
49a25a0
unify behavior for macOS (#40)
xmh0511 Feb 4, 2024
a4055d6
Update README.md
xmh0511 Feb 4, 2024
6cdb4b3
remove unused_imports
xmh0511 Feb 4, 2024
ae3a3f5
Update README.md
xmh0511 Feb 4, 2024
b4d7c91
Update Cargo.toml
xmh0511 Feb 4, 2024
0a0d6a9
bump version
xmh0511 Feb 6, 2024
3551885
migrate to ipnet (#42)
xmh0511 Feb 6, 2024
ef11d6f
Bump version (#44)
xmh0511 Feb 6, 2024
d7ef515
fixing windows '*mut c_void' cannot be sent between threads safely
ssrlive Feb 12, 2024
fac5d3a
feat: replace deprecated ioctl with nix (#46) (#47)
zonyitoo Feb 18, 2024
b15d2c9
Bump version 1.1.1
ssrlive Feb 19, 2024
8fece34
change to tun_name for easy to search (#48)
ssrlive Feb 25, 2024
ac97228
Bump version 1.1.2
ssrlive Feb 25, 2024
b36fcf3
bring back `name` and specify it as `deprecated` (#50)
xmh0511 Feb 28, 2024
294ac06
test_sockaddr
ssrlive Mar 1, 2024
dff31b4
Update rust.yml
xmh0511 Mar 1, 2024
c320066
Freebsd (#51)
xmh0511 Mar 1, 2024
a6f30ca
Update README.md
xmh0511 Mar 1, 2024
2d1aa51
Update README.md
xmh0511 Mar 1, 2024
6c01af7
Update README.md
xmh0511 Mar 1, 2024
f37e314
Update Cargo.toml
xmh0511 Mar 1, 2024
e1d374a
Update rust.yml
xmh0511 Mar 1, 2024
7683b6c
add set_tun_name for freebsd (#52)
xmh0511 Mar 1, 2024
b699339
Update Cargo.toml
xmh0511 Mar 1, 2024
565568a
unify the access of internal api (#54)
xmh0511 Mar 1, 2024
dbb7b23
fix packet-information (#55)
xmh0511 Mar 6, 2024
2e9b777
Update Cargo.toml
xmh0511 Mar 6, 2024
f139620
refine code (#57)
ssrlive Mar 28, 2024
a5da438
Bump version 1.2.4
ssrlive Mar 28, 2024
e7dc0cf
doc refine
ssrlive Mar 28, 2024
971232c
Bump version 1.2.5
ssrlive Mar 28, 2024
de0b3b4
Bump version 1.2.6
ssrlive Mar 28, 2024
736d769
Bump version 1.2.7
ssrlive Mar 28, 2024
980f041
PI issues (#59)
ssrlive Mar 30, 2024
5bf9c31
Bump version 1.2.8
ssrlive Mar 30, 2024
5eeeef9
feat(deps): enable libc extra_traits feature to fix Debug trait unimp…
cavivie Apr 9, 2024
116bc82
Fix channel closed loop (#61)
RoDmitry Apr 9, 2024
e29c6cd
Bump version 1.2.9
Apr 10, 2024
74bdd4b
Merge pull request #65 from tun2proxy/bump_version
cavivie Apr 10, 2024
00f0f16
update readme file
ssrlive Apr 10, 2024
d73452b
Sockaddr (#67)
ssrlive Apr 14, 2024
96cb935
Configure whether fd should be closed on drop (#66)
neacsu Apr 14, 2024
bb721ba
Bump version 1.3.0
ssrlive Apr 14, 2024
cb57d8c
issues of memory layout for sockaddr_union
ssrlive May 25, 2024
d446317
Bump version 1.3.1
ssrlive May 25, 2024
078a52d
Add support for dynamic Wintun library loading and update dependencie…
mokhtarabadi May 26, 2024
719aae3
Bump version 1.3.2
ssrlive May 26, 2024
f6cab50
Support custom wintun.dll paths (#73)
blechschmidt May 27, 2024
8ec27d1
chore!: refine windows platform configuration (#76)
cavivie Jun 13, 2024
f9f59f6
Support set wintun dns servers (#74)
feifeigood Jun 13, 2024
78985e6
Refine windows platform configuration (#77)
cavivie Jun 14, 2024
228b43e
Windows DLL verification module
ssrlive Jun 14, 2024
b8479d0
make rustc compiler happy
ssrlive Jun 14, 2024
209c8c1
Bump version 2.0.0
ssrlive Jun 14, 2024
3d5e573
fix FreeBSD build problem (#78)
ge9 Jun 16, 2024
5b6a80a
Bump version 2.0.1
ssrlive Jun 17, 2024
721a481
feat: windows set dns_servers behind feature gate "wintun-dns" (#85)
zonyitoo Jul 14, 2024
b87ebb0
Bump version 2.0.2
ssrlive Jul 14, 2024
777ba78
Check semver script
ssrlive Jul 20, 2024
93af5c3
Add support for concurrent send/recv apis for tun device (#87)
kp-mariappan-ramasamy Jul 23, 2024
42cc6a8
Bump version (#89)
xmh0511 Jul 23, 2024
48dbbba
chore(deps): bump actions/checkout from 2 to 4 (#88)
dependabot[bot] Jul 23, 2024
96fb69e
Update wintun version
ssrlive Jul 23, 2024
ed0a23a
Fix ios and android builds (#90)
kp-mariappan-ramasamy Jul 23, 2024
25fc58a
Fix #91
ssrlive Jul 25, 2024
3644c33
Rust 1.79 new feature
ssrlive Jul 26, 2024
c8d052a
chore(deps): update windows-sys requirement from 0.52 to 0.59
dependabot[bot] Jul 30, 2024
d5128ae
chore: impl Deref/DerefMut traits for AsyncDevice
cavivie Aug 5, 2024
e02baf0
Merge pull request #93 from tun2proxy/dependabot/cargo/windows-sys-0.59
cavivie Aug 5, 2024
3e961ce
Merge pull request #94 from cavivie/v2
cavivie Aug 5, 2024
c770830
enable docs for async feature
ssrlive Aug 5, 2024
1f28799
Set wintun ring capacity (#95)
vnt-dev Aug 18, 2024
cf1468b
compatible with low-version rustc (#97)
xmh0511 Aug 19, 2024
5043d96
split DeviceWriter&DeviceReader (#98)
vnt-dev Aug 19, 2024
16612c7
windows set mtu (#99)
vnt-dev Aug 19, 2024
097d8c9
modify wintun
vnt-dev Aug 20, 2024
bd6e971
set interface metric
vnt-dev Aug 20, 2024
9c5965e
Merge pull request #100 from vnt-dev/v2
vnt-dev Aug 20, 2024
7efa7ea
prepare to support windows-tap
ssrlive Aug 21, 2024
93944a6
Bump version
xmh0511 Aug 22, 2024
a7293ea
Update Cargo.toml wintun-bindings
xmh0511 Aug 22, 2024
08fc54a
Update Cargo.toml
xmh0511 Aug 22, 2024
751bece
Update Cargo.toml
xmh0511 Aug 22, 2024
6058f74
Bump version
xmh0511 Aug 30, 2024
2afc42e
Apply 'wintun-bindings' crate full
ssrlive Aug 31, 2024
79b68eb
Remove complex macro usage
ssrlive Aug 31, 2024
11866ed
re-write AsyncWrite & AsyncRead for windows
ssrlive Sep 2, 2024
008ddc7
CI testing script
ssrlive Sep 2, 2024
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions .cargo/config.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
[registries.crates-io]
protocol = "sparse"

[build]
# target = ["x86_64-unknown-linux-musl"]
# target = ["x86_64-unknown-linux-gnu"]
# target = ["aarch64-linux-android"]
# target = ["aarch64-apple-ios"]
# target = ["x86_64-pc-windows-msvc"]
# target = ["x86_64-apple-darwin"]
# target = ["x86_64-unknown-freebsd"]
10 changes: 10 additions & 0 deletions .github/dependabot.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
version: 2
updates:
- package-ecosystem: "github-actions"
directory: "/"
schedule:
interval: "daily"
- package-ecosystem: "cargo"
directory: "/"
schedule:
interval: "daily"
77 changes: 71 additions & 6 deletions .github/workflows/rust.yml
Original file line number Diff line number Diff line change
@@ -1,29 +1,94 @@
name: Push or PR

on:
push:
branches: [ "master" ]
pull_request:
branches: [ "master" ]
[push, pull_request]

env:
CARGO_TERM_COLOR: always

jobs:
build_n_test:
strategy:
fail-fast: false
matrix:
os: [ubuntu-latest, macos-latest, windows-latest]

runs-on: ${{ matrix.os }}

steps:
- uses: actions/checkout@v3
- uses: actions/checkout@v4
- name: rustfmt
if: ${{ !cancelled() }}
run: cargo fmt --all -- --check
- name: check
if: ${{ !cancelled() }}
run: cargo check --verbose
- name: clippy
if: ${{ !cancelled() }}
run: cargo clippy --all-targets --all-features -- -D warnings
- name: Build
run: cargo build --verbose --examples --tests --all-features --features="async tokio/rt-multi-thread"
if: ${{ !cancelled() }}
run: |
cargo build --verbose --examples --tests --all-features --features="async tokio/rt-multi-thread"
cargo clean
cargo build --verbose --examples --tests --no-default-features
- name: Abort on error
if: ${{ failure() }}
run: echo "Some of jobs failed" && false

build_n_test_android:
strategy:
fail-fast: false
runs-on: ubuntu-latest

steps:
- uses: actions/checkout@v4
- name: Install cargo ndk and rust compiler for android target
if: ${{ !cancelled() }}
run: |
cargo install --locked cargo-ndk
rustup target add x86_64-linux-android
- name: clippy
if: ${{ !cancelled() }}
run: cargo ndk -t x86_64 clippy --all-features --features="async tokio/rt-multi-thread" -- -D warnings
- name: Build
if: ${{ !cancelled() }}
run: |
cargo ndk -t x86_64 rustc --verbose --all-features --features="async tokio/rt-multi-thread" --lib --crate-type=cdylib
- name: Abort on error
if: ${{ failure() }}
run: echo "Android build job failed" && false

build_n_test_ios:
strategy:
fail-fast: false
runs-on: macos-latest

steps:
- uses: actions/checkout@v4
- name: Install cargo lipo and rust compiler for ios target
if: ${{ !cancelled() }}
run: |
cargo install --locked cargo-lipo
rustup target add x86_64-apple-ios aarch64-apple-ios
- name: clippy
if: ${{ !cancelled() }}
run: cargo clippy --target x86_64-apple-ios --all-features --features="async tokio/rt-multi-thread" -- -D warnings
- name: Build
if: ${{ !cancelled() }}
run: |
cargo lipo --verbose --all-features --features="async tokio/rt-multi-thread"
- name: Abort on error
if: ${{ failure() }}
run: echo "iOs build job failed" && false

semver:
name: Check semver
strategy:
matrix:
os: [ubuntu-latest, macos-latest, windows-latest]
runs-on: ${{ matrix.os }}
steps:
- uses: actions/checkout@v4
- uses: dtolnay/rust-toolchain@stable
- uses: obi1kenobi/cargo-semver-checks-action@v2
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
.vscode/
.VSCodeCounter/
build/
tmp/
target/
**/*.rs.bk
Cargo.lock
wintun.dll
74 changes: 57 additions & 17 deletions Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,46 +1,86 @@
[package]
name = "tun"
version = "0.6.1"
name = "tun2"
version = "2.0.9"
edition = "2021"

authors = ["meh. <[email protected]>"]
authors = ["meh. <[email protected]>", "@ssrlive"]
license = "WTFPL"

description = "TUN device creation and handling."
repository = "https://github.com/meh/rust-tun"
repository = "https://github.com/ssrlive/rust-tun"
keywords = ["tun", "network", "tunnel", "bindings"]
#rust-version = "1.79"

[lib]
crate-type = ["staticlib", "cdylib", "lib"]

[dependencies]
byteorder = { version = "1", optional = true }
bytes = { version = "1", optional = true }
bytes = { version = "1" }
cfg-if = "1"
futures-core = { version = "0.3", optional = true }
libc = "0.2"
libc = { version = "0.2", features = ["extra_traits"] }
log = "0.4"
thiserror = "1"
tokio = { version = "1", features = ["net", "macros"], optional = true }
tokio = { version = "1", features = [
"net",
"macros",
"io-util",
], optional = true }
tokio-util = { version = "0.7", features = ["codec"], optional = true }

[target.'cfg(any(target_os = "linux", target_os = "macos"))'.dependencies]
ioctl = { version = "0.8", package = "ioctl-sys" }
[target.'cfg(any(target_os = "linux", target_os = "macos", target_os = "android", target_os="freebsd"))'.dependencies]
nix = { version = "0.29", features = ["ioctl"] }

[target.'cfg(target_os = "windows")'.dependencies]
wintun = { version = "0.3", features = ["panic_on_unsent_packets"] }
futures = { version = "0.3", optional = true }
windows-sys = { version = "0.59", features = [
"Win32_Foundation",
"Win32_Security",
"Win32_Security_WinTrust",
"Win32_Security_Cryptography",
"Win32_System_Threading",
"Win32_UI_WindowsAndMessaging",
"Win32_System_LibraryLoader",
] }
wintun-bindings = { version = "0.7", features = [
"panic_on_unsent_packets",
"verify_binary_signature",
"async",
] }
libloading = "0.8"

[target.'cfg(any(target_os = "macos", target_os = "freebsd"))'.dependencies]
ipnet = "2"

[dev-dependencies]
ctrlc2 = { version = "3", features = ["tokio", "termination"] }
env_logger = "0.11"
futures = "0.3"
packet = "0.1"
serde_json = "1"
tokio = { version = "1", features = ["rt-multi-thread"] }

[features]
async = ["tokio", "tokio-util", "bytes", "byteorder", "futures-core"]
default = ["wintun-dns"]
# default = ["wintun-dns", "async"]
async = [
"tokio",
"futures-core",
"futures",
"tokio-util",
"wintun-bindings/async",
]
wintun-dns = []

[package.metadata.docs.rs]
features = ["async"]

[[example]]
name = "read-async"
required-features = ["async", "tokio/rt-multi-thread"]
required-features = ["async"]

[[example]]
name = "read-async-codec"
required-features = ["async", "tokio/rt-multi-thread"]
required-features = ["async"]

[[example]]
name = "ping-tun"
required-features = ["async", "tokio/rt-multi-thread"]
required-features = ["async"]
97 changes: 61 additions & 36 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,27 +1,38 @@
TUN interfaces [![Crates.io](https://img.shields.io/crates/v/tun.svg)](https://crates.io/crates/tun) ![tun](https://docs.rs/tun/badge.svg) ![WTFPL](http://img.shields.io/badge/license-WTFPL-blue.svg)
TUN interfaces
==============
[![Crates.io](https://img.shields.io/crates/v/tun2.svg)](https://crates.io/crates/tun2)
![tun2](https://docs.rs/tun2/badge.svg)
![WTFPL](http://img.shields.io/badge/license-WTFPL-blue.svg)

This crate allows the creation and usage of TUN interfaces, the aim is to make this cross-platform.

> Since the original maintainer @meh is no longer interested in continuing to maintain
> [tun](https://crates.io/crates/tun) at [repo](https://github.com/meh/rust-tun),
> We (@ssrlive, @xmh0511) created the [tun2](https://github.com/ssrlive/rust-tun) branch repo and
> continued to actively update. Welcome to any interested contributor.
> If you want to be a co-contributor and publisher of [tun2](https://crates.io/crates/tun2),
> please contact me in [issues](https://github.com/ssrlive/rust-tun/issues).
>
> For me, a submitted PR has not been reviewed for a long time,
> cannot be merged to the main branch, and cannot be published.
> It is like a patient who has not been sutured on the operating table for a long time.
> This is a bad experience.
> I believe that many people feel the same.

Usage
-----
First, add the following to your `Cargo.toml`:

```toml
[dependencies]
tun = "0.6.1"
```

Next, add this to your crate root:

```rust
extern crate tun;
tun2 = "2"
```

If you want to use the TUN interface with mio/tokio, you need to enable the `async` feature:

```toml
[dependencies]
tun = { version = "0.6.1", features = ["async"] }
tun2 = { version = "2", features = ["async"] }
```

Example
Expand All @@ -32,55 +43,65 @@ packets from it.
```rust
use std::io::Read;

extern crate tun;

fn main() {
let mut config = tun::Configuration::default();
config.address((10, 0, 0, 1))
.netmask((255, 255, 255, 0))
.up();

#[cfg(target_os = "linux")]
config.platform(|config| {
config.packet_information(true);
});
fn main() -> Result<(), Box<dyn std::error::Error + Send + Sync + 'static>> {
let mut config = tun2::Configuration::default();
config
.address((10, 0, 0, 9))
.netmask((255, 255, 255, 0))
.destination((10, 0, 0, 1))
.up();

#[cfg(target_os = "linux")]
config.platform_config(|config| {
// requiring root privilege to acquire complete functions
config.ensure_root_privileges(true);
});

let mut dev = tun::create(&config).unwrap();
let mut buf = [0; 4096];
let mut dev = tun2::create(&config)?;
let mut buf = [0; 4096];

loop {
let amount = dev.read(&mut buf).unwrap();
println!("{:?}", &buf[0 .. amount]);
}
loop {
let amount = dev.read(&mut buf)?;
println!("{:?}", &buf[0..amount]);
}
}
```

Platforms
=========
Not every platform is supported.
## Supported Platforms

- [x] Windows
- [x] Linux
- [x] macOS
- [x] FreeBSD
- [x] Android
- [x] iOS


Linux
-----
You will need the `tun` module to be loaded and root is required to create
You will need the `tun2` module to be loaded and root is required to create
interfaces.

macOS
macOS & FreeBSD
-----
It just works, but you have to set up routing manually. For example:
`tun2` will automatically set up a route according to the provided configuration, which does a similar thing like this:
> sudo route -n add -net 10.0.0.0/24 10.0.0.1


iOS
----
You can pass the file descriptor of the TUN device to `rust-tun` to create the interface.
You can pass the file descriptor of the TUN device to `tun2` to create the interface.

Here is an example to create the TUN device on iOS and pass the `fd` to `rust-tun`:
Here is an example to create the TUN device on iOS and pass the `fd` to `tun2`:
```swift
// Swift
class PacketTunnelProvider: NEPacketTunnelProvider {
override func startTunnel(options: [String : NSObject]?, completionHandler: @escaping (Error?) -> Void) {
let tunnelNetworkSettings = createTunnelSettings() // Configure TUN address, DNS, mtu, routing...
setTunnelNetworkSettings(tunnelNetworkSettings) { [weak self] error in
// The tunnel of this tunFd is contains `Packet Information` prifix.
let tunFd = self?.packetFlow.value(forKeyPath: "socket.fileDescriptor") as! Int32
DispatchQueue.global(qos: .default).async {
start_tun(tunFd)
Expand All @@ -96,9 +117,13 @@ class PacketTunnelProvider: NEPacketTunnelProvider {
pub extern "C" fn start_tun(fd: std::os::raw::c_int) {
let mut rt = tokio::runtime::Runtime::new().unwrap();
rt.block_on(async {
let mut cfg = tun::Configuration::default();
let mut cfg = tun2::Configuration::default();
cfg.raw_fd(fd);
let mut tun = tun::create_as_async(&cfg).unwrap();
#[cfg(target_os = "ios")]
cfg.platform_config(|p_cfg| {
p_cfg.packet_information(true);
});
let mut tun = tun2::create_as_async(&cfg).unwrap();
let mut framed = tun.into_framed();
while let Some(packet) = framed.next().await {
...
Expand All @@ -110,4 +135,4 @@ pub extern "C" fn start_tun(fd: std::os::raw::c_int) {
Windows
-----
You need to copy the [wintun.dll](https://wintun.net/) file which matches your architecture to
the same directory as your executable and run you program as administrator.
the same directory as your executable and run your program as administrator.
Loading