Reformat the world

This commit is contained in:
Tim Wojtulewicz 2021-09-16 15:35:39 -07:00
parent 194cb24547
commit b2f171ec69
714 changed files with 35149 additions and 35203 deletions

View file

@ -5,7 +5,8 @@
#include "zeek/Desc.h"
#include "zeek/Reporter.h"
namespace zeek::plugin {
namespace zeek::plugin
{
Component::Component(component::Type arg_type, const std::string& arg_name)
{
@ -14,9 +15,7 @@ Component::Component(component::Type arg_type, const std::string& arg_name)
canon_name = util::canonify_name(name);
}
Component::~Component()
{
}
Component::~Component() { }
const std::string& Component::Name() const
{
@ -33,48 +32,49 @@ void Component::Describe(ODesc* d) const
d->Add(" ");
d->Add("[");
switch ( type ) {
case component::READER:
d->Add("Reader");
break;
switch ( type )
{
case component::READER:
d->Add("Reader");
break;
case component::WRITER:
d->Add("Writer");
break;
case component::WRITER:
d->Add("Writer");
break;
case component::ANALYZER:
d->Add("Analyzer");
break;
case component::ANALYZER:
d->Add("Analyzer");
break;
case component::PACKET_ANALYZER:
d->Add("Packet Analyzer");
break;
case component::PACKET_ANALYZER:
d->Add("Packet Analyzer");
break;
case component::FILE_ANALYZER:
d->Add("File Analyzer");
break;
case component::FILE_ANALYZER:
d->Add("File Analyzer");
break;
case component::IOSOURCE:
d->Add("I/O Source");
break;
case component::IOSOURCE:
d->Add("I/O Source");
break;
case component::PKTSRC:
d->Add("Packet Source");
break;
case component::PKTSRC:
d->Add("Packet Source");
break;
case component::PKTDUMPER:
d->Add("Packet Dumper");
break;
case component::PKTDUMPER:
d->Add("Packet Dumper");
break;
case component::SESSION_ADAPTER:
d->Add("Session Adapter");
break;
case component::SESSION_ADAPTER:
d->Add("Session Adapter");
break;
default:
reporter->InternalWarning("unknown component type in plugin::Component::Describe");
d->Add("<unknown component type>");
break;
}
default:
reporter->InternalWarning("unknown component type in plugin::Component::Describe");
d->Add("<unknown component type>");
break;
}
d->Add("]");
d->Add(" ");
@ -84,4 +84,4 @@ void Component::Describe(ODesc* d) const
d->Add(")");
}
} // namespace zeek::plugin
} // namespace zeek::plugin

View file

@ -2,32 +2,37 @@
#pragma once
#include "zeek/zeek-config.h"
#include <string>
namespace zeek {
#include "zeek/zeek-config.h"
namespace zeek
{
class ODesc;
namespace plugin {
namespace component {
namespace plugin
{
namespace component
{
/**
* Component types.
*/
enum Type {
READER, /// An input reader (not currently used).
WRITER, /// A logging writer (not currently used).
ANALYZER, /// A protocol analyzer.
PACKET_ANALYZER, /// A packet analyzer.
FILE_ANALYZER, /// A file analyzer.
IOSOURCE, /// An I/O source, excluding packet sources.
PKTSRC, /// A packet source.
enum Type
{
READER, /// An input reader (not currently used).
WRITER, /// A logging writer (not currently used).
ANALYZER, /// A protocol analyzer.
PACKET_ANALYZER, /// A packet analyzer.
FILE_ANALYZER, /// A file analyzer.
IOSOURCE, /// An I/O source, excluding packet sources.
PKTSRC, /// A packet source.
PKTDUMPER, /// A packet dumper.
SESSION_ADAPTER, /// A session adapter analyzer.
SESSION_ADAPTER, /// A session adapter analyzer.
};
} // namespace component
} // namespace component
/**
* Base class for plugin components. A component is a specific piece of
@ -35,7 +40,7 @@ enum Type {
* writer.
*/
class Component
{
{
public:
/**
* Constructor.
@ -57,7 +62,7 @@ public:
* plugin component functionality is used; it commonly is used to add the
* plugin component to the list of components and to initialize tags
*/
virtual void Initialize() {}
virtual void Initialize() { }
/**
* Returns the compoment's type.
@ -75,7 +80,7 @@ public:
* upper-cased and transformed to allow being part of a script-level
* ID.
*/
const std::string& CanonicalName() const { return canon_name; }
const std::string& CanonicalName() const { return canon_name; }
/**
* Returns a textual representation of the component. This goes into
@ -95,8 +100,8 @@ protected:
* The default version does nothing.
*
* @param d The description object to use.
*/
virtual void DoDescribe(ODesc* d) const { }
*/
virtual void DoDescribe(ODesc* d) const { }
private:
// Disable.
@ -106,7 +111,7 @@ private:
component::Type type;
std::string name;
std::string canon_name;
};
};
} // namespace plugin
} // namespace zeek
} // namespace plugin
} // namespace zeek

View file

@ -1,18 +1,19 @@
#pragma once
#include <map>
#include <list>
#include <map>
#include <string>
#include "zeek/Type.h"
#include "zeek/Var.h" // for add_type()
#include "zeek/Val.h"
#include "zeek/DebugLogger.h"
#include "zeek/Reporter.h"
#include "zeek/Scope.h"
#include "zeek/Type.h"
#include "zeek/Val.h"
#include "zeek/Var.h" // for add_type()
#include "zeek/zeekygen/Manager.h"
#include "zeek/DebugLogger.h"
namespace zeek::plugin {
namespace zeek::plugin
{
/**
* A class that manages tracking of plugin components (e.g. analyzers) and
@ -22,10 +23,9 @@ namespace zeek::plugin {
* @tparam T A ::Tag type or derivative.
* @tparam C A plugin::TaggedComponent type derivative.
*/
template <class T, class C>
class ComponentManager {
template <class T, class C> class ComponentManager
{
public:
/**
* Constructor creates a new enum type to associate with
* a component.
@ -125,44 +125,39 @@ private:
std::map<std::string, C*> components_by_name;
std::map<T, C*> components_by_tag;
std::map<int, C*> components_by_val;
};
};
template <class T, class C>
ComponentManager<T, C>::ComponentManager(const std::string& arg_module, const std::string& local_id)
: module(arg_module),
tag_enum_type(make_intrusive<EnumType>(module + "::" + local_id))
: module(arg_module), tag_enum_type(make_intrusive<EnumType>(module + "::" + local_id))
{
auto id = zeek::detail::install_ID(local_id.c_str(), module.c_str(), true, true);
zeek::detail::add_type(id.get(), tag_enum_type, nullptr);
zeek::detail::zeekygen_mgr->Identifier(std::move(id));
}
template <class T, class C>
const std::string& ComponentManager<T, C>::GetModule() const
template <class T, class C> const std::string& ComponentManager<T, C>::GetModule() const
{
return module;
}
template <class T, class C>
std::list<C*> ComponentManager<T, C>::GetComponents() const
template <class T, class C> std::list<C*> ComponentManager<T, C>::GetComponents() const
{
std::list<C*> rval;
typename std::map<T, C*>::const_iterator i;
for ( i = components_by_tag.begin(); i != components_by_tag.end(); ++i )
rval.push_back(i->second);
rval.push_back(i->second);
return rval;
}
template <class T, class C>
const EnumTypePtr& ComponentManager<T, C>::GetTagType() const
template <class T, class C> const EnumTypePtr& ComponentManager<T, C>::GetTagType() const
{
return tag_enum_type;
}
template <class T, class C>
const std::string& ComponentManager<T, C>::GetComponentName(T tag) const
template <class T, class C> const std::string& ComponentManager<T, C>::GetComponentName(T tag) const
{
static const std::string error = "<error>";
@ -174,8 +169,7 @@ const std::string& ComponentManager<T, C>::GetComponentName(T tag) const
if ( c )
return c->CanonicalName();
reporter->InternalWarning("requested name of unknown component tag %s",
tag.AsString().c_str());
reporter->InternalWarning("requested name of unknown component tag %s", tag.AsString().c_str());
return error;
}
@ -185,66 +179,57 @@ const std::string& ComponentManager<T, C>::GetComponentName(EnumValPtr val) cons
return GetComponentName(T(std::move(val)));
}
template <class T, class C>
T ComponentManager<T, C>::GetComponentTag(const std::string& name) const
template <class T, class C> T ComponentManager<T, C>::GetComponentTag(const std::string& name) const
{
C* c = Lookup(name);
return c ? c->Tag() : T();
}
template <class T, class C>
T ComponentManager<T, C>::GetComponentTag(Val* v) const
template <class T, class C> T ComponentManager<T, C>::GetComponentTag(Val* v) const
{
C* c = Lookup(v->AsEnumVal());
return c ? c->Tag() : T();
}
template <class T, class C>
C* ComponentManager<T, C>::Lookup(const std::string& name) const
template <class T, class C> C* ComponentManager<T, C>::Lookup(const std::string& name) const
{
typename std::map<std::string, C*>::const_iterator i =
components_by_name.find(util::to_upper(name));
return i != components_by_name.end() ? i->second : 0;
}
template <class T, class C>
C* ComponentManager<T, C>::Lookup(const T& tag) const
template <class T, class C> C* ComponentManager<T, C>::Lookup(const T& tag) const
{
typename std::map<T, C*>::const_iterator i = components_by_tag.find(tag);
return i != components_by_tag.end() ? i->second : 0;
}
template <class T, class C>
C* ComponentManager<T, C>::Lookup(EnumVal* val) const
template <class T, class C> C* ComponentManager<T, C>::Lookup(EnumVal* val) const
{
typename std::map<int, C*>::const_iterator i =
components_by_val.find(val->InternalInt());
typename std::map<int, C*>::const_iterator i = components_by_val.find(val->InternalInt());
return i != components_by_val.end() ? i->second : 0;
}
template <class T, class C>
void ComponentManager<T, C>::RegisterComponent(C* component,
const std::string& prefix)
void ComponentManager<T, C>::RegisterComponent(C* component, const std::string& prefix)
{
std::string cname = component->CanonicalName();
if ( Lookup(cname) )
reporter->FatalError("Component '%s::%s' defined more than once",
module.c_str(), cname.c_str());
reporter->FatalError("Component '%s::%s' defined more than once", module.c_str(),
cname.c_str());
DBG_LOG(DBG_PLUGINS, "Registering component %s (tag %s)",
component->Name().c_str(), component->Tag().AsString().c_str());
DBG_LOG(DBG_PLUGINS, "Registering component %s (tag %s)", component->Name().c_str(),
component->Tag().AsString().c_str());
components_by_name.insert(std::make_pair(cname, component));
components_by_tag.insert(std::make_pair(component->Tag(), component));
components_by_val.insert(std::make_pair(
component->Tag().AsVal()->InternalInt(), component));
components_by_val.insert(std::make_pair(component->Tag().AsVal()->InternalInt(), component));
// Install an identfier for enum value
std::string id = util::fmt("%s%s", prefix.c_str(), cname.c_str());
tag_enum_type->AddName(module, id.c_str(),
component->Tag().AsVal()->InternalInt(), true,
tag_enum_type->AddName(module, id.c_str(), component->Tag().AsVal()->InternalInt(), true,
nullptr);
}
} // namespace zeek::plugin
} // namespace zeek::plugin

View file

@ -3,27 +3,28 @@
#include "zeek/plugin/Manager.h"
#include <dirent.h>
#include <glob.h>
#include <dlfcn.h>
#include <errno.h>
#include <sys/stat.h>
#include <glob.h>
#include <limits.h> // for PATH_MAX
#include <sys/stat.h>
#include <cstdlib>
#include <optional>
#include <sstream>
#include <fstream>
#include <optional>
#include <regex>
#include <sstream>
#include "zeek/Reporter.h"
#include "zeek/Func.h"
#include "zeek/Event.h"
#include "zeek/Func.h"
#include "zeek/Reporter.h"
#include "zeek/Val.h"
#include "zeek/util.h"
#include "zeek/input.h"
#include "zeek/util.h"
using namespace std;
namespace zeek::plugin {
namespace zeek::plugin
{
Plugin* Manager::current_plugin = nullptr;
const char* Manager::current_dir = nullptr;
@ -45,7 +46,7 @@ Manager::~Manager()
for ( int i = 0; i < NUM_HOOKS; i++ )
delete hooks[i];
delete [] hooks;
delete[] hooks;
}
void Manager::SearchDynamicPlugins(const std::string& dir)
@ -76,8 +77,8 @@ void Manager::SearchDynamicPlugins(const std::string& dir)
char canon_path[PATH_MAX];
if ( ! realpath(dir.data(), canon_path) )
{
DBG_LOG(DBG_PLUGINS, "skip dynamic plugin search in %s, realpath failed: %s",
dir.data(), strerror(errno));
DBG_LOG(DBG_PLUGINS, "skip dynamic plugin search in %s, realpath failed: %s", dir.data(),
strerror(errno));
return;
}
@ -108,7 +109,8 @@ void Manager::SearchDynamicPlugins(const std::string& dir)
if ( dynamic_plugins.find(lower_name) != dynamic_plugins.end() )
{
DBG_LOG(DBG_PLUGINS, "Found already known plugin %s in %s, ignoring", name.c_str(), dir.c_str());
DBG_LOG(DBG_PLUGINS, "Found already known plugin %s in %s, ignoring", name.c_str(),
dir.c_str());
return;
}
@ -131,19 +133,18 @@ void Manager::SearchDynamicPlugins(const std::string& dir)
bool found = false;
struct dirent *dp;
struct dirent* dp;
while ( (dp = readdir(d)) )
{
struct stat st;
if ( strcmp(dp->d_name, "..") == 0
|| strcmp(dp->d_name, ".") == 0 )
if ( strcmp(dp->d_name, "..") == 0 || strcmp(dp->d_name, ".") == 0 )
continue;
string path = dir + "/" + dp->d_name;
if( stat(path.c_str(), &st) < 0 )
if ( stat(path.c_str(), &st) < 0 )
{
DBG_LOG(DBG_PLUGINS, "Cannot stat %s: %s", path.c_str(), strerror(errno));
continue;
@ -156,7 +157,8 @@ void Manager::SearchDynamicPlugins(const std::string& dir)
closedir(d);
}
bool Manager::ActivateDynamicPluginInternal(const std::string& name, bool ok_if_not_found, std::vector<std::string>* errors)
bool Manager::ActivateDynamicPluginInternal(const std::string& name, bool ok_if_not_found,
std::vector<std::string>* errors)
{
errors->clear(); // caller should pass it in empty, but just to be sure
@ -217,21 +219,25 @@ bool Manager::ActivateDynamicPluginInternal(const std::string& name, bool ok_if_
if ( ! hdl )
{
const char* err = dlerror();
errors->push_back(util::fmt("cannot load plugin library %s: %s", path, err ? err : "<unknown error>"));
errors->push_back(util::fmt("cannot load plugin library %s: %s", path,
err ? err : "<unknown error>"));
continue;
}
if ( ! current_plugin ) {
errors->push_back(util::fmt("load plugin library %s did not instantiate a plugin", path));
if ( ! current_plugin )
{
errors->push_back(
util::fmt("load plugin library %s did not instantiate a plugin", path));
continue;
}
}
current_plugin->SetDynamic(true);
current_plugin->DoConfigure();
DBG_LOG(DBG_PLUGINS, " InitializingComponents");
current_plugin->InitializeComponents();
plugins_by_path.insert(std::make_pair(util::detail::normalize_path(dir), current_plugin));
plugins_by_path.insert(
std::make_pair(util::detail::normalize_path(dir), current_plugin));
// We execute the pre-script initialization here; this in
// fact could be *during* script initialization if we got
@ -240,11 +246,12 @@ bool Manager::ActivateDynamicPluginInternal(const std::string& name, bool ok_if_
// Make sure the name the plugin reports is consistent with
// what we expect from its magic file.
if ( util::strtolower(current_plugin->Name()) != util::strtolower(name) ) {
if ( util::strtolower(current_plugin->Name()) != util::strtolower(name) )
{
errors->push_back(util::fmt("inconsistent plugin name: %s vs %s",
current_plugin->Name().c_str(), name.c_str()));
current_plugin->Name().c_str(), name.c_str()));
continue;
}
}
current_plugin = nullptr;
DBG_LOG(DBG_PLUGINS, " Loaded %s", path);
@ -314,7 +321,8 @@ void Manager::ActivateDynamicPlugin(const std::string& name)
requested_plugins.insert(std::move(name));
}
void Manager::ActivateDynamicPlugins(bool all) {
void Manager::ActivateDynamicPlugins(bool all)
{
// Tracks plugins we need to activate as pairs of their names and booleans
// indicating whether an activation failure is to be deemed a fatal error.
std::set<std::pair<std::string, bool>> plugins_to_activate;
@ -341,7 +349,8 @@ void Manager::ActivateDynamicPlugins(bool all) {
// long as we're successful for at least one further of them each round.
// Doing so ensures that we can resolve (non-cyclic) load dependencies
// independent of any particular order.
while ( ! plugins_to_activate.empty() ) {
while ( ! plugins_to_activate.empty() )
{
std::vector<std::string> errors;
auto plugins_left = plugins_to_activate;
@ -380,7 +389,7 @@ static bool plugin_cmp(const Plugin* a, const Plugin* b)
return util::strtolower(a->Name()) < util::strtolower(b->Name());
}
void Manager::RegisterPlugin(Plugin *plugin)
void Manager::RegisterPlugin(Plugin* plugin)
{
Manager::ActivePluginsInternal()->push_back(plugin);
@ -468,7 +477,8 @@ void Manager::InitBifs()
if ( b != bifs->end() )
{
for ( bif_init_func_list::const_iterator j = b->second->begin(); j != b->second->end(); ++j )
for ( bif_init_func_list::const_iterator j = b->second->begin(); j != b->second->end();
++j )
(**j)(*i);
}
}
@ -507,7 +517,8 @@ Manager::inactive_plugin_list Manager::InactivePlugins() const
inactive_plugin_list inactives;
for ( dynamic_plugin_map::const_iterator i = dynamic_plugins.begin(); i != dynamic_plugins.end(); i++ )
for ( dynamic_plugin_map::const_iterator i = dynamic_plugins.begin();
i != dynamic_plugins.end(); i++ )
{
bool found = false;
@ -581,9 +592,9 @@ static bool hook_cmp(std::pair<int, Plugin*> a, std::pair<int, Plugin*> b)
return a.first > b.first;
}
std::list<std::pair<HookType, int> > Manager::HooksEnabledForPlugin(const Plugin* plugin) const
std::list<std::pair<HookType, int>> Manager::HooksEnabledForPlugin(const Plugin* plugin) const
{
std::list<std::pair<HookType, int> > enabled;
std::list<std::pair<HookType, int>> enabled;
for ( int i = 0; i < NUM_HOOKS; i++ )
{
@ -639,8 +650,7 @@ void Manager::DisableHook(HookType hook, Plugin* plugin)
void Manager::RequestEvent(EventHandlerPtr handler, Plugin* plugin)
{
DBG_LOG(DBG_PLUGINS, "Plugin %s requested event %s",
plugin->Name().c_str(), handler->Name());
DBG_LOG(DBG_PLUGINS, "Plugin %s requested event %s", plugin->Name().c_str(), handler->Name());
handler->SetGenerateAlways();
}
@ -682,9 +692,8 @@ int Manager::HookLoadFile(const Plugin::LoadType type, const string& file, const
return rc;
}
std::pair<bool, ValPtr>
Manager::HookCallFunction(const Func* func, zeek::detail::Frame* parent,
Args* vecargs) const
std::pair<bool, ValPtr> Manager::HookCallFunction(const Func* func, zeek::detail::Frame* parent,
Args* vecargs) const
{
HookArgumentList args;
ValPList vargs;
@ -776,10 +785,9 @@ void Manager::HookDrainEvents() const
if ( HavePluginForHook(META_HOOK_POST) )
MetaHookPost(HOOK_DRAIN_EVENTS, args, HookArgument());
}
void Manager::HookSetupAnalyzerTree(Connection *conn) const
void Manager::HookSetupAnalyzerTree(Connection* conn) const
{
HookArgumentList args;
@ -789,13 +797,13 @@ void Manager::HookSetupAnalyzerTree(Connection *conn) const
MetaHookPre(HOOK_SETUP_ANALYZER_TREE, args);
}
hook_list *l = hooks[HOOK_SETUP_ANALYZER_TREE];
hook_list* l = hooks[HOOK_SETUP_ANALYZER_TREE];
if ( l )
{
for (hook_list::iterator i = l->begin() ; i != l->end(); ++i)
for ( hook_list::iterator i = l->begin(); i != l->end(); ++i )
{
Plugin *p = (*i).second;
Plugin* p = (*i).second;
p->HookSetupAnalyzerTree(conn);
}
}
@ -852,12 +860,9 @@ void Manager::HookBroObjDtor(void* obj) const
MetaHookPost(HOOK_BRO_OBJ_DTOR, args, HookArgument());
}
void Manager::HookLogInit(const std::string& writer,
const std::string& instantiating_filter,
bool local, bool remote,
const logging::WriterBackend::WriterInfo& info,
int num_fields,
const threading::Field* const* fields) const
void Manager::HookLogInit(const std::string& writer, const std::string& instantiating_filter,
bool local, bool remote, const logging::WriterBackend::WriterInfo& info,
int num_fields, const threading::Field* const* fields) const
{
HookArgumentList args;
@ -879,20 +884,16 @@ void Manager::HookLogInit(const std::string& writer,
for ( hook_list::iterator i = l->begin(); i != l->end(); ++i )
{
Plugin* p = (*i).second;
p->HookLogInit(writer, instantiating_filter, local, remote, info,
num_fields, fields);
p->HookLogInit(writer, instantiating_filter, local, remote, info, num_fields, fields);
}
if ( HavePluginForHook(META_HOOK_POST) )
MetaHookPost(HOOK_LOG_INIT, args, HookArgument());
}
bool Manager::HookLogWrite(const std::string& writer,
const std::string& filter,
const logging::WriterBackend::WriterInfo& info,
int num_fields,
const threading::Field* const* fields,
threading::Value** vals) const
bool Manager::HookLogWrite(const std::string& writer, const std::string& filter,
const logging::WriterBackend::WriterInfo& info, int num_fields,
const threading::Field* const* fields, threading::Value** vals) const
{
HookArgumentList args;
@ -916,8 +917,7 @@ bool Manager::HookLogWrite(const std::string& writer,
{
Plugin* p = (*i).second;
if ( ! p->HookLogWrite(writer, filter, info, num_fields, fields,
vals) )
if ( ! p->HookLogWrite(writer, filter, info, num_fields, fields, vals) )
{
result = false;
break;
@ -933,8 +933,8 @@ bool Manager::HookLogWrite(const std::string& writer,
bool Manager::HookReporter(const std::string& prefix, const EventHandlerPtr event,
const Connection* conn, const ValPList* addl, bool location,
const zeek::detail::Location* location1,
const zeek::detail::Location* location2,
bool time, const std::string& message)
const zeek::detail::Location* location2, bool time,
const std::string& message)
{
HookArgumentList args;
@ -962,7 +962,8 @@ bool Manager::HookReporter(const std::string& prefix, const EventHandlerPtr even
{
Plugin* p = (*i).second;
if ( ! p->HookReporter(prefix, event, conn, addl, location, location1, location2, time, message) )
if ( ! p->HookReporter(prefix, event, conn, addl, location, location1, location2, time,
message) )
{
result = false;
break;
@ -976,7 +977,6 @@ bool Manager::HookReporter(const std::string& prefix, const EventHandlerPtr even
return result;
}
void Manager::MetaHookPre(HookType hook, const HookArgumentList& args) const
{
if ( hook_list* l = hooks[HOOK_CALL_FUNCTION] )
@ -984,11 +984,12 @@ void Manager::MetaHookPre(HookType hook, const HookArgumentList& args) const
plugin->MetaHookPre(hook, args);
}
void Manager::MetaHookPost(HookType hook, const HookArgumentList& args, const HookArgument& result) const
void Manager::MetaHookPost(HookType hook, const HookArgumentList& args,
const HookArgument& result) const
{
if ( hook_list* l = hooks[HOOK_CALL_FUNCTION] )
for ( const auto& [hook_type, plugin] : *l )
plugin->MetaHookPost(hook, args, result);
}
} // namespace zeek::plugin
} // namespace zeek::plugin

View file

@ -7,13 +7,15 @@
#include <string_view>
#include <utility>
#include "zeek/plugin/Plugin.h"
#include "zeek/plugin/Component.h"
#include "zeek/Reporter.h"
#include "zeek/ZeekArgs.h"
#include "zeek/plugin/Component.h"
#include "zeek/plugin/Plugin.h"
namespace zeek {
namespace plugin {
namespace zeek
{
namespace plugin
{
// Macros that trigger plugin hooks. We put this into macros to short-cut the
// code for the most common case that no plugin defines the hook.
@ -25,8 +27,11 @@ namespace plugin {
*
* @param method_call The \a Manager method corresponding to the hook.
*/
#define PLUGIN_HOOK_VOID(hook, method_call) \
{ if ( zeek::plugin_mgr->HavePluginForHook(zeek::plugin::hook) ) zeek::plugin_mgr->method_call; }
#define PLUGIN_HOOK_VOID(hook, method_call) \
{ \
if ( zeek::plugin_mgr->HavePluginForHook(zeek::plugin::hook) ) \
zeek::plugin_mgr->method_call; \
}
/**
* Macro to trigger hooks that return a result.
@ -38,16 +43,17 @@ namespace plugin {
* @param default_result: The result to use if there's no plugin implementing
* the hook.
*/
#define PLUGIN_HOOK_WITH_RESULT(hook, method_call, default_result) \
(zeek::plugin_mgr->HavePluginForHook(zeek::plugin::hook) ? zeek::plugin_mgr->method_call : (default_result))
#define PLUGIN_HOOK_WITH_RESULT(hook, method_call, default_result) \
(zeek::plugin_mgr->HavePluginForHook(zeek::plugin::hook) ? zeek::plugin_mgr->method_call \
: (default_result))
/**
* A singleton object managing all plugins.
*/
class Manager
{
{
public:
typedef void (*bif_init_func)(Plugin *);
typedef void (*bif_init_func)(Plugin*);
using plugin_list = std::list<Plugin*>;
using component_list = Plugin::component_list;
using inactive_plugin_list = std::list<std::pair<std::string, std::string>>;
@ -146,7 +152,7 @@ public:
* are derived from a specific class. The class is given as the
* template parameter \c T.
*/
template<class T> std::list<T *> Components() const;
template <class T> std::list<T*> Components() const;
/**
* Returns the (dynamic) plugin associated with a given filesytem
@ -175,7 +181,7 @@ public:
*
* @param plugin The plugin to return the hooks for.
*/
std::list<std::pair<HookType, int> > HooksEnabledForPlugin(const Plugin* plugin) const;
std::list<std::pair<HookType, int>> HooksEnabledForPlugin(const Plugin* plugin) const;
/**
* Enables a hook for a given plugin.
@ -236,7 +242,8 @@ public:
* if a plugin took over the file but had trouble loading it; and -1 if
* no plugin was interested in the file at all.
*/
virtual int HookLoadFile(const Plugin::LoadType type, const std::string& file, const std::string& resolved);
virtual int HookLoadFile(const Plugin::LoadType type, const std::string& file,
const std::string& resolved);
/**
* Hook that filters calls to a script function/event/hook.
@ -253,8 +260,8 @@ public:
* it may be any Val and must be ignored). If no plugin handled the call,
* the method returns null.
*/
std::pair<bool, ValPtr>
HookCallFunction(const Func* func, zeek::detail::Frame* parent, Args* args) const;
std::pair<bool, ValPtr> HookCallFunction(const Func* func, zeek::detail::Frame* parent,
Args* args) const;
/**
* Hook that filters the queuing of an event.
@ -280,7 +287,7 @@ public:
*
* @param conn The connection.
*/
void HookSetupAnalyzerTree(Connection *conn) const;
void HookSetupAnalyzerTree(Connection* conn) const;
/**
* Hook that informs plugins that the event queue is being drained.
@ -318,11 +325,8 @@ public:
*
* @param fields threading::Field description of the fields being logged.
*/
void HookLogInit(const std::string& writer,
const std::string& instantiating_filter,
bool local, bool remote,
const logging::WriterBackend::WriterInfo& info,
int num_fields,
void HookLogInit(const std::string& writer, const std::string& instantiating_filter, bool local,
bool remote, const logging::WriterBackend::WriterInfo& info, int num_fields,
const threading::Field* const* fields) const;
/**
@ -351,11 +355,9 @@ public:
* @return true if log line should be written, false if log line should be
* skipped and not passed on to the writer.
*/
bool HookLogWrite(const std::string& writer,
const std::string& filter,
const logging::WriterBackend::WriterInfo& info,
int num_fields, const threading::Field* const* fields,
threading::Value** vals) const;
bool HookLogWrite(const std::string& writer, const std::string& filter,
const logging::WriterBackend::WriterInfo& info, int num_fields,
const threading::Field* const* fields, threading::Value** vals) const;
/**
* Hook into reporting. This method will be called for each reporter call
@ -387,8 +389,9 @@ public:
*/
bool HookReporter(const std::string& prefix, const EventHandlerPtr event,
const Connection* conn, const ValPList* addl, bool location,
const zeek::detail::Location* location1, const zeek::detail::Location* location2,
bool time, const std::string& message);
const zeek::detail::Location* location1,
const zeek::detail::Location* location2, bool time,
const std::string& message);
/**
* Internal method that registers a freshly instantiated plugin with
@ -417,10 +420,12 @@ public:
void ExtendZeekPathForPlugins();
private:
bool ActivateDynamicPluginInternal(const std::string& name, bool ok_if_not_found, std::vector<std::string>* errors);
bool ActivateDynamicPluginInternal(const std::string& name, bool ok_if_not_found,
std::vector<std::string>* errors);
void UpdateInputFiles();
void MetaHookPre(HookType hook, const HookArgumentList& args) const;
void MetaHookPost(HookType hook, const HookArgumentList& args, const HookArgument& result) const;
void MetaHookPost(HookType hook, const HookArgumentList& args,
const HookArgument& result) const;
// Directories that have already been searched for dynamic plugins.
// Used to prevent multiple searches of the same dirs (e.g. via symlinks).
@ -431,7 +436,7 @@ private:
// load at first.
std::set<std::string> requested_plugins;
// All found dynamic plugins, mapping their names to base directory.
// All found dynamic plugins, mapping their names to base directory.
using dynamic_plugin_map = std::map<std::string, std::string>;
dynamic_plugin_map dynamic_plugins;
@ -440,7 +445,7 @@ private:
using file_list = std::list<std::string>;
file_list scripts_to_load;
bool init; // Flag indicating whether InitPreScript() has run yet.
bool init; // Flag indicating whether InitPreScript() has run yet.
// A hook list keeps pairs of plugin and priority interested in a
// given hook.
@ -470,20 +475,20 @@ private:
// so that plugins can register their bifs even before the manager
// exists.
static bif_init_func_map* BifFilesInternal();
};
};
template<class T>
std::list<T *> Manager::Components() const
template <class T> std::list<T*> Manager::Components() const
{
std::list<T *> result;
std::list<T*> result;
for ( plugin_list::const_iterator p = ActivePluginsInternal()->begin(); p != ActivePluginsInternal()->end(); p++ )
for ( plugin_list::const_iterator p = ActivePluginsInternal()->begin();
p != ActivePluginsInternal()->end(); p++ )
{
component_list components = (*p)->Components();
for ( component_list::const_iterator c = components.begin(); c != components.end(); c++ )
{
T* t = dynamic_cast<T *>(*c);
T* t = dynamic_cast<T*>(*c);
if ( t )
result.push_back(t);
@ -493,22 +498,24 @@ std::list<T *> Manager::Components() const
return result;
}
namespace detail {
namespace detail
{
/**
* Internal class used by bifcl-generated code to register its init functions at runtime.
*/
class __RegisterBif {
class __RegisterBif
{
public:
__RegisterBif(const char* plugin, Manager::bif_init_func init)
{
Manager::RegisterBifFile(plugin, init);
}
};
};
} // namespace detail
} // namespace plugin
} // namespace detail
} // namespace plugin
extern plugin::Manager* plugin_mgr;
} // namespace zeek
} // namespace zeek

View file

@ -4,21 +4,22 @@
#include <cassert>
#include "zeek/plugin/Manager.h"
#include "zeek/plugin/Component.h"
#include "zeek/Val.h"
#include "zeek/Conn.h"
#include "zeek/Desc.h"
#include "zeek/Event.h"
#include "zeek/Func.h"
#include "zeek/Conn.h"
#include "zeek/Val.h"
#include "zeek/input.h"
#include "zeek/plugin/Component.h"
#include "zeek/plugin/Manager.h"
#include "zeek/threading/SerialTypes.h"
namespace zeek::plugin {
namespace zeek::plugin
{
const char* hook_name(HookType h)
{
static constexpr const char* hook_names[int(NUM_HOOKS) + 1] = {
{
static constexpr const char* hook_names[int(NUM_HOOKS) + 1] = {
// Order must match that of HookType.
"LoadFile",
"CallFunction",
@ -62,175 +63,174 @@ BifItem& BifItem::operator=(const BifItem& other)
return *this;
}
BifItem::~BifItem()
{
}
BifItem::~BifItem() { }
void HookArgument::Describe(ODesc* d) const
{
switch ( type ) {
case BOOL:
d->Add(arg.bool_ ? "true" : "false");
break;
switch ( type )
{
case BOOL:
d->Add(arg.bool_ ? "true" : "false");
break;
case DOUBLE:
d->Add(arg.double_);
break;
case DOUBLE:
d->Add(arg.double_);
break;
case EVENT:
if ( arg.event )
{
d->Add(arg.event->Handler()->Name());
d->Add("(");
describe_vals(arg.event->Args(), d);
d->Add(")");
}
else
d->Add("<null>");
break;
case CONN:
if ( arg.conn )
arg.conn->Describe(d);
break;
case FUNC_RESULT:
if ( func_result.first )
{
if( func_result.second )
func_result.second->Describe(d);
case EVENT:
if ( arg.event )
{
d->Add(arg.event->Handler()->Name());
d->Add("(");
describe_vals(arg.event->Args(), d);
d->Add(")");
}
else
d->Add("<null>");
}
else
d->Add("<no result>");
break;
break;
case CONN:
if ( arg.conn )
arg.conn->Describe(d);
break;
case FRAME:
if ( arg.frame )
d->Add("<frame>");
else
d->Add("<null>");
break;
case FUNC:
if ( arg.func )
d->Add(arg.func->Name());
else
d->Add("<null>");
break;
case INT:
d->Add(arg.int_);
break;
case STRING:
d->Add(arg_string);
break;
case VAL:
if ( arg.val )
arg.val->Describe(d);
else
d->Add("<null>");
break;
case VAL_LIST:
if ( arg.vals )
{
d->Add("(");
describe_vals(arg.vals, d);
d->Add(")");
}
else
d->Add("<null>");
break;
case ARG_LIST:
if ( arg.args)
{
d->Add("(");
describe_vals(*arg.args, d);
d->Add(")");
}
else
d->Add("<null>");
break;
case VOID:
d->Add("<void>");
break;
case VOIDP:
d->Add("<void ptr>");
break;
case WRITER_INFO:
{
d->Add(arg.winfo->path);
d->Add("(");
d->Add(arg.winfo->network_time);
d->Add(",");
d->Add(arg.winfo->rotation_interval);
d->Add(",");
d->Add(arg.winfo->rotation_base);
if ( arg.winfo->config.size() > 0 )
{
bool first = true;
d->Add("config: {");
for ( auto& v: arg.winfo->config )
case FUNC_RESULT:
if ( func_result.first )
{
if ( ! first )
d->Add(", ");
d->Add(v.first);
d->Add(": ");
d->Add(v.second);
first = false;
if ( func_result.second )
func_result.second->Describe(d);
else
d->Add("<null>");
}
else
d->Add("<no result>");
d->Add("}");
}
break;
d->Add(")");
case FRAME:
if ( arg.frame )
d->Add("<frame>");
else
d->Add("<null>");
break;
case FUNC:
if ( arg.func )
d->Add(arg.func->Name());
else
d->Add("<null>");
break;
case INT:
d->Add(arg.int_);
break;
case STRING:
d->Add(arg_string);
break;
case VAL:
if ( arg.val )
arg.val->Describe(d);
else
d->Add("<null>");
break;
case VAL_LIST:
if ( arg.vals )
{
d->Add("(");
describe_vals(arg.vals, d);
d->Add(")");
}
else
d->Add("<null>");
break;
case ARG_LIST:
if ( arg.args )
{
d->Add("(");
describe_vals(*arg.args, d);
d->Add(")");
}
else
d->Add("<null>");
break;
case VOID:
d->Add("<void>");
break;
case VOIDP:
d->Add("<void ptr>");
break;
case WRITER_INFO:
{
d->Add(arg.winfo->path);
d->Add("(");
d->Add(arg.winfo->network_time);
d->Add(",");
d->Add(arg.winfo->rotation_interval);
d->Add(",");
d->Add(arg.winfo->rotation_base);
if ( arg.winfo->config.size() > 0 )
{
bool first = true;
d->Add("config: {");
for ( auto& v : arg.winfo->config )
{
if ( ! first )
d->Add(", ");
d->Add(v.first);
d->Add(": ");
d->Add(v.second);
first = false;
}
d->Add("}");
}
d->Add(")");
}
break;
case THREAD_FIELDS:
{
d->Add("{");
for ( int i = 0; i < tfields.first; i++ )
{
const threading::Field* f = tfields.second[i];
if ( i > 0 )
d->Add(", ");
d->Add(f->name);
d->Add(" (");
d->Add(f->TypeName());
d->Add(")");
}
d->Add("}");
}
break;
case LOCATION:
if ( arg.loc )
{
arg.loc->Describe(d);
}
else
{
d->Add("<no location>");
}
}
break;
case THREAD_FIELDS:
{
d->Add("{");
for ( int i=0; i < tfields.first; i++ )
{
const threading::Field* f = tfields.second[i];
if ( i > 0 )
d->Add(", ");
d->Add(f->name);
d->Add(" (");
d->Add(f->TypeName());
d->Add(")");
}
d->Add("}");
}
break;
case LOCATION:
if ( arg.loc )
{
arg.loc->Describe(d);
}
else
{
d->Add("<no location>");
}
}
}
Plugin::Plugin()
@ -290,13 +290,9 @@ void Plugin::SetDynamic(bool is_dynamic)
dynamic = is_dynamic;
}
void Plugin::InitPreScript()
{
}
void Plugin::InitPreScript() { }
void Plugin::InitPostScript()
{
}
void Plugin::InitPostScript() { }
Plugin::bif_item_list Plugin::BifItems() const
{
@ -372,9 +368,8 @@ int Plugin::HookLoadFile(const LoadType type, const std::string& file, const std
return -1;
}
std::pair<bool, ValPtr>
Plugin::HookFunctionCall(const Func* func, zeek::detail::Frame* parent,
Args* args)
std::pair<bool, ValPtr> Plugin::HookFunctionCall(const Func* func, zeek::detail::Frame* parent,
Args* args)
{
return {false, nullptr};
}
@ -384,34 +379,23 @@ bool Plugin::HookQueueEvent(Event* event)
return false;
}
void Plugin::HookDrainEvents()
{
}
void Plugin::HookDrainEvents() { }
void Plugin::HookUpdateNetworkTime(double network_time)
{
}
void Plugin::HookUpdateNetworkTime(double network_time) { }
void Plugin::HookSetupAnalyzerTree(Connection *conn)
{
}
void Plugin::HookSetupAnalyzerTree(Connection* conn) { }
void Plugin::HookBroObjDtor(void* obj)
{
}
void Plugin::HookBroObjDtor(void* obj) { }
void Plugin::HookLogInit(const std::string& writer,
const std::string& instantiating_filter,
bool local, bool remote,
const logging::WriterBackend::WriterInfo& info,
void Plugin::HookLogInit(const std::string& writer, const std::string& instantiating_filter,
bool local, bool remote, const logging::WriterBackend::WriterInfo& info,
int num_fields, const threading::Field* const* fields)
{
}
bool Plugin::HookLogWrite(const std::string& writer, const std::string& filter,
const logging::WriterBackend::WriterInfo& info,
int num_fields, const threading::Field* const* fields,
threading::Value** vals)
const logging::WriterBackend::WriterInfo& info, int num_fields,
const threading::Field* const* fields, threading::Value** vals)
{
return true;
}
@ -419,19 +403,15 @@ bool Plugin::HookLogWrite(const std::string& writer, const std::string& filter,
bool Plugin::HookReporter(const std::string& prefix, const EventHandlerPtr event,
const Connection* conn, const ValPList* addl, bool location,
const zeek::detail::Location* location1,
const zeek::detail::Location* location2,
bool time, const std::string& message)
const zeek::detail::Location* location2, bool time,
const std::string& message)
{
return true;
}
void Plugin::MetaHookPre(HookType hook, const HookArgumentList& args)
{
}
void Plugin::MetaHookPre(HookType hook, const HookArgumentList& args) { }
void Plugin::MetaHookPost(HookType hook, const HookArgumentList& args, HookArgument result)
{
}
void Plugin::MetaHookPost(HookType hook, const HookArgumentList& args, HookArgument result) { }
void Plugin::InitializeComponents()
{
@ -487,30 +467,31 @@ void Plugin::Describe(ODesc* d) const
{
const char* type = nullptr;
switch ( (*i).GetType() ) {
case BifItem::FUNCTION:
type = "Function";
break;
switch ( (*i).GetType() )
{
case BifItem::FUNCTION:
type = "Function";
break;
case BifItem::EVENT:
type = "Event";
break;
case BifItem::EVENT:
type = "Event";
break;
case BifItem::CONSTANT:
type = "Constant";
break;
case BifItem::CONSTANT:
type = "Constant";
break;
case BifItem::GLOBAL:
type = "Global";
break;
case BifItem::GLOBAL:
type = "Global";
break;
case BifItem::TYPE:
type = "Type";
break;
case BifItem::TYPE:
type = "Type";
break;
default:
type = "<unknown>";
}
default:
type = "<unknown>";
}
d->Add(" ");
d->Add("[");
@ -535,4 +516,4 @@ void Plugin::Describe(ODesc* d) const
}
}
} // namespace zeek::plugin
} // namespace zeek::plugin

View file

@ -2,14 +2,13 @@
#pragma once
#include "zeek/zeek-config.h"
#include <list>
#include <string>
#include <utility>
#include "zeek/logging/WriterBackend.h"
#include "zeek/ZeekArgs.h"
#include "zeek/logging/WriterBackend.h"
#include "zeek/zeek-config.h"
// Increase this when making incompatible changes to the plugin API. Note
// that the constant is never used in C code. It's picked up on by CMake.
@ -17,9 +16,13 @@
#define BRO_PLUGIN_BRO_VERSION BRO_VERSION_FUNCTION
namespace zeek::threading { struct Field; }
namespace zeek::threading
{
struct Field;
}
namespace zeek {
namespace zeek
{
class ODesc;
class Event;
@ -29,10 +32,17 @@ class Obj;
template <class T> class IntrusivePtr;
using ValPtr = IntrusivePtr<Val>;
namespace threading { struct Field; }
namespace detail { class Frame; }
namespace threading
{
struct Field;
}
namespace detail
{
class Frame;
}
namespace plugin {
namespace plugin
{
class Manager;
class Component;
@ -42,26 +52,27 @@ class Plugin;
* Hook types that a plugin may define. Each label maps to the corresponding
* virtual method in \a Plugin.
*/
enum HookType {
enum HookType
{
// Note: when changing this table, update hook_name() in Plugin.cc.
HOOK_LOAD_FILE, //< Activates Plugin::HookLoadFile().
HOOK_CALL_FUNCTION, //< Activates Plugin::HookCallFunction().
HOOK_QUEUE_EVENT, //< Activates Plugin::HookQueueEvent().
HOOK_DRAIN_EVENTS, //< Activates Plugin::HookDrainEvents()
HOOK_UPDATE_NETWORK_TIME, //< Activates Plugin::HookUpdateNetworkTime.
HOOK_BRO_OBJ_DTOR, //< Activates Plugin::HookBroObjDtor.
HOOK_SETUP_ANALYZER_TREE, //< Activates Plugin::HookAddToAnalyzerTree
HOOK_LOG_INIT, //< Activates Plugin::HookLogInit
HOOK_LOG_WRITE, //< Activates Plugin::HookLogWrite
HOOK_REPORTER, //< Activates Plugin::HookReporter
HOOK_LOAD_FILE, //< Activates Plugin::HookLoadFile().
HOOK_CALL_FUNCTION, //< Activates Plugin::HookCallFunction().
HOOK_QUEUE_EVENT, //< Activates Plugin::HookQueueEvent().
HOOK_DRAIN_EVENTS, //< Activates Plugin::HookDrainEvents()
HOOK_UPDATE_NETWORK_TIME, //< Activates Plugin::HookUpdateNetworkTime.
HOOK_BRO_OBJ_DTOR, //< Activates Plugin::HookBroObjDtor.
HOOK_SETUP_ANALYZER_TREE, //< Activates Plugin::HookAddToAnalyzerTree
HOOK_LOG_INIT, //< Activates Plugin::HookLogInit
HOOK_LOG_WRITE, //< Activates Plugin::HookLogWrite
HOOK_REPORTER, //< Activates Plugin::HookReporter
// Meta hooks.
META_HOOK_PRE, //< Activates Plugin::MetaHookPre().
META_HOOK_POST, //< Activates Plugin::MetaHookPost().
META_HOOK_PRE, //< Activates Plugin::MetaHookPre().
META_HOOK_POST, //< Activates Plugin::MetaHookPost().
// End marker.
NUM_HOOKS,
};
};
/**
* Converts a hook type into a readable hook name.
@ -71,53 +82,60 @@ extern const char* hook_name(HookType h);
/**
* Helper class to capture a plugin's version.
* */
struct VersionNumber {
struct VersionNumber
{
int major = -1; //< Major version number.
int minor = -1; //< Minor version number.
int patch = 0; //< Patch version number (available since Zeek 3.0).
int patch = 0; //< Patch version number (available since Zeek 3.0).
/**
* Returns true if the version is set to a non-negative value.
*/
explicit operator bool() const { return major >= 0 && minor >= 0 && patch >= 0; }
};
};
/**
* A class defining a plugin's static configuration parameters.
*/
class Configuration {
class Configuration
{
public:
std::string name = ""; //< The plugin's name, including a namespace. Mandatory.
std::string description= ""; //< A short textual description of the plugin. Mandatory.
VersionNumber version; //< THe plugin's version. Optional.
std::string name = ""; //< The plugin's name, including a namespace. Mandatory.
std::string description = ""; //< A short textual description of the plugin. Mandatory.
VersionNumber version; //< THe plugin's version. Optional.
// We force this to inline so that the API version gets hardcoded
// into the external plugin. (Technically, it's not a "force", just a
// strong hint.). The attribute seems generally available.
inline Configuration() __attribute__((always_inline))
{
bro_version = BRO_PLUGIN_BRO_VERSION;
}
inline Configuration() __attribute__((always_inline)) { bro_version = BRO_PLUGIN_BRO_VERSION; }
/**
* One can assign BRO_PLUGIN_BRO_VERSION to this to catch
* version mismatches at link(!) time.
*/
/**
* One can assign BRO_PLUGIN_BRO_VERSION to this to catch
* version mismatches at link(!) time.
*/
const char* (*bro_version)();
private:
friend class Plugin;
};
};
/**
* A class describing an item defined in \c *.bif file.
*/
class BifItem {
class BifItem
{
public:
/**
* Type of the item.
*/
enum Type { FUNCTION = 1, EVENT = 2, CONSTANT = 3, GLOBAL = 4, TYPE = 5 };
enum Type
{
FUNCTION = 1,
EVENT = 2,
CONSTANT = 3,
GLOBAL = 4,
TYPE = 5
};
/**
* Constructor.
@ -147,211 +165,350 @@ public:
/**
* Returns the script-level ID as passed into the constructor.
*/
const std::string& GetID() const { return id; }
const std::string& GetID() const { return id; }
/**
* Returns the type as passed into the constructor.
*/
Type GetType() const { return type; }
Type GetType() const { return type; }
private:
std::string id;
Type type;
};
};
/**
* A class encapsulating an event argument to then pass along with a meta hook.
*/
class HookArgument
{
{
public:
/**
* Type of the argument.
*/
enum Type {
BOOL, DOUBLE, EVENT, FRAME, FUNC, FUNC_RESULT, INT, STRING, VAL,
VAL_LIST, VOID, VOIDP, WRITER_INFO, CONN, THREAD_FIELDS, LOCATION,
enum Type
{
BOOL,
DOUBLE,
EVENT,
FRAME,
FUNC,
FUNC_RESULT,
INT,
STRING,
VAL,
VAL_LIST,
VOID,
VOIDP,
WRITER_INFO,
CONN,
THREAD_FIELDS,
LOCATION,
ARG_LIST
};
};
/**
* Default constructor initialized the argument with type VOID.
*/
HookArgument() { type = VOID; }
HookArgument() { type = VOID; }
/**
* Constructor with a boolean argument.
*/
explicit HookArgument(bool a) { type = BOOL; arg.bool_ = a; }
explicit HookArgument(bool a)
{
type = BOOL;
arg.bool_ = a;
}
/**
* Constructor with a double argument.
*/
explicit HookArgument(double a) { type = DOUBLE; arg.double_ = a; }
explicit HookArgument(double a)
{
type = DOUBLE;
arg.double_ = a;
}
/**
* Constructor with an event argument.
*/
explicit HookArgument(const Event* a) { type = EVENT; arg.event = a; }
explicit HookArgument(const Event* a)
{
type = EVENT;
arg.event = a;
}
/**
* Constructor with an connection argument.
*/
explicit HookArgument(const Connection* c) { type = CONN; arg.conn = c; }
explicit HookArgument(const Connection* c)
{
type = CONN;
arg.conn = c;
}
/**
* Constructor with a function argument.
*/
explicit HookArgument(const Func* a) { type = FUNC; arg.func = a; }
explicit HookArgument(const Func* a)
{
type = FUNC;
arg.func = a;
}
/**
* Constructor with an integer argument.
*/
explicit HookArgument(int a) { type = INT; arg.int_ = a; }
explicit HookArgument(int a)
{
type = INT;
arg.int_ = a;
}
/**
* Constructor with a string argument.
*/
explicit HookArgument(const std::string& a) { type = STRING; arg_string = a; }
explicit HookArgument(const std::string& a)
{
type = STRING;
arg_string = a;
}
/**
* Constructor with a Bro value argument.
*/
explicit HookArgument(const Val* a) { type = VAL; arg.val = a; }
explicit HookArgument(const Val* a)
{
type = VAL;
arg.val = a;
}
/**
* Constructor with a list of Bro values argument.
*/
explicit HookArgument(const ValPList* a) { type = VAL_LIST; arg.vals = a; }
explicit HookArgument(const ValPList* a)
{
type = VAL_LIST;
arg.vals = a;
}
/**
* Constructor with a void pointer argument.
*/
explicit HookArgument(void* p) { type = VOIDP; arg.voidp = p; }
explicit HookArgument(void* p)
{
type = VOIDP;
arg.voidp = p;
}
/**
* Constructor with a function result argument.
*/
explicit HookArgument(std::pair<bool, Val*> fresult) { type = FUNC_RESULT; func_result = fresult; }
explicit HookArgument(std::pair<bool, Val*> fresult)
{
type = FUNC_RESULT;
func_result = fresult;
}
/**
* Constructor with a Frame argument.
*/
explicit HookArgument(detail::Frame* f) { type = FRAME; arg.frame = f; }
explicit HookArgument(detail::Frame* f)
{
type = FRAME;
arg.frame = f;
}
/**
* Constructor with a WriterInfo argument.
*/
explicit HookArgument(const logging::WriterBackend::WriterInfo* i) { type = WRITER_INFO; arg.winfo = i; }
explicit HookArgument(const logging::WriterBackend::WriterInfo* i)
{
type = WRITER_INFO;
arg.winfo = i;
}
/**
* Constructor with a threading field argument.
*/
explicit HookArgument(const std::pair<int, const threading::Field* const*> fpair) { type = THREAD_FIELDS; tfields = fpair; }
explicit HookArgument(const std::pair<int, const threading::Field* const*> fpair)
{
type = THREAD_FIELDS;
tfields = fpair;
}
/**
* Constructor with a location argument.
*/
explicit HookArgument(const zeek::detail::Location* location) { type = LOCATION; arg.loc = location; }
explicit HookArgument(const zeek::detail::Location* location)
{
type = LOCATION;
arg.loc = location;
}
/**
* Constructor with a zeek::Args argument.
*/
explicit HookArgument(const Args* args) { type = ARG_LIST; arg.args = args; }
explicit HookArgument(const Args* args)
{
type = ARG_LIST;
arg.args = args;
}
/**
* Returns the value for a boolen argument. The argument's type must
* match accordingly.
*/
bool AsBool() const { assert(type == BOOL); return arg.bool_; }
bool AsBool() const
{
assert(type == BOOL);
return arg.bool_;
}
/**
* Returns the value for a double argument. The argument's type must
* match accordingly.
*/
double AsDouble() const { assert(type == DOUBLE); return arg.double_; }
double AsDouble() const
{
assert(type == DOUBLE);
return arg.double_;
}
/**
* Returns the value for an event argument. The argument's type must
* match accordingly.
*/
const Event* AsEvent() const { assert(type == EVENT); return arg.event; }
const Event* AsEvent() const
{
assert(type == EVENT);
return arg.event;
}
/**
* Returns the value for an connection argument. The argument's type must
* match accordingly.
*/
const Connection* AsConnection() const { assert(type == CONN); return arg.conn; }
const Connection* AsConnection() const
{
assert(type == CONN);
return arg.conn;
}
/**
* Returns the value for a function argument. The argument's type must
* match accordingly.
*/
const Func* AsFunc() const { assert(type == FUNC); return arg.func; }
const Func* AsFunc() const
{
assert(type == FUNC);
return arg.func;
}
/**
* Returns the value for an integer argument. The argument's type must
* match accordingly.
*/
double AsInt() const { assert(type == INT); return arg.int_; }
double AsInt() const
{
assert(type == INT);
return arg.int_;
}
/**
* Returns the value for a string argument. The argument's type must
* match accordingly.
*/
const std::string& AsString() const { assert(type == STRING); return arg_string; }
const std::string& AsString() const
{
assert(type == STRING);
return arg_string;
}
/**
* Returns the value for a Bro value argument. The argument's type must
* match accordingly.
*/
const Val* AsVal() const { assert(type == VAL); return arg.val; }
const Val* AsVal() const
{
assert(type == VAL);
return arg.val;
}
/**
* Returns the value for a Bro wrapped value argument. The argument's type must
* match accordingly.
*/
const std::pair<bool, Val*> AsFuncResult() const { assert(type == FUNC_RESULT); return func_result; }
const std::pair<bool, Val*> AsFuncResult() const
{
assert(type == FUNC_RESULT);
return func_result;
}
/**
* Returns the value for a Bro frame argument. The argument's type must
* match accordingly.
*/
const zeek::detail::Frame* AsFrame() const { assert(type == FRAME); return arg.frame; }
const zeek::detail::Frame* AsFrame() const
{
assert(type == FRAME);
return arg.frame;
}
/**
* Returns the value for a logging WriterInfo argument. The argument's type must
* match accordingly.
*/
const logging::WriterBackend::WriterInfo* AsWriterInfo() const { assert(type == WRITER_INFO); return arg.winfo; }
const logging::WriterBackend::WriterInfo* AsWriterInfo() const
{
assert(type == WRITER_INFO);
return arg.winfo;
}
/**
* Returns the value for a threading fields argument. The argument's type must
* match accordingly.
*/
const std::pair<int, const threading::Field* const*> AsThreadFields() const { assert(type == THREAD_FIELDS); return tfields; }
const std::pair<int, const threading::Field* const*> AsThreadFields() const
{
assert(type == THREAD_FIELDS);
return tfields;
}
/**
* Returns the value for a list of Bro values argument. The argument's type must
* match accordingly.
*/
const ValPList* AsValList() const { assert(type == VAL_LIST); return arg.vals; }
const ValPList* AsValList() const
{
assert(type == VAL_LIST);
return arg.vals;
}
/**
* Returns the value as a Args.
*/
const Args* AsArgList() const { assert(type == ARG_LIST); return arg.args; }
const Args* AsArgList() const
{
assert(type == ARG_LIST);
return arg.args;
}
/**
* Returns the value for a vod pointer argument. The argument's type
* must match accordingly.
*/
const void* AsVoidPtr() const { assert(type == VOIDP); return arg.voidp; }
const void* AsVoidPtr() const
{
assert(type == VOIDP);
return arg.voidp;
}
/**
* Returns the argument's type.
*/
Type GetType() const { return type; }
Type GetType() const { return type; }
/**
* Returns a textual representation of the argument.
@ -362,7 +519,7 @@ public:
private:
Type type;
union {
union {
bool bool_;
double double_;
const Event* event;
@ -376,13 +533,13 @@ private:
const void* voidp;
const logging::WriterBackend::WriterInfo* winfo;
const detail::Location* loc;
} arg;
} arg;
// Outside union because these have dtors.
std::pair<bool, Val*> func_result;
std::pair<int, const threading::Field* const*> tfields;
std::string arg_string;
};
};
using HookArgumentList = std::list<HookArgument>;
@ -413,18 +570,22 @@ using HookArgumentList = std::list<HookArgument>;
* virtual methods.
*
*/
class Plugin {
class Plugin
{
public:
typedef std::list<Component *> component_list;
typedef std::list<Component*> component_list;
typedef std::list<BifItem> bif_item_list;
typedef std::list<std::pair<HookType, int> > hook_list;
typedef std::list<std::pair<HookType, int>> hook_list;
/**
* The different types of @loads supported by HookLoadFile.
*/
enum LoadType {
SCRIPT, SIGNATURES, PLUGIN
};
enum LoadType
{
SCRIPT,
SIGNATURES,
PLUGIN
};
/**
* Constructor.
@ -650,7 +811,8 @@ protected:
* have printed an error message); and -1 if the plugin wasn't
* interested in the file at all.
*/
virtual int HookLoadFile(const LoadType type, const std::string& file, const std::string& resolved);
virtual int HookLoadFile(const LoadType type, const std::string& file,
const std::string& resolved);
/**
* Hook into executing a script-level function/event/hook. Whenever
@ -674,8 +836,8 @@ protected:
* interpreter. If the plugin did not handle the call, it must return a
* pair with the first member set to 'false' and null result value.
*/
virtual std::pair<bool, ValPtr>
HookFunctionCall(const Func* func, zeek::detail::Frame* parent, Args* args);
virtual std::pair<bool, ValPtr> HookFunctionCall(const Func* func, zeek::detail::Frame* parent,
Args* args);
/**
* Hook into raising events. Whenever the script interpreter is about
@ -718,7 +880,7 @@ protected:
*
* @param conn The connection.
*/
virtual void HookSetupAnalyzerTree(Connection *conn);
virtual void HookSetupAnalyzerTree(Connection* conn);
/**
* Hook for destruction of objects registered with
@ -757,11 +919,9 @@ protected:
*
* @param fields threading::Field description of the fields being logged.
*/
virtual void HookLogInit(const std::string& writer,
const std::string& instantiating_filter,
virtual void HookLogInit(const std::string& writer, const std::string& instantiating_filter,
bool local, bool remote,
const logging::WriterBackend::WriterInfo& info,
int num_fields,
const logging::WriterBackend::WriterInfo& info, int num_fields,
const threading::Field* const* fields);
/**
@ -790,12 +950,9 @@ protected:
* @return true if log line should be written, false if log line should be
* skipped and not passed on to the writer.
*/
virtual bool HookLogWrite(const std::string& writer,
const std::string& filter,
const logging::WriterBackend::WriterInfo& info,
int num_fields,
const threading::Field* const* fields,
threading::Value** vals);
virtual bool HookLogWrite(const std::string& writer, const std::string& filter,
const logging::WriterBackend::WriterInfo& info, int num_fields,
const threading::Field* const* fields, threading::Value** vals);
/**
* Hook into reporting. This method will be called for each reporter call
@ -827,15 +984,15 @@ protected:
*/
virtual bool HookReporter(const std::string& prefix, const EventHandlerPtr event,
const Connection* conn, const ValPList* addl, bool location,
const zeek::detail::Location* location1, const zeek::detail::Location* location2,
bool time, const std::string& message);
const zeek::detail::Location* location1,
const zeek::detail::Location* location2, bool time,
const std::string& message);
// Meta hooks.
virtual void MetaHookPre(HookType hook, const HookArgumentList& args);
virtual void MetaHookPost(HookType hook, const HookArgumentList& args, HookArgument result);
private:
/**
* A function called when the plugin is instantiated to query basic
* configuration parameters.
@ -878,13 +1035,13 @@ private:
Configuration config;
std::string base_dir; // The plugin's base directory.
std::string sopath; // For dynamic plugins, the full path to the shared library.
bool dynamic; // True if a dynamic plugin.
std::string base_dir; // The plugin's base directory.
std::string sopath; // For dynamic plugins, the full path to the shared library.
bool dynamic; // True if a dynamic plugin.
component_list components; // Components the plugin provides.
bif_item_list bif_items; // BiF items the plugin provides.
};
component_list components; // Components the plugin provides.
bif_item_list bif_items; // BiF items the plugin provides.
};
} // namespace plugin
} // namespace zeek
} // namespace plugin
} // namespace zeek

View file

@ -2,17 +2,17 @@
#include <cassert>
namespace zeek::plugin {
namespace zeek::plugin
{
/**
* A class which has a tag of a given type associated with it.
*
* @tparam T A ::Tag type or derivative.
*/
template <class T>
class TaggedComponent {
template <class T> class TaggedComponent
{
public:
/**
* Constructor for TaggedComponend. Note that a unique value
* for this component is only created when InitializeTag is
@ -43,31 +43,28 @@ private:
bool initialized;
static typename T::type_t type_counter; /**< Used to generate globally
unique tags. */
};
};
template <class T>
TaggedComponent<T>::TaggedComponent(typename T::subtype_t subtype)
template <class T> TaggedComponent<T>::TaggedComponent(typename T::subtype_t subtype)
{
tag = T(1,0);
tag = T(1, 0);
this->subtype = subtype;
initialized = false;
}
template <class T>
void TaggedComponent<T>::InitializeTag()
template <class T> void TaggedComponent<T>::InitializeTag()
{
assert( initialized == false );
assert(initialized == false);
initialized = true;
tag = T(++type_counter, subtype);
}
template <class T>
T TaggedComponent<T>::Tag() const
template <class T> T TaggedComponent<T>::Tag() const
{
assert( initialized );
assert(initialized);
return tag;
}
template <class T> typename T::type_t TaggedComponent<T>::type_counter(0);
} // namespace zeek::plugin
} // namespace zeek::plugin