From Lars Ronald :
[metze/wireshark/wip.git] / epan / plugins.c
1 /* plugins.c
2  * plugin routines
3  *
4  * $Id: plugins.c,v 1.63 2003/03/06 09:01:43 sahlberg Exp $
5  *
6  * Ethereal - Network traffic analyzer
7  * By Gerald Combs <gerald@ethereal.com>
8  * Copyright 1999 Gerald Combs
9  *
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.
14  *
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.
19  *
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.
23  */
24
25 #ifdef HAVE_CONFIG_H
26 # include "config.h"
27 #endif
28
29 #ifdef NEED_SNPRINTF_H
30 # include "snprintf.h"
31 #endif
32
33 #include "plugins.h"
34
35 #ifdef HAVE_PLUGINS
36
37 #include <time.h>
38
39 #ifdef HAVE_DIRENT_H
40 #include <dirent.h>
41 #endif
42
43 #ifdef HAVE_DIRECT_H
44 #include <direct.h>
45 #endif
46
47 #include <string.h>
48 #include <stdlib.h>
49 #include <errno.h>
50
51 #ifdef HAVE_SYS_STAT_H
52 #include <sys/stat.h>
53 #endif
54
55 #ifdef HAVE_UNISTD_H
56 #include <unistd.h>
57 #endif
58
59 #include "filesystem.h"
60
61 #ifdef PLUGINS_NEED_ADDRESS_TABLE
62 #include "conversation.h"
63 #include "reassemble.h"
64 #include "prefs.h"
65 #include "packet-giop.h"
66 #include "packet-tpkt.h"
67 #include "packet-tcp.h"
68 #include "tap.h"
69 #include "plugins/plugin_table.h"
70 static plugin_address_table_t   patable;
71 #endif
72
73 /* linked list of all plugins */
74 plugin *plugin_list;
75
76 static gchar *user_plug_dir = NULL;
77
78 #define PLUGINS_DIR_NAME        "plugins"
79
80 /*
81  * add a new plugin to the list
82  * returns :
83  * - 0 : OK
84  * - ENOMEM : memory allocation problem
85  * - EEXIST : the same plugin (i.e. name/version) was already registered.
86  */
87 static int
88 add_plugin(void *handle, gchar *name, gchar *version,
89            void (*reg_handoff)(void))
90 {
91     plugin *new_plug, *pt_plug;
92
93     pt_plug = plugin_list;
94     if (!pt_plug) /* the list is empty */
95     {
96         new_plug = (plugin *)g_malloc(sizeof(plugin));
97         if (new_plug == NULL) return ENOMEM;
98         plugin_list = new_plug;
99     }
100     else
101     {
102         while (1)
103         {
104             /* check if the same name/version is already registered */
105             if (!strcmp(pt_plug->name, name) &&
106                 !strcmp(pt_plug->version, version))
107             {
108                 return EEXIST;
109             }
110
111             /* we found the last plugin in the list */
112             if (pt_plug->next == NULL) break;
113
114             pt_plug = pt_plug->next;
115         }
116         new_plug = (plugin *)g_malloc(sizeof(plugin));
117         if (new_plug == NULL) return ENOMEM;
118         pt_plug->next = new_plug;
119     }
120
121     new_plug->handle = handle;
122     new_plug->name = name;
123     new_plug->version = version;
124     new_plug->reg_handoff = reg_handoff;
125     new_plug->next = NULL;
126     return 0;
127 }
128
129 /*
130  * XXX - when we remove support for old-style plugins (which we should
131  * probably do eventually, as all plugins should be written as new-style
132  * ones), we may want to have "init_plugins()" merely save a pointer
133  * to the plugin's "init" routine, just as we save a pointer to its
134  * "reg_handoff" routine, and have a "register_all_plugins()" routine
135  * to go through the list of plugins and call all of them.
136  *
137  * Then we'd have "epan_init()", or perhaps even something higher up
138  * in the call tree, call "init_plugins()", and have "proto_init()"
139  * call "register_all_plugins()" right after calling "register_all_protocols()";
140  * this might be a bit cleaner.
141  */
142 static void
143 plugins_scan_dir(const char *dirname)
144 {
145 #define FILENAME_LEN    1024
146     gchar         *hack_path;       /* pathname used to construct lt_lib_ext */
147     gchar         *lt_lib_ext;      /* extension for loadable modules */
148     DIR           *dir;             /* scanned directory */
149     struct dirent *file;            /* current file */
150     gchar          filename[FILENAME_LEN];   /* current file name */
151     GModule       *handle;          /* handle returned by dlopen */
152     gchar         *name;
153     gchar         *version;
154     void         (*init)(void *);
155     void         (*reg_handoff)(void);
156     gchar         *dot;
157     int            cr;
158
159     /*
160      * We find the extension used on this platform for loadable modules
161      * by the sneaky hack of calling "g_module_build_path" to build
162      * the pathname for a module with an empty directory name and
163      * empty module name, and then search for the last "." and use
164      * everything from the last "." on.
165      *
166      * GLib 2.0 will probably define G_MODULE_SUFFIX as the extension
167      * to use, but that's not checked into the GLib CVS tree yet,
168      * and we can't use it on systems that don't have GLib 2.0.
169      */
170     hack_path = g_module_build_path("", "");
171     lt_lib_ext = strrchr(hack_path, '.');
172     if (lt_lib_ext == NULL)
173     {
174         /*
175          * Does this mean there *is* no extension?  Assume so.
176          *
177          * XXX - the code below assumes that all loadable modules have
178          * an extension....
179          */
180         lt_lib_ext = "";
181     }
182
183     if ((dir = opendir(dirname)) != NULL)
184     {
185         while ((file = readdir(dir)) != NULL)
186         {
187             /* don't try to open "." and ".." */
188             if (!(strcmp(file->d_name, "..") &&
189                   strcmp(file->d_name, "."))) continue;
190
191             /* skip anything but files with lt_lib_ext */
192             dot = strrchr(file->d_name, '.');
193             if (dot == NULL || strcmp(dot, lt_lib_ext) != 0) continue;
194
195             snprintf(filename, FILENAME_LEN, "%s" G_DIR_SEPARATOR_S "%s",
196                 dirname, file->d_name);
197             if ((handle = g_module_open(filename, 0)) == NULL)
198             {
199                 g_warning("Couldn't load module %s: %s", filename,
200                           g_module_error());
201                 continue;
202             }
203             name = (gchar *)file->d_name;
204             if (g_module_symbol(handle, "version", (gpointer*)&version) == FALSE)
205             {
206                 g_warning("The plugin %s has no version symbol", name);
207                 g_module_close(handle);
208                 continue;
209             }
210
211             /*
212              * Old-style dissectors don't have a "plugin_reg_handoff()"
213              * routine; we no longer support them.
214              *
215              * New-style dissectors have one, because, otherwise, there's
216              * no way for them to arrange that they ever be called.
217              */
218             if (g_module_symbol(handle, "plugin_reg_handoff",
219                                          (gpointer*)&reg_handoff))
220             {
221                 /*
222                  * We require it to have a "plugin_init()" routine.
223                  */
224                 if (!g_module_symbol(handle, "plugin_init", (gpointer*)&init))
225                 {
226                     g_warning("The plugin %s has a plugin_reg_handoff symbol but no plugin_init routine",
227                               name);
228                     g_module_close(handle);
229                     continue;
230                 }
231
232                 /*
233                  * We have a "plugin_reg_handoff()" routine, so we don't
234                  * need the protocol, filter string, or dissector pointer.
235                  */
236                 if ((cr = add_plugin(handle, g_strdup(file->d_name), version,
237                                      reg_handoff)))
238                 {
239                     if (cr == EEXIST)
240                         fprintf(stderr, "The plugin %s, version %s\n"
241                             "was found in multiple directories\n", name, version);
242                     else
243                         fprintf(stderr, "Memory allocation problem\n"
244                             "when processing plugin %s, version %s\n",
245                             name, version);
246                     g_module_close(handle);
247                     continue;
248                 }
249
250                 /*
251                  * Call its init routine.
252                  */
253 #ifdef PLUGINS_NEED_ADDRESS_TABLE
254                 init(&patable);
255 #else
256                 init(NULL);
257 #endif
258             }
259             else
260             {
261                 /*
262                  * This is an old-style dissector; warn that it won't
263                  * be used, as those aren't supported.
264                  */
265                 fprintf(stderr,
266                     "The plugin %s, version %s is an old-style plugin;\n"
267                     "Those are no longer supported.\n", name, version);
268             }
269         }
270         closedir(dir);
271     }
272     g_free(hack_path);
273 }
274
275 /*
276  * init plugins
277  */
278 void
279 init_plugins(const char *plugin_dir)
280 {
281 #ifdef WIN32
282     const char *datafile_dir;
283     char *install_plugin_dir;
284 #endif
285
286     if (plugin_list == NULL)      /* ensure init_plugins is only run once */
287     {
288 #ifdef PLUGINS_NEED_ADDRESS_TABLE
289         /* Intialize address table */
290         patable.p_check_col                     = check_col;
291         patable.p_col_clear                     = col_clear;
292         patable.p_col_add_fstr                  = col_add_fstr;
293         patable.p_col_append_fstr               = col_append_fstr;
294         patable.p_col_prepend_fstr              = col_prepend_fstr;
295         patable.p_col_add_str                   = col_add_str;
296         patable.p_col_append_str                = col_append_str;
297         patable.p_col_set_str                   = col_set_str;
298
299         patable.p_register_init_routine         = register_init_routine;
300         patable.p_register_postseq_cleanup_routine      = register_postseq_cleanup_routine;
301
302         patable.p_match_strval                  = match_strval;
303         patable.p_val_to_str                    = val_to_str;
304
305         patable.p_conversation_new              = conversation_new;
306         patable.p_find_conversation             = find_conversation;
307         patable.p_conversation_set_dissector    = conversation_set_dissector;
308
309         patable.p_proto_register_protocol       = proto_register_protocol;
310         patable.p_proto_register_field_array    = proto_register_field_array;
311         patable.p_proto_register_subtree_array  = proto_register_subtree_array;
312
313         patable.p_dissector_add                 = dissector_add;
314         patable.p_dissector_delete              = dissector_delete;
315         patable.p_dissector_add_handle          = dissector_add_handle;
316
317         patable.p_heur_dissector_add            = heur_dissector_add;
318
319         patable.p_register_dissector            = register_dissector;
320         patable.p_find_dissector                = find_dissector;
321         patable.p_create_dissector_handle       = create_dissector_handle;
322         patable.p_call_dissector                = call_dissector;
323
324         patable.p_tcp_dissect_pdus              = tcp_dissect_pdus;
325
326         patable.p_proto_is_protocol_enabled     = proto_is_protocol_enabled;
327
328         patable.p_proto_item_get_len            = proto_item_get_len;
329         patable.p_proto_item_set_len            = proto_item_set_len;
330         patable.p_proto_item_set_text           = proto_item_set_text;
331         patable.p_proto_item_append_text        = proto_item_append_text;
332         patable.p_proto_item_add_subtree        = proto_item_add_subtree;
333         patable.p_proto_tree_add_item           = proto_tree_add_item;
334         patable.p_proto_tree_add_item_hidden    = proto_tree_add_item_hidden;
335         patable.p_proto_tree_add_protocol_format = proto_tree_add_protocol_format;
336         patable.p_proto_tree_add_bytes          = proto_tree_add_bytes;
337         patable.p_proto_tree_add_bytes_hidden   = proto_tree_add_bytes_hidden;
338         patable.p_proto_tree_add_bytes_format   = proto_tree_add_bytes_format;
339         patable.p_proto_tree_add_time           = proto_tree_add_time;
340         patable.p_proto_tree_add_time_hidden    = proto_tree_add_time_hidden;
341         patable.p_proto_tree_add_time_format    = proto_tree_add_time_format;
342         patable.p_proto_tree_add_ipxnet         = proto_tree_add_ipxnet;
343         patable.p_proto_tree_add_ipxnet_hidden  = proto_tree_add_ipxnet_hidden;
344         patable.p_proto_tree_add_ipxnet_format  = proto_tree_add_ipxnet_format;
345         patable.p_proto_tree_add_ipv4           = proto_tree_add_ipv4;
346         patable.p_proto_tree_add_ipv4_hidden    = proto_tree_add_ipv4_hidden;
347         patable.p_proto_tree_add_ipv4_format    = proto_tree_add_ipv4_format;
348         patable.p_proto_tree_add_ipv6           = proto_tree_add_ipv6;
349         patable.p_proto_tree_add_ipv6_hidden    = proto_tree_add_ipv6_hidden;
350         patable.p_proto_tree_add_ipv6_format    = proto_tree_add_ipv6_format;
351         patable.p_proto_tree_add_ether          = proto_tree_add_ether;
352         patable.p_proto_tree_add_ether_hidden   = proto_tree_add_ether_hidden;
353         patable.p_proto_tree_add_ether_format   = proto_tree_add_ether_format;
354         patable.p_proto_tree_add_string         = proto_tree_add_string;
355         patable.p_proto_tree_add_string_hidden  = proto_tree_add_string_hidden;
356         patable.p_proto_tree_add_string_format  = proto_tree_add_string_format;
357         patable.p_proto_tree_add_boolean        = proto_tree_add_boolean;
358         patable.p_proto_tree_add_boolean_hidden = proto_tree_add_boolean_hidden;
359         patable.p_proto_tree_add_boolean_format = proto_tree_add_boolean_format;
360         patable.p_proto_tree_add_double         = proto_tree_add_double;
361         patable.p_proto_tree_add_double_hidden  = proto_tree_add_double_hidden;
362         patable.p_proto_tree_add_double_format  = proto_tree_add_double_format;
363         patable.p_proto_tree_add_uint           = proto_tree_add_uint;
364         patable.p_proto_tree_add_uint_hidden    = proto_tree_add_uint_hidden;
365         patable.p_proto_tree_add_uint_format    = proto_tree_add_uint_format;
366         patable.p_proto_tree_add_int            = proto_tree_add_int;
367         patable.p_proto_tree_add_int_hidden     = proto_tree_add_int_hidden;
368         patable.p_proto_tree_add_int_format     = proto_tree_add_int_format;
369         patable.p_proto_tree_add_text           = proto_tree_add_text;
370
371         patable.p_tvb_new_subset                = tvb_new_subset;
372         patable.p_tvb_set_free_cb               = tvb_set_free_cb;
373         patable.p_tvb_set_child_real_data_tvbuff = tvb_set_child_real_data_tvbuff;
374         patable.p_tvb_new_real_data             = tvb_new_real_data;
375
376         patable.p_tvb_length                    = tvb_length;
377         patable.p_tvb_length_remaining          = tvb_length_remaining;
378         patable.p_tvb_bytes_exist               = tvb_bytes_exist;
379         patable.p_tvb_offset_exists             = tvb_offset_exists;
380         patable.p_tvb_reported_length           = tvb_reported_length;
381         patable.p_tvb_reported_length_remaining = tvb_reported_length_remaining;
382
383         patable.p_tvb_get_guint8                = tvb_get_guint8;
384
385         patable.p_tvb_get_ntohs                 = tvb_get_ntohs;
386         patable.p_tvb_get_ntoh24                = tvb_get_ntoh24;
387         patable.p_tvb_get_ntohl                 = tvb_get_ntohl;
388
389         patable.p_tvb_get_letohs                = tvb_get_letohs;
390         patable.p_tvb_get_letoh24               = tvb_get_letoh24;
391         patable.p_tvb_get_letohl                = tvb_get_letohl;
392
393         patable.p_tvb_memcpy                    = tvb_memcpy;
394         patable.p_tvb_memdup                    = tvb_memdup;
395
396         patable.p_tvb_get_ptr                   = tvb_get_ptr;
397
398         patable.p_tvb_find_guint8               = tvb_find_guint8;
399         patable.p_tvb_pbrk_guint8               = tvb_pbrk_guint8;
400
401         patable.p_tvb_strnlen                   = tvb_strnlen;
402
403         patable.p_tvb_format_text               = tvb_format_text;
404
405         patable.p_tvb_get_nstringz              = tvb_get_nstringz;
406         patable.p_tvb_get_nstringz0             = tvb_get_nstringz0;
407
408         patable.p_tvb_find_line_end             = tvb_find_line_end;
409         patable.p_tvb_find_line_end_unquoted    = tvb_find_line_end_unquoted;
410
411         patable.p_tvb_strneql                   = tvb_strneql;
412         patable.p_tvb_strncaseeql               = tvb_strncaseeql;
413
414         patable.p_tvb_bytes_to_str              = tvb_bytes_to_str;
415
416         patable.p_prefs_register_protocol       = prefs_register_protocol;
417         patable.p_prefs_register_uint_preference = prefs_register_uint_preference;
418         patable.p_prefs_register_bool_preference = prefs_register_bool_preference;
419         patable.p_prefs_register_enum_preference = prefs_register_enum_preference;
420         patable.p_prefs_register_string_preference = prefs_register_string_preference;
421
422         patable.p_register_giop_user            = register_giop_user;
423         patable.p_is_big_endian                 = is_big_endian;
424         patable.p_get_CDR_encap_info            = get_CDR_encap_info;
425
426         patable.p_get_CDR_any                   = get_CDR_any;
427         patable.p_get_CDR_boolean               = get_CDR_boolean;
428         patable.p_get_CDR_char                  = get_CDR_char;
429         patable.p_get_CDR_double                = get_CDR_double;
430         patable.p_get_CDR_enum                  = get_CDR_enum;
431         patable.p_get_CDR_fixed                 = get_CDR_fixed;
432         patable.p_get_CDR_float                 = get_CDR_float;
433         patable.p_get_CDR_interface             = get_CDR_interface;
434         patable.p_get_CDR_long                  = get_CDR_long;
435         patable.p_get_CDR_object                = get_CDR_object;
436         patable.p_get_CDR_octet                 = get_CDR_octet;
437         patable.p_get_CDR_octet_seq             = get_CDR_octet_seq;
438         patable.p_get_CDR_short                 = get_CDR_short;
439         patable.p_get_CDR_string                = get_CDR_string;
440         patable.p_get_CDR_typeCode              = get_CDR_typeCode;
441         patable.p_get_CDR_ulong                 = get_CDR_ulong;
442         patable.p_get_CDR_ushort                = get_CDR_ushort;
443         patable.p_get_CDR_wchar                 = get_CDR_wchar;
444         patable.p_get_CDR_wstring               = get_CDR_wstring;
445
446         patable.p_is_tpkt                       = is_tpkt;
447         patable.p_dissect_tpkt_encap            = dissect_tpkt_encap;
448
449         patable.p_set_actual_length             = set_actual_length;
450         patable.p_decode_boolean_bitfield       = decode_boolean_bitfield;
451         patable.p_decode_numeric_bitfield       = decode_numeric_bitfield;
452         patable.p_decode_enumerated_bitfield    = decode_enumerated_bitfield;
453         patable.p_register_dissector_table      = register_dissector_table;
454         patable.p_except_throw                  = except_throw;
455         patable.p_dissector_try_port            = dissector_try_port;
456
457         patable.p_conversation_add_proto_data   = conversation_add_proto_data;
458         patable.p_conversation_get_proto_data   = conversation_get_proto_data;
459         patable.p_conversation_delete_proto_data = conversation_delete_proto_data;
460         patable.p_p_add_proto_data              = p_add_proto_data;
461         patable.p_p_get_proto_data              = p_get_proto_data;
462
463         patable.p_ip_to_str                     = ip_to_str;
464         patable.p_ip6_to_str                    = ip6_to_str;
465         patable.p_time_secs_to_str              = time_secs_to_str;
466         patable.p_time_msecs_to_str             = time_msecs_to_str;
467         patable.p_abs_time_to_str               = abs_time_to_str;
468
469         patable.p_proto_get_id_by_filter_name   = proto_get_id_by_filter_name;
470         patable.p_proto_get_protocol_name       = proto_get_protocol_name;
471         patable.p_proto_get_protocol_short_name = proto_get_protocol_short_name;
472         patable.p_proto_get_protocol_filter_name        = proto_get_protocol_filter_name;
473
474         patable.p_prefs_register_obsolete_preference    = prefs_register_obsolete_preference;
475
476         patable.p_add_new_data_source           = add_new_data_source;
477
478         patable.p_fragment_table_init           = fragment_table_init;
479         patable.p_reassembled_table_init        = reassembled_table_init;
480         patable.p_fragment_add                  = fragment_add;
481         patable.p_fragment_add_seq              = fragment_add_seq;
482         patable.p_fragment_add_seq_check        = fragment_add_seq_check;
483         patable.p_fragment_add_seq_next         = fragment_add_seq_next;
484         patable.p_fragment_get                  = fragment_get;
485         patable.p_fragment_set_tot_len          = fragment_set_tot_len;
486         patable.p_fragment_get_tot_len          = fragment_get_tot_len;
487         patable.p_fragment_set_partial_reassembly       = fragment_set_partial_reassembly;
488         patable.p_fragment_delete               = fragment_delete;
489         patable.p_show_fragment_tree            = show_fragment_tree;
490         patable.p_show_fragment_seq_tree        = show_fragment_seq_tree;
491         
492         patable.p_register_tap                  = register_tap;
493         patable.p_tap_queue_packet              = tap_queue_packet;
494         
495 #endif
496
497 #ifdef WIN32
498         /*
499          * On Windows, the data file directory is the installation
500          * directory; the plugins are stored under it.
501          *
502          * Assume we're running the installed version of Ethereal;
503          * on Windows, the data file directory is the directory
504          * in which the Ethereal binary resides.
505          */
506         datafile_dir = get_datafile_dir();
507         install_plugin_dir = g_malloc(strlen(datafile_dir) + strlen("plugins") +
508             strlen(VERSION) + 3);
509         sprintf(install_plugin_dir, "%s\\plugins\\%s", datafile_dir, VERSION);
510
511         /*
512          * Make sure that pathname refers to a directory.
513          */
514         if (test_for_directory(install_plugin_dir) != EISDIR) {
515                 /*
516                  * Either it doesn't refer to a directory or it
517                  * refers to something that doesn't exist.
518                  *
519                  * Assume that means we're running, for example,
520                  * a version of Ethereal we've built in a source
521                  * directory, and fall back on the default
522                  * installation directory, so you can put the plugins
523                  * somewhere so they can be used with this version
524                  * of Ethereal.
525                  *
526                  * XXX - should we, instead, have the Windows build
527                  * procedure create a subdirectory of the "plugins"
528                  * source directory, and copy the plugin DLLs there,
529                  * so that you use the plugins from the build tree?
530                  */
531                 install_plugin_dir =
532                     g_strdup("C:\\Program Files\\Ethereal\\plugins\\" VERSION);
533         }
534
535         /*
536          * Scan that directory.
537          */
538         plugins_scan_dir(install_plugin_dir);
539         g_free(install_plugin_dir);
540 #else
541         /*
542          * Scan the plugin directory.
543          */
544         plugins_scan_dir(plugin_dir);
545 #endif
546         if (!user_plug_dir)
547             user_plug_dir = get_persconffile_path(PLUGINS_DIR_NAME, FALSE);
548         plugins_scan_dir(user_plug_dir);
549     }
550 }
551
552 void
553 register_all_plugin_handoffs(void)
554 {
555   plugin *pt_plug;
556
557   /*
558    * For all new-style plugins, call the register-handoff routine.
559    * This is called from "proto_init()"; it must be called after
560    * "register_all_protocols()" and "init_plugins()" are called,
561    * in case one plugin registers itself either with a built-in
562    * dissector or with another plugin; we must first register all
563    * dissectors, whether built-in or plugin, so their dissector tables
564    * are initialized, and only then register all handoffs.
565    *
566    * We treat those protocols as always being enabled; they should
567    * use the standard mechanism for enabling/disabling protocols, not
568    * the plugin-specific mechanism.
569    */
570   for (pt_plug = plugin_list; pt_plug != NULL; pt_plug = pt_plug->next)
571     (pt_plug->reg_handoff)();
572 }
573 #endif