diff options
| author | Emīls Piņķis <emils@mullvad.net> | 2018-11-21 11:10:00 +0000 |
|---|---|---|
| committer | Emīls Piņķis <emils@mullvad.net> | 2018-11-21 16:39:46 +0000 |
| commit | 9f72a8be77d60e14c103ceb6c893c15089a173ee (patch) | |
| tree | 67fbaa332c57dace461b1e1b42cf58506f3ba9f8 | |
| parent | 1a5099d9b437e0d94373e7389905280a440b35a0 (diff) | |
| download | mullvadvpn-9f72a8be77d60e14c103ceb6c893c15089a173ee.tar.xz mullvadvpn-9f72a8be77d60e14c103ceb6c893c15089a173ee.zip | |
Add routing trait to talpid-core
| -rw-r--r-- | talpid-core/src/lib.rs | 4 | ||||
| -rw-r--r-- | talpid-core/src/routing/mod.rs | 56 |
2 files changed, 60 insertions, 0 deletions
diff --git a/talpid-core/src/lib.rs b/talpid-core/src/lib.rs index 20c9302c8a..c570d94f85 100644 --- a/talpid-core/src/lib.rs +++ b/talpid-core/src/lib.rs @@ -43,6 +43,10 @@ extern crate talpid_types; #[cfg(windows)] mod winnet; +#[cfg(unix)] +/// Abstraction over operating system routing table. +pub mod routing; + mod offline; /// Working with processes. diff --git a/talpid-core/src/routing/mod.rs b/talpid-core/src/routing/mod.rs new file mode 100644 index 0000000000..864a824d06 --- /dev/null +++ b/talpid-core/src/routing/mod.rs @@ -0,0 +1,56 @@ +use ipnetwork::IpNetwork; +use std::net::IpAddr; + +/// A single route +#[derive(Hash, Eq, PartialEq)] +pub struct Route { + /// Route prefix + pub prefix: IpNetwork, + /// Route node + pub node: NetNode, +} + +impl Route { + /// Create a new route + pub fn new(prefix: IpNetwork, node: NetNode) -> Self { + Self { prefix, node } + } +} + +/// A network node for a given route +#[derive(Hash, Eq, PartialEq, Clone)] +pub enum NetNode { + /// For routing something through a network host + Address(IpAddr), + /// For routing something through an interface + Device(String), +} + +/// Contains a set of routes to be added +pub struct RequiredRoutes { + /// List of routes to be applied to the routing table. + pub routes: Vec<Route>, + /// Optionally apply the routes to a specific table and only apply routes when a firewall mark + /// is not used. Currently only used on Linux. + pub fwmark: Option<String>, +} + + +/// This trait unifies platform specific implementations of route managers +pub trait RoutingT: Sized { + /// Error type of the implementation + type Error: ::std::error::Error; + + /// Creates a new router + fn new() -> Result<Self, Self::Error>; + + /// Adds routes to the system routing table. + fn add_routes(&mut self, required_routes: RequiredRoutes) -> Result<(), Self::Error>; + + /// Removes previously set routes. If routes were set for specific tables, the whole tables + /// will be removed. + fn delete_routes(&mut self) -> Result<(), Self::Error>; + + /// Retrieves the gateway for the default route + fn get_default_route_node(&mut self) -> Result<std::net::IpAddr, Self::Error>; +} |
