Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve tunnel writer trait #19

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions src/platform/linux/tun.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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(()),
}
Expand Down
2 changes: 1 addition & 1 deletion src/platform/tun.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
2 changes: 1 addition & 1 deletion src/wireguard/router/receive.rs
Original file line number Diff line number Diff line change
Expand Up @@ -173,7 +173,7 @@ impl<E: Endpoint, C: Callbacks, T: tun::Writer, B: udp::Writer<E>> 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);
});
}
Expand Down