forked from wireshark/wireshark
-
Notifications
You must be signed in to change notification settings - Fork 0
/
extcap.c
2078 lines (1753 loc) · 62.8 KB
/
extcap.c
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
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/* extcap.c
*
* Routines for extcap external capture
* Copyright 2013, Mike Ryan <[email protected]>
*
* Wireshark - Network traffic analyzer
* By Gerald Combs <[email protected]>
* Copyright 1998 Gerald Combs
*
* SPDX-License-Identifier: GPL-2.0-or-later
*/
#include <config.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#ifdef _WIN32
#include <windows.h>
#include <process.h>
#include <time.h>
#else
/* Include for unlink */
#include <unistd.h>
#endif
#include <sys/types.h>
#ifdef HAVE_SYS_WAIT_H
#include <sys/wait.h>
#endif
#include <glib.h>
#include <log.h>
#include <epan/prefs.h>
#include "ui/iface_toolbar.h"
#include <wsutil/file_util.h>
#include <wsutil/filesystem.h>
#include <wsutil/ws_pipe.h>
#include <wsutil/ws_printf.h>
#include <wsutil/tempfile.h>
#include "capture_opts.h"
#include "extcap.h"
#include "extcap_parser.h"
#include "version_info.h"
#ifdef _WIN32
static HANDLE pipe_h = INVALID_HANDLE_VALUE;
#endif
static void extcap_child_watch_cb(GPid pid, gint status, gpointer user_data);
/* internal container, for all the extcap executables that have been found.
* Will be resetted if extcap_clear_interfaces() is being explicitly called
* and is being used for printing information about all extcap interfaces found,
* as well as storing all sub-interfaces
*/
static GHashTable * _loaded_interfaces = NULL;
/* Internal container, which maps each ifname to the tool providing it, for faster
* lookup. The key and string value are owned by this table.
*/
static GHashTable * _tool_for_ifname = NULL;
/* internal container, for all the extcap executables that have been found
* and that provides a toolbar with controls to be added to a Interface Toolbar
*/
static GHashTable *_toolbars = NULL;
/* internal container, to map preference names to pointers that hold preference
* values. These ensure that preferences can survive extcap if garbage
* collection, and does not lead to dangling pointers in the prefs subsystem.
*/
static GHashTable *extcap_prefs_dynamic_vals = NULL;
typedef struct _extcap_callback_info_t
{
const gchar * extcap;
const gchar * ifname;
gchar * output;
void * data;
gchar ** err_str;
} extcap_callback_info_t;
/* Callback definition for extcap_foreach */
typedef gboolean(*extcap_cb_t)(extcap_callback_info_t info_structure);
/** GThreadPool does not support pushing new work from a thread while waiting
* for the thread pool to finish. This data structure tracks ongoing work.
* See https://gitlab.gnome.org/GNOME/glib/issues/1598 */
typedef struct thread_pool {
GThreadPool *pool;
gint count; /**< Number of tasks that have not finished. */
GCond cond;
GMutex data_mutex;
} thread_pool_t;
/**
* Callback definition for extcap_run_all, invoked with a thread pool (to
* schedule new tasks), an opaque data parameter, and the output from last task
* (or NULL if it failed). The output must be freed by the callback function.
* The implementation MUST be thread-safe.
*/
typedef void (*extcap_run_cb_t)(thread_pool_t *pool, void *data, char *output);
typedef struct extcap_run_task {
const char *extcap_path;
char **argv; /**< NULL-terminated arguments list, freed when the task is completed. */
extcap_run_cb_t output_cb;
void *data; /** Parameter to be passed to output_cb. */
} extcap_run_task_t;
typedef struct extcap_iface_info {
char *ifname; /**< Interface name. */
char *output; /**< Output of --extcap-config. */
} extcap_iface_info_t;
typedef struct extcap_run_extcaps_info {
char *extcap_path; /**< Extcap program path, MUST be the first member. */
char *output; /**< Output of --extcap-interfaces. */
guint num_interfaces; /**< Number of discovered interfaces. */
extcap_iface_info_t *iface_infos; /**< Per-interface information. */
} extcap_run_extcaps_info_t;
static void extcap_load_interface_list(void);
static gboolean
thread_pool_push(thread_pool_t *pool, gpointer data, GError **error)
{
g_mutex_lock(&pool->data_mutex);
++pool->count;
g_mutex_unlock(&pool->data_mutex);
return g_thread_pool_push(pool->pool, data, error);
}
static void
thread_pool_wait(thread_pool_t *pool)
{
g_mutex_lock(&pool->data_mutex);
while (pool->count != 0) {
g_cond_wait(&pool->cond, &pool->data_mutex);
}
g_cond_clear(&pool->cond);
g_mutex_unlock(&pool->data_mutex);
g_mutex_clear(&pool->data_mutex);
}
static GHashTable *
extcap_loaded_interfaces(void)
{
if (prefs.capture_no_extcap)
return NULL;
if ( !_loaded_interfaces || g_hash_table_size(_loaded_interfaces) == 0 )
extcap_load_interface_list();
return _loaded_interfaces;
}
void
extcap_clear_interfaces(void)
{
if ( _loaded_interfaces )
g_hash_table_destroy(_loaded_interfaces);
_loaded_interfaces = NULL;
if ( _tool_for_ifname )
g_hash_table_destroy(_tool_for_ifname);
_tool_for_ifname = NULL;
}
static gint
compare_tools(gconstpointer a, gconstpointer b)
{
return g_strcmp0((*(extcap_info *const *)a)->basename, (*(extcap_info *const *)b)->basename);
}
void
extcap_get_descriptions(plugin_description_callback callback, void *callback_data)
{
GHashTable * tools = extcap_loaded_interfaces();
GPtrArray *tools_array = g_ptr_array_new();
if (tools && g_hash_table_size(tools) > 0) {
GList * walker = g_list_first(g_hash_table_get_keys(tools));
while (walker && walker->data) {
extcap_info * tool = (extcap_info *)g_hash_table_lookup(tools, walker->data);
if (tool) {
g_ptr_array_add(tools_array, tool);
}
walker = g_list_next(walker);
}
}
g_ptr_array_sort(tools_array, compare_tools);
for (guint i = 0; i < tools_array->len; i++) {
extcap_info *tool = (extcap_info *)tools_array->pdata[i];
callback(tool->basename, tool->version, "extcap", tool->full_path, callback_data);
}
g_ptr_array_free(tools_array, TRUE);
}
static void
print_extcap_description(const char *basename, const char *version,
const char *description, const char *filename,
void *user_data _U_)
{
ws_debug_printf("%-16s\t%s\t%s\t%s\n", basename, version, description, filename);
}
void
extcap_dump_all(void)
{
extcap_get_descriptions(print_extcap_description, NULL);
}
/**
* Obtains a list of extcap program paths. Use g_slist_free_full(paths, g_free)
* to destroy the list.
*/
static GSList *
extcap_get_extcap_paths(void)
{
GDir *dir;
const char *dirname = get_extcap_dir();
const gchar *file;
GSList *paths = NULL;
if ((dir = g_dir_open(dirname, 0, NULL)) != NULL) {
while ((file = g_dir_read_name(dir)) != NULL) {
/* full path to extcap binary */
gchar *extcap_path = g_strdup_printf("%s" G_DIR_SEPARATOR_S "%s", dirname, file);
/* treat anything executable as an extcap binary */
if (g_file_test(extcap_path, G_FILE_TEST_IS_REGULAR) &&
g_file_test(extcap_path, G_FILE_TEST_IS_EXECUTABLE)) {
paths = g_slist_append(paths, extcap_path);
} else {
g_free(extcap_path);
}
}
g_dir_close(dir);
}
return paths;
}
static extcap_interface *
extcap_find_interface_for_ifname(const gchar *ifname)
{
extcap_interface * result = NULL;
if ( !ifname || ! _tool_for_ifname || ! _loaded_interfaces )
return result;
gchar * extcap_util = (gchar *)g_hash_table_lookup(_tool_for_ifname, ifname);
if ( ! extcap_util )
return result;
extcap_info * element = (extcap_info *)g_hash_table_lookup(_loaded_interfaces, extcap_util);
if ( ! element )
return result;
GList * walker = element->interfaces;
while ( walker && walker->data && ! result )
{
extcap_interface * interface = (extcap_interface *)walker->data;
if ( g_strcmp0(interface->call, ifname) == 0 )
{
result = interface;
break;
}
walker = g_list_next ( walker );
}
return result;
}
static void
extcap_free_toolbar_value(iface_toolbar_value *value)
{
if (!value)
{
return;
}
g_free(value->value);
g_free(value->display);
g_free(value);
}
static void
extcap_free_toolbar_control(iface_toolbar_control *control)
{
if (!control)
{
return;
}
g_free(control->display);
g_free(control->validation);
g_free(control->tooltip);
if (control->ctrl_type == INTERFACE_TYPE_STRING) {
g_free(control->default_value.string);
}
g_list_free_full(control->values, (GDestroyNotify)extcap_free_toolbar_value);
g_free(control);
}
static void
extcap_free_toolbar(gpointer data)
{
if (!data)
{
return;
}
iface_toolbar *toolbar = (iface_toolbar *)data;
g_free(toolbar->menu_title);
g_free(toolbar->help);
g_list_free_full(toolbar->ifnames, g_free);
g_list_free_full(toolbar->controls, (GDestroyNotify)extcap_free_toolbar_control);
g_free(toolbar);
}
static gchar *
extcap_if_executable(const gchar *ifname)
{
extcap_interface *interface = extcap_find_interface_for_ifname(ifname);
return interface != NULL ? interface->extcap_path : NULL;
}
static void
extcap_iface_toolbar_add(const gchar *extcap, iface_toolbar *toolbar_entry)
{
char *toolname;
if (!extcap || !toolbar_entry)
{
return;
}
toolname = g_path_get_basename(extcap);
if (!g_hash_table_lookup(_toolbars, toolname))
{
g_hash_table_insert(_toolbars, g_strdup(toolname), toolbar_entry);
}
g_free(toolname);
}
static gchar **
extcap_convert_arguments_to_array(GList * arguments)
{
gchar ** result = NULL;
if ( arguments )
{
GList * walker = g_list_first(arguments);
int cnt = 0;
result = (gchar **) g_malloc0(sizeof(gchar *) * (g_list_length(arguments)));
while(walker)
{
result[cnt] = g_strdup((const gchar *)walker->data);
walker = g_list_next(walker);
cnt++;
}
}
return result;
}
static void extcap_free_array(gchar ** args, int argc)
{
int cnt = 0;
for ( cnt = 0; cnt < argc; cnt++ )
g_free(args[cnt]);
g_free(args);
}
static void
extcap_free_extcaps_info_array(extcap_run_extcaps_info_t *infos, guint count)
{
for (guint i = 0; i < count; i++) {
g_free(infos[i].extcap_path);
g_free(infos[i].output);
for (guint j = 0; j < infos[i].num_interfaces; j++) {
extcap_iface_info_t *iface_info = &infos[i].iface_infos[j];
g_free(iface_info->ifname);
g_free(iface_info->output);
}
g_free(infos[i].iface_infos);
}
g_free(infos);
}
static void
extcap_run_one(const extcap_interface *interface, GList *arguments, extcap_cb_t cb, void *user_data, char **err_str)
{
const char *dirname = get_extcap_dir();
gchar **args = extcap_convert_arguments_to_array(arguments);
int cnt = g_list_length(arguments);
gchar *command_output;
if (ws_pipe_spawn_sync(dirname, interface->extcap_path, cnt, args, &command_output)) {
extcap_callback_info_t cb_info = {
.ifname = interface->call,
.extcap = interface->extcap_path,
.output = command_output,
.data = user_data,
.err_str = err_str,
};
cb(cb_info);
g_free(command_output);
}
extcap_free_array(args, cnt);
}
/** Thread callback to run an extcap program and pass its output. */
static void
extcap_thread_callback(gpointer data, gpointer user_data)
{
extcap_run_task_t *task = (extcap_run_task_t *)data;
thread_pool_t *pool = (thread_pool_t *)user_data;
const char *dirname = get_extcap_dir();
char *command_output;
if (ws_pipe_spawn_sync(dirname, task->extcap_path, g_strv_length(task->argv), task->argv, &command_output)) {
task->output_cb(pool, task->data, command_output);
} else {
task->output_cb(pool, task->data, NULL);
}
g_strfreev(task->argv);
g_free(task);
// Notify when all tasks are completed and no new subtasks were created.
g_mutex_lock(&pool->data_mutex);
if (--pool->count == 0) {
g_cond_signal(&pool->cond);
}
g_mutex_unlock(&pool->data_mutex);
}
/*
* Run all extcap programs with the given arguments list, invoke the callback to
* do some processing and return the results.
*
* @param [IN] argv NULL-terminated arguments list.
* @param [IN] output_cb Thread callback function that receives the output.
* @param [IN] data_size Size of the per-program information that will be returned.
* @param [OUT] count Size of the returned array.
* @return Array of information or NULL if there are none. The first member of
* each element (char *extcap_path) must be freed.
*/
static gpointer
extcap_run_all(const char *argv[], extcap_run_cb_t output_cb, gsize data_size, guint *count)
{
/* Need enough space for at least 'extcap_path'. */
g_assert(data_size >= sizeof(char *));
GSList *paths = extcap_get_extcap_paths();
int i = 0;
#if GLIB_CHECK_VERSION(2,36,0)
int max_threads = (int)g_get_num_processors();
#else
// If the number of processors is unavailable, just use some sane maximum.
// extcap should not be CPU bound, so -1 could also be used for unlimited.
int max_threads = 8;
#endif
if (!paths) {
*count = 0;
return NULL;
}
guint64 start_time = g_get_monotonic_time();
guint paths_count = g_slist_length(paths);
/* GSList is not thread-safe, so pre-allocate an array instead. */
gpointer infos = g_malloc0_n(paths_count, data_size);
thread_pool_t pool;
pool.pool = g_thread_pool_new(extcap_thread_callback, &pool, max_threads, FALSE, NULL);
pool.count = 0;
g_cond_init(&pool.cond);
g_mutex_init(&pool.data_mutex);
for (GSList *path = paths; path; path = g_slist_next(path), i++) {
extcap_run_task_t *task = g_new0(extcap_run_task_t, 1);
task->extcap_path = (char *)path->data;
task->argv = g_strdupv((char **)argv);
task->output_cb = output_cb;
task->data = ((char *)infos) + (i * data_size);
*((char **)task->data) = (char *)path->data;
thread_pool_push(&pool, task, NULL);
}
g_slist_free(paths); /* Note: the contents are transferred to 'infos'. */
/* Wait for all (sub)tasks to complete. */
thread_pool_wait(&pool);
g_thread_pool_free(pool.pool, FALSE, TRUE);
g_log(LOG_DOMAIN_CAPTURE, G_LOG_LEVEL_DEBUG, "extcap: completed discovery of %d tools in %.3fms",
paths_count, (g_get_monotonic_time() - start_time) / 1000.0);
*count = paths_count;
return infos;
}
static void extcap_free_dlt(gpointer d, gpointer user_data _U_)
{
if (d == NULL)
{
return;
}
g_free(((extcap_dlt *)d)->name);
g_free(((extcap_dlt *)d)->display);
g_free(d);
}
static void extcap_free_dlts(GList *dlts)
{
g_list_foreach(dlts, extcap_free_dlt, NULL);
g_list_free(dlts);
}
static gboolean cb_dlt(extcap_callback_info_t cb_info)
{
GList *dlts = NULL, *temp = NULL;
if_capabilities_t *caps;
GList *linktype_list = NULL;
data_link_info_t *data_link_info;
extcap_dlt *dlt_item;
dlts = extcap_parse_dlts(cb_info.output);
temp = dlts;
g_log(LOG_DOMAIN_CAPTURE, G_LOG_LEVEL_DEBUG, "Extcap pipe %s ", cb_info.extcap);
/*
* Allocate the interface capabilities structure.
*/
caps = (if_capabilities_t *) g_malloc(sizeof * caps);
caps->can_set_rfmon = FALSE;
caps->timestamp_types = NULL;
while (dlts)
{
dlt_item = (extcap_dlt *)dlts->data;
if (dlt_item)
{
g_log(LOG_DOMAIN_CAPTURE, G_LOG_LEVEL_DEBUG,
" DLT %d name=\"%s\" display=\"%s\" ", dlt_item->number,
dlt_item->name, dlt_item->display);
data_link_info = g_new(data_link_info_t, 1);
data_link_info->dlt = dlt_item->number;
data_link_info->name = g_strdup(dlt_item->name);
data_link_info->description = g_strdup(dlt_item->display);
linktype_list = g_list_append(linktype_list, data_link_info);
}
dlts = g_list_next(dlts);
}
/* Check to see if we built a list */
if (linktype_list != NULL && cb_info.data != NULL)
{
caps->data_link_types = linktype_list;
*(if_capabilities_t **) cb_info.data = caps;
}
else
{
if (cb_info.err_str)
{
g_log(LOG_DOMAIN_CAPTURE, G_LOG_LEVEL_DEBUG, " returned no DLTs");
*(cb_info.err_str) = g_strdup("Extcap returned no DLTs");
}
g_free(caps);
}
extcap_free_dlts(temp);
return FALSE;
}
if_capabilities_t *
extcap_get_if_dlts(const gchar *ifname, char **err_str)
{
GList * arguments = NULL;
if_capabilities_t *caps = NULL;
if (err_str != NULL)
{
*err_str = NULL;
}
extcap_interface *interface = extcap_find_interface_for_ifname(ifname);
if (interface)
{
arguments = g_list_append(arguments, g_strdup(EXTCAP_ARGUMENT_LIST_DLTS));
arguments = g_list_append(arguments, g_strdup(EXTCAP_ARGUMENT_INTERFACE));
arguments = g_list_append(arguments, g_strdup(ifname));
extcap_run_one(interface, arguments, cb_dlt, &caps, err_str);
g_list_free_full(arguments, g_free);
}
return caps;
}
static void extcap_free_interface(gpointer i)
{
extcap_interface *interface = (extcap_interface *)i;
if (i == NULL)
{
return;
}
g_free(interface->call);
g_free(interface->display);
g_free(interface->version);
g_free(interface->help);
g_free(interface->extcap_path);
g_free(interface);
}
static void extcap_free_interfaces(GList *interfaces)
{
if (interfaces == NULL)
{
return;
}
g_list_free_full(interfaces, extcap_free_interface);
}
static gint
if_info_compare(gconstpointer a, gconstpointer b)
{
gint comp = 0;
const if_info_t *if_a = (const if_info_t *)a;
const if_info_t *if_b = (const if_info_t *)b;
if ((comp = g_strcmp0(if_a->name, if_b->name)) == 0)
{
return g_strcmp0(if_a->friendly_name, if_b->friendly_name);
}
return comp;
}
gchar *
extcap_get_help_for_ifname(const char *ifname)
{
extcap_interface *interface = extcap_find_interface_for_ifname(ifname);
return interface != NULL ? interface->help : NULL;
}
GList *
append_extcap_interface_list(GList *list, char **err_str _U_)
{
GList *interface_list = NULL;
extcap_interface *data = NULL;
GList *ifutilkeys_head = NULL, *ifutilkeys = NULL;
if (prefs.capture_no_extcap)
return list;
/* Update the extcap interfaces and get a list of their if_infos */
if ( !_loaded_interfaces || g_hash_table_size(_loaded_interfaces) == 0 )
extcap_load_interface_list();
ifutilkeys_head = g_hash_table_get_keys(_loaded_interfaces);
ifutilkeys = ifutilkeys_head;
while ( ifutilkeys && ifutilkeys->data )
{
extcap_info * extinfo =
(extcap_info *) g_hash_table_lookup(_loaded_interfaces, (gchar *)ifutilkeys->data);
GList * walker = extinfo->interfaces;
while ( walker && walker->data )
{
interface_list = g_list_append(interface_list, walker->data);
walker = g_list_next(walker);
}
ifutilkeys = g_list_next(ifutilkeys);
}
g_list_free(ifutilkeys_head);
/* Sort that list */
interface_list = g_list_sort(interface_list, if_info_compare);
/* Append the interfaces in that list to the list we're handed. */
while (interface_list != NULL)
{
GList *entry = g_list_first(interface_list);
data = (extcap_interface *)entry->data;
interface_list = g_list_delete_link(interface_list, entry);
if_info_t * if_info = g_new0(if_info_t, 1);
if_info->name = g_strdup(data->call);
if_info->friendly_name = g_strdup(data->display);
if_info->type = IF_EXTCAP;
if_info->extcap = g_strdup(data->extcap_path);
list = g_list_append(list, if_info);
}
return list;
}
void extcap_register_preferences(void)
{
if (prefs.capture_no_extcap)
return;
module_t *dev_module = prefs_find_module("extcap");
if (!dev_module)
{
return;
}
// Will load information about extcaps and their supported config.
extcap_load_interface_list();
}
/**
* Releases the dynamic preference value pointers. Must not be called before
* prefs_cleanup since these pointers could still be in use.
*/
void extcap_cleanup(void)
{
if (extcap_prefs_dynamic_vals)
g_hash_table_destroy(extcap_prefs_dynamic_vals);
if (_loaded_interfaces)
g_hash_table_destroy(_loaded_interfaces);
if (_tool_for_ifname)
g_hash_table_destroy(_tool_for_ifname);
}
/**
* Obtains a pointer which can store a value for the given preference name.
* The preference name that can be passed to the prefs API is stored into
* 'prefs_name'.
*
* Extcap interfaces (and their preferences) are dynamic, they can be created
* and destroyed at will. Thus their data structures are insufficient to pass to
* the preferences APIs which require pointers which are valid until the
* preferences are removed (at exit).
*/
static gchar **extcap_prefs_dynamic_valptr(const char *name, char **pref_name)
{
gchar **valp;
if (!extcap_prefs_dynamic_vals)
{
/* Initialize table only as needed, most preferences are not dynamic */
extcap_prefs_dynamic_vals = g_hash_table_new_full(g_str_hash, g_str_equal,
g_free, g_free);
}
if (!g_hash_table_lookup_extended(extcap_prefs_dynamic_vals, name,
(gpointer *)pref_name, (gpointer *)&valp))
{
/* New dynamic pref, allocate, initialize and store. */
valp = g_new0(gchar *, 1);
*pref_name = g_strdup(name);
g_hash_table_insert(extcap_prefs_dynamic_vals, *pref_name, valp);
}
return valp;
}
void extcap_free_if_configuration(GList *list, gboolean free_args)
{
GList *elem, *sl;
for (elem = g_list_first(list); elem; elem = elem->next)
{
if (elem->data != NULL)
{
sl = g_list_first((GList *)elem->data);
if (free_args)
{
extcap_free_arg_list(sl);
}
else
{
g_list_free(sl);
}
}
}
g_list_free(list);
}
struct preference *
extcap_pref_for_argument(const gchar *ifname, struct _extcap_arg *arg)
{
struct preference *pref = NULL;
GRegex *regex_name = g_regex_new("[-]+", G_REGEX_RAW, (GRegexMatchFlags) 0, NULL);
GRegex *regex_ifname = g_regex_new("(?![a-zA-Z0-9_]).", G_REGEX_RAW, (GRegexMatchFlags) 0, NULL);
if (regex_name && regex_ifname)
{
if (prefs_find_module("extcap"))
{
gchar *pref_name = g_regex_replace(regex_name, arg->call, strlen(arg->call), 0, "", (GRegexMatchFlags) 0, NULL);
gchar *ifname_underscore = g_regex_replace(regex_ifname, ifname, strlen(ifname), 0, "_", (GRegexMatchFlags) 0, NULL);
gchar *ifname_lowercase = g_ascii_strdown(ifname_underscore, -1);
gchar *pref_ifname = g_strconcat(ifname_lowercase, ".", pref_name, NULL);
pref = prefs_find_preference(prefs_find_module("extcap"), pref_ifname);
g_free(pref_name);
g_free(ifname_underscore);
g_free(ifname_lowercase);
g_free(pref_ifname);
}
}
if (regex_name)
{
g_regex_unref(regex_name);
}
if (regex_ifname)
{
g_regex_unref(regex_ifname);
}
return pref;
}
static gboolean cb_preference(extcap_callback_info_t cb_info)
{
GList *arguments = NULL;
GList **il = (GList **) cb_info.data;
module_t *dev_module = NULL;
arguments = extcap_parse_args(cb_info.output);
dev_module = prefs_find_module("extcap");
if (dev_module)
{
GList *walker = arguments;
GRegex *regex_name = g_regex_new("[-]+", G_REGEX_RAW, (GRegexMatchFlags) 0, NULL);
GRegex *regex_ifname = g_regex_new("(?![a-zA-Z0-9_]).", G_REGEX_RAW, (GRegexMatchFlags) 0, NULL);
if (regex_name && regex_ifname)
{
while (walker != NULL)
{
extcap_arg *arg = (extcap_arg *)walker->data;
arg->device_name = g_strdup(cb_info.ifname);
if (arg->save)
{
struct preference *pref = NULL;
gchar *pref_name = g_regex_replace(regex_name, arg->call, strlen(arg->call), 0, "", (GRegexMatchFlags) 0, NULL);
gchar *ifname_underscore = g_regex_replace(regex_ifname, cb_info.ifname, strlen(cb_info.ifname), 0, "_", (GRegexMatchFlags) 0, NULL);
gchar *ifname_lowercase = g_ascii_strdown(ifname_underscore, -1);
gchar *pref_ifname = g_strconcat(ifname_lowercase, ".", pref_name, NULL);
if ((pref = prefs_find_preference(dev_module, pref_ifname)) == NULL)
{
char *pref_name_for_prefs;
char *pref_title = wmem_strdup(wmem_epan_scope(), arg->display);
arg->pref_valptr = extcap_prefs_dynamic_valptr(pref_ifname, &pref_name_for_prefs);
/* Set an initial value if any (the string will be copied at registration) */
if (arg->default_complex)
{
*arg->pref_valptr = arg->default_complex->_val;
}
prefs_register_string_preference(dev_module, pref_name_for_prefs,
pref_title, pref_title, (const char **)arg->pref_valptr);
}
else
{
/* Been here before, restore stored value */
if (arg->pref_valptr == NULL)
{
arg->pref_valptr = (gchar**)g_hash_table_lookup(extcap_prefs_dynamic_vals, pref_ifname);
}
}
g_free(pref_name);
g_free(ifname_underscore);
g_free(ifname_lowercase);
g_free(pref_ifname);
}
walker = g_list_next(walker);
}
}
if (regex_name)
{
g_regex_unref(regex_name);
}
if (regex_ifname)
{
g_regex_unref(regex_ifname);
}
}
*il = g_list_append(*il, arguments);
/* By returning false, extcap_foreach will break on first found */
return TRUE;
}
GList *
extcap_get_if_configuration(const char *ifname)
{
GList * arguments = NULL;
GList *ret = NULL;
extcap_interface *interface = extcap_find_interface_for_ifname(ifname);
if (interface)
{
g_log(LOG_DOMAIN_CAPTURE, G_LOG_LEVEL_DEBUG, "Extcap path %s",
get_extcap_dir());
arguments = g_list_append(arguments, g_strdup(EXTCAP_ARGUMENT_CONFIG));
arguments = g_list_append(arguments, g_strdup(EXTCAP_ARGUMENT_INTERFACE));
arguments = g_list_append(arguments, g_strdup(ifname));
extcap_run_one(interface, arguments, cb_preference, &ret, NULL);
g_list_free_full(arguments, g_free);
}
return ret;
}
static gboolean cb_reload_preference(extcap_callback_info_t cb_info)
{
GList *arguments = NULL, * walker = NULL;
GList **il = (GList **) cb_info.data;
arguments = extcap_parse_values(cb_info.output);
walker = g_list_first(arguments);
while (walker != NULL)
{
extcap_value * val = (extcap_value *)walker->data;
*il = g_list_append(*il, val);
walker = g_list_next(walker);
}
g_list_free(arguments);
/* By returning false, extcap_foreach will break on first found */
return FALSE;
}
GList *
extcap_get_if_configuration_values(const char * ifname, const char * argname, GHashTable *arguments)
{
GList * args = NULL;
GList *ret = NULL;
extcap_interface *interface = extcap_find_interface_for_ifname(ifname);
if (interface)
{
g_log(LOG_DOMAIN_CAPTURE, G_LOG_LEVEL_DEBUG, "Extcap path %s",
get_extcap_dir());
args = g_list_append(args, g_strdup(EXTCAP_ARGUMENT_CONFIG));
args = g_list_append(args, g_strdup(EXTCAP_ARGUMENT_INTERFACE));
args = g_list_append(args, g_strdup(ifname));
args = g_list_append(args, g_strdup(EXTCAP_ARGUMENT_RELOAD_OPTION));
args = g_list_append(args, g_strdup(argname));
if ( arguments )
{
GList * keys = g_hash_table_get_keys(arguments);
while ( keys )
{
const gchar * key_data = (const gchar *)keys->data;