Skip to content

Commit

Permalink
Fix interface priority bug when either of IPv4/IPv6 is disabled (#4)
Browse files Browse the repository at this point in the history
  • Loading branch information
jacob-pro authored Oct 27, 2022
1 parent f17cc8f commit 1d83b43
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 4 deletions.
2 changes: 1 addition & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -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)"
Expand Down
22 changes: 20 additions & 2 deletions src/dns.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -59,6 +60,8 @@ struct Adapter {
ipv6_interface_index: u32,
dns_servers: Vec<IpAddr>,
dns_suffixes: Vec<String>,
ipv4_enabled: bool,
ipv6_enabled: bool,
}

/// Returns a list of the system network adapters
Expand Down Expand Up @@ -118,6 +121,8 @@ fn get_adapters() -> Result<Vec<Adapter>, 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;
}
Expand All @@ -128,7 +133,19 @@ fn get_adapters() -> Result<Vec<Adapter>, 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)
}
}

Expand Down Expand Up @@ -170,6 +187,7 @@ pub fn get_configuration() -> Result<DnsConfiguration, Error> {
})
.sorted_by_key(Adapter::interface_metric)
.collect::<Vec<_>>();
log::info!("Found adapters: {:?}", internet_adapters);

let servers = internet_adapters
.iter()
Expand Down

0 comments on commit 1d83b43

Please sign in to comment.