2016-12-10 17:23:40 +08:00
|
|
|
use core::fmt;
|
|
|
|
use byteorder::{ByteOrder, NetworkEndian};
|
2016-12-20 07:50:04 +08:00
|
|
|
|
2020-12-27 07:11:30 +08:00
|
|
|
use crate::{Error, Result};
|
2016-12-10 17:23:40 +08:00
|
|
|
|
|
|
|
enum_with_unknown! {
|
|
|
|
/// Ethernet protocol type.
|
2016-12-11 00:25:43 +08:00
|
|
|
pub enum EtherType(u16) {
|
2016-12-10 17:23:40 +08:00
|
|
|
Ipv4 = 0x0800,
|
|
|
|
Arp = 0x0806,
|
2016-12-11 00:25:43 +08:00
|
|
|
Ipv6 = 0x86DD
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl fmt::Display for EtherType {
|
|
|
|
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
2020-12-26 16:28:05 +08:00
|
|
|
match *self {
|
|
|
|
EtherType::Ipv4 => write!(f, "IPv4"),
|
|
|
|
EtherType::Ipv6 => write!(f, "IPv6"),
|
|
|
|
EtherType::Arp => write!(f, "ARP"),
|
|
|
|
EtherType::Unknown(id) => write!(f, "0x{:04x}", id)
|
2016-12-11 00:25:43 +08:00
|
|
|
}
|
2016-12-10 17:23:40 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// A six-octet Ethernet II address.
|
2018-02-03 09:17:15 +08:00
|
|
|
#[derive(Debug, Hash, PartialEq, Eq, PartialOrd, Ord, Clone, Copy, Default)]
|
2016-12-10 17:23:40 +08:00
|
|
|
pub struct Address(pub [u8; 6]);
|
|
|
|
|
|
|
|
impl Address {
|
2017-09-25 07:29:42 +08:00
|
|
|
/// The broadcast address.
|
2017-09-21 05:52:28 +08:00
|
|
|
pub const BROADCAST: Address = Address([0xff; 6]);
|
2016-12-12 10:39:46 +08:00
|
|
|
|
2016-12-10 17:23:40 +08:00
|
|
|
/// Construct an Ethernet address from a sequence of octets, in big-endian.
|
|
|
|
///
|
|
|
|
/// # Panics
|
|
|
|
/// The function panics if `data` is not six octets long.
|
|
|
|
pub fn from_bytes(data: &[u8]) -> Address {
|
|
|
|
let mut bytes = [0; 6];
|
|
|
|
bytes.copy_from_slice(data);
|
|
|
|
Address(bytes)
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Return an Ethernet address as a sequence of octets, in big-endian.
|
|
|
|
pub fn as_bytes(&self) -> &[u8] {
|
|
|
|
&self.0
|
|
|
|
}
|
2016-12-12 10:39:46 +08:00
|
|
|
|
2017-03-07 14:32:09 +08:00
|
|
|
/// Query whether the address is an unicast address.
|
|
|
|
pub fn is_unicast(&self) -> bool {
|
|
|
|
!(self.is_broadcast() ||
|
|
|
|
self.is_multicast())
|
|
|
|
}
|
|
|
|
|
2017-03-06 12:00:13 +08:00
|
|
|
/// Query whether this address is the broadcast address.
|
|
|
|
pub fn is_broadcast(&self) -> bool {
|
2017-09-21 05:52:28 +08:00
|
|
|
*self == Self::BROADCAST
|
2017-03-06 12:00:13 +08:00
|
|
|
}
|
|
|
|
|
2016-12-12 10:39:46 +08:00
|
|
|
/// Query whether the "multicast" bit in the OUI is set.
|
|
|
|
pub fn is_multicast(&self) -> bool {
|
|
|
|
self.0[0] & 0x01 != 0
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Query whether the "locally administered" bit in the OUI is set.
|
|
|
|
pub fn is_local(&self) -> bool {
|
|
|
|
self.0[0] & 0x02 != 0
|
|
|
|
}
|
2016-12-10 17:23:40 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
impl fmt::Display for Address {
|
|
|
|
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
|
|
|
let bytes = self.0;
|
2016-12-12 15:19:53 +08:00
|
|
|
write!(f, "{:02x}-{:02x}-{:02x}-{:02x}-{:02x}-{:02x}",
|
2016-12-10 17:23:40 +08:00
|
|
|
bytes[0], bytes[1], bytes[2], bytes[3], bytes[4], bytes[5])
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-12-13 01:26:06 +08:00
|
|
|
/// A read/write wrapper around an Ethernet II frame buffer.
|
2018-02-15 16:45:21 +08:00
|
|
|
#[derive(Debug, Clone)]
|
2016-12-12 15:19:53 +08:00
|
|
|
pub struct Frame<T: AsRef<[u8]>> {
|
|
|
|
buffer: T
|
|
|
|
}
|
2016-12-10 17:23:40 +08:00
|
|
|
|
|
|
|
mod field {
|
2020-12-27 07:11:30 +08:00
|
|
|
use crate::wire::field::*;
|
2016-12-10 17:23:40 +08:00
|
|
|
|
2016-12-14 08:11:45 +08:00
|
|
|
pub const DESTINATION: Field = 0..6;
|
|
|
|
pub const SOURCE: Field = 6..12;
|
|
|
|
pub const ETHERTYPE: Field = 12..14;
|
|
|
|
pub const PAYLOAD: Rest = 14..;
|
2016-12-10 17:23:40 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
impl<T: AsRef<[u8]>> Frame<T> {
|
Do not attempt to validate length of packets being emitted.
This is a form of an uninitialized read bug; although safe it caused
panics. In short, transmit buffers received from the network stack
should be considered uninitialized (in practice they will often
contain previously transmitted packets or parts thereof). Wrapping
them with the only method we had (e.g. Ipv4Packet) treated the buffer
as if it contained a valid incoming packet, which can easily fail
with Error::Truncated.
This commit splits every `fn new(buffer: T) -> Result<Self, Error>`
method on a `Packet` into three smaller ones:
* `fn check_len(&self) -> Result<(), Error>`, purely a validator;
* `fn new(T) -> Self`, purely a wrapper;
* `fn new_checked(T) -> Result<Self, Error>`, a validating wrapper.
This makes it easy to process ingress packets (using `new_checked`),
egress packets (using `new`), and, if needed, maintain the invariants
at any point during packet construction (using `check_len`).
Fixes #17.
2017-06-24 17:15:22 +08:00
|
|
|
/// Imbue a raw octet buffer with Ethernet frame structure.
|
2018-07-11 08:22:43 +08:00
|
|
|
pub fn new_unchecked(buffer: T) -> Frame<T> {
|
Do not attempt to validate length of packets being emitted.
This is a form of an uninitialized read bug; although safe it caused
panics. In short, transmit buffers received from the network stack
should be considered uninitialized (in practice they will often
contain previously transmitted packets or parts thereof). Wrapping
them with the only method we had (e.g. Ipv4Packet) treated the buffer
as if it contained a valid incoming packet, which can easily fail
with Error::Truncated.
This commit splits every `fn new(buffer: T) -> Result<Self, Error>`
method on a `Packet` into three smaller ones:
* `fn check_len(&self) -> Result<(), Error>`, purely a validator;
* `fn new(T) -> Self`, purely a wrapper;
* `fn new_checked(T) -> Result<Self, Error>`, a validating wrapper.
This makes it easy to process ingress packets (using `new_checked`),
egress packets (using `new`), and, if needed, maintain the invariants
at any point during packet construction (using `check_len`).
Fixes #17.
2017-06-24 17:15:22 +08:00
|
|
|
Frame { buffer }
|
|
|
|
}
|
|
|
|
|
2018-07-11 08:22:43 +08:00
|
|
|
/// Shorthand for a combination of [new_unchecked] and [check_len].
|
Do not attempt to validate length of packets being emitted.
This is a form of an uninitialized read bug; although safe it caused
panics. In short, transmit buffers received from the network stack
should be considered uninitialized (in practice they will often
contain previously transmitted packets or parts thereof). Wrapping
them with the only method we had (e.g. Ipv4Packet) treated the buffer
as if it contained a valid incoming packet, which can easily fail
with Error::Truncated.
This commit splits every `fn new(buffer: T) -> Result<Self, Error>`
method on a `Packet` into three smaller ones:
* `fn check_len(&self) -> Result<(), Error>`, purely a validator;
* `fn new(T) -> Self`, purely a wrapper;
* `fn new_checked(T) -> Result<Self, Error>`, a validating wrapper.
This makes it easy to process ingress packets (using `new_checked`),
egress packets (using `new`), and, if needed, maintain the invariants
at any point during packet construction (using `check_len`).
Fixes #17.
2017-06-24 17:15:22 +08:00
|
|
|
///
|
2018-07-11 08:22:43 +08:00
|
|
|
/// [new_unchecked]: #method.new_unchecked
|
Do not attempt to validate length of packets being emitted.
This is a form of an uninitialized read bug; although safe it caused
panics. In short, transmit buffers received from the network stack
should be considered uninitialized (in practice they will often
contain previously transmitted packets or parts thereof). Wrapping
them with the only method we had (e.g. Ipv4Packet) treated the buffer
as if it contained a valid incoming packet, which can easily fail
with Error::Truncated.
This commit splits every `fn new(buffer: T) -> Result<Self, Error>`
method on a `Packet` into three smaller ones:
* `fn check_len(&self) -> Result<(), Error>`, purely a validator;
* `fn new(T) -> Self`, purely a wrapper;
* `fn new_checked(T) -> Result<Self, Error>`, a validating wrapper.
This makes it easy to process ingress packets (using `new_checked`),
egress packets (using `new`), and, if needed, maintain the invariants
at any point during packet construction (using `check_len`).
Fixes #17.
2017-06-24 17:15:22 +08:00
|
|
|
/// [check_len]: #method.check_len
|
2017-07-27 21:51:02 +08:00
|
|
|
pub fn new_checked(buffer: T) -> Result<Frame<T>> {
|
2018-07-11 08:22:43 +08:00
|
|
|
let packet = Self::new_unchecked(buffer);
|
2017-06-25 00:34:32 +08:00
|
|
|
packet.check_len()?;
|
Do not attempt to validate length of packets being emitted.
This is a form of an uninitialized read bug; although safe it caused
panics. In short, transmit buffers received from the network stack
should be considered uninitialized (in practice they will often
contain previously transmitted packets or parts thereof). Wrapping
them with the only method we had (e.g. Ipv4Packet) treated the buffer
as if it contained a valid incoming packet, which can easily fail
with Error::Truncated.
This commit splits every `fn new(buffer: T) -> Result<Self, Error>`
method on a `Packet` into three smaller ones:
* `fn check_len(&self) -> Result<(), Error>`, purely a validator;
* `fn new(T) -> Self`, purely a wrapper;
* `fn new_checked(T) -> Result<Self, Error>`, a validating wrapper.
This makes it easy to process ingress packets (using `new_checked`),
egress packets (using `new`), and, if needed, maintain the invariants
at any point during packet construction (using `check_len`).
Fixes #17.
2017-06-24 17:15:22 +08:00
|
|
|
Ok(packet)
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Ensure that no accessor method will panic if called.
|
|
|
|
/// Returns `Err(Error::Truncated)` if the buffer is too short.
|
2017-07-27 21:51:02 +08:00
|
|
|
pub fn check_len(&self) -> Result<()> {
|
Do not attempt to validate length of packets being emitted.
This is a form of an uninitialized read bug; although safe it caused
panics. In short, transmit buffers received from the network stack
should be considered uninitialized (in practice they will often
contain previously transmitted packets or parts thereof). Wrapping
them with the only method we had (e.g. Ipv4Packet) treated the buffer
as if it contained a valid incoming packet, which can easily fail
with Error::Truncated.
This commit splits every `fn new(buffer: T) -> Result<Self, Error>`
method on a `Packet` into three smaller ones:
* `fn check_len(&self) -> Result<(), Error>`, purely a validator;
* `fn new(T) -> Self`, purely a wrapper;
* `fn new_checked(T) -> Result<Self, Error>`, a validating wrapper.
This makes it easy to process ingress packets (using `new_checked`),
egress packets (using `new`), and, if needed, maintain the invariants
at any point during packet construction (using `check_len`).
Fixes #17.
2017-06-24 17:15:22 +08:00
|
|
|
let len = self.buffer.as_ref().len();
|
2016-12-12 15:19:53 +08:00
|
|
|
if len < field::PAYLOAD.start {
|
|
|
|
Err(Error::Truncated)
|
2016-12-10 17:23:40 +08:00
|
|
|
} else {
|
Do not attempt to validate length of packets being emitted.
This is a form of an uninitialized read bug; although safe it caused
panics. In short, transmit buffers received from the network stack
should be considered uninitialized (in practice they will often
contain previously transmitted packets or parts thereof). Wrapping
them with the only method we had (e.g. Ipv4Packet) treated the buffer
as if it contained a valid incoming packet, which can easily fail
with Error::Truncated.
This commit splits every `fn new(buffer: T) -> Result<Self, Error>`
method on a `Packet` into three smaller ones:
* `fn check_len(&self) -> Result<(), Error>`, purely a validator;
* `fn new(T) -> Self`, purely a wrapper;
* `fn new_checked(T) -> Result<Self, Error>`, a validating wrapper.
This makes it easy to process ingress packets (using `new_checked`),
egress packets (using `new`), and, if needed, maintain the invariants
at any point during packet construction (using `check_len`).
Fixes #17.
2017-06-24 17:15:22 +08:00
|
|
|
Ok(())
|
2016-12-10 17:23:40 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Consumes the frame, returning the underlying buffer.
|
|
|
|
pub fn into_inner(self) -> T {
|
2016-12-12 15:19:53 +08:00
|
|
|
self.buffer
|
2016-12-10 17:23:40 +08:00
|
|
|
}
|
|
|
|
|
2017-01-27 10:49:06 +08:00
|
|
|
/// Return the length of a frame header.
|
|
|
|
pub fn header_len() -> usize {
|
|
|
|
field::PAYLOAD.start
|
|
|
|
}
|
|
|
|
|
2016-12-20 07:50:04 +08:00
|
|
|
/// Return the length of a buffer required to hold a packet with the payload
|
|
|
|
/// of a given length.
|
|
|
|
pub fn buffer_len(payload_len: usize) -> usize {
|
2016-12-21 03:18:35 +08:00
|
|
|
field::PAYLOAD.start + payload_len
|
2016-12-20 07:50:04 +08:00
|
|
|
}
|
|
|
|
|
2016-12-12 15:19:53 +08:00
|
|
|
/// Return the destination address field.
|
2016-12-31 00:55:31 +08:00
|
|
|
#[inline]
|
2016-12-13 01:26:06 +08:00
|
|
|
pub fn dst_addr(&self) -> Address {
|
2016-12-12 15:19:53 +08:00
|
|
|
let data = self.buffer.as_ref();
|
|
|
|
Address::from_bytes(&data[field::DESTINATION])
|
2016-12-10 17:23:40 +08:00
|
|
|
}
|
|
|
|
|
2016-12-12 15:19:53 +08:00
|
|
|
/// Return the source address field.
|
2016-12-31 00:55:31 +08:00
|
|
|
#[inline]
|
2016-12-13 01:26:06 +08:00
|
|
|
pub fn src_addr(&self) -> Address {
|
2016-12-12 15:19:53 +08:00
|
|
|
let data = self.buffer.as_ref();
|
|
|
|
Address::from_bytes(&data[field::SOURCE])
|
2016-12-10 17:23:40 +08:00
|
|
|
}
|
|
|
|
|
2016-12-11 00:25:43 +08:00
|
|
|
/// Return the EtherType field, without checking for 802.1Q.
|
2016-12-31 00:55:31 +08:00
|
|
|
#[inline]
|
2016-12-11 00:25:43 +08:00
|
|
|
pub fn ethertype(&self) -> EtherType {
|
2016-12-12 15:19:53 +08:00
|
|
|
let data = self.buffer.as_ref();
|
|
|
|
let raw = NetworkEndian::read_u16(&data[field::ETHERTYPE]);
|
2016-12-11 00:25:43 +08:00
|
|
|
EtherType::from(raw)
|
2016-12-10 17:23:40 +08:00
|
|
|
}
|
2016-12-13 23:18:56 +08:00
|
|
|
}
|
2016-12-10 17:23:40 +08:00
|
|
|
|
2016-12-13 23:18:56 +08:00
|
|
|
impl<'a, T: AsRef<[u8]> + ?Sized> Frame<&'a T> {
|
2016-12-10 17:23:40 +08:00
|
|
|
/// Return a pointer to the payload, without checking for 802.1Q.
|
2016-12-31 00:55:31 +08:00
|
|
|
#[inline]
|
2016-12-13 23:18:56 +08:00
|
|
|
pub fn payload(&self) -> &'a [u8] {
|
2016-12-12 15:19:53 +08:00
|
|
|
let data = self.buffer.as_ref();
|
|
|
|
&data[field::PAYLOAD]
|
2016-12-10 17:23:40 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<T: AsRef<[u8]> + AsMut<[u8]>> Frame<T> {
|
2016-12-12 15:19:53 +08:00
|
|
|
/// Set the destination address field.
|
2016-12-31 00:55:31 +08:00
|
|
|
#[inline]
|
2016-12-13 01:26:06 +08:00
|
|
|
pub fn set_dst_addr(&mut self, value: Address) {
|
2016-12-12 15:19:53 +08:00
|
|
|
let data = self.buffer.as_mut();
|
|
|
|
data[field::DESTINATION].copy_from_slice(value.as_bytes())
|
2016-12-10 17:23:40 +08:00
|
|
|
}
|
|
|
|
|
2016-12-12 15:19:53 +08:00
|
|
|
/// Set the source address field.
|
2016-12-31 00:55:31 +08:00
|
|
|
#[inline]
|
2016-12-13 01:26:06 +08:00
|
|
|
pub fn set_src_addr(&mut self, value: Address) {
|
2016-12-12 15:19:53 +08:00
|
|
|
let data = self.buffer.as_mut();
|
|
|
|
data[field::SOURCE].copy_from_slice(value.as_bytes())
|
2016-12-10 17:23:40 +08:00
|
|
|
}
|
|
|
|
|
2016-12-11 00:25:43 +08:00
|
|
|
/// Set the EtherType field.
|
2016-12-31 00:55:31 +08:00
|
|
|
#[inline]
|
2016-12-11 00:25:43 +08:00
|
|
|
pub fn set_ethertype(&mut self, value: EtherType) {
|
2016-12-12 15:19:53 +08:00
|
|
|
let data = self.buffer.as_mut();
|
|
|
|
NetworkEndian::write_u16(&mut data[field::ETHERTYPE], value.into())
|
2016-12-10 17:23:40 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Return a mutable pointer to the payload.
|
2016-12-31 00:55:31 +08:00
|
|
|
#[inline]
|
2016-12-10 17:23:40 +08:00
|
|
|
pub fn payload_mut(&mut self) -> &mut [u8] {
|
2016-12-12 15:19:53 +08:00
|
|
|
let data = self.buffer.as_mut();
|
|
|
|
&mut data[field::PAYLOAD]
|
2016-12-10 17:23:40 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-02-08 16:54:50 +08:00
|
|
|
impl<T: AsRef<[u8]>> AsRef<[u8]> for Frame<T> {
|
|
|
|
fn as_ref(&self) -> &[u8] {
|
|
|
|
self.buffer.as_ref()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-12-11 00:25:43 +08:00
|
|
|
impl<T: AsRef<[u8]>> fmt::Display for Frame<T> {
|
|
|
|
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
|
|
|
write!(f, "EthernetII src={} dst={} type={}",
|
2016-12-13 01:26:06 +08:00
|
|
|
self.src_addr(), self.dst_addr(), self.ethertype())
|
2016-12-11 00:25:43 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-12-27 07:11:30 +08:00
|
|
|
use crate::wire::pretty_print::{PrettyPrint, PrettyIndent};
|
2016-12-11 07:15:56 +08:00
|
|
|
|
2016-12-12 20:30:35 +08:00
|
|
|
impl<T: AsRef<[u8]>> PrettyPrint for Frame<T> {
|
2019-06-22 16:19:39 +08:00
|
|
|
fn pretty_print(buffer: &dyn AsRef<[u8]>, f: &mut fmt::Formatter,
|
2016-12-11 07:15:56 +08:00
|
|
|
indent: &mut PrettyIndent) -> fmt::Result {
|
Do not attempt to validate length of packets being emitted.
This is a form of an uninitialized read bug; although safe it caused
panics. In short, transmit buffers received from the network stack
should be considered uninitialized (in practice they will often
contain previously transmitted packets or parts thereof). Wrapping
them with the only method we had (e.g. Ipv4Packet) treated the buffer
as if it contained a valid incoming packet, which can easily fail
with Error::Truncated.
This commit splits every `fn new(buffer: T) -> Result<Self, Error>`
method on a `Packet` into three smaller ones:
* `fn check_len(&self) -> Result<(), Error>`, purely a validator;
* `fn new(T) -> Self`, purely a wrapper;
* `fn new_checked(T) -> Result<Self, Error>`, a validating wrapper.
This makes it easy to process ingress packets (using `new_checked`),
egress packets (using `new`), and, if needed, maintain the invariants
at any point during packet construction (using `check_len`).
Fixes #17.
2017-06-24 17:15:22 +08:00
|
|
|
let frame = match Frame::new_checked(buffer) {
|
2017-12-17 05:42:19 +08:00
|
|
|
Err(err) => return write!(f, "{}({})", indent, err),
|
2016-12-11 07:15:56 +08:00
|
|
|
Ok(frame) => frame
|
|
|
|
};
|
2017-12-17 05:42:19 +08:00
|
|
|
write!(f, "{}{}", indent, frame)?;
|
2016-12-11 07:15:56 +08:00
|
|
|
|
|
|
|
match frame.ethertype() {
|
2017-12-24 21:28:59 +08:00
|
|
|
#[cfg(feature = "proto-ipv4")]
|
2017-12-17 05:42:19 +08:00
|
|
|
EtherType::Arp => {
|
|
|
|
indent.increase(f)?;
|
|
|
|
super::ArpPacket::<&[u8]>::pretty_print(&frame.payload(), f, indent)
|
|
|
|
}
|
2017-12-24 21:28:59 +08:00
|
|
|
#[cfg(feature = "proto-ipv4")]
|
2017-12-17 05:42:19 +08:00
|
|
|
EtherType::Ipv4 => {
|
|
|
|
indent.increase(f)?;
|
|
|
|
super::Ipv4Packet::<&[u8]>::pretty_print(&frame.payload(), f, indent)
|
|
|
|
}
|
2017-12-01 13:05:05 +08:00
|
|
|
#[cfg(feature = "proto-ipv6")]
|
|
|
|
EtherType::Ipv6 => {
|
|
|
|
indent.increase(f)?;
|
|
|
|
super::Ipv6Packet::<&[u8]>::pretty_print(&frame.payload(), f, indent)
|
|
|
|
}
|
2016-12-11 07:15:56 +08:00
|
|
|
_ => Ok(())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-02-15 17:03:04 +08:00
|
|
|
/// A high-level representation of an Internet Protocol version 4 packet header.
|
|
|
|
#[derive(Debug, PartialEq, Eq, Clone, Copy)]
|
|
|
|
pub struct Repr {
|
|
|
|
pub src_addr: Address,
|
|
|
|
pub dst_addr: Address,
|
|
|
|
pub ethertype: EtherType,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Repr {
|
|
|
|
/// Parse an Ethernet II frame and return a high-level representation.
|
|
|
|
pub fn parse<T: AsRef<[u8]> + ?Sized>(frame: &Frame<&T>) -> Result<Repr> {
|
|
|
|
frame.check_len()?;
|
|
|
|
Ok(Repr {
|
|
|
|
src_addr: frame.src_addr(),
|
|
|
|
dst_addr: frame.dst_addr(),
|
|
|
|
ethertype: frame.ethertype(),
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Return the length of a header that will be emitted from this high-level representation.
|
|
|
|
pub fn buffer_len(&self) -> usize {
|
|
|
|
field::PAYLOAD.start
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Emit a high-level representation into an Ethernet II frame.
|
|
|
|
pub fn emit<T: AsRef<[u8]> + AsMut<[u8]>>(&self, frame: &mut Frame<T>) {
|
|
|
|
frame.set_src_addr(self.src_addr);
|
|
|
|
frame.set_dst_addr(self.dst_addr);
|
|
|
|
frame.set_ethertype(self.ethertype);
|
|
|
|
}
|
|
|
|
}
|
2017-12-10 11:09:50 +08:00
|
|
|
|
2016-12-10 17:23:40 +08:00
|
|
|
#[cfg(test)]
|
|
|
|
mod test {
|
2017-12-10 11:09:50 +08:00
|
|
|
// Tests that are valid with any combination of
|
|
|
|
// "proto-*" features.
|
|
|
|
use super::*;
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_broadcast() {
|
|
|
|
assert!(Address::BROADCAST.is_broadcast());
|
|
|
|
assert!(!Address::BROADCAST.is_unicast());
|
|
|
|
assert!(Address::BROADCAST.is_multicast());
|
|
|
|
assert!(Address::BROADCAST.is_local());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[cfg(test)]
|
|
|
|
#[cfg(feature = "proto-ipv4")]
|
|
|
|
mod test_ipv4 {
|
|
|
|
// Tests that are valid only with "proto-ipv4"
|
2016-12-10 17:23:40 +08:00
|
|
|
use super::*;
|
|
|
|
|
|
|
|
static FRAME_BYTES: [u8; 64] =
|
|
|
|
[0x01, 0x02, 0x03, 0x04, 0x05, 0x06,
|
|
|
|
0x11, 0x12, 0x13, 0x14, 0x15, 0x16,
|
2016-12-11 08:22:37 +08:00
|
|
|
0x08, 0x00,
|
2016-12-10 17:23:40 +08:00
|
|
|
0xaa, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
|
|
|
|
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
|
|
|
|
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
|
|
|
|
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
|
|
|
|
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
|
|
|
|
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
|
|
|
|
0x00, 0xff];
|
|
|
|
|
|
|
|
static PAYLOAD_BYTES: [u8; 50] =
|
|
|
|
[0xaa, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
|
|
|
|
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
|
|
|
|
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
|
|
|
|
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
|
|
|
|
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
|
|
|
|
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
|
|
|
|
0x00, 0xff];
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_deconstruct() {
|
2018-07-11 08:22:43 +08:00
|
|
|
let frame = Frame::new_unchecked(&FRAME_BYTES[..]);
|
2016-12-13 01:26:06 +08:00
|
|
|
assert_eq!(frame.dst_addr(), Address([0x01, 0x02, 0x03, 0x04, 0x05, 0x06]));
|
|
|
|
assert_eq!(frame.src_addr(), Address([0x11, 0x12, 0x13, 0x14, 0x15, 0x16]));
|
2016-12-11 08:22:37 +08:00
|
|
|
assert_eq!(frame.ethertype(), EtherType::Ipv4);
|
2016-12-10 17:23:40 +08:00
|
|
|
assert_eq!(frame.payload(), &PAYLOAD_BYTES[..]);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_construct() {
|
2017-06-26 11:44:36 +08:00
|
|
|
let mut bytes = vec![0xa5; 64];
|
2018-07-11 08:22:43 +08:00
|
|
|
let mut frame = Frame::new_unchecked(&mut bytes);
|
2016-12-13 01:26:06 +08:00
|
|
|
frame.set_dst_addr(Address([0x01, 0x02, 0x03, 0x04, 0x05, 0x06]));
|
|
|
|
frame.set_src_addr(Address([0x11, 0x12, 0x13, 0x14, 0x15, 0x16]));
|
2016-12-11 08:22:37 +08:00
|
|
|
frame.set_ethertype(EtherType::Ipv4);
|
2016-12-10 17:23:40 +08:00
|
|
|
frame.payload_mut().copy_from_slice(&PAYLOAD_BYTES[..]);
|
|
|
|
assert_eq!(&frame.into_inner()[..], &FRAME_BYTES[..]);
|
|
|
|
}
|
2017-12-10 11:09:50 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
#[cfg(test)]
|
|
|
|
#[cfg(feature = "proto-ipv6")]
|
|
|
|
mod test_ipv6 {
|
|
|
|
// Tests that are valid only with "proto-ipv6"
|
|
|
|
use super::*;
|
|
|
|
|
|
|
|
static FRAME_BYTES: [u8; 54] =
|
|
|
|
[0x01, 0x02, 0x03, 0x04, 0x05, 0x06,
|
|
|
|
0x11, 0x12, 0x13, 0x14, 0x15, 0x16,
|
|
|
|
0x86, 0xdd,
|
|
|
|
0x60, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
|
|
|
|
0xfe, 0x80, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
|
|
|
|
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01,
|
|
|
|
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
|
|
|
|
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01];
|
|
|
|
|
|
|
|
static PAYLOAD_BYTES: [u8; 40] =
|
|
|
|
[0x60, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
|
|
|
|
0xfe, 0x80, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
|
|
|
|
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01,
|
|
|
|
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
|
|
|
|
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01];
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_deconstruct() {
|
2018-07-11 08:22:43 +08:00
|
|
|
let frame = Frame::new_unchecked(&FRAME_BYTES[..]);
|
2017-12-10 11:09:50 +08:00
|
|
|
assert_eq!(frame.dst_addr(), Address([0x01, 0x02, 0x03, 0x04, 0x05, 0x06]));
|
|
|
|
assert_eq!(frame.src_addr(), Address([0x11, 0x12, 0x13, 0x14, 0x15, 0x16]));
|
|
|
|
assert_eq!(frame.ethertype(), EtherType::Ipv6);
|
|
|
|
assert_eq!(frame.payload(), &PAYLOAD_BYTES[..]);
|
|
|
|
}
|
2017-09-21 05:52:28 +08:00
|
|
|
|
|
|
|
#[test]
|
2017-12-10 11:09:50 +08:00
|
|
|
fn test_construct() {
|
|
|
|
let mut bytes = vec![0xa5; 54];
|
2018-07-11 08:22:43 +08:00
|
|
|
let mut frame = Frame::new_unchecked(&mut bytes);
|
2017-12-10 11:09:50 +08:00
|
|
|
frame.set_dst_addr(Address([0x01, 0x02, 0x03, 0x04, 0x05, 0x06]));
|
|
|
|
frame.set_src_addr(Address([0x11, 0x12, 0x13, 0x14, 0x15, 0x16]));
|
|
|
|
frame.set_ethertype(EtherType::Ipv6);
|
|
|
|
assert_eq!(PAYLOAD_BYTES.len(), frame.payload_mut().len());
|
|
|
|
frame.payload_mut().copy_from_slice(&PAYLOAD_BYTES[..]);
|
|
|
|
assert_eq!(&frame.into_inner()[..], &FRAME_BYTES[..]);
|
2017-09-21 05:52:28 +08:00
|
|
|
}
|
2016-12-10 17:23:40 +08:00
|
|
|
}
|