#![macro_use]
pub trait Packet {
fn packet(&self) -> &[u8];
fn payload(&self) -> &[u8];
}
pub trait MutablePacket : Packet {
fn packet_mut(&mut self) -> &mut [u8];
fn payload_mut(&mut self) -> &mut [u8];
fn clone_from<T: Packet>(&mut self, other: &T) {
use std::ptr;
assert!(self.packet().len() >= other.packet().len());
unsafe {
ptr::copy_nonoverlapping(other.packet().as_ptr(),
self.packet_mut().as_mut_ptr(),
other.packet().len());
}
}
}
pub trait FromPacket : Packet {
type T;
fn from_packet(&self) -> Self::T;
}
pub trait PacketSize : Packet {
fn packet_size(&self) -> usize;
}
pub trait PrimitiveValues {
type T;
fn to_primitive_values(&self) -> Self::T;
}
impl PrimitiveValues for ::std::net::Ipv4Addr {
type T = (u8, u8, u8, u8);
fn to_primitive_values(&self) -> (u8, u8, u8, u8) {
let octets = self.octets();
(octets[0], octets[1], octets[2], octets[3])
}
}
impl PrimitiveValues for ::std::net::Ipv6Addr {
type T = (u16, u16, u16, u16, u16, u16, u16, u16);
fn to_primitive_values(&self) -> (u16, u16, u16, u16, u16, u16, u16, u16) {
let segments = self.segments();
(segments[0],
segments[1],
segments[2],
segments[3],
segments[4],
segments[5],
segments[6],
segments[7])
}
}
pub mod ethernet;
pub mod ip;
pub mod ipv4;
pub mod ipv6;
pub mod udp;