Skip to content

Commit

Permalink
Merge pull request #263 from jbaublitz/issue-261
Browse files Browse the repository at this point in the history
Improve usability for Getlink and fix unit tests
  • Loading branch information
jbaublitz authored Jan 24, 2025
2 parents 7e6f016 + 4d394c0 commit 3c07f7b
Show file tree
Hide file tree
Showing 5 changed files with 75 additions and 14 deletions.
2 changes: 2 additions & 0 deletions .githooks/pre-commit
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 4 additions & 0 deletions .github/workflows/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down Expand Up @@ -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:
Expand Down
41 changes: 41 additions & 0 deletions examples/getlink.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
use neli::{
consts::{
nl::NlmF,
rtnl::{Ifla, RtAddrFamily, Rtm},
socket::NlFamily,
},
nl::NlPayload,
router::synchronous::NlRouter,
rtnl::{Ifinfomsg, IfinfomsgBuilder},
utils::Groups,
};

fn main() -> Result<(), Box<dyn std::error::Error>> {
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)
.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::<String>(Ifla::Ifname)?,
)
}
}

Ok(())
}
41 changes: 27 additions & 14 deletions src/rtnl.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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")]
Expand Down Expand Up @@ -428,6 +430,7 @@ mod test {

use crate::{
consts::{nl::NlmF, socket::NlFamily},
err::RouterError,
nl::NlPayload,
router::synchronous::NlRouter,
test::setup,
Expand Down Expand Up @@ -474,31 +477,41 @@ 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,
NlPayload::Payload(
IfinfomsgBuilder::default()
.ifi_family(RtAddrFamily::Unspecified)
.ifi_type(Arphrd::None)
.ifi_index(0)
.build()
.unwrap(),
),
)
.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::<String>(Ifla::Ifname)
.unwrap();
// Assert length of ethernet address
if let Ok(attr) = handle.get_attr_payload_as_with_len::<Vec<u8>>(Ifla::Address) {
assert_eq!(attr.len(), 6);
}
let all_msgs = recv
.try_fold(Vec::new(), |mut v, m| {
v.push(m?);
Result::<_, RouterError<Rtm, Ifinfomsg>>::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::<String>(Ifla::Ifname)
.unwrap();
// Assert length of ethernet address
if let Ok(attr) = handle.get_attr_payload_as_with_len::<Vec<u8>>(Ifla::Address) {
assert_eq!(attr.len(), 6);
}
}
}
Expand Down
1 change: 1 addition & 0 deletions src/socket/synchronous.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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(())
Expand Down

0 comments on commit 3c07f7b

Please sign in to comment.