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/arp/ArpPacketParser.lvt

namespace Livt.Net

/**
 * Parses complete Ethernet/IPv4 ARP messages relative to their ARP header.
 * ARP has no checksum; trailing Ethernet padding is excluded.
 * Bind a protocol-relative provider. Invalidate children before this parser, then
 * release the source. Metadata is cached only for the current parse lifetime.
 */
component ArpPacketParser<S: IPacketData> : IPacketData
{
	public const HEADER_LENGTH: int = 28

	// 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(0) != 1 || header.GetWord(2) != IEthernetPayload.ETHER_TYPE_IPV4 || header.GetByte(4) != 6 || header.GetByte(5) != 4) {
			status = PacketParseResult.Unsupported
			return status
		}

		status = header.TryOpenPayload(HEADER_LENGTH, 0)
		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 Operation from the supported cached header; unavailable metadata returns 0.
	 */
	public fn GetOperation() int
	{
		if (!this.IsReady()) {
			return 0
		}

		return header.GetWord(6)
	}

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

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

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

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