From 496ca31b8035e68c6e95fd1aadb8c08d81adc472 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Em=C4=ABls=20Pi=C5=86=C4=B7is?= Date: Mon, 26 Apr 2021 16:07:56 +0100 Subject: [PATCH] Pass whole buffer and offset to tunnel writer --- src/platform/linux/tun.rs | 5 +++-- src/platform/tun.rs | 2 +- src/wireguard/router/receive.rs | 2 +- 3 files changed, 5 insertions(+), 4 deletions(-) diff --git a/src/platform/linux/tun.rs b/src/platform/linux/tun.rs index 82fada1..304966d 100644 --- a/src/platform/linux/tun.rs +++ b/src/platform/linux/tun.rs @@ -106,8 +106,9 @@ impl Reader for LinuxTunReader { impl Writer for LinuxTunWriter { type Error = LinuxTunError; - fn write(&self, src: &[u8]) -> Result<(), Self::Error> { - match unsafe { libc::write(self.fd, src.as_ptr() as _, src.len() as _) } { + fn write(&self, src: &[u8], offset: usize) -> Result<(), Self::Error> { + let data = &src[offset..]; + match unsafe { libc::write(self.fd, data.as_ptr() as _, data.len() as _) } { -1 => Err(LinuxTunError::Closed), _ => Ok(()), } diff --git a/src/platform/tun.rs b/src/platform/tun.rs index 38c95bf..b54179c 100644 --- a/src/platform/tun.rs +++ b/src/platform/tun.rs @@ -25,7 +25,7 @@ pub trait Writer: Send + Sync + 'static { /// # Returns /// /// Unit type or an error - fn write(&self, src: &[u8]) -> Result<(), Self::Error>; + fn write(&self, src: &mut [u8], offset: usize) -> Result<(), Self::Error>; } pub trait Reader: Send + 'static { diff --git a/src/wireguard/router/receive.rs b/src/wireguard/router/receive.rs index 15eb8fb..034216a 100644 --- a/src/wireguard/router/receive.rs +++ b/src/wireguard/router/receive.rs @@ -173,7 +173,7 @@ impl> SequentialJob // (keep-alive and malformed packets will have no inner length) if let Some(inner) = inner_length(packet) { if inner + SIZE_TAG <= packet.len() { - let _ = peer.device.inbound.write(&packet[..inner]).map_err(|e| { + let _ = peer.device.inbound.write(&packet, inner).map_err(|e| { log::debug!("failed to write inbound packet to TUN: {:?}", e); }); }