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
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
|
/* extcap.c
*
* Routines for extcap external capture
* Copyright 2013, Mike Ryan <mikeryan@lacklustre.net>
*
* 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>
#define WS_LOG_DOMAIN LOG_DOMAIN_EXTCAP
#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>
#include <glib.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/tempfile.h>
#include <wsutil/wslog.h>
#include <wsutil/ws_assert.h>
#include <wsutil/version_info.h>
#include "capture/capture_session.h"
#include "capture_opts.h"
#include "extcap.h"
#include "extcap_parser.h"
/* Number of seconds to wait for extcap process to exit after cleanup.
* If extcap does not exit before the timeout, it is forcefully terminated.
*/
#ifdef _WIN32
/* Extcap interface does not specify SIGTERM replacement on Windows yet */
#define EXTCAP_CLEANUP_TIMEOUT 0
#else
#define EXTCAP_CLEANUP_TIMEOUT 30
#endif
/* internal container, for all the extcap executables that have been found.
* Will be reset 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);
/* Used for lazily loading our interfaces. */
static void extcap_ensure_all_interfaces_loaded(void) {
if ( !_loaded_interfaces || g_hash_table_size(_loaded_interfaces) == 0 )
extcap_load_interface_list();
}
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_mutex_unlock(&pool->data_mutex);
}
static GHashTable *
extcap_loaded_interfaces(void)
{
if (prefs.capture_no_extcap)
return NULL;
extcap_ensure_all_interfaces_loaded();
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)
{
extcap_ensure_all_interfaces_loaded();
GHashTable * tools = extcap_loaded_interfaces();
GPtrArray *tools_array = g_ptr_array_new();
if (tools && g_hash_table_size(tools) > 0) {
GList * keys = g_hash_table_get_keys(tools);
GList * walker = g_list_first(keys);
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_list_free(keys);
}
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_)
{
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);
}
static GSList *
extcap_get_extcap_paths_from_dir(GSList * list, const char * dirname)
{
GDir * dir;
const char * file;
GSList * paths = list;
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 = ws_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;
}
/**
* 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)
{
GSList *paths = NULL;
paths = extcap_get_extcap_paths_from_dir(paths, get_extcap_pers_dir());
paths = extcap_get_extcap_paths_from_dir(paths, get_extcap_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(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 gboolean
extcap_iface_toolbar_add(const gchar *extcap, iface_toolbar *toolbar_entry)
{
char *toolname;
gboolean ret = FALSE;
if (!extcap || !toolbar_entry)
{
return ret;
}
toolname = g_path_get_basename(extcap);
if (!g_hash_table_lookup(_toolbars, toolname))
{
g_hash_table_insert(_toolbars, g_strdup(toolname), toolbar_entry);
ret = TRUE;
}
g_free(toolname);
return ret;
}
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'. */
ws_assert(data_size >= sizeof(char *));
GSList *paths = extcap_get_extcap_paths();
int i = 0;
int max_threads = (int)g_get_num_processors();
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_mutex_clear(&pool.data_mutex);
g_cond_clear(&pool.cond);
g_thread_pool_free(pool.pool, FALSE, TRUE);
ws_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;
ws_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)
{
ws_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)
{
ws_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;
}
/* Update the extcap interfaces and get a list of their if_infos */
extcap_ensure_all_interfaces_loaded();
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_ensure_all_interfaces_loaded();
extcap_interface *interface = extcap_find_interface_for_ifname(ifname);
return interface != NULL ? interface->help : NULL;
}
GList *
append_extcap_interface_list(GList *list)
{
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 */
extcap_ensure_all_interfaces_loaded();
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_ensure_all_interfaces_loaded();
}
/**
* 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;
extcap_ensure_all_interfaces_loaded();
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)
{
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 (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;
}
if (arg->arg_type == EXTCAP_ARG_PASSWORD)
{
prefs_register_password_preference(dev_module, pref_name_for_prefs,
pref_title, pref_title, (const char **)arg->pref_valptr);
} else {
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_ensure_all_interfaces_loaded();
extcap_interface *interface = extcap_find_interface_for_ifname(ifname);
if (interface)
{
ws_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_ensure_all_interfaces_loaded();
extcap_interface *interface = extcap_find_interface_for_ifname(ifname);
if (interface)
{
ws_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);
GList * walker = g_list_first(keys);
while ( walker )
{
const gchar * key_data = (const gchar *)walker->data;
args = g_list_append(args, g_strdup(key_data));
args = g_list_append(args, g_strdup((const gchar *)g_hash_table_lookup(arguments, key_data)));
walker = g_list_next(walker);
}
g_list_free(keys);
}
extcap_run_one(interface, args, cb_reload_preference, &ret, NULL);
g_list_free_full(args, g_free);
}
return ret;
}
gboolean
_extcap_requires_configuration_int(const char *ifname, gboolean check_required)
{
GList *arguments = 0;
GList *walker = 0, * item = 0;
gboolean found = FALSE;
extcap_ensure_all_interfaces_loaded();
arguments = extcap_get_if_configuration(ifname);
walker = g_list_first(arguments);
while (walker != NULL && !found)
{
item = g_list_first((GList *)(walker->data));
while (item != NULL && !found)
{
if ((extcap_arg *)(item->data) != NULL)
{
extcap_arg *arg = (extcap_arg *)(item->data);
/* Should required options be present, or any kind of options */
if (!check_required)
{
found = TRUE;
}
/* Following branch is executed when check of required items is requested */
else if (arg->is_required)
{
const gchar *stored = NULL;
const gchar *defval = NULL;
if (arg->pref_valptr != NULL)
{
stored = *arg->pref_valptr;
}
if (arg->default_complex != NULL && arg->default_complex->_val != NULL)
{
defval = arg->default_complex->_val;
}
if (arg->is_required)
{
if (!defval && (!stored || !*stored))
{
found = TRUE;
}
}
if (arg->arg_type == EXTCAP_ARG_FILESELECT)
{
if (arg->fileexists && !(file_exists(defval) || file_exists(stored)))
{
found = TRUE;
}
}
}
}
item = item->next;
}
walker = walker->next;
}
extcap_free_if_configuration(arguments, TRUE);
return found;
}
gboolean
extcap_has_configuration(const char *ifname)
{
return _extcap_requires_configuration_int(ifname, FALSE);
}
gboolean
extcap_requires_configuration(const char *ifname)
{
return _extcap_requires_configuration_int(ifname, TRUE);
}
static gboolean cb_verify_filter(extcap_callback_info_t cb_info)
{
extcap_filter_status *status = (extcap_filter_status *)cb_info.data;
size_t output_size, i;
output_size = strlen(cb_info.output);
if (output_size == 0) {
*status = EXTCAP_FILTER_VALID;
} else {
*status = EXTCAP_FILTER_INVALID;
for (i = 0; i < output_size; i++) {
if (cb_info.output[i] == '\n' || cb_info.output[i] == '\r') {
cb_info.output[i] = '\0';
break;
}
}
*cb_info.err_str = g_strdup(cb_info.output);
}
return TRUE;
}
extcap_filter_status
extcap_verify_capture_filter(const char *ifname, const char *filter, gchar **err_str)
{
GList * arguments = NULL;
extcap_filter_status status = EXTCAP_FILTER_UNKNOWN;
extcap_ensure_all_interfaces_loaded();
extcap_interface *interface = extcap_find_interface_for_ifname(ifname);
if (interface)
{
ws_debug("Extcap path %s", get_extcap_dir());
arguments = g_list_append(arguments, g_strdup(EXTCAP_ARGUMENT_CAPTURE_FILTER));
arguments = g_list_append(arguments, g_strdup(filter));
arguments = g_list_append(arguments, g_strdup(EXTCAP_ARGUMENT_INTERFACE));
arguments = g_list_append(arguments, g_strdup(ifname));
extcap_run_one(interface, arguments, cb_verify_filter, &status, err_str);
g_list_free_full(arguments, g_free);
}
return status;
}
gboolean
extcap_has_toolbar(const char *ifname)
{
if (!iface_toolbar_use())
{
return FALSE;
}
extcap_ensure_all_interfaces_loaded();
GList *toolbar_list = g_hash_table_get_values (_toolbars);
for (GList *walker = toolbar_list; walker; walker = walker->next)
{
iface_toolbar *toolbar = (iface_toolbar *) walker->data;
if (g_list_find_custom(toolbar->ifnames, ifname, (GCompareFunc) g_strcmp0))
{
g_list_free(toolbar_list);
return TRUE;
}
}
g_list_free(toolbar_list);
return FALSE;
}
#ifdef HAVE_LIBPCAP
static gboolean extcap_terminate_cb(gpointer user_data)
{
capture_session *cap_session = (capture_session *)user_data;
capture_options *capture_opts = cap_session->capture_opts;
interface_options *interface_opts;
guint icnt;
gboolean all_finished = TRUE;
for (icnt = 0; icnt < capture_opts->ifaces->len; icnt++)
{
interface_opts = &g_array_index(capture_opts->ifaces, interface_options,
icnt);
/* skip native interfaces */
if (interface_opts->if_type != IF_EXTCAP)
{
continue;
}
if (interface_opts->extcap_pid != WS_INVALID_PID)
{
#ifdef _WIN32
TerminateProcess(interface_opts->extcap_pid, 0);
#else
kill(interface_opts->extcap_pid, SIGKILL);
#endif
all_finished = FALSE;
}
/* Do not care about stdout/stderr anymore */
if (interface_opts->extcap_stdout_watch > 0)
{
g_source_remove(interface_opts->extcap_stdout_watch);
interface_opts->extcap_stdout_watch = 0;
}
if (interface_opts->extcap_stderr_watch > 0)
{
g_source_remove(interface_opts->extcap_stderr_watch);
interface_opts->extcap_stderr_watch = 0;
}
}
capture_opts->wait_for_extcap_cbs = TRUE;
capture_opts->extcap_terminate_id = 0;
if (all_finished)
{
capture_process_finished(cap_session);
}
return G_SOURCE_REMOVE;
}
void extcap_request_stop(capture_session *cap_session)
{
capture_options *capture_opts = cap_session->capture_opts;
interface_options *interface_opts;
guint icnt = 0;
if (capture_opts->extcap_terminate_id > 0)
{
/* Already requested, do not extend timeout */
return;
}
if (capture_opts->wait_for_extcap_cbs)
{
/* Terminate callback was called, waiting for child callbacks */
return;
}
if (extcap_session_stop(cap_session))
{
/* Nothing left to do, all extcaps have fully finished */
return;
}
for (icnt = 0; icnt < capture_opts->ifaces->len; icnt++)
{
interface_opts = &g_array_index(capture_opts->ifaces, interface_options,
icnt);
/* skip native interfaces */
if (interface_opts->if_type != IF_EXTCAP)
{
continue;
}
ws_debug("Extcap [%s] - Requesting stop PID: %"PRIdMAX, interface_opts->name,
(intmax_t)interface_opts->extcap_pid);
#ifndef _WIN32
if (interface_opts->extcap_pid != WS_INVALID_PID)
{
kill(interface_opts->extcap_pid, SIGTERM);
}
#endif
}
capture_opts->extcap_terminate_id =
g_timeout_add_seconds(EXTCAP_CLEANUP_TIMEOUT, extcap_terminate_cb, cap_session);
}
static gboolean
extcap_add_arg_and_remove_cb(gpointer key, gpointer value, gpointer data)
{
GPtrArray *args = (GPtrArray *)data;
if (key != NULL)
{
g_ptr_array_add(args, g_strdup((const gchar *)key));
if (value != NULL)
{
g_ptr_array_add(args, g_strdup((const gchar *)value));
}
return TRUE;
}
return FALSE;
}
gboolean extcap_session_stop(capture_session *cap_session)
{
capture_options *capture_opts = cap_session->capture_opts;
interface_options *interface_opts;
guint i;
for (i = 0; i < capture_opts->ifaces->len; i++)
{
interface_opts = &g_array_index(capture_opts->ifaces, interface_options, i);
if (interface_opts->if_type != IF_EXTCAP)
{
continue;
}
if ((interface_opts->extcap_pid != WS_INVALID_PID) ||
(interface_opts->extcap_stdout_watch > 0) ||
(interface_opts->extcap_stderr_watch > 0))
{
/* Capture session is not finished, wait for remaining watches */
return FALSE;
}
g_free(interface_opts->extcap_pipedata);
interface_opts->extcap_pipedata = NULL;
#ifdef _WIN32
if (interface_opts->extcap_pipe_h != INVALID_HANDLE_VALUE)
{
ws_debug("Extcap [%s] - Closing pipe", interface_opts->name);
FlushFileBuffers(interface_opts->extcap_pipe_h);
DisconnectNamedPipe(interface_opts->extcap_pipe_h);
CloseHandle(interface_opts->extcap_pipe_h);
interface_opts->extcap_pipe_h = INVALID_HANDLE_VALUE;
}
if (interface_opts->extcap_control_in_h != INVALID_HANDLE_VALUE)
{
ws_debug("Extcap [%s] - Closing control_in pipe", interface_opts->name);
FlushFileBuffers(interface_opts->extcap_control_in_h);
DisconnectNamedPipe(interface_opts->extcap_control_in_h);
CloseHandle(interface_opts->extcap_control_in_h);
interface_opts->extcap_control_in_h = INVALID_HANDLE_VALUE;
}
if (interface_opts->extcap_control_out_h != INVALID_HANDLE_VALUE)
{
ws_debug("Extcap [%s] - Closing control_out pipe", interface_opts->name);
FlushFileBuffers(interface_opts->extcap_control_out_h);
DisconnectNamedPipe(interface_opts->extcap_control_out_h);
CloseHandle(interface_opts->extcap_control_out_h);
interface_opts->extcap_control_out_h = INVALID_HANDLE_VALUE;
}
#else
if (interface_opts->extcap_fifo != NULL && file_exists(interface_opts->extcap_fifo))
{
/* If extcap didn't open the fifo, dumpcap would be waiting on it
* until user manually stops capture. Simply open and close fifo
* here to let dumpcap return from the select() call. This has no
* effect if dumpcap is not waiting.
*/
int fd = ws_open(interface_opts->extcap_fifo, O_WRONLY|O_NONBLOCK, 0000);
if (fd != -1) {
close(fd);
}
/* the fifo will not be freed here, but with the other capture_opts in capture_sync */
ws_unlink(interface_opts->extcap_fifo);
get_dirname(interface_opts->extcap_fifo);
rmdir(interface_opts->extcap_fifo);
interface_opts->extcap_fifo = NULL;
}
if (interface_opts->extcap_control_in && file_exists(interface_opts->extcap_control_in))
{
ws_unlink(interface_opts->extcap_control_in);
interface_opts->extcap_control_in = NULL;
}
if (interface_opts->extcap_control_out && file_exists(interface_opts->extcap_control_out))
{
ws_unlink(interface_opts->extcap_control_out);
interface_opts->extcap_control_out = NULL;
}
#endif
}
/* All child processes finished */
capture_opts->wait_for_extcap_cbs = FALSE;
if (capture_opts->extcap_terminate_id > 0)
{
g_source_remove(capture_opts->extcap_terminate_id);
capture_opts->extcap_terminate_id = 0;
}
/* Nothing left to do, do not prevent capture session stop */
return TRUE;
}
static void
extcap_watch_removed(capture_session *cap_session, interface_options *interface_opts)
{
if ((interface_opts->extcap_pid == WS_INVALID_PID) &&
(interface_opts->extcap_stdout_watch == 0) &&
(interface_opts->extcap_stderr_watch == 0))
{
/* Close session if this was the last remaining process */
capture_process_finished(cap_session);
}
}
static interface_options *
extcap_find_channel_interface(capture_session *cap_session, GIOChannel *source)
{
capture_options *capture_opts = cap_session->capture_opts;
interface_options *interface_opts;
guint i;
for (i = 0; i < capture_opts->ifaces->len; i++)
{
ws_pipe_t *pipedata;
interface_opts = &g_array_index(capture_opts->ifaces, interface_options, i);
pipedata = (ws_pipe_t *)interface_opts->extcap_pipedata;
if (pipedata &&
((pipedata->stdout_io == source) || (pipedata->stderr_io == source)))
{
return interface_opts;
}
}
ws_assert_not_reached();
}
static gboolean
extcap_stdout_cb(GIOChannel *source, GIOCondition condition _U_, gpointer data)
{
capture_session *cap_session = (capture_session *)data;
interface_options *interface_opts = extcap_find_channel_interface(cap_session, source);
char buf[128];
gsize bytes_read = 0;
GIOStatus status = G_IO_STATUS_EOF;
/* Discard data to prevent child process hanging on stdout write */
if (condition & G_IO_IN)
{
status = g_io_channel_read_chars(source, buf, sizeof(buf), &bytes_read, NULL);
}
if ((bytes_read == 0) || (status != G_IO_STATUS_NORMAL))
{
interface_opts->extcap_stdout_watch = 0;
extcap_watch_removed(cap_session, interface_opts);
return G_SOURCE_REMOVE;
}
return G_SOURCE_CONTINUE;
}
static gboolean
extcap_stderr_cb(GIOChannel *source, GIOCondition condition, gpointer data)
{
capture_session *cap_session = (capture_session *)data;
interface_options *interface_opts = extcap_find_channel_interface(cap_session, source);
char buf[128];
gsize bytes_read = 0;
GIOStatus status = G_IO_STATUS_EOF;
if (condition & G_IO_IN)
{
status = g_io_channel_read_chars(source, buf, sizeof(buf), &bytes_read, NULL);
}
#define STDERR_BUFFER_SIZE 1024
if (bytes_read > 0)
{
if (interface_opts->extcap_stderr == NULL)
{
interface_opts->extcap_stderr = g_string_new_len(buf, bytes_read);
}
else
{
gssize remaining = STDERR_BUFFER_SIZE - interface_opts->extcap_stderr->len;
if (remaining > 0)
{
gssize bytes = bytes_read;
bytes = MIN(bytes, remaining);
g_string_append_len(interface_opts->extcap_stderr, buf, bytes);
}
}
}
if ((bytes_read == 0) || (status != G_IO_STATUS_NORMAL))
{
interface_opts->extcap_stderr_watch = 0;
extcap_watch_removed(cap_session, interface_opts);
return G_SOURCE_REMOVE;
}
return G_SOURCE_CONTINUE;
}
static void extcap_child_watch_cb(GPid pid, gint status _U_, gpointer user_data)
{
guint i;
interface_options *interface_opts;
capture_session *cap_session = (capture_session *)(user_data);
capture_options *capture_opts = cap_session->capture_opts;
/* Close handle to child process. */
g_spawn_close_pid(pid);
/* Update extcap_pid in interface options structure. */
for (i = 0; i < capture_opts->ifaces->len; i++)
{
interface_opts = &g_array_index(capture_opts->ifaces, interface_options, i);
if (interface_opts->extcap_pid == pid)
{
ws_debug("Extcap [%s] - Closing spawned PID: %"PRIdMAX, interface_opts->name,
(intmax_t)interface_opts->extcap_pid);
interface_opts->extcap_pid = WS_INVALID_PID;
extcap_watch_removed(cap_session, interface_opts);
break;
}
}
}
static
GPtrArray *extcap_prepare_arguments(interface_options *interface_opts)
{
GPtrArray *result = NULL;
if (interface_opts->if_type == IF_EXTCAP)
{
result = g_ptr_array_new();
#define add_arg(X) g_ptr_array_add(result, g_strdup(X))
add_arg(interface_opts->extcap);
add_arg(EXTCAP_ARGUMENT_RUN_CAPTURE);
add_arg(EXTCAP_ARGUMENT_INTERFACE);
add_arg(interface_opts->name);
if (interface_opts->cfilter && strlen(interface_opts->cfilter) > 0)
{
add_arg(EXTCAP_ARGUMENT_CAPTURE_FILTER);
add_arg(interface_opts->cfilter);
}
add_arg(EXTCAP_ARGUMENT_RUN_PIPE);
add_arg(interface_opts->extcap_fifo);
if (interface_opts->extcap_control_in)
{
add_arg(EXTCAP_ARGUMENT_CONTROL_OUT);
add_arg(interface_opts->extcap_control_in);
}
if (interface_opts->extcap_control_out)
{
add_arg(EXTCAP_ARGUMENT_CONTROL_IN);
add_arg(interface_opts->extcap_control_out);
}
if (interface_opts->extcap_args == NULL || g_hash_table_size(interface_opts->extcap_args) == 0)
{
/* User did not perform interface configuration.
*
* Check if there are any boolean flags that are set by default
* and hence their argument should be added.
*/
GList *arglist;
GList *elem;
arglist = extcap_get_if_configuration(interface_opts->name);
for (elem = g_list_first(arglist); elem; elem = elem->next)
{
GList *arg_list;
extcap_arg *arg_iter;
if (elem->data == NULL)
{
continue;
}
arg_list = g_list_first((GList *)elem->data);
while (arg_list != NULL)
{
const gchar *stored = NULL;
/* In case of boolflags only first element in arg_list is relevant. */
arg_iter = (extcap_arg *)(arg_list->data);
if (arg_iter->pref_valptr != NULL)
{
stored = *arg_iter->pref_valptr;
}
if (arg_iter->arg_type == EXTCAP_ARG_BOOLFLAG)
{
if (!stored && extcap_complex_get_bool(arg_iter->default_complex))
{
add_arg(arg_iter->call);
}
else if (g_strcmp0(stored, "true") == 0)
{
add_arg(arg_iter->call);
}
}
else
{
if (stored && strlen(stored) > 0) {
add_arg(arg_iter->call);
add_arg(stored);
}
}
arg_list = arg_list->next;
}
}
extcap_free_if_configuration(arglist, TRUE);
}
else
{
g_hash_table_foreach_remove(interface_opts->extcap_args, extcap_add_arg_and_remove_cb, result);
}
add_arg(NULL);
#undef add_arg
}
return result;
}
static void ptr_array_free(gpointer data, gpointer user_data _U_)
{
g_free(data);
}
#ifdef _WIN32
static gboolean extcap_create_pipe(const gchar *ifname, gchar **fifo, HANDLE *handle_out, const gchar *pipe_prefix)
{
gchar timestr[ 14 + 1 ];
time_t current_time;
gchar *pipename = NULL;
SECURITY_ATTRIBUTES security;
/* create pipename */
current_time = time(NULL);
/*
* XXX - we trust Windows not to return a time before the Epoch here,
* so we won't get a null pointer back from localtime().
*/
strftime(timestr, sizeof(timestr), "%Y%m%d%H%M%S", localtime(¤t_time));
pipename = g_strconcat("\\\\.\\pipe\\", pipe_prefix, "_", ifname, "_", timestr, NULL);
/* Security struct to enable Inheritable HANDLE */
memset(&security, 0, sizeof(SECURITY_ATTRIBUTES));
security.nLength = sizeof(SECURITY_ATTRIBUTES);
security.bInheritHandle = FALSE;
security.lpSecurityDescriptor = NULL;
/* create a namedPipe */
*handle_out = CreateNamedPipe(
utf_8to16(pipename),
PIPE_ACCESS_DUPLEX | FILE_FLAG_OVERLAPPED,
PIPE_TYPE_BYTE | PIPE_READMODE_BYTE | PIPE_WAIT,
1, 65536, 65536,
300,
&security);
if (*handle_out == INVALID_HANDLE_VALUE)
{
ws_debug("Error creating pipe => (%ld)", GetLastError());
g_free (pipename);
return FALSE;
}
else
{
ws_debug("Wireshark Created pipe =>(%s) handle (%" PRIuMAX ")", pipename, (uintmax_t)*handle_out);
*fifo = g_strdup(pipename);
}
return TRUE;
}
#else
static gboolean extcap_create_pipe(const gchar *ifname, gchar **fifo, const gchar *temp_dir, const gchar *pipe_prefix)
{
gchar *subdir_tmpl = g_strdup_printf("%s_%s_XXXXXX", pipe_prefix, ifname);
gchar *temp_subdir = create_tempdir(temp_dir, subdir_tmpl, NULL);
g_free(subdir_tmpl);
if (temp_subdir == NULL)
{
return FALSE;
}
gchar *fifo_path = g_build_path(G_DIR_SEPARATOR_S, temp_subdir, "fifo", NULL);
g_free(temp_subdir);
ws_debug("Extcap - Creating fifo: %s", fifo_path);
if (mkfifo(fifo_path, 0600) == 0)
{
*fifo = fifo_path;
}
else
{
g_free(fifo_path);
}
return TRUE;
}
#endif
/* call mkfifo for each extcap,
* returns FALSE if there's an error creating a FIFO */
gboolean
extcap_init_interfaces(capture_session *cap_session)
{
capture_options *capture_opts = cap_session->capture_opts;
guint i;
interface_options *interface_opts;
ws_pipe_t *pipedata;
extcap_ensure_all_interfaces_loaded();
for (i = 0; i < capture_opts->ifaces->len; i++)
{
GPtrArray *args = NULL;
GPid pid = WS_INVALID_PID;
interface_opts = &g_array_index(capture_opts->ifaces, interface_options, i);
/* skip native interfaces */
if (interface_opts->if_type != IF_EXTCAP)
{
continue;
}
/* create control pipes if having toolbar */
if (extcap_has_toolbar(interface_opts->name))
{
extcap_create_pipe(interface_opts->name, &interface_opts->extcap_control_in,
#ifdef _WIN32
&interface_opts->extcap_control_in_h,
#else
capture_opts->temp_dir,
#endif
EXTCAP_CONTROL_IN_PREFIX);
extcap_create_pipe(interface_opts->name, &interface_opts->extcap_control_out,
#ifdef _WIN32
&interface_opts->extcap_control_out_h,
#else
capture_opts->temp_dir,
#endif
EXTCAP_CONTROL_OUT_PREFIX);
}
/* create pipe for fifo */
if (!extcap_create_pipe(interface_opts->name, &interface_opts->extcap_fifo,
#ifdef _WIN32
&interface_opts->extcap_pipe_h,
#else
capture_opts->temp_dir,
#endif
EXTCAP_PIPE_PREFIX))
{
return FALSE;
}
/* Create extcap call */
args = extcap_prepare_arguments(interface_opts);
pipedata = g_new0(ws_pipe_t, 1);
pid = ws_pipe_spawn_async(pipedata, args);
g_ptr_array_foreach(args, ptr_array_free, NULL);
g_ptr_array_free(args, TRUE);
if (pid == WS_INVALID_PID)
{
g_free(pipedata);
continue;
}
g_io_channel_unref(pipedata->stdin_io);
pipedata->stdin_io = NULL;
interface_opts->extcap_pid = pid;
g_child_watch_add_full(G_PRIORITY_HIGH, pid, extcap_child_watch_cb,
(gpointer)cap_session, NULL);
interface_opts->extcap_stdout_watch =
g_io_add_watch(pipedata->stdout_io, G_IO_IN | G_IO_HUP,
extcap_stdout_cb, (gpointer)cap_session);
interface_opts->extcap_stderr_watch =
g_io_add_watch(pipedata->stderr_io, G_IO_IN | G_IO_HUP,
extcap_stderr_cb, (gpointer)cap_session);
/* Pipedata pointers are only used to match GIOChannel to interface.
* GIOChannel watch holds the only remaining reference.
*/
g_io_channel_unref(pipedata->stdout_io);
g_io_channel_unref(pipedata->stderr_io);
#ifdef _WIN32
/* On Windows, wait for extcap to connect to named pipe.
* Some extcaps will present UAC screen to user.
* 30 second timeout should be reasonable timeout for extcap to
* connect to named pipe (including user interaction).
* Wait on multiple object in case of extcap termination
* without opening pipe.
*/
if (pid != WS_INVALID_PID)
{
HANDLE pipe_handles[3];
int num_pipe_handles = 1;
pipe_handles[0] = interface_opts->extcap_pipe_h;
if (extcap_has_toolbar(interface_opts->name))
{
pipe_handles[1] = interface_opts->extcap_control_in_h;
pipe_handles[2] = interface_opts->extcap_control_out_h;
num_pipe_handles += 2;
}
ws_pipe_wait_for_pipe(pipe_handles, num_pipe_handles, pid);
}
#endif
interface_opts->extcap_pipedata = (gpointer) pipedata;
}
return TRUE;
}
#endif /* HAVE_LIBPCAP */
/************* EXTCAP LOAD INTERFACE LIST ***************
*
* The following code handles loading and reloading the interface list. It is explicitly
* kept separate from the rest
*/
static void
extcap_free_interface_info(gpointer data)
{
extcap_info *info = (extcap_info *)data;
g_free(info->basename);
g_free(info->full_path);
g_free(info->version);
g_free(info->help);
extcap_free_interfaces(info->interfaces);
g_free(info);
}
static extcap_info *
extcap_ensure_interface(const gchar * toolname, gboolean create_if_nonexist)
{
extcap_info * element = 0;
if ( prefs.capture_no_extcap )
return NULL;
if ( ! toolname )
return element;
if ( ! _loaded_interfaces )
_loaded_interfaces = g_hash_table_new_full(g_str_hash, g_str_equal, g_free, extcap_free_interface);
element = (extcap_info *) g_hash_table_lookup(_loaded_interfaces, toolname );
if ( element )
return NULL;
if ( ! element && create_if_nonexist )
{
g_hash_table_insert(_loaded_interfaces, g_strdup(toolname), g_new0(extcap_info, 1));
element = (extcap_info *) g_hash_table_lookup(_loaded_interfaces, toolname );
}
return element;
}
extcap_info *
extcap_get_tool_by_ifname(const gchar *ifname)
{
extcap_ensure_all_interfaces_loaded();
if ( ifname && _tool_for_ifname )
{
gchar * toolname = (gchar *)g_hash_table_lookup(_tool_for_ifname, ifname);
if ( toolname )
return extcap_ensure_interface(toolname, FALSE);
}
return NULL;
}
extcap_info *
extcap_get_tool_info(const gchar * toolname)
{
extcap_ensure_all_interfaces_loaded();
return extcap_ensure_interface(toolname, FALSE);
}
static void remove_extcap_entry(gpointer entry, gpointer data _U_)
{
extcap_interface *int_iter = (extcap_interface*)entry;
if (int_iter->if_type == EXTCAP_SENTENCE_EXTCAP)
extcap_free_interface(entry);
}
static void
process_new_extcap(const char *extcap, char *output)
{
GList * interfaces = NULL, * control_items = NULL, * walker = NULL;
extcap_interface * int_iter = NULL;
extcap_info * element = NULL;
iface_toolbar * toolbar_entry = NULL;
gchar * toolname = g_path_get_basename(extcap);
GList * interface_keys = g_hash_table_get_keys(_loaded_interfaces);
/* Load interfaces from utility */
interfaces = extcap_parse_interfaces(output, &control_items);
ws_debug("Loading interface list for %s ", extcap);
/* Seems, that there where no interfaces to be loaded */
if ( ! interfaces || g_list_length(interfaces) == 0 )
{
ws_debug("Cannot load interfaces for %s", extcap );
g_list_free(interface_keys);
g_free(toolname);
return;
}
/* Load or create the storage element for the tool */
element = extcap_ensure_interface(toolname, TRUE);
if ( element == NULL )
{
ws_warning("Cannot store interface %s, already loaded as personal plugin", extcap );
g_list_foreach(interfaces, remove_extcap_entry, NULL);
g_list_free(interfaces);
g_list_free(interface_keys);
g_free(toolname);
return;
}
if (control_items)
{
toolbar_entry = g_new0(iface_toolbar, 1);
toolbar_entry->controls = control_items;
}
walker = interfaces;
gchar* help = NULL;
while (walker != NULL)
{
int_iter = (extcap_interface *)walker->data;
if (int_iter->call != NULL)
ws_debug("Interface found %s\n", int_iter->call);
/* Help is not necessarily stored with the interface, but rather with the version string.
* As the version string always comes in front of the interfaces, this ensures, that it gets
* properly stored with the interface */
if (int_iter->if_type == EXTCAP_SENTENCE_EXTCAP)
{
if (int_iter->call != NULL)
ws_debug(" Extcap [%s] ", int_iter->call);
/* Only initialize values if none are set. Need to check only one element here */
if ( ! element->version )
{
element->version = g_strdup(int_iter->version);
element->basename = g_strdup(toolname);
element->full_path = g_strdup(extcap);
element->help = g_strdup(int_iter->help);
}
help = int_iter->help;
if (toolbar_entry)
{
toolbar_entry->menu_title = g_strdup(int_iter->display);
toolbar_entry->help = g_strdup(int_iter->help);
}
walker = g_list_next(walker);
continue;
}
/* Only interface definitions will be parsed here. help is already set by the extcap element,
* which makes it necessary to have version in the list before the interfaces. This is normally
* the case by design, but could be changed by separating the information in extcap-base. */
if ( int_iter->if_type == EXTCAP_SENTENCE_INTERFACE )
{
if ( g_list_find(interface_keys, int_iter->call) )
{
ws_warning("Extcap interface \"%s\" is already provided by \"%s\" ",
int_iter->call, extcap_if_executable(int_iter->call));
walker = g_list_next(walker);
continue;
}
if ((int_iter->call != NULL) && (int_iter->display))
ws_debug(" Interface [%s] \"%s\" ", int_iter->call, int_iter->display);
int_iter->extcap_path = g_strdup(extcap);
/* Only set the help, if it exists and no parsed help information is present */
if ( ! int_iter->help && help )
int_iter->help = g_strdup(help);
element->interfaces = g_list_append(element->interfaces, int_iter);
g_hash_table_insert(_tool_for_ifname, g_strdup(int_iter->call), g_strdup(toolname));
if (toolbar_entry)
{
if (!toolbar_entry->menu_title)
{
toolbar_entry->menu_title = g_strdup(int_iter->display);
}
toolbar_entry->ifnames = g_list_append(toolbar_entry->ifnames, g_strdup(int_iter->call));
}
}
walker = g_list_next(walker);
}
if (toolbar_entry && toolbar_entry->menu_title)
{
iface_toolbar_add(toolbar_entry);
if (extcap_iface_toolbar_add(extcap, toolbar_entry))
{
toolbar_entry = NULL;
}
}
extcap_free_toolbar(toolbar_entry);
g_list_foreach(interfaces, remove_extcap_entry, NULL);
g_list_free(interfaces);
g_list_free(interface_keys);
g_free(toolname);
}
/** Thread callback to save the output of a --extcap-config call. */
static void
extcap_process_config_cb(thread_pool_t *pool _U_, void *data, char *output)
{
extcap_iface_info_t *iface_info = (extcap_iface_info_t *)data;
iface_info->output = output;
}
/**
* Thread callback to process discovered interfaces, scheduling more tasks to
* retrieve the configuration for each interface. Called once for every extcap
* program.
*/
static void
extcap_process_interfaces_cb(thread_pool_t *pool, void *data, char *output)
{
extcap_run_extcaps_info_t *info = (extcap_run_extcaps_info_t *)data;
guint i = 0;
guint num_interfaces = 0;
if (!output) {
// No interfaces available, nothing to do.
return;
}
// Save output for process_new_extcap.
info->output = output;
// Are there any interfaces to query information from?
GList *interfaces = extcap_parse_interfaces(output, NULL);
for (GList *iface = interfaces; iface; iface = g_list_next(iface)) {
extcap_interface *intf = (extcap_interface *)iface->data;
if (intf->if_type == EXTCAP_SENTENCE_INTERFACE) {
++num_interfaces;
}
}
if (num_interfaces == 0) {
// nothing to do.
g_list_free_full(interfaces, extcap_free_interface);
return;
}
/* GSList is not thread-safe, so pre-allocate an array instead. */
info->iface_infos = g_new0(extcap_iface_info_t, num_interfaces);
info->num_interfaces = num_interfaces;
// Schedule new commands to retrieve the configuration.
for (GList *iface = interfaces; iface; iface = g_list_next(iface)) {
extcap_interface *intf = (extcap_interface *)iface->data;
if (intf->if_type != EXTCAP_SENTENCE_INTERFACE) {
continue;
}
const char *argv[] = {
EXTCAP_ARGUMENT_CONFIG,
EXTCAP_ARGUMENT_INTERFACE,
intf->call,
NULL
};
extcap_run_task_t *task = g_new0(extcap_run_task_t, 1);
extcap_iface_info_t *iface_info = &info->iface_infos[i++];
task->extcap_path = info->extcap_path;
task->argv = g_strdupv((char **)argv);
task->output_cb = extcap_process_config_cb;
task->data = iface_info;
iface_info->ifname = g_strdup(intf->call);
thread_pool_push(pool, task, NULL);
}
g_list_free_full(interfaces, extcap_free_interface);
}
/**
* Thread callback to check whether the new-style --list-interfaces call with an
* explicit function succeeded. If not, schedule a call without the new version
* argument.
*/
static void
extcap_list_interfaces_cb(thread_pool_t *pool, void *data, char *output)
{
extcap_run_extcaps_info_t *info = (extcap_run_extcaps_info_t *)data;
if (!output) {
/* No output available, schedule a fallback query. */
const char *argv[] = {
EXTCAP_ARGUMENT_LIST_INTERFACES,
NULL
};
extcap_run_task_t *task = g_new0(extcap_run_task_t, 1);
task->extcap_path = info->extcap_path;
task->argv = g_strdupv((char **)argv);
task->output_cb = extcap_process_interfaces_cb;
task->data = info;
thread_pool_push(pool, task, NULL);
} else {
extcap_process_interfaces_cb(pool, info, output);
}
}
/* Handles loading of the interfaces. */
static void
extcap_load_interface_list(void)
{
if (prefs.capture_no_extcap)
return;
if (_toolbars)
{
// Remove existing interface toolbars here instead of in extcap_clear_interfaces()
// to avoid flicker in shown toolbars when refreshing interfaces.
GList *toolbar_list = g_hash_table_get_values (_toolbars);
for (GList *walker = toolbar_list; walker; walker = walker->next)
{
iface_toolbar *toolbar = (iface_toolbar *) walker->data;
iface_toolbar_remove(toolbar->menu_title);
}
g_list_free(toolbar_list);
g_hash_table_remove_all(_toolbars);
} else {
_toolbars = g_hash_table_new_full(g_str_hash, g_str_equal, g_free, extcap_free_toolbar);
}
if (_loaded_interfaces == NULL)
{
int major = 0;
int minor = 0;
guint count = 0;
extcap_run_extcaps_info_t *infos;
GList *unused_arguments = NULL;
_loaded_interfaces = g_hash_table_new_full(g_str_hash, g_str_equal, g_free, extcap_free_interface_info);
/* Cleanup lookup table */
if ( _tool_for_ifname )
{
g_hash_table_remove_all(_tool_for_ifname);
_tool_for_ifname = 0;
} else {
_tool_for_ifname = g_hash_table_new_full(g_str_hash, g_str_equal, g_free, g_free);
}
get_ws_version_number(&major, &minor, NULL);
char *arg_version = ws_strdup_printf("%s=%d.%d", EXTCAP_ARGUMENT_VERSION, major, minor);
const char *argv[] = {
EXTCAP_ARGUMENT_LIST_INTERFACES,
arg_version,
NULL
};
infos = (extcap_run_extcaps_info_t *)extcap_run_all(argv,
extcap_list_interfaces_cb, sizeof(extcap_run_extcaps_info_t),
&count);
for (guint i = 0; i < count; i++) {
if (!infos[i].output) {
continue;
}
// Save new extcap and each discovered interface.
process_new_extcap(infos[i].extcap_path, infos[i].output);
for (guint j = 0; j < infos[i].num_interfaces; j++) {
extcap_iface_info_t *iface_info = &infos[i].iface_infos[j];
if (!iface_info->output) {
continue;
}
extcap_callback_info_t cb_info = {
.ifname = iface_info->ifname,
.output = iface_info->output,
.data = &unused_arguments,
};
cb_preference(cb_info);
}
}
/* XXX rework cb_preference such that this unused list can be removed. */
extcap_free_if_configuration(unused_arguments, TRUE);
extcap_free_extcaps_info_array(infos, count);
g_free(arg_version);
}
}
/*
* Editor modelines - https://www.wireshark.org/tools/modelines.html
*
* Local variables:
* c-basic-offset: 4
* tab-width: 8
* indent-tabs-mode: nil
* End:
*
* vi: set shiftwidth=4 tabstop=8 expandtab:
* :indentSize=4:tabSize=8:noTabs=true:
*/
|