basic/
basic.rs

1extern crate rips_packets;
2
3use rips_packets::ethernet::{EthernetPacket, MacAddr, MutEthernetPacket, EtherType};
4
5fn main() {
6    // Allocate a byte buffer that hold the bytes in the Ethernet frame.
7    let mut buffer = [0; 14];
8
9    {
10        // Lend the buffer mutably to `MutEthernetPacket` so it can manipulate the
11        // header fields.
12        let mut ethernet_packet = MutEthernetPacket::new(&mut buffer[..])
13            .expect("Too short buffer");
14
15        // Use the setter methods to change the data in `buffer`
16        ethernet_packet.set_destination(MacAddr::BROADCAST);
17        ethernet_packet.set_source(MacAddr([0x01, 0x02, 0x03, 0x04, 0x05, 0x06]));
18        ethernet_packet.set_ether_type(EtherType::IPV4);
19
20        // When `ethernet_packet` goes out of scope, the mutable borrow of `buffer` ends
21        // and we can access the buffer again.
22    }
23
24    // Create an immutable representation of the ethernet frame based on the same
25    // buffer. Where a mutable `MutEthernetPacket` has setters `EthernetPacket` has the
26    // corresponding getters.
27    let packet = EthernetPacket::new(&buffer[..]).expect("Too short buffer");
28
29    println!("Destination MAC: {}", packet.destination());
30    println!("Source MAC: {}", packet.source());
31    println!("EtherType: {:?}", packet.ether_type());
32    println!("Packet data, including header: {:?}", packet.data())
33}