Skip to content

Issue 4 neli error handling #14

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Mar 30, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 6 additions & 4 deletions user-rust/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion user-rust/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,4 @@ publish = false
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[dependencies]
neli = "0.5.2"
neli = "0.5.3"
3 changes: 1 addition & 2 deletions user-rust/src/bin/echo.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,6 @@ use neli::{
nl::{NlPayload, Nlmsghdr},
socket::NlSocketHandle,
types::{Buffer, GenlBuffer},
utils::U32Bitmask,
};
use std::process;
use user_rust::{FAMILY_NAME, NlFoobarXmplAttribute, NlFoobarXmplCommand};
Expand All @@ -46,7 +45,7 @@ fn main() {
NlFamily::Generic,
// 0 is pid of kernel -> socket is connected to kernel
Some(0),
U32Bitmask::empty(),
&[],
)
.unwrap();

Expand Down
4 changes: 2 additions & 2 deletions user-rust/src/bin/echo_with_dump_flag.rs
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,6 @@ use neli::{
nl::{NlPayload, Nlmsghdr},
socket::NlSocketHandle,
types::{Buffer, GenlBuffer},
utils::U32Bitmask,
};
use std::process;
use user_rust::{FAMILY_NAME, NlFoobarXmplAttribute, NlFoobarXmplCommand};
Expand All @@ -57,7 +56,7 @@ fn main() {
NlFamily::Generic,
// 0 is pid of kernel -> socket is connected to kernel
Some(0),
U32Bitmask::empty(),
&[],
).unwrap();

let family_id;
Expand Down Expand Up @@ -98,6 +97,7 @@ fn main() {

let done_msg: Nlmsghdr<u16, Genlmsghdr<NlFoobarXmplCommand, NlFoobarXmplAttribute>> = sock.recv().expect("Should receive message").unwrap();
assert_eq!(u16::from(Nlmsg::Done), done_msg.nl_type, "Must receive NLMSG_DONE response" /* 3 is NLMSG_DONE */);
println!("Received NLMSG_DONE");
}

}
Expand Down
45 changes: 36 additions & 9 deletions user-rust/src/bin/reply_with_error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,11 +31,10 @@ use neli::{
nl::{NlmF, NlmFFlags},
socket::NlFamily,
},
genl::{Genlmsghdr},
genl::{Genlmsghdr, Nlattr},
nl::{NlPayload, Nlmsghdr},
socket::NlSocketHandle,
types::{Buffer, GenlBuffer},
utils::U32Bitmask,
};
use std::process;
use user_rust::{NlFoobarXmplAttribute, NlFoobarXmplCommand, FAMILY_NAME};
Expand All @@ -51,7 +50,7 @@ fn main() {
NlFamily::Generic,
// 0 is pid of kernel -> socket is connected to kernel
Some(0),
U32Bitmask::empty(),
&[],
)
.unwrap();

Expand All @@ -70,11 +69,19 @@ fn main() {
}
}

let gnmsghdr = Genlmsghdr::new(
NlFoobarXmplCommand::ReplyWithNlmsgErr,
1,
GenlBuffer::<NlFoobarXmplAttribute, Buffer>::new(),
// some attribute
let mut attrs: GenlBuffer<NlFoobarXmplAttribute, Buffer> = GenlBuffer::new();
attrs.push(
Nlattr::new(
None,
false,
false,
NlFoobarXmplAttribute::Msg,
"We expect this message to fail",
)
.unwrap(),
);
let gnmsghdr = Genlmsghdr::new(NlFoobarXmplCommand::ReplyWithNlmsgErr, 1, attrs);
let nlmsghdr = Nlmsghdr::new(
None,
family_id,
Expand All @@ -86,10 +93,30 @@ fn main() {

sock.send(nlmsghdr).expect("Send must work");

// TODO this fails until https://github.com/jbaublitz/neli/issues/116 gets resolved!
let res: Result<
Option<Nlmsghdr<u16, Genlmsghdr<NlFoobarXmplCommand, NlFoobarXmplAttribute>>>,
NlError,
> = sock.recv();
println!("{:#?}", res);
let received_err = res.unwrap_err();
let received_err = match received_err {
NlError::Nlmsgerr(err) => err,
_ => panic!("We expected an error here!"),
};
println!(
"Inside the kernel this error code occurred: {}",
received_err.error
);
let nlmsg_that_caused_error = received_err
.nlmsg
.get_payload_as::<Genlmsghdr<NlFoobarXmplCommand, NlFoobarXmplAttribute>>();
let nlmsg_that_caused_error = nlmsg_that_caused_error.expect("Must contain original payload");
let attr_handle = nlmsg_that_caused_error.get_attr_handle();
println!(
"ECHO-attribute was: {}",
attr_handle
.get_attr_payload_as::<String>(NlFoobarXmplAttribute::Msg)
.expect("This attribute was sent and must be in the response")
);

println!("Everything okay; successfully received error")
}