dfilter: Add "bitand" as an alternative operator keyword

It's more compact than "bitwise_and" and inspired by C.
This commit is contained in:
João Valverde 2023-10-23 00:34:16 +01:00
parent 1ee70cf25f
commit c86a85022d
6 changed files with 26 additions and 10 deletions

View File

@ -49,6 +49,8 @@ The following features are new (or have been significantly updated) since versio
the multiplier/divisor must be an integer or float and appear on the right-hand
side of the operator.
** The keyword "bitand" can be used as an alternative syntax for the bitwise-and operator.
//=== Removed Features and Support
// === Removed Dissectors

View File

@ -488,7 +488,7 @@ can be convenient:
It is also possible to define tests with bitwise operations. Currently the
following bitwise operator is supported:
bitwise_and, & Bitwise AND
bitand, bitwise_and, & Bitwise AND
The bitwise AND operation allows masking bits and testing to see if one or
more bits are set. Bitwise AND operates on integer protocol fields and slices.

View File

@ -830,16 +830,16 @@ You can perform the arithmetic operations on numeric fields shown in <<Arithmeti
[#ArithmeticOps]
.Display Filter Arithmetic Operations
[options="header",cols="1,1,4"]
[options="header",cols="1,1,1,4"]
|===
|Name |Syntax | Description
|Unary minus |-A | Negation of A
|Addition |A + B | Add B to A
|Subtraction |A - B | Subtract B from A
|Multiplication |A * B | Multiply A times B
|Division |A / B | Divide A by B
|Modulo |A % B | Remainder of A divided by B
|Bitwise AND |A & B | Bitwise AND of A and B
|Name |Syntax | Alternative | Description
|Unary minus |-A | | Negation of A
|Addition |A + B | | Add B to A
|Subtraction |A - B | | Subtract B from A
|Multiplication |A * B | | Multiply A times B
|Division |A / B | | Divide A by B
|Modulo |A % B | | Remainder of A divided by B
|Bitwise AND |A & B | A bitand B | Bitwise AND of A and B
|===
Arithmetic expressions can be grouped using curly braces.

View File

@ -206,6 +206,7 @@ HexExponent ([pP][+-]?[[:digit:]]+)
"/" return math(TOKEN_RSLASH);
"%" return math(TOKEN_PERCENT);
"&" return math(TOKEN_BITWISE_AND);
"bitand" return math(TOKEN_BITWISE_AND);
"bitwise_and" return math(TOKEN_BITWISE_AND);
"#" {

View File

@ -484,6 +484,7 @@ static const char *reserved_filter_names[] = {
"ge",
"lt",
"le",
"bitand",
"bitwise_and",
"contains",
"matches",

View File

@ -218,6 +218,10 @@ class TestDfilterBitwise:
checkDFilterCount(dfilter, 1)
def test_exists_2(self, checkDFilterCount):
dfilter = "tcp.flags bitand 0x8"
checkDFilterCount(dfilter, 1)
def test_exists_3(self, checkDFilterCount):
dfilter = "eth[0] & 1"
checkDFilterCount(dfilter, 0)
@ -229,6 +233,14 @@ class TestDfilterBitwise:
dfilter = "tcp.srcport != tcp.dstport & 0x0F"
checkDFilterCount(dfilter, 1)
def test_equal_3(self, checkDFilterCount):
dfilter = "tcp.srcport != tcp.dstport bitand 0x0F"
checkDFilterCount(dfilter, 1)
def test_equal_4(self, checkDFilterCount):
dfilter = "tcp.srcport != tcp.dstport bitwise_and 0x0F"
checkDFilterCount(dfilter, 1)
class TestDfilterUnaryMinus:
trace_file = "http.pcap"