use std::ptr; use std::net::SocketAddr; use std::collections::HashMap; use super::cloud::{Error, Table, Protocol, Address}; use time::{Duration, SteadyTime}; #[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 = Vec::with_capacity(8); let mut dst = Vec::with_capacity(8); unsafe { ptr::copy_nonoverlapping(data.as_ptr(), src.as_mut_ptr(), 2); ptr::copy_nonoverlapping(src_data.as_ptr(), src[2..].as_mut_ptr(), 6); src.set_len(8); ptr::copy_nonoverlapping(data.as_ptr(), dst.as_mut_ptr(), 2); ptr::copy_nonoverlapping(dst_data.as_ptr(), dst[2..].as_mut_ptr(), 6); dst.set_len(8); } Ok((Address(src), Address(dst))) } else { let mut src = Vec::with_capacity(6); let mut dst = Vec::with_capacity(6); unsafe { ptr::copy_nonoverlapping(src_data.as_ptr(), src.as_mut_ptr(), 6); src.set_len(6); ptr::copy_nonoverlapping(dst_data.as_ptr(), dst.as_mut_ptr(), 6); dst.set_len(6); } Ok((Address(src), Address(dst))) } } } struct MacTableValue { address: SocketAddr, timeout: SteadyTime } pub struct MacTable { table: HashMap
, timeout: Duration } impl MacTable { pub fn new(timeout: Duration) -> MacTable { MacTable{table: HashMap::new(), timeout: timeout} } } impl Table for MacTable { fn housekeep(&mut self) { let now = SteadyTime::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); } } fn learn(&mut self, key: Address, _prefix_len: Option