diff options
Diffstat (limited to 'src/types/bind.rs')
-rw-r--r-- | src/types/bind.rs | 79 |
1 files changed, 17 insertions, 62 deletions
diff --git a/src/types/bind.rs b/src/types/bind.rs index 62adbbb..fcc38c8 100644 --- a/src/types/bind.rs +++ b/src/types/bind.rs @@ -1,73 +1,28 @@ use super::Endpoint; -use std::error; +use std::error::Error; -/// Traits representing the "internet facing" end of the VPN. -/// -/// In practice this is a UDP socket (but the router interface is agnostic). -/// Often these traits will be implemented on the same type. +pub trait Reader<E: Endpoint>: Send + Sync { + type Error: Error; -/// Bind interface provided to the router code -pub trait RouterBind: Send + Sync { - type Error: error::Error; - type Endpoint: Endpoint; + fn read(&self, buf: &mut [u8]) -> Result<(usize, E), Self::Error>; +} - /// Receive a buffer on the bind - /// - /// # Arguments - /// - /// - `buf`, buffer for storing the packet. If the buffer is too short, the packet should just be truncated. - /// - /// # Note - /// - /// The size of the buffer is derieved from the MTU of the Tun device. - fn recv(&self, buf: &mut [u8]) -> Result<(usize, Self::Endpoint), Self::Error>; +pub trait Writer<E: Endpoint>: Send + Sync + Clone + 'static { + type Error: Error; - /// Send a buffer to the endpoint - /// - /// # Arguments - /// - /// - `buf`, packet src buffer (in practice the body of a UDP datagram) - /// - `dst`, destination endpoint (in practice, src: (ip, port) + dst: (ip, port) for sticky sockets) - /// - /// # Returns - /// - /// The unit type or an error if transmission failed - fn send(&self, buf: &[u8], dst: &Self::Endpoint) -> Result<(), Self::Error>; + fn write(&self, buf: &[u8], dst: &E) -> Result<(), Self::Error>; } -/// Bind interface provided for configuration (setting / getting the port) -pub trait ConfigBind { - type Error: error::Error; - - /// Return a new (unbound) instance of a configuration bind - fn new() -> Self; +pub trait Bind: Send + Sync + 'static { + type Error: Error; + type Endpoint: Endpoint; - /// Updates the port of the bind - /// - /// # Arguments - /// - /// - `port`, the new port to bind to. 0 means any available port. - /// - /// # Returns - /// - /// The unit type or an error, if binding fails - fn set_port(&self, port: u16) -> Result<(), Self::Error>; + /* Until Rust gets type equality constraints these have to be generic */ + type Writer: Writer<Self::Endpoint>; + type Reader: Reader<Self::Endpoint>; - /// Returns the current port of the bind - fn get_port(&self) -> Option<u16>; + /* Used to close the reader/writer when binding to a new port */ + type Closer; - /// Set the mark (e.g. on Linus this is the fwmark) on the bind - /// - /// # Arguments - /// - /// - `mark`, the mark to set - /// - /// # Note - /// - /// The mark should be retained accross calls to `set_port`. - /// - /// # Returns - /// - /// The unit type or an error, if the operation fails due to permission errors - fn set_mark(&self, mark: u16) -> Result<(), Self::Error>; + fn bind(port: u16) -> Result<(Self::Reader, Self::Writer, Self::Closer, u16), Self::Error>; } |