4 * $Id: plugins.c,v 1.6 2000/01/29 16:41:14 gram 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>
61 /* linked list of all plugins */
64 static gchar std_plug_dir[] = "/usr/lib/ethereal/plugins/0.8";
65 static gchar local_plug_dir[] = "/usr/local/lib/ethereal/plugins/0.8";
66 static gchar *user_plug_dir = NULL;
67 static gchar *plugin_status_file = NULL;
70 * add a new plugin to the list
73 * - ENOMEM : memory allocation problem
74 * - EEXIST : the same plugin (i.e. name/version) was already registered.
77 add_plugin(void *handle, gchar *name, gchar *version, gchar *protocol,
78 gchar *filter_string, dfilter *filter,
79 void (*dissector) (const u_char *,
84 plugin *new_plug, *pt_plug;
86 pt_plug = plugin_list;
87 if (!pt_plug) /* the list is empty */
89 new_plug = (plugin *)g_malloc(sizeof(plugin));
90 if (new_plug == NULL) return ENOMEM;
91 plugin_list = new_plug;
97 /* check if the same name/version is already registered */
98 if (!strcmp(pt_plug->name, name) &&
99 !strcmp(pt_plug->version, version))
104 /* we found the last plugin in the list */
105 if (pt_plug->next == NULL) break;
107 pt_plug = pt_plug->next;
109 new_plug = (plugin *)g_malloc(sizeof(plugin));
110 if (new_plug == NULL) return ENOMEM;
111 pt_plug->next = new_plug;
114 new_plug->handle = handle;
115 new_plug->name = name;
116 new_plug->version = version;
117 new_plug->enabled = FALSE;
118 new_plug->protocol = protocol;
119 new_plug->filter_string = g_strdup(filter_string);
120 new_plug->filter = filter;
121 new_plug->dissector = dissector;
122 new_plug->next = NULL;
128 * returns a pointer to the enabled plugin, or NULL if the plugin wasn't found
132 enable_plugin(const gchar *name, const gchar *version)
136 pt_plug = plugin_list;
139 if (!strcmp(pt_plug->name, name) && !strcmp(pt_plug->version, version))
141 pt_plug->enabled = TRUE;
144 pt_plug = pt_plug->next;
151 * returns a pointer to the disabled plugin, or NULL if the plugin wasn't found
155 disable_plugin(const gchar *name, const gchar *version)
159 pt_plug = plugin_list;
162 if (!strcmp(pt_plug->name, name) && !strcmp(pt_plug->version, version))
164 pt_plug->enabled = FALSE;
167 pt_plug = pt_plug->next;
173 * find a plugin using its name/version
176 find_plugin(const gchar *name, const gchar *version)
180 pt_plug = plugin_list;
183 if (!strcmp(pt_plug->name, name) && !strcmp(pt_plug->version, version))
187 pt_plug = pt_plug->next;
193 * check if a plugin is enabled
196 is_enabled(const gchar *name, const gchar *version)
200 pt_plug = plugin_list;
203 if (!strcmp(pt_plug->name, name) && !strcmp(pt_plug->version, version))
204 return pt_plug->enabled;
205 pt_plug = pt_plug->next;
211 * replace the filter used by a plugin (filter string and dfilter)
214 plugin_replace_filter(const gchar *name, const gchar *version,
215 const gchar *filter_string, dfilter *filter)
219 pt_plug = plugin_list;
222 if (!strcmp(pt_plug->name, name) && !strcmp(pt_plug->version, version))
224 g_free(pt_plug->filter_string);
225 pt_plug->filter_string = g_strdup(filter_string);
226 dfilter_destroy(pt_plug->filter);
227 pt_plug->filter = filter;
230 pt_plug = pt_plug->next;
235 * save plugin status, returns 0 on success, -1 on failure:
237 * for each plugin, two lines are saved :
238 * plugin_name plugin_version [0|1] (0: disabled, 1: enabled)
252 if (!plugin_status_file) {
253 plugin_status_file = (gchar *)g_malloc(strlen(get_home_dir()) + 26);
254 sprintf(plugin_status_file, "%s/%s/plugins.status", get_home_dir(), PF_DIR);
256 statusfile=fopen(plugin_status_file, "w");
258 pf_path = g_malloc(strlen(get_home_dir()) + strlen(PF_DIR) + 2);
259 sprintf(pf_path, "%s/%s", get_home_dir(), PF_DIR);
263 mkdir(pf_path, 0755);
266 statusfile=fopen(plugin_status_file, "w");
267 if (!statusfile) return -1;
270 pt_plug = plugin_list;
273 fprintf(statusfile,"%s %s %s\n%s\n", pt_plug->name, pt_plug->version,
274 (pt_plug->enabled ? "1" : "0"), pt_plug->filter_string);
275 pt_plug = pt_plug->next;
282 * Check if the status of this plugin has been saved.
283 * If necessary, enable the plugin, and change the filter.
286 check_plugin_status(gchar *name, gchar *version, GModule *handle,
287 gchar *filter_string, FILE *statusfile)
290 guint16 ref_string_len;
292 void (*proto_init)();
295 if (!statusfile) return;
297 ref_string = (gchar *)g_malloc(strlen(name) + strlen(version) + 2);
298 ref_string_len = sprintf(ref_string, "%s %s", name, version);
300 while (!feof(statusfile))
302 if (fgets(line, 512, statusfile) == NULL) return;
303 if (strncmp(line, ref_string, ref_string_len) != 0) { /* not the right plugin */
304 if (fgets(line, 512, statusfile) == NULL) return;
306 else { /* found the plugin */
307 if (line[ref_string_len+1] == '1') {
308 enable_plugin(name, version);
309 if (g_module_symbol(handle, "proto_init", (gpointer*)&proto_init) == TRUE) {
314 if (fgets(line, 512, statusfile) == NULL) return;
315 if (line[strlen(line)-1] == '\n') line[strlen(line)-1] = '\0';
316 /* only compile the new filter if it is different from the default */
317 if (strcmp(line, filter_string) && dfilter_compile(line, &filter) == 0)
318 plugin_replace_filter(name, version, line, filter);
326 plugins_scan_dir(const char *dirname)
328 DIR *dir; /* scanned directory */
329 struct dirent *file; /* current file */
330 gchar filename[512]; /* current file name */
331 GModule *handle; /* handle returned by dlopen */
335 gchar *filter_string;
337 dfilter *filter = NULL;
338 void (*dissector) (const u_char *, int, frame_data *, proto_tree *);
343 #define LT_LIB_EXT ".dll"
345 #define LT_LIB_EXT ".la"
348 if (!plugin_status_file)
350 plugin_status_file = (gchar *)g_malloc(strlen(get_home_dir()) + 26);
351 sprintf(plugin_status_file, "%s/%s/plugins.status", get_home_dir(), PF_DIR);
353 statusfile = fopen(plugin_status_file, "r");
355 if ((dir = opendir(dirname)) != NULL)
357 while ((file = readdir(dir)) != NULL)
359 /* don't try to open "." and ".." */
360 if (!(strcmp(file->d_name, "..") &&
361 strcmp(file->d_name, "."))) continue;
363 /* skip anything but .la */
364 dot = strrchr(file->d_name, '.');
365 if (dot == NULL || ! strcmp(dot, LT_LIB_EXT)) continue;
367 sprintf(filename, "%s/%s", dirname, file->d_name);
369 if ((handle = g_module_open(filename, 0)) == NULL) continue;
370 name = (gchar *)file->d_name;
371 if (g_module_symbol(handle, "version", (gpointer*)&version) == FALSE)
373 g_module_close(handle);
376 if (g_module_symbol(handle, "protocol", (gpointer*)&protocol) == FALSE)
378 g_module_close(handle);
381 if (g_module_symbol(handle, "filter_string", (gpointer*)&filter_string) == FALSE)
383 g_module_close(handle);
386 if (dfilter_compile(filter_string, &filter) != 0) {
387 g_module_close(handle);
390 if (g_module_symbol(handle, "dissector", (gpointer*)&dissector) == FALSE) {
392 dfilter_destroy(filter);
393 g_module_close(handle);
397 if ((cr = add_plugin(handle, g_strdup(file->d_name), version,
398 protocol, filter_string, filter, dissector)))
401 fprintf(stderr, "The plugin : %s, version %s\n"
402 "was found in multiple directories\n", name, version);
404 fprintf(stderr, "Memory allocation problem\n"
405 "when processing plugin %s, version %sn",
408 dfilter_destroy(filter);
409 g_module_close(handle);
413 check_plugin_status(file->d_name, version, handle,
414 filter_string, statusfile);
420 if (statusfile) fclose(statusfile);
429 if (plugin_list == NULL) /* ensure init_plugins is only run once */
431 plugins_scan_dir(std_plug_dir);
432 plugins_scan_dir(local_plug_dir);
433 if ((strcmp(std_plug_dir, PLUGIN_DIR) != 0) &&
434 (strcmp(local_plug_dir, PLUGIN_DIR) != 0))
436 plugins_scan_dir(PLUGIN_DIR);
440 user_plug_dir = (gchar *)g_malloc(strlen(get_home_dir()) + 19);
441 sprintf(user_plug_dir, "%s/%s/plugins", get_home_dir(), PF_DIR);
443 plugins_scan_dir(user_plug_dir);