Commit Graph

120 Commits

Author SHA1 Message Date
Guy Harris 656f00ef29 Change what iso8601_to_nstime() and unix_epoch_to_nstime() return.
Instead of rturning an 8-bit(!) integer giving the number of characters
parsed, have them return a pointer to the first character *after* the
string that was parsed, similarly to what strto*() (and Wireshark's
wrappers for them in wsutil) and strptime() return.

This cleans up some code that uses those routines.

For the 3GPP 32.423 trace files, we also reject files where there isn't
an ISO 8601-format time where we expect there to be one.  (Having the
string-format date/time routines return NULL on an error means you have
to think about what to do on failure.)
2023-09-10 02:05:45 +00:00
João Valverde 6aa22b952d dfilter: Extend support for timezones in filter expressions
Extends the dfilter syntax with more the timezones supported
by ws_strptime().

We remove the handling of tm_gmtoff and tm_zone in struct tm,
because it is more trouble than it is worth due to platform
incompatibilites, and instead use an extra arguments to pass
these two non-standard fields.

Remove a test that is no longer relevant (we do not need to
assert that WET in particular is not supported).
2023-09-08 14:10:11 +01:00
João Valverde f58b451bae dfilter: Fix printing of nanosecond fractions of time.
The code is incorrectly also omitting zeros that are significant
from the representation of fractional values of absolute time
in display filters (with FTREPR_DFILTER).

Instead of fixing the representation just remove the buggy code and
include all 9 digits of fractional seconds, including trailing zeros
to the right of the decimal point, which in this case can be inferred
to indicate the precision (to the nanosecond).
2023-09-06 17:56:12 +00:00
João Valverde 2aaab8f88f Revert "dfilter: Clean up absolute_val_to_repr()"
This reverts commit 8c113b9db9.

The behaviour of the new code is not exactly the same as the
old and that is apparently causing some failures in the test
suite.
2023-09-05 23:43:01 +01:00
João Valverde 8c113b9db9 dfilter: Clean up absolute_val_to_repr() 2023-09-05 01:41:04 +00:00
João Valverde 33adf427aa dfilter: Cleanup parsing of some time literals 2023-09-04 18:14:47 +00:00
João Valverde 51d16ae6b3 dfilter: Fix parsing a time format with "UTC"
Before:

    Filter:
     frame.time <= "2002-12-31 13:56:31.3 UTC"

    Error: Unexpected data after time value.
      frame.time <= "2002-12-31 13:56:31.3 UTC"

After:

    Filter:
     frame.time <= "2002-12-31 13:56:31.3 UTC"

    Instructions:
     0000 READ_TREE        frame.time       -> R0
     0001 IF_FALSE_GOTO    3
     0002 ANY_LE           R0 <= "2002-12-31 13:56:31.3+0000"
     0003 RETURN
2023-09-04 16:04:48 +01:00
João Valverde fe802272a2 Build with NetBSD's strptime()
Replace our strptime code, which is from gnulib,
with the simpler and better NetBSD implementation.

This changes the ws_strptime() stub to unconditionally use
the internal implementation. Previously it would use the
system implementation of available. This is still possible
but is opt-in, i.e., code should add the necessary #ifdefs
and assume responsability for handling non-portable formats
or providing limited functionality on some platforms.

Text import allows the user to specify the strptime()
format freely, so in that case it makes sense to use the
system's implementation, and pass the responsability
for understanding the implementation and the supported
specifiers to the user.

Only fall back to our implementation if the system libc
lacks a strptime().
2023-09-03 18:05:59 +01:00
João Valverde 6730cc3a65 Add Unix time support for absolute time field type
Add ABSOLUTE_TIME_UNIX absolute time type, to allow
date and time values to be represented in Unix time,
besides other existing formats.
2023-07-03 21:45:50 +00:00
João Valverde 09d5f4bdb4 dfilter: Add some arithmetic to FT_ABSOLUTE_TIME
FT_ABSOLUTE_TIME Hhas the same arithmetic properties as
FT_RELATIVE_TIME. Give it the same methods.
2023-07-03 21:45:50 +00:00
João Valverde a3eab66d52 dfilter: Use safe-math.h with time ftypes 2023-06-27 18:31:28 +00:00
João Valverde 1ff7b83037 ftypes: Add a hash/equal method
Add methods to make fvalues hashable with GHashTable.
2023-04-19 16:16:28 +01:00
João Valverde 4c975b770e dfilter: Improve compatibility of integer types
Before:

$ dftest '_ws.ftypes.int64 == _ws.ftypes.int8'
Filter: _ws.ftypes.int64 == _ws.ftypes.int8
dftest: _ws.ftypes.int64 and _ws.ftypes.int8 are not of compatible types.
	_ws.ftypes.int64 == _ws.ftypes.int8
	                    ^~~~~~~~~~~~~~~

After:

$ dftest '_ws.ftypes.int64 == _ws.ftypes.int8'
Filter: _ws.ftypes.int64 == _ws.ftypes.int8

Syntax tree:
 0 TEST_ANY_EQ:
   1 FIELD(_ws.ftypes.int64 <FT_INT64>)
   1 FIELD(_ws.ftypes.int8 <FT_INT8>)

Instructions:
00000 READ_TREE		_ws.ftypes.int64 <FT_INT64> -> reg#0
00001 IF_FALSE_GOTO	5
00002 READ_TREE		_ws.ftypes.int8 <FT_INT8> -> reg#1
00003 IF_FALSE_GOTO	5
00004 ANY_EQ		reg#0 === reg#1
00005 RETURN
2022-07-14 20:12:30 +00:00
João Valverde 47348ae598 dfilter: Add support for literal strings with null bytes
Before:
    Filter: frame matches "abc\x00def"
    dftest: \x00 (NUL byte) cannot be used with a regular string.
    	frame matches "abc\x00def"
    	                  ^~~~
    Filter: _ws.ftypes.string == "a string with a \0 byte"
    dftest: \0 (NUL byte) cannot be used with a regular string.
    	_ws.ftypes.string == "a string with a \0 byte"
    	                                      ^~

After:
    Filter: frame matches "abc\x00def"

    Syntax tree:
     0 TEST_MATCHES:
       1 FIELD(frame)
       1 PCRE(abc\0def)

    Instructions:
    00000 READ_TREE		frame -> reg#0
    00001 IF_FALSE_GOTO	3
    00002 ANY_MATCHES	reg#0 matches abc\0def
    00003 RETURN

    Filter: _ws.ftypes.string == "a string with a \0 byte"

    Syntax tree:
     0 TEST_ANY_EQ:
       1 FIELD(_ws.ftypes.string)
       1 FVALUE("a string with a \0 byte" <FT_STRING>)

    Instructions:
    00000 READ_TREE		_ws.ftypes.string -> reg#0
    00001 IF_FALSE_GOTO	3
    00002 ANY_EQ		reg#0 == "a string with a \0 byte" <FT_STRING>
    00003 RETURN

Fixes issue #16156.
2022-06-21 15:10:08 +00:00
João Valverde e42a4de47c ftypes: Fix an error message 2022-06-20 17:55:56 +00:00
João Valverde 0615ba6317 ftypes: Make accessor functions type safe 2022-06-20 17:29:57 +00:00
João Valverde eb2a9889c3 dfilter: Add abs() function
Add an absolute value function for ftypes.
2022-04-18 17:09:00 +01:00
João Valverde 8bc214b5bb dfilter: Add remaining arithmetic integer ops 2022-03-31 16:49:42 +01:00
João Valverde 2a9cb588aa dfilter: Add binary arithmetic (add/subtract)
Add support for display filter binary addition and subtraction.

The grammar is intentionally kept simple for now. The use case
is to add a constant to a protocol field, or (maybe) add two
fields in an expression.

We use signed arithmetic with unsigned numbers, checking for
overflow and casting where necessary to do the conversion.
We could legitimately opt to use traditional modular arithmetic
instead (like C) and if it turns out that that is more useful for
some reason we may want to in the future.

Fixes #15504.
2022-03-31 11:27:34 +01:00
João Valverde 9ab2837637 dfilter: Add fvalue duplicate method 2022-03-30 14:05:22 +01:00
João Valverde b9b45a4a8f dfilter: Add ftypes pseudofields
This adds a _ws.ftypes namespace with protocol fields with all
the existing field types.

Currently this is only useful to debug the display filter compiler,
without having to find a real protocol field with the desired type.

Later it may find other uses.
2022-03-28 15:42:32 +01:00
João Valverde ac0a69636b dfilter: Add support for unary arithmetic
This change implements a unary minus operator.

Filter: tcp.window_size_scalefactor == -tcp.dstport

Instructions:
00000 READ_TREE		tcp.window_size_scalefactor -> reg#0
00001 IF_FALSE_GOTO	6
00002 READ_TREE		tcp.dstport -> reg#1
00003 IF_FALSE_GOTO	6
00004 MK_MINUS		-reg#1 -> reg#2
00005 ANY_EQ		reg#0 == reg#2
00006 RETURN

It is supported for integer types, floats and relative time values.
The unsigned integer types are promoted to a 32 bit signed integer.

Unary plus is implemented as a no-op. The plus sign is simply ignored.

Constant arithmetic expressions are computed during compilation.

Overflow with constants is a compile time error. Overflow with
variables is a run time error and silently ignored. Only a debug
message will be printed to the console.

Related to #15504.
2022-03-28 11:20:41 +00:00
João Valverde 0335ebdc3a dfilter: ftype_is_true -> ftype_is_zero 2022-03-23 11:04:41 +00:00
João Valverde 16729be2c1 dfilter: Add bitwise masking of bits
Add support for masking of bits. Before the bitwise operator
could only test bits, it did not support clearing bits.

This allows testing if any combination of bits are set/unset
more naturally with a single test. Previously this was only
possible by combining several bitwise predicates.

Bitwise is implemented as a test node, even though it is not.
Maybe the test node should be renamed to something else.

Fixes #17246.
2022-03-22 12:58:04 +00:00
João Valverde c4f9d8abda dfilter: Rename "unparsed" to "literal"
A literal value is a value that cannot be interpreted as a
registered protocol. An unparsed value can be a literal or
an identifier (protocol/field) according to context and the
current disambiguation rules.

Strictly literal here is to be understood to  mean "numeric
literal, including numeric arrays, but not strings or character
constants".
2022-03-05 11:10:54 +00:00
Guy Harris ec0aaf1811 ftype-time: check for NULL from gmtime() and localtime().
On Windows, they return NULL for times prior to the Epoch.
2022-01-04 15:35:18 -08:00
João Valverde dd9ac15ff2 dfilter: Require separators with ISO 8601 time
Require date/time separators when entering a time value, e,g:
    2014-07-04 12:34:56.789+00:00

Separators in the timezone offset are an exception, they are
never mandatory.

This excludes ISO basic format to avoid inputs that could
be entirely numbers indistinguishable from Epoch time, in case
we want to support that in the future.
2022-01-02 10:44:01 +00:00
João Valverde e724a4baf6 dfilter: Use ISO8601 as the default time format
Change from a default custom time representation to ISO8601.
All the existing formats are still supported for backward-
compatibility.

Before:

  Filter: frame.time == "2011-07-04 12:34:56"

  Constants:
  00000 PUT_FVALUE	"Jul  4, 2011 12:34:56.000000000" <FT_ABSOLUTE_TIME> -> reg#1
  (...)

After:

  Filter: frame.time == "2011-07-04 12:34:56"

  Constants:
  00000 PUT_FVALUE	"2011-07-04 12:34:56+0100" <FT_ABSOLUTE_TIME> -> reg#1
  (...)
2021-12-31 15:01:41 +00:00
João Valverde 0047ca961f dfilter: Add support for entering time in UTC
Add the option to enter a filter with an absolute time
value in UTC. Otherwise the value is interpreted in
local time.

The syntax used is an "UTC" suffix, for example:

    frame.time == "Dec 31, 2002 13:55:31.3 UTC"

This also changes the behavior of "Apply Selected as filter".
Fields using a local time display type will use local time
and fields using UTC display type will be applied using UTC.

Fixes #13268.
2021-12-30 17:53:09 +00:00
João Valverde 64572a11f9 dfilter: Use better error messages for absolute times 2021-12-29 02:25:38 +00:00
João Valverde 445dcd3117 epan: Extend abs_time_to_str() with a flags argument 2021-12-28 04:05:20 +00:00
João Valverde a566076839 epan: Move time display types to field_display_e
This makes it easier to understand the code, avoids conflicts
and ugly and unnecessary casts.

The field display enum has evolved over time from integer types
to a type generic parameter.
2021-12-27 22:31:31 +00:00
João Valverde 0d5bfd44a8 Use a wrapper function to call strptime()
Encapsulate the feature requirements for strptime() in a
portability wrapper.

Use _GNU_SOURCE to expose strptime. It should be enough on glibc
without the side-effect of selecting a particular SUS version,
which we don't need and might hide other definitions.
2021-12-27 14:07:32 +00:00
João Valverde c5a19582e4 epan: Convert to use stdio.h from GLib
Replace:
    g_snprintf() -> snprintf()
    g_vsnprintf() -> vsnprintf()
    g_strdup_printf() -> ws_strdup_printf()
    g_strdup_vprintf() -> ws_strdup_vprintf()

This is more portable, user-friendly and faster on platforms
where GLib does not like the native I/O.

Adjust the format string to use macros from intypes.h.
2021-12-19 19:29:53 +00:00
João Valverde 943c282009 dfilter: Parse character constants in lexer
Invalid character constants should be handled in the lexical scanner.

Todo: See if some code could be shared to parse double quoted strings.

It also fixes some unintuitive type coercions to string. Character
constants should be treated as characters, or maybe integers, or
maybe even throw an invalid comparison error, but coverting to a
literal string or byte array is surprising and not particularly
useful:
  '\xFF' -> "'\xFF'" (equals)
  '\xFF' -> "FF"     (contains)

Before:

    Filter: http.request.method contains "\x63"

    Constants:
    00000 PUT_FVALUE	"c" <FT_STRING> -> reg#1
    (...)

    Filter: http.request.method contains '\x63'

    Constants:
    00000 PUT_FVALUE	"63" <FT_STRING> -> reg#1
    (...)

    Filter: http.request.method == "\x63"

    Constants:
    00000 PUT_FVALUE	"c" <FT_STRING> -> reg#1
    (...)

    Filter: http.request.method == '\x63'

    Constants:
    00000 PUT_FVALUE	"'\\x63'" <FT_STRING> -> reg#1
    (...)

After:

    Filter: http.request.method contains '\x63'

    Constants:
    00000 PUT_FVALUE	"c" <FT_STRING> -> reg#1
    (...)

    Filter: http.request.method == '\x63'

    Constants:
    00000 PUT_FVALUE	"c" <FT_STRING> -> reg#1
    (...)
2021-11-24 08:40:20 +00:00
João Valverde 6ad14ac4fa ftypes: Remove fvalue_string_repr_len()
The implementation is pre-computing the length and using that
to allocate a buffer. This doesn't have any practical advantage
and is inefficient because the code is mostly doing the same work
twice. Remove the unnecessary length pre-computation step.
2021-11-10 16:02:45 +00:00
João Valverde db04d188e1 Remove some unnecessary casts.
Casts are best avoided unless they are truly required. Fix some
constness mismatches this revealed.
2021-10-27 10:24:20 +01:00
João Valverde 13e9e7199c ftypes: Use an order function to compare ftypes
All the order operators can be defined in terms of 'lt'
and 'eq' so use that to reduce the number of required
methods from 6 to 2.

Further reduce to one by combining those two into a single
function that has memcmp semantics: negative return is
"less than", positive is "greater than" and zero is equal.
2021-10-10 20:48:29 +00:00
John Thacker 2ee57a0e46 ftype-time: Absolute times for DFILTER are always local time
absolute_val_from_string() doesn't allow a time zone and always
assumes that time strings are in local time zone, so
absolute_val_to_repr() needs to produce that output for FTREPR_DFILTER
so that construct_match_selected_string() produces the correct filter
string for FT_ABSOLUTE_TIME fields that are not ABSOLUTE_TIME_LOCAL.

Fix #17617
2021-09-30 06:23:05 +00:00
Guy Harris 57a1514ac7 Cast away the return value of g_strlcpy() and g_strlcat().
Most of the time, the return value tells us nothing useful, as we've
already decided that we're perfectly willing to live with string
truncation.  Hopefully this keeps Coverity from whining that those
routines could return an error code (NARRATOR: They don't) and thus that
we're ignoring the possibility of failure (as indicated, we've already
decided that we can live with string truncation, so truncation is *NOT*
a failure).
2021-04-30 03:19:19 -07:00
Thomas Wiens babbe57a1a ftype-time: Use time zone from hfinfo->display
Change-Id: Ib062a812d21b9e2e800bec78417194313fd3b8b4
Reviewed-on: https://code.wireshark.org/review/37938
Petri-Dish: Guy Harris <gharris@sonic.net>
Tested-by: Petri Dish Buildbot
Reviewed-by: Guy Harris <gharris@sonic.net>
2020-07-23 21:49:33 +00:00
Guy Harris 20800366dd HTTPS (almost) everywhere.
Change all wireshark.org URLs to use https.

Fix some broken links while we're at it.

Change-Id: I161bf8eeca43b8027605acea666032da86f5ea1c
Reviewed-on: https://code.wireshark.org/review/34089
Reviewed-by: Guy Harris <guy@alum.mit.edu>
2019-07-26 18:44:40 +00:00
Peter Wu a946eb3141 ftype-time: parse the month independent of the locale
Do not rely on strptime("%b") to parse the month, it does not correctly
recognize English month abbreviations on non-English systems. While at
it, do not try to parse milliseconds if seconds are missing.

Change-Id: Ia049bf362195eef1eba2f04ff7217049fa6a7d9d
Reviewed-on: https://code.wireshark.org/review/31707
Petri-Dish: Peter Wu <peter@lekensteyn.nl>
Tested-by: Petri Dish Buildbot
Reviewed-by: João Valverde <j@v6e.pt>
Reviewed-by: Peter Wu <peter@lekensteyn.nl>
2019-01-24 09:20:10 +00:00
Guy Harris bb47336086 Make white space consistent.
Change-Id: I19053ecc53b7f0d2b4dfb0462f381f7d28bb578a
Reviewed-on: https://code.wireshark.org/review/28502
Reviewed-by: Guy Harris <guy@alum.mit.edu>
2018-06-28 07:32:08 +00:00
Dario Lombardo 55c68ee69c epan: use SPDX indentifiers.
Skipping dissectors dir for now.

Change-Id: I717b66bfbc7cc81b83f8c2cbc011fcad643796aa
Reviewed-on: https://code.wireshark.org/review/25694
Petri-Dish: Dario Lombardo <lomato@gmail.com>
Tested-by: Petri Dish Buildbot
Reviewed-by: Anders Broman <a.broman58@gmail.com>
2018-02-08 19:29:45 +00:00
Martin Kaiser 2d0ae39bd6 ftypes: move get_value_ptr into the union
Delete get_value_ptr from struct _ftype_t, make it
part of the get_value union.

Change-Id: I947331069662a7043bd838e622d286629cc7be9a
Reviewed-on: https://code.wireshark.org/review/20647
Reviewed-by: Martin Kaiser <wireshark@kaiser.cx>
Petri-Dish: Martin Kaiser <wireshark@kaiser.cx>
Tested-by: Petri Dish Buildbot <buildbot-no-reply@wireshark.org>
Reviewed-by: Michael Mann <mmann78@netscape.net>
2017-03-20 17:01:10 +00:00
Martin Kaiser c258b6b056 ftypes: move get_value_uinteger into the union
Delete get_value_uinteger from struct _ftype_t, make it
part of the get_value union.

Change-Id: I4a6c8341676c442e2bf8ae3b8f771b72161d133c
Reviewed-on: https://code.wireshark.org/review/20640
Petri-Dish: Michael Mann <mmann78@netscape.net>
Tested-by: Petri Dish Buildbot <buildbot-no-reply@wireshark.org>
Reviewed-by: Michael Mann <mmann78@netscape.net>
2017-03-20 01:28:42 +00:00
Martin Kaiser e94bd9b4ee ftypes: move get_value_sinteger into the union
Delete get_value_sinteger from struct _ftype_t, make it
part of the get_value union.

Change-Id: I3127252cafc62389ce426639992f1d59f7ac9731
Reviewed-on: https://code.wireshark.org/review/20637
Reviewed-by: Martin Kaiser <wireshark@kaiser.cx>
Petri-Dish: Martin Kaiser <wireshark@kaiser.cx>
Tested-by: Petri Dish Buildbot <buildbot-no-reply@wireshark.org>
Reviewed-by: Michael Mann <mmann78@netscape.net>
2017-03-19 22:53:57 +00:00
Martin Kaiser 5d9cfea5d9 ftypes: move get_value_uinteger64 into the union
Delete get_value_uinteger64 from struct _ftype_t, make it
part of the get_value union.

Change-Id: I2b06efb7691c1bd4089994849373ab8b5ff0bcc7
Reviewed-on: https://code.wireshark.org/review/20618
Petri-Dish: Michael Mann <mmann78@netscape.net>
Tested-by: Petri Dish Buildbot <buildbot-no-reply@wireshark.org>
Reviewed-by: Michael Mann <mmann78@netscape.net>
2017-03-19 15:01:09 +00:00
Martin Kaiser 5bb8f1a241 ftypes: move get_value_sinteger64 into the union
Delete get_value_sinteger64 from struct _ftype_t, make it
part of the get_value union.

Change-Id: I0113f70ab0aadd1aa655466e896e3acce6c8faeb
Reviewed-on: https://code.wireshark.org/review/20617
Reviewed-by: Martin Kaiser <wireshark@kaiser.cx>
Petri-Dish: Martin Kaiser <wireshark@kaiser.cx>
Tested-by: Petri Dish Buildbot <buildbot-no-reply@wireshark.org>
Reviewed-by: Michael Mann <mmann78@netscape.net>
2017-03-19 13:47:57 +00:00