Heikki Vatiainen's patch to add a flag to control whether to interpret
[obnox/wireshark/wip.git] / plugins.c
1 /* plugins.c
2  * plugin routines
3  *
4  * $Id: plugins.c,v 1.5 2000/01/15 00:22:34 gram Exp $
5  *
6  * Ethereal - Network traffic analyzer
7  * By Gerald Combs <gerald@zing.org>
8  * Copyright 1999 Gerald Combs
9  *
10  * 
11  * This program is free software; you can redistribute it and/or
12  * modify it under the terms of the GNU General Public License
13  * as published by the Free Software Foundation; either version 2
14  * of the License, or (at your option) any later version.
15  * 
16  * This program is distributed in the hope that it will be useful,
17  * but WITHOUT ANY WARRANTY; without even the implied warranty of
18  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
19  * GNU General Public License for more details.
20  * 
21  * You should have received a copy of the GNU General Public License
22  * along with this program; if not, write to the Free Software
23  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
24  */
25
26 #ifdef HAVE_CONFIG_H
27 # include "config.h"
28 #endif
29
30 #include "plugins.h"
31
32 #ifdef HAVE_PLUGINS
33
34 #include <time.h>
35
36 #ifdef HAVE_DIRENT_H
37 #include <dirent.h>
38 #endif
39
40 #ifdef HAVE_DIRECT_H
41 #include <direct.h>
42 #endif
43
44 #include <string.h>
45 #include <stdlib.h>
46 #include <sys/stat.h>
47 #include <sys/types.h>
48
49 #ifdef HAVE_FCNTL_H
50 #include <fcntl.h>
51 #endif
52
53 #ifdef HAVE_UNISTD_H
54 #include <unistd.h>
55 #endif
56
57 #include "globals.h"
58
59
60 /* linked list of all plugins */
61 plugin *plugin_list;
62
63 static gchar std_plug_dir[] = "/usr/lib/ethereal/plugins/0.8";
64 static gchar local_plug_dir[] = "/usr/local/lib/ethereal/plugins/0.8";
65 static gchar *user_plug_dir = NULL;
66 static gchar *plugin_status_file = NULL;
67
68 /*
69  * add a new plugin to the list
70  * returns :
71  * - 0 : OK
72  * - ENOMEM : memory allocation problem
73  * - EEXIST : the same plugin (i.e. name/version) was already registered.
74  */
75 int
76 add_plugin(void *handle, gchar *name, gchar *version, gchar *protocol,
77            gchar *filter_string, dfilter *filter,
78            void (*dissector) (const u_char *,
79                               int,
80                               frame_data *,
81                               proto_tree *))
82 {
83     plugin *new_plug, *pt_plug;
84
85     pt_plug = plugin_list;
86     if (!pt_plug) /* the list is empty */
87     {
88         new_plug = (plugin *)g_malloc(sizeof(plugin));
89         if (new_plug == NULL) return ENOMEM;
90         plugin_list = new_plug;
91     }
92     else
93     {
94         while (1)
95         {
96             /* check if the same name/version is already registered */
97             if (!strcmp(pt_plug->name, name) &&
98                 !strcmp(pt_plug->version, version))
99             {
100                 return EEXIST;
101             }
102
103             /* we found the last plugin in the list */
104             if (pt_plug->next == NULL) break;
105
106             pt_plug = pt_plug->next;
107         }
108         new_plug = (plugin *)g_malloc(sizeof(plugin));
109         if (new_plug == NULL) return ENOMEM;
110         pt_plug->next = new_plug;
111     }
112
113     new_plug->handle = handle;
114     new_plug->name = name;
115     new_plug->version = version;
116     new_plug->enabled = FALSE;
117     new_plug->protocol = protocol;
118     new_plug->filter_string = g_strdup(filter_string);
119     new_plug->filter = filter;
120     new_plug->dissector = dissector;
121     new_plug->next = NULL;
122     return 0;
123 }
124
125 /*
126  * enable a plugin
127  * returns a pointer to the enabled plugin, or NULL if the plugin wasn't found
128  * in the list
129  */
130 void *
131 enable_plugin(const gchar *name, const gchar *version)
132 {
133     plugin *pt_plug;
134
135     pt_plug = plugin_list;
136     while (pt_plug)
137     {
138         if (!strcmp(pt_plug->name, name) && !strcmp(pt_plug->version, version))
139         {
140             pt_plug->enabled = TRUE;
141             return pt_plug;
142         }
143         pt_plug = pt_plug->next;
144     }
145     return NULL;
146 }
147
148 /*
149  * disable a plugin
150  * returns a pointer to the disabled plugin, or NULL if the plugin wasn't found
151  * in the list
152  */
153 void *
154 disable_plugin(const gchar *name, const gchar *version)
155 {
156     plugin *pt_plug;
157
158     pt_plug = plugin_list;
159     while (pt_plug)
160     {
161         if (!strcmp(pt_plug->name, name) && !strcmp(pt_plug->version, version))
162         {
163             pt_plug->enabled = FALSE;
164             return pt_plug;
165         }
166         pt_plug = pt_plug->next;
167     }
168     return NULL;
169 }
170
171 /*
172  * find a plugin using its name/version
173  */
174 void *
175 find_plugin(const gchar *name, const gchar *version)
176 {
177     plugin *pt_plug;
178
179     pt_plug = plugin_list;
180     while (pt_plug)
181     {
182         if (!strcmp(pt_plug->name, name) && !strcmp(pt_plug->version, version))
183         {
184             return pt_plug;
185         }
186         pt_plug = pt_plug->next;
187     }
188     return NULL;
189 }
190
191 /*
192  * check if a plugin is enabled
193  */
194 gboolean
195 is_enabled(const gchar *name, const gchar *version)
196 {
197     plugin *pt_plug;
198
199     pt_plug = plugin_list;
200     while (pt_plug)
201     {
202         if (!strcmp(pt_plug->name, name) && !strcmp(pt_plug->version, version))
203             return pt_plug->enabled;
204         pt_plug = pt_plug->next;
205     }
206     return FALSE;
207 }
208
209 /*
210  * replace the filter used by a plugin (filter string and dfilter)
211  */
212 void
213 plugin_replace_filter(const gchar *name, const gchar *version,
214         const gchar *filter_string, dfilter *filter)
215 {
216     plugin *pt_plug;
217
218     pt_plug = plugin_list;
219     while (pt_plug)
220     {
221         if (!strcmp(pt_plug->name, name) && !strcmp(pt_plug->version, version))
222         {
223             g_free(pt_plug->filter_string);
224             pt_plug->filter_string = g_strdup(filter_string);
225             dfilter_destroy(pt_plug->filter);
226             pt_plug->filter = filter;
227             return;
228         }
229         pt_plug = pt_plug->next;
230     }
231 }
232
233 /*
234  * save plugin status, returns 0 on success, -1 on failure:
235  * file format :
236  * for each plugin, two lines are saved :
237  * plugin_name plugin_version [0|1]    (0: disabled, 1: enabled)
238  * filter_string
239  *
240  * Ex :
241  * gryphon.so 0.8.0 1
242  * tcp.port == 7000
243  */
244 int
245 save_plugin_status()
246 {
247     gchar  *pf_path;
248     FILE   *statusfile;
249     plugin *pt_plug;
250
251     if (!plugin_status_file) {
252         plugin_status_file = (gchar *)g_malloc(strlen(getenv("HOME")) + 26);
253         sprintf(plugin_status_file, "%s/%s/plugins.status", getenv("HOME"), PF_DIR);
254     }
255     statusfile=fopen(plugin_status_file, "w");
256     if (!statusfile) {
257         pf_path = g_malloc(strlen(getenv("HOME")) + strlen(PF_DIR) + 2);
258         sprintf(pf_path, "%s/%s", getenv("HOME"), PF_DIR);
259         #ifdef WIN32
260         mkdir(pf_path);
261         #else
262         mkdir(pf_path, 0755);
263         #endif
264         g_free(pf_path);
265         statusfile=fopen(plugin_status_file, "w");
266         if (!statusfile) return -1;
267     }
268
269     pt_plug = plugin_list;
270     while (pt_plug)
271     {
272         fprintf(statusfile,"%s %s %s\n%s\n", pt_plug->name, pt_plug->version,
273                 (pt_plug->enabled ? "1" : "0"), pt_plug->filter_string);
274         pt_plug = pt_plug->next;
275     }
276     fclose(statusfile);
277     return 0;
278 }
279
280 /*
281  * Check if the status of this plugin has been saved.
282  * If necessary, enable the plugin, and change the filter.
283  */
284 static void
285 check_plugin_status(gchar *name, gchar *version, GModule *handle,
286                     gchar *filter_string, FILE *statusfile)
287 {
288     gchar   *ref_string;
289     guint16  ref_string_len;
290     gchar    line[512];
291     void   (*proto_init)();
292     dfilter *filter;
293
294     if (!statusfile) return;
295
296     ref_string = (gchar *)g_malloc(strlen(name) + strlen(version) + 2);
297     ref_string_len = sprintf(ref_string, "%s %s", name, version);
298
299     while (!feof(statusfile))
300     {
301         if (fgets(line, 512, statusfile) == NULL) return;
302         if (strncmp(line, ref_string, ref_string_len) != 0) { /* not the right plugin */
303             if (fgets(line, 512, statusfile) == NULL) return;
304         }
305         else { /* found the plugin */
306             if (line[ref_string_len+1] == '1') {
307                 enable_plugin(name, version);
308                 if (g_module_symbol(handle, "proto_init", (gpointer*)&proto_init) == TRUE) {
309                     proto_init();
310                 }
311             }
312
313             if (fgets(line, 512, statusfile) == NULL) return;
314             if (line[strlen(line)-1] == '\n') line[strlen(line)-1] = '\0';
315             /* only compile the new filter if it is different from the default */
316             if (strcmp(line, filter_string) && dfilter_compile(line, &filter) == 0)
317                 plugin_replace_filter(name, version, line, filter);
318             return;
319         }
320     }
321     g_free(ref_string);
322 }
323
324 static void
325 plugins_scan_dir(const char *dirname)
326 {
327     DIR           *dir;             /* scanned directory */
328     struct dirent *file;            /* current file */
329     gchar          filename[512];   /* current file name */
330     GModule       *handle;          /* handle returned by dlopen */
331     gchar         *name;
332     gchar         *version;
333     gchar         *protocol;
334     gchar         *filter_string;
335     gchar         *dot;
336     dfilter       *filter = NULL;
337     void         (*dissector) (const u_char *, int, frame_data *, proto_tree *);
338     int            cr;
339     FILE          *statusfile;
340
341 #ifdef WIN32
342 #define LT_LIB_EXT ".dll"
343 #else
344 #define LT_LIB_EXT ".la"
345 #endif
346
347     if (!plugin_status_file)
348     {
349         plugin_status_file = (gchar *)g_malloc(strlen(getenv("HOME")) + 26);
350         sprintf(plugin_status_file, "%s/%s/plugins.status", getenv("HOME"), PF_DIR);
351     }
352     statusfile = fopen(plugin_status_file, "r");
353
354     if ((dir = opendir(dirname)) != NULL)
355     {
356         while ((file = readdir(dir)) != NULL)
357         {
358             /* don't try to open "." and ".." */
359             if (!(strcmp(file->d_name, "..") &&
360                   strcmp(file->d_name, "."))) continue;
361
362             /* skip anything but .la */
363             dot = strrchr(file->d_name, '.');
364             if (dot == NULL || ! strcmp(dot, LT_LIB_EXT)) continue;
365
366             sprintf(filename, "%s/%s", dirname, file->d_name);
367
368             if ((handle = g_module_open(filename, 0)) == NULL) continue;
369             name = (gchar *)file->d_name;
370             if (g_module_symbol(handle, "version", (gpointer*)&version) == FALSE)
371             {
372                 g_module_close(handle);
373                 continue;
374             }
375             if (g_module_symbol(handle, "protocol", (gpointer*)&protocol) == FALSE)
376             {
377                 g_module_close(handle);
378                 continue;
379             }
380             if (g_module_symbol(handle, "filter_string", (gpointer*)&filter_string) == FALSE)
381             {
382                 g_module_close(handle);
383                 continue;
384             }
385             if (dfilter_compile(filter_string, &filter) != 0) {
386                 g_module_close(handle);
387                 continue;
388             }
389             if (g_module_symbol(handle, "dissector", (gpointer*)&dissector) == FALSE) {
390                 if (filter != NULL)
391                     dfilter_destroy(filter);
392                 g_module_close(handle);
393                 continue;
394             }
395
396             if ((cr = add_plugin(handle, g_strdup(file->d_name), version,
397                                  protocol, filter_string, filter, dissector)))
398             {
399                 if (cr == EEXIST)
400                     fprintf(stderr, "The plugin : %s, version %s\n"
401                             "was found in multiple directories\n", name, version);
402                 else
403                     fprintf(stderr, "Memory allocation problem\n"
404                             "when processing plugin %s, version %sn",
405                             name, version);
406                 if (filter != NULL)
407                     dfilter_destroy(filter);
408                 g_module_close(handle);
409                 continue;
410             }
411             if (statusfile) {
412                 check_plugin_status(file->d_name, version, handle,
413                                     filter_string, statusfile);
414                 rewind(statusfile);
415             }
416         }
417         closedir(dir);
418     }
419     if (statusfile) fclose(statusfile);
420 }
421
422 /*
423  * init plugins
424  */
425 void
426 init_plugins()
427 {
428     if (plugin_list == NULL)      /* ensure init_plugins is only run once */
429     {
430         plugins_scan_dir(std_plug_dir);
431         plugins_scan_dir(local_plug_dir);
432         if ((strcmp(std_plug_dir, PLUGIN_DIR) != 0) &&
433             (strcmp(local_plug_dir, PLUGIN_DIR) != 0))
434         {
435           plugins_scan_dir(PLUGIN_DIR);
436         }
437         if (!user_plug_dir)
438         {
439             user_plug_dir = (gchar *)g_malloc(strlen(getenv("HOME")) + 19);
440             sprintf(user_plug_dir, "%s/%s/plugins", getenv("HOME"), PF_DIR);
441         }
442         plugins_scan_dir(user_plug_dir);
443     }
444 }
445
446 #endif