Cleanup of plugin component API.

- Move more functionality into base class.
- Remove cctors and assignment operators (weren't actually needed anymore)
- Switch from const char* to std::string.
This commit is contained in:
Robin Sommer 2013-12-12 17:39:03 -08:00
parent e9413c9361
commit 987452beff
18 changed files with 114 additions and 216 deletions

View file

@ -473,7 +473,7 @@ static void WritePluginSectionHeading(FILE* f, const plugin::Plugin* p)
static void WriteAnalyzerComponent(FILE* f, const analyzer::Component* c) static void WriteAnalyzerComponent(FILE* f, const analyzer::Component* c)
{ {
EnumType* atag = analyzer_mgr->GetTagEnumType(); EnumType* atag = analyzer_mgr->GetTagEnumType();
string tag = fmt("ANALYZER_%s", c->CanonicalName()); string tag = fmt("ANALYZER_%s", c->CanonicalName().c_str());
if ( atag->Lookup("Analyzer", tag.c_str()) < 0 ) if ( atag->Lookup("Analyzer", tag.c_str()) < 0 )
reporter->InternalError("missing analyzer tag for %s", tag.c_str()); reporter->InternalError("missing analyzer tag for %s", tag.c_str());
@ -484,7 +484,7 @@ static void WriteAnalyzerComponent(FILE* f, const analyzer::Component* c)
static void WriteAnalyzerComponent(FILE* f, const file_analysis::Component* c) static void WriteAnalyzerComponent(FILE* f, const file_analysis::Component* c)
{ {
EnumType* atag = file_mgr->GetTagEnumType(); EnumType* atag = file_mgr->GetTagEnumType();
string tag = fmt("ANALYZER_%s", c->CanonicalName()); string tag = fmt("ANALYZER_%s", c->CanonicalName().c_str());
if ( atag->Lookup("Files", tag.c_str()) < 0 ) if ( atag->Lookup("Files", tag.c_str()) < 0 )
reporter->InternalError("missing analyzer tag for %s", tag.c_str()); reporter->InternalError("missing analyzer tag for %s", tag.c_str());

View file

@ -60,11 +60,11 @@ RuleActionAnalyzer::RuleActionAnalyzer(const char* arg_analyzer)
void RuleActionAnalyzer::PrintDebug() void RuleActionAnalyzer::PrintDebug()
{ {
if ( ! child_analyzer ) if ( ! child_analyzer )
fprintf(stderr, "|%s|\n", analyzer_mgr->GetComponentName(analyzer)); fprintf(stderr, "|%s|\n", analyzer_mgr->GetComponentName(analyzer).c_str());
else else
fprintf(stderr, "|%s:%s|\n", fprintf(stderr, "|%s:%s|\n",
analyzer_mgr->GetComponentName(analyzer), analyzer_mgr->GetComponentName(analyzer).c_str(),
analyzer_mgr->GetComponentName(child_analyzer)); analyzer_mgr->GetComponentName(child_analyzer).c_str());
} }

View file

@ -75,7 +75,7 @@ analyzer::ID Analyzer::id_counter = 0;
const char* Analyzer::GetAnalyzerName() const const char* Analyzer::GetAnalyzerName() const
{ {
assert(tag); assert(tag);
return analyzer_mgr->GetComponentName(tag); return analyzer_mgr->GetComponentName(tag).c_str();
} }
void Analyzer::SetAnalyzerTag(const Tag& arg_tag) void Analyzer::SetAnalyzerTag(const Tag& arg_tag)
@ -87,7 +87,7 @@ void Analyzer::SetAnalyzerTag(const Tag& arg_tag)
bool Analyzer::IsAnalyzer(const char* name) bool Analyzer::IsAnalyzer(const char* name)
{ {
assert(tag); assert(tag);
return strcmp(analyzer_mgr->GetComponentName(tag), name) == 0; return strcmp(analyzer_mgr->GetComponentName(tag).c_str(), name) == 0;
} }
// Used in debugging output. // Used in debugging output.

View file

@ -8,12 +8,11 @@
using namespace analyzer; using namespace analyzer;
Component::Component(const char* arg_name, factory_callback arg_factory, Tag::subtype_t arg_subtype, bool arg_enabled, bool arg_partial) Component::Component(const std::string& name, factory_callback arg_factory, Tag::subtype_t arg_subtype, bool arg_enabled, bool arg_partial)
: plugin::Component(plugin::component::ANALYZER), : plugin::Component(plugin::component::ANALYZER, name),
plugin::TaggedComponent<analyzer::Tag>(arg_subtype) plugin::TaggedComponent<analyzer::Tag>(arg_subtype)
{ {
name = copy_string(arg_name); canon_name = canonify_name(name);
canon_name = canonify_name(arg_name);
factory = arg_factory; factory = arg_factory;
enabled = arg_enabled; enabled = arg_enabled;
partial = arg_partial; partial = arg_partial;
@ -21,31 +20,12 @@ Component::Component(const char* arg_name, factory_callback arg_factory, Tag::su
analyzer_mgr->RegisterComponent(this, "ANALYZER_"); analyzer_mgr->RegisterComponent(this, "ANALYZER_");
} }
Component::Component(const Component& other)
: plugin::Component(Type()),
plugin::TaggedComponent<analyzer::Tag>(other)
{
name = copy_string(other.name);
canon_name = copy_string(other.canon_name);
factory = other.factory;
enabled = other.enabled;
partial = other.partial;
// TODO: Do we need the RegisterComponent() call here?
}
Component::~Component() Component::~Component()
{ {
delete [] name;
delete [] canon_name;
} }
void Component::Describe(ODesc* d) const void Component::DoDescribe(ODesc* d) const
{ {
plugin::Component::Describe(d);
d->Add(name);
d->Add(" (");
if ( factory ) if ( factory )
{ {
d->Add("ANALYZER_"); d->Add("ANALYZER_");
@ -54,20 +34,4 @@ void Component::Describe(ODesc* d) const
} }
d->Add(enabled ? "enabled" : "disabled"); d->Add(enabled ? "enabled" : "disabled");
d->Add(")");
}
Component& Component::operator=(const Component& other)
{
plugin::TaggedComponent<analyzer::Tag>::operator=(other);
if ( &other != this )
{
name = copy_string(other.name);
factory = other.factory;
enabled = other.enabled;
partial = other.partial;
}
return *this;
} }

View file

@ -1,7 +1,7 @@
// See the file "COPYING" in the main distribution directory for copyright. // See the file "COPYING" in the main distribution directory for copyright.
#ifndef ANALYZER_PLUGIN_COMPONENT_H #ifndef ANALYZER_COMPONENT_H
#define ANALYZER_PLUGIN_COMPONENT_H #define ANALYZER_COMPONENT_H
#include "Tag.h" #include "Tag.h"
#include "plugin/Component.h" #include "plugin/Component.h"
@ -56,33 +56,20 @@ public:
* connections has generally not seen much testing yet as virtually * connections has generally not seen much testing yet as virtually
* no existing analyzer supports it. * no existing analyzer supports it.
*/ */
Component(const char* name, factory_callback factory, Tag::subtype_t subtype = 0, bool enabled = true, bool partial = false); Component(const std::string& name, factory_callback factory, Tag::subtype_t subtype = 0, bool enabled = true, bool partial = false);
/**
* Copy constructor.
*/
Component(const Component& other);
/** /**
* Destructor. * Destructor.
*/ */
~Component(); ~Component();
/**
* Returns the name of the analyzer. This name is unique across all
* analyzers and used to identify it. The returned name is derived
* from what's passed to the constructor but upper-cased and
* canonified to allow being part of a script-level ID.
*/
virtual const char* Name() const { return name; }
/** /**
* Returns a canonocalized version of the analyzer's name. The * Returns a canonocalized version of the analyzer's name. The
* returned name is derived from what's passed to the constructor but * returned name is derived from what's passed to the constructor but
* upper-cased and transformed to allow being part of a script-level * upper-cased and transformed to allow being part of a script-level
* ID. * ID.
*/ */
const char* CanonicalName() const { return canon_name; } const std::string& CanonicalName() const { return canon_name; }
/** /**
* Returns the analyzer's factory function. * Returns the analyzer's factory function.
@ -110,17 +97,14 @@ public:
*/ */
void SetEnabled(bool arg_enabled) { enabled = arg_enabled; } void SetEnabled(bool arg_enabled) { enabled = arg_enabled; }
protected:
/** /**
* Generates a human-readable description of the component's main * Overriden from plugin::Component.
* parameters. This goes into the output of \c "bro -NN". */
*/ virtual void DoDescribe(ODesc* d) const;
virtual void Describe(ODesc* d) const;
Component& operator=(const Component& other);
private: private:
const char* name; // The analyzer's name. std::string canon_name; // The analyzer's canonical name.
const char* canon_name; // The analyzer's canonical name.
factory_callback factory; // The analyzer's factory callback. factory_callback factory; // The analyzer's factory callback.
bool partial; // True if the analyzer supports partial connections. bool partial; // True if the analyzer supports partial connections.
bool enabled; // True if the analyzer is enabled. bool enabled; // True if the analyzer is enabled.

View file

@ -104,7 +104,8 @@ void Manager::DumpDebug()
DBG_LOG(DBG_ANALYZER, "Available analyzers after bro_init():"); DBG_LOG(DBG_ANALYZER, "Available analyzers after bro_init():");
list<Component*> all_analyzers = GetComponents(); list<Component*> all_analyzers = GetComponents();
for ( list<Component*>::const_iterator i = all_analyzers.begin(); i != all_analyzers.end(); ++i ) for ( list<Component*>::const_iterator i = all_analyzers.begin(); i != all_analyzers.end(); ++i )
DBG_LOG(DBG_ANALYZER, " %s (%s)", (*i)->Name(), IsEnabled((*i)->Tag()) ? "enabled" : "disabled"); DBG_LOG(DBG_ANALYZER, " %s (%s)", (*i)->Name().c_str(),
IsEnabled((*i)->Tag()) ? "enabled" : "disabled");
DBG_LOG(DBG_ANALYZER, ""); DBG_LOG(DBG_ANALYZER, "");
DBG_LOG(DBG_ANALYZER, "Analyzers by port:"); DBG_LOG(DBG_ANALYZER, "Analyzers by port:");
@ -143,7 +144,7 @@ bool Manager::EnableAnalyzer(Tag tag)
if ( ! p ) if ( ! p )
return false; return false;
DBG_LOG(DBG_ANALYZER, "Enabling analyzer %s", p->Name()); DBG_LOG(DBG_ANALYZER, "Enabling analyzer %s", p->Name().c_str());
p->SetEnabled(true); p->SetEnabled(true);
return true; return true;
@ -156,7 +157,7 @@ bool Manager::EnableAnalyzer(EnumVal* val)
if ( ! p ) if ( ! p )
return false; return false;
DBG_LOG(DBG_ANALYZER, "Enabling analyzer %s", p->Name()); DBG_LOG(DBG_ANALYZER, "Enabling analyzer %s", p->Name().c_str());
p->SetEnabled(true); p->SetEnabled(true);
return true; return true;
@ -169,7 +170,7 @@ bool Manager::DisableAnalyzer(Tag tag)
if ( ! p ) if ( ! p )
return false; return false;
DBG_LOG(DBG_ANALYZER, "Disabling analyzer %s", p->Name()); DBG_LOG(DBG_ANALYZER, "Disabling analyzer %s", p->Name().c_str());
p->SetEnabled(false); p->SetEnabled(false);
return true; return true;
@ -182,7 +183,7 @@ bool Manager::DisableAnalyzer(EnumVal* val)
if ( ! p ) if ( ! p )
return false; return false;
DBG_LOG(DBG_ANALYZER, "Disabling analyzer %s", p->Name()); DBG_LOG(DBG_ANALYZER, "Disabling analyzer %s", p->Name().c_str());
p->SetEnabled(false); p->SetEnabled(false);
return true; return true;
@ -254,7 +255,7 @@ bool Manager::RegisterAnalyzerForPort(Tag tag, TransportProto proto, uint32 port
return false; return false;
#ifdef DEBUG #ifdef DEBUG
const char* name = GetComponentName(tag); const char* name = GetComponentName(tag).c_str();
DBG_LOG(DBG_ANALYZER, "Registering analyzer %s for port %" PRIu32 "/%d", name, port, proto); DBG_LOG(DBG_ANALYZER, "Registering analyzer %s for port %" PRIu32 "/%d", name, port, proto);
#endif #endif
@ -270,7 +271,7 @@ bool Manager::UnregisterAnalyzerForPort(Tag tag, TransportProto proto, uint32 po
return true; // still a "successful" unregistration return true; // still a "successful" unregistration
#ifdef DEBUG #ifdef DEBUG
const char* name = GetComponentName(tag); const char* name = GetComponentName(tag).c_str();
DBG_LOG(DBG_ANALYZER, "Unregistering analyzer %s for port %" PRIu32 "/%d", name, port, proto); DBG_LOG(DBG_ANALYZER, "Unregistering analyzer %s for port %" PRIu32 "/%d", name, port, proto);
#endif #endif
@ -293,7 +294,8 @@ Analyzer* Manager::InstantiateAnalyzer(Tag tag, Connection* conn)
if ( ! c->Factory() ) if ( ! c->Factory() )
{ {
reporter->InternalWarning("analyzer %s cannot be instantiated dynamically", GetComponentName(tag)); reporter->InternalWarning("analyzer %s cannot be instantiated dynamically",
GetComponentName(tag).c_str());
return 0; return 0;
} }
@ -403,7 +405,7 @@ bool Manager::BuildInitialAnalyzerTree(Connection* conn)
root->AddChildAnalyzer(analyzer, false); root->AddChildAnalyzer(analyzer, false);
DBG_ANALYZER_ARGS(conn, "activated %s analyzer as scheduled", DBG_ANALYZER_ARGS(conn, "activated %s analyzer as scheduled",
analyzer_mgr->GetComponentName(*i)); analyzer_mgr->GetComponentName(*i).c_str());
} }
} }
@ -429,7 +431,7 @@ bool Manager::BuildInitialAnalyzerTree(Connection* conn)
root->AddChildAnalyzer(analyzer, false); root->AddChildAnalyzer(analyzer, false);
DBG_ANALYZER_ARGS(conn, "activated %s analyzer due to port %d", DBG_ANALYZER_ARGS(conn, "activated %s analyzer due to port %d",
analyzer_mgr->GetComponentName(*j), resp_port); analyzer_mgr->GetComponentName(*j).c_str(), resp_port);
} }
} }
} }
@ -555,7 +557,7 @@ void Manager::ExpireScheduledAnalyzers()
conns.erase(i); conns.erase(i);
DBG_LOG(DBG_ANALYZER, "Expiring expected analyzer %s for connection %s", DBG_LOG(DBG_ANALYZER, "Expiring expected analyzer %s for connection %s",
analyzer_mgr->GetComponentName(a->analyzer), analyzer_mgr->GetComponentName(a->analyzer).c_str(),
fmt_conn_id(a->conn.orig, 0, a->conn.resp, a->conn.resp_p)); fmt_conn_id(a->conn.orig, 0, a->conn.resp, a->conn.resp_p));
delete a; delete a;

View file

@ -8,7 +8,7 @@ file_analysis::ID file_analysis::Analyzer::id_counter = 0;
file_analysis::Analyzer::~Analyzer() file_analysis::Analyzer::~Analyzer()
{ {
DBG_LOG(DBG_FILE_ANALYSIS, "Destroy file analyzer %s", DBG_LOG(DBG_FILE_ANALYSIS, "Destroy file analyzer %s",
file_mgr->GetComponentName(tag)); file_mgr->GetComponentName(tag).c_str());
Unref(args); Unref(args);
} }

View file

@ -53,7 +53,7 @@ bool AnalyzerSet::Add(file_analysis::Tag tag, RecordVal* args)
if ( analyzer_map.Lookup(key) ) if ( analyzer_map.Lookup(key) )
{ {
DBG_LOG(DBG_FILE_ANALYSIS, "Instantiate analyzer %s skipped for file id" DBG_LOG(DBG_FILE_ANALYSIS, "Instantiate analyzer %s skipped for file id"
" %s: already exists", file_mgr->GetComponentName(tag), " %s: already exists", file_mgr->GetComponentName(tag).c_str(),
file->GetID().c_str()); file->GetID().c_str());
delete key; delete key;
return true; return true;
@ -93,7 +93,7 @@ bool AnalyzerSet::AddMod::Perform(AnalyzerSet* set)
if ( set->analyzer_map.Lookup(key) ) if ( set->analyzer_map.Lookup(key) )
{ {
DBG_LOG(DBG_FILE_ANALYSIS, "Add analyzer %s skipped for file id" DBG_LOG(DBG_FILE_ANALYSIS, "Add analyzer %s skipped for file id"
" %s: already exists", file_mgr->GetComponentName(a->Tag()), " %s: already exists", file_mgr->GetComponentName(a->Tag()).c_str(),
a->GetFile()->GetID().c_str()); a->GetFile()->GetID().c_str());
Abort(); Abort();
@ -120,12 +120,12 @@ bool AnalyzerSet::Remove(file_analysis::Tag tag, HashKey* key)
if ( ! a ) if ( ! a )
{ {
DBG_LOG(DBG_FILE_ANALYSIS, "Skip remove analyzer %s for file id %s", DBG_LOG(DBG_FILE_ANALYSIS, "Skip remove analyzer %s for file id %s",
file_mgr->GetComponentName(tag), file->GetID().c_str()); file_mgr->GetComponentName(tag).c_str(), file->GetID().c_str());
return false; return false;
} }
DBG_LOG(DBG_FILE_ANALYSIS, "Remove analyzer %s for file id %s", DBG_LOG(DBG_FILE_ANALYSIS, "Remove analyzer %s for file id %s",
file_mgr->GetComponentName(tag), file_mgr->GetComponentName(tag).c_str(),
file->GetID().c_str()); file->GetID().c_str());
a->Done(); a->Done();
@ -169,7 +169,7 @@ file_analysis::Analyzer* AnalyzerSet::InstantiateAnalyzer(Tag tag,
if ( ! a ) if ( ! a )
{ {
reporter->Error("Failed file analyzer %s instantiation for file id %s", reporter->Error("Failed file analyzer %s instantiation for file id %s",
file_mgr->GetComponentName(tag), file->GetID().c_str()); file_mgr->GetComponentName(tag).c_str(), file->GetID().c_str());
return 0; return 0;
} }
@ -179,7 +179,7 @@ file_analysis::Analyzer* AnalyzerSet::InstantiateAnalyzer(Tag tag,
void AnalyzerSet::Insert(file_analysis::Analyzer* a, HashKey* key) void AnalyzerSet::Insert(file_analysis::Analyzer* a, HashKey* key)
{ {
DBG_LOG(DBG_FILE_ANALYSIS, "Add analyzer %s for file id %s", DBG_LOG(DBG_FILE_ANALYSIS, "Add analyzer %s for file id %s",
file_mgr->GetComponentName(a->Tag()), file->GetID().c_str()); file_mgr->GetComponentName(a->Tag()).c_str(), file->GetID().c_str());
analyzer_map.Insert(key, a); analyzer_map.Insert(key, a);
delete key; delete key;

View file

@ -8,58 +8,25 @@
using namespace file_analysis; using namespace file_analysis;
Component::Component(const char* arg_name, factory_callback arg_factory, Tag::subtype_t subtype) Component::Component(const std::string& name, factory_callback arg_factory, Tag::subtype_t subtype)
: plugin::Component(plugin::component::FILE_ANALYZER), : plugin::Component(plugin::component::FILE_ANALYZER, name),
plugin::TaggedComponent<file_analysis::Tag>(subtype) plugin::TaggedComponent<file_analysis::Tag>(subtype)
{ {
name = copy_string(arg_name); canon_name = canonify_name(name);
canon_name = canonify_name(arg_name);
factory = arg_factory; factory = arg_factory;
file_mgr->RegisterComponent(this, "ANALYZER_"); file_mgr->RegisterComponent(this, "ANALYZER_");
} }
Component::Component(const Component& other)
: plugin::Component(Type()),
plugin::TaggedComponent<file_analysis::Tag>(other)
{
name = copy_string(other.name);
canon_name = copy_string(other.canon_name);
factory = other.factory;
// TODO: Do we need the RegisterComponent() call here?
}
Component::~Component() Component::~Component()
{ {
delete [] name;
delete [] canon_name;
} }
void Component::Describe(ODesc* d) const void Component::DoDescribe(ODesc* d) const
{ {
plugin::Component::Describe(d);
d->Add(name);
d->Add(" (");
if ( factory ) if ( factory )
{ {
d->Add("ANALYZER_"); d->Add("ANALYZER_");
d->Add(canon_name); d->Add(canon_name);
} }
d->Add(")");
}
Component& Component::operator=(const Component& other)
{
plugin::TaggedComponent<file_analysis::Tag>::operator=(other);
if ( &other != this )
{
name = copy_string(other.name);
factory = other.factory;
}
return *this;
} }

View file

@ -1,7 +1,7 @@
// See the file "COPYING" in the main distribution directory for copyright. // See the file "COPYING" in the main distribution directory for copyright.
#ifndef FILE_ANALYZER_PLUGIN_COMPONENT_H #ifndef FILE_ANALYZER_COMPONENT_H
#define FILE_ANALYZER_PLUGIN_COMPONENT_H #define FILE_ANALYZER_COMPONENT_H
#include "Tag.h" #include "Tag.h"
#include "plugin/Component.h" #include "plugin/Component.h"
@ -47,50 +47,34 @@ public:
* analyzer instances can accordingly access it via analyzer::Tag(). * analyzer instances can accordingly access it via analyzer::Tag().
* If not used, leave at zero. * If not used, leave at zero.
*/ */
Component(const char* name, factory_callback factory, Tag::subtype_t subtype = 0); Component(const std::string& name, factory_callback factory, Tag::subtype_t subtype = 0);
/**
* Copy constructor.
*/
Component(const Component& other);
/** /**
* Destructor. * Destructor.
*/ */
~Component(); ~Component();
/**
* Returns the name of the analyzer. This name is unique across all
* analyzers and used to identify it. The returned name is derived
* from what's passed to the constructor but upper-cased and
* canonified to allow being part of a script-level ID.
*/
virtual const char* Name() const { return name; }
/** /**
* Returns a canonocalized version of the analyzer's name. The * Returns a canonocalized version of the analyzer's name. The
* returned name is derived from what's passed to the constructor but * returned name is derived from what's passed to the constructor but
* upper-cased and transformed to allow being part of a script-level * upper-cased and transformed to allow being part of a script-level
* ID. * ID.
*/ */
const char* CanonicalName() const { return canon_name; } const std::string& CanonicalName() const { return canon_name; }
/** /**
* Returns the analyzer's factory function. * Returns the analyzer's factory function.
*/ */
factory_callback Factory() const { return factory; } factory_callback Factory() const { return factory; }
protected:
/** /**
* Generates a human-readable description of the component's main * Overriden from plugin::Component.
* parameters. This goes into the output of \c "bro -NN". */
*/ virtual void DoDescribe(ODesc* d) const;
virtual void Describe(ODesc* d) const;
Component& operator=(const Component& other);
private: private:
const char* name; // The analyzer's name. std::string canon_name; // The analyzer's canonical name.
const char* canon_name; // The analyzer's canonical name.
factory_callback factory; // The analyzer's factory callback. factory_callback factory; // The analyzer's factory callback.
}; };

View file

@ -254,7 +254,7 @@ void File::ScheduleInactivityTimer() const
bool File::AddAnalyzer(file_analysis::Tag tag, RecordVal* args) bool File::AddAnalyzer(file_analysis::Tag tag, RecordVal* args)
{ {
DBG_LOG(DBG_FILE_ANALYSIS, "[%s] Queuing addition of %s analyzer", DBG_LOG(DBG_FILE_ANALYSIS, "[%s] Queuing addition of %s analyzer",
id.c_str(), file_mgr->GetComponentName(tag)); id.c_str(), file_mgr->GetComponentName(tag).c_str());
return done ? false : analyzers.QueueAdd(tag, args); return done ? false : analyzers.QueueAdd(tag, args);
} }
@ -262,7 +262,7 @@ bool File::AddAnalyzer(file_analysis::Tag tag, RecordVal* args)
bool File::RemoveAnalyzer(file_analysis::Tag tag, RecordVal* args) bool File::RemoveAnalyzer(file_analysis::Tag tag, RecordVal* args)
{ {
DBG_LOG(DBG_FILE_ANALYSIS, "[%s] Queuing remove of %s analyzer", DBG_LOG(DBG_FILE_ANALYSIS, "[%s] Queuing remove of %s analyzer",
id.c_str(), file_mgr->GetComponentName(tag)); id.c_str(), file_mgr->GetComponentName(tag).c_str());
return done ? false : analyzers.QueueRemove(tag, args); return done ? false : analyzers.QueueRemove(tag, args);
} }

View file

@ -351,7 +351,7 @@ void Manager::GetFileHandle(analyzer::Tag tag, Connection* c, bool is_orig)
return; return;
DBG_LOG(DBG_FILE_ANALYSIS, "Raise get_file_handle() for protocol analyzer %s", DBG_LOG(DBG_FILE_ANALYSIS, "Raise get_file_handle() for protocol analyzer %s",
analyzer_mgr->GetComponentName(tag)); analyzer_mgr->GetComponentName(tag).c_str());
EnumVal* tagval = tag.AsEnumVal(); EnumVal* tagval = tag.AsEnumVal();
Ref(tagval); Ref(tagval);
@ -398,12 +398,12 @@ Analyzer* Manager::InstantiateAnalyzer(Tag tag, RecordVal* args, File* f) const
if ( ! c->Factory() ) if ( ! c->Factory() )
{ {
reporter->InternalWarning("file analyzer %s cannot be instantiated " reporter->InternalWarning("file analyzer %s cannot be instantiated "
"dynamically", c->CanonicalName()); "dynamically", c->CanonicalName().c_str());
return 0; return 0;
} }
DBG_LOG(DBG_FILE_ANALYSIS, "Instantiate analyzer %s for file %s", DBG_LOG(DBG_FILE_ANALYSIS, "Instantiate analyzer %s for file %s",
GetComponentName(tag), f->id.c_str()); GetComponentName(tag).c_str(), f->id.c_str());
Analyzer* a = c->Factory()(args, f); Analyzer* a = c->Factory()(args, f);
@ -445,7 +445,7 @@ bool Manager::RegisterAnalyzerForMIMEType(Tag tag, const string& mtype)
TagSet* l = LookupMIMEType(mtype, true); TagSet* l = LookupMIMEType(mtype, true);
DBG_LOG(DBG_FILE_ANALYSIS, "Register analyzer %s for MIME type %s", DBG_LOG(DBG_FILE_ANALYSIS, "Register analyzer %s for MIME type %s",
GetComponentName(tag), mtype.c_str()); GetComponentName(tag).c_str(), mtype.c_str());
l->insert(tag); l->insert(tag);
return true; return true;
@ -466,7 +466,7 @@ bool Manager::UnregisterAnalyzerForMIMEType(Tag tag, const string& mtype)
TagSet* l = LookupMIMEType(mtype, true); TagSet* l = LookupMIMEType(mtype, true);
DBG_LOG(DBG_FILE_ANALYSIS, "Unregister analyzer %s for MIME type %s", DBG_LOG(DBG_FILE_ANALYSIS, "Unregister analyzer %s for MIME type %s",
GetComponentName(tag), mtype.c_str()); GetComponentName(tag).c_str(), mtype.c_str());
l->erase(tag); l->erase(tag);
return true; return true;

View file

@ -7,15 +7,21 @@
using namespace plugin; using namespace plugin;
Component::Component(component::Type arg_type) Component::Component(component::Type arg_type, const std::string& arg_name)
{ {
type = arg_type; type = arg_type;
name = arg_name;
} }
Component::~Component() Component::~Component()
{ {
} }
const std::string& Component::Name() const
{
return name;
}
component::Type Component::Type() const component::Type Component::Type() const
{ {
return type; return type;
@ -51,4 +57,8 @@ void Component::Describe(ODesc* d) const
d->Add("]"); d->Add("]");
d->Add(" "); d->Add(" ");
d->Add(name);
d->Add(" (");
DoDescribe(d);
d->Add(")");
} }

View file

@ -3,6 +3,8 @@
#ifndef PLUGIN_COMPONENT_H #ifndef PLUGIN_COMPONENT_H
#define PLUGIN_COMPONENT_H #define PLUGIN_COMPONENT_H
#include <string>
class ODesc; class ODesc;
namespace plugin { namespace plugin {
@ -32,8 +34,11 @@ public:
* Constructor. * Constructor.
* *
* @param type The type of the compoment. * @param type The type of the compoment.
*
* @param name A descriptive name for the component. This name must
* be unique across all components of the same type.
*/ */
Component(component::Type type); Component(component::Type type, const std::string& name);
/** /**
* Destructor. * Destructor.
@ -46,22 +51,37 @@ public:
component::Type Type() const; component::Type Type() const;
/** /**
* Returns a descriptive name for the analyzer. This name must be * Returns the compoment's name.
* unique across all components of the same type.
*/ */
virtual const char* Name() const = 0; const std::string& Name() const;
/** /**
* Returns a textual representation of the component. The default * Returns a textual representation of the component. This goes into
* version just output the type. Derived version should call the * the output of "bro -NN".
* parent's implementation and that add further information. *
* By default version, this just outputs the type and the name.
* Derived versions should override DoDescribe() to add type specific
* details.
* *
* @param d The description object to use. * @param d The description object to use.
*/ */
virtual void Describe(ODesc* d) const; virtual void Describe(ODesc* d) const;
protected:
/**
* Adds type specific information to the outout of Describe().
*
* @param d The description object to use.
*/
virtual void DoDescribe(ODesc* d) const { }
private: private:
// Disable.
Component(const Component& other);
Component operator=(const Component& other);
component::Type type; component::Type type;
std::string name;
}; };
} }

View file

@ -37,7 +37,7 @@ public:
/** /**
* @return The script-layer module in which the component's "Tag" ID lives. * @return The script-layer module in which the component's "Tag" ID lives.
*/ */
const char* GetModule() const; const std::string& GetModule() const;
/** /**
* @return A list of all registered components. * @return A list of all registered components.
@ -55,7 +55,7 @@ public:
* @param tag A component's tag. * @param tag A component's tag.
* @return The canonical component name. * @return The canonical component name.
*/ */
const char* GetComponentName(T tag) const; const std::string& GetComponentName(T tag) const;
/** /**
* Get a component name from it's enum value. * Get a component name from it's enum value.
@ -63,7 +63,7 @@ public:
* @param val A component's enum value. * @param val A component's enum value.
* @return The canonical component name. * @return The canonical component name.
*/ */
const char* GetComponentName(Val* val) const; const std::string& GetComponentName(Val* val) const;
/** /**
* Get a component tag from its name. * Get a component tag from its name.
@ -134,7 +134,7 @@ ComponentManager<T, C>::ComponentManager(const string& arg_module)
} }
template <class T, class C> template <class T, class C>
const char* ComponentManager<T, C>::GetModule() const const std::string& ComponentManager<T, C>::GetModule() const
{ {
return module.c_str(); return module.c_str();
} }
@ -158,9 +158,9 @@ EnumType* ComponentManager<T, C>::GetTagEnumType() const
} }
template <class T, class C> template <class T, class C>
const char* ComponentManager<T, C>::GetComponentName(T tag) const const std::string& ComponentManager<T, C>::GetComponentName(T tag) const
{ {
static const char* error = "<error>"; static const std::string& error = "<error>";
if ( ! tag ) if ( ! tag )
return error; return error;
@ -176,7 +176,7 @@ const char* ComponentManager<T, C>::GetComponentName(T tag) const
} }
template <class T, class C> template <class T, class C>
const char* ComponentManager<T, C>::GetComponentName(Val* val) const const std::string& ComponentManager<T, C>::GetComponentName(Val* val) const
{ {
return GetComponentName(T(val->AsEnumVal())); return GetComponentName(T(val->AsEnumVal()));
} }
@ -222,14 +222,14 @@ template <class T, class C>
void ComponentManager<T, C>::RegisterComponent(C* component, void ComponentManager<T, C>::RegisterComponent(C* component,
const string& prefix) const string& prefix)
{ {
const char* cname = component->CanonicalName(); std::string cname = component->CanonicalName();
if ( Lookup(cname) ) if ( Lookup(cname) )
reporter->FatalError("Component '%s::%s' defined more than once", reporter->FatalError("Component '%s::%s' defined more than once",
module.c_str(), cname); module.c_str(), cname.c_str());
DBG_LOG(DBG_PLUGINS, "Registering component %s (tag %s)", DBG_LOG(DBG_PLUGINS, "Registering component %s (tag %s)",
component->Name(), component->Tag().AsString().c_str()); component->Name().c_str(), component->Tag().AsString().c_str());
components_by_name.insert(std::make_pair(cname, component)); components_by_name.insert(std::make_pair(cname, component));
components_by_tag.insert(std::make_pair(component->Tag(), component)); components_by_tag.insert(std::make_pair(component->Tag(), component));
@ -237,7 +237,7 @@ void ComponentManager<T, C>::RegisterComponent(C* component,
component->Tag().AsEnumVal()->InternalInt(), component)); component->Tag().AsEnumVal()->InternalInt(), component));
// Install an identfier for enum value // Install an identfier for enum value
string id = fmt("%s%s", prefix.c_str(), cname); string id = fmt("%s%s", prefix.c_str(), cname.c_str());
tag_enum_type->AddName(module, id.c_str(), tag_enum_type->AddName(module, id.c_str(),
component->Tag().AsEnumVal()->InternalInt(), true); component->Tag().AsEnumVal()->InternalInt(), true);
} }

View file

@ -23,28 +23,12 @@ public:
*/ */
TaggedComponent(typename T::subtype_t subtype = 0); TaggedComponent(typename T::subtype_t subtype = 0);
/**
* Copy constructor.
*
* @param other Another component from which to copy its tag value.
*/
TaggedComponent(const TaggedComponent& other);
/**
* Assignment operator.
*
* @param other A component to assign.
* @return The assigned object.
*/
TaggedComponent& operator=(const TaggedComponent& other);
/** /**
* @return The component's tag. * @return The component's tag.
*/ */
T Tag() const; T Tag() const;
private: private:
T tag; /**< The automatically assigned analyzer tag. */ T tag; /**< The automatically assigned analyzer tag. */
static typename T::type_t type_counter; /**< Used to generate globally static typename T::type_t type_counter; /**< Used to generate globally
unique tags. */ unique tags. */
@ -56,22 +40,6 @@ TaggedComponent<T>::TaggedComponent(typename T::subtype_t subtype)
tag = T(++type_counter, subtype); tag = T(++type_counter, subtype);
} }
template <class T>
TaggedComponent<T>::TaggedComponent(const TaggedComponent<T>& other)
{
tag = other.tag;
}
template <class T>
TaggedComponent<T>&
TaggedComponent<T>::operator =(const TaggedComponent<T>& other)
{
if ( &other != this )
tag = other.tag;
return *this;
}
template <class T> template <class T>
T TaggedComponent<T>::Tag() const T TaggedComponent<T>::Tag() const
{ {

View file

@ -1701,17 +1701,16 @@ const char* bro_magic_buffer(magic_t cookie, const void* buffer, size_t length)
return rval; return rval;
} }
const char* canonify_name(const char* name) std::string canonify_name(const std::string& name)
{ {
unsigned int len = strlen(name); unsigned int len = name.size();
char* nname = new char[len + 1]; std::string nname;
for ( unsigned int i = 0; i < len; i++ ) for ( unsigned int i = 0; i < len; i++ )
{ {
char c = isalnum(name[i]) ? name[i] : '_'; char c = isalnum(name[i]) ? name[i] : '_';
nname[i] = toupper(c); nname += toupper(c);
} }
nname[len] = '\0';
return nname; return nname;
} }

View file

@ -406,6 +406,6 @@ const char* bro_magic_buffer(magic_t cookie, const void* buffer, size_t length);
* @param name The string to canonicalize. * @param name The string to canonicalize.
* @return The canonicalized version of \a name which caller may later delete[]. * @return The canonicalized version of \a name which caller may later delete[].
*/ */
const char* canonify_name(const char* name); std::string canonify_name(const std::string& name);
#endif #endif