1 use etherparse::*;
2 
main()3 fn main() {
4     //setup the packet headers
5     let builder = PacketBuilder::ethernet2(
6         [1, 2, 3, 4, 5, 6],    //source mac
7         [7, 8, 9, 10, 11, 12], //destination mac
8     )
9     .ipv4(
10         [192, 168, 1, 1], //source ip
11         [192, 168, 1, 2], //destination ip
12         20,               //time to life
13     )
14     .udp(
15         21,   //source port
16         1234, //desitnation port
17     );
18 
19     //payload of the udp packet
20     let payload = [1, 2, 3, 4, 5, 6, 7, 8];
21 
22     //get some memory to store the result
23     let mut result = Vec::<u8>::with_capacity(builder.size(payload.len()));
24 
25     //serialize
26     //this will automatically set all length fields, checksums and identifiers (ethertype & protocol)
27     builder.write(&mut result, &payload).unwrap();
28     println!("{:?}", result);
29 }
30