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
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
2308
2309
2310
2311
2312
2313
2314
2315
2316
2317
2318
2319
2320
2321
2322
2323
2324
2325
2326
2327
2328
2329
2330
2331
2332
2333
2334
2335
2336
2337
2338
2339
2340
2341
2342
2343
2344
2345
2346
2347
2348
2349
2350
2351
2352
2353
2354
2355
2356
2357
2358
2359
2360
2361
2362
2363
2364
2365
2366
2367
2368
2369
2370
2371
2372
2373
2374
2375
2376
2377
2378
2379
2380
2381
2382
2383
2384
2385
2386
2387
2388
2389
2390
2391
2392
2393
2394
2395
2396
2397
2398
2399
2400
2401
2402
2403
2404
2405
2406
2407
2408
2409
2410
2411
2412
2413
2414
2415
2416
2417
2418
2419
2420
2421
2422
2423
2424
2425
2426
2427
2428
2429
2430
2431
2432
2433
2434
2435
2436
2437
2438
2439
2440
2441
2442
2443
2444
2445
2446
2447
2448
2449
2450
2451
2452
2453
2454
2455
2456
2457
2458
2459
2460
2461
2462
2463
2464
2465
2466
2467
2468
2469
2470
2471
2472
2473
2474
2475
2476
2477
2478
2479
2480
2481
2482
2483
2484
2485
2486
2487
2488
2489
2490
2491
2492
2493
2494
2495
2496
2497
2498
2499
2500
2501
2502
2503
2504
2505
2506
2507
2508
2509
2510
2511
2512
2513
2514
2515
2516
2517
2518
2519
2520
2521
2522
2523
2524
2525
2526
2527
2528
2529
2530
2531
2532
2533
2534
2535
2536
2537
2538
2539
2540
2541
2542
2543
2544
2545
2546
2547
2548
2549
2550
2551
2552
2553
2554
2555
2556
2557
2558
2559
2560
2561
2562
2563
2564
2565
2566
2567
2568
2569
2570
2571
2572
2573
2574
2575
2576
2577
2578
2579
2580
2581
2582
2583
2584
2585
2586
2587
2588
2589
2590
2591
2592
2593
2594
2595
2596
2597
2598
2599
2600
2601
2602
2603
2604
2605
2606
2607
2608
2609
2610
2611
2612
2613
2614
2615
2616
2617
2618
2619
2620
2621
2622
2623
2624
2625
2626
2627
2628
2629
2630
2631
2632
2633
2634
2635
2636
2637
2638
2639
2640
2641
2642
2643
2644
2645
2646
2647
2648
2649
2650
2651
2652
2653
2654
2655
2656
2657
2658
2659
2660
2661
2662
2663
2664
2665
2666
2667
2668
2669
2670
2671
2672
2673
2674
2675
2676
2677
2678
2679
2680
2681
2682
2683
2684
2685
2686
2687
2688
2689
2690
2691
2692
2693
2694
2695
2696
2697
2698
2699
2700
2701
2702
2703
2704
2705
2706
2707
2708
2709
2710
2711
2712
2713
2714
2715
2716
2717
2718
2719
2720
2721
2722
2723
2724
2725
2726
2727
2728
2729
2730
2731
2732
2733
2734
2735
2736
2737
2738
2739
2740
2741
2742
2743
2744
2745
2746
2747
2748
2749
2750
2751
2752
2753
2754
2755
2756
2757
2758
2759
2760
2761
2762
2763
2764
2765
2766
2767
2768
2769
2770
2771
2772
2773
2774
2775
2776
2777
2778
2779
2780
2781
2782
2783
2784
2785
2786
2787
2788
2789
2790
2791
2792
2793
2794
2795
2796
2797
2798
2799
2800
2801
2802
2803
2804
2805
2806
2807
2808
2809
2810
2811
2812
2813
2814
2815
2816
2817
2818
2819
2820
2821
2822
2823
2824
2825
2826
2827
2828
2829
2830
2831
2832
2833
2834
2835
2836
2837
2838
2839
2840
2841
2842
2843
2844
2845
2846
2847
2848
2849
2850
2851
2852
2853
2854
2855
2856
2857
2858
2859
2860
2861
2862
2863
2864
2865
2866
2867
2868
2869
2870
2871
2872
2873
2874
2875
2876
2877
2878
2879
2880
2881
2882
2883
2884
2885
2886
2887
2888
2889
2890
2891
2892
2893
2894
2895
2896
2897
2898
2899
2900
2901
2902
2903
2904
2905
2906
2907
2908
2909
2910
2911
2912
2913
2914
2915
2916
2917
2918
2919
2920
2921
2922
2923
2924
2925
2926
2927
2928
2929
2930
2931
2932
2933
2934
2935
2936
2937
2938
2939
2940
2941
2942
2943
2944
2945
2946
2947
2948
2949
2950
2951
2952
2953
2954
2955
2956
2957
2958
2959
2960
2961
2962
2963
2964
2965
2966
2967
2968
2969
2970
2971
2972
2973
2974
2975
2976
2977
2978
2979
2980
2981
2982
2983
2984
2985
2986
2987
2988
2989
2990
2991
2992
2993
2994
2995
2996
2997
2998
2999
3000
3001
3002
3003
3004
3005
3006
3007
3008
3009
3010
3011
3012
3013
3014
3015
3016
3017
3018
3019
3020
3021
3022
3023
3024
3025
3026
3027
3028
3029
3030
3031
3032
3033
3034
3035
3036
3037
3038
3039
3040
3041
3042
3043
3044
3045
3046
3047
3048
3049
3050
3051
3052
3053
3054
3055
3056
3057
3058
3059
3060
3061
3062
3063
3064
3065
3066
3067
3068
3069
3070
3071
3072
3073
3074
3075
3076
3077
3078
3079
3080
3081
3082
3083
3084
3085
3086
3087
3088
3089
3090
3091
3092
3093
3094
3095
3096
3097
3098
3099
3100
3101
3102
3103
3104
3105
3106
3107
3108
3109
3110
3111
3112
3113
3114
3115
3116
3117
3118
3119
3120
3121
3122
3123
3124
3125
3126
3127
3128
3129
3130
3131
3132
3133
3134
3135
3136
3137
3138
3139
3140
3141
3142
3143
3144
3145
3146
3147
3148
3149
3150
3151
3152
3153
3154
3155
3156
3157
3158
3159
3160
3161
3162
3163
3164
3165
3166
3167
3168
3169
3170
3171
3172
3173
3174
3175
3176
3177
3178
3179
3180
3181
3182
3183
3184
3185
3186
3187
3188
3189
3190
3191
3192
3193
3194
3195
3196
3197
3198
3199
3200
3201
3202
3203
3204
3205
3206
3207
3208
3209
3210
3211
3212
3213
3214
3215
3216
3217
3218
3219
3220
3221
3222
3223
3224
3225
3226
3227
3228
3229
3230
3231
3232
3233
3234
3235
3236
3237
3238
3239
3240
3241
3242
3243
3244
3245
3246
3247
3248
3249
3250
3251
3252
3253
3254
3255
3256
3257
3258
3259
3260
3261
3262
3263
3264
3265
3266
3267
3268
3269
3270
3271
3272
3273
3274
3275
3276
3277
3278
3279
3280
3281
3282
3283
3284
3285
3286
3287
3288
3289
3290
3291
3292
3293
3294
3295
3296
3297
3298
3299
3300
3301
3302
3303
3304
3305
3306
3307
3308
3309
3310
3311
3312
3313
3314
3315
3316
3317
3318
3319
3320
3321
3322
3323
3324
3325
3326
3327
3328
3329
3330
3331
3332
3333
3334
3335
3336
3337
3338
3339
3340
3341
3342
3343
3344
3345
3346
3347
3348
3349
3350
3351
3352
3353
3354
3355
3356
3357
3358
3359
3360
3361
3362
3363
3364
3365
3366
3367
3368
3369
3370
3371
3372
3373
3374
3375
3376
3377
3378
3379
3380
3381
3382
3383
3384
3385
3386
3387
3388
3389
3390
3391
3392
3393
3394
3395
3396
3397
3398
3399
3400
3401
3402
3403
3404
3405
3406
3407
3408
3409
3410
3411
3412
3413
3414
3415
3416
3417
3418
3419
3420
3421
3422
3423
3424
3425
3426
3427
3428
3429
3430
3431
3432
3433
3434
3435
3436
3437
3438
3439
3440
3441
3442
3443
3444
3445
3446
3447
3448
3449
3450
3451
3452
3453
3454
3455
3456
3457
3458
3459
3460
3461
3462
3463
3464
3465
3466
3467
3468
3469
3470
3471
3472
3473
3474
3475
3476
3477
3478
3479
3480
3481
3482
3483
3484
3485
3486
3487
3488
3489
3490
3491
3492
3493
3494
3495
3496
3497
3498
3499
3500
3501
3502
3503
3504
3505
3506
3507
3508
3509
3510
3511
3512
3513
3514
3515
3516
3517
3518
3519
3520
3521
3522
3523
3524
3525
3526
3527
3528
3529
3530
3531
3532
3533
3534
3535
3536
3537
3538
3539
3540
3541
3542
3543
3544
3545
3546
3547
3548
3549
3550
3551
3552
3553
3554
3555
3556
3557
3558
3559
3560
3561
3562
3563
3564
3565
3566
3567
3568
3569
3570
3571
3572
3573
3574
3575
3576
3577
3578
3579
3580
3581
3582
3583
3584
3585
3586
3587
3588
3589
3590
3591
3592
3593
3594
3595
3596
3597
3598
3599
3600
3601
3602
3603
3604
3605
3606
3607
3608
3609
3610
3611
3612
3613
3614
3615
3616
3617
3618
3619
3620
3621
3622
3623
3624
3625
3626
3627
3628
3629
3630
3631
3632
3633
3634
3635
3636
3637
3638
3639
3640
3641
3642
3643
3644
3645
3646
3647
3648
3649
3650
3651
3652
3653
3654
3655
3656
3657
3658
3659
3660
3661
3662
3663
3664
3665
3666
3667
3668
3669
3670
3671
3672
3673
3674
3675
3676
3677
3678
3679
3680
3681
3682
3683
3684
3685
3686
3687
3688
3689
3690
3691
3692
3693
3694
3695
3696
3697
3698
3699
3700
3701
3702
3703
3704
3705
3706
3707
3708
3709
3710
3711
3712
3713
3714
3715
3716
3717
3718
3719
3720
3721
3722
3723
3724
3725
3726
3727
3728
3729
3730
3731
3732
3733
3734
3735
3736
3737
3738
3739
3740
3741
3742
3743
3744
3745
3746
3747
3748
3749
3750
3751
3752
3753
3754
3755
3756
3757
3758
3759
3760
3761
3762
3763
3764
3765
3766
3767
3768
3769
3770
3771
3772
3773
3774
3775
3776
3777
3778
3779
3780
3781
3782
3783
3784
3785
3786
3787
3788
3789
3790
3791
3792
3793
3794
3795
3796
3797
3798
3799
3800
3801
3802
3803
3804
3805
3806
3807
3808
3809
3810
3811
3812
3813
3814
3815
3816
3817
3818
3819
3820
3821
3822
3823
3824
3825
3826
3827
3828
3829
3830
3831
3832
3833
3834
3835
3836
3837
3838
3839
3840
3841
3842
3843
3844
3845
3846
3847
3848
3849
3850
3851
3852
3853
3854
3855
3856
3857
3858
3859
3860
3861
3862
3863
3864
3865
3866
3867
3868
3869
3870
3871
3872
3873
3874
3875
3876
3877
3878
3879
3880
3881
3882
3883
3884
3885
3886
3887
3888
3889
3890
3891
3892
3893
3894
3895
3896
3897
3898
3899
3900
3901
3902
3903
3904
3905
3906
3907
3908
3909
3910
3911
3912
3913
3914
3915
3916
3917
3918
3919
3920
3921
3922
3923
3924
3925
3926
3927
3928
3929
3930
3931
3932
3933
3934
3935
3936
3937
3938
3939
3940
3941
3942
3943
3944
3945
3946
3947
3948
3949
3950
3951
3952
3953
3954
3955
3956
3957
3958
3959
3960
3961
3962
3963
3964
3965
3966
3967
3968
3969
3970
3971
3972
3973
3974
3975
3976
3977
3978
3979
3980
3981
3982
3983
3984
3985
3986
3987
3988
3989
3990
3991
3992
3993
3994
3995
3996
3997
3998
3999
4000
4001
4002
4003
4004
4005
4006
4007
4008
4009
4010
4011
4012
4013
4014
4015
4016
4017
4018
4019
4020
4021
4022
4023
4024
4025
4026
4027
4028
4029
4030
4031
4032
4033
4034
4035
4036
4037
4038
4039
4040
4041
4042
4043
4044
4045
4046
4047
4048
4049
4050
4051
4052
4053
4054
4055
4056
4057
4058
4059
4060
4061
4062
4063
4064
4065
4066
4067
4068
4069
4070
4071
4072
4073
4074
4075
4076
4077
4078
4079
4080
4081
4082
4083
4084
4085
4086
4087
4088
4089
4090
4091
4092
4093
4094
4095
4096
4097
4098
4099
4100
4101
4102
4103
4104
4105
4106
4107
4108
4109
4110
4111
4112
4113
4114
4115
4116
4117
4118
4119
4120
4121
4122
4123
4124
4125
4126
4127
4128
4129
4130
4131
4132
4133
4134
4135
4136
4137
4138
4139
4140
4141
4142
4143
4144
4145
4146
4147
4148
4149
4150
4151
4152
4153
4154
4155
4156
4157
4158
4159
4160
4161
4162
4163
4164
4165
4166
4167
4168
4169
4170
4171
4172
4173
4174
4175
4176
4177
4178
4179
4180
4181
4182
4183
4184
4185
4186
4187
4188
4189
4190
4191
4192
4193
4194
4195
4196
4197
4198
4199
4200
4201
4202
4203
4204
4205
4206
4207
4208
4209
4210
4211
4212
4213
4214
4215
4216
4217
4218
4219
4220
4221
4222
4223
4224
4225
4226
4227
4228
4229
4230
4231
4232
4233
4234
4235
4236
4237
4238
4239
4240
4241
4242
4243
4244
4245
4246
4247
4248
4249
4250
4251
4252
4253
4254
4255
4256
4257
4258
4259
4260
4261
4262
4263
4264
4265
4266
4267
4268
4269
4270
4271
4272
4273
4274
4275
4276
4277
4278
4279
4280
4281
4282
4283
4284
4285
4286
4287
4288
4289
4290
4291
4292
4293
4294
4295
4296
4297
4298
4299
4300
4301
4302
4303
4304
4305
4306
4307
4308
4309
4310
4311
4312
4313
4314
4315
4316
4317
4318
4319
4320
4321
4322
4323
4324
4325
4326
4327
4328
4329
4330
4331
4332
4333
4334
4335
4336
4337
4338
4339
4340
4341
4342
4343
4344
4345
4346
4347
4348
4349
4350
4351
4352
4353
4354
4355
4356
4357
4358
4359
4360
4361
4362
4363
4364
4365
4366
4367
4368
4369
4370
4371
4372
4373
4374
4375
4376
4377
4378
4379
4380
4381
4382
4383
4384
4385
4386
4387
4388
4389
4390
4391
4392
4393
4394
4395
4396
4397
4398
4399
4400
4401
4402
4403
4404
4405
4406
4407
4408
4409
4410
4411
4412
4413
4414
4415
4416
4417
4418
4419
4420
4421
4422
4423
4424
4425
4426
4427
4428
4429
4430
4431
4432
4433
4434
4435
4436
4437
4438
4439
4440
4441
4442
4443
4444
4445
4446
4447
4448
4449
4450
4451
4452
4453
4454
4455
4456
4457
4458
4459
4460
4461
4462
4463
4464
4465
4466
4467
4468
4469
4470
4471
4472
4473
4474
4475
4476
4477
4478
4479
4480
4481
4482
4483
4484
4485
4486
4487
4488
4489
4490
4491
4492
4493
4494
4495
4496
4497
4498
4499
4500
4501
4502
4503
4504
4505
4506
4507
4508
4509
4510
4511
4512
4513
4514
4515
4516
4517
4518
4519
4520
4521
4522
4523
4524
4525
4526
4527
4528
4529
4530
4531
4532
4533
4534
4535
4536
4537
4538
4539
4540
4541
4542
4543
4544
4545
4546
4547
4548
4549
4550
4551
4552
4553
4554
4555
4556
4557
4558
4559
4560
4561
4562
4563
4564
4565
4566
4567
4568
4569
4570
4571
4572
4573
4574
4575
4576
4577
4578
4579
4580
4581
4582
4583
4584
4585
4586
4587
4588
4589
4590
4591
4592
4593
4594
4595
4596
4597
4598
4599
4600
4601
4602
4603
4604
4605
4606
4607
4608
4609
4610
4611
4612
4613
4614
4615
4616
4617
4618
4619
4620
4621
4622
4623
4624
4625
4626
4627
4628
4629
4630
4631
4632
4633
4634
4635
4636
4637
4638
4639
4640
4641
4642
4643
4644
4645
4646
4647
4648
4649
4650
4651
4652
4653
4654
4655
4656
4657
4658
4659
4660
4661
4662
4663
4664
4665
4666
4667
4668
4669
4670
4671
4672
4673
4674
4675
4676
4677
4678
4679
4680
4681
4682
4683
4684
4685
4686
4687
4688
4689
4690
4691
4692
4693
4694
4695
4696
4697
4698
4699
4700
4701
4702
4703
4704
4705
4706
4707
4708
4709
4710
4711
4712
4713
4714
4715
4716
4717
4718
4719
4720
4721
4722
4723
4724
4725
4726
4727
4728
4729
4730
4731
4732
4733
4734
4735
4736
4737
4738
4739
4740
4741
4742
4743
4744
4745
4746
4747
4748
4749
4750
4751
4752
4753
4754
4755
4756
4757
4758
4759
4760
4761
4762
4763
4764
4765
4766
4767
4768
4769
4770
4771
4772
4773
4774
4775
4776
4777
4778
4779
4780
4781
4782
4783
4784
4785
4786
4787
4788
4789
4790
4791
4792
4793
4794
4795
4796
4797
4798
4799
4800
4801
4802
4803
4804
4805
4806
4807
4808
4809
4810
4811
4812
4813
4814
4815
4816
4817
4818
4819
4820
4821
4822
4823
4824
4825
4826
4827
4828
4829
4830
4831
4832
4833
4834
4835
4836
4837
4838
4839
4840
4841
4842
4843
4844
4845
4846
4847
4848
4849
4850
4851
4852
4853
4854
4855
4856
4857
4858
4859
4860
4861
4862
4863
4864
4865
4866
4867
4868
4869
4870
4871
4872
4873
4874
4875
4876
4877
4878
4879
4880
4881
4882
4883
4884
4885
4886
4887
4888
4889
4890
4891
4892
4893
4894
4895
4896
4897
4898
4899
4900
4901
4902
4903
4904
4905
4906
4907
4908
4909
4910
4911
4912
4913
4914
4915
4916
4917
4918
4919
4920
4921
4922
4923
4924
4925
4926
4927
4928
4929
4930
4931
4932
4933
4934
4935
4936
4937
4938
4939
4940
4941
4942
4943
4944
4945
4946
4947
4948
4949
4950
4951
4952
4953
4954
4955
4956
4957
4958
4959
4960
4961
4962
4963
4964
4965
4966
4967
4968
4969
4970
4971
4972
4973
4974
4975
4976
4977
4978
4979
4980
4981
4982
4983
4984
4985
4986
4987
4988
4989
4990
4991
4992
4993
4994
4995
4996
4997
4998
4999
5000
5001
5002
5003
5004
5005
5006
5007
5008
5009
5010
5011
5012
5013
5014
5015
5016
5017
5018
5019
5020
5021
5022
5023
5024
5025
5026
5027
5028
5029
5030
5031
5032
5033
5034
5035
5036
5037
5038
5039
5040
5041
5042
5043
5044
5045
5046
5047
5048
5049
5050
5051
5052
5053
5054
5055
5056
5057
5058
5059
5060
5061
5062
5063
5064
5065
5066
5067
5068
5069
5070
5071
5072
5073
5074
5075
5076
5077
5078
5079
5080
5081
5082
5083
5084
5085
5086
5087
5088
5089
5090
5091
5092
5093
5094
5095
5096
5097
5098
5099
5100
5101
5102
5103
5104
5105
5106
5107
5108
5109
5110
5111
5112
5113
5114
5115
5116
5117
5118
5119
5120
5121
5122
5123
5124
5125
5126
5127
5128
5129
5130
5131
5132
5133
5134
5135
5136
5137
5138
5139
5140
5141
5142
5143
5144
5145
5146
5147
5148
5149
5150
5151
5152
5153
5154
5155
5156
5157
5158
5159
5160
5161
5162
5163
5164
5165
5166
5167
5168
5169
5170
5171
5172
5173
5174
5175
5176
5177
5178
5179
5180
5181
5182
5183
5184
5185
5186
5187
5188
5189
5190
5191
5192
5193
5194
5195
5196
5197
5198
5199
5200
5201
5202
5203
5204
5205
5206
5207
5208
5209
5210
5211
5212
5213
5214
5215
5216
5217
5218
5219
5220
5221
5222
5223
5224
5225
5226
5227
5228
5229
5230
5231
5232
5233
5234
5235
5236
5237
5238
5239
5240
5241
5242
5243
5244
5245
5246
5247
5248
5249
5250
5251
5252
5253
5254
5255
5256
5257
5258
5259
5260
5261
5262
5263
5264
5265
5266
5267
5268
5269
5270
5271
5272
5273
5274
5275
5276
5277
5278
5279
5280
5281
5282
5283
5284
5285
5286
5287
5288
5289
5290
5291
5292
5293
5294
5295
5296
5297
5298
5299
5300
5301
5302
5303
5304
5305
5306
5307
5308
5309
5310
5311
5312
5313
5314
5315
5316
5317
5318
5319
5320
5321
5322
5323
5324
5325
5326
5327
5328
5329
5330
5331
5332
5333
5334
5335
5336
5337
5338
5339
5340
5341
5342
5343
5344
5345
5346
5347
5348
5349
5350
5351
5352
5353
5354
5355
5356
5357
5358
5359
5360
5361
5362
5363
5364
5365
5366
5367
5368
5369
5370
5371
5372
5373
5374
5375
5376
5377
5378
5379
5380
5381
5382
5383
5384
5385
5386
5387
5388
5389
5390
5391
5392
5393
5394
5395
5396
5397
5398
5399
5400
5401
5402
5403
5404
5405
5406
5407
5408
5409
5410
5411
5412
5413
5414
5415
5416
5417
5418
5419
5420
5421
5422
5423
5424
5425
5426
5427
5428
5429
5430
5431
5432
5433
5434
5435
5436
5437
5438
5439
5440
5441
5442
5443
|
/* file.c
* File I/O routines
*
* 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_CAPTURE
#include <time.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <ctype.h>
#include <errno.h>
#include <wsutil/file_util.h>
#include <wsutil/filesystem.h>
#include <wsutil/json_dumper.h>
#include <wsutil/wslog.h>
#include <wsutil/ws_assert.h>
#include <wsutil/version_info.h>
#include <wiretap/merge.h>
#include <epan/exceptions.h>
#include <epan/epan.h>
#include <epan/column.h>
#include <epan/packet.h>
#include <epan/column-utils.h>
#include <epan/expert.h>
#include <epan/prefs.h>
#include <epan/dfilter/dfilter.h>
#include <epan/epan_dissect.h>
#include <epan/tap.h>
#include <epan/timestamp.h>
#include <epan/strutil.h>
#include <epan/addr_resolv.h>
#include <epan/color_filters.h>
#include <epan/secrets.h>
#include "cfile.h"
#include "file.h"
#include "fileset.h"
#include "frame_tvbuff.h"
#include "ui/alert_box.h"
#include "ui/simple_dialog.h"
#include "ui/main_statusbar.h"
#include "ui/progress_dlg.h"
#include "ui/urls.h"
#include "ui/ws_ui_util.h"
#include "ui/packet_list_utils.h"
/* Needed for addrinfo */
#include <sys/types.h>
#ifdef HAVE_SYS_SOCKET_H
#include <sys/socket.h>
#endif
#ifdef HAVE_NETINET_IN_H
# include <netinet/in.h>
#endif
#ifdef _WIN32
# include <winsock2.h>
# include <ws2tcpip.h>
#endif
static gboolean read_record(capture_file *cf, wtap_rec *rec, Buffer *buf,
dfilter_t *dfcode, epan_dissect_t *edt, column_info *cinfo, gint64 offset,
fifo_string_cache_t *frame_dup_cache, GChecksum *frame_cksum);
static void rescan_packets(capture_file *cf, const char *action, const char *action_item, gboolean redissect);
typedef enum {
MR_NOTMATCHED,
MR_MATCHED,
MR_ERROR
} match_result;
typedef match_result (*ws_match_function)(capture_file *, frame_data *,
wtap_rec *, Buffer *, void *);
static match_result match_protocol_tree(capture_file *cf, frame_data *fdata,
wtap_rec *, Buffer *, void *criterion);
static void match_subtree_text(proto_node *node, gpointer data);
static match_result match_summary_line(capture_file *cf, frame_data *fdata,
wtap_rec *, Buffer *, void *criterion);
static match_result match_narrow_and_wide(capture_file *cf, frame_data *fdata,
wtap_rec *, Buffer *, void *criterion);
static match_result match_narrow_and_wide_case(capture_file *cf, frame_data *fdata,
wtap_rec *, Buffer *, void *criterion);
static match_result match_narrow(capture_file *cf, frame_data *fdata,
wtap_rec *, Buffer *, void *criterion);
static match_result match_narrow_case(capture_file *cf, frame_data *fdata,
wtap_rec *, Buffer *, void *criterion);
static match_result match_wide(capture_file *cf, frame_data *fdata,
wtap_rec *, Buffer *, void *criterion);
static match_result match_wide_case(capture_file *cf, frame_data *fdata,
wtap_rec *, Buffer *, void *criterion);
static match_result match_binary(capture_file *cf, frame_data *fdata,
wtap_rec *, Buffer *, void *criterion);
static match_result match_regex(capture_file *cf, frame_data *fdata,
wtap_rec *, Buffer *, void *criterion);
static match_result match_dfilter(capture_file *cf, frame_data *fdata,
wtap_rec *, Buffer *, void *criterion);
static match_result match_marked(capture_file *cf, frame_data *fdata,
wtap_rec *, Buffer *, void *criterion);
static match_result match_time_reference(capture_file *cf, frame_data *fdata,
wtap_rec *, Buffer *, void *criterion);
static gboolean find_packet(capture_file *cf, ws_match_function match_function,
void *criterion, search_direction dir);
static void cf_rename_failure_alert_box(const char *filename, int err);
/* Seconds spent processing packets between pushing UI updates. */
#define PROGBAR_UPDATE_INTERVAL 0.150
/* Show the progress bar after this many seconds. */
#define PROGBAR_SHOW_DELAY 0.5
/*
* Maximum number of records we support in a file.
*
* It is, at most, the maximum value of a guint32, as we use a guint32
* for the frame number.
*
* We allow it to be set to a lower value; see issue #16908 for why
* we're doing this. Thanks, Qt!
*/
static guint32 max_records = G_MAXUINT32;
void
cf_set_max_records(guint max_records_arg)
{
max_records = max_records_arg;
}
/*
* We could probably use g_signal_...() instead of the callbacks below but that
* would require linking our CLI programs to libgobject and creating an object
* instance for the signals.
*/
typedef struct {
cf_callback_t cb_fct;
gpointer user_data;
} cf_callback_data_t;
static GList *cf_callbacks = NULL;
static void
cf_callback_invoke(int event, gpointer data)
{
cf_callback_data_t *cb;
GList *cb_item = cf_callbacks;
/* there should be at least one interested */
ws_assert(cb_item != NULL);
while (cb_item != NULL) {
cb = (cf_callback_data_t *)cb_item->data;
cb->cb_fct(event, data, cb->user_data);
cb_item = g_list_next(cb_item);
}
}
void
cf_callback_add(cf_callback_t func, gpointer user_data)
{
cf_callback_data_t *cb;
cb = g_new(cf_callback_data_t,1);
cb->cb_fct = func;
cb->user_data = user_data;
cf_callbacks = g_list_prepend(cf_callbacks, cb);
}
void
cf_callback_remove(cf_callback_t func, gpointer user_data)
{
cf_callback_data_t *cb;
GList *cb_item = cf_callbacks;
while (cb_item != NULL) {
cb = (cf_callback_data_t *)cb_item->data;
if (cb->cb_fct == func && cb->user_data == user_data) {
cf_callbacks = g_list_remove(cf_callbacks, cb);
g_free(cb);
return;
}
cb_item = g_list_next(cb_item);
}
ws_assert_not_reached();
}
gulong
cf_get_computed_elapsed(capture_file *cf)
{
return cf->computed_elapsed;
}
static void
compute_elapsed(capture_file *cf, gint64 start_time)
{
gint64 delta_time = g_get_monotonic_time() - start_time;
cf->computed_elapsed = (gulong) (delta_time / 1000); /* ms */
}
static const nstime_t *
ws_get_frame_ts(struct packet_provider_data *prov, guint32 frame_num)
{
const frame_data *fd = NULL;
if (prov->prev_dis && prov->prev_dis->num == frame_num) {
fd = prov->prev_dis;
} else if (prov->prev_cap && prov->prev_cap->num == frame_num) {
fd = prov->prev_cap;
} else if (prov->frames) {
fd = frame_data_sequence_find(prov->frames, frame_num);
}
return (fd && fd->has_ts) ? &fd->abs_ts : NULL;
}
static epan_t *
ws_epan_new(capture_file *cf)
{
static const struct packet_provider_funcs funcs = {
ws_get_frame_ts,
cap_file_provider_get_interface_name,
cap_file_provider_get_interface_description,
cap_file_provider_get_modified_block
};
return epan_new(&cf->provider, &funcs);
}
cf_status_t
cf_open(capture_file *cf, const char *fname, unsigned int type, gboolean is_tempfile, int *err)
{
wtap *wth;
gchar *err_info;
wth = wtap_open_offline(fname, type, err, &err_info, TRUE);
if (wth == NULL)
goto fail;
/* The open succeeded. Close whatever capture file we had open,
and fill in the information for this file. */
cf_close(cf);
/* Initialize the record metadata. */
wtap_rec_init(&cf->rec);
/* XXX - we really want to initialize this after we've read all
the packets, so we know how much we'll ultimately need. */
ws_buffer_init(&cf->buf, 1514);
/* We're about to start reading the file. */
cf->state = FILE_READ_IN_PROGRESS;
cf->provider.wth = wth;
cf->f_datalen = 0;
/* Set the file name because we need it to set the follow stream filter.
XXX - is that still true? We need it for other reasons, though,
in any case. */
cf->filename = g_strdup(fname);
/* Indicate whether it's a permanent or temporary file. */
cf->is_tempfile = is_tempfile;
/* No user changes yet. */
cf->unsaved_changes = FALSE;
cf->computed_elapsed = 0;
cf->cd_t = wtap_file_type_subtype(cf->provider.wth);
cf->open_type = type;
cf->linktypes = g_array_sized_new(FALSE, FALSE, (guint) sizeof(int), 1);
cf->count = 0;
cf->packet_comment_count = 0;
cf->displayed_count = 0;
cf->marked_count = 0;
cf->ignored_count = 0;
cf->ref_time_count = 0;
cf->drops_known = FALSE;
cf->drops = 0;
cf->snap = wtap_snapshot_length(cf->provider.wth);
/* Allocate a frame_data_sequence for the frames in this file */
cf->provider.frames = new_frame_data_sequence();
nstime_set_zero(&cf->elapsed_time);
cf->provider.ref = NULL;
cf->provider.prev_dis = NULL;
cf->provider.prev_cap = NULL;
cf->cum_bytes = 0;
/* Create new epan session for dissection.
* (The old one was freed in cf_close().)
*/
cf->epan = ws_epan_new(cf);
packet_list_queue_draw();
cf_callback_invoke(cf_cb_file_opened, cf);
wtap_set_cb_new_ipv4(cf->provider.wth, add_ipv4_name);
wtap_set_cb_new_ipv6(cf->provider.wth, (wtap_new_ipv6_callback_t) add_ipv6_name);
wtap_set_cb_new_secrets(cf->provider.wth, secrets_wtap_callback);
return CF_OK;
fail:
cfile_open_failure_alert_box(fname, *err, err_info);
return CF_ERROR;
}
/*
* Add an encapsulation type to cf->linktypes.
*/
static void
cf_add_encapsulation_type(capture_file *cf, int encap)
{
guint i;
for (i = 0; i < cf->linktypes->len; i++) {
if (g_array_index(cf->linktypes, gint, i) == encap)
return; /* it's already there */
}
/* It's not already there - add it. */
g_array_append_val(cf->linktypes, encap);
}
/* Reset everything to a pristine state */
void
cf_close(capture_file *cf)
{
cf->stop_flag = FALSE;
if (cf->state == FILE_CLOSED || cf->state == FILE_READ_PENDING)
return; /* Nothing to do */
/* Die if we're in the middle of reading a file. */
ws_assert(cf->state != FILE_READ_IN_PROGRESS);
ws_assert(!cf->read_lock);
cf_callback_invoke(cf_cb_file_closing, cf);
/* close things, if not already closed before */
color_filters_cleanup();
if (cf->provider.wth) {
wtap_close(cf->provider.wth);
cf->provider.wth = NULL;
}
/* We have no file open... */
if (cf->filename != NULL) {
/* If it's a temporary file, remove it. */
if (cf->is_tempfile)
ws_unlink(cf->filename);
g_free(cf->filename);
cf->filename = NULL;
}
/* ...which means we have no changes to that file to save. */
cf->unsaved_changes = FALSE;
/* no open_routine type */
cf->open_type = WTAP_TYPE_AUTO;
/* Clean up the record metadata. */
wtap_rec_cleanup(&cf->rec);
/* Clear the packet list. */
packet_list_freeze();
packet_list_clear();
packet_list_thaw();
/* Free up the packet buffer. */
ws_buffer_free(&cf->buf);
dfilter_free(cf->rfcode);
cf->rfcode = NULL;
if (cf->provider.frames != NULL) {
free_frame_data_sequence(cf->provider.frames);
cf->provider.frames = NULL;
}
if (cf->provider.frames_modified_blocks) {
g_tree_destroy(cf->provider.frames_modified_blocks);
cf->provider.frames_modified_blocks = NULL;
}
cf_unselect_packet(cf); /* nothing to select */
cf->first_displayed = 0;
cf->last_displayed = 0;
/* No frames, no frame selected, no field in that frame selected. */
cf->count = 0;
cf->current_frame = NULL;
cf->finfo_selected = NULL;
/* No frame link-layer types, either. */
if (cf->linktypes != NULL) {
g_array_free(cf->linktypes, TRUE);
cf->linktypes = NULL;
}
cf->f_datalen = 0;
nstime_set_zero(&cf->elapsed_time);
reset_tap_listeners();
epan_free(cf->epan);
cf->epan = NULL;
/* We have no file open. */
cf->state = FILE_CLOSED;
cf_callback_invoke(cf_cb_file_closed, cf);
}
/*
* TRUE if the progress dialog doesn't exist and it looks like we'll
* take > PROGBAR_SHOW_DELAY (500ms) to load, FALSE otherwise.
*/
static inline gboolean
progress_is_slow(progdlg_t *progdlg, GTimer *prog_timer, gint64 size, gint64 pos)
{
double elapsed;
if (progdlg) return FALSE;
elapsed = g_timer_elapsed(prog_timer, NULL);
/* This only gets checked between reading records, which doesn't help if
* a single record takes a very long time, e.g., the first TLS packet if
* the SSLKEYLOGFILE is very large. (#17051) */
if ((elapsed * 2 > PROGBAR_SHOW_DELAY && (size / pos) >= 2) /* It looks like we're going to be slow. */
|| elapsed > PROGBAR_SHOW_DELAY) { /* We are indeed slow. */
return TRUE;
}
return FALSE;
}
static float
calc_progbar_val(capture_file *cf, gint64 size, gint64 file_pos, gchar *status_str, gulong status_size)
{
float progbar_val;
progbar_val = (gfloat) file_pos / (gfloat) size;
if (progbar_val > 1.0) {
/* The file probably grew while we were reading it.
* Update file size, and try again.
*/
size = wtap_file_size(cf->provider.wth, NULL);
if (size >= 0)
progbar_val = (gfloat) file_pos / (gfloat) size;
/* If it's still > 1, either "wtap_file_size()" failed (in which
* case there's not much we can do about it), or the file
* *shrank* (in which case there's not much we can do about
* it); just clip the progress value at 1.0.
*/
if (progbar_val > 1.0f)
progbar_val = 1.0f;
}
snprintf(status_str, status_size,
"%" PRId64 "KB of %" PRId64 "KB",
file_pos / 1024, size / 1024);
return progbar_val;
}
cf_read_status_t
cf_read(capture_file *cf, gboolean reloading)
{
int err = 0;
gchar *err_info = NULL;
volatile gboolean too_many_records = FALSE;
gchar *name_ptr;
progdlg_t *volatile progbar = NULL;
GTimer *prog_timer = g_timer_new();
gint64 size;
gint64 start_time;
epan_dissect_t edt;
wtap_rec rec;
Buffer buf;
dfilter_t *dfcode = NULL;
column_info *cinfo;
volatile gboolean create_proto_tree;
guint tap_flags;
gboolean compiled _U_;
volatile gboolean is_read_aborted = FALSE;
/* The update_progress_dlg call below might end up accepting a user request to
* trigger redissection/rescans which can modify/destroy the dissection
* context ("cf->epan"). That condition should be prevented by callers, but in
* case it occurs let's fail gracefully.
*/
if (cf->read_lock) {
ws_warning("Failing due to recursive cf_read(\"%s\", %d) call!",
cf->filename, reloading);
return CF_READ_ERROR;
}
cf->read_lock = TRUE;
/* Compile the current display filter.
* We assume this will not fail since cf->dfilter is only set in
* cf_filter IFF the filter was valid.
*/
if (cf->dfilter) {
compiled = dfilter_compile(cf->dfilter, &dfcode, NULL);
ws_assert(compiled && dfcode);
}
/* Get the union of the flags for all tap listeners. */
tap_flags = union_of_tap_listener_flags();
/*
* Determine whether we need to create a protocol tree.
* We do if:
*
* we're going to apply a display filter;
*
* one of the tap listeners is going to apply a filter;
*
* one of the tap listeners requires a protocol tree;
*
* a postdissector wants field values or protocols on
* the first pass.
*/
create_proto_tree =
(dfcode != NULL || have_filtering_tap_listeners() ||
(tap_flags & TL_REQUIRES_PROTO_TREE) || postdissectors_want_hfids());
reset_tap_listeners();
name_ptr = g_filename_display_basename(cf->filename);
if (reloading)
cf_callback_invoke(cf_cb_file_reload_started, cf);
else
cf_callback_invoke(cf_cb_file_read_started, cf);
/* Record the file's compression type.
XXX - do we know this at open time? */
cf->compression_type = wtap_get_compression_type(cf->provider.wth);
/* The packet list window will be empty until the file is completely loaded */
packet_list_freeze();
cf->stop_flag = FALSE;
start_time = g_get_monotonic_time();
epan_dissect_init(&edt, cf->epan, create_proto_tree, FALSE);
/* If the display filter or any tap listeners require the columns,
* construct them. */
cinfo = (tap_listeners_require_columns() ||
dfilter_requires_columns(dfcode)) ? &cf->cinfo : NULL;
/* Find the size of the file. */
size = wtap_file_size(cf->provider.wth, NULL);
/* If we are to ignore duplicate frames, we need a container to store
* hashes frame contents */
fifo_string_cache_t frame_dup_cache;
GChecksum *volatile cksum = NULL;
if (prefs.ignore_dup_frames) {
fifo_string_cache_init(&frame_dup_cache, prefs.ignore_dup_frames_cache_entries, g_free);
cksum = g_checksum_new(G_CHECKSUM_SHA256);
}
g_timer_start(prog_timer);
wtap_rec_init(&rec);
ws_buffer_init(&buf, 1514);
TRY {
gint64 file_pos;
gint64 data_offset;
float progbar_val;
gchar status_str[100];
while ((wtap_read(cf->provider.wth, &rec, &buf, &err, &err_info,
&data_offset))) {
if (size >= 0) {
if (cf->count == max_records) {
/*
* Quit if we've already read the maximum number of
* records allowed.
*/
too_many_records = TRUE;
break;
}
file_pos = wtap_read_so_far(cf->provider.wth);
/* Create the progress bar if necessary. */
if (progress_is_slow(progbar, prog_timer, size, file_pos)) {
progbar_val = calc_progbar_val(cf, size, file_pos, status_str, sizeof(status_str));
progbar = delayed_create_progress_dlg(cf->window, NULL, NULL, TRUE,
&cf->stop_flag, progbar_val);
}
/*
* Update the progress bar, but do it only after
* PROGBAR_UPDATE_INTERVAL has elapsed. Calling update_progress_dlg
* and packets_bar_update will likely trigger UI paint events, which
* might take a while depending on the platform and display. Reset
* our timer *after* painting.
*/
if (progbar && g_timer_elapsed(prog_timer, NULL) > PROGBAR_UPDATE_INTERVAL) {
progbar_val = calc_progbar_val(cf, size, file_pos, status_str, sizeof(status_str));
/* update the packet bar content on the first run or frequently on very large files */
update_progress_dlg(progbar, progbar_val, status_str);
compute_elapsed(cf, start_time);
packets_bar_update();
g_timer_start(prog_timer);
}
/*
* The previous GUI triggers should not have destroyed the running
* session. If that did happen, it could blow up when read_record tries
* to use the destroyed edt.session, so detect it right here.
*/
ws_assert(edt.session == cf->epan);
}
if (cf->state == FILE_READ_ABORTED) {
/* Well, the user decided to exit Wireshark. Break out of the
loop, and let the code below (which is called even if there
aren't any packets left to read) exit. */
is_read_aborted = TRUE;
break;
}
if (cf->stop_flag) {
/* Well, the user decided to abort the read. He/She will be warned and
it might be enough for him/her to work with the already loaded
packets.
This is especially true for very large capture files, where you don't
want to wait loading the whole file (which may last minutes or even
hours even on fast machines) just to see that it was the wrong file. */
break;
}
read_record(cf, &rec, &buf, dfcode, &edt, cinfo, data_offset, &frame_dup_cache, cksum);
wtap_rec_reset(&rec);
}
}
CATCH(OutOfMemoryError) {
simple_message_box(ESD_TYPE_ERROR, NULL,
"More information and workarounds can be found at\n"
WS_WIKI_URL("KnownBugs/OutOfMemory"),
"Sorry, but Wireshark has run out of memory and has to terminate now.");
#if 0
/* Could we close the current capture and free up memory from that? */
#else
/* we have to terminate, as we cannot recover from the memory error */
exit(1);
#endif
}
ENDTRY;
// If we're ignoring duplicate frames, clear the data structures.
// We really could look at prefs.ignore_dup_frames here, but it's even
// safer to check if we had allocated 'cksum'.
if (cksum != NULL) {
fifo_string_cache_free(&frame_dup_cache);
g_checksum_free(cksum);
}
/* We're done reading sequentially through the file. */
cf->state = FILE_READ_DONE;
/* Destroy the progress bar if it was created. */
if (progbar != NULL)
destroy_progress_dlg(progbar);
g_timer_destroy(prog_timer);
/* Free the display name */
g_free(name_ptr);
/* Cleanup and release all dfilter resources */
dfilter_free(dfcode);
epan_dissect_cleanup(&edt);
wtap_rec_cleanup(&rec);
ws_buffer_free(&buf);
/* Close the sequential I/O side, to free up memory it requires. */
wtap_sequential_close(cf->provider.wth);
/* Allow the protocol dissectors to free up memory that they
* don't need after the sequential run-through of the packets. */
postseq_cleanup_all_protocols();
/* compute the time it took to load the file */
compute_elapsed(cf, start_time);
/* Set the file encapsulation type now; we don't know what it is until
we've looked at all the packets, as we don't know until then whether
there's more than one type (and thus whether it's
WTAP_ENCAP_PER_PACKET). */
cf->lnk_t = wtap_file_encap(cf->provider.wth);
cf->current_frame = frame_data_sequence_find(cf->provider.frames, cf->first_displayed);
packet_list_thaw();
/* It is safe again to execute redissections or sort. */
ws_assert(cf->read_lock);
cf->read_lock = FALSE;
if (reloading)
cf_callback_invoke(cf_cb_file_reload_finished, cf);
else
cf_callback_invoke(cf_cb_file_read_finished, cf);
/* If we have any displayed packets to select, select the first of those
packets by making the first row the selected row. */
if (cf->first_displayed != 0) {
packet_list_select_row_from_data(NULL);
}
if (is_read_aborted) {
/*
* Well, the user decided to exit Wireshark while reading this *offline*
* capture file (Live captures are handled by something like
* cf_continue_tail). Clean up accordingly.
*/
cf_close(cf);
cf->redissection_queued = RESCAN_NONE;
return CF_READ_ABORTED;
}
if (cf->redissection_queued != RESCAN_NONE) {
/* Redissection was queued up. Clear the request and perform it now. */
gboolean redissect = cf->redissection_queued == RESCAN_REDISSECT;
rescan_packets(cf, NULL, NULL, redissect);
}
if (cf->stop_flag) {
simple_message_box(ESD_TYPE_WARN, NULL,
"The remaining packets in the file were discarded.\n"
"\n"
"As a lot of packets from the original file will be missing,\n"
"remember to be careful when saving the current content to a file.\n",
"File loading was cancelled.");
return CF_READ_ERROR;
}
if (err != 0) {
/* Put up a message box noting that the read failed somewhere along
the line. Don't throw out the stuff we managed to read, though,
if any. */
cfile_read_failure_alert_box(NULL, err, err_info);
return CF_READ_ERROR;
} else if (too_many_records) {
simple_message_box(ESD_TYPE_WARN, NULL,
"The remaining packets in the file were discarded.\n"
"\n"
"As a lot of packets from the original file will be missing,\n"
"remember to be careful when saving the current content to a file.\n"
"\n"
"The command-line utility editcap can be used to split "
"the file into multiple smaller files",
"The file contains more records than the maximum "
"supported number of records, %u.", max_records);
return CF_READ_ERROR;
} else
return CF_READ_OK;
}
#ifdef HAVE_LIBPCAP
cf_read_status_t
cf_continue_tail(capture_file *cf, volatile int to_read, wtap_rec *rec,
Buffer *buf, int *err, fifo_string_cache_t *frame_dup_cache, GChecksum *frame_cksum)
{
gchar *err_info;
volatile int newly_displayed_packets = 0;
dfilter_t *dfcode = NULL;
epan_dissect_t edt;
gboolean create_proto_tree;
guint tap_flags;
gboolean compiled _U_;
/* Compile the current display filter.
* We assume this will not fail since cf->dfilter is only set in
* cf_filter IFF the filter was valid.
*/
if (cf->dfilter) {
compiled = dfilter_compile(cf->dfilter, &dfcode, NULL);
ws_assert(compiled && dfcode);
}
/* Get the union of the flags for all tap listeners. */
tap_flags = union_of_tap_listener_flags();
/*
* Determine whether we need to create a protocol tree.
* We do if:
*
* we're going to apply a display filter;
*
* one of the tap listeners is going to apply a filter;
*
* one of the tap listeners requires a protocol tree;
*
* a postdissector wants field values or protocols on
* the first pass.
*/
create_proto_tree =
(dfcode != NULL || have_filtering_tap_listeners() ||
(tap_flags & TL_REQUIRES_PROTO_TREE) || postdissectors_want_hfids());
*err = 0;
/* Don't freeze/thaw the list when doing live capture */
/*packet_list_freeze();*/
epan_dissect_init(&edt, cf->epan, create_proto_tree, FALSE);
TRY {
gint64 data_offset = 0;
column_info *cinfo;
/* If the display filter or any tap listeners require the columns,
* construct them. */
cinfo = (tap_listeners_require_columns() ||
dfilter_requires_columns(dfcode)) ? &cf->cinfo : NULL;
while (to_read != 0) {
wtap_cleareof(cf->provider.wth);
if (!wtap_read(cf->provider.wth, rec, buf, err, &err_info,
&data_offset)) {
break;
}
if (cf->state == FILE_READ_ABORTED) {
/* Well, the user decided to exit Wireshark. Break out of the
loop, and let the code below (which is called even if there
aren't any packets left to read) exit. */
break;
}
if (read_record(cf, rec, buf, dfcode, &edt, cinfo, data_offset, frame_dup_cache, frame_cksum)) {
newly_displayed_packets++;
}
to_read--;
}
wtap_rec_reset(rec);
}
CATCH(OutOfMemoryError) {
simple_message_box(ESD_TYPE_ERROR, NULL,
"More information and workarounds can be found at\n"
WS_WIKI_URL("KnownBugs/OutOfMemory"),
"Sorry, but Wireshark has run out of memory and has to terminate now.");
#if 0
/* Could we close the current capture and free up memory from that? */
return CF_READ_ABORTED;
#else
/* we have to terminate, as we cannot recover from the memory error */
exit(1);
#endif
}
ENDTRY;
/* Update the file encapsulation; it might have changed based on the
packets we've read. */
cf->lnk_t = wtap_file_encap(cf->provider.wth);
/* Cleanup and release all dfilter resources */
dfilter_free(dfcode);
epan_dissect_cleanup(&edt);
/* Don't freeze/thaw the list when doing live capture */
/*packet_list_thaw();*/
/* With the new packet list the first packet
* isn't automatically selected.
*/
if (!cf->current_frame && !packet_list_multi_select_active())
packet_list_select_row_from_data(NULL);
if (cf->state == FILE_READ_ABORTED) {
/* Well, the user decided to exit Wireshark. Return CF_READ_ABORTED
so that our caller can kill off the capture child process;
this will cause an EOF on the pipe from the child, so
"cf_finish_tail()" will be called, and it will clean up
and exit. */
return CF_READ_ABORTED;
} else if (*err != 0) {
/* We got an error reading the capture file.
XXX - pop up a dialog box instead? */
if (err_info != NULL) {
ws_warning("Error \"%s\" while reading \"%s\" (\"%s\")",
wtap_strerror(*err), cf->filename, err_info);
g_free(err_info);
} else {
ws_warning("Error \"%s\" while reading \"%s\"",
wtap_strerror(*err), cf->filename);
}
return CF_READ_ERROR;
} else
return CF_READ_OK;
}
void
cf_fake_continue_tail(capture_file *cf)
{
if (cf->state == FILE_CLOSED) {
cf->state = FILE_READ_PENDING;
}
}
cf_read_status_t
cf_finish_tail(capture_file *cf, wtap_rec *rec, Buffer *buf, int *err,
fifo_string_cache_t *frame_dup_cache, GChecksum *frame_cksum)
{
gchar *err_info;
gint64 data_offset;
dfilter_t *dfcode = NULL;
column_info *cinfo;
epan_dissect_t edt;
gboolean create_proto_tree;
guint tap_flags;
gboolean compiled _U_;
/* Compile the current display filter.
* We assume this will not fail since cf->dfilter is only set in
* cf_filter IFF the filter was valid.
*/
if (cf->dfilter) {
compiled = dfilter_compile(cf->dfilter, &dfcode, NULL);
ws_assert(compiled && dfcode);
}
/* Get the union of the flags for all tap listeners. */
tap_flags = union_of_tap_listener_flags();
/* If the display filter or any tap listeners require the columns,
* construct them. */
cinfo = (tap_listeners_require_columns() ||
dfilter_requires_columns(dfcode)) ? &cf->cinfo : NULL;
/*
* Determine whether we need to create a protocol tree.
* We do if:
*
* we're going to apply a display filter;
*
* one of the tap listeners is going to apply a filter;
*
* one of the tap listeners requires a protocol tree;
*
* a postdissector wants field values or protocols on
* the first pass.
*/
create_proto_tree =
(dfcode != NULL || have_filtering_tap_listeners() ||
(tap_flags & TL_REQUIRES_PROTO_TREE) || postdissectors_want_hfids());
if (cf->provider.wth == NULL) {
cf_close(cf);
return CF_READ_ERROR;
}
/* Don't freeze/thaw the list when doing live capture */
/*packet_list_freeze();*/
epan_dissect_init(&edt, cf->epan, create_proto_tree, FALSE);
while ((wtap_read(cf->provider.wth, rec, buf, err, &err_info, &data_offset))) {
if (cf->state == FILE_READ_ABORTED) {
/* Well, the user decided to abort the read. Break out of the
loop, and let the code below (which is called even if there
aren't any packets left to read) exit. */
break;
}
read_record(cf, rec, buf, dfcode, &edt, cinfo, data_offset, frame_dup_cache, frame_cksum);
wtap_rec_reset(rec);
}
/* Cleanup and release all dfilter resources */
dfilter_free(dfcode);
epan_dissect_cleanup(&edt);
/* Don't freeze/thaw the list when doing live capture */
/*packet_list_thaw();*/
if (cf->state == FILE_READ_ABORTED) {
/* Well, the user decided to abort the read. We're only called
when the child capture process closes the pipe to us (meaning
it's probably exited), so we can just close the capture
file; we return CF_READ_ABORTED so our caller can do whatever
is appropriate when that happens. */
cf_close(cf);
return CF_READ_ABORTED;
}
/* We're done reading sequentially through the file. */
cf->state = FILE_READ_DONE;
/* We're done reading sequentially through the file; close the
sequential I/O side, to free up memory it requires. */
wtap_sequential_close(cf->provider.wth);
/* Allow the protocol dissectors to free up memory that they
* don't need after the sequential run-through of the packets. */
postseq_cleanup_all_protocols();
/* Update the file encapsulation; it might have changed based on the
packets we've read. */
cf->lnk_t = wtap_file_encap(cf->provider.wth);
/* Update the details in the file-set dialog, as the capture file
* has likely grown since we first stat-ed it */
fileset_update_file(cf->filename);
if (*err != 0) {
/* We got an error reading the capture file.
XXX - pop up a dialog box? */
if (err_info != NULL) {
ws_warning("Error \"%s\" while reading \"%s\" (\"%s\")",
wtap_strerror(*err), cf->filename, err_info);
g_free(err_info);
} else {
ws_warning("Error \"%s\" while reading \"%s\"",
wtap_strerror(*err), cf->filename);
}
return CF_READ_ERROR;
} else {
return CF_READ_OK;
}
}
#endif /* HAVE_LIBPCAP */
gchar *
cf_get_display_name(capture_file *cf)
{
gchar *displayname;
/* Return a name to use in displays */
if (!cf->is_tempfile) {
/* Get the last component of the file name, and use that. */
if (cf->filename) {
displayname = g_filename_display_basename(cf->filename);
} else {
displayname=g_strdup("(No file)");
}
} else {
/* The file we read is a temporary file from a live capture or
a merge operation; we don't mention its name, but, if it's
from a capture, give the source of the capture. */
if (cf->source) {
displayname = g_strdup(cf->source);
} else {
displayname = g_strdup("(Untitled)");
}
}
return displayname;
}
gchar *
cf_get_basename(capture_file *cf)
{
gchar *displayname;
/* Return a name to use in the GUI for the basename for files to
which we save statistics */
if (!cf->is_tempfile) {
/* Get the last component of the file name, and use that. */
if (cf->filename) {
displayname = g_filename_display_basename(cf->filename);
/* If the file name ends with any extension that corresponds
to a file type we support - including compressed versions
of those files - strip it off. */
size_t displayname_len = strlen(displayname);
GSList *extensions = wtap_get_all_file_extensions_list();
GSList *suffix;
for (suffix = extensions; suffix != NULL; suffix = g_slist_next(suffix)) {
/* Does the file name end with that extension? */
const char *extension = (char *)suffix->data;
size_t extension_len = strlen(extension);
if (displayname_len > extension_len &&
displayname[displayname_len - extension_len - 1] == '.' &&
strcmp(&displayname[displayname_len - extension_len], extension) == 0) {
/* Yes. Strip the extension off, and return the result. */
displayname[displayname_len - extension_len - 1] = '\0';
break;
}
}
wtap_free_extensions_list(extensions);
} else {
displayname=g_strdup("");
}
} else {
/* The file we read is a temporary file from a live capture or
a merge operation; we don't mention its name, but, if it's
from a capture, give the source of the capture. */
if (cf->source) {
displayname = g_strdup(cf->source);
} else {
displayname = g_strdup("");
}
}
return displayname;
}
void
cf_set_tempfile_source(capture_file *cf, gchar *source)
{
if (cf->source) {
g_free(cf->source);
}
if (source) {
cf->source = g_strdup(source);
} else {
cf->source = g_strdup("");
}
}
const gchar *
cf_get_tempfile_source(capture_file *cf)
{
if (!cf->source) {
return "";
}
return cf->source;
}
/* XXX - use a macro instead? */
int
cf_get_packet_count(capture_file *cf)
{
return cf->count;
}
/* XXX - use a macro instead? */
gboolean
cf_is_tempfile(capture_file *cf)
{
return cf->is_tempfile;
}
void
cf_set_tempfile(capture_file *cf, gboolean is_tempfile)
{
cf->is_tempfile = is_tempfile;
}
/* XXX - use a macro instead? */
void
cf_set_drops_known(capture_file *cf, gboolean drops_known)
{
cf->drops_known = drops_known;
}
/* XXX - use a macro instead? */
void
cf_set_drops(capture_file *cf, guint32 drops)
{
cf->drops = drops;
}
/* XXX - use a macro instead? */
gboolean
cf_get_drops_known(capture_file *cf)
{
return cf->drops_known;
}
/* XXX - use a macro instead? */
guint32
cf_get_drops(capture_file *cf)
{
return cf->drops;
}
void
cf_set_rfcode(capture_file *cf, dfilter_t *rfcode)
{
cf->rfcode = rfcode;
}
static void
add_packet_to_packet_list(frame_data *fdata, capture_file *cf,
epan_dissect_t *edt, dfilter_t *dfcode, column_info *cinfo,
wtap_rec *rec, Buffer *buf, gboolean add_to_packet_list)
{
frame_data_set_before_dissect(fdata, &cf->elapsed_time,
&cf->provider.ref, cf->provider.prev_dis);
cf->provider.prev_cap = fdata;
if (dfcode != NULL) {
epan_dissect_prime_with_dfilter(edt, dfcode);
}
#if 0
/* Prepare coloring rules, this ensures that display filter rules containing
* frame.color_rule references are still processed.
* TODO: actually detect that situation or maybe apply other optimizations? */
if (edt->tree && color_filters_used()) {
color_filters_prime_edt(edt);
fdata->need_colorize = 1;
}
#endif
if (!fdata->visited) {
/* This is the first pass, so prime the epan_dissect_t with the
hfids postdissectors want on the first pass. */
prime_epan_dissect_with_postdissector_wanted_hfids(edt);
}
/* Dissect the frame. */
epan_dissect_run_with_taps(edt, cf->cd_t, rec,
frame_tvbuff_new_buffer(&cf->provider, fdata, buf),
fdata, cinfo);
/* If we don't have a display filter, set "passed_dfilter" to 1. */
if (dfcode != NULL) {
fdata->passed_dfilter = dfilter_apply_edt(dfcode, edt) ? 1 : 0;
if (fdata->passed_dfilter && edt->pi.fd->dependent_frames) {
/* This frame passed the display filter but it may depend on other
* (potentially not displayed) frames. Find those frames and mark them
* as depended upon.
*/
g_hash_table_foreach(edt->pi.fd->dependent_frames, find_and_mark_frame_depended_upon, cf->provider.frames);
}
} else
fdata->passed_dfilter = 1;
if (fdata->passed_dfilter || fdata->ref_time)
cf->displayed_count++;
if (add_to_packet_list) {
/* We fill the needed columns from new_packet_list */
packet_list_append(cinfo, fdata);
}
if (fdata->passed_dfilter || fdata->ref_time)
{
frame_data_set_after_dissect(fdata, &cf->cum_bytes);
/* The only way we use prev_dis is to get the time stamp of
* the previous displayed frame, so ignore it if it doesn't
* have a time stamp, because we're presumably interested in
* the timestamp of the previously displayed frame with a
* time. XXX: What if in the future we want to use the previously
* displayed frame for something else, too?
*/
if (fdata->has_ts) {
cf->provider.prev_dis = fdata;
}
/* If we haven't yet seen the first frame, this is it. */
if (cf->first_displayed == 0)
cf->first_displayed = fdata->num;
/* This is the last frame we've seen so far. */
cf->last_displayed = fdata->num;
}
epan_dissect_reset(edt);
}
/*
* Read in a new record.
* Returns TRUE if the packet was added to the packet (record) list,
* FALSE otherwise.
*/
static gboolean
read_record(capture_file *cf, wtap_rec *rec, Buffer *buf, dfilter_t *dfcode,
epan_dissect_t *edt, column_info *cinfo, gint64 offset,
fifo_string_cache_t *frame_dup_cache, GChecksum *frame_cksum)
{
frame_data fdlocal;
frame_data *fdata;
gboolean passed = TRUE;
gboolean added = FALSE;
const gchar *cksum_string;
gboolean was_in_cache;
/* Add this packet's link-layer encapsulation type to cf->linktypes, if
it's not already there.
XXX - yes, this is O(N), so if every packet had a different
link-layer encapsulation type, it'd be O(N^2) to read the file, but
there are probably going to be a small number of encapsulation types
in a file. */
if (rec->rec_type == REC_TYPE_PACKET) {
cf_add_encapsulation_type(cf, rec->rec_header.packet_header.pkt_encap);
}
/* The frame number of this packet, if we add it to the set of frames,
would be one more than the count of frames in the file so far. */
frame_data_init(&fdlocal, cf->count + 1, rec, offset, cf->cum_bytes);
if (cf->rfcode) {
epan_dissect_t rf_edt;
column_info *rf_cinfo = NULL;
epan_dissect_init(&rf_edt, cf->epan, TRUE, FALSE);
epan_dissect_prime_with_dfilter(&rf_edt, cf->rfcode);
if (dfilter_requires_columns(cf->rfcode)) {
rf_cinfo = &cf->cinfo;
}
epan_dissect_run(&rf_edt, cf->cd_t, rec,
frame_tvbuff_new_buffer(&cf->provider, &fdlocal, buf),
&fdlocal, rf_cinfo);
passed = dfilter_apply_edt(cf->rfcode, &rf_edt);
epan_dissect_cleanup(&rf_edt);
}
if (passed) {
added = TRUE;
/* This does a shallow copy of fdlocal, which is good enough. */
fdata = frame_data_sequence_add(cf->provider.frames, &fdlocal);
cf->count++;
if (rec->block != NULL)
cf->packet_comment_count += wtap_block_count_option(rec->block, OPT_COMMENT);
cf->f_datalen = offset + fdlocal.cap_len;
// Should we check if the frame data is a duplicate, and thus, ignore
// this frame?
if (frame_cksum != NULL && rec->rec_type == REC_TYPE_PACKET) {
g_checksum_reset(frame_cksum);
g_checksum_update(frame_cksum, ws_buffer_start_ptr(buf), ws_buffer_length(buf));
cksum_string = g_strdup(g_checksum_get_string(frame_cksum));
was_in_cache = fifo_string_cache_insert(frame_dup_cache, cksum_string);
if (was_in_cache) {
g_free((gpointer)cksum_string);
fdata->ignored = TRUE;
cf->ignored_count++;
}
}
/* When a redissection is in progress (or queued), do not process packets.
* This will be done once all (new) packets have been scanned. */
if (!cf->redissecting && cf->redissection_queued == RESCAN_NONE) {
add_packet_to_packet_list(fdata, cf, edt, dfcode, cinfo, rec, buf, TRUE);
}
}
return added;
}
typedef struct _callback_data_t {
gpointer pd_window;
gint64 f_len;
progdlg_t *progbar;
GTimer *prog_timer;
gboolean stop_flag;
} callback_data_t;
static gboolean
merge_callback(merge_event event, int num _U_,
const merge_in_file_t in_files[], const guint in_file_count,
void *data)
{
guint i;
callback_data_t *cb_data = (callback_data_t*) data;
ws_assert(cb_data != NULL);
switch (event) {
case MERGE_EVENT_INPUT_FILES_OPENED:
/* do nothing */
break;
case MERGE_EVENT_FRAME_TYPE_SELECTED:
/* do nothing */
break;
case MERGE_EVENT_READY_TO_MERGE:
/* Get the sum of the sizes of all the files. */
for (i = 0; i < in_file_count; i++)
cb_data->f_len += in_files[i].size;
cb_data->prog_timer = g_timer_new();
g_timer_start(cb_data->prog_timer);
break;
case MERGE_EVENT_RECORD_WAS_READ:
{
/* Create the progress bar if necessary.
We check on every iteration of the loop, so that it takes no
longer than the standard time to create it (otherwise, for a
large file, we might take considerably longer than that standard
time in order to get to the next progress bar step). */
if (cb_data->progbar == NULL) {
cb_data->progbar = delayed_create_progress_dlg(cb_data->pd_window, NULL, NULL,
FALSE, &cb_data->stop_flag, 0.0f);
}
/*
* Update the progress bar, but do it only after
* PROGBAR_UPDATE_INTERVAL has elapsed. Calling update_progress_dlg
* and packets_bar_update will likely trigger UI paint events, which
* might take a while depending on the platform and display. Reset
* our timer *after* painting.
*/
if (g_timer_elapsed(cb_data->prog_timer, NULL) > PROGBAR_UPDATE_INTERVAL) {
float progbar_val;
gint64 file_pos = 0;
/* Get the sum of the seek positions in all of the files. */
for (i = 0; i < in_file_count; i++)
file_pos += wtap_read_so_far(in_files[i].wth);
progbar_val = (gfloat) file_pos / (gfloat) cb_data->f_len;
if (progbar_val > 1.0f) {
/* Some file probably grew while we were reading it.
That "shouldn't happen", so we'll just clip the progress
value at 1.0. */
progbar_val = 1.0f;
}
if (cb_data->progbar != NULL) {
gchar status_str[100];
snprintf(status_str, sizeof(status_str),
"%" PRId64 "KB of %" PRId64 "KB",
file_pos / 1024, cb_data->f_len / 1024);
update_progress_dlg(cb_data->progbar, progbar_val, status_str);
}
g_timer_start(cb_data->prog_timer);
}
}
break;
case MERGE_EVENT_DONE:
/* We're done merging the files; destroy the progress bar if it was created. */
if (cb_data->progbar != NULL)
destroy_progress_dlg(cb_data->progbar);
g_timer_destroy(cb_data->prog_timer);
break;
}
return cb_data->stop_flag;
}
cf_status_t
cf_merge_files_to_tempfile(gpointer pd_window, const char *temp_dir, char **out_filenamep,
int in_file_count, const char *const *in_filenames,
int file_type, gboolean do_append)
{
int err = 0;
gchar *err_info = NULL;
guint err_fileno;
guint32 err_framenum;
merge_result status;
merge_progress_callback_t cb;
callback_data_t *cb_data = g_new0(callback_data_t, 1);
/* prepare our callback routine */
cb_data->pd_window = pd_window;
cb.callback_func = merge_callback;
cb.data = cb_data;
cf_callback_invoke(cf_cb_file_merge_started, NULL);
/* merge the files */
status = merge_files_to_tempfile(temp_dir, out_filenamep, "wireshark", file_type,
in_filenames,
in_file_count, do_append,
IDB_MERGE_MODE_ALL_SAME, 0 /* snaplen */,
"Wireshark", &cb, &err, &err_info,
&err_fileno, &err_framenum);
g_free(cb.data);
switch (status) {
case MERGE_OK:
break;
case MERGE_USER_ABORTED:
/* this isn't really an error, though we will return CF_ERROR later */
break;
case MERGE_ERR_CANT_OPEN_INFILE:
cfile_open_failure_alert_box(in_filenames[err_fileno], err, err_info);
break;
case MERGE_ERR_CANT_OPEN_OUTFILE:
cfile_dump_open_failure_alert_box(*out_filenamep, err, err_info,
file_type);
break;
case MERGE_ERR_CANT_READ_INFILE:
cfile_read_failure_alert_box(in_filenames[err_fileno], err, err_info);
break;
case MERGE_ERR_BAD_PHDR_INTERFACE_ID:
simple_error_message_box("Record %u of \"%s\" has an interface ID that does not match any IDB in its file.",
err_framenum, in_filenames[err_fileno]);
break;
case MERGE_ERR_CANT_WRITE_OUTFILE:
cfile_write_failure_alert_box(in_filenames[err_fileno],
*out_filenamep, err, err_info,
err_framenum, file_type);
break;
case MERGE_ERR_CANT_CLOSE_OUTFILE:
cfile_close_failure_alert_box(*out_filenamep, err, err_info);
break;
default:
simple_error_message_box("Unknown merge_files error %d", status);
break;
}
cf_callback_invoke(cf_cb_file_merge_finished, NULL);
if (status != MERGE_OK) {
/* Callers aren't expected to treat an error or an explicit abort
differently - we put up error dialogs ourselves, so they don't
have to. */
return CF_ERROR;
} else
return CF_OK;
}
cf_status_t
cf_filter_packets(capture_file *cf, gchar *dftext, gboolean force)
{
const char *filter_new = dftext ? dftext : "";
const char *filter_old = cf->dfilter ? cf->dfilter : "";
dfilter_t *dfcode;
df_error_t *df_err;
/* if new filter equals old one, do nothing unless told to do so */
if (!force && strcmp(filter_new, filter_old) == 0) {
return CF_OK;
}
dfcode=NULL;
if (dftext == NULL) {
/* The new filter is an empty filter (i.e., display all packets).
* so leave dfcode==NULL
*/
} else {
/*
* We have a filter; make a copy of it (as we'll be saving it),
* and try to compile it.
*/
dftext = g_strdup(dftext);
if (!dfilter_compile(dftext, &dfcode, &df_err)) {
/* The attempt failed; report an error. */
simple_message_box(ESD_TYPE_ERROR, NULL,
"See the help for a description of the display filter syntax.",
"\"%s\" isn't a valid display filter: %s",
dftext, df_err->msg);
df_error_free(&df_err);
g_free(dftext);
return CF_ERROR;
}
/* Was it empty? */
if (dfcode == NULL) {
/* Yes - free the filter text, and set it to null. */
g_free(dftext);
dftext = NULL;
}
}
/* We have a valid filter. Replace the current filter. */
g_free(cf->dfilter);
cf->dfilter = dftext;
/* Now rescan the packet list, applying the new filter, but not
* throwing away information constructed on a previous pass.
* If a dissection is already in progress, queue it.
*/
if (cf->redissection_queued == RESCAN_NONE) {
if (cf->read_lock) {
cf->redissection_queued = RESCAN_SCAN;
} else if (cf->state != FILE_CLOSED) {
if (dftext == NULL) {
rescan_packets(cf, "Resetting", "filter", FALSE);
} else {
rescan_packets(cf, "Filtering", dftext, FALSE);
}
}
}
/* Cleanup and release all dfilter resources */
dfilter_free(dfcode);
return CF_OK;
}
void
cf_redissect_packets(capture_file *cf)
{
if (cf->read_lock || cf->redissection_queued == RESCAN_SCAN) {
/* Dissection in progress, signal redissection rather than rescanning. That
* would destroy the current (in-progress) dissection in "cf_read" which
* will cause issues when "cf_read" tries to add packets to the list.
* If a previous rescan was requested, "upgrade" it to a full redissection.
*/
cf->redissection_queued = RESCAN_REDISSECT;
}
if (cf->redissection_queued != RESCAN_NONE) {
/* Redissection is (already) queued, wait for "cf_read" to finish. */
return;
}
if (cf->state != FILE_CLOSED) {
/* Restart dissection in case no cf_read is pending. */
rescan_packets(cf, "Reprocessing", "all packets", TRUE);
}
}
gboolean
cf_read_record(capture_file *cf, const frame_data *fdata,
wtap_rec *rec, Buffer *buf)
{
int err;
gchar *err_info;
if (!wtap_seek_read(cf->provider.wth, fdata->file_off, rec, buf, &err, &err_info)) {
cfile_read_failure_alert_box(cf->filename, err, err_info);
return FALSE;
}
return TRUE;
}
gboolean
cf_read_record_no_alert(capture_file *cf, const frame_data *fdata,
wtap_rec *rec, Buffer *buf)
{
int err;
gchar *err_info;
if (!wtap_seek_read(cf->provider.wth, fdata->file_off, rec, buf, &err, &err_info)) {
g_free(err_info);
return FALSE;
}
return TRUE;
}
gboolean
cf_read_current_record(capture_file *cf)
{
return cf_read_record(cf, cf->current_frame, &cf->rec, &cf->buf);
}
/* Rescan the list of packets, reconstructing the CList.
"action" describes why we're doing this; it's used in the progress
dialog box.
"action_item" describes what we're doing; it's used in the progress
dialog box.
"redissect" is TRUE if we need to make the dissectors reconstruct
any state information they have (because a preference that affects
some dissector has changed, meaning some dissector might construct
its state differently from the way it was constructed the last time). */
static void
rescan_packets(capture_file *cf, const char *action, const char *action_item, gboolean redissect)
{
/* Rescan packets new packet list */
guint32 framenum;
frame_data *fdata;
wtap_rec rec;
Buffer buf;
progdlg_t *progbar = NULL;
GTimer *prog_timer = g_timer_new();
int count;
frame_data *selected_frame, *preceding_frame, *following_frame, *prev_frame;
int selected_frame_num, preceding_frame_num, following_frame_num, prev_frame_num;
gboolean selected_frame_seen;
float progbar_val;
gint64 start_time;
gchar status_str[100];
epan_dissect_t edt;
dfilter_t *dfcode = NULL;
column_info *cinfo;
gboolean create_proto_tree;
gboolean filtering_tap_listeners = FALSE;
guint tap_flags;
gboolean add_to_packet_list = FALSE;
gboolean compiled _U_;
guint32 frames_count;
gboolean queued_rescan_type = RESCAN_NONE;
if (cf->state == FILE_CLOSED || cf->state == FILE_READ_PENDING) {
return;
}
/* Rescan in progress, clear pending actions. */
cf->redissection_queued = RESCAN_NONE;
ws_assert(!cf->read_lock);
cf->read_lock = TRUE;
wtap_rec_init(&rec);
ws_buffer_init(&buf, 1514);
/* Compile the current display filter.
* We assume this will not fail since cf->dfilter is only set in
* cf_filter IFF the filter was valid.
*/
if (cf->dfilter) {
compiled = dfilter_compile(cf->dfilter, &dfcode, NULL);
ws_assert(compiled && dfcode);
}
/* Do we have any tap listeners with filters? */
filtering_tap_listeners = have_filtering_tap_listeners();
/* Update references in filters (if any) for the protocol
* tree corresponding to the currently selected frame in the GUI. */
if (cf->edt != NULL && cf->edt->tree != NULL) {
if (dfcode)
dfilter_load_field_references(dfcode, cf->edt->tree);
if (filtering_tap_listeners)
tap_listeners_load_field_references(cf->edt);
}
if (dfcode != NULL) {
dfilter_log_full(LOG_DOMAIN_DFILTER, LOG_LEVEL_NOISY, NULL, -1, NULL,
dfcode, "Rescanning packets with display filter");
}
/* Get the union of the flags for all tap listeners. */
tap_flags = union_of_tap_listener_flags();
/* If the display filter or any tap listeners require the columns,
* construct them. */
cinfo = (tap_listeners_require_columns() ||
dfilter_requires_columns(dfcode)) ? &cf->cinfo : NULL;
/*
* Determine whether we need to create a protocol tree.
* We do if:
*
* we're going to apply a display filter;
*
* one of the tap listeners is going to apply a filter;
*
* one of the tap listeners requires a protocol tree;
*
* we're redissecting and a postdissector wants field
* values or protocols on the first pass.
*/
create_proto_tree =
(dfcode != NULL || filtering_tap_listeners ||
(tap_flags & TL_REQUIRES_PROTO_TREE) ||
(redissect && postdissectors_want_hfids()));
reset_tap_listeners();
/* Which frame, if any, is the currently selected frame?
XXX - should the selected frame or the focus frame be the "current"
frame, that frame being the one from which "Find Frame" searches
start? */
selected_frame = cf->current_frame;
/* Mark frame num as not found */
selected_frame_num = -1;
/* Freeze the packet list while we redo it, so we don't get any
screen updates while it happens. */
packet_list_freeze();
if (redissect) {
/* We need to re-initialize all the state information that protocols
keep, because some preference that controls a dissector has changed,
which might cause the state information to be constructed differently
by that dissector. */
/* We might receive new packets while redissecting, and we don't
want to dissect those before their time. */
cf->redissecting = TRUE;
/* 'reset' dissection session */
epan_free(cf->epan);
if (cf->edt && cf->edt->pi.fd) {
/* All pointers in "per frame proto data" for the currently selected
packet are allocated in wmem_file_scope() and deallocated in epan_free().
Free them here to avoid unintended usage in packet_list_clear(). */
frame_data_destroy(cf->edt->pi.fd);
}
cf->epan = ws_epan_new(cf);
cf->cinfo.epan = cf->epan;
/* A new Lua tap listener may be registered in lua_prime_all_fields()
called via epan_new() / init_dissection() when reloading Lua plugins. */
if (!create_proto_tree && have_filtering_tap_listeners()) {
create_proto_tree = TRUE;
}
if (!cinfo && tap_listeners_require_columns()) {
cinfo = &cf->cinfo;
}
/* We need to redissect the packets so we have to discard our old
* packet list store. */
packet_list_clear();
add_to_packet_list = TRUE;
}
/* We don't yet know which will be the first and last frames displayed. */
cf->first_displayed = 0;
cf->last_displayed = 0;
/* We currently don't display any packets */
cf->displayed_count = 0;
/* Iterate through the list of frames. Call a routine for each frame
to check whether it should be displayed and, if so, add it to
the display list. */
cf->provider.ref = NULL;
cf->provider.prev_dis = NULL;
cf->provider.prev_cap = NULL;
cf->cum_bytes = 0;
cf_callback_invoke(cf_cb_file_rescan_started, cf);
g_timer_start(prog_timer);
/* Count of packets at which we've looked. */
count = 0;
/* Progress so far. */
progbar_val = 0.0f;
cf->stop_flag = FALSE;
start_time = g_get_monotonic_time();
/* no previous row yet */
prev_frame_num = -1;
prev_frame = NULL;
preceding_frame_num = -1;
preceding_frame = NULL;
following_frame_num = -1;
following_frame = NULL;
selected_frame_seen = FALSE;
frames_count = cf->count;
epan_dissect_init(&edt, cf->epan, create_proto_tree, FALSE);
if (redissect) {
/*
* Decryption secrets and name resolution blocks are read while
* sequentially processing records and then passed to the dissector.
* During redissection, the previous information is lost (see epan_free
* above), but they are not read again from the file as only packet
* records are re-read. Therefore reset the wtap secrets and name
* resolution callbacks such that wtap resupplies the callbacks with
* previously read information.
*/
wtap_set_cb_new_ipv4(cf->provider.wth, add_ipv4_name);
wtap_set_cb_new_ipv6(cf->provider.wth, (wtap_new_ipv6_callback_t) add_ipv6_name);
wtap_set_cb_new_secrets(cf->provider.wth, secrets_wtap_callback);
}
for (framenum = 1; framenum <= frames_count; framenum++) {
fdata = frame_data_sequence_find(cf->provider.frames, framenum);
/* Create the progress bar if necessary.
We check on every iteration of the loop, so that it takes no
longer than the standard time to create it (otherwise, for a
large file, we might take considerably longer than that standard
time in order to get to the next progress bar step). */
if (progbar == NULL)
progbar = delayed_create_progress_dlg(cf->window, action, action_item, TRUE,
&cf->stop_flag,
progbar_val);
/*
* Update the progress bar, but do it only after PROGBAR_UPDATE_INTERVAL
* has elapsed. Calling update_progress_dlg and packets_bar_update will
* likely trigger UI paint events, which might take a while depending on
* the platform and display. Reset our timer *after* painting.
*/
if (g_timer_elapsed(prog_timer, NULL) > PROGBAR_UPDATE_INTERVAL) {
/* let's not divide by zero. I should never be started
* with count == 0, so let's assert that
*/
ws_assert(cf->count > 0);
progbar_val = (gfloat) count / frames_count;
if (progbar != NULL) {
snprintf(status_str, sizeof(status_str),
"%4u of %u frames", count, frames_count);
update_progress_dlg(progbar, progbar_val, status_str);
}
g_timer_start(prog_timer);
}
queued_rescan_type = cf->redissection_queued;
if (queued_rescan_type != RESCAN_NONE) {
/* A redissection was requested while an existing redissection was
* pending. */
break;
}
if (cf->stop_flag) {
/* Well, the user decided to abort the filtering. Just stop.
XXX - go back to the previous filter? Users probably just
want not to wait for a filtering operation to finish;
unless we cancel by having no filter, reverting to the
previous filter will probably be even more expensive than
continuing the filtering, as it involves going back to the
beginning and filtering, and even with no filter we currently
have to re-generate the entire clist, which is also expensive.
I'm not sure what Network Monitor does, but it doesn't appear
to give you an unfiltered display if you cancel. */
break;
}
count++;
if (redissect) {
/* Since all state for the frame was destroyed, mark the frame
* as not visited, free the GSList referring to the state
* data (the per-frame data itself was freed by
* "init_dissection()"), and null out the GSList pointer. */
frame_data_reset(fdata);
frames_count = cf->count;
}
/* Frame dependencies from the previous dissection/filtering are no longer valid. */
fdata->dependent_of_displayed = 0;
if (!cf_read_record(cf, fdata, &rec, &buf))
break; /* error reading the frame */
/* If the previous frame is displayed, and we haven't yet seen the
selected frame, remember that frame - it's the closest one we've
yet seen before the selected frame. */
if (prev_frame_num != -1 && !selected_frame_seen && prev_frame->passed_dfilter) {
preceding_frame_num = prev_frame_num;
preceding_frame = prev_frame;
}
add_packet_to_packet_list(fdata, cf, &edt, dfcode,
cinfo, &rec, &buf,
add_to_packet_list);
/* If this frame is displayed, and this is the first frame we've
seen displayed after the selected frame, remember this frame -
it's the closest one we've yet seen at or after the selected
frame. */
if (fdata->passed_dfilter && selected_frame_seen && following_frame_num == -1) {
following_frame_num = fdata->num;
following_frame = fdata;
}
if (fdata == selected_frame) {
selected_frame_seen = TRUE;
if (fdata->passed_dfilter)
selected_frame_num = fdata->num;
}
/* Remember this frame - it'll be the previous frame
on the next pass through the loop. */
prev_frame_num = fdata->num;
prev_frame = fdata;
wtap_rec_reset(&rec);
}
epan_dissect_cleanup(&edt);
wtap_rec_cleanup(&rec);
ws_buffer_free(&buf);
/* We are done redissecting the packet list. */
cf->redissecting = FALSE;
if (redissect) {
frames_count = cf->count;
/* Clear out what remains of the visited flags and per-frame data
pointers.
XXX - that may cause various forms of bogosity when dissecting
these frames, as they won't have been seen by this sequential
pass, but the only alternative I see is to keep scanning them
even though the user requested that the scan stop, and that
would leave the user stuck with an Wireshark grinding on
until it finishes. Should we just stick them with that? */
for (; framenum <= frames_count; framenum++) {
fdata = frame_data_sequence_find(cf->provider.frames, framenum);
frame_data_reset(fdata);
}
}
/* We're done filtering the packets; destroy the progress bar if it
was created. */
if (progbar != NULL)
destroy_progress_dlg(progbar);
g_timer_destroy(prog_timer);
/* Unfreeze the packet list. */
if (!add_to_packet_list)
packet_list_recreate_visible_rows();
/* Compute the time it took to filter the file */
compute_elapsed(cf, start_time);
packet_list_thaw();
/* It is safe again to execute redissections or sort. */
ws_assert(cf->read_lock);
cf->read_lock = FALSE;
cf_callback_invoke(cf_cb_file_rescan_finished, cf);
if (selected_frame_num == -1) {
/* The selected frame didn't pass the filter. */
if (selected_frame == NULL) {
/* That's because there *was* no selected frame. Make the first
displayed frame the current frame. */
selected_frame_num = 0;
} else {
/* Find the nearest displayed frame to the selected frame (whether
it's before or after that frame) and make that the current frame.
If the next and previous displayed frames are equidistant from the
selected frame, choose the next one. */
ws_assert(following_frame == NULL ||
following_frame->num >= selected_frame->num);
ws_assert(preceding_frame == NULL ||
preceding_frame->num <= selected_frame->num);
if (following_frame == NULL) {
/* No frame after the selected frame passed the filter, so we
have to select the last displayed frame before the selected
frame. */
selected_frame_num = preceding_frame_num;
selected_frame = preceding_frame;
} else if (preceding_frame == NULL) {
/* No frame before the selected frame passed the filter, so we
have to select the first displayed frame after the selected
frame. */
selected_frame_num = following_frame_num;
selected_frame = following_frame;
} else {
/* Frames before and after the selected frame passed the filter, so
we'll select the previous frame */
selected_frame_num = preceding_frame_num;
selected_frame = preceding_frame;
}
}
}
if (selected_frame_num == -1) {
/* There are no frames displayed at all. */
cf_unselect_packet(cf);
} else {
/* Either the frame that was selected passed the filter, or we've
found the nearest displayed frame to that frame. Select it, make
it the focus row, and make it visible. */
/* Set to invalid to force update of packet list and packet details */
if (selected_frame_num == 0) {
packet_list_select_row_from_data(NULL);
}else{
if (!packet_list_select_row_from_data(selected_frame)) {
/* We didn't find a row corresponding to this frame.
This means that the frame isn't being displayed currently,
so we can't select it. */
simple_message_box(ESD_TYPE_INFO, NULL,
"The capture file is probably not fully dissected.",
"End of capture exceeded.");
}
}
}
/* Cleanup and release all dfilter resources */
dfilter_free(dfcode);
/* If another rescan (due to dfilter change) or redissection (due to profile
* change) was requested, the rescan above is aborted and restarted here. */
if (queued_rescan_type != RESCAN_NONE) {
redissect = redissect || queued_rescan_type == RESCAN_REDISSECT;
rescan_packets(cf, "Reprocessing", "all packets", redissect);
}
}
/*
* Scan through all frame data and recalculate the ref time
* without rereading the file.
* XXX - do we need a progress bar or is this fast enough?
*/
void
cf_reftime_packets(capture_file* cf)
{
guint32 framenum;
frame_data *fdata;
nstime_t rel_ts;
cf->provider.ref = NULL;
cf->provider.prev_dis = NULL;
cf->cum_bytes = 0;
for (framenum = 1; framenum <= cf->count; framenum++) {
fdata = frame_data_sequence_find(cf->provider.frames, framenum);
/* just add some value here until we know if it is being displayed or not */
fdata->cum_bytes = cf->cum_bytes + fdata->pkt_len;
/*
* Timestamps
*/
if (fdata->has_ts) {
/* If we don't have the time stamp of the first packet in the
capture, it's because this is the first packet. Save the time
stamp of this packet as the time stamp of the first packet. */
if (cf->provider.ref == NULL)
cf->provider.ref = fdata;
/* if this frames is marked as a reference time frame, reset
firstsec and firstusec to this frame */
if (fdata->ref_time)
cf->provider.ref = fdata;
/* Get the time elapsed between the first packet and this one. */
fdata->frame_ref_num = (fdata != cf->provider.ref) ? cf->provider.ref->num : 0;
nstime_delta(&rel_ts, &fdata->abs_ts, &cf->provider.ref->abs_ts);
/* If it's greater than the current elapsed time, set the elapsed
time to it (we check for "greater than" so as not to be
confused by time moving backwards). */
if ((gint32)cf->elapsed_time.secs < rel_ts.secs
|| ((gint32)cf->elapsed_time.secs == rel_ts.secs && (gint32)cf->elapsed_time.nsecs < rel_ts.nsecs)) {
cf->elapsed_time = rel_ts;
}
/* If this frame is displayed, get the time elapsed between the
previous displayed packet and this packet. */
/* XXX: What if in the future we want to use the previously
* displayed frame for something else, too? Then we'd want
* to store this frame as prev_dis even if it doesn't have a
* timestamp. */
if ( fdata->passed_dfilter ) {
/* If we don't have the time stamp of the previous displayed
packet, it's because this is the first displayed packet.
Save the time stamp of this packet as the time stamp of
the previous displayed packet. */
if (cf->provider.prev_dis == NULL) {
cf->provider.prev_dis = fdata;
}
fdata->prev_dis_num = cf->provider.prev_dis->num;
cf->provider.prev_dis = fdata;
}
} else {
/* If this frame doesn't have a timestamp, don't calculate
anything with relative times. */
/* However, if this frame is marked as a reference time frame,
clear the reference frame so that the next frame with a
timestamp becomes the reference frame. */
if (fdata->ref_time) {
cf->provider.ref = NULL;
}
}
/*
* Byte counts
*/
if ( (fdata->passed_dfilter) || (fdata->ref_time) ) {
/* This frame either passed the display filter list or is marked as
a time reference frame. All time reference frames are displayed
even if they don't pass the display filter */
if (fdata->ref_time) {
/* if this was a TIME REF frame we should reset the cum_bytes field */
cf->cum_bytes = fdata->pkt_len;
fdata->cum_bytes = cf->cum_bytes;
} else {
/* increase cum_bytes with this packets length */
cf->cum_bytes += fdata->pkt_len;
}
}
}
}
typedef enum {
PSP_FINISHED,
PSP_STOPPED,
PSP_FAILED
} psp_return_t;
static psp_return_t
process_specified_records(capture_file *cf, packet_range_t *range,
const char *string1, const char *string2, gboolean terminate_is_stop,
gboolean (*callback)(capture_file *, frame_data *,
wtap_rec *, Buffer *, void *),
void *callback_args,
gboolean show_progress_bar)
{
guint32 framenum;
frame_data *fdata;
wtap_rec rec;
Buffer buf;
psp_return_t ret = PSP_FINISHED;
progdlg_t *progbar = NULL;
GTimer *prog_timer = g_timer_new();
int progbar_count;
float progbar_val;
gchar progbar_status_str[100];
range_process_e process_this;
wtap_rec_init(&rec);
ws_buffer_init(&buf, 1514);
g_timer_start(prog_timer);
/* Count of packets at which we've looked. */
progbar_count = 0;
/* Progress so far. */
progbar_val = 0.0f;
if (cf->read_lock) {
ws_warning("Failing due to nested process_specified_records(\"%s\") call!", cf->filename);
return PSP_FAILED;
}
cf->read_lock = TRUE;
cf->stop_flag = FALSE;
if (range != NULL)
packet_range_process_init(range);
/* Iterate through all the packets, printing the packets that
were selected by the current display filter. */
for (framenum = 1; framenum <= cf->count; framenum++) {
fdata = frame_data_sequence_find(cf->provider.frames, framenum);
/* Create the progress bar if necessary.
We check on every iteration of the loop, so that it takes no
longer than the standard time to create it (otherwise, for a
large file, we might take considerably longer than that standard
time in order to get to the next progress bar step). */
if (show_progress_bar && progbar == NULL)
progbar = delayed_create_progress_dlg(cf->window, string1, string2,
terminate_is_stop,
&cf->stop_flag,
progbar_val);
/*
* Update the progress bar, but do it only after PROGBAR_UPDATE_INTERVAL
* has elapsed. Calling update_progress_dlg and packets_bar_update will
* likely trigger UI paint events, which might take a while depending on
* the platform and display. Reset our timer *after* painting.
*/
if (progbar && g_timer_elapsed(prog_timer, NULL) > PROGBAR_UPDATE_INTERVAL) {
/* let's not divide by zero. I should never be started
* with count == 0, so let's assert that
*/
ws_assert(cf->count > 0);
progbar_val = (gfloat) progbar_count / cf->count;
snprintf(progbar_status_str, sizeof(progbar_status_str),
"%4u of %u packets", progbar_count, cf->count);
update_progress_dlg(progbar, progbar_val, progbar_status_str);
g_timer_start(prog_timer);
}
if (cf->stop_flag) {
/* Well, the user decided to abort the operation. Just stop,
and arrange to return PSP_STOPPED to our caller, so they know
it was stopped explicitly. */
ret = PSP_STOPPED;
break;
}
progbar_count++;
if (range != NULL) {
/* do we have to process this packet? */
process_this = packet_range_process_packet(range, fdata);
if (process_this == range_process_next) {
/* this packet uninteresting, continue with next one */
continue;
} else if (process_this == range_processing_finished) {
/* all interesting packets processed, stop the loop */
break;
}
}
/* Get the packet */
if (!cf_read_record(cf, fdata, &rec, &buf)) {
/* Attempt to get the packet failed. */
ret = PSP_FAILED;
break;
}
/* Process the packet */
if (!callback(cf, fdata, &rec, &buf, callback_args)) {
/* Callback failed. We assume it reported the error appropriately. */
ret = PSP_FAILED;
break;
}
wtap_rec_reset(&rec);
}
/* We're done printing the packets; destroy the progress bar if
it was created. */
if (progbar != NULL)
destroy_progress_dlg(progbar);
g_timer_destroy(prog_timer);
ws_assert(cf->read_lock);
cf->read_lock = FALSE;
wtap_rec_cleanup(&rec);
ws_buffer_free(&buf);
return ret;
}
typedef struct {
epan_dissect_t edt;
column_info *cinfo;
} retap_callback_args_t;
static gboolean
retap_packet(capture_file *cf, frame_data *fdata, wtap_rec *rec, Buffer *buf,
void *argsp)
{
retap_callback_args_t *args = (retap_callback_args_t *)argsp;
epan_dissect_run_with_taps(&args->edt, cf->cd_t, rec,
frame_tvbuff_new_buffer(&cf->provider, fdata, buf),
fdata, args->cinfo);
epan_dissect_reset(&args->edt);
return TRUE;
}
cf_read_status_t
cf_retap_packets(capture_file *cf)
{
packet_range_t range;
retap_callback_args_t callback_args;
gboolean create_proto_tree;
gboolean filtering_tap_listeners;
guint tap_flags;
psp_return_t ret;
/* Presumably the user closed the capture file. */
if (cf == NULL) {
return CF_READ_ABORTED;
}
cf_callback_invoke(cf_cb_file_retap_started, cf);
/* Do we have any tap listeners with filters? */
filtering_tap_listeners = have_filtering_tap_listeners();
/* Update references in filters (if any) for the protocol
* tree corresponding to the currently selected frame in the GUI. */
if (cf->edt != NULL && cf->edt->tree != NULL) {
if (filtering_tap_listeners)
tap_listeners_load_field_references(cf->edt);
}
/* Get the union of the flags for all tap listeners. */
tap_flags = union_of_tap_listener_flags();
/* If any tap listeners require the columns, construct them. */
callback_args.cinfo = (tap_listeners_require_columns()) ? &cf->cinfo : NULL;
/*
* Determine whether we need to create a protocol tree.
* We do if:
*
* one of the tap listeners is going to apply a filter;
*
* one of the tap listeners requires a protocol tree.
*/
create_proto_tree =
(filtering_tap_listeners || (tap_flags & TL_REQUIRES_PROTO_TREE));
/* Reset the tap listeners. */
reset_tap_listeners();
epan_dissect_init(&callback_args.edt, cf->epan, create_proto_tree, FALSE);
/* Iterate through the list of packets, dissecting all packets and
re-running the taps. */
packet_range_init(&range, cf);
packet_range_process_init(&range);
ret = process_specified_records(cf, &range, "Recalculating statistics on",
"all packets", TRUE, retap_packet,
&callback_args, TRUE);
packet_range_cleanup(&range);
epan_dissect_cleanup(&callback_args.edt);
cf_callback_invoke(cf_cb_file_retap_finished, cf);
switch (ret) {
case PSP_FINISHED:
/* Completed successfully. */
return CF_READ_OK;
case PSP_STOPPED:
/* Well, the user decided to abort the refiltering.
Return CF_READ_ABORTED so our caller knows they did that. */
return CF_READ_ABORTED;
case PSP_FAILED:
/* Error while retapping. */
return CF_READ_ERROR;
}
ws_assert_not_reached();
return CF_READ_OK;
}
typedef struct {
print_args_t *print_args;
gboolean print_header_line;
char *header_line_buf;
int header_line_buf_len;
gboolean print_formfeed;
gboolean print_separator;
char *line_buf;
int line_buf_len;
gint *col_widths;
int num_visible_cols;
gint *visible_cols;
epan_dissect_t edt;
} print_callback_args_t;
static gboolean
print_packet(capture_file *cf, frame_data *fdata, wtap_rec *rec, Buffer *buf,
void *argsp)
{
print_callback_args_t *args = (print_callback_args_t *)argsp;
int i;
char *cp;
int line_len;
int column_len;
int cp_off;
char bookmark_name[9+10+1]; /* "__frameNNNNNNNNNN__\0" */
char bookmark_title[6+10+1]; /* "Frame NNNNNNNNNN__\0" */
col_item_t* col_item;
const gchar* col_text;
/* Fill in the column information if we're printing the summary
information. */
if (args->print_args->print_summary) {
col_custom_prime_edt(&args->edt, &cf->cinfo);
epan_dissect_run(&args->edt, cf->cd_t, rec,
frame_tvbuff_new_buffer(&cf->provider, fdata, buf),
fdata, &cf->cinfo);
epan_dissect_fill_in_columns(&args->edt, FALSE, TRUE);
} else
epan_dissect_run(&args->edt, cf->cd_t, rec,
frame_tvbuff_new_buffer(&cf->provider, fdata, buf),
fdata, NULL);
if (args->print_formfeed) {
if (!new_page(args->print_args->stream))
goto fail;
/*
* Print another header line if we print a packet summary on the
* new page.
*/
if (args->print_args->print_col_headings)
args->print_header_line = TRUE;
} else {
if (args->print_separator) {
if (!print_line(args->print_args->stream, 0, ""))
goto fail;
}
}
/*
* We generate bookmarks, if the output format supports them.
* The name is "__frameN__".
*/
snprintf(bookmark_name, sizeof bookmark_name, "__frame%u__", fdata->num);
if (args->print_args->print_summary) {
if (!args->print_args->print_col_headings)
args->print_header_line = FALSE;
if (args->print_header_line) {
if (!print_line(args->print_args->stream, 0, args->header_line_buf))
goto fail;
args->print_header_line = FALSE; /* we might not need to print any more */
}
cp = &args->line_buf[0];
line_len = 0;
for (i = 0; i < args->num_visible_cols; i++) {
col_item = &cf->cinfo.columns[args->visible_cols[i]];
col_text = get_column_text(&cf->cinfo, args->visible_cols[i]);
/* Find the length of the string for this column. */
column_len = (int) strlen(col_text);
if (args->col_widths[i] > column_len)
column_len = args->col_widths[i];
/* Make sure there's room in the line buffer for the column; if not,
double its length. */
line_len += column_len + 1; /* "+1" for space */
if (line_len > args->line_buf_len) {
cp_off = (int) (cp - args->line_buf);
args->line_buf_len = 2 * line_len;
args->line_buf = (char *)g_realloc(args->line_buf, args->line_buf_len + 1);
cp = args->line_buf + cp_off;
}
/* Right-justify the packet number column. */
if (col_item->col_fmt == COL_NUMBER)
snprintf(cp, column_len+1, "%*s", args->col_widths[i], col_text);
else
snprintf(cp, column_len+1, "%-*s", args->col_widths[i], col_text);
cp += column_len;
if (i != args->num_visible_cols - 1)
*cp++ = ' ';
}
*cp = '\0';
/*
* Generate a bookmark, using the summary line as the title.
*/
if (!print_bookmark(args->print_args->stream, bookmark_name,
args->line_buf))
goto fail;
if (!print_line(args->print_args->stream, 0, args->line_buf))
goto fail;
} else {
/*
* Generate a bookmark, using "Frame N" as the title, as we're not
* printing the summary line.
*/
snprintf(bookmark_title, sizeof bookmark_title, "Frame %u", fdata->num);
if (!print_bookmark(args->print_args->stream, bookmark_name,
bookmark_title))
goto fail;
} /* if (print_summary) */
if (args->print_args->print_dissections != print_dissections_none) {
if (args->print_args->print_summary) {
/* Separate the summary line from the tree with a blank line. */
if (!print_line(args->print_args->stream, 0, ""))
goto fail;
}
/* Print the information in that tree. */
if (!proto_tree_print(args->print_args->print_dissections,
args->print_args->print_hex, &args->edt, NULL,
args->print_args->stream))
goto fail;
/* Print a blank line if we print anything after this (aka more than one packet). */
args->print_separator = TRUE;
/* Print a header line if we print any more packet summaries */
if (args->print_args->print_col_headings)
args->print_header_line = TRUE;
}
if (args->print_args->print_hex) {
if (args->print_args->print_summary || (args->print_args->print_dissections != print_dissections_none)) {
if (!print_line(args->print_args->stream, 0, ""))
goto fail;
}
/* Print the full packet data as hex. */
if (!print_hex_data(args->print_args->stream, &args->edt, args->print_args->hexdump_options))
goto fail;
/* Print a blank line if we print anything after this (aka more than one packet). */
args->print_separator = TRUE;
/* Print a header line if we print any more packet summaries */
if (args->print_args->print_col_headings)
args->print_header_line = TRUE;
} /* if (args->print_args->print_dissections != print_dissections_none) */
epan_dissect_reset(&args->edt);
/* do we want to have a formfeed between each packet from now on? */
if (args->print_args->print_formfeed) {
args->print_formfeed = TRUE;
}
return TRUE;
fail:
epan_dissect_reset(&args->edt);
return FALSE;
}
cf_print_status_t
cf_print_packets(capture_file *cf, print_args_t *print_args,
gboolean show_progress_bar)
{
print_callback_args_t callback_args;
gint data_width;
char *cp;
int i, cp_off, column_len, line_len;
int num_visible_col = 0, last_visible_col = 0, visible_col_count;
psp_return_t ret;
GList *clp;
fmt_data *cfmt;
gboolean proto_tree_needed;
callback_args.print_args = print_args;
callback_args.print_header_line = print_args->print_col_headings;
callback_args.header_line_buf = NULL;
callback_args.header_line_buf_len = 256;
callback_args.print_formfeed = FALSE;
callback_args.print_separator = FALSE;
callback_args.line_buf = NULL;
callback_args.line_buf_len = 256;
callback_args.col_widths = NULL;
callback_args.num_visible_cols = 0;
callback_args.visible_cols = NULL;
if (!print_preamble(print_args->stream, cf->filename, get_ws_vcs_version_info())) {
destroy_print_stream(print_args->stream);
return CF_PRINT_WRITE_ERROR;
}
if (print_args->print_summary) {
/* We're printing packet summaries. Allocate the header line buffer
and get the column widths. */
callback_args.header_line_buf = (char *)g_malloc(callback_args.header_line_buf_len + 1);
/* Find the number of visible columns and the last visible column */
for (i = 0; i < prefs.num_cols; i++) {
clp = g_list_nth(prefs.col_list, i);
if (clp == NULL) /* Sanity check, Invalid column requested */
continue;
cfmt = (fmt_data *) clp->data;
if (cfmt->visible) {
num_visible_col++;
last_visible_col = i;
}
}
/* if num_visible_col is 0, we are done */
if (num_visible_col == 0) {
g_free(callback_args.header_line_buf);
return CF_PRINT_OK;
}
/* Find the widths for each of the columns - maximum of the
width of the title and the width of the data - and construct
a buffer with a line containing the column titles. */
callback_args.num_visible_cols = num_visible_col;
callback_args.col_widths = g_new(gint, num_visible_col);
callback_args.visible_cols = g_new(gint, num_visible_col);
cp = &callback_args.header_line_buf[0];
line_len = 0;
visible_col_count = 0;
for (i = 0; i < cf->cinfo.num_cols; i++) {
clp = g_list_nth(prefs.col_list, i);
if (clp == NULL) /* Sanity check, Invalid column requested */
continue;
cfmt = (fmt_data *) clp->data;
if (cfmt->visible == FALSE)
continue;
/* Save the order of visible columns */
callback_args.visible_cols[visible_col_count] = i;
/* Don't pad the last column. */
if (i == last_visible_col)
callback_args.col_widths[visible_col_count] = 0;
else {
callback_args.col_widths[visible_col_count] = (gint) strlen(cf->cinfo.columns[i].col_title);
data_width = get_column_char_width(get_column_format(i));
if (data_width > callback_args.col_widths[visible_col_count])
callback_args.col_widths[visible_col_count] = data_width;
}
/* Find the length of the string for this column. */
column_len = (int) strlen(cf->cinfo.columns[i].col_title);
if (callback_args.col_widths[visible_col_count] > column_len)
column_len = callback_args.col_widths[visible_col_count];
/* Make sure there's room in the line buffer for the column; if not,
double its length. */
line_len += column_len + 1; /* "+1" for space */
if (line_len > callback_args.header_line_buf_len) {
cp_off = (int) (cp - callback_args.header_line_buf);
callback_args.header_line_buf_len = 2 * line_len;
callback_args.header_line_buf = (char *)g_realloc(callback_args.header_line_buf,
callback_args.header_line_buf_len + 1);
cp = callback_args.header_line_buf + cp_off;
}
/* Right-justify the packet number column. */
/* if (cf->cinfo.col_fmt[i] == COL_NUMBER)
snprintf(cp, column_len+1, "%*s", callback_args.col_widths[visible_col_count], cf->cinfo.columns[i].col_title);
else*/
snprintf(cp, column_len+1, "%-*s", callback_args.col_widths[visible_col_count], cf->cinfo.columns[i].col_title);
cp += column_len;
if (i != cf->cinfo.num_cols - 1)
*cp++ = ' ';
visible_col_count++;
}
*cp = '\0';
/* Now start out the main line buffer with the same length as the
header line buffer. */
callback_args.line_buf_len = callback_args.header_line_buf_len;
callback_args.line_buf = (char *)g_malloc(callback_args.line_buf_len + 1);
} /* if (print_summary) */
/* Create the protocol tree, and make it visible, if we're printing
the dissection or the hex data.
XXX - do we need it if we're just printing the hex data? */
proto_tree_needed =
callback_args.print_args->print_dissections != print_dissections_none ||
callback_args.print_args->print_hex ||
have_custom_cols(&cf->cinfo) || have_field_extractors();
epan_dissect_init(&callback_args.edt, cf->epan, proto_tree_needed, proto_tree_needed);
/* Iterate through the list of packets, printing the packets we were
told to print. */
ret = process_specified_records(cf, &print_args->range, "Printing",
"selected packets", TRUE, print_packet,
&callback_args, show_progress_bar);
epan_dissect_cleanup(&callback_args.edt);
g_free(callback_args.header_line_buf);
g_free(callback_args.line_buf);
g_free(callback_args.col_widths);
g_free(callback_args.visible_cols);
switch (ret) {
case PSP_FINISHED:
/* Completed successfully. */
break;
case PSP_STOPPED:
/* Well, the user decided to abort the printing.
XXX - note that what got generated before they did that
will get printed if we're piping to a print program; we'd
have to write to a file and then hand that to the print
program to make it actually not print anything. */
break;
case PSP_FAILED:
/* Error while printing.
XXX - note that what got generated before they did that
will get printed if we're piping to a print program; we'd
have to write to a file and then hand that to the print
program to make it actually not print anything. */
destroy_print_stream(print_args->stream);
return CF_PRINT_WRITE_ERROR;
}
if (!print_finale(print_args->stream)) {
destroy_print_stream(print_args->stream);
return CF_PRINT_WRITE_ERROR;
}
if (!destroy_print_stream(print_args->stream))
return CF_PRINT_WRITE_ERROR;
return CF_PRINT_OK;
}
typedef struct {
FILE *fh;
epan_dissect_t edt;
print_args_t *print_args;
json_dumper jdumper;
} write_packet_callback_args_t;
static gboolean
write_pdml_packet(capture_file *cf, frame_data *fdata, wtap_rec *rec,
Buffer *buf, void *argsp)
{
write_packet_callback_args_t *args = (write_packet_callback_args_t *)argsp;
/* Create the protocol tree, but don't fill in the column information. */
epan_dissect_run(&args->edt, cf->cd_t, rec,
frame_tvbuff_new_buffer(&cf->provider, fdata, buf),
fdata, NULL);
/* Write out the information in that tree. */
write_pdml_proto_tree(NULL, &args->edt, &cf->cinfo, args->fh, FALSE);
epan_dissect_reset(&args->edt);
return !ferror(args->fh);
}
cf_print_status_t
cf_write_pdml_packets(capture_file *cf, print_args_t *print_args)
{
write_packet_callback_args_t callback_args;
FILE *fh;
psp_return_t ret;
fh = ws_fopen(print_args->file, "w");
if (fh == NULL)
return CF_PRINT_OPEN_ERROR; /* attempt to open destination failed */
write_pdml_preamble(fh, cf->filename);
if (ferror(fh)) {
fclose(fh);
return CF_PRINT_WRITE_ERROR;
}
callback_args.fh = fh;
callback_args.print_args = print_args;
epan_dissect_init(&callback_args.edt, cf->epan, TRUE, TRUE);
/* Iterate through the list of packets, printing the packets we were
told to print. */
ret = process_specified_records(cf, &print_args->range, "Writing PDML",
"selected packets", TRUE,
write_pdml_packet, &callback_args, TRUE);
epan_dissect_cleanup(&callback_args.edt);
switch (ret) {
case PSP_FINISHED:
/* Completed successfully. */
break;
case PSP_STOPPED:
/* Well, the user decided to abort the printing. */
break;
case PSP_FAILED:
/* Error while printing. */
fclose(fh);
return CF_PRINT_WRITE_ERROR;
}
write_pdml_finale(fh);
if (ferror(fh)) {
fclose(fh);
return CF_PRINT_WRITE_ERROR;
}
/* XXX - check for an error */
fclose(fh);
return CF_PRINT_OK;
}
static gboolean
write_psml_packet(capture_file *cf, frame_data *fdata, wtap_rec *rec,
Buffer *buf, void *argsp)
{
write_packet_callback_args_t *args = (write_packet_callback_args_t *)argsp;
/* Fill in the column information */
col_custom_prime_edt(&args->edt, &cf->cinfo);
epan_dissect_run(&args->edt, cf->cd_t, rec,
frame_tvbuff_new_buffer(&cf->provider, fdata, buf),
fdata, &cf->cinfo);
epan_dissect_fill_in_columns(&args->edt, FALSE, TRUE);
/* Write out the column information. */
write_psml_columns(&args->edt, args->fh, FALSE);
epan_dissect_reset(&args->edt);
return !ferror(args->fh);
}
cf_print_status_t
cf_write_psml_packets(capture_file *cf, print_args_t *print_args)
{
write_packet_callback_args_t callback_args;
FILE *fh;
psp_return_t ret;
gboolean proto_tree_needed;
fh = ws_fopen(print_args->file, "w");
if (fh == NULL)
return CF_PRINT_OPEN_ERROR; /* attempt to open destination failed */
write_psml_preamble(&cf->cinfo, fh);
if (ferror(fh)) {
fclose(fh);
return CF_PRINT_WRITE_ERROR;
}
callback_args.fh = fh;
callback_args.print_args = print_args;
/* Fill in the column information, only create the protocol tree
if having custom columns or field extractors. */
proto_tree_needed = have_custom_cols(&cf->cinfo) || have_field_extractors();
epan_dissect_init(&callback_args.edt, cf->epan, proto_tree_needed, proto_tree_needed);
/* Iterate through the list of packets, printing the packets we were
told to print. */
ret = process_specified_records(cf, &print_args->range, "Writing PSML",
"selected packets", TRUE,
write_psml_packet, &callback_args, TRUE);
epan_dissect_cleanup(&callback_args.edt);
switch (ret) {
case PSP_FINISHED:
/* Completed successfully. */
break;
case PSP_STOPPED:
/* Well, the user decided to abort the printing. */
break;
case PSP_FAILED:
/* Error while printing. */
fclose(fh);
return CF_PRINT_WRITE_ERROR;
}
write_psml_finale(fh);
if (ferror(fh)) {
fclose(fh);
return CF_PRINT_WRITE_ERROR;
}
/* XXX - check for an error */
fclose(fh);
return CF_PRINT_OK;
}
static gboolean
write_csv_packet(capture_file *cf, frame_data *fdata, wtap_rec *rec,
Buffer *buf, void *argsp)
{
write_packet_callback_args_t *args = (write_packet_callback_args_t *)argsp;
/* Fill in the column information */
col_custom_prime_edt(&args->edt, &cf->cinfo);
epan_dissect_run(&args->edt, cf->cd_t, rec,
frame_tvbuff_new_buffer(&cf->provider, fdata, buf),
fdata, &cf->cinfo);
epan_dissect_fill_in_columns(&args->edt, FALSE, TRUE);
/* Write out the column information. */
write_csv_columns(&args->edt, args->fh);
epan_dissect_reset(&args->edt);
return !ferror(args->fh);
}
cf_print_status_t
cf_write_csv_packets(capture_file *cf, print_args_t *print_args)
{
write_packet_callback_args_t callback_args;
gboolean proto_tree_needed;
FILE *fh;
psp_return_t ret;
fh = ws_fopen(print_args->file, "w");
if (fh == NULL)
return CF_PRINT_OPEN_ERROR; /* attempt to open destination failed */
write_csv_column_titles(&cf->cinfo, fh);
if (ferror(fh)) {
fclose(fh);
return CF_PRINT_WRITE_ERROR;
}
callback_args.fh = fh;
callback_args.print_args = print_args;
/* only create the protocol tree if having custom columns or field extractors. */
proto_tree_needed = have_custom_cols(&cf->cinfo) || have_field_extractors();
epan_dissect_init(&callback_args.edt, cf->epan, proto_tree_needed, proto_tree_needed);
/* Iterate through the list of packets, printing the packets we were
told to print. */
ret = process_specified_records(cf, &print_args->range, "Writing CSV",
"selected packets", TRUE,
write_csv_packet, &callback_args, TRUE);
epan_dissect_cleanup(&callback_args.edt);
switch (ret) {
case PSP_FINISHED:
/* Completed successfully. */
break;
case PSP_STOPPED:
/* Well, the user decided to abort the printing. */
break;
case PSP_FAILED:
/* Error while printing. */
fclose(fh);
return CF_PRINT_WRITE_ERROR;
}
/* XXX - check for an error */
fclose(fh);
return CF_PRINT_OK;
}
static gboolean
carrays_write_packet(capture_file *cf, frame_data *fdata, wtap_rec *rec,
Buffer *buf, void *argsp)
{
write_packet_callback_args_t *args = (write_packet_callback_args_t *)argsp;
epan_dissect_run(&args->edt, cf->cd_t, rec,
frame_tvbuff_new_buffer(&cf->provider, fdata, buf),
fdata, NULL);
write_carrays_hex_data(fdata->num, args->fh, &args->edt);
epan_dissect_reset(&args->edt);
return !ferror(args->fh);
}
cf_print_status_t
cf_write_carrays_packets(capture_file *cf, print_args_t *print_args)
{
write_packet_callback_args_t callback_args;
FILE *fh;
psp_return_t ret;
fh = ws_fopen(print_args->file, "w");
if (fh == NULL)
return CF_PRINT_OPEN_ERROR; /* attempt to open destination failed */
if (ferror(fh)) {
fclose(fh);
return CF_PRINT_WRITE_ERROR;
}
callback_args.fh = fh;
callback_args.print_args = print_args;
epan_dissect_init(&callback_args.edt, cf->epan, TRUE, TRUE);
/* Iterate through the list of packets, printing the packets we were
told to print. */
ret = process_specified_records(cf, &print_args->range,
"Writing C Arrays",
"selected packets", TRUE,
carrays_write_packet, &callback_args, TRUE);
epan_dissect_cleanup(&callback_args.edt);
switch (ret) {
case PSP_FINISHED:
/* Completed successfully. */
break;
case PSP_STOPPED:
/* Well, the user decided to abort the printing. */
break;
case PSP_FAILED:
/* Error while printing. */
fclose(fh);
return CF_PRINT_WRITE_ERROR;
}
fclose(fh);
return CF_PRINT_OK;
}
static gboolean
write_json_packet(capture_file *cf, frame_data *fdata, wtap_rec *rec,
Buffer *buf, void *argsp)
{
write_packet_callback_args_t *args = (write_packet_callback_args_t *)argsp;
/* Create the protocol tree, but don't fill in the column information. */
epan_dissect_run(&args->edt, cf->cd_t, rec,
frame_tvbuff_new_buffer(&cf->provider, fdata, buf),
fdata, NULL);
/* Write out the information in that tree. */
write_json_proto_tree(NULL, args->print_args->print_dissections,
args->print_args->print_hex,
&args->edt, &cf->cinfo, proto_node_group_children_by_unique,
&args->jdumper);
epan_dissect_reset(&args->edt);
return !ferror(args->fh);
}
cf_print_status_t
cf_write_json_packets(capture_file *cf, print_args_t *print_args)
{
write_packet_callback_args_t callback_args;
FILE *fh;
psp_return_t ret;
fh = ws_fopen(print_args->file, "w");
if (fh == NULL)
return CF_PRINT_OPEN_ERROR; /* attempt to open destination failed */
callback_args.jdumper = write_json_preamble(fh);
if (ferror(fh)) {
fclose(fh);
return CF_PRINT_WRITE_ERROR;
}
callback_args.fh = fh;
callback_args.print_args = print_args;
epan_dissect_init(&callback_args.edt, cf->epan, TRUE, TRUE);
/* Iterate through the list of packets, printing the packets we were
told to print. */
ret = process_specified_records(cf, &print_args->range, "Writing JSON",
"selected packets", TRUE,
write_json_packet, &callback_args, TRUE);
epan_dissect_cleanup(&callback_args.edt);
switch (ret) {
case PSP_FINISHED:
/* Completed successfully. */
break;
case PSP_STOPPED:
/* Well, the user decided to abort the printing. */
break;
case PSP_FAILED:
/* Error while printing. */
fclose(fh);
return CF_PRINT_WRITE_ERROR;
}
write_json_finale(&callback_args.jdumper);
if (ferror(fh)) {
fclose(fh);
return CF_PRINT_WRITE_ERROR;
}
/* XXX - check for an error */
fclose(fh);
return CF_PRINT_OK;
}
gboolean
cf_find_packet_protocol_tree(capture_file *cf, const char *string,
search_direction dir)
{
match_data mdata;
mdata.string = string;
mdata.string_len = strlen(string);
return find_packet(cf, match_protocol_tree, &mdata, dir);
}
field_info*
cf_find_string_protocol_tree(capture_file *cf, proto_tree *tree)
{
match_data mdata;
mdata.frame_matched = FALSE;
mdata.string = convert_string_case(cf->sfilter, cf->case_type);
mdata.string_len = strlen(mdata.string);
mdata.cf = cf;
/* Iterate through all the nodes looking for matching text */
proto_tree_children_foreach(tree, match_subtree_text, &mdata);
g_free((char *)mdata.string);
return mdata.frame_matched ? mdata.finfo : NULL;
}
static match_result
match_protocol_tree(capture_file *cf, frame_data *fdata,
wtap_rec *rec, Buffer *buf, void *criterion)
{
match_data *mdata = (match_data *)criterion;
epan_dissect_t edt;
/* Load the frame's data. */
if (!cf_read_record(cf, fdata, rec, buf)) {
/* Attempt to get the packet failed. */
return MR_ERROR;
}
/* Construct the protocol tree, including the displayed text */
epan_dissect_init(&edt, cf->epan, TRUE, TRUE);
/* We don't need the column information */
epan_dissect_run(&edt, cf->cd_t, rec,
frame_tvbuff_new_buffer(&cf->provider, fdata, buf),
fdata, NULL);
/* Iterate through all the nodes, seeing if they have text that matches. */
mdata->cf = cf;
mdata->frame_matched = FALSE;
proto_tree_children_foreach(edt.tree, match_subtree_text, mdata);
epan_dissect_cleanup(&edt);
return mdata->frame_matched ? MR_MATCHED : MR_NOTMATCHED;
}
static void
match_subtree_text(proto_node *node, gpointer data)
{
match_data *mdata = (match_data *) data;
const gchar *string = mdata->string;
size_t string_len = mdata->string_len;
capture_file *cf = mdata->cf;
field_info *fi = PNODE_FINFO(node);
gchar label_str[ITEM_LABEL_LENGTH];
gchar *label_ptr;
size_t label_len;
guint32 i, i_restart;
guint8 c_char;
size_t c_match = 0;
/* dissection with an invisible proto tree? */
ws_assert(fi);
if (mdata->frame_matched) {
/* We already had a match; don't bother doing any more work. */
return;
}
/* Don't match invisible entries. */
if (proto_item_is_hidden(node))
return;
/* was a free format label produced? */
if (fi->rep) {
label_ptr = fi->rep->representation;
} else {
/* no, make a generic label */
label_ptr = label_str;
proto_item_fill_label(fi, label_str);
}
if (cf->regex) {
if (ws_regex_matches(cf->regex, label_ptr)) {
mdata->frame_matched = TRUE;
mdata->finfo = fi;
return;
}
} else if (cf->case_type) {
/* Case insensitive match */
label_len = strlen(label_ptr);
i_restart = 0;
for (i = 0; i < label_len; i++) {
if (i_restart == 0 && c_match == 0 && (label_len - i < string_len))
break;
c_char = label_ptr[i];
c_char = g_ascii_toupper(c_char);
/* If c_match is non-zero, save candidate for retrying full match. */
if (c_match > 0 && i_restart == 0 && c_char == string[0])
i_restart = i;
if (c_char == string[c_match]) {
c_match++;
if (c_match == string_len) {
/* No need to look further; we have a match */
mdata->frame_matched = TRUE;
mdata->finfo = fi;
return;
}
} else if (i_restart) {
i = i_restart;
c_match = 1;
i_restart = 0;
} else
c_match = 0;
}
} else if (strstr(label_ptr, string) != NULL) {
/* Case sensitive match */
mdata->frame_matched = TRUE;
mdata->finfo = fi;
return;
}
/* Recurse into the subtree, if it exists */
if (node->first_child != NULL)
proto_tree_children_foreach(node, match_subtree_text, mdata);
}
gboolean
cf_find_packet_summary_line(capture_file *cf, const char *string,
search_direction dir)
{
match_data mdata;
mdata.string = string;
mdata.string_len = strlen(string);
return find_packet(cf, match_summary_line, &mdata, dir);
}
static match_result
match_summary_line(capture_file *cf, frame_data *fdata,
wtap_rec *rec, Buffer *buf, void *criterion)
{
match_data *mdata = (match_data *)criterion;
const gchar *string = mdata->string;
size_t string_len = mdata->string_len;
epan_dissect_t edt;
const char *info_column;
size_t info_column_len;
match_result result = MR_NOTMATCHED;
gint colx;
guint32 i, i_restart;
guint8 c_char;
size_t c_match = 0;
/* Load the frame's data. */
if (!cf_read_record(cf, fdata, rec, buf)) {
/* Attempt to get the packet failed. */
return MR_ERROR;
}
/* Don't bother constructing the protocol tree */
epan_dissect_init(&edt, cf->epan, FALSE, FALSE);
/* Get the column information */
epan_dissect_run(&edt, cf->cd_t, rec,
frame_tvbuff_new_buffer(&cf->provider, fdata, buf),
fdata, &cf->cinfo);
/* Find the Info column */
for (colx = 0; colx < cf->cinfo.num_cols; colx++) {
if (cf->cinfo.columns[colx].fmt_matx[COL_INFO]) {
/* Found it. See if we match. */
info_column = get_column_text(edt.pi.cinfo, colx);
info_column_len = strlen(info_column);
if (cf->regex) {
if (ws_regex_matches(cf->regex, info_column)) {
result = MR_MATCHED;
break;
}
} else if (cf->case_type) {
/* Case insensitive match */
i_restart = 0;
for (i = 0; i < info_column_len; i++) {
if (i_restart == 0 && c_match == 0 && (info_column_len - i < string_len))
break;
c_char = info_column[i];
c_char = g_ascii_toupper(c_char);
/* If c_match is non-zero, save candidate for retrying full match. */
if (c_match > 0 && i_restart == 0 && c_char == string[0])
i_restart = i;
if (c_char == string[c_match]) {
c_match++;
if (c_match == string_len) {
result = MR_MATCHED;
break;
}
} else if (i_restart) {
i = i_restart;
c_match = 1;
i_restart = 0;
} else
c_match = 0;
}
} else if (strstr(info_column, string) != NULL) {
/* Case sensitive match */
result = MR_MATCHED;
}
break;
}
}
epan_dissect_cleanup(&edt);
return result;
}
typedef struct {
const guint8 *data;
size_t data_len;
ws_mempbrk_pattern *pattern;
} cbs_t; /* "Counted byte string" */
/*
* The current match_* routines only support ASCII case insensitivity and don't
* convert UTF-8 inputs to UTF-16 for matching. The UTF-16 support just
* interleaves with \0 bytes, which works for 7 bit ASCII.
*
* We could modify them to use the GLib Unicode routines or the International
* Components for Unicode library but it's not apparent that we could do so
* without consuming a lot more CPU and memory or that searching would be
* significantly better.
*
* XXX: We could test the search string to see if it's all ASCII, and if not
* use Unicode aware routines for case insensitive searches or any UTF-16
* search.
*/
gboolean
cf_find_packet_data(capture_file *cf, const guint8 *string, size_t string_size,
search_direction dir)
{
cbs_t info;
guint8 needles[3];
ws_mempbrk_pattern pattern;
info.data = string;
info.data_len = string_size;
/* Regex, String or hex search? */
if (cf->regex) {
/* Regular Expression search */
return find_packet(cf, match_regex, NULL, dir);
} else if (cf->string) {
/* String search - what type of string? */
if (cf->case_type) {
needles[0] = string[0];
needles[1] = g_ascii_tolower(needles[0]);
needles[2] = '\0';
ws_mempbrk_compile(&pattern, needles);
info.pattern = &pattern;
switch (cf->scs_type) {
case SCS_NARROW_AND_WIDE:
return find_packet(cf, match_narrow_and_wide_case, &info, dir);
case SCS_NARROW:
return find_packet(cf, match_narrow_case, &info, dir);
case SCS_WIDE:
return find_packet(cf, match_wide_case, &info, dir);
default:
ws_assert_not_reached();
return FALSE;
}
} else {
switch (cf->scs_type) {
case SCS_NARROW_AND_WIDE:
return find_packet(cf, match_narrow_and_wide, &info, dir);
case SCS_NARROW:
return find_packet(cf, match_narrow, &info, dir);
case SCS_WIDE:
return find_packet(cf, match_wide, &info, dir);
default:
ws_assert_not_reached();
return FALSE;
}
}
} else
return find_packet(cf, match_binary, &info, dir);
}
static match_result
match_narrow_and_wide(capture_file *cf, frame_data *fdata,
wtap_rec *rec, Buffer *buf, void *criterion)
{
cbs_t *info = (cbs_t *)criterion;
const guint8 *ascii_text = info->data;
size_t textlen = info->data_len;
match_result result;
guint32 buf_len;
guint8 *pd, *buf_start, *buf_end;
guint32 i;
guint8 c_char;
size_t c_match = 0;
/* Load the frame's data. */
if (!cf_read_record(cf, fdata, rec, buf)) {
/* Attempt to get the packet failed. */
return MR_ERROR;
}
result = MR_NOTMATCHED;
buf_len = fdata->cap_len;
buf_start = ws_buffer_start_ptr(buf);
buf_end = buf_start + buf_len;
for (pd = buf_start; pd < buf_end; pd++) {
pd = (guint8 *)memchr(pd, ascii_text[0], buf_end - pd);
if (pd == NULL) break;
/* Try narrow match at this start location */
c_match = 0;
for (i = 0; pd + i < buf_end; i++) {
c_char = pd[i];
if (c_char == ascii_text[c_match]) {
c_match++;
if (c_match == textlen) {
result = MR_MATCHED;
cf->search_pos = i + (guint32)(pd - buf_start);
/* Save the position of the last character
for highlighting the field. */
cf->search_len = i + 1;
goto done;
}
} else {
break;
}
}
/* Now try wide match at the same start location. */
c_match = 0;
for (i = 0; pd + i < buf_end; i++) {
c_char = pd[i];
if (c_char == ascii_text[c_match]) {
c_match++;
if (c_match == textlen) {
result = MR_MATCHED;
cf->search_pos = i + (guint32)(pd - buf_start);
/* Save the position of the last character
for highlighting the field. */
cf->search_len = i + 1;
goto done;
}
i++;
if (pd + i >= buf_end || pd[i] != '\0') break;
} else {
break;
}
}
}
done:
return result;
}
/* Case insensitive match */
static match_result
match_narrow_and_wide_case(capture_file *cf, frame_data *fdata,
wtap_rec *rec, Buffer *buf, void *criterion)
{
cbs_t *info = (cbs_t *)criterion;
const guint8 *ascii_text = info->data;
size_t textlen = info->data_len;
ws_mempbrk_pattern *pattern = info->pattern;
match_result result;
guint32 buf_len;
guint8 *pd, *buf_start, *buf_end;
guint32 i;
guint8 c_char;
size_t c_match = 0;
/* Load the frame's data. */
if (!cf_read_record(cf, fdata, rec, buf)) {
/* Attempt to get the packet failed. */
return MR_ERROR;
}
ws_assert(pattern != NULL);
result = MR_NOTMATCHED;
buf_len = fdata->cap_len;
buf_start = ws_buffer_start_ptr(buf);
buf_end = buf_start + buf_len;
for (pd = buf_start; pd < buf_end; pd++) {
pd = (guint8 *)ws_mempbrk_exec(pd, buf_end - pd, pattern, &c_char);
if (pd == NULL) break;
/* Try narrow match at this start location */
c_match = 0;
for (i = 0; pd + i < buf_end; i++) {
c_char = g_ascii_toupper(pd[i]);
if (c_char == ascii_text[c_match]) {
c_match++;
if (c_match == textlen) {
result = MR_MATCHED;
cf->search_pos = i + (guint32)(pd - buf_start);
/* Save the position of the last character
for highlighting the field. */
cf->search_len = i + 1;
goto done;
}
} else {
break;
}
}
/* Now try wide match at the same start location. */
c_match = 0;
for (i = 0; pd + i < buf_end; i++) {
c_char = g_ascii_toupper(pd[i]);
if (c_char == ascii_text[c_match]) {
c_match++;
if (c_match == textlen) {
result = MR_MATCHED;
cf->search_pos = i + (guint32)(pd - buf_start);
/* Save the position of the last character
for highlighting the field. */
cf->search_len = i + 1;
goto done;
}
i++;
if (pd + i >= buf_end || pd[i] != '\0') break;
} else {
break;
}
}
}
done:
return result;
}
static match_result
match_narrow(capture_file *cf, frame_data *fdata,
wtap_rec *rec, Buffer *buf, void *criterion)
{
cbs_t *info = (cbs_t *)criterion;
const guint8 *ascii_text = info->data;
size_t textlen = info->data_len;
match_result result;
guint32 buf_len;
guint8 *pd, *buf_start, *buf_end;
guint32 i;
guint8 c_char;
size_t c_match = 0;
/* Load the frame's data. */
if (!cf_read_record(cf, fdata, rec, buf)) {
/* Attempt to get the packet failed. */
return MR_ERROR;
}
result = MR_NOTMATCHED;
buf_len = fdata->cap_len;
buf_start = ws_buffer_start_ptr(buf);
buf_end = buf_start + buf_len;
for (pd = buf_start; pd < buf_end; pd++) {
pd = (guint8 *)memchr(pd, ascii_text[0], buf_end - pd);
if (pd == NULL) break;
c_match = 0;
for (i = 0; pd + i < buf_end; i++) {
c_char = pd[i];
if (c_char == ascii_text[c_match]) {
c_match++;
if (c_match == textlen) {
result = MR_MATCHED;
cf->search_pos = i + (guint32)(pd - buf_start);
/* Save the position of the last character
for highlighting the field. */
cf->search_len = i + 1;
goto done;
}
} else {
break;
}
}
}
done:
return result;
}
/* Case insensitive match */
static match_result
match_narrow_case(capture_file *cf, frame_data *fdata,
wtap_rec *rec, Buffer *buf, void *criterion)
{
cbs_t *info = (cbs_t *)criterion;
const guint8 *ascii_text = info->data;
size_t textlen = info->data_len;
ws_mempbrk_pattern *pattern = info->pattern;
match_result result;
guint32 buf_len;
guint8 *pd, *buf_start, *buf_end;
guint32 i;
guint8 c_char;
size_t c_match = 0;
/* Load the frame's data. */
if (!cf_read_record(cf, fdata, rec, buf)) {
/* Attempt to get the packet failed. */
return MR_ERROR;
}
ws_assert(pattern != NULL);
result = MR_NOTMATCHED;
buf_len = fdata->cap_len;
buf_start = ws_buffer_start_ptr(buf);
buf_end = buf_start + buf_len;
for (pd = buf_start; pd < buf_end; pd++) {
pd = (guint8 *)ws_mempbrk_exec(pd, buf_end - pd, pattern, &c_char);
if (pd == NULL) break;
c_match = 0;
for (i = 0; pd + i < buf_end; i++) {
c_char = g_ascii_toupper(pd[i]);
if (c_char == ascii_text[c_match]) {
c_match++;
if (c_match == textlen) {
result = MR_MATCHED;
cf->search_pos = i + (guint32)(pd - buf_start);
/* Save the position of the last character
for highlighting the field. */
cf->search_len = i + 1;
goto done;
}
} else {
break;
}
}
}
done:
return result;
}
static match_result
match_wide(capture_file *cf, frame_data *fdata,
wtap_rec *rec, Buffer *buf, void *criterion)
{
cbs_t *info = (cbs_t *)criterion;
const guint8 *ascii_text = info->data;
size_t textlen = info->data_len;
match_result result;
guint32 buf_len;
guint8 *pd, *buf_start, *buf_end;
guint32 i;
guint8 c_char;
size_t c_match = 0;
/* Load the frame's data. */
if (!cf_read_record(cf, fdata, rec, buf)) {
/* Attempt to get the packet failed. */
return MR_ERROR;
}
result = MR_NOTMATCHED;
buf_len = fdata->cap_len;
buf_start = ws_buffer_start_ptr(buf);
buf_end = buf_start + buf_len;
for (pd = buf_start; pd < buf_end; pd++) {
pd = (guint8 *)memchr(pd, ascii_text[0], buf_end - pd);
if (pd == NULL) break;
c_match = 0;
for (i = 0; pd + i < buf_end; i++) {
c_char = pd[i];
if (c_char == ascii_text[c_match]) {
c_match++;
if (c_match == textlen) {
result = MR_MATCHED;
cf->search_pos = i + (guint32)(pd - buf_start);
/* Save the position of the last character
for highlighting the field. */
cf->search_len = i + 1;
goto done;
}
i++;
if (pd + i >= buf_end || pd[i] != '\0') break;
} else {
break;
}
}
}
done:
return result;
}
/* Case insensitive match */
static match_result
match_wide_case(capture_file *cf, frame_data *fdata,
wtap_rec *rec, Buffer *buf, void *criterion)
{
cbs_t *info = (cbs_t *)criterion;
const guint8 *ascii_text = info->data;
size_t textlen = info->data_len;
ws_mempbrk_pattern *pattern = info->pattern;
match_result result;
guint32 buf_len;
guint8 *pd, *buf_start, *buf_end;
guint32 i;
guint8 c_char;
size_t c_match = 0;
/* Load the frame's data. */
if (!cf_read_record(cf, fdata, rec, buf)) {
/* Attempt to get the packet failed. */
return MR_ERROR;
}
ws_assert(pattern != NULL);
result = MR_NOTMATCHED;
buf_len = fdata->cap_len;
buf_start = ws_buffer_start_ptr(buf);
buf_end = buf_start + buf_len;
for (pd = buf_start; pd < buf_end; pd++) {
pd = (guint8 *)ws_mempbrk_exec(pd, buf_end - pd, pattern, &c_char);
if (pd == NULL) break;
c_match = 0;
for (i = 0; pd + i < buf_end; i++) {
c_char = g_ascii_toupper(pd[i]);
if (c_char == ascii_text[c_match]) {
c_match++;
if (c_match == textlen) {
result = MR_MATCHED;
cf->search_pos = i + (guint32)(pd - buf_start);
/* Save the position of the last character
for highlighting the field. */
cf->search_len = i + 1;
goto done;
}
i++;
if (pd + i >= buf_end || pd[i] != '\0') break;
} else {
break;
}
}
}
done:
return result;
}
static match_result
match_binary(capture_file *cf, frame_data *fdata,
wtap_rec *rec, Buffer *buf, void *criterion)
{
cbs_t *info = (cbs_t *)criterion;
const guint8 *binary_data = info->data;
size_t datalen = info->data_len;
match_result result;
guint32 buf_len;
guint8 *pd, *buf_start, *buf_end;
guint32 i;
size_t c_match = 0;
/* Load the frame's data. */
if (!cf_read_record(cf, fdata, rec, buf)) {
/* Attempt to get the packet failed. */
return MR_ERROR;
}
result = MR_NOTMATCHED;
buf_len = fdata->cap_len;
buf_start = ws_buffer_start_ptr(buf);
buf_end = buf_start + buf_len;
/* Not clear if using memcmp() is faster. memmem() on systems that
* have it should be faster, though.
*/
for (pd = buf_start; pd < buf_end; pd++) {
pd = (guint8 *)memchr(pd, binary_data[0], buf_end - pd);
if (pd == NULL) break;
c_match = 0;
for (i = 0; pd + i < buf_end; i++) {
if (pd[i] == binary_data[c_match]) {
c_match++;
if (c_match == datalen) {
result = MR_MATCHED;
cf->search_pos = i + (guint32)(pd - buf_start);
/* Save the position of the last character
for highlighting the field. */
cf->search_len = i + 1;
goto done;
}
} else {
break;
}
}
}
done:
return result;
}
static match_result
match_regex(capture_file *cf, frame_data *fdata,
wtap_rec *rec, Buffer *buf, void *criterion _U_)
{
match_result result = MR_NOTMATCHED;
size_t result_pos[2] = {0, 0};
/* Load the frame's data. */
if (!cf_read_record(cf, fdata, rec, buf)) {
/* Attempt to get the packet failed. */
return MR_ERROR;
}
if (ws_regex_matches_pos(cf->regex,
(const gchar *)ws_buffer_start_ptr(buf),
fdata->cap_len,
result_pos)) {
//TODO: Fix cast.
cf->search_pos = (guint32)(result_pos[1] - 1); /* last byte = end position - 1 */
cf->search_len = (guint32)(result_pos[1] - result_pos[0]);
result = MR_MATCHED;
}
return result;
}
gboolean
cf_find_packet_dfilter(capture_file *cf, dfilter_t *sfcode,
search_direction dir)
{
return find_packet(cf, match_dfilter, sfcode, dir);
}
gboolean
cf_find_packet_dfilter_string(capture_file *cf, const char *filter,
search_direction dir)
{
dfilter_t *sfcode;
gboolean result;
if (!dfilter_compile(filter, &sfcode, NULL)) {
/*
* XXX - this shouldn't happen, as the filter string is machine
* generated
*/
return FALSE;
}
if (sfcode == NULL) {
/*
* XXX - this shouldn't happen, as the filter string is machine
* generated.
*/
return FALSE;
}
result = find_packet(cf, match_dfilter, sfcode, dir);
dfilter_free(sfcode);
return result;
}
static match_result
match_dfilter(capture_file *cf, frame_data *fdata,
wtap_rec *rec, Buffer *buf, void *criterion)
{
dfilter_t *sfcode = (dfilter_t *)criterion;
epan_dissect_t edt;
match_result result;
/* Load the frame's data. */
if (!cf_read_record(cf, fdata, rec, buf)) {
/* Attempt to get the packet failed. */
return MR_ERROR;
}
epan_dissect_init(&edt, cf->epan, TRUE, FALSE);
epan_dissect_prime_with_dfilter(&edt, sfcode);
epan_dissect_run(&edt, cf->cd_t, rec,
frame_tvbuff_new_buffer(&cf->provider, fdata, buf),
fdata, NULL);
result = dfilter_apply_edt(sfcode, &edt) ? MR_MATCHED : MR_NOTMATCHED;
epan_dissect_cleanup(&edt);
return result;
}
gboolean
cf_find_packet_marked(capture_file *cf, search_direction dir)
{
return find_packet(cf, match_marked, NULL, dir);
}
static match_result
match_marked(capture_file *cf _U_, frame_data *fdata, wtap_rec *rec _U_,
Buffer *buf _U_, void *criterion _U_)
{
return fdata->marked ? MR_MATCHED : MR_NOTMATCHED;
}
gboolean
cf_find_packet_time_reference(capture_file *cf, search_direction dir)
{
return find_packet(cf, match_time_reference, NULL, dir);
}
static match_result
match_time_reference(capture_file *cf _U_, frame_data *fdata, wtap_rec *rec _U_,
Buffer *buf _U_, void *criterion _U_)
{
return fdata->ref_time ? MR_MATCHED : MR_NOTMATCHED;
}
static gboolean
find_packet(capture_file *cf, ws_match_function match_function,
void *criterion, search_direction dir)
{
frame_data *start_fd;
guint32 framenum;
guint32 prev_framenum;
frame_data *fdata;
wtap_rec rec;
Buffer buf;
frame_data *new_fd = NULL;
progdlg_t *progbar = NULL;
GTimer *prog_timer = g_timer_new();
int count;
gboolean succeeded;
float progbar_val;
gchar status_str[100];
match_result result;
wtap_rec_init(&rec);
ws_buffer_init(&buf, 1514);
start_fd = cf->current_frame;
if (start_fd != NULL) {
prev_framenum = start_fd->num;
} else {
prev_framenum = 0; /* No start packet selected. */
}
/* Iterate through the list of packets, starting at the packet we've
picked, calling a routine to run the filter on the packet, see if
it matches, and stop if so. */
count = 0;
framenum = prev_framenum;
g_timer_start(prog_timer);
/* Progress so far. */
progbar_val = 0.0f;
cf->stop_flag = FALSE;
for (;;) {
/* Create the progress bar if necessary.
We check on every iteration of the loop, so that it takes no
longer than the standard time to create it (otherwise, for a
large file, we might take considerably longer than that standard
time in order to get to the next progress bar step). */
if (progbar == NULL)
progbar = delayed_create_progress_dlg(cf->window, NULL, NULL,
FALSE, &cf->stop_flag, progbar_val);
/*
* Update the progress bar, but do it only after PROGBAR_UPDATE_INTERVAL
* has elapsed. Calling update_progress_dlg and packets_bar_update will
* likely trigger UI paint events, which might take a while depending on
* the platform and display. Reset our timer *after* painting.
*/
if (g_timer_elapsed(prog_timer, NULL) > PROGBAR_UPDATE_INTERVAL) {
/* let's not divide by zero. I should never be started
* with count == 0, so let's assert that
*/
ws_assert(cf->count > 0);
progbar_val = (gfloat) count / cf->count;
snprintf(status_str, sizeof(status_str),
"%4u of %u packets", count, cf->count);
update_progress_dlg(progbar, progbar_val, status_str);
g_timer_start(prog_timer);
}
if (cf->stop_flag) {
/* Well, the user decided to abort the search. Go back to the
frame where we started. */
new_fd = start_fd;
break;
}
/* Go past the current frame. */
if (dir == SD_BACKWARD) {
/* Go on to the previous frame. */
if (framenum <= 1) {
/*
* XXX - other apps have a bit more of a detailed message
* for this, and instead of offering "OK" and "Cancel",
* they offer things such as "Continue" and "Cancel";
* we need an API for popping up alert boxes with
* {Verb} and "Cancel".
*/
if (prefs.gui_find_wrap) {
statusbar_push_temporary_msg("Search reached the beginning. Continuing at end.");
framenum = cf->count; /* wrap around */
} else {
statusbar_push_temporary_msg("Search reached the beginning.");
framenum = prev_framenum; /* stay on previous packet */
}
} else
framenum--;
} else {
/* Go on to the next frame. */
if (framenum == cf->count) {
if (prefs.gui_find_wrap) {
statusbar_push_temporary_msg("Search reached the end. Continuing at beginning.");
framenum = 1; /* wrap around */
} else {
statusbar_push_temporary_msg("Search reached the end.");
framenum = prev_framenum; /* stay on previous packet */
}
} else
framenum++;
}
fdata = frame_data_sequence_find(cf->provider.frames, framenum);
count++;
/* Is this packet in the display? */
if (fdata && fdata->passed_dfilter) {
/* Yes. Does it match the search criterion? */
result = (*match_function)(cf, fdata, &rec, &buf, criterion);
if (result == MR_ERROR) {
/* Error; our caller has reported the error. Go back to the frame
where we started. */
new_fd = start_fd;
break;
} else if (result == MR_MATCHED) {
/* Yes. Go to the new frame. */
new_fd = fdata;
break;
}
wtap_rec_reset(&rec);
}
if (fdata == start_fd) {
/* We're back to the frame we were on originally, and that frame
doesn't match the search filter. The search failed. */
break;
}
}
/* We're done scanning the packets; destroy the progress bar if it
was created. */
if (progbar != NULL)
destroy_progress_dlg(progbar);
g_timer_destroy(prog_timer);
if (new_fd != NULL) {
/* We found a frame that's displayed and that matches.
Try to find and select the packet summary list row for that frame. */
gboolean found_row;
cf->search_in_progress = TRUE;
found_row = packet_list_select_row_from_data(new_fd);
cf->search_in_progress = FALSE;
cf->search_pos = 0; /* Reset the position */
cf->search_len = 0; /* Reset length */
if (!found_row) {
/* We didn't find a row corresponding to this frame.
This means that the frame isn't being displayed currently,
so we can't select it. */
simple_message_box(ESD_TYPE_INFO, NULL,
"The capture file is probably not fully dissected.",
"End of capture exceeded.");
succeeded = FALSE; /* The search succeeded but we didn't find the row */
} else
succeeded = TRUE; /* The search succeeded and we found the row */
} else
succeeded = FALSE; /* The search failed */
wtap_rec_cleanup(&rec);
ws_buffer_free(&buf);
return succeeded;
}
gboolean
cf_goto_frame(capture_file *cf, guint fnumber)
{
frame_data *fdata;
if (cf == NULL || cf->provider.frames == NULL) {
/* we don't have a loaded capture file - fix for bugs 11810 & 11989 */
statusbar_push_temporary_msg("There is no file loaded");
return FALSE; /* we failed to go to that packet */
}
fdata = frame_data_sequence_find(cf->provider.frames, fnumber);
if (fdata == NULL) {
/* we didn't find a packet with that packet number */
statusbar_push_temporary_msg("There is no packet number %u.", fnumber);
return FALSE; /* we failed to go to that packet */
}
if (!fdata->passed_dfilter) {
/* that packet currently isn't displayed */
/* XXX - add it to the set of displayed packets? */
statusbar_push_temporary_msg("Packet number %u isn't displayed.", fnumber);
return FALSE; /* we failed to go to that packet */
}
if (!packet_list_select_row_from_data(fdata)) {
/* We didn't find a row corresponding to this frame.
This means that the frame isn't being displayed currently,
so we can't select it. */
simple_message_box(ESD_TYPE_INFO, NULL,
"The capture file is probably not fully dissected.",
"End of capture exceeded.");
return FALSE;
}
return TRUE; /* we got to that packet */
}
/*
* Go to frame specified by currently selected protocol tree item.
*/
gboolean
cf_goto_framenum(capture_file *cf)
{
header_field_info *hfinfo;
guint32 framenum;
if (cf->finfo_selected) {
hfinfo = cf->finfo_selected->hfinfo;
ws_assert(hfinfo);
if (hfinfo->type == FT_FRAMENUM) {
framenum = fvalue_get_uinteger(cf->finfo_selected->value);
if (framenum != 0)
return cf_goto_frame(cf, framenum);
}
}
return FALSE;
}
/* Select the packet on a given row. */
void
cf_select_packet(capture_file *cf, frame_data *fdata)
{
epan_dissect_t *old_edt;
/* check the frame data struct pointer for this frame */
if (fdata == NULL) {
return;
}
/* Get the data in that frame. */
if (!cf_read_record(cf, fdata, &cf->rec, &cf->buf)) {
return;
}
/* Record that this frame is the current frame. */
cf->current_frame = fdata;
/*
* The change to defer freeing the current epan_dissect_t was in
* commit a2bb94c3b33d53f42534aceb7cc67aab1d1fb1f9; to quote
* that commit's comment:
*
* Clear GtkTreeStore before freeing edt
*
* When building current data for packet details treeview we store two
* things.
* - Generated string with item label
* - Pointer to node field_info structure
*
* After epan_dissect_{free, cleanup} pointer to field_info node is no
* longer valid so we should clear GtkTreeStore before freeing.
*
* XXX - we're no longer using GTK+; is there a way to ensure that
* *nothing* refers to any of the current frame information before
* we replace it?
*/
old_edt = cf->edt;
/* Create the logical protocol tree. */
/* We don't need the columns here. */
cf->edt = epan_dissect_new(cf->epan, TRUE, TRUE);
tap_build_interesting(cf->edt);
epan_dissect_run(cf->edt, cf->cd_t, &cf->rec,
frame_tvbuff_new_buffer(&cf->provider, cf->current_frame, &cf->buf),
cf->current_frame, NULL);
if (old_edt != NULL)
epan_dissect_free(old_edt);
}
/* Unselect the selected packet, if any. */
void
cf_unselect_packet(capture_file *cf)
{
epan_dissect_t *old_edt = cf->edt;
/*
* See the comment in cf_select_packet() about deferring the freeing
* of the old cf->edt.
*/
cf->edt = NULL;
/* No packet is selected. */
cf->current_frame = NULL;
/* Destroy the epan_dissect_t for the unselected packet. */
if (old_edt != NULL)
epan_dissect_free(old_edt);
}
/*
* Mark a particular frame.
*/
void
cf_mark_frame(capture_file *cf, frame_data *frame)
{
if (! frame->marked) {
frame->marked = TRUE;
if (cf->count > cf->marked_count)
cf->marked_count++;
}
}
/*
* Unmark a particular frame.
*/
void
cf_unmark_frame(capture_file *cf, frame_data *frame)
{
if (frame->marked) {
frame->marked = FALSE;
if (cf->marked_count > 0)
cf->marked_count--;
}
}
/*
* Ignore a particular frame.
*/
void
cf_ignore_frame(capture_file *cf, frame_data *frame)
{
if (! frame->ignored) {
frame->ignored = TRUE;
if (cf->count > cf->ignored_count)
cf->ignored_count++;
}
}
/*
* Un-ignore a particular frame.
*/
void
cf_unignore_frame(capture_file *cf, frame_data *frame)
{
if (frame->ignored) {
frame->ignored = FALSE;
if (cf->ignored_count > 0)
cf->ignored_count--;
}
}
/*
* Modify the section comment.
*/
void
cf_update_section_comment(capture_file *cf, gchar *comment)
{
wtap_block_t shb_inf;
gchar *shb_comment;
/* Get the first SHB. */
/* XXX - support multiple SHBs */
shb_inf = wtap_file_get_shb(cf->provider.wth, 0);
/* Get the first comment from the SHB. */
/* XXX - support multiple comments */
if (wtap_block_get_nth_string_option_value(shb_inf, OPT_COMMENT, 0, &shb_comment) != WTAP_OPTTYPE_SUCCESS) {
/* There's no comment - add one. */
wtap_block_add_string_option(shb_inf, OPT_COMMENT, comment, strlen(comment));
} else {
/* See if the comment has changed or not */
if (strcmp(shb_comment, comment) == 0) {
g_free(comment);
return;
}
/* The comment has changed, let's update it */
wtap_block_set_nth_string_option_value(shb_inf, OPT_COMMENT, 0, comment, strlen(comment));
}
/* Mark the file as having unsaved changes */
cf->unsaved_changes = TRUE;
}
/*
* Get the packet block for a packet (record).
* If the block has been edited, it returns the result of the edit,
* otherwise it returns the block from the file.
* NB. Caller must wtap_block_unref() the result when done.
*/
wtap_block_t
cf_get_packet_block(capture_file *cf, const frame_data *fd)
{
/* If this block has been modified, fetch the modified version */
if (fd->has_modified_block)
return wtap_block_ref(cap_file_provider_get_modified_block(&cf->provider, fd));
else {
wtap_rec rec; /* Record metadata */
Buffer buf; /* Record data */
wtap_block_t block;
/* fetch record block */
wtap_rec_init(&rec);
ws_buffer_init(&buf, 1514);
if (!cf_read_record(cf, fd, &rec, &buf))
{ /* XXX, what we can do here? */ }
/* rec.block is owned by the record, steal it before it is gone. */
block = wtap_block_ref(rec.block);
wtap_rec_cleanup(&rec);
ws_buffer_free(&buf);
return block;
}
}
/*
* Update(replace) the block on a capture from a frame
*/
gboolean
cf_set_modified_block(capture_file *cf, frame_data *fd, const wtap_block_t new_block)
{
wtap_block_t pkt_block = cf_get_packet_block(cf, fd);
/* It's possible to further modify the modified block "in place" by doing
* a call to cf_get_packet_block() that returns an already created modified
* block, modifying that, and calling this function.
* If the caller did that, then the block pointers will be equal.
*/
if (pkt_block == new_block) {
/* No need to save anything here, the caller changes went right
* onto the block.
* Unfortunately we don't have a way to know how many comments were
* in the block before the caller modified it, so tell the caller
* it is its responsibility to update the comment count.
*/
return FALSE;
}
else {
if (pkt_block)
cf->packet_comment_count -= wtap_block_count_option(pkt_block, OPT_COMMENT);
if (new_block)
cf->packet_comment_count += wtap_block_count_option(new_block, OPT_COMMENT);
cap_file_provider_set_modified_block(&cf->provider, fd, new_block);
expert_update_comment_count(cf->packet_comment_count);
}
/* Either way, we have unsaved changes. */
wtap_block_unref(pkt_block);
cf->unsaved_changes = TRUE;
return TRUE;
}
/*
* What types of comments does this capture file have?
*/
guint32
cf_comment_types(capture_file *cf)
{
guint32 comment_types = 0;
/*
* Does this file have any sections with at least one comment?
*/
for (guint section_number = 0;
section_number < wtap_file_get_num_shbs(cf->provider.wth);
section_number++) {
wtap_block_t shb_inf;
char *shb_comment;
shb_inf = wtap_file_get_shb(cf->provider.wth, section_number);
/* Try to get the first comment from that SHB. */
if (wtap_block_get_nth_string_option_value(shb_inf, OPT_COMMENT, 0,
&shb_comment) == WTAP_OPTTYPE_SUCCESS) {
/* We succeeded, so this file has at least one section comment. */
comment_types |= WTAP_COMMENT_PER_SECTION;
/* We don't need to search any more. */
break;
}
}
if (cf->packet_comment_count != 0)
comment_types |= WTAP_COMMENT_PER_PACKET;
return comment_types;
}
/*
* Add a resolved address to this file's list of resolved addresses.
*/
gboolean
cf_add_ip_name_from_string(capture_file *cf, const char *addr, const char *name)
{
/*
* XXX - support multiple resolved address lists, and add to the one
* attached to this file?
*/
if (!add_ip_name_from_string(addr, name))
return FALSE;
/* OK, we have unsaved changes. */
cf->unsaved_changes = TRUE;
return TRUE;
}
typedef struct {
wtap_dumper *pdh;
const char *fname;
int file_type;
gboolean export;
} save_callback_args_t;
/*
* Save a capture to a file, in a particular format, saving either
* all packets, all currently-displayed packets, or all marked packets.
*
* Returns TRUE if it succeeds, FALSE otherwise; if it fails, it pops
* up a message box for the failure.
*/
static gboolean
save_record(capture_file *cf, frame_data *fdata, wtap_rec *rec,
Buffer *buf, void *argsp)
{
save_callback_args_t *args = (save_callback_args_t *)argsp;
wtap_rec new_rec;
int err;
gchar *err_info;
wtap_block_t pkt_block;
/* Copy the record information from what was read in from the file. */
new_rec = *rec;
/* Make changes based on anything that the user has done but that
hasn't been saved yet. */
if (fdata->has_modified_block)
pkt_block = cap_file_provider_get_modified_block(&cf->provider, fdata);
else
pkt_block = rec->block;
new_rec.block = pkt_block;
new_rec.block_was_modified = fdata->has_modified_block ? TRUE : FALSE;
if (!nstime_is_zero(&fdata->shift_offset)) {
if (new_rec.presence_flags & WTAP_HAS_TS) {
nstime_add(&new_rec.ts, &fdata->shift_offset);
}
}
/* and save the packet */
if (!wtap_dump(args->pdh, &new_rec, ws_buffer_start_ptr(buf), &err, &err_info)) {
cfile_write_failure_alert_box(NULL, args->fname, err, err_info, fdata->num,
args->file_type);
return FALSE;
}
/* If we are saving (i.e., replacing the current file with the one we're
* writing), then update the frame data to clear the shift offset.
* This keeps us from having to re-read the entire file.
* We could do this in rescan_file(), but
* 1) Ideally we shouldn't have to call rescan_file if all we're doing
* is changing the timestamps, since that shouldn't change the offsets.
* 2) The long term goal is to try to do the offset adjustment here
* instead of using rescan_file, which should be faster (#1257).
*
* If we're exporting to a different file, then don't do that.
*/
if (!args->export && new_rec.presence_flags & WTAP_HAS_TS) {
nstime_set_zero(&fdata->shift_offset);
}
return TRUE;
}
/*
* Can this capture file be written out in any format using Wiretap
* rather than by copying the raw data?
*/
gboolean
cf_can_write_with_wiretap(capture_file *cf)
{
/* We don't care whether we support the comments in this file or not;
if we can't, we'll offer the user the option of discarding the
comments. */
return wtap_dump_can_write(cf->linktypes, 0);
}
/*
* Should we let the user do a save?
*
* We should if:
*
* the file has unsaved changes, and we can save it in some
* format through Wiretap
*
* or
*
* the file is a temporary file and has no unsaved changes (so
* that "saving" it just means copying it).
*
* XXX - we shouldn't allow files to be edited if they can't be saved,
* so cf->unsaved_changes should be true only if the file can be saved.
*
* We don't care whether we support the comments in this file or not;
* if we can't, we'll offer the user the option of discarding the
* comments.
*/
gboolean
cf_can_save(capture_file *cf)
{
if (cf->unsaved_changes && wtap_dump_can_write(cf->linktypes, 0)) {
/* Saved changes, and we can write it out with Wiretap. */
return TRUE;
}
if (cf->is_tempfile && !cf->unsaved_changes) {
/*
* Temporary file with no unsaved changes, so we can just do a
* raw binary copy.
*/
return TRUE;
}
/* Nothing to save. */
return FALSE;
}
/*
* Should we let the user do a "save as"?
*
* That's true if:
*
* we can save it in some format through Wiretap
*
* or
*
* the file is a temporary file and has no unsaved changes (so
* that "saving" it just means copying it).
*
* XXX - we shouldn't allow files to be edited if they can't be saved,
* so cf->unsaved_changes should be true only if the file can be saved.
*
* We don't care whether we support the comments in this file or not;
* if we can't, we'll offer the user the option of discarding the
* comments.
*/
gboolean
cf_can_save_as(capture_file *cf)
{
if (wtap_dump_can_write(cf->linktypes, 0)) {
/* We can write it out with Wiretap. */
return TRUE;
}
if (cf->is_tempfile && !cf->unsaved_changes) {
/*
* Temporary file with no unsaved changes, so we can just do a
* raw binary copy.
*/
return TRUE;
}
/* Nothing to save. */
return FALSE;
}
/*
* Does this file have unsaved data?
*/
gboolean
cf_has_unsaved_data(capture_file *cf)
{
/*
* If this is a temporary file, or a file with unsaved changes, it
* has unsaved data.
*/
return (cf->is_tempfile && cf->count>0) || cf->unsaved_changes;
}
/*
* Quick scan to find packet offsets.
*/
static cf_read_status_t
rescan_file(capture_file *cf, const char *fname, gboolean is_tempfile)
{
wtap_rec rec;
Buffer buf;
int err;
gchar *err_info;
gchar *name_ptr;
gint64 data_offset;
progdlg_t *progbar = NULL;
GTimer *prog_timer = g_timer_new();
gint64 size;
float progbar_val;
gint64 start_time;
gchar status_str[100];
guint32 framenum;
frame_data *fdata;
/* Close the old handle. */
wtap_close(cf->provider.wth);
/* Open the new file. */
/* XXX: this will go through all open_routines for a matching one. But right
now rescan_file() is only used when a file is being saved to a different
format than the original, and the user is not given a choice of which
reader to use (only which format to save it in), so doing this makes
sense for now. (XXX: Now it is also used when saving a changed file,
e.g. comments or time-shifted frames.) */
cf->provider.wth = wtap_open_offline(fname, WTAP_TYPE_AUTO, &err, &err_info, TRUE);
if (cf->provider.wth == NULL) {
cfile_open_failure_alert_box(fname, err, err_info);
return CF_READ_ERROR;
}
/* We're scanning a file whose contents should be the same as what
we had before, so we don't discard dissection state etc.. */
cf->f_datalen = 0;
/* Set the file name because we need it to set the follow stream filter.
XXX - is that still true? We need it for other reasons, though,
in any case. */
if (cf->filename != NULL) {
g_free(cf->filename);
}
cf->filename = g_strdup(fname);
/* Indicate whether it's a permanent or temporary file. */
cf->is_tempfile = is_tempfile;
/* No user changes yet. */
cf->unsaved_changes = FALSE;
cf->cd_t = wtap_file_type_subtype(cf->provider.wth);
if (cf->linktypes != NULL) {
g_array_free(cf->linktypes, TRUE);
}
cf->linktypes = g_array_sized_new(FALSE, FALSE, (guint) sizeof(int), 1);
cf->snap = wtap_snapshot_length(cf->provider.wth);
name_ptr = g_filename_display_basename(cf->filename);
cf_callback_invoke(cf_cb_file_rescan_started, cf);
/* Record the file's compression type.
XXX - do we know this at open time? */
cf->compression_type = wtap_get_compression_type(cf->provider.wth);
/* Find the size of the file. */
size = wtap_file_size(cf->provider.wth, NULL);
g_timer_start(prog_timer);
cf->stop_flag = FALSE;
start_time = g_get_monotonic_time();
framenum = 0;
wtap_rec_init(&rec);
ws_buffer_init(&buf, 1514);
while ((wtap_read(cf->provider.wth, &rec, &buf, &err, &err_info,
&data_offset))) {
framenum++;
fdata = frame_data_sequence_find(cf->provider.frames, framenum);
if (G_LIKELY(fdata != NULL)) {
fdata->file_off = data_offset;
}
if (size >= 0) {
cf->f_datalen = wtap_read_so_far(cf->provider.wth);
/* Create the progress bar if necessary. */
if (progress_is_slow(progbar, prog_timer, size, cf->f_datalen)) {
progbar_val = calc_progbar_val(cf, size, cf->f_datalen, status_str, sizeof(status_str));
progbar = delayed_create_progress_dlg(cf->window, NULL, NULL,
TRUE, &cf->stop_flag, progbar_val);
}
/*
* Update the progress bar, but do it only after PROGBAR_UPDATE_INTERVAL
* has elapsed. Calling update_progress_dlg and packets_bar_update will
* likely trigger UI paint events, which might take a while depending on
* the platform and display. Reset our timer *after* painting.
*/
if (progbar && g_timer_elapsed(prog_timer, NULL) > PROGBAR_UPDATE_INTERVAL) {
progbar_val = calc_progbar_val(cf, size, cf->f_datalen, status_str, sizeof(status_str));
/* update the packet bar content on the first run or frequently on very large files */
update_progress_dlg(progbar, progbar_val, status_str);
compute_elapsed(cf, start_time);
packets_bar_update();
g_timer_start(prog_timer);
}
}
if (cf->stop_flag) {
/* Well, the user decided to abort the rescan. Sadly, as this
isn't a reread, recovering is difficult, so we'll just
close the current capture. */
break;
}
/* Add this packet's link-layer encapsulation type to cf->linktypes, if
it's not already there.
XXX - yes, this is O(N), so if every packet had a different
link-layer encapsulation type, it'd be O(N^2) to read the file, but
there are probably going to be a small number of encapsulation types
in a file. */
if (rec.rec_type == REC_TYPE_PACKET) {
cf_add_encapsulation_type(cf, rec.rec_header.packet_header.pkt_encap);
}
wtap_rec_reset(&rec);
}
wtap_rec_cleanup(&rec);
ws_buffer_free(&buf);
/* Free the display name */
g_free(name_ptr);
/* We're done reading the file; destroy the progress bar if it was created. */
if (progbar != NULL)
destroy_progress_dlg(progbar);
g_timer_destroy(prog_timer);
/* We're done reading sequentially through the file. */
cf->state = FILE_READ_DONE;
/* Close the sequential I/O side, to free up memory it requires. */
wtap_sequential_close(cf->provider.wth);
/* compute the time it took to load the file */
compute_elapsed(cf, start_time);
/* Set the file encapsulation type now; we don't know what it is until
we've looked at all the packets, as we don't know until then whether
there's more than one type (and thus whether it's
WTAP_ENCAP_PER_PACKET). */
cf->lnk_t = wtap_file_encap(cf->provider.wth);
cf_callback_invoke(cf_cb_file_rescan_finished, cf);
if (cf->stop_flag) {
/* Our caller will give up at this point. */
return CF_READ_ABORTED;
}
if (err != 0) {
/* Put up a message box noting that the read failed somewhere along
the line. Don't throw out the stuff we managed to read, though,
if any. */
cfile_read_failure_alert_box(NULL, err, err_info);
return CF_READ_ERROR;
} else
return CF_READ_OK;
}
cf_write_status_t
cf_save_records(capture_file *cf, const char *fname, guint save_format,
wtap_compression_type compression_type,
gboolean discard_comments, gboolean dont_reopen)
{
gchar *err_info;
gchar *fname_new = NULL;
wtap_dumper *pdh;
frame_data *fdata;
addrinfo_lists_t *addr_lists;
guint framenum;
int err;
#ifdef _WIN32
gchar *display_basename;
#endif
enum {
SAVE_WITH_MOVE,
SAVE_WITH_COPY,
SAVE_WITH_WTAP
} how_to_save;
save_callback_args_t callback_args;
callback_args.export = FALSE;
gboolean needs_reload = FALSE;
/* XXX caller should avoid saving the file while a read is pending
* (e.g. by delaying the save action) */
if (cf->read_lock) {
ws_warning("cf_save_records(\"%s\") while the file is being read, potential crash ahead", fname);
}
cf_callback_invoke(cf_cb_file_save_started, (gpointer)fname);
addr_lists = get_addrinfo_list();
if (save_format == cf->cd_t && compression_type == cf->compression_type
&& !discard_comments && !cf->unsaved_changes
&& (wtap_addrinfo_list_empty(addr_lists) || wtap_file_type_subtype_supports_block(save_format, WTAP_BLOCK_NAME_RESOLUTION) == BLOCK_NOT_SUPPORTED)) {
/* We're saving in the format it's already in, and we're not discarding
comments, and there are no changes we have in memory that aren't saved
to the file, and we have no name resolution information to write or
the file format we're saving in doesn't support writing name
resolution information, so we can just move or copy the raw data. */
if (cf->is_tempfile) {
/* The file being saved is a temporary file from a live
capture, so it doesn't need to stay around under that name;
first, try renaming the capture buffer file to the new name.
This acts as a "safe save", in that, if the file already
exists, the existing file will be removed only if the rename
succeeds.
Sadly, on Windows, as we have the current capture file
open, even MoveFileEx() with MOVEFILE_REPLACE_EXISTING
(to cause the rename to remove an existing target), as
done by ws_stdio_rename() (ws_rename() is #defined to
be ws_stdio_rename() on Windows) will fail.
According to the MSDN documentation for CreateFile(), if,
when we open a capture file, we were to directly do a CreateFile(),
opening with FILE_SHARE_DELETE|FILE_SHARE_READ, and then
convert it to a file descriptor with _open_osfhandle(),
that would allow the file to be renamed out from under us.
However, that doesn't work in practice. Perhaps the problem
is that the process doing the rename is the process that
has the file open. */
#ifndef _WIN32
if (ws_rename(cf->filename, fname) == 0) {
/* That succeeded - there's no need to copy the source file. */
how_to_save = SAVE_WITH_MOVE;
} else {
if (errno == EXDEV) {
/* They're on different file systems, so we have to copy the
file. */
how_to_save = SAVE_WITH_COPY;
} else {
/* The rename failed, but not because they're on different
file systems - put up an error message. (Or should we
just punt and try to copy? The only reason why I'd
expect the rename to fail and the copy to succeed would
be if we didn't have permission to remove the file from
the temporary directory, and that might be fixable - but
is it worth requiring the user to go off and fix it?) */
cf_rename_failure_alert_box(fname, errno);
goto fail;
}
}
#else
/* Windows - copy the file to its new location. */
how_to_save = SAVE_WITH_COPY;
#endif
} else {
/* It's a permanent file, so we should copy it, and not remove the
original. */
how_to_save = SAVE_WITH_COPY;
}
if (how_to_save == SAVE_WITH_COPY) {
/* Copy the file, if we haven't moved it. If we're overwriting
an existing file, we do it with a "safe save", by writing
to a new file and, if the write succeeds, renaming the
new file on top of the old file. */
if (file_exists(fname)) {
fname_new = ws_strdup_printf("%s~", fname);
if (!copy_file_binary_mode(cf->filename, fname_new))
goto fail;
} else {
if (!copy_file_binary_mode(cf->filename, fname))
goto fail;
}
}
} else {
/* Either we're saving in a different format or we're saving changes,
such as added, modified, or removed comments, that haven't yet
been written to the underlying file; we can't do that by copying
or moving the capture file, we have to do it by writing the packets
out in Wiretap. */
wtap_dump_params params;
int encap;
how_to_save = SAVE_WITH_WTAP;
wtap_dump_params_init(¶ms, cf->provider.wth);
/* Determine what file encapsulation type we should use. */
encap = wtap_dump_required_file_encap_type(cf->linktypes);
params.encap = encap;
/* Use the snaplen from cf (XXX - does wtap_dump_params_init handle that?) */
params.snaplen = cf->snap;
if (file_exists(fname)) {
/* We're overwriting an existing file; write out to a new file,
and, if that succeeds, rename the new file on top of the
old file. That makes this a "safe save", so that we don't
lose the old file if we have a problem writing out the new
file. (If the existing file is the current capture file,
we *HAVE* to do that, otherwise we're overwriting the file
from which we're reading the packets that we're writing!) */
fname_new = ws_strdup_printf("%s~", fname);
pdh = wtap_dump_open(fname_new, save_format, compression_type, ¶ms,
&err, &err_info);
} else {
pdh = wtap_dump_open(fname, save_format, compression_type, ¶ms,
&err, &err_info);
}
/* XXX idb_inf is documented to be used until wtap_dump_close. */
g_free(params.idb_inf);
params.idb_inf = NULL;
if (pdh == NULL) {
cfile_dump_open_failure_alert_box(fname, err, err_info, save_format);
goto fail;
}
/* Add address resolution */
wtap_dump_set_addrinfo_list(pdh, addr_lists);
/* Iterate through the list of packets, processing all the packets. */
callback_args.pdh = pdh;
callback_args.fname = fname;
callback_args.file_type = save_format;
switch (process_specified_records(cf, NULL, "Saving", "packets",
TRUE, save_record, &callback_args, TRUE)) {
case PSP_FINISHED:
/* Completed successfully. */
break;
case PSP_STOPPED:
/* The user decided to abort the saving.
If we're writing to a temporary file, remove it.
XXX - should we do so even if we're not writing to a
temporary file? */
wtap_dump_close(pdh, NULL, &err, &err_info);
if (fname_new != NULL)
ws_unlink(fname_new);
cf_callback_invoke(cf_cb_file_save_stopped, NULL);
wtap_dump_params_cleanup(¶ms);
return CF_WRITE_ABORTED;
case PSP_FAILED:
/* Error while saving.
If we're writing to a temporary file, remove it. */
if (fname_new != NULL)
ws_unlink(fname_new);
wtap_dump_close(pdh, NULL, &err, &err_info);
wtap_dump_params_cleanup(¶ms);
goto fail;
}
if (!wtap_dump_close(pdh, &needs_reload, &err, &err_info)) {
cfile_close_failure_alert_box(fname, err, err_info);
wtap_dump_params_cleanup(¶ms);
goto fail;
}
wtap_dump_params_cleanup(¶ms);
}
if (fname_new != NULL) {
/* We wrote out to fname_new, and should rename it on top of
fname. fname_new is now closed, so that should be possible even
on Windows. However, on Windows, we first need to close whatever
file descriptors we have open for fname. */
#ifdef _WIN32
wtap_fdclose(cf->provider.wth);
#endif
/* Now do the rename. */
if (ws_rename(fname_new, fname) == -1) {
/* Well, the rename failed. */
cf_rename_failure_alert_box(fname, errno);
#ifdef _WIN32
/* Attempt to reopen the random file descriptor using the
current file's filename. (At this point, the sequential
file descriptor is closed.) */
if (!wtap_fdreopen(cf->provider.wth, cf->filename, &err)) {
/* Oh, well, we're screwed. */
display_basename = g_filename_display_basename(cf->filename);
simple_error_message_box(
file_open_error_message(err, FALSE), display_basename);
g_free(display_basename);
}
#endif
goto fail;
}
g_free(fname_new);
}
/* If this was a temporary file, and we didn't do the save by doing
a move, so the tempoary file is still around under its old name,
remove it. */
if (cf->is_tempfile && how_to_save != SAVE_WITH_MOVE) {
/* If this fails, there's not much we can do, so just ignore errors. */
ws_unlink(cf->filename);
}
cf_callback_invoke(cf_cb_file_save_finished, NULL);
cf->unsaved_changes = FALSE;
if (!dont_reopen) {
switch (how_to_save) {
case SAVE_WITH_MOVE:
/* We just moved the file, so the wtap structure refers to the
new file, and all the information other than the filename
and the "is temporary" status applies to the new file; just
update that. */
g_free(cf->filename);
cf->filename = g_strdup(fname);
cf->is_tempfile = FALSE;
cf_callback_invoke(cf_cb_file_fast_save_finished, cf);
break;
case SAVE_WITH_COPY:
/* We just copied the file, so all the information other than
the file descriptors, the filename, and the "is temporary"
status applies to the new file; just update that. */
wtap_fdclose(cf->provider.wth);
/* Attempt to reopen the random file descriptor using the
new file's filename. (At this point, the sequential
file descriptor is closed.) */
if (!wtap_fdreopen(cf->provider.wth, fname, &err)) {
cfile_open_failure_alert_box(fname, err, err_info);
cf_close(cf);
} else {
g_free(cf->filename);
cf->filename = g_strdup(fname);
cf->is_tempfile = FALSE;
}
cf_callback_invoke(cf_cb_file_fast_save_finished, cf);
break;
case SAVE_WITH_WTAP:
/* Open and read the file we saved to.
XXX - this is somewhat of a waste; we already have the
packets, all this gets us is updated file type information
(which we could just stuff into "cf"), and having the new
file be the one we have opened and from which we're reading
the data, and it means we have to spend time opening and
reading the file, which could be a significant amount of
time if the file is large.
If the capture-file-writing code were to return the
seek offset of each packet it writes, we could save that
in the frame_data structure for the frame, and just open
the file without reading it again...
...as long as, for gzipped files, the process of writing
out the file *also* generates the information needed to
support fast random access to the compressed file. */
/* rescan_file will cause us to try all open_routines, so
reset cfile's open_type */
cf->open_type = WTAP_TYPE_AUTO;
/* There are cases when SAVE_WITH_WTAP can result in new packets
being written to the file, e.g ERF records
In that case, we need to reload the whole file */
if(needs_reload) {
if (cf_open(cf, fname, WTAP_TYPE_AUTO, FALSE, &err) == CF_OK) {
if (cf_read(cf, /*reloading=*/TRUE) != CF_READ_OK) {
/* The rescan failed; just close the file. Either
a dialog was popped up for the failure, so the
user knows what happened, or they stopped the
rescan, in which case they know what happened. */
/* XXX: This is inconsistent with normal open/reload behaviour. */
cf_close(cf);
}
}
}
else {
if (rescan_file(cf, fname, FALSE) != CF_READ_OK) {
/* The rescan failed; just close the file. Either
a dialog was popped up for the failure, so the
user knows what happened, or they stopped the
rescan, in which case they know what happened. */
cf_close(cf);
}
}
break;
}
/* If we were told to discard the comments, do so. */
if (discard_comments) {
/* Remove SHB comment, if any. */
wtap_write_shb_comment(cf->provider.wth, NULL);
/* remove all user comments */
for (framenum = 1; framenum <= cf->count; framenum++) {
fdata = frame_data_sequence_find(cf->provider.frames, framenum);
// XXX: This also ignores non-comment options like verdict
fdata->has_modified_block = FALSE;
}
if (cf->provider.frames_modified_blocks) {
g_tree_destroy(cf->provider.frames_modified_blocks);
cf->provider.frames_modified_blocks = NULL;
}
cf->packet_comment_count = 0;
}
}
return CF_WRITE_OK;
fail:
if (fname_new != NULL) {
/* We were trying to write to a temporary file; get rid of it if it
exists. (We don't care whether this fails, as, if it fails,
there's not much we can do about it. I guess if it failed for
a reason other than "it doesn't exist", we could report an
error, so the user knows there's a junk file that they might
want to clean up.) */
ws_unlink(fname_new);
g_free(fname_new);
}
cf_callback_invoke(cf_cb_file_save_failed, NULL);
return CF_WRITE_ERROR;
}
cf_write_status_t
cf_export_specified_packets(capture_file *cf, const char *fname,
packet_range_t *range, guint save_format,
wtap_compression_type compression_type)
{
gchar *fname_new = NULL;
int err;
gchar *err_info;
wtap_dumper *pdh;
save_callback_args_t callback_args;
wtap_dump_params params;
int encap;
callback_args.export = TRUE;
packet_range_process_init(range);
/* We're writing out specified packets from the specified capture
file to another file. Even if all captured packets are to be
written, don't special-case the operation - read each packet
and then write it out if it's one of the specified ones. */
wtap_dump_params_init(¶ms, cf->provider.wth);
/* Determine what file encapsulation type we should use. */
encap = wtap_dump_required_file_encap_type(cf->linktypes);
params.encap = encap;
/* Use the snaplen from cf (XXX - does wtap_dump_params_init handle that?) */
params.snaplen = cf->snap;
if (file_exists(fname)) {
/* We're overwriting an existing file; write out to a new file,
and, if that succeeds, rename the new file on top of the
old file. That makes this a "safe save", so that we don't
lose the old file if we have a problem writing out the new
file. (If the existing file is the current capture file,
we *HAVE* to do that, otherwise we're overwriting the file
from which we're reading the packets that we're writing!) */
fname_new = ws_strdup_printf("%s~", fname);
pdh = wtap_dump_open(fname_new, save_format, compression_type, ¶ms,
&err, &err_info);
} else {
pdh = wtap_dump_open(fname, save_format, compression_type, ¶ms,
&err, &err_info);
}
/* XXX idb_inf is documented to be used until wtap_dump_close. */
g_free(params.idb_inf);
params.idb_inf = NULL;
if (pdh == NULL) {
cfile_dump_open_failure_alert_box(fname, err, err_info, save_format);
goto fail;
}
/* Add address resolution */
wtap_dump_set_addrinfo_list(pdh, get_addrinfo_list());
/* Iterate through the list of packets, processing the packets we were
told to process.
XXX - we've already called "packet_range_process_init(range)", but
"process_specified_records()" will do it again. Fortunately,
that's harmless in this case, as we haven't done anything to
"range" since we initialized it. */
callback_args.pdh = pdh;
callback_args.fname = fname;
callback_args.file_type = save_format;
switch (process_specified_records(cf, range, "Writing", "specified records",
TRUE, save_record, &callback_args, TRUE)) {
case PSP_FINISHED:
/* Completed successfully. */
break;
case PSP_STOPPED:
/* The user decided to abort the saving.
If we're writing to a temporary file, remove it.
XXX - should we do so even if we're not writing to a
temporary file? */
wtap_dump_close(pdh, NULL, &err, &err_info);
if (fname_new != NULL) {
ws_unlink(fname_new);
g_free(fname_new);
}
wtap_dump_params_cleanup(¶ms);
return CF_WRITE_ABORTED;
break;
case PSP_FAILED:
/* Error while saving. */
wtap_dump_close(pdh, NULL, &err, &err_info);
/*
* We don't report any error from closing; the error that caused
* process_specified_records() to fail has already been reported.
*/
goto fail;
}
if (!wtap_dump_close(pdh, NULL, &err, &err_info)) {
cfile_close_failure_alert_box(fname, err, err_info);
goto fail;
}
if (fname_new != NULL) {
/* We wrote out to fname_new, and should rename it on top of
fname; fname is now closed, so that should be possible even
on Windows. Do the rename. */
if (ws_rename(fname_new, fname) == -1) {
/* Well, the rename failed. */
cf_rename_failure_alert_box(fname, errno);
goto fail;
}
g_free(fname_new);
}
wtap_dump_params_cleanup(¶ms);
return CF_WRITE_OK;
fail:
if (fname_new != NULL) {
/* We were trying to write to a temporary file; get rid of it if it
exists. (We don't care whether this fails, as, if it fails,
there's not much we can do about it. I guess if it failed for
a reason other than "it doesn't exist", we could report an
error, so the user knows there's a junk file that they might
want to clean up.) */
ws_unlink(fname_new);
g_free(fname_new);
}
wtap_dump_params_cleanup(¶ms);
return CF_WRITE_ERROR;
}
/*
* XXX - whether we mention the source pathname, the target pathname,
* or both depends on the error and on what we find if we look for
* one or both of them.
*/
static void
cf_rename_failure_alert_box(const char *filename, int err)
{
gchar *display_basename;
display_basename = g_filename_display_basename(filename);
switch (err) {
case ENOENT:
/* XXX - should check whether the source exists and, if not,
report it as the problem and, if so, report the destination
as the problem. */
simple_error_message_box("The path to the file \"%s\" doesn't exist.",
display_basename);
break;
case EACCES:
/* XXX - if we're doing a rename after a safe save, we should
probably say something else. */
simple_error_message_box("You don't have permission to move the capture file to \"%s\".",
display_basename);
break;
default:
/* XXX - this should probably mention both the source and destination
pathnames. */
simple_error_message_box("The file \"%s\" could not be moved: %s.",
display_basename, wtap_strerror(err));
break;
}
g_free(display_basename);
}
/* Reload the current capture file. */
cf_status_t
cf_reload(capture_file *cf)
{
gchar *filename;
gboolean is_tempfile;
cf_status_t cf_status = CF_OK;
int err;
if (cf->read_lock) {
ws_warning("Failing cf_reload(\"%s\") since a read is in progress", cf->filename);
return CF_ERROR;
}
/* If the file could be opened, "cf_open()" calls "cf_close()"
to get rid of state for the old capture file before filling in state
for the new capture file. "cf_close()" will remove the file if
it's a temporary file; we don't want that to happen (for one thing,
it'd prevent subsequent reopens from working). Remember whether it's
a temporary file, mark it as not being a temporary file, and then
reopen it as the type of file it was.
Also, "cf_close()" will free "cf->filename", so we must make
a copy of it first. */
filename = g_strdup(cf->filename);
is_tempfile = cf->is_tempfile;
cf->is_tempfile = FALSE;
if (cf_open(cf, filename, cf->open_type, is_tempfile, &err) == CF_OK) {
switch (cf_read(cf, /*reloading=*/TRUE)) {
case CF_READ_OK:
case CF_READ_ERROR:
/* Just because we got an error, that doesn't mean we were unable
to read any of the file; we handle what we could get from the
file. */
break;
case CF_READ_ABORTED:
/* The user bailed out of re-reading the capture file; the
capture file has been closed. */
break;
}
} else {
/* The open failed, so "cf->is_tempfile" wasn't set to "is_tempfile".
Instead, the file was left open, so we should restore "cf->is_tempfile"
ourselves.
XXX - change the menu? Presumably "cf_open()" will do that;
make sure it does! */
cf->is_tempfile = is_tempfile;
cf_status = CF_ERROR;
}
/* "cf_open()" made a copy of the file name we handed it, so
we should free up our copy. */
g_free(filename);
return cf_status;
}
|