SatCat5
tcp_core.cc
1 // Copyright 2025 The Aerospace Corporation.
3 // This file is a part of SatCat5, licensed under CERN-OHL-W v2 or later.
5 
6 #include <satcat5/io_readable.h>
7 #include <satcat5/io_writeable.h>
8 #include <satcat5/tcp_core.h>
9 
11 
12 void Header::chk_incr16(u16 prev, u16 next) {
13  // Apply the ~m + m' method of RFC1624 Section 3.
14  u16 tmp[2] = {u16(~prev), next};
15  data[8] = satcat5::ip::checksum(2, tmp, chk());
16 }
17 
18 void Header::chk_incr32(u32 prev, u32 next) {
19  // Apply the ~m + m' method of RFC1624 Section 3.
20  u32 tmp[2] = {u32(~prev), next};
21  data[8] = satcat5::ip::checksum(4, (u16*)tmp, chk());
22 }
23 
25  unsigned hdr = 2 * ihl(); // Header length (16-bit words)
26  for (unsigned a = 0 ; a < hdr ; ++a)
27  wr->write_u16(data[a]); // Write each word in network order
28 }
29 
31  // Sanity check before we start.
32  if (rd->get_read_ready() < tcp::HDR_MIN_BYTES) return false;
33 
34  // Read each word in the "core" header (i.e., first 20 bytes).
35  for (unsigned a = 0 ; a < tcp::HDR_MIN_SHORTS ; ++a)
36  data[a] = rd->read_u16();
37 
38  // Sanity check the "data offset" field.
39  return ihl() >= tcp::HDR_MIN_WORDS;
40 }
41 
43  // Attempt to read the initial header (first 20 bytes).
44  if (!read_core(rd)) return false;
45 
46  // Bytes remaining in header options?
47  unsigned hdr = 2 * ihl(); // Header length (16-bit words)
48  unsigned rem = 2 * hdr - tcp::HDR_MIN_BYTES;
49  if (rd->get_read_ready() < rem) return false;
50 
51  // Read extended header options, if any.
52  for (unsigned a = tcp::HDR_MIN_SHORTS ; a < hdr ; ++a)
53  data[a] = rd->read_u16();
54 
55  return true;
56 }
Abstract API for reading byte-streams and packets.
Definition: io_readable.h:68
virtual unsigned get_read_ready() const =0
How many bytes can be read without blocking?
Abstract API for writing byte-streams and packets.
Definition: io_writeable.h:24
"Readable" I/O interface core definitions
"Writeable" I/O interface core definitions
TCP header contents.
Definition: tcp_core.h:34
void write_to(satcat5::io::Writeable *wr) const
Write TCP header to the designated stream.
Definition: tcp_core.cc:24
constexpr u16 chk() const
< Checksum (incoming only)
Definition: tcp_core.h:51
u16 data[HDR_MAX_SHORTS]
Raw access to the underlying header contents.
Definition: tcp_core.h:36
bool read_from(satcat5::io::Readable *rd)
Read TCP header from the designated stream.
Definition: tcp_core.cc:42
bool read_core(satcat5::io::Readable *rd)
Read a partial TCP header from the designated stream.
Definition: tcp_core.cc:30
constexpr unsigned ihl() const
< Header length (4-byte words)
Definition: tcp_core.h:49
Type definitions for manipulating TCP segments.