Skip to content

Instantly share code, notes, and snippets.

@ian-p-cooke
Created October 22, 2019 10:57
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ian-p-cooke/baccf3337295d8872ad83e6ccb58d9a4 to your computer and use it in GitHub Desktop.
Save ian-p-cooke/baccf3337295d8872ad83e6ccb58d9a4 to your computer and use it in GitHub Desktop.
// So this code looks wrong to me. What the correct way to "switch" on ethertype to get the next layer?
let packet = // some bytes from a PCAP file
let ether = EthernetPacket::new(&packet).expect("could not parse Ethernet frame");
// I don't know if the next line is valid; what if there's no Vlan layer and it doesn't parse ether.payload()?
let vlan = VlanPacket::new(ether.payload()).expect("could not parse Vlan frame");
let payload = if ether.get_ethertype() == ethernet::EtherTypes::Vlan {
vlan.payload()
} else {
ether.payload()
};
let ip = Ipv4Packet::new(payload).expect("could not parse Ipv4 frame");
let udp = UdpPacket::new(ip.payload()).expect("could not parse Udp frame");
let data = udp.payload();
// process `data`
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment