//..............................................................................
//
// Sniffer log ethernet packets handling
//

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

char const* formatEthernetString(uint8_t const* ethernet)
{
	return std.format(
		"%02X:%02X:%02X:%02X:%02X:%02X",
		ethernet[0], ethernet[1], ethernet[2],
		ethernet[3], ethernet[4], ethernet[5]
		);
}

representEthernet(
	log.RepresenterTarget* target,
	void const* p,
	size_t size,
	uint_t foldFlags
	)
{
	io.EthernetHdr const* ethernetHdr = (io.EthernetHdr const*) p;

	target.m_lineAttr.m_backColor = gui.StdColor.PastelGray;

	if (size < sizeof(io.EthernetHdr))
	{
		target.addHyperText("\e[1mBAD FRAME");

		target.m_lineAttr.m_textColor = gui.StdColor.Gray;
		target.m_lineAttr.m_backColor = gui.ColorFlags.Transparent;
		target.addBin(p, size);
		return;
	}

	if (foldFlags)
	{
		target.addHyperText(std.format(
			"[\e^-1\e[34m-\e[m]\n"
			"\e[1mETHERNET\n"
			"   \e[90mTYPE: \e^!%d,%d\e[34m%04Xh\n"
			"   \e[90mSRC:  \e^!%d,%d\e[34m%s\n"
			"   \e[90mDST:  \e^!%d,%d\e[34m%s",

			offsetof(ethernetHdr.m_type),
			sizeof(ethernetHdr.m_type),
			ethernetHdr.m_type,

			offsetof(ethernetHdr.m_srcAddress),
			sizeof(ethernetHdr.m_srcAddress),
			formatEthernetString(ethernetHdr.m_srcAddress),

			offsetof(ethernetHdr.m_dstAddress),
			sizeof(ethernetHdr.m_dstAddress),
			formatEthernetString(ethernetHdr.m_dstAddress)
			));

		size_t hdrSizeTable[8] = { sizeof(io.EthernetHdr) }
		uint_t hdrColorTable[8] = { SnifferLogHdrColor.Ethernet }
		size_t hdrCount = 1;

		switch (ethernetHdr.m_type)
		{
		case io.EthernetType.Ip:
			hdrCount = representIpHdr(target, p, size, sizeof(io.EthernetHdr), hdrSizeTable, hdrColorTable, 1);
			break;

		case io.EthernetType.Arp:
			hdrCount = representArpHdr(target, p, size, sizeof(io.EthernetHdr), hdrSizeTable, hdrColorTable, 1);
			break;
		}

		representColorizedBin(target, p, size, hdrSizeTable, hdrColorTable, hdrCount);
	}
	else
	{
		switch (ethernetHdr.m_type)
		{
		case io.EthernetType.Ip:
			representIp(target, p, size, sizeof(io.EthernetHdr));
			return;

		case io.EthernetType.Arp:
			representArp(target, p, size, sizeof(io.EthernetHdr));
			return;
		}

		target.addHyperText(std.format(
			"[\e^+1\e[34m+\e[m] \e[1mETHERNET \e[10;34m%s\e[m > \e[34m%s\e[m [TYPE:%04Xh]",
			formatEthernetString(ethernetHdr.m_srcAddress),
			formatEthernetString(ethernetHdr.m_dstAddress),
			ethernetHdr.m_type
			));

		representPacketData(target, p, size, sizeof(io.EthernetHdr), -1);
	}
}

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