From f2ab1ef64792e8e5d5f81a13d10b93e96c7e476f Mon Sep 17 00:00:00 2001 From: Nicolas Vandamme Date: Wed, 3 Apr 2024 01:14:51 +0200 Subject: [PATCH 1/4] fix pupblishing ipv6 link-local --- .gitignore | 1 + Cargo.toml | 11 ++++++----- src/fsm.rs | 32 +++++++++++++++++++++++--------- 3 files changed, 30 insertions(+), 14 deletions(-) diff --git a/.gitignore b/.gitignore index 903c355..5e0c35e 100644 --- a/.gitignore +++ b/.gitignore @@ -2,3 +2,4 @@ target Cargo.lock __pycache__ *.pyc +examples/register_all.rs diff --git a/Cargo.toml b/Cargo.toml index df297f1..88f40c0 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -11,24 +11,25 @@ edition = "2018" [dependencies] byteorder = "1.5" -if-addrs = "0.11.0" -hostname = "0.3.1" +if-addrs = "0.12.0" +hostname = "0.4.0" log = "0.4" -multimap = "0.9" +multimap = "0.10.0" rand = "0.8" futures-util = "0.3" thiserror = "1.0" tokio = { version = "1.0", features = ["sync", "net", "rt"] } socket2 = { version = "0.5", features = ["all"] } +local-ip-address = "0.6.1" [target.'cfg(windows)'.dependencies] winapi = { version = "0.3", features = ["netioapi"] } [target.'cfg(not(windows))'.dependencies] -nix = { version = "0.27", features = ["net"] } +nix = { version = "0.28", features = ["net"] } [dev-dependencies] -env_logger = { version = "0.10", default-features = false, features = [ +env_logger = { version = "0.11.3", default-features = false, features = [ "color", "humantime", "auto-color", diff --git a/src/fsm.rs b/src/fsm.rs index b429de0..739359b 100644 --- a/src/fsm.rs +++ b/src/fsm.rs @@ -7,7 +7,7 @@ use std::collections::VecDeque; use std::io; use std::io::ErrorKind::WouldBlock; use std::marker::PhantomData; -use std::net::{IpAddr, SocketAddr}; +use std::net::{IpAddr, SocketAddr, Ipv4Addr, Ipv6Addr}; use std::{ future::Future, pin::Pin, @@ -20,6 +20,8 @@ use super::{DEFAULT_TTL, MDNS_PORT}; use crate::address_family::AddressFamily; use crate::services::{ServiceData, Services}; +use local_ip_address::{list_afinet_netifas}; + pub type AnswerBuilder = dns_parser::Builder; const SERVICE_TYPE_ENUMERATION_NAME: Cow<'static, str> = @@ -221,7 +223,7 @@ impl FSM { } fn add_ip_rr(&self, hostname: &Name, mut builder: AnswerBuilder, ttl: u32) -> AnswerBuilder { - let interfaces = match get_if_addrs() { + let interfaces = match list_afinet_netifas() { Ok(interfaces) => interfaces, Err(err) => { error!("could not get list of interfaces: {}", err); @@ -230,22 +232,25 @@ impl FSM { }; for iface in interfaces { - if iface.is_loopback() { - continue; - } trace!("found interface {:?}", iface); - if !self.allowed_ip.is_empty() && !self.allowed_ip.contains(&iface.ip()) { + if !self.allowed_ip.is_empty() && !self.allowed_ip.contains(&iface.1) { trace!(" -> interface dropped"); continue; } - match (iface.ip(), AF::DOMAIN) { + match (iface.1, AF::DOMAIN) { (IpAddr::V4(ip), Domain::IPV4) => { - builder = builder.add_answer(hostname, QueryClass::IN, ttl, &RRData::A(ip)) + if !is_loopback_ipv4(ip) { + builder = builder.add_answer(hostname, QueryClass::IN, ttl, &RRData::A(ip)); + trace!(" -> adding IP address {:?}", iface.1); + } } (IpAddr::V6(ip), Domain::IPV6) => { - builder = builder.add_answer(hostname, QueryClass::IN, ttl, &RRData::AAAA(ip)) + if !is_loopback_ipv6(ip) { + builder = builder.add_answer(hostname, QueryClass::IN, ttl, &RRData::AAAA(ip)); + trace!(" -> adding IP address {:?}", iface.1); + } } _ => (), } @@ -318,6 +323,15 @@ impl Future for FSM { } } + +fn is_loopback_ipv6(ip: Ipv6Addr) -> bool { + ip.segments() == [0, 0, 0, 0, 0, 0, 0, 1] +} + +fn is_loopback_ipv4(ip: Ipv4Addr) -> bool { + ip.octets()[0] == 127 +} + #[cfg(test)] mod tests { use super::*; From 83ad9982e3fc9a020249cefd269ed9508c1a4652 Mon Sep 17 00:00:00 2001 From: Nicolas Vandamme Date: Fri, 5 Apr 2024 00:55:59 +0200 Subject: [PATCH 2/4] force if-addrs to use link-local via "link-local feature --- .gitignore | 1 + Cargo.toml | 3 +-- examples/zeroconf_test.py | 4 ++-- src/fsm.rs | 32 +++++++++----------------------- 4 files changed, 13 insertions(+), 27 deletions(-) diff --git a/.gitignore b/.gitignore index 5e0c35e..32af2a6 100644 --- a/.gitignore +++ b/.gitignore @@ -3,3 +3,4 @@ Cargo.lock __pycache__ *.pyc examples/register_all.rs +examples/test_get_ip.rs diff --git a/Cargo.toml b/Cargo.toml index 88f40c0..da86009 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -11,7 +11,7 @@ edition = "2018" [dependencies] byteorder = "1.5" -if-addrs = "0.12.0" +if-addrs = { version = "0.12.0", features = ["link-local"] } hostname = "0.4.0" log = "0.4" multimap = "0.10.0" @@ -20,7 +20,6 @@ futures-util = "0.3" thiserror = "1.0" tokio = { version = "1.0", features = ["sync", "net", "rt"] } socket2 = { version = "0.5", features = ["all"] } -local-ip-address = "0.6.1" [target.'cfg(windows)'.dependencies] winapi = { version = "0.3", features = ["netioapi"] } diff --git a/examples/zeroconf_test.py b/examples/zeroconf_test.py index 97f23aa..82e0019 100644 --- a/examples/zeroconf_test.py +++ b/examples/zeroconf_test.py @@ -1,4 +1,4 @@ -from zeroconf import ServiceBrowser, Zeroconf, IPVersion, ZeroconfServiceTypes +from zeroconf import ServiceBrowser, ServiceListener, Zeroconf, IPVersion, ZeroconfServiceTypes from time import sleep @@ -6,7 +6,7 @@ NAME = "libmdns Web Server" -class MyListener: +class MyListener(ServiceListener): def __init__(self): self.found = [] diff --git a/src/fsm.rs b/src/fsm.rs index 739359b..b429de0 100644 --- a/src/fsm.rs +++ b/src/fsm.rs @@ -7,7 +7,7 @@ use std::collections::VecDeque; use std::io; use std::io::ErrorKind::WouldBlock; use std::marker::PhantomData; -use std::net::{IpAddr, SocketAddr, Ipv4Addr, Ipv6Addr}; +use std::net::{IpAddr, SocketAddr}; use std::{ future::Future, pin::Pin, @@ -20,8 +20,6 @@ use super::{DEFAULT_TTL, MDNS_PORT}; use crate::address_family::AddressFamily; use crate::services::{ServiceData, Services}; -use local_ip_address::{list_afinet_netifas}; - pub type AnswerBuilder = dns_parser::Builder; const SERVICE_TYPE_ENUMERATION_NAME: Cow<'static, str> = @@ -223,7 +221,7 @@ impl FSM { } fn add_ip_rr(&self, hostname: &Name, mut builder: AnswerBuilder, ttl: u32) -> AnswerBuilder { - let interfaces = match list_afinet_netifas() { + let interfaces = match get_if_addrs() { Ok(interfaces) => interfaces, Err(err) => { error!("could not get list of interfaces: {}", err); @@ -232,25 +230,22 @@ impl FSM { }; for iface in interfaces { + if iface.is_loopback() { + continue; + } trace!("found interface {:?}", iface); - if !self.allowed_ip.is_empty() && !self.allowed_ip.contains(&iface.1) { + if !self.allowed_ip.is_empty() && !self.allowed_ip.contains(&iface.ip()) { trace!(" -> interface dropped"); continue; } - match (iface.1, AF::DOMAIN) { + match (iface.ip(), AF::DOMAIN) { (IpAddr::V4(ip), Domain::IPV4) => { - if !is_loopback_ipv4(ip) { - builder = builder.add_answer(hostname, QueryClass::IN, ttl, &RRData::A(ip)); - trace!(" -> adding IP address {:?}", iface.1); - } + builder = builder.add_answer(hostname, QueryClass::IN, ttl, &RRData::A(ip)) } (IpAddr::V6(ip), Domain::IPV6) => { - if !is_loopback_ipv6(ip) { - builder = builder.add_answer(hostname, QueryClass::IN, ttl, &RRData::AAAA(ip)); - trace!(" -> adding IP address {:?}", iface.1); - } + builder = builder.add_answer(hostname, QueryClass::IN, ttl, &RRData::AAAA(ip)) } _ => (), } @@ -323,15 +318,6 @@ impl Future for FSM { } } - -fn is_loopback_ipv6(ip: Ipv6Addr) -> bool { - ip.segments() == [0, 0, 0, 0, 0, 0, 0, 1] -} - -fn is_loopback_ipv4(ip: Ipv4Addr) -> bool { - ip.octets()[0] == 127 -} - #[cfg(test)] mod tests { use super::*; From 249d64b5f82b8069d91b25c7d4daa5e2dfbc10f1 Mon Sep 17 00:00:00 2001 From: Nicolas Vandamme Date: Sat, 6 Apr 2024 09:50:04 +0200 Subject: [PATCH 3/4] remove tests from examples --- .gitignore | 2 -- 1 file changed, 2 deletions(-) diff --git a/.gitignore b/.gitignore index 32af2a6..903c355 100644 --- a/.gitignore +++ b/.gitignore @@ -2,5 +2,3 @@ target Cargo.lock __pycache__ *.pyc -examples/register_all.rs -examples/test_get_ip.rs From 0b0e6cf4bad9a8bb81b0736e244521b7fdf0b328 Mon Sep 17 00:00:00 2001 From: Nicolas Vandamme Date: Tue, 9 Apr 2024 14:03:37 +0200 Subject: [PATCH 4/4] donwgraded env_logger to satisfy buildbot --- Cargo.toml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Cargo.toml b/Cargo.toml index da86009..fa33319 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -28,7 +28,7 @@ winapi = { version = "0.3", features = ["netioapi"] } nix = { version = "0.28", features = ["net"] } [dev-dependencies] -env_logger = { version = "0.11.3", default-features = false, features = [ +env_logger = { version = "0.10.2", default-features = false, features = [ "color", "humantime", "auto-color",