summaryrefslogtreecommitdiff
path: root/src/protocol/ethernet.rs
diff options
context:
space:
mode:
Diffstat (limited to 'src/protocol/ethernet.rs')
-rw-r--r--src/protocol/ethernet.rs51
1 files changed, 30 insertions, 21 deletions
diff --git a/src/protocol/ethernet.rs b/src/protocol/ethernet.rs
index a588c0d..396dc22 100644
--- a/src/protocol/ethernet.rs
+++ b/src/protocol/ethernet.rs
@@ -10,7 +10,7 @@ use nom::IResult;
pub struct MacAddress(pub [u8; 6]);
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
-pub enum EtherType {
+pub enum EthType {
LANMIN, // 802.3 Min data length
LANMAX, // 802.3 Max data length
IPv4, // Internet Protocol version 4 (IPv4) [RFC7042]
@@ -25,6 +25,7 @@ pub enum EtherType {
IPX, // IPX [Xerox]
Qnet, // QNX Qnet [QNX Software Systems]
IPv6, // Internet Protocol Version 6 (IPv6) [RFC7042]
+ PPP, // Point-to-Point Protocol (PPP) [RFC7042]
FlowControl, // Ethernet Flow Control [IEEE 802.3x]
CobraNet, // CobraNet [CobraNet]
MPLSuni, // MPLS Unicast [RFC 3032]
@@ -61,17 +62,17 @@ pub enum EtherType {
}
#[derive(Clone, Debug, PartialEq, Eq)]
-pub struct EthernetFrame {
+pub struct EthHeader {
pub source_mac: MacAddress,
pub dest_mac: MacAddress,
- pub ether_type: EtherType,
+ pub ether_type: EthType,
}
/******************************************************************************
* API
******************************************************************************/
-impl From<u16> for EtherType {
+impl From<u16> for EthType {
fn from(raw: u16) -> Self {
match raw {
0x002E => Self::LANMIN,
@@ -88,6 +89,7 @@ impl From<u16> for EtherType {
0x8137 => Self::IPX,
0x8204 => Self::Qnet,
0x86DD => Self::IPv6,
+ 0x880b => Self::PPP,
0x8808 => Self::FlowControl,
0x8819 => Self::CobraNet,
0x8847 => Self::MPLSuni,
@@ -125,15 +127,17 @@ impl From<u16> for EtherType {
}
}
-impl EtherType {
- pub fn decode(input: &[u8]) -> IResult<&[u8], EtherType> {
+impl Decode for EthType {
+ type Iterm = EthType;
+ fn decode(input: &[u8]) -> IResult<&[u8], EthType> {
let (input, ether_type) = number::streaming::be_u16(input)?;
Ok((input, ether_type.into()))
}
}
-impl MacAddress {
+impl Decode for MacAddress {
+ type Iterm = MacAddress;
fn decode(input: &[u8]) -> IResult<&[u8], MacAddress> {
let (input, mac_address) = nom::bytes::streaming::take(6u8)(input)?;
@@ -141,16 +145,16 @@ impl MacAddress {
}
}
-impl Decode for EthernetFrame {
- type Iterm = EthernetFrame;
- fn decode(input: &[u8]) -> IResult<&[u8], EthernetFrame> {
+impl Decode for EthHeader {
+ type Iterm = EthHeader;
+ fn decode(input: &[u8]) -> IResult<&[u8], EthHeader> {
let (input, dest_mac) = MacAddress::decode(input)?;
let (input, source_mac) = MacAddress::decode(input)?;
- let (input, ether_type) = EtherType::decode(input)?;
+ let (input, ether_type) = EthType::decode(input)?;
Ok((
input,
- EthernetFrame {
+ EthHeader {
source_mac,
dest_mac,
ether_type,
@@ -165,7 +169,7 @@ impl Decode for EthernetFrame {
#[cfg(test)]
mod tests {
- use super::{EtherType, EthernetFrame, MacAddress};
+ use super::{EthHeader, EthType, MacAddress};
use crate::protocol::codec::Decode;
const LAST_SLICE: &'static [u8] = &[0xff];
@@ -190,20 +194,25 @@ mod tests {
0xff, /* Payload */
];
- let expectation = EthernetFrame {
+ let expectation = EthHeader {
source_mac: MacAddress([0x3c, 0xa6, 0xf6, 0x0a, 0xc5, 0xea]),
dest_mac: MacAddress([0x4c, 0xbc, 0x98, 0x08, 0x02, 0xbe]),
- ether_type: EtherType::IPv4,
+ ether_type: EthType::IPv4,
};
- assert_eq!(EthernetFrame::decode(&bytes), Ok((LAST_SLICE, expectation)));
+ assert_eq!(EthHeader::decode(&bytes), Ok((LAST_SLICE, expectation)));
// example
- let ethernet = EthernetFrame::decode(&bytes);
- if let Ok((payload, header)) = ethernet {
- println!("return: {:?}, payload: {}", header, payload.len());
- } else {
- println!("return: Incomplete data");
+ let result = EthHeader::decode(&bytes);
+ match result {
+ Ok((payload, header)) => {
+ println!("OK: {:?}, payload: {}", header, payload.len());
+ }
+ Err(e) => {
+ println!("ERR: {:?}", e);
+ }
}
+
+ // assert_eq!(1, 0);
}
}