Skip to content

Commit

Permalink
wiretap: more work on file type/subtypes.
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
guyharris committed Feb 17, 2021
1 parent 2ff9f3e commit a7256d5
Show file tree
Hide file tree
Showing 72 changed files with 1,053 additions and 617 deletions.
6 changes: 4 additions & 2 deletions debian/libwiretap0.symbols
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ libwiretap.so.0 libwiretap0 #MINVER#
file_read@Base 1.9.1
file_seek@Base 1.9.1
file_tell@Base 1.9.1
get_backwards_compatibility_lua_table@Base 3.5.0
init_open_routines@Base 1.12.0~rc1
merge_files@Base 1.99.9
merge_files_to_stdout@Base 2.3.0
Expand Down Expand Up @@ -119,8 +120,8 @@ libwiretap.so.0 libwiretap0 #MINVER#
wtap_get_next_interface_description@Base 3.3.2
wtap_get_num_encap_types@Base 1.9.1
wtap_get_num_file_type_extensions@Base 1.12.0~rc1
wtap_get_num_file_types_subtypes@Base 1.12.0~rc1
wtap_get_savable_file_types_subtypes@Base 1.12.0~rc1
wtap_get_savable_file_types_subtypes_for_file@Base 3.5.0
wtap_get_writable_file_types_subtypes@Base 3.5.0
wtap_has_open_info@Base 1.12.0~rc1
wtap_init@Base 2.3.0
wtap_name_to_encap@Base 2.9.1
Expand All @@ -137,6 +138,7 @@ libwiretap.so.0 libwiretap0 #MINVER#
wtap_read_so_far@Base 1.9.1
wtap_rec_cleanup@Base 2.5.1
wtap_rec_init@Base 2.5.1
wtap_register_backwards_compatibility_lua_name@Base 3.5.0
wtap_register_encap_type@Base 1.9.1
wtap_register_file_type_extension@Base 1.12.0~rc1
wtap_register_file_type_subtypes@Base 1.12.0~rc1
Expand Down
27 changes: 7 additions & 20 deletions editcap.c
Original file line number Diff line number Diff line change
Expand Up @@ -871,13 +871,6 @@ struct string_elem {
const char *lstr; /* The long string */
};

static gint
string_compare(gconstpointer a, gconstpointer b)
{
return strcmp(((const struct string_elem *)a)->sstr,
((const struct string_elem *)b)->sstr);
}

static gint
string_nat_compare(gconstpointer a, gconstpointer b)
{
Expand All @@ -895,22 +888,16 @@ string_elem_print(gpointer data, gpointer stream_ptr)

static void
list_capture_types(FILE *stream) {
int i;
struct string_elem *captypes;
GSList *list = NULL;
GArray *writable_type_subtypes;

captypes = g_new(struct string_elem,WTAP_NUM_FILE_TYPES_SUBTYPES);
fprintf(stream, "editcap: The available capture file types for the \"-F\" flag are:\n");
for (i = 0; i < WTAP_NUM_FILE_TYPES_SUBTYPES; i++) {
if (wtap_dump_can_open(i)) {
captypes[i].sstr = wtap_file_type_subtype_name(i);
captypes[i].lstr = wtap_file_type_subtype_description(i);
list = g_slist_insert_sorted(list, &captypes[i], string_compare);
}
writable_type_subtypes = wtap_get_writable_file_types_subtypes(FT_SORT_BY_NAME);
for (guint i = 0; i < writable_type_subtypes->len; i++) {
int ft = g_array_index(writable_type_subtypes, int, i);
fprintf(stderr, " %s - %s\n", wtap_file_type_subtype_name(ft),
wtap_file_type_subtype_description(ft));
}
g_slist_foreach(list, string_elem_print, stream);
g_slist_free(list);
g_free(captypes);
g_array_free(writable_type_subtypes, TRUE);
}

static void
Expand Down
6 changes: 0 additions & 6 deletions epan/wslua/make-init-lua.pl
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@
die "'$WSROOT' is not a directory" unless -d $WSROOT;

my $wtap_encaps_table = '';
my $wtap_filetypes_table = '';
my $wtap_tsprecs_table = '';
my $wtap_commenttypes_table = '';
my $ft_types_table = '';
Expand All @@ -36,7 +35,6 @@

my %replacements = %{{
WTAP_ENCAPS => \$wtap_encaps_table,
WTAP_FILETYPES => \$wtap_filetypes_table,
WTAP_TSPRECS => \$wtap_tsprecs_table,
WTAP_COMMENTTYPES => \$wtap_commenttypes_table,
FT_TYPES => \$ft_types_table,
Expand Down Expand Up @@ -70,7 +68,6 @@
#

$wtap_encaps_table = "-- Wiretap encapsulations XXX\nwtap_encaps = {\n";
$wtap_filetypes_table = "-- Wiretap file types\nwtap_filetypes = {\n";
$wtap_tsprecs_table = "-- Wiretap timestamp precision types\nwtap_tsprecs = {\n";
$wtap_commenttypes_table = "-- Wiretap file comment types\nwtap_comments = {\n";
$wtap_rec_types_table = "-- Wiretap record_types\nwtap_rec_types = {\n";
Expand All @@ -85,8 +82,6 @@

if ( /^#define WTAP_TSPREC_([A-Z0-9_]+)\s+(\d+)/ ) {
$wtap_tsprecs_table .= "\t[\"$1\"] = $2,\n";
# for backwards compatibility we need to add them to the filetypes table too
$wtap_filetypes_table .= "\t[\"TSPREC_$1\"] = $2,\n";
}

if ( /^#define WTAP_COMMENT_([A-Z0-9_]+)\s+(0x\d+)/ ) {
Expand All @@ -104,7 +99,6 @@
}

$wtap_encaps_table =~ s/,\n$/\n}\nwtap = wtap_encaps -- for bw compatibility\n/msi;
$wtap_filetypes_table =~ s/,\n$/\n}\n/msi;
$wtap_tsprecs_table =~ s/,\n$/\n}\n/msi;
$wtap_commenttypes_table =~ s/,\n$/\n}\n/msi;
# wtap_rec_types_table has comments at the end (not a comma),
Expand Down
8 changes: 8 additions & 0 deletions epan/wslua/make-reg.pl
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@

my @classes = ();
my @functions = ();
my @internal_functions = ();
my $source_dir = "";

GetOptions('dir=s' => \$source_dir);
Expand All @@ -33,6 +34,7 @@
while (<FILE>) {
push @classes, $1 if /WSLUA_CLASS_DEFINE(?:_BASE)?\050\s*([A-Za-z0-9]+)/;
push @functions, $1 if /WSLUA_FUNCTION\s+wslua_([a-z_0-9]+)/;
push @internal_functions, $1 if /WSLUA_INTERNAL_FUNCTION\s+wslua_([a-z_0-9]+)/;
}
}

Expand All @@ -52,6 +54,9 @@
for (@functions) {
print H "\tWSLUA_FUNCTION wslua_$_(lua_State* L);\\\n"
}
for (@internal_functions) {
print H "\tWSLUA_INTERNAL_FUNCTION wslua_$_(lua_State* L);\\\n"
}
print H "\n\n";
print H "extern void wslua_register_classes(lua_State* L);\n";
print H "extern void wslua_register_functions(lua_State* L);\n";
Expand Down Expand Up @@ -83,6 +88,9 @@
for (@functions) {
print C "\tWSLUA_REGISTER_FUNCTION($_); \n"
}
for (@internal_functions) {
print C "\tWSLUA_REGISTER_FUNCTION($_); \n"
}
print C "}\n\n";

close H;
Expand Down
121 changes: 1 addition & 120 deletions epan/wslua/template-init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -78,12 +78,6 @@ end

%WTAP_ENCAPS%

--
-- This pulls in the WTAP_TSPREC_ values that are included in
-- wtap_filetypes for backwards compatibility.
--
%WTAP_FILETYPES%

--
-- Generate the wtap_filetypes items for file types, for backwards
-- compatibility.
Expand All @@ -94,120 +88,7 @@ end
-- New code should use wtap_name_to_file_type_subtype to
-- look up file types by name.
--
-- "UNKNOWN" is a special case; it has no name.
--
wtap_filetypes["UNKNOWN"] = 0
for filetype = 1, wtap_get_num_file_types_subtypes() - 1 do
local typename = wtap_file_type_subtype_name(filetype)
--
-- In most cases, the old #define was just a capitalized
-- version of the file type name.
--
-- We process the exceptions here.
--
if typename == "nsecpcap" then
wtap_filetypes["PCAP_NSEC"] = filetype
elseif typename == "aixpcap" then
wtap_filetypes["PCAP_AIX"] = filetype
elseif typename == "modpcap" then
wtap_filetypes["PCAP_SS991029"] = filetype
elseif typename == "nokiapcap" then
wtap_filetypes["PCAP_NOKIA"] = filetype
elseif typename == "rh6_1pcap" then
wtap_filetypes["PCAP_SS990417"] = filetype
elseif typename == "suse6_3pcap" then
wtap_filetypes["PCAP_SS990915"] = filetype
elseif typename == "iptrace_1" then
wtap_filetypes["IPTRACE_1_0"] = filetype
elseif typename == "iptrace_2" then
wtap_filetypes["IPTRACE_2_0"] = filetype
elseif typename == "dct2000" then
wtap_filetypes["CATAPULT_DCT2000"] = filetype
elseif typename == "netxray1" then
wtap_filetypes["NETXRAY_OLD"] = filetype
elseif typename == "netxray2" then
-- This is correct; the #define was wrong
wtap_filetypes["NETXRAY_1_0"] = filetype
elseif typename == "etherwatch" then
wtap_filetypes["DBS_ETHERWATCH"] = filetype
elseif typename == "iseries_ascii" then
wtap_filetypes["ISERIES"] = filetype
elseif typename == "iseries_unicode" then
wtap_filetypes["ISERIES_UNICODE"] = filetype
elseif typename == "netmon1" then
wtap_filetypes["NETMON_1_x"] = filetype
elseif typename == "netmon2" then
wtap_filetypes["NETMON_2_x"] = filetype
elseif typename == "ngsniffer" then
wtap_filetypes["NGSNIFFER_UNCOMPRESSED"] = filetype
elseif typename == "ngsniffer_comp" then
wtap_filetypes["NGSNIFFER_COMPRESSED"] = filetype
elseif typename == "ngwsniffer_1_1" then
wtap_filetypes["NETXRAY_1_1"] = filetype
elseif typename == "ngwsniffer_2_0" then
wtap_filetypes["NETXRAY_2_00x"] = filetype
elseif typename == "niobserver" then
wtap_filetypes["NETWORK_INSTRUMENTS"] = filetype
elseif typename == "pppd" then
wtap_filetypes["PPPDUMP"] = filetype
elseif typename == "tcpiptrace" then
wtap_filetypes["VMS"] = filetype
elseif typename == "rf5" then
wtap_filetypes["K12"] = filetype
elseif typename == "visual" then
wtap_filetypes["VISUAL_NETWORKS"] = filetype
elseif typename == "peekclassic56" then
wtap_filetypes["PEEKCLASSIC_V56"] = filetype
elseif typename == "peekclassic7" then
wtap_filetypes["PEEKCLASSIC_V7"] = filetype
elseif typename == "pklg" then
wtap_filetypes["PACKETLOGGER"] = filetype
elseif typename == "dsna" then
wtap_filetypes["DAINTREE_SNA"] = filetype
elseif typename == "nstrace10" then
wtap_filetypes["NETSCALER_1_0"] = filetype
elseif typename == "nstrace20" then
wtap_filetypes["NETSCALER_2_0"] = filetype
elseif typename == "nstrace30" then
wtap_filetypes["NETSCALER_3_0"] = filetype
elseif typename == "nstrace35" then
wtap_filetypes["NETSCALER_3_5"] = filetype
elseif typename == "jpeg" then
wtap_filetypes["JPEG_JFIF"] = filetype
elseif typename == "mp2t" then
wtap_filetypes["MPEG_2_TS"] = filetype
elseif typename == "vwr80211" then
wtap_filetypes["VWR_80211"] = filetype
elseif typename == "vwreth" then
wtap_filetypes["VWR_ETH"] = filetype
elseif typename == "stanag4607" then
wtap_filetypes["STANAG_4607"] = filetype
elseif typename == "logcat-brief" then
wtap_filetypes["LOGCAT_BRIEF"] = filetype
elseif typename == "logcat-process" then
wtap_filetypes["LOGCAT_PROCESS"] = filetype
elseif typename == "logcat-tag" then
wtap_filetypes["LOGCAT_TAG"] = filetype
elseif typename == "logcat-thread" then
wtap_filetypes["LOGCAT_THREAD"] = filetype
elseif typename == "logcat-time" then
wtap_filetypes["LOGCAT_TIME"] = filetype
elseif typename == "logcat-threadtime" then
wtap_filetypes["LOGCAT_THREADTIME"] = filetype
elseif typename == "logcat-long" then
wtap_filetypes["LOGCAT_LONG"] = filetype
elseif typename == "colasoft-pb" then
wtap_filetypes["PACKET_BUILDER"] = filetype
elseif typename == "3gpp32423" then
wtap_filetypes["NETTRACE_3GPP_32_423"] = filetype
elseif typename == "3gpp_log" then
wtap_filetypes["LOG_3GPP"] = filetype
elseif typename == "jpeg" then
wtap_filetypes["JPEG_JFIF"] = filetype
else
wtap_filetypes[string.upper(typename)] = filetype
end
end
wtap_filetypes = get_wtap_filetypes()

%WTAP_TSPRECS%

Expand Down
2 changes: 2 additions & 0 deletions epan/wslua/wslua.h
Original file line number Diff line number Diff line change
Expand Up @@ -462,6 +462,8 @@ extern int wslua_reg_attributes(lua_State *L, const wslua_attribute_table *t, gb
#endif

#define WSLUA_FUNCTION extern int
/* This is for functions intended only to be used in init.lua */
#define WSLUA_INTERNAL_FUNCTION extern int

#define WSLUA_REGISTER_FUNCTION(name) { lua_pushcfunction(L, wslua_## name); lua_setglobal(L, #name); }

Expand Down
12 changes: 1 addition & 11 deletions epan/wslua/wslua_file_handler.c
Original file line number Diff line number Diff line change
Expand Up @@ -719,16 +719,6 @@ WSLUA_FUNCTION wslua_register_filehandler(lua_State* L) {
if (!verify_filehandler_complete(fh))
return luaL_error(L,"this FileHandler is not complete enough to register");

/* If a Lua file handler is reloaded, try to reuse the previous subtype.
* XXX wtap_register_file_type_subtypes will abort the program if a builtin
* file handler is overridden, so plugin authors should not try that.
*/
int file_type = wtap_name_to_file_type_subtype(fh->finfo.name);
if (file_type == -1) {
/* File type was not registered before, create a new one. */
file_type = WTAP_FILE_TYPE_SUBTYPE_UNKNOWN;
}

if (fh->is_writer) {
if (fh->extensions && fh->extensions[0]) {
char *extension = g_strdup(fh->extensions);
Expand All @@ -747,7 +737,7 @@ WSLUA_FUNCTION wslua_register_filehandler(lua_State* L) {
fh->finfo.dump_open = wslua_filehandler_dump_open;
}

fh->file_type = wtap_register_file_type_subtypes(&(fh->finfo), file_type);
fh->file_type = wtap_register_file_type_subtypes(&(fh->finfo));

if (fh->is_reader) {
struct open_info oi = { NULL, OPEN_INFO_HEURISTIC, NULL, NULL, NULL, NULL };
Expand Down
48 changes: 39 additions & 9 deletions epan/wslua/wslua_wtap.c
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,12 @@
#include "wslua.h"
#include <wiretap/wtap.h>

/*
* Solely for the function that gets the table of backwards-compatibility
* Lua names for file types/subtypes.
*/
#include <wiretap/wtap-int.h>

WSLUA_FUNCTION wslua_wtap_file_type_subtype_description(lua_State* LS) {
/*
Get a string describing a capture file type, given a filetype
Expand Down Expand Up @@ -84,15 +90,39 @@ WSLUA_FUNCTION wslua_wtap_name_to_file_type_subtype(lua_State* LS) {
WSLUA_RETURN(1); /* The filetype value for the file type with that name, or nil if there is no such file type. */
}

WSLUA_FUNCTION wslua_wtap_get_num_file_types_subtypes(lua_State* LS) {
/*
Get the total number of filetype values.
/*
* init.wslua-only function to return a table to assign to
* wtap_filetypes.
*/
WSLUA_INTERNAL_FUNCTION wslua_get_wtap_filetypes(lua_State* LS) {
/* Get the GArray from which we initialize this. */
const GArray *table = get_backwards_compatibility_lua_table();

Note: as filetype values begin with 0, this is one *greater*
than the largest filetype value.
/*
* Create the table; it's indexted by strings, not numbers,
* so none of the entries will be in a sequence.
*/
lua_createtable(LS,0,table->len);
for (guint i = 0; i < table->len; i++) {
struct backwards_compatibiliity_lua_name *entry;

@since 3.5.0
*/
lua_pushnumber(LS,wtap_get_num_file_types_subtypes());
WSLUA_RETURN(1); /* The total number of registered file types. */
entry = &g_array_index(table,
struct backwards_compatibiliity_lua_name, i);
/*
* Push the name and the ft, in order, so that the ft,
* which should be the value at the top of the stack,
* is at the top of the stack, and the name, which should
* be the value just below that, is the value just below
* it.
*/
lua_pushstring(LS, entry->name);
lua_pushnumber(LS, entry->ft);
/*
* The -3 is the index, relative to the top of the stack, of
* the table; the two elements on top of it are the ft and
* the name, so it's -3.
*/
lua_settable(LS, -3);
}
WSLUA_RETURN(1); /* The table. */
}
Loading

0 comments on commit a7256d5

Please sign in to comment.