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 .tcp(
15 21, //source port
16 1234, //desitnation port
17 1, //sequence number
18 26180, //window size
19 )
20 //set additional tcp header fields
21 //supported flags: ns(), fin(), syn(), rst(), psh(), ece(), cwr()
22 .ns() //set the ns flag
23 .ack(123) //ack flag + the ack number
24 .urg(23) //urg flag + urgent pointer
25 //tcp header options
26 .options(&[
27 TcpOptionElement::Noop,
28 TcpOptionElement::MaximumSegmentSize(1234),
29 ])
30 .unwrap();
31
32 //payload of the tcp packet
33 let payload = [1, 2, 3, 4, 5, 6, 7, 8];
34
35 //get some memory to store the result
36 let mut result = Vec::<u8>::with_capacity(builder.size(payload.len()));
37
38 //serialize
39 //this will automatically set all length fields, checksums and identifiers (ethertype & protocol)
40 builder.write(&mut result, &payload).unwrap();
41 println!("{:?}", result);
42 }
43