4 * $Id: plugins.c,v 1.26 2001/08/18 23:21:31 guy Exp $
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.
48 #ifdef HAVE_SYS_STAT_H
52 #ifdef HAVE_SYS_TYPES_H
53 #include <sys/types.h>
60 #include "filesystem.h"
64 #ifdef PLUGINS_NEED_ADDRESS_TABLE
65 #include "packet-giop.h"
66 #include "plugins/plugin_table.h"
67 static plugin_address_table_t patable;
70 /* linked list of all plugins */
75 static gchar std_plug_dir[] = "C:\\Program Files\\Ethereal\\plugins\\" VERSION;
77 static gchar std_plug_dir[] = "/usr/lib/ethereal/plugins/" VERSION;
78 static gchar local_plug_dir[] = "/usr/local/lib/ethereal/plugins/" VERSION;
80 static gchar *user_plug_dir = NULL;
82 #define PLUGINS_DIR_NAME "plugins"
85 * add a new plugin to the list
88 * - ENOMEM : memory allocation problem
89 * - EEXIST : the same plugin (i.e. name/version) was already registered.
92 add_plugin(void *handle, gchar *name, gchar *version,
93 void (*reg_handoff)(void))
95 plugin *new_plug, *pt_plug;
97 pt_plug = plugin_list;
98 if (!pt_plug) /* the list is empty */
100 new_plug = (plugin *)g_malloc(sizeof(plugin));
101 if (new_plug == NULL) return ENOMEM;
102 plugin_list = new_plug;
108 /* check if the same name/version is already registered */
109 if (!strcmp(pt_plug->name, name) &&
110 !strcmp(pt_plug->version, version))
115 /* we found the last plugin in the list */
116 if (pt_plug->next == NULL) break;
118 pt_plug = pt_plug->next;
120 new_plug = (plugin *)g_malloc(sizeof(plugin));
121 if (new_plug == NULL) return ENOMEM;
122 pt_plug->next = new_plug;
125 new_plug->handle = handle;
126 new_plug->name = name;
127 new_plug->version = version;
128 new_plug->reg_handoff = reg_handoff;
129 new_plug->next = NULL;
134 * XXX - when we remove support for old-style plugins (which we should
135 * probably do eventually, as all plugins should be written as new-style
136 * ones), we may want to have "init_plugins()" merely save a pointer
137 * to the plugin's "init" routine, just as we save a pointer to its
138 * "reg_handoff" routine, and have a "register_all_plugins()" routine
139 * to go through the list of plugins and call all of them.
141 * Then we'd have "epan_init()", or perhaps even something higher up
142 * in the call tree, call "init_plugins()", and have "proto_init()"
143 * call "register_all_plugins()" right after calling "register_all_protocols()";
144 * this might be a bit cleaner.
147 plugins_scan_dir(const char *dirname)
149 #define FILENAME_LEN 1024
150 gchar *hack_path; /* pathname used to construct lt_lib_ext */
151 gchar *lt_lib_ext; /* extension for loadable modules */
152 DIR *dir; /* scanned directory */
153 struct dirent *file; /* current file */
154 gchar filename[FILENAME_LEN]; /* current file name */
155 GModule *handle; /* handle returned by dlopen */
158 void (*init)(void *);
159 void (*reg_handoff)(void);
164 * We find the extension used on this platform for loadable modules
165 * by the sneaky hack of calling "g_module_build_path" to build
166 * the pathname for a module with an empty directory name and
167 * empty module name, and then search for the last "." and use
168 * everything from the last "." on.
170 * GLib 2.0 will probably define G_MODULE_SUFFIX as the extension
171 * to use, but that's not checked into the GLib CVS tree yet,
172 * and we can't use it on systems that don't have GLib 2.0.
174 hack_path = g_module_build_path("", "");
175 lt_lib_ext = strrchr(hack_path, '.');
176 if (lt_lib_ext == NULL)
179 * Does this mean there *is* no extension? Assume so.
181 * XXX - the code below assumes that all loadable modules have
187 if ((dir = opendir(dirname)) != NULL)
189 while ((file = readdir(dir)) != NULL)
191 /* don't try to open "." and ".." */
192 if (!(strcmp(file->d_name, "..") &&
193 strcmp(file->d_name, "."))) continue;
195 /* skip anything but files with lt_lib_ext */
196 dot = strrchr(file->d_name, '.');
197 if (dot == NULL || strcmp(dot, lt_lib_ext) != 0) continue;
199 snprintf(filename, FILENAME_LEN, "%s" G_DIR_SEPARATOR_S "%s",
200 dirname, file->d_name);
201 if ((handle = g_module_open(filename, 0)) == NULL) continue;
202 name = (gchar *)file->d_name;
203 if (g_module_symbol(handle, "version", (gpointer*)&version) == FALSE)
205 g_warning("The plugin %s has no version symbol", name);
206 g_module_close(handle);
211 * Old-style dissectors don't have a "plugin_reg_handoff()"
212 * routine; we no longer support them.
214 * New-style dissectors have one, because, otherwise, there's
215 * no way for them to arrange that they ever be called.
217 if (g_module_symbol(handle, "plugin_reg_handoff",
218 (gpointer*)®_handoff))
221 * We require it to have a "plugin_init()" routine.
223 if (!g_module_symbol(handle, "plugin_init", (gpointer*)&init))
225 g_warning("The plugin %s has a plugin_reg_handoff symbol but no plugin_init routine", name);
226 g_module_close(handle);
231 * We have a "plugin_reg_handoff()" routine, so we don't
232 * need the protocol, filter string, or dissector pointer.
234 if ((cr = add_plugin(handle, g_strdup(file->d_name), version,
238 fprintf(stderr, "The plugin %s, version %s\n"
239 "was found in multiple directories\n", name, version);
241 fprintf(stderr, "Memory allocation problem\n"
242 "when processing plugin %s, version %s\n",
244 g_module_close(handle);
249 * Call its init routine.
251 #ifdef PLUGINS_NEED_ADDRESS_TABLE
260 * This is an old-style dissector; warn that it won't
261 * be used, as those aren't supported.
264 "The plugin %s, version %s is an old-style plugin;\n"
265 "Those are no longer supported.\n", name, version);
277 init_plugins(const char *plugin_dir)
280 char prog_pathname[_MAX_PATH+2];
282 size_t plugin_dir_len;
285 struct stat std_dir_stat, local_dir_stat, plugin_dir_stat;
288 if (plugin_list == NULL) /* ensure init_plugins is only run once */
290 #ifdef PLUGINS_NEED_ADDRESS_TABLE
291 /* Intialize address table */
292 patable.p_check_col = check_col;
293 patable.p_col_clear = col_clear;
294 patable.p_col_add_fstr = col_add_fstr;
295 patable.p_col_append_fstr = col_append_fstr;
296 patable.p_col_add_str = col_add_str;
297 patable.p_col_append_str = col_append_str;
298 patable.p_col_set_str = col_set_str;
302 patable.p_proto_register_protocol = proto_register_protocol;
303 patable.p_proto_register_field_array = proto_register_field_array;
304 patable.p_proto_register_subtree_array = proto_register_subtree_array;
306 patable.p_dissector_add = dissector_add;
307 patable.p_dissector_delete = dissector_delete;
309 patable.p_heur_dissector_add = heur_dissector_add;
311 patable.p_register_dissector = register_dissector;
312 patable.p_find_dissector = find_dissector;
313 patable.p_call_dissector = call_dissector;
315 patable.p_dissect_data = dissect_data;
317 patable.p_proto_is_protocol_enabled = proto_is_protocol_enabled;
319 patable.p_proto_item_get_len = proto_item_get_len;
320 patable.p_proto_item_set_len = proto_item_set_len;
321 patable.p_proto_item_set_text = proto_item_set_text;
322 patable.p_proto_item_add_subtree = proto_item_add_subtree;
323 patable.p_proto_tree_add_item = proto_tree_add_item;
324 patable.p_proto_tree_add_item_hidden = proto_tree_add_item_hidden;
325 patable.p_proto_tree_add_protocol_format = proto_tree_add_protocol_format;
326 patable.p_proto_tree_add_bytes = proto_tree_add_bytes;
327 patable.p_proto_tree_add_bytes_hidden = proto_tree_add_bytes_hidden;
328 patable.p_proto_tree_add_bytes_format = proto_tree_add_bytes_format;
329 patable.p_proto_tree_add_time = proto_tree_add_time;
330 patable.p_proto_tree_add_time_hidden = proto_tree_add_time_hidden;
331 patable.p_proto_tree_add_time_format = proto_tree_add_time_format;
332 patable.p_proto_tree_add_ipxnet = proto_tree_add_ipxnet;
333 patable.p_proto_tree_add_ipxnet_hidden = proto_tree_add_ipxnet_hidden;
334 patable.p_proto_tree_add_ipxnet_format = proto_tree_add_ipxnet_format;
335 patable.p_proto_tree_add_ipv4 = proto_tree_add_ipv4;
336 patable.p_proto_tree_add_ipv4_hidden = proto_tree_add_ipv4_hidden;
337 patable.p_proto_tree_add_ipv4_format = proto_tree_add_ipv4_format;
338 patable.p_proto_tree_add_ipv6 = proto_tree_add_ipv6;
339 patable.p_proto_tree_add_ipv6_hidden = proto_tree_add_ipv6_hidden;
340 patable.p_proto_tree_add_ipv6_format = proto_tree_add_ipv6_format;
341 patable.p_proto_tree_add_ether = proto_tree_add_ether;
342 patable.p_proto_tree_add_ether_hidden = proto_tree_add_ether_hidden;
343 patable.p_proto_tree_add_ether_format = proto_tree_add_ether_format;
344 patable.p_proto_tree_add_string = proto_tree_add_string;
345 patable.p_proto_tree_add_string_hidden = proto_tree_add_string_hidden;
346 patable.p_proto_tree_add_string_format = proto_tree_add_string_format;
347 patable.p_proto_tree_add_boolean = proto_tree_add_boolean;
348 patable.p_proto_tree_add_boolean_hidden = proto_tree_add_boolean_hidden;
349 patable.p_proto_tree_add_boolean_format = proto_tree_add_boolean_format;
350 patable.p_proto_tree_add_double = proto_tree_add_double;
351 patable.p_proto_tree_add_double_hidden = proto_tree_add_double_hidden;
352 patable.p_proto_tree_add_double_format = proto_tree_add_double_format;
353 patable.p_proto_tree_add_uint = proto_tree_add_uint;
354 patable.p_proto_tree_add_uint_hidden = proto_tree_add_uint_hidden;
355 patable.p_proto_tree_add_uint_format = proto_tree_add_uint_format;
356 patable.p_proto_tree_add_int = proto_tree_add_int;
357 patable.p_proto_tree_add_int_hidden = proto_tree_add_int_hidden;
358 patable.p_proto_tree_add_int_format = proto_tree_add_int_format;
359 patable.p_proto_tree_add_text = proto_tree_add_text;
360 patable.p_proto_tree_add_notext = proto_tree_add_notext;
362 patable.p_tvb_new_subset = tvb_new_subset;
364 patable.p_tvb_length = tvb_length;
365 patable.p_tvb_length_remaining = tvb_length_remaining;
366 patable.p_tvb_bytes_exist = tvb_bytes_exist;
367 patable.p_tvb_offset_exists = tvb_offset_exists;
368 patable.p_tvb_reported_length = tvb_reported_length;
369 patable.p_tvb_reported_length_remaining = tvb_reported_length_remaining;
371 patable.p_tvb_get_guint8 = tvb_get_guint8;
373 patable.p_tvb_get_ntohs = tvb_get_ntohs;
374 patable.p_tvb_get_ntoh24 = tvb_get_ntoh24;
375 patable.p_tvb_get_ntohl = tvb_get_ntohl;
377 patable.p_tvb_get_ntohll = tvb_get_ntohll;
380 patable.p_tvb_get_letohs = tvb_get_letohs;
381 patable.p_tvb_get_letoh24 = tvb_get_letoh24;
382 patable.p_tvb_get_letohl = tvb_get_letohl;
384 patable.p_tvb_get_letohll = tvb_get_letohll;
387 patable.p_tvb_memcpy = tvb_memcpy;
388 patable.p_tvb_memdup = tvb_memdup;
390 patable.p_tvb_get_ptr = tvb_get_ptr;
392 patable.p_tvb_find_guint8 = tvb_find_guint8;
393 patable.p_tvb_pbrk_guint8 = tvb_pbrk_guint8;
395 patable.p_tvb_strnlen = tvb_strnlen;
397 patable.p_tvb_format_text = tvb_format_text;
399 patable.p_tvb_get_nstringz = tvb_get_nstringz;
400 patable.p_tvb_get_nstringz0 = tvb_get_nstringz0;
402 patable.p_tvb_find_line_end = tvb_find_line_end;
403 patable.p_tvb_find_line_end_unquoted = tvb_find_line_end_unquoted;
405 patable.p_tvb_strneql = tvb_strneql;
406 patable.p_tvb_strncaseeql = tvb_strncaseeql;
408 patable.p_tvb_bytes_to_str = tvb_bytes_to_str;
410 patable.p_prefs_register_protocol = prefs_register_protocol;
411 patable.p_prefs_register_uint_preference = prefs_register_uint_preference;
412 patable.p_prefs_register_bool_preference = prefs_register_bool_preference;
413 patable.p_prefs_register_enum_preference = prefs_register_enum_preference;
415 patable.p_register_giop_user = register_giop_user;
416 patable.p_is_big_endian = is_big_endian;
417 patable.p_get_CDR_string = get_CDR_string;
418 patable.p_get_CDR_ulong = get_CDR_ulong;
419 patable.p_get_CDR_enum = get_CDR_enum;
420 patable.p_get_CDR_object = get_CDR_object;
421 patable.p_get_CDR_boolean = get_CDR_boolean;
426 * Scan the default installation directory.
428 * This may not, in fact, be where Ethereal is installed;
429 * we check that directory later, but we check this directory
430 * as well, so that if you're running the program from a
431 * source directory in which you've built it, you still get
432 * to see the plugins installed on your system.
434 plugins_scan_dir(std_plug_dir);
437 * Now attempt to get the full pathname of the currently running
438 * program, and assume that might contain the pathname of the
439 * directory in which Ethereal was installed.
441 if (GetModuleFileName(NULL, prog_pathname, sizeof prog_pathname) != 0) {
443 * If the program is an installed version, the full pathname
444 * includes the pathname of the directory in which it was
445 * installed; get that directory's pathname, and construct
446 * from it the pathname of the directory in which the
447 * plugins were installed.
449 * First, find the last "\\" in the directory, as that
450 * marks the end of the directory pathname.
452 dir_end = strrchr(prog_pathname, '\\');
453 if (dir_end != NULL) {
455 * Include the "\\" in the directory's pathname,
456 * as we'll be appending to that pathname.
461 * Found it - now figure out how long the plugin
462 * directory pathname will be.
463 * It's the length of the directory pathname
464 * (dir_end - prog_pathname), plus the length of
465 * the plugin directory's relative pathname.
467 plugin_dir_len = (dir_end - prog_pathname) +
468 strlen("plugins\\"VERSION);
471 * Allocate a buffer for the plugin directory
472 * pathname, and construct it.
474 plugin_dir = g_malloc(plugin_dir_len + 1);
475 strncpy(plugin_dir, prog_pathname,
476 dir_end - prog_pathname);
477 strcpy(plugin_dir + (dir_end - prog_pathname),
481 * Is it the same as the standard plugin directory?
482 * (Do a case-insensitive string comparison; this
483 * *is* Windows, after all.)
485 * XXX - is there another way to determine whether
486 * two pathnames refer to the same file?
488 if (g_strcasecmp(std_plug_dir, plugin_dir) != 0) {
490 * It's a different directory - scan it.
492 plugins_scan_dir(plugin_dir);
498 * XXX - why not just scan "plugin_dir"? That's where we
499 * installed the plugins; if Ethereal isn't installed under
500 * "/usr" or "/usr/local", why should we search for its plugins
503 plugins_scan_dir(std_plug_dir);
504 plugins_scan_dir(local_plug_dir);
505 if ((strcmp(std_plug_dir, plugin_dir) != 0) &&
506 (strcmp(local_plug_dir, plugin_dir) != 0))
508 if (stat(plugin_dir, &plugin_dir_stat) == 0)
510 /* check if plugin_dir is really different from std_dir and
511 * local_dir if they exist ! */
512 if (stat(std_plug_dir, &std_dir_stat) == 0)
514 if (stat(local_plug_dir, &local_dir_stat) == 0)
516 if ((plugin_dir_stat.st_dev != std_dir_stat.st_dev ||
517 plugin_dir_stat.st_ino != std_dir_stat.st_ino) &&
518 (plugin_dir_stat.st_dev != local_dir_stat.st_dev ||
519 plugin_dir_stat.st_ino != local_dir_stat.st_ino))
520 plugins_scan_dir(plugin_dir);
524 if ((plugin_dir_stat.st_dev != std_dir_stat.st_dev ||
525 plugin_dir_stat.st_ino != std_dir_stat.st_ino))
526 plugins_scan_dir(plugin_dir);
529 else if (stat(local_plug_dir, &local_dir_stat) == 0)
531 if ((plugin_dir_stat.st_dev != local_dir_stat.st_dev ||
532 plugin_dir_stat.st_ino != local_dir_stat.st_ino))
533 plugins_scan_dir(plugin_dir);
535 else plugins_scan_dir(plugin_dir);
541 user_plug_dir = (gchar *)g_malloc(strlen(get_home_dir()) +
543 strlen(PLUGINS_DIR_NAME) + 3);
544 sprintf(user_plug_dir, "%s/%s/%s", get_home_dir(),
545 PF_DIR, PLUGINS_DIR_NAME);
547 plugins_scan_dir(user_plug_dir);
552 register_all_plugin_handoffs(void)
557 * For all new-style plugins, call the register-handoff routine.
558 * This is called from "proto_init()"; it must be called after
559 * "register_all_protocols()" and "init_plugins()" are called,
560 * in case one plugin registers itself either with a built-in
561 * dissector or with another plugin; we must first register all
562 * dissectors, whether built-in or plugin, so their dissector tables
563 * are initialized, and only then register all handoffs.
565 * We treat those protocols as always being enabled; they should
566 * use the standard mechanism for enabling/disabling protocols, not
567 * the plugin-specific mechanism.
569 for (pt_plug = plugin_list; pt_plug != NULL; pt_plug = pt_plug->next)
570 (pt_plug->reg_handoff)();