6 * Ethereal - Network traffic analyzer
7 * By Gerald Combs <gerald@ethereal.com>
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 "file_util.h"
53 #include "report_err.h"
55 /* linked list of all plugins */
58 #define PLUGINS_DIR_NAME "plugins"
61 * add a new plugin to the list
64 * - ENOMEM : memory allocation problem
65 * - EEXIST : the same plugin (i.e. name/version) was already registered.
68 add_plugin(void *handle, gchar *name, gchar *version,
69 void (*register_protoinfo)(void), void (*reg_handoff)(void),
70 void (*register_tap_listener)(void))
72 plugin *new_plug, *pt_plug;
74 pt_plug = plugin_list;
75 if (!pt_plug) /* the list is empty */
77 new_plug = (plugin *)g_malloc(sizeof(plugin));
78 if (new_plug == NULL) return ENOMEM;
79 plugin_list = new_plug;
85 /* check if the same name/version is already registered */
86 if (!strcmp(pt_plug->name, name) &&
87 !strcmp(pt_plug->version, version))
92 /* we found the last plugin in the list */
93 if (pt_plug->next == NULL) break;
95 pt_plug = pt_plug->next;
97 new_plug = (plugin *)g_malloc(sizeof(plugin));
98 if (new_plug == NULL) return ENOMEM;
99 pt_plug->next = new_plug;
102 new_plug->handle = handle;
103 new_plug->name = name;
104 new_plug->version = version;
105 new_plug->register_protoinfo = register_protoinfo;
106 new_plug->reg_handoff = reg_handoff;
107 new_plug->register_tap_listener = register_tap_listener;
108 new_plug->next = NULL;
113 * XXX - when we remove support for old-style plugins (which we should
114 * probably do eventually, as all plugins should be written as new-style
115 * ones), we may want to have "init_plugins()" merely save a pointer
116 * to the plugin's "init" routine, just as we save a pointer to its
117 * "reg_handoff" routine, and have a "register_all_plugins()" routine
118 * to go through the list of plugins and call all of them.
120 * Then we'd have "epan_init()", or perhaps even something higher up
121 * in the call tree, call "init_plugins()", and have "proto_init()"
122 * call "register_all_plugins()" right after calling "register_all_protocols()";
123 * this might be a bit cleaner.
126 plugins_scan_dir(const char *dirname)
128 #define FILENAME_LEN 1024
129 ETH_DIR *dir; /* scanned directory */
130 ETH_DIRENT *file; /* current file */
132 #if GLIB_MAJOR_VERSION < 2
133 gchar *hack_path; /* pathname used to construct lt_lib_ext */
134 gchar *lt_lib_ext; /* extension for loadable modules */
136 gchar filename[FILENAME_LEN]; /* current file name */
137 GModule *handle; /* handle returned by dlopen */
140 void (*register_protoinfo)(void);
141 void (*reg_handoff)(void);
142 void (*register_tap_listener)(void);
146 #if GLIB_MAJOR_VERSION < 2
148 * We find the extension used on this platform for loadable modules
149 * by the sneaky hack of calling "g_module_build_path" to build
150 * the pathname for a module with an empty directory name and
151 * empty module name, and then search for the last "." and use
152 * everything from the last "." on.
154 hack_path = g_module_build_path("", "");
155 lt_lib_ext = strrchr(hack_path, '.');
156 if (lt_lib_ext == NULL)
159 * Does this mean there *is* no extension? Assume so.
161 * XXX - the code below assumes that all loadable modules have
168 if ((dir = g_dir_open(dirname, 0, NULL)) != NULL)
170 while ((file = eth_dir_read_name(dir)) != NULL)
172 name = eth_dir_get_name(file);
173 #if GLIB_MAJOR_VERSION < 2
174 /* don't try to open "." and ".." */
175 if (!(strcmp(name, "..") &&
176 strcmp(name, "."))) continue;
178 /* skip anything but files with lt_lib_ext */
179 dot = strrchr(name, '.');
180 if (dot == NULL || strcmp(dot, lt_lib_ext) != 0) continue;
184 * GLib 2.x defines G_MODULE_SUFFIX as the extension used on this
185 * platform for loadable modules.
187 /* skip anything but files with G_MODULE_SUFFIX */
188 dot = strrchr(name, '.');
189 if (dot == NULL || strcmp(dot+1, G_MODULE_SUFFIX) != 0) continue;
192 g_snprintf(filename, FILENAME_LEN, "%s" G_DIR_SEPARATOR_S "%s",
194 if ((handle = g_module_open(filename, 0)) == NULL)
196 report_failure("Couldn't load module %s: %s", filename,
200 if (!g_module_symbol(handle, "version", &gp))
202 report_failure("The plugin %s has no version symbol", name);
203 g_module_close(handle);
209 * Do we have a register routine?
211 if (g_module_symbol(handle, "plugin_register", &gp))
214 * Yes - this plugin includes one or more dissectors.
216 register_protoinfo = gp;
221 * No - no dissectors.
223 register_protoinfo = NULL;
227 * Do we have a reg_handoff routine?
229 if (g_module_symbol(handle, "plugin_reg_handoff", &gp))
239 * No - that's OK even if we have dissectors, as long
240 * as the plugin registers by name *and* there's
241 * a caller looking for that name.
247 * Do we have a register_tap_listener routine?
249 if (g_module_symbol(handle, "plugin_register_tap_listener", &gp))
252 * Yes - this plugin includes one or more taps.
254 register_tap_listener = gp;
261 register_tap_listener = NULL;
265 * Do we have an old-style init routine?
267 if (g_module_symbol(handle, "plugin_init", &gp))
270 * Yes - do we also have a register routine or a
271 * register_tap_listener routine? If so, this is a bogus
272 * hybrid of an old-style and new-style plugin.
274 if (register_protoinfo != NULL || register_tap_listener != NULL)
276 report_failure("The plugin %s has an old plugin init routine\nand a new register or register_tap_listener routine.",
278 g_module_close(handle);
283 * It's just an unsupported old-style plugin;
285 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",
287 g_module_close(handle);
292 * Does this dissector do anything useful?
294 if (register_protoinfo == NULL &&
295 register_tap_listener == NULL)
300 report_failure("The plugin %s has neither a register routine, or a register_tap_listener routine",
302 g_module_close(handle);
307 * OK, attempt to add it to the list of plugins.
309 if ((cr = add_plugin(handle, g_strdup(name), version,
310 register_protoinfo, reg_handoff,
311 register_tap_listener)))
314 fprintf(stderr, "The plugin %s, version %s\n"
315 "was found in multiple directories\n", name, version);
317 fprintf(stderr, "Memory allocation problem\n"
318 "when processing plugin %s, version %s\n",
320 g_module_close(handle);
325 * Call its register routine if it has one.
326 * XXX - just save this and call it with the built-in
327 * dissector register routines?
329 if (register_protoinfo != NULL)
330 register_protoinfo();
335 #if GLIB_MAJOR_VERSION < 2
341 /* get the global plugin dir */
342 /* Return value is malloced so the caller should g_free() it. */
343 char *get_plugins_global_dir(const char *plugin_dir)
346 char *install_plugin_dir;
349 * On Windows, the data file directory is the installation
350 * directory; the plugins are stored under it.
352 * Assume we're running the installed version of Ethereal;
353 * on Windows, the data file directory is the directory
354 * in which the Ethereal binary resides.
356 install_plugin_dir = g_strdup_printf("%s\\plugins\\%s", get_datafile_dir(), VERSION);
359 * Make sure that pathname refers to a directory.
361 if (test_for_directory(install_plugin_dir) != EISDIR) {
363 * Either it doesn't refer to a directory or it
364 * refers to something that doesn't exist.
366 * Assume that means we're running, for example,
367 * a version of Ethereal we've built in a source
368 * directory, and fall back on the default
369 * installation directory, so you can put the plugins
370 * somewhere so they can be used with this version
373 * XXX - should we, instead, have the Windows build
374 * procedure create a subdirectory of the "plugins"
375 * source directory, and copy the plugin DLLs there,
376 * so that you use the plugins from the build tree?
378 g_free(install_plugin_dir);
380 g_strdup("C:\\Program Files\\Ethereal\\plugins\\" VERSION);
383 return install_plugin_dir;
386 * Scan the plugin directory.
388 return g_strdup(plugin_dir);
393 /* get the personal plugin dir */
394 /* Return value is malloced so the caller should g_free() it. */
395 char *get_plugins_pers_dir(void)
397 return get_persconffile_path(PLUGINS_DIR_NAME, FALSE);
404 init_plugins(const char *plugin_dir)
408 if (plugin_list == NULL) /* ensure init_plugins is only run once */
411 * Scan the global plugin directory.
413 datafile_dir = get_plugins_global_dir(plugin_dir);
414 plugins_scan_dir(datafile_dir);
415 g_free(datafile_dir);
418 * Scan the users plugin directory.
420 datafile_dir = get_plugins_pers_dir();
421 plugins_scan_dir(datafile_dir);
422 g_free(datafile_dir);
427 register_all_plugin_handoffs(void)
432 * For all plugins with register-handoff routines, call the routines.
433 * This is called from "proto_init()"; it must be called after
434 * "register_all_protocols()" and "init_plugins()" are called,
435 * in case one plugin registers itself either with a built-in
436 * dissector or with another plugin; we must first register all
437 * dissectors, whether built-in or plugin, so their dissector tables
438 * are initialized, and only then register all handoffs.
440 for (pt_plug = plugin_list; pt_plug != NULL; pt_plug = pt_plug->next)
442 if (pt_plug->reg_handoff)
443 (pt_plug->reg_handoff)();
448 register_all_plugin_tap_listeners(void)
453 * For all plugins with register-tap-listener routines, call the
456 for (pt_plug = plugin_list; pt_plug != NULL; pt_plug = pt_plug->next)
458 if (pt_plug->register_tap_listener)
459 (pt_plug->register_tap_listener)();