Skip to content

Commit

Permalink
force if-addrs to use link-local via "link-local feature
Browse files Browse the repository at this point in the history
  • Loading branch information
Nicolas Vandamme committed Apr 4, 2024
1 parent f2ab1ef commit 83ad998
Show file tree
Hide file tree
Showing 4 changed files with 13 additions and 27 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,4 @@ Cargo.lock
__pycache__
*.pyc
examples/register_all.rs
examples/test_get_ip.rs
3 changes: 1 addition & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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"] }
Expand Down
4 changes: 2 additions & 2 deletions examples/zeroconf_test.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
from zeroconf import ServiceBrowser, Zeroconf, IPVersion, ZeroconfServiceTypes
from zeroconf import ServiceBrowser, ServiceListener, Zeroconf, IPVersion, ZeroconfServiceTypes
from time import sleep


TYPE = "_http._tcp.local."
NAME = "libmdns Web Server"


class MyListener:
class MyListener(ServiceListener):
def __init__(self):
self.found = []

Expand Down
32 changes: 9 additions & 23 deletions src/fsm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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<dns_parser::Answers>;

const SERVICE_TYPE_ENUMERATION_NAME: Cow<'static, str> =
Expand Down Expand Up @@ -223,7 +221,7 @@ impl<AF: AddressFamily> FSM<AF> {
}

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);
Expand All @@ -232,25 +230,22 @@ impl<AF: AddressFamily> FSM<AF> {
};

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))
}
_ => (),
}
Expand Down Expand Up @@ -323,15 +318,6 @@ impl<AF: Unpin + AddressFamily> Future for FSM<AF> {
}
}


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

0 comments on commit 83ad998

Please sign in to comment.