UID, ..: un-inline methods to reduce header dependencies

Only 1% build time speedup, but still, it declutters the headers a bit.

Before this patch:

 2565.17user 141.83system 2:25.46elapsed 1860%CPU (0avgtext+0avgdata 1489076maxresident)k
 72576inputs+9130920outputs (1667major+49400430minor)pagefaults 0swaps

After this patch:

 2537.19user 142.94system 2:26.90elapsed 1824%CPU (0avgtext+0avgdata 1434268maxresident)k
 16240inputs+8887152outputs (1931major+48728888minor)pagefaults 0swaps
This commit is contained in:
Max Kellermann 2020-02-02 16:29:27 +01:00
parent 0db61f3094
commit 6a815b4b06
38 changed files with 348 additions and 211 deletions

View file

@ -9,6 +9,7 @@
#include "net_util.h" #include "net_util.h"
#include "Val.h" #include "Val.h"
#include "NetVar.h" #include "NetVar.h"
#include "Reporter.h"
AnonymizeIPAddr* ip_anonymizer[NUM_ADDR_ANONYMIZATION_METHODS] = {0}; AnonymizeIPAddr* ip_anonymizer[NUM_ADDR_ANONYMIZATION_METHODS] = {0};
@ -67,6 +68,13 @@ ipaddr32_t AnonymizeIPAddr::Anonymize(ipaddr32_t addr)
} }
} }
// Keep the specified prefix unchanged.
int AnonymizeIPAddr::PreservePrefix(ipaddr32_t /* input */, int /* num_bits */)
{
reporter->InternalError("prefix preserving is not supported for the anonymizer");
return 0;
}
int AnonymizeIPAddr::PreserveNet(ipaddr32_t input) int AnonymizeIPAddr::PreserveNet(ipaddr32_t input)
{ {
switch ( addr_to_class(ntohl(input)) ) { switch ( addr_to_class(ntohl(input)) ) {

View file

@ -13,8 +13,6 @@
#include <vector> #include <vector>
#include <map> #include <map>
#include "Reporter.h"
using std::map; using std::map;
// TODO: Anon.h may not be the right place to put these functions ... // TODO: Anon.h may not be the right place to put these functions ...
@ -46,12 +44,7 @@ public:
ipaddr32_t Anonymize(ipaddr32_t addr); ipaddr32_t Anonymize(ipaddr32_t addr);
// Keep the specified prefix unchanged. virtual int PreservePrefix(ipaddr32_t input, int num_bits);
virtual int PreservePrefix(ipaddr32_t /* input */, int /* num_bits */)
{
reporter->InternalError("prefix preserving is not supported for the anonymizer");
return 0;
}
virtual ipaddr32_t anonymize(ipaddr32_t addr) = 0; virtual ipaddr32_t anonymize(ipaddr32_t addr) = 0;

View file

@ -1,6 +1,8 @@
#include "zeek-config.h" #include "zeek-config.h"
#include "Base64.h" #include "Base64.h"
#include "BroString.h" #include "BroString.h"
#include "Reporter.h"
#include "Conn.h"
#include <math.h> #include <math.h>
@ -217,6 +219,14 @@ int Base64Converter::Done(int* pblen, char** pbuf)
return 0; return 0;
} }
void Base64Converter::IllegalEncoding(const char* msg)
{
// strncpy(error_msg, msg, sizeof(error_msg));
if ( conn )
conn->Weird("base64_illegal_encoding", msg);
else
reporter->Error("%s", msg);
}
BroString* decode_base64(const BroString* s, const BroString* a, Connection* conn) BroString* decode_base64(const BroString* s, const BroString* a, Connection* conn)
{ {
@ -268,4 +278,3 @@ BroString* encode_base64(const BroString* s, const BroString* a, Connection* con
return new BroString(1, (u_char*)outbuf, outlen); return new BroString(1, (u_char*)outbuf, outlen);
} }

View file

@ -1,9 +1,11 @@
#pragma once #pragma once
#include "Reporter.h" #include <string>
#include "Conn.h"
using std::string;
class BroString; class BroString;
class Connection;
// Maybe we should have a base class for generic decoders? // Maybe we should have a base class for generic decoders?
class Base64Converter { class Base64Converter {
@ -36,14 +38,7 @@ public:
int Errored() const { return errored; } int Errored() const { return errored; }
const char* ErrorMsg() const { return error_msg; } const char* ErrorMsg() const { return error_msg; }
void IllegalEncoding(const char* msg) void IllegalEncoding(const char* msg);
{
// strncpy(error_msg, msg, sizeof(error_msg));
if ( conn )
conn->Weird("base64_illegal_encoding", msg);
else
reporter->Error("%s", msg);
}
protected: protected:
char error_msg[256]; char error_msg[256];

View file

@ -10,6 +10,7 @@
#include "Val.h" #include "Val.h"
#include "Var.h" #include "Var.h"
#include "Reporter.h" #include "Reporter.h"
#include "util.h"
#ifdef DEBUG #ifdef DEBUG
#define DEBUG_STR(msg) DBG_LOG(DBG_STRING, msg) #define DEBUG_STR(msg) DBG_LOG(DBG_STRING, msg)
@ -274,6 +275,11 @@ void BroString::ToUpper()
b[i] = toupper(b[i]); b[i] = toupper(b[i]);
} }
unsigned int BroString::MemoryAllocation() const
{
return padded_sizeof(*this) + pad_size(n + final_NUL);
}
BroString* BroString::GetSubstring(int start, int len) const BroString* BroString::GetSubstring(int start, int len) const
{ {
// This code used to live in zeek.bif's sub_bytes() routine. // This code used to live in zeek.bif's sub_bytes() routine.

View file

@ -2,8 +2,6 @@
#pragma once #pragma once
#include "util.h"
#include <vector> #include <vector>
#include <string> #include <string>
#include <iosfwd> #include <iosfwd>
@ -114,8 +112,7 @@ public:
// XXX and to_upper; the latter doesn't use BroString::ToUpper(). // XXX and to_upper; the latter doesn't use BroString::ToUpper().
void ToUpper(); void ToUpper();
unsigned int MemoryAllocation() const unsigned int MemoryAllocation() const;
{ return padded_sizeof(*this) + pad_size(n + final_NUL); }
// Returns new string containing the substring of this string, // Returns new string containing the substring of this string,
// starting at @start >= 0 for going up to @length elements, // starting at @start >= 0 for going up to @length elements,

View file

@ -43,3 +43,8 @@ void CCL::Sort()
{ {
std::sort(syms->begin(), syms->end()); std::sort(syms->begin(), syms->end());
} }
unsigned int CCL::MemoryAllocation() const
{
return padded_sizeof(*this) + padded_sizeof(*syms) + pad_size(syms->size() * sizeof(int_list::value_type));
}

View file

@ -25,8 +25,7 @@ public:
void ReplaceSyms(int_list* new_syms) void ReplaceSyms(int_list* new_syms)
{ delete syms; syms = new_syms; } { delete syms; syms = new_syms; }
unsigned int MemoryAllocation() const unsigned int MemoryAllocation() const;
{ return padded_sizeof(*this) + padded_sizeof(*syms) + pad_size(syms->size() * sizeof(int_list::value_type)); }
protected: protected:
int_list* syms; int_list* syms;

View file

@ -7,6 +7,7 @@
#include "Func.h" #include "Func.h"
#include "NetVar.h" #include "NetVar.h"
#include "Trigger.h" #include "Trigger.h"
#include "Val.h"
#include "plugin/Manager.h" #include "plugin/Manager.h"
EventMgr mgr; EventMgr mgr;
@ -102,6 +103,19 @@ EventMgr::~EventMgr()
Unref(src_val); Unref(src_val);
} }
void EventMgr::QueueEvent(const EventHandlerPtr &h, val_list vl,
SourceID src, analyzer::ID aid,
TimerMgr* mgr, BroObj* obj)
{
if ( h )
QueueEvent(new Event(h, std::move(vl), src, aid, mgr, obj));
else
{
for ( const auto& v : vl )
Unref(v);
}
}
void EventMgr::QueueEvent(Event* event) void EventMgr::QueueEvent(Event* event)
{ {
bool done = PLUGIN_HOOK_WITH_RESULT(HOOK_QUEUE_EVENT, HookQueueEvent(event), false); bool done = PLUGIN_HOOK_WITH_RESULT(HOOK_QUEUE_EVENT, HookQueueEvent(event), false);
@ -120,6 +134,13 @@ void EventMgr::QueueEvent(Event* event)
++num_events_queued; ++num_events_queued;
} }
void EventMgr::Dispatch(Event* event, bool no_remote)
{
current_src = event->Source();
event->Dispatch(no_remote);
Unref(event);
}
void EventMgr::Drain() void EventMgr::Drain()
{ {
if ( event_queue_flush_point ) if ( event_queue_flush_point )

View file

@ -2,8 +2,8 @@
#pragma once #pragma once
#include "BroList.h"
#include "analyzer/Analyzer.h" #include "analyzer/Analyzer.h"
#include "Val.h"
class EventMgr; class EventMgr;
@ -77,16 +77,7 @@ public:
// existence check. // existence check.
void QueueEvent(const EventHandlerPtr &h, val_list vl, void QueueEvent(const EventHandlerPtr &h, val_list vl,
SourceID src = SOURCE_LOCAL, analyzer::ID aid = 0, SourceID src = SOURCE_LOCAL, analyzer::ID aid = 0,
TimerMgr* mgr = 0, BroObj* obj = 0) TimerMgr* mgr = 0, BroObj* obj = 0);
{
if ( h )
QueueEvent(new Event(h, std::move(vl), src, aid, mgr, obj));
else
{
for ( const auto& v : vl )
Unref(v);
}
}
// Same as QueueEvent, except taking the event's argument list via a // Same as QueueEvent, except taking the event's argument list via a
// pointer instead of by value. This function takes ownership of the // pointer instead of by value. This function takes ownership of the
@ -100,12 +91,7 @@ public:
delete vl; delete vl;
} }
void Dispatch(Event* event, bool no_remote = false) void Dispatch(Event* event, bool no_remote = false);
{
current_src = event->Source();
event->Dispatch(no_remote);
Unref(event);
}
void Drain(); void Drain();
bool IsDraining() const { return draining; } bool IsDraining() const { return draining; }

View file

@ -135,12 +135,32 @@ Val* Expr::InitVal(const BroType* t, Val* aggr) const
return check_and_promote(Eval(0), t, 1); return check_and_promote(Eval(0), t, 1);
} }
int Expr::IsError() const
{
return type && type->Tag() == TYPE_ERROR;
}
void Expr::SetError()
{
SetType(error_type());
}
void Expr::SetError(const char* msg) void Expr::SetError(const char* msg)
{ {
Error(msg); Error(msg);
SetError(); SetError();
} }
int Expr::IsZero() const
{
return IsConst() && ExprVal()->IsZero();
}
int Expr::IsOne() const
{
return IsConst() && ExprVal()->IsOne();
}
void Expr::Describe(ODesc* d) const void Expr::Describe(ODesc* d) const
{ {
if ( IsParen() && ! d->IsBinary() ) if ( IsParen() && ! d->IsBinary() )
@ -2079,6 +2099,11 @@ AssignExpr::AssignExpr(Expr* arg_op1, Expr* arg_op2, int arg_is_init,
SetLocationInfo(arg_op1->GetLocationInfo(), arg_op2->GetLocationInfo()); SetLocationInfo(arg_op1->GetLocationInfo(), arg_op2->GetLocationInfo());
} }
AssignExpr::~AssignExpr()
{
Unref(val);
}
bool AssignExpr::TypeCheck(attr_list* attrs) bool AssignExpr::TypeCheck(attr_list* attrs)
{ {
TypeTag bt1 = op1->Type()->Tag(); TypeTag bt1 = op1->Type()->Tag();

View file

@ -7,7 +7,6 @@
#include "BroList.h" #include "BroList.h"
#include "Timer.h" #include "Timer.h"
#include "Type.h" #include "Type.h"
#include "Val.h"
#include "EventHandler.h" #include "EventHandler.h"
#include "TraverseTypes.h" #include "TraverseTypes.h"
@ -119,10 +118,10 @@ public:
int IsConst() const { return tag == EXPR_CONST; } int IsConst() const { return tag == EXPR_CONST; }
// True if the expression is in error (to alleviate error propagation). // True if the expression is in error (to alleviate error propagation).
int IsError() const { return type && type->Tag() == TYPE_ERROR; } int IsError() const;
// Mark expression as in error. // Mark expression as in error.
void SetError() { SetType(error_type()); } void SetError();
void SetError(const char* msg); void SetError(const char* msg);
// Returns the expression's constant value, or complains // Returns the expression's constant value, or complains
@ -130,16 +129,10 @@ public:
inline Val* ExprVal() const; inline Val* ExprVal() const;
// True if the expression is a constant zero, false otherwise. // True if the expression is a constant zero, false otherwise.
int IsZero() const int IsZero() const;
{
return IsConst() && ExprVal()->IsZero();
}
// True if the expression is a constant one, false otherwise. // True if the expression is a constant one, false otherwise.
int IsOne() const int IsOne() const;
{
return IsConst() && ExprVal()->IsOne();
}
// True if the expression supports the "add" or "delete" operations, // True if the expression supports the "add" or "delete" operations,
// false otherwise. // false otherwise.
@ -605,7 +598,7 @@ public:
// If val is given, evaluating this expression will always yield the val // If val is given, evaluating this expression will always yield the val
// yet still perform the assignment. Used for triggers. // yet still perform the assignment. Used for triggers.
AssignExpr(Expr* op1, Expr* op2, int is_init, Val* val = 0, attr_list* attrs = 0); AssignExpr(Expr* op1, Expr* op2, int is_init, Val* val = 0, attr_list* attrs = 0);
~AssignExpr() override { Unref(val); } ~AssignExpr() override;
Val* Eval(Frame* f) const override; Val* Eval(Frame* f) const override;
void EvalIntoAggregate(const BroType* t, Val* aggr, Frame* f) const override; void EvalIntoAggregate(const BroType* t, Val* aggr, Frame* f) const override;

View file

@ -9,6 +9,7 @@
#include "Desc.h" #include "Desc.h"
#include "IntrusivePtr.h" #include "IntrusivePtr.h"
#include "Trigger.h" #include "Trigger.h"
#include "Val.h"
vector<Frame*> g_frame_stack; vector<Frame*> g_frame_stack;
@ -535,6 +536,14 @@ void Frame::ClearTrigger()
trigger = nullptr; trigger = nullptr;
} }
void Frame::UnrefElement(int n)
{
if ( weak_refs && weak_refs[n] )
return;
Unref(frame[n]);
}
bool Frame::IsOuterID(const ID* in) const bool Frame::IsOuterID(const ID* in) const
{ {
return std::any_of(outer_ids.begin(), outer_ids.end(), return std::any_of(outer_ids.begin(), outer_ids.end(),

View file

@ -3,7 +3,7 @@
#pragma once #pragma once
#include "BroList.h" // for typedef val_list #include "BroList.h" // for typedef val_list
#include "Val.h" #include "Obj.h"
#include <unordered_map> #include <unordered_map>
#include <string> #include <string>
@ -235,13 +235,7 @@ private:
/** /**
* Unrefs the value at offset 'n' frame unless it's a weak reference. * Unrefs the value at offset 'n' frame unless it's a weak reference.
*/ */
void UnrefElement(int n) void UnrefElement(int n);
{
if ( weak_refs && weak_refs[n] )
return;
Unref(frame[n]);
}
/** Have we captured this id? */ /** Have we captured this id? */
bool IsOuterID(const ID* in) const; bool IsOuterID(const ID* in) const;

View file

@ -3,12 +3,14 @@
#include "zeek-config.h" #include "zeek-config.h"
#include "ID.h" #include "ID.h"
#include "Attr.h"
#include "Desc.h" #include "Desc.h"
#include "Expr.h" #include "Expr.h"
#include "Dict.h" #include "Dict.h"
#include "EventRegistry.h" #include "EventRegistry.h"
#include "Func.h" #include "Func.h"
#include "Scope.h" #include "Scope.h"
#include "Type.h"
#include "File.h" #include "File.h"
#include "Scope.h" #include "Scope.h"
#include "Traverse.h" #include "Traverse.h"
@ -56,6 +58,11 @@ string ID::ModuleName() const
return extract_module_name(name); return extract_module_name(name);
} }
void ID::SetType(BroType* t)
{
Unref(type); type = t;
}
void ID::ClearVal() void ID::ClearVal()
{ {
if ( ! weak_ref ) if ( ! weak_ref )
@ -148,6 +155,11 @@ void ID::SetVal(Expr* ev, init_class c)
EvalFunc(a->AttrExpr(), ev); EvalFunc(a->AttrExpr(), ev);
} }
bool ID::IsRedefinable() const
{
return FindAttr(ATTR_REDEF) != 0;
}
void ID::SetAttrs(Attributes* a) void ID::SetAttrs(Attributes* a)
{ {
Unref(attrs); Unref(attrs);
@ -194,6 +206,16 @@ void ID::UpdateValAttrs()
} }
} }
Attr* ID::FindAttr(attr_tag t) const
{
return attrs ? attrs->FindAttr(t) : 0;
}
bool ID::IsDeprecated() const
{
return FindAttr(ATTR_DEPRECATED) != 0;
}
void ID::MakeDeprecated(Expr* deprecation) void ID::MakeDeprecated(Expr* deprecation)
{ {
if ( IsDeprecated() ) if ( IsDeprecated() )

View file

@ -3,7 +3,6 @@
#pragma once #pragma once
#include "Obj.h" #include "Obj.h"
#include "Type.h"
#include "Attr.h" #include "Attr.h"
#include "Notifier.h" #include "Notifier.h"
#include "TraverseTypes.h" #include "TraverseTypes.h"
@ -36,7 +35,7 @@ public:
std::string ModuleName() const; std::string ModuleName() const;
void SetType(BroType* t) { Unref(type); type = t; } void SetType(BroType* t);
BroType* Type() { return type; } BroType* Type() { return type; }
const BroType* Type() const { return type; } const BroType* Type() const { return type; }
@ -74,7 +73,7 @@ public:
void SetOffset(int arg_offset) { offset = arg_offset; } void SetOffset(int arg_offset) { offset = arg_offset; }
int Offset() const { return offset; } int Offset() const { return offset; }
bool IsRedefinable() const { return FindAttr(ATTR_REDEF) != 0; } bool IsRedefinable() const;
void SetAttrs(Attributes* attr); void SetAttrs(Attributes* attr);
void AddAttrs(Attributes* attr); void AddAttrs(Attributes* attr);
@ -82,11 +81,9 @@ public:
void UpdateValAttrs(); void UpdateValAttrs();
Attributes* Attrs() const { return attrs; } Attributes* Attrs() const { return attrs; }
Attr* FindAttr(attr_tag t) const Attr* FindAttr(attr_tag t) const;
{ return attrs ? attrs->FindAttr(t) : 0; }
bool IsDeprecated() const bool IsDeprecated() const;
{ return FindAttr(ATTR_DEPRECATED) != 0; }
void MakeDeprecated(Expr* deprecation); void MakeDeprecated(Expr* deprecation);

View file

@ -6,9 +6,11 @@
#include <netinet/in.h> #include <netinet/in.h>
#include <netinet/icmp6.h> #include <netinet/icmp6.h>
#include "IPAddr.h"
#include "Type.h" #include "Type.h"
#include "Val.h" #include "Val.h"
#include "Var.h" #include "Var.h"
#include "Reporter.h"
static RecordType* ip4_hdr_type = 0; static RecordType* ip4_hdr_type = 0;
static RecordType* ip6_hdr_type = 0; static RecordType* ip6_hdr_type = 0;
@ -305,6 +307,26 @@ RecordVal* IPv6_Hdr::BuildRecordVal(VectorVal* chain) const
return rv; return rv;
} }
IPAddr IP_Hdr::IPHeaderSrcAddr() const
{
return ip4 ? IPAddr(ip4->ip_src) : IPAddr(ip6->ip6_src);
}
IPAddr IP_Hdr::IPHeaderDstAddr() const
{
return ip4 ? IPAddr(ip4->ip_dst) : IPAddr(ip6->ip6_dst);
}
IPAddr IP_Hdr::SrcAddr() const
{
return ip4 ? IPAddr(ip4->ip_src) : ip6_hdrs->SrcAddr();
}
IPAddr IP_Hdr::DstAddr() const
{
return ip4 ? IPAddr(ip4->ip_dst) : ip6_hdrs->DstAddr();
}
RecordVal* IP_Hdr::BuildIPHdrVal() const RecordVal* IP_Hdr::BuildIPHdrVal() const
{ {
RecordVal* rval = 0; RecordVal* rval = 0;
@ -447,6 +469,15 @@ static inline bool isIPv6ExtHeader(uint8_t type)
} }
} }
IPv6_Hdr_Chain::~IPv6_Hdr_Chain()
{
for ( size_t i = 0; i < chain.size(); ++i ) delete chain[i];
#ifdef ENABLE_MOBILE_IPV6
delete homeAddr;
#endif
delete finalDst;
}
void IPv6_Hdr_Chain::Init(const struct ip6_hdr* ip6, int total_len, void IPv6_Hdr_Chain::Init(const struct ip6_hdr* ip6, int total_len,
bool set_next, uint16_t next) bool set_next, uint16_t next)
{ {
@ -511,6 +542,46 @@ void IPv6_Hdr_Chain::Init(const struct ip6_hdr* ip6, int total_len,
isIPv6ExtHeader(next_type) ); isIPv6ExtHeader(next_type) );
} }
bool IPv6_Hdr_Chain::IsFragment() const
{
if ( chain.empty() )
{
reporter->InternalWarning("empty IPv6 header chain");
return false;
}
return chain[chain.size()-1]->Type() == IPPROTO_FRAGMENT;
}
IPAddr IPv6_Hdr_Chain::SrcAddr() const
{
#ifdef ENABLE_MOBILE_IPV6
if ( homeAddr )
return IPAddr(*homeAddr);
#endif
if ( chain.empty() )
{
reporter->InternalWarning("empty IPv6 header chain");
return IPAddr();
}
return IPAddr(((const struct ip6_hdr*)(chain[0]->Data()))->ip6_src);
}
IPAddr IPv6_Hdr_Chain::DstAddr() const
{
if ( finalDst )
return IPAddr(*finalDst);
if ( chain.empty() )
{
reporter->InternalWarning("empty IPv6 header chain");
return IPAddr();
}
return IPAddr(((const struct ip6_hdr*)(chain[0]->Data()))->ip6_dst);
}
void IPv6_Hdr_Chain::ProcessRoutingHeader(const struct ip6_rthdr* r, uint16_t len) void IPv6_Hdr_Chain::ProcessRoutingHeader(const struct ip6_rthdr* r, uint16_t len)
{ {
if ( finalDst ) if ( finalDst )

View file

@ -3,16 +3,23 @@
#pragma once #pragma once
#include "zeek-config.h" #include "zeek-config.h"
#include "IPAddr.h"
#include "Reporter.h"
#include <vector> #include <vector>
#include <sys/types.h> // for u_char #include <sys/types.h> // for u_char
#include <netinet/in.h> #include <netinet/in.h>
#include <netinet/ip.h> #include <netinet/ip.h>
#ifdef HAVE_NETINET_IP6_H
#include <netinet/ip6.h>
#endif
using std::vector; using std::vector;
class IPAddr;
class RecordVal;
class VectorVal;
#ifdef ENABLE_MOBILE_IPV6 #ifdef ENABLE_MOBILE_IPV6
#ifndef IPPROTO_MOBILITY #ifndef IPPROTO_MOBILITY
@ -148,14 +155,7 @@ public:
finalDst(0) finalDst(0)
{ Init(ip6, len, false); } { Init(ip6, len, false); }
~IPv6_Hdr_Chain() ~IPv6_Hdr_Chain();
{
for ( size_t i = 0; i < chain.size(); ++i ) delete chain[i];
#ifdef ENABLE_MOBILE_IPV6
delete homeAddr;
#endif
delete finalDst;
}
/** /**
* @return a copy of the header chain, but with pointers to individual * @return a copy of the header chain, but with pointers to individual
@ -181,16 +181,7 @@ public:
/** /**
* Returns whether the header chain indicates a fragmented packet. * Returns whether the header chain indicates a fragmented packet.
*/ */
bool IsFragment() const bool IsFragment() const;
{
if ( chain.empty() )
{
reporter->InternalWarning("empty IPv6 header chain");
return false;
}
return chain[chain.size()-1]->Type() == IPPROTO_FRAGMENT;
}
/** /**
* Returns pointer to fragment header structure if the chain contains one. * Returns pointer to fragment header structure if the chain contains one.
@ -225,39 +216,14 @@ public:
* option as defined by Mobile IPv6 (RFC 6275), then return it, else * option as defined by Mobile IPv6 (RFC 6275), then return it, else
* return the source address in the main IPv6 header. * return the source address in the main IPv6 header.
*/ */
IPAddr SrcAddr() const IPAddr SrcAddr() const;
{
#ifdef ENABLE_MOBILE_IPV6
if ( homeAddr )
return IPAddr(*homeAddr);
#endif
if ( chain.empty() )
{
reporter->InternalWarning("empty IPv6 header chain");
return IPAddr();
}
return IPAddr(((const struct ip6_hdr*)(chain[0]->Data()))->ip6_src);
}
/** /**
* If the chain contains a Routing header with non-zero segments left, * If the chain contains a Routing header with non-zero segments left,
* then return the last address of the first such header, else return * then return the last address of the first such header, else return
* the destination address of the main IPv6 header. * the destination address of the main IPv6 header.
*/ */
IPAddr DstAddr() const IPAddr DstAddr() const;
{
if ( finalDst )
return IPAddr(*finalDst);
if ( chain.empty() )
{
reporter->InternalWarning("empty IPv6 header chain");
return IPAddr();
}
return IPAddr(((const struct ip6_hdr*)(chain[0]->Data()))->ip6_dst);
}
/** /**
* Returns a vector of ip6_ext_hdr RecordVals that includes script-layer * Returns a vector of ip6_ext_hdr RecordVals that includes script-layer
@ -401,22 +367,19 @@ public:
/** /**
* Returns the source address held in the IP header. * Returns the source address held in the IP header.
*/ */
IPAddr IPHeaderSrcAddr() const IPAddr IPHeaderSrcAddr() const;
{ return ip4 ? IPAddr(ip4->ip_src) : IPAddr(ip6->ip6_src); }
/** /**
* Returns the destination address held in the IP header. * Returns the destination address held in the IP header.
*/ */
IPAddr IPHeaderDstAddr() const IPAddr IPHeaderDstAddr() const;
{ return ip4 ? IPAddr(ip4->ip_dst) : IPAddr(ip6->ip6_dst); }
/** /**
* For IPv4 or IPv6 headers that don't contain a Home Address option * For IPv4 or IPv6 headers that don't contain a Home Address option
* (Mobile IPv6, RFC 6275), return source address held in the IP header. * (Mobile IPv6, RFC 6275), return source address held in the IP header.
* For IPv6 headers that contain a Home Address option, return that address. * For IPv6 headers that contain a Home Address option, return that address.
*/ */
IPAddr SrcAddr() const IPAddr SrcAddr() const;
{ return ip4 ? IPAddr(ip4->ip_src) : ip6_hdrs->SrcAddr(); }
/** /**
* For IPv4 or IPv6 headers that don't contain a Routing header with * For IPv4 or IPv6 headers that don't contain a Routing header with
@ -424,8 +387,7 @@ public:
* For IPv6 headers with a Routing header that has non-zero segments left, * For IPv6 headers with a Routing header that has non-zero segments left,
* return the last address in the first such Routing header. * return the last address in the first such Routing header.
*/ */
IPAddr DstAddr() const IPAddr DstAddr() const;
{ return ip4 ? IPAddr(ip4->ip_dst) : ip6_hdrs->DstAddr(); }
/** /**
* Returns a pointer to the payload of the IP packet, usually an * Returns a pointer to the payload of the IP packet, usually an

View file

@ -5,7 +5,9 @@
#include <vector> #include <vector>
#include "IPAddr.h" #include "IPAddr.h"
#include "Reporter.h" #include "Reporter.h"
#include "BroString.h"
#include "Conn.h" #include "Conn.h"
#include "Hash.h"
#include "bro_inet_ntop.h" #include "bro_inet_ntop.h"
#include "analyzer/Manager.h" #include "analyzer/Manager.h"
@ -45,6 +47,16 @@ ConnIDKey BuildConnIDKey(const ConnID& id)
return key; return key;
} }
IPAddr::IPAddr(const BroString& s)
{
Init(s.CheckString());
}
HashKey* IPAddr::GetHashKey() const
{
return new HashKey((void*)in6.s6_addr, sizeof(in6.s6_addr));
}
static inline uint32_t bit_mask32(int bottom_bits) static inline uint32_t bit_mask32(int bottom_bits)
{ {
if ( bottom_bits >= 32 ) if ( bottom_bits >= 32 )
@ -290,6 +302,19 @@ string IPPrefix::AsString() const
return prefix.AsString() +"/" + l; return prefix.AsString() +"/" + l;
} }
HashKey* IPPrefix::GetHashKey() const
{
struct {
in6_addr ip;
uint32_t len;
} key;
key.ip = prefix.in6;
key.len = Length();
return new HashKey(&key, sizeof(key));
}
bool IPPrefix::ConvertString(const char* text, IPPrefix* result) bool IPPrefix::ConvertString(const char* text, IPPrefix* result)
{ {
string s(text); string s(text);

View file

@ -2,8 +2,6 @@
#pragma once #pragma once
#include "BroString.h"
#include "Hash.h"
#include "threading/SerialTypes.h" #include "threading/SerialTypes.h"
#include <netinet/in.h> #include <netinet/in.h>
@ -13,6 +11,8 @@
using std::string; using std::string;
struct ConnID; struct ConnID;
class BroString;
class HashKey;
namespace analyzer { class ExpectedConn; } namespace analyzer { class ExpectedConn; }
typedef in_addr in4_addr; typedef in_addr in4_addr;
@ -112,10 +112,7 @@ public:
* @param s String containing an IP address as either a dotted IPv4 * @param s String containing an IP address as either a dotted IPv4
* address or a hex IPv6 address. * address or a hex IPv6 address.
*/ */
explicit IPAddr(const BroString& s) explicit IPAddr(const BroString& s);
{
Init(s.CheckString());
}
/** /**
* Constructs an address instance from a raw byte representation. * Constructs an address instance from a raw byte representation.
@ -254,10 +251,7 @@ public:
* Returns a key that can be used to lookup the IP Address in a hash * Returns a key that can be used to lookup the IP Address in a hash
* table. Passes ownership to caller. * table. Passes ownership to caller.
*/ */
HashKey* GetHashKey() const HashKey* GetHashKey() const;
{
return new HashKey((void*)in6.s6_addr, sizeof(in6.s6_addr));
}
/** /**
* Masks out lower bits of the address. * Masks out lower bits of the address.
@ -639,18 +633,7 @@ public:
* Returns a key that can be used to lookup the IP Prefix in a hash * Returns a key that can be used to lookup the IP Prefix in a hash
* table. Passes ownership to caller. * table. Passes ownership to caller.
*/ */
HashKey* GetHashKey() const HashKey* GetHashKey() const;
{
struct {
in6_addr ip;
uint32_t len;
} key;
key.ip = prefix.in6;
key.len = Length();
return new HashKey(&key, sizeof(key));
}
/** Converts the prefix into the type used internally by the /** Converts the prefix into the type used internally by the
* inter-thread communication. * inter-thread communication.

View file

@ -12,6 +12,11 @@ using std::string;
#include "analyzer/Manager.h" #include "analyzer/Manager.h"
RuleActionEvent::RuleActionEvent(const char* arg_msg)
{
msg = copy_string(arg_msg);
}
void RuleActionEvent::DoAction(const Rule* parent, RuleEndpointState* state, void RuleActionEvent::DoAction(const Rule* parent, RuleEndpointState* state,
const u_char* data, int len) const u_char* data, int len)
{ {
@ -30,6 +35,12 @@ void RuleActionEvent::PrintDebug()
fprintf(stderr, " RuleActionEvent: |%s|\n", msg); fprintf(stderr, " RuleActionEvent: |%s|\n", msg);
} }
RuleActionMIME::RuleActionMIME(const char* arg_mime, int arg_strength)
{
mime = copy_string(arg_mime);
strength = arg_strength;
}
void RuleActionMIME::PrintDebug() void RuleActionMIME::PrintDebug()
{ {
fprintf(stderr, " RuleActionMIME: |%s|\n", mime); fprintf(stderr, " RuleActionMIME: |%s|\n", mime);

View file

@ -1,7 +1,5 @@
#pragma once #pragma once
#include "util.h"
#include "analyzer/Tag.h" #include "analyzer/Tag.h"
#include <string> #include <string>
@ -27,7 +25,7 @@ public:
// Implements the "event" keyword. // Implements the "event" keyword.
class RuleActionEvent : public RuleAction { class RuleActionEvent : public RuleAction {
public: public:
explicit RuleActionEvent(const char* arg_msg) { msg = copy_string(arg_msg); } explicit RuleActionEvent(const char* arg_msg);
~RuleActionEvent() override { delete [] msg; } ~RuleActionEvent() override { delete [] msg; }
void DoAction(const Rule* parent, RuleEndpointState* state, void DoAction(const Rule* parent, RuleEndpointState* state,
@ -41,8 +39,7 @@ private:
class RuleActionMIME : public RuleAction { class RuleActionMIME : public RuleAction {
public: public:
explicit RuleActionMIME(const char* arg_mime, int arg_strength = 0) explicit RuleActionMIME(const char* arg_mime, int arg_strength = 0);
{ mime = copy_string(arg_mime); strength = arg_strength; }
~RuleActionMIME() override ~RuleActionMIME() override
{ delete [] mime; } { delete [] mime; }

View file

@ -93,6 +93,14 @@ void Stmt::Describe(ODesc* d) const
AddTag(d); AddTag(d);
} }
void Stmt::DecrBPCount()
{
if ( breakpoint_count )
--breakpoint_count;
else
reporter->InternalError("breakpoint count decremented below 0");
}
void Stmt::AddTag(ODesc* d) const void Stmt::AddTag(ODesc* d) const
{ {
if ( d->IsBinary() ) if ( d->IsBinary() )
@ -1643,6 +1651,13 @@ void EventBodyList::Describe(ODesc* d) const
StmtList::Describe(d); StmtList::Describe(d);
} }
InitStmt::InitStmt(id_list* arg_inits) : Stmt(STMT_INIT)
{
inits = arg_inits;
if ( arg_inits && arg_inits->length() )
SetLocationInfo((*arg_inits)[0]->GetLocationInfo());
}
InitStmt::~InitStmt() InitStmt::~InitStmt()
{ {
for ( const auto& init : *inits ) for ( const auto& init : *inits )

View file

@ -8,7 +8,6 @@
#include "Dict.h" #include "Dict.h"
#include "ID.h" #include "ID.h"
#include "Obj.h" #include "Obj.h"
#include "Reporter.h"
#include "StmtEnums.h" #include "StmtEnums.h"
@ -63,13 +62,7 @@ public:
void Describe(ODesc* d) const override; void Describe(ODesc* d) const override;
virtual void IncrBPCount() { ++breakpoint_count; } virtual void IncrBPCount() { ++breakpoint_count; }
virtual void DecrBPCount() virtual void DecrBPCount();
{
if ( breakpoint_count )
--breakpoint_count;
else
reporter->InternalError("breakpoint count decremented below 0");
}
virtual unsigned int BPCount() const { return breakpoint_count; } virtual unsigned int BPCount() const { return breakpoint_count; }
@ -436,12 +429,7 @@ protected:
class InitStmt : public Stmt { class InitStmt : public Stmt {
public: public:
explicit InitStmt(id_list* arg_inits) : Stmt(STMT_INIT) explicit InitStmt(id_list* arg_inits);
{
inits = arg_inits;
if ( arg_inits && arg_inits->length() )
SetLocationInfo((*arg_inits)[0]->GetLocationInfo());
}
~InitStmt() override; ~InitStmt() override;

View file

@ -475,6 +475,11 @@ void Trigger::Disable()
disabled = true; disabled = true;
} }
void Trigger::Describe(ODesc* d) const
{
d->Add("<trigger>");
}
void Trigger::Modified(notifier::Modifiable* m) void Trigger::Modified(notifier::Modifiable* m)
{ {
trigger_mgr->Queue(this); trigger_mgr->Queue(this);

View file

@ -1,7 +1,6 @@
#pragma once #pragma once
#include "Obj.h" #include "Obj.h"
#include "Desc.h"
#include "Notifier.h" #include "Notifier.h"
#include "iosource/IOSource.h" #include "iosource/IOSource.h"
@ -15,6 +14,7 @@ class Stmt;
class Frame; class Frame;
class Val; class Val;
class ID; class ID;
class ODesc;
namespace trigger { namespace trigger {
// Triggers are the heart of "when" statements: expressions that when // Triggers are the heart of "when" statements: expressions that when
@ -69,8 +69,7 @@ public:
bool Disabled() const { return disabled; } bool Disabled() const { return disabled; }
void Describe(ODesc* d) const override void Describe(ODesc* d) const override;
{ d->Add("<trigger>"); }
// Overidden from Notifier. We queue the trigger and evaluate it // Overidden from Notifier. We queue the trigger and evaluate it
// later to avoid race conditions. // later to avoid race conditions.

View file

@ -273,6 +273,13 @@ void TypeList::Describe(ODesc* d) const
} }
} }
unsigned int TypeList::MemoryAllocation() const
{
return BroType::MemoryAllocation()
+ padded_sizeof(*this) - padded_sizeof(BroType)
+ types.MemoryAllocation() - padded_sizeof(types);
}
IndexType::~IndexType() IndexType::~IndexType()
{ {
Unref(indices); Unref(indices);

View file

@ -318,12 +318,7 @@ public:
void Describe(ODesc* d) const override; void Describe(ODesc* d) const override;
unsigned int MemoryAllocation() const override unsigned int MemoryAllocation() const override;
{
return BroType::MemoryAllocation()
+ padded_sizeof(*this) - padded_sizeof(BroType)
+ types.MemoryAllocation() - padded_sizeof(types);
}
protected: protected:
BroType* pure_type; BroType* pure_type;

View file

@ -1,6 +1,8 @@
// See the file "COPYING" in the main distribution directory for copyright. // See the file "COPYING" in the main distribution directory for copyright.
#include "UID.h" #include "UID.h"
#include "Reporter.h"
#include "util.h"
#include <cstdlib> #include <cstdlib>
@ -26,3 +28,15 @@ void UID::Set(bro_uint_t bits, const uint64_t* v, size_t n)
if ( res.rem ) if ( res.rem )
uid[0] >>= 64 - res.rem; uid[0] >>= 64 - res.rem;
} }
std::string UID::Base62(std::string prefix) const
{
if ( ! initialized )
reporter->InternalError("use of uninitialized UID");
char tmp[sizeof(uid) * 8 + 1]; // enough for even binary representation
for ( size_t i = 0; i < BRO_UID_LEN; ++i )
prefix.append(uitoa_n(uid[i], tmp, sizeof(tmp), 62));
return prefix;
}

View file

@ -2,7 +2,6 @@
#pragma once #pragma once
#include "Reporter.h"
#include "util.h" // for bro_int_t #include "util.h" // for bro_int_t
#include <string> #include <string>
@ -99,16 +98,4 @@ inline UID& UID::operator=(const UID& other)
return *this; return *this;
} }
inline std::string UID::Base62(std::string prefix) const
{
if ( ! initialized )
reporter->InternalError("use of uninitialized UID");
char tmp[sizeof(uid) * 8 + 1]; // enough for even binary representation
for ( size_t i = 0; i < BRO_UID_LEN; ++i )
prefix.append(uitoa_n(uid[i], tmp, sizeof(tmp), 62));
return prefix;
}
} // namespace Bro } // namespace Bro

View file

@ -13,11 +13,13 @@
#include <stdio.h> #include <stdio.h>
#include <stdlib.h> #include <stdlib.h>
#include "Attr.h"
#include "Net.h" #include "Net.h"
#include "File.h" #include "File.h"
#include "Func.h" #include "Func.h"
#include "Desc.h" #include "Desc.h"
#include "IntrusivePtr.h" #include "IntrusivePtr.h"
#include "ID.h"
#include "RE.h" #include "RE.h"
#include "Scope.h" #include "Scope.h"
#include "NetVar.h" #include "NetVar.h"
@ -360,6 +362,14 @@ void Val::ValDescribeReST(ODesc* d) const
} }
#ifdef DEBUG
void Val::SetID(ID* id)
{
delete [] bound_id;
bound_id = id ? copy_string(id->Name()) : 0;
}
#endif
bool Val::WouldOverflow(const BroType* from_type, const BroType* to_type, const Val* val) bool Val::WouldOverflow(const BroType* from_type, const BroType* to_type, const Val* val)
{ {
if ( !to_type || !from_type ) if ( !to_type || !from_type )
@ -1993,6 +2003,11 @@ ListVal* TableVal::ConvertToPureList() const
return ConvertToList((*tl)[0]->Tag()); return ConvertToList((*tl)[0]->Tag());
} }
Attr* TableVal::FindAttr(attr_tag t) const
{
return attrs ? attrs->FindAttr(t) : 0;
}
void TableVal::Describe(ODesc* d) const void TableVal::Describe(ODesc* d) const
{ {
const PDict<TableEntryVal>* tbl = AsTable(); const PDict<TableEntryVal>* tbl = AsTable();

View file

@ -6,9 +6,7 @@
#include "Dict.h" #include "Dict.h"
#include "CompHash.h" #include "CompHash.h"
#include "BroString.h" #include "BroString.h"
#include "Attr.h"
#include "Timer.h" #include "Timer.h"
#include "ID.h"
#include "Scope.h" #include "Scope.h"
#include "Notifier.h" #include "Notifier.h"
#include "RE.h" #include "RE.h"
@ -293,11 +291,7 @@ public:
return bound_id ? global_scope()->Lookup(bound_id) : 0; return bound_id ? global_scope()->Lookup(bound_id) : 0;
} }
void SetID(ID* id) void SetID(ID* id);
{
delete [] bound_id;
bound_id = id ? copy_string(id->Name()) : 0;
}
#endif #endif
static bool WouldOverflow(const BroType* from_type, const BroType* to_type, const Val* val); static bool WouldOverflow(const BroType* from_type, const BroType* to_type, const Val* val);
@ -792,8 +786,7 @@ public:
ListVal* ConvertToPureList() const; // must be single index type ListVal* ConvertToPureList() const; // must be single index type
void SetAttrs(Attributes* attrs); void SetAttrs(Attributes* attrs);
Attr* FindAttr(attr_tag t) const Attr* FindAttr(attr_tag t) const;
{ return attrs ? attrs->FindAttr(t) : 0; }
Attributes* Attrs() { return attrs; } Attributes* Attrs() { return attrs; }
// Returns the size of the table. // Returns the size of the table.

View file

@ -1,5 +1,6 @@
#include "Data.h" #include "Data.h"
#include "File.h" #include "File.h"
#include "Desc.h"
#include "IntrusivePtr.h" #include "IntrusivePtr.h"
#include "module_util.h" #include "module_util.h"
#include "3rdparty/doctest.h" #include "3rdparty/doctest.h"
@ -1150,6 +1151,13 @@ broker::data& bro_broker::opaque_field_to_data(RecordVal* v, Frame* f)
return static_cast<DataVal*>(d)->data; return static_cast<DataVal*>(d)->data;
} }
void bro_broker::DataVal::ValDescribe(ODesc* d) const
{
d->Add("broker::data{");
d->Add(broker::to_string(data));
d->Add("}");
}
bool bro_broker::DataVal::canCastTo(BroType* t) const bool bro_broker::DataVal::canCastTo(BroType* t) const
{ {
return data_type_check(data, t); return data_type_check(data, t);

View file

@ -4,12 +4,13 @@
#include "Reporter.h" #include "Reporter.h"
#include "Frame.h" #include "Frame.h"
#include "Expr.h" #include "Expr.h"
#include "Desc.h"
#include "Var.h" // for internal_type() #include "Var.h" // for internal_type()
template <class T> template <class T>
class IntrusivePtr; class IntrusivePtr;
class ODesc;
namespace bro_broker { namespace bro_broker {
extern OpaqueType* opaque_of_data_type; extern OpaqueType* opaque_of_data_type;
@ -102,12 +103,7 @@ public:
: OpaqueVal(bro_broker::opaque_of_data_type), data(std::move(arg_data)) : OpaqueVal(bro_broker::opaque_of_data_type), data(std::move(arg_data))
{} {}
void ValDescribe(ODesc* d) const override void ValDescribe(ODesc* d) const override;
{
d->Add("broker::data{");
d->Add(broker::to_string(data));
d->Add("}");
}
IntrusivePtr<Val> castTo(BroType* t); IntrusivePtr<Val> castTo(BroType* t);
bool canCastTo(BroType* t) const; bool canCastTo(BroType* t) const;

View file

@ -1,7 +1,7 @@
#include "Packet.h" #include "Packet.h"
#include "Sessions.h" #include "Sessions.h"
#include "Desc.h" #include "Desc.h"
#include "IP.h"
#include "iosource/Manager.h" #include "iosource/Manager.h"
extern "C" { extern "C" {
@ -62,6 +62,11 @@ void Packet::Init(int arg_link_type, pkt_timeval *arg_ts, uint32_t arg_caplen,
ProcessLayer2(); ProcessLayer2();
} }
const IP_Hdr Packet::IP() const
{
return IP_Hdr((struct ip *) (data + hdr_size), false);
}
void Packet::Weird(const char* name) void Packet::Weird(const char* name)
{ {
sessions->Weird(name, this); sessions->Weird(name, this);

View file

@ -1,7 +1,5 @@
#pragma once #pragma once
#include "IP.h"
#include <string> #include <string>
#include <stdint.h> #include <stdint.h>
@ -16,6 +14,8 @@ typedef struct timeval pkt_timeval;
class Val; class Val;
class ODesc; class ODesc;
class IP_Hdr;
class RecordVal;
/** /**
* The Layer 3 type of a packet, as determined by the parsing code in Packet. * The Layer 3 type of a packet, as determined by the parsing code in Packet.
@ -119,8 +119,7 @@ public:
* Interprets the Layer 3 of the packet as IP and returns a * Interprets the Layer 3 of the packet as IP and returns a
* correspondign object. * correspondign object.
*/ */
const IP_Hdr IP() const const IP_Hdr IP() const;
{ return IP_Hdr((struct ip *) (data + hdr_size), false); }
/** /**
* Returns a \c raw_pkt_hdr RecordVal, which includes layer 2 and * Returns a \c raw_pkt_hdr RecordVal, which includes layer 2 and

View file

@ -180,3 +180,8 @@ IdentifierInfo::Redefinition::~Redefinition()
{ {
Unref(init_expr); Unref(init_expr);
} }
IdentifierInfo::RecordField::~RecordField()
{
delete field;
}

View file

@ -4,7 +4,6 @@
#include "Info.h" #include "Info.h"
#include "ID.h" #include "ID.h"
#include "Type.h"
#include <string> #include <string>
#include <vector> #include <vector>
@ -167,8 +166,7 @@ private:
std::string DoReStructuredText(bool roles_only) const override; std::string DoReStructuredText(bool roles_only) const override;
struct RecordField { struct RecordField {
~RecordField() ~RecordField();
{ delete field; }
TypeDecl* field; TypeDecl* field;
std::string from_script; std::string from_script;