netlink_bindings/traits.rs
1#[derive(Debug, Clone, Copy, PartialEq, Eq)]
2pub enum Protocol {
3 /// Netlink-raw protocol
4 Raw {
5 /// Value supplied to socket(2)
6 protonum: u16,
7 /// Value of `type` field in the message header
8 request_type: u16,
9 },
10 /// Generic netlink protocol
11 Generic(&'static [u8]),
12}
13
14/// A trait describing how to handle a particular request.
15/// It designed to be used by a netlink socket implementation.
16pub trait NetlinkRequest {
17 /// Netlink protocol to use
18 fn protocol(&self) -> Protocol;
19
20 /// Additional `flags` specified in the message header
21 fn flags(&self) -> u16;
22
23 /// Encoded payload of the message (without message header)
24 fn payload(&self) -> &[u8];
25
26 // type RequestType<'buf>;
27 // fn decode_request(buf: &[u8]) -> Self::RequestType<'_>;
28
29 type ReplyType<'buf>;
30 fn decode_reply(buf: &[u8]) -> Self::ReplyType<'_>;
31
32 /// Lookup an attribute and it's parents in the request payload by offset
33 fn lookup(
34 buf: &[u8],
35 offset: usize,
36 missing_type: Option<u16>,
37 ) -> (Vec<(&'static str, usize)>, Option<&'static str>) {
38 let _ = buf;
39 let _ = offset;
40 let _ = missing_type;
41 (Vec::new(), None)
42 }
43}
44
45/// Function signature of [`NetlinkRequest::lookup`]
46pub type LookupFn =
47 fn(&[u8], usize, Option<u16>) -> (Vec<(&'static str, usize)>, Option<&'static str>);
48
49/// A chain of requests encoded into the single buffer (experimental)
50pub trait NetlinkChained {
51 fn protonum(&self) -> u16;
52
53 /// Encoded payload of the messages (including message headers)
54 fn payload(&self) -> &[u8];
55
56 /// Number of messages in the chain
57 fn chain_len(&self) -> usize;
58
59 fn get_index(&self, seq: u32) -> Option<usize>;
60
61 fn name(&self, index: usize) -> &'static str;
62
63 fn lookup(&self, index: usize) -> LookupFn {
64 let _ = index;
65 |_, _, _| Default::default()
66 }
67
68 /// Packet supports ack on success with NLM_F_ACK (assumed true by default).
69 /// To date, this's only used to workaround a bug in nftables prior to linux 6.10.
70 ///
71 /// Caller sequentially peeks indexes 0..chain_len() until it encounters None.
72 #[doc(hidden)]
73 fn supports_ack(&self, index: usize) -> Option<bool> {
74 let _ = index;
75 None
76 }
77}