Skip to content

Commit

Permalink
mdns: get_interface_addr_for_ip once per query (#275)
Browse files Browse the repository at this point in the history
  • Loading branch information
melekes authored Sep 1, 2022
1 parent e549e67 commit 86255e8
Showing 1 changed file with 19 additions and 12 deletions.
31 changes: 19 additions & 12 deletions mdns/src/conn/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -297,6 +297,7 @@ async fn run(
dst_addr: SocketAddr,
queries: &Arc<Mutex<Vec<Query>>>,
) {
let mut interface_addr = None;
for _ in 0..=MAX_MESSAGE_RECORDS {
let q = match p.question() {
Ok(q) => q,
Expand All @@ -313,16 +314,22 @@ async fn run(

for local_name in local_names {
if *local_name == q.name.data {
let interface_addr = match get_interface_addr_for_ip(src).await {
Ok(e) => e,
Err(e) => {
log::warn!(
"Failed to get local interface to communicate with {}: {:?}",
&src,
e
);
continue;
}
let interface_addr = match interface_addr {
Some(addr) => addr,
None => match get_interface_addr_for_ip(src).await {
Ok(addr) => {
interface_addr.replace(addr);
addr
}
Err(e) => {
log::warn!(
"Failed to get local interface to communicate with {}: {:?}",
&src,
e
);
continue;
}
},
};

log::trace!(
Expand All @@ -332,7 +339,7 @@ async fn run(
interface_addr
);
if let Err(e) =
send_answer(socket, interface_addr, &q.name.data, src.ip(), dst_addr).await
send_answer(socket, &interface_addr, &q.name.data, src.ip(), dst_addr).await
{
log::error!("Error sending answer to client: {:?}", e);
continue;
Expand Down Expand Up @@ -374,7 +381,7 @@ async fn run(

async fn send_answer(
socket: &Arc<UdpSocket>,
interface_addr: SocketAddr,
interface_addr: &SocketAddr,
name: &str,
dst: IpAddr,
dst_addr: SocketAddr,
Expand Down

0 comments on commit 86255e8

Please sign in to comment.