summaryrefslogtreecommitdiff
path: root/src/protocol/ethernet.rs
diff options
context:
space:
mode:
authorluwenpeng <[email protected]>2023-09-22 15:30:04 +0800
committerluwenpeng <[email protected]>2023-09-22 16:16:44 +0800
commita01e8ffad311862e2ab3af61560dc74e42ae337d (patch)
tree8ab3da65e6307e3d20a7b8c3f5c49d035f76171c /src/protocol/ethernet.rs
parentafd40bfc655fa660513c5464e43655255655c53d (diff)
[refactor] Packet Decode: Protocol names are named in capital letters
Diffstat (limited to 'src/protocol/ethernet.rs')
-rw-r--r--src/protocol/ethernet.rs36
1 files changed, 19 insertions, 17 deletions
diff --git a/src/protocol/ethernet.rs b/src/protocol/ethernet.rs
index 6c42c2d..d3b9fc0 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]
@@ -62,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,
@@ -127,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)?;
@@ -143,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,
@@ -167,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];
@@ -192,16 +194,16 @@ 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);
+ let ethernet = EthHeader::decode(&bytes);
if let Ok((payload, header)) = ethernet {
println!("return: {:?}, payload: {}", header, payload.len());
} else {