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