Skip to main content

Module data_link

Module data_link 

Source
Expand description

The DataLink module provides functionality to parse and analyze data link layer packets, specifically Ethernet frames. It extracts MAC addresses, Ethertype, and the payload from a raw byte slice.

§Overview

The DataLink structure represents an Ethernet frame with the following fields:

  • destination_mac: The destination MAC address of the packet as a string.
  • source_mac: The source MAC address of the packet as a string.
  • ethertype: The Ethertype value, which indicates the protocol used in the payload.
  • payload: The remaining packet data after the Ethernet header.

This module includes:

  • A TryFrom<&[u8]> implementation to parse an Ethernet frame from a raw byte slice.
  • A validation step to ensure the packet length is sufficient before parsing.

§Example

use packet_parser::parse::data_link::DataLink;

let raw_packet: [u8; 18] = [
    0x00, 0x11, 0x22, 0x33, 0x44, 0x55, // Destination MAC
    0x66, 0x77, 0x88, 0x99, 0xAA, 0xBB, // Source MAC
    0x08, 0x00, // Ethertype (IPv4)
    0x45, 0x00, 0x00, 0x54, // Payload (IPv4 Header fragment)
];

let datalink = DataLink::try_from(raw_packet.as_ref()).expect("Failed to parse valid packet");
println!("{:?}", datalink);

§Errors

The TryFrom<&[u8]> implementation can return a DataLinkError if:

  • The packet is too short to contain a valid Ethernet frame.
  • The MAC addresses or Ethertype are invalid.

§See Also

Modules§

ethertype
mac_addres
The MacAddress module provides a structured representation of MAC addresses and their associated Organizationally Unique Identifier (OUI).
vlan_tag

Structs§

DataLink
Represents a parsed Ethernet frame, containing source and destination MAC addresses, an Ethertype, and the payload.