@@ -160,7 +160,9 @@ pub struct Nic {
160160 pub address : IpAddr ,
161161 pub nic_type : NicType ,
162162 pub name : String ,
163- pub priority : usize ,
163+ priority : usize ,
164+ interface_index : u32 ,
165+ addr_index : usize ,
164166}
165167impl PartialOrd for Nic {
166168 fn partial_cmp ( & self , other : & Self ) -> Option < std:: cmp:: Ordering > {
@@ -178,8 +180,8 @@ impl Ord for Nic {
178180 . then_with ( || other. address . is_ipv4 ( ) . cmp ( & self . address . is_ipv4 ( ) ) )
179181 // Then rdma interfaces
180182 . then_with ( || self . nic_type . cmp ( & other. nic_type ) )
181- . then_with ( || self . address . cmp ( & other. address ) )
182- . then_with ( || self . name . cmp ( & other. name ) )
183+ . then_with ( || self . interface_index . cmp ( & other. interface_index ) )
184+ . then_with ( || self . addr_index . cmp ( & other. addr_index ) )
183185 }
184186}
185187
@@ -191,7 +193,7 @@ pub fn query_nics(filter: &[NicFilter], use_ipv6: bool) -> Result<Vec<Nic>> {
191193 let mut filtered_nics = vec ! [ ] ;
192194
193195 for interface in pnet_datalink:: interfaces ( ) {
194- for ip in interface. ips {
196+ for ( addr_index , ip ) in interface. ips . iter ( ) . enumerate ( ) {
195197 if !use_ipv6 && ip. is_ipv6 ( ) {
196198 continue ;
197199 }
@@ -202,6 +204,8 @@ pub fn query_nics(filter: &[NicFilter], use_ipv6: bool) -> Result<Vec<Nic>> {
202204 address : ip. ip ( ) ,
203205 nic_type : NicType :: Ethernet ,
204206 priority,
207+ interface_index : interface. index ,
208+ addr_index,
205209 } ) ;
206210 }
207211 }
@@ -459,39 +463,67 @@ mod test {
459463 nic_type : NicType :: Ethernet ,
460464 name : "a" . into ( ) ,
461465 priority : 0 ,
466+ interface_index : 0 ,
467+ addr_index : 0 ,
462468 } ,
463469 Nic {
464470 address : IpAddr :: from_str ( "192.168.0.2" ) . unwrap ( ) ,
465471 nic_type : NicType :: Ethernet ,
466472 name : "b" . into ( ) ,
467473 priority : 1 ,
474+ interface_index : 0 ,
475+ addr_index : 0 ,
468476 } ,
469477 Nic {
470478 address : IpAddr :: from_str ( "192.168.0.1" ) . unwrap ( ) ,
471479 nic_type : NicType :: Ethernet ,
472480 name : "a" . into ( ) ,
473481 priority : 0 ,
482+ interface_index : 0 ,
483+ addr_index : 0 ,
474484 } ,
475485 Nic {
476486 address : IpAddr :: from_str ( "192.168.0.3" ) . unwrap ( ) ,
477487 nic_type : NicType :: Rdma ,
478488 name : "a" . into ( ) ,
479489 priority : 0 ,
490+ interface_index : 1 ,
491+ addr_index : 0 ,
480492 } ,
481493 Nic {
482494 address : IpAddr :: from_str ( "::1" ) . unwrap ( ) ,
483495 nic_type : NicType :: Ethernet ,
484496 name : "a" . into ( ) ,
485497 priority : 0 ,
498+ interface_index : 0 ,
499+ addr_index : 0 ,
500+ } ,
501+ Nic {
502+ address : IpAddr :: from_str ( "192.168.0.4" ) . unwrap ( ) ,
503+ nic_type : NicType :: Rdma ,
504+ name : "a" . into ( ) ,
505+ priority : 0 ,
506+ interface_index : 0 ,
507+ addr_index : 1 ,
508+ } ,
509+ Nic {
510+ address : IpAddr :: from_str ( "192.168.0.5" ) . unwrap ( ) ,
511+ nic_type : NicType :: Rdma ,
512+ name : "a" . into ( ) ,
513+ priority : 0 ,
514+ interface_index : 0 ,
515+ addr_index : 0 ,
486516 } ,
487517 ] ;
488518
489519 nics. sort ( ) ;
490520
491- assert_eq ! ( nics[ 0 ] . address, IpAddr :: from_str( "192.168.0.3" ) . unwrap( ) ) ;
492- assert_eq ! ( nics[ 1 ] . address, IpAddr :: from_str( "192.168.0.1" ) . unwrap( ) ) ;
493- assert_eq ! ( nics[ 2 ] . address, IpAddr :: from_str( "127.0.0.1" ) . unwrap( ) ) ;
494- assert_eq ! ( nics[ 3 ] . address, IpAddr :: from_str( "::1" ) . unwrap( ) ) ;
495- assert_eq ! ( nics[ 4 ] . address, IpAddr :: from_str( "192.168.0.2" ) . unwrap( ) ) ;
521+ assert_eq ! ( nics[ 0 ] . address, IpAddr :: from_str( "192.168.0.5" ) . unwrap( ) ) ;
522+ assert_eq ! ( nics[ 1 ] . address, IpAddr :: from_str( "192.168.0.4" ) . unwrap( ) ) ;
523+ assert_eq ! ( nics[ 2 ] . address, IpAddr :: from_str( "192.168.0.3" ) . unwrap( ) ) ;
524+ assert_eq ! ( nics[ 3 ] . address, IpAddr :: from_str( "192.168.0.1" ) . unwrap( ) ) ;
525+ assert_eq ! ( nics[ 4 ] . address, IpAddr :: from_str( "127.0.0.1" ) . unwrap( ) ) ;
526+ assert_eq ! ( nics[ 5 ] . address, IpAddr :: from_str( "::1" ) . unwrap( ) ) ;
527+ assert_eq ! ( nics[ 6 ] . address, IpAddr :: from_str( "192.168.0.2" ) . unwrap( ) ) ;
496528 }
497529}
0 commit comments