mirror of
https://github.com/zeek/zeek.git
synced 2025-10-06 00:28:21 +00:00

The Zeek code base has very inconsistent #includes. Many sources included a few headers, and those headers included other headers, and in the end, nearly everything is included everywhere, so missing #includes were never noticed. Another side effect was a lot of header bloat which slows down the build. First step to fix it: in each source file, its own header should be included first to verify that each header's includes are correct, and none is missing. After adding the missing #includes, I replaced lots of #includes inside headers with class forward declarations. In most headers, object pointers are never referenced, so declaring the function prototypes with forward-declared classes is just fine. This patch speeds up the build by 19%, because each compilation unit gets smaller. Here are the "time" numbers for a fresh build (with a warm page cache but without ccache): Before this patch: 3144.94user 161.63system 3:02.87elapsed 1808%CPU (0avgtext+0avgdata 2168608maxresident)k 760inputs+12008400outputs (1511major+57747204minor)pagefaults 0swaps After this patch: 2565.17user 141.83system 2:25.46elapsed 1860%CPU (0avgtext+0avgdata 1489076maxresident)k 72576inputs+9130920outputs (1667major+49400430minor)pagefaults 0swaps
117 lines
3 KiB
C++
117 lines
3 KiB
C++
#include "PacketFilter.h"
|
|
#include "IP.h"
|
|
|
|
void PacketFilter::DeleteFilter(void* data)
|
|
{
|
|
auto f = static_cast<Filter*>(data);
|
|
delete f;
|
|
}
|
|
|
|
PacketFilter::PacketFilter(bool arg_default)
|
|
{
|
|
default_match = arg_default;
|
|
src_filter.SetDeleteFunction(PacketFilter::DeleteFilter);
|
|
dst_filter.SetDeleteFunction(PacketFilter::DeleteFilter);
|
|
}
|
|
|
|
void PacketFilter::AddSrc(const IPAddr& src, uint32_t tcp_flags, double probability)
|
|
{
|
|
Filter* f = new Filter;
|
|
f->tcp_flags = tcp_flags;
|
|
f->probability = uint32_t(probability * RAND_MAX);
|
|
auto prev = static_cast<Filter*>(src_filter.Insert(src, 128, f));
|
|
delete prev;
|
|
}
|
|
|
|
void PacketFilter::AddSrc(Val* src, uint32_t tcp_flags, double probability)
|
|
{
|
|
Filter* f = new Filter;
|
|
f->tcp_flags = tcp_flags;
|
|
f->probability = uint32_t(probability * RAND_MAX);
|
|
auto prev = static_cast<Filter*>(src_filter.Insert(src, f));
|
|
delete prev;
|
|
}
|
|
|
|
void PacketFilter::AddDst(const IPAddr& dst, uint32_t tcp_flags, double probability)
|
|
{
|
|
Filter* f = new Filter;
|
|
f->tcp_flags = tcp_flags;
|
|
f->probability = uint32_t(probability * RAND_MAX);
|
|
auto prev = static_cast<Filter*>(dst_filter.Insert(dst, 128, f));
|
|
delete prev;
|
|
}
|
|
|
|
void PacketFilter::AddDst(Val* dst, uint32_t tcp_flags, double probability)
|
|
{
|
|
Filter* f = new Filter;
|
|
f->tcp_flags = tcp_flags;
|
|
f->probability = uint32_t(probability * RAND_MAX);
|
|
auto prev = static_cast<Filter*>(dst_filter.Insert(dst, f));
|
|
delete prev;
|
|
}
|
|
|
|
bool PacketFilter::RemoveSrc(const IPAddr& src)
|
|
{
|
|
auto f = static_cast<Filter*>(src_filter.Remove(src, 128));
|
|
delete f;
|
|
return f != nullptr;
|
|
}
|
|
|
|
bool PacketFilter::RemoveSrc(Val* src)
|
|
{
|
|
auto f = static_cast<Filter*>(src_filter.Remove(src));
|
|
delete f;
|
|
return f != nullptr;
|
|
}
|
|
|
|
bool PacketFilter::RemoveDst(const IPAddr& dst)
|
|
{
|
|
auto f = static_cast<Filter*>(dst_filter.Remove(dst, 128));
|
|
delete f;
|
|
return f != nullptr;
|
|
}
|
|
|
|
bool PacketFilter::RemoveDst(Val* dst)
|
|
{
|
|
auto f = static_cast<Filter*>(dst_filter.Remove(dst));
|
|
delete f;
|
|
return f != nullptr;
|
|
}
|
|
|
|
bool PacketFilter::Match(const IP_Hdr* ip, int len, int caplen)
|
|
{
|
|
Filter* f = (Filter*) src_filter.Lookup(ip->SrcAddr(), 128);
|
|
if ( f )
|
|
return MatchFilter(*f, *ip, len, caplen);
|
|
|
|
f = (Filter*) dst_filter.Lookup(ip->DstAddr(), 128);
|
|
if ( f )
|
|
return MatchFilter(*f, *ip, len, caplen);
|
|
|
|
return default_match;
|
|
}
|
|
|
|
bool PacketFilter::MatchFilter(const Filter& f, const IP_Hdr& ip,
|
|
int len, int caplen)
|
|
{
|
|
if ( ip.NextProto() == IPPROTO_TCP && f.tcp_flags )
|
|
{
|
|
// Caution! The packet sanity checks have not been performed yet
|
|
int ip_hdr_len = ip.HdrLen();
|
|
len -= ip_hdr_len; // remove IP header
|
|
caplen -= ip_hdr_len;
|
|
|
|
if ( (unsigned int) len < sizeof(struct tcphdr) ||
|
|
(unsigned int) caplen < sizeof(struct tcphdr) )
|
|
// Packet too short, will be dropped anyway.
|
|
return false;
|
|
|
|
const struct tcphdr* tp = (const struct tcphdr*) ip.Payload();
|
|
|
|
if ( tp->th_flags & f.tcp_flags )
|
|
// At least one of the flags is set, so don't drop
|
|
return false;
|
|
}
|
|
|
|
return uint32_t(bro_random()) < f.probability;
|
|
}
|