//..............................................................................
//
// Sniffer log UDP packets handling
//

//..............................................................................

size_t
representUdpHdr(
	log.RepresenterTarget* target,
	void const* p,
	size_t size,
	size_t baseOffset,
	size_t* hdrSizeTable,
	uint_t* hdrColorTable,
	size_t hdrIdx
	)
{
	io.UdpHdr const* udpHdr = (io.UdpHdr const*) (p + baseOffset);

	target.addHyperText(std.format(
		"\e[1mUDP\n"
		"   \e[90mSRC:      \e^!%d,%d\e[34m%d\n"
		"   \e[90mDST:      \e^!%d,%d\e[34m%d\n"
		"   \e[90mLENGTH:   \e^!%d,%d\e[34m%d\n"
		"   \e[90mCHECKSUM: \e^!%d,%d\e[34m%04Xh",

		baseOffset + offsetof(udpHdr.m_srcPort),
		sizeof(udpHdr.m_srcPort),
		udpHdr.m_srcPort,

		baseOffset + offsetof(udpHdr.m_dstPort),
		sizeof(udpHdr.m_dstPort),
		udpHdr.m_dstPort,

		baseOffset + offsetof(udpHdr.m_length),
		sizeof(udpHdr.m_length),
		udpHdr.m_length,

		baseOffset + offsetof(udpHdr.m_checksum),
		sizeof(udpHdr.m_checksum),
		udpHdr.m_checksum
		));

	hdrSizeTable[hdrIdx] = sizeof(io.UdpHdr);
	hdrColorTable[hdrIdx] = SnifferLogHdrColor.Udp;
	return hdrIdx + 1;
}

representUdp(
	log.RepresenterTarget* target,
	io.IpHdr const* ipHdr,
	void const* p,
	size_t size,
	size_t baseOffset
	)
{
	if (baseOffset + sizeof(io.UdpHdr) > size)
	{
		target.addHyperText(std.format(
			"[\e^+1\e[34m+\e[m] \e[1mUDP \e[10;34m%s\e[m > \e[34m%s\e[m [INVALID SIZE:%d]",
			formatIpString(ipHdr.m_srcAddress),
			formatIpString(ipHdr.m_dstAddress),
			size
			));

		return;
	}

	io.UdpHdr const* udpHdr = (io.UdpHdr const*) (p + baseOffset);
	size_t dataSize = udpHdr.m_length - sizeof(io.UdpHdr);
	baseOffset += sizeof(io.UdpHdr);

	char const* lengthString;

	if (baseOffset + dataSize <= size)
	{
		lengthString = $"$dataSize bytes";
	}
	else
	{
		lengthString = $"INVALID SIZE: $dataSize bytes";
		dataSize = size - baseOffset;
	}

	target.addHyperText(std.format(
		"[\e^+1\e[34m+\e[m] \e[1mUDP \e[10;34m%s:%d\e[m > \e[34m%s:%d\e[m (%s)",
		formatIpString(ipHdr.m_srcAddress),
		udpHdr.m_srcPort,
		formatIpString(ipHdr.m_dstAddress),
		udpHdr.m_dstPort,
		lengthString
		));

	representPacketData(target, p, size, baseOffset, dataSize);
}

//..............................................................................
