tracert/packet/
tcp.rs

1use nex_packet::tcp::{MutableTcpPacket, TcpFlags, TcpOption};
2use std::net::IpAddr;
3
4pub fn build_tcp_packet(
5    tcp_packet: &mut MutableTcpPacket,
6    src_ip: IpAddr,
7    src_port: u16,
8    dst_ip: IpAddr,
9    dst_port: u16,
10) {
11    tcp_packet.set_source(src_port);
12    tcp_packet.set_destination(dst_port);
13    tcp_packet.set_window(64240);
14    tcp_packet.set_data_offset(8);
15    tcp_packet.set_urgent_ptr(0);
16    tcp_packet.set_sequence(0);
17    tcp_packet.set_options(&[
18        TcpOption::mss(1460),
19        TcpOption::sack_perm(),
20        TcpOption::nop(),
21        TcpOption::nop(),
22        TcpOption::wscale(7),
23    ]);
24    tcp_packet.set_flags(TcpFlags::SYN);
25    match src_ip {
26        IpAddr::V4(src_ip) => match dst_ip {
27            IpAddr::V4(dst_ip) => {
28                let checksum =
29                    nex_packet::tcp::ipv4_checksum(&tcp_packet.to_immutable(), &src_ip, &dst_ip);
30                tcp_packet.set_checksum(checksum);
31            }
32            IpAddr::V6(_) => {}
33        },
34        IpAddr::V6(src_ip) => match dst_ip {
35            IpAddr::V4(_) => {}
36            IpAddr::V6(dst_ip) => {
37                let checksum =
38                    nex_packet::tcp::ipv6_checksum(&tcp_packet.to_immutable(), &src_ip, &dst_ip);
39                tcp_packet.set_checksum(checksum);
40            }
41        },
42    }
43}