diff options
author | Mathias Hall-Andersen <mathias@hall-andersen.dk> | 2019-08-17 16:31:08 +0200 |
---|---|---|
committer | Mathias Hall-Andersen <mathias@hall-andersen.dk> | 2019-08-17 16:31:08 +0200 |
commit | 78ab1a93e6d519bf404fbe61fc7ec3c3ab35a72a (patch) | |
tree | 75106e1ff89a03a6869184994b902a70315dfc30 /src/types | |
parent | Begin drafting cross-platform interface (diff) | |
download | wireguard-rs-78ab1a93e6d519bf404fbe61fc7ec3c3ab35a72a.tar.xz wireguard-rs-78ab1a93e6d519bf404fbe61fc7ec3c3ab35a72a.zip |
Remove peer from cryptkey router on drop
Diffstat (limited to 'src/types')
-rw-r--r-- | src/types/keys.rs | 26 | ||||
-rw-r--r-- | src/types/mod.rs | 31 | ||||
-rw-r--r-- | src/types/tun.rs | 43 | ||||
-rw-r--r-- | src/types/udp.rs | 26 |
4 files changed, 101 insertions, 25 deletions
diff --git a/src/types/keys.rs b/src/types/keys.rs new file mode 100644 index 0000000..0b52d18 --- /dev/null +++ b/src/types/keys.rs @@ -0,0 +1,26 @@ +use std::time::Instant; + +/* This file holds types passed between components. + * Whenever a type cannot be held local to a single module. + */ + +#[derive(Debug, Clone, Copy)] +pub struct Key { + pub key: [u8; 32], + pub id: u32, +} + +#[cfg(test)] +impl PartialEq for Key { + fn eq(&self, other: &Self) -> bool { + self.id == other.id && self.key[..] == other.key[..] + } +} + +#[derive(Debug, Clone, Copy)] +pub struct KeyPair { + pub birth: Instant, // when was the key-pair created + pub confirmed: bool, // has the key-pair been confirmed? + pub send: Key, // key for outbound messages + pub recv: Key, // key for inbound messages +}
\ No newline at end of file diff --git a/src/types/mod.rs b/src/types/mod.rs index ea7c570..868fb71 100644 --- a/src/types/mod.rs +++ b/src/types/mod.rs @@ -1,26 +1,7 @@ -use std::time::Instant; +mod keys; +mod tun; +mod udp; -/* This file holds types passed between components. - * Whenever a type cannot be held local to a single module. - */ - -#[derive(Debug, Clone, Copy)] -pub struct Key { - pub key: [u8; 32], - pub id: u32, -} - -#[cfg(test)] -impl PartialEq for Key { - fn eq(&self, other: &Self) -> bool { - self.id == other.id && self.key[..] == other.key[..] - } -} - -#[derive(Debug, Clone, Copy)] -pub struct KeyPair { - pub birth: Instant, // when was the key-pair created - pub confirmed: bool, // has the key-pair been confirmed? - pub send: Key, // key for outbound messages - pub recv: Key, // key for inbound messages -} +pub use keys::{Key, KeyPair}; +pub use tun::Tun; +pub use udp::Bind;
\ No newline at end of file diff --git a/src/types/tun.rs b/src/types/tun.rs new file mode 100644 index 0000000..72caa71 --- /dev/null +++ b/src/types/tun.rs @@ -0,0 +1,43 @@ +use std::error; + +pub trait Tun: Send + Sync { + type Error: error::Error; + + /// Returns the MTU of the device + /// + /// This function needs to be efficient (called for every read). + /// The goto implementation stragtegy is to .load an atomic variable, + /// then use e.g. netlink to update the variable in a seperate thread. + /// + /// # Returns + /// + /// The MTU of the interface in bytes + fn mtu(&self) -> usize; + + /// Reads an IP packet into dst[offset:] from the tunnel device + /// + /// The reason for providing space for a prefix + /// is to efficiently accommodate platforms on which the packet is prefaced by a header. + /// This space is later used to construct the transport message inplace. + /// + /// # Arguments + /// + /// - dst: Destination buffer (enough space for MTU bytes + header) + /// - offset: Offset for the beginning of the IP packet + /// + /// # Returns + /// + /// The size of the IP packet (ignoring the header) or an std::error::Error instance: + fn read(&self, dst: &mut [u8], offset: usize) -> Result<usize, Self::Error>; + + /// Writes an IP packet to the tunnel device + /// + /// # Arguments + /// + /// - src: Buffer containing the IP packet to be written + /// + /// # Returns + /// + /// Unit type or an error + fn write(&self, src: &[u8]) -> Result<(), Self::Error>; +} diff --git a/src/types/udp.rs b/src/types/udp.rs new file mode 100644 index 0000000..f45cf85 --- /dev/null +++ b/src/types/udp.rs @@ -0,0 +1,26 @@ +use std::error; + +/* Often times an a file descriptor in an atomic might suffice. + */ +pub trait Bind<Endpoint>: Send + Sync { + type Error : error::Error; + + fn new() -> Self; + + /// 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>; + + /// Returns the current port of the bind + fn get_port(&self) -> u16; + + fn recv(&self, dst: &mut [u8]) -> Endpoint; + fn send(&self, src: &[u8], dst: &Endpoint); +} |