use packet::{Packet, PrimitiveValues};
use util::MacAddr;
#[packet]
pub struct Ethernet {
#[construct_with(u8, u8, u8, u8, u8, u8)]
destination: MacAddr,
#[construct_with(u8, u8, u8, u8, u8, u8)]
source: MacAddr,
#[construct_with(u16)]
ethertype: EtherType,
#[payload]
payload: Vec<u8>,
}
#[test]
fn ethernet_header_test() {
let mut packet = [0u8; 14];
{
let mut ethernet_header = MutableEthernetPacket::new(&mut packet[..]).unwrap();
let source = MacAddr(0x12, 0x34, 0x56, 0x78, 0x9a, 0xbc);
ethernet_header.set_source(source);
assert_eq!(ethernet_header.get_source(), source);
let dest = MacAddr(0xde, 0xf0, 0x12, 0x34, 0x45, 0x67);
ethernet_header.set_destination(dest);
assert_eq!(ethernet_header.get_destination(), dest);
ethernet_header.set_ethertype(EtherTypes::Ipv6);
assert_eq!(ethernet_header.get_ethertype(), EtherTypes::Ipv6);
}
let ref_packet = [0xde, 0xf0, 0x12, 0x34, 0x45, 0x67,
0x12, 0x34, 0x56, 0x78, 0x9a, 0xbc,
0x86, 0xdd ];
assert_eq!(&ref_packet[..], &packet[..]);
}
#[allow(non_snake_case)]
#[allow(non_upper_case_globals)]
pub mod EtherTypes {
use packet::ethernet::EtherType;
pub const Ipv4: EtherType = EtherType(0x0800);
pub const Arp: EtherType = EtherType(0x0806);
pub const WakeOnLan: EtherType = EtherType(0x0842);
pub const Rarp: EtherType = EtherType(0x8035);
pub const Ipv6: EtherType = EtherType(0x86DD);
}
#[derive(Copy, Clone, Debug, PartialEq, Eq, PartialOrd, Ord)]
pub struct EtherType(pub u16);
impl EtherType {
pub fn new(val: u16) -> EtherType {
EtherType(val)
}
}
impl PrimitiveValues for EtherType {
type T = (u16,);
fn to_primitive_values(&self) -> (u16,) {
(self.0,)
}
}