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.
29 #ifdef NEED_SNPRINTF_H
30 # include "snprintf.h"
51 #ifdef HAVE_SYS_STAT_H
59 #include "filesystem.h"
61 #ifdef PLUGINS_NEED_ADDRESS_TABLE
62 #include "conversation.h"
63 #include "reassemble.h"
64 #include <epan/prefs.h>
65 #include <epan/dissectors/packet-giop.h>
66 #include <epan/dissectors/packet-tpkt.h>
67 #include <epan/dissectors/packet-tcp.h>
68 #include <epan/dissectors/packet-rpc.h>
71 #include <epan/dissectors/packet-per.h>
72 #include <epan/dissectors/packet-ber.h>
73 #include <epan/dissectors/packet-rtp.h>
74 #include <epan/dissectors/packet-rtcp.h>
75 #include <epan/xdlc.h>
76 #include <epan/crc16.h>
77 #include "report_err.h"
78 #include "plugins/plugin_table.h"
79 static plugin_address_table_t patable = {
80 /* file generated by plugin_gen.py */
81 #include "plugins/Xass-list"
85 /* linked list of all plugins */
88 #define PLUGINS_DIR_NAME "plugins"
91 * add a new plugin to the list
94 * - ENOMEM : memory allocation problem
95 * - EEXIST : the same plugin (i.e. name/version) was already registered.
98 add_plugin(void *handle, gchar *name, gchar *version,
99 void (*reg_handoff)(void))
101 plugin *new_plug, *pt_plug;
103 pt_plug = plugin_list;
104 if (!pt_plug) /* the list is empty */
106 new_plug = (plugin *)g_malloc(sizeof(plugin));
107 if (new_plug == NULL) return ENOMEM;
108 plugin_list = new_plug;
114 /* check if the same name/version is already registered */
115 if (!strcmp(pt_plug->name, name) &&
116 !strcmp(pt_plug->version, version))
121 /* we found the last plugin in the list */
122 if (pt_plug->next == NULL) break;
124 pt_plug = pt_plug->next;
126 new_plug = (plugin *)g_malloc(sizeof(plugin));
127 if (new_plug == NULL) return ENOMEM;
128 pt_plug->next = new_plug;
131 new_plug->handle = handle;
132 new_plug->name = name;
133 new_plug->version = version;
134 new_plug->reg_handoff = reg_handoff;
135 new_plug->next = NULL;
140 * XXX - when we remove support for old-style plugins (which we should
141 * probably do eventually, as all plugins should be written as new-style
142 * ones), we may want to have "init_plugins()" merely save a pointer
143 * to the plugin's "init" routine, just as we save a pointer to its
144 * "reg_handoff" routine, and have a "register_all_plugins()" routine
145 * to go through the list of plugins and call all of them.
147 * Then we'd have "epan_init()", or perhaps even something higher up
148 * in the call tree, call "init_plugins()", and have "proto_init()"
149 * call "register_all_plugins()" right after calling "register_all_protocols()";
150 * this might be a bit cleaner.
153 plugins_scan_dir(const char *dirname)
155 #define FILENAME_LEN 1024
156 #if GLIB_MAJOR_VERSION < 2
157 gchar *hack_path; /* pathname used to construct lt_lib_ext */
158 gchar *lt_lib_ext; /* extension for loadable modules */
159 DIR *dir; /* scanned directory */
160 struct dirent *file; /* current file */
163 GDir *dir; /* scanned directory */
167 gchar filename[FILENAME_LEN]; /* current file name */
168 GModule *handle; /* handle returned by dlopen */
170 void (*init)(void *);
171 void (*reg_handoff)(void);
175 #if GLIB_MAJOR_VERSION < 2
177 * We find the extension used on this platform for loadable modules
178 * by the sneaky hack of calling "g_module_build_path" to build
179 * the pathname for a module with an empty directory name and
180 * empty module name, and then search for the last "." and use
181 * everything from the last "." on.
183 hack_path = g_module_build_path("", "");
184 lt_lib_ext = strrchr(hack_path, '.');
185 if (lt_lib_ext == NULL)
188 * Does this mean there *is* no extension? Assume so.
190 * XXX - the code below assumes that all loadable modules have
196 if ((dir = opendir(dirname)) != NULL)
198 while ((file = readdir(dir)) != NULL)
200 /* don't try to open "." and ".." */
201 if (!(strcmp(file->d_name, "..") &&
202 strcmp(file->d_name, "."))) continue;
204 /* skip anything but files with lt_lib_ext */
205 dot = strrchr(file->d_name, '.');
206 if (dot == NULL || strcmp(dot, lt_lib_ext) != 0) continue;
208 snprintf(filename, FILENAME_LEN, "%s" G_DIR_SEPARATOR_S "%s",
209 dirname, file->d_name);
210 name = (gchar *)file->d_name;
213 * GLib 2.x defines G_MODULE_SUFFIX as the extension used on this
214 * platform for loadable modules.
216 dummy = g_malloc(sizeof(GError *));
218 if ((dir = g_dir_open(dirname, 0, dummy)) != NULL)
220 while ((name = g_dir_read_name(dir)) != NULL)
222 /* skip anything but files with G_MODULE_SUFFIX */
223 dot = strrchr(name, '.');
224 if (dot == NULL || strcmp(dot+1, G_MODULE_SUFFIX) != 0) continue;
226 snprintf(filename, FILENAME_LEN, "%s" G_DIR_SEPARATOR_S "%s",
229 if ((handle = g_module_open(filename, 0)) == NULL)
231 g_warning("Couldn't load module %s: %s", filename,
235 if (g_module_symbol(handle, "version", (gpointer*)&version) == FALSE)
237 g_warning("The plugin %s has no version symbol", name);
238 g_module_close(handle);
243 * Old-style dissectors don't have a "plugin_reg_handoff()"
244 * routine; we no longer support them.
246 * New-style dissectors have one, because, otherwise, there's
247 * no way for them to arrange that they ever be called.
249 if (g_module_symbol(handle, "plugin_reg_handoff",
250 (gpointer*)®_handoff))
253 * We require it to have a "plugin_init()" routine.
255 if (!g_module_symbol(handle, "plugin_init", (gpointer*)&init))
257 g_warning("The plugin %s has a plugin_reg_handoff symbol but no plugin_init routine",
259 g_module_close(handle);
264 * We have a "plugin_reg_handoff()" routine, so we don't
265 * need the protocol, filter string, or dissector pointer.
267 if ((cr = add_plugin(handle, g_strdup(name), version,
271 fprintf(stderr, "The plugin %s, version %s\n"
272 "was found in multiple directories\n", name, version);
274 fprintf(stderr, "Memory allocation problem\n"
275 "when processing plugin %s, version %s\n",
277 g_module_close(handle);
282 * Call its init routine.
284 #ifdef PLUGINS_NEED_ADDRESS_TABLE
293 * This is an old-style dissector; warn that it won't
294 * be used, as those aren't supported.
297 "The plugin %s, version %s is an old-style plugin;\n"
298 "Those are no longer supported.\n", name, version);
301 #if GLIB_MAJOR_VERSION < 2
308 g_clear_error(dummy);
314 /* get the global plugin dir */
315 /* Return value is malloced so the caller should g_free() it. */
316 const char *get_plugins_global_dir(const char *plugin_dir)
319 char *install_plugin_dir;
322 * On Windows, the data file directory is the installation
323 * directory; the plugins are stored under it.
325 * Assume we're running the installed version of Ethereal;
326 * on Windows, the data file directory is the directory
327 * in which the Ethereal binary resides.
329 install_plugin_dir = g_strdup_printf("%s\\plugins\\%s", get_datafile_dir(), VERSION);
332 * Make sure that pathname refers to a directory.
334 if (test_for_directory(install_plugin_dir) != EISDIR) {
336 * Either it doesn't refer to a directory or it
337 * refers to something that doesn't exist.
339 * Assume that means we're running, for example,
340 * a version of Ethereal we've built in a source
341 * directory, and fall back on the default
342 * installation directory, so you can put the plugins
343 * somewhere so they can be used with this version
346 * XXX - should we, instead, have the Windows build
347 * procedure create a subdirectory of the "plugins"
348 * source directory, and copy the plugin DLLs there,
349 * so that you use the plugins from the build tree?
351 g_free(install_plugin_dir);
353 g_strdup("C:\\Program Files\\Ethereal\\plugins\\" VERSION);
356 return install_plugin_dir;
359 * Scan the plugin directory.
361 return strdup(plugin_dir);
366 /* get the personal plugin dir */
367 /* Return value is malloced so the caller should g_free() it. */
368 const char *get_plugins_pers_dir(void)
370 return get_persconffile_path(PLUGINS_DIR_NAME, FALSE);
377 init_plugins(const char *plugin_dir)
379 const char *datafile_dir;
381 if (plugin_list == NULL) /* ensure init_plugins is only run once */
384 * Scan the global plugin directory.
386 datafile_dir = get_plugins_global_dir(plugin_dir);
387 plugins_scan_dir(datafile_dir);
388 g_free((char *) datafile_dir);
391 * Scan the users plugin directory.
393 datafile_dir = get_plugins_pers_dir();
394 plugins_scan_dir(datafile_dir);
395 g_free((char *) datafile_dir);
400 register_all_plugin_handoffs(void)
405 * For all new-style plugins, call the register-handoff routine.
406 * This is called from "proto_init()"; it must be called after
407 * "register_all_protocols()" and "init_plugins()" are called,
408 * in case one plugin registers itself either with a built-in
409 * dissector or with another plugin; we must first register all
410 * dissectors, whether built-in or plugin, so their dissector tables
411 * are initialized, and only then register all handoffs.
413 * We treat those protocols as always being enabled; they should
414 * use the standard mechanism for enabling/disabling protocols, not
415 * the plugin-specific mechanism.
417 for (pt_plug = plugin_list; pt_plug != NULL; pt_plug = pt_plug->next)
418 (pt_plug->reg_handoff)();