net_util: Change libc::getrandom to getrandom::getrandom

libc::getrandom need to be called inside unsafe and it is not
cross-platform friendly.
Change it to getrandom::getrandom that is safe and cross-platform
friendly.

Signed-off-by: Hui Zhu <teawater@antfin.com>
This commit is contained in:
Hui Zhu 2021-09-15 21:50:03 +08:00 committed by Sebastien Boeuf
parent c741c01cc0
commit c313bcbfd4
3 changed files with 8 additions and 15 deletions

1
Cargo.lock generated
View File

@ -600,6 +600,7 @@ name = "net_util"
version = "0.1.0"
dependencies = [
"epoll",
"getrandom",
"lazy_static",
"libc",
"log",

View File

@ -6,6 +6,7 @@ edition = "2018"
[dependencies]
epoll = "4.3.1"
getrandom = "0.2"
libc = "0.2.102"
log = "0.4.14"
net_gen = { path = "../net_gen" }

View File

@ -82,21 +82,12 @@ impl MacAddr {
pub fn local_random() -> MacAddr {
// Generate a fully random MAC
let mut random_bytes = [0u8; MAC_ADDR_LEN];
unsafe {
// Man page says this function will not be interrupted by a signal
// for requests less than 256 bytes
if libc::getrandom(
random_bytes.as_mut_ptr() as *mut _ as *mut libc::c_void,
MAC_ADDR_LEN,
0,
) < 0
{
if let Err(e) = getrandom::getrandom(&mut random_bytes) {
error!(
"Error populating MAC address with random data: {}",
std::io::Error::last_os_error()
)
e.to_string()
);
}
};
// Set the first byte to make the OUI a locally administered OUI
random_bytes[0] = 0x2e;