Bugfix from Lars Roland: included missing xdlc.h
[obnox/wireshark/wip.git] / epan / plugins.c
1 /* plugins.c
2  * plugin routines
3  *
4  * $Id: plugins.c,v 1.78 2004/01/05 15:02:08 ulfl 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 "asn1.h"
70 #include "packet-per.h"
71 #include "xdlc.h"
72 #include "plugins/plugin_table.h"
73 static plugin_address_table_t   patable = {
74 /* file generated by plugin_gen.py */
75 #include "plugins/Xass-list"
76 };
77 #endif
78
79 /* linked list of all plugins */
80 plugin *plugin_list;
81
82 static gchar *user_plug_dir = NULL;
83
84 #define PLUGINS_DIR_NAME        "plugins"
85
86 /*
87  * add a new plugin to the list
88  * returns :
89  * - 0 : OK
90  * - ENOMEM : memory allocation problem
91  * - EEXIST : the same plugin (i.e. name/version) was already registered.
92  */
93 static int
94 add_plugin(void *handle, gchar *name, gchar *version,
95            void (*reg_handoff)(void))
96 {
97     plugin *new_plug, *pt_plug;
98
99     pt_plug = plugin_list;
100     if (!pt_plug) /* the list is empty */
101     {
102         new_plug = (plugin *)g_malloc(sizeof(plugin));
103         if (new_plug == NULL) return ENOMEM;
104         plugin_list = new_plug;
105     }
106     else
107     {
108         while (1)
109         {
110             /* check if the same name/version is already registered */
111             if (!strcmp(pt_plug->name, name) &&
112                 !strcmp(pt_plug->version, version))
113             {
114                 return EEXIST;
115             }
116
117             /* we found the last plugin in the list */
118             if (pt_plug->next == NULL) break;
119
120             pt_plug = pt_plug->next;
121         }
122         new_plug = (plugin *)g_malloc(sizeof(plugin));
123         if (new_plug == NULL) return ENOMEM;
124         pt_plug->next = new_plug;
125     }
126
127     new_plug->handle = handle;
128     new_plug->name = name;
129     new_plug->version = version;
130     new_plug->reg_handoff = reg_handoff;
131     new_plug->next = NULL;
132     return 0;
133 }
134
135 /*
136  * XXX - when we remove support for old-style plugins (which we should
137  * probably do eventually, as all plugins should be written as new-style
138  * ones), we may want to have "init_plugins()" merely save a pointer
139  * to the plugin's "init" routine, just as we save a pointer to its
140  * "reg_handoff" routine, and have a "register_all_plugins()" routine
141  * to go through the list of plugins and call all of them.
142  *
143  * Then we'd have "epan_init()", or perhaps even something higher up
144  * in the call tree, call "init_plugins()", and have "proto_init()"
145  * call "register_all_plugins()" right after calling "register_all_protocols()";
146  * this might be a bit cleaner.
147  */
148 static void
149 plugins_scan_dir(const char *dirname)
150 {
151 #define FILENAME_LEN    1024
152 #if GLIB_MAJOR_VERSION < 2
153     gchar         *hack_path;       /* pathname used to construct lt_lib_ext */
154     gchar         *lt_lib_ext;      /* extension for loadable modules */
155     DIR           *dir;             /* scanned directory */
156     struct dirent *file;            /* current file */
157     gchar         *name;
158 #else /* GLIB 2 */
159     GDir          *dir;             /* scanned directory */
160     GError        **dummy;
161     const gchar   *name;
162 #endif
163     gchar          filename[FILENAME_LEN];   /* current file name */
164     GModule       *handle;          /* handle returned by dlopen */
165     gchar         *version;
166     void         (*init)(void *);
167     void         (*reg_handoff)(void);
168     gchar         *dot;
169     int            cr;
170
171 #if GLIB_MAJOR_VERSION < 2
172     /*
173      * We find the extension used on this platform for loadable modules
174      * by the sneaky hack of calling "g_module_build_path" to build
175      * the pathname for a module with an empty directory name and
176      * empty module name, and then search for the last "." and use
177      * everything from the last "." on.
178      */
179     hack_path = g_module_build_path("", "");
180     lt_lib_ext = strrchr(hack_path, '.');
181     if (lt_lib_ext == NULL)
182     {
183         /*
184          * Does this mean there *is* no extension?  Assume so.
185          *
186          * XXX - the code below assumes that all loadable modules have
187          * an extension....
188          */
189         lt_lib_ext = "";
190     }
191
192     if ((dir = opendir(dirname)) != NULL)
193     {
194         while ((file = readdir(dir)) != NULL)
195         {
196             /* don't try to open "." and ".." */
197             if (!(strcmp(file->d_name, "..") &&
198                   strcmp(file->d_name, "."))) continue;
199
200             /* skip anything but files with lt_lib_ext */
201             dot = strrchr(file->d_name, '.');
202             if (dot == NULL || strcmp(dot, lt_lib_ext) != 0) continue;
203
204             snprintf(filename, FILENAME_LEN, "%s" G_DIR_SEPARATOR_S "%s",
205                 dirname, file->d_name);
206             name = (gchar *)file->d_name;
207 #else /* GLIB 2 */
208     /*
209      * GLib 2.x defines G_MODULE_SUFFIX as the extension used on this
210      * platform for loadable modules.
211      */
212     dummy = g_malloc(sizeof(GError *));
213     *dummy = NULL;
214     if ((dir = g_dir_open(dirname, 0, dummy)) != NULL)
215     {
216         while ((name = g_dir_read_name(dir)) != NULL)
217         {
218             /* skip anything but files with G_MODULE_SUFFIX */
219             dot = strrchr(name, '.');
220             if (dot == NULL || strcmp(dot+1, G_MODULE_SUFFIX) != 0) continue;
221
222             snprintf(filename, FILENAME_LEN, "%s" G_DIR_SEPARATOR_S "%s",
223                 dirname, name);
224 #endif
225             if ((handle = g_module_open(filename, 0)) == NULL)
226             {
227                 g_warning("Couldn't load module %s: %s", filename,
228                           g_module_error());
229                 continue;
230             }
231             if (g_module_symbol(handle, "version", (gpointer*)&version) == FALSE)
232             {
233                 g_warning("The plugin %s has no version symbol", name);
234                 g_module_close(handle);
235                 continue;
236             }
237
238             /*
239              * Old-style dissectors don't have a "plugin_reg_handoff()"
240              * routine; we no longer support them.
241              *
242              * New-style dissectors have one, because, otherwise, there's
243              * no way for them to arrange that they ever be called.
244              */
245             if (g_module_symbol(handle, "plugin_reg_handoff",
246                                          (gpointer*)&reg_handoff))
247             {
248                 /*
249                  * We require it to have a "plugin_init()" routine.
250                  */
251                 if (!g_module_symbol(handle, "plugin_init", (gpointer*)&init))
252                 {
253                     g_warning("The plugin %s has a plugin_reg_handoff symbol but no plugin_init routine",
254                               name);
255                     g_module_close(handle);
256                     continue;
257                 }
258
259                 /*
260                  * We have a "plugin_reg_handoff()" routine, so we don't
261                  * need the protocol, filter string, or dissector pointer.
262                  */
263                 if ((cr = add_plugin(handle, g_strdup(name), version,
264                                      reg_handoff)))
265                 {
266                     if (cr == EEXIST)
267                         fprintf(stderr, "The plugin %s, version %s\n"
268                             "was found in multiple directories\n", name, version);
269                     else
270                         fprintf(stderr, "Memory allocation problem\n"
271                             "when processing plugin %s, version %s\n",
272                             name, version);
273                     g_module_close(handle);
274                     continue;
275                 }
276
277                 /*
278                  * Call its init routine.
279                  */
280 #ifdef PLUGINS_NEED_ADDRESS_TABLE
281                 init(&patable);
282 #else
283                 init(NULL);
284 #endif
285             }
286             else
287             {
288                 /*
289                  * This is an old-style dissector; warn that it won't
290                  * be used, as those aren't supported.
291                  */
292                 fprintf(stderr,
293                     "The plugin %s, version %s is an old-style plugin;\n"
294                     "Those are no longer supported.\n", name, version);
295             }
296         }
297 #if GLIB_MAJOR_VERSION < 2
298         closedir(dir);
299     }
300     g_free(hack_path);
301 #else /* GLIB 2 */
302         g_dir_close(dir);
303     }
304     g_clear_error(dummy);
305     g_free(dummy);
306 #endif
307 }
308
309 /*
310  * init plugins
311  */
312 void
313 init_plugins(const char *plugin_dir)
314 {
315 #ifdef WIN32
316     const char *datafile_dir;
317     char *install_plugin_dir;
318 #endif
319
320     if (plugin_list == NULL)      /* ensure init_plugins is only run once */
321     {
322 #ifdef WIN32
323         /*
324          * On Windows, the data file directory is the installation
325          * directory; the plugins are stored under it.
326          *
327          * Assume we're running the installed version of Ethereal;
328          * on Windows, the data file directory is the directory
329          * in which the Ethereal binary resides.
330          */
331         datafile_dir = get_datafile_dir();
332         install_plugin_dir = g_malloc(strlen(datafile_dir) + strlen("plugins") +
333             strlen(VERSION) + 3);
334         sprintf(install_plugin_dir, "%s\\plugins\\%s", datafile_dir, VERSION);
335
336         /*
337          * Make sure that pathname refers to a directory.
338          */
339         if (test_for_directory(install_plugin_dir) != EISDIR) {
340                 /*
341                  * Either it doesn't refer to a directory or it
342                  * refers to something that doesn't exist.
343                  *
344                  * Assume that means we're running, for example,
345                  * a version of Ethereal we've built in a source
346                  * directory, and fall back on the default
347                  * installation directory, so you can put the plugins
348                  * somewhere so they can be used with this version
349                  * of Ethereal.
350                  *
351                  * XXX - should we, instead, have the Windows build
352                  * procedure create a subdirectory of the "plugins"
353                  * source directory, and copy the plugin DLLs there,
354                  * so that you use the plugins from the build tree?
355                  */
356                 install_plugin_dir =
357                     g_strdup("C:\\Program Files\\Ethereal\\plugins\\" VERSION);
358         }
359
360         /*
361          * Scan that directory.
362          */
363         plugins_scan_dir(install_plugin_dir);
364         g_free(install_plugin_dir);
365 #else
366         /*
367          * Scan the plugin directory.
368          */
369         plugins_scan_dir(plugin_dir);
370 #endif
371         if (!user_plug_dir)
372             user_plug_dir = get_persconffile_path(PLUGINS_DIR_NAME, FALSE);
373         plugins_scan_dir(user_plug_dir);
374     }
375 }
376
377 void
378 register_all_plugin_handoffs(void)
379 {
380   plugin *pt_plug;
381
382   /*
383    * For all new-style plugins, call the register-handoff routine.
384    * This is called from "proto_init()"; it must be called after
385    * "register_all_protocols()" and "init_plugins()" are called,
386    * in case one plugin registers itself either with a built-in
387    * dissector or with another plugin; we must first register all
388    * dissectors, whether built-in or plugin, so their dissector tables
389    * are initialized, and only then register all handoffs.
390    *
391    * We treat those protocols as always being enabled; they should
392    * use the standard mechanism for enabling/disabling protocols, not
393    * the plugin-specific mechanism.
394    */
395   for (pt_plug = plugin_list; pt_plug != NULL; pt_plug = pt_plug->next)
396     (pt_plug->reg_handoff)();
397 }
398 #endif