2 * An API for Wireshark plugins
4 * This enables wireshark dissectors, especially those implemented by plugins
5 * to register menubar entries, which then will call a pre-defined callback
6 * function for the dissector or plugin.
8 * Also it implements additional methods, which allow plugins to interoperate
11 * Wireshark - Network traffic analyzer
12 * By Gerald Combs <gerald@wireshark.org>
13 * Copyright 1998 Gerald Combs
15 * This program is free software; you can redistribute it and/or
16 * modify it under the terms of the GNU General Public License
17 * as published by the Free Software Foundation; either version 2
18 * of the License, or (at your option) any later version.
20 * This program is distributed in the hope that it will be useful,
21 * but WITHOUT ANY WARRANTY; without even the implied warranty of
22 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
23 * GNU General Public License for more details.
25 * You should have received a copy of the GNU General Public License
26 * along with this program; if not, write to the Free Software
27 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
33 #include <epan/epan.h>
34 #include <epan/proto.h>
36 #include "plugin_if.h"
38 static GList * menubar_entries = NULL;
39 static GList * menubar_menunames = NULL;
42 extern GList * ext_menubar_get_entries(void)
44 return menubar_entries;
47 extern ext_menu_t * ext_menubar_register_menu(int proto_id, const gchar * menulabel,
50 ext_menubar_t * entry = NULL;
53 /* A name for the entry must be provided */
54 g_assert(menulabel != NULL && strlen ( menulabel ) > 0 );
56 /* A protocol must exist for the given id */
57 g_assert(find_protocol_by_id(proto_id) != NULL);
59 /* Create unique name, which is used by GTK to provide the menu */
60 name = g_strconcat(proto_get_protocol_filter_name(proto_id), "Menu", NULL);
62 /* For now, a protocol may only register one main menu */
63 g_assert(g_list_find(menubar_menunames, name) == NULL);
65 entry = (ext_menubar_t *)g_malloc0(sizeof(ext_menubar_t));
66 entry->type = EXT_MENUBAR_MENU;
67 entry->proto = proto_id;
68 entry->is_plugin = is_plugin;
70 entry->parent_menu = 0;
72 /* Create a name for this submenu */
74 entry->label = g_strdup(menulabel);
75 entry->tooltip = g_strdup(menulabel);
77 entry->submenu_cnt = 0;
80 menubar_entries = g_list_append(menubar_entries, entry);
81 menubar_menunames = g_list_append(menubar_menunames, name);
86 extern ext_menu_t * ext_menubar_set_parentmenu(ext_menu_t * menu, const gchar * parentmenu)
88 g_assert(menu != NULL && menu->parent == NULL);
90 g_assert(parentmenu != 0);
92 menu->parent_menu = g_strdup(parentmenu);
97 extern ext_menu_t * ext_menubar_add_submenu(ext_menu_t * parent, const gchar *menulabel)
99 ext_menubar_t * entry = NULL;
101 /* A name for the entry must be provided */
102 g_assert(menulabel != NULL && strlen ( menulabel ) > 0 );
104 /* Parent must be a valid parent */
105 g_assert(parent != NULL && parent->type == EXT_MENUBAR_MENU);
107 parent->submenu_cnt++;
109 /* Create submenu entry */
110 entry = (ext_menubar_t *)g_malloc0(sizeof(ext_menubar_t));
111 entry->type = EXT_MENUBAR_MENU;
112 entry->parent = parent;
113 /* Just a convenience */
114 entry->proto = parent->proto;
115 entry->is_plugin = parent->is_plugin;
116 /* Create unique name, which is used by GTK to provide the menu */
117 entry->name = g_strdup_printf("%sS%02d", parent->name, parent->submenu_cnt);
118 entry->label = g_strdup(menulabel);
119 entry->tooltip = g_strdup(menulabel);
121 parent->children = g_list_append(parent->children, entry);
126 static void ext_menubar_add_generic_entry (
127 ext_menubar_entry_t type, ext_menu_t * parent, const gchar * label,
128 const gchar * tooltip, ext_menubar_action_cb callback, gpointer user_data )
130 ext_menubar_t * entry = NULL;
132 /* A valid parent must exist */
133 g_assert(parent != NULL && parent->type == EXT_MENUBAR_MENU);
134 /* A label for the entry must be provided */
135 g_assert(label != NULL && strlen ( label ) > 0 );
139 /* Create menu entry */
140 entry = (ext_menubar_t*)g_malloc0(sizeof(ext_menubar_t));
142 /* Create unique name, which is used by GTK to provide the menu */
143 entry->name = g_strdup_printf("%sI%02d", parent->name, parent->item_cnt);
144 entry->label = g_strdup(label);
146 if ( tooltip != NULL && strlen(tooltip) > 0 )
147 entry->tooltip = g_strdup(tooltip);
149 entry->callback = callback;
150 entry->user_data = user_data;
152 parent->children = g_list_append(parent->children, entry);
155 extern void ext_menubar_add_entry(ext_menu_t * parent, const gchar *label,
156 const gchar *tooltip, ext_menubar_action_cb callback, gpointer user_data)
158 /* A callback must be provided */
159 g_assert(callback != NULL);
161 ext_menubar_add_generic_entry ( EXT_MENUBAR_ITEM, parent, label, tooltip, callback, user_data );
164 extern void ext_menubar_add_website(ext_menu_t * parent, const gchar *label,
165 const gchar *tooltip, const gchar *url)
167 /* An url for the entry must be provided */
168 g_assert(url != NULL && strlen ( url ) > 0 );
170 ext_menubar_add_generic_entry ( EXT_MENUBAR_URL, parent, label, tooltip, NULL, (gpointer) g_strdup(url) );
173 extern void ext_menubar_add_separator(ext_menu_t *parent)
175 ext_menubar_add_generic_entry ( EXT_MENUBAR_SEPARATOR, parent, g_strdup("-"), NULL, NULL, NULL );
178 /* Implementation of GUI callback methods follows.
179 * This is a necessity, as using modern UI systems, gui interfaces often operate
180 * in different threads then the calling application. Even more so, if the calling
181 * application is implemented using a separate plugin. Therefore the external menubars
182 * cannot call gui functionality directly, the gui has to perform the function within
185 static GHashTable * plugin_if_callback_functions;
188 plugin_if_init_hashtable(void)
190 if ( plugin_if_callback_functions == 0 )
191 plugin_if_callback_functions = g_hash_table_new(g_int_hash, g_int_equal);
194 static void plugin_if_call_gui_cb(plugin_if_callback_t actionType, GHashTable * dataSet)
196 plugin_if_gui_cb action;
199 key = (gint *)g_malloc0(sizeof(gint));
200 *key = (gint) actionType;
202 plugin_if_init_hashtable();
204 if ( g_hash_table_size(plugin_if_callback_functions) != 0 )
206 if ( g_hash_table_lookup_extended(plugin_if_callback_functions, key, NULL, (gpointer*)&action) )
208 if ( action != NULL )
214 extern void plugin_if_apply_filter(const char * filter_string, gboolean force)
216 plugin_if_callback_t actionType;
217 GHashTable * dataSet = NULL;
219 actionType = ( force == TRUE ) ? PLUGIN_IF_FILTER_ACTION_APPLY : PLUGIN_IF_FILTER_ACTION_PREPARE;
220 dataSet = g_hash_table_new(g_str_hash, g_str_equal);
222 g_hash_table_insert( dataSet, g_strdup("action_type"), (gpointer) &actionType );
223 g_hash_table_insert( dataSet, g_strdup("filter_string"), g_strdup(filter_string) );
224 g_hash_table_insert( dataSet, g_strdup("force"), (gpointer) &force );
226 plugin_if_call_gui_cb(actionType, dataSet);
229 extern void plugin_if_goto_frame(guint32 framenr)
231 GHashTable * dataSet = NULL;
233 dataSet = g_hash_table_new(g_str_hash, g_str_equal);
235 g_hash_table_insert( dataSet, g_strdup("frame_nr"), GUINT_TO_POINTER(framenr) );
237 plugin_if_call_gui_cb(PLUGIN_IF_GOTO_FRAME, dataSet);
240 extern void plugin_if_save_preference(const char * pref_module, const char * pref_key, const char * pref_value)
242 GHashTable * dataSet = NULL;
244 dataSet = g_hash_table_new(g_str_hash, g_str_equal);
246 g_hash_table_insert( dataSet, g_strdup("pref_module"), g_strdup(pref_module) );
247 g_hash_table_insert( dataSet, g_strdup("pref_key"), g_strdup(pref_key) );
248 g_hash_table_insert( dataSet, g_strdup("pref_value"), g_strdup(pref_value) );
250 plugin_if_call_gui_cb(PLUGIN_IF_PREFERENCE_SAVE, dataSet);
253 extern void plugin_if_get_ws_info(ws_info_t **ws_info_ptr)
255 static ws_info_t ws_info = { FALSE, FILE_CLOSED, NULL, 0, 0, FALSE };
258 GHashTable * dataSet;
259 gchar * pluginKey = g_strdup("ws_info");
261 dataSet = g_hash_table_new(g_str_hash, g_str_equal);
263 g_hash_table_insert(dataSet, pluginKey, &ws_info);
265 plugin_if_call_gui_cb(PLUGIN_IF_GET_WS_INFO, dataSet);
267 g_hash_table_destroy(dataSet);
272 /* Initialise the ws_info structure */
274 ws_info.ws_info_supported = FALSE;
275 ws_info.cf_count = 0;
276 ws_info.cf_filename = NULL;
277 ws_info.cf_framenr = 0;
278 ws_info.frame_passed_dfilter = FALSE;
279 ws_info.cf_state = FILE_CLOSED;
281 #endif /* HAVE_LIBPCAP */
283 *ws_info_ptr = &ws_info;
286 extern void plugin_if_register_gui_cb(plugin_if_callback_t actionType, plugin_if_gui_cb callback)
290 key = (gint *)g_malloc0(sizeof(gint));
293 plugin_if_init_hashtable();
295 if ( ! g_hash_table_lookup_extended(plugin_if_callback_functions, key, NULL, NULL ) )
296 g_hash_table_insert(plugin_if_callback_functions, key, (gpointer)callback);
305 * indent-tabs-mode: nil
308 * ex: set shiftwidth=4 tabstop=8 expandtab:
309 * :indentSize=4:tabSize=8:noTabs=true: