diff options
| author | David Lönnhager <david.l@mullvad.net> | 2024-09-02 12:21:54 +0200 |
|---|---|---|
| committer | David Lönnhager <david.l@mullvad.net> | 2024-09-02 15:01:24 +0200 |
| commit | 6df61153bc7385587b209c67699bdaa1cd61a9b0 (patch) | |
| tree | 33a9582934d0c2d65593a63870c145613a7271da | |
| parent | c88f8b13122e9bd405b070eaab3c03554b16e784 (diff) | |
| download | mullvadvpn-6df61153bc7385587b209c67699bdaa1cd61a9b0.tar.xz mullvadvpn-6df61153bc7385587b209c67699bdaa1cd61a9b0.zip | |
Remove unused NetworkInterface trait
| -rw-r--r-- | talpid-tunnel/src/tun_provider/unix.rs | 81 |
1 files changed, 29 insertions, 52 deletions
diff --git a/talpid-tunnel/src/tun_provider/unix.rs b/talpid-tunnel/src/tun_provider/unix.rs index 6aa71ed393..1081b3ce44 100644 --- a/talpid-tunnel/src/tun_provider/unix.rs +++ b/talpid-tunnel/src/tun_provider/unix.rs @@ -11,17 +11,25 @@ use tun::{platform, Configuration, Device}; /// Errors that can occur while setting up a tunnel device. #[derive(Debug, thiserror::Error)] pub enum Error { - /// Failure to create a tunnel device. - #[error("Failed to create a tunnel device")] - CreateTunnelDevice(#[source] NetworkInterfaceError), + /// Failed to set IP address + #[error("Failed to set IPv4 address")] + SetIpv4(#[source] tun::Error), + + /// Failed to set IP address + #[error("Failed to set IPv6 address")] + SetIpv6(#[source] io::Error), - /// Failure to set a tunnel device IP address. - #[error("Failed to set tunnel IP address: {0}")] - SetIpAddr(IpAddr, #[source] NetworkInterfaceError), + /// Unable to open a tunnel device + #[error("Unable to open a tunnel device")] + CreateDevice(#[source] tun::Error), - /// Failure to set the tunnel device as up. - #[error("Failed to set the tunnel device as up")] - SetUp(#[source] NetworkInterfaceError), + /// Failed to apply async flags to tunnel device + #[error("Failed to apply async flags to tunnel device")] + SetDeviceAsync(#[source] nix::Error), + + /// Failed to enable/disable link device + #[error("Failed to enable/disable link device")] + ToggleDevice(#[source] tun::Error), } /// Factory of tunnel devices on Unix systems. @@ -51,16 +59,14 @@ impl UnixTunProvider { if let Some(ref name) = self.config.name { builder.name(name); } - builder.create().map_err(Error::CreateTunnelDevice)? + builder.create()? }; for ip in self.config.addresses.iter() { - tunnel_device - .set_ip(*ip) - .map_err(|cause| Error::SetIpAddr(*ip, cause))?; + tunnel_device.set_ip(*ip)?; } - tunnel_device.set_up(true).map_err(Error::SetUp)?; + tunnel_device.set_up(true)?; Ok(UnixTun(tunnel_device)) } @@ -86,30 +92,6 @@ impl Deref for UnixTun { } } -/// Errors that can happen when working with *nix tunnel interfaces. -#[derive(thiserror::Error, Debug)] -pub enum NetworkInterfaceError { - /// Failed to set IP address - #[error("Failed to set IPv4 address")] - SetIpv4(#[source] tun::Error), - - /// Failed to set IP address - #[error("Failed to set IPv6 address")] - SetIpv6(#[source] io::Error), - - /// Unable to open a tunnel device - #[error("Unable to open a tunnel device")] - CreateDevice(#[source] tun::Error), - - /// Failed to apply async flags to tunnel device - #[error("Failed to apply async flags to tunnel device")] - SetDeviceAsync(#[source] nix::Error), - - /// Failed to enable/disable link device - #[error("Failed to enable/disable link device")] - ToggleDevice(#[source] tun::Error), -} - /// A tunnel device pub struct TunnelDevice { dev: platform::Device, @@ -124,7 +106,7 @@ pub struct TunnelDeviceBuilder { impl TunnelDeviceBuilder { /// Create a [`TunnelDevice`] from this builder. - pub fn create(self) -> Result<TunnelDevice, NetworkInterfaceError> { + pub fn create(self) -> Result<TunnelDevice, Error> { fn apply_async_flags(fd: RawFd) -> Result<(), nix::Error> { fcntl::fcntl(fd, fcntl::FcntlArg::F_GETFL)?; let arg = fcntl::FcntlArg::F_SETFL(fcntl::OFlag::O_RDWR | fcntl::OFlag::O_NONBLOCK); @@ -132,8 +114,8 @@ impl TunnelDeviceBuilder { Ok(()) } - let dev = platform::create(&self.config).map_err(NetworkInterfaceError::CreateDevice)?; - apply_async_flags(dev.as_raw_fd()).map_err(NetworkInterfaceError::SetDeviceAsync)?; + let dev = platform::create(&self.config).map_err(Error::CreateDevice)?; + apply_async_flags(dev.as_raw_fd()).map_err(Error::SetDeviceAsync)?; Ok(TunnelDevice { dev }) } @@ -175,12 +157,9 @@ impl IntoRawFd for TunnelDevice { } impl TunnelDevice { - fn set_ip(&mut self, ip: IpAddr) -> Result<(), NetworkInterfaceError> { + fn set_ip(&mut self, ip: IpAddr) -> Result<(), Error> { match ip { - IpAddr::V4(ipv4) => self - .dev - .set_address(ipv4) - .map_err(NetworkInterfaceError::SetIpv4), + IpAddr::V4(ipv4) => self.dev.set_address(ipv4).map_err(Error::SetIpv4), IpAddr::V6(ipv6) => { #[cfg(target_os = "linux")] { @@ -195,7 +174,7 @@ impl TunnelDevice { ) .run() .map(|_| ()) - .map_err(NetworkInterfaceError::SetIpv6) + .map_err(Error::SetIpv6) } #[cfg(target_os = "macos")] { @@ -208,16 +187,14 @@ impl TunnelDevice { ) .run() .map(|_| ()) - .map_err(NetworkInterfaceError::SetIpv6) + .map_err(Error::SetIpv6) } } } } - fn set_up(&mut self, up: bool) -> Result<(), NetworkInterfaceError> { - self.dev - .enabled(up) - .map_err(NetworkInterfaceError::ToggleDevice) + fn set_up(&mut self, up: bool) -> Result<(), Error> { + self.dev.enabled(up).map_err(Error::ToggleDevice) } fn get_name(&self) -> &str { |
