From 8fa1f5ba6b6f927a20caa57c51adac1f1a3d4730 Mon Sep 17 00:00:00 2001 From: John Baublitz Date: Thu, 23 Jan 2025 15:15:19 -0500 Subject: [PATCH 1/4] Fix test and add example --- examples/getlink.rs | 43 +++++++++++++++++++++++++++++++++++++++++++ src/rtnl.rs | 37 +++++++++++++++++++++++++------------ 2 files changed, 68 insertions(+), 12 deletions(-) create mode 100644 examples/getlink.rs diff --git a/examples/getlink.rs b/examples/getlink.rs new file mode 100644 index 00000000..bee8571e --- /dev/null +++ b/examples/getlink.rs @@ -0,0 +1,43 @@ +use neli::{ + consts::{ + nl::NlmF, + rtnl::{Arphrd, Ifla, RtAddrFamily, Rtm}, + socket::NlFamily, + }, + nl::NlPayload, + router::synchronous::NlRouter, + rtnl::{Ifinfomsg, IfinfomsgBuilder}, + utils::Groups, +}; + +fn main() -> Result<(), Box> { + env_logger::init(); + + let (rtnl, _) = NlRouter::connect(NlFamily::Route, None, Groups::empty())?; + rtnl.enable_ext_ack(true)?; + rtnl.enable_strict_checking(true)?; + let ifinfomsg = IfinfomsgBuilder::default() + .ifi_family(RtAddrFamily::Inet) + .ifi_type(Arphrd::None) + .ifi_index(0) + .build()?; + + let recv = rtnl.send::<_, _, Rtm, Ifinfomsg>( + Rtm::Getlink, + NlmF::DUMP | NlmF::ACK, + NlPayload::Payload(ifinfomsg), + )?; + for response in recv { + if let Some(payload) = response?.get_payload() { + println!( + "{:?}", + payload + .rtattrs() + .get_attr_handle() + .get_attr_payload_as_with_len::(Ifla::Ifname)?, + ) + } + } + + Ok(()) +} diff --git a/src/rtnl.rs b/src/rtnl.rs index f110ae0f..fb3e8bee 100644 --- a/src/rtnl.rs +++ b/src/rtnl.rs @@ -428,6 +428,7 @@ mod test { use crate::{ consts::{nl::NlmF, socket::NlFamily}, + err::RouterError, nl::NlPayload, router::synchronous::NlRouter, test::setup, @@ -474,7 +475,7 @@ mod test { let (sock, _) = NlRouter::connect(NlFamily::Route, None, Groups::empty()).unwrap(); sock.enable_strict_checking(true).unwrap(); - let recv = sock + let mut recv = sock .send::<_, _, Rtm, Ifinfomsg>( Rtm::Getlink, NlmF::DUMP | NlmF::ACK, @@ -488,17 +489,29 @@ mod test { ), ) .unwrap(); - for msg in recv { - let msg = msg.unwrap(); - if let Some(payload) = msg.get_payload() { - let handle = payload.rtattrs.get_attr_handle(); - handle - .get_attr_payload_as_with_len::(Ifla::Ifname) - .unwrap(); - // Assert length of ethernet address - if let Ok(attr) = handle.get_attr_payload_as_with_len::>(Ifla::Address) { - assert_eq!(attr.len(), 6); - } + let all_msgs = recv + .try_fold(Vec::new(), |mut v, m| { + v.push(m?); + Result::<_, RouterError>::Ok(v) + }) + .unwrap(); + let non_err_payloads = all_msgs.iter().fold(Vec::new(), |mut v, m| { + if let Some(p) = m.get_payload() { + v.push(p); + } + v + }); + if non_err_payloads.is_empty() { + panic!("Only received done message and no additional information"); + } + for payload in non_err_payloads { + let handle = payload.rtattrs.get_attr_handle(); + handle + .get_attr_payload_as_with_len::(Ifla::Ifname) + .unwrap(); + // Assert length of ethernet address + if let Ok(attr) = handle.get_attr_payload_as_with_len::>(Ifla::Address) { + assert_eq!(attr.len(), 6); } } } From ca352af37ab69fb3ed48e2557452cd35b056d757 Mon Sep 17 00:00:00 2001 From: John Baublitz Date: Fri, 24 Jan 2025 11:11:37 -0500 Subject: [PATCH 2/4] Add additional logging for buffer sent at trace level --- src/socket/synchronous.rs | 1 + 1 file changed, 1 insertion(+) diff --git a/src/socket/synchronous.rs b/src/socket/synchronous.rs index 2ec394c5..1e9e5d55 100644 --- a/src/socket/synchronous.rs +++ b/src/socket/synchronous.rs @@ -76,6 +76,7 @@ impl NlSocketHandle { let mut buffer = Cursor::new(vec![0; msg.padded_size()]); msg.to_bytes(&mut buffer)?; + trace!("Buffer sent: {:?}", buffer.get_ref()); self.socket.send(buffer.get_ref(), Msg::empty())?; Ok(()) From f23bd2cf99bffc559aad6114ae3af4d7ba71a1e0 Mon Sep 17 00:00:00 2001 From: John Baublitz Date: Fri, 24 Jan 2025 11:11:37 -0500 Subject: [PATCH 3/4] Add defaults for fields of Ifinfomsg builder --- examples/getlink.rs | 4 +--- src/rtnl.rs | 4 ++-- 2 files changed, 3 insertions(+), 5 deletions(-) diff --git a/examples/getlink.rs b/examples/getlink.rs index bee8571e..b9eb5d23 100644 --- a/examples/getlink.rs +++ b/examples/getlink.rs @@ -1,7 +1,7 @@ use neli::{ consts::{ nl::NlmF, - rtnl::{Arphrd, Ifla, RtAddrFamily, Rtm}, + rtnl::{Ifla, RtAddrFamily, Rtm}, socket::NlFamily, }, nl::NlPayload, @@ -18,8 +18,6 @@ fn main() -> Result<(), Box> { rtnl.enable_strict_checking(true)?; let ifinfomsg = IfinfomsgBuilder::default() .ifi_family(RtAddrFamily::Inet) - .ifi_type(Arphrd::None) - .ifi_index(0) .build()?; let recv = rtnl.send::<_, _, Rtm, Ifinfomsg>( diff --git a/src/rtnl.rs b/src/rtnl.rs index fb3e8bee..45910371 100644 --- a/src/rtnl.rs +++ b/src/rtnl.rs @@ -34,9 +34,11 @@ pub struct Ifinfomsg { padding: u8, /// Interface type #[getset(get = "pub")] + #[builder(default = "Arphrd::from(0)")] ifi_type: Arphrd, /// Interface index #[getset(get = "pub")] + #[builder(default = "0")] ifi_index: libc::c_int, /// Interface flags #[getset(get = "pub")] @@ -482,8 +484,6 @@ mod test { NlPayload::Payload( IfinfomsgBuilder::default() .ifi_family(RtAddrFamily::Unspecified) - .ifi_type(Arphrd::None) - .ifi_index(0) .build() .unwrap(), ), From 4d394c037e7af6c9d2e76efcdc0043ed244ab8c9 Mon Sep 17 00:00:00 2001 From: John Baublitz Date: Fri, 24 Jan 2025 11:11:37 -0500 Subject: [PATCH 4/4] Update CI and githooks to run new example --- .githooks/pre-commit | 2 ++ .github/workflows/main.yml | 4 ++++ 2 files changed, 6 insertions(+) diff --git a/.githooks/pre-commit b/.githooks/pre-commit index f7eafe59..e964ca9c 100755 --- a/.githooks/pre-commit +++ b/.githooks/pre-commit @@ -13,12 +13,14 @@ cargo fmt -- --check \ && ./target/debug/examples/ctrl-list \ && ./target/debug/examples/error_packet \ && ./target/debug/examples/nl80211 \ + && ./target/debug/examples/getlink \ && cargo build --examples --features=async \ && ./target/debug/examples/getips \ && ./target/debug/examples/route-list \ && ./target/debug/examples/ctrl-list \ && ./target/debug/examples/error_packet \ && ./target/debug/examples/nl80211 \ + && ./target/debug/examples/getlink \ && cargo test \ && cargo test --all-targets --all-features \ || exit 1 diff --git a/.github/workflows/main.yml b/.github/workflows/main.yml index 9efa3d4b..aa7c8d67 100644 --- a/.github/workflows/main.yml +++ b/.github/workflows/main.yml @@ -88,6 +88,8 @@ jobs: run: ./target/debug/examples/route-list - name: Run error_packet run: ./target/debug/examples/error_packet + - name: Run getlink + run: ./target/debug/examples/getlink # nl80211 not included due to no wireless interfaces on test machines musl-checks: strategy: @@ -143,6 +145,8 @@ jobs: run: ./target/debug/examples/route-list - name: Run error_packet run: ./target/debug/examples/error_packet + - name: Run getlink + run: ./target/debug/examples/getlink # nl80211 not included due to no wireless interfaces on test machines concurrency: