drsuapi_SupportedExtensionsExt
[wireshark-sm.git] / epan / funnel.c
blob79d3bea298879079c4f879c772236efa87b849ff
1 /*
2 * funnel.c
4 * EPAN's GUI mini-API
6 * (c) 2006, Luis E. Garcia Ontanon <luis@ontanon.org>
8 * Wireshark - Network traffic analyzer
9 * By Gerald Combs <gerald@wireshark.org>
10 * Copyright 1998 Gerald Combs
12 * SPDX-License-Identifier: GPL-2.0-or-later
15 #include "config.h"
17 #include <epan/funnel.h>
18 #include <wsutil/glib-compat.h>
20 typedef struct _funnel_menu_t {
21 char *name;
22 register_stat_group_t group;
23 funnel_menu_callback callback;
24 void *callback_data;
25 funnel_menu_callback_data_free callback_data_free;
26 bool retap;
27 struct _funnel_menu_t* next;
28 } funnel_menu_t;
30 typedef struct _console_menu {
31 char *name;
32 funnel_console_eval_cb_t eval_cb;
33 funnel_console_open_cb_t open_cb;
34 funnel_console_close_cb_t close_cb;
35 void *user_data;
36 funnel_console_data_free_cb_t data_free_cb;
37 } funnel_console_menu_t;
39 /* XXX This assumes one main window and one capture file. */
40 static const funnel_ops_t* ops;
41 static funnel_menu_t* registered_menus;
42 static funnel_menu_t* added_menus;
43 static funnel_menu_t* removed_menus;
44 static bool menus_registered;
46 /**
47 * Represents a single packet menu entry and callback
49 typedef struct _funnel_packet_menu_t {
50 char *name; /**< Name to display in the GUI */
51 char *required_fields; /**< comma-separated list of fields
52 that must be present for the
53 packet menu to be displayed */
54 funnel_packet_menu_callback callback; /**< Lua function to be called on
55 menu item selection. */
56 void *callback_data; /**< Lua state for the callback
57 function */
58 bool retap; /**< Whether or not to rescan the
59 capture file's packets */
60 struct _funnel_packet_menu_t* next; /**< Pointer to the next
61 _funnel_packet_menu_t for the
62 singly-linked list
63 implemenation */
64 } funnel_packet_menu_t;
67 * List of all registered funnel_packet_menu_t's
69 static funnel_packet_menu_t* registered_packet_menus;
71 static GSList *registered_console_menus;
74 * true if the packet menus were modified since the last registration
76 static bool packet_menus_modified;
77 static void funnel_clear_packet_menu (funnel_packet_menu_t** menu_list);
79 const funnel_ops_t* funnel_get_funnel_ops(void) { return ops; }
80 void funnel_set_funnel_ops(const funnel_ops_t* o) { ops = o; }
82 static void funnel_clear_console_menu(void);
84 static void funnel_insert_menu (funnel_menu_t** menu_list, funnel_menu_t *menu)
86 if (!(*menu_list)) {
87 *menu_list = menu;
88 } else {
89 funnel_menu_t* c;
90 for (c = *menu_list; c->next; c = c->next);
91 c->next = menu;
95 static void funnel_remove_menu (funnel_menu_t ** menu_list, funnel_menu_t *menu)
97 funnel_menu_t *m = *menu_list, *p = NULL;
99 while (m) {
100 if (m->callback == menu->callback) {
101 if (p) {
102 p->next = m->next;
103 } else {
104 *menu_list = m->next;
106 g_free(m->name);
107 if (m->callback_data_free) {
108 m->callback_data_free(m->callback_data);
110 g_free(m);
111 if (p) {
112 m = p->next;
113 } else {
114 m = *menu_list;
116 } else {
117 p = m;
118 m = m->next;
123 static void funnel_clear_menu (funnel_menu_t** menu_list)
125 funnel_menu_t *m;
127 while (*menu_list) {
128 m = *menu_list;
129 *menu_list = m->next;
130 g_free(m->name);
131 g_free(m);
133 *menu_list = NULL;
136 void funnel_register_menu(const char *name,
137 register_stat_group_t group,
138 funnel_menu_callback callback,
139 void *callback_data,
140 funnel_menu_callback_data_free callback_data_free,
141 bool retap)
143 funnel_menu_t* m = g_new(funnel_menu_t, 1);
144 m->name = g_strdup(name);
145 m->group = group;
146 m->callback = callback;
147 m->callback_data = callback_data;
148 m->callback_data_free = callback_data_free;
149 m->retap = retap;
150 m->next = NULL;
152 funnel_insert_menu(&registered_menus, m);
153 if (menus_registered) {
154 funnel_menu_t* m_r = (funnel_menu_t *)g_memdup2(m, sizeof *m);
155 m_r->name = g_strdup(name);
156 funnel_insert_menu(&added_menus, m_r);
160 void funnel_deregister_menus(funnel_menu_callback callback)
162 funnel_menu_t* m = g_new0(funnel_menu_t, 1);
163 m->callback = callback;
165 funnel_remove_menu(&registered_menus, m);
166 funnel_insert_menu(&removed_menus, m);
168 // Clear and free memory of packet menus
169 funnel_clear_packet_menu(&registered_packet_menus);
170 packet_menus_modified = true;
173 void funnel_register_all_menus(funnel_registration_cb_t r_cb)
175 funnel_menu_t* c;
176 for (c = registered_menus; c; c = c->next) {
177 r_cb(c->name,c->group,c->callback,c->callback_data,c->retap);
179 menus_registered = true;
182 void funnel_reload_menus(funnel_deregistration_cb_t d_cb,
183 funnel_registration_cb_t r_cb)
185 funnel_menu_t* c;
186 for (c = removed_menus; c; c = c->next) {
187 d_cb(c->callback);
189 for (c = added_menus; c; c = c->next) {
190 r_cb(c->name,c->group,c->callback,c->callback_data,c->retap);
193 funnel_clear_menu(&removed_menus);
194 funnel_clear_menu(&added_menus);
199 * Inserts a funnel_packet_menu_t into a list of funnel_packet_menu_t's
201 * @param menu_list the list of menus that the menu will be added to
202 * @param menu the menu to add to the list of menus
204 static void funnel_insert_packet_menu (funnel_packet_menu_t** menu_list, funnel_packet_menu_t *menu)
206 if (!(*menu_list)) {
207 *menu_list = menu;
208 } else {
209 funnel_packet_menu_t* c;
210 for (c = *menu_list; c->next; c = c->next);
211 c->next = menu;
216 * Entry point for Lua code to register a packet menu
218 * Stores the menu name and callback from the Lua code
219 * into registered_packet_menus so that the
220 * Wireshark GUI code can retrieve it with
221 * funnel_register_all_packet_menus().
223 void funnel_register_packet_menu(const char *name,
224 const char *required_fields,
225 funnel_packet_menu_callback callback,
226 void *callback_data,
227 bool retap)
229 funnel_packet_menu_t* m = g_new0(funnel_packet_menu_t, 1);
230 m->name = g_strdup(name);
231 m->required_fields = g_strdup(required_fields);
232 m->callback = callback;
233 m->callback_data = callback_data;
234 m->retap = retap;
235 m->next = NULL;
237 funnel_insert_packet_menu(&registered_packet_menus, m);
238 packet_menus_modified = true;
242 * Clears a list of funnel_packet_menu_t's and free()s all associated memory
244 * @param menu_list the list of menus to clear
246 static void funnel_clear_packet_menu (funnel_packet_menu_t** menu_list)
248 funnel_packet_menu_t *m;
250 while (*menu_list) {
251 m = *menu_list;
252 *menu_list = m->next;
253 g_free(m->name);
254 g_free(m->required_fields);
255 if (m->callback_data) {
256 g_free(m->callback_data);
258 g_free(m);
260 *menu_list = NULL;
264 * Entry point for Wireshark GUI to obtain all registered packet menus
266 * Calls the supplied callback for each packet menu registered with
267 * funnel_register_packet_menu().
269 * @param r_cb the callback function to call with each registered packet menu
271 void funnel_register_all_packet_menus(funnel_registration_packet_cb_t r_cb)
273 funnel_packet_menu_t* c;
274 for (c = registered_packet_menus; c; c = c->next) {
275 r_cb(c->name,c->required_fields,c->callback,c->callback_data,c->retap);
277 packet_menus_modified = false;
281 * Returns whether the packet menus have been modified since they were last registered
283 * @return true if the packet menus were modified since the last registration
285 bool funnel_packet_menus_modified(void)
287 return packet_menus_modified;
290 void funnel_cleanup(void)
292 funnel_clear_menu(&registered_menus);
293 funnel_clear_packet_menu(&registered_packet_menus);
294 funnel_clear_console_menu();
298 * Entry point for code to register a console menu
300 void funnel_register_console_menu(const char *name,
301 funnel_console_eval_cb_t eval_cb,
302 funnel_console_open_cb_t open_cb,
303 funnel_console_close_cb_t close_cb,
304 void *callback_data,
305 funnel_console_data_free_cb_t free_data)
307 funnel_console_menu_t* m = g_new0(funnel_console_menu_t, 1);
308 m->name = g_strdup(name);
309 m->eval_cb = eval_cb;
310 m->open_cb = open_cb;
311 m->close_cb = close_cb;
312 m->user_data = callback_data;
313 m->data_free_cb = free_data;
315 registered_console_menus = g_slist_prepend(registered_console_menus, m);
318 void funnel_register_all_console_menus(funnel_registration_console_cb_t r_cb)
320 GSList *l;
321 for (l = registered_console_menus; l != NULL; l = l->next) {
322 funnel_console_menu_t *m = l->data;
323 r_cb(m->name, m->eval_cb, m->open_cb, m->close_cb, m->user_data);
327 static void funnel_clear_console_menu(void)
329 GSList *l;
330 for (l = registered_console_menus; l != NULL; l = l->next) {
331 funnel_console_menu_t *m = l->data;
332 g_free(m->name);
333 if (m->data_free_cb && m->user_data) {
334 m->data_free_cb(m->user_data);
336 g_free(l->data);
337 l->data = NULL;
339 g_slist_free(registered_console_menus);
340 registered_console_menus = NULL;
344 * Editor modelines - https://www.wireshark.org/tools/modelines.html
346 * Local variables:
347 * c-basic-offset: 4
348 * tab-width: 8
349 * indent-tabs-mode: nil
350 * End:
352 * vi: set shiftwidth=4 tabstop=8 expandtab:
353 * :indentSize=4:tabSize=8:noTabs=true: