diff options
| author | Janito Vaqueiro Ferreira Filho <janito@mullvad.net> | 2020-01-23 19:56:02 +0000 |
|---|---|---|
| committer | Janito Vaqueiro Ferreira Filho <janito@mullvad.net> | 2020-02-06 12:05:57 +0000 |
| commit | 391829b302256cd37c39cc854b528721ff4fc039 (patch) | |
| tree | d6b8062fc149cb54ea763ff5f9655ef38d2955cc | |
| parent | 900e2e50f1883f1bae373001b929085d35a16059 (diff) | |
| download | mullvadvpn-391829b302256cd37c39cc854b528721ff4fc039.tar.xz mullvadvpn-391829b302256cd37c39cc854b528721ff4fc039.zip | |
Create `AbstractIpNetwork` concept trait
| -rw-r--r-- | talpid-core/src/tunnel/tun_provider/android/ipnetwork_sub.rs | 75 | ||||
| -rw-r--r-- | talpid-core/src/tunnel/tun_provider/android/mod.rs | 2 |
2 files changed, 77 insertions, 0 deletions
diff --git a/talpid-core/src/tunnel/tun_provider/android/ipnetwork_sub.rs b/talpid-core/src/tunnel/tun_provider/android/ipnetwork_sub.rs new file mode 100644 index 0000000000..a4feb1f0f7 --- /dev/null +++ b/talpid-core/src/tunnel/tun_provider/android/ipnetwork_sub.rs @@ -0,0 +1,75 @@ +use ipnetwork::{Ipv4Network, Ipv6Network}; +use std::{ + fmt::Debug, + ops::{Add, BitAnd, BitXor, Not, Shl, Sub}, +}; + +pub trait AbstractIpNetwork: Clone + Copy + 'static { + type Representation: Add<Output = Self::Representation> + + BitAnd<Output = Self::Representation> + + BitXor<Output = Self::Representation> + + Clone + + Copy + + Debug + + PartialEq + + Not<Output = Self::Representation> + + Shl<u8, Output = Self::Representation> + + Sub<Output = Self::Representation>; + + const ZERO: Self::Representation; + const ONE: Self::Representation; + const MAX_PREFIX: u8; + + fn new(network: Self::Representation, prefix: u8) -> Self; + fn mask(self) -> Self::Representation; + fn network(self) -> Self::Representation; + fn prefix(self) -> u8; +} + +impl AbstractIpNetwork for Ipv4Network { + type Representation = u32; + + const ZERO: Self::Representation = 0; + const ONE: Self::Representation = 1; + const MAX_PREFIX: u8 = 32; + + fn new(network: Self::Representation, prefix: u8) -> Self { + Ipv4Network::new(network.into(), prefix).expect("Invalid IPv4 network prefix") + } + + fn mask(self) -> Self::Representation { + Ipv4Network::mask(&self).into() + } + + fn network(self) -> Self::Representation { + Ipv4Network::network(&self).into() + } + + fn prefix(self) -> u8 { + Ipv4Network::prefix(&self) + } +} + +impl AbstractIpNetwork for Ipv6Network { + type Representation = u128; + + const ZERO: Self::Representation = 0; + const ONE: Self::Representation = 1; + const MAX_PREFIX: u8 = 128; + + fn new(network: Self::Representation, prefix: u8) -> Self { + Ipv6Network::new(network.into(), prefix).expect("Invalid IPv6 network prefix") + } + + fn mask(self) -> Self::Representation { + Ipv6Network::mask(&self).into() + } + + fn network(self) -> Self::Representation { + Ipv6Network::network(&self).into() + } + + fn prefix(self) -> u8 { + Ipv6Network::prefix(&self) + } +} diff --git a/talpid-core/src/tunnel/tun_provider/android/mod.rs b/talpid-core/src/tunnel/tun_provider/android/mod.rs index 487598332c..229118656f 100644 --- a/talpid-core/src/tunnel/tun_provider/android/mod.rs +++ b/talpid-core/src/tunnel/tun_provider/android/mod.rs @@ -1,3 +1,5 @@ +mod ipnetwork_sub; + use super::TunConfig; use ipnetwork::IpNetwork; use jnix::{ |
