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