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

feat: extract match addr to type as a function #205

Merged
merged 5 commits into from
May 3, 2024
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: 9 additions & 6 deletions src/dns_parser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,14 @@ pub const FLAGS_AA: u16 = 0x0400; // mask for Authoritative answer bit

pub type DnsRecordBox = Box<dyn DnsRecordExt + Send>;

#[inline]
pub const fn ip_address_to_type(address: &IpAddr) -> u16 {
match address {
IpAddr::V4(_) => TYPE_A,
IpAddr::V6(_) => TYPE_AAAA,
}
}

#[derive(Eq, PartialEq, Debug)]
pub struct DnsEntry {
pub(crate) name: String, // always lower case.
Expand Down Expand Up @@ -865,14 +873,9 @@ impl DnsOutgoing {
)));

for address in intf_addrs {
let t = match address {
IpAddr::V4(_) => TYPE_A,
IpAddr::V6(_) => TYPE_AAAA,
};

self.add_additional_answer(Box::new(DnsAddress::new(
service.get_hostname(),
t,
ip_address_to_type(&address),
CLASS_IN | CLASS_CACHE_FLUSH,
service.get_host_ttl(),
address,
Expand Down
45 changes: 13 additions & 32 deletions src/service_daemon.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,10 +32,10 @@
use crate::log::{debug, error, warn};
use crate::{
dns_parser::{
current_time_millis, DnsAddress, DnsIncoming, DnsOutgoing, DnsPointer, DnsRecordBox,
DnsRecordExt, DnsSrv, DnsTxt, CLASS_CACHE_FLUSH, CLASS_IN, FLAGS_AA, FLAGS_QR_QUERY,
FLAGS_QR_RESPONSE, MAX_MSG_ABSOLUTE, TYPE_A, TYPE_AAAA, TYPE_ANY, TYPE_NSEC, TYPE_PTR,
TYPE_SRV, TYPE_TXT,
current_time_millis, ip_address_to_type, DnsAddress, DnsIncoming, DnsOutgoing, DnsPointer,
DnsRecordBox, DnsRecordExt, DnsSrv, DnsTxt, CLASS_CACHE_FLUSH, CLASS_IN, FLAGS_AA,
FLAGS_QR_QUERY, FLAGS_QR_RESPONSE, MAX_MSG_ABSOLUTE, TYPE_A, TYPE_AAAA, TYPE_ANY,
TYPE_NSEC, TYPE_PTR, TYPE_SRV, TYPE_TXT,
},
error::{Error, Result},
service_info::{ifaddr_subnet, split_sub_domain, ServiceInfo},
Expand Down Expand Up @@ -565,10 +565,7 @@ impl ServiceDaemon {
for (hostname, ip_addr) in
zc.cache.refresh_due_hostname_resolutions(hostname).iter()
{
match ip_addr {
IpAddr::V4(_) => zc.send_query(hostname, TYPE_A),
IpAddr::V6(_) => zc.send_query(hostname, TYPE_AAAA),
}
zc.send_query(hostname, ip_address_to_type(ip_addr));
query_count += 1;
}
}
Expand Down Expand Up @@ -1443,18 +1440,14 @@ impl Zeroconf {
debug!("No valid addrs to add on intf {:?}", &intf_sock.intf);
return false;
}
for addr in intf_addrs {
let t = match addr {
IpAddr::V4(_) => TYPE_A,
IpAddr::V6(_) => TYPE_AAAA,
};
for address in intf_addrs {
out.add_answer_at_time(
Box::new(DnsAddress::new(
info.get_hostname(),
t,
ip_address_to_type(&address),
CLASS_IN | CLASS_CACHE_FLUSH,
info.get_host_ttl(),
addr,
address,
)),
0,
);
Expand Down Expand Up @@ -1514,18 +1507,14 @@ impl Zeroconf {
0,
);

for addr in info.get_addrs_on_intf(&intf_sock.intf) {
let t = match addr {
IpAddr::V4(_) => TYPE_A,
IpAddr::V6(_) => TYPE_AAAA,
};
for address in info.get_addrs_on_intf(&intf_sock.intf) {
out.add_answer_at_time(
Box::new(DnsAddress::new(
info.get_hostname(),
t,
ip_address_to_type(&address),
CLASS_IN | CLASS_CACHE_FLUSH,
0,
addr,
address,
)),
0,
);
Expand Down Expand Up @@ -2027,15 +2016,11 @@ impl Zeroconf {
return;
}
for address in intf_addrs {
let t = match address {
IpAddr::V4(_) => TYPE_A,
IpAddr::V6(_) => TYPE_AAAA,
};
out.add_answer(
&msg,
Box::new(DnsAddress::new(
&question.entry.name,
t,
ip_address_to_type(&address),
CLASS_IN | CLASS_CACHE_FLUSH,
service.get_host_ttl(),
address,
Expand Down Expand Up @@ -2090,13 +2075,9 @@ impl Zeroconf {
return;
}
for address in intf_addrs {
let t = match address {
IpAddr::V4(_) => TYPE_A,
IpAddr::V6(_) => TYPE_AAAA,
};
out.add_additional_answer(Box::new(DnsAddress::new(
service.get_hostname(),
t,
ip_address_to_type(&address),
CLASS_IN | CLASS_CACHE_FLUSH,
service.get_host_ttl(),
address,
Expand Down