plugin/Manager: Fix MetaHookPre and MetaHookPost using HOOK_CALL_FUNCTION

This commit is contained in:
Arne Welzel 2025-04-08 10:46:53 +02:00
parent 3b478ddc0a
commit 6d97d5526a
10 changed files with 166 additions and 2 deletions

View file

@ -962,13 +962,13 @@ void Manager::HookUnprocessedPacket(const Packet* packet) const {
}
void Manager::MetaHookPre(HookType hook, const HookArgumentList& args) const {
if ( hook_list* l = hooks[HOOK_CALL_FUNCTION] )
if ( hook_list* l = hooks[META_HOOK_PRE] )
for ( const auto& [hook_type, plugin] : *l )
plugin->MetaHookPre(hook, args);
}
void Manager::MetaHookPost(HookType hook, const HookArgumentList& args, const HookArgument& result) const {
if ( hook_list* l = hooks[HOOK_CALL_FUNCTION] )
if ( hook_list* l = hooks[META_HOOK_POST] )
for ( const auto& [hook_type, plugin] : *l )
plugin->MetaHookPost(hook, args, result);
}

View file

@ -0,0 +1,11 @@
### BTest baseline data generated by btest-diff. Do not edit. Use "btest -U/-u" to update. Requires BTest >= 0.63.
0.000000 MetaHookPre QueueEvent(zeek_init())
0.000000 HookQueueEvent zeek_init()
0.000000 MetaHookPost QueueEvent(zeek_init()) -> false
zeek_init()
0.000000 MetaHookPre QueueEvent(net_done(1.0))
0.000000 HookQueueEvent net_done()
0.000000 MetaHookPost QueueEvent(net_done(1.0)) -> false
0.000000 MetaHookPre QueueEvent(Broker::log_flush())
0.000000 HookQueueEvent Broker::log_flush()
0.000000 MetaHookPost QueueEvent(Broker::log_flush()) -> false

View file

@ -0,0 +1,5 @@
### BTest baseline data generated by btest-diff. Do not edit. Use "btest -U/-u" to update. Requires BTest >= 0.63.
0.000000 HookQueueEvent zeek_init()
zeek_init()
0.000000 HookQueueEvent net_done()
0.000000 HookQueueEvent Broker::log_flush()

View file

@ -0,0 +1,8 @@
### BTest baseline data generated by btest-diff. Do not edit. Use "btest -U/-u" to update. Requires BTest >= 0.63.
0.000000 HookQueueEvent zeek_init()
0.000000 MetaHookPost QueueEvent() -> false
zeek_init()
0.000000 HookQueueEvent net_done()
0.000000 MetaHookPost QueueEvent() -> false
0.000000 HookQueueEvent Broker::log_flush()
0.000000 MetaHookPost QueueEvent() -> false

View file

@ -0,0 +1,8 @@
### BTest baseline data generated by btest-diff. Do not edit. Use "btest -U/-u" to update. Requires BTest >= 0.63.
0.000000 MetaHookPre QueueEvent(zeek_init())
0.000000 HookQueueEvent zeek_init()
zeek_init()
0.000000 MetaHookPre QueueEvent(net_done(1.0))
0.000000 HookQueueEvent net_done()
0.000000 MetaHookPre QueueEvent(Broker::log_flush())
0.000000 HookQueueEvent Broker::log_flush()

View file

@ -0,0 +1,5 @@
### BTest baseline data generated by btest-diff. Do not edit. Use "btest -U/-u" to update. Requires BTest >= 0.63.
0.000000 HookQueueEvent zeek_init()
zeek_init()
0.000000 HookQueueEvent net_done()
0.000000 HookQueueEvent Broker::log_flush()

View file

@ -0,0 +1,83 @@
#include "Plugin.h"
#include <zeek/Desc.h>
#include <zeek/Event.h>
#include <zeek/Func.h>
#include <zeek/threading/Formatter.h>
#include <cstdlib>
namespace btest::plugin::Demo_Meta_Hooks {
Plugin plugin;
}
using namespace btest::plugin::Demo_Meta_Hooks;
zeek::plugin::Configuration Plugin::Configure() {
zeek::plugin::Configuration config;
config.name = "Demo::Meta_Hooks";
config.description = "Test if the meta hooks are working";
config.version.major = 1;
config.version.minor = 0;
config.version.patch = 0;
// This plugin enables HookQueueEvent() and optionally the pre and post
// meta hooks controlled by environment variables for easier testing.
EnableHook(zeek::plugin::HOOK_QUEUE_EVENT);
if ( getenv("TEST_META_HOOK_PRE") )
EnableHook(zeek::plugin::META_HOOK_PRE);
if ( getenv("TEST_META_HOOK_POST") )
EnableHook(zeek::plugin::META_HOOK_POST);
return config;
}
static void describe_hook_args(const zeek::plugin::HookArgumentList& args, zeek::ODesc* d) {
bool first = true;
for ( const auto& arg : args ) {
if ( ! first )
d->Add(", ");
arg.Describe(d);
first = false;
}
}
bool Plugin::HookQueueEvent(zeek::Event* e) {
fprintf(stdout, "%.6f %-15s %s()\n", zeek::run_state::network_time, " HookQueueEvent", e->Handler()->Name());
return false;
}
void Plugin::MetaHookPre(zeek::plugin::HookType hook, const zeek::plugin::HookArgumentList& args) {
// The spicy integration enables HOOK_LOAD_FILE and this plugin receives
// meta hooks also for that :-/
if ( hook != zeek::plugin::HOOK_QUEUE_EVENT )
return;
zeek::ODesc d;
d.SetShort();
describe_hook_args(args, &d);
fprintf(stdout, "%.6f %-15s %s(%s)\n", zeek::run_state::network_time, " MetaHookPre", hook_name(hook),
d.Description());
}
void Plugin::MetaHookPost(zeek::plugin::HookType hook, const zeek::plugin::HookArgumentList& args,
zeek::plugin::HookArgument result) {
// The spicy integration enables HOOK_LOAD_FILE and this plugin receives
// meta hooks also for that :-/
if ( hook != zeek::plugin::HOOK_QUEUE_EVENT )
return;
zeek::ODesc d1;
zeek::ODesc d2;
describe_hook_args(args, &d1);
result.Describe(&d2);
fprintf(stdout, "%.6f %-15s %s(%s) -> %s\n", zeek::run_state::network_time, " MetaHookPost", hook_name(hook),
d1.Description(), d2.Description());
}

View file

@ -0,0 +1,21 @@
#pragma once
#include <zeek/plugin/Plugin.h>
namespace btest::plugin::Demo_Meta_Hooks {
class Plugin : public zeek::plugin::Plugin {
protected:
bool HookQueueEvent(zeek::Event* e) override;
void MetaHookPre(zeek::plugin::HookType hook, const zeek::plugin::HookArgumentList& args) override;
void MetaHookPost(zeek::plugin::HookType hook, const zeek::plugin::HookArgumentList& args,
zeek::plugin::HookArgument result) override;
// Overridden from plugin::Plugin.
zeek::plugin::Configuration Configure() override;
};
extern Plugin plugin;
} // namespace btest::plugin::Demo_Meta_Hooks

View file

@ -0,0 +1,23 @@
# @TEST-DOC: Plugin testing the meta hooks specifically. This is a regression test for these being enabled with HookCallFunction() instead.
#
# @TEST-EXEC: ${DIST}/auxil/zeek-aux/plugin-support/init-plugin -u . Demo Meta_Hooks
# @TEST-EXEC: cp -r %DIR/meta-hook-plugin/* .
# @TEST-EXEC: ./configure --zeek-dist=${DIST} && make
# @TEST-EXEC: ZEEK_PLUGIN_PATH=`pwd` zeek -b %INPUT >out-none
# @TEST-EXEC: TEST_META_HOOK_PRE=1 ZEEK_PLUGIN_PATH=`pwd` zeek -b %INPUT >out-pre-only
# @TEST-EXEC: TEST_META_HOOK_POST=1 ZEEK_PLUGIN_PATH=`pwd` zeek -b %INPUT >out-post-only
# @TEST-EXEC: TEST_META_HOOK_PRE=1 TEST_META_HOOK_POST=1 ZEEK_PLUGIN_PATH=`pwd` zeek -b %INPUT >out-both
#
# @TEST-EXEC: btest-diff out-none
# @TEST-EXEC: btest-diff out-pre-only
# @TEST-EXEC: btest-diff out-post-only
# @TEST-EXEC: btest-diff out-both
@load-plugin Demo::Meta_Hooks
redef allow_network_time_forward = F;
event zeek_init()
{
print "zeek_init()";
}