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