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.
47 #ifdef HAVE_SYS_STAT_H
55 #include "filesystem.h"
56 #include "report_err.h"
58 /* linked list of all plugins */
61 #define PLUGINS_DIR_NAME "plugins"
64 * add a new plugin to the list
67 * - ENOMEM : memory allocation problem
68 * - EEXIST : the same plugin (i.e. name/version) was already registered.
71 add_plugin(void *handle, gchar *name, gchar *version,
72 void (*register_protoinfo)(void), void (*reg_handoff)(void),
73 void (*register_tap_listener)(void))
75 plugin *new_plug, *pt_plug;
77 pt_plug = plugin_list;
78 if (!pt_plug) /* the list is empty */
80 new_plug = (plugin *)g_malloc(sizeof(plugin));
81 if (new_plug == NULL) return ENOMEM;
82 plugin_list = new_plug;
88 /* check if the same name/version is already registered */
89 if (!strcmp(pt_plug->name, name) &&
90 !strcmp(pt_plug->version, version))
95 /* we found the last plugin in the list */
96 if (pt_plug->next == NULL) break;
98 pt_plug = pt_plug->next;
100 new_plug = (plugin *)g_malloc(sizeof(plugin));
101 if (new_plug == NULL) return ENOMEM;
102 pt_plug->next = new_plug;
105 new_plug->handle = handle;
106 new_plug->name = name;
107 new_plug->version = version;
108 new_plug->register_protoinfo = register_protoinfo;
109 new_plug->reg_handoff = reg_handoff;
110 new_plug->register_tap_listener = register_tap_listener;
111 new_plug->next = NULL;
116 * XXX - when we remove support for old-style plugins (which we should
117 * probably do eventually, as all plugins should be written as new-style
118 * ones), we may want to have "init_plugins()" merely save a pointer
119 * to the plugin's "init" routine, just as we save a pointer to its
120 * "reg_handoff" routine, and have a "register_all_plugins()" routine
121 * to go through the list of plugins and call all of them.
123 * Then we'd have "epan_init()", or perhaps even something higher up
124 * in the call tree, call "init_plugins()", and have "proto_init()"
125 * call "register_all_plugins()" right after calling "register_all_protocols()";
126 * this might be a bit cleaner.
129 plugins_scan_dir(const char *dirname)
131 #define FILENAME_LEN 1024
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 */
135 DIR *dir; /* scanned directory */
136 struct dirent *file; /* current file */
139 GDir *dir; /* scanned directory */
143 gchar filename[FILENAME_LEN]; /* current file name */
144 GModule *handle; /* handle returned by dlopen */
147 void (*register_protoinfo)(void);
148 void (*reg_handoff)(void);
149 void (*register_tap_listener)(void);
153 #if GLIB_MAJOR_VERSION < 2
155 * We find the extension used on this platform for loadable modules
156 * by the sneaky hack of calling "g_module_build_path" to build
157 * the pathname for a module with an empty directory name and
158 * empty module name, and then search for the last "." and use
159 * everything from the last "." on.
161 hack_path = g_module_build_path("", "");
162 lt_lib_ext = strrchr(hack_path, '.');
163 if (lt_lib_ext == NULL)
166 * Does this mean there *is* no extension? Assume so.
168 * XXX - the code below assumes that all loadable modules have
174 if ((dir = opendir(dirname)) != NULL)
176 while ((file = readdir(dir)) != NULL)
178 /* don't try to open "." and ".." */
179 if (!(strcmp(file->d_name, "..") &&
180 strcmp(file->d_name, "."))) continue;
182 /* skip anything but files with lt_lib_ext */
183 dot = strrchr(file->d_name, '.');
184 if (dot == NULL || strcmp(dot, lt_lib_ext) != 0) continue;
186 g_snprintf(filename, FILENAME_LEN, "%s" G_DIR_SEPARATOR_S "%s",
187 dirname, file->d_name);
188 name = (gchar *)file->d_name;
191 * GLib 2.x defines G_MODULE_SUFFIX as the extension used on this
192 * platform for loadable modules.
194 dummy = g_malloc(sizeof(GError *));
196 if ((dir = g_dir_open(dirname, 0, dummy)) != NULL)
198 while ((name = g_dir_read_name(dir)) != NULL)
200 /* skip anything but files with G_MODULE_SUFFIX */
201 dot = strrchr(name, '.');
202 if (dot == NULL || strcmp(dot+1, G_MODULE_SUFFIX) != 0) continue;
204 g_snprintf(filename, FILENAME_LEN, "%s" G_DIR_SEPARATOR_S "%s",
207 if ((handle = g_module_open(filename, 0)) == NULL)
209 report_failure("Couldn't load module %s: %s", filename,
213 if (!g_module_symbol(handle, "version", &gp))
215 report_failure("The plugin %s has no version symbol", name);
216 g_module_close(handle);
222 * Do we have a register routine?
224 if (g_module_symbol(handle, "plugin_register", &gp))
227 * Yes - this plugin includes one or more dissectors.
229 register_protoinfo = gp;
234 * No - no dissectors.
236 register_protoinfo = NULL;
240 * Do we have a reg_handoff routine?
242 if (g_module_symbol(handle, "plugin_reg_handoff", &gp))
252 * No - that's OK even if we have dissectors, as long
253 * as the plugin registers by name *and* there's
254 * a caller looking for that name.
260 * Do we have a register_tap_listener routine?
262 if (g_module_symbol(handle, "plugin_register_tap_listener", &gp))
265 * Yes - this plugin includes one or more taps.
267 register_tap_listener = gp;
274 register_tap_listener = NULL;
278 * Do we have an old-style init routine?
280 if (g_module_symbol(handle, "plugin_init", &gp))
283 * Yes - do we also have a register routine or a
284 * register_tap_listener routine? If so, this is a bogus
285 * hybrid of an old-style and new-style plugin.
287 if (register_protoinfo != NULL || register_tap_listener != NULL)
289 report_failure("The plugin %s has an old plugin init routine\nand a new register or register_tap_listener routine.",
291 g_module_close(handle);
296 * It's just an unsupported old-style plugin;
298 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",
300 g_module_close(handle);
305 * Does this dissector do anything useful?
307 if (register_protoinfo == NULL &&
308 register_tap_listener == NULL)
313 report_failure("The plugin %s has neither a register routine, or a register_tap_listener routine",
315 g_module_close(handle);
320 * OK, attempt to add it to the list of plugins.
322 if ((cr = add_plugin(handle, g_strdup(name), version,
323 register_protoinfo, reg_handoff,
324 register_tap_listener)))
327 fprintf(stderr, "The plugin %s, version %s\n"
328 "was found in multiple directories\n", name, version);
330 fprintf(stderr, "Memory allocation problem\n"
331 "when processing plugin %s, version %s\n",
333 g_module_close(handle);
338 * Call its register routine if it has one.
339 * XXX - just save this and call it with the built-in
340 * dissector register routines?
342 if (register_protoinfo != NULL)
343 register_protoinfo();
346 #if GLIB_MAJOR_VERSION < 2
353 g_clear_error(dummy);
359 /* get the global plugin dir */
360 /* Return value is malloced so the caller should g_free() it. */
361 char *get_plugins_global_dir(const char *plugin_dir)
364 char *install_plugin_dir;
367 * On Windows, the data file directory is the installation
368 * directory; the plugins are stored under it.
370 * Assume we're running the installed version of Ethereal;
371 * on Windows, the data file directory is the directory
372 * in which the Ethereal binary resides.
374 install_plugin_dir = g_strdup_printf("%s\\plugins\\%s", get_datafile_dir(), VERSION);
377 * Make sure that pathname refers to a directory.
379 if (test_for_directory(install_plugin_dir) != EISDIR) {
381 * Either it doesn't refer to a directory or it
382 * refers to something that doesn't exist.
384 * Assume that means we're running, for example,
385 * a version of Ethereal we've built in a source
386 * directory, and fall back on the default
387 * installation directory, so you can put the plugins
388 * somewhere so they can be used with this version
391 * XXX - should we, instead, have the Windows build
392 * procedure create a subdirectory of the "plugins"
393 * source directory, and copy the plugin DLLs there,
394 * so that you use the plugins from the build tree?
396 g_free(install_plugin_dir);
398 g_strdup("C:\\Program Files\\Ethereal\\plugins\\" VERSION);
401 return install_plugin_dir;
404 * Scan the plugin directory.
406 return strdup(plugin_dir);
411 /* get the personal plugin dir */
412 /* Return value is malloced so the caller should g_free() it. */
413 char *get_plugins_pers_dir(void)
415 return get_persconffile_path(PLUGINS_DIR_NAME, FALSE);
422 init_plugins(const char *plugin_dir)
426 if (plugin_list == NULL) /* ensure init_plugins is only run once */
429 * Scan the global plugin directory.
431 datafile_dir = get_plugins_global_dir(plugin_dir);
432 plugins_scan_dir(datafile_dir);
433 g_free(datafile_dir);
436 * Scan the users plugin directory.
438 datafile_dir = get_plugins_pers_dir();
439 plugins_scan_dir(datafile_dir);
440 g_free(datafile_dir);
445 register_all_plugin_handoffs(void)
450 * For all plugins with register-handoff routines, call the routines.
451 * This is called from "proto_init()"; it must be called after
452 * "register_all_protocols()" and "init_plugins()" are called,
453 * in case one plugin registers itself either with a built-in
454 * dissector or with another plugin; we must first register all
455 * dissectors, whether built-in or plugin, so their dissector tables
456 * are initialized, and only then register all handoffs.
458 for (pt_plug = plugin_list; pt_plug != NULL; pt_plug = pt_plug->next)
460 if (pt_plug->reg_handoff)
461 (pt_plug->reg_handoff)();
466 register_all_plugin_tap_listeners(void)
471 * For all plugins with register-tap-listener routines, call the
474 for (pt_plug = plugin_list; pt_plug != NULL; pt_plug = pt_plug->next)
476 if (pt_plug->register_tap_listener)
477 (pt_plug->register_tap_listener)();