Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ServiceInfo: support get_addresses_v4 #132

Merged
merged 2 commits into from
Oct 9, 2023
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
15 changes: 14 additions & 1 deletion src/service_info.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ use std::{
collections::{HashMap, HashSet},
convert::TryInto,
fmt,
net::IpAddr,
net::{IpAddr, Ipv4Addr},
str::FromStr,
};

Expand Down Expand Up @@ -202,6 +202,19 @@ impl ServiceInfo {
&self.addresses
}

/// Returns the service's IPv4 addresses only.
pub fn get_addresses_v4(&self) -> HashSet<&Ipv4Addr> {
let mut ipv4_addresses = HashSet::new();

for ip in &self.addresses {
if let IpAddr::V4(ipv4) = ip {
ipv4_addresses.insert(ipv4);
}
}

ipv4_addresses
}

/// Returns the service's TTL used for SRV and Address records.
#[inline]
pub fn get_host_ttl(&self) -> u32 {
Expand Down
6 changes: 1 addition & 5 deletions tests/mdns_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -263,11 +263,7 @@ fn service_without_properties_with_alter_net_v4() {
);
// match only our service and not v6 one
if fullname.as_str() == info.get_fullname() {
let addrs: Vec<&IpAddr> = info
.get_addresses()
.into_iter()
.filter(|a| a.is_ipv4())
.collect();
let addrs = info.get_addresses_v4();
assert_eq!(addrs.len(), 1); // first_ipv4 but no alter_ipv.
found = true;
break;
Expand Down