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

Mtu #85

Closed
wants to merge 59 commits into from
Closed

Mtu #85

Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
59 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
32721c8
MTU issues
ssrlive Jan 23, 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
9 changes: 9 additions & 0 deletions .cargo/config.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
[registries.crates-io]
protocol = "sparse"

[build]
# 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"]
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"
7 changes: 2 additions & 5 deletions .github/workflows/rust.yml
Original file line number Diff line number Diff line change
@@ -1,10 +1,7 @@
name: Push or PR

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

env:
CARGO_TERM_COLOR: always
Expand All @@ -18,7 +15,7 @@ jobs:
runs-on: ${{ matrix.os }}

steps:
- uses: actions/checkout@v3
- uses: actions/checkout@v4
- name: rustfmt
run: cargo fmt --all -- --check
- name: check
Expand Down
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,4 @@
target/
**/*.rs.bk
Cargo.lock
wintun.dll
31 changes: 18 additions & 13 deletions Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,46 +1,51 @@
[package]
name = "tun"
version = "0.6.1"
name = "tun2"
version = "0.7.22"
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"]

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

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

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

[target.'cfg(target_os = "windows")'.dependencies]
wintun = { version = "0.3", features = ["panic_on_unsent_packets"] }
wintun = { version = "0.4", features = ["panic_on_unsent_packets"] }

[dev-dependencies]
futures = "0.3"
packet = "0.1"
tokio = { version = "1", features = ["rt-multi-thread", "io-util"] }

[features]
async = ["tokio", "tokio-util", "bytes", "byteorder", "futures-core"]
default = []
async = ["tokio", "futures-core"]

[[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"]
79 changes: 45 additions & 34 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),
> I (@ssrlive) 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 = "0.7"
```

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 = "0.7", features = ["async"] }
```

Example
Expand All @@ -32,36 +43,36 @@ 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);
});

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

loop {
let amount = dev.read(&mut buf).unwrap();
println!("{:?}", &buf[0 .. amount]);
}
let mut config = tun2::Configuration::default();
config
.address((10, 0, 0, 1))
.netmask((255, 255, 255, 0))
.up();

#[cfg(target_os = "linux")]
config.platform_config(|config| {
config.packet_information(true);
config.apply_settings(true);
});

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

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

Platforms
=========
Not every platform is supported.
Recently, `tun2` supports Linux, Android, macOS, iOS and Windows.

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
Expand All @@ -72,9 +83,9 @@ It just works, but you have to set up routing manually. For example:

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 {
Expand All @@ -96,9 +107,9 @@ 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();
let mut tun = tun2::create_as_async(&cfg).unwrap();
let mut framed = tun.into_framed();
while let Some(packet) = framed.next().await {
...
Expand Down
15 changes: 9 additions & 6 deletions examples/ping-tun.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@

use futures::{SinkExt, StreamExt};
use packet::{builder::Builder, icmp, ip, Packet};
use tun::{self, Configuration, TunPacket};
use tun2::{self, AbstractDevice, Configuration, TunPacket};

#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
Expand All @@ -27,16 +27,18 @@ async fn main() -> Result<(), Box<dyn std::error::Error>> {
.up();

#[cfg(target_os = "linux")]
config.platform(|config| {
config.platform_config(|config| {
config.packet_information(true);
config.apply_settings(true);
});

#[cfg(target_os = "windows")]
config.platform(|config| {
config.initialize();
config.platform_config(|config| {
config.device_guid(Some(9099482345783245345345_u128));
});

let dev = tun::create_as_async(&config)?;
let dev = tun2::create_as_async(&config)?;
let packet_information = dev.as_ref().packet_information();

let mut framed = dev.into_framed();

Expand All @@ -59,7 +61,8 @@ async fn main() -> Result<(), Box<dyn std::error::Error>> {
.sequence(icmp.sequence())?
.payload(icmp.payload())?
.build()?;
framed.send(TunPacket::new(reply)).await?;
let pkt = TunPacket::new(packet_information, reply);
framed.send(pkt).await?;
}
}
}
Expand Down
4 changes: 2 additions & 2 deletions examples/read-async-codec.rs
Original file line number Diff line number Diff line change
Expand Up @@ -41,14 +41,14 @@ impl Decoder for IPPacketCodec {

#[tokio::main]
async fn main() {
let mut config = tun::Configuration::default();
let mut config = tun2::Configuration::default();

config
.address((10, 0, 0, 1))
.netmask((255, 255, 255, 0))
.up();

let dev = tun::create_as_async(&config).unwrap();
let dev = tun2::create_as_async(&config).unwrap();

let mut stream = FramedRead::new(dev, IPPacketCodec);

Expand Down
28 changes: 15 additions & 13 deletions examples/read-async.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,31 +12,33 @@
//
// 0. You just DO WHAT THE FUCK YOU WANT TO.

use futures::StreamExt;
use packet::ip::Packet;
use tokio::io::AsyncReadExt;
use tun2::AbstractDevice;

#[tokio::main]
async fn main() {
let mut config = tun::Configuration::default();
async fn main() -> Result<(), Box<dyn std::error::Error + Send + Sync>> {
let mut config = tun2::Configuration::default();

config
.address((10, 0, 0, 1))
.netmask((255, 255, 255, 0))
.mtu(tun2::DEFAULT_MTU)
.up();

#[cfg(target_os = "linux")]
config.platform(|config| {
config.platform_config(|config| {
config.packet_information(true);
config.apply_settings(true);
});

let dev = tun::create_as_async(&config).unwrap();

let mut stream = dev.into_framed();

while let Some(packet) = stream.next().await {
match packet {
Ok(pkt) => println!("pkt: {:#?}", Packet::unchecked(pkt.get_bytes())),
Err(err) => panic!("Error: {:?}", err),
let mut dev = tun2::create_as_async(&config)?;
let size = dev.as_ref().mtu()? + tun2::PACKET_INFORMATION_LENGTH;
let mut buf = vec![0; size];
loop {
if let Ok(len) = dev.read(&mut buf).await {
println!("pkt: {:?}", &buf[..len]);
}
}
#[allow(unreachable_code)]
Ok(())
}
7 changes: 4 additions & 3 deletions examples/read.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
use std::io::Read;

fn main() -> Result<(), Box<dyn std::error::Error>> {
let mut config = tun::Configuration::default();
let mut config = tun2::Configuration::default();

config
.address((10, 0, 0, 9))
Expand All @@ -24,11 +24,12 @@ fn main() -> Result<(), Box<dyn std::error::Error>> {
.up();

#[cfg(target_os = "linux")]
config.platform(|config| {
config.platform_config(|config| {
config.packet_information(true);
config.apply_settings(true);
});

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

loop {
Expand Down
Loading