Skip to content

Commit a25704c

Browse files
committed
Port bifcl code from Bro
1 parent 7ad146e commit a25704c

14 files changed

+1722
-0
lines changed

.gitignore

+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
build
2+
tmp

.gitmodules

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
[submodule "cmake"]
2+
path = cmake
3+
url = git://git.bro.org/cmake

CMakeLists.txt

+71
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
project(BifCl C CXX)
2+
3+
cmake_minimum_required(VERSION 2.8.12 FATAL_ERROR)
4+
5+
include(cmake/CommonCMakeConfig.cmake)
6+
7+
include(FindRequiredPackage)
8+
9+
FindRequiredPackage(BISON)
10+
FindRequiredPackage(FLEX)
11+
12+
if ( MISSING_PREREQS )
13+
foreach (prereq ${MISSING_PREREQ_DESCS})
14+
message(SEND_ERROR ${prereq})
15+
endforeach ()
16+
message(FATAL_ERROR "Configuration aborted due to missing prerequisites")
17+
endif ()
18+
19+
include_directories(BEFORE
20+
${CMAKE_CURRENT_SOURCE_DIR}
21+
${CMAKE_CURRENT_BINARY_DIR}
22+
)
23+
24+
set(BISON_FLAGS "--debug")
25+
26+
# BIF parser/scanner
27+
bison_target(BIFParser builtin-func.y
28+
${CMAKE_CURRENT_BINARY_DIR}/bif_parse.cc
29+
HEADER ${CMAKE_CURRENT_BINARY_DIR}/bif_parse.h
30+
#VERBOSE ${CMAKE_CURRENT_BINARY_DIR}/bif_parse.output
31+
COMPILE_FLAGS "${BISON_FLAGS}")
32+
flex_target(BIFScanner builtin-func.l ${CMAKE_CURRENT_BINARY_DIR}/bif_lex.cc)
33+
add_flex_bison_dependency(BIFScanner BIFParser)
34+
35+
set(bifcl_SRCS
36+
${BISON_BIFParser_INPUT}
37+
${FLEX_BIFScanner_INPUT}
38+
${BISON_BIFParser_OUTPUTS}
39+
${FLEX_BIFScanner_OUTPUTS}
40+
bif_arg.cc
41+
bif_arg.h
42+
module_util.cc
43+
module_util.h
44+
)
45+
46+
add_executable(bifcl ${bifcl_SRCS})
47+
48+
install(TARGETS bifcl DESTINATION bin)
49+
50+
if (CMAKE_BUILD_TYPE)
51+
string(TOUPPER ${CMAKE_BUILD_TYPE} BuildType)
52+
endif ()
53+
54+
message(
55+
"\n====================| Bifcl Build Summary |====================="
56+
"\n"
57+
"\nBuild type: ${CMAKE_BUILD_TYPE}"
58+
"\nBuild dir: ${CMAKE_BINARY_DIR}"
59+
"\nInstall prefix: ${CMAKE_INSTALL_PREFIX}"
60+
"\nDebug mode: ${ENABLE_DEBUG}"
61+
"\n"
62+
"\nCC: ${CMAKE_C_COMPILER}"
63+
"\nCFLAGS: ${CMAKE_C_FLAGS} ${CMAKE_C_FLAGS_${BuildType}}"
64+
"\nCXX: ${CMAKE_CXX_COMPILER}"
65+
"\nCXXFLAGS: ${CMAKE_CXX_FLAGS} ${CMAKE_CXX_FLAGS_${BuildType}}"
66+
"\nCPP: ${CMAKE_CXX_COMPILER}"
67+
"\n"
68+
"\n================================================================\n"
69+
)
70+
71+
include(UserChangedWarning)

COPYING

+34
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
Copyright (c) 1995-2018, The Regents of the University of California
2+
through the Lawrence Berkeley National Laboratory and the
3+
International Computer Science Institute. All rights reserved.
4+
5+
Redistribution and use in source and binary forms, with or without
6+
modification, are permitted provided that the following conditions are met:
7+
8+
(1) Redistributions of source code must retain the above copyright
9+
notice, this list of conditions and the following disclaimer.
10+
11+
(2) Redistributions in binary form must reproduce the above copyright
12+
notice, this list of conditions and the following disclaimer in the
13+
documentation and/or other materials provided with the distribution.
14+
15+
(3) Neither the name of the University of California, Lawrence Berkeley
16+
National Laboratory, U.S. Dept. of Energy, International Computer
17+
Science Institute, nor the names of contributors may be used to endorse
18+
or promote products derived from this software without specific prior
19+
written permission.
20+
21+
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
22+
AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23+
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24+
ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
25+
LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
26+
CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
27+
SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
28+
INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
29+
CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
30+
ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
31+
POSSIBILITY OF SUCH DAMAGE.
32+
33+
Note that some files in the distribution may carry their own copyright
34+
notices.

README

+19
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
.. _Bro: https://bro.org
2+
3+
================
4+
Bro BIF Compiler
5+
================
6+
7+
The ``bifcl`` program simply takes a ``.bif`` file as input and
8+
generates C++ header/source files along with a ``.bro`` script
9+
that all-together provide the declaration and implementation of Bro_
10+
Built-In-Functions (BIFs), which can then be compiled and shipped
11+
as part of a Bro plugin.
12+
13+
A BIF allows one to write arbitrary C++ code and access it via a
14+
function call inside a Bro script. In this way, they can also be
15+
used to access parts of Bro's internal C++ API that aren't already
16+
exposed via their own BIFs.
17+
18+
At the moment, learning the format of a ``.bif`` file is likely easiest
19+
by just taking a look at the ``.bif`` files inside the Bro source-tree.

bif_arg.cc

+82
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
2+
#include <set>
3+
#include <string>
4+
using namespace std;
5+
6+
#include <string.h>
7+
8+
#include "bif_arg.h"
9+
10+
static struct {
11+
const char* bif_type;
12+
const char* bro_type;
13+
const char* c_type;
14+
const char* accessor;
15+
const char* constructor;
16+
} builtin_func_arg_type[] = {
17+
#define DEFINE_BIF_TYPE(id, bif_type, bro_type, c_type, accessor, constructor) \
18+
{bif_type, bro_type, c_type, accessor, constructor},
19+
#include "bif_type.def"
20+
#undef DEFINE_BIF_TYPE
21+
};
22+
23+
extern const char* arg_list_name;
24+
25+
BuiltinFuncArg::BuiltinFuncArg(const char* arg_name, int arg_type)
26+
{
27+
name = arg_name;
28+
type = arg_type;
29+
type_str = "";
30+
attr_str = "";
31+
}
32+
33+
BuiltinFuncArg::BuiltinFuncArg(const char* arg_name, const char* arg_type_str,
34+
const char* arg_attr_str)
35+
{
36+
name = arg_name;
37+
type = TYPE_OTHER;
38+
type_str = arg_type_str;
39+
attr_str = arg_attr_str;
40+
41+
for ( int i = 0; builtin_func_arg_type[i].bif_type[0] != '\0'; ++i )
42+
if ( ! strcmp(builtin_func_arg_type[i].bif_type, arg_type_str) )
43+
{
44+
type = i;
45+
type_str = "";
46+
}
47+
}
48+
49+
void BuiltinFuncArg::PrintBro(FILE* fp)
50+
{
51+
fprintf(fp, "%s: %s%s %s", name, builtin_func_arg_type[type].bro_type,
52+
type_str, attr_str);
53+
}
54+
55+
void BuiltinFuncArg::PrintCDef(FILE* fp, int n)
56+
{
57+
fprintf(fp,
58+
"\t%s %s = (%s) (",
59+
builtin_func_arg_type[type].c_type,
60+
name,
61+
builtin_func_arg_type[type].c_type);
62+
63+
char buf[1024];
64+
snprintf(buf, sizeof(buf), "(*%s)[%d]", arg_list_name, n);
65+
// Print the accessor expression.
66+
fprintf(fp, builtin_func_arg_type[type].accessor, buf);
67+
68+
fprintf(fp, ");\n");
69+
}
70+
71+
void BuiltinFuncArg::PrintCArg(FILE* fp, int n)
72+
{
73+
const char* ctype = builtin_func_arg_type[type].c_type;
74+
char buf[1024];
75+
76+
fprintf(fp, "%s %s", ctype, name);
77+
}
78+
79+
void BuiltinFuncArg::PrintBroValConstructor(FILE* fp)
80+
{
81+
fprintf(fp, builtin_func_arg_type[type].constructor, name);
82+
}

bif_arg.h

+51
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
#ifndef bif_arg_h
2+
#define bif_arg_h
3+
4+
#include <stdio.h>
5+
6+
enum builtin_func_arg_type {
7+
#define DEFINE_BIF_TYPE(id, bif_type, bro_type, c_type, accessor, constructor) \
8+
id,
9+
#include "bif_type.def"
10+
#undef DEFINE_BIF_TYPE
11+
/*
12+
TYPE_ANY,
13+
TYPE_BOOL,
14+
TYPE_COUNT,
15+
TYPE_INT,
16+
TYPE_STRING,
17+
TYPE_PATTERN,
18+
TYPE_PORT,
19+
TYPE_OTHER,
20+
*/
21+
};
22+
23+
extern const char* builtin_func_arg_type_bro_name[];
24+
25+
class BuiltinFuncArg {
26+
public:
27+
BuiltinFuncArg(const char* arg_name, int arg_type);
28+
BuiltinFuncArg(const char* arg_name, const char* arg_type_str,
29+
const char* arg_attr_str = "");
30+
31+
void SetAttrStr(const char* arg_attr_str)
32+
{
33+
attr_str = arg_attr_str;
34+
};
35+
36+
const char* Name() const { return name; }
37+
int Type() const { return type; }
38+
39+
void PrintBro(FILE* fp);
40+
void PrintCDef(FILE* fp, int n);
41+
void PrintCArg(FILE* fp, int n);
42+
void PrintBroValConstructor(FILE* fp);
43+
44+
protected:
45+
const char* name;
46+
int type;
47+
const char* type_str;
48+
const char* attr_str;
49+
};
50+
51+
#endif

bif_type.def

+22
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
// DEFINE_BIF_TYPE(id, bif_type, bro_type, c_type, accessor, constructor)
2+
3+
DEFINE_BIF_TYPE(TYPE_ADDR, "addr", "addr", "AddrVal*", "%s->AsAddrVal()", "%s")
4+
DEFINE_BIF_TYPE(TYPE_ANY, "any", "any", "Val*", "%s", "%s")
5+
DEFINE_BIF_TYPE(TYPE_BOOL, "bool", "bool", "int", "%s->AsBool()", "new Val(%s, TYPE_BOOL)")
6+
DEFINE_BIF_TYPE(TYPE_CONN_ID, "conn_id", "conn_id", "Val*", "%s", "%s")
7+
DEFINE_BIF_TYPE(TYPE_CONNECTION, "connection", "connection", "Connection*", "%s->AsRecordVal()->GetOrigin()", "%s->BuildConnVal()")
8+
DEFINE_BIF_TYPE(TYPE_COUNT, "count", "count", "bro_uint_t", "%s->AsCount()", "new Val(%s, TYPE_COUNT)")
9+
DEFINE_BIF_TYPE(TYPE_DOUBLE, "double", "double", "double", "%s->AsDouble()", "new Val(%s, TYPE_DOUBLE)")
10+
DEFINE_BIF_TYPE(TYPE_FILE, "file", "file", "BroFile*", "%s->AsFile()", "new Val(%s)")
11+
DEFINE_BIF_TYPE(TYPE_INT, "int", "int", "bro_int_t", "%s->AsInt()", "new Val(%s, TYPE_INT)")
12+
DEFINE_BIF_TYPE(TYPE_INTERVAL, "interval", "interval", "double", "%s->AsInterval()", "new IntervalVal(%s, Seconds)")
13+
DEFINE_BIF_TYPE(TYPE_PACKET, "packet", "packet", "TCP_TracePacket*", "%s->AsRecordVal()->GetOrigin()", "%s->PacketVal()")
14+
DEFINE_BIF_TYPE(TYPE_PATTERN, "pattern", "pattern", "RE_Matcher*", "%s->AsPattern()", "new PatternVal(%s)")
15+
// DEFINE_BIF_TYPE(TYPE_PORT, "port", "port", "uint32", "%s->AsPortVal()->Port()", "incomplete data")
16+
DEFINE_BIF_TYPE(TYPE_PORT, "port", "port", "PortVal*", "%s->AsPortVal()", "%s")
17+
DEFINE_BIF_TYPE(TYPE_PORTVAL, "portval", "port", "PortVal*", "%s->AsPortVal()", "%s")
18+
DEFINE_BIF_TYPE(TYPE_STRING, "string", "string", "StringVal*", "%s->AsStringVal()", "%s")
19+
// DEFINE_BIF_TYPE(TYPE_STRING, "string", "string", "BroString*", "%s->AsString()", "new StringVal(%s)")
20+
DEFINE_BIF_TYPE(TYPE_SUBNET, "subnet", "subnet", "SubNetVal*", "%s->AsSubNetVal()", "%s")
21+
DEFINE_BIF_TYPE(TYPE_TIME, "time", "time", "double", "%s->AsTime()", "new Val(%s, TYPE_TIME)")
22+
DEFINE_BIF_TYPE(TYPE_OTHER, "", "", "Val*", "%s", "%s")

0 commit comments

Comments
 (0)