4 * $Id: plugins.c,v 1.65 2003/04/24 21:15:12 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.
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"
65 #include "packet-giop.h"
66 #include "packet-tpkt.h"
67 #include "packet-tcp.h"
70 #include "plugins/plugin_table.h"
71 static plugin_address_table_t patable;
74 /* linked list of all plugins */
77 static gchar *user_plug_dir = NULL;
79 #define PLUGINS_DIR_NAME "plugins"
82 * add a new plugin to the list
85 * - ENOMEM : memory allocation problem
86 * - EEXIST : the same plugin (i.e. name/version) was already registered.
89 add_plugin(void *handle, gchar *name, gchar *version,
90 void (*reg_handoff)(void))
92 plugin *new_plug, *pt_plug;
94 pt_plug = plugin_list;
95 if (!pt_plug) /* the list is empty */
97 new_plug = (plugin *)g_malloc(sizeof(plugin));
98 if (new_plug == NULL) return ENOMEM;
99 plugin_list = new_plug;
105 /* check if the same name/version is already registered */
106 if (!strcmp(pt_plug->name, name) &&
107 !strcmp(pt_plug->version, version))
112 /* we found the last plugin in the list */
113 if (pt_plug->next == NULL) break;
115 pt_plug = pt_plug->next;
117 new_plug = (plugin *)g_malloc(sizeof(plugin));
118 if (new_plug == NULL) return ENOMEM;
119 pt_plug->next = new_plug;
122 new_plug->handle = handle;
123 new_plug->name = name;
124 new_plug->version = version;
125 new_plug->reg_handoff = reg_handoff;
126 new_plug->next = NULL;
131 * XXX - when we remove support for old-style plugins (which we should
132 * probably do eventually, as all plugins should be written as new-style
133 * ones), we may want to have "init_plugins()" merely save a pointer
134 * to the plugin's "init" routine, just as we save a pointer to its
135 * "reg_handoff" routine, and have a "register_all_plugins()" routine
136 * to go through the list of plugins and call all of them.
138 * Then we'd have "epan_init()", or perhaps even something higher up
139 * in the call tree, call "init_plugins()", and have "proto_init()"
140 * call "register_all_plugins()" right after calling "register_all_protocols()";
141 * this might be a bit cleaner.
144 plugins_scan_dir(const char *dirname)
146 #define FILENAME_LEN 1024
147 gchar *hack_path; /* pathname used to construct lt_lib_ext */
148 gchar *lt_lib_ext; /* extension for loadable modules */
149 DIR *dir; /* scanned directory */
150 struct dirent *file; /* current file */
151 gchar filename[FILENAME_LEN]; /* current file name */
152 GModule *handle; /* handle returned by dlopen */
155 void (*init)(void *);
156 void (*reg_handoff)(void);
161 * We find the extension used on this platform for loadable modules
162 * by the sneaky hack of calling "g_module_build_path" to build
163 * the pathname for a module with an empty directory name and
164 * empty module name, and then search for the last "." and use
165 * everything from the last "." on.
167 * GLib 2.0 will probably define G_MODULE_SUFFIX as the extension
168 * to use, but that's not checked into the GLib CVS tree yet,
169 * and we can't use it on systems that don't have GLib 2.0.
171 hack_path = g_module_build_path("", "");
172 lt_lib_ext = strrchr(hack_path, '.');
173 if (lt_lib_ext == NULL)
176 * Does this mean there *is* no extension? Assume so.
178 * XXX - the code below assumes that all loadable modules have
184 if ((dir = opendir(dirname)) != NULL)
186 while ((file = readdir(dir)) != NULL)
188 /* don't try to open "." and ".." */
189 if (!(strcmp(file->d_name, "..") &&
190 strcmp(file->d_name, "."))) continue;
192 /* skip anything but files with lt_lib_ext */
193 dot = strrchr(file->d_name, '.');
194 if (dot == NULL || strcmp(dot, lt_lib_ext) != 0) continue;
196 snprintf(filename, FILENAME_LEN, "%s" G_DIR_SEPARATOR_S "%s",
197 dirname, file->d_name);
198 if ((handle = g_module_open(filename, 0)) == NULL)
200 g_warning("Couldn't load module %s: %s", filename,
204 name = (gchar *)file->d_name;
205 if (g_module_symbol(handle, "version", (gpointer*)&version) == FALSE)
207 g_warning("The plugin %s has no version symbol", name);
208 g_module_close(handle);
213 * Old-style dissectors don't have a "plugin_reg_handoff()"
214 * routine; we no longer support them.
216 * New-style dissectors have one, because, otherwise, there's
217 * no way for them to arrange that they ever be called.
219 if (g_module_symbol(handle, "plugin_reg_handoff",
220 (gpointer*)®_handoff))
223 * We require it to have a "plugin_init()" routine.
225 if (!g_module_symbol(handle, "plugin_init", (gpointer*)&init))
227 g_warning("The plugin %s has a plugin_reg_handoff symbol but no plugin_init routine",
229 g_module_close(handle);
234 * We have a "plugin_reg_handoff()" routine, so we don't
235 * need the protocol, filter string, or dissector pointer.
237 if ((cr = add_plugin(handle, g_strdup(file->d_name), version,
241 fprintf(stderr, "The plugin %s, version %s\n"
242 "was found in multiple directories\n", name, version);
244 fprintf(stderr, "Memory allocation problem\n"
245 "when processing plugin %s, version %s\n",
247 g_module_close(handle);
252 * Call its init routine.
254 #ifdef PLUGINS_NEED_ADDRESS_TABLE
263 * This is an old-style dissector; warn that it won't
264 * be used, as those aren't supported.
267 "The plugin %s, version %s is an old-style plugin;\n"
268 "Those are no longer supported.\n", name, version);
280 init_plugins(const char *plugin_dir)
283 const char *datafile_dir;
284 char *install_plugin_dir;
287 if (plugin_list == NULL) /* ensure init_plugins is only run once */
289 #ifdef PLUGINS_NEED_ADDRESS_TABLE
290 /* Intialize address table */
291 patable.p_check_col = check_col;
292 patable.p_col_clear = col_clear;
293 patable.p_col_add_fstr = col_add_fstr;
294 patable.p_col_append_fstr = col_append_fstr;
295 patable.p_col_prepend_fstr = col_prepend_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;
300 patable.p_register_init_routine = register_init_routine;
301 patable.p_register_postseq_cleanup_routine = register_postseq_cleanup_routine;
303 patable.p_match_strval = match_strval;
304 patable.p_val_to_str = val_to_str;
306 patable.p_conversation_new = conversation_new;
307 patable.p_find_conversation = find_conversation;
308 patable.p_conversation_set_dissector = conversation_set_dissector;
310 patable.p_proto_register_protocol = proto_register_protocol;
311 patable.p_proto_register_field_array = proto_register_field_array;
312 patable.p_proto_register_subtree_array = proto_register_subtree_array;
314 patable.p_dissector_add = dissector_add;
315 patable.p_dissector_delete = dissector_delete;
316 patable.p_dissector_add_handle = dissector_add_handle;
318 patable.p_heur_dissector_add = heur_dissector_add;
320 patable.p_register_dissector = register_dissector;
321 patable.p_find_dissector = find_dissector;
322 patable.p_create_dissector_handle = create_dissector_handle;
323 patable.p_call_dissector = call_dissector;
325 patable.p_tcp_dissect_pdus = tcp_dissect_pdus;
327 patable.p_proto_is_protocol_enabled = proto_is_protocol_enabled;
329 patable.p_proto_item_get_len = proto_item_get_len;
330 patable.p_proto_item_set_len = proto_item_set_len;
331 patable.p_proto_item_set_text = proto_item_set_text;
332 patable.p_proto_item_append_text = proto_item_append_text;
333 patable.p_proto_item_add_subtree = proto_item_add_subtree;
334 patable.p_proto_tree_add_item = proto_tree_add_item;
335 patable.p_proto_tree_add_item_hidden = proto_tree_add_item_hidden;
336 patable.p_proto_tree_add_none_format = proto_tree_add_none_format;
337 patable.p_proto_tree_add_protocol_format = proto_tree_add_protocol_format;
338 patable.p_proto_tree_add_bytes = proto_tree_add_bytes;
339 patable.p_proto_tree_add_bytes_hidden = proto_tree_add_bytes_hidden;
340 patable.p_proto_tree_add_bytes_format = proto_tree_add_bytes_format;
341 patable.p_proto_tree_add_time = proto_tree_add_time;
342 patable.p_proto_tree_add_time_hidden = proto_tree_add_time_hidden;
343 patable.p_proto_tree_add_time_format = proto_tree_add_time_format;
344 patable.p_proto_tree_add_ipxnet = proto_tree_add_ipxnet;
345 patable.p_proto_tree_add_ipxnet_hidden = proto_tree_add_ipxnet_hidden;
346 patable.p_proto_tree_add_ipxnet_format = proto_tree_add_ipxnet_format;
347 patable.p_proto_tree_add_ipv4 = proto_tree_add_ipv4;
348 patable.p_proto_tree_add_ipv4_hidden = proto_tree_add_ipv4_hidden;
349 patable.p_proto_tree_add_ipv4_format = proto_tree_add_ipv4_format;
350 patable.p_proto_tree_add_ipv6 = proto_tree_add_ipv6;
351 patable.p_proto_tree_add_ipv6_hidden = proto_tree_add_ipv6_hidden;
352 patable.p_proto_tree_add_ipv6_format = proto_tree_add_ipv6_format;
353 patable.p_proto_tree_add_ether = proto_tree_add_ether;
354 patable.p_proto_tree_add_ether_hidden = proto_tree_add_ether_hidden;
355 patable.p_proto_tree_add_ether_format = proto_tree_add_ether_format;
356 patable.p_proto_tree_add_string = proto_tree_add_string;
357 patable.p_proto_tree_add_string_hidden = proto_tree_add_string_hidden;
358 patable.p_proto_tree_add_string_format = proto_tree_add_string_format;
359 patable.p_proto_tree_add_boolean = proto_tree_add_boolean;
360 patable.p_proto_tree_add_boolean_hidden = proto_tree_add_boolean_hidden;
361 patable.p_proto_tree_add_boolean_format = proto_tree_add_boolean_format;
362 patable.p_proto_tree_add_double = proto_tree_add_double;
363 patable.p_proto_tree_add_double_hidden = proto_tree_add_double_hidden;
364 patable.p_proto_tree_add_double_format = proto_tree_add_double_format;
365 patable.p_proto_tree_add_uint = proto_tree_add_uint;
366 patable.p_proto_tree_add_uint_hidden = proto_tree_add_uint_hidden;
367 patable.p_proto_tree_add_uint_format = proto_tree_add_uint_format;
368 patable.p_proto_tree_add_int = proto_tree_add_int;
369 patable.p_proto_tree_add_int_hidden = proto_tree_add_int_hidden;
370 patable.p_proto_tree_add_int_format = proto_tree_add_int_format;
371 patable.p_proto_tree_add_text = proto_tree_add_text;
373 patable.p_tvb_new_subset = tvb_new_subset;
374 patable.p_tvb_set_free_cb = tvb_set_free_cb;
375 patable.p_tvb_set_child_real_data_tvbuff = tvb_set_child_real_data_tvbuff;
376 patable.p_tvb_new_real_data = tvb_new_real_data;
378 patable.p_tvb_length = tvb_length;
379 patable.p_tvb_length_remaining = tvb_length_remaining;
380 patable.p_tvb_bytes_exist = tvb_bytes_exist;
381 patable.p_tvb_offset_exists = tvb_offset_exists;
382 patable.p_tvb_reported_length = tvb_reported_length;
383 patable.p_tvb_reported_length_remaining = tvb_reported_length_remaining;
385 patable.p_tvb_get_guint8 = tvb_get_guint8;
387 patable.p_tvb_get_ntohs = tvb_get_ntohs;
388 patable.p_tvb_get_ntoh24 = tvb_get_ntoh24;
389 patable.p_tvb_get_ntohl = tvb_get_ntohl;
391 patable.p_tvb_get_letohs = tvb_get_letohs;
392 patable.p_tvb_get_letoh24 = tvb_get_letoh24;
393 patable.p_tvb_get_letohl = tvb_get_letohl;
395 patable.p_tvb_memcpy = tvb_memcpy;
396 patable.p_tvb_memdup = tvb_memdup;
398 patable.p_tvb_get_ptr = tvb_get_ptr;
400 patable.p_tvb_find_guint8 = tvb_find_guint8;
401 patable.p_tvb_pbrk_guint8 = tvb_pbrk_guint8;
403 patable.p_tvb_strnlen = tvb_strnlen;
405 patable.p_tvb_format_text = tvb_format_text;
407 patable.p_tvb_get_nstringz = tvb_get_nstringz;
408 patable.p_tvb_get_nstringz0 = tvb_get_nstringz0;
410 patable.p_tvb_find_line_end = tvb_find_line_end;
411 patable.p_tvb_find_line_end_unquoted = tvb_find_line_end_unquoted;
413 patable.p_tvb_strneql = tvb_strneql;
414 patable.p_tvb_strncaseeql = tvb_strncaseeql;
416 patable.p_tvb_bytes_to_str = tvb_bytes_to_str;
418 patable.p_prefs_register_protocol = prefs_register_protocol;
419 patable.p_prefs_register_uint_preference = prefs_register_uint_preference;
420 patable.p_prefs_register_bool_preference = prefs_register_bool_preference;
421 patable.p_prefs_register_enum_preference = prefs_register_enum_preference;
422 patable.p_prefs_register_string_preference = prefs_register_string_preference;
424 patable.p_register_giop_user = register_giop_user;
425 patable.p_is_big_endian = is_big_endian;
426 patable.p_get_CDR_encap_info = get_CDR_encap_info;
428 patable.p_get_CDR_any = get_CDR_any;
429 patable.p_get_CDR_boolean = get_CDR_boolean;
430 patable.p_get_CDR_char = get_CDR_char;
431 patable.p_get_CDR_double = get_CDR_double;
432 patable.p_get_CDR_enum = get_CDR_enum;
433 patable.p_get_CDR_fixed = get_CDR_fixed;
434 patable.p_get_CDR_float = get_CDR_float;
435 patable.p_get_CDR_interface = get_CDR_interface;
436 patable.p_get_CDR_long = get_CDR_long;
437 patable.p_get_CDR_object = get_CDR_object;
438 patable.p_get_CDR_octet = get_CDR_octet;
439 patable.p_get_CDR_octet_seq = get_CDR_octet_seq;
440 patable.p_get_CDR_short = get_CDR_short;
441 patable.p_get_CDR_string = get_CDR_string;
442 patable.p_get_CDR_typeCode = get_CDR_typeCode;
443 patable.p_get_CDR_ulong = get_CDR_ulong;
444 patable.p_get_CDR_ushort = get_CDR_ushort;
445 patable.p_get_CDR_wchar = get_CDR_wchar;
446 patable.p_get_CDR_wstring = get_CDR_wstring;
448 patable.p_is_tpkt = is_tpkt;
449 patable.p_dissect_tpkt_encap = dissect_tpkt_encap;
451 patable.p_set_actual_length = set_actual_length;
452 patable.p_decode_boolean_bitfield = decode_boolean_bitfield;
453 patable.p_decode_numeric_bitfield = decode_numeric_bitfield;
454 patable.p_decode_enumerated_bitfield = decode_enumerated_bitfield;
455 patable.p_register_dissector_table = register_dissector_table;
456 patable.p_except_throw = except_throw;
457 patable.p_dissector_try_port = dissector_try_port;
459 patable.p_conversation_add_proto_data = conversation_add_proto_data;
460 patable.p_conversation_get_proto_data = conversation_get_proto_data;
461 patable.p_conversation_delete_proto_data = conversation_delete_proto_data;
462 patable.p_p_add_proto_data = p_add_proto_data;
463 patable.p_p_get_proto_data = p_get_proto_data;
465 patable.p_ip_to_str = ip_to_str;
466 patable.p_ip6_to_str = ip6_to_str;
467 patable.p_time_secs_to_str = time_secs_to_str;
468 patable.p_time_msecs_to_str = time_msecs_to_str;
469 patable.p_abs_time_to_str = abs_time_to_str;
471 patable.p_proto_get_id_by_filter_name = proto_get_id_by_filter_name;
472 patable.p_proto_get_protocol_name = proto_get_protocol_name;
473 patable.p_proto_get_protocol_short_name = proto_get_protocol_short_name;
474 patable.p_proto_get_protocol_filter_name = proto_get_protocol_filter_name;
476 patable.p_prefs_register_obsolete_preference = prefs_register_obsolete_preference;
478 patable.p_add_new_data_source = add_new_data_source;
480 patable.p_fragment_table_init = fragment_table_init;
481 patable.p_reassembled_table_init = reassembled_table_init;
482 patable.p_fragment_add = fragment_add;
483 patable.p_fragment_add_seq = fragment_add_seq;
484 patable.p_fragment_add_seq_check = fragment_add_seq_check;
485 patable.p_fragment_add_seq_next = fragment_add_seq_next;
486 patable.p_fragment_get = fragment_get;
487 patable.p_fragment_set_tot_len = fragment_set_tot_len;
488 patable.p_fragment_get_tot_len = fragment_get_tot_len;
489 patable.p_fragment_set_partial_reassembly = fragment_set_partial_reassembly;
490 patable.p_fragment_delete = fragment_delete;
491 patable.p_show_fragment_tree = show_fragment_tree;
492 patable.p_show_fragment_seq_tree = show_fragment_seq_tree;
494 patable.p_register_tap = register_tap;
495 patable.p_tap_queue_packet = tap_queue_packet;
497 patable.p_asn1_open = asn1_open;
498 patable.p_asn1_close = asn1_close;
499 patable.p_asn1_octet_decode = asn1_octet_decode;
500 patable.p_asn1_tag_decode = asn1_tag_decode;
501 patable.p_asn1_id_decode = asn1_id_decode;
502 patable.p_asn1_length_decode = asn1_length_decode;
503 patable.p_asn1_header_decode = asn1_header_decode;
504 patable.p_asn1_eoc = asn1_eoc;
505 patable.p_asn1_eoc_decode = asn1_eoc_decode;
506 patable.p_asn1_null_decode = asn1_null_decode;
507 patable.p_asn1_bool_decode = asn1_bool_decode;
508 patable.p_asn1_int32_value_decode = asn1_int32_value_decode;
509 patable.p_asn1_int32_decode = asn1_int32_decode;
510 patable.p_asn1_uint32_value_decode = asn1_uint32_value_decode;
511 patable.p_asn1_uint32_decode = asn1_uint32_decode;
512 patable.p_asn1_bits_decode = asn1_bits_decode;
513 patable.p_asn1_string_value_decode = asn1_string_value_decode;
514 patable.p_asn1_string_decode = asn1_octet_string_decode;
515 patable.p_asn1_octet_string_decode = asn1_octet_string_decode;
516 patable.p_asn1_subid_decode = asn1_subid_decode;
517 patable.p_asn1_oid_value_decode = asn1_oid_value_decode;
518 patable.p_asn1_oid_decode = asn1_oid_decode;
519 patable.p_asn1_sequence_decode = asn1_sequence_decode;
520 patable.p_asn1_err_to_str = asn1_err_to_str;
526 * On Windows, the data file directory is the installation
527 * directory; the plugins are stored under it.
529 * Assume we're running the installed version of Ethereal;
530 * on Windows, the data file directory is the directory
531 * in which the Ethereal binary resides.
533 datafile_dir = get_datafile_dir();
534 install_plugin_dir = g_malloc(strlen(datafile_dir) + strlen("plugins") +
535 strlen(VERSION) + 3);
536 sprintf(install_plugin_dir, "%s\\plugins\\%s", datafile_dir, VERSION);
539 * Make sure that pathname refers to a directory.
541 if (test_for_directory(install_plugin_dir) != EISDIR) {
543 * Either it doesn't refer to a directory or it
544 * refers to something that doesn't exist.
546 * Assume that means we're running, for example,
547 * a version of Ethereal we've built in a source
548 * directory, and fall back on the default
549 * installation directory, so you can put the plugins
550 * somewhere so they can be used with this version
553 * XXX - should we, instead, have the Windows build
554 * procedure create a subdirectory of the "plugins"
555 * source directory, and copy the plugin DLLs there,
556 * so that you use the plugins from the build tree?
559 g_strdup("C:\\Program Files\\Ethereal\\plugins\\" VERSION);
563 * Scan that directory.
565 plugins_scan_dir(install_plugin_dir);
566 g_free(install_plugin_dir);
569 * Scan the plugin directory.
571 plugins_scan_dir(plugin_dir);
574 user_plug_dir = get_persconffile_path(PLUGINS_DIR_NAME, FALSE);
575 plugins_scan_dir(user_plug_dir);
580 register_all_plugin_handoffs(void)
585 * For all new-style plugins, call the register-handoff routine.
586 * This is called from "proto_init()"; it must be called after
587 * "register_all_protocols()" and "init_plugins()" are called,
588 * in case one plugin registers itself either with a built-in
589 * dissector or with another plugin; we must first register all
590 * dissectors, whether built-in or plugin, so their dissector tables
591 * are initialized, and only then register all handoffs.
593 * We treat those protocols as always being enabled; they should
594 * use the standard mechanism for enabling/disabling protocols, not
595 * the plugin-specific mechanism.
597 for (pt_plug = plugin_list; pt_plug != NULL; pt_plug = pt_plug->next)
598 (pt_plug->reg_handoff)();