From 304d926775279aeea9c86f5f002c60cff5f7871b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jan=20Sch=C3=B6ppach?= Date: Tue, 5 Mar 2024 16:03:57 +0100 Subject: [PATCH] Fix ipv6 on windows --- Cargo.lock | 7 +++++++ Cargo.toml | 1 + src/main.rs | 33 ++++++++++++++++++++------------- 3 files changed, 28 insertions(+), 13 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 69c979a..95208fe 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -55,6 +55,7 @@ version = "1.1.3" dependencies = [ "atty", "crossterm", + "current_platform", "dirs", "hex", "json", @@ -163,6 +164,12 @@ dependencies = [ "winapi", ] +[[package]] +name = "current_platform" +version = "0.2.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a74858bcfe44b22016cb49337d7b6f04618c58e5dbfdef61b06b8c434324a0bc" + [[package]] name = "dirs" version = "5.0.1" diff --git a/Cargo.toml b/Cargo.toml index 8a34824..bb931ae 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -31,3 +31,4 @@ network-interface = "1.1" openssl = "0.10" openssl-sys = "0.9" hex = "0.4.3" +current_platform = "0.2.0" diff --git a/src/main.rs b/src/main.rs index d18270b..2988e20 100644 --- a/src/main.rs +++ b/src/main.rs @@ -12,6 +12,7 @@ use crossterm::{ terminal::size, }; +use current_platform::CURRENT_PLATFORM; use rustyline::{error::ReadlineError, CompletionType, Config, Editor}; use textplots::{Chart, Plot, Shape}; @@ -325,25 +326,31 @@ fn get_ipv6_link_local_from_serial(serial: u32) -> String { let mut interface = None; - for itf in network_interfaces.iter() { - let addrs = &itf.addr; - for addr in addrs.iter() { - if addr.ip().is_ipv6() && !addr.ip().is_loopback() { - if &addr.ip().to_string()[..6] == "fe80::" { - interface = Some(itf.name.clone()); - break; + if !CURRENT_PLATFORM.to_string().contains("windows") { + for itf in network_interfaces.iter() { + let addrs = &itf.addr; + for addr in addrs.iter() { + if addr.ip().is_ipv6() && !addr.ip().is_loopback() { + if &addr.ip().to_string()[..6] == "fe80::" { + interface = Some(itf.name.clone()); + break; + } } } } } let hex = format!("{:04x}", serial); - format!( - "fe80::b5:b1ff:fe{}:{}%{}", - &hex[..2], - &hex[2..], - interface.unwrap() - ) + if interface.is_some() { + format!( + "fe80::b5:b1ff:fe{}:{}%{}", + &hex[..2], + &hex[2..], + interface.unwrap() + ) + } else { + format!("fe80::b5:b1ff:fe{}:{}", &hex[..2], &hex[2..]) + } } fn parse_shortcuts(command: &str) -> &str {