Skip to content

Commit 83ad998

Browse files
author
Nicolas Vandamme
committed
force if-addrs to use link-local via "link-local feature
1 parent f2ab1ef commit 83ad998

File tree

4 files changed

+13
-27
lines changed

4 files changed

+13
-27
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,3 +3,4 @@ Cargo.lock
33
__pycache__
44
*.pyc
55
examples/register_all.rs
6+
examples/test_get_ip.rs

Cargo.toml

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ edition = "2018"
1111

1212
[dependencies]
1313
byteorder = "1.5"
14-
if-addrs = "0.12.0"
14+
if-addrs = { version = "0.12.0", features = ["link-local"] }
1515
hostname = "0.4.0"
1616
log = "0.4"
1717
multimap = "0.10.0"
@@ -20,7 +20,6 @@ futures-util = "0.3"
2020
thiserror = "1.0"
2121
tokio = { version = "1.0", features = ["sync", "net", "rt"] }
2222
socket2 = { version = "0.5", features = ["all"] }
23-
local-ip-address = "0.6.1"
2423

2524
[target.'cfg(windows)'.dependencies]
2625
winapi = { version = "0.3", features = ["netioapi"] }

examples/zeroconf_test.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
1-
from zeroconf import ServiceBrowser, Zeroconf, IPVersion, ZeroconfServiceTypes
1+
from zeroconf import ServiceBrowser, ServiceListener, Zeroconf, IPVersion, ZeroconfServiceTypes
22
from time import sleep
33

44

55
TYPE = "_http._tcp.local."
66
NAME = "libmdns Web Server"
77

88

9-
class MyListener:
9+
class MyListener(ServiceListener):
1010
def __init__(self):
1111
self.found = []
1212

src/fsm.rs

Lines changed: 9 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ use std::collections::VecDeque;
77
use std::io;
88
use std::io::ErrorKind::WouldBlock;
99
use std::marker::PhantomData;
10-
use std::net::{IpAddr, SocketAddr, Ipv4Addr, Ipv6Addr};
10+
use std::net::{IpAddr, SocketAddr};
1111
use std::{
1212
future::Future,
1313
pin::Pin,
@@ -20,8 +20,6 @@ use super::{DEFAULT_TTL, MDNS_PORT};
2020
use crate::address_family::AddressFamily;
2121
use crate::services::{ServiceData, Services};
2222

23-
use local_ip_address::{list_afinet_netifas};
24-
2523
pub type AnswerBuilder = dns_parser::Builder<dns_parser::Answers>;
2624

2725
const SERVICE_TYPE_ENUMERATION_NAME: Cow<'static, str> =
@@ -223,7 +221,7 @@ impl<AF: AddressFamily> FSM<AF> {
223221
}
224222

225223
fn add_ip_rr(&self, hostname: &Name, mut builder: AnswerBuilder, ttl: u32) -> AnswerBuilder {
226-
let interfaces = match list_afinet_netifas() {
224+
let interfaces = match get_if_addrs() {
227225
Ok(interfaces) => interfaces,
228226
Err(err) => {
229227
error!("could not get list of interfaces: {}", err);
@@ -232,25 +230,22 @@ impl<AF: AddressFamily> FSM<AF> {
232230
};
233231

234232
for iface in interfaces {
233+
if iface.is_loopback() {
234+
continue;
235+
}
235236

236237
trace!("found interface {:?}", iface);
237-
if !self.allowed_ip.is_empty() && !self.allowed_ip.contains(&iface.1) {
238+
if !self.allowed_ip.is_empty() && !self.allowed_ip.contains(&iface.ip()) {
238239
trace!(" -> interface dropped");
239240
continue;
240241
}
241242

242-
match (iface.1, AF::DOMAIN) {
243+
match (iface.ip(), AF::DOMAIN) {
243244
(IpAddr::V4(ip), Domain::IPV4) => {
244-
if !is_loopback_ipv4(ip) {
245-
builder = builder.add_answer(hostname, QueryClass::IN, ttl, &RRData::A(ip));
246-
trace!(" -> adding IP address {:?}", iface.1);
247-
}
245+
builder = builder.add_answer(hostname, QueryClass::IN, ttl, &RRData::A(ip))
248246
}
249247
(IpAddr::V6(ip), Domain::IPV6) => {
250-
if !is_loopback_ipv6(ip) {
251-
builder = builder.add_answer(hostname, QueryClass::IN, ttl, &RRData::AAAA(ip));
252-
trace!(" -> adding IP address {:?}", iface.1);
253-
}
248+
builder = builder.add_answer(hostname, QueryClass::IN, ttl, &RRData::AAAA(ip))
254249
}
255250
_ => (),
256251
}
@@ -323,15 +318,6 @@ impl<AF: Unpin + AddressFamily> Future for FSM<AF> {
323318
}
324319
}
325320

326-
327-
fn is_loopback_ipv6(ip: Ipv6Addr) -> bool {
328-
ip.segments() == [0, 0, 0, 0, 0, 0, 0, 1]
329-
}
330-
331-
fn is_loopback_ipv4(ip: Ipv4Addr) -> bool {
332-
ip.octets()[0] == 127
333-
}
334-
335321
#[cfg(test)]
336322
mod tests {
337323
use super::*;

0 commit comments

Comments
 (0)