diff options
author | Mathias Hall-Andersen <mathias@hall-andersen.dk> | 2019-11-24 18:41:43 +0100 |
---|---|---|
committer | Mathias Hall-Andersen <mathias@hall-andersen.dk> | 2019-11-24 18:41:43 +0100 |
commit | 3bff078e3f1c59454d8db14e5dc7603e6fdbeaba (patch) | |
tree | da301a422eb8aa38ac7960324b8b6fe2d0909302 /src/platform/udp.rs | |
parent | Daemonization (diff) | |
download | wireguard-rs-3bff078e3f1c59454d8db14e5dc7603e6fdbeaba.tar.xz wireguard-rs-3bff078e3f1c59454d8db14e5dc7603e6fdbeaba.zip |
Make IO traits suitable for Tun events (up/down)
Diffstat (limited to 'src/platform/udp.rs')
-rw-r--r-- | src/platform/udp.rs | 47 |
1 files changed, 47 insertions, 0 deletions
diff --git a/src/platform/udp.rs b/src/platform/udp.rs new file mode 100644 index 0000000..3671229 --- /dev/null +++ b/src/platform/udp.rs @@ -0,0 +1,47 @@ +use super::Endpoint; +use std::error::Error; + +pub trait Reader<E: Endpoint>: Send + Sync { + type Error: Error; + + fn read(&self, buf: &mut [u8]) -> Result<(usize, E), Self::Error>; +} + +pub trait Writer<E: Endpoint>: Send + Sync + Clone + 'static { + type Error: Error; + + fn write(&self, buf: &[u8], dst: &E) -> Result<(), Self::Error>; +} + +pub trait UDP: Send + Sync + 'static { + type Error: Error; + type Endpoint: Endpoint; + + /* Until Rust gets type equality constraints these have to be generic */ + type Writer: Writer<Self::Endpoint>; + type Reader: Reader<Self::Endpoint>; +} + +/// On platforms where fwmark can be set and the +/// implementation can bind to a new port during later configuration (UAPI support), +/// this type provides the ability to set the fwmark and close the socket (by dropping the instance) +pub trait Owner: Send { + type Error: Error; + + fn get_port(&self) -> u16; + + fn get_fwmark(&self) -> Option<u32>; + + fn set_fwmark(&mut self, value: Option<u32>) -> Result<(), Self::Error>; +} + +/// On some platforms the application can itself bind to a socket. +/// This enables configuration using the UAPI interface. +pub trait PlatformUDP: UDP { + type Owner: Owner; + + /// Bind to a new port, returning the reader/writer and + /// an associated instance of the owner type, which closes the UDP socket upon "drop" + /// and enables configuration of the fwmark value. + fn bind(port: u16) -> Result<(Vec<Self::Reader>, Self::Writer, Self::Owner), Self::Error>; +} |