Skip to content

Commit

Permalink
fwproto: Implement Windows named pipe
Browse files Browse the repository at this point in the history
  • Loading branch information
mbuesch committed Jan 5, 2025
1 parent c6e9cb6 commit 6fe35db
Showing 1 changed file with 27 additions and 20 deletions.
47 changes: 27 additions & 20 deletions letmein-fwproto/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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";

Expand Down Expand Up @@ -339,25 +339,32 @@ pub trait Stream {
fn try_write(&self, buf: &[u8]) -> std::io::Result<usize>;
}

#[cfg(any(target_os = "linux", target_os = "android"))]
impl Stream for UnixStream {
fn readable(&self) -> impl Future<Output = std::io::Result<()>> + Send {
self.readable()
}

fn try_read(&self, buf: &mut [u8]) -> std::io::Result<usize> {
self.try_read(buf)
}

fn writable(&self) -> impl Future<Output = std::io::Result<()>> + Send {
self.writable()
}

fn try_write(&self, buf: &[u8]) -> std::io::Result<usize> {
self.try_write(buf)
}
macro_rules! impl_stream_for {
($ty:ty) => {
impl Stream for $ty {
fn readable(&self) -> impl Future<Output = std::io::Result<()>> + Send {
self.readable()
}
fn try_read(&self, buf: &mut [u8]) -> std::io::Result<usize> {
self.try_read(buf)
}
fn writable(&self) -> impl Future<Output = std::io::Result<()>> + Send {
self.writable()
}
fn try_write(&self, buf: &[u8]) -> std::io::Result<usize> {
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::*;
Expand Down

0 comments on commit 6fe35db

Please sign in to comment.