4 * $Id: plugins.c,v 1.15 2000/05/05 09:32:10 guy Exp $
6 * Ethereal - Network traffic analyzer
7 * By Gerald Combs <gerald@zing.org>
8 * Copyright 1999 Gerald Combs
11 * This program is free software; you can redistribute it and/or
12 * modify it under the terms of the GNU General Public License
13 * as published by the Free Software Foundation; either version 2
14 * of the License, or (at your option) any later version.
16 * This program is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 * GNU General Public License for more details.
21 * You should have received a copy of the GNU General Public License
22 * along with this program; if not, write to the Free Software
23 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
47 #include <sys/types.h>
60 #ifdef PLUGINS_NEED_ADDRESS_TABLE
61 #include "plugins/plugin_table.h"
62 plugin_address_table_t patable;
65 /* linked list of all plugins */
67 guint32 enabled_plugins_number;
70 static gchar std_plug_dir[] = "c:/program files/ethereal/plugins/0.8.7";
71 static gchar local_plug_dir[] = "c:/ethereal/plugins/0.8.7";
73 static gchar std_plug_dir[] = "/usr/lib/ethereal/plugins/0.8.7";
74 static gchar local_plug_dir[] = "/usr/local/lib/ethereal/plugins/0.8.7";
76 static gchar *user_plug_dir = NULL;
77 static gchar *plugin_status_file = NULL;
80 * add a new plugin to the list
83 * - ENOMEM : memory allocation problem
84 * - EEXIST : the same plugin (i.e. name/version) was already registered.
87 add_plugin(void *handle, gchar *name, gchar *version, gchar *protocol,
88 gchar *filter_string, dfilter *filter,
89 void (*dissector) (const u_char *,
94 plugin *new_plug, *pt_plug;
96 pt_plug = plugin_list;
97 if (!pt_plug) /* the list is empty */
99 new_plug = (plugin *)g_malloc(sizeof(plugin));
100 if (new_plug == NULL) return ENOMEM;
101 plugin_list = new_plug;
107 /* check if the same name/version is already registered */
108 if (!strcmp(pt_plug->name, name) &&
109 !strcmp(pt_plug->version, version))
114 /* we found the last plugin in the list */
115 if (pt_plug->next == NULL) break;
117 pt_plug = pt_plug->next;
119 new_plug = (plugin *)g_malloc(sizeof(plugin));
120 if (new_plug == NULL) return ENOMEM;
121 pt_plug->next = new_plug;
124 new_plug->handle = handle;
125 new_plug->name = name;
126 new_plug->version = version;
127 new_plug->enabled = FALSE;
128 new_plug->protocol = protocol;
129 new_plug->filter_string = g_strdup(filter_string);
130 new_plug->filter = filter;
131 new_plug->dissector = dissector;
132 new_plug->next = NULL;
138 * returns a pointer to the enabled plugin, or NULL if the plugin wasn't found
142 enable_plugin(const gchar *name, const gchar *version)
146 pt_plug = plugin_list;
149 if (!strcmp(pt_plug->name, name) && !strcmp(pt_plug->version, version))
151 pt_plug->enabled = TRUE;
152 enabled_plugins_number++;
155 pt_plug = pt_plug->next;
162 * returns a pointer to the disabled plugin, or NULL if the plugin wasn't found
166 disable_plugin(const gchar *name, const gchar *version)
170 pt_plug = plugin_list;
173 if (!strcmp(pt_plug->name, name) && !strcmp(pt_plug->version, version))
175 pt_plug->enabled = FALSE;
176 enabled_plugins_number--;
179 pt_plug = pt_plug->next;
185 * find a plugin using its name/version
188 find_plugin(const gchar *name, const gchar *version)
192 pt_plug = plugin_list;
195 if (!strcmp(pt_plug->name, name) && !strcmp(pt_plug->version, version))
199 pt_plug = pt_plug->next;
205 * check if a plugin is enabled
208 is_enabled(const gchar *name, const gchar *version)
212 pt_plug = plugin_list;
215 if (!strcmp(pt_plug->name, name) && !strcmp(pt_plug->version, version))
216 return pt_plug->enabled;
217 pt_plug = pt_plug->next;
223 * replace the filter used by a plugin (filter string and dfilter)
226 plugin_replace_filter(const gchar *name, const gchar *version,
227 const gchar *filter_string, dfilter *filter)
231 pt_plug = plugin_list;
234 if (!strcmp(pt_plug->name, name) && !strcmp(pt_plug->version, version))
236 g_free(pt_plug->filter_string);
237 pt_plug->filter_string = g_strdup(filter_string);
238 dfilter_destroy(pt_plug->filter);
239 pt_plug->filter = filter;
242 pt_plug = pt_plug->next;
247 * save plugin status, returns 0 on success, -1 on failure:
249 * for each plugin, two lines are saved :
250 * plugin_name plugin_version [0|1] (0: disabled, 1: enabled)
264 if (!plugin_status_file) {
265 plugin_status_file = (gchar *)g_malloc(strlen(get_home_dir()) + 26);
266 sprintf(plugin_status_file, "%s/%s/plugins.status", get_home_dir(), PF_DIR);
268 statusfile=fopen(plugin_status_file, "w");
270 pf_path = g_malloc(strlen(get_home_dir()) + strlen(PF_DIR) + 2);
271 sprintf(pf_path, "%s/%s", get_home_dir(), PF_DIR);
275 mkdir(pf_path, 0755);
278 statusfile=fopen(plugin_status_file, "w");
279 if (!statusfile) return -1;
282 pt_plug = plugin_list;
285 fprintf(statusfile,"%s %s %s\n%s\n", pt_plug->name, pt_plug->version,
286 (pt_plug->enabled ? "1" : "0"), pt_plug->filter_string);
287 pt_plug = pt_plug->next;
294 * Check if the status of this plugin has been saved.
295 * If necessary, enable the plugin, and change the filter.
298 check_plugin_status(gchar *name, gchar *version, GModule *handle,
299 gchar *filter_string, FILE *statusfile)
302 guint16 ref_string_len;
304 void (*plugin_init)(void*);
307 if (!statusfile) return;
309 ref_string = (gchar *)g_malloc(strlen(name) + strlen(version) + 2);
310 ref_string_len = sprintf(ref_string, "%s %s", name, version);
312 while (!feof(statusfile))
314 if (fgets(line, 512, statusfile) == NULL) return;
315 if (strncmp(line, ref_string, ref_string_len) != 0) { /* not the right plugin */
316 if (fgets(line, 512, statusfile) == NULL) return;
318 else { /* found the plugin */
319 if (line[ref_string_len+1] == '1') {
320 enable_plugin(name, version);
321 if (g_module_symbol(handle, "plugin_init", (gpointer*)&plugin_init) == TRUE) {
322 #ifdef PLUGINS_NEED_ADDRESS_TABLE
323 plugin_init(&patable);
328 #ifdef PLUGINS_NEED_ADDRESS_TABLE
335 if (fgets(line, 512, statusfile) == NULL) return;
336 if (line[strlen(line)-1] == '\n') line[strlen(line)-1] = '\0';
337 /* only compile the new filter if it is different from the default */
338 if (strcmp(line, filter_string) && dfilter_compile(line, &filter) == 0)
339 plugin_replace_filter(name, version, line, filter);
347 plugins_scan_dir(const char *dirname)
349 DIR *dir; /* scanned directory */
350 struct dirent *file; /* current file */
351 gchar filename[512]; /* current file name */
352 GModule *handle; /* handle returned by dlopen */
356 gchar *filter_string;
358 dfilter *filter = NULL;
359 void (*dissector) (const u_char *, int, frame_data *, proto_tree *);
364 #define LT_LIB_EXT ".dll"
366 #define LT_LIB_EXT ".so"
369 if (!plugin_status_file)
371 plugin_status_file = (gchar *)g_malloc(strlen(get_home_dir()) + 26);
372 sprintf(plugin_status_file, "%s/%s/plugins.status", get_home_dir(), PF_DIR);
374 statusfile = fopen(plugin_status_file, "r");
376 if ((dir = opendir(dirname)) != NULL)
378 while ((file = readdir(dir)) != NULL)
380 /* don't try to open "." and ".." */
381 if (!(strcmp(file->d_name, "..") &&
382 strcmp(file->d_name, "."))) continue;
384 /* skip anything but files with LT_LIB_EXT */
385 dot = strrchr(file->d_name, '.');
386 if (dot == NULL || strcmp(dot, LT_LIB_EXT) != 0) continue;
388 sprintf(filename, "%s/%s", dirname, file->d_name);
389 if ((handle = g_module_open(filename, 0)) == NULL) continue;
390 name = (gchar *)file->d_name;
391 if (g_module_symbol(handle, "version", (gpointer*)&version) == FALSE)
393 g_module_close(handle);
396 if (g_module_symbol(handle, "protocol", (gpointer*)&protocol) == FALSE)
398 g_module_close(handle);
401 if (g_module_symbol(handle, "filter_string", (gpointer*)&filter_string) == FALSE)
403 g_module_close(handle);
406 if (dfilter_compile(filter_string, &filter) != 0) {
407 g_module_close(handle);
410 if (g_module_symbol(handle, "dissector", (gpointer*)&dissector) == FALSE) {
412 dfilter_destroy(filter);
413 g_module_close(handle);
417 if ((cr = add_plugin(handle, g_strdup(file->d_name), version,
418 protocol, filter_string, filter, dissector)))
421 fprintf(stderr, "The plugin : %s, version %s\n"
422 "was found in multiple directories\n", name, version);
424 fprintf(stderr, "Memory allocation problem\n"
425 "when processing plugin %s, version %sn",
428 dfilter_destroy(filter);
429 g_module_close(handle);
433 check_plugin_status(file->d_name, version, handle,
434 filter_string, statusfile);
440 if (statusfile) fclose(statusfile);
449 struct stat std_dir_stat, local_dir_stat, plugin_dir_stat;
451 if (plugin_list == NULL) /* ensure init_plugins is only run once */
453 enabled_plugins_number = 0;
455 #ifdef PLUGINS_NEED_ADDRESS_TABLE
456 /* Intialize address table */
457 patable.p_check_col = check_col;
458 patable.p_col_add_fstr = col_add_fstr;
459 patable.p_col_append_fstr = col_append_str;
460 patable.p_col_add_str = col_add_str;
461 patable.p_col_append_str = col_append_str;
463 patable.p_dfilter_init = dfilter_init;
464 patable.p_dfilter_cleanup = dfilter_cleanup;
468 patable.p_proto_register_protocol = proto_register_protocol;
469 patable.p_proto_register_field_array = proto_register_field_array;
470 patable.p_proto_register_subtree_array = proto_register_subtree_array;
472 patable.p_dissector_add = dissector_add;
474 patable.p_heur_dissector_add = heur_dissector_add;
476 patable.p_proto_item_add_subtree = proto_item_add_subtree;
477 patable.p_proto_tree_add_item = proto_tree_add_item;
478 patable.p_proto_tree_add_item_hidden = proto_tree_add_item_hidden;
479 patable.p_proto_tree_add_protocol_format = proto_tree_add_protocol_format;
480 patable.p_proto_tree_add_bytes_format = proto_tree_add_bytes_format;
481 patable.p_proto_tree_add_time_format = proto_tree_add_time_format;
482 patable.p_proto_tree_add_ipxnet_format = proto_tree_add_ipxnet_format;
483 patable.p_proto_tree_add_ipv4_format = proto_tree_add_ipv4_format;
484 patable.p_proto_tree_add_ipv6_format = proto_tree_add_ipv6_format;
485 patable.p_proto_tree_add_ether_format = proto_tree_add_ether_format;
486 patable.p_proto_tree_add_string_format = proto_tree_add_string_format;
487 patable.p_proto_tree_add_boolean_format = proto_tree_add_boolean_format;
488 patable.p_proto_tree_add_uint_format = proto_tree_add_uint_format;
489 patable.p_proto_tree_add_text = proto_tree_add_text;
490 patable.p_proto_tree_add_notext = proto_tree_add_notext;
493 plugins_scan_dir(std_plug_dir);
494 plugins_scan_dir(local_plug_dir);
495 if ((strcmp(std_plug_dir, PLUGIN_DIR) != 0) &&
496 (strcmp(local_plug_dir, PLUGIN_DIR) != 0))
498 if (stat(PLUGIN_DIR, &plugin_dir_stat) == 0)
500 /* check if PLUGIN_DIR is really different from std_dir and
501 * local_dir if they exist ! */
502 if (stat(std_plug_dir, &std_dir_stat) == 0)
504 if (stat(local_plug_dir, &local_dir_stat) == 0)
506 if ((plugin_dir_stat.st_dev != std_dir_stat.st_dev ||
507 plugin_dir_stat.st_ino != std_dir_stat.st_ino) &&
508 (plugin_dir_stat.st_dev != local_dir_stat.st_dev ||
509 plugin_dir_stat.st_ino != local_dir_stat.st_ino))
510 plugins_scan_dir(PLUGIN_DIR);
514 if ((plugin_dir_stat.st_dev != std_dir_stat.st_dev ||
515 plugin_dir_stat.st_ino != std_dir_stat.st_ino))
516 plugins_scan_dir(PLUGIN_DIR);
519 else if (stat(local_plug_dir, &local_dir_stat) == 0)
521 if ((plugin_dir_stat.st_dev != local_dir_stat.st_dev ||
522 plugin_dir_stat.st_ino != local_dir_stat.st_ino))
523 plugins_scan_dir(PLUGIN_DIR);
525 else plugins_scan_dir(PLUGIN_DIR);
530 user_plug_dir = (gchar *)g_malloc(strlen(get_home_dir()) + 19);
531 sprintf(user_plug_dir, "%s/%s/plugins", get_home_dir(), PF_DIR);
533 plugins_scan_dir(user_plug_dir);