mirror of
https://github.com/zeek/zeek.git
synced 2025-10-02 06:38:20 +00:00
Remove GET_FIELD_AS macro, replace with template methods
This commit is contained in:
parent
a94fcad957
commit
4962986df1
15 changed files with 61 additions and 53 deletions
|
@ -434,7 +434,7 @@ void Connection::AppendAddl(const char* str)
|
|||
{
|
||||
const auto& cv = ConnVal();
|
||||
|
||||
const char* old = cv->GetStringField(6)->CheckString();
|
||||
const char* old = cv->GetFieldAs<StringVal>(6)->CheckString();
|
||||
const char* format = *old ? "%s %s" : "%s%s";
|
||||
|
||||
cv->Assign(6, make_intrusive<StringVal>(util::fmt(format, old, str)));
|
||||
|
|
|
@ -369,11 +369,11 @@ bool Reporter::PermitFlowWeird(const char* name,
|
|||
|
||||
bool Reporter::PermitExpiredConnWeird(const char* name, const RecordVal& conn_id)
|
||||
{
|
||||
auto conn_tuple = std::make_tuple(conn_id.GetAddrField("orig_h"),
|
||||
conn_id.GetAddrField("resp_h"),
|
||||
conn_id.GetPortValField("orig_p")->Port(),
|
||||
conn_id.GetPortValField("resp_p")->Port(),
|
||||
conn_id.GetPortValField("resp_p")->PortType());
|
||||
auto conn_tuple = std::make_tuple(conn_id.GetFieldAs<AddrVal>("orig_h"),
|
||||
conn_id.GetFieldAs<AddrVal>("resp_h"),
|
||||
conn_id.GetFieldAs<PortVal>("orig_p")->Port(),
|
||||
conn_id.GetFieldAs<PortVal>("resp_p")->Port(),
|
||||
conn_id.GetFieldAs<PortVal>("resp_p")->PortType());
|
||||
|
||||
auto& map = expired_conn_weird_state[conn_tuple];
|
||||
|
||||
|
|
|
@ -346,11 +346,11 @@ Connection* NetSessions::FindConnection(Val* v)
|
|||
// types, too.
|
||||
}
|
||||
|
||||
const IPAddr& orig_addr = vl->GetAddrField(orig_h);
|
||||
const IPAddr& resp_addr = vl->GetAddrField(resp_h);
|
||||
const IPAddr& orig_addr = vl->GetFieldAs<AddrVal>(orig_h);
|
||||
const IPAddr& resp_addr = vl->GetFieldAs<AddrVal>(resp_h);
|
||||
|
||||
const PortVal* orig_portv = vl->GetPortValField(orig_p);
|
||||
const PortVal* resp_portv = vl->GetPortValField(resp_p);
|
||||
const PortVal* orig_portv = vl->GetFieldAs<PortVal>(orig_p);
|
||||
const PortVal* resp_portv = vl->GetFieldAs<PortVal>(resp_p);
|
||||
|
||||
ConnID id;
|
||||
|
||||
|
|
|
@ -109,19 +109,19 @@ Substring::Vec* Substring::VecFromPolicy(VectorVal* vec)
|
|||
if ( ! v )
|
||||
continue;
|
||||
|
||||
const String* str = v->AsRecordVal()->GetStringField(0);
|
||||
const String* str = v->AsRecordVal()->GetFieldAs<StringVal>(0);
|
||||
auto* substr = new Substring(*str);
|
||||
|
||||
const VectorVal* aligns = v->AsRecordVal()->GetField(1)->AsVectorVal();
|
||||
for ( unsigned int j = 1; j <= aligns->Size(); ++j )
|
||||
{
|
||||
const RecordVal* align = aligns->AsVectorVal()->At(j)->AsRecordVal();
|
||||
const String* str = align->GetStringField(0);
|
||||
int index = align->GetCountField(1);
|
||||
const String* str = align->GetFieldAs<StringVal>(0);
|
||||
int index = align->GetFieldAs<CountVal>(1);
|
||||
substr->AddAlignment(str, index);
|
||||
}
|
||||
|
||||
bool new_alignment = v->AsRecordVal()->GetBoolField(2);
|
||||
bool new_alignment = v->AsRecordVal()->GetFieldAs<BoolVal>(2);
|
||||
substr->MarkNewAlignment(new_alignment);
|
||||
|
||||
result->push_back(substr);
|
||||
|
|
37
src/Val.h
37
src/Val.h
|
@ -497,7 +497,7 @@ public:
|
|||
// Returns a masked port number
|
||||
static uint32_t Mask(uint32_t port_num, TransportProto port_type);
|
||||
|
||||
bro_uint_t Get() const { return uint_val; }
|
||||
const PortVal* Get() const { return AsPortVal(); }
|
||||
|
||||
protected:
|
||||
friend class ValManager;
|
||||
|
@ -1259,22 +1259,27 @@ public:
|
|||
// underlying value. We provide these to enable efficient
|
||||
// access to record fields (without requiring an intermediary Val)
|
||||
// if we change the underlying representation of records.
|
||||
#define GET_FIELD_AS(ctype, name ) \
|
||||
ctype Get ## name ## Field(int field) const \
|
||||
{ return GetField(field)->As ## name(); } \
|
||||
ctype Get ## name ## Field(const char* field) const \
|
||||
{ return GetField(field)->As ## name(); }
|
||||
template <typename T>
|
||||
auto GetFieldAs(int field) const -> std::invoke_result_t<decltype(&T::Get), T>
|
||||
{
|
||||
auto& field_ptr = GetField(field);
|
||||
auto field_val_ptr = dynamic_cast<T*>(field_ptr.get());
|
||||
if ( ! field_val_ptr )
|
||||
reporter->InternalError("Typecast failed in TableVal::GetFieldAs");
|
||||
|
||||
GET_FIELD_AS(bool, Bool)
|
||||
GET_FIELD_AS(int, Enum)
|
||||
GET_FIELD_AS(bro_int_t, Int)
|
||||
GET_FIELD_AS(bro_uint_t, Count)
|
||||
GET_FIELD_AS(double, Double)
|
||||
GET_FIELD_AS(double, Time)
|
||||
GET_FIELD_AS(double, Interval)
|
||||
GET_FIELD_AS(const PortVal*, PortVal)
|
||||
GET_FIELD_AS(const IPAddr&, Addr)
|
||||
GET_FIELD_AS(const String*, String)
|
||||
return field_val_ptr->Get();
|
||||
}
|
||||
|
||||
template <typename T>
|
||||
auto GetFieldAs(const char* field) const -> std::invoke_result_t<decltype(&T::Get), T>
|
||||
{
|
||||
auto& field_ptr = GetField(field);
|
||||
auto field_val_ptr = dynamic_cast<T*>(field_ptr.get());
|
||||
if ( ! field_val_ptr )
|
||||
reporter->InternalError("Typecast failed in TableVal::GetFieldAs");
|
||||
|
||||
return field_val_ptr->Get();
|
||||
}
|
||||
|
||||
/**
|
||||
* Looks up the value of a field by field name. If the field doesn't
|
||||
|
|
|
@ -263,7 +263,10 @@ refine connection KRB_Conn += {
|
|||
rv->Assign(1, zeek::val_mgr->Bool(${msg.ap_options.mutual_required}));
|
||||
|
||||
auto rvticket = proc_ticket(${msg.ticket});
|
||||
auto authenticationinfo = zeek_analyzer()->GetAuthenticationInfo(rvticket->GetStringField(2), rvticket->GetStringField(4), rvticket->GetCountField(3));
|
||||
auto authenticationinfo = zeek_analyzer()->GetAuthenticationInfo(
|
||||
rvticket->GetFieldAs<zeek::StringVal>(2),
|
||||
rvticket->GetFieldAs<zeek::StringVal>(4),
|
||||
rvticket->GetFieldAs<zeek::CountVal>(3));
|
||||
|
||||
if ( authenticationinfo )
|
||||
rvticket->Assign(5, authenticationinfo);
|
||||
|
|
|
@ -176,7 +176,7 @@ bool NFS_Interp::RPC_BuildReply(RPC_CallInfo* c, BifEnum::rpc_status rpc_status,
|
|||
|
||||
case BifEnum::NFS3::PROC_READ:
|
||||
bro_uint_t offset;
|
||||
offset = c->RequestVal()->AsRecordVal()->GetCountField(1);
|
||||
offset = c->RequestVal()->AsRecordVal()->GetFieldAs<CountVal>(1);
|
||||
reply = nfs3_read_reply(buf, n, nfs_status, offset);
|
||||
event = nfs_proc_read;
|
||||
break;
|
||||
|
|
|
@ -436,7 +436,7 @@ bool Manager::PublishEvent(string topic, RecordVal* args)
|
|||
if ( ! args->GetField(0) )
|
||||
return false;
|
||||
|
||||
auto event_name = args->GetStringField(0)->CheckString();
|
||||
auto event_name = args->GetFieldAs<StringVal>(0)->CheckString();
|
||||
auto vv = args->GetField(1)->AsVectorVal();
|
||||
broker::vector xs;
|
||||
xs.reserve(vv->Size());
|
||||
|
|
|
@ -69,7 +69,7 @@ broker::backend_options to_backend_options(broker::backend backend,
|
|||
case broker::backend::sqlite:
|
||||
{
|
||||
auto path = options->GetField(0)->AsRecordVal()
|
||||
->GetStringField(0)->CheckString();
|
||||
->GetFieldAs<StringVal>(0)->CheckString();
|
||||
return {{"path", path}};
|
||||
}
|
||||
|
||||
|
|
|
@ -120,7 +120,7 @@ void File::UpdateLastActivityTime()
|
|||
|
||||
double File::GetLastActivityTime() const
|
||||
{
|
||||
return val->GetTimeField(last_active_idx);
|
||||
return val->GetFieldAs<TimeVal>(last_active_idx);
|
||||
}
|
||||
|
||||
bool File::UpdateConnectionFields(Connection* conn, bool is_orig)
|
||||
|
|
|
@ -1543,7 +1543,7 @@ std::string Manager::FormatRotationPath(EnumValPtr writer,
|
|||
auto res = rotation_format_func->Invoke(ri);
|
||||
auto rp_val = res->AsRecordVal();
|
||||
auto dir_val = rp_val->GetFieldOrDefault(0);
|
||||
auto prefix = rp_val->GetStringField(1)->CheckString();
|
||||
auto prefix = rp_val->GetFieldAs<StringVal>(1)->CheckString();
|
||||
auto dir = dir_val->AsString()->CheckString();
|
||||
|
||||
if ( ! util::streq(dir, "") && ! util::detail::ensure_intermediate_dirs(dir) )
|
||||
|
|
|
@ -148,7 +148,7 @@ function Reporter::conn_weird%(name: string, c: connection, addl: string &defaul
|
|||
## Returns: true if the file was still valid, else false.
|
||||
function Reporter::file_weird%(name: string, f: fa_file, addl: string &default="", source: string&default=""%): bool
|
||||
%{
|
||||
auto fuid = f->AsRecordVal()->GetStringField(0);
|
||||
auto fuid = f->AsRecordVal()->GetFieldAs<zeek::StringVal>(0);
|
||||
auto file = zeek::file_mgr->LookupFile(fuid->CheckString());
|
||||
|
||||
if ( ! file )
|
||||
|
|
|
@ -684,8 +684,8 @@ function string_to_ascii_hex%(s: string%): string
|
|||
function str_smith_waterman%(s1: string, s2: string, params: sw_params%) : sw_substring_vec
|
||||
%{
|
||||
zeek::detail::SWParams sw_params(
|
||||
params->AsRecordVal()->GetCountField(0),
|
||||
zeek::detail::SWVariant(params->AsRecordVal()->GetCountField(1)));
|
||||
params->AsRecordVal()->GetFieldAs<zeek::CountVal>(0),
|
||||
zeek::detail::SWVariant(params->AsRecordVal()->GetFieldAs<zeek::CountVal>(1)));
|
||||
|
||||
auto* subseq = zeek::detail::smith_waterman(s1->AsString(), s2->AsString(), sw_params);
|
||||
auto result = zeek::VectorValPtr{zeek::AdoptRef{}, zeek::detail::Substring::VecToPolicy(subseq)};
|
||||
|
|
|
@ -1225,7 +1225,7 @@ static BifEnum::Supervisor::ClusterRole role_str_to_enum(std::string_view r)
|
|||
Supervisor::NodeConfig Supervisor::NodeConfig::FromRecord(const RecordVal* node)
|
||||
{
|
||||
Supervisor::NodeConfig rval;
|
||||
rval.name = node->GetStringField("name")->CheckString();
|
||||
rval.name = node->GetFieldAs<StringVal>("name")->CheckString();
|
||||
const auto& iface_val = node->GetField("interface");
|
||||
|
||||
if ( iface_val )
|
||||
|
@ -1273,9 +1273,9 @@ Supervisor::NodeConfig Supervisor::NodeConfig::FromRecord(const RecordVal* node)
|
|||
auto rv = v->GetVal()->AsRecordVal();
|
||||
|
||||
Supervisor::ClusterEndpoint ep;
|
||||
ep.role = static_cast<BifEnum::Supervisor::ClusterRole>(rv->GetEnumField("role"));
|
||||
ep.host = rv->GetAddrField("host").AsString();
|
||||
ep.port = rv->GetPortValField("p")->Port();
|
||||
ep.role = static_cast<BifEnum::Supervisor::ClusterRole>(rv->GetFieldAs<EnumVal>("role"));
|
||||
ep.host = rv->GetFieldAs<AddrVal>("host").AsString();
|
||||
ep.port = rv->GetFieldAs<PortVal>("p")->Port();
|
||||
|
||||
const auto& iface = rv->GetField("interface");
|
||||
|
||||
|
|
20
src/zeek.bif
20
src/zeek.bif
|
@ -3531,10 +3531,10 @@ const char* conn_id_string(zeek::Val* c)
|
|||
auto id = c->As<zeek::RecordVal*>()->GetField(0);
|
||||
auto id_r = id->As<zeek::RecordVal*>();
|
||||
|
||||
const zeek::IPAddr& orig_h = id_r->GetAddrField(0);
|
||||
uint32_t orig_p = id_r->GetPortValField(1)->Port();
|
||||
const zeek::IPAddr& resp_h = id_r->GetAddrField(2);
|
||||
uint32_t resp_p = id_r->GetPortValField(3)->Port();
|
||||
const zeek::IPAddr& orig_h = id_r->GetFieldAs<zeek::AddrVal>(0);
|
||||
uint32_t orig_p = id_r->GetFieldAs<zeek::PortVal>(1)->Port();
|
||||
const zeek::IPAddr& resp_h = id_r->GetFieldAs<zeek::AddrVal>(2);
|
||||
uint32_t resp_p = id_r->GetFieldAs<zeek::PortVal>(3)->Port();
|
||||
|
||||
return zeek::util::fmt("%s/%u -> %s/%u\n", orig_h.AsString().c_str(), orig_p,
|
||||
resp_h.AsString().c_str(), resp_p);
|
||||
|
@ -3656,12 +3656,12 @@ function dump_packet%(pkt: pcap_packet, file_name: string%) : bool
|
|||
|
||||
auto pkt_r = pkt->As<zeek::RecordVal*>();
|
||||
|
||||
ts.tv_sec = pkt_r->GetCountField(0);
|
||||
ts.tv_usec = pkt_r->GetCountField(1);
|
||||
caplen = pkt_r->GetCountField(2);
|
||||
len = pkt_r->GetCountField(3);
|
||||
data = pkt_r->GetStringField(4)->Bytes();
|
||||
link_type = pkt_r->GetEnumField(5);
|
||||
ts.tv_sec = pkt_r->GetFieldAs<zeek::CountVal>(0);
|
||||
ts.tv_usec = pkt_r->GetFieldAs<zeek::CountVal>(1);
|
||||
caplen = pkt_r->GetFieldAs<zeek::CountVal>(2);
|
||||
len = pkt_r->GetFieldAs<zeek::CountVal>(3);
|
||||
data = pkt_r->GetFieldAs<zeek::StringVal>(4)->Bytes();
|
||||
link_type = pkt_r->GetFieldAs<zeek::EnumVal>(5);
|
||||
Packet p(link_type, &ts, caplen, len, data, true);
|
||||
|
||||
addl_pkt_dumper->Dump(&p);
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue