11#![ allow( dead_code) ]
22
33use crate :: MacAddressError ;
4- use nix:: { ifaddrs:: * , sys :: socket :: SockAddr } ;
4+ use nix:: ifaddrs:: * ;
55
66/// Uses the `getifaddrs` call to retrieve a list of network interfaces on the
77/// host device and returns the first MAC address listed that isn't
@@ -10,15 +10,19 @@ pub fn get_mac(name: Option<&str>) -> Result<Option<[u8; 6]>, MacAddressError> {
1010 let ifiter = getifaddrs ( ) ?;
1111
1212 for interface in ifiter {
13- if let Some ( SockAddr :: Link ( link) ) = interface. address {
14- let bytes = link. addr ( ) ;
15-
16- if let Some ( name) = name {
17- if interface. interface_name == name {
18- return Ok ( Some ( bytes) ) ;
13+ if let Some ( iface_address) = interface. address {
14+ if let Some ( link) = iface_address. as_link_addr ( ) {
15+ let bytes = link. addr ( ) ;
16+
17+ if let Some ( name) = name {
18+ if interface. interface_name == name {
19+ return Ok ( bytes) ;
20+ }
21+ } else if let Some ( bytes) = bytes {
22+ if bytes. iter ( ) . any ( |& x| x != 0 ) {
23+ return Ok ( Some ( bytes) ) ;
24+ }
1925 }
20- } else if bytes. iter ( ) . any ( |& x| x != 0 ) {
21- return Ok ( Some ( bytes) ) ;
2226 }
2327 }
2428 }
@@ -30,11 +34,13 @@ pub fn get_ifname(mac: &[u8; 6]) -> Result<Option<String>, MacAddressError> {
3034 let ifiter = getifaddrs ( ) ?;
3135
3236 for interface in ifiter {
33- if let Some ( SockAddr :: Link ( link) ) = interface. address {
34- let bytes = link. addr ( ) ;
37+ if let Some ( iface_address) = interface. address {
38+ if let Some ( link) = iface_address. as_link_addr ( ) {
39+ let bytes = link. addr ( ) ;
3540
36- if & bytes == mac {
37- return Ok ( Some ( interface. interface_name ) ) ;
41+ if bytes == Some ( * mac) {
42+ return Ok ( Some ( interface. interface_name ) ) ;
43+ }
3844 }
3945 }
4046 }
0 commit comments