File tree 1 file changed +15
-6
lines changed
1 file changed +15
-6
lines changed Original file line number Diff line number Diff line change @@ -351,27 +351,36 @@ async fn get_default_interface(
351
351
) )
352
352
. await ?;
353
353
354
- let mut default_index = 0 ;
354
+ let mut best_index = 0 ;
355
+ let mut best_metric = u32:: MAX ;
355
356
356
357
recv_until_done ! ( sock, msg: Rtmsg => {
357
358
if msg. rtm_type != Rtn :: Unicast {
358
359
continue ;
359
360
}
361
+ // Only check default routes (rtm_dst_len == 0)
362
+ if msg. rtm_dst_len != 0 {
363
+ continue ;
364
+ }
365
+
360
366
let mut index = None ;
361
- let mut is_default = false ;
367
+ let mut metric = 0u32 ;
362
368
for attr in msg. rtattrs. iter( ) {
363
369
match attr. rta_type {
364
370
Rta :: Oif => index = Some ( attr. get_payload_as:: <i32 >( ) ?) ,
365
- Rta :: Gateway => is_default = true ,
371
+ Rta :: Priority => metric = attr . get_payload_as :: < u32 > ( ) ? ,
366
372
_ => ( ) ,
367
373
}
368
374
}
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
+ }
371
380
}
372
381
} ) ;
373
382
374
- Ok ( default_index )
383
+ Ok ( best_index )
375
384
}
376
385
377
386
async fn ip_payload < const BYTES : usize > (
You can’t perform that action at this time.
0 commit comments