Table of Contents

Class TcpPacket

Namespace
VpnHood.Net.Packets
Assembly
VpnHood.Net.Packets.dll

Represents a TCP segment (RFC 9293) over a buffer, which it reads and writes in place.

public class TcpPacket : IChecksumPayloadPacket, IPayloadPacket
Inheritance
TcpPacket
Implements
Inherited Members

Remarks

Changing a field does not update the checksum: call UpdateChecksum(ReadOnlySpan<byte>, ReadOnlySpan<byte>) before sending.

Constructors

TcpPacket(Memory<byte>)

Initializes a new instance of the TcpPacket class by parsing a segment.

public TcpPacket(Memory<byte> buffer)

Parameters

buffer Memory<byte>

The segment's bytes, header and payload, exactly as long as the segment.

Exceptions

ArgumentException

buffer is shorter than 20 bytes, or the data offset gives a header shorter than 20 bytes or longer than the buffer.

TcpPacket(Memory<byte>, int)

Initializes a new instance of the TcpPacket class by building a segment.

public TcpPacket(Memory<byte> buffer, int optionsLength)

Parameters

buffer Memory<byte>

The segment's bytes, header and payload, exactly as long as the segment.

optionsLength int

The length of the options, a multiple of 4 from 0 to 40.

Remarks

The header and its options are cleared and the data offset is set; the payload is left as it is.

Exceptions

ArgumentOutOfRangeException

optionsLength is out of range or not a multiple of 4.

ArgumentException

buffer is shorter than the header with its options.

Properties

Acknowledgment

Gets or sets the ACK flag: AcknowledgmentNumber counts.

public bool Acknowledgment { get; set; }

Property Value

bool

AcknowledgmentNumber

Gets or sets the acknowledgment number, which counts only when Acknowledgment is set.

public uint AcknowledgmentNumber { get; set; }

Property Value

uint

Buffer

Gets the packet's bytes, header and payload. When the packet is an IP packet's payload, they are that packet's own bytes, not a copy, and live only as long as its memory.

public Memory<byte> Buffer { get; }

Property Value

Memory<byte>

Checksum

Gets or sets the checksum field as it is in the header.

public ushort Checksum { get; set; }

Property Value

ushort

DestinationPort

Gets or sets the destination port.

public ushort DestinationPort { get; set; }

Property Value

ushort

Finish

Gets or sets the FIN flag: the sender has no more data.

public bool Finish { get; set; }

Property Value

bool

Flags

Gets or sets the flags byte: CWR, ECE, URG, ACK, PSH, RST, SYN and FIN, from the highest bit.

public byte Flags { get; set; }

Property Value

byte

Options

Gets the options: the header's bytes after its fixed 20.

public Memory<byte> Options { get; }

Property Value

Memory<byte>

Payload

Gets the segment's payload: the bytes after the header and its options.

public Memory<byte> Payload { get; }

Property Value

Memory<byte>

Push

Gets or sets the PSH flag: the receiver should pass the data on at once.

public bool Push { get; set; }

Property Value

bool

Reset

Gets or sets the RST flag: the connection is reset.

public bool Reset { get; set; }

Property Value

bool

SequenceNumber

Gets or sets the sequence number.

public uint SequenceNumber { get; set; }

Property Value

uint

SourcePort

Gets or sets the source port.

public ushort SourcePort { get; set; }

Property Value

ushort

Synchronize

Gets or sets the SYN flag: the segment opens a connection.

public bool Synchronize { get; set; }

Property Value

bool

Urgent

Gets or sets the URG flag: UrgentPointer counts.

public bool Urgent { get; set; }

Property Value

bool

UrgentPointer

Gets or sets the urgent pointer, which counts only when Urgent is set.

public ushort UrgentPointer { get; set; }

Property Value

ushort

WindowSize

Gets or sets the window size, in bytes before any window scaling.

public ushort WindowSize { get; set; }

Property Value

ushort

Methods

ComputeChecksum(ReadOnlySpan<byte>, ReadOnlySpan<byte>)

Computes the checksum the segment calls for, without writing it.

public ushort ComputeChecksum(ReadOnlySpan<byte> sourceAddress, ReadOnlySpan<byte> destinationAddress)

Parameters

sourceAddress ReadOnlySpan<byte>

The source address of the IP packet, 4 or 16 bytes.

destinationAddress ReadOnlySpan<byte>

The destination address of the IP packet, of the same length.

Returns

ushort

The checksum, as a number to write in network byte order.

Remarks

The checksum field is zero while the sum runs and is restored after, so no other thread may use the packet meanwhile.

Exceptions

ArgumentException

The addresses are not both 4 or both 16 bytes long.

IsChecksumValid(ReadOnlySpan<byte>, ReadOnlySpan<byte>)

Checks the checksum field against the segment and the IP pseudo-header.

public bool IsChecksumValid(ReadOnlySpan<byte> sourceAddress, ReadOnlySpan<byte> destinationAddress)

Parameters

sourceAddress ReadOnlySpan<byte>

The source address of the IP packet, 4 or 16 bytes.

destinationAddress ReadOnlySpan<byte>

The destination address of the IP packet, of the same length.

Returns

bool

true if the checksum is valid; otherwise, false.

Exceptions

ArgumentException

The addresses are not both 4 or both 16 bytes long.

ToString()

Returns a one-line description of the segment for logs: its ports, sequence numbers, flags and payload length.

public override string ToString()

Returns

string

The description.

UpdateChecksum(ReadOnlySpan<byte>, ReadOnlySpan<byte>)

Computes the checksum and writes it to Checksum.

public void UpdateChecksum(ReadOnlySpan<byte> sourceAddress, ReadOnlySpan<byte> destinationAddress)

Parameters

sourceAddress ReadOnlySpan<byte>

The source address of the IP packet, 4 or 16 bytes.

destinationAddress ReadOnlySpan<byte>

The destination address of the IP packet, of the same length.

Exceptions

ArgumentException

The addresses are not both 4 or both 16 bytes long.