1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
|
/* plugin_if.c
* An API for Wireshark plugins
*
* This enables wireshark dissectors, especially those implemented by plugins
* to register menubar entries, which then will call a pre-defined callback
* function for the dissector or plugin.
*
* Also it implements additional methods, which allow plugins to interoperate
* with the main GUI.
*
* Wireshark - Network traffic analyzer
* By Gerald Combs <gerald@wireshark.org>
* Copyright 1998 Gerald Combs
*
* SPDX-License-Identifier: GPL-2.0-or-later
*/
#include "config.h"
#include <glib.h>
#include <epan/epan.h>
#include <epan/proto.h>
#include <wsutil/ws_assert.h>
#include "plugin_if.h"
static GList * menubar_entries;
static GList * menubar_menunames;
static GHashTable * plugin_if_callback_functions;
static void
plugin_if_init_hashtable(void)
{
if ( plugin_if_callback_functions == 0 )
plugin_if_callback_functions = g_hash_table_new(g_direct_hash, g_direct_equal);
}
static void plugin_if_call_gui_cb(plugin_if_callback_t actionType, GHashTable * dataSet)
{
plugin_if_gui_cb action;
plugin_if_init_hashtable();
if ( g_hash_table_lookup_extended(plugin_if_callback_functions, GINT_TO_POINTER(actionType), NULL, (void **)&action) )
{
if ( action != NULL )
action(dataSet);
}
}
extern GList * ext_menubar_get_entries(void)
{
return menubar_entries;
}
extern ext_menu_t * ext_menubar_register_menu(int proto_id, const char * menulabel,
bool is_plugin)
{
ext_menubar_t * entry = NULL;
char * name = NULL;
/* A name for the entry must be provided */
ws_assert(menulabel != NULL && strlen ( menulabel ) > 0 );
/* A protocol must exist for the given id */
ws_assert(find_protocol_by_id(proto_id) != NULL);
/* Create unique name, which is used by GTK to provide the menu */
name = g_strconcat(proto_get_protocol_filter_name(proto_id), "Menu", NULL);
/* For now, a protocol may only register one main menu */
ws_assert(g_list_find(menubar_menunames, name) == NULL);
entry = g_new0(ext_menubar_t, 1);
entry->type = EXT_MENUBAR_MENU;
entry->proto = proto_id;
entry->is_plugin = is_plugin;
entry->parent_menu = 0;
/* Create a name for this submenu */
entry->name = name;
entry->label = g_strdup(menulabel);
entry->tooltip = g_strdup(menulabel);
entry->submenu_cnt = 0;
entry->item_cnt = 0;
menubar_entries = g_list_append(menubar_entries, entry);
menubar_menunames = g_list_append(menubar_menunames, name);
return entry;
}
extern ext_menu_t * ext_menubar_set_parentmenu(ext_menu_t * menu, const char * parentmenu)
{
ws_assert(menu != NULL && menu->parent == NULL);
ws_assert(parentmenu != 0);
menu->parent_menu = g_strdup(parentmenu);
return menu;
}
extern ext_menu_t * ext_menubar_add_submenu(ext_menu_t * parent, const char *menulabel)
{
ext_menubar_t * entry = NULL;
/* A name for the entry must be provided */
ws_assert(menulabel != NULL && strlen ( menulabel ) > 0 );
/* Parent must be a valid parent */
ws_assert(parent != NULL && parent->type == EXT_MENUBAR_MENU);
parent->submenu_cnt++;
/* Create submenu entry */
entry = g_new0(ext_menubar_t, 1);
entry->type = EXT_MENUBAR_MENU;
entry->parent = parent;
/* Just a convenience */
entry->proto = parent->proto;
entry->is_plugin = parent->is_plugin;
/* Create unique name, which is used by GTK to provide the menu */
entry->name = ws_strdup_printf("%sS%02d", parent->name, parent->submenu_cnt);
entry->label = g_strdup(menulabel);
entry->tooltip = g_strdup(menulabel);
parent->children = g_list_append(parent->children, entry);
return entry;
}
static void ext_menubar_add_generic_entry (
ext_menubar_entry_t type, ext_menu_t * parent, const char * label,
const char * tooltip, ext_menubar_action_cb callback, void *user_data )
{
ext_menubar_t * entry = NULL;
/* A valid parent must exist */
ws_assert(parent != NULL && parent->type == EXT_MENUBAR_MENU);
/* A label for the entry must be provided */
ws_assert(label != NULL && strlen ( label ) > 0 );
parent->item_cnt++;
/* Create menu entry */
entry = g_new0(ext_menubar_t, 1);
entry->type = type;
/* Create unique name, which is used by GTK to provide the menu */
entry->name = ws_strdup_printf("%sI%02d", parent->name, parent->item_cnt);
entry->label = g_strdup(label);
if ( tooltip != NULL && strlen(tooltip) > 0 )
entry->tooltip = g_strdup(tooltip);
entry->callback = callback;
entry->user_data = user_data;
parent->children = g_list_append(parent->children, entry);
}
extern void ext_menubar_add_entry(ext_menu_t * parent, const char *label,
const char *tooltip, ext_menubar_action_cb callback, void *user_data)
{
/* A callback must be provided */
ws_assert(callback != NULL);
ext_menubar_add_generic_entry ( EXT_MENUBAR_ITEM, parent, label, tooltip, callback, user_data );
}
extern void ext_menubar_add_website(ext_menu_t * parent, const char *label,
const char *tooltip, const char *url)
{
/* An url for the entry must be provided */
ws_assert(url != NULL && strlen ( url ) > 0 );
ext_menubar_add_generic_entry ( EXT_MENUBAR_URL, parent, label, tooltip, NULL, (void *) g_strdup(url) );
}
extern void ext_menubar_add_separator(ext_menu_t *parent)
{
ext_menubar_add_generic_entry ( EXT_MENUBAR_SEPARATOR, parent, "-", NULL, NULL, NULL );
}
/* Implementation of external toolbar handlers */
static GList * toolbar_entries;
extern GList * ext_toolbar_get_entries(void)
{
return toolbar_entries;
}
ext_toolbar_t * ext_toolbar_register_toolbar(const char * toolbarlabel)
{
ext_toolbar_t * entry = NULL;
/* A name for the entry must be provided */
ws_assert(toolbarlabel != NULL && strlen ( toolbarlabel ) > 0 );
entry = g_new0(ext_toolbar_t, 1);
entry->type = EXT_TOOLBAR_BAR;
/* Create a name for this toolbar */
entry->name = g_strdup(toolbarlabel);
entry->tooltip = g_strdup(toolbarlabel);
entry->submenu_cnt = 0;
entry->item_cnt = 0;
toolbar_entries = g_list_append(toolbar_entries, entry);
return entry;
}
static int
ext_toolbar_compare(const void * a, const void * b)
{
if ( !a || !b )
return -1;
const ext_toolbar_t * ta = (const ext_toolbar_t *)a;
const ext_toolbar_t * tb = (const ext_toolbar_t *)b;
return strcmp(ta->name, tb->name);
}
void ext_toolbar_unregister_toolbar_by_name(const char * toolbar_name)
{
GList * walker = 0;
if ( ! toolbar_name )
return;
walker = toolbar_entries;
while ( walker && walker->data )
{
ext_toolbar_t * entry = (ext_toolbar_t *)walker->data;
if ( g_strcmp0(entry->name, toolbar_name) == 0)
{
ext_toolbar_unregister_toolbar(entry);
break;
}
walker = g_list_next(walker);
}
}
void ext_toolbar_unregister_toolbar(ext_toolbar_t * toolbar)
{
if ( ! toolbar )
return;
GList * entry = g_list_find_custom(toolbar_entries, toolbar, (GCompareFunc) ext_toolbar_compare);
if ( entry && entry->data )
{
ext_toolbar_t * et = (ext_toolbar_t *)entry->data;
toolbar_entries = g_list_remove(toolbar_entries, et);
if ( ! g_list_find_custom(toolbar_entries, toolbar, (GCompareFunc) ext_toolbar_compare) )
{
GHashTable * dataSet = g_hash_table_new(g_str_hash, g_str_equal);
g_hash_table_insert( dataSet, g_strdup("toolbar_name"), g_strdup(et->name) );
plugin_if_call_gui_cb(PLUGIN_IF_REMOVE_TOOLBAR, dataSet);
g_free(et->name);
g_free(et->tooltip);
g_free(et->defvalue);
g_free(et->regex);
g_free(et);
}
}
}
static int
ext_toolbar_insert_sort(const void *a, const void *b)
{
const ext_toolbar_t * ca = (const ext_toolbar_t *)a;
const ext_toolbar_t * cb = (const ext_toolbar_t *)b;
if ( ca == 0 || cb == 0 )
return 0;
/* Sort buttons after rest of objects */
if ( ca->item_type == EXT_TOOLBAR_BUTTON && cb->item_type != EXT_TOOLBAR_BUTTON )
return 1;
else if ( ca->item_type != EXT_TOOLBAR_BUTTON && cb->item_type == EXT_TOOLBAR_BUTTON )
return -1;
else
{
if ( ca->item_cnt > cb->item_cnt )
return 2;
else if ( ca->item_cnt < cb->item_cnt )
return -2;
else
return 0;
}
}
ext_toolbar_t *
ext_toolbar_add_entry( ext_toolbar_t * parent, ext_toolbar_item_t type, const char *label,
const char *defvalue, const char *tooltip, bool capture_only, GList * value_list,
bool is_required, const char * regex, ext_toolbar_action_cb callback, void *user_data)
{
ext_toolbar_t * entry = NULL;
/* A valid parent must exist */
ws_assert(parent != NULL && parent->type == EXT_TOOLBAR_BAR);
/* A label for the entry must be provided */
ws_assert(label != NULL && strlen ( label ) > 0 );
/* A callback must be provided */
ws_assert(callback != NULL);
parent->item_cnt++;
/* Create menu entry */
entry = g_new0(ext_toolbar_t, 1);
entry->type = EXT_TOOLBAR_ITEM;
entry->item_type = type;
entry->item_cnt = g_list_length(parent->children) + 1;
entry->name = g_strdup(label);
if ( tooltip != NULL && strlen(tooltip) > 0 )
entry->tooltip = g_strdup(tooltip);
if ( defvalue != NULL && strlen(defvalue) > 0 )
entry->defvalue = g_strdup(defvalue);
if ( value_list != NULL && g_list_length(value_list) > 0 )
entry->values = g_list_copy(value_list);
entry->regex = g_strdup(regex);
entry->is_required = is_required;
entry->capture_only = capture_only;
entry->callback = callback;
entry->user_data = user_data;
parent->children = g_list_insert_sorted(parent->children, entry, ext_toolbar_insert_sort);
return entry;
}
static int
ext_toolbar_search_label(const void *tb, const void *lbl)
{
if ( ! tb || ! lbl )
return -1;
const ext_toolbar_t * toolbar = (const ext_toolbar_t *) tb;
if ( toolbar->type != EXT_TOOLBAR_ITEM )
return -2;
const char * label = (const char * )lbl;
return g_strcmp0(toolbar->name, label);
}
ext_toolbar_t * ext_toolbar_entry_by_label(const ext_toolbar_t * toolbar, const char * label)
{
ext_toolbar_t * result = 0;
GList * entry = g_list_find_custom(toolbar->children, label, ext_toolbar_search_label);
if ( entry )
result = (ext_toolbar_t *)entry->data;
return result;
}
GList * ext_toolbar_add_val(GList * entries, char * value, char * display, bool is_default)
{
ext_toolbar_value_t * newval = g_new0(ext_toolbar_value_t, 1);
newval->value = g_strdup(value);
newval->display = g_strdup(display);
newval->is_default = is_default;
return g_list_append(entries, newval);
}
typedef struct _ext_toolbar_update_entry_t
{
ext_toolbar_action_cb callback;
void *item_data;
} ext_toolbar_update_entry_t;
typedef struct _ext_toolbar_update_list_t
{
ext_toolbar_t * item;
GList * entries;
} ext_toolbar_update_list_t;
static int
ext_toolbar_find_item(const void *a, const void *b)
{
if ( a == 0 || b == 0 )
return -1;
const ext_toolbar_update_list_t * item = (const ext_toolbar_update_list_t *)a;
const ext_toolbar_t * entry = (const ext_toolbar_t *)b;
if ( item->item && g_strcmp0 ( item->item->name, entry->name ) == 0 )
return 0;
return -1;
}
static GList * toolbar_updates;
void ext_toolbar_register_update_cb(ext_toolbar_t * entry, ext_toolbar_action_cb callback, void *item_data)
{
if ( entry == 0 || item_data == 0 || callback == 0 )
return;
ext_toolbar_update_list_t * update = NULL;
GList * update_list = g_list_find_custom(toolbar_updates, entry, ext_toolbar_find_item);
if ( ! update_list )
{
update = g_new0(ext_toolbar_update_list_t, 1);
update->item = entry;
toolbar_updates = g_list_append(toolbar_updates, update);
}
else
{
update = (ext_toolbar_update_list_t*)update_list->data;
}
ext_toolbar_update_entry_t * update_entry = g_new0(ext_toolbar_update_entry_t, 1);
update_entry->callback = callback;
update_entry->item_data = item_data;
update->entries = g_list_append(update->entries, update_entry);
}
static void
ext_toolbar_update_entry(ext_toolbar_update_type_t update_type, ext_toolbar_t * entry, void *data, void *idx, bool silent)
{
GList * update = g_list_find_custom(toolbar_updates, entry, ext_toolbar_find_item);
GList * walker = NULL;
if ( ! update || ! update->data )
return;
ext_toolbar_update_t * update_data = g_new0(ext_toolbar_update_t, 1);
update_data->user_data = data;
update_data->data_index = idx;
update_data->silent = silent;
update_data->type = update_type;
walker = ((ext_toolbar_update_list_t *)(update->data))->entries;
while ( walker && walker->data )
{
ext_toolbar_update_entry_t * update_entry = (ext_toolbar_update_entry_t *)walker->data;
/* Call Callback */
if ( update_entry->callback && update_entry->item_data )
update_entry->callback(entry, update_entry->item_data, update_data);
walker = g_list_next(walker);
}
g_free(update_data);
}
void ext_toolbar_update_value(ext_toolbar_t * entry, void *data, bool silent)
{
ext_toolbar_update_entry( EXT_TOOLBAR_UPDATE_VALUE, entry, data, NULL, silent );
}
void ext_toolbar_update_data(ext_toolbar_t * entry, void *data, bool silent)
{
if ( entry->item_type == EXT_TOOLBAR_SELECTOR )
ext_toolbar_update_entry( EXT_TOOLBAR_UPDATE_DATA, entry, data, NULL, silent );
}
void ext_toolbar_update_data_by_index(ext_toolbar_t * entry, void *data, void *idx, bool silent)
{
if ( entry->item_type == EXT_TOOLBAR_SELECTOR )
ext_toolbar_update_entry( EXT_TOOLBAR_UPDATE_DATABYINDEX, entry, data, idx, silent );
}
void ext_toolbar_update_data_add_entry(ext_toolbar_t * entry, void *data, void *idx, bool silent)
{
if ( entry->item_type == EXT_TOOLBAR_SELECTOR )
ext_toolbar_update_entry( EXT_TOOLBAR_UPDATE_DATA_ADD, entry, data, idx, silent );
}
void ext_toolbar_update_data_remove_entry(ext_toolbar_t * entry, void *data, void *idx, bool silent)
{
if ( entry->item_type == EXT_TOOLBAR_SELECTOR )
ext_toolbar_update_entry( EXT_TOOLBAR_UPDATE_DATA_REMOVE, entry, data, idx, silent );
}
void ext_toolbar_update_data_set_active(ext_toolbar_t * entry, bool status)
{
ext_toolbar_update_entry(EXT_TOOLBAR_SET_ACTIVE, entry, GINT_TO_POINTER(status ? 1 : 0), 0, true );
}
/* Implementation of GUI callback methods follows.
* This is a necessity, as using modern UI systems, gui interfaces often operate
* in different threads then the calling application. Even more so, if the calling
* application is implemented using a separate plugin. Therefore the external menubars
* cannot call gui functionality directly, the gui has to perform the function within
* it' own scope. */
extern void plugin_if_apply_filter(const char * filter_string, bool force)
{
plugin_if_callback_t actionType;
GHashTable * dataSet = NULL;
actionType = ( force == true ) ? PLUGIN_IF_FILTER_ACTION_APPLY : PLUGIN_IF_FILTER_ACTION_PREPARE;
dataSet = g_hash_table_new(g_str_hash, g_str_equal);
g_hash_table_insert( dataSet, g_strdup("action_type"), (void *) &actionType );
g_hash_table_insert( dataSet, g_strdup("filter_string"), g_strdup(filter_string) );
g_hash_table_insert( dataSet, g_strdup("force"), (void *) &force );
plugin_if_call_gui_cb(actionType, dataSet);
}
extern void plugin_if_goto_frame(uint32_t framenr)
{
GHashTable * dataSet = NULL;
dataSet = g_hash_table_new(g_str_hash, g_str_equal);
g_hash_table_insert( dataSet, g_strdup("frame_nr"), GUINT_TO_POINTER(framenr) );
plugin_if_call_gui_cb(PLUGIN_IF_GOTO_FRAME, dataSet);
}
extern void plugin_if_save_preference(const char * pref_module, const char * pref_key, const char * pref_value)
{
GHashTable * dataSet = NULL;
dataSet = g_hash_table_new(g_str_hash, g_str_equal);
g_hash_table_insert( dataSet, g_strdup("pref_module"), g_strdup(pref_module) );
g_hash_table_insert( dataSet, g_strdup("pref_key"), g_strdup(pref_key) );
g_hash_table_insert( dataSet, g_strdup("pref_value"), g_strdup(pref_value) );
plugin_if_call_gui_cb(PLUGIN_IF_PREFERENCE_SAVE, dataSet);
}
extern void plugin_if_get_ws_info(ws_info_t **ws_info_ptr)
{
static ws_info_t ws_info = { false, FILE_CLOSED, NULL, 0, 0, false };
#ifdef HAVE_LIBPCAP
GHashTable * dataSet;
char * pluginKey = g_strdup("ws_info");
dataSet = g_hash_table_new(g_str_hash, g_str_equal);
g_hash_table_insert(dataSet, pluginKey, &ws_info);
plugin_if_call_gui_cb(PLUGIN_IF_GET_WS_INFO, dataSet);
g_hash_table_destroy(dataSet);
g_free(pluginKey);
#else
/* Initialise the ws_info structure */
ws_info.ws_info_supported = false;
ws_info.cf_count = 0;
ws_info.cf_filename = NULL;
ws_info.cf_framenr = 0;
ws_info.frame_passed_dfilter = false;
ws_info.cf_state = FILE_CLOSED;
#endif /* HAVE_LIBPCAP */
*ws_info_ptr = &ws_info;
}
extern void* plugin_if_get_frame_data(plugin_if_frame_data_cb extract_cb, void* user_data) {
GHashTable* dataSet = NULL;
void* ret_value = NULL;
dataSet = g_hash_table_new(g_str_hash, g_str_equal);
g_hash_table_insert(dataSet, g_strdup("extract_cb"), extract_cb);
g_hash_table_insert(dataSet, g_strdup("user_data"), user_data);
g_hash_table_insert(dataSet, g_strdup("ret_value_ptr"), &ret_value);
plugin_if_call_gui_cb(PLUGIN_IF_GET_FRAME_DATA, dataSet);
g_hash_table_destroy(dataSet);
return ret_value;
}
extern void* plugin_if_get_capture_file(plugin_if_capture_file_cb extract_cb, void* user_data) {
GHashTable* dataSet = NULL;
void* ret_value = NULL;
dataSet = g_hash_table_new(g_str_hash, g_str_equal);
g_hash_table_insert(dataSet, g_strdup("extract_cb"), extract_cb);
g_hash_table_insert(dataSet, g_strdup("user_data"), user_data);
g_hash_table_insert(dataSet, g_strdup("ret_value_ptr"), &ret_value);
plugin_if_call_gui_cb(PLUGIN_IF_GET_CAPTURE_FILE, dataSet);
g_hash_table_destroy(dataSet);
return ret_value;
}
extern void plugin_if_register_gui_cb(plugin_if_callback_t actionType, plugin_if_gui_cb callback)
{
plugin_if_init_hashtable();
if ( ! g_hash_table_lookup_extended(plugin_if_callback_functions, GINT_TO_POINTER(actionType), NULL, NULL ) )
g_hash_table_insert(plugin_if_callback_functions, GINT_TO_POINTER(actionType), (void *)callback);
}
/*
* Editor modelines
*
* Local Variables:
* c-basic-offset: 4
* tab-width: 8
* indent-tabs-mode: nil
* End:
*
* ex: set shiftwidth=4 tabstop=8 expandtab:
* :indentSize=4:tabSize=8:noTabs=true:
*/
|