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
17 #include <epan/funnel.h>
18 #include <wsutil/glib-compat.h>
20 typedef struct _funnel_menu_t
{
22 register_stat_group_t group
;
23 funnel_menu_callback callback
;
25 funnel_menu_callback_data_free callback_data_free
;
27 struct _funnel_menu_t
* next
;
30 typedef struct _console_menu
{
32 funnel_console_eval_cb_t eval_cb
;
33 funnel_console_open_cb_t open_cb
;
34 funnel_console_close_cb_t close_cb
;
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
;
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
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
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
)
90 for (c
= *menu_list
; c
->next
; c
= c
->next
);
95 static void funnel_remove_menu (funnel_menu_t
** menu_list
, funnel_menu_t
*menu
)
97 funnel_menu_t
*m
= *menu_list
, *p
= NULL
;
100 if (m
->callback
== menu
->callback
) {
104 *menu_list
= m
->next
;
107 if (m
->callback_data_free
) {
108 m
->callback_data_free(m
->callback_data
);
123 static void funnel_clear_menu (funnel_menu_t
** menu_list
)
129 *menu_list
= m
->next
;
136 void funnel_register_menu(const char *name
,
137 register_stat_group_t group
,
138 funnel_menu_callback callback
,
140 funnel_menu_callback_data_free callback_data_free
,
143 funnel_menu_t
* m
= g_new(funnel_menu_t
, 1);
144 m
->name
= g_strdup(name
);
146 m
->callback
= callback
;
147 m
->callback_data
= callback_data
;
148 m
->callback_data_free
= callback_data_free
;
152 funnel_insert_menu(®istered_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(®istered_menus
, m
);
166 funnel_insert_menu(&removed_menus
, m
);
168 // Clear and free memory of packet menus
169 funnel_clear_packet_menu(®istered_packet_menus
);
170 packet_menus_modified
= true;
173 void funnel_register_all_menus(funnel_registration_cb_t r_cb
)
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
)
186 for (c
= removed_menus
; c
; c
= c
->next
) {
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
)
209 funnel_packet_menu_t
* c
;
210 for (c
= *menu_list
; c
->next
; c
= c
->next
);
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
,
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
;
237 funnel_insert_packet_menu(®istered_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
;
252 *menu_list
= m
->next
;
254 g_free(m
->required_fields
);
255 if (m
->callback_data
) {
256 g_free(m
->callback_data
);
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(®istered_menus
);
293 funnel_clear_packet_menu(®istered_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
,
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
)
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)
330 for (l
= registered_console_menus
; l
!= NULL
; l
= l
->next
) {
331 funnel_console_menu_t
*m
= l
->data
;
333 if (m
->data_free_cb
&& m
->user_data
) {
334 m
->data_free_cb(m
->user_data
);
339 g_slist_free(registered_console_menus
);
340 registered_console_menus
= NULL
;
344 * Editor modelines - https://www.wireshark.org/tools/modelines.html
349 * indent-tabs-mode: nil
352 * vi: set shiftwidth=4 tabstop=8 expandtab:
353 * :indentSize=4:tabSize=8:noTabs=true: