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
use std::net::{Ipv4Addr, Ipv6Addr};
use nom::error::{ErrorKind, ParseError};
use nom::{Err, IResult};
use crate::endianness::{PcapBE, PcapEndianness, PcapLE};
use crate::{opt_parse_options, Linktype, PcapError, PcapNGOption, IDB_MAGIC};
use super::*;
/// An Interface Description Block (IDB) is the container for information
/// describing an interface on which packet data is captured.
#[derive(Debug)]
pub struct InterfaceDescriptionBlock<'a> {
pub block_type: u32,
pub block_len1: u32,
pub linktype: Linktype,
pub reserved: u16,
pub snaplen: u32,
pub options: Vec<PcapNGOption<'a>>,
pub block_len2: u32,
pub if_tsresol: u8,
pub if_tsoffset: i64,
}
impl InterfaceDescriptionBlock<'_> {
/// Decode the interface time resolution, in units per second
///
/// Return the resolution, or `None` if the resolution is invalid (for ex. greater than `2^64`)
#[inline]
pub fn ts_resolution(&self) -> Option<u64> {
build_ts_resolution(self.if_tsresol)
}
/// Return the interface timestamp offset
#[inline]
pub fn ts_offset(&self) -> i64 {
self.if_tsoffset
}
/// Return the `if_name` option value, if present
///
/// If the option is present multiple times, the first value is returned.
///
/// Returns `None` if option is not present, `Some(Ok(value))` if the value is present and valid,
/// or `Some(Err(_))` if value is present but invalid
pub fn if_name(&self) -> Option<Result<&str, PcapNGOptionError>> {
options_get_as_str(&self.options, OptionCode::IfName)
}
/// Return the `if_description` option value, if present
///
/// If the option is present multiple times, the first value is returned.
///
/// Returns `None` if option is not present, `Some(Ok(value))` if the value is present and valid,
/// or `Some(Err(_))` if value is present but invalid
pub fn if_description(&self) -> Option<Result<&str, PcapNGOptionError>> {
options_get_as_str(&self.options, OptionCode::IfDescription)
}
/// Return the `if_os` option value, if present
///
/// If the option is present multiple times, the first value is returned.
///
/// Returns `None` if option is not present, `Some(Ok(value))` if the value is present and valid,
/// or `Some(Err(_))` if value is present but invalid
pub fn if_os(&self) -> Option<Result<&str, PcapNGOptionError>> {
options_get_as_str(&self.options, OptionCode::IfOs)
}
/// Return the `if_ipv4addr` option values, if present
///
/// This option can be multi-valued.
///
/// Returns `None` if option is not present, `Some(Ok(Vec))` if the value is present and valid,
/// or `Some(Err(_))` if value is present but invalid
///
/// Each item of the `Vec` is a pair `(IPv4Addr, IPv4Mask)`
pub fn if_ipv4addr(&self) -> Option<Result<Vec<(Ipv4Addr, Ipv4Addr)>, PcapNGOptionError>> {
let res = self.options.iter().try_fold(Vec::new(), |mut acc, opt| {
if opt.code == OptionCode::IfIpv4Addr {
let b = opt.as_bytes()?;
if b.len() != 8 {
return Err(PcapNGOptionError::InvalidLength);
}
let addr = Ipv4Addr::new(b[0], b[1], b[2], b[3]);
let mask = Ipv4Addr::new(b[4], b[5], b[6], b[7]);
acc.push((addr, mask));
Ok(acc)
} else {
Ok(acc)
}
});
if res.as_ref().map_or(false, |v| v.is_empty()) {
None
} else {
Some(res)
}
}
/// Return the `if_ipv6addr` option values, if present
///
/// This option can be multi-valued.
///
/// Returns `None` if option is not present, `Some(Ok(Vec))` if the value is present and valid,
/// or `Some(Err(_))` if value is present but invalid
///
/// Each item of the `Vec` is a pair `(IPv6Addr, PrefixLen)`
pub fn if_ipv6addr(&self) -> Option<Result<Vec<(Ipv6Addr, u8)>, PcapNGOptionError>> {
let res = self.options.iter().try_fold(Vec::new(), |mut acc, opt| {
if opt.code == OptionCode::IfIpv4Addr {
let b = opt.as_bytes()?;
if b.len() != 17 {
return Err(PcapNGOptionError::InvalidLength);
}
let mut array_u16 = [0u16; 8];
for i in 0..8 {
array_u16[i] = ((b[2 * i] as u16) << 8) + b[2 * i + 1] as u16;
}
let addr = Ipv6Addr::new(
array_u16[0],
array_u16[1],
array_u16[2],
array_u16[3],
array_u16[4],
array_u16[5],
array_u16[6],
array_u16[7],
);
let mask = b[16];
acc.push((addr, mask));
Ok(acc)
} else {
Ok(acc)
}
});
if res.as_ref().map_or(false, |v| v.is_empty()) {
None
} else {
Some(res)
}
}
/// Return the `if_macaddr` option value, if present
///
/// If the option is present multiple times, the first value is returned.
///
/// Returns `None` if option is not present, `Some(Ok(value))` if the value is present and valid,
/// or `Some(Err(_))` if value is present but invalid
pub fn if_macaddr(&self) -> Option<Result<&[u8], PcapNGOptionError>> {
options_get_as_bytes(&self.options, OptionCode::IfMacAddr)
}
/// Return the `if_euiaddr` option value, if present
///
/// If the option is present multiple times, the first value is returned.
///
/// Returns `None` if option is not present, `Some(Ok(value))` if the value is present and valid,
/// or `Some(Err(_))` if value is present but invalid
pub fn if_euiaddr(&self) -> Option<Result<&[u8], PcapNGOptionError>> {
options_get_as_bytes(&self.options, OptionCode::IfEuiAddr)
}
/// Return the `if_speed` option value, if present
///
/// If the option is present multiple times, the first value is returned.
///
/// Returns `None` if option is not present, `Some(Ok(value))` if the value is present and valid,
/// or `Some(Err(_))` if value is present but invalid
pub fn if_speed(&self) -> Option<Result<u64, PcapNGOptionError>> {
options_get_as_u64_le(&self.options, OptionCode::IfSpeed)
}
/// Return the `if_tsresol` option value, if present
///
/// If the option is present multiple times, the first value is returned.
///
/// Returns `None` if option is not present, `Some(Ok(value))` if the value is present and valid,
/// or `Some(Err(_))` if value is present but invalid
pub fn if_tsresol(&self) -> Option<Result<u8, PcapNGOptionError>> {
options_get_as_u8(&self.options, OptionCode::IfTsresol)
}
/// Return the `if_filter` option value, if present
///
/// If the option is present multiple times, the first value is returned.
///
/// Returns `None` if option is not present, `Some(Ok(value))` if the value is present and valid,
/// or `Some(Err(_))` if value is present but invalid
pub fn if_filter(&self) -> Option<Result<&str, PcapNGOptionError>> {
options_get_as_str(&self.options, OptionCode::IfFilter)
}
/// Return the `if_tsoffset` option value, if present
///
/// If the option is present multiple times, the first value is returned.
///
/// Returns `None` if option is not present, `Some(Ok(value))` if the value is present and valid,
/// or `Some(Err(_))` if value is present but invalid
pub fn if_tsoffset(&self) -> Option<Result<i64, PcapNGOptionError>> {
options_get_as_i64_le(&self.options, OptionCode::IfTsoffset)
}
}
impl<'a, En: PcapEndianness> PcapNGBlockParser<'a, En, InterfaceDescriptionBlock<'a>>
for InterfaceDescriptionBlock<'a>
{
const HDR_SZ: usize = 20;
const MAGIC: u32 = IDB_MAGIC;
fn inner_parse<E: ParseError<&'a [u8]>>(
block_type: u32,
block_len1: u32,
i: &'a [u8],
block_len2: u32,
) -> IResult<&'a [u8], InterfaceDescriptionBlock<'a>, E> {
// caller function already tested header type(magic) and length
// read end of header
let (i, linktype) = En::parse_u16(i)?;
let (i, reserved) = En::parse_u16(i)?;
let (i, snaplen) = En::parse_u32(i)?;
// read options
let (i, options) = opt_parse_options::<En, E>(i, block_len1 as usize, 20)?;
if block_len2 != block_len1 {
return Err(Err::Error(E::from_error_kind(i, ErrorKind::Verify)));
}
let (if_tsresol, if_tsoffset) = if_extract_tsoffset_and_tsresol(&options);
let block = InterfaceDescriptionBlock {
block_type,
block_len1,
linktype: Linktype(linktype as i32),
reserved,
snaplen,
options,
block_len2,
if_tsresol,
if_tsoffset,
};
Ok((i, block))
}
}
/// Parse an Interface Packet Block (little-endian)
pub fn parse_interfacedescriptionblock_le(
i: &[u8],
) -> IResult<&[u8], InterfaceDescriptionBlock<'_>, PcapError<&[u8]>> {
ng_block_parser::<InterfaceDescriptionBlock, PcapLE, _, _>()(i)
}
/// Parse an Interface Packet Block (big-endian)
pub fn parse_interfacedescriptionblock_be(
i: &[u8],
) -> IResult<&[u8], InterfaceDescriptionBlock<'_>, PcapError<&[u8]>> {
ng_block_parser::<InterfaceDescriptionBlock, PcapBE, _, _>()(i)
}