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    /// Additional `flags` specified in the message header
20    fn flags(&self) -> u16;
21
22    /// Encoded payload of the message (without message header)
23    fn payload(&self) -> &[u8];
24
25    type ReplyType<'buf>;
26    fn decode_reply(buf: &[u8]) -> Self::ReplyType<'_>;
27
28    /// Lookup an attribute and it's parents in the request payload by offset
29    fn lookup(
30        buf: &[u8],
31        offset: usize,
32        missing_type: Option<u16>,
33    ) -> (Vec<(&'static str, usize)>, Option<&'static str>) {
34        let _ = buf;
35        let _ = offset;
36        let _ = missing_type;
37        (Vec::new(), None)
38    }
39}
40
41/// Function signature of [`NetlinkRequest::lookup`]
42pub type LookupFn =
43    fn(&[u8], usize, Option<u16>) -> (Vec<(&'static str, usize)>, Option<&'static str>);
44
45/// A chain of requests encoded into the single buffer (experimental)
46pub trait NetlinkChained {
47    fn protonum(&self) -> u16;
48
49    /// Encoded payload of the messages (including message headers)
50    fn payload(&self) -> &[u8];
51
52    /// Number of messages in the chain
53    fn chain_len(&self) -> usize;
54
55    fn get_index(&self, seq: u32) -> Option<usize>;
56
57    fn name(&self, index: usize) -> &'static str;
58
59    fn lookup(&self, index: usize) -> LookupFn {
60        let _ = index;
61        |_, _, _| Default::default()
62    }
63}