Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 2 additions & 19 deletions src/dns_parser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ use std::{
collections::HashMap,
convert::TryInto,
fmt,
hash::{Hash, Hasher},
hash::Hash,
net::{IpAddr, Ipv4Addr, Ipv6Addr},
str,
time::SystemTime,
Expand Down Expand Up @@ -66,31 +66,14 @@ impl From<&Interface> for InterfaceId {
}

/// An IPv4 address with interface identifiers indicating which interfaces discovered it.
///
/// Two `ScopedIpV4` values are considered equal if their addresses are equal,
/// regardless of which interfaces discovered them.
#[derive(Debug, Clone)]
#[derive(Debug, Clone, Eq, PartialEq, Hash)]
#[cfg_attr(feature = "serde", derive(Deserialize, Serialize))]
pub struct ScopedIpV4 {
addr: Ipv4Addr,
/// The interfaces this address was discovered on.
interface_ids: Vec<InterfaceId>,
}

impl PartialEq for ScopedIpV4 {
fn eq(&self, other: &Self) -> bool {
self.addr == other.addr
}
}

impl Eq for ScopedIpV4 {}

impl Hash for ScopedIpV4 {
fn hash<H: Hasher>(&self, state: &mut H) {
self.addr.hash(state);
}
}

impl ScopedIpV4 {
/// Creates a new `ScopedIpV4` with a single interface identifier.
pub fn new(addr: Ipv4Addr, interface_id: InterfaceId) -> Self {
Expand Down
11 changes: 9 additions & 2 deletions src/service_daemon.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2561,8 +2561,15 @@ impl Zeroconf {
} else {
let scoped = dns_a.address();
if let ScopedIp::V4(v4) = &scoped {
// Merge interface_ids if this V4 addr already exists
if let Some(mut existing) = resolved_service.addresses.take(&scoped) {
// Merge interface_ids if this V4 addr already exists.
// Linear scan by IP since Eq/Hash include interface_ids.
let existing = resolved_service
.addresses
.iter()
.find(|a| a.to_ip_addr() == IpAddr::V4(*v4.addr()))
.cloned();
if let Some(mut existing) = existing {
resolved_service.addresses.remove(&existing);
if let ScopedIp::V4(existing_v4) = &mut existing {
for id in v4.interface_ids() {
existing_v4.add_interface_id(id.clone());
Expand Down
27 changes: 27 additions & 0 deletions src/service_info.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1706,6 +1706,33 @@ mod tests {
assert!(service_info.is_address_supported(&intf_loopback_v6));
}

#[test]
fn test_scoped_ip_set_detects_interface_id_change() {
use crate::{InterfaceId, ScopedIp, ScopedIpV4};
use std::collections::HashSet;

let intf1 = InterfaceId {
name: "en0".to_string(),
index: 1,
};
let intf2 = InterfaceId {
name: "en1".to_string(),
index: 2,
};
let addr = Ipv4Addr::new(192, 168, 1, 100);

let scoped_v4_one_intf = ScopedIpV4::new(addr, intf1);
let mut scoped_v4_two_intfs = scoped_v4_one_intf.clone();
scoped_v4_two_intfs.add_interface_id(intf2);

assert_ne!(scoped_v4_one_intf, scoped_v4_two_intfs);

let set_old: HashSet<ScopedIp> = HashSet::from([ScopedIp::V4(scoped_v4_one_intf)]);
let set_new: HashSet<ScopedIp> = HashSet::from([ScopedIp::V4(scoped_v4_two_intfs)]);

assert_ne!(set_old, set_new);
}

#[cfg(test)]
#[cfg(feature = "serde")]
mod serde {
Expand Down