use std::ptr; use std::net::SocketAddr; use std::collections::HashMap; use super::types::{Error, Table, Protocol, Address}; use super::util::{now, Time, Duration}; #[derive(PartialEq)] pub struct Frame; impl Protocol for Frame { fn parse(data: &[u8]) -> Result<(Address, Address), Error> { if data.len() < 14 { return Err(Error::ParseError("Frame is too short")); } let mut pos = 0; let dst_data = &data[pos..pos+6]; pos += 6; let src_data = &data[pos..pos+6]; pos += 6; if data[pos] == 0x81 && data[pos+1] == 0x00 { pos += 2; if data.len() < pos + 2 { return Err(Error::ParseError("Vlan frame is too short")); } let mut src = [0; 16]; let mut dst = [0; 16]; unsafe { ptr::copy_nonoverlapping(data[pos..].as_ptr(), src.as_mut_ptr(), 2); ptr::copy_nonoverlapping(src_data.as_ptr(), src[2..].as_mut_ptr(), 6); ptr::copy_nonoverlapping(data[pos..].as_ptr(), dst.as_mut_ptr(), 2); ptr::copy_nonoverlapping(dst_data.as_ptr(), dst[2..].as_mut_ptr(), 6); } Ok((Address(src, 8), Address(dst, 8))) } else { let mut src = [0; 16]; let mut dst = [0; 16]; unsafe { ptr::copy_nonoverlapping(src_data.as_ptr(), src.as_mut_ptr(), 6); ptr::copy_nonoverlapping(dst_data.as_ptr(), dst.as_mut_ptr(), 6); } Ok((Address(src, 6), Address(dst, 6))) } } } struct SwitchTableValue { address: SocketAddr, timeout: Time } pub struct SwitchTable { table: HashMap
, cache: Option<(Address, SocketAddr)>, timeout: Duration } impl SwitchTable { pub fn new(timeout: Duration) -> Self { SwitchTable{table: HashMap::new(), cache: None, timeout: timeout} } } impl Table for SwitchTable { fn housekeep(&mut self) { let now = now(); let mut del: Vec = Vec::new(); for (key, val) in &self.table { if val.timeout < now { del.push(key.clone()); } } for key in del { info!("Forgot address {:?}", key); self.table.remove(&key); } self.cache = None; } #[inline] fn learn(&mut self, key: Address, _prefix_len: Option