Livt packages

Livt.Net

Version 1.4.1

Ethernet, IPv4, TCP, UDP, ARP, TFTP, and Telnet networking components for Livt hardware.

Published files (163)

src/tcp/TcpHeaderParser.lvt

namespace Livt.Net

/**
 * Parses TCP headers, including the extent of uninterpreted options.
 * Success validates the captured header, not the complete enclosing IP packet
 * or TCP checksum. Reserved/extended flags are unsupported.
 * Bind a protocol-relative provider. Invalidate children before this parser, then
 * release the source. Metadata is cached only for the current parse lifetime.
 */
component TcpHeaderParser<S: IPacketData> : IPacketData
{
	public const HEADER_LENGTH: int = 20

	// Owned header snapshot and payload view borrowing the constructor source.
	header: PacketHeader<S, HEADER_LENGTH>

	// Validation state for the current parse lifetime.
	status: PacketParseResult

	/**
	 * Binds the protocol input; construction performs no reads.
	 */
	new(source: S)
	{
		header = new PacketHeader<S, HEADER_LENGTH>(source)
	}

	/**
	 * Discards validated metadata and closes the borrowed payload view.
	 */
	public fn Invalidate()
	{
		status = PacketParseResult.NotReady
		header.Invalidate()
	}

	/**
	 * Parses once and replaces prior state even on failure. Checks structure,
	 * not endpoint policy or transport checksums.
	 */
	public fn TryParse() PacketParseResult
	{
		status = header.TryLoad()

		if (status != PacketParseResult.Success) {
			return status
		}

		var dataOffset = header.GetByte(12)
		var size = (((dataOffset as uint) >> 4) << 2) as int

		if (size < HEADER_LENGTH) {
			status = PacketParseResult.Invalid
			return status
		}

		if (((dataOffset as uint) & (0x0F as uint)) != (0 as uint)) {
			status = PacketParseResult.Unsupported
			return status
		}

		if (size > header.GetCapturedLength()) {
			status = PacketParseResult.Truncated
			return status
		}

		status = header.TryOpenPayload(size, header.GetCapturedLength() - size)
		return status
	}

	/**
	 * Returns current validation state; unavailable sources invalidate access.
	 */
	public fn GetStatus() PacketParseResult
	{
		if ((status == PacketParseResult.Success || status == PacketParseResult.HeaderOnly) && !header.IsReady()) {
			return PacketParseResult.NotReady
		}

		return status
	}

	/**
	 * Reports available supported metadata, including partial captures.
	 */
	override fn IsReady() bool
	{
		return (status == PacketParseResult.Success || status == PacketParseResult.HeaderOnly) && header.IsReady()
	}

	/**
	 * Returns bounded captured payload bytes, not declared or wire length.
	 */
	override fn GetAvailableLength() int
	{
		if (!this.IsReady()) {
			return 0
		}

		return header.GetAvailableLength()
	}

	/**
	 * Reads the borrowed payload relative to this protocol's header.
	 */
	override fn TryRead(index: int, value: out byte) PacketDataResult
	{
		value = 0x00

		if (!this.IsReady()) {
			return PacketDataResult.NotReady
		}

		var readValue: byte = 0x00
		var result = header.TryRead(index, readValue)
		value = readValue
		return result
	}

	/**
	 * Returns SourcePort from the supported cached header; unavailable metadata returns 0.
	 */
	public fn GetSourcePort() int
	{
		if (!this.IsReady()) {
			return 0
		}

		return header.GetWord(0)
	}

	/**
	 * Returns DestinationPort from the supported cached header; unavailable metadata returns 0.
	 */
	public fn GetDestinationPort() int
	{
		if (!this.IsReady()) {
			return 0
		}

		return header.GetWord(2)
	}

	/**
	 * Returns SequenceNumber from the supported cached header; unavailable metadata returns 0.
	 */
	public fn GetSequenceNumber() uint
	{
		if (!this.IsReady()) {
			return 0
		}

		return header.GetSequence(4)
	}

	/**
	 * Returns AcknowledgmentNumber from the supported cached header; unavailable metadata returns 0.
	 */
	public fn GetAcknowledgmentNumber() uint
	{
		if (!this.IsReady()) {
			return 0
		}

		return header.GetSequence(8)
	}

	/**
	 * Returns HeaderLength from the supported cached header; unavailable metadata returns 0.
	 */
	public fn GetHeaderLength() int
	{
		if (!this.IsReady()) {
			return 0
		}

		var offset = header.GetByte(12)
		return (((offset as uint) >> 4) << 2) as int
	}

	/**
	 * Returns Flags from the supported cached header; unavailable metadata returns 0.
	 */
	public fn GetFlags() byte
	{
		if (!this.IsReady()) {
			return 0
		}

		return header.GetByte(13)
	}
}