refactor(netlink): Migrate nl_interface_get

This commit is contained in:
Nils Andreas Svee committed 2026-02-21 00:22:57 +07:00
1 parent 200ec32201
commit ca1bc8f8b5
1 file changed
+35 -27
+35 -27
View File
@@ -88,56 +88,64 @@ pub enum TcaCake {
pub struct Netlink {} pub struct Netlink {}
impl Netlink { impl Netlink {
fn nl_interface_get(socket: &mut NlSocketHandle, ifname: &str) -> Result<(), NetlinkError> { fn nl_interface_get(
socket: &NlRouter,
ifname: &str,
) -> Result<neli::router::synchronous::NlRouterReceiverHandle<Rtm, Ifinfomsg>, NetlinkError>
{
let mut attrs = RtBuffer::new(); let mut attrs = RtBuffer::new();
const RTEXT_FILTER_VF: i32 = 1 << 0; const RTEXT_FILTER_VF: i32 = 1 << 0;
let attr_ifname = Rtattr::new(None, Ifla::Ifname, ifname)?; let attr_ifname = RtattrBuilder::default()
let attr_ext_mask = Rtattr::new(None, Ifla::ExtMask, RTEXT_FILTER_VF)?; .rta_type(Ifla::Ifname)
.rta_payload(ifname)
.build()
.unwrap();
let attr_ext_mask = RtattrBuilder::default()
.rta_type(Ifla::ExtMask)
.rta_payload(RTEXT_FILTER_VF)
.build()
.unwrap();
attrs.push(attr_ifname); attrs.push(attr_ifname);
attrs.push(attr_ext_mask); attrs.push(attr_ext_mask);
let if_msg = Ifinfomsg::new( let if_msg = IfinfomsgBuilder::default()
RtAddrFamily::Unspecified, .ifi_family(RtAddrFamily::Unspecified)
Arphrd::None, .ifi_type(Arphrd::None)
-1, .ifi_index(-1)
IffFlags::empty(), .ifi_flags(Iff::empty())
IffFlags::empty(), .ifi_change(Iff::empty())
attrs, .rtattrs(attrs)
); .build()
.unwrap();
let nlhdr = Nlmsghdr::new( let recv = socket.send::<_, _, Rtm, Ifinfomsg>(
None,
Rtm::Getlink, Rtm::Getlink,
NlmFFlags::new(&[NlmF::Request, NlmF::Ack]), NlmF::REQUEST | NlmF::ACK,
None,
None,
NlPayload::Payload(if_msg), NlPayload::Payload(if_msg),
); )?;
socket.send(nlhdr)?; Ok(recv)
Ok(())
} }
pub fn find_interface(ifname: &str) -> Result<i32, NetlinkError> { pub fn find_interface(ifname: &str) -> Result<i32, NetlinkError> {
let mut socket = NlSocketHandle::connect(NlFamily::Route, None, &[]).unwrap(); let (socket, _) = NlRouter::connect(NlFamily::Route, None, Groups::empty()).unwrap();
Self::nl_interface_get(&mut socket, ifname)?; let recv = Self::nl_interface_get(&socket, ifname)?;
for response in socket.iter(false) { for response in recv {
let header: Nlmsghdr<Rtm, Ifinfomsg> = response?; let header: Nlmsghdr<Rtm, Ifinfomsg> = response?;
if header.nl_type != Rtm::Newlink { if header.nl_type() != &Rtm::Newlink {
return Err(NetlinkError::WrongType { return Err(NetlinkError::WrongType {
expected: Rtm::Newlink, expected: Rtm::Newlink,
found: header.nl_type, found: *header.nl_type(),
}); });
} }
if let NlPayload::Payload(p) = header.nl_payload { if let NlPayload::Payload(p) = header.nl_payload() {
return Ok(p.ifi_index); return Ok(*p.ifi_index());
} }
} }