SatCat5
tcp_core.h
Go to the documentation of this file.
1 // Copyright 2025 The Aerospace Corporation.
3 // This file is a part of SatCat5, licensed under CERN-OHL-W v2 or later.
12 
13 #pragma once
14 
15 #include <satcat5/ip_core.h>
16 
17 namespace satcat5 {
18  namespace tcp {
19  // Alias for address and port types in the "tcp" namespace.
20  typedef satcat5::ip::Addr Addr;
21  typedef satcat5::ip::Port Port;
22 
25  constexpr unsigned HDR_MIN_WORDS = 5;
26  constexpr unsigned HDR_MIN_SHORTS = 2 * HDR_MIN_WORDS;
27  constexpr unsigned HDR_MIN_BYTES = 4 * HDR_MIN_WORDS;
28  constexpr unsigned HDR_MAX_WORDS = 15;
29  constexpr unsigned HDR_MAX_SHORTS = 2 * HDR_MAX_WORDS;
30  constexpr unsigned HDR_MAX_BYTES = 4 * HDR_MAX_WORDS;
32 
34  struct Header {
36  u16 data[HDR_MAX_SHORTS];
37 
38  // Explicitly declare default constructor and assignment methods.
39  Header() = default;
40  Header(const Header& t) = default;
41  Header& operator=(const Header& t) = default;
42 
43  // Accessors for specific sub-fields.
44  // https://en.wikipedia.org/wiki/Transmission_Control_Protocol#TCP_segment_structure
45  constexpr satcat5::tcp::Port src() const
46  { return satcat5::tcp::Port{data[0]}; }
47  constexpr satcat5::tcp::Port dst() const
48  { return satcat5::tcp::Port{data[1]}; }
49  constexpr unsigned ihl() const
50  { return (data[6] >> 12) & 0x0F; }
51  constexpr u16 chk() const
52  { return data[8]; }
53 
57  void chk_incr16(u16 prev, u16 next);
58  void chk_incr32(u32 prev, u32 next);
59 
61  void write_to(satcat5::io::Writeable* wr) const;
62 
71 
78  };
79  }
80 }
Abstract API for reading byte-streams and packets.
Definition: io_readable.h:68
Abstract API for writing byte-streams and packets.
Definition: io_writeable.h:24
IPv4 address is a 32-bit unsigned integer.
Definition: ip_core.h:15
UDP and TCP ports are both 16-bit unsigned integers.
Definition: ip_core.h:119
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
void chk_incr16(u16 prev, u16 next)
Incremental update to checksum by replacing a given field.
Definition: tcp_core.cc:12
constexpr satcat5::tcp::Port dst() const
< Destination port
Definition: tcp_core.h:47
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
constexpr satcat5::tcp::Port src() const
< Source port
Definition: tcp_core.h:45