Skip to content

Commit

Permalink
merge back from tun2 crate
Browse files Browse the repository at this point in the history
  • Loading branch information
ssrlive committed Oct 27, 2024
1 parent 7e6e7dd commit 0721bb8
Show file tree
Hide file tree
Showing 12 changed files with 40 additions and 43 deletions.
4 changes: 2 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "tun2"
version = "4.0.0"
name = "tun"
version = "0.7.0"
edition = "2021"
authors = ["meh. <meh@schizofreni.co>", "@ssrlive"]
license = "WTFPL"
Expand Down
25 changes: 11 additions & 14 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,28 +1,25 @@
TUN interfaces
==============
[![Crates.io](https://img.shields.io/crates/v/tun2.svg)](https://crates.io/crates/tun2)
![tun2](https://docs.rs/tun2/badge.svg)
[![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)

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

<b>Now that I (@ssrlive) am a co-contributor of the [tun](https://crates.io/crates/tun) crate,
this crate is no longer maintained and all code is merged into the [tun](https://crates.io/crates/tun) crate.</b>

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

```toml
[dependencies]
tun2 = "4"
tun = "0.7"
```

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

```toml
[dependencies]
tun2 = { version = "4", features = ["async"] }
tun = { version = "0.7", features = ["async"] }
```

Example
Expand All @@ -34,7 +31,7 @@ packets from it.
use std::io::Read;

fn main() -> Result<(), Box<dyn std::error::Error + Send + Sync + 'static>> {
let mut config = tun2::Configuration::default();
let mut config = tun::Configuration::default();
config
.address((10, 0, 0, 9))
.netmask((255, 255, 255, 0))
Expand All @@ -47,7 +44,7 @@ fn main() -> Result<(), Box<dyn std::error::Error + Send + Sync + 'static>> {
config.ensure_root_privileges(true);
});

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

loop {
Expand Down Expand Up @@ -76,15 +73,15 @@ interfaces.

macOS & FreeBSD
-----
`tun2` will automatically set up a route according to the provided configuration, which does a similar thing like this:
`tun` 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 `tun2` to create the interface.
You can pass the file descriptor of the TUN device to `tun` to create the interface.

Here is an example to create the TUN device on iOS and pass the `fd` to `tun2`:
Here is an example to create the TUN device on iOS and pass the `fd` to `tun`:
```swift
// Swift
class PacketTunnelProvider: NEPacketTunnelProvider {
Expand All @@ -107,13 +104,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 = tun2::Configuration::default();
let mut cfg = tun::Configuration::default();
cfg.raw_fd(fd);
#[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 tun = tun::create_as_async(&cfg).unwrap();
let mut framed = tun.into_framed();
while let Some(packet) = framed.next().await {
...
Expand Down
6 changes: 3 additions & 3 deletions examples/dev-config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
use packet::{builder::Builder, icmp, ip, Packet};
use std::io::{Read, Write};
use std::{net::Ipv4Addr, sync::mpsc::Receiver};
use tun2::{AbstractDevice, BoxError};
use tun::{AbstractDevice, BoxError};

fn main() -> Result<(), BoxError> {
env_logger::Builder::from_env(env_logger::Env::default().default_filter_or("trace")).init();
Expand All @@ -33,7 +33,7 @@ fn main() -> Result<(), BoxError> {
}

fn main_entry(quit: Receiver<()>) -> Result<(), BoxError> {
let mut config = tun2::Configuration::default();
let mut config = tun::Configuration::default();

config
.address((10, 0, 0, 9))
Expand All @@ -46,7 +46,7 @@ fn main_entry(quit: Receiver<()>) -> Result<(), BoxError> {
config.ensure_root_privileges(true);
});

let mut dev = tun2::create(&config)?;
let mut dev = tun::create(&config)?;

let r = dev.tun_index()?;
println!("Index: {:?}", r);
Expand Down
4 changes: 2 additions & 2 deletions examples/ping-tun.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
use futures::{SinkExt, StreamExt};
use packet::{builder::Builder, icmp, ip, Packet};
use tokio::sync::mpsc::Receiver;
use tun2::{self, BoxError, Configuration};
use tun::{self, BoxError, Configuration};

#[tokio::main]
async fn main() -> Result<(), BoxError> {
Expand Down Expand Up @@ -52,7 +52,7 @@ async fn main_entry(mut quit: Receiver<()>) -> Result<(), BoxError> {
config.device_guid(9099482345783245345345_u128);
});

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

let mut framed = dev.into_framed();

Expand Down
6 changes: 3 additions & 3 deletions examples/read-async-codec.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ use futures::StreamExt;
use packet::{ip::Packet, Error};
use tokio::sync::mpsc::Receiver;
use tokio_util::codec::{Decoder, FramedRead};
use tun2::BoxError;
use tun::BoxError;

pub struct IPPacketCodec;

Expand Down Expand Up @@ -56,7 +56,7 @@ async fn main() -> Result<(), BoxError> {
}

async fn main_entry(mut quit: Receiver<()>) -> Result<(), BoxError> {
let mut config = tun2::Configuration::default();
let mut config = tun::Configuration::default();

config
.address((10, 0, 0, 9))
Expand All @@ -76,7 +76,7 @@ async fn main_entry(mut quit: Receiver<()>) -> Result<(), BoxError> {
config.device_guid(9099482345783245345345_u128);
});

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

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

Expand Down
10 changes: 5 additions & 5 deletions examples/read-async.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@

use tokio::io::AsyncReadExt;
use tokio::sync::mpsc::Receiver;
use tun2::{AbstractDevice, BoxError};
use tun::{AbstractDevice, BoxError};

#[tokio::main]
async fn main() -> Result<(), BoxError> {
Expand All @@ -31,22 +31,22 @@ async fn main() -> Result<(), BoxError> {
}

async fn main_entry(mut quit: Receiver<()>) -> Result<(), BoxError> {
let mut config = tun2::Configuration::default();
let mut config = tun::Configuration::default();

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

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

let mut dev = tun2::create_as_async(&config)?;
let size = dev.mtu()? as usize + tun2::PACKET_INFORMATION_LENGTH;
let mut dev = tun::create_as_async(&config)?;
let size = dev.mtu()? as usize + tun::PACKET_INFORMATION_LENGTH;
let mut buf = vec![0; size];
loop {
tokio::select! {
Expand Down
6 changes: 3 additions & 3 deletions examples/read.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@

use std::io::Read;
use std::sync::mpsc::Receiver;
use tun2::BoxError;
use tun::BoxError;

fn main() -> Result<(), BoxError> {
env_logger::Builder::from_env(env_logger::Env::default().default_filter_or("trace")).init();
Expand All @@ -32,7 +32,7 @@ fn main() -> Result<(), BoxError> {
}

fn main_entry(quit: Receiver<()>) -> Result<(), BoxError> {
let mut config = tun2::Configuration::default();
let mut config = tun::Configuration::default();

config
.address((10, 0, 0, 9))
Expand All @@ -45,7 +45,7 @@ fn main_entry(quit: Receiver<()>) -> Result<(), BoxError> {
config.ensure_root_privileges(true);
});

let mut dev = tun2::create(&config)?;
let mut dev = tun::create(&config)?;
std::thread::spawn(move || {
let mut buf = [0; 4096];
loop {
Expand Down
6 changes: 3 additions & 3 deletions examples/split.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
use packet::{builder::Builder, icmp, ip, Packet};
use std::io::{Read, Write};
use std::sync::mpsc::Receiver;
use tun2::BoxError;
use tun::BoxError;

fn main() -> Result<(), BoxError> {
env_logger::Builder::from_env(env_logger::Env::default().default_filter_or("trace")).init();
Expand All @@ -33,7 +33,7 @@ fn main() -> Result<(), BoxError> {
}

fn main_entry(quit: Receiver<()>) -> Result<(), BoxError> {
let mut config = tun2::Configuration::default();
let mut config = tun::Configuration::default();

config
.address((10, 0, 0, 9))
Expand All @@ -46,7 +46,7 @@ fn main_entry(quit: Receiver<()>) -> Result<(), BoxError> {
config.ensure_root_privileges(true);
});

let dev = tun2::create(&config)?;
let dev = tun::create(&config)?;
let (mut reader, mut writer) = dev.split();

let (tx, rx) = std::sync::mpsc::channel();
Expand Down
6 changes: 3 additions & 3 deletions examples/write.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
use packet::{builder::Builder, icmp, ip, Packet};
use std::io::{Read, Write};
use std::sync::mpsc::Receiver;
use tun2::BoxError;
use tun::BoxError;

fn main() -> Result<(), BoxError> {
env_logger::Builder::from_env(env_logger::Env::default().default_filter_or("trace")).init();
Expand All @@ -33,7 +33,7 @@ fn main() -> Result<(), BoxError> {
}

fn main_entry(quit: Receiver<()>) -> Result<(), BoxError> {
let mut config = tun2::Configuration::default();
let mut config = tun::Configuration::default();

config
.address((10, 0, 0, 9))
Expand All @@ -46,7 +46,7 @@ fn main_entry(quit: Receiver<()>) -> Result<(), BoxError> {
config.ensure_root_privileges(true);
});

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

std::thread::spawn(move || {
Expand Down
2 changes: 1 addition & 1 deletion src/configuration.rs
Original file line number Diff line number Diff line change
Expand Up @@ -175,7 +175,7 @@ impl Configuration {
self
}
/// Set whether to close the received raw file descriptor on drop or not.
/// The default behaviour is to close the received or tun2 generated file descriptor.
/// The default behaviour is to close the received or tun generated file descriptor.
/// Note: If this is set to false, it is up to the caller to ensure the
/// file descriptor that they pass via [Configuration::raw_fd] is properly closed.
#[cfg(unix)]
Expand Down
2 changes: 1 addition & 1 deletion src/device.rs
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,6 @@ pub trait AbstractDevice: Read + Write {

/// Return whether the underlying tun device on the platform has packet information
///
/// [Note: This value is not used to specify whether the packets delivered from/to tun2 have packet information. -- end note]
/// [Note: This value is not used to specify whether the packets delivered from/to tun have packet information. -- end note]
fn packet_information(&self) -> bool;
}
6 changes: 3 additions & 3 deletions src/platform/linux/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -53,17 +53,17 @@ impl PlatformConfig {
/// Enable or disable packet information, the first 4 bytes of
/// each packet delivered from/to Linux underlying API is a header with flags and protocol type when enabled.
///
/// [Note: This configuration just applies to the Linux underlying API and is a no-op on tun2(i.e. the packets delivered from/to tun2 always contain no packet information) -- end note].
/// [Note: This configuration just applies to the Linux underlying API and is a no-op on tun(i.e. the packets delivered from/to tun always contain no packet information) -- end note].
#[deprecated(
since = "1.0.0",
note = "No effect applies to the packets delivered from/to tun2 since the packets always contain no header on all platforms."
note = "No effect applies to the packets delivered from/to tun since the packets always contain no header on all platforms."
)]
pub fn packet_information(&mut self, value: bool) -> &mut Self {
self.packet_information = value;
self
}

/// Indicated whether tun2 running in root privilege,
/// Indicated whether tun running in root privilege,
/// since some operations need it such as assigning IP/netmask/destination etc.
pub fn ensure_root_privileges(&mut self, value: bool) -> &mut Self {
self.ensure_root_privileges = value;
Expand Down

0 comments on commit 0721bb8

Please sign in to comment.