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