Skip to content

Commit

Permalink
new(libsinsp): add iglob operator
Browse files Browse the repository at this point in the history
Signed-off-by: Roberto Scolaro <[email protected]>
  • Loading branch information
therealbobo authored and poiana committed Jan 18, 2024
1 parent a7c88dc commit 2c1ef6c
Show file tree
Hide file tree
Showing 10 changed files with 40 additions and 10 deletions.
4 changes: 2 additions & 2 deletions userspace/libsinsp/container_info.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,7 @@ const sinsp_container_info::container_mount_info *sinsp_container_info::mount_by
// note: linear search
for (auto &mntinfo :m_mounts)
{
if(sinsp_utils::glob_match(source.c_str(), mntinfo.m_source.c_str()))
if(sinsp_utils::glob_match(source.c_str(), mntinfo.m_source.c_str(), false))
{
return &mntinfo;
}
Expand All @@ -139,7 +139,7 @@ const sinsp_container_info::container_mount_info *sinsp_container_info::mount_by
// note: linear search
for (auto &mntinfo :m_mounts)
{
if(sinsp_utils::glob_match(dest.c_str(), mntinfo.m_dest.c_str()))
if(sinsp_utils::glob_match(dest.c_str(), mntinfo.m_dest.c_str(), false))
{
return &mntinfo;
}
Expand Down
4 changes: 4 additions & 0 deletions userspace/libsinsp/filter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -373,6 +373,10 @@ cmpop sinsp_filter_compiler::str_to_cmpop(const std::string& str)
{
return CO_GLOB;
}
else if(str == "iglob")
{
return CO_IGLOB;
}
// we are not supposed to get here, as the parser pre-checks this
ASSERT(false);
throw sinsp_exception("filter error: unrecognized comparison operator '" + std::string(str) + "'");
Expand Down
4 changes: 2 additions & 2 deletions userspace/libsinsp/filter/parser.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -59,8 +59,8 @@ static const vector<string> binary_num_ops =

static const vector<string> binary_str_ops =
{
"==", "=", "!=", "glob ", "contains ", "icontains ", "bcontains ",
"startswith ", "bstartswith ", "endswith ",
"==", "=", "!=", "glob ", "iglob ", "contains ", "icontains ",
"bcontains ", "startswith ", "bstartswith ", "endswith ",
};

static const vector<string> binary_list_ops =
Expand Down
1 change: 1 addition & 0 deletions userspace/libsinsp/gen_filter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,7 @@ std::string std::to_string(cmpop c)
case CO_ICONTAINS: return "ICONTAINS";
case CO_STARTSWITH: return "STARTSWITH";
case CO_GLOB: return "GLOB";
case CO_IGLOB: return "IGLOB";
case CO_PMATCH: return "PMATCH";
case CO_ENDSWITH: return "ENDSWITH";
case CO_INTERSECTS: return "INTERSECTS";
Expand Down
1 change: 1 addition & 0 deletions userspace/libsinsp/gen_filter.h
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ enum cmpop {
CO_INTERSECTS = 15,
CO_BCONTAINS = 16,
CO_BSTARTSWITH = 17,
CO_IGLOB = 18,
};

enum boolop
Expand Down
2 changes: 1 addition & 1 deletion userspace/libsinsp/prefix_search.h
Original file line number Diff line number Diff line change
Expand Up @@ -339,7 +339,7 @@ Value *path_prefix_map<Value>::match_components_glob(const path_prefix_map_ut::f
{
for(auto& it : m_glob_dirs)
{
if(sinsp_utils::glob_match(it.first.c_str(), comp->c_str()))
if(sinsp_utils::glob_match(it.first.c_str(), comp->c_str(), false))
{
Value *v = check_match_value(it.second, components, ++comp);
if(v != NULL)
Expand Down
24 changes: 23 additions & 1 deletion userspace/libsinsp/sinsp_filtercheck.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,9 @@ bool flt_compare_uint64(cmpop op, uint64_t operand1, uint64_t operand2)
case CO_GLOB:
throw sinsp_exception("'glob' not supported for numeric filters");
return false;
case CO_IGLOB:
throw sinsp_exception("'iglob' not supported for numeric filters");
return false;
default:
throw sinsp_exception("'unknown' not supported for numeric filters");
return false;
Expand Down Expand Up @@ -123,6 +126,9 @@ bool flt_compare_int64(cmpop op, int64_t operand1, int64_t operand2)
case CO_GLOB:
throw sinsp_exception("'glob' not supported for numeric filters");
return false;
case CO_IGLOB:
throw sinsp_exception("'iglob' not supported for numeric filters");
return false;
default:
throw sinsp_exception("'unknown' not supported for numeric filters");
return false;
Expand Down Expand Up @@ -160,7 +166,9 @@ bool flt_compare_string(cmpop op, char* operand1, char* operand2)
case CO_ENDSWITH:
return (sinsp_utils::endswith(operand1, operand2));
case CO_GLOB:
return sinsp_utils::glob_match(operand2, operand1);
return sinsp_utils::glob_match(operand2, operand1, false);
case CO_IGLOB:
return sinsp_utils::glob_match(operand2, operand1, true);
case CO_LT:
return (strcmp(operand1, operand2) < 0);
case CO_LE:
Expand Down Expand Up @@ -198,6 +206,8 @@ bool flt_compare_buffer(cmpop op, char* operand1, char* operand2, uint32_t op1_l
return (sinsp_utils::endswith(operand1, operand2, op1_len, op2_len));
case CO_GLOB:
throw sinsp_exception("'glob' not supported for buffer filters");
case CO_IGLOB:
throw sinsp_exception("'iglob' not supported for buffer filters");
case CO_LT:
throw sinsp_exception("'<' not supported for buffer filters");
case CO_LE:
Expand Down Expand Up @@ -250,6 +260,9 @@ bool flt_compare_double(cmpop op, double operand1, double operand2)
case CO_GLOB:
throw sinsp_exception("'glob' not supported for numeric filters");
return false;
case CO_IGLOB:
throw sinsp_exception("'iglob' not supported for numeric filters");
return false;
default:
throw sinsp_exception("'unknown' not supported for numeric filters");
return false;
Expand Down Expand Up @@ -288,6 +301,9 @@ bool flt_compare_ipv4net(cmpop op, uint64_t operand1, const ipv4net* operand2)
case CO_GLOB:
throw sinsp_exception("'glob' not supported for numeric filters");
return false;
case CO_IGLOB:
throw sinsp_exception("'iglob' not supported for numeric filters");
return false;
default:
throw sinsp_exception("comparison operator not supported for ipv4 networks");
}
Expand Down Expand Up @@ -320,6 +336,9 @@ bool flt_compare_ipv6addr(cmpop op, ipv6addr *operand1, ipv6addr *operand2)
case CO_GLOB:
throw sinsp_exception("'glob' not supported for ipv6 addresses");
return false;
case CO_IGLOB:
throw sinsp_exception("'iglob' not supported for ipv6 addresses");
return false;
default:
throw sinsp_exception("comparison operator not supported for ipv6 addresses");
}
Expand Down Expand Up @@ -352,6 +371,9 @@ bool flt_compare_ipv6net(cmpop op, const ipv6addr *operand1, const ipv6net *oper
case CO_GLOB:
throw sinsp_exception("'glob' not supported for ipv6 networks");
return false;
case CO_IGLOB:
throw sinsp_exception("'iglob' not supported for ipv6 networks");
return false;
default:
throw sinsp_exception("comparison operator not supported for ipv6 networks");
}
Expand Down
4 changes: 3 additions & 1 deletion userspace/libsinsp/test/filter_parser.ut.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ TEST(parser, supported_operators)
{
static vector<string> expected_all = {
"=", "==", "!=", "<=", ">=", "<", ">", "exists",
"contains", "icontains", "bcontains", "glob", "bstartswith",
"contains", "icontains", "bcontains", "glob", "iglob", "bstartswith",
"startswith", "endswith", "in", "intersects", "pmatch"};
static vector<string> expected_list_only = {
"in", "intersects", "pmatch"};
Expand Down Expand Up @@ -335,6 +335,7 @@ TEST(parser, parse_operators)
test_accept("test.op == value");
test_accept("test.op != value");
test_accept("test.op glob value");
test_accept("test.op iglob value");
test_accept("test.op contains value");
test_accept("test.op icontains value");
test_accept("test.op bcontains 48545450");
Expand Down Expand Up @@ -363,6 +364,7 @@ TEST(parser, parse_operators)
test_reject("test.op icontainsvalue");
test_reject("test.op bcontainsvalue");
test_reject("test.op globvalue");
test_reject("test.op iglobvalue");
}

TEST(parser, parse_position_info)
Expand Down
4 changes: 2 additions & 2 deletions userspace/libsinsp/utils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -703,12 +703,12 @@ uint64_t sinsp_utils::get_current_time_ns()
return tv.tv_sec * (uint64_t) 1000000000 + tv.tv_usec * 1000;
}

bool sinsp_utils::glob_match(const char *pattern, const char *string)
bool sinsp_utils::glob_match(const char *pattern, const char *string, const bool& case_insensitive)
{
#ifdef _WIN32
return PathMatchSpec(string, pattern) == TRUE;
#else
int flags = 0;
int flags = case_insensitive ? FNM_CASEFOLD : 0;
return fnmatch(pattern, string, flags) == 0;
#endif
}
Expand Down
2 changes: 1 addition & 1 deletion userspace/libsinsp/utils.h
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ class sinsp_utils

static uint64_t get_current_time_ns();

static bool glob_match(const char *pattern, const char *string);
static bool glob_match(const char *pattern, const char *string, const bool& case_insensitive);

#ifndef _WIN32
//
Expand Down

0 comments on commit 2c1ef6c

Please sign in to comment.