Skip to content

Commit f2ab1ef

Browse files
author
Nicolas Vandamme
committed
fix pupblishing ipv6 link-local
1 parent c43829a commit f2ab1ef

File tree

3 files changed

+30
-14
lines changed

3 files changed

+30
-14
lines changed

.gitignore

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

Cargo.toml

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -11,24 +11,25 @@ edition = "2018"
1111

1212
[dependencies]
1313
byteorder = "1.5"
14-
if-addrs = "0.11.0"
15-
hostname = "0.3.1"
14+
if-addrs = "0.12.0"
15+
hostname = "0.4.0"
1616
log = "0.4"
17-
multimap = "0.9"
17+
multimap = "0.10.0"
1818
rand = "0.8"
1919
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"
2324

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

2728
[target.'cfg(not(windows))'.dependencies]
28-
nix = { version = "0.27", features = ["net"] }
29+
nix = { version = "0.28", features = ["net"] }
2930

3031
[dev-dependencies]
31-
env_logger = { version = "0.10", default-features = false, features = [
32+
env_logger = { version = "0.11.3", default-features = false, features = [
3233
"color",
3334
"humantime",
3435
"auto-color",

src/fsm.rs

Lines changed: 23 additions & 9 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};
10+
use std::net::{IpAddr, SocketAddr, Ipv4Addr, Ipv6Addr};
1111
use std::{
1212
future::Future,
1313
pin::Pin,
@@ -20,6 +20,8 @@ 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+
2325
pub type AnswerBuilder = dns_parser::Builder<dns_parser::Answers>;
2426

2527
const SERVICE_TYPE_ENUMERATION_NAME: Cow<'static, str> =
@@ -221,7 +223,7 @@ impl<AF: AddressFamily> FSM<AF> {
221223
}
222224

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

232234
for iface in interfaces {
233-
if iface.is_loopback() {
234-
continue;
235-
}
236235

237236
trace!("found interface {:?}", iface);
238-
if !self.allowed_ip.is_empty() && !self.allowed_ip.contains(&iface.ip()) {
237+
if !self.allowed_ip.is_empty() && !self.allowed_ip.contains(&iface.1) {
239238
trace!(" -> interface dropped");
240239
continue;
241240
}
242241

243-
match (iface.ip(), AF::DOMAIN) {
242+
match (iface.1, AF::DOMAIN) {
244243
(IpAddr::V4(ip), Domain::IPV4) => {
245-
builder = builder.add_answer(hostname, QueryClass::IN, ttl, &RRData::A(ip))
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+
}
246248
}
247249
(IpAddr::V6(ip), Domain::IPV6) => {
248-
builder = builder.add_answer(hostname, QueryClass::IN, ttl, &RRData::AAAA(ip))
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+
}
249254
}
250255
_ => (),
251256
}
@@ -318,6 +323,15 @@ impl<AF: Unpin + AddressFamily> Future for FSM<AF> {
318323
}
319324
}
320325

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+
321335
#[cfg(test)]
322336
mod tests {
323337
use super::*;

0 commit comments

Comments
 (0)