Skip to content

Commit

Permalink
refine TunPacketCodec
Browse files Browse the repository at this point in the history
  • Loading branch information
ssrlive committed Jan 11, 2024
1 parent e944758 commit 8f2f109
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 16 deletions.
10 changes: 7 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
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/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.

Usage
Expand All @@ -8,7 +12,7 @@ First, add the following to your `Cargo.toml`:

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

Next, add this to your crate root:
Expand All @@ -21,7 +25,7 @@ If you want to use the TUN interface with mio/tokio, you need to enable the `asy

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

Example
Expand Down
31 changes: 18 additions & 13 deletions src/async/codec.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@

use std::io;

use byteorder::{NativeEndian, NetworkEndian, WriteBytesExt};
use bytes::{BufMut, Bytes, BytesMut};
use tokio_util::codec::{Decoder, Encoder};

Expand Down Expand Up @@ -54,6 +53,7 @@ impl PacketProtocol {
}

#[cfg(target_os = "windows")]
#[allow(dead_code)]
fn into_pi_field(self) -> Result<u16, io::Error> {
unimplemented!()
}
Expand Down Expand Up @@ -132,19 +132,24 @@ impl Encoder<TunPacket> for TunPacketCodec {
type Error = io::Error;

fn encode(&mut self, item: TunPacket, dst: &mut BytesMut) -> Result<(), Self::Error> {
dst.reserve(item.get_bytes().len() + 4);
dst.reserve(item.get_bytes().len() + if self.0 { 4 } else { 0 });
match item {
TunPacket(proto, bytes) if self.0 => {
// build the packet information header comprising of 2 u16
// fields: flags and protocol.
let mut buf = Vec::<u8>::with_capacity(4);

// flags is always 0
buf.write_u16::<NativeEndian>(0)?;
// write the protocol as network byte order
buf.write_u16::<NetworkEndian>(proto.into_pi_field()?)?;

dst.put_slice(&buf);
TunPacket(_proto, bytes) if self.0 => {
#[cfg(unix)]
{
use byteorder::{NativeEndian, NetworkEndian, WriteBytesExt};

// build the packet information header comprising of 2 u16
// fields: flags and protocol.
let mut buf = Vec::<u8>::with_capacity(4);

// flags is always 0
buf.write_u16::<NativeEndian>(0)?;
// write the protocol as network byte order
buf.write_u16::<NetworkEndian>(_proto.into_pi_field()?)?;

dst.put_slice(&buf);
}
dst.put(bytes);
}
TunPacket(_, bytes) => dst.put(bytes),
Expand Down
9 changes: 9 additions & 0 deletions src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -54,4 +54,13 @@ pub enum Error {
WintunError(#[from] wintun::Error),
}

impl From<Error> for io::Error {
fn from(value: Error) -> Self {
match value {
Error::Io(err) => err,
_ => io::Error::new(io::ErrorKind::Other, value),
}
}
}

pub type Result<T, E = Error> = ::std::result::Result<T, E>;

0 comments on commit 8f2f109

Please sign in to comment.