1use std::convert::TryFrom;
2
3use pcap_parser::*;
4
5use crate::duration::Duration;
6
7#[derive(Clone, Default)]
9pub struct ParseBlockContext {
10 pub block_index: usize,
12}
13
14#[derive(Clone, Default)]
16pub struct ParseContext {
17 pub first_packet_ts: Duration,
19 pub rel_ts: Duration,
21 pub pcap_index: usize,
23}
24
25#[derive(Clone)]
27pub struct InterfaceInfo {
28 pub link_type: Linktype,
30 pub if_tsresol: u8,
32 pub ts_unit: u64,
34 pub if_tsoffset: u64,
36 pub snaplen: u32,
38}
39
40impl Default for InterfaceInfo {
41 fn default() -> Self {
42 InterfaceInfo {
43 link_type: Linktype(0),
44 if_tsresol: 0,
45 ts_unit: 0,
46 if_tsoffset: 0,
47 snaplen: 0,
48 }
49 }
50}
51pub fn pcapng_build_interface<'a>(idb: &'a InterfaceDescriptionBlock<'a>) -> InterfaceInfo {
52 let link_type = idb.linktype;
53 let mut if_tsresol: u8 = 6;
55 let mut ts_unit: u64 = 1_000_000;
56 let mut if_tsoffset: u64 = 0;
57 for opt in idb.options.iter() {
58 match opt.code {
59 OptionCode::IfTsresol => {
60 if !opt.value.is_empty() {
61 if_tsresol = opt.value[0];
62 if let Some(resol) = pcap_parser::build_ts_resolution(if_tsresol) {
63 ts_unit = resol;
64 }
65 }
66 }
67 OptionCode::IfTsoffset => {
68 if opt.value.len() >= 8 {
69 let int_bytes = <[u8; 8]>::try_from(opt.value).expect("Convert bytes to u64");
70 if_tsoffset = u64::from_le_bytes(int_bytes) ;
71 }
72 }
73 _ => (),
74 }
75 }
76 InterfaceInfo {
77 link_type,
78 if_tsresol,
79 ts_unit,
80 if_tsoffset,
81 snaplen: idb.snaplen,
82 }
83}
84
85