Skip to content

Commit b2f7247

Browse files
committed
nla: Copy from utils
Signed-off-by: Enrique Llorente <[email protected]>
1 parent 5f65820 commit b2f7247

File tree

8 files changed

+472
-51
lines changed

8 files changed

+472
-51
lines changed

src/buffer.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
use byteorder::{ByteOrder, NativeEndian};
44

5-
use crate::{Field, Rest, DecodeError, ErrorContext};
5+
use crate::{DecodeError, ErrorContext, Field, Rest};
66

77
const LENGTH: Field = 0..4;
88
const MESSAGE_TYPE: Field = 4..6;

src/done.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ use std::mem::size_of;
44

55
use byteorder::{ByteOrder, NativeEndian};
66

7-
use crate::{Emitable, Field, Parseable, Rest, DecodeError, ErrorContext};
7+
use crate::{DecodeError, Emitable, ErrorContext, Field, Parseable, Rest};
88

99
const CODE: Field = 0..4;
1010
const EXTENDED_ACK: Rest = 4..;

src/error.rs

Lines changed: 29 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@ const CODE: Field = 0..4;
1010
const PAYLOAD: Rest = 4..;
1111
const ERROR_HEADER_LEN: usize = PAYLOAD.start;
1212

13-
1413
pub trait ErrorContext {
1514
fn context(self, msg: &str) -> Self;
1615
}
@@ -40,24 +39,21 @@ where
4039
}
4140
}
4241

43-
44-
impl From<&str> for DecodeError{
42+
impl From<&str> for DecodeError {
4543
fn from(msg: &str) -> Self {
4644
Self {
4745
msg: msg.to_string(),
4846
}
4947
}
5048
}
5149

52-
impl From<String> for DecodeError{
50+
impl From<String> for DecodeError {
5351
fn from(msg: String) -> Self {
54-
Self {
55-
msg: msg,
56-
}
52+
Self { msg: msg }
5753
}
5854
}
5955

60-
impl From<std::string::FromUtf8Error> for DecodeError{
56+
impl From<std::string::FromUtf8Error> for DecodeError {
6157
fn from(err: std::string::FromUtf8Error) -> Self {
6258
Self {
6359
msg: format!("Invalid UTF-8 sequence: {}", err),
@@ -85,12 +81,30 @@ impl DecodeError {
8581
msg: format!("Invalid IP address. Expected 4 or 16 bytes, received {received} bytes"),
8682
}
8783
}
88-
84+
8985
pub fn invalid_number(expected: usize, received: usize) -> Self {
9086
Self{
9187
msg: format!("Invalid number. Expected {expected} bytes, received {received} bytes"),
9288
}
9389
}
90+
91+
pub fn nla_buffer_too_small(buffer_len: usize, nla_len: usize) -> Self {
92+
Self{
93+
msg: format!("buffer has length {buffer_len}, but an NLA header is {nla_len} bytes"),
94+
}
95+
}
96+
97+
pub fn nla_length_mismatch(buffer_len: usize, nla_len: usize) -> Self {
98+
Self{
99+
msg: format!("buffer has length: {buffer_len}, but the NLA is {nla_len} bytes"),
100+
}
101+
}
102+
103+
pub fn nla_invalid_length(buffer_len: usize, nla_len: usize) -> Self {
104+
Self{
105+
msg: format!("NLA has invalid length: {nla_len} (should be at least {buffer_len} bytes)"),
106+
}
107+
}
94108
}
95109

96110
#[derive(Debug, PartialEq, Eq, Clone)]
@@ -118,10 +132,12 @@ impl<T: AsRef<[u8]>> ErrorBuffer<T> {
118132
fn check_buffer_length(&self) -> Result<(), DecodeError> {
119133
let len = self.buffer.as_ref().len();
120134
if len < ERROR_HEADER_LEN {
121-
Err(DecodeError{msg: format!(
122-
"invalid ErrorBuffer: length is {len} but ErrorBuffer are \
123-
at least {ERROR_HEADER_LEN} bytes")
124-
})
135+
Err(DecodeError {
136+
msg: format!(
137+
"invalid ErrorBuffer: length is {len} but ErrorBuffer are \
138+
at least {ERROR_HEADER_LEN} bytes"
139+
),
140+
})
125141
} else {
126142
Ok(())
127143
}

src/header.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
// SPDX-License-Identifier: MIT
22

3-
4-
use crate::{buffer::NETLINK_HEADER_LEN, Emitable, NetlinkBuffer, Parseable, DecodeError};
3+
use crate::{
4+
buffer::NETLINK_HEADER_LEN, DecodeError, Emitable, NetlinkBuffer, Parseable,
5+
};
56

67
/// A Netlink header representation. A netlink header has the following
78
/// structure:

src/lib.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -271,5 +271,8 @@ pub use self::constants::*;
271271
pub mod parsers;
272272
pub use self::parsers::*;
273273

274+
pub mod nla;
275+
pub use self::nla::*;
276+
274277
#[macro_use]
275278
mod macros;

src/message.rs

Lines changed: 30 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,9 @@ use std::fmt::Debug;
44

55
use crate::{
66
payload::{NLMSG_DONE, NLMSG_ERROR, NLMSG_NOOP, NLMSG_OVERRUN},
7-
DoneBuffer, DoneMessage, Emitable, ErrorBuffer, ErrorMessage,
8-
NetlinkBuffer, NetlinkDeserializable, NetlinkHeader, NetlinkPayload,
9-
NetlinkSerializable, Parseable, DecodeError, ErrorContext,
7+
DecodeError, DoneBuffer, DoneMessage, Emitable, ErrorBuffer, ErrorContext,
8+
ErrorMessage, NetlinkBuffer, NetlinkDeserializable, NetlinkHeader,
9+
NetlinkPayload, NetlinkSerializable, Parseable,
1010
};
1111

1212
/// Represent a netlink message.
@@ -37,7 +37,8 @@ where
3737
{
3838
/// Parse the given buffer as a netlink message
3939
pub fn deserialize(buffer: &[u8]) -> Result<Self, DecodeError> {
40-
let netlink_buffer = NetlinkBuffer::new_checked(&buffer).context("TODO")?;
40+
let netlink_buffer =
41+
NetlinkBuffer::new_checked(&buffer).context("TODO")?;
4142
<Self as Parseable<NetlinkBuffer<&&[u8]>>>::parse(&netlink_buffer)
4243
}
4344
}
@@ -91,31 +92,34 @@ where
9192
use self::NetlinkPayload::*;
9293

9394
let header =
94-
<NetlinkHeader as Parseable<NetlinkBuffer<&B>>>::parse(buf).context("TODO")?;
95+
<NetlinkHeader as Parseable<NetlinkBuffer<&B>>>::parse(buf)
96+
.context("TODO")?;
9597

9698
let bytes = buf.payload();
97-
let payload = match header.message_type {
98-
NLMSG_ERROR => {
99-
let msg = ErrorBuffer::new_checked(&bytes)
100-
.and_then(|buf| ErrorMessage::parse(&buf)).context("TODO")?;
101-
Error(msg)
102-
}
103-
NLMSG_NOOP => Noop,
104-
NLMSG_DONE => {
105-
let msg = DoneBuffer::new_checked(&bytes)
106-
.and_then(|buf| DoneMessage::parse(&buf)).context("TODO")?;
107-
Done(msg)
108-
}
109-
NLMSG_OVERRUN => Overrun(bytes.to_vec()),
110-
message_type => match I::deserialize(&header, bytes) {
111-
Err(e) => {
112-
return Err(
113-
format!("Failed to parse message with type {message_type}: {e}").into()
114-
)
99+
let payload =
100+
match header.message_type {
101+
NLMSG_ERROR => {
102+
let msg = ErrorBuffer::new_checked(&bytes)
103+
.and_then(|buf| ErrorMessage::parse(&buf))
104+
.context("TODO")?;
105+
Error(msg)
115106
}
116-
Ok(inner_msg) => InnerMessage(inner_msg),
117-
},
118-
};
107+
NLMSG_NOOP => Noop,
108+
NLMSG_DONE => {
109+
let msg = DoneBuffer::new_checked(&bytes)
110+
.and_then(|buf| DoneMessage::parse(&buf))
111+
.context("TODO")?;
112+
Done(msg)
113+
}
114+
NLMSG_OVERRUN => Overrun(bytes.to_vec()),
115+
message_type => match I::deserialize(&header, bytes) {
116+
Err(e) => return Err(format!(
117+
"Failed to parse message with type {message_type}: {e}"
118+
)
119+
.into()),
120+
Ok(inner_msg) => InnerMessage(inner_msg),
121+
},
122+
};
119123
Ok(NetlinkMessage { header, payload })
120124
}
121125
}

0 commit comments

Comments
 (0)