Commit Graph

928 Commits

Author SHA1 Message Date
João Valverde 5f79503d67 Fix config.h include order
The header "config.h" needs to be the first header included in the
source file, no exceptions.
2021-09-20 20:16:52 +01:00
Evan Huus b39fc2aafc wslua: avoid global memory scopes
Use pinfo->pool when available, or allocate/free directly in paths that
don't raise exceptions.
2021-09-14 18:48:56 +00:00
Guy Harris 3cb6403a4c wiretap: always allocate a block for a record.
Without that, you could add a comment to a record in a file format the
reading code for which doesn't allocate blocks, but the comment doesn't
get saved, as there's no block in which to save the comment option.

This simplifies some code paths, as we're either using the record's
modified block or we're using the block as read from the file, there's
no third possibility.

If we attempt to read a record, and we get an error, and a block was
allocated for the record, unreference it, so the individual file readers
don't have to worry about it.
2021-08-29 19:12:13 -07:00
João Valverde 133b0c583f Move epan/wmem/wmem_scopes.h to epan/
This header was installed incorrectly to epan/wmem_scopes.h.

Instead of creating additional installation rules for a single
header in a subfolder (kept for backward compatibility) just
rename the standard "epan/wmem/wmem.h" include to
"epan/wmem_scopes.h" and fix the documentation.

Now the header is installed *correctly* to epan/wmem_scopes.h.
2021-07-26 14:56:11 +00:00
Developer Alexander 69fb2a17e4 lua: dialog with prefilled values
new_dialog() lua api gets extended to enable that dialog fields can be
prefilled by lua scripts instead of always starting empty.
2021-07-13 06:52:55 +00:00
Guy Harris dd5907d2a3 Consistently refer to blocks that have been modified as "modified".
"User" sounds as if the blocks belong to the user; at most, the current
user might have modified them directly, but they might also have, for
example, run a Lua script that, unknown to them, modified comments.
Also, a file might have "user comments" added by a previous user, who
them wrote the file and and provided it to the current user.

"Modified" seems a bit clearer than "changed".
2021-07-08 00:05:35 -07:00
David Perry 73087d6fb4 Use wtap_blocks for packet comments
Mostly functioning proof of concept for #14329. This work is intended to
allow Wireshark to support multiple packet comments per packet.

Uses and expands upon the `wtap_block` API in `wiretap/wtap_opttypes.h`.
It attaches a `wtap_block` structure to `wtap_rec` in place of its
current `opt_comment` and `packet_verdict` members to hold OPT_COMMENT
and OPT_PKT_VERDICT option values.
2021-07-07 18:40:24 +00:00
Chuck Craft 54baebad48 Lua: reconcile expert info groups; add PI_ASSUMPTION 2021-06-21 15:27:51 +00:00
Stig Bjørlykke 553e9e83d2 Lua: Add redissect_packets()
Add Lua function redissect_packets() to redissect packets in live
capture. The use case is to reload packets after a preference change.
2021-06-21 09:52:46 +02:00
João Valverde ff9acff6f2 Replace usage of GLogLevel flags everywhere
ws_log_domains.h needs to be included before wslog.h to be used
to define WS_LOG_DOMAIN. Also the definition for enum ws_log_level
needs to be exported for other APIs so move that to ws_log_domains.h
and rename the file to ws_log_defs.h to reflect the new scope.
2021-06-17 12:00:10 +01:00
João Valverde 39df3ae3c0 Replace g_log() calls with ws_log() 2021-06-16 12:50:27 +00:00
Guy Harris 6c043d5c73 wslua_tvb: fix documentation for reported_length_remaining().
In the past, tvb_reported_length_remaining(), and thus
Tvb:reported_length_remaining(), may have returned -1 if the offset was
invalid.  That's no longer the case; the former returns 0, and, as the
latter just returns the former's return value, that's true of the latter
as well.
2021-06-16 00:43:07 -07:00
Guy Harris bd9ceaebef wslua: a tvbuff doesn't have an "actual length".
It has a "reported length", which is the closes thing to an "actual
length", as it represents the length the packet, or subset thereof, had
on the network, and a "captured length", which is the amount of the
packet that the capture process saved.

In 99.999999999999999999999999999999% of all cases, a dissector should
look at the "reported length", not at the "captured length".

Rename the "len" method to "captured_len", leaving "len" around for
backwards compatibility.

Fix the documentation to reflect reality, to avoid issues such as #15655.
2021-06-16 07:12:12 +00:00
João Valverde 1a702e5430 Try to avoid -Wunused with vanishing macros 2021-06-10 00:51:59 +01:00
João Valverde 9ba97d12d6 Add ws_debug() and use it
Replace most instances of ws_debug_printf() except in
epan/dissectors and dissector plugins.

Some replacements use printf(), some use ws_debug(), and
some were removed because they were dead or judged to be
temporary.
2021-05-24 01:13:19 +00:00
João Valverde e10f761362 wslua: Replace g_assert() with ws_assert() 2021-05-19 03:52:45 +01: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
Guy Harris eb4d68033e Don't cast away upper bits when assigning to a nstime_t's secs field.
The secs field is a time_t, which is not necessarily 32 bits.  If it's
not, casting away the upper bits, by casting to guint32, introduces a
Y2.038K bug.

Either cast to time_t or, if you're assigning a time_t to it, don't
bother with the cast.
2021-04-28 21:31:15 +00:00
Pascal Quantin ebdb6784fa wslua: fix compilation with gcc 11
Fixes the misleading-indentation warning
2021-04-28 20:12:19 +00:00
Moshe Kaplan 00b86cf64d wslua: Fix crash on FieldInfo.range and FieldInfo.value for a nonexistent TVB
Fields such as '_ws.expert' have no underlying tvb; they are added
with offset 0 and length 0 and the field's underlying tvb is NULL. FieldInfo__call
passes tvb to tvb_memdup() without checking if the tvb is null and
assumes that a NULL tvb means that the tvb is expired and therefore raises an error:
"epan/tvbuff.c:477: failed assertion "tvb && tvb->initialized"

Fields such as '_ws.expert.group' have no underlying tvb; they are added
with offset 0 and length 0 and the field's underlying tvb is NULL. FieldInfo_get_range
calls push_TvbRange, which assumes that a NULL tvb means that the tvb is expired
and therefore raises a lua error of "expired tvb".

This commit explicitly adds a check to FieldInfo__call() to see if the tvb is null when
attempting to access the underlying tvb.

It also explicitly checks if the tvb is null when attempting to access the range
and if it is, returns nil. This is consistent with how FieldInfo.source also
returns nil for such fields.

This commit should fix issue #13542.
2021-04-28 16:59:54 +00:00
David Perry 26c5635973 [#17212] Define loading order for lua plugins
Instead of loading lua plugins in the random-seeming order that is
returned by the filesystem, sort the list of plugin filenames in
ASCIIbetical order. This makes the load order of plugins predictable.

This particular order was chosen to be consistent with the precedent set
by various *nix tools.
2021-04-20 05:38:32 +00:00
Nardi Ivan a97506d166 wslua_utils: fix a memory leak 2021-04-04 16:05:30 +00:00
Guy Harris cda18f951e wslua_tvb: correctly trim off the end of a tvbuff.
The length specified in a TvbRange is the *actual packet length*, not
the *sliced-to* length, so use tvb_new_subset_length() to cut it short.

This fixes the fix for #15655, and addresses at least some of the issues
in #17255.
2021-03-27 19:31:37 -07:00
Daniel Dulaney 62af671bf3 Lua: add DissectorTable.try_heuristics() function
Add DissectorTable.try_heuristics(name, tvb, pinfo, tree). Previously,
there was no way for a Lua plugin to run an existing heuristic
dissector.

Based on Gerrit change 18718. Closes #17220.
2021-03-26 07:15:16 +00:00
Dario Lombardo ea929d6401 wsutils: add local implementation of g_memdup2.
g_memdup() was deprecated and replaced with g_memdup2() in GLib 2.68,
we provide our own copy of g_memdup2() for older GLib versions.
2021-03-25 09:38:10 +00:00
Guy Harris b61fd6d76a dfilter, ftypes: get rid of FT_PCRE.
It's not a valid field type, it's only a hack to support regular
expression matching in packet-matching expressions.

Instead, in the packet-matching code, have a separate syntax tree type
for Perl-compatible regular expressions, and a separate instruction to
load one into a register, and have the "matching" operator for field
types take a GRegex * as the second argument.
2021-03-21 03:27:44 -07:00
Guy Harris 2820156fbd Move still *more* headers outside of extern "C".
If a header declares a function, or anything else requiring the extern
"C" decoration, have it wrap the declaration itself; don't rely on the
header itself being included inside extern "C".
2021-03-16 13:50:13 -07:00
Guy Harris ce20c00049 Remove unnecessary inclues of wiretap/pcapng.h. 2021-03-15 15:29:40 -07:00
João Valverde 7f105d3981 CMake: Use CheckAPI's abort/termoutput with dissectors only
I believe this was the original intention, to use these API restricitons
with dissectors only (not that I necessarily agree with that policy either),
and through copy-paste and lack of clear guidelines it spread to other
parts of the build.

Rename the checkAPI groups to make it very clear that this is dissector-only.

This doesn't mean, of course, that good programming practices shouldn't be
followed everywhere. In particular assertions need to be used properly.
Don't use them to catch runtime errors or validate input data.

This commit will be followed by another removing the various ugly hacks
people have been using to get around the checkAPI hammer.
2021-03-01 20:59:39 +00:00
Guy Harris ed86f51e49 wiretap: rename wtap_register_file_type_subtypes().
It only registers one file type/subtype, so rename it to
wtap_register_file_type_subtype().

That will also force plugins to be recompiled; that will produce compile
errors for some plugins that didn't change to match the new contents of
the file_type_subtype_info structure.

Also check to make sure that the registered file type/subtype supports
at least one type of block; a file type/subtype that doesn't return
*any* blocks and doesn't permit *any* block types to be written is not
very useful.  That should also catch most if not all other plugins that
didn't change to match the new contents of the file_type_subtype_info
structure.

Don't make errors registering a file type/subtype fatal; just complain,
don't register the bogus file type/subtype, and drive on.
2021-02-23 20:39:16 -08:00
Guy Harris 166159f15d wiretap: eliminate the pcap/nspcap/pcapng WTAP_FILE_TYPE_SUBTYPE_ values.
Register the pcap and pcapng file types/subtypes rather than hardwiring
them into the table.

Call the registration routines for them directly, rather than through a
generated table; they're always supposed to be there, as some code in
Wireshark either writes only one of those formats or defaults to writing
one of those formats.  Don't run their source code through the
registration-routine-finder script.

Have the file type/subtype codes for them be directly exported to the
libwiretap core, and provide routines to return each of them, to be used
by the aforementioned code.

When reporting errors with cfile_write_failure_message(), use
wtap_dump_file_type_subtype() to get the file type/subtype value for the
wtap_dumper to which we're writing, rather than hardcoding it.

Have the "export PDU" code capable of supporting arbitrary file
types/subtypes, although we currently only use pcapng.

Get rid of declarations of now-static can_write_encap and
dump_open routines in various headers.
2021-02-23 21:56:20 +00:00
Guy Harris bc3cc17bc4 Lua: add routines to return pcap/nsec pcap/pcapng file type/subtypes.
These will be backported, for the benefit of Lua scripts that want those
specific file types/subtypes (typically in order to write files of those
types); that allows those types to be fetched without having to know the
right string to hand to wslua_wtap_name_to_file_type_subtype().
2021-02-22 22:23:54 -08:00
Guy Harris 842a7cccf9 wiretap: have file handlers advertise blocks and options supported.
Instead of a "supports name resolution" Boolean and bitflags for types of
comments supported, provide a list of block types that the file
type/subtype supports, with each block type having a list of options
supported.  Indicate whether "supported" means "one instance" or
"multiple instances".

"Supports" doesn't just mean "can be written", it also means "could be
read".

Rename WTAP_BLOCK_IF_DESCRIPTION to WTAP_BLOCK_IF_ID_AND_INFO, to
indicate that it provides, in addition to information about the
interface, an ID (implicitly, in pcapng files, by its ordinal number)
that is associated with every packet in the file.  Emphasize that in
comments - just because your capture file format can list the interfaces
on which a capture was done, that doesn't mean it supports this; it
doesn't do so if the file doesn't indicate, for every packet, on which
of those interfaces it was captured (I'm looking at *you*, Microsoft
Network Monitor...).

Use APIs to query that information to do what the "does this file
type/subtype support name resolution information", "does this file
type/subtype support all of these comment types", and "does this file
type/subtype support - and require - interface IDs" APIs did.

Provide backwards compatibility for Lua.

This allows us to eliminate the WTAP_FILE_TYPE_SUBTYPE_ values for IBM's
iptrace; do so.
2021-02-21 23:18:35 +00:00
Gerald Combs 4fd5224ecf CMake: Use target_include_directores more.
The include_directories documentation at
https://cmake.org/cmake/help/latest/command/include_directories.html
says:

"Note: Prefer the target_include_directories() command to add include
 directories to individual targets and optionally propagate/export them
 to dependents."

Switch from include_directories to target_include_directories in a bunch
of places.

Add "SYSTEM" to the remaining external include_directories calls in
order to minimize our compiler warning blast radius.
2021-02-18 06:34:46 +00:00
Guy Harris a7256d50b5 wiretap: more work on file type/subtypes.
Provide a wiretap routine to get an array of all savable file
type/subtypes, sorted with pcap and pcapng at the top, followed by the
other types, sorted either by the name or the description.

Use that routine to list options for the -F flag for various commands

Rename wtap_get_savable_file_types_subtypes() to
wtap_get_savable_file_types_subtypes_for_file(), to indicate that it
provides an array of all file type/subtypes in which a given file can be
saved.  Have it sort all types, other than the default type/subtype and,
if there is one, the "other" type (both of which are put at the top), by
the name or the description.

Don't allow wtap_register_file_type_subtypes() to override any existing
registrations; have them always register a new type.  In that routine,
if there are any emply slots in the table, due to an entry being
unregistered, use it rather than allocating a new slot.

Don't allow unregistration of built-in types.

Rename the "dump open table" to the "file type/subtype table", as it has
entries for all types/subtypes, even if we can't write them.

Initialize that table in a routine that pre-allocates the GArray before
filling it with built-in types/subtypes, so it doesn't keep getting
reallocated.

Get rid of wtap_num_file_types_subtypes - it's just a copy of the size
of the GArray.

Don't have wtap_file_type_subtype_description() crash if handed an
file type/subtype that isn't a valid array index - just return NULL, as
we do with wtap_file_type_subtype_name().

In wtap_name_to_file_type_subtype(), don't use WTAP_FILE_TYPE_SUBTYPE_
names for the backwards-compatibility names - map those names to the
current names, and then look them up.  This reduces the number of
uses of hardwired WTAP_FILE_TYPE_SUBTYPE_ values.

Clean up the type of wtap_module_count - it has no need to be a gulong.

Have built-in wiretap file handlers register names to be used for their
file type/subtypes, rather than building the table in init.lua.

Add a new Lua C function get_wtap_filetypes() to construct the
wtap_filetypes table, based on the registered names, and use it in
init.lua.

Add a #define WSLUA_INTERNAL_FUNCTION to register functions intended
only for internal use in init.lua, so they can be made available from
Lua without being documented.

Get rid of WTAP_NUM_FILE_TYPES_SUBTYPES - most code has no need to use
it, as it can just request arrays of types, and the space of
type/subtype codes can be sparse due to registration in any case, so
code has to be careful using it.

wtap_get_num_file_types_subtypes() is no longer used, so remove it.  It
returns the number of elements in the file type/subtype array, which is
not necessarily the name of known file type/subtypes, as there may have
been some deregistered types, and those types do *not* get removed from
the array, they just get cleared so that they're available for future
allocation (we don't want the indices of any registered types to changes
if another type is deregistered, as those indicates are the type/subtype
values, so we can't shrink the array).

Clean up white space and remove some comments that shouldn't have been
added.
2021-02-17 21:54:28 +00:00
Gerald Combs e99c9afce8 Be more strict about opening URLs.
In the proto tree, copy URLs instead of opening them.

In the export dialog, enable previews only if the advertised MIME type
*and* the contents of the file are plain text, GIF, JPEG, or PNG.

Add warnings to the wslua browser_open_url and browser_open_data_file
documentation.

Fixes #17232.
2021-02-15 08:47:57 +00:00
Gerald Combs 270c8ed746 CMake: Make sure system headers are treated as such.
Use target_include_directories instead of include_directories in a few
places as recommended at

https://cmake.org/cmake/help/latest/command/include_directories.html

Doing so lets us mark a bunch of dependency includes SYSTEM PRIVATE, in
particular LIBXML2_INCLUDE_DIRS. On macOS this keeps us from triggering
the nullability warnings described at

https://www.wireshark.org/lists/wireshark-dev/202004/msg00056.html

(This might also keep the Visual Studio code analyzer from complaining
about various Qt headers, but I haven't tested this.)
2021-02-15 08:31:08 +00:00
Guy Harris b8b3531883 wiretap: register most built-in file types from its module.
Remove most of the built-in file types from the table in
wiretap/file_access.c and, instead, have the file types register
themselves, using wtap_register_file_type_subtypes().

This reduces the source code changes needed to add a new file type from
three (add the handler, add the file type to the table in file_access.c,
add a #define for the file type in wiretap/wtap.h) to one (add the
handler).  (It also requires adding the handler's source file to
wiretap/CMakeLists.txt, but that's required in both cases.)

A few remain because the WTAP_FILE_TYPE_SUBTYPE_ #define is used
elsewhere; that needs to be fixed.

Fix the wiretap/CMakefile.txt file to scan k12text.l, as that now
contains a registration routine.  In the process, avoid scanning files
that don't implement a file type and won't ever have a registration
routine.

Add a Lua routine to fetch the total number of file types; we use that
in some code to construct the wtap_filetypes table, which we need to do
in order to continue to have all the values that used to come from the
WTAP_FILE_TYPE_SUBTYPE_ types.

While we're at it, add modelines to a file that lacked them.
2021-02-14 00:58:46 -08:00
Guy Harris 5b3c3d0682 WSDG: deprecate wtap_filetypes.
Recommend the use of wtap_name_to_file_type_subtype() to get filetype
values, unless you need to run on older versions of Wireshark that don't
have it.

Don't even *mention* wtap_filetypes in the documentation for the new
wtap_ routines, as, if you have those routines, you have
wtap_name_to_file_type_subtype(), because it's one of those routines.

Fix references to "nul" while we're at it - it's "nil" in Lua.

(That part of the WSDG - the Lua reference - is generated, so this
involves changing the source code implementing the Lua routines.)
2021-02-13 22:12:23 -08:00
Guy Harris 24acef0885 wiretap: file types have a name and a description.
The "short name" is really just the name, used to look it up.  The
"name" is really a description intended solely for human consumption.
Rename the fields, and the functions that access them, to match.

The "description" maintained by Lua for file type handlers is used
*only* for one debugging message; we should probably just eliminate it.
Call it an "internal description" for now.
2021-02-13 01:25:39 -08:00
Guy Harris f0ebc50762 wslua: expose some libwiretap APIs in Lua.
Provide Lua version of wtap_file_type_subtype_string(),
wtap_file_type_subtype_short_string(), and
wtap_short_string_to_file_type_subtype().

This will be backported to the 3.2 and 3.4 branches, to allow scripts
not run on the bleeding-edge version to use them.
2021-02-12 21:25:29 -08:00
Guy Harris 23467afdc5 wslua: explain, in a comment, how .init routines work for a Proto. 2021-02-05 22:02:54 -08:00
Moshe Kaplan e16166a74c Detect and replace bad allocation patterns
Adds a pre-commit hook for detecting and replacing
occurrences of `g_malloc()` and `wmem_alloc()` with
`g_new()` and `wmem_new()`, to improve the
readability of Wireshark's code, and
occurrences of
`g_malloc(sizeof(struct myobj) * foo)`
with
`g_new(struct myobj, foo)`
to prevent integer overflows

Also fixes all existing occurrences across
the codebase.
2020-12-22 14:56:38 +00:00
Stig Bjørlykke 9cafd342ac Lua: Add get_preference() and set_preference() utility functions.
Support uint, bool, enum, string, range, filename and directory preferences.
Added apply_preferences() to write to file and apply changes.
2020-12-08 22:56:50 +00:00
George Powers e7ec6739b6 Fix reported_len in Lua framewark when creating tvb from range.
This bug affects Lua plugin dissectors for encapsulation protocols like
GRE.  Typically the dissector creates a range for the payload packet, then
calls the next dissector with a tvb derived from the range, using
TvbRange_tvb().  The original version calls
tvb_new_subset_length_caplen() using the remaining capture length for the
reported_len argument.  The fix passes -1 as the reported length, and
tvb_new_subset_length_caplen() calculates the new reported_len as required.

The bug only affects large packets captured with a snaplen and
truncated, then decoded with a Lua plugin for the encapsulation header.

Here's the typical bug symptom, gleaned from tshark decode of
an encapsulated IP payload:

        [Expert Info (Error/Protocol): IPv4 total length exceeds packet length (114 bytes)]
            [IPv4 total length exceeds packet length (114 bytes)]

Closes #15655.
2020-12-06 12:38:46 +00:00
Stig Bjørlykke d104571e8a Lua: Improve Listener error handling
Fix error handlers in Listener draw() and reset() to avoid getting
LUA_ERRERR from lua_pcall(). Added error handler for Listener draw()
callback.

Handle LUA_ERRERR from lua_pcall() to avoid assert on this.
Changed some capitalized words in various error message.

Closes #16974.
2020-12-04 13:38:00 +00:00
David Perry 27c00ca0e9 Lua: make TvbRange:raw() return a subset #12046 v2
My initial fix caused several double-offset errors in TvbRange_raw()
because I was adjusting for the TvbRange's offset too early in the
process. The proper fix is to only adjust for it in the final call to
get the data.

I also simplified some of the bounds checks to be based on the values in
the TvbRange instead of calling `tvb_captured_length()` and the like,
because its bounds are already checked against the backing Tvb when it's
first taken.

Massively expanded the lua test suite to account for every combination
of passing offsets and lengths to a Tvb or TvbRange and to the
subsequent `:raw()` call.
2020-12-02 20:09:55 +00:00
John Thacker 15d315c2ae GLib: Bump requirement 2.32 -> 2.36 2020-11-25 06:25:38 +00:00
Stig Bjørlykke ebfa1f8a4b wslua: Return nil from Dissector.get() when not found
Return nil from Dissector.get() and DissectorTable.get() when the
reference is not found. This can be used to check for existence of
a dissector or dissector table before use.

We already do this for DissectorTable.get_dissector().
2020-11-05 06:23:24 +00:00
Stig Bjørlykke 10e6cb5f37 wslua: Make pinfo.p2p_dir writable
Make pinfo.p2p_dir writeble from Lua to be able to set the direction
for sub-dissectors.
2020-10-22 14:18:58 +00:00