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::*;