Port broker::data variant usages to use CAF API directly

Old code still all worked, but made use of Broker functions which
now just redirect to CAF ones.
This commit is contained in:
Jon Siwek 2018-07-17 14:20:19 -05:00
parent 4c072409f0
commit 9caad8a042
8 changed files with 125 additions and 118 deletions

View file

@ -1,4 +1,9 @@
2.5-743 | 2018-07-17 14:20:19 -0500
* Port broker::data variant usages to use CAF API directly
(Jon Siwek, Corelight)
2.5-741 | 2018-07-16 16:06:02 -0500 2.5-741 | 2018-07-16 16:06:02 -0500
* Improve Specific_RE_Matcher::CompileSet() error condition cleanup * Improve Specific_RE_Matcher::CompileSet() error condition cleanup

View file

@ -1 +1 @@
2.5-741 2.5-743

@ -1 +1 @@
Subproject commit 1d90b931cc888e31b0d4774dbae54f5758841ab3 Subproject commit 467024ab09e3443798126e69054fb8862c69562f

View file

@ -215,7 +215,7 @@ struct val_converter {
{ {
auto expected_index_types = tt->Indices()->Types(); auto expected_index_types = tt->Indices()->Types();
broker::vector composite_key; broker::vector composite_key;
auto indices = broker::get_if<broker::vector>(item); auto indices = caf::get_if<broker::vector>(&item);
if ( indices ) if ( indices )
{ {
@ -283,7 +283,7 @@ struct val_converter {
{ {
auto expected_index_types = tt->Indices()->Types(); auto expected_index_types = tt->Indices()->Types();
broker::vector composite_key; broker::vector composite_key;
auto indices = broker::get_if<broker::vector>(item.first); auto indices = caf::get_if<broker::vector>(&item.first);
if ( indices ) if ( indices )
{ {
@ -384,7 +384,7 @@ struct val_converter {
return nullptr; return nullptr;
} }
if ( broker::get_if<broker::none>(a[idx]) != nullptr ) if ( caf::get_if<broker::none>(&a[idx]) != nullptr )
{ {
rval->Assign(i, nullptr); rval->Assign(i, nullptr);
++idx; ++idx;
@ -411,8 +411,8 @@ struct val_converter {
if ( a.size() != 2 ) if ( a.size() != 2 )
return nullptr; return nullptr;
auto exact_text = broker::get_if<std::string>(a[0]); auto exact_text = caf::get_if<std::string>(&a[0]);
auto anywhere_text = broker::get_if<std::string>(a[1]); auto anywhere_text = caf::get_if<std::string>(&a[1]);
if ( ! exact_text || ! anywhere_text ) if ( ! exact_text || ! anywhere_text )
return nullptr; return nullptr;
@ -582,7 +582,7 @@ struct type_checker {
for ( const auto& item : a ) for ( const auto& item : a )
{ {
auto expected_index_types = tt->Indices()->Types(); auto expected_index_types = tt->Indices()->Types();
auto indices = broker::get_if<broker::vector>(item); auto indices = caf::get_if<broker::vector>(&item);
vector<const broker::data*> indices_to_check; vector<const broker::data*> indices_to_check;
if ( indices ) if ( indices )
@ -624,7 +624,7 @@ struct type_checker {
auto expect = (*expected_index_types)[i]; auto expect = (*expected_index_types)[i];
auto& index_to_check = *(indices_to_check)[i]; auto& index_to_check = *(indices_to_check)[i];
if ( ! broker::visit(type_checker{expect}, index_to_check) ) if ( ! caf::visit(type_checker{expect}, index_to_check) )
return false; return false;
} }
} }
@ -642,7 +642,7 @@ struct type_checker {
for ( auto& item : a ) for ( auto& item : a )
{ {
auto expected_index_types = tt->Indices()->Types(); auto expected_index_types = tt->Indices()->Types();
auto indices = broker::get_if<broker::vector>(item.first); auto indices = caf::get_if<broker::vector>(&item.first);
vector<const broker::data*> indices_to_check; vector<const broker::data*> indices_to_check;
if ( indices ) if ( indices )
@ -689,11 +689,11 @@ struct type_checker {
auto expect = (*expected_index_types)[i]; auto expect = (*expected_index_types)[i];
auto& index_to_check = *(indices_to_check)[i]; auto& index_to_check = *(indices_to_check)[i];
if ( ! broker::visit(type_checker{expect}, index_to_check) ) if ( ! caf::visit(type_checker{expect}, index_to_check) )
return false; return false;
} }
if ( ! broker::visit(type_checker{tt->YieldType()}, if ( ! caf::visit(type_checker{tt->YieldType()},
item.second) ) item.second) )
return false; return false;
} }
@ -709,7 +709,7 @@ struct type_checker {
for ( auto& item : a ) for ( auto& item : a )
{ {
if ( ! broker::visit(type_checker{vt->YieldType()}, item) ) if ( ! caf::visit(type_checker{vt->YieldType()}, item) )
return false; return false;
} }
@ -725,13 +725,13 @@ struct type_checker {
if ( idx >= a.size() ) if ( idx >= a.size() )
return false; return false;
if ( broker::get_if<broker::none>(a[idx]) != nullptr ) if ( caf::get_if<broker::none>(&a[idx]) != nullptr )
{ {
++idx; ++idx;
continue; continue;
} }
if ( ! broker::visit(type_checker{rt->FieldType(i)}, if ( ! caf::visit(type_checker{rt->FieldType(i)},
a[idx]) ) a[idx]) )
return false; return false;
@ -745,8 +745,8 @@ struct type_checker {
if ( a.size() != 2 ) if ( a.size() != 2 )
return false; return false;
auto exact_text = broker::get_if<std::string>(a[0]); auto exact_text = caf::get_if<std::string>(&a[0]);
auto anywhere_text = broker::get_if<std::string>(a[1]); auto anywhere_text = caf::get_if<std::string>(&a[1]);
if ( ! exact_text || ! anywhere_text ) if ( ! exact_text || ! anywhere_text )
return false; return false;
@ -775,7 +775,7 @@ Val* bro_broker::data_to_val(broker::data d, BroType* type)
if ( type->Tag() == TYPE_ANY ) if ( type->Tag() == TYPE_ANY )
return bro_broker::make_data_val(move(d)); return bro_broker::make_data_val(move(d));
return broker::visit(val_converter{type}, std::move(d)); return caf::visit(val_converter{type}, std::move(d));
} }
broker::expected<broker::data> bro_broker::val_to_data(Val* v) broker::expected<broker::data> bro_broker::val_to_data(Val* v)
@ -900,7 +900,7 @@ broker::expected<broker::data> bro_broker::val_to_data(Val* v)
key = move(composite_key); key = move(composite_key);
if ( is_set ) if ( is_set )
broker::get<broker::set>(rval).emplace(move(key)); caf::get<broker::set>(rval).emplace(move(key));
else else
{ {
auto val = val_to_data(entry->Value()); auto val = val_to_data(entry->Value());
@ -908,7 +908,7 @@ broker::expected<broker::data> bro_broker::val_to_data(Val* v)
if ( ! val ) if ( ! val )
return broker::ec::invalid_data; return broker::ec::invalid_data;
broker::get<broker::table>(rval).emplace(move(key), move(*val)); caf::get<broker::table>(rval).emplace(move(key), move(*val));
} }
} }
@ -1115,7 +1115,7 @@ struct data_type_getter {
EnumVal* bro_broker::get_data_type(RecordVal* v, Frame* frame) EnumVal* bro_broker::get_data_type(RecordVal* v, Frame* frame)
{ {
return broker::visit(data_type_getter{}, opaque_field_to_data(v, frame)); return caf::visit(data_type_getter{}, opaque_field_to_data(v, frame));
} }
broker::data& bro_broker::opaque_field_to_data(RecordVal* v, Frame* f) broker::data& bro_broker::opaque_field_to_data(RecordVal* v, Frame* f)
@ -1131,7 +1131,7 @@ broker::data& bro_broker::opaque_field_to_data(RecordVal* v, Frame* f)
bool bro_broker::DataVal::canCastTo(BroType* t) const bool bro_broker::DataVal::canCastTo(BroType* t) const
{ {
return broker::visit(type_checker{t}, data); return caf::visit(type_checker{t}, data);
} }
Val* bro_broker::DataVal::castTo(BroType* t) Val* bro_broker::DataVal::castTo(BroType* t)
@ -1192,24 +1192,24 @@ broker::data bro_broker::threading_field_to_data(const threading::Field* f)
threading::Field* bro_broker::data_to_threading_field(broker::data d) threading::Field* bro_broker::data_to_threading_field(broker::data d)
{ {
if ( ! broker::is<broker::vector>(d) ) if ( ! caf::holds_alternative<broker::vector>(d) )
return nullptr; return nullptr;
auto& v = broker::get<broker::vector>(d); auto& v = caf::get<broker::vector>(d);
auto name = broker::get_if<std::string>(v[0]); auto name = caf::get_if<std::string>(&v[0]);
auto secondary = v[1]; auto secondary = v[1];
auto type = broker::get_if<broker::count>(v[2]); auto type = caf::get_if<broker::count>(&v[2]);
auto subtype = broker::get_if<broker::count>(v[3]); auto subtype = caf::get_if<broker::count>(&v[3]);
auto optional = broker::get_if<broker::boolean>(v[4]); auto optional = caf::get_if<broker::boolean>(&v[4]);
if ( ! (name && type && subtype && optional) ) if ( ! (name && type && subtype && optional) )
return nullptr; return nullptr;
if ( secondary != broker::nil && ! broker::is<std::string>(secondary) ) if ( secondary != broker::nil && ! caf::holds_alternative<std::string>(secondary) )
return nullptr; return nullptr;
return new threading::Field(name->c_str(), return new threading::Field(name->c_str(),
secondary != broker::nil ? broker::get<std::string>(secondary).c_str() : nullptr, secondary != broker::nil ? caf::get<std::string>(secondary).c_str() : nullptr,
static_cast<TypeTag>(*type), static_cast<TypeTag>(*type),
static_cast<TypeTag>(*subtype), static_cast<TypeTag>(*subtype),
*optional); *optional);

View file

@ -210,11 +210,11 @@ broker::data& opaque_field_to_data(RecordVal* v, Frame* f);
template <typename T> template <typename T>
T& require_data_type(broker::data& d, TypeTag tag, Frame* f) T& require_data_type(broker::data& d, TypeTag tag, Frame* f)
{ {
auto ptr = broker::get_if<T>(d); auto ptr = caf::get_if<T>(&d);
if ( ! ptr ) if ( ! ptr )
reporter->RuntimeError(f->GetCall()->GetLocationInfo(), reporter->RuntimeError(f->GetCall()->GetLocationInfo(),
"data is of type '%s' not of type '%s'", "data is of type '%s' not of type '%s'",
broker::visit(type_name_getter{tag}, d), caf::visit(type_name_getter{tag}, d),
type_name(tag)); type_name(tag));
return *ptr; return *ptr;

View file

@ -942,13 +942,13 @@ void Manager::Process()
{ {
had_input = true; had_input = true;
if ( auto stat = broker::get_if<broker::status>(status_msg) ) if ( auto stat = caf::get_if<broker::status>(&status_msg) )
{ {
ProcessStatus(std::move(*stat)); ProcessStatus(std::move(*stat));
continue; continue;
} }
if ( auto err = broker::get_if<broker::error>(status_msg) ) if ( auto err = caf::get_if<broker::error>(&status_msg) )
{ {
ProcessError(std::move(*err)); ProcessError(std::move(*err));
continue; continue;
@ -1048,7 +1048,7 @@ void Manager::ProcessRelayEvent(broker::bro::RelayEvent ev)
++statistics.num_events_incoming; ++statistics.num_events_incoming;
for ( auto& t : ev.topics() ) for ( auto& t : ev.topics() )
PublishEvent(std::move(broker::get<std::string>(t)), PublishEvent(std::move(caf::get<std::string>(t)),
std::move(ev.name()), std::move(ev.name()),
std::move(ev.args())); std::move(ev.args()));
} }
@ -1060,7 +1060,7 @@ void Manager::ProcessHandleAndRelayEvent(broker::bro::HandleAndRelayEvent ev)
ProcessEvent(ev.name(), ev.args()); ProcessEvent(ev.name(), ev.args());
for ( auto& t : ev.topics() ) for ( auto& t : ev.topics() )
PublishEvent(std::move(broker::get<std::string>(t)), PublishEvent(std::move(caf::get<std::string>(t)),
std::move(ev.name()), std::move(ev.name()),
std::move(ev.args())); std::move(ev.args()));
} }
@ -1095,40 +1095,38 @@ bool bro_broker::Manager::ProcessLogCreate(broker::bro::LogCreate lc)
} }
// Get log fields. // Get log fields.
auto fields_data = caf::get_if<broker::vector>(&lc.fields_data());
try if ( ! fields_data )
{
auto fields_data = std::move(broker::get<broker::vector>(lc.fields_data()));
auto num_fields = fields_data.size();
auto fields = new threading::Field* [num_fields];
for ( auto i = 0u; i < num_fields; ++i )
{
if ( auto field = data_to_threading_field(std::move(fields_data[i])) )
fields[i] = field;
else
{
reporter->Warning("failed to convert remote log field # %d", i);
return false;
}
}
if ( ! log_mgr->CreateWriterForRemoteLog(stream_id->AsEnumVal(), writer_id->AsEnumVal(), writer_info.get(), num_fields, fields) )
{
ODesc d;
stream_id->Describe(&d);
reporter->Warning("failed to create remote log stream for %s locally", d.Description());
}
writer_info.release(); // log_mgr took ownership.
return true;
}
catch (const broker::bad_variant_access& e)
{ {
reporter->Warning("failed to unpack remote log fields"); reporter->Warning("failed to unpack remote log fields");
return false; return false;
} }
auto num_fields = fields_data->size();
auto fields = new threading::Field* [num_fields];
for ( auto i = 0u; i < num_fields; ++i )
{
if ( auto field = data_to_threading_field(std::move((*fields_data)[i])) )
fields[i] = field;
else
{
reporter->Warning("failed to convert remote log field # %d", i);
delete [] fields;
return false;
}
}
if ( ! log_mgr->CreateWriterForRemoteLog(stream_id->AsEnumVal(), writer_id->AsEnumVal(), writer_info.get(), num_fields, fields) )
{
ODesc d;
stream_id->Describe(&d);
reporter->Warning("failed to create remote log stream for %s locally", d.Description());
}
writer_info.release(); // log_mgr took ownership.
return true;
} }
bool bro_broker::Manager::ProcessLogWrite(broker::bro::LogWrite lw) bool bro_broker::Manager::ProcessLogWrite(broker::bro::LogWrite lw)
@ -1159,52 +1157,56 @@ bool bro_broker::Manager::ProcessLogWrite(broker::bro::LogWrite lw)
} }
unref_guard writer_id_unreffer{writer_id}; unref_guard writer_id_unreffer{writer_id};
auto path = caf::get_if<std::string>(&lw.path());
try if ( ! path )
{ {
auto& path = broker::get<std::string>(lw.path()); reporter->Warning("failed to unpack remote log values (bad path variant) for stream: %s", stream_id_name.data());
auto& serial_data = broker::get<std::string>(lw.serial_data());
BinarySerializationFormat fmt;
fmt.StartRead(serial_data.data(), serial_data.size());
int num_fields;
bool success = fmt.Read(&num_fields, "num_fields");
if ( ! success )
{
reporter->Warning("failed to unserialize remote log num fields for stream: %s", stream_id_name.data());
return false;
}
auto vals = new threading::Value* [num_fields];
for ( int i = 0; i < num_fields; ++i )
{
vals[i] = new threading::Value;
if ( ! vals[i]->Read(&fmt) )
{
for ( int j = 0; j <=i; ++j )
delete vals[j];
delete [] vals;
reporter->Warning("failed to unserialize remote log field %d for stream: %s", i, stream_id_name.data());
return false;
}
}
log_mgr->WriteFromRemote(stream_id->AsEnumVal(), writer_id->AsEnumVal(), std::move(path), num_fields, vals);
fmt.EndRead();
return true;
}
catch ( const broker::bad_variant_access& e)
{
reporter->Warning("failed to unpack remote log values (bad variant) for stream: %s", stream_id_name.data());
return false; return false;
} }
auto serial_data = caf::get_if<std::string>(&lw.serial_data());
if ( ! serial_data )
{
reporter->Warning("failed to unpack remote log values (bad serial_data variant) for stream: %s", stream_id_name.data());
return false;
}
BinarySerializationFormat fmt;
fmt.StartRead(serial_data->data(), serial_data->size());
int num_fields;
bool success = fmt.Read(&num_fields, "num_fields");
if ( ! success )
{
reporter->Warning("failed to unserialize remote log num fields for stream: %s", stream_id_name.data());
return false;
}
auto vals = new threading::Value* [num_fields];
for ( int i = 0; i < num_fields; ++i )
{
vals[i] = new threading::Value;
if ( ! vals[i]->Read(&fmt) )
{
for ( int j = 0; j <=i; ++j )
delete vals[j];
delete [] vals;
reporter->Warning("failed to unserialize remote log field %d for stream: %s", i, stream_id_name.data());
return false;
}
}
log_mgr->WriteFromRemote(stream_id->AsEnumVal(), writer_id->AsEnumVal(),
std::move(*path), num_fields, vals);
fmt.EndRead();
return true;
} }
bool Manager::ProcessIdentifierUpdate(broker::bro::IdentifierUpdate iu) bool Manager::ProcessIdentifierUpdate(broker::bro::IdentifierUpdate iu)

View file

@ -457,7 +457,7 @@ function Broker::__record_lookup%(r: Broker::Data, idx: count%): Broker::Data
auto& v = bro_broker::require_data_type<broker::vector>(r->AsRecordVal(), auto& v = bro_broker::require_data_type<broker::vector>(r->AsRecordVal(),
TYPE_RECORD, frame); TYPE_RECORD, frame);
if ( idx >= v.size() || broker::get_if<broker::none>(v[idx]) ) if ( idx >= v.size() || caf::get_if<broker::none>(&v[idx]) )
return new RecordVal(BifType::Record::Broker::Data); return new RecordVal(BifType::Record::Broker::Data);
return bro_broker::make_data_val(v[idx]); return bro_broker::make_data_val(v[idx]);
@ -496,7 +496,7 @@ function Broker::__record_iterator_value%(it: opaque of Broker::RecordIterator%)
return rval; return rval;
} }
if ( broker::get_if<broker::none>(*ri->it) ) if ( caf::get_if<broker::none>(&(*ri->it)) )
return rval; // field isn't set return rval; // field isn't set
rval->Assign(0, new bro_broker::DataVal(*ri->it)); rval->Assign(0, new bro_broker::DataVal(*ri->it));

View file

@ -137,15 +137,15 @@ broker::data WriterBackend::WriterInfo::ToBroker() const
bool WriterBackend::WriterInfo::FromBroker(broker::data d) bool WriterBackend::WriterInfo::FromBroker(broker::data d)
{ {
if ( ! broker::is<broker::vector>(d) ) if ( ! caf::holds_alternative<broker::vector>(d) )
return false; return false;
auto v = broker::get<broker::vector>(d); auto v = caf::get<broker::vector>(d);
auto bpath = broker::get_if<std::string>(v[0]); auto bpath = caf::get_if<std::string>(&v[0]);
auto brotation_base = broker::get_if<double>(v[1]); auto brotation_base = caf::get_if<double>(&v[1]);
auto brotation_interval = broker::get_if<double>(v[2]); auto brotation_interval = caf::get_if<double>(&v[2]);
auto bnetwork_time = broker::get_if<double>(v[3]); auto bnetwork_time = caf::get_if<double>(&v[3]);
auto bconfig = broker::get_if<broker::table>(v[4]); auto bconfig = caf::get_if<broker::table>(&v[4]);
if ( ! (bpath && brotation_base && brotation_interval && bnetwork_time && bconfig) ) if ( ! (bpath && brotation_base && brotation_interval && bnetwork_time && bconfig) )
return false; return false;
@ -157,8 +157,8 @@ bool WriterBackend::WriterInfo::FromBroker(broker::data d)
for ( auto i : *bconfig ) for ( auto i : *bconfig )
{ {
auto k = broker::get_if<std::string>(i.first); auto k = caf::get_if<std::string>(&i.first);
auto v = broker::get_if<std::string>(i.second); auto v = caf::get_if<std::string>(&i.second);
if ( ! (k && v) ) if ( ! (k && v) )
return false; return false;