JFLib

netpkt/tcp.rs#

Source: lib/netpkt/tcp.rs

tcp -- the TCP header and its options (RFC 9293, 7323, 2018)

  TcpView::parse(b)     ports, sequence and acknowledgement numbers, flags, window, options,
                        payload; checksum_ok_v4 / _v6
  TcpOpts               MSS, window scale, SACK permitted, SACK blocks, timestamps
  TcpHdr { .. }         push_v4 / push_v6 (&mut Pkt) with the checksum; the options are
                        written in the usual order and padded with NOPs / EOL
  flags_str(flags)      "SYN, ACK"
  seq_lt / seq_le / seq_gt / seq_ge   comparisons in sequence space (modulo 2^32)
  port_name(port)       well-known names ("http", "ssh", ...)
TCP_MIN: usize = 20
TCP_MAX: usize = 60
FIN: u8 = 0x01
SYN: u8 = 0x02
RST: u8 = 0x04
PSH: u8 = 0x08
ACK: u8 = 0x10
URG: u8 = 0x20
ECE: u8 = 0x40
CWR: u8 = 0x80
OPT_EOL: u8 = 0
OPT_NOP: u8 = 1
OPT_MSS: u8 = 2
OPT_WSCALE: u8 = 3
OPT_SACK_OK: u8 = 4
OPT_SACK: u8 = 5
OPT_TS: u8 = 8
  • fn seq_lt(a: u32, b: u32) -> bool a < b in sequence space

  • fn seq_le(a: u32, b: u32) -> bool

  • fn seq_gt(a: u32, b: u32) -> bool

  • fn seq_ge(a: u32, b: u32) -> bool

  • struct TcpOpts the TCP options this library understands

    • mss: Option<u16>
    • wscale: Option<u8>
    • sack_ok: bool
    • sack: [(u32, u32); 4] -- SACK blocks (left, right edge); sack_n of them are valid
    • sack_n: u8
    • ts: Option<(u32, u32)> -- timestamp value and echo reply

impl TcpOpts

  • fn parse(b: &[u8]) -> TcpOpts parses an option block; unknown options are skipped, a malformed one ends the walk

  • fn len(&self) -> usize the encoded length, padded to a multiple of 4

  • fn is_empty(&self) -> bool

  • fn write(&self, out: &mut [u8]) writes the options (MSS, SACK-permitted, timestamps, window scale, SACK), NOP padding

  • struct TcpView<'a> a received TCP segment

impl<'a> TcpView<'a>

  • fn parse(b: &'a [u8]) -> Option<TcpView<'a>>

  • fn src_port(&self) -> u16

  • fn dst_port(&self) -> u16

  • fn seq(&self) -> u32

  • fn ack(&self) -> u32

  • fn header_len(&self) -> usize

  • fn flags(&self) -> u8

  • fn has(&self, f: u8) -> bool

  • fn window(&self) -> u16

  • fn checksum(&self) -> u16

  • fn urgent(&self) -> u16

  • fn options_raw(&self) -> &'a [u8]

  • fn opts(&self) -> TcpOpts

  • fn payload(&self) -> &'a [u8]

  • fn bytes(&self) -> &'a [u8]

  • fn seg_len(&self) -> u32 the sequence space the segment occupies: payload length + SYN + FIN

  • fn checksum_ok_v4(&self, src: Ipv4, dst: Ipv4) -> bool

  • fn checksum_ok_v6(&self, src: Ipv6, dst: Ipv6) -> bool

  • struct TcpHdr a TCP header to write

    • src_port: u16
    • dst_port: u16
    • seq: u32
    • ack: u32
    • flags: u8
    • window: u16
    • urgent: u16
    • opts: TcpOpts

impl TcpHdr

  • fn read(v: &TcpView) -> TcpHdr

  • fn len(&self) -> usize

  • fn push_v4(&self, p: &mut Pkt, src: Ipv4, dst: Ipv4) prepends the header (the packet's content is the payload) with the IPv4 checksum

  • fn push_v6(&self, p: &mut Pkt, src: Ipv6, dst: Ipv6) the same with the IPv6 pseudo header

  • fn flags_str(f: u8) -> String "SYN, ACK" (Wireshark order: FIN SYN RST PSH ACK URG ECE CWR)

  • fn port_name(port: u16) -> Option<&'static str> a short name of a well-known TCP port

Source: docs/API.md in the JFLib repository.