From 9c0bfd10b905c46ceaa9c648a11dfe565631aec4 Mon Sep 17 00:00:00 2001 From: fylorn <249551762+fylorn@users.noreply.github.com> Date: Wed, 23 Sep 2026 12:59:13 +0800 Subject: [PATCH] feat(nics): list the network interfaces on Windows too `getifaddrs`, `freeifaddrs` and `if_nametoindex` sat in this module with no cfg around them at all, so it was not that the interface picker would be empty on Windows -- the crate would not build. Windows answers the same question through `GetAdaptersAddresses`. The two systems are asked separately and `windows-sys` is a pure-Rust binding that compiles no C, which is the same trade this module already made by reaching for libc instead of a cross-platform wrapper: together both sides are under a hundred and fifty lines, and the choices a wrapper would have to make between the two are exactly what the comments here are about. What to keep and how to order it is now shared rather than written twice. Skipping link-local IPv6 and sorting loopback last are rules about the picker, not about how a system hands us its interfaces, so the platform modules only read, and the module decides. Two things are specific to Windows and worth saying out loud: The name is `FriendlyName`, not `AdapterName`. The latter is a GUID. The configuration stores this name, so it has to survive a restart *and* be something the user recognises -- the picker shows what they saw in system settings. A GUID is only the first of those, and a `bind:` value nobody can read pushes them back to copying an address from somewhere else. The buffer is a `Vec`, not a `Vec`, because we read it back as `IP_ADAPTER_ADDRESSES_LH` and `Vec` only promises one-byte alignment. That is undefined behaviour that no compiler and no test would report, and on x86 it would most likely appear to work. A const assertion keeps the claim that `u64` covers that alignment true in the future. Verified by checking this module on its own against both `x86_64-pc-windows-msvc` and `aarch64-pc-windows-msvc` -- the whole workspace cannot cross-check from macOS (blake3 wants ml64.exe), but one self-contained file can, and it caught a missing windows-sys feature that would otherwise have waited for a Windows runner to exist. Co-Authored-By: Claude Opus 5 --- Cargo.lock | 1 + Cargo.toml | 3 + crates/tw-config/Cargo.toml | 9 + crates/tw-config/src/nics.rs | 351 +++++++++++++++++++++++++++++------ 4 files changed, 309 insertions(+), 55 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index fe6bc90e..6c6cdbd1 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -2817,6 +2817,7 @@ dependencies = [ "tw-secret", "tw-types", "tw-yaml", + "windows-sys 0.61.2", ] [[package]] diff --git a/Cargo.toml b/Cargo.toml index 6b8d6b6e..02abe23d 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -79,6 +79,9 @@ notify = "8" # 请求历史。bundled 是刻意的:零外部依赖,用户不必装 sqlite rusqlite = { version = "0.40", features = ["bundled"] } libc = "0.2" +# Windows 上问系统要网卡清单。**纯 Rust 的绑定**,不编译任何 C —— +# 和 unix 那边直接用 libc 是同一个取舍,见 tw-config 的 nics。 +windows-sys = "0.61" # 内置价目表压缩着放(2.3 MB → 110 KB) flate2 = "1" # regex-automata / regex-syntax 已经在依赖树里(tracing 的 EnvFilter), diff --git a/crates/tw-config/Cargo.toml b/crates/tw-config/Cargo.toml index cbf11dcf..f52af35f 100644 --- a/crates/tw-config/Cargo.toml +++ b/crates/tw-config/Cargo.toml @@ -29,5 +29,14 @@ notify = { workspace = true } libc = { workspace = true } tokio = { workspace = true } +[target.'cfg(windows)'.dependencies] +windows-sys = { workspace = true, features = [ + "Win32_Foundation", + "Win32_NetworkManagement_IpHelper", + # IP_ADAPTER_ADDRESSES_LH 挂在这个 feature 下,不是 IpHelper 下 + "Win32_NetworkManagement_Ndis", + "Win32_Networking_WinSock", +] } + [dev-dependencies] tempfile = "3" diff --git a/crates/tw-config/src/nics.rs b/crates/tw-config/src/nics.rs index c7e0c58d..7045db5a 100644 --- a/crates/tw-config/src/nics.rs +++ b/crates/tw-config/src/nics.rs @@ -4,11 +4,15 @@ //! 能自己去 `ifconfig` 抄一个地址填进配置文件 —— 而填错的后果是网关起 //! 不来,错误信息只会说 "Can't assign requested address"。 //! -//! 用 `getifaddrs` 直接问系统,不引第三方 crate:`libc` 本来就在工作区 -//! 依赖里(tw-store 在用),而这件事一共不到五十行。 +//! 问系统要,不引一个跨平台的封装:unix 上是 `getifaddrs`(`libc` 本来就在 +//! 工作区依赖里),Windows 上是 `GetAdaptersAddresses`(`windows-sys` 是一层 +//! 纯 Rust 的绑定,不编译任何 C)。两边加起来不到一百五十行,而一个封装 +//! crate 要在这两套语义之间做的取舍,正是下面这些注释在说的事。 +//! +//! **挑哪些、怎么排,两边共用一套规则**,在这个文件下半部分;平台各自只 +//! 负责「把系统里的东西读出来」。 -use std::ffi::CStr; -use std::net::{IpAddr, Ipv4Addr, Ipv6Addr}; +use std::net::{IpAddr, Ipv6Addr}; /// 一张网卡上的一个地址。 /// @@ -16,59 +20,31 @@ use std::net::{IpAddr, Ipv4Addr, Ipv6Addr}; /// 「网卡 + 地址」而不是「网卡」。 #[derive(Debug, Clone, PartialEq, Eq)] pub struct Nic { - /// `en0`、`lo0`、`utun3` + /// unix 上是 `en0`、`lo0`、`utun3`;Windows 上是「以太网」「Wi-Fi」这种 + /// 用户在系统设置里看得见的名字(见 [`windows::adapters`] 里那段注释)。 pub name: String, pub addr: IpAddr, } +/// 链路本地的 IPv6(`fe80::/10`)。 +/// +/// 它们要带 scope id 才能用(`fe80::1%en0`),而那个写法放进配置文件里几乎 +/// 必然是个坑 —— 所以两个平台都跳过。 +fn is_link_local_v6(a: &Ipv6Addr) -> bool { + a.segments()[0] & 0xffc0 == 0xfe80 +} + /// 列出所有已配好地址、并且已经 up 的网卡。 /// /// **跳过没有地址的和没 up 的** —— 一张插着网线但没拿到 IP 的网卡绑不 /// 上去,列在选单里只会让人选了之后发现起不来。 -/// -/// 也跳过 IPv6 的链路本地地址(`fe80::/10`):它们要带 scope id 才能用 -/// (`fe80::1%en0`),而那个写法放进配置文件里几乎必然是个坑。 pub fn list() -> Vec { - let mut out = Vec::new(); - // SAFETY: getifaddrs 成功时给出一条以空指针结尾的链表,freeifaddrs - // 负责释放。中间只读不写,且在 free 之前把需要的字段都拷出来。 - unsafe { - let mut head: *mut libc::ifaddrs = std::ptr::null_mut(); - if libc::getifaddrs(&mut head) != 0 { - return out; - } - let mut cur = head; - while !cur.is_null() { - let e = &*cur; - cur = e.ifa_next; - if e.ifa_addr.is_null() || e.ifa_flags & libc::IFF_UP as u32 == 0 { - continue; - } - let name = match CStr::from_ptr(e.ifa_name).to_str() { - Ok(n) => n.to_string(), - Err(_) => continue, - }; - let addr = match (*e.ifa_addr).sa_family as i32 { - libc::AF_INET => { - let s = &*(e.ifa_addr as *const libc::sockaddr_in); - IpAddr::V4(Ipv4Addr::from(u32::from_be(s.sin_addr.s_addr))) - } - libc::AF_INET6 => { - let s = &*(e.ifa_addr as *const libc::sockaddr_in6); - let a = Ipv6Addr::from(s.sin6_addr.s6_addr); - if a.segments()[0] & 0xffc0 == 0xfe80 { - continue; - } - IpAddr::V6(a) - } - _ => continue, - }; - out.push(Nic { name, addr }); - } - libc::freeifaddrs(head); - } + let mut out = imp::list(); // 回环排最后:它在选单里对应的是「仅本机」那一档,不该混在 // 「选一张网卡」的候选里排在前面。 + // + // **排序在这里,不在各自的平台实现里** —— 它是一条界面上的规矩, + // 和系统怎么把网卡交给我们没有关系。 out.sort_by_key(|n| (n.addr.is_loopback(), n.name.clone(), n.addr.to_string())); out } @@ -103,11 +79,245 @@ fn one_per_name(all: Vec) -> Vec { /// 网卡」和「这张网卡此刻没有地址」要分开说 —— 前者多半是拼错了,后者 /// 插上网线就好。 pub fn exists(name: &str) -> bool { - let Ok(c) = std::ffi::CString::new(name) else { - return false; + imp::exists(name) +} + +#[cfg(unix)] +use unix as imp; +#[cfg(windows)] +use windows as imp; + +#[cfg(unix)] +mod unix { + use super::{Nic, is_link_local_v6}; + use std::ffi::CStr; + use std::net::{IpAddr, Ipv4Addr, Ipv6Addr}; + + pub fn list() -> Vec { + let mut out = Vec::new(); + // SAFETY: getifaddrs 成功时给出一条以空指针结尾的链表,freeifaddrs + // 负责释放。中间只读不写,且在 free 之前把需要的字段都拷出来。 + unsafe { + let mut head: *mut libc::ifaddrs = std::ptr::null_mut(); + if libc::getifaddrs(&mut head) != 0 { + return out; + } + let mut cur = head; + while !cur.is_null() { + let e = &*cur; + cur = e.ifa_next; + if e.ifa_addr.is_null() || e.ifa_flags & libc::IFF_UP as u32 == 0 { + continue; + } + let name = match CStr::from_ptr(e.ifa_name).to_str() { + Ok(n) => n.to_string(), + Err(_) => continue, + }; + let addr = match (*e.ifa_addr).sa_family as i32 { + libc::AF_INET => { + let s = &*(e.ifa_addr as *const libc::sockaddr_in); + IpAddr::V4(Ipv4Addr::from(u32::from_be(s.sin_addr.s_addr))) + } + libc::AF_INET6 => { + let s = &*(e.ifa_addr as *const libc::sockaddr_in6); + let a = Ipv6Addr::from(s.sin6_addr.s6_addr); + if is_link_local_v6(&a) { + continue; + } + IpAddr::V6(a) + } + _ => continue, + }; + out.push(Nic { name, addr }); + } + libc::freeifaddrs(head); + } + out + } + + pub fn exists(name: &str) -> bool { + let Ok(c) = std::ffi::CString::new(name) else { + return false; + }; + // SAFETY: 传进去的是一个以 NUL 结尾的字符串,函数只读它 + unsafe { libc::if_nametoindex(c.as_ptr()) != 0 } + } +} + +#[cfg(windows)] +mod windows { + use super::{Nic, is_link_local_v6}; + use std::net::{IpAddr, Ipv4Addr, Ipv6Addr}; + + use windows_sys::Win32::Foundation::{ERROR_BUFFER_OVERFLOW, ERROR_SUCCESS}; + use windows_sys::Win32::NetworkManagement::IpHelper::{ + GAA_FLAG_SKIP_ANYCAST, GAA_FLAG_SKIP_DNS_SERVER, GAA_FLAG_SKIP_MULTICAST, + GetAdaptersAddresses, IP_ADAPTER_ADDRESSES_LH, + }; + use windows_sys::Win32::Networking::WinSock::{ + AF_INET, AF_INET6, AF_UNSPEC, SOCKADDR_IN, SOCKADDR_IN6, }; - // SAFETY: 传进去的是一个以 NUL 结尾的字符串,函数只读它 - unsafe { libc::if_nametoindex(c.as_ptr()) != 0 } + + /// `IfOperStatusUp`。**只认「up」这一档**,和 unix 那边的 `IFF_UP` 对齐: + /// 「正在连」「已断开」的网卡绑不上去。 + const OPER_STATUS_UP: i32 = 1; + + /// 一张网卡上我们关心的东西。 + struct Adapter { + name: String, + up: bool, + addrs: Vec, + } + + /// 走一遍系统给的适配器链表。 + /// + /// **名字取 `FriendlyName`,不取 `AdapterName`。**后者是 + /// `{3F2504E0-4F89-...}` 这样的 GUID —— 配置文件里存的是这个名字,而它 + /// 既要让用户在选单里认得出来(他在系统设置里看到的就是「Wi-Fi」), + /// 也要在下次启动时还能按它找回同一张网卡。GUID 满足后者但不满足前者, + /// 而一个用户看不懂的 `bind:` 值等于逼他去别处抄一个地址。 + fn adapters() -> Vec { + // 先问要多大,再按那个大小要一次。**中间网卡可能变**(插拔、VPN + // 起落),所以第二次仍然可能说不够 —— 重试几次,而不是一次问不到 + // 就交白卷。 + let flags = GAA_FLAG_SKIP_ANYCAST | GAA_FLAG_SKIP_MULTICAST | GAA_FLAG_SKIP_DNS_SERVER; + let mut buf: Vec = Vec::new(); + let mut size: u32 = 0; + for _ in 0..4 { + // SAFETY: 第一次传空指针只为问尺寸;之后传的是一块至少 `size` + // 字节、并且对齐得下 IP_ADAPTER_ADDRESSES_LH 的缓冲区(见 `words`)。 + let rc = unsafe { + GetAdaptersAddresses( + AF_UNSPEC as u32, + flags, + std::ptr::null_mut(), + if buf.is_empty() { + std::ptr::null_mut() + } else { + buf.as_mut_ptr() as *mut IP_ADAPTER_ADDRESSES_LH + }, + &mut size, + ) + }; + match rc { + ERROR_SUCCESS if !buf.is_empty() => { + // SAFETY: 调用成功,缓冲区里是一条以空指针结尾的链表。 + return unsafe { walk(buf.as_ptr() as *const IP_ADAPTER_ADDRESSES_LH) }; + } + // 第一次(空缓冲区)必然走这里,`size` 现在是要的字节数。 + ERROR_BUFFER_OVERFLOW | ERROR_SUCCESS => { + // 多要一点:问到尺寸和真正取数之间网卡还可能变多。 + buf = vec![0u64; words((size as usize).saturating_add(4096))]; + } + // 其余错误:给不出清单就给空的,和 unix 那边 getifaddrs + // 失败时一样 —— 界面上会退到「让用户自己填地址」那一档。 + _ => return Vec::new(), + } + } + Vec::new() + } + + /// 这么多字节要几个 `u64`。 + /// + /// **缓冲区是 `Vec` 而不是 `Vec`,为的是对齐。**我们要把这块 + /// 内存当成 `IP_ADAPTER_ADDRESSES_LH` 读,而 `Vec` 只保证 1 字节 + /// 对齐 —— 那是未定义行为,且编译器和测试都不会吭一声,在 x86 上甚至 + /// 多半「能跑」。`u64` 的对齐(8)盖得住那个结构体,下面这行保证这句话 + /// 将来仍然成立。 + const _: () = + assert!(std::mem::align_of::() <= std::mem::align_of::()); + + fn words(bytes: usize) -> usize { + bytes.div_ceil(std::mem::size_of::()) + } + + /// SAFETY: `head` 要么是空指针,要么指向一条以空指针结尾、由 + /// `GetAdaptersAddresses` 填好的链表,且在这次调用期间一直有效。 + unsafe fn walk(head: *const IP_ADAPTER_ADDRESSES_LH) -> Vec { + let mut out = Vec::new(); + let mut cur = head; + while !cur.is_null() { + let a = unsafe { &*cur }; + cur = a.Next; + let Some(name) = (unsafe { wide_string(a.FriendlyName) }) else { + continue; + }; + let mut addrs = Vec::new(); + let mut ua = a.FirstUnicastAddress; + while !ua.is_null() { + let u = unsafe { &*ua }; + ua = u.Next; + let sa = u.Address.lpSockaddr; + if sa.is_null() { + continue; + } + match unsafe { (*sa).sa_family } { + AF_INET => { + let s = unsafe { &*(sa as *const SOCKADDR_IN) }; + // `S_un.S_addr` 是网络字节序 + let bits = unsafe { s.sin_addr.S_un.S_addr }; + addrs.push(IpAddr::V4(Ipv4Addr::from(u32::from_be(bits)))); + } + AF_INET6 => { + let s = unsafe { &*(sa as *const SOCKADDR_IN6) }; + let v6 = Ipv6Addr::from(unsafe { s.sin6_addr.u.Byte }); + if is_link_local_v6(&v6) { + continue; + } + addrs.push(IpAddr::V6(v6)); + } + _ => continue, + } + } + out.push(Adapter { + name, + up: a.OperStatus as i32 == OPER_STATUS_UP, + addrs, + }); + } + out + } + + /// Windows 的字符串是 UTF-16,以 NUL 结尾。 + /// + /// 名字可以是任何语言(中文系统上就是「以太网」),所以**不是 ASCII**, + /// 而 `from_utf16_lossy` 保证一个畸形的名字变成一个难看的名字, + /// 不是让整份清单消失。 + /// + /// SAFETY: `p` 要么是空指针,要么指向一段以 NUL 结尾的 UTF-16。 + unsafe fn wide_string(p: *const u16) -> Option { + if p.is_null() { + return None; + } + let mut len = 0usize; + // SAFETY: 调用方保证有 NUL 结尾,所以这个循环会停。 + while unsafe { *p.add(len) } != 0 { + len += 1; + } + if len == 0 { + return None; + } + let s = String::from_utf16_lossy(unsafe { std::slice::from_raw_parts(p, len) }); + Some(s) + } + + pub fn list() -> Vec { + adapters() + .into_iter() + .filter(|a| a.up) + .flat_map(|a| { + a.addrs.into_iter().map(move |addr| Nic { + name: a.name.clone(), + addr, + }) + }) + .collect() + } + + /// **不看 up、也不看有没有地址** —— 见 [`super::exists`] 上那段。 + pub fn exists(name: &str) -> bool { + !name.is_empty() && adapters().iter().any(|a| a.name == name) + } } #[cfg(test)] @@ -127,11 +337,7 @@ mod tests { assert!(!n.name.is_empty(), "网卡名是空的:{n:?}"); // 链路本地的 IPv6 要带 scope id 才能用,不该出现在这里 if let IpAddr::V6(v6) = n.addr { - assert_ne!( - v6.segments()[0] & 0xffc0, - 0xfe80, - "漏掉了链路本地地址:{n:?}" - ); + assert!(!is_link_local_v6(&v6), "漏掉了链路本地地址:{n:?}"); } } } @@ -165,12 +371,47 @@ mod tests { ); } + /// 名字里可以有空格和中文 —— Windows 上「以太网」「Wi-Fi 2」都是常见的。 + #[test] + fn a_name_with_spaces_or_non_ascii_is_still_one_row() { + let nic = |name: &str, addr: &str| Nic { + name: name.into(), + addr: addr.parse().unwrap(), + }; + let rows = one_per_name(vec![ + nic("以太网", "fd00::1"), + nic("以太网", "192.168.1.2"), + nic("Wi-Fi 2", "192.168.1.3"), + ]); + assert_eq!( + rows, + vec![nic("以太网", "192.168.1.2"), nic("Wi-Fi 2", "192.168.1.3")] + ); + } + + /// 两个平台共用的那条规矩,单独测一次。 + #[test] + fn link_local_v6_is_recognised_and_nothing_else_is() { + let v6 = |s: &str| s.parse::().unwrap(); + assert!(is_link_local_v6(&v6("fe80::1"))); + assert!(is_link_local_v6(&v6("febf::1")), "fe80::/10 的另一头"); + assert!(!is_link_local_v6(&v6("fec0::1")), "刚好出界"); + assert!( + !is_link_local_v6(&v6("fd00::1")), + "唯一本地地址不是链路本地" + ); + assert!(!is_link_local_v6(&v6("::1"))); + assert!(!is_link_local_v6(&v6("2001:db8::1"))); + } + #[test] fn every_listed_interface_exists_and_a_made_up_one_does_not() { for n in by_name() { assert!(exists(&n.name), "{n:?}"); } assert!(!exists("tw-no-such0")); + assert!(!exists("")); + #[cfg(unix)] assert!(!exists("bad\0name")); } }