806488fd7d31447c64031700b1b414bda91c0bdb
[obnox/wireshark/wip.git] / epan / plugins.c
1 /* plugins.c
2  * plugin routines
3  *
4  * $Id$
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_UNISTD_H
48 #include <unistd.h>
49 #endif
50
51 #include "filesystem.h"
52 #include "file_util.h"
53 #include "report_err.h"
54
55 /* linked list of all plugins */
56 plugin *plugin_list;
57
58 #define PLUGINS_DIR_NAME        "plugins"
59
60 /*
61  * add a new plugin to the list
62  * returns :
63  * - 0 : OK
64  * - ENOMEM : memory allocation problem
65  * - EEXIST : the same plugin (i.e. name/version) was already registered.
66  */
67 static int
68 add_plugin(void *handle, gchar *name, gchar *version,
69            void (*register_protoinfo)(void), void (*reg_handoff)(void),
70            void (*register_tap_listener)(void))
71 {
72     plugin *new_plug, *pt_plug;
73
74     pt_plug = plugin_list;
75     if (!pt_plug) /* the list is empty */
76     {
77         new_plug = (plugin *)g_malloc(sizeof(plugin));
78         if (new_plug == NULL) return ENOMEM;
79         plugin_list = new_plug;
80     }
81     else
82     {
83         while (1)
84         {
85             /* check if the same name/version is already registered */
86             if (!strcmp(pt_plug->name, name) &&
87                 !strcmp(pt_plug->version, version))
88             {
89                 return EEXIST;
90             }
91
92             /* we found the last plugin in the list */
93             if (pt_plug->next == NULL) break;
94
95             pt_plug = pt_plug->next;
96         }
97         new_plug = (plugin *)g_malloc(sizeof(plugin));
98         if (new_plug == NULL) return ENOMEM;
99         pt_plug->next = new_plug;
100     }
101
102     new_plug->handle = handle;
103     new_plug->name = name;
104     new_plug->version = version;
105     new_plug->register_protoinfo = register_protoinfo;
106     new_plug->reg_handoff = reg_handoff;
107     new_plug->register_tap_listener = register_tap_listener;
108     new_plug->next = NULL;
109     return 0;
110 }
111
112 /*
113  * XXX - when we remove support for old-style plugins (which we should
114  * probably do eventually, as all plugins should be written as new-style
115  * ones), we may want to have "init_plugins()" merely save a pointer
116  * to the plugin's "init" routine, just as we save a pointer to its
117  * "reg_handoff" routine, and have a "register_all_plugins()" routine
118  * to go through the list of plugins and call all of them.
119  *
120  * Then we'd have "epan_init()", or perhaps even something higher up
121  * in the call tree, call "init_plugins()", and have "proto_init()"
122  * call "register_all_plugins()" right after calling "register_all_protocols()";
123  * this might be a bit cleaner.
124  */
125 static void
126 plugins_scan_dir(const char *dirname)
127 {
128 #define FILENAME_LEN    1024
129     ETH_DIR       *dir;             /* scanned directory */
130     ETH_DIRENT    *file;            /* current file */
131     const char    *name;
132 #if GLIB_MAJOR_VERSION < 2
133     gchar         *hack_path;       /* pathname used to construct lt_lib_ext */
134     gchar         *lt_lib_ext;      /* extension for loadable modules */
135 #endif
136     gchar          filename[FILENAME_LEN];   /* current file name */
137     GModule       *handle;          /* handle returned by dlopen */
138     gchar         *version;
139     gpointer       gp;
140     void         (*register_protoinfo)(void);
141     void         (*reg_handoff)(void);
142     void         (*register_tap_listener)(void);
143     gchar         *dot;
144     int            cr;
145
146 #if GLIB_MAJOR_VERSION < 2
147     /*
148      * We find the extension used on this platform for loadable modules
149      * by the sneaky hack of calling "g_module_build_path" to build
150      * the pathname for a module with an empty directory name and
151      * empty module name, and then search for the last "." and use
152      * everything from the last "." on.
153      */
154     hack_path = g_module_build_path("", "");
155     lt_lib_ext = strrchr(hack_path, '.');
156     if (lt_lib_ext == NULL)
157     {
158         /*
159          * Does this mean there *is* no extension?  Assume so.
160          *
161          * XXX - the code below assumes that all loadable modules have
162          * an extension....
163          */
164         lt_lib_ext = "";
165     }
166 #endif
167
168     if ((dir = g_dir_open(dirname, 0, NULL)) != NULL)
169     {
170     while ((file = eth_dir_read_name(dir)) != NULL)
171         {
172             name = eth_dir_get_name(file);
173 #if GLIB_MAJOR_VERSION < 2
174             /* don't try to open "." and ".." */
175             if (!(strcmp(name, "..") &&
176                   strcmp(name, "."))) continue;
177
178             /* skip anything but files with lt_lib_ext */
179             dot = strrchr(name, '.');
180             if (dot == NULL || strcmp(dot, lt_lib_ext) != 0) continue;
181
182 #else /* GLIB 2 */
183     /*
184      * GLib 2.x defines G_MODULE_SUFFIX as the extension used on this
185      * platform for loadable modules.
186      */
187             /* skip anything but files with G_MODULE_SUFFIX */
188             dot = strrchr(name, '.');
189             if (dot == NULL || strcmp(dot+1, G_MODULE_SUFFIX) != 0) continue;
190
191 #endif
192             g_snprintf(filename, FILENAME_LEN, "%s" G_DIR_SEPARATOR_S "%s",
193                 dirname, name);
194             if ((handle = g_module_open(filename, 0)) == NULL)
195             {
196                 report_failure("Couldn't load module %s: %s", filename,
197                           g_module_error());
198                 continue;
199             }
200             if (!g_module_symbol(handle, "version", &gp))
201             {
202                 report_failure("The plugin %s has no version symbol", name);
203                 g_module_close(handle);
204                 continue;
205             }
206             version = gp;
207             
208             /*
209              * Do we have a register routine?
210              */
211             if (g_module_symbol(handle, "plugin_register", &gp))
212             {
213                 /*
214                  * Yes - this plugin includes one or more dissectors.
215                  */
216                 register_protoinfo = gp;
217             }
218             else
219             {
220                 /*
221                  * No - no dissectors.
222                  */
223                 register_protoinfo = NULL;
224             }
225
226             /*
227              * Do we have a reg_handoff routine?
228              */
229             if (g_module_symbol(handle, "plugin_reg_handoff", &gp))
230             {
231                 /*
232                  * Yes.
233                  */
234                 reg_handoff = gp;
235             }
236             else
237             {
238                 /*
239                  * No - that's OK even if we have dissectors, as long
240                  * as the plugin registers by name *and* there's
241                  * a caller looking for that name.
242                  */
243                 reg_handoff = NULL;
244             }
245
246             /*
247              * Do we have a register_tap_listener routine?
248              */
249             if (g_module_symbol(handle, "plugin_register_tap_listener", &gp))
250             {
251                 /*
252                  * Yes - this plugin includes one or more taps.
253                  */
254                 register_tap_listener = gp;
255             }
256             else
257             {
258                 /*
259                  * No - no taps here.
260                  */
261                 register_tap_listener = NULL;
262             }
263
264             /*
265              * Do we have an old-style init routine?
266              */
267             if (g_module_symbol(handle, "plugin_init", &gp))
268             {
269                 /*
270                  * Yes - do we also have a register routine or a
271                  * register_tap_listener routine?  If so, this is a bogus
272                  * hybrid of an old-style and new-style plugin.
273                  */
274                 if (register_protoinfo != NULL || register_tap_listener != NULL)
275                 {
276                     report_failure("The plugin %s has an old plugin init routine\nand a new register or register_tap_listener routine.",
277                         name);
278                     g_module_close(handle);
279                     continue;
280                 }
281
282                 /*
283                  * It's just an unsupported old-style plugin;
284                  */
285                 report_failure("The plugin %s has an old plugin init routine. Support has been dropped.\n Information on how to update your plugin is available at \nhttp://anonsvn.ethereal.com/ethereal/trunk/doc/README.plugins",
286                     name);
287                 g_module_close(handle);
288                 continue;
289             }
290
291             /*
292              * Does this dissector do anything useful?
293              */
294             if (register_protoinfo == NULL &&
295                 register_tap_listener == NULL)
296             {
297                 /*
298                  * No.
299                  */
300                 report_failure("The plugin %s has neither a register routine, or a register_tap_listener routine",
301                     name);
302                 g_module_close(handle);
303                 continue;
304             }
305
306             /*
307              * OK, attempt to add it to the list of plugins.
308              */
309             if ((cr = add_plugin(handle, g_strdup(name), version,
310                                  register_protoinfo, reg_handoff,
311                                  register_tap_listener)))
312             {
313                 if (cr == EEXIST)
314                     fprintf(stderr, "The plugin %s, version %s\n"
315                             "was found in multiple directories\n", name, version);
316                 else
317                     fprintf(stderr, "Memory allocation problem\n"
318                             "when processing plugin %s, version %s\n",
319                             name, version);
320                 g_module_close(handle);
321                 continue;
322             }
323
324             /*
325              * Call its register routine if it has one.
326              * XXX - just save this and call it with the built-in
327              * dissector register routines?
328              */
329             if (register_protoinfo != NULL)
330                     register_protoinfo();
331
332         }
333         eth_dir_close(dir);
334         }
335 #if GLIB_MAJOR_VERSION < 2
336     g_free(hack_path);
337 #endif
338 }
339
340
341 /* get the global plugin dir */
342 /* Return value is malloced so the caller should g_free() it. */
343 char *get_plugins_global_dir(const char *plugin_dir)
344 {
345 #ifdef _WIN32
346         char *install_plugin_dir;
347
348         /*
349          * On Windows, the data file directory is the installation
350          * directory; the plugins are stored under it.
351          *
352          * Assume we're running the installed version of Ethereal;
353          * on Windows, the data file directory is the directory
354          * in which the Ethereal binary resides.
355          */
356         install_plugin_dir = g_strdup_printf("%s\\plugins\\%s", get_datafile_dir(), VERSION);
357
358         /*
359          * Make sure that pathname refers to a directory.
360          */
361         if (test_for_directory(install_plugin_dir) != EISDIR) {
362                 /*
363                  * Either it doesn't refer to a directory or it
364                  * refers to something that doesn't exist.
365                  *
366                  * Assume that means we're running, for example,
367                  * a version of Ethereal we've built in a source
368                  * directory, and fall back on the default
369                  * installation directory, so you can put the plugins
370                  * somewhere so they can be used with this version
371                  * of Ethereal.
372                  *
373                  * XXX - should we, instead, have the Windows build
374                  * procedure create a subdirectory of the "plugins"
375                  * source directory, and copy the plugin DLLs there,
376                  * so that you use the plugins from the build tree?
377                  */
378                 g_free(install_plugin_dir);
379                 install_plugin_dir =
380                     g_strdup("C:\\Program Files\\Ethereal\\plugins\\" VERSION);
381         }
382
383         return install_plugin_dir;
384 #else
385         /*
386          * Scan the plugin directory.
387          */
388         return g_strdup(plugin_dir);
389 #endif
390 }
391
392
393 /* get the personal plugin dir */
394 /* Return value is malloced so the caller should g_free() it. */
395 char *get_plugins_pers_dir(void)
396 {
397     return get_persconffile_path(PLUGINS_DIR_NAME, FALSE);
398 }
399
400 /*
401  * init plugins
402  */
403 void
404 init_plugins(const char *plugin_dir)
405 {
406     char *datafile_dir;
407
408     if (plugin_list == NULL)      /* ensure init_plugins is only run once */
409     {
410         /*
411          * Scan the global plugin directory.
412          */
413         datafile_dir = get_plugins_global_dir(plugin_dir);
414         plugins_scan_dir(datafile_dir);
415         g_free(datafile_dir);
416
417         /*
418          * Scan the users plugin directory.
419          */
420         datafile_dir = get_plugins_pers_dir();
421         plugins_scan_dir(datafile_dir);
422         g_free(datafile_dir);
423     }
424 }
425
426 void
427 register_all_plugin_handoffs(void)
428 {
429     plugin *pt_plug;
430
431     /*
432      * For all plugins with register-handoff routines, call the routines.
433      * This is called from "proto_init()"; it must be called after
434      * "register_all_protocols()" and "init_plugins()" are called,
435      * in case one plugin registers itself either with a built-in
436      * dissector or with another plugin; we must first register all
437      * dissectors, whether built-in or plugin, so their dissector tables
438      * are initialized, and only then register all handoffs.
439      */
440     for (pt_plug = plugin_list; pt_plug != NULL; pt_plug = pt_plug->next)
441     {
442         if (pt_plug->reg_handoff)
443             (pt_plug->reg_handoff)();
444     }
445 }
446
447 void
448 register_all_plugin_tap_listeners(void)
449 {
450     plugin *pt_plug;
451
452     /*
453      * For all plugins with register-tap-listener routines, call the
454      * routines.
455      */
456     for (pt_plug = plugin_list; pt_plug != NULL; pt_plug = pt_plug->next)
457     {
458         if (pt_plug->register_tap_listener)
459             (pt_plug->register_tap_listener)();
460     }
461 }
462 #endif