MCPcopy Create free account
hub / github.com/cloud-hypervisor/cloud-hypervisor / get_mac_addr

Method get_mac_addr

net_util/src/tap.rs:396–419  ·  view source on GitHub ↗

Get mac addr for tap interface.

(&self)

Source from the content-addressed store, hash-verified

394
395 /// Get mac addr for tap interface.
396 pub fn get_mac_addr(&self) -> Result<MacAddr> {
397 let sock = create_unix_socket().map_err(Error::NetUtil)?;
398
399 let ifreq = self.get_ifreq();
400
401 // SAFETY: ioctl is safe. Called with a valid sock fd, and we check the return.
402 unsafe { Self::ioctl_with_ref(&sock, libc::SIOCGIFHWADDR as c_ulong, &ifreq)? };
403
404 let addr = {
405 let bytes: Vec<u8> =
406 // SAFETY: The `ioctl_with_ref` ensures accessing `ifru_hwaddr` is valid.
407 unsafe { ifreq.ifr_ifru.ifru_hwaddr.sa_data[0..MAC_ADDR_LEN].iter() }
408 .map(|byte| {
409 // On some architectures, `c_char` is already a `u8`.
410 #[allow(clippy::unnecessary_cast)]
411 {
412 *byte as u8
413 }
414 })
415 .collect();
416 MacAddr::from_bytes(&bytes).map_err(Error::MacParsing)?
417 };
418 Ok(addr)
419 }
420
421 #[cfg(not(fuzzing))]
422 pub fn mtu(&self) -> Result<i32> {

Callers 2

set_mac_addrMethod · 0.80
open_tap_rx_q_0Function · 0.80

Calls 4

create_unix_socketFunction · 0.85
get_ifreqMethod · 0.80
iterMethod · 0.80
mapMethod · 0.45

Tested by

no test coverage detected