diff --git a/README.md b/README.md index 4961ff7e..3cb277cb 100644 --- a/README.md +++ b/README.md @@ -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 @@ -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: @@ -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 diff --git a/src/async/codec.rs b/src/async/codec.rs index 07b2b55a..f81dd088 100644 --- a/src/async/codec.rs +++ b/src/async/codec.rs @@ -14,7 +14,6 @@ use std::io; -use byteorder::{NativeEndian, NetworkEndian, WriteBytesExt}; use bytes::{BufMut, Bytes, BytesMut}; use tokio_util::codec::{Decoder, Encoder}; @@ -54,6 +53,7 @@ impl PacketProtocol { } #[cfg(target_os = "windows")] + #[allow(dead_code)] fn into_pi_field(self) -> Result { unimplemented!() } @@ -132,19 +132,24 @@ impl Encoder 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::::with_capacity(4); - - // flags is always 0 - buf.write_u16::(0)?; - // write the protocol as network byte order - buf.write_u16::(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::::with_capacity(4); + + // flags is always 0 + buf.write_u16::(0)?; + // write the protocol as network byte order + buf.write_u16::(_proto.into_pi_field()?)?; + + dst.put_slice(&buf); + } dst.put(bytes); } TunPacket(_, bytes) => dst.put(bytes), diff --git a/src/error.rs b/src/error.rs index f71efefe..f6e9e53b 100644 --- a/src/error.rs +++ b/src/error.rs @@ -54,4 +54,13 @@ pub enum Error { WintunError(#[from] wintun::Error), } +impl From for io::Error { + fn from(value: Error) -> Self { + match value { + Error::Io(err) => err, + _ => io::Error::new(io::ErrorKind::Other, value), + } + } +} + pub type Result = ::std::result::Result;