mirror of
https://github.com/zeek/zeek.git
synced 2025-10-05 08:08:19 +00:00
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.
This commit is contained in:
parent
f8f343fd3a
commit
db6f028003
79 changed files with 1910 additions and 111 deletions
|
@ -161,12 +161,13 @@ bool Value::IsCompatibleType(BroType* t, bool atomic_only)
|
|||
|
||||
bool Value::Read(SerializationFormat* fmt)
|
||||
{
|
||||
int ty;
|
||||
int ty, sty;
|
||||
|
||||
if ( ! (fmt->Read(&ty, "type") && fmt->Read(&present, "present")) )
|
||||
if ( ! (fmt->Read(&ty, "type") && fmt->Read(&sty, "subtype") && fmt->Read(&present, "present")) )
|
||||
return false;
|
||||
|
||||
type = (TypeTag)(ty);
|
||||
subtype = (TypeTag)(sty);
|
||||
|
||||
if ( ! present )
|
||||
return true;
|
||||
|
@ -311,6 +312,7 @@ bool Value::Read(SerializationFormat* fmt)
|
|||
bool Value::Write(SerializationFormat* fmt) const
|
||||
{
|
||||
if ( ! (fmt->Write((int)type, "type") &&
|
||||
fmt->Write((int)subtype, "subtype") &&
|
||||
fmt->Write(present, "present")) )
|
||||
return false;
|
||||
|
||||
|
|
|
@ -26,7 +26,7 @@ struct Field {
|
|||
//! port, one for the type), and this specifies the secondary name.
|
||||
const char* secondary_name;
|
||||
TypeTag type; //! Type of the field.
|
||||
TypeTag subtype; //! Inner type for sets.
|
||||
TypeTag subtype; //! Inner type for sets and vectors.
|
||||
bool optional; //! True if field is optional.
|
||||
|
||||
/**
|
||||
|
@ -92,6 +92,7 @@ private:
|
|||
*/
|
||||
struct Value {
|
||||
TypeTag type; //! The type of the value.
|
||||
TypeTag subtype; //! Inner type for sets and vectors.
|
||||
bool present; //! False for optional record fields that are not set.
|
||||
|
||||
struct set_t { bro_int_t size; Value** vals; };
|
||||
|
@ -146,7 +147,20 @@ struct Value {
|
|||
* that is not set.
|
||||
*/
|
||||
Value(TypeTag arg_type = TYPE_ERROR, bool arg_present = true)
|
||||
: type(arg_type), present(arg_present) {}
|
||||
: type(arg_type), subtype(TYPE_VOID), present(arg_present) {}
|
||||
|
||||
/**
|
||||
* Constructor.
|
||||
*
|
||||
* arg_type: The type of the value.
|
||||
*
|
||||
* arg_type: The subtype of the value for sets and vectors.
|
||||
*
|
||||
* arg_present: False if the value represents an optional record field
|
||||
* that is not set.
|
||||
*/
|
||||
Value(TypeTag arg_type, TypeTag arg_subtype, bool arg_present = true)
|
||||
: type(arg_type), subtype(arg_subtype), present(arg_present) {}
|
||||
|
||||
/**
|
||||
* Destructor.
|
||||
|
@ -185,4 +199,4 @@ private:
|
|||
|
||||
}
|
||||
|
||||
#endif /* THREADING_SERIALIZATIONTZPES_H */
|
||||
#endif /* THREADING_SERIALIZATIONTYPES_H */
|
||||
|
|
|
@ -207,13 +207,14 @@ bool Ascii::Describe(ODesc* desc, threading::Value* val, const string& name) con
|
|||
|
||||
threading::Value* Ascii::ParseValue(const string& s, const string& name, TypeTag type, TypeTag subtype) const
|
||||
{
|
||||
if ( s.compare(separators.unset_field) == 0 ) // field is not set...
|
||||
if ( ! separators.unset_field.empty() && s.compare(separators.unset_field) == 0 ) // field is not set...
|
||||
return new threading::Value(type, false);
|
||||
|
||||
threading::Value* val = new threading::Value(type, true);
|
||||
threading::Value* val = new threading::Value(type, subtype, true);
|
||||
const char* start = s.c_str();
|
||||
char* end = 0;
|
||||
errno = 0;
|
||||
size_t pos;
|
||||
|
||||
switch ( type ) {
|
||||
case TYPE_ENUM:
|
||||
|
@ -260,11 +261,21 @@ threading::Value* Ascii::ParseValue(const string& s, const string& name, TypeTag
|
|||
break;
|
||||
|
||||
case TYPE_PORT:
|
||||
val->val.port_val.proto = TRANSPORT_UNKNOWN;
|
||||
pos = s.find('/');
|
||||
if ( pos != std::string::npos && s.length() > pos + 1 )
|
||||
{
|
||||
auto proto = s.substr(pos+1);
|
||||
if ( strtolower(proto) == "tcp" )
|
||||
val->val.port_val.proto = TRANSPORT_TCP;
|
||||
else if ( strtolower(proto) == "udp" )
|
||||
val->val.port_val.proto = TRANSPORT_UDP;
|
||||
else if ( strtolower(proto) == "icmp" )
|
||||
val->val.port_val.proto = TRANSPORT_ICMP;
|
||||
}
|
||||
val->val.port_val.port = strtoull(start, &end, 10);
|
||||
if ( CheckNumberError(start, end) )
|
||||
goto parse_error;
|
||||
|
||||
val->val.port_val.proto = TRANSPORT_UNKNOWN;
|
||||
break;
|
||||
|
||||
case TYPE_SUBNET:
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue