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