bifcl: Deprecate BifEvent::generate_*, add BifEvent::enqueue_*

This commit is contained in:
Jon Siwek 2020-04-17 18:41:14 -07:00 committed by Tim Wojtulewicz
parent 0f5c621bd7
commit b29ecfd822
4 changed files with 89 additions and 65 deletions

View file

@ -11,11 +11,13 @@ static struct {
const char* bif_type; const char* bif_type;
const char* bro_type; const char* bro_type;
const char* c_type; const char* c_type;
const char* c_type_smart;
const char* accessor; const char* accessor;
const char* constructor; const char* constructor;
const char* ctor_smart;
} builtin_func_arg_type[] = { } builtin_func_arg_type[] = {
#define DEFINE_BIF_TYPE(id, bif_type, bro_type, c_type, accessor, constructor) \ #define DEFINE_BIF_TYPE(id, bif_type, bro_type, c_type, c_type_smart, accessor, constructor, ctor_smart) \
{bif_type, bro_type, c_type, accessor, constructor}, {bif_type, bro_type, c_type, c_type_smart, accessor, constructor, ctor_smart},
#include "bif_type.def" #include "bif_type.def"
#undef DEFINE_BIF_TYPE #undef DEFINE_BIF_TYPE
}; };
@ -68,15 +70,19 @@ void BuiltinFuncArg::PrintCDef(FILE* fp, int n)
fprintf(fp, ");\n"); fprintf(fp, ");\n");
} }
void BuiltinFuncArg::PrintCArg(FILE* fp, int n) void BuiltinFuncArg::PrintCArg(FILE* fp, int n, bool smart)
{ {
const char* ctype = builtin_func_arg_type[type].c_type; const char* ctype = smart ? builtin_func_arg_type[type].c_type_smart
: builtin_func_arg_type[type].c_type;
char buf[1024]; char buf[1024];
fprintf(fp, "%s %s", ctype, name); fprintf(fp, "%s %s", ctype, name);
} }
void BuiltinFuncArg::PrintBroValConstructor(FILE* fp) void BuiltinFuncArg::PrintBroValConstructor(FILE* fp, bool smart)
{ {
if ( smart )
fprintf(fp, builtin_func_arg_type[type].ctor_smart, name);
else
fprintf(fp, builtin_func_arg_type[type].constructor, name); fprintf(fp, builtin_func_arg_type[type].constructor, name);
} }

View file

@ -63,6 +63,8 @@ struct decl_struct {
string generate_c_fullname; string generate_c_fullname;
string generate_c_namespace_start; string generate_c_namespace_start;
string generate_c_namespace_end; string generate_c_namespace_end;
string enqueue_c_barename;
string enqueue_c_fullname;
} decl; } decl;
void set_definition_type(int type, const char *arg_type_name) void set_definition_type(int type, const char *arg_type_name)
@ -90,7 +92,9 @@ void set_decl_name(const char *name)
decl.bro_name = ""; decl.bro_name = "";
decl.generate_c_fullname = ""; decl.generate_c_fullname = "";
decl.enqueue_c_fullname = "";
decl.generate_bare_name = string("generate_") + decl.bare_name; decl.generate_bare_name = string("generate_") + decl.bare_name;
decl.enqueue_c_barename = string("enqueue_") + decl.bare_name;
decl.generate_c_namespace_start = ""; decl.generate_c_namespace_start = "";
decl.generate_c_namespace_end = ""; decl.generate_c_namespace_end = "";
@ -120,6 +124,7 @@ void set_decl_name(const char *name)
decl.generate_c_namespace_start = "namespace BifEvent { "; decl.generate_c_namespace_start = "namespace BifEvent { ";
decl.generate_c_namespace_end = " } "; decl.generate_c_namespace_end = " } ";
decl.generate_c_fullname = "BifEvent::"; decl.generate_c_fullname = "BifEvent::";
decl.enqueue_c_fullname = "BifEvent::";
break; break;
default: default:
@ -136,6 +141,7 @@ void set_decl_name(const char *name)
decl.generate_c_namespace_start += "namespace " + decl.module_name + " { "; decl.generate_c_namespace_start += "namespace " + decl.module_name + " { ";
decl.generate_c_namespace_end += " } "; decl.generate_c_namespace_end += " } ";
decl.generate_c_fullname += decl.module_name + "::"; decl.generate_c_fullname += decl.module_name + "::";
decl.enqueue_c_fullname += decl.module_name + "::";
} }
decl.bro_fullname += decl.bare_name; decl.bro_fullname += decl.bare_name;
@ -145,7 +151,7 @@ void set_decl_name(const char *name)
decl.c_fullname += decl.bare_name; decl.c_fullname += decl.bare_name;
decl.bro_name += name; decl.bro_name += name;
decl.generate_c_fullname += decl.generate_bare_name; decl.generate_c_fullname += decl.generate_bare_name;
decl.enqueue_c_fullname += decl.enqueue_c_barename;
} }
const char* arg_list_name = "BiF_ARGS"; const char* arg_list_name = "BiF_ARGS";
@ -157,11 +163,13 @@ static struct {
const char* bif_type; const char* bif_type;
const char* bro_type; const char* bro_type;
const char* c_type; const char* c_type;
const char* c_type_smart;
const char* accessor; const char* accessor;
const char* constructor; const char* constructor;
const char* ctor_smatr;
} builtin_types[] = { } builtin_types[] = {
#define DEFINE_BIF_TYPE(id, bif_type, bro_type, c_type, accessor, constructor) \ #define DEFINE_BIF_TYPE(id, bif_type, bro_type, c_type, c_type_smart, accessor, constructor, ctor_smart) \
{bif_type, bro_type, c_type, accessor, constructor}, {bif_type, bro_type, c_type, c_type_smart, accessor, constructor, ctor_smart},
#include "bif_type.def" #include "bif_type.def"
#undef DEFINE_BIF_TYPE #undef DEFINE_BIF_TYPE
}; };
@ -199,32 +207,54 @@ char* concat(const char* str1, const char* str2)
return s; return s;
} }
// Print the bro_event_* function prototype in C++, without the ending ';' static void print_event_c_prototype_args(FILE* fp, bool smart)
void print_event_c_prototype(FILE *fp, bool is_header)
{ {
if ( is_header ) for ( auto i = 0u; i < args.size(); ++i )
{
if ( i > 0 )
fprintf(fp, ", ");
args[i]->PrintCArg(fp, i, smart);
}
}
static void print_event_c_prototype_header(FILE* fp, bool smart)
{
if ( smart )
fprintf(fp, "%s void %s(analyzer::Analyzer* analyzer%s", fprintf(fp, "%s void %s(analyzer::Analyzer* analyzer%s",
decl.generate_c_namespace_start.c_str(), decl.generate_bare_name.c_str(), decl.generate_c_namespace_start.c_str(),
decl.enqueue_c_barename.c_str(),
args.size() ? ", " : "" );
else
fprintf(fp, "%s [[deprecated(\"Remove in 4.1. Use %s instead.\")]] void %s(analyzer::Analyzer* analyzer%s",
decl.generate_c_namespace_start.c_str(),
decl.enqueue_c_fullname.c_str(),
decl.generate_bare_name.c_str(),
args.size() ? ", " : "" );
print_event_c_prototype_args(fp, smart);
fprintf(fp, ")");
fprintf(fp, "; %s\n", decl.generate_c_namespace_end.c_str());
}
static void print_event_c_prototype_impl(FILE* fp, bool smart)
{
if ( smart )
fprintf(fp, "void %s(analyzer::Analyzer* analyzer%s",
decl.enqueue_c_fullname.c_str(),
args.size() ? ", " : "" ); args.size() ? ", " : "" );
else else
fprintf(fp, "void %s(analyzer::Analyzer* analyzer%s", fprintf(fp, "void %s(analyzer::Analyzer* analyzer%s",
decl.generate_c_fullname.c_str(), decl.generate_c_fullname.c_str(),
args.size() ? ", " : "" ); args.size() ? ", " : "" );
for ( int i = 0; i < (int) args.size(); ++i )
{ print_event_c_prototype_args(fp, smart);
if ( i > 0 )
fprintf(fp, ", ");
args[i]->PrintCArg(fp, i);
}
fprintf(fp, ")"); fprintf(fp, ")");
if ( is_header )
fprintf(fp, "; %s\n", decl.generate_c_namespace_end.c_str());
else
fprintf(fp, "\n"); fprintf(fp, "\n");
} }
// Print the bro_event_* function body in C++. static void print_event_c_body(FILE* fp, bool smart)
void print_event_c_body(FILE *fp)
{ {
fprintf(fp, "\t{\n"); fprintf(fp, "\t{\n");
fprintf(fp, "\t// Note that it is intentional that here we do not\n"); fprintf(fp, "\t// Note that it is intentional that here we do not\n");
@ -242,7 +272,7 @@ void print_event_c_body(FILE *fp)
for ( int i = 0; i < (int) args.size(); ++i ) for ( int i = 0; i < (int) args.size(); ++i )
{ {
fprintf(fp, "\t "); fprintf(fp, "\t ");
args[i]->PrintBroValConstructor(fp); args[i]->PrintBroValConstructor(fp, smart);
fprintf(fp, ",\n"); fprintf(fp, ",\n");
if ( args[i]->Type() == TYPE_CONNECTION ) if ( args[i]->Type() == TYPE_CONNECTION )
@ -266,7 +296,7 @@ void print_event_c_body(FILE *fp)
fprintf(fp, ", %s", connection_arg->Name()); fprintf(fp, ", %s", connection_arg->Name());
fprintf(fp, ");\n"); fprintf(fp, ");\n");
fprintf(fp, "\t} // event generation\n"); fprintf(fp, "\t}\n\n");
//fprintf(fp, "%s // end namespace\n", decl.generate_c_namespace_end.c_str()); //fprintf(fp, "%s // end namespace\n", decl.generate_c_namespace_end.c_str());
} }
@ -386,9 +416,12 @@ event_def: event_prefix opt_ws plain_head opt_func_attrs
{ {
if ( events.find(decl.bro_fullname) == events.end() ) if ( events.find(decl.bro_fullname) == events.end() )
{ {
print_event_c_prototype(fp_func_h, true); print_event_c_prototype_header(fp_func_h, false);
print_event_c_prototype(fp_func_def, false); print_event_c_prototype_impl(fp_func_def, false);
print_event_c_body(fp_func_def); print_event_c_body(fp_func_def, false);
print_event_c_prototype_header(fp_func_h, true);
print_event_c_prototype_impl(fp_func_def, true);
print_event_c_body(fp_func_def, true);
events.insert(decl.bro_fullname); events.insert(decl.bro_fullname);
} }
} }

View file

@ -3,20 +3,10 @@
#include <stdio.h> #include <stdio.h>
enum builtin_func_arg_type { enum builtin_func_arg_type {
#define DEFINE_BIF_TYPE(id, bif_type, bro_type, c_type, accessor, constructor) \ #define DEFINE_BIF_TYPE(id, bif_type, bro_type, c_type, c_type_smart, accessor, constructor, ctor_smart) \
id, id,
#include "bif_type.def" #include "bif_type.def"
#undef DEFINE_BIF_TYPE #undef DEFINE_BIF_TYPE
/*
TYPE_ANY,
TYPE_BOOL,
TYPE_COUNT,
TYPE_INT,
TYPE_STRING,
TYPE_PATTERN,
TYPE_PORT,
TYPE_OTHER,
*/
}; };
extern const char* builtin_func_arg_type_bro_name[]; extern const char* builtin_func_arg_type_bro_name[];
@ -37,8 +27,8 @@ public:
void PrintBro(FILE* fp); void PrintBro(FILE* fp);
void PrintCDef(FILE* fp, int n); void PrintCDef(FILE* fp, int n);
void PrintCArg(FILE* fp, int n); void PrintCArg(FILE* fp, int n, bool smart);
void PrintBroValConstructor(FILE* fp); void PrintBroValConstructor(FILE* fp, bool smart);
protected: protected:
const char* name; const char* name;

View file

@ -1,22 +1,17 @@
// DEFINE_BIF_TYPE(id, bif_type, bro_type, c_type, accessor, constructor) // (id, bif_type, bro_type, c_type, c_type_smart, accessor, constructor, ctor_smart)
DEFINE_BIF_TYPE(TYPE_ADDR, "addr", "addr", "AddrVal*", "IntrusivePtr<AddrVal>", "%s->AsAddrVal()", "IntrusivePtr{AdoptRef{}, %s}", "std::move(%s)")
DEFINE_BIF_TYPE(TYPE_ADDR, "addr", "addr", "AddrVal*", "%s->AsAddrVal()", "IntrusivePtr{AdoptRef{}, %s}") DEFINE_BIF_TYPE(TYPE_ANY, "any", "any", "Val*", "IntrusivePtr<Val>", "%s", "IntrusivePtr{AdoptRef{}, %s}", "std::move(%s)")
DEFINE_BIF_TYPE(TYPE_ANY, "any", "any", "Val*", "%s", "IntrusivePtr{AdoptRef{}, %s}") DEFINE_BIF_TYPE(TYPE_BOOL, "bool", "bool", "int", "int", "%s->AsBool()", "val_mgr->Bool(%s)", "val_mgr->Bool(%s)")
DEFINE_BIF_TYPE(TYPE_BOOL, "bool", "bool", "int", "%s->AsBool()", "val_mgr->Bool(%s)") DEFINE_BIF_TYPE(TYPE_CONN_ID, "conn_id", "conn_id", "Val*", "IntrusivePtr<Val>", "%s", "IntrusivePtr{AdoptRef{}, %s}", "std::move(%s)")
DEFINE_BIF_TYPE(TYPE_CONN_ID, "conn_id", "conn_id", "Val*", "%s", "IntrusivePtr{AdoptRef{}, %s}") DEFINE_BIF_TYPE(TYPE_CONNECTION, "connection", "connection", "Connection*", "Connection*", "%s->AsRecordVal()->GetOrigin()", "%s->ConnVal()", "%s->ConnVal()")
DEFINE_BIF_TYPE(TYPE_CONNECTION, "connection", "connection", "Connection*", "%s->AsRecordVal()->GetOrigin()", "%s->ConnVal()") DEFINE_BIF_TYPE(TYPE_COUNT, "count", "count", "bro_uint_t", "bro_uint_t", "%s->AsCount()", "val_mgr->Count(%s)", "val_mgr->Count(%s)")
DEFINE_BIF_TYPE(TYPE_COUNT, "count", "count", "bro_uint_t", "%s->AsCount()", "val_mgr->Count(%s)") DEFINE_BIF_TYPE(TYPE_DOUBLE, "double", "double", "double", "double", "%s->AsDouble()", "make_intrusive<Val>(%s, TYPE_DOUBLE)", "make_intrusive<Val>(%s, TYPE_DOUBLE)")
DEFINE_BIF_TYPE(TYPE_DOUBLE, "double", "double", "double", "%s->AsDouble()", "make_intrusive<Val>(%s, TYPE_DOUBLE)") DEFINE_BIF_TYPE(TYPE_FILE, "file", "file", "BroFile*", "IntrusivePtr<Val>", "%s->AsFile()", "make_intrusive<Val>(%s)", "std::move(%s)")
DEFINE_BIF_TYPE(TYPE_FILE, "file", "file", "BroFile*", "%s->AsFile()", "make_intrusive<Val>(%s)") DEFINE_BIF_TYPE(TYPE_INT, "int", "int", "bro_int_t", "bro_int_t", "%s->AsInt()", "val_mgr->Int(%s)", "val_mgr->Int(%s)")
DEFINE_BIF_TYPE(TYPE_INT, "int", "int", "bro_int_t", "%s->AsInt()", "val_mgr->Int(%s)") DEFINE_BIF_TYPE(TYPE_INTERVAL, "interval", "interval", "double", "double", "%s->AsInterval()", "make_intrusive<IntervalVal>(%s, Seconds)", "make_intrusive<IntervalVal>(%s, Seconds)")
DEFINE_BIF_TYPE(TYPE_INTERVAL, "interval", "interval", "double", "%s->AsInterval()", "make_intrusive<IntervalVal>(%s, Seconds)") DEFINE_BIF_TYPE(TYPE_PATTERN, "pattern", "pattern", "RE_Matcher*", "IntrusivePtr<PatternVal>", "%s->AsPattern()", "make_intrusive<PatternVal>(%s)", "std::move(%s)")
DEFINE_BIF_TYPE(TYPE_PACKET, "packet", "packet", "TCP_TracePacket*", "%s->AsRecordVal()->GetOrigin()", "IntrusivePtr{AdoptRef{}, %s->PacketVal()}") DEFINE_BIF_TYPE(TYPE_PORT, "port", "port", "PortVal*", "IntrusivePtr<PortVal>", "%s->AsPortVal()", "IntrusivePtr{AdoptRef{}, %s}", "std::move(%s)")
DEFINE_BIF_TYPE(TYPE_PATTERN, "pattern", "pattern", "RE_Matcher*", "%s->AsPattern()", "make_intrusive<PatternVal>(%s)") DEFINE_BIF_TYPE(TYPE_STRING, "string", "string", "StringVal*", "IntrusivePtr<StringVal>", "%s->AsStringVal()", "IntrusivePtr{AdoptRef{}, %s}", "std::move(%s)")
// DEFINE_BIF_TYPE(TYPE_PORT, "port", "port", "uint32", "%s->AsPortVal()->Port()", "incomplete data") DEFINE_BIF_TYPE(TYPE_SUBNET, "subnet", "subnet", "SubNetVal*", "IntrusivePtr<SubNetVal>", "%s->AsSubNetVal()", "IntrusivePtr{AdoptRef{}, %s}", "std::move(%s)")
DEFINE_BIF_TYPE(TYPE_PORT, "port", "port", "PortVal*", "%s->AsPortVal()", "IntrusivePtr{AdoptRef{}, %s}") DEFINE_BIF_TYPE(TYPE_TIME, "time", "time", "double", "double", "%s->AsTime()", "make_intrusive<Val>(%s, TYPE_TIME)", "make_intrusive<Val>(%s, TYPE_TIME)")
DEFINE_BIF_TYPE(TYPE_PORTVAL, "portval", "port", "PortVal*", "%s->AsPortVal()", "IntrusivePtr{AdoptRef{}, %s}") DEFINE_BIF_TYPE(TYPE_OTHER, "", "", "Val*", "IntrusivePtr<Val>", "%s", "IntrusivePtr{AdoptRef{}, %s}", "std::move(%s)")
DEFINE_BIF_TYPE(TYPE_STRING, "string", "string", "StringVal*", "%s->AsStringVal()", "IntrusivePtr{AdoptRef{}, %s}")
// DEFINE_BIF_TYPE(TYPE_STRING, "string", "string", "BroString*", "%s->AsString()", "make_intrusive<StringVal>(%s)")
DEFINE_BIF_TYPE(TYPE_SUBNET, "subnet", "subnet", "SubNetVal*", "%s->AsSubNetVal()", "IntrusivePtr{AdoptRef{}, %s}")
DEFINE_BIF_TYPE(TYPE_TIME, "time", "time", "double", "%s->AsTime()", "make_intrusive<Val>(%s, TYPE_TIME)")
DEFINE_BIF_TYPE(TYPE_OTHER, "", "", "Val*", "%s", "IntrusivePtr{AdoptRef{}, %s}")