From 1d83b43f479ed01a68b181c5f6d93fbbbec6d49f Mon Sep 17 00:00:00 2001 From: Jacob Halsey Date: Thu, 27 Oct 2022 14:08:58 +0100 Subject: [PATCH] Fix interface priority bug when either of IPv4/IPv6 is disabled (#4) --- Cargo.lock | 2 +- Cargo.toml | 2 +- src/dns.rs | 22 ++++++++++++++++++++-- 3 files changed, 22 insertions(+), 4 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 1a28b59..b98637d 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -437,7 +437,7 @@ checksum = "f4dd6dc7df2d84cf7b33822ed5b86318fb1781948e9663bacd047fc9dd52259d" [[package]] name = "wsl2-dns-agent" -version = "0.3.3" +version = "0.3.4" dependencies = [ "chrono", "configparser", diff --git a/Cargo.toml b/Cargo.toml index 07cc807..37a152d 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "wsl2-dns-agent" -version = "0.3.3" +version = "0.3.4" edition = "2021" license = "GPL-3.0" description = "An agent that automatically patches your WSL2 DNS configuration for users of Cisco AnyConnect (or similar VPNs)" diff --git a/src/dns.rs b/src/dns.rs index 2122928..4331d86 100644 --- a/src/dns.rs +++ b/src/dns.rs @@ -9,7 +9,8 @@ use win32_utils::str::FromWin32Str; use windows::Win32::Foundation::{ERROR_BUFFER_OVERFLOW, WIN32_ERROR}; use windows::Win32::NetworkManagement::IpHelper::{ FreeMibTable, GetAdaptersAddresses, GetIpForwardTable2, GET_ADAPTERS_ADDRESSES_FLAGS, - IP_ADAPTER_ADDRESSES_LH, MIB_IPFORWARD_ROW2, MIB_IPFORWARD_TABLE2, + IP_ADAPTER_ADDRESSES_LH, IP_ADAPTER_IPV4_ENABLED, IP_ADAPTER_IPV6_ENABLED, MIB_IPFORWARD_ROW2, + MIB_IPFORWARD_TABLE2, }; use windows::Win32::Networking::WinSock::AF_UNSPEC; @@ -59,6 +60,8 @@ struct Adapter { ipv6_interface_index: u32, dns_servers: Vec, dns_suffixes: Vec, + ipv4_enabled: bool, + ipv6_enabled: bool, } /// Returns a list of the system network adapters @@ -118,6 +121,8 @@ fn get_adapters() -> Result, Error> { ipv6_interface_index: adapter.Ipv6IfIndex, dns_servers, dns_suffixes, + ipv4_enabled: adapter.Anonymous2.Flags & IP_ADAPTER_IPV4_ENABLED != 0, + ipv6_enabled: adapter.Anonymous2.Flags & IP_ADAPTER_IPV6_ENABLED != 0, }); next = adapter.Next; } @@ -128,7 +133,19 @@ fn get_adapters() -> Result, Error> { impl Adapter { // For the purposes of DNS, the interface metric is whichever one is lowest fn interface_metric(&self) -> u32 { - std::cmp::min(self.ipv4_metric, self.ipv6_metric) + // When IPv4/IPv6 is disabled then Windows returns a metric of 0 which isn't + // very helpful + let ipv4 = if self.ipv4_enabled { + self.ipv4_metric + } else { + u32::MAX + }; + let ipv6 = if self.ipv6_enabled { + self.ipv6_metric + } else { + u32::MAX + }; + std::cmp::min(ipv4, ipv6) } } @@ -170,6 +187,7 @@ pub fn get_configuration() -> Result { }) .sorted_by_key(Adapter::interface_metric) .collect::>(); + log::info!("Found adapters: {:?}", internet_adapters); let servers = internet_adapters .iter()