Skip to content

Commit

Permalink
use anyhow instead of boxed error
Browse files Browse the repository at this point in the history
update dependencies and minor improvements
  • Loading branch information
yaa110 committed Jul 5, 2023
1 parent 3630598 commit 84b6f37
Show file tree
Hide file tree
Showing 10 changed files with 49 additions and 58 deletions.
21 changes: 11 additions & 10 deletions Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,21 +1,22 @@
[package]
name = "tokio-tun"
version = "0.7.0"
authors = ["Navid <yaa110@gmail.com>"]
categories = ["asynchronous", "network-programming"]
description = "Asynchronous allocation of TUN/TAP devices using tokio"
documentation = "https://docs.rs/tokio-tun"
edition = "2021"
homepage = "https://github.com/yaa110/tokio-tun"
repository = "https://github.com/yaa110/tokio-tun"
documentation = "https://docs.rs/tokio-tun"
keywords = ["tun", "tap", "async", "tokio"]
license = "MIT OR Apache-2.0"
name = "tokio-tun"
readme = "README.md"
description = "Asynchronous allocation of TUN/TAP devices using tokio"
categories = ["asynchronous", "network-programming"]
keywords = ["tun", "tap", "async", "tokio"]
repository = "https://github.com/yaa110/tokio-tun"
version = "0.8.0"

[dependencies]
tokio = { version = "1", features = ["net"] }
anyhow = "1"
libc = "0.2"
nix = { version = "0.25", default-features = false, features = ["ioctl"] }
nix = {version = "0.26", default-features = false, features = ["ioctl"]}
tokio = {version = "1", features = ["net"]}

[dev-dependencies]
tokio = { version = "1", features = ["full"] }
tokio = {version = "1", features = ["full"]}
21 changes: 13 additions & 8 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,12 @@ Asynchronous allocation of TUN/TAP devices in Rust using [`tokio`](https://crate

## Getting Started

- Create a tun device using `TunBuilder` and read from it in a loop:
- Create a tun device using `Tun::builder()` and read from it in a loop:

```rust
#[tokio::main]
async fn main() -> Result<()> {
let tun = TunBuilder::new()
let tun = Tun::builder()
.name("") // if name is empty, then it is set by kernel.
.tap(false) // false (default): TUN, true: TAP.
.packet_info(false) // false: IFF_NO_PI, default is true.
Expand All @@ -33,26 +33,26 @@ async fn main() -> Result<()> {
- Run the code using `sudo`:

```bash
sudo -E /path/to/cargo run
sudo -E $(which cargo) run
```

- Set the address of device (address and netmask could also be set using `TunBuilder`):

```bash
sudo ip a add 10.0.0.1/24 dev <tun-name>
sudo ip a add 10.0.0.1/24 dev <tun-name>
```

- Ping to read packets:

```bash
ping 10.0.0.2
ping 10.0.0.2
```

- Display devices and analyze the network traffic:

```
ip tuntap
sudo tshark -i <tun-name>
```bash
ip tuntap
sudo tshark -i <tun-name>
```

## Supported Platforms
Expand All @@ -68,3 +68,8 @@ async fn main() -> Result<()> {

- [`read`](examples/read.rs): Split tun to (reader, writer) pair and read packets from reader.
- [`read-mq`](examples/read-mq.rs): Read from multi-queue tun using `tokio::select!`.

```bash
sudo -E $(which cargo) run --example read
sudo -E $(which cargo) run --example read-mq
```
6 changes: 3 additions & 3 deletions examples/read-mq.rs
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
use anyhow::Result;
use std::net::Ipv4Addr;
use std::os::unix::io::AsRawFd;
use tokio::io::AsyncReadExt;
use tokio_tun::result::Result;
use tokio_tun::TunBuilder;
use tokio_tun::Tun;

#[tokio::main]
async fn main() -> Result<()> {
let queues = 3;

let tuns = TunBuilder::new()
let tuns = Tun::builder()
.name("")
.tap(false)
.packet_info(false)
Expand Down
6 changes: 3 additions & 3 deletions examples/read.rs
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
use anyhow::Result;
use std::net::Ipv4Addr;
use std::os::unix::io::AsRawFd;
use tokio::io::AsyncReadExt;
use tokio_tun::result::Result;
use tokio_tun::TunBuilder;
use tokio_tun::Tun;

#[tokio::main]
async fn main() -> Result<()> {
let tun = TunBuilder::new()
let tun = Tun::builder()
.name("")
.tap(false)
.packet_info(false)
Expand Down
20 changes: 10 additions & 10 deletions src/builder.rs
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
use super::result::Result;
#[cfg(target_os = "linux")]
use crate::linux::params::Params;
#[cfg(target_os = "linux")]
use crate::tun::Tun;
use anyhow::Result;
use core::convert::From;
use libc::{IFF_NO_PI, IFF_TAP, IFF_TUN};
use std::net::Ipv4Addr;

/// Represents a factory to build new instances of [`Tun`](struct.Tun.html).
pub struct TunBuilder<'a> {
name: &'a str,
pub struct TunBuilder {
name: String,
is_tap: bool,
packet_info: bool,
persist: bool,
Expand All @@ -23,10 +23,10 @@ pub struct TunBuilder<'a> {
netmask: Option<Ipv4Addr>,
}

impl<'a> Default for TunBuilder<'a> {
impl Default for TunBuilder {
fn default() -> Self {
Self {
name: "",
name: "".into(),
owner: None,
group: None,
is_tap: false,
Expand All @@ -42,15 +42,15 @@ impl<'a> Default for TunBuilder<'a> {
}
}

impl<'a> TunBuilder<'a> {
impl TunBuilder {
/// Creates a new instance of [`TunBuilder`](struct.TunBuilder.html).
pub fn new() -> Self {
Default::default()
}

/// Sets the name of device (max length: 16 characters), if it is empty, then device name is set by kernel. Default value is empty.
pub fn name(mut self, name: &'a str) -> Self {
self.name = name;
pub fn name(mut self, name: &str) -> Self {
self.name = name.into();
self
}

Expand Down Expand Up @@ -176,14 +176,14 @@ impl<'a> TunBuilder<'a> {
}
}

impl<'a> From<TunBuilder<'a>> for Params {
impl From<TunBuilder> for Params {
#[cfg(target_os = "linux")]
fn from(builder: TunBuilder) -> Self {
Params {
name: if builder.name.is_empty() {
None
} else {
Some(builder.name.into())
Some(builder.name)
},
flags: {
let mut flags = if builder.is_tap { IFF_TAP } else { IFF_TUN } as _;
Expand Down
2 changes: 0 additions & 2 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,5 @@ mod linux {
mod builder;
mod tun;

pub mod result;

pub use self::builder::TunBuilder;
pub use self::tun::Tun;
17 changes: 2 additions & 15 deletions src/linux/address.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,32 +7,19 @@ pub trait Ipv4AddrExt {
fn from_address(sock: sockaddr) -> Self;
}

fn hton(octets: [u8; 4]) -> u32 {
(octets[3] as u32) << 24 | (octets[2] as u32) << 16 | (octets[1] as u32) << 8 | octets[0] as u32
}

fn ntoh(number: u32) -> [u8; 4] {
[
(number & 0xff) as u8,
(number >> 8 & 0xff) as u8,
(number >> 16 & 0xff) as u8,
(number >> 24 & 0xff) as u8,
]
}

impl Ipv4AddrExt for Ipv4Addr {
fn to_address(&self) -> sockaddr {
let mut addr: libc::sockaddr_in = unsafe { mem::zeroed() };
addr.sin_family = libc::AF_INET as _;
addr.sin_addr = libc::in_addr {
s_addr: hton(self.octets()),
s_addr: u32::from_ne_bytes(self.octets()),
};
addr.sin_port = 0;
unsafe { mem::transmute(addr) }
}

fn from_address(addr: sockaddr) -> Self {
let sock: libc::sockaddr_in = unsafe { mem::transmute(addr) };
ntoh(sock.sin_addr.s_addr).into()
sock.sin_addr.s_addr.to_ne_bytes().into()
}
}
2 changes: 1 addition & 1 deletion src/linux/interface.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use super::params::Params;
use super::request::ifreq;
use crate::linux::address::Ipv4AddrExt;
use crate::result::Result;
use anyhow::Result;
use std::net::Ipv4Addr;

nix::ioctl_write_int!(tunsetiff, b'T', 202);
Expand Down
5 changes: 0 additions & 5 deletions src/result.rs

This file was deleted.

7 changes: 6 additions & 1 deletion src/tun.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
use crate::linux::interface::Interface;
use crate::linux::io::TunIo;
use crate::linux::params::Params;
use crate::result::Result;
use crate::TunBuilder;
use anyhow::Result;
use std::io;
use std::io::{Read, Write};
use std::net::Ipv4Addr;
Expand Down Expand Up @@ -92,6 +93,10 @@ impl AsyncWrite for Tun {
}

impl Tun {
pub fn builder() -> TunBuilder {
TunBuilder::new()
}

/// Creates a new instance of Tun/Tap device.
pub(crate) fn new(params: Params) -> Result<Self> {
let iface = Self::allocate(params, 1)?;
Expand Down

0 comments on commit 84b6f37

Please sign in to comment.