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