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
// This file is part of linux-support. It is subject to the license terms in the COPYRIGHT file found in the top-level directory of this distribution and at https://raw.githubusercontent.com/lemonrock/linux-support/master/COPYRIGHT. No part of linux-support, including this file, may be copied, modified, propagated, or distributed except according to the terms contained in the COPYRIGHT file.
// Copyright © 2020 The developers of linux-support. See the COPYRIGHT file in the top-level directory of this distribution and at https://raw.githubusercontent.com/lemonrock/linux-support/master/COPYRIGHT.
/// See also <https://linux-hacks.blogspot.com/2009/01/sample-code-to-learn-netlink.html>.
#[repr(C)]
pub(crate) struct nlmsghdr
{
pub(super) nlmsg_len: u32,
pub(super) nlmsg_type: NetlinkMessageType,
pub(super) nlmsg_flags: NetlinkMessageFlags,
pub(super) nlmsg_seq: SequenceNumber,
pub(super) nlmsg_pid: PortIdentifier,
}
impl nlmsghdr
{
#[inline(always)]
pub(super) fn error(&self) -> &nlmsgerr
{
unsafe { & * (self.NLMSG_DATA() as *const nlmsgerr) }
}
#[inline(always)]
pub(super) fn data(&self) -> &[u8]
{
unsafe { from_raw_parts(self.NLMSG_DATA(), self.NLMSG_PAYLOAD()) }
}
#[inline(always)]
pub(crate) fn NLMSG_OK(remaining_length: usize, might_be_invalid_pointer: *const Self) -> bool
{
if remaining_length < Self::NLMSG_HDRLEN
{
return false
}
let our_length = unsafe { & * might_be_invalid_pointer }.length();
our_length >= Self::NLMSG_HDRLEN && our_length <= remaining_length
}
#[inline(always)]
pub(crate) fn NLMSG_NEXT(&self, remaining_length: &mut usize) -> *const Self
{
let length = Self::NLMSG_ALIGN(self.length());
*remaining_length = (*remaining_length) - length;
unsafe { (self as *const Self as *const u8).add(length) as *const Self }
}
const NLMSG_ALIGNTO: usize = 4;
const NLMSG_HDRLEN: usize = size_of::<Self>();
#[inline(always)]
pub const fn NLMSG_ALIGN(length: usize) -> usize
{
(length + Self::NLMSG_ALIGNTO - 1) & !(Self::NLMSG_ALIGNTO - 1)
}
#[inline(always)]
pub(crate) fn NLMSG_LENGTH(length: usize) -> usize
{
length + Self::NLMSG_HDRLEN
}
#[inline(always)]
fn NLMSG_SPACE(length: usize) -> usize
{
Self::NLMSG_ALIGN(Self::NLMSG_LENGTH(length))
}
#[inline(always)]
fn NLMSG_DATA(&self) -> *const u8
{
unsafe { (self as *const Self as *const u8).add(Self::NLMSG_LENGTH(0)) }
}
#[allow(dead_code)]
#[inline(always)]
fn NLMSG_DATALEN(&self) -> usize
{
self.length() - Self::NLMSG_HDRLEN
}
#[allow(dead_code)]
#[inline(always)]
fn NLMSG_DATAEND(&self) -> *const u8
{
unsafe { (self as *const Self as *const u8).add(self.length()) }
}
#[inline(always)]
fn NLMSG_PAYLOAD(&self) -> usize
{
self.length() - Self::NLMSG_SPACE(0)
}
#[allow(dead_code)]
#[inline(always)]
pub(crate) fn NLMSG_RTA<NAT: NetlinkAttributeType>(&self, length: usize) -> *const rtattr<NAT>
{
unsafe { (self as *const Self as *const u8).add(Self::NLMSG_HDRLEN).add(Self::NLMSG_ALIGN(length)) as *const rtattr<NAT> }
}
#[inline(always)]
pub(crate) fn length(&self) -> usize
{
self.nlmsg_len as usize
}
}