Skip to content

Commit 8f62dc7

Browse files
committed
pcapng: add helpers for SHB options
1 parent fc912cd commit 8f62dc7

File tree

2 files changed

+42
-6
lines changed

2 files changed

+42
-6
lines changed

examples/pcap-info.rs

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -34,11 +34,15 @@ fn print_pcap_info(arg: &str) -> Result<(), Box<dyn Error>> {
3434
// first block should be a SectionHeader
3535
if let Block::SectionHeader(sh) = block {
3636
println!("\t\tVersion: {}.{}", sh.major_version, sh.minor_version);
37-
let shb_hardware = sh.shb_hardware();
38-
println!(
39-
"\t\tshb_hardware: {}",
40-
shb_hardware.transpose()?.unwrap_or("<invalid>")
41-
);
37+
if let Some(option) = sh.shb_hardware() {
38+
println!("\t\tshb_hardware: {}", option.unwrap_or("<none>"));
39+
}
40+
if let Some(option) = sh.shb_os() {
41+
println!("\t\tshb_os: {}", option.unwrap_or("<none>"));
42+
}
43+
if let Some(option) = sh.shb_userappl() {
44+
println!("\t\tshb_userappl: {}", option.unwrap_or("<none>"));
45+
}
4246
} else {
4347
return Err("pcapng: block is not a section header".into());
4448
}

src/pcapng/section_header.rs

Lines changed: 33 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ impl<'a> SectionHeaderBlock<'a> {
3434

3535
/// Return the `shb_hardware` option value, if present
3636
///
37-
/// Attempt to retrieve the `shb_hardware` value
37+
/// If the option is present multiple times, the first value is returned.
3838
///
3939
/// Returns `None` if option is not present, `Some(Ok(value))` if the value is present and valid,
4040
/// or `Some(Err(_))` if value is present but invalid
@@ -47,6 +47,38 @@ impl<'a> SectionHeaderBlock<'a> {
4747
}
4848
})
4949
}
50+
51+
/// Return the `shb_os` option value, if present
52+
///
53+
/// If the option is present multiple times, the first value is returned.
54+
///
55+
/// Returns `None` if option is not present, `Some(Ok(value))` if the value is present and valid,
56+
/// or `Some(Err(_))` if value is present but invalid
57+
pub fn shb_os(&self) -> Option<Result<&str, PcapNGOptionError>> {
58+
self.options.iter().find_map(|opt| {
59+
if opt.code == OptionCode::ShbOs {
60+
Some(opt.as_str())
61+
} else {
62+
None
63+
}
64+
})
65+
}
66+
67+
/// Return the `shb_userappl` option value, if present
68+
///
69+
/// If the option is present multiple times, the first value is returned.
70+
///
71+
/// Returns `None` if option is not present, `Some(Ok(value))` if the value is present and valid,
72+
/// or `Some(Err(_))` if value is present but invalid
73+
pub fn shb_userappl(&self) -> Option<Result<&str, PcapNGOptionError>> {
74+
self.options.iter().find_map(|opt| {
75+
if opt.code == OptionCode::ShbUserAppl {
76+
Some(opt.as_str())
77+
} else {
78+
None
79+
}
80+
})
81+
}
5082
}
5183

5284
impl<'a> PcapNGBlockParser<'a, PcapBE, SectionHeaderBlock<'a>> for SectionHeaderBlock<'a> {

0 commit comments

Comments
 (0)