//..............................................................................
//
// Ethernet Tap log filter
//

import "log_Filter.jnc"
import "io_pcap.jncx"

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

class EthernetTapLogFilter: log.Filter {
protected:
	io.PcapFilter m_pcapFilter;

public:
	// suspend log engine before calling setFilter

	bool errorcode setFilter(
		io.Pcap* pcap,
		string_t filter,
		bool isOptimized = true,
		uint32_t netMask = -1
	) {
		return m_pcapFilter.compile(pcap, filter, isOptimized, netMask);
	}

	bool errorcode setFilter(
		string_t filter,
		bool isOptimized = true,
		uint32_t netMask = -1
	) {
		return m_pcapFilter.compile(filter, isOptimized, netMask);
	}

protected:
	override bool filter(
		uint64_t timestamp,
		uint64_t recordCode,
		void const* p,
		size_t size
	);
}

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

bool EthernetTapLogFilter.filter(
	uint64_t timestamp,
	uint64_t recordCode,
	void const* p,
	size_t size
) {
	switch (recordCode) {
	case EthernetTapLogRecordCode.Packet_ch1:
	case EthernetTapLogRecordCode.Packet_ch2:
		return m_pcapFilter.match(p + MetaSize, size - MetaSize);

	default:
		return true; // everything else is visible
	}
}

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