use std::io::Cursor;
use byteorder::{NetworkEndian, ReadBytesExt, WriteBytesExt};
use packet::checksum::compute_checksum;
pub static HEADER_SIZE: usize = 8;
pub type Type = u8;
pub static ECHO_REPLY_TYPE: Type = 0;
pub static ECHO_REQUEST_TYPE: Type = 8;
pub type TypeCode = u8;
pub static ECHO_REPLY_CODE: TypeCode = 0u8;
pub static ECHO_REQUEST_CODE: TypeCode = 0u8;
#[derive(Debug)]
pub struct Header {
ttype: Type,
code: TypeCode,
checksum: u16,
data: u32,
}
impl Header {
pub fn new(ttype: Type, code: TypeCode, data: u32) -> Header {
Header {
ttype: ttype,
code: code,
checksum: 0,
data: data,
}
}
pub fn from(b: &[u8]) -> Header {
let mut r = Cursor::new(b);
Header {
ttype: r.read_u8().unwrap() as Type,
code: r.read_u8().unwrap() as TypeCode,
checksum: r.read_u16::<NetworkEndian>().unwrap(),
data: r.read_u32::<NetworkEndian>().unwrap(),
}
}
pub fn do_checksum(&mut self) {
self.checksum = 0u16;
self.checksum = compute_checksum(&self.as_bytes());
}
pub fn as_bytes(&self) -> [u8; 8] {
let mut w = vec![];
w.write_u8(self.ttype).unwrap();
w.write_u8(self.code).unwrap();
w.write_u16::<NetworkEndian>(self.checksum).unwrap();
w.write_u32::<NetworkEndian>(self.data).unwrap();
let mut header = [0u8; 8];
for i in 0..header.len() {
header[i] = w[i];
}
header
}
pub fn set_data(&mut self, data: u32) {
self.data = data
}
pub fn get_data(&self) -> u32 {
self.data
}
}