use std::net::{IpAddr, Ipv4Addr, Ipv6Addr};
use std::ptr;
pub fn get_users<T: From<libc::utmpx>>() -> Vec<T> {
let mut users = Vec::with_capacity(1);
unsafe {
libc::setutxent();
loop {
let entry = libc::getutxent();
if entry.is_null() {
break;
}
if (*entry).ut_type != libc::USER_PROCESS {
continue;
}
users.push(T::from(*entry))
}
libc::endutxent();
}
users
}
#[allow(unused, trivial_casts)]
pub(crate) fn from_ut_addr_v6(addr: &[i32; 4]) -> Option<IpAddr> {
match addr {
[0, 0, 0, 0] => None,
[octet, 0, 0, 0] => Some(Ipv4Addr::from(*octet as u32).into()),
octets => {
let mut raw_ip: u128 = 0;
unsafe {
ptr::copy_nonoverlapping(octets.as_ptr(), &mut raw_ip as *mut u128 as *mut i32, 16);
}
Some(Ipv6Addr::from(raw_ip).into())
}
}
}