|
|
@ -1,5 +1,15 @@ |
|
|
|
use once_cell::sync::OnceCell; |
|
|
|
use once_cell::sync::OnceCell; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
pub fn get_libc_ifaddrs() -> Option<( |
|
|
|
|
|
|
|
unsafe extern "C" fn(*mut *mut libc::ifaddrs) -> libc::c_int, |
|
|
|
|
|
|
|
unsafe extern "C" fn(*mut libc::ifaddrs), |
|
|
|
|
|
|
|
)> { |
|
|
|
|
|
|
|
match (get_getifaddrs(), get_freeifaddrs()) { |
|
|
|
|
|
|
|
(Some(a), Some(b)) => Some((a, b)), |
|
|
|
|
|
|
|
_ => None, |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
fn load_symbol<T>(sym: &'static str) -> Option<T> { |
|
|
|
fn load_symbol<T>(sym: &'static str) -> Option<T> { |
|
|
|
const LIB_NAME: &str = "libc.so"; |
|
|
|
const LIB_NAME: &str = "libc.so"; |
|
|
|
|
|
|
|
|
|
|
@ -33,32 +43,191 @@ fn get_freeifaddrs() -> Option<unsafe extern "C" fn(*mut libc::ifaddrs)> { |
|
|
|
*INSTANCE.get_or_init(|| load_symbol("freeifaddrs")) |
|
|
|
*INSTANCE.get_or_init(|| load_symbol("freeifaddrs")) |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
pub unsafe fn getifaddrs(ifap: *mut *mut libc::ifaddrs) -> libc::c_int { |
|
|
|
mod netlink { |
|
|
|
// Android is complicated
|
|
|
|
//! Netlink based getifaddrs.
|
|
|
|
|
|
|
|
//!
|
|
|
|
|
|
|
|
//! Based on the logic found in https://git.musl-libc.org/cgit/musl/tree/src/network/getifaddrs.c
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
use netlink_packet_core::{ |
|
|
|
|
|
|
|
NetlinkHeader, NetlinkMessage, NetlinkPayload, NLM_F_DUMP, NLM_F_REQUEST, |
|
|
|
|
|
|
|
}; |
|
|
|
|
|
|
|
use netlink_packet_route::{ |
|
|
|
|
|
|
|
rtnl::address::nlas::Nla as AddressNla, rtnl::link::nlas::Nla as LinkNla, AddressMessage, |
|
|
|
|
|
|
|
LinkMessage, RtnlMessage, |
|
|
|
|
|
|
|
}; |
|
|
|
|
|
|
|
use netlink_sys::{protocols::NETLINK_ROUTE, Socket}; |
|
|
|
|
|
|
|
use std::net::{Ipv4Addr, Ipv6Addr}; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
use crate::interface::{Interface, InterfaceType, Ipv4Net, Ipv6Net, MacAddr}; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
pub fn unix_interfaces() -> Vec<Interface> { |
|
|
|
|
|
|
|
let socket = Socket::new(NETLINK_ROUTE).unwrap(); |
|
|
|
|
|
|
|
|
|
|
|
// API 24+ contains the getifaddrs and freeifaddrs functions but the NDK doesn't
|
|
|
|
let mut ifaces = Vec::new(); |
|
|
|
// expose those functions in ifaddrs.h when the minimum supported SDK is lower than 24
|
|
|
|
enumerate_netlink( |
|
|
|
// and therefore we need to load them manually.
|
|
|
|
&socket, |
|
|
|
if let Some(dyn_getifaddrs) = get_getifaddrs() { |
|
|
|
RtnlMessage::GetLink(LinkMessage::default()), |
|
|
|
return dyn_getifaddrs(ifap); |
|
|
|
&mut ifaces, |
|
|
|
|
|
|
|
); |
|
|
|
|
|
|
|
enumerate_netlink( |
|
|
|
|
|
|
|
&socket, |
|
|
|
|
|
|
|
RtnlMessage::GetAddress(AddressMessage::default()), |
|
|
|
|
|
|
|
&mut ifaces, |
|
|
|
|
|
|
|
); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
ifaces |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
// If API < 24 (or we can't load libc for some other reason), we fallback to using netlink
|
|
|
|
fn enumerate_netlink(socket: &Socket, msg: RtnlMessage, ifaces: &mut Vec<Interface>) { |
|
|
|
netlink_getifaddrs(ifap) |
|
|
|
let mut packet = NetlinkMessage::new(NetlinkHeader::default(), NetlinkPayload::from(msg)); |
|
|
|
} |
|
|
|
packet.header.flags = NLM_F_DUMP | NLM_F_REQUEST; |
|
|
|
|
|
|
|
packet.header.sequence_number = 1; |
|
|
|
|
|
|
|
packet.finalize(); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
let mut buf = vec![0; packet.header.length as usize]; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// TODO: gracefully handle error
|
|
|
|
|
|
|
|
assert!(buf.len() == packet.buffer_len()); |
|
|
|
|
|
|
|
packet.serialize(&mut buf[..]); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
socket.send(&buf[..], 0).unwrap(); |
|
|
|
|
|
|
|
|
|
|
|
pub unsafe fn freeifaddrs(ifa: *mut libc::ifaddrs) { |
|
|
|
let mut receive_buffer = vec![0; 4096]; |
|
|
|
if let Some(dyn_freeifaddrs) = get_freeifaddrs() { |
|
|
|
let mut offset = 0; |
|
|
|
return dyn_freeifaddrs(ifa); |
|
|
|
|
|
|
|
|
|
|
|
loop { |
|
|
|
|
|
|
|
let size = socket.recv(&mut &mut receive_buffer[..], 0).unwrap(); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
loop { |
|
|
|
|
|
|
|
let bytes = &receive_buffer[offset..]; |
|
|
|
|
|
|
|
let rx_packet: NetlinkMessage<RtnlMessage> = |
|
|
|
|
|
|
|
NetlinkMessage::deserialize(bytes).unwrap(); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
match rx_packet.payload { |
|
|
|
|
|
|
|
NetlinkPayload::Done => { |
|
|
|
|
|
|
|
return; |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
NetlinkPayload::Error(err) => { |
|
|
|
|
|
|
|
eprintln!("Error: {:?}", err); |
|
|
|
|
|
|
|
return; |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
NetlinkPayload::InnerMessage(msg) => { |
|
|
|
|
|
|
|
match msg { |
|
|
|
|
|
|
|
RtnlMessage::NewLink(link_msg) => { |
|
|
|
|
|
|
|
let mut interface: Interface = Interface { |
|
|
|
|
|
|
|
index: link_msg.header.index, |
|
|
|
|
|
|
|
name: String::new(), |
|
|
|
|
|
|
|
friendly_name: None, |
|
|
|
|
|
|
|
description: None, |
|
|
|
|
|
|
|
if_type: InterfaceType::try_from( |
|
|
|
|
|
|
|
link_msg.header.link_layer_type as u32, |
|
|
|
|
|
|
|
) |
|
|
|
|
|
|
|
.unwrap_or(InterfaceType::Unknown), |
|
|
|
|
|
|
|
mac_addr: None, |
|
|
|
|
|
|
|
ipv4: Vec::new(), |
|
|
|
|
|
|
|
ipv6: Vec::new(), |
|
|
|
|
|
|
|
flags: link_msg.header.flags, |
|
|
|
|
|
|
|
transmit_speed: None, |
|
|
|
|
|
|
|
receive_speed: None, |
|
|
|
|
|
|
|
gateway: None, |
|
|
|
|
|
|
|
}; |
|
|
|
|
|
|
|
|
|
|
|
netlink_freeifaddrs(ifa) |
|
|
|
for nla in link_msg.nlas { |
|
|
|
} |
|
|
|
match nla { |
|
|
|
|
|
|
|
LinkNla::IfName(name) => { |
|
|
|
|
|
|
|
interface.name = name; |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
LinkNla::Address(addr) => { |
|
|
|
|
|
|
|
match addr.len() { |
|
|
|
|
|
|
|
6 => { |
|
|
|
|
|
|
|
interface.mac_addr = Some(MacAddr::new( |
|
|
|
|
|
|
|
addr.try_into().unwrap(), |
|
|
|
|
|
|
|
)); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
4 => { |
|
|
|
|
|
|
|
let ip = Ipv4Addr::from( |
|
|
|
|
|
|
|
<[u8; 4]>::try_from(addr).unwrap(), |
|
|
|
|
|
|
|
); |
|
|
|
|
|
|
|
interface.ipv4.push(Ipv4Net::new_with_netmask( |
|
|
|
|
|
|
|
ip, |
|
|
|
|
|
|
|
Ipv4Addr::UNSPECIFIED, |
|
|
|
|
|
|
|
)); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
_ => { |
|
|
|
|
|
|
|
// unclear what these would be
|
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
_ => {} |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
ifaces.push(interface); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
RtnlMessage::NewAddress(addr_msg) => { |
|
|
|
|
|
|
|
println!("NewAddress: {:?}", addr_msg); |
|
|
|
|
|
|
|
if let Some(interface) = |
|
|
|
|
|
|
|
ifaces.iter_mut().find(|i| i.index == addr_msg.header.index) |
|
|
|
|
|
|
|
{ |
|
|
|
|
|
|
|
for nla in addr_msg.nlas { |
|
|
|
|
|
|
|
match nla { |
|
|
|
|
|
|
|
AddressNla::Address(addr) => match addr.len() { |
|
|
|
|
|
|
|
4 => { |
|
|
|
|
|
|
|
let ip = Ipv4Addr::from( |
|
|
|
|
|
|
|
<[u8; 4]>::try_from(addr).unwrap(), |
|
|
|
|
|
|
|
); |
|
|
|
|
|
|
|
interface.ipv4.push(Ipv4Net::new( |
|
|
|
|
|
|
|
ip, |
|
|
|
|
|
|
|
addr_msg.header.prefix_len, |
|
|
|
|
|
|
|
)); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
16 => { |
|
|
|
|
|
|
|
let ip = Ipv6Addr::from( |
|
|
|
|
|
|
|
<[u8; 16]>::try_from(addr).unwrap(), |
|
|
|
|
|
|
|
); |
|
|
|
|
|
|
|
interface.ipv6.push(Ipv6Net::new( |
|
|
|
|
|
|
|
ip, |
|
|
|
|
|
|
|
addr_msg.header.prefix_len, |
|
|
|
|
|
|
|
)); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
_ => { |
|
|
|
|
|
|
|
// what else?
|
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
}, |
|
|
|
|
|
|
|
_ => {} |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
} else { |
|
|
|
|
|
|
|
eprintln!( |
|
|
|
|
|
|
|
"found unknown interface with index: {}", |
|
|
|
|
|
|
|
addr_msg.header.index |
|
|
|
|
|
|
|
); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
_ => { |
|
|
|
|
|
|
|
// not expecting other messages
|
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
_ => {} |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
offset += rx_packet.header.length as usize; |
|
|
|
|
|
|
|
if offset == size || rx_packet.header.length == 0 { |
|
|
|
|
|
|
|
offset = 0; |
|
|
|
|
|
|
|
break; |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
unsafe fn netlink_getifaddrs(ifap: *mut *mut libc::ifaddrs) -> libc::c_int { |
|
|
|
#[cfg(test)] |
|
|
|
todo!() |
|
|
|
mod tests { |
|
|
|
} |
|
|
|
use super::*; |
|
|
|
|
|
|
|
|
|
|
|
unsafe fn netlink_freeifaddrs(ifa: *mut libc::ifaddrs) { |
|
|
|
#[test] |
|
|
|
todo!() |
|
|
|
fn test_netlink_ifaddrs() { |
|
|
|
|
|
|
|
let interfaces = unix_interfaces(); |
|
|
|
|
|
|
|
dbg!(&interfaces); |
|
|
|
|
|
|
|
assert!(!interfaces.is_empty()); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|