mirror of
https://github.com/zeek/zeek.git
synced 2025-10-07 09:08:20 +00:00
Add table_keys function
This commit is contained in:
parent
cb71b15eab
commit
ba552ceeaf
5 changed files with 73 additions and 21 deletions
|
@ -932,6 +932,8 @@ public:
|
||||||
|
|
||||||
const PDict<TableEntryVal>* Get() const { return table_val; }
|
const PDict<TableEntryVal>* Get() const { return table_val; }
|
||||||
|
|
||||||
|
const detail::CompositeHash* GetTableHash() const { return table_hash; }
|
||||||
|
|
||||||
// Returns the size of the table.
|
// Returns the size of the table.
|
||||||
int Size() const;
|
int Size() const;
|
||||||
int RecursiveSize() const;
|
int RecursiveSize() const;
|
||||||
|
|
58
src/zeek.bif
58
src/zeek.bif
|
@ -28,6 +28,7 @@
|
||||||
#include "zeek/IntrusivePtr.h"
|
#include "zeek/IntrusivePtr.h"
|
||||||
#include "zeek/input.h"
|
#include "zeek/input.h"
|
||||||
#include "zeek/Hash.h"
|
#include "zeek/Hash.h"
|
||||||
|
#include "zeek/CompHash.h"
|
||||||
#include "zeek/packet_analysis/Manager.h"
|
#include "zeek/packet_analysis/Manager.h"
|
||||||
|
|
||||||
using namespace std;
|
using namespace std;
|
||||||
|
@ -1190,23 +1191,52 @@ function clear_table%(v: any%): any
|
||||||
## t: The :zeek:type:`table`
|
## t: The :zeek:type:`table`
|
||||||
##
|
##
|
||||||
## Returns: A ``vector of T`` of all the values in t.
|
## Returns: A ``vector of T`` of all the values in t.
|
||||||
|
##
|
||||||
|
## .. zeek:see:: table_keys
|
||||||
function table_values%(t: any%): any
|
function table_values%(t: any%): any
|
||||||
%{
|
%{
|
||||||
if ( ! t->GetType()->IsTable() )
|
if ( ! t->GetType()->IsTable() )
|
||||||
{
|
{
|
||||||
zeek::emit_builtin_error("table_values() requires a table argument");
|
zeek::emit_builtin_error("table_values() requires a table argument");
|
||||||
return nullptr;
|
return nullptr;
|
||||||
}
|
}
|
||||||
|
|
||||||
auto vt = zeek::make_intrusive<zeek::VectorType>(t->GetType()->AsTableType()->Yield());
|
auto vt = zeek::make_intrusive<zeek::VectorType>(t->GetType()->AsTableType()->Yield());
|
||||||
auto vv = zeek::make_intrusive<zeek::VectorVal>(std::move(vt));
|
auto vv = zeek::make_intrusive<zeek::VectorVal>(std::move(vt));
|
||||||
for ( const auto& iter : *t->AsTable() )
|
for ( const auto& iter : *t->AsTable() )
|
||||||
{
|
{
|
||||||
vv->Append(iter.value->GetVal());
|
vv->Append(iter.value->GetVal());
|
||||||
}
|
}
|
||||||
|
|
||||||
return vv;
|
return vv;
|
||||||
%}
|
%}
|
||||||
|
|
||||||
|
## Gets all keys from a table.
|
||||||
|
##
|
||||||
|
## t: The :zeek:type:`table`
|
||||||
|
##
|
||||||
|
## Returns: A ``set of T`` of all the keys in t.
|
||||||
|
##
|
||||||
|
## .. zeek:see:: table_values
|
||||||
|
function table_keys%(t: any%): any
|
||||||
|
%{
|
||||||
|
if ( ! t->GetType()->IsTable() )
|
||||||
|
{
|
||||||
|
zeek::emit_builtin_error("table_keys() requires a table argument");
|
||||||
|
return nullptr;
|
||||||
|
}
|
||||||
|
|
||||||
|
auto tl = t->GetType()->AsTableType()->GetIndices();
|
||||||
|
auto st = make_intrusive<zeek::SetType>(std::move(tl), nullptr);
|
||||||
|
auto sv = zeek::make_intrusive<zeek::TableVal>(std::move(st));
|
||||||
|
auto th = t->AsTableVal()->GetTableHash();
|
||||||
|
for ( const auto& iter : *t->AsTable() )
|
||||||
|
{
|
||||||
|
sv->Assign(th->RecoverVals(*iter.GetHashKey()), nullptr);
|
||||||
|
}
|
||||||
|
|
||||||
|
return sv;
|
||||||
|
%}
|
||||||
|
|
||||||
## Gets all subnets that contain a given subnet from a set/table[subnet].
|
## Gets all subnets that contain a given subnet from a set/table[subnet].
|
||||||
##
|
##
|
||||||
|
|
5
testing/btest/Baseline/bifs.table_keys/out
Normal file
5
testing/btest/Baseline/bifs.table_keys/out
Normal file
|
@ -0,0 +1,5 @@
|
||||||
|
### BTest baseline data generated by btest-diff. Do not edit. Use "btest -U/-u" to update. Requires BTest >= 0.63.
|
||||||
|
{
|
||||||
|
http,
|
||||||
|
https
|
||||||
|
}
|
15
testing/btest/bifs/table_keys.zeek
Normal file
15
testing/btest/bifs/table_keys.zeek
Normal file
|
@ -0,0 +1,15 @@
|
||||||
|
#
|
||||||
|
# @TEST-EXEC: zeek -b %INPUT > out
|
||||||
|
# @TEST-EXEC: btest-diff out
|
||||||
|
|
||||||
|
event zeek_init()
|
||||||
|
{
|
||||||
|
local t = table(
|
||||||
|
["http"] = "http://www.google.com/",
|
||||||
|
["https"] = "https://www.google.com/"
|
||||||
|
);
|
||||||
|
|
||||||
|
local v: set[string] = table_keys(t);
|
||||||
|
|
||||||
|
print v;
|
||||||
|
}
|
|
@ -5,14 +5,14 @@
|
||||||
event zeek_init()
|
event zeek_init()
|
||||||
{
|
{
|
||||||
local t = table(
|
local t = table(
|
||||||
["web"] = { [80/tcp, "http"], [443/tcp, "https"] },
|
["web"] = { [80/tcp, "http"], [443/tcp, "https"] },
|
||||||
["login"] = { [21/tcp, "ftp"], [23/tcp, "telnet"] }
|
["login"] = { [21/tcp, "ftp"], [23/tcp, "telnet"] }
|
||||||
);
|
);
|
||||||
|
|
||||||
local v: vector of set[port, string] = table_values(t);
|
local v: vector of set[port, string] = table_values(t);
|
||||||
|
|
||||||
for ( i in v )
|
for ( i in v )
|
||||||
{
|
{
|
||||||
print v[i];
|
print v[i];
|
||||||
}
|
}
|
||||||
}
|
}
|
Loading…
Add table
Add a link
Reference in a new issue