Skip to content
Open
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
47 changes: 40 additions & 7 deletions src/dns_parser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,18 @@ pub struct InterfaceId {
pub index: u32,
}

impl InterfaceId {
/// Returns all IP addresses associated with this interface by querying the OS.
pub fn get_addrs(&self) -> Vec<IpAddr> {
if_addrs::get_if_addrs()
.unwrap_or_default()
.into_iter()
.filter(|iface| iface.index == Some(self.index))
.map(|iface| iface.ip())
.collect()
}
}

impl fmt::Display for InterfaceId {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "{}('{}')", self.index, self.name)
Expand All @@ -48,20 +60,24 @@ impl From<&Interface> for InterfaceId {
}
}

/// An IPv4 address used in `ScopedIp`.
///
/// Note: IPv4 addresses don't have scope IDs, but this type is named for consistency
/// with the rest of the addressing system.
/// An IPv4 address with an interface identifier.
#[derive(Debug, Clone, Eq, PartialEq, Hash)]
pub struct ScopedIpV4 {
addr: Ipv4Addr,
/// The `interface_id` indicates which interface this address is associated with.
interface_id: InterfaceId,
}

impl ScopedIpV4 {
/// Returns the IPv4 address.
pub const fn addr(&self) -> &Ipv4Addr {
&self.addr
}

/// Returns the interface this address is found on.
pub const fn interface_id(&self) -> &InterfaceId {
&self.interface_id
}
}

/// An IPv6 address with scope_id (interface identifier).
Expand Down Expand Up @@ -113,12 +129,23 @@ impl ScopedIp {
ScopedIp::V6(v6) => v6.addr.is_loopback(),
}
}

/// Returns the interface identifier for this address.
pub const fn interface_id(&self) -> &InterfaceId {
match self {
ScopedIp::V4(v4) => &v4.interface_id,
ScopedIp::V6(v6) => &v6.scope_id,
}
}
}

impl From<IpAddr> for ScopedIp {
fn from(ip: IpAddr) -> Self {
match ip {
IpAddr::V4(v4) => ScopedIp::V4(ScopedIpV4 { addr: v4 }),
IpAddr::V4(v4) => ScopedIp::V4(ScopedIpV4 {
addr: v4,
interface_id: InterfaceId::default(),
}),
IpAddr::V6(v6) => ScopedIp::V6(ScopedIpV6 {
addr: v6,
scope_id: InterfaceId::default(),
Expand All @@ -130,7 +157,10 @@ impl From<IpAddr> for ScopedIp {
impl From<&Interface> for ScopedIp {
fn from(interface: &Interface) -> Self {
match interface.ip() {
IpAddr::V4(v4) => ScopedIp::V4(ScopedIpV4 { addr: v4 }),
IpAddr::V4(v4) => ScopedIp::V4(ScopedIpV4 {
addr: v4,
interface_id: InterfaceId::from(interface),
}),
IpAddr::V6(v6) => ScopedIp::V6(ScopedIpV6 {
addr: v6,
scope_id: InterfaceId::from(interface),
Expand Down Expand Up @@ -666,7 +696,10 @@ impl DnsAddress {

pub fn address(&self) -> ScopedIp {
match self.address {
IpAddr::V4(v4) => ScopedIp::V4(ScopedIpV4 { addr: v4 }),
IpAddr::V4(v4) => ScopedIp::V4(ScopedIpV4 {
addr: v4,
interface_id: self.interface_id.clone(),
}),
IpAddr::V6(v6) => ScopedIp::V6(ScopedIpV6 {
addr: v6,
scope_id: self.interface_id.clone(),
Expand Down
2 changes: 1 addition & 1 deletion src/service_daemon.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4966,7 +4966,7 @@ mod tests {
let service_ip_addr: ScopedIp = my_ip_interfaces(false)
.iter()
.find(|iface| iface.ip().is_ipv4())
.map(|iface| iface.ip().into())
.map(|iface| iface.into())
.unwrap();

let mut my_service = ServiceInfo::new(
Expand Down
24 changes: 22 additions & 2 deletions tests/mdns_test.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use if_addrs::{IfAddr, Interface};
use mdns_sd::{
DaemonEvent, DaemonStatus, HostnameResolutionEvent, IfKind, IntoTxtProperties, ScopedIp,
ServiceDaemon, ServiceEvent, ServiceInfo, TxtProperty, UnregisterStatus,
DaemonEvent, DaemonStatus, HostnameResolutionEvent, IfKind, InterfaceId, IntoTxtProperties,
ScopedIp, ServiceDaemon, ServiceEvent, ServiceInfo, TxtProperty, UnregisterStatus,
};
use std::collections::{HashMap, HashSet};
use std::net::{IpAddr, Ipv4Addr, Ipv6Addr};
Expand Down Expand Up @@ -2553,6 +2553,26 @@ fn test_rfc6763_utf8_network_integration() {
d.shutdown().unwrap();
}

#[test]
fn test_interface_id_get_addrs() {
// Get actual interfaces from the OS to build a valid InterfaceId.
let if_addrs = if_addrs::get_if_addrs().expect("failed to get interfaces");
let first = if_addrs.first().expect("no interfaces found");
let intf_id = InterfaceId::from(first);

let addrs = intf_id.get_addrs();
assert!(
!addrs.is_empty(),
"interface {} should have at least one address",
intf_id.name
);
assert!(
addrs.contains(&first.ip()),
"get_addrs() should contain the address we constructed from: {}",
first.ip()
);
}

/// A helper function to include a timestamp for println.
fn timed_println(msg: String) {
let now = SystemTime::now();
Expand Down