mirror of
https://github.com/zeek/zeek.git
synced 2025-10-02 14:48:21 +00:00
Table expiry now raises &on_change handlers
Expiry is remprted as ELEMENT_EXPIRED (which was newly introduced), to allow distinction from table deletion.
This commit is contained in:
parent
7f9f66fce9
commit
18ab7838a9
5 changed files with 138 additions and 9 deletions
28
src/Val.cc
28
src/Val.cc
|
@ -1969,6 +1969,8 @@ void TableVal::CallChangeFunc(const Val* index, Val* old_value, OnChangeType tpe
|
||||||
case element_removed:
|
case element_removed:
|
||||||
type = BifType::Enum::TableChange->GetVal(BifEnum::TableChange::TABLE_ELEMENT_REMOVED);
|
type = BifType::Enum::TableChange->GetVal(BifEnum::TableChange::TABLE_ELEMENT_REMOVED);
|
||||||
break;
|
break;
|
||||||
|
case element_expired:
|
||||||
|
type = BifType::Enum::TableChange->GetVal(BifEnum::TableChange::TABLE_ELEMENT_EXPIRED);
|
||||||
}
|
}
|
||||||
vl.append(type);
|
vl.append(type);
|
||||||
|
|
||||||
|
@ -2262,9 +2264,9 @@ void TableVal::DoExpire(double t)
|
||||||
tbl->MakeRobustCookie(expire_cookie);
|
tbl->MakeRobustCookie(expire_cookie);
|
||||||
}
|
}
|
||||||
|
|
||||||
HashKey* k = 0;
|
HashKey* k = nullptr;
|
||||||
TableEntryVal* v = 0;
|
TableEntryVal* v = nullptr;
|
||||||
TableEntryVal* v_saved = 0;
|
TableEntryVal* v_saved = nullptr;
|
||||||
bool modified = false;
|
bool modified = false;
|
||||||
|
|
||||||
for ( int i = 0; i < table_incremental_step &&
|
for ( int i = 0; i < table_incremental_step &&
|
||||||
|
@ -2281,10 +2283,11 @@ void TableVal::DoExpire(double t)
|
||||||
|
|
||||||
else if ( v->ExpireAccessTime() + timeout < t )
|
else if ( v->ExpireAccessTime() + timeout < t )
|
||||||
{
|
{
|
||||||
|
Val* idx = nullptr;
|
||||||
if ( expire_func )
|
if ( expire_func )
|
||||||
{
|
{
|
||||||
Val* idx = RecoverIndex(k);
|
idx = RecoverIndex(k);
|
||||||
double secs = CallExpireFunc(idx);
|
double secs = CallExpireFunc(idx->Ref());
|
||||||
|
|
||||||
// It's possible that the user-provided
|
// It's possible that the user-provided
|
||||||
// function modified or deleted the table
|
// function modified or deleted the table
|
||||||
|
@ -2295,6 +2298,7 @@ void TableVal::DoExpire(double t)
|
||||||
if ( ! v )
|
if ( ! v )
|
||||||
{ // user-provided function deleted it
|
{ // user-provided function deleted it
|
||||||
v = v_saved;
|
v = v_saved;
|
||||||
|
Unref(idx);
|
||||||
delete k;
|
delete k;
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
@ -2304,6 +2308,7 @@ void TableVal::DoExpire(double t)
|
||||||
// User doesn't want us to expire
|
// User doesn't want us to expire
|
||||||
// this now.
|
// this now.
|
||||||
v->SetExpireAccess(network_time - timeout + secs);
|
v->SetExpireAccess(network_time - timeout + secs);
|
||||||
|
Unref(idx);
|
||||||
delete k;
|
delete k;
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
@ -2312,13 +2317,20 @@ void TableVal::DoExpire(double t)
|
||||||
|
|
||||||
if ( subnets )
|
if ( subnets )
|
||||||
{
|
{
|
||||||
Val* index = RecoverIndex(k);
|
if ( ! idx )
|
||||||
if ( ! subnets->Remove(index) )
|
idx = RecoverIndex(k);
|
||||||
|
if ( ! subnets->Remove(idx) )
|
||||||
reporter->InternalWarning("index not in prefix table");
|
reporter->InternalWarning("index not in prefix table");
|
||||||
Unref(index);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
tbl->RemoveEntry(k);
|
tbl->RemoveEntry(k);
|
||||||
|
if ( change_func )
|
||||||
|
{
|
||||||
|
if ( ! idx )
|
||||||
|
idx = RecoverIndex(k);
|
||||||
|
CallChangeFunc(idx, v->Value(), element_expired);
|
||||||
|
}
|
||||||
|
Unref(idx);
|
||||||
Unref(v->Value());
|
Unref(v->Value());
|
||||||
delete v;
|
delete v;
|
||||||
modified = true;
|
modified = true;
|
||||||
|
|
|
@ -848,7 +848,7 @@ protected:
|
||||||
double CallExpireFunc(Val *idx);
|
double CallExpireFunc(Val *idx);
|
||||||
|
|
||||||
// Enum for the different kinds of changes an &on_change handler can see
|
// Enum for the different kinds of changes an &on_change handler can see
|
||||||
enum OnChangeType { element_new, element_changed, element_removed };
|
enum OnChangeType { element_new, element_changed, element_removed, element_expired };
|
||||||
|
|
||||||
// Calls &change_func. Does not take ownership of values. (Refs if needed).
|
// Calls &change_func. Does not take ownership of values. (Refs if needed).
|
||||||
void CallChangeFunc(const Val* index, Val* old_value, OnChangeType tpe);
|
void CallChangeFunc(const Val* index, Val* old_value, OnChangeType tpe);
|
||||||
|
|
|
@ -229,6 +229,7 @@ enum TableChange %{
|
||||||
TABLE_ELEMENT_NEW,
|
TABLE_ELEMENT_NEW,
|
||||||
TABLE_ELEMENT_CHANGED,
|
TABLE_ELEMENT_CHANGED,
|
||||||
TABLE_ELEMENT_REMOVED,
|
TABLE_ELEMENT_REMOVED,
|
||||||
|
TABLE_ELEMENT_EXPIRED,
|
||||||
%}
|
%}
|
||||||
|
|
||||||
module Reporter;
|
module Reporter;
|
||||||
|
|
91
testing/btest/Baseline/language.on_change_expire/output
Normal file
91
testing/btest/Baseline/language.on_change_expire/output
Normal file
|
@ -0,0 +1,91 @@
|
||||||
|
change_function, a, 5, TABLE_ELEMENT_NEW
|
||||||
|
change_function, [orig_h=172.16.238.1, orig_p=49656/tcp, resp_h=172.16.238.131, resp_p=22/tcp], 1, TABLE_ELEMENT_NEW
|
||||||
|
change_function, [orig_h=172.16.238.131, orig_p=37975/udp, resp_h=172.16.238.2, resp_p=53/udp], 1, TABLE_ELEMENT_NEW
|
||||||
|
change_function, [orig_h=fe80::20c:29ff:febd:6f01, orig_p=5353/udp, resp_h=ff02::fb, resp_p=5353/udp], 1, TABLE_ELEMENT_NEW
|
||||||
|
change_function, [orig_h=172.16.238.131, orig_p=5353/udp, resp_h=224.0.0.251, resp_p=5353/udp], 1, TABLE_ELEMENT_NEW
|
||||||
|
change_function, [orig_h=172.16.238.1, orig_p=5353/udp, resp_h=224.0.0.251, resp_p=5353/udp], 1, TABLE_ELEMENT_NEW
|
||||||
|
change_function, [orig_h=172.16.238.1, orig_p=49657/tcp, resp_h=172.16.238.131, resp_p=80/tcp], 1, TABLE_ELEMENT_NEW
|
||||||
|
change_function, [orig_h=172.16.238.1, orig_p=49658/tcp, resp_h=172.16.238.131, resp_p=80/tcp], 1, TABLE_ELEMENT_NEW
|
||||||
|
change_function, [orig_h=172.16.238.1, orig_p=17500/udp, resp_h=172.16.238.255, resp_p=17500/udp], 1, TABLE_ELEMENT_NEW
|
||||||
|
expired [orig_h=172.16.238.1, orig_p=49658/tcp, resp_h=172.16.238.131, resp_p=80/tcp]
|
||||||
|
change_function, [orig_h=172.16.238.1, orig_p=49658/tcp, resp_h=172.16.238.131, resp_p=80/tcp], 1, TABLE_ELEMENT_EXPIRED
|
||||||
|
expired [orig_h=172.16.238.1, orig_p=17500/udp, resp_h=172.16.238.255, resp_p=17500/udp]
|
||||||
|
change_function, [orig_h=172.16.238.1, orig_p=17500/udp, resp_h=172.16.238.255, resp_p=17500/udp], 1, TABLE_ELEMENT_EXPIRED
|
||||||
|
expired [orig_h=172.16.238.1, orig_p=49657/tcp, resp_h=172.16.238.131, resp_p=80/tcp]
|
||||||
|
change_function, [orig_h=172.16.238.1, orig_p=49657/tcp, resp_h=172.16.238.131, resp_p=80/tcp], 1, TABLE_ELEMENT_EXPIRED
|
||||||
|
expired [orig_h=172.16.238.131, orig_p=37975/udp, resp_h=172.16.238.2, resp_p=53/udp]
|
||||||
|
change_function, [orig_h=172.16.238.131, orig_p=37975/udp, resp_h=172.16.238.2, resp_p=53/udp], 1, TABLE_ELEMENT_EXPIRED
|
||||||
|
expired [orig_h=fe80::20c:29ff:febd:6f01, orig_p=5353/udp, resp_h=ff02::fb, resp_p=5353/udp]
|
||||||
|
change_function, [orig_h=fe80::20c:29ff:febd:6f01, orig_p=5353/udp, resp_h=ff02::fb, resp_p=5353/udp], 1, TABLE_ELEMENT_EXPIRED
|
||||||
|
expired [orig_h=172.16.238.1, orig_p=5353/udp, resp_h=224.0.0.251, resp_p=5353/udp]
|
||||||
|
change_function, [orig_h=172.16.238.1, orig_p=5353/udp, resp_h=224.0.0.251, resp_p=5353/udp], 1, TABLE_ELEMENT_EXPIRED
|
||||||
|
expired a
|
||||||
|
change_function, a, 5, TABLE_ELEMENT_EXPIRED
|
||||||
|
expired [orig_h=172.16.238.1, orig_p=49656/tcp, resp_h=172.16.238.131, resp_p=22/tcp]
|
||||||
|
change_function, [orig_h=172.16.238.1, orig_p=49656/tcp, resp_h=172.16.238.131, resp_p=22/tcp], 1, TABLE_ELEMENT_EXPIRED
|
||||||
|
expired [orig_h=172.16.238.131, orig_p=5353/udp, resp_h=224.0.0.251, resp_p=5353/udp]
|
||||||
|
change_function, [orig_h=172.16.238.131, orig_p=5353/udp, resp_h=224.0.0.251, resp_p=5353/udp], 1, TABLE_ELEMENT_EXPIRED
|
||||||
|
change_function, [orig_h=172.16.238.1, orig_p=49659/tcp, resp_h=172.16.238.131, resp_p=21/tcp], 1, TABLE_ELEMENT_NEW
|
||||||
|
change_function, [orig_h=172.16.238.131, orig_p=45126/udp, resp_h=172.16.238.2, resp_p=53/udp], 1, TABLE_ELEMENT_NEW
|
||||||
|
expired [orig_h=172.16.238.1, orig_p=49659/tcp, resp_h=172.16.238.131, resp_p=21/tcp]
|
||||||
|
change_function, [orig_h=172.16.238.1, orig_p=49659/tcp, resp_h=172.16.238.131, resp_p=21/tcp], 1, TABLE_ELEMENT_EXPIRED
|
||||||
|
expired [orig_h=172.16.238.131, orig_p=45126/udp, resp_h=172.16.238.2, resp_p=53/udp]
|
||||||
|
change_function, [orig_h=172.16.238.131, orig_p=45126/udp, resp_h=172.16.238.2, resp_p=53/udp], 1, TABLE_ELEMENT_EXPIRED
|
||||||
|
change_function, [orig_h=172.16.238.131, orig_p=55515/tcp, resp_h=74.125.225.81, resp_p=80/tcp], 1, TABLE_ELEMENT_NEW
|
||||||
|
change_function, [orig_h=172.16.238.131, orig_p=37846/udp, resp_h=172.16.238.2, resp_p=53/udp], 1, TABLE_ELEMENT_NEW
|
||||||
|
change_function, [orig_h=172.16.238.131, orig_p=51970/udp, resp_h=172.16.238.2, resp_p=53/udp], 1, TABLE_ELEMENT_NEW
|
||||||
|
change_function, [orig_h=172.16.238.131, orig_p=54304/udp, resp_h=172.16.238.2, resp_p=53/udp], 1, TABLE_ELEMENT_NEW
|
||||||
|
change_function, [orig_h=172.16.238.131, orig_p=44555/udp, resp_h=172.16.238.2, resp_p=53/udp], 1, TABLE_ELEMENT_NEW
|
||||||
|
change_function, [orig_h=172.16.238.131, orig_p=33109/udp, resp_h=172.16.238.2, resp_p=53/udp], 1, TABLE_ELEMENT_NEW
|
||||||
|
change_function, [orig_h=172.16.238.131, orig_p=50205/udp, resp_h=172.16.238.2, resp_p=53/udp], 1, TABLE_ELEMENT_NEW
|
||||||
|
change_function, [orig_h=172.16.238.131, orig_p=57272/udp, resp_h=172.16.238.2, resp_p=53/udp], 1, TABLE_ELEMENT_NEW
|
||||||
|
change_function, [orig_h=172.16.238.131, orig_p=33818/udp, resp_h=172.16.238.2, resp_p=53/udp], 1, TABLE_ELEMENT_NEW
|
||||||
|
change_function, [orig_h=172.16.238.131, orig_p=45140/udp, resp_h=172.16.238.2, resp_p=53/udp], 1, TABLE_ELEMENT_NEW
|
||||||
|
change_function, [orig_h=172.16.238.131, orig_p=55368/udp, resp_h=172.16.238.2, resp_p=53/udp], 1, TABLE_ELEMENT_NEW
|
||||||
|
change_function, [orig_h=172.16.238.131, orig_p=53102/udp, resp_h=172.16.238.2, resp_p=53/udp], 1, TABLE_ELEMENT_NEW
|
||||||
|
change_function, [orig_h=172.16.238.131, orig_p=59573/udp, resp_h=172.16.238.2, resp_p=53/udp], 1, TABLE_ELEMENT_NEW
|
||||||
|
change_function, [orig_h=172.16.238.131, orig_p=52952/udp, resp_h=172.16.238.2, resp_p=53/udp], 1, TABLE_ELEMENT_NEW
|
||||||
|
change_function, [orig_h=172.16.238.131, orig_p=48621/udp, resp_h=172.16.238.2, resp_p=53/udp], 1, TABLE_ELEMENT_NEW
|
||||||
|
expired [orig_h=172.16.238.131, orig_p=44555/udp, resp_h=172.16.238.2, resp_p=53/udp]
|
||||||
|
change_function, [orig_h=172.16.238.131, orig_p=44555/udp, resp_h=172.16.238.2, resp_p=53/udp], 1, TABLE_ELEMENT_EXPIRED
|
||||||
|
expired [orig_h=172.16.238.131, orig_p=33109/udp, resp_h=172.16.238.2, resp_p=53/udp]
|
||||||
|
change_function, [orig_h=172.16.238.131, orig_p=33109/udp, resp_h=172.16.238.2, resp_p=53/udp], 1, TABLE_ELEMENT_EXPIRED
|
||||||
|
expired [orig_h=172.16.238.131, orig_p=45140/udp, resp_h=172.16.238.2, resp_p=53/udp]
|
||||||
|
change_function, [orig_h=172.16.238.131, orig_p=45140/udp, resp_h=172.16.238.2, resp_p=53/udp], 1, TABLE_ELEMENT_EXPIRED
|
||||||
|
expired [orig_h=172.16.238.131, orig_p=57272/udp, resp_h=172.16.238.2, resp_p=53/udp]
|
||||||
|
change_function, [orig_h=172.16.238.131, orig_p=57272/udp, resp_h=172.16.238.2, resp_p=53/udp], 1, TABLE_ELEMENT_EXPIRED
|
||||||
|
expired [orig_h=172.16.238.131, orig_p=54304/udp, resp_h=172.16.238.2, resp_p=53/udp]
|
||||||
|
change_function, [orig_h=172.16.238.131, orig_p=54304/udp, resp_h=172.16.238.2, resp_p=53/udp], 1, TABLE_ELEMENT_EXPIRED
|
||||||
|
expired [orig_h=172.16.238.131, orig_p=37846/udp, resp_h=172.16.238.2, resp_p=53/udp]
|
||||||
|
change_function, [orig_h=172.16.238.131, orig_p=37846/udp, resp_h=172.16.238.2, resp_p=53/udp], 1, TABLE_ELEMENT_EXPIRED
|
||||||
|
expired [orig_h=172.16.238.131, orig_p=55515/tcp, resp_h=74.125.225.81, resp_p=80/tcp]
|
||||||
|
change_function, [orig_h=172.16.238.131, orig_p=55515/tcp, resp_h=74.125.225.81, resp_p=80/tcp], 1, TABLE_ELEMENT_EXPIRED
|
||||||
|
expired [orig_h=172.16.238.131, orig_p=51970/udp, resp_h=172.16.238.2, resp_p=53/udp]
|
||||||
|
change_function, [orig_h=172.16.238.131, orig_p=51970/udp, resp_h=172.16.238.2, resp_p=53/udp], 1, TABLE_ELEMENT_EXPIRED
|
||||||
|
expired [orig_h=172.16.238.131, orig_p=50205/udp, resp_h=172.16.238.2, resp_p=53/udp]
|
||||||
|
change_function, [orig_h=172.16.238.131, orig_p=50205/udp, resp_h=172.16.238.2, resp_p=53/udp], 1, TABLE_ELEMENT_EXPIRED
|
||||||
|
expired [orig_h=172.16.238.131, orig_p=53102/udp, resp_h=172.16.238.2, resp_p=53/udp]
|
||||||
|
change_function, [orig_h=172.16.238.131, orig_p=53102/udp, resp_h=172.16.238.2, resp_p=53/udp], 1, TABLE_ELEMENT_EXPIRED
|
||||||
|
expired [orig_h=172.16.238.131, orig_p=33818/udp, resp_h=172.16.238.2, resp_p=53/udp]
|
||||||
|
change_function, [orig_h=172.16.238.131, orig_p=33818/udp, resp_h=172.16.238.2, resp_p=53/udp], 1, TABLE_ELEMENT_EXPIRED
|
||||||
|
expired [orig_h=172.16.238.131, orig_p=48621/udp, resp_h=172.16.238.2, resp_p=53/udp]
|
||||||
|
change_function, [orig_h=172.16.238.131, orig_p=48621/udp, resp_h=172.16.238.2, resp_p=53/udp], 1, TABLE_ELEMENT_EXPIRED
|
||||||
|
expired [orig_h=172.16.238.131, orig_p=55368/udp, resp_h=172.16.238.2, resp_p=53/udp]
|
||||||
|
change_function, [orig_h=172.16.238.131, orig_p=55368/udp, resp_h=172.16.238.2, resp_p=53/udp], 1, TABLE_ELEMENT_EXPIRED
|
||||||
|
expired [orig_h=172.16.238.131, orig_p=59573/udp, resp_h=172.16.238.2, resp_p=53/udp]
|
||||||
|
change_function, [orig_h=172.16.238.131, orig_p=59573/udp, resp_h=172.16.238.2, resp_p=53/udp], 1, TABLE_ELEMENT_EXPIRED
|
||||||
|
expired [orig_h=172.16.238.131, orig_p=52952/udp, resp_h=172.16.238.2, resp_p=53/udp]
|
||||||
|
change_function, [orig_h=172.16.238.131, orig_p=52952/udp, resp_h=172.16.238.2, resp_p=53/udp], 1, TABLE_ELEMENT_EXPIRED
|
||||||
|
change_function, [orig_h=172.16.238.131, orig_p=54935/udp, resp_h=172.16.238.2, resp_p=53/udp], 1, TABLE_ELEMENT_NEW
|
||||||
|
change_function, [orig_h=172.16.238.131, orig_p=33624/udp, resp_h=172.16.238.2, resp_p=53/udp], 1, TABLE_ELEMENT_NEW
|
||||||
|
change_function, [orig_h=172.16.238.131, orig_p=45908/tcp, resp_h=141.142.192.39, resp_p=22/tcp], 1, TABLE_ELEMENT_NEW
|
||||||
|
change_function, [orig_h=172.16.238.131, orig_p=56214/udp, resp_h=172.16.238.2, resp_p=53/udp], 1, TABLE_ELEMENT_NEW
|
||||||
|
change_function, [orig_h=172.16.238.131, orig_p=38118/udp, resp_h=172.16.238.2, resp_p=53/udp], 1, TABLE_ELEMENT_NEW
|
||||||
|
change_function, [orig_h=172.16.238.131, orig_p=37934/udp, resp_h=172.16.238.2, resp_p=53/udp], 1, TABLE_ELEMENT_NEW
|
||||||
|
change_function, [orig_h=172.16.238.131, orig_p=36682/udp, resp_h=172.16.238.2, resp_p=53/udp], 1, TABLE_ELEMENT_NEW
|
||||||
|
change_function, [orig_h=172.16.238.131, orig_p=46552/udp, resp_h=172.16.238.2, resp_p=53/udp], 1, TABLE_ELEMENT_NEW
|
||||||
|
change_function, [orig_h=172.16.238.131, orig_p=58367/udp, resp_h=172.16.238.2, resp_p=53/udp], 1, TABLE_ELEMENT_NEW
|
||||||
|
change_function, [orig_h=172.16.238.131, orig_p=42269/udp, resp_h=172.16.238.2, resp_p=53/udp], 1, TABLE_ELEMENT_NEW
|
||||||
|
change_function, [orig_h=172.16.238.131, orig_p=56485/udp, resp_h=172.16.238.2, resp_p=53/udp], 1, TABLE_ELEMENT_NEW
|
||||||
|
change_function, [orig_h=172.16.238.131, orig_p=39723/udp, resp_h=172.16.238.2, resp_p=53/udp], 1, TABLE_ELEMENT_NEW
|
||||||
|
change_function, [orig_h=172.16.238.131, orig_p=123/udp, resp_h=69.50.219.51, resp_p=123/udp], 1, TABLE_ELEMENT_NEW
|
25
testing/btest/language/on_change_expire.test
Normal file
25
testing/btest/language/on_change_expire.test
Normal file
|
@ -0,0 +1,25 @@
|
||||||
|
# @TEST-EXEC: zeek -C -r $TRACES/var-services-std-ports.trace %INPUT >output
|
||||||
|
# @TEST-EXEC: btest-diff output
|
||||||
|
|
||||||
|
function inform_me(s: table[string] of count, idx: string): interval
|
||||||
|
{
|
||||||
|
print fmt("expired %s", idx);
|
||||||
|
return 0secs;
|
||||||
|
}
|
||||||
|
|
||||||
|
function change_function(t: table[string] of count, tpe: TableChange, idx: string, val: count)
|
||||||
|
{
|
||||||
|
print "change_function", idx, val, tpe;
|
||||||
|
}
|
||||||
|
|
||||||
|
global s: table[string] of count &create_expire=1secs &expire_func=inform_me &on_change=change_function;
|
||||||
|
|
||||||
|
event new_connection(c: connection)
|
||||||
|
{
|
||||||
|
s[fmt("%s", c$id)] = 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
event zeek_init()
|
||||||
|
{
|
||||||
|
s["a"] = 5;
|
||||||
|
}
|
Loading…
Add table
Add a link
Reference in a new issue