JulianSchmid / etherparse

A rust library for parsing ethernet & ethernet using protocols.
Apache License 2.0
285 stars 54 forks source link

Any examples of working with ARP packets? #75

Closed gardc closed 10 months ago

gardc commented 10 months ago

I'm trying to work with ARP packets, but I can't find any examples or documentation for this. More specifically, I'm trying to get the opcode (1=request, 2=reply) of an ARP packet. Any tips?

JulianSchmid commented 10 months ago

Hi @gardc ,

Etherparse currently does not directly support parsing of ARP packets. But you can get ARP packets by parsing the ethernet header and checking for the ARP ether_type:

use etherparse::{Ethernet2Header, ether_type, ReadError::UnexpectedEndOfSlice};

let (eth_header, eth_payload) = Ethernet2Header::from_slice(eth_packet)?;
if ether_type::ARP == eth.ether_type {
    if eth_payload.len() < 8 {
        return Err(UnexpectedEndOfSlice(eth_header.header_len()));
    }
    let arp_opcode = u16::from_be_bytes([eth_payload[6], eth_payload[7]]);
}
gardc commented 10 months ago

Oh ok, thanks for the example!