diff options
| author | David Lönnhager <david.l@mullvad.net> | 2020-04-23 11:02:29 +0200 |
|---|---|---|
| committer | David Lönnhager <david.l@mullvad.net> | 2020-04-24 16:51:47 +0200 |
| commit | 0cbcd2cedfef07651c99e804a63e0d8ba2c3a09a (patch) | |
| tree | f54f36bb0a9bbd0b73840b249b411cfdd46f8ed9 | |
| parent | 896454f0d01c8e42a9d613299f48078694d17392 (diff) | |
| download | mullvadvpn-0cbcd2cedfef07651c99e804a63e0d8ba2c3a09a.tar.xz mullvadvpn-0cbcd2cedfef07651c99e804a63e0d8ba2c3a09a.zip | |
Expose IPv6-enabling function to Rust
| -rw-r--r-- | talpid-core/src/winnet.rs | 37 |
1 files changed, 36 insertions, 1 deletions
diff --git a/talpid-core/src/winnet.rs b/talpid-core/src/winnet.rs index 21b5bb3ae7..3e7a52bf75 100644 --- a/talpid-core/src/winnet.rs +++ b/talpid-core/src/winnet.rs @@ -3,7 +3,11 @@ pub use self::api::{WinNet_ActivateConnectivityMonitor, WinNet_DeactivateConnect use crate::{logging::windows::log_sink, routing::Node}; use ipnetwork::IpNetwork; use libc::{c_void, wchar_t}; -use std::{ffi::OsString, net::IpAddr, ptr}; +use std::{ + ffi::{OsStr, OsString}, + net::IpAddr, + ptr, +}; use widestring::WideCString; /// Errors that this module may produce. @@ -17,6 +21,10 @@ pub enum Error { #[error(display = "Supplied interface alias is invalid")] InvalidInterfaceAlias(#[error(source)] widestring::NulError<u16>), + /// Failed to enable IPv6 on the network interface. + #[error(display = "Failed to enable IPv6 on the network interface")] + EnableIpv6, + /// Failed to read IPv6 status on the TAP network interface. #[error(display = "Failed to read IPv6 status on the TAP network interface")] GetIpv6Status, @@ -62,6 +70,26 @@ pub fn ensure_best_metric_for_interface(interface_alias: &str) -> Result<bool, E } } +/// Enables IPv6 for a given interface. +pub fn enable_ipv6_for_adapter(interface_alias: &OsStr) -> Result<(), Error> { + let interface_alias_ws = + WideCString::from_os_str(interface_alias).map_err(Error::InvalidInterfaceAlias)?; + + let result = unsafe { + WinNet_EnableIpv6ForAdapter( + interface_alias_ws.as_ptr(), + Some(log_sink), + logging_context(), + ) + }; + + if result { + Ok(()) + } else { + Err(Error::EnableIpv6) + } +} + /// Checks if IPv6 is enabled for the TAP interface pub fn get_tap_interface_ipv6_status() -> Result<bool, Error> { // WinNet_GetTapInterfaceIpv6Status() will fail if the alias cannot be retrieved. @@ -380,6 +408,13 @@ mod api { sink_context: *const u8, ) -> u32; + #[link_name = "WinNet_EnableIpv6ForAdapter"] + pub fn WinNet_EnableIpv6ForAdapter( + tunnel_interface_alias: *const wchar_t, + sink: Option<LogSink>, + sink_context: *const u8, + ) -> bool; + #[link_name = "WinNet_GetTapInterfaceIpv6Status"] pub fn WinNet_GetTapInterfaceIpv6Status( sink: Option<LogSink>, |
