netlink_packet_generic/
message.rs1use crate::buffer::GenlBuffer;
6use crate::header::GenlHeader;
7use crate::traits::*;
8use netlink_packet_core::{
9 NetlinkDeserializable, NetlinkHeader, NetlinkPayload, NetlinkSerializable,
10};
11use netlink_packet_utils::{DecodeError, Emitable, ParseableParametrized};
12use std::fmt::Debug;
13
14#[cfg(doc)]
15use netlink_packet_core::NetlinkMessage;
16
17#[derive(Clone, Debug, PartialEq, Eq)]
24pub struct GenlMessage<F> {
25 pub header: GenlHeader,
26 pub payload: F,
27 resolved_family_id: u16,
28}
29
30impl<F> GenlMessage<F>
31where
32 F: Debug,
33{
34 pub fn new(header: GenlHeader, payload: F, family_id: u16) -> Self {
36 Self { header, payload, resolved_family_id: family_id }
37 }
38
39 pub fn from_parts(header: GenlHeader, payload: F) -> Self {
41 Self { header, payload, resolved_family_id: 0 }
42 }
43
44 pub fn into_parts(self) -> (GenlHeader, F) {
46 (self.header, self.payload)
47 }
48
49 pub fn resolved_family_id(&self) -> u16 {
54 self.resolved_family_id
55 }
56
57 pub fn set_resolved_family_id(&mut self, family_id: u16) {
72 self.resolved_family_id = family_id;
73 }
74}
75
76impl<F> GenlMessage<F>
77where
78 F: GenlFamily + Debug,
79{
80 pub fn from_payload(payload: F) -> Self {
86 Self {
87 header: GenlHeader { cmd: payload.command(), version: payload.version() },
88 payload,
89 resolved_family_id: 0,
90 }
91 }
92
93 pub fn finalize(&mut self) {
102 self.header.cmd = self.payload.command();
103 self.header.version = self.payload.version();
104 }
105
106 pub fn family_id(&self) -> u16 {
114 let static_id = self.payload.family_id();
115 if static_id == 0 {
116 self.resolved_family_id
117 } else {
118 static_id
119 }
120 }
121}
122
123impl<F> Emitable for GenlMessage<F>
124where
125 F: GenlFamily + Emitable + Debug,
126{
127 fn buffer_len(&self) -> usize {
128 self.header.buffer_len() + self.payload.buffer_len()
129 }
130
131 fn emit(&self, buffer: &mut [u8]) {
132 self.header.emit(buffer);
133
134 let buffer = &mut buffer[self.header.buffer_len()..];
135 self.payload.emit(buffer);
136 }
137}
138
139impl<F> NetlinkSerializable for GenlMessage<F>
140where
141 F: GenlFamily + Emitable + Debug,
142{
143 fn message_type(&self) -> u16 {
144 self.family_id()
145 }
146
147 fn buffer_len(&self) -> usize {
148 <Self as Emitable>::buffer_len(self)
149 }
150
151 fn serialize(&self, buffer: &mut [u8]) {
152 self.emit(buffer)
153 }
154}
155
156impl<F> NetlinkDeserializable for GenlMessage<F>
157where
158 F: ParseableParametrized<[u8], GenlHeader> + Debug,
159 F::Error: Into<DecodeError>,
160{
161 type Error = DecodeError;
162 fn deserialize(header: &NetlinkHeader, payload: &[u8]) -> Result<Self, Self::Error> {
163 let buffer = GenlBuffer::new_checked(payload)?;
164 GenlMessage::parse_with_param(&buffer, header.message_type)
165 }
166}
167
168impl<F> From<GenlMessage<F>> for NetlinkPayload<GenlMessage<F>>
169where
170 F: Debug,
171{
172 fn from(message: GenlMessage<F>) -> Self {
173 NetlinkPayload::InnerMessage(message)
174 }
175}