6 * Wireshark - Network traffic analyzer
7 * By Gerald Combs <gerald@wireshark.org>
8 * Copyright 1999 Gerald Combs
10 * This program is free software; you can redistribute it and/or
11 * modify it under the terms of the GNU General Public License
12 * as published by the Free Software Foundation; either version 2
13 * of the License, or (at your option) any later version.
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU General Public License for more details.
20 * You should have received a copy of the GNU General Public License
21 * along with this program; if not, write to the Free Software
22 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
51 #include "filesystem.h"
52 #include "privileges.h"
53 #include <wiretap/file_util.h>
54 #include "report_err.h"
56 /* linked list of all plugins */
59 #define PLUGINS_DIR_NAME "plugins"
62 * add a new plugin to the list
65 * - ENOMEM : memory allocation problem
66 * - EEXIST : the same plugin (i.e. name/version) was already registered.
69 add_plugin(void *handle, gchar *name, gchar *version,
70 void (*register_protoinfo)(void), void (*reg_handoff)(void),
71 void (*register_tap_listener)(void))
73 plugin *new_plug, *pt_plug;
75 pt_plug = plugin_list;
76 if (!pt_plug) /* the list is empty */
78 new_plug = (plugin *)g_malloc(sizeof(plugin));
79 if (new_plug == NULL) return ENOMEM;
80 plugin_list = new_plug;
86 /* check if the same name/version is already registered */
87 if (!strcmp(pt_plug->name, name) &&
88 !strcmp(pt_plug->version, version))
93 /* we found the last plugin in the list */
94 if (pt_plug->next == NULL) break;
96 pt_plug = pt_plug->next;
98 new_plug = (plugin *)g_malloc(sizeof(plugin));
99 if (new_plug == NULL) return ENOMEM;
100 pt_plug->next = new_plug;
103 new_plug->handle = handle;
104 new_plug->name = name;
105 new_plug->version = version;
106 new_plug->register_protoinfo = register_protoinfo;
107 new_plug->reg_handoff = reg_handoff;
108 new_plug->register_tap_listener = register_tap_listener;
109 new_plug->next = NULL;
114 * XXX - when we remove support for old-style plugins (which we should
115 * probably do eventually, as all plugins should be written as new-style
116 * ones), we may want to have "init_plugins()" merely save a pointer
117 * to the plugin's "init" routine, just as we save a pointer to its
118 * "reg_handoff" routine, and have a "register_all_plugins()" routine
119 * to go through the list of plugins and call all of them.
121 * Then we'd have "epan_init()", or perhaps even something higher up
122 * in the call tree, call "init_plugins()", and have "proto_init()"
123 * call "register_all_plugins()" right after calling "register_all_protocols()";
124 * this might be a bit cleaner.
127 plugins_scan_dir(const char *dirname)
129 #define FILENAME_LEN 1024
130 ETH_DIR *dir; /* scanned directory */
131 ETH_DIRENT *file; /* current file */
133 #if GLIB_MAJOR_VERSION < 2
134 gchar *hack_path; /* pathname used to construct lt_lib_ext */
135 gchar *lt_lib_ext; /* extension for loadable modules */
137 gchar filename[FILENAME_LEN]; /* current file name */
138 GModule *handle; /* handle returned by dlopen */
141 void (*register_protoinfo)(void);
142 void (*reg_handoff)(void);
143 void (*register_tap_listener)(void);
147 #if GLIB_MAJOR_VERSION < 2
149 * We find the extension used on this platform for loadable modules
150 * by the sneaky hack of calling "g_module_build_path" to build
151 * the pathname for a module with an empty directory name and
152 * empty module name, and then search for the last "." and use
153 * everything from the last "." on.
155 hack_path = g_module_build_path("", "");
156 lt_lib_ext = strrchr(hack_path, '.');
157 if (lt_lib_ext == NULL)
160 * Does this mean there *is* no extension? Assume so.
162 * XXX - the code below assumes that all loadable modules have
169 if ((dir = eth_dir_open(dirname, 0, NULL)) != NULL)
171 while ((file = eth_dir_read_name(dir)) != NULL)
173 name = eth_dir_get_name(file);
174 #if GLIB_MAJOR_VERSION < 2
175 /* don't try to open "." and ".." */
176 if (!(strcmp(name, "..") &&
177 strcmp(name, "."))) continue;
179 /* skip anything but files with lt_lib_ext */
180 dot = strrchr(name, '.');
181 if (dot == NULL || strcmp(dot, lt_lib_ext) != 0) continue;
185 * GLib 2.x defines G_MODULE_SUFFIX as the extension used on this
186 * platform for loadable modules.
188 /* skip anything but files with G_MODULE_SUFFIX */
189 dot = strrchr(name, '.');
190 if (dot == NULL || strcmp(dot+1, G_MODULE_SUFFIX) != 0) continue;
193 g_snprintf(filename, FILENAME_LEN, "%s" G_DIR_SEPARATOR_S "%s",
195 if ((handle = g_module_open(filename, 0)) == NULL)
197 report_failure("Couldn't load module %s: %s", filename,
201 if (!g_module_symbol(handle, "version", &gp))
203 report_failure("The plugin %s has no version symbol", name);
204 g_module_close(handle);
210 * Do we have a register routine?
212 if (g_module_symbol(handle, "plugin_register", &gp))
215 * Yes - this plugin includes one or more dissectors.
217 register_protoinfo = gp;
222 * No - no dissectors.
224 register_protoinfo = NULL;
228 * Do we have a reg_handoff routine?
230 if (g_module_symbol(handle, "plugin_reg_handoff", &gp))
240 * No - that's OK even if we have dissectors, as long
241 * as the plugin registers by name *and* there's
242 * a caller looking for that name.
248 * Do we have a register_tap_listener routine?
250 if (g_module_symbol(handle, "plugin_register_tap_listener", &gp))
253 * Yes - this plugin includes one or more taps.
255 register_tap_listener = gp;
262 register_tap_listener = NULL;
266 * Do we have an old-style init routine?
268 if (g_module_symbol(handle, "plugin_init", &gp))
271 * Yes - do we also have a register routine or a
272 * register_tap_listener routine? If so, this is a bogus
273 * hybrid of an old-style and new-style plugin.
275 if (register_protoinfo != NULL || register_tap_listener != NULL)
277 report_failure("The plugin %s has an old plugin init routine\nand a new register or register_tap_listener routine.",
279 g_module_close(handle);
284 * It's just an unsupported old-style plugin;
286 report_failure("The plugin %s has an old plugin init routine. Support has been dropped.\n Information on how to update your plugin is available at \nhttp://anonsvn.ethereal.com/ethereal/trunk/doc/README.plugins",
288 g_module_close(handle);
293 * Does this dissector do anything useful?
295 if (register_protoinfo == NULL &&
296 register_tap_listener == NULL)
301 report_failure("The plugin %s has neither a register routine, or a register_tap_listener routine",
303 g_module_close(handle);
308 * OK, attempt to add it to the list of plugins.
310 if ((cr = add_plugin(handle, g_strdup(name), version,
311 register_protoinfo, reg_handoff,
312 register_tap_listener)))
315 fprintf(stderr, "The plugin %s, version %s\n"
316 "was found in multiple directories\n", name, version);
318 fprintf(stderr, "Memory allocation problem\n"
319 "when processing plugin %s, version %s\n",
321 g_module_close(handle);
326 * Call its register routine if it has one.
327 * XXX - just save this and call it with the built-in
328 * dissector register routines?
330 if (register_protoinfo != NULL)
331 register_protoinfo();
336 #if GLIB_MAJOR_VERSION < 2
342 /* get the global plugin dir */
343 /* Return value is malloced so the caller should g_free() it. */
344 char *get_plugins_global_dir(const char *plugin_dir)
347 char *install_plugin_dir;
350 * On Windows, the data file directory is the installation
351 * directory; the plugins are stored under it.
353 * Assume we're running the installed version of Wireshark;
354 * on Windows, the data file directory is the directory
355 * in which the Ethereal binary resides.
357 install_plugin_dir = g_strdup_printf("%s\\plugins\\%s", get_datafile_dir(), VERSION);
360 * Make sure that pathname refers to a directory.
362 if (test_for_directory(install_plugin_dir) != EISDIR) {
364 * Either it doesn't refer to a directory or it
365 * refers to something that doesn't exist.
367 * Assume that means we're running, for example,
368 * a version of Wireshark we've built in a source
369 * directory, and fall back on the default
370 * installation directory, so you can put the plugins
371 * somewhere so they can be used with this version
374 * XXX - should we, instead, have the Windows build
375 * procedure create a subdirectory of the "plugins"
376 * source directory, and copy the plugin DLLs there,
377 * so that you use the plugins from the build tree?
379 g_free(install_plugin_dir);
381 g_strdup("C:\\Program Files\\Ethereal\\plugins\\" VERSION);
384 return install_plugin_dir;
387 * Scan the plugin directory.
389 return g_strdup(plugin_dir);
394 /* get the personal plugin dir */
395 /* Return value is malloced so the caller should g_free() it. */
396 char *get_plugins_pers_dir(void)
398 return get_persconffile_path(PLUGINS_DIR_NAME, FALSE);
405 init_plugins(const char *plugin_dir)
409 if (plugin_list == NULL) /* ensure init_plugins is only run once */
412 * Scan the global plugin directory.
414 datafile_dir = get_plugins_global_dir(plugin_dir);
415 plugins_scan_dir(datafile_dir);
416 g_free(datafile_dir);
419 * If the program wasn't started with special privileges,
420 * scan the users plugin directory. (Even if we relinquish
421 * them, plugins aren't safe unless we've *permanently*
422 * relinquished them, and we can't do that in Ethereal as,
423 * if we need privileges to start capturing, we'd need to
424 * reclaim them before each time we start capturing.)
426 if (!started_with_special_privs()) {
427 datafile_dir = get_plugins_pers_dir();
428 plugins_scan_dir(datafile_dir);
429 g_free(datafile_dir);
435 register_all_plugin_handoffs(void)
440 * For all plugins with register-handoff routines, call the routines.
441 * This is called from "proto_init()"; it must be called after
442 * "register_all_protocols()" and "init_plugins()" are called,
443 * in case one plugin registers itself either with a built-in
444 * dissector or with another plugin; we must first register all
445 * dissectors, whether built-in or plugin, so their dissector tables
446 * are initialized, and only then register all handoffs.
448 for (pt_plug = plugin_list; pt_plug != NULL; pt_plug = pt_plug->next)
450 if (pt_plug->reg_handoff)
451 (pt_plug->reg_handoff)();
456 register_all_plugin_tap_listeners(void)
461 * For all plugins with register-tap-listener routines, call the
464 for (pt_plug = plugin_list; pt_plug != NULL; pt_plug = pt_plug->next)
466 if (pt_plug->register_tap_listener)
467 (pt_plug->register_tap_listener)();