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
Exceptions
- ArgumentException
bufferis 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
bufferMemory<byte>The segment's bytes, header and payload, exactly as long as the segment.
optionsLengthintThe 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
optionsLengthis out of range or not a multiple of 4.- ArgumentException
bufferis shorter than the header with its options.
Properties
Acknowledgment
Gets or sets the ACK flag: AcknowledgmentNumber counts.
public bool Acknowledgment { get; set; }
Property Value
AcknowledgmentNumber
Gets or sets the acknowledgment number, which counts only when Acknowledgment is set.
public uint AcknowledgmentNumber { get; set; }
Property Value
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
Checksum
Gets or sets the checksum field as it is in the header.
public ushort Checksum { get; set; }
Property Value
DestinationPort
Gets or sets the destination port.
public ushort DestinationPort { get; set; }
Property Value
Finish
Gets or sets the FIN flag: the sender has no more data.
public bool Finish { get; set; }
Property Value
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
Options
Gets the options: the header's bytes after its fixed 20.
public Memory<byte> Options { get; }
Property Value
Payload
Gets the segment's payload: the bytes after the header and its options.
public Memory<byte> Payload { get; }
Property Value
Push
Gets or sets the PSH flag: the receiver should pass the data on at once.
public bool Push { get; set; }
Property Value
Reset
Gets or sets the RST flag: the connection is reset.
public bool Reset { get; set; }
Property Value
SequenceNumber
Gets or sets the sequence number.
public uint SequenceNumber { get; set; }
Property Value
SourcePort
Gets or sets the source port.
public ushort SourcePort { get; set; }
Property Value
Synchronize
Gets or sets the SYN flag: the segment opens a connection.
public bool Synchronize { get; set; }
Property Value
Urgent
Gets or sets the URG flag: UrgentPointer counts.
public bool Urgent { get; set; }
Property Value
UrgentPointer
Gets or sets the urgent pointer, which counts only when Urgent is set.
public ushort UrgentPointer { get; set; }
Property Value
WindowSize
Gets or sets the window size, in bytes before any window scaling.
public ushort WindowSize { get; set; }
Property Value
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
sourceAddressReadOnlySpan<byte>The source address of the IP packet, 4 or 16 bytes.
destinationAddressReadOnlySpan<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
sourceAddressReadOnlySpan<byte>The source address of the IP packet, 4 or 16 bytes.
destinationAddressReadOnlySpan<byte>The destination address of the IP packet, of the same length.
Returns
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
sourceAddressReadOnlySpan<byte>The source address of the IP packet, 4 or 16 bytes.
destinationAddressReadOnlySpan<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.