netlink-bindings 0.2.3

Type-safe Rust bindings for Netlink generated from YAML specifications
Documentation
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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
use std::fmt;

pub use crate::primitives::*;
pub use std::{ffi::CStr, fmt::Debug, iter::Iterator};

pub fn dump_hex(buf: &[u8]) {
    let mut len = 0;
    for chunk in buf.chunks(16) {
        print!("{len:04x?}: ");
        print!("{chunk:02x?} ");
        for b in chunk {
            if b.is_ascii() && !b.is_ascii_control() {
                print!("{}", char::from_u32(*b as u32).unwrap());
            } else {
                print!(".");
            }
        }
        println!();
        len += chunk.len();
    }
}

pub fn dump_assert_eq(left: &[u8], right: &[u8]) {
    if left.len() != right.len() {
        dump_hex(left);
        dump_hex(right);
        panic!("Length mismatched");
    }
    if let Some(pos) = left.iter().zip(right.iter()).position(|(l, r)| *l != *r) {
        println!();
        println!("Left:");
        dump_hex(left);
        println!();
        println!("Right:");
        dump_hex(right);
        panic!("Differ at byte {pos} (0x{pos:x?})");
    }
}

pub struct FormatHex<'a>(pub &'a [u8]);

impl Debug for FormatHex<'_> {
    fn fmt(&self, fmt: &mut fmt::Formatter<'_>) -> fmt::Result {
        write!(fmt, "\"")?;
        for i in self.0 {
            write!(fmt, "{i:02x}")?
        }
        write!(fmt, "\"")?;
        Ok(())
    }
}

pub struct FormatEnum<T: Debug>(pub u64, pub fn(u64) -> Option<T>);

impl<T: Debug> Debug for FormatEnum<T> {
    fn fmt(&self, fmt: &mut fmt::Formatter<'_>) -> fmt::Result {
        write!(fmt, "{} ", self.0)?;

        if let Some(var) = (self.1)(self.0) {
            write!(fmt, "[{var:?}]")?;
        } else {
            write!(fmt, "(unknown variant)")?;
        }

        Ok(())
    }
}

pub struct FormatFlags<T: Debug>(pub u64, pub fn(u64) -> Option<T>);

impl<T: Debug> Debug for FormatFlags<T> {
    fn fmt(&self, fmt: &mut fmt::Formatter<'_>) -> fmt::Result {
        write!(fmt, "{} ", self.0)?;

        if self.0 == 0 {
            write!(fmt, "(empty)")?;
            return Ok(());
        }

        let mut seen_variant = false;
        for i in 0..u64::BITS {
            let bit = self.0 & (1 << i);
            if bit == 0 {
                continue;
            }

            if !seen_variant {
                seen_variant = true;
                write!(fmt, "[")?;
            } else {
                write!(fmt, ",")?;
            }

            if let Some(var) = (self.1)(bit) {
                write!(fmt, "{var:?}")?;
            } else {
                write!(fmt, "(unknown bit {i})")?;
            }
        }

        if seen_variant {
            write!(fmt, "]")?;
        }

        Ok(())
    }
}

pub struct DisplayAsDebug<T>(T);

impl<T: fmt::Display> fmt::Debug for DisplayAsDebug<T> {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        write!(f, "{}", self.0)
    }
}

pub struct FlattenErrorContext<T: fmt::Debug>(pub Result<T, ErrorContext>);

impl<T: Debug> fmt::Debug for FlattenErrorContext<T> {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        match &self.0 {
            Ok(ok) => ok.fmt(f),
            Err(err) => {
                f.write_str("Err(")?;
                err.fmt(f)?;
                f.write_str(")")
            }
        }
    }
}

pub struct MapFormatArray<I, T, M, D>(pub T, pub M)
where
    T: Clone + Iterator<Item = Result<I, ErrorContext>>,
    M: Clone + FnMut(I) -> D,
    D: fmt::Debug;

impl<I, T, M, D> fmt::Debug for MapFormatArray<I, T, M, D>
where
    T: Clone + Iterator<Item = Result<I, ErrorContext>>,
    M: Clone + FnMut(I) -> D,
    D: fmt::Debug,
{
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        let mut f = f.debug_list();
        for item in self.0.clone() {
            f.entry(&FlattenErrorContext(item.map(self.1.clone())));
        }
        f.finish()
    }
}

pub const NLA_F_NESTED: u16 = 1 << 15;
pub const NLA_F_NET_BYTEORDER: u16 = 1 << 14;

pub const fn nla_type(r#type: u16) -> u16 {
    r#type & (!(NLA_F_NESTED | NLA_F_NET_BYTEORDER))
}

pub const NLA_ALIGNTO: usize = 4;

pub const fn nla_align_up(len: usize) -> usize {
    ((len) + NLA_ALIGNTO - 1) & !(NLA_ALIGNTO - 1)
}

pub fn align(buf: &mut Vec<u8>) {
    let len = buf.len();
    buf.extend(std::iter::repeat_n(0u8, nla_align_up(len) - len));
}

/// Returns header offset
pub fn push_nested_header(buf: &mut Vec<u8>, r#type: u16) -> usize {
    push_header_type(buf, r#type, 0, true)
}

/// Returns header offset
pub fn push_header(buf: &mut Vec<u8>, r#type: u16, len: u16) -> usize {
    push_header_type(buf, r#type, len, false)
}

/// Returns header offset
/// The kernel doesn't really check byteorder bit nor set it correctly
fn push_header_type(buf: &mut Vec<u8>, mut r#type: u16, len: u16, is_nested: bool) -> usize {
    align(buf);

    let header_offset = buf.len();

    if is_nested {
        r#type |= NLA_F_NESTED;
    }

    // TODO: alignment for 8 byte types?
    buf.extend((len + 4).to_ne_bytes());
    buf.extend(r#type.to_ne_bytes());

    align(buf);

    header_offset
}

pub fn finalize_nested_header(buf: &mut Vec<u8>, offset: usize) {
    align(buf);

    let len = (buf.len() - offset) as u16;
    buf[offset..(offset + 2)].copy_from_slice(&len.to_ne_bytes());
}

#[derive(Debug, Clone, Copy)]
pub struct Header {
    pub r#type: u16,
    pub is_nested: bool,
}

pub fn chop_header<'a>(buf: &'a [u8], pos: &mut usize) -> Option<(Header, &'a [u8])> {
    let buf = &buf[*pos..];

    if buf.len() < 4 {
        return None;
    }

    let len = parse_u16(&buf[0..2]).unwrap();
    let r#type = parse_u16(&buf[2..4]).unwrap();

    let next_len = nla_align_up(len as usize);

    if len < 4 || buf.len() < len as usize {
        return None;
    }

    let next = &buf[4..len as usize];
    *pos += next_len.min(buf.len());

    Some((
        Header {
            r#type: nla_type(r#type),
            is_nested: r#type & NLA_F_NESTED != 0,
        },
        next,
    ))
}

pub trait Rec {
    fn as_rec_mut(&mut self) -> &mut Vec<u8>;
}

impl Rec for &mut Vec<u8> {
    fn as_rec_mut(&mut self) -> &mut Vec<u8> {
        self
    }
}

#[derive(Debug, Clone, PartialEq, Eq)]
pub enum ErrorReason {
    /// Only used in `.get_<attr>()` methods
    AttrMissing,
    /// Value of the attribute can't be parsed
    ParsingError,
    /// Found attribute of type not mentioned in the specification
    UnknownAttr,
}

#[derive(Clone, PartialEq, Eq)]
pub struct ErrorContext {
    pub attrs: &'static str,
    pub attr: Option<&'static str>,
    pub offset: usize,
    pub reason: ErrorReason,
}

impl std::error::Error for ErrorContext {}

impl From<ErrorContext> for std::io::Error {
    fn from(value: ErrorContext) -> Self {
        Self::other(value)
    }
}

impl fmt::Debug for ErrorContext {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        f.debug_struct("ErrorContext")
            .field("message", &DisplayAsDebug(&self))
            .field("reason", &self.reason)
            .field("attrs", &self.attrs)
            .field("attr", &self.attr)
            .field("offset", &self.offset)
            .finish()
    }
}

impl fmt::Display for ErrorContext {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        let attrs = self.attrs;
        if matches!(self.reason, ErrorReason::AttrMissing) {
            let attr = self.attr.unwrap();
            write!(f, "Missing attribute {attr:?} in {attrs:?}")?;
            return Ok(());
        } else {
            write!(f, "Error parsing ")?;
            if let Some(attr) = self.attr {
                write!(f, "attribute {attr:?} of {attrs:?}")?;
            } else {
                write!(f, "header of {attrs:?}")?;
                if matches!(self.reason, ErrorReason::UnknownAttr) {
                    write!(f, " (unknown attribute)")?;
                }
            }
        }
        write!(f, " at offset {}", self.offset)?;
        Ok(())
    }
}

impl ErrorContext {
    #[cold]
    pub(crate) fn new(
        attrs: &'static str,
        attr: Option<&'static str>,
        orig_loc: usize,
        loc: usize,
    ) -> ErrorContext {
        let ctx = ErrorContext {
            attrs,
            attr,
            offset: Self::calc_offset(orig_loc, loc),
            reason: if attr.is_some() {
                ErrorReason::ParsingError
            } else {
                ErrorReason::UnknownAttr
            },
        };

        if cfg!(test) {
            panic!("{ctx}")
        } else {
            ctx
        }
    }

    #[cold]
    pub(crate) fn new_missing(
        attrs: &'static str,
        attr: &'static str,
        orig_loc: usize,
        loc: usize,
    ) -> ErrorContext {
        let ctx = ErrorContext {
            attrs,
            attr: Some(attr),
            offset: Self::calc_offset(orig_loc, loc),
            reason: ErrorReason::AttrMissing,
        };

        if cfg!(test) {
            panic!("{ctx}")
        } else {
            ctx
        }
    }

    pub(crate) fn calc_offset(orig_loc: usize, loc: usize) -> usize {
        if orig_loc <= loc && loc - orig_loc <= u16::MAX as usize {
            loc - orig_loc
        } else {
            0
        }
    }
}

#[derive(Clone)]
pub struct MultiAttrIterable<I, T, V>
where
    I: Iterator<Item = Result<T, ErrorContext>>,
{
    pub(crate) inner: I,
    pub(crate) f: fn(T) -> Option<V>,
}

impl<I, T, V> MultiAttrIterable<I, T, V>
where
    I: Iterator<Item = Result<T, ErrorContext>>,
{
    pub fn new(inner: I, f: fn(T) -> Option<V>) -> Self {
        Self { inner, f }
    }
}

impl<I, T, V> Iterator for MultiAttrIterable<I, T, V>
where
    I: Iterator<Item = Result<T, ErrorContext>>,
{
    type Item = V;
    fn next(&mut self) -> Option<Self::Item> {
        match self.inner.next() {
            Some(Ok(val)) => (self.f)(val),
            _ => None,
        }
    }
}

#[derive(Clone)]
pub struct ArrayIterable<I, T>
where
    I: Iterator<Item = Result<T, ErrorContext>>,
{
    pub(crate) inner: I,
}

impl<I, T> ArrayIterable<I, T>
where
    I: Iterator<Item = Result<T, ErrorContext>>,
{
    pub fn new(inner: I) -> Self {
        Self { inner }
    }
}

impl<I, T> Iterator for ArrayIterable<I, T>
where
    I: Iterator<Item = Result<T, ErrorContext>>,
{
    type Item = T;
    fn next(&mut self) -> Option<Self::Item> {
        match self.inner.next() {
            Some(Ok(val)) => Some(val),
            _ => None,
        }
    }
}

#[derive(Debug)]
pub enum RequestBuf<'a> {
    Ref(&'a mut Vec<u8>),
    Own(Vec<u8>),
}

impl RequestBuf<'_> {
    pub fn buf(&self) -> &Vec<u8> {
        match self {
            RequestBuf::Ref(buf) => buf,
            RequestBuf::Own(buf) => buf,
        }
    }

    pub fn buf_mut(&mut self) -> &mut Vec<u8> {
        match self {
            RequestBuf::Ref(buf) => buf,
            RequestBuf::Own(buf) => buf,
        }
    }
}

impl Rec for RequestBuf<'_> {
    fn as_rec_mut(&mut self) -> &mut Vec<u8> {
        self.buf_mut()
    }
}