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
// Copyright (C) 2020 - Will Glozer. All rights reserved.

use std::mem::size_of;
use crate::ffi::*;

pub unsafe trait Bytes {
    fn tail(&self, max: usize) -> usize {
        max
    }
}

unsafe impl Bytes for ()                {}
unsafe impl Bytes for u8                {}
unsafe impl Bytes for u16               {}
unsafe impl Bytes for u32               {}
unsafe impl Bytes for nlmsgerr          {}
unsafe impl Bytes for ndmsg             {}
unsafe impl Bytes for rtmsg             {}
unsafe impl Bytes for rtgenmsg          {}
unsafe impl Bytes for ifaddrmsg         {}
unsafe impl Bytes for ifinfomsg         {}
unsafe impl Bytes for rtnl_link_stats   {}
unsafe impl Bytes for rtnl_link_stats64 {}
unsafe impl Bytes for inet_diag_req_v2  {}
unsafe impl Bytes for inet_diag_msg     {}
unsafe impl Bytes for tcp_info          {}
unsafe impl Bytes for cn_proc_event     {}

unsafe impl Bytes for nlmsghdr {
    fn tail(&self, max: usize) -> usize {
        let x = self.nlmsg_len as usize;
        let y = size_of::<Self>();
        x.checked_sub(y).unwrap_or(max)
    }
}

unsafe impl Bytes for nlattr {
    fn tail(&self, max: usize) -> usize {
        let x = self.nla_len as usize;
        let y = size_of::<Self>();
        x.checked_sub(y).unwrap_or(max)
    }
}

unsafe impl Bytes for rtattr {
    fn tail(&self, max: usize) -> usize {
        let x = self.rta_len as usize;
        let y = size_of::<Self>();
        x.checked_sub(y).unwrap_or(max)
    }
}