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
use std;
use std::error::Error;
use std::fmt::{self, Display};
use std::io;
use std::str;
use std::string;
use buffering::copy::{StreamReadBuffer, StreamWriteBuffer};
use libc;
use consts::NlType;
use nl::{NlEmpty, Nlmsghdr};
use Nl;
macro_rules! try_err_compat {
( $err_name:ident, $( $from_err_name:path ),* ) => {
$(
impl From<$from_err_name> for $err_name {
fn from(v: $from_err_name) -> Self {
$err_name::new(v.description())
}
}
)*
}
}
pub struct Nlmsgerr<T> {
pub error: libc::c_int,
pub nlmsg: Nlmsghdr<T, NlEmpty>,
}
impl<T> Nl for Nlmsgerr<T>
where
T: NlType,
{
fn serialize(&self, mem: &mut StreamWriteBuffer) -> Result<(), SerError> {
self.error.serialize(mem)?;
self.nlmsg.serialize(mem)?;
self.pad(mem)?;
Ok(())
}
fn deserialize<B>(mem: &mut StreamReadBuffer<B>) -> Result<Self, DeError>
where
B: AsRef<[u8]>,
{
let nlmsg = Nlmsgerr {
error: libc::c_int::deserialize(mem)?,
nlmsg: Nlmsghdr::<T, NlEmpty>::deserialize(mem)?,
};
nlmsg.strip(mem)?;
Ok(nlmsg)
}
fn size(&self) -> usize {
self.error.size() + self.nlmsg.size()
}
}
#[derive(Debug)]
pub enum NlError {
Msg(String),
NoAck,
BadSeq,
BadPid,
}
try_err_compat!(NlError, io::Error, SerError, DeError);
impl NlError {
pub fn new(s: &str) -> Self {
NlError::Msg(s.to_string())
}
}
impl Display for NlError {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
let msg = match *self {
NlError::Msg(ref msg) => msg,
NlError::NoAck => "No ack received",
NlError::BadSeq => "Sequence number does not match the request",
NlError::BadPid => "PID does not match the socket",
};
write!(f, "{}", msg)
}
}
impl Error for NlError {
fn description(&self) -> &str {
match *self {
NlError::Msg(ref msg) => msg.as_str(),
NlError::NoAck => "No ack received",
NlError::BadSeq => "Sequence number does not match the request",
NlError::BadPid => "PID does not match the socket",
}
}
}
#[derive(Debug)]
pub struct SerError(String);
impl SerError {
pub fn new<T: ToString>(msg: T) -> Self {
SerError(msg.to_string())
}
}
try_err_compat!(SerError, io::Error);
impl Display for SerError {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(f, "{}", self.0)
}
}
impl Error for SerError {
fn description(&self) -> &str {
self.0.as_str()
}
}
#[derive(Debug)]
pub struct DeError(String);
impl DeError {
pub fn new(s: &str) -> Self {
DeError(s.to_string())
}
}
try_err_compat!(
DeError,
io::Error,
str::Utf8Error,
string::FromUtf8Error,
std::ffi::FromBytesWithNulError
);
impl Display for DeError {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(f, "{}", self.0)
}
}
impl Error for DeError {
fn description(&self) -> &str {
self.0.as_str()
}
}