From 6fe35db17f116a9cb6c906f98fbe688ce18d13e1 Mon Sep 17 00:00:00 2001 From: Michael Buesch Date: Sun, 5 Jan 2025 22:32:58 +0100 Subject: [PATCH] fwproto: Implement Windows named pipe --- letmein-fwproto/src/lib.rs | 47 ++++++++++++++++++++++---------------- 1 file changed, 27 insertions(+), 20 deletions(-) diff --git a/letmein-fwproto/src/lib.rs b/letmein-fwproto/src/lib.rs index c5d1900..aac2453 100644 --- a/letmein-fwproto/src/lib.rs +++ b/letmein-fwproto/src/lib.rs @@ -14,9 +14,6 @@ #![forbid(unsafe_code)] -#[cfg(not(any(target_os = "linux", target_os = "android")))] -std::compile_error!("letmeind server and letmein-fwproto do not support non-Linux platforms."); - use anyhow::{self as ah, format_err as err, Context as _}; use std::{ future::Future, @@ -27,6 +24,9 @@ use tokio::io::ErrorKind; #[cfg(any(target_os = "linux", target_os = "android"))] use tokio::net::UnixStream; +#[cfg(target_os = "windows")] +use tokio::net::windows::named_pipe::{NamedPipeClient, NamedPipeServer}; + /// Firewall daemon Unix socket file name. pub const SOCK_FILE: &str = "letmeinfwd.sock"; @@ -339,25 +339,32 @@ pub trait Stream { fn try_write(&self, buf: &[u8]) -> std::io::Result; } -#[cfg(any(target_os = "linux", target_os = "android"))] -impl Stream for UnixStream { - fn readable(&self) -> impl Future> + Send { - self.readable() - } - - fn try_read(&self, buf: &mut [u8]) -> std::io::Result { - self.try_read(buf) - } - - fn writable(&self) -> impl Future> + Send { - self.writable() - } - - fn try_write(&self, buf: &[u8]) -> std::io::Result { - self.try_write(buf) - } +macro_rules! impl_stream_for { + ($ty:ty) => { + impl Stream for $ty { + fn readable(&self) -> impl Future> + Send { + self.readable() + } + fn try_read(&self, buf: &mut [u8]) -> std::io::Result { + self.try_read(buf) + } + fn writable(&self) -> impl Future> + Send { + self.writable() + } + fn try_write(&self, buf: &[u8]) -> std::io::Result { + self.try_write(buf) + } + } + }; } +#[cfg(any(target_os = "linux", target_os = "android"))] +impl_stream_for!(UnixStream); +#[cfg(target_os = "windows")] +impl_stream_for!(NamedPipeClient); +#[cfg(target_os = "windows")] +impl_stream_for!(NamedPipeServer); + #[cfg(test)] mod tests { use super::*;