Skip to content

Commit 0eaf8cb

Browse files
committed
100
1 parent 71cb8d7 commit 0eaf8cb

File tree

1 file changed

+15
-6
lines changed

1 file changed

+15
-6
lines changed

src/netlink.rs

+15-6
Original file line numberDiff line numberDiff line change
@@ -351,27 +351,36 @@ async fn get_default_interface(
351351
))
352352
.await?;
353353

354-
let mut default_index = 0;
354+
let mut best_index = 0;
355+
let mut best_metric = u32::MAX;
355356

356357
recv_until_done!(sock, msg: Rtmsg => {
357358
if msg.rtm_type != Rtn::Unicast {
358359
continue;
359360
}
361+
// Only check default routes (rtm_dst_len == 0)
362+
if msg.rtm_dst_len != 0 {
363+
continue;
364+
}
365+
360366
let mut index = None;
361-
let mut is_default = false;
367+
let mut metric = 0u32;
362368
for attr in msg.rtattrs.iter() {
363369
match attr.rta_type {
364370
Rta::Oif => index = Some(attr.get_payload_as::<i32>()?),
365-
Rta::Gateway => is_default = true,
371+
Rta::Priority => metric = attr.get_payload_as::<u32>()?,
366372
_ => (),
367373
}
368374
}
369-
if is_default && default_index == 0 {
370-
default_index = index.unwrap();
375+
if let Some(i) = index {
376+
if metric < best_metric {
377+
best_metric = metric;
378+
best_index = i;
379+
}
371380
}
372381
});
373382

374-
Ok(default_index)
383+
Ok(best_index)
375384
}
376385

377386
async fn ip_payload<const BYTES: usize>(

0 commit comments

Comments
 (0)