Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add 'endswith' filter #1209

Merged
merged 3 commits into from
Sep 5, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 21 additions & 0 deletions userspace/libsinsp/filter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -192,6 +192,9 @@ bool flt_compare_uint64(cmpop op, uint64_t operand1, uint64_t operand2)
case CO_STARTSWITH:
throw sinsp_exception("'startswith' not supported for numeric filters");
return false;
case CO_ENDSWITH:
throw sinsp_exception("'endswith' not supported for numeric filters");
return false;
case CO_GLOB:
throw sinsp_exception("'glob' not supported for numeric filters");
return false;
Expand Down Expand Up @@ -226,6 +229,9 @@ bool flt_compare_int64(cmpop op, int64_t operand1, int64_t operand2)
case CO_STARTSWITH:
throw sinsp_exception("'startswith' not supported for numeric filters");
return false;
case CO_ENDSWITH:
throw sinsp_exception("'endswith' not supported for numeric filters");
return false;
case CO_GLOB:
throw sinsp_exception("'glob' not supported for numeric filters");
return false;
Expand Down Expand Up @@ -253,6 +259,8 @@ bool flt_compare_string(cmpop op, char* operand1, char* operand2)
#endif
case CO_STARTSWITH:
return (strncmp(operand1, operand2, strlen(operand2)) == 0);
case CO_ENDSWITH:
return (sinsp_utils::endswith(operand1, operand2));
case CO_GLOB:
return sinsp_utils::glob_match(operand2, operand1);
case CO_LT:
Expand Down Expand Up @@ -284,6 +292,8 @@ bool flt_compare_buffer(cmpop op, char* operand1, char* operand2, uint32_t op1_l
throw sinsp_exception("'icontains' not supported for buffer filters");
case CO_STARTSWITH:
return (memcmp(operand1, operand2, op2_len) == 0);
case CO_ENDSWITH:
return (sinsp_utils::endswith(operand1, operand2, op1_len, op2_len));
case CO_GLOB:
throw sinsp_exception("'glob' not supported for buffer filters");
case CO_LT:
Expand Down Expand Up @@ -326,6 +336,9 @@ bool flt_compare_double(cmpop op, double operand1, double operand2)
case CO_STARTSWITH:
throw sinsp_exception("'startswith' not supported for numeric filters");
return false;
case CO_ENDSWITH:
throw sinsp_exception("'endswith' not supported for numeric filters");
return false;
case CO_GLOB:
throw sinsp_exception("'glob' not supported for numeric filters");
return false;
Expand Down Expand Up @@ -355,6 +368,9 @@ bool flt_compare_ipv4net(cmpop op, uint64_t operand1, ipv4net* operand2)
case CO_STARTSWITH:
throw sinsp_exception("'startswith' not supported for numeric filters");
return false;
case CO_ENDSWITH:
throw sinsp_exception("'endswith' not supported for numeric filters");
return false;
case CO_GLOB:
throw sinsp_exception("'glob' not supported for numeric filters");
return false;
Expand Down Expand Up @@ -1697,6 +1713,11 @@ cmpop sinsp_filter_compiler::next_comparison_operator()
m_scanpos += 10;
return CO_STARTSWITH;
}
else if(compare_no_consume("endswith"))
{
m_scanpos += 8;
return CO_ENDSWITH;
}
else if(compare_no_consume("glob"))
{
m_scanpos += 4;
Expand Down
3 changes: 2 additions & 1 deletion userspace/libsinsp/filter.h
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,8 @@ enum cmpop {
CO_ICONTAINS = 10,
CO_STARTSWITH = 11,
CO_GLOB = 12,
CO_PMATCH = 13
CO_PMATCH = 13,
CO_ENDSWITH = 14
};

enum boolop
Expand Down
4 changes: 4 additions & 0 deletions userspace/libsinsp/lua_parser_api.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,10 @@ cmpop string_to_cmpop(const char* str)
{
return CO_STARTSWITH;
}
else if(strcmp(str, "endswith") == 0)
{
return CO_ENDSWITH;
}
else if(strcmp(str, "in") == 0)
{
return CO_IN;
Expand Down
21 changes: 21 additions & 0 deletions userspace/libsinsp/utils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1293,6 +1293,27 @@ string replace(const string& str, const string& search, const string& replacemen
return s;
}


bool sinsp_utils::endswith(const string& str, const string& ending)
{
if (ending.size() <= str.size())
{
return (0 == str.compare(str.length() - ending.length(), ending.length(), ending));
}
return false;
}


bool sinsp_utils::endswith(const char *str, const char *ending, uint32_t lstr, uint32_t lend)
{
if (lstr >= lend)
{
return (0 == memcmp(ending, str + (lstr - lend), lend));
}
return 0;
}


///////////////////////////////////////////////////////////////////////////////
// sinsp_numparser implementation
///////////////////////////////////////////////////////////////////////////////
Expand Down
6 changes: 6 additions & 0 deletions userspace/libsinsp/utils.h
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,12 @@ class sinsp_utils
//
static bool sockinfo_to_str(sinsp_sockinfo* sinfo, scap_fd_type stype, char* targetbuf, uint32_t targetbuf_size, bool resolve = false);

//
// Check if string ends with another
//
static bool endswith(const string& str, const string& ending);
static bool endswith(const char *str, const char *ending, uint32_t lstr, uint32_t lend);

//
// Concatenate two paths and puts the result in "target".
// If path2 is relative, the concatenation happens and the result is true.
Expand Down