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/ethernet/EthernetFrameParser.lvt

namespace Livt.Net

/**
 * Parses Ethernet II headers. HeaderOnly is intentional: IPacketData supplies
 * no original wire length or FCS validity. VLAN tags are not decoded here.
 * Bind a protocol-relative provider. Invalidate children before this parser, then
 * release the source. Metadata is cached only for the current parse lifetime.
 */
component EthernetFrameParser<S: IPacketData> : IPacketData
{
	public const HEADER_LENGTH: int = 14

	// 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
		}

		if (header.GetWord(12) < 1536) {
			status = PacketParseResult.Unsupported
			return status
		}

		status = header.TryOpenPayload(HEADER_LENGTH, header.GetCapturedLength() - HEADER_LENGTH)

		if (status == PacketParseResult.Success) {
			status = PacketParseResult.HeaderOnly
		}

		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 EtherType from the supported cached header; unavailable metadata returns 0.
	 */
	public fn GetEtherType() int
	{
		if (!this.IsReady()) {
			return 0
		}

		return header.GetWord(12)
	}

	/**
	 * Copies the cached SourceMac value; unavailable metadata returns zero bytes.
	 */
	public fn GetSourceMac() byte[6]
	{
		if (!this.IsReady()) { return [0x00, 0x00, 0x00, 0x00, 0x00, 0x00] }
		var word0 = header.GetWord(6)
		var word1 = header.GetWord(8)
		var word2 = header.GetWord(10)
		return [NetworkOrder.HighByte(word0), NetworkOrder.LowByte(word0), NetworkOrder.HighByte(word1), NetworkOrder.LowByte(word1), NetworkOrder.HighByte(word2), NetworkOrder.LowByte(word2)]
	}

	/**
	 * Copies the cached DestinationMac value; unavailable metadata returns zero bytes.
	 */
	public fn GetDestinationMac() byte[6]
	{
		if (!this.IsReady()) { return [0x00, 0x00, 0x00, 0x00, 0x00, 0x00] }
		var word0 = header.GetWord(0)
		var word1 = header.GetWord(2)
		var word2 = header.GetWord(4)
		return [NetworkOrder.HighByte(word0), NetworkOrder.LowByte(word0), NetworkOrder.HighByte(word1), NetworkOrder.LowByte(word1), NetworkOrder.HighByte(word2), NetworkOrder.LowByte(word2)]
	}
}