Implement plugins status save/restore :

Add a "Save status" button to the Plugins window which saves the status
(enabled/disabled) and the filter used by all the plugins in
~/.ethereal/plugins.status

Moved plugins_scan_dir() from gtk/plugins_dlg.c to plugins.c because it is
GUI independent. Read plugins.status in this function and restore the saved
status.

Add a init_plugins() function in plugins.c which calls plugins_scan_dir() in
order to build the plugin_list. It is called by ethereal_proto_init() in
gtk/main.c

svn path=/trunk/; revision=1417
This commit is contained in:
Olivier Abad 2000-01-04 20:37:18 +00:00
parent ef7014e92b
commit a04e67e0ab
4 changed files with 234 additions and 112 deletions

View File

@ -1,6 +1,6 @@
/* main.c
*
* $Id: main.c,v 1.80 2000/01/03 06:59:21 guy Exp $
* $Id: main.c,v 1.81 2000/01/04 20:37:16 oabad Exp $
*
* Ethereal - Network traffic analyzer
* By Gerald Combs <gerald@zing.org>
@ -104,6 +104,7 @@
#include "dfilter.h"
#include "keys.h"
#include "gtkglobals.h"
#include "plugins.h"
FILE *data_out_file = NULL;
packet_info pi;
@ -853,6 +854,7 @@ ethereal_proto_init(void) {
proto_init();
init_dissect_udp();
dfilter_init();
init_plugins();
}
static void

View File

@ -1,7 +1,7 @@
/* plugins_dlg.c
* Dialog boxes for plugins
*
* $Id: plugins_dlg.c,v 1.8 2000/01/03 20:18:25 oabad Exp $
* $Id: plugins_dlg.c,v 1.9 2000/01/04 20:37:18 oabad Exp $
*
* Ethereal - Network traffic analyzer
* By Gerald Combs <gerald@zing.org>
@ -31,10 +31,8 @@
#include <errno.h>
#include <sys/types.h>
#include <dirent.h>
#include <stdlib.h>
#include <string.h>
#include <dlfcn.h>
#include "globals.h"
#include "plugins.h"
@ -46,13 +44,10 @@ static gint selected_row;
static gchar *selected_name;
static gchar *selected_version;
static gchar *selected_enabled;
static gchar std_plug_dir[] = "/usr/lib/ethereal/plugins/0.8";
static gchar local_plug_dir[] = "/usr/local/lib/ethereal/plugins/0.8";
static gchar *user_plug_dir = NULL;
static void plugins_close_cb(GtkWidget *, gpointer);
static void plugins_save_cb(GtkWidget *, gpointer);
static void plugins_scan(GtkWidget *);
static void plugins_scan_dir(const char *);
static void plugins_clist_select_cb(GtkWidget *, gint, gint,
GdkEventButton *, gpointer);
static void plugins_clist_unselect_cb(GtkWidget *, gint, gint,
@ -79,6 +74,7 @@ tools_plugins_cmd_cb(GtkWidget *widget, gpointer data)
GtkWidget *filter_bn;
GtkWidget *main_hbnbox;
GtkWidget *close_bn;
GtkWidget *save_bn;
gchar *titles[] = {"Name", "Description", "Version", "Enabled"};
plugins_window = gtk_window_new(GTK_WINDOW_TOPLEVEL);
@ -125,7 +121,7 @@ tools_plugins_cmd_cb(GtkWidget *widget, gpointer data)
frame_vbnbox = gtk_vbutton_box_new();
gtk_box_pack_start(GTK_BOX(frame_hbox), frame_vbnbox, FALSE, TRUE, 0);
gtk_container_set_border_width(GTK_CONTAINER(frame_vbnbox), 20);
gtk_button_box_set_layout(GTK_BUTTON_BOX(frame_vbnbox), GTK_BUTTONBOX_START);
gtk_button_box_set_layout(GTK_BUTTON_BOX(frame_vbnbox), GTK_BUTTONBOX_SPREAD);
gtk_widget_show(frame_vbnbox);
enable_bn = gtk_button_new_with_label("Enable");
@ -147,6 +143,7 @@ tools_plugins_cmd_cb(GtkWidget *widget, gpointer data)
main_hbnbox = gtk_hbutton_box_new();
gtk_box_pack_start(GTK_BOX(main_vbox), main_hbnbox, FALSE, TRUE, 0);
gtk_container_set_border_width(GTK_CONTAINER(main_hbnbox), 10);
gtk_button_box_set_layout(GTK_BUTTON_BOX(main_hbnbox), GTK_BUTTONBOX_SPREAD);
gtk_widget_show(main_hbnbox);
close_bn = gtk_button_new_with_label("Close");
@ -155,6 +152,12 @@ tools_plugins_cmd_cb(GtkWidget *widget, gpointer data)
gtk_signal_connect(GTK_OBJECT(close_bn), "clicked",
GTK_SIGNAL_FUNC(plugins_close_cb), GTK_OBJECT(plugins_window));
save_bn = gtk_button_new_with_label("Save status");
gtk_container_add(GTK_CONTAINER(main_hbnbox), save_bn);
gtk_widget_show(save_bn);
gtk_signal_connect(GTK_OBJECT(save_bn), "clicked",
GTK_SIGNAL_FUNC(plugins_save_cb), GTK_OBJECT(plugins_window));
gtk_widget_show(plugins_window);
lt_dlinit();
@ -179,23 +182,6 @@ plugins_scan(GtkWidget *clist)
plugin *pt_plug;
gchar *plugent[4]; /* new entry added in clist */
if (plugin_list == NULL) /* first intialisation */
{
plugins_scan_dir(std_plug_dir);
plugins_scan_dir(local_plug_dir);
if ((strcmp(std_plug_dir, PLUGIN_DIR) != 0) &&
(strcmp(local_plug_dir, PLUGIN_DIR) != 0))
{
plugins_scan_dir(PLUGIN_DIR);
}
if (!user_plug_dir)
{
user_plug_dir = (gchar *)g_malloc(strlen(getenv("HOME")) + 19);
sprintf(user_plug_dir, "%s/.ethereal/plugins", getenv("HOME"));
}
plugins_scan_dir(user_plug_dir);
}
pt_plug = plugin_list;
while (pt_plug)
{
@ -208,87 +194,6 @@ plugins_scan(GtkWidget *clist)
}
}
static void
plugins_scan_dir(const char *dirname)
{
DIR *dir; /* scanned directory */
struct dirent *file; /* current file */
gchar filename[512]; /* current file name */
lt_dlhandle handle; /* handle returned by dlopen */
gchar *name;
gchar *version;
gchar *protocol;
gchar *filter_string;
gchar *dot;
dfilter *filter = NULL;
void (*dissector) (const u_char *, int, frame_data *, proto_tree *);
int cr;
#define LT_LIB_EXT ".la"
if ((dir = opendir(dirname)) != NULL)
{
while ((file = readdir(dir)) != NULL)
{
/* don't try to open "." and ".." */
if (!(strcmp(file->d_name, "..") &&
strcmp(file->d_name, "."))) continue;
/* skip anything but .la */
dot = strrchr(file->d_name, '.');
if (dot == NULL || ! strcmp(dot, LT_LIB_EXT)) continue;
sprintf(filename, "%s/%s", dirname, file->d_name);
if ((handle = lt_dlopen(filename)) == NULL) continue;
name = (gchar *)file->d_name;
if ((version = (gchar *)lt_dlsym(handle, "version")) == NULL)
{
dlclose(handle);
continue;
}
if ((protocol = (gchar *)lt_dlsym(handle, "protocol")) == NULL)
{
dlclose(handle);
continue;
}
if ((filter_string = (gchar *)lt_dlsym(handle, "filter_string")) == NULL)
{
dlclose(handle);
continue;
}
if (dfilter_compile(filter_string, &filter) != 0) {
dlclose(handle);
continue;
}
if ((dissector = (void (*)(const u_char *, int,
frame_data *,
proto_tree *)) lt_dlsym(handle, "dissector")) == NULL)
{
if (filter != NULL)
dfilter_destroy(filter);
dlclose(handle);
continue;
}
if ((cr = add_plugin(handle, g_strdup(file->d_name), version,
protocol, filter_string, filter, dissector)))
{
if (cr == EEXIST)
simple_dialog(ESD_TYPE_WARN, NULL, "The plugin : %s, version %s\n"
"was found in multiple directories", name, version);
else
simple_dialog(ESD_TYPE_WARN, NULL, "Memory allocation problem");
if (filter != NULL)
dfilter_destroy(filter);
dlclose(handle);
continue;
}
}
closedir(dir);
}
}
static void
plugins_close_cb(GtkWidget *close_bt, gpointer parent_w)
{
@ -296,6 +201,14 @@ plugins_close_cb(GtkWidget *close_bt, gpointer parent_w)
gtk_widget_destroy(GTK_WIDGET(parent_w));
}
static void
plugins_save_cb(GtkWidget *close_bt, gpointer parent_w)
{
if (save_plugin_status())
simple_dialog(ESD_TYPE_WARN, NULL, "Can't open ~/.ethereal/plugins.status\n"
"for writing");
}
void plugins_clist_select_cb(GtkWidget *clist, gint row, gint column,
GdkEventButton *event, gpointer data)
{
@ -419,7 +332,7 @@ plugins_filter_cb(GtkWidget *button, gpointer clist)
}
static void
filter_ok_cb(GtkWidget *close_bt, gpointer parent_w)
filter_ok_cb(GtkWidget *button, gpointer parent_w)
{
GtkWidget *filter_entry;
gchar *filter_string;
@ -442,14 +355,14 @@ filter_ok_cb(GtkWidget *close_bt, gpointer parent_w)
}
static void
filter_cancel_cb(GtkWidget *close_bt, gpointer parent_w)
filter_cancel_cb(GtkWidget *button, gpointer parent_w)
{
gtk_grab_remove(GTK_WIDGET(parent_w));
gtk_widget_destroy(GTK_WIDGET(parent_w));
}
static void
filter_default_cb(GtkWidget *close_bt, gpointer parent_w)
filter_default_cb(GtkWidget *button, gpointer parent_w)
{
GtkWidget *filter_entry;
gchar *filter_string;

207
plugins.c
View File

@ -1,7 +1,7 @@
/* plugins.c
* plugin routines
*
* $Id: plugins.c,v 1.2 1999/12/09 20:55:36 oabad Exp $
* $Id: plugins.c,v 1.3 2000/01/04 20:37:07 oabad Exp $
*
* Ethereal - Network traffic analyzer
* By Gerald Combs <gerald@zing.org>
@ -30,6 +30,8 @@
#ifdef HAVE_DLFCN_H
#include <time.h>
#include <dirent.h>
#include <string.h>
#include "globals.h"
@ -38,6 +40,11 @@
/* linked list of all plugins */
plugin *plugin_list;
static gchar std_plug_dir[] = "/usr/lib/ethereal/plugins/0.8";
static gchar local_plug_dir[] = "/usr/local/lib/ethereal/plugins/0.8";
static gchar *user_plug_dir = NULL;
static gchar *plugin_status_file = NULL;
/*
* add a new plugin to the list
* returns :
@ -203,4 +210,202 @@ plugin_replace_filter(const gchar *name, const gchar *version,
}
}
/*
* save plugin status, returns 0 on success, -1 on failure:
* file format :
* for each plugin, two lines are saved :
* plugin_name plugin_version [0|1] (0: disabled, 1: enabled)
* filter_string
*
* Ex :
* gryphon.so 0.8.0 1
* tcp.port == 7000
*/
int
save_plugin_status()
{
gchar *plugin_status_file;
FILE *statusfile;
plugin *pt_plug;
plugin_status_file = (gchar *)g_malloc(strlen(getenv("HOME")) + 26);
sprintf(plugin_status_file, "%s/.ethereal/plugins.status", getenv("HOME"));
statusfile=fopen(plugin_status_file, "w");
if (!statusfile) return -1;
pt_plug = plugin_list;
while (pt_plug)
{
fprintf(statusfile,"%s %s %s\n%s\n", pt_plug->name, pt_plug->version,
(pt_plug->enabled ? "1" : "0"), pt_plug->filter_string);
pt_plug = pt_plug->next;
}
fclose(statusfile);
return 0;
}
/*
* Check if the status of this plugin has been saved.
* If necessary, enable the plugin, and change the filter.
*/
static void
check_plugin_status(gchar *name, gchar *version, lt_dlhandle handle,
gchar *filter_string, FILE *statusfile)
{
gchar *ref_string;
guint16 ref_string_len;
gchar line[512];
void (*proto_init)();
dfilter *filter;
if (!statusfile) return;
ref_string = (gchar *)g_malloc(strlen(name) + strlen(version) + 2);
ref_string_len = sprintf(ref_string, "%s %s", name, version);
while (!feof(statusfile))
{
if (fgets(line, 512, statusfile) == NULL) return;
if (strncmp(line, ref_string, ref_string_len) != 0) { /* not the right plugin */
if (fgets(line, 512, statusfile) == NULL) return;
}
else { /* found the plugin */
if (line[ref_string_len+1] == '1') {
enable_plugin(name, version);
proto_init = (void (*)())lt_dlsym(handle, "proto_init");
if (proto_init)
proto_init();
}
if (fgets(line, 512, statusfile) == NULL) return;
if (line[strlen(line)-1] == '\n') line[strlen(line)-1] = '\0';
/* only compile the new filter if it is different from the default */
if (strcmp(line, filter_string) && dfilter_compile(line, &filter) == 0)
plugin_replace_filter(name, version, line, filter);
return;
}
}
}
static void
plugins_scan_dir(const char *dirname)
{
DIR *dir; /* scanned directory */
struct dirent *file; /* current file */
gchar filename[512]; /* current file name */
lt_dlhandle handle; /* handle returned by dlopen */
gchar *name;
gchar *version;
gchar *protocol;
gchar *filter_string;
gchar *dot;
dfilter *filter = NULL;
void (*dissector) (const u_char *, int, frame_data *, proto_tree *);
int cr;
FILE *statusfile;
#define LT_LIB_EXT ".la"
if (!plugin_status_file)
{
plugin_status_file = (gchar *)g_malloc(strlen(getenv("HOME")) + 26);
sprintf(plugin_status_file, "%s/.ethereal/plugins.status", getenv("HOME"));
}
statusfile = fopen(plugin_status_file, "r");
if ((dir = opendir(dirname)) != NULL)
{
while ((file = readdir(dir)) != NULL)
{
/* don't try to open "." and ".." */
if (!(strcmp(file->d_name, "..") &&
strcmp(file->d_name, "."))) continue;
/* skip anything but .la */
dot = strrchr(file->d_name, '.');
if (dot == NULL || ! strcmp(dot, LT_LIB_EXT)) continue;
sprintf(filename, "%s/%s", dirname, file->d_name);
if ((handle = lt_dlopen(filename)) == NULL) continue;
name = (gchar *)file->d_name;
if ((version = (gchar *)lt_dlsym(handle, "version")) == NULL)
{
lt_dlclose(handle);
continue;
}
if ((protocol = (gchar *)lt_dlsym(handle, "protocol")) == NULL)
{
lt_dlclose(handle);
continue;
}
if ((filter_string = (gchar *)lt_dlsym(handle, "filter_string")) == NULL)
{
lt_dlclose(handle);
continue;
}
if (dfilter_compile(filter_string, &filter) != 0) {
lt_dlclose(handle);
continue;
}
if ((dissector = (void (*)(const u_char *, int,
frame_data *,
proto_tree *)) lt_dlsym(handle, "dissector")) == NULL)
{
if (filter != NULL)
dfilter_destroy(filter);
lt_dlclose(handle);
continue;
}
if ((cr = add_plugin(handle, g_strdup(file->d_name), version,
protocol, filter_string, filter, dissector)))
{
if (cr == EEXIST)
fprintf(stderr, "The plugin : %s, version %s\n"
"was found in multiple directories\n", name, version);
else
fprintf(stderr, "Memory allocation problem\n"
"when processing plugin %s, version %sn",
name, version);
if (filter != NULL)
dfilter_destroy(filter);
lt_dlclose(handle);
continue;
}
if (statusfile) {
check_plugin_status(file->d_name, version, handle,
filter_string, statusfile);
rewind(statusfile);
}
}
closedir(dir);
}
if (statusfile) fclose(statusfile);
}
/*
* init plugins
*/
void
init_plugins()
{
if (plugin_list == NULL) /* ensure init_plugins is only run once */
{
plugins_scan_dir(std_plug_dir);
plugins_scan_dir(local_plug_dir);
if ((strcmp(std_plug_dir, PLUGIN_DIR) != 0) &&
(strcmp(local_plug_dir, PLUGIN_DIR) != 0))
{
plugins_scan_dir(PLUGIN_DIR);
}
if (!user_plug_dir)
{
user_plug_dir = (gchar *)g_malloc(strlen(getenv("HOME")) + 19);
sprintf(user_plug_dir, "%s/.ethereal/plugins", getenv("HOME"));
}
plugins_scan_dir(user_plug_dir);
}
}
#endif

View File

@ -1,7 +1,7 @@
/* plugins.h
* definitions for plugins structures
*
* $Id: plugins.h,v 1.3 1999/12/26 22:37:19 gerald Exp $
* $Id: plugins.h,v 1.4 2000/01/04 20:37:07 oabad Exp $
*
* Ethereal - Network traffic analyzer
* By Gerald Combs <gerald@zing.org>
@ -52,5 +52,7 @@ void *disable_plugin(const gchar *, const gchar *);
void *find_plugin(const gchar *, const gchar *);
gboolean is_enabled(const gchar *, const gchar *);
void plugin_replace_filter(const gchar *, const gchar *, const gchar *, dfilter *);
int save_plugin_status();
void init_plugins();
#endif /* __PLUGINS_H__ */