Skip to content

Commit

Permalink
linux: use correct message types for requests (#121)
Browse files Browse the repository at this point in the history
Commit 109ff03 ("feat(linux): rewrite list_afinet_netifas (#103)") used
the wrong message types for requests, while using the correct ones for
responses.  According to rtnetlink(7) RTM_GETLINK messages use the type
ifinfomsg and RTM_GETADDR messages use the type ifaddrmsg.

Fixes: 109ff03 ("feat(linux): rewrite list_afinet_netifas (#103)")
  • Loading branch information
cgzones authored May 5, 2023
1 parent 788c02e commit 5d606b6
Showing 1 changed file with 18 additions and 24 deletions.
42 changes: 18 additions & 24 deletions src/linux.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ use neli::consts::nl::{NlmF, NlmFFlags};
use neli::consts::socket::NlFamily;
use neli::consts::rtnl::{
Ifa, IfaFFlags, RtAddrFamily, RtScope, Rtm, RtTable, Rtprot, Rtn, RtmFFlags, RtmF, Rta, Ifla,
IffFlags, Arphrd,
};
use neli::nl::{NlPayload, Nlmsghdr};
use neli::rtnl::{Ifaddrmsg, Ifinfomsg, Rtattr, Rtmsg};
Expand Down Expand Up @@ -254,25 +255,22 @@ pub fn list_afinet_netifas() -> Result<Vec<(String, IpAddr)>, Error> {

// First get list of interfaces via RTM_GETLINK

let ifroutemsg = Rtmsg {
rtm_family: RtAddrFamily::Unspecified,
rtm_dst_len: 0,
rtm_src_len: 0,
rtm_tos: 0,
rtm_table: RtTable::Unspec,
rtm_protocol: Rtprot::Unspec,
rtm_scope: RtScope::Universe,
rtm_type: Rtn::Unspec,
rtm_flags: RtmFFlags::new(RTM_FLAGS_LOOKUP),
rtattrs: RtBuffer::new(),
};
let ifinfomsg = Ifinfomsg::new(
RtAddrFamily::Unspecified,
Arphrd::from(0),
0,
IffFlags::empty(),
IffFlags::empty(),
RtBuffer::new(),
);

let netlink_message = Nlmsghdr::new(
None,
Rtm::Getlink,
NlmFFlags::new(&[NlmF::Request, NlmF::Dump]),
None,
None,
NlPayload::Payload(ifroutemsg),
NlPayload::Payload(ifinfomsg),
);

netlink_socket
Expand Down Expand Up @@ -315,16 +313,12 @@ pub fn list_afinet_netifas() -> Result<Vec<(String, IpAddr)>, Error> {

// Secondly get addresses of interfaces via RTM_GETADDR

let ifroutemsg = Rtmsg {
rtm_family: RtAddrFamily::Unspecified,
rtm_dst_len: 0,
rtm_src_len: 0,
rtm_tos: 0,
rtm_table: RtTable::Unspec,
rtm_protocol: Rtprot::Unspec,
rtm_scope: RtScope::Universe,
rtm_type: Rtn::Unspec,
rtm_flags: RtmFFlags::new(RTM_FLAGS_LOOKUP),
let ifaddrmsg = Ifaddrmsg {
ifa_family: RtAddrFamily::Unspecified,
ifa_prefixlen: 0,
ifa_flags: IfaFFlags::empty(),
ifa_scope: 0,
ifa_index: 0,
rtattrs: RtBuffer::new(),
};
let netlink_message = Nlmsghdr::new(
Expand All @@ -333,7 +327,7 @@ pub fn list_afinet_netifas() -> Result<Vec<(String, IpAddr)>, Error> {
NlmFFlags::new(&[NlmF::Request, NlmF::Dump]),
None,
None,
NlPayload::Payload(ifroutemsg),
NlPayload::Payload(ifaddrmsg),
);

netlink_socket
Expand Down

0 comments on commit 5d606b6

Please sign in to comment.