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/ipv4/Ipv4HeaderChecksum.lvt

namespace Livt.Net

/**
 * Pure checksum calculation for fixed IPv4 headers: version/IHL 0x45, DSCP=0,
 * DF=1, fragment offset=0 and TTL=64. No state or component instance is needed.
 */
class Ipv4HeaderChecksum
{
	/**
	 * Returns a 16-bit one's-complement checksum. totalLength is 20..65535 bytes;
	 * identification and addresses are network-order octets. The caller validates
	 * packet length before preparation. Protocol is the on-wire IPv4 octet.
	 */
	public static inline fn Calculate[](
		totalLength: int, identification: byte[2],
		sourceIp: byte[4], destinationIp: byte[4], protocol: byte) int
	{
		// Keep the adder depth logarithmic so the pure helper remains a short
		// combinational datapath when used inside a scheduled preparation step.
		var a = 0x4500 + totalLength
		var b = NetworkOrder.Word(identification[0], identification[1]) + 0x4000
		var c = 0x4000 + NetworkOrder.ByteValue(protocol)
		var d = NetworkOrder.Word(sourceIp[0], sourceIp[1]) + NetworkOrder.Word(sourceIp[2], sourceIp[3])
		var e = NetworkOrder.Word(destinationIp[0], destinationIp[1]) + NetworkOrder.Word(destinationIp[2], destinationIp[3])
		var left = a + b
		var right = c + d + e
		return ChecksumArithmetic.Finish(left + right)
	}
}