use std::convert::TryFrom;
use pcap_parser::*;
use crate::duration::Duration;
#[derive(Clone, Default)]
pub struct ParseBlockContext {
pub block_index: usize,
}
#[derive(Clone, Default)]
pub struct ParseContext {
pub first_packet_ts: Duration,
pub rel_ts: Duration,
pub pcap_index: usize,
}
#[derive(Clone)]
pub struct InterfaceInfo {
pub link_type: Linktype,
pub if_tsresol: u8,
pub ts_unit: u64,
pub if_tsoffset: u64,
pub snaplen: u32,
}
impl Default for InterfaceInfo {
fn default() -> Self {
InterfaceInfo {
link_type: Linktype(0),
if_tsresol: 0,
ts_unit: 0,
if_tsoffset: 0,
snaplen: 0,
}
}
}
pub fn pcapng_build_interface<'a>(idb: &'a InterfaceDescriptionBlock<'a>) -> InterfaceInfo {
let link_type = idb.linktype;
let mut if_tsresol: u8 = 6;
let mut ts_unit: u64 = 1_000_000;
let mut if_tsoffset: u64 = 0;
for opt in idb.options.iter() {
match opt.code {
OptionCode::IfTsresol => {
if !opt.value.is_empty() {
if_tsresol = opt.value[0];
if let Some(resol) = pcap_parser::build_ts_resolution(if_tsresol) {
ts_unit = resol;
}
}
}
OptionCode::IfTsoffset => {
if opt.value.len() >= 8 {
let int_bytes = <[u8; 8]>::try_from(opt.value).expect("Convert bytes to u64");
if_tsoffset = u64::from_le_bytes(int_bytes) ;
}
}
_ => (),
}
}
InterfaceInfo {
link_type,
if_tsresol,
ts_unit,
if_tsoffset,
snaplen: idb.snaplen,
}
}