Commit graph

143 commits

Author SHA1 Message Date
Johanna Amann
6d612ced3d Mark one-parameter constructors as explicit & use override where possible
This commit marks (hopefully) ever one-parameter constructor as explicit.

It also uses override in (hopefully) all circumstances where a virtual
method is overridden.

There are a very few other minor changes - most of them were necessary
to get everything to compile (like one additional constructor). In one
case I changed an implicit operation to an explicit string conversion -
I think the automatically chosen conversion was much more convoluted.

This took longer than I want to admit but not as long as I feared :)
2018-03-27 07:17:32 -07:00
Johanna Amann
196994a48d Allow the empty field separator to be empty; use in config framework.
This small change allows the empty field separator to be empty. This
means that we can represent an empty list by a empty input string, which
was not possible before.

Before, an empty empty field separator meant that there is no empty
field - to get back to this behavior one now has to set the empty field
separator to a string that is guaranteed to not be part of the input
data. Note that we did not use "empty" empty field separators anywhere
and I am not aware of this being used by anyone - the new behavior seems
like it is much more useful in practice.

This also changes the config framework to interpret empty lists as...
empty, instead of interpreting them as lists that have one zero-length
element; this seems like the saner default.
2018-01-29 14:22:59 -08:00
Johanna Amann
b0be6c90fe Fix segmentation fault when parsing sets containing invalid elements.
Currently the destructor would try to free unallocated memory. This
could e.g. be triggered by the input framework reading a set with an
invalid element.
2018-01-29 14:22:44 -08:00
Johanna Amann
db6f028003 Add config framework.
The configuration framework consists of three mostly distinct parts:

* option variables
* the config reader
* the script level framework

I will describe the three elements in the following.

Internally, this commit also performs a range of changes to the Input
manager; it marks a lot of functions as const and introduces a new
ValueToVal method (which could in theory replace the already existing
one - it is a bit more powerful).

This also changes SerialTypes to have a subtype for Values, just as
Fields already have it; I think it was mostly an oversight that this was
not introduced from the beginning. This should not necessitate any code
changes for people already using SerialTypes.

option variable
===============

The option keyword allows variables to be specified as run-tine options.
Such variables cannot be changed using normal assignments. Instead, they
can be changed using Option::set. It is possible to "subscribe" to
options and be notified when an option value changes.

Change handlers can also change values before they are applied; this
gives them the opportunity to reject changes. Priorities can be
specified if there are several handlers for one option.

Example script:

option testbool: bool = T;

function option_changed(ID: string, new_value: bool): bool
  {
  print fmt("Value of %s changed from %s to %s", ID, testbool, new_value);
  return new_value;
  }

event bro_init()
  {
  print "Old value", testbool;
  Option::set_change_handler("testbool", option_changed);
  Option::set("testbool", F);
  print "New value", testbool;
  }

config reader
=============

The config reader provides a way to read configuration files back into
Bro. Most importantly it automatically converts values to the correct
types. This is important because it is at least inconvenient (and
sometimes near impossible) to perform the necessary type conversions in
Bro scripts themselves. This is especially true for sets/vectors.

Configuration generally look like this:

[option name][tab/spaces][new variable value]

so, for example:

testaddr 2607:f8b0:4005:801::200e
testinterval 60
testtime 1507321987
test_set a	b	c	d	erdbeerschnitzel

The reader uses the option name to look up the type that variable has in
the Bro core and automatically converts the value to the correct type.

Example script use:

type Idx: record {
  option_name: string;
};

type Val: record {
  option_val: string;
};

global currconfig: table[string] of string = table();

event InputConfig::new_value(name: string, source: string, id: string, value: any)
  {
  print id, value;
  }

event bro_init()
  {
  Input::add_table([$reader=Input::READER_CONFIG, $source="../configfile", $name="configuration", $idx=Idx, $val=Val, $destination=currconfig, $want_record=F]);
  }

Script-level config framework
=============================

The script-level framework ties these two features together and makes
them a bit more convenient to use. Configuration files can simply be
specified by placing them into Config::config_files. The framework also
creates a config.log that shows all value changes that took place.

Usage example:

redef Config::config_files += {configfile};

export {
  option testbool : bool = F;
}

The file is now monitored for changes; when a change occurs the
respective option values are automatically updated and the value change
is written to config.log.
2017-11-29 13:46:59 -08:00
Johanna Amann
fc33bf2014 Make strerror_r portable.
This uses the same code that broker already uses to determine if we use
the XSI or gnu version of strerror_r. Patch by Thomas Petersen.
2017-09-18 14:50:30 -07:00
Johanna Amann
684ea8aa37 Plugin: Add hooks for log init and writing.
The two hooks being added are:

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);

which is called when a writer is being instantiated and contains
information about the fields being logged, as well as

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);

which is called for each log line being written by each writer. It
contains all the data being written. The data can be changed in the
function call and lines can be prevented from being written.

This commit also fixes a few small problems with plugin hooks itself,
and extends the tests that were already there, besides introducing tests
for the added functionality.
2017-04-24 14:02:05 -07:00
Johanna Amann
a7b1161c12 Threading Types: add a bit of documentation to subnet type.
This explains how the length field is currently handled when exchanging
data in both directions.
2017-04-10 11:37:43 -07:00
Johanna Amann
17fa1b6fed Fix compiler warnings raised by llvm 8.0.
Warning was:

warning: moving a temporary object prevents copy elision [-Wpessimizing-move]
2017-03-13 11:16:19 -07:00
Robin Sommer
6302b103f4 Merge remote-tracking branch 'origin/topic/johanna/threads'
Tweaked the new threading code a bit more.

* origin/topic/johanna/threads:
  Move threading to c++11 primitives (mostly).
2017-03-03 10:40:04 -08:00
Johanna Amann
9341ff801c Move threading to c++11 primitives (mostly).
This moves all threading code in Bro from pthreads to the c++11
primitives, which make for shorter, easier to use, and less error-prone
code.

pthreads is still used in 2 places in Bro currently. BasicThread uses
two bits of functionality that are not available using the c++ API
(setting thread names & setting signal masks). Since all c++
implementations that I am aware of still use an underlying pthreads
implementation, we just use native_handle to access the underlying
pthreads implementation for these cases. I do not expect this to lead to
problems in the forseable future. If we ever encounter a platform where
a different thread architecture is used, we might have to change that
around.

This code is guarded by static_asserts, so we will notice if a platform
uses a different implementation.

sqlite also uses pthreads directly.
2017-03-02 08:53:38 -08:00
Johanna Amann
ed3447acaa Fix one more format error found by gcc 5.3.1 2016-08-15 16:54:22 -07:00
Johanna Amann
bfdce4d419 Add argument checking to a few more printf-style calls. 2016-08-15 15:34:27 -07:00
Robin Sommer
5b73345b73 Merge remote-tracking branch 'origin/topic/johanna/bit-1181'
BIT-1181 #merged

* origin/topic/johanna/bit-1181:
  Input: Further small changes to error handling
  Add error events to input framework.
2016-07-26 14:52:27 -07:00
Johanna Amann
6b9abe85a7 Add error events to input framework.
This change introduces error events for Table and Event readers. Users
can now specify an event that is called when an info, warning, or error
is emitted by their input reader. This can, e.g., be used to raise
notices in case errors occur when reading an important input stream.

Example:

event error_event(desc: Input::TableDescription, msg: string, level: Reporter::Level)
	{
	...
	}

event bro_init()
	{
	Input::add_table([$source="a", $error_ev=error_event, ...]);
	}

For the moment, this converts all errors in the Asciiformatter into
warnings (to show that they are non-fatal) - the Reader itself also has
to throw an Error to show that a fatal error occurred and processing
will be abort.

It might be nicer to change this and require readers to mark fatal
errors as such when throwing them.

Addresses BIT-1181
2016-07-22 19:45:28 -07:00
Robin Sommer
721693425f Escape the empty indicator in logs if it occurs literally as a field's
actual content.

Addresses BIT-931.
2016-07-05 16:34:24 -07:00
Robin Sommer
d86bf15dbf Do not use scientific notations when printing doubles in logs.
Closes BIT-1558.
2016-05-23 16:07:22 -07:00
Seth Hall
40e9724de7 Switching all use of gmtime and localtime to use reentrant variants.
This was causing occasional problems with the time on processes
running lots of threads.  The use of gmtime in the json
formatter is the likely culprit due to the fact that the
json formatter runs in threads.  More evidence for this is that
the problem only appears to exhibit when logs are being written
as JSON.
2016-05-07 01:22:38 -04:00
Robin Sommer
9d7ec6b6d2 Merge branch 'master' of https://github.com/aeppert/bro
Cleaned up the surrounding code a bit and also added '[' as another
case (not sure that can happen, but doesn't hurt eihter).

* 'master' of https://github.com/aeppert/bro:
  Whitespace
  Remove
  Remove.
  Fix for JSON formatter
  A fatal error, especially in DEBUG, should result in a core.
  Seems to fix a case where an entry in the table may be null on insert.
2015-10-26 16:52:47 -07:00
Aaron Eppert
295dbc3055 Fix for JSON formatter
In the event that the first entry in a record is optional AND not present, the serializer will incorrectly add a leading comma. This leading common is invalid JSON and will, more often than not, cause parser failures downstream.
2015-10-26 17:55:01 -04:00
Robin Sommer
3957091e1b Renaming config.h to bro-config.h.
A couple times now I had this conflicting with files of the same name
in other projects.
2015-07-28 11:57:04 -07:00
Seth Hall
b00bd7702f Add the ability to remove surrounding braces from the JSON formatter. 2015-02-13 22:02:54 -05:00
Jon Siwek
59c54a0fc6 Add a simple FD_Set wrapper/helper class. 2014-09-09 16:28:04 -05:00
Jon Siwek
cf66bd8b69 Merge branch 'master' into topic/jsiwek/improve_comm_loop
Conflicts:
	src/CMakeLists.txt
	src/FlowSrc.cc
	src/FlowSrc.h
	src/IOSource.h
	src/PktSrc.cc
	src/PktSrc.h
	src/iosource/Manager.cc
2014-09-09 14:19:43 -05:00
Jon Siwek
675fba3fde Remove timeouts from remote communication loop.
The select() now blocks until there's work to do instead of relying on a
small timeout value which can cause unproductive use of cpu cycles.
2014-08-28 13:13:30 -05:00
Robin Sommer
bf6dd2e9ca Merge remote-tracking branch 'origin/master' into topic/robin/pktsrc
Conflicts:
	configure
	src/CMakeLists.txt
	src/Net.cc
	src/PacketSort.cc
	src/PacketSort.h
	src/RemoteSerializer.cc
	src/Sessions.cc
	src/Sessions.h
2014-08-22 15:41:42 -07:00
Jon Siwek
d7d5497436 Improve/standardize some malloc/realloc return val checks. 2014-04-29 15:26:19 -05:00
Jon Siwek
e8a5ea8844 Refactor various hex escaping code. 2014-04-18 13:19:50 -05:00
Robin Sommer
1ef4daf0a7 Merge remote-tracking branch 'origin/fastpath'
* origin/fastpath:
  Change how input/logging threads set their name.
  Fix bug when clearing Bloom filter contents.
2014-04-17 17:49:52 -05:00
Jon Siwek
c9b40f1ca7 Change how input/logging threads set their name.
Setting the thread name on every heartbeat uses a mild amount of
cycles and there's not much benefit to doing it there to get the
additional info regarding the number of processed messages since thread
names usually get truncated to 16 characters and omit that part anyway.
2014-04-15 16:36:47 -05:00
Robin Sommer
2d170edb1e Merge remote-tracking branch 'origin/topic/bernhard/ticket-1163'
* origin/topic/bernhard/ticket-1163:
  Change #types description of sets to set

BIT-1163 #merged
2014-04-01 16:50:58 -07:00
Bernhard Amann
01d075bf2d Change #types description of sets to set
Addresses BIT-1163
2014-04-01 16:25:47 -07:00
Bernhard Amann
636d25e526 Fix compile errror on freebsd - defines have to be moved up due to
header dependencies.
2014-03-16 22:22:44 -07:00
Robin Sommer
8b241947d6 Fixing a few cases of undefined behaviour introduced by recent
formatter work.

Thanks, Coverity!
2014-03-14 08:45:36 -07:00
Robin Sommer
17f9d0a47d Fixing compiler error.
Hopefully ...
2014-03-13 18:02:41 -07:00
Robin Sommer
ff261ea626 Merge remote-tracking branch 'origin/topic/seth/json-formatter'
* origin/topic/seth/json-formatter:
  Updating a couple of tests.
  Expanded support for modifying the timestamp format in the JSON formatter.
  Ascii input reader now supports all config options per-input stream.
  Added an option to the JSON formatter to use ISO 8601 for timestamps.
  Refactored formatters and updated the the writers a bit.

Includes some minor bugfixes and cleanup at various places, including
in old code.
2014-03-13 16:50:56 -07:00
Seth Hall
c591e4f57f Expanded support for modifying the timestamp format in the JSON formatter. 2014-03-12 10:01:59 -04:00
Seth Hall
c9aaf9e753 Added an option to the JSON formatter to use ISO 8601 for timestamps.
- It's not *exactly* ISO 8601 which doesn't seem to support
   subseconds, but subseconds are very important to us and
   most things that support ISO8601 seem to also support subseconds
   in the way I'm implemented it.
2014-03-10 14:22:35 -04:00
Seth Hall
a56c343715 Refactored formatters and updated the the writers a bit.
- Formatters have been abstracted similarly to readers and writers now.
 - The Ascii writer has a new option for writing out logs as JSON.
 - The Ascii writer now has all options availble as per-filter
   options as well as global.
2014-03-10 10:42:59 -04:00
Robin Sommer
93d9dde969 IOSource reorg.
A bunch of infrastructure work to move IOSource, IOSourceRegistry (now
iosource::Manager) and PktSrc/PktDumper code into iosource/, and over
to a plugin structure.

Other IOSources aren't touched yet, they are still in src/*.

It compiles and does something with a small trace, but that's all I've
tested so far. There are quite certainly a number of problems left, as
well as various TODOs and cleanup; and nothing's cast in stone yet.

Will continue to work on this.
2013-12-11 18:00:34 -08:00
Jon Siwek
22d35d2c8c Fix thread processing/termination conditions.
A thread that is done/killed should signify that the thread manager has
some processing to do -- it needs to process any messages in its out
queue, join the thread, and delete it.  Otherwise the thread manager
may reach a state where it makes no progress in processing the last
remaining done/killed thread.
2013-10-29 14:40:07 -05:00
Robin Sommer
64812daa50 Next version of the threading queue deadlock fix.
We now just use the read/write counters, as suggested by Gilbert.
2013-10-26 19:15:43 -07:00
Robin Sommer
15e72d1422 Merge remote-tracking branch 'origin/topic/bernhard/alternative-deadlock-fix'
* origin/topic/bernhard/alternative-deadlock-fix:
  alternative deadlock issue fix.
2013-10-25 11:10:08 -07:00
Bernhard Amann
2ac0d77f06 alternative deadlock issue fix.
This fix also fixes the deadlock issue without putting any
new strain into the main packet processing path.

Instead of occasionally returning true in MaybeReady sometime,
we occasionally process threads if time_mgr time is not running.

If time_mgr time is running, we have heartbeat messages that will
trigger processing in any case -- processing always checks the
exact state of the Queues.

This fix probably also means that we can remove the communication
loads from all input framework tests and run them all simultaneously.
2013-10-24 23:39:16 -07:00
Robin Sommer
c980d1055e Fix for input readers occasionally dead-locking.
Bernhard and I tracked it down we believe: the thread queue could
deadlock in certain cases. As a fix we tuned the heuristic for telling
if a queue might have input to occasionaly err on the safe side by
flagging "yes", so that processing will proceed.

It's a bit unfortunate to apply this fix last minute before the
release as it could potentially impact performance if the heuristic
fails to often. We believe the chosen parmaterization should be fine ...
2013-10-24 18:21:24 -07:00
Robin Sommer
d6855dc4eb Pass over the InternalError() changes. 2013-10-11 15:11:06 -07:00
Jon Siwek
b828a6ddc7 Review usage of Reporter::InternalError, addresses BIT-1045.
Replaced some with InternalWarning or InternalAnalyzerError, the later
being a new method which signals the analyzer to not process further
input.  Some usages I just removed if they didn't make sense or clearly
couldn't happen.  Also did some minor refactors of related code while
reviewing/exploring ways to get rid of InternalError usages.

Also, for TCP content file write failures there's a new event:
"contents_file_write_failure".
2013-10-10 14:45:06 -05:00
Jon Siwek
775ec6795e Fix uninitialized (or unused) fields. 2013-09-27 10:13:52 -05:00
Jon Siwek
0b97343ff7 Fix various potential memory leaks.
Though I expect most not to be exercised in practice.
2013-09-12 15:23:52 -05:00
Jon Siwek
c4e8908c8e Fix invalid pointer dereference in AsciiFormatter.
Using a temporary object with strtol() makes the end pointer that it
sets invalid after the call.
2013-08-29 15:50:46 -05:00
Bernhard Amann
bd02da8a0c change sqlite3 default threading mode to no-mutex, disable memory
statistics, finalize prepared statement before exitting logger.

This might fix the deadlock issue, at least it did not happen for
me on my tried on the test system where it happened quite regularly
before.
2013-05-17 13:38:26 -07:00