// $Id: PacketFilter.h,v 1.1 2003/08/31 00:38:11 vern Exp $
//
// Provides some very limited but fast packet filter mechanisms

#ifndef PACKETFILTER_H
#define PACKETFILTER_H

#include "IP.h"
#include "PrefixTable.h"

// Upper bound for packet-dropping probability.  (I.e., specifying
// MAX_PROB means "drop them all".
const int MAX_PROB = 100;

class PacketFilter {
public:
	PacketFilter()	{}
	~PacketFilter()	{}

	// Drops all packets from a particular source (which may be given
	// as an AddrVal or a SubnetVal) which hasn't any of TCP flags set
	// (TH_*) with the given probability (from 0..MAX_PROB).
	void AddSrc(addr_type src, uint32 tcp_flags, uint32 prob = MAX_PROB);
	void AddSrc(Val* src, uint32 tcp_flags, uint32 prob = MAX_PROB);
	void AddDst(addr_type src, uint32 tcp_flags, uint32 prob = MAX_PROB);
	void AddDst(Val* src, uint32 tcp_flags, uint32 prob = MAX_PROB);

	// Removes the filter entry for the given src/dst
	// Returns false if filter doesn not exist.
	bool RemoveSrc(addr_type src);
	bool RemoveSrc(Val* dst);
	bool RemoveDst(addr_type dst);
	bool RemoveDst(Val* dst);

	// Returns true if packet matches a drop filter
	bool Match(const IP_Hdr* ip, int len, int caplen);

private:
	struct Filter {
		uint32 tcp_flags;
		uint32 prob;
	};

	bool MatchFilter(const Filter& f, const IP_Hdr& ip, int len, int caplen);

	PrefixTable src_filter;
	PrefixTable dst_filter;
};

#endif
