diff options
author | Mathias Hall-Andersen <mathias@hall-andersen.dk> | 2019-10-23 12:08:35 +0200 |
---|---|---|
committer | Mathias Hall-Andersen <mathias@hall-andersen.dk> | 2019-10-23 12:08:35 +0200 |
commit | ee3599d5507ceee23ef3382dbda9de8e73c54a00 (patch) | |
tree | d681a3f8a5a2d5e7bea779acecd1fc0798285d9e /src/platform/bind.rs | |
parent | Work on platform specific code (Linux) (diff) | |
download | wireguard-rs-ee3599d5507ceee23ef3382dbda9de8e73c54a00.tar.xz wireguard-rs-ee3599d5507ceee23ef3382dbda9de8e73c54a00.zip |
Moved IO traits into platform module
Diffstat (limited to 'src/platform/bind.rs')
-rw-r--r-- | src/platform/bind.rs | 43 |
1 files changed, 43 insertions, 0 deletions
diff --git a/src/platform/bind.rs b/src/platform/bind.rs new file mode 100644 index 0000000..f22a5d7 --- /dev/null +++ b/src/platform/bind.rs @@ -0,0 +1,43 @@ +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 Bind: 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 set_fwmark(&self, value: Option<u32>) -> Option<Self::Error>; +} + +/// On some platforms the application can itself bind to a socket. +/// This enables configuration using the UAPI interface. +pub trait Platform: Bind { + 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>; +} |