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
#![allow(clippy::cast_lossless)]
use crate::pcapng::blocks::common::opts_from_slice;
use crate::errors::PcapError;
use crate::DataLink;
use byteorder::{ByteOrder, ReadBytesExt};
use crate::pcapng::{CustomUtf8Option, CustomBinaryOption, UnknownOption};
use std::borrow::Cow;
use derive_into_owned::IntoOwned;
#[derive(Clone, Debug, IntoOwned)]
pub struct InterfaceDescriptionBlock<'a> {
pub linktype: DataLink,
pub reserved: u16,
pub snaplen: u32,
pub options: Vec<InterfaceDescriptionOption<'a>>
}
impl<'a> InterfaceDescriptionBlock<'a> {
pub fn from_slice<B:ByteOrder>(mut slice: &'a [u8]) -> Result<(&'a [u8], Self), PcapError> {
if slice.len() < 8 {
return Err(PcapError::InvalidField("InterfaceDescriptionBlock: block length < 8"));
}
let linktype = (slice.read_u16::<B>()? as u32).into();
let reserved = slice.read_u16::<B>()?;
let snaplen = slice.read_u32::<B>()?;
let (slice, options) = InterfaceDescriptionOption::from_slice::<B>(slice)?;
let block = InterfaceDescriptionBlock {
linktype,
reserved,
snaplen,
options
};
Ok((slice, block))
}
}
#[derive(Clone, Debug, IntoOwned)]
pub enum InterfaceDescriptionOption<'a> {
Comment(Cow<'a, str>),
IfName(Cow<'a, str>),
IfDescription(Cow<'a, str>),
IfIpv4Addr(Cow<'a, [u8]>),
IfIpv6Addr(Cow<'a, [u8]>),
IfMacAddr(Cow<'a, [u8]>),
IfEuIAddr(u64),
IfSpeed(u64),
IfTsResol(u8),
IfTzone(u32),
IfFilter(Cow<'a, [u8]>),
IfOs(Cow<'a, str>),
IfFcsLen(u8),
IfTsOffset(u64),
IfHardware(Cow<'a, str>),
CustomBinary(CustomBinaryOption<'a>),
CustomUtf8(CustomUtf8Option<'a>),
Unknown(UnknownOption<'a>)
}
impl<'a> InterfaceDescriptionOption<'a> {
fn from_slice<B:ByteOrder>(slice: &'a[u8]) -> Result<(&'a[u8], Vec<Self>), PcapError> {
opts_from_slice::<B, _, _>(slice, |mut slice, code, length| {
let opt = match code {
1 => InterfaceDescriptionOption::Comment(Cow::Borrowed(std::str::from_utf8(slice)?)),
2 => InterfaceDescriptionOption::IfName(Cow::Borrowed(std::str::from_utf8(slice)?)),
3 => InterfaceDescriptionOption::IfDescription(Cow::Borrowed(std::str::from_utf8(slice)?)),
4 => {
if slice.len() != 8 {
return Err(PcapError::InvalidField("InterfaceDescriptionOption: IfIpv4Addr length != 8"))
}
InterfaceDescriptionOption::IfIpv4Addr(Cow::Borrowed(slice))
},
5 => {
if slice.len() != 17 {
return Err(PcapError::InvalidField("InterfaceDescriptionOption: IfIpv6Addr length != 17"))
}
InterfaceDescriptionOption::IfIpv6Addr(Cow::Borrowed(slice))
},
6 => {
if slice.len() != 6 {
return Err(PcapError::InvalidField("InterfaceDescriptionOption: IfMacAddr length != 6"))
}
InterfaceDescriptionOption::IfMacAddr(Cow::Borrowed(slice))
},
7 => {
if slice.len() != 8 {
return Err(PcapError::InvalidField("InterfaceDescriptionOption: IfEuIAddr length != 8"))
}
InterfaceDescriptionOption::IfEuIAddr(slice.read_u64::<B>()?)
},
8 => {
if slice.len() != 8 {
return Err(PcapError::InvalidField("InterfaceDescriptionOption: IfSpeed length != 8"))
}
InterfaceDescriptionOption::IfSpeed(slice.read_u64::<B>()?)
},
9 => {
if slice.len() != 1 {
return Err(PcapError::InvalidField("InterfaceDescriptionOption: IfTsResol length != 1"))
}
InterfaceDescriptionOption::IfTsResol(slice.read_u8()?)
},
10 => {
if slice.len() != 1 {
return Err(PcapError::InvalidField("InterfaceDescriptionOption: IfTzone length != 1"))
}
InterfaceDescriptionOption::IfTzone(slice.read_u32::<B>()?)
},
11 => {
if slice.is_empty() {
return Err(PcapError::InvalidField("InterfaceDescriptionOption: IfFilter is empty"))
}
InterfaceDescriptionOption::IfFilter(Cow::Borrowed(slice))
},
12 => InterfaceDescriptionOption::IfOs(Cow::Borrowed(std::str::from_utf8(slice)?)),
13 => {
if slice.len() != 1 {
return Err(PcapError::InvalidField("InterfaceDescriptionOption: IfFcsLen length != 1"))
}
InterfaceDescriptionOption::IfFcsLen(slice.read_u8()?)
},
14 => {
if slice.len() != 8 {
return Err(PcapError::InvalidField("InterfaceDescriptionOption: IfTsOffset length != 8"))
}
InterfaceDescriptionOption::IfTsOffset(slice.read_u64::<B>()?)
},
15 => InterfaceDescriptionOption::IfHardware(Cow::Borrowed(std::str::from_utf8(slice)?)),
2988 | 19372 => InterfaceDescriptionOption::CustomUtf8(CustomUtf8Option::from_slice::<B>(code, slice)?),
2989 | 19373 => InterfaceDescriptionOption::CustomBinary(CustomBinaryOption::from_slice::<B>(code, slice)?),
_ => InterfaceDescriptionOption::Unknown(UnknownOption::new(code, length, slice))
};
Ok(opt)
})
}
}