plugins: Allow multiple types in description

Allow plugins to declare their type, for the purpose of inserting
a description in the UI. The type consist of one or more bit ORed
flags.

This fixes the 'stats_tree' plugin description in the UI.
The plugin is not a dissector type plugin, as was being displayed
before. Now it correctly shows "tap listener" plugin.
This commit is contained in:
João Valverde 2023-10-25 20:11:01 +01:00
parent 061eea1c37
commit 7fbed5b306
8 changed files with 94 additions and 32 deletions

View File

@ -9,6 +9,7 @@
#define WS_BUILD_DLL
#include <wireshark.h>
#include <wsutil/plugins.h>
#include <epan/packet.h>
#include <epan/proto.h>
@ -21,7 +22,7 @@ WS_DLL_PUBLIC_DEF const int plugin_want_major = WIRESHARK_VERSION_MAJOR;
WS_DLL_PUBLIC_DEF const int plugin_want_minor = WIRESHARK_VERSION_MINOR;
WS_DLL_PUBLIC void plugin_register(void);
WS_DLL_PUBLIC int plugin_describe(void);
static int proto_hello = -1;
static dissector_handle_t handle_hello;
@ -56,3 +57,9 @@ plugin_register(void)
plug.register_handoff = proto_reg_handoff_hello; /* or NULL */
proto_register_plugin(&plug);
}
int
plugin_describe(void)
{
return WS_PLUGIN_DESC_DISSECTOR;
}

View File

@ -102,8 +102,10 @@ Too many other protocol updates have been made to list them all here.
//_Non-empty section placeholder._
//=== Major API Changes
=== Major API Changes
* Plugins should provide a `plugin_describe()` function that returns an ORed
list of flags consisting of the plugin types used (declared in wsutil/plugins.h).
== Getting Wireshark

View File

@ -188,7 +188,7 @@ compare_tools(gconstpointer a, gconstpointer b)
}
void
extcap_get_descriptions(plugin_description_callback callback, void *callback_data)
extcap_get_descriptions(extcap_plugin_description_callback callback, void *callback_data)
{
extcap_ensure_all_interfaces_loaded();

View File

@ -62,6 +62,10 @@ typedef enum {
EXTCAP_FILTER_INVALID
} extcap_filter_status;
typedef void (*extcap_plugin_description_callback)(const char *, const char *,
const char *, const char *,
void *);
struct _extcap_arg;
#ifdef __cplusplus
@ -134,7 +138,7 @@ extcap_clear_interfaces(void);
* @param callback_data Data to be passed to the callback routine.
*/
void
extcap_get_descriptions(plugin_description_callback callback, void *callback_data);
extcap_get_descriptions(extcap_plugin_description_callback callback, void *callback_data);
/**
* Print information about all available extcap executables.

View File

@ -116,6 +116,7 @@ reg_code += """
/* plugins are DLLs on Windows */
#define WS_BUILD_DLL
#include "ws_symbol_export.h"
#include <wsutil/plugins.h>
"""
@ -139,16 +140,29 @@ for symbol in regs['codec_register']:
for symbol in regs['register_tap_listener']:
reg_code += "void register_tap_listener_%s(void);\n" % (symbol)
DESCRIPTION_FLAG = {
'plugin': 'WS_PLUGIN_DESC_DISSECTOR',
'plugin_wtap': 'WS_PLUGIN_DESC_FILE_TYPE',
'plugin_codec': 'WS_PLUGIN_DESC_CODEC',
'plugin_tap': 'WS_PLUGIN_DESC_TAP_LISTENER'
}
reg_code += """
WS_DLL_PUBLIC_DEF const gchar plugin_version[] = PLUGIN_VERSION;
WS_DLL_PUBLIC_DEF const int plugin_want_major = VERSION_MAJOR;
WS_DLL_PUBLIC_DEF const int plugin_want_minor = VERSION_MINOR;
WS_DLL_PUBLIC void plugin_register(void);
WS_DLL_PUBLIC int plugin_describe(void);
int plugin_describe(void)
{
return %s;
}
void plugin_register(void)
{
"""
""" % DESCRIPTION_FLAG[registertype]
if registertype == "plugin":
for symbol in regs['proto_reg']:

View File

@ -100,7 +100,31 @@ QStringList AuthorListModel::headerColumns() const
return QStringList() << tr("Name") << tr("Email");
}
#ifdef HAVE_PLUGINS
static void plugins_add_description(const char *name, const char *version,
int desc_flags, const char *filename,
void *user_data)
{
QList<QStringList> *plugin_data = (QList<QStringList> *)user_data;
QStringList plugin_types;
if (desc_flags & WS_PLUGIN_DESC_DISSECTOR)
plugin_types << "dissector";
if (desc_flags & WS_PLUGIN_DESC_FILE_TYPE)
plugin_types << "file type";
if (desc_flags & WS_PLUGIN_DESC_CODEC)
plugin_types << "codec";
if (desc_flags & WS_PLUGIN_DESC_EPAN)
plugin_types << "epan";
if (desc_flags & WS_PLUGIN_DESC_TAP_LISTENER)
plugin_types << "tap listener";
if (plugin_types.empty())
plugin_types << "unknown";
QStringList plugin_row = QStringList() << name << version << plugin_types.join(", ") << filename;
*plugin_data << plugin_row;
}
#endif
static void other_plugins_add_description(const char *name, const char *version,
const char *types, const char *filename,
void *user_data)
{
@ -109,7 +133,7 @@ static void plugins_add_description(const char *name, const char *version,
*plugin_data << plugin_row;
}
PluginListModel::PluginListModel(QObject * parent) : AStringListListModel(parent)
PluginListModel::PluginListModel(QObject *parent) : AStringListListModel(parent)
{
QList<QStringList> plugin_data;
#ifdef HAVE_PLUGINS
@ -117,10 +141,10 @@ PluginListModel::PluginListModel(QObject * parent) : AStringListListModel(parent
#endif
#ifdef HAVE_LUA
wslua_plugins_get_descriptions(plugins_add_description, &plugin_data);
wslua_plugins_get_descriptions(other_plugins_add_description, &plugin_data);
#endif
extcap_get_descriptions(plugins_add_description, &plugin_data);
extcap_get_descriptions(other_plugins_add_description, &plugin_data);
typeNames_ << QString("");
foreach(QStringList row, plugin_data)

View File

@ -30,18 +30,13 @@ typedef struct _plugin {
GModule *handle; /* handle returned by g_module_open */
char *name; /* plugin name */
const char *version; /* plugin version */
const char *type_name; /* user-facing name (what it does). Should these be capitalized? */
int desc_flags; /* user-facing description flags (what it does) */
} plugin;
#define TYPE_DIR_EPAN "epan"
#define TYPE_DIR_WIRETAP "wiretap"
#define TYPE_DIR_CODECS "codecs"
#define TYPE_NAME_DISSECTOR "dissector"
#define TYPE_NAME_FILE_TYPE "file type"
#define TYPE_NAME_CODEC "codec"
static GSList *plugins_module_list = NULL;
@ -63,20 +58,22 @@ type_to_dir(plugin_type_e type)
}
static inline const char *
type_to_name(plugin_type_e type)
desc_flags_to_str(int type_flags)
{
switch (type) {
case WS_PLUGIN_EPAN:
return TYPE_NAME_DISSECTOR;
case WS_PLUGIN_WIRETAP:
return TYPE_NAME_FILE_TYPE;
case WS_PLUGIN_CODEC:
return TYPE_NAME_CODEC;
default:
ws_error("Unknown plugin type: %u. Aborting.", (unsigned) type);
break;
}
ws_assert_not_reached();
/* XXX: Allow joining multiple types? Our plugins only implement a
* single type but out in the wild this may not be true. */
if (type_flags & WS_PLUGIN_DESC_DISSECTOR)
return "dissector";
else if (type_flags & WS_PLUGIN_DESC_FILE_TYPE)
return "file type";
else if (type_flags & WS_PLUGIN_DESC_CODEC)
return "codec";
else if (type_flags & WS_PLUGIN_DESC_EPAN)
return "epan";
else if (type_flags & WS_PLUGIN_DESC_TAP_LISTENER)
return "tap listener";
else
return "unknown";
}
static void
@ -138,6 +135,7 @@ scan_plugins_dir(GHashTable *plugins_module, const char *dirpath, plugin_type_e
GModule *handle; /* handle returned by g_module_open */
void * symbol;
const char *plug_version;
int desc_flags;
plugin *new_plug;
if (append_type)
@ -206,11 +204,17 @@ DIAG_OFF_PEDANTIC
((plugin_register_func)symbol)();
DIAG_ON_PEDANTIC
/* Search for the (optional) description flag registration function */
if (g_module_symbol(handle, "plugin_describe", &symbol))
desc_flags = ((plugin_describe_func)symbol)();
else
desc_flags = 0;
new_plug = g_new(plugin, 1);
new_plug->handle = handle;
new_plug->name = g_strdup(name);
new_plug->version = plug_version;
new_plug->type_name = type_to_name(type);
new_plug->desc_flags = desc_flags;
/* Add it to the list of plugins. */
g_hash_table_replace(plugins_module, new_plug->name, new_plug);
@ -272,7 +276,7 @@ plugins_get_descriptions(plugin_description_callback callback, void *callback_da
for (unsigned i = 0; i < plugins_array->len; i++) {
plugin *plug = (plugin *)plugins_array->pdata[i];
callback(plug->name, plug->version, plug->type_name, g_module_name(plug->handle), callback_data);
callback(plug->name, plug->version, plug->desc_flags, g_module_name(plug->handle), callback_data);
}
g_ptr_array_free(plugins_array, true);
@ -280,10 +284,10 @@ plugins_get_descriptions(plugin_description_callback callback, void *callback_da
static void
print_plugin_description(const char *name, const char *version,
const char *description, const char *filename,
int desc_flags, const char *filename,
void *user_data _U_)
{
printf("%-16s\t%s\t%s\t%s\n", name, version, description, filename);
printf("%-16s\t%s\t%s\t%s\n", name, version, desc_flags_to_str(desc_flags), filename);
}
void

View File

@ -18,6 +18,7 @@ extern "C" {
#endif /* __cplusplus */
typedef void (*plugin_register_func)(void);
typedef int (*plugin_describe_func)(void);
typedef void plugins_t;
@ -27,10 +28,16 @@ typedef enum {
WS_PLUGIN_CODEC
} plugin_type_e;
#define WS_PLUGIN_DESC_DISSECTOR (1 << 0)
#define WS_PLUGIN_DESC_FILE_TYPE (1 << 1)
#define WS_PLUGIN_DESC_CODEC (1 << 2)
#define WS_PLUGIN_DESC_EPAN (1 << 3)
#define WS_PLUGIN_DESC_TAP_LISTENER (1 << 4)
WS_DLL_PUBLIC plugins_t *plugins_init(plugin_type_e type);
typedef void (*plugin_description_callback)(const char *name, const char *version,
const char *types, const char *filename,
int desc_flags, const char *filename,
void *user_data);
WS_DLL_PUBLIC void plugins_get_descriptions(plugin_description_callback callback, void *user_data);