Skip to content

Commit

Permalink
beginning v2
Browse files Browse the repository at this point in the history
  • Loading branch information
ssrlive committed Jan 13, 2024
1 parent a74731b commit 4025f98
Show file tree
Hide file tree
Showing 6 changed files with 37 additions and 32 deletions.
8 changes: 4 additions & 4 deletions Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
[package]
name = "tun"
version = "0.6.1"
name = "tun2"
version = "0.6.2"
edition = "2021"

authors = ["meh. <meh@schizofreni.co>"]
authors = ["meh. <meh@schizofreni.co>", "@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"]

[dependencies]
Expand Down
45 changes: 25 additions & 20 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,31 +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)
[![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),
> I (@ssrlive) created the [tun2](https://github.com/ssrlive/rust-tun) branch 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 released.
> 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"
```

Next, add this to your crate root:

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

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

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

Example
Expand All @@ -36,10 +43,8 @@ packets from it.
```rust
use std::io::Read;

extern crate tun;

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();
Expand All @@ -50,7 +55,7 @@ fn main() {
config.apply_settings(true);
});

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

loop {
Expand All @@ -62,11 +67,11 @@ fn main() {

Platforms
=========
Recently, `tun` supports Linux, Android, macOS, iOS and Windows.
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 @@ -77,9 +82,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 @@ -101,9 +106,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
4 changes: 2 additions & 2 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, Configuration, TunPacket};

#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
Expand All @@ -37,7 +37,7 @@ async fn main() -> Result<(), Box<dyn std::error::Error>> {
config.initialize(Some(9099482345783245345345_u128));
});

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

let mut framed = dev.into_framed();

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
4 changes: 2 additions & 2 deletions examples/read-async.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ use packet::ip::Packet;

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

config
.address((10, 0, 0, 1))
Expand All @@ -30,7 +30,7 @@ async fn main() {
config.apply_settings(true);
});

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

let mut stream = dev.into_framed();

Expand Down
4 changes: 2 additions & 2 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 @@ -29,7 +29,7 @@ fn main() -> Result<(), Box<dyn std::error::Error>> {
config.apply_settings(true);
});

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

loop {
Expand Down

0 comments on commit 4025f98

Please sign in to comment.