#pragma once #include "broker/store.bif.h" #include "broker/data.bif.h" #include "OpaqueVal.h" #include "Trigger.h" #include #include #include namespace bro_broker { extern IntrusivePtr opaque_of_store_handle; /** * Create a Broker::QueryStatus value. * @param success whether the query status should be set to success or failure. * @return a Broker::QueryStatus value. */ IntrusivePtr query_status(bool success); /** * @return a Broker::QueryResult value that has a Broker::QueryStatus indicating * a failure. */ inline IntrusivePtr query_result() { auto rval = make_intrusive(zeek::BifType::Record::Broker::QueryResult); rval->Assign(0, query_status(false)); rval->Assign(1, make_intrusive(zeek::BifType::Record::Broker::Data)); return rval; } /** * @param data the result of the query. * @return a Broker::QueryResult value that has a Broker::QueryStatus indicating * a success. */ inline IntrusivePtr query_result(IntrusivePtr data) { auto rval = make_intrusive(zeek::BifType::Record::Broker::QueryResult); rval->Assign(0, query_status(true)); rval->Assign(1, std::move(data)); return rval; } /** * Used for asynchronous data store queries which use "when" statements. */ class StoreQueryCallback { public: StoreQueryCallback(zeek::detail::trigger::Trigger* arg_trigger, const zeek::detail::CallExpr* arg_call, broker::store store) : trigger(arg_trigger), call(arg_call), store(std::move(store)) { Ref(trigger); } ~StoreQueryCallback() { Unref(trigger); } void Result(const IntrusivePtr& result) { trigger->Cache(call, result.get()); trigger->Release(); } void Abort() { auto result = query_result(); trigger->Cache(call, result.get()); trigger->Release(); } bool Disabled() const { return trigger->Disabled(); } const broker::store& Store() const { return store; } private: zeek::detail::trigger::Trigger* trigger; const zeek::detail::CallExpr* call; broker::store store; }; /** * An opaque handle which wraps a Broker data store. */ class StoreHandleVal : public OpaqueVal { public: StoreHandleVal(broker::store s) : OpaqueVal(bro_broker::opaque_of_store_handle), store{s}, proxy{store} { } void ValDescribe(ODesc* d) const override; broker::store store; broker::store::proxy proxy; protected: StoreHandleVal() : OpaqueVal(bro_broker::opaque_of_store_handle) {} DECLARE_OPAQUE_VALUE(StoreHandleVal) }; // Helper function to construct a broker backend type from script land. broker::backend to_backend_type(BifEnum::Broker::BackendType type); // Helper function to construct broker backend options from script land. broker::backend_options to_backend_options(broker::backend backend, RecordVal* options); } // namespace bro_broker