1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
use crate::{buffer::NETLINK_HEADER_LEN, DecodeError, Emitable, NetlinkBuffer, Parseable};
#[derive(Debug, PartialEq, Eq, Clone, Copy, Hash, Default)]
pub struct NetlinkHeader {
pub length: u32,
pub message_type: u16,
pub flags: u16,
pub sequence_number: u32,
pub port_number: u32,
}
impl Emitable for NetlinkHeader {
fn buffer_len(&self) -> usize {
NETLINK_HEADER_LEN
}
fn emit(&self, buffer: &mut [u8]) {
let mut buffer = NetlinkBuffer::new(buffer);
buffer.set_message_type(self.message_type);
buffer.set_length(self.length);
buffer.set_flags(self.flags);
buffer.set_sequence_number(self.sequence_number);
buffer.set_port_number(self.port_number);
}
}
impl<'a, T: AsRef<[u8]> + ?Sized> Parseable<NetlinkBuffer<&'a T>> for NetlinkHeader {
fn parse(buf: &NetlinkBuffer<&'a T>) -> Result<NetlinkHeader, DecodeError> {
Ok(NetlinkHeader {
length: buf.length(),
message_type: buf.message_type(),
flags: buf.flags(),
sequence_number: buf.sequence_number(),
port_number: buf.port_number(),
})
}
}
#[cfg(test)]
mod tests {
use super::*;
use crate::constants::*;
#[rustfmt::skip]
static IP_LINK_SHOW_PKT: [u8; 40] = [
0x28, 0x00, 0x00, 0x00,
0x12, 0x00,
0x01, 0x03,
0x34, 0x0e, 0xf9, 0x5a,
0x00, 0x00, 0x00, 0x00,
0x11, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x08, 0x00, 0x1d, 0x00, 0x01, 0x00, 0x00, 0x00];
const RTM_GETLINK: u16 = 18;
#[test]
fn repr_parse() {
let repr =
NetlinkHeader::parse(&NetlinkBuffer::new_checked(&IP_LINK_SHOW_PKT[..]).unwrap())
.unwrap();
assert_eq!(repr.length, 40);
assert_eq!(repr.message_type, RTM_GETLINK);
assert_eq!(repr.sequence_number, 1_526_271_540);
assert_eq!(repr.port_number, 0);
assert_eq!(repr.flags, NLM_F_ROOT | NLM_F_REQUEST | NLM_F_MATCH);
}
#[test]
fn repr_emit() {
let repr = NetlinkHeader {
length: 40,
message_type: RTM_GETLINK,
sequence_number: 1_526_271_540,
flags: NLM_F_ROOT | NLM_F_REQUEST | NLM_F_MATCH,
port_number: 0,
};
assert_eq!(repr.buffer_len(), 16);
let mut buf = vec![0; 16];
repr.emit(&mut buf[..]);
assert_eq!(&buf[..], &IP_LINK_SHOW_PKT[..16]);
}
}