summaryrefslogtreecommitdiffstats
path: root/extcap/ciscodump.c
blob: e01669ccb9a05e025e4331436edc94f2abfd4cc0 (plain)
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
/* ciscodump.c
 * ciscodump is extcap tool used to capture data using a ssh on a remote cisco router
 *
 * Copyright 2015, Dario Lombardo
 *
 * 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 "ciscodump"

#include <extcap/extcap-base.h>
#include <wsutil/interface.h>
#include <wsutil/strtoi.h>
#include <wsutil/filesystem.h>
#include <wsutil/privileges.h>
#include <wsutil/please_report_bug.h>
#include <wsutil/wslog.h>
#include <extcap/ssh-base.h>
#include <writecap/pcapio.h>

#include <errno.h>
#include <string.h>
#include <fcntl.h>

#include <wsutil/time_util.h>
#include <wsutil/ws_strptime.h>

#include <cli_main.h>

#define CISCODUMP_VERSION_MAJOR "1"
#define CISCODUMP_VERSION_MINOR "0"
#define CISCODUMP_VERSION_RELEASE "0"

/* The read timeout in msec */
#define CISCODUMP_READ_TIMEOUT_MSEC 300

#define CISCODUMP_EXTCAP_INTERFACE "ciscodump"
#define SSH_READ_BLOCK_SIZE 1024
#define SSH_READ_TIMEOUT_MSES 10000
#define SSH_READ_TIMEOUT_USEC (SSH_READ_TIMEOUT_MSES*1000)

#define WIRESHARK_CAPTURE "WSC"
#define WIRESHARK_CAPTURE_POINT "WSC_P"
#define WIRESHARK_CAPTURE_BUFFER "WSC_B"
#define WIRESHARK_CAPTURE_ACCESSLIST "WSC_ACL"

#define PCAP_SNAPLEN 0xffff

#define PACKET_MAX_SIZE 65535

#define MINIMUM_IOS_MAJOR 12
#define MINIMUM_IOS_MINOR 4
#define MINIMUM_IOS_XE_MAJOR_16 16
#define MINIMUM_IOS_XE_MINOR_16 1
#define MINIMUM_IOS_XE_MAJOR_17 17
#define MINIMUM_IOS_XE_MINOR_17 1
#define MINIMUM_ASA_MAJOR 8
#define MINIMUM_ASA_MINOR 4

#define READ_PROMPT_ERROR -1
#define READ_PROMPT_EOLN 0
#define READ_PROMPT_PROMPT 1
#define READ_PROMPT_TOO_LONG 2

#define READ_LINE_ERROR -1
#define READ_LINE_EOLN 0
#define READ_LINE_TIMEOUT 1
#define READ_LINE_TOO_LONG 2

/* Type of Cisco device */
typedef enum {
	CISCO_UNKNOWN,
	CISCO_IOS,
	CISCO_IOS_XE_16,
	CISCO_IOS_XE_17,
	CISCO_ASA
} CISCO_SW_TYPE;

/* Status of the parser */
enum {
	CISCODUMP_PARSER_STARTING,
	CISCODUMP_PARSER_IN_PACKET,
	CISCODUMP_PARSER_IN_HEADER,
	CISCODUMP_PARSER_END_PACKET,
	CISCODUMP_PARSER_UNKNOWN
};

enum {
	EXTCAP_BASE_OPTIONS_ENUM,
	OPT_HELP,
	OPT_VERSION,
	OPT_REMOTE_HOST,
	OPT_REMOTE_PORT,
	OPT_REMOTE_USERNAME,
	OPT_REMOTE_PASSWORD,
	OPT_REMOTE_INTERFACE,
	OPT_REMOTE_FILTER,
	OPT_SSHKEY,
	OPT_SSHKEY_PASSPHRASE,
	OPT_PROXYCOMMAND,
	OPT_SSH_SHA1,
	OPT_REMOTE_COUNT
};

static char prompt_str[SSH_READ_BLOCK_SIZE + 1];
static int32_t prompt_len = -1;
CISCO_SW_TYPE global_sw_type = CISCO_UNKNOWN;
static bool send_output_quit;	/* IOS XE 17: send quit during output */

static const struct ws_option longopts[] = {
	EXTCAP_BASE_OPTIONS,
	{ "help", ws_no_argument, NULL, OPT_HELP},
	{ "version", ws_no_argument, NULL, OPT_VERSION},
	SSH_BASE_OPTIONS,
	{ 0, 0, 0, 0}
};

static void graceful_shutdown_cb(void)
{
	if (global_sw_type == CISCO_IOS_XE_17) {
		send_output_quit = true;
	}
}

/* Replaces needle with rep in line */
static char* str_replace_char(char *line, char needle, char rep)
{
	for(int i = 0; line[i] != '\0'; i++) {
		if (line[i] == needle) {
			line[i] = rep;
		}
	}

	return line;
}

/* Replaces CR with LN */
static char* crtoln(char *line)
{
	return str_replace_char(line, '\r', '\n');
}

static char* interfaces_list_to_filter(GSList* interfaces, unsigned int remote_port)
{
	GString* filter = g_string_new(NULL);
	GSList* cur;

	if (interfaces) {
		g_string_append_printf(filter, "deny tcp host %s any eq %u, deny tcp any eq %u host %s",
				(char*)interfaces->data, remote_port, remote_port, (char*)interfaces->data);
		cur = g_slist_next(interfaces);
		while (cur) {
			g_string_append_printf(filter, ", deny tcp host %s any eq %u, deny tcp any eq %u host %s",
				(char*)cur->data, remote_port, remote_port, (char*)cur->data);
			cur = g_slist_next(cur);
		}
		g_string_append_printf(filter, ", permit ip any any");
	}

	return g_string_free(filter, FALSE);
}

static char* local_interfaces_to_filter(const unsigned int remote_port)
{
	GSList* interfaces = local_interfaces_to_list();
	char* filter = interfaces_list_to_filter(interfaces, remote_port);
	g_slist_free_full(interfaces, g_free);
	return filter;
}

/* Read bytes from the channel with no escape character.
 * If bytes == -1, read all data (until timeout). If outbuf != NULL, data are stored there
 */
static int read_output_bytes_any(ssh_channel channel, int bytes, char* outbuf)
{
	char chr;
	int total;
	int bytes_read;

	total = (bytes > 0 ? bytes : INT_MAX);
	bytes_read = 0;

	while(ssh_channel_read_timeout(channel, &chr, 1, 0, CISCODUMP_READ_TIMEOUT_MSEC) > 0 && bytes_read < total) {
		ws_noisy("%c %02x", chr, chr);
		if (outbuf)
			outbuf[bytes_read] = chr;
		bytes_read++;
	}
	if (outbuf)
		outbuf[bytes_read+1] = '\0';
	return EXIT_SUCCESS;
}

/* Read bytes from the channel. Recognize escape char '^'.
 * If bytes == -1, read all data (until timeout). If outbuf != NULL, data are stored there
 */
static int read_output_bytes(ssh_channel channel, int bytes, char* outbuf)
{
	char chr;
	int total;
	int bytes_read;

	total = (bytes > 0 ? bytes : INT_MAX);
	bytes_read = 0;

	while(ssh_channel_read_timeout(channel, &chr, 1, 0, CISCODUMP_READ_TIMEOUT_MSEC) > 0 && bytes_read < total) {
		ws_noisy("%c %02x", chr, chr);
		if (chr == '^')
			return EXIT_FAILURE;
		if (outbuf)
			outbuf[bytes_read] = chr;
		bytes_read++;
	}
	return EXIT_SUCCESS;
}

/* Reads input to buffer and parses EOL
 *   If line is NULL, just received count of characters in len is calculated
 * It returns:
 *   READ_LINE_ERROR - any ssh error occurred
 *   READ_LINE_EOLN - EOLN found, line/len contains \0 terminated string
 *   READ_LINE_TIMEOUT - reading ended with timeout, line/len contains \0 terminate prompt
 *   READ_LINE_TOO_LONG - buffer is full with no EOLN nor PROMPT found, line is filled with NOT \0 terminated data
 */
static int ssh_channel_read_line_timeout(ssh_channel channel, char *line, int *len, int max_len) {
	char chr;
	int rlen = 0;

	*len = 0;
	do {
		rlen = ssh_channel_read_timeout(channel, &chr, 1, false, CISCODUMP_READ_TIMEOUT_MSEC);
		ws_noisy("%c %02x %d", chr, chr, rlen);
		if (rlen == SSH_ERROR) {
			ws_warning("Error reading from channel");
			return READ_LINE_ERROR;
		} else if (rlen > 0) {
			if (chr != '\n') {
				/* Ignore \r */
				if (chr != '\r') {
					if (line) {
						line[*len] = chr;
					}
					(*len)++;
				}
			} else {
				/* Parse the current line */
				if (line) {
					line[*len] = '\0';
				}
				return READ_LINE_EOLN;
			}
		} else {
			return READ_LINE_TIMEOUT;
		}
	} while (*len < max_len);

	return READ_LINE_TOO_LONG;
}

/* Reads input to buffer and parses EOL or prompt_str PROMPT
 * It returns:
 *   READ_PROMPT_ERROR - any ssh error occurred
 *   READ_PROMPT_EOLN - EOLN found, line/len contains \0 terminated string
 *   READ_PROMPT_PROMPT - reading ended and it ends with PROMPT, line/len contains \0 terminate prompt
 *   READ_PROMPT_TOO_LONG - buffer is full with no EOLN nor PROMPT found, line is filled with NOT \0 terminated data
 */
static int ssh_channel_read_prompt(ssh_channel channel, char *line, uint32_t *len, uint32_t max_len) {
	char chr;
	int rlen = 0;
	int64_t start_time = g_get_monotonic_time();

	do {
		rlen = ssh_channel_read_timeout(channel, &chr, 1, false, CISCODUMP_READ_TIMEOUT_MSEC);
		ws_noisy("%c %02x %d", chr, chr, rlen);
		if (rlen == SSH_ERROR) {
			ws_warning("Error reading from channel");
			return READ_PROMPT_ERROR;
		} else if (rlen > 0) {
			if (chr != '\n') {
				line[*len] = chr;
				(*len)++;
			} else {
				/* Parse the current line */
				line[*len] = '\0';
				ws_noisy("  exiting: READ_PROMPT_EOLN (%d/%d)", *len, max_len);
				return READ_PROMPT_EOLN;
			}
		} else {
			int64_t cur_time = g_get_monotonic_time();

			/* ssh timeout, we might be on prompt */
			/* IOS, IOS-XE: check if line has same length as prompt and if it match prompt */
			if ((*len == (uint32_t)prompt_len) && (0 == strncmp(line, prompt_str, prompt_len))) {
				line[*len] = '\0';
				ws_noisy("  exiting: READ_PROMPT_PROMPT (%d/%d)", *len, max_len);
				return READ_PROMPT_PROMPT;
			}
			/* ASA: check if line begins with \r and has same length as prompt and if it match prompt */
			if ((line[0] == '\r') && (*len == (uint32_t)prompt_len+1) && (0 == strncmp(line+1, prompt_str, prompt_len))) {
				line[*len] = '\0';
				ws_noisy("  exiting: READ_PROMPT_PROMPT (%d/%d)", *len, max_len);
				return READ_PROMPT_PROMPT;
			}
			/* no prompt found, so we continue in waiting for data, but we should check global timeout */
			if ((cur_time-start_time) > SSH_READ_TIMEOUT_USEC) {
				line[*len] = '\0';
				ws_noisy("  exiting: READ_PROMPT_ERROR");
				return READ_PROMPT_ERROR;
			}
		}
	} while (!extcap_end_application && (*len < max_len));

	ws_noisy("  exiting: READ_PROMPT_TOO_LONG (%d/%d/%d)", *len, max_len, extcap_end_application);
	line[*len] = '\0';
	return READ_PROMPT_TOO_LONG;
}

static int ssh_channel_wait_prompt(ssh_channel channel, char *line, uint32_t *len, uint32_t max_len) {
	char line2[SSH_READ_BLOCK_SIZE + 1];
	uint32_t len2;
	int status;

	memset(line2, 0x0, SSH_READ_BLOCK_SIZE + 1);
	line[0] = '\0';
	*len = 0;
	do {
		len2 = 0;
		switch (status = ssh_channel_read_prompt(channel, line2, &len2, SSH_READ_BLOCK_SIZE)) {
			case READ_PROMPT_EOLN:
				*len = (uint32_t)g_strlcat(line, line2, max_len);
				len2 = 0;
				break;
			case READ_PROMPT_PROMPT:
				*len = (uint32_t)g_strlcat(line, line2, max_len);
				len2 = 0;
				break;
			default:
				/* We do not have better solution for that cases */
				/* Just terminate the line and return error */
				*len = (uint32_t)g_strlcat(line, line2, max_len);
				line[max_len] = '\0';
				ws_noisy("Returning READ_PROMPT_ERROR (%d/%d)", *len, max_len);
				return READ_PROMPT_ERROR;
		}
	} while (status == READ_PROMPT_EOLN);

	ws_noisy("Returning READ_PROMPT_PROMPT (%d/%d)", *len, max_len);
	return READ_PROMPT_PROMPT;
}

/* true if prompt and no error text in response. false otherwise */
/* Note: It do not catch all CISCO CLI errors, but many of them */
static bool ssh_channel_wait_prompt_check_error(ssh_channel channel, char *line, uint32_t *len, uint32_t max_len, char *error_re) {
	/* Did we received prompt? */
	if (ssh_channel_wait_prompt(channel, line, len, max_len) != READ_PROMPT_PROMPT) {
		return false;
	}

	/* Is there ERROR: text in output? */
	if (NULL != g_strstr_len(line, -1, "ERROR:")) {
		return false;
	}

	/* Is there ERROR: text in output? */
	if (NULL != g_strstr_len(line, -1, "% Invalid input detected at")) {
		return false;
	}

	/* Is there error_re text in output? */
	if (error_re &&
	    g_regex_match_simple(error_re, line, (GRegexCompileFlags) (G_REGEX_CASELESS | G_REGEX_RAW), 0)
	   ) {
		return false;
	}

	return true;
}

static void ciscodump_cleanup_ios(ssh_channel channel, const char* iface, const char* cfilter)
{
	char* iface_copy = g_strdup(iface);
	char* iface_one;
	char* str = NULL;
	int wscp_cnt = 1;
	char* wscp_str = NULL;

	extcap_end_application = false;
	if (channel) {
		ws_debug("Removing configuration...");
		read_output_bytes(channel, -1, NULL);

		wscp_cnt = 1;
		for (str = iface_copy; ; str = NULL) {
			iface_one = strtok(str, ",");
			if (iface_one == NULL)
				break;

			wscp_str = g_strdup_printf("%s_%d", WIRESHARK_CAPTURE_POINT, wscp_cnt);
			wscp_cnt++;

			ssh_channel_printf(channel, "monitor capture point stop %s\n", wscp_str);
			ssh_channel_printf(channel, "no monitor capture point ip cef %s %s\n", wscp_str, iface_one);

			g_free(wscp_str);
			wscp_str = NULL;
		}

		ssh_channel_printf(channel, "no monitor capture buffer %s\n", WIRESHARK_CAPTURE_BUFFER);
		if (cfilter) {
			ssh_channel_printf(channel, "configure terminal\n");
			ssh_channel_printf(channel, "no ip access-list ex %s\n", WIRESHARK_CAPTURE_ACCESSLIST);
		}

		read_output_bytes(channel, -1, NULL);
		ws_debug("Configuration removed");
	}

	g_free(iface_copy);
}

static void ciscodump_cleanup_ios_xe_16(ssh_channel channel, const char* cfilter)
{
	if (channel) {
		ws_debug("Removing configuration...");
		read_output_bytes(channel, -1, NULL);

		ssh_channel_printf(channel, "monitor capture %s stop\n", WIRESHARK_CAPTURE);
		ssh_channel_printf(channel, "no monitor capture %s\n", WIRESHARK_CAPTURE);
		if (cfilter) {
			ssh_channel_printf(channel, "configure terminal\n");
			ssh_channel_printf(channel, "no ip access-list extended %s\n", WIRESHARK_CAPTURE_ACCESSLIST);
			ssh_channel_printf(channel, "\nend\n");
		}

		read_output_bytes(channel, -1, NULL);
		ws_debug("Configuration removed");
	}
}

static void ciscodump_cleanup_ios_xe_17(ssh_channel channel, const char* cfilter)
{
	if (channel) {
		ws_debug("Removing configuration...");
		read_output_bytes(channel, -1, NULL);

		ssh_channel_printf(channel, "monitor capture %s stop\n", WIRESHARK_CAPTURE);
		ssh_channel_printf(channel, "no monitor capture %s\n", WIRESHARK_CAPTURE);
		if (cfilter) {
			ssh_channel_printf(channel, "configure terminal\n");
			ssh_channel_printf(channel, "no ip access-list extended %s\n", WIRESHARK_CAPTURE_ACCESSLIST);
			ssh_channel_printf(channel, "\nend\n");
		}

		read_output_bytes(channel, -1, NULL);
		ws_debug("Configuration removed");
	}
}

static void ciscodump_cleanup_asa(ssh_channel channel, const char* cfilter)
{
	if (channel) {
		ws_debug("Removing configuration...");
		read_output_bytes(channel, -1, NULL);

		ssh_channel_printf(channel, "no capture %s\n", WIRESHARK_CAPTURE);
		if (cfilter) {
			ssh_channel_printf(channel, "configure terminal\n");
			ssh_channel_printf(channel, "clear configure access-list %s\n", WIRESHARK_CAPTURE_ACCESSLIST);
			ssh_channel_printf(channel, "\nend\n", WIRESHARK_CAPTURE_ACCESSLIST);
		}

		read_output_bytes(channel, -1, NULL);
		ws_debug("Configuration removed");
	}
}

static void ciscodump_cleanup(ssh_channel channel, const char* iface, const char* cfilter, CISCO_SW_TYPE sw_type)
{
	ws_debug("Starting config cleanup");
	switch (sw_type) {
		case CISCO_IOS:
			ciscodump_cleanup_ios(channel, iface, cfilter);
			break;
		case CISCO_IOS_XE_16:
			ciscodump_cleanup_ios_xe_16(channel, cfilter);
			break;
		case CISCO_IOS_XE_17:
			ciscodump_cleanup_ios_xe_17(channel, cfilter);
			break;
		case CISCO_ASA:
			ciscodump_cleanup_asa(channel, cfilter);
			break;
		case CISCO_UNKNOWN:
			break;
	}
	ws_debug("Config cleanup finished");
}

static void packets_captured_count_ios(char *line, uint32_t *max, bool *running) {
	char** part;

	*max = 0;

	ws_debug("Analyzing response: %s", line);

	/* Read count of packets */
	part = g_regex_split_simple(
		"Packets :\\s*(\\d+)",
		line, G_REGEX_CASELESS, 0);
	if (*part && *(part+1)) {
		/* RE matched */
		if (strlen(*(part+1)) > 0) {
			ws_strtou32(*(part+1), NULL, max);
		}
	}
	g_strfreev(part);

	*running = false;
	if (g_regex_match_simple("Status : Active", line, (GRegexCompileFlags) (G_REGEX_CASELESS | G_REGEX_RAW), 0)) {
		*running = true;
	}
	ws_debug("Count of packets: %d", *max);
	ws_debug("Capture is running: %d", *running);
}

static void packets_captured_count_ios_xe_16(char *line, uint32_t *max, bool *running) {
	char** part;

	*max = 0;

	ws_debug("Analyzing response: %s", line);

	part = g_regex_split_simple(
		"packets in buf\\s+:\\s+(\\d+)",
		line, G_REGEX_CASELESS, 0);
	if (*part && *(part+1)) {
		/* RE matched */
		if (strlen(*(part+1)) > 0) {
			ws_strtou32(*(part+1), NULL, max);
		}
	}
	g_strfreev(part);

	*running = false;
	/* Check if capture is running */
	if (g_regex_match_simple("Status : Active", line, (GRegexCompileFlags) (G_REGEX_CASELESS | G_REGEX_RAW), 0)) {
		*running = true;
	}
	ws_debug("Count of packets: %d", *max);
	ws_debug("Capture is running: %d", *running);
}

static void packets_captured_count_asa(char *line, uint32_t *max, bool *running) {
	char** part;

	*max = 0;

	ws_debug("Analyzing response: %s", line);

	/* Read count of packets */
	part = g_regex_split_simple(
		"(\\d+) packets captured",
		line, G_REGEX_CASELESS, 0);
	if (*part && *(part+1)) {
		/* RE matched */
		if (strlen(*(part+1)) > 0) {
			ws_strtou32(*(part+1), NULL, max);
		}
	}
	g_strfreev(part);

        if (running != NULL) {
		*running = false;
		/* Check if capture is running */
		if (g_regex_match_simple("\\[Capturing -", line, (GRegexCompileFlags) (G_REGEX_CASELESS | G_REGEX_RAW), 0)) {
			*running = true;
		}
		ws_debug("Capture is running: %d", *running);
	}
	ws_debug("Count of packets: %d", *max);
}

static int parse_line_ios(uint8_t* packet, unsigned* offset, char* line, int status, time_t *pkt_time, uint32_t *pkt_usec)
{
	char** parts;
	char** part;
	uint32_t value;
	size_t size;

	if (strlen(line) <= 1) {
		if (status == CISCODUMP_PARSER_IN_PACKET)
			return CISCODUMP_PARSER_END_PACKET;
		else
			return status;
	}

/*
22:45:44.700 UTC Nov 27 2021 : IPv4 LES CEF    : Fa4.320 None

0F1A2C00:                   B0FAEBC7 A8620050          0zkG(b.P
0F1A2C10: 568494FB 08004588 004EF201 40003F11  V..{..E..Nr.@.?.
0F1A2C20: 5263AC10 1F60AC10 7F31D0DF 00A1003A  Rc,..`,..1P_.!.:
0F1A2C30: D1753030 02010104 07707269 76617465  Qu00.....private
0F1A2C40: A0220204 1A00A2E8 02010002 01003014   "...."h......0.
0F1A2C50: 3012060E 2B060104 0182CB21 01040103  0...+.....K!....
0F1A2C60: 06000500 00                          .....

22:45:44.700 UTC Nov 27 2021 : IPv4 LES CEF    : Fa4.320 None

0F1A2C00:                   B0FAEBC7 A8620050          0zkG(b.P
0F1A2C10: 568494FB 08004588 004E7FFB 40003F11  V..{..E..N.{@.?.
0F1A2C20: EB72AC10 1F60AC10 5828E872 00A1003A  kr,..`,.X(hr.!.:
0F1A2C30: 2B393030 02010104 07707269 76617465  +900.....private
0F1A2C40: A0220204 07836B18 02010002 01003014   "....k.......0.
0F1A2C50: 3012060E 2B060104 0182CB21 01040103  0...+.....K!....
0F1A2C60: 06000500 00                          .....
*/

	/* we got the packet header                                    */
	/* The packet header is a line like:                           */
	/* 16:09:37.171 ITA Mar 18 2016 : IPv4 LES CEF    : Gi0/1 None */
	parts = g_regex_split_simple(
		"^(\\d{2}:\\d{2}:\\d{2}).(\\d+) (\\w+) (\\w+ \\d+ \\d+) :",
		line, G_REGEX_CASELESS, 0);
	if (parts && *(parts+1)) {
		/* RE matched */
		char* cp;
		struct tm tm;
		/* Date without msec, with timezone */
		char* d1 = g_strdup_printf("%s %s %s", *(parts+1), *(parts+3), *(parts+4));
		/* Date without msec, without timezone */
		char* d2 = g_strdup_printf("%s %s", *(parts+1), *(parts+4));

		memset(&tm, 0x0, sizeof(struct tm));

		cp = ws_strptime_p(d1, "%H:%M:%S %Z %b %d %Y", &tm);
		if (!cp || (*cp != '\0')) {
			/* Time zone parse failed */
			cp = ws_strptime_p(d2, "%H:%M:%S %b %d %Y", &tm);
			if (!cp || (*cp != '\0')) {
				/* Time parse failed, use now */
				time_t t;
				struct tm *tm2;

				t = time(0);
				tm2 = localtime(&t);
				memcpy(&tm, tm2, sizeof(struct tm));
			}
		}
		ws_strtou32(*(parts+2), NULL, pkt_usec);
		*pkt_usec *= 1000;
		*pkt_time = mktime(&tm);

		g_strfreev(parts);
		return CISCODUMP_PARSER_IN_HEADER;
	}
	g_strfreev(parts);

	/* we got a line of the packet                                                          */
	/* A line looks like                                                                    */
	/* <address>: <1st group> <2nd group> <3rd group> <4th group> <ascii representation>    */
	/* ABCDEF01: 01020304 05060708 090A0B0C 0D0E0F10 ................                       */
	/* Note that any of the 4 groups are optional and that a group can be 1 to 4 bytes long */
	parts = g_regex_split_simple(
		"^[\\dA-F]{8,8}:\\s+([\\dA-F]{2,8})\\s+([\\dA-F]{2,8}){0,1}\\s+([\\dA-F]{2,8}){0,1}\\s+([\\dA-F]{2,8}){0,1}.*",
		line, G_REGEX_CASELESS, 0);

	part = parts;
	if (*part && *(part+1)) {
		/* There is at least one match. Skip first string */
		part++;
		while(*part) {
			/* RE matched */
			if (strlen(*part) > 1) {
				ws_hexstrtou32(*part, NULL, &value);
				value = g_ntohl(value);
				size = strlen(*part) / 2;
				memcpy(packet + *offset, &value, size);
				*offset += (uint32_t)size;
			}
			part++;
		}
	}
	g_strfreev(parts);
	return CISCODUMP_PARSER_IN_PACKET;
}

static int parse_line_ios_xe_16(uint8_t* packet, unsigned* offset, char* line)
{
	char** parts;
	char** part;
	uint32_t value;
	size_t size;

	if (strlen(line) <= 1) {
		return CISCODUMP_PARSER_END_PACKET;
	}

	/*
0
  0000:  00000C07 AC154C5D 3C259068 08004500   ......L]<%.h..E.
  0010:  00547549 40003F01 B582C0A8 4983C0A8   .TuI@.?.....I...
  0020:  46090800 B28E456E 00030000 00000000   F.....En........
  0030:  00000000 00000000 00000000 00000000   ................
  0040:  00000000 00000000 00000000 00000000   ................
  0050:  00000000 00000000 00000000 00000000   ................
  0060:  0000                                  ..

1
  0000:  4C5D3C25 9068A49B CD904C74 08004500   L]<%.h....Lt..E.
  0010:  00547549 4000FF01 F581C0A8 4609C0A8   .TuI@.......F...
  0020:  49830000 BA8E456E 00030000 00000000   I.....En........
  0030:  00000000 00000000 00000000 00000000   ................
  0040:  00000000 00000000 00000000 00000000   ................
  0050:  00000000 00000000 00000000 00000000   ................
  0060:  0000                                  ..
*/

	/* we got the packet header                                    */
	/*0*/
	if (g_regex_match_simple("^\\d+$", line, (GRegexCompileFlags) (G_REGEX_CASELESS | G_REGEX_RAW), 0)) {
		return CISCODUMP_PARSER_IN_HEADER;
	}

	/* we got a line of the packet                                                          */
	/* A line looks like                                                                    */
	/*   0000:  00000C07 AC154C5D 3C259068 08004500   ......L]<%.h..E.                      */
	/*   ...                                                                                */
	/*   0060:  0000                                  ..                                    */
	/* Note that any of the 4 groups are optional and that a group can be 1 to 8 bytes long */
	parts = g_regex_split_simple(
		"^\\s+[0-9A-F]{4,4}:  ([0-9A-F]{2,8}) ([0-9A-F]{2,8}){0,1} ([0-9A-F]{2,8}){0,1} ([0-9A-F]{2,8}){0,1}\\s+.*",
		line, G_REGEX_CASELESS, 0);

	part = parts;
	if (*part && *(part+1)) {
		/* There is at least one match. Skip first string */
		part++;
		while(*part) {
			if (strlen(*part) > 1) {
				ws_hexstrtou32(*part, NULL, &value);
				value = g_ntohl(value);
				size = strlen(*part) / 2;
				memcpy(packet + *offset, &value, size);
				*offset += (uint32_t)size;
			}
			part++;
		}
	}
	g_strfreev(parts);

	return CISCODUMP_PARSER_IN_PACKET;
}

static int parse_line_ios_xe_17(uint8_t* packet, unsigned* offset, char* line)
{
	char** parts;
	char** part;
	uint8_t value;

	if (strlen(line) <= 1) {
		return CISCODUMP_PARSER_END_PACKET;
	}

	/*
0000  6c 5e 3b 88 5e 80 6c 5e 3b 88 5e 80 08 00 45 00   l^;.^.l^;.^...E.
0010  00 28 9b 00 00 00 ff 06 0f 02 c0 a8 c8 01 c0 a8   .(..............
0020  c8 7a 7e 77 00 31 4c ba ba a5 92 d3 0a eb 50 10   .z~w.1L.......P.
0030  10 20 6a 20 00 00 00 00 00 00 00 00               . j ........
*/

	/* we got a line of the packet                                                          */
	/* A line looks like                                                                    */
	/*   0000  6c 5e 3b 88 5e 80 6c 5e 3b 88 5e 80 08 00 45 00   l^;.^.l^;.^...E            */
	/*   ...                                                                                */
	/*   0030  10 20 6a 20 00 00 00 00 00 00 00 00               . j ........               */
	/* Note that any of the 4 groups are optional and that a group can be 1 to 8 bytes long */
	parts = g_regex_split_simple(
		"^\\s*[0-9A-F]{4,4}  ([0-9A-F]{2}) ([0-9A-F]{2}){0,1} ([0-9A-F]{2}){0,1} ([0-9A-F]{2}){0,1} ([0-9A-F]{2}){0,1} ([0-9A-F]{2}){0,1} ([0-9A-F]{2}){0,1} ([0-9A-F]{2}){0,1} ([0-9A-F]{2}){0,1} ([0-9A-F]{2}){0,1} ([0-9A-F]{2}){0,1} ([0-9A-F]{2}){0,1} ([0-9A-F]{2}){0,1} ([0-9A-F]{2}){0,1} ([0-9A-F]{2}){0,1} ([0-9A-F]{2}){0,1}\\s+.*",
		line, G_REGEX_CASELESS, 0);

	part = parts;
	if (*part && *(part+1)) {
		/* There is at least one match. Skip first string */
		part++;
		while(*part) {
			if (strlen(*part) > 1) {
				ws_hexstrtou8(*part, NULL, &value);
				memcpy(packet + *offset, &value, 1);
				*offset += 1;
			}
			part++;
		}
	}
	g_strfreev(parts);

	return CISCODUMP_PARSER_IN_PACKET;
}

static int parse_line_asa(uint8_t* packet, unsigned* offset, char* line, uint32_t *current_max, time_t *pkt_time, uint32_t *pkt_usec)
{
	char** parts;
	char** part;
	uint16_t value;
	size_t size;
	uint32_t new_max;

	if (strlen(line) <= 1) {
		return CISCODUMP_PARSER_UNKNOWN;
	}

	/*
4599 packets captured

   1: 20:40:01.108469       10.124.255.212 > 10.124.255.5 icmp: echo request
0x0000 a453 0ef3 7fc0 74ad 98e5 0004 0800 4500 .S....t.......E.
0x0010 0054 dc73 4000 4001 4a63 0a7c ffd4 0a7c .T.s@.@.Jc.|...|
0x0020 ff05 0800 275b d0a4 0000 0000 0000 0000 ....'[..........
0x0030 0000 0000 0000 0000 0000 0000 0000 0000 ................
0x0040 0000 0000 0000 0000 0000 0000 0000 0000 ................
0x0050 0000 0000 0000 0000 0000 0000 0000 0000 ................
0x0060 0000                                    ..
1 packet shown
*/

	/* Update count of available packets */
	/* 4599 packets captured */
	packets_captured_count_asa(line, &new_max, NULL);
	if (new_max > 0) {
		*current_max = new_max;
		return CISCODUMP_PARSER_IN_HEADER;
	}

	/* we got the packet header                                    */
	/*   1: 20:40:01.108469       10.124.255.212 > 10.124.255.5 icmp: echo request */
	parts = g_regex_split_simple("^\\s*\\d+:\\s+(\\d+):(\\d+):(\\d+)\\.(\\d+)\\s+", line, (GRegexCompileFlags) (G_REGEX_CASELESS | G_REGEX_RAW), 0);
	if (parts && *(parts+1)) {
		/* RE matched */
		struct tm *tm;
		time_t t;

		t = time(0);
		tm = localtime(&t);
		ws_strtoi32(*(parts+1), NULL, &(tm->tm_hour));
		ws_strtoi32(*(parts+2), NULL, &(tm->tm_min));
		ws_strtoi32(*(parts+3), NULL, &(tm->tm_sec));
		ws_strtou32(*(parts+4), NULL, pkt_usec);
		*pkt_time = mktime(tm);

		g_strfreev(parts);
		return CISCODUMP_PARSER_IN_HEADER;
	}
	g_strfreev(parts);

	/* we got the packet tail */
	/* 1 packet shown         */
	if (g_regex_match_simple("^\\s*1 packet shown.*$", line, (GRegexCompileFlags) (G_REGEX_CASELESS | G_REGEX_RAW), 0)) {
		return CISCODUMP_PARSER_END_PACKET;
	}

	/* we got a line of the packet                                                          */
	/* A line looks like                                                                    */
	/* 0x<address>: <1st group> <...> <8th group> <5th group> <ascii representation>        */
	/* 0x0000 a453 0ef3 7fc0 74ad 98e5 0004 0800 4500 -.S....t.......E.                     */
	/* 0x0060 0000                                    ..                                    */
	/* Note that any of the 8 groups are optional and that a group can be 1 to 8 bytes long */
	parts = g_regex_split_simple(
		"^0x[0-9A-F]{4,4}\\s+([0-9A-F]{2,4}) ([0-9A-F]{2,4}){0,1} ([0-9A-F]{2,4}){0,1} ([0-9A-F]{2,4}){0,1} ([0-9A-F]{2,4}){0,1} ([0-9A-F]{2,4}){0,1} ([0-9A-F]{2,4}){0,1} ([0-9A-F]{2,4}){0,1}\\s+.*",
		line, G_REGEX_CASELESS, 0);

	part = parts;
	if (*part && *(part+1)) {
		/* There is at least one match. Skip first string */
		part++;
		while(*part) {
			if (strlen(*part) > 1) {
				ws_hexstrtou16(*part, NULL, &value);
				value = g_ntohs(value);
				size = strlen(*part) / 2;
				memcpy(packet + *offset, &value, size);
				*offset += (uint32_t)size;
			}
			part++;
		}
	}
	g_strfreev(parts);

	return CISCODUMP_PARSER_IN_PACKET;
}

/* IOS: Reads response and parses buffer till prompt received */
static int process_buffer_response_ios(ssh_channel channel, uint8_t* packet, FILE* fp, const uint32_t count, uint32_t *processed_packets)
{
	char line[SSH_READ_BLOCK_SIZE + 1];
	uint32_t read_packets = 1;
	int status = CISCODUMP_PARSER_STARTING;
	int loop_end = 0;
	unsigned packet_size = 0;
	time_t pkt_time = 0;
	uint32_t pkt_usec = 0;
	uint32_t len = 0;

	/* Process response */
	do {

		loop_end = 0;
		/* Read input till EOLN or prompt */
		switch (ssh_channel_read_prompt(channel, line, &len, SSH_READ_BLOCK_SIZE)) {
			case READ_PROMPT_EOLN:
				status = parse_line_ios(packet, &packet_size, line, status, &pkt_time, &pkt_usec);

				if (status == CISCODUMP_PARSER_END_PACKET) {
					ws_debug("Read packet %d\n", read_packets);
					if (read_packets > *processed_packets) {
						int err;
						uint64_t bytes_written;

						ws_debug("Exporting packet %d\n", *processed_packets);
						/*  dump the packet to the pcap file */
						if (!libpcap_write_packet(fp,
								pkt_time, pkt_usec,
								packet_size, packet_size, packet, &bytes_written, &err)) {
							ws_debug("Error in libpcap_write_packet(): %s", g_strerror(err));
							break;
						}
						fflush(fp);
						ws_debug("Dumped packet %u size: %u\n", *processed_packets, packet_size);
						(*processed_packets)++;
					}
					packet_size = 0;
					read_packets++;
				}
				break;
			case READ_PROMPT_PROMPT:
				ws_debug("Prompt found");
				loop_end = 1;
				break;
			default:
				/* We do not have better solution for that cases */
				if (extcap_end_application) {
					/* End by signal causes timeout so we decrease priority */
					ws_debug("Timeout or response was too long\n");
				} else {
					ws_warning("Timeout or response was too long\n");
				}
				return false;
		}
		len = 0;
		ws_debug("loop end detection %d %d %d %d", extcap_end_application, loop_end, *processed_packets, count);
	} while ((!extcap_end_application) && (!loop_end) && (*processed_packets < count));

	return true;
}

/* IOS: Queries buffer content and reads it */
static void ssh_loop_read_ios(ssh_channel channel, FILE* fp, const uint32_t count)
{
	char line[SSH_READ_BLOCK_SIZE + 1];
	uint8_t* packet;
	uint32_t processed_packets = 0;
	bool running = true;
	uint32_t current_max = 0;
	uint32_t new_max;

	/* This is big enough to put on the heap */
	packet = (uint8_t*)g_malloc(PACKET_MAX_SIZE);

	do {
		uint32_t len = 0;

		/* Query count of available packets in buffer */
		if (ssh_channel_printf(channel, "show monitor capture buffer %s parameters\n", WIRESHARK_CAPTURE_BUFFER) == EXIT_FAILURE) {
			g_free(packet);
			return;
		}
		if (!ssh_channel_wait_prompt_check_error(channel, line, &len, SSH_READ_BLOCK_SIZE, NULL)) {
			g_free(packet);
			return;
		}
		ws_debug("Read: %s", line);
		packets_captured_count_ios(line, &new_max, &running);
		ws_debug("Max counts %d %d", current_max, new_max);
		if (new_max == current_max) {
			/* There is no change in count of available packets, repeat the loop */
			continue;
		} else if (new_max < current_max) {
			/* Buffer was cleared, stop */
			g_free(packet);
			return;
		}
		current_max = new_max;
		ws_debug("New packet count %d\n", current_max);

		/* Dump buffer */
		if (ssh_channel_printf(channel, "show monitor capture buffer %s dump\n", WIRESHARK_CAPTURE_BUFFER) == EXIT_FAILURE) {
			g_free(packet);
			return;
		}

		/* Process buffer */
		if (!process_buffer_response_ios(channel, packet, fp, count, &processed_packets)) {
			g_free(packet);
			return;
		}
	} while (!extcap_end_application && running && (processed_packets < count));

	g_free(packet);

	/* Discard any subsequent messages */
	read_output_bytes_any(channel, -1, NULL);
}

/* IOS-XE 16: Reads response and parses buffer till prompt received */
static int process_buffer_response_ios_xe_16(ssh_channel channel, uint8_t* packet, FILE* fp, const uint32_t count, uint32_t *processed_packets)
{
	char line[SSH_READ_BLOCK_SIZE + 1];
	uint32_t read_packets = 1;
	int status = CISCODUMP_PARSER_STARTING;
	int loop_end = 0;
	unsigned packet_size = 0;
	uint32_t len = 0;

	/* Process response */
	do {
		loop_end = 0;
		/* Read input till EOLN or prompt */
		switch (ssh_channel_read_prompt(channel, line, &len, SSH_READ_BLOCK_SIZE)) {
			case READ_PROMPT_EOLN:
				status = parse_line_ios_xe_16(packet, &packet_size, line);

				if (status == CISCODUMP_PARSER_END_PACKET) {
					ws_debug("Read packet %d\n", read_packets);
					if (read_packets > *processed_packets) {
						int err;
						int64_t cur_time = g_get_real_time();
						uint64_t bytes_written = 0;

						ws_debug("Exporting packet %d\n", *processed_packets);
						/*  dump the packet to the pcap file */
						if (!libpcap_write_packet(fp,
								(uint32_t)(cur_time / G_USEC_PER_SEC), (uint32_t)(cur_time % G_USEC_PER_SEC),
								packet_size, packet_size, packet, &bytes_written, &err)) {
							ws_debug("Error in libpcap_write_packet(): %s", g_strerror(err));
							break;
						}
						fflush(fp);
						ws_debug("Dumped packet %u size: %u\n", *processed_packets, packet_size);
						(*processed_packets)++;
					}
					packet_size = 0;
					read_packets++;
				}
				break;
			case READ_PROMPT_PROMPT:
				ws_debug("Prompt found");
				loop_end = 1;
				break;
			default:
				/* We do not have better solution for that cases */
				if (extcap_end_application) {
					/* End by signal causes timeout so we decrease priority */
					ws_debug("Timeout or response was too long\n");
				} else {
					ws_warning("Timeout or response was too long\n");
				}
				return false;
		}
		len = 0;
		ws_debug("loop end detection %d %d %d %d", extcap_end_application, loop_end, *processed_packets, count);
	} while ((!extcap_end_application) && (!loop_end) && (*processed_packets < count));

	return true;
}

/* IOS-XE 17: Reads response and parses buffer till prompt received */
static int process_buffer_response_ios_xe_17(ssh_channel channel, uint8_t* packet, FILE* fp, const uint32_t count, uint32_t *processed_packets)
{
	char line[SSH_READ_BLOCK_SIZE + 1];
	uint32_t read_packets = 1;
	int status = CISCODUMP_PARSER_STARTING;
	int loop_end = 0;
	unsigned packet_size = 0;
	uint32_t len = 0;

	/* Process response */
	do {
		loop_end = 0;
		/* Read input till EOLN */
		switch (ssh_channel_read_line_timeout(channel, line, &len, SSH_READ_BLOCK_SIZE)) {
			case READ_PROMPT_EOLN:
				/* Starting message? Ignore... */
				if (NULL != g_strstr_len(line, -1, "Starting the packet display")) {
					break;
				}

				/* End of dump message? End loop. */
				if (NULL != g_strstr_len(line, -1, "Started capture point :")) {
					loop_end = 1;
					break;
				}

				status = parse_line_ios_xe_17(packet, &packet_size, line);
				ws_debug("Packet offset %d\n", packet_size);

				if ((status == CISCODUMP_PARSER_END_PACKET) && (packet_size > 0)) {
					ws_debug("Read packet %d\n", read_packets);
					if (read_packets > *processed_packets) {
						int err;
						int64_t cur_time = g_get_real_time();
						uint64_t bytes_written;

						ws_debug("Exporting packet %d\n", *processed_packets);
						/*  dump the packet to the pcap file */
						if (!libpcap_write_packet(fp,
								(uint32_t)(cur_time / G_USEC_PER_SEC), (uint32_t)(cur_time % G_USEC_PER_SEC),
								packet_size, packet_size, packet, &bytes_written, &err)) {
							ws_debug("Error in libpcap_write_packet(): %s", g_strerror(err));
							break;
						}
						fflush(fp);
						ws_debug("Dumped packet %u size: %u\n", *processed_packets, packet_size);
						(*processed_packets)++;
					}
					packet_size = 0;
					read_packets++;
				}
				break;
			case READ_LINE_TIMEOUT:
				/* Timeout is OK during reading of IOS XE 17 buffer */
				break;
			default:
				/* We do not have better solution for that cases */
				ws_warning("Error or response was too long\n");
				return false;
		}
		len = 0;
		ws_debug("loop end detection %d %d %d %d", extcap_end_application, loop_end, *processed_packets, count);
	} while ((!extcap_end_application) && (!loop_end) && (*processed_packets < count));

	return true;
}

/* IOS-XE 16: Queries buffer content and reads it */
static void ssh_loop_read_ios_xe_16(ssh_channel channel, FILE* fp, const uint32_t count)
{
	char line[SSH_READ_BLOCK_SIZE + 1];
	uint8_t* packet;
	uint32_t processed_packets = 0;
	bool running = true;
	uint32_t current_max = 0;
	uint32_t new_max;

	/* This is big enough to put on the heap */
	packet = (uint8_t*)g_malloc(PACKET_MAX_SIZE);

	do {
		uint32_t len = 0;

		/* Query count of available packets in buffer */
		if (ssh_channel_printf(channel, "show monitor capture %s buffer | inc packets in buf\nshow monitor capture %s | inc Status :\n", WIRESHARK_CAPTURE, WIRESHARK_CAPTURE) == EXIT_FAILURE) {
			g_free(packet);
			return;
		}
		if (!ssh_channel_wait_prompt_check_error(channel, line, &len, SSH_READ_BLOCK_SIZE, NULL)) {
			g_free(packet);
			return;
		}
		ws_debug("Read: %s", line);
		packets_captured_count_ios_xe_16(line, &new_max, &running);
		ws_debug("Max counts %d %d", current_max, new_max);
		if (new_max == current_max) {
			/* There is no change in count of available packets, repeat the loop */
			continue;
		} else if (new_max < current_max) {
			/* Buffer was cleared, stop */
			g_free(packet);
			return;
		}
		current_max = new_max;
		ws_debug("New packet count %d\n", current_max);

		/* Dump buffer */
		if (ssh_channel_printf(channel, "show monitor capture %s buffer dump\n", WIRESHARK_CAPTURE) == EXIT_FAILURE) {
			g_free(packet);
			return;
		}

		/* Process buffer */
		if (!process_buffer_response_ios_xe_16(channel, packet, fp, count, &processed_packets)) {
			g_free(packet);
			return;
		}
	} while (!extcap_end_application && running && (processed_packets < count));

	g_free(packet);

	/* Discard any subsequent messages */
	read_output_bytes_any(channel, -1, NULL);
}

/* IOS-XE 17: Queries buffer content and reads it */
static void ssh_loop_read_ios_xe_17(ssh_channel channel, FILE* fp, const uint32_t count)
{
	uint8_t* packet;
	uint32_t processed_packets = 0;
	bool running = true;

	/* This is big enough to put on the heap */
	packet = (uint8_t*)g_malloc(PACKET_MAX_SIZE);

	do {
		//uint32_t len = 0;

		/* Process buffer */
		if (!process_buffer_response_ios_xe_17(channel, packet, fp, count, &processed_packets)) {
			g_free(packet);
			return;
		}
		if (extcap_end_application && send_output_quit) {
			ws_debug("Terminating the output\n");
			ssh_channel_printf(channel, "\n\x1e\n");
		}
	} while (!extcap_end_application && running && (processed_packets < count));

	g_free(packet);

	/* Discard any subsequent messages */
	read_output_bytes_any(channel, -1, NULL);
}

/* ASA: Reads response and parses buffer till prompt end of packet received */
static int process_buffer_response_asa(ssh_channel channel, uint8_t* packet, FILE* fp, const uint32_t count, uint32_t *processed_packets, uint32_t *current_max)
{
	char line[SSH_READ_BLOCK_SIZE + 1];
	uint32_t read_packets = 1;
	int status = CISCODUMP_PARSER_STARTING;
	int loop_end = 0;
	unsigned packet_size = 0;

	do {
		time_t pkt_time = 0;
		uint32_t pkt_usec = 0;
		uint32_t len = 0;

		/* Dump buffer */
		if (ssh_channel_printf(channel, "show cap %s packet-number %ld dump\n", WIRESHARK_CAPTURE, (*processed_packets)+1) == EXIT_FAILURE) {
			return false;
		}

		/* Process response */
		do {
			loop_end = 0;
			/* Read input till EOLN or prompt */
			switch (ssh_channel_read_prompt(channel, line, &len, SSH_READ_BLOCK_SIZE)) {
				case READ_PROMPT_EOLN:
					status = parse_line_asa(packet, &packet_size, line, current_max, &pkt_time, &pkt_usec);

					if (status == CISCODUMP_PARSER_END_PACKET) {
						ws_debug("Read packet %d\n", read_packets);
						int err;
						uint64_t bytes_written;

						ws_debug("Exporting packet %d\n", *processed_packets);
						/*  dump the packet to the pcap file */
						if (!libpcap_write_packet(fp,
								pkt_time, pkt_usec,
								packet_size, packet_size, packet, &bytes_written, &err)) {
							ws_debug("Error in libpcap_write_packet(): %s", g_strerror(err));
							break;
						}
						fflush(fp);
						ws_debug("Dumped packet %u size: %u\n", *processed_packets, packet_size);
						(*processed_packets)++;
						packet_size = 0;
						read_packets++;
						loop_end = 1;
					}
					break;
				case READ_PROMPT_PROMPT:
					ws_debug("Prompt found");
					loop_end = 1;
					break;
				default:
					/* We do not have better solution for that cases */
					if (extcap_end_application) {
						/* End by signal causes timeout so we decrease priority */
						ws_debug("Timeout or response was too long\n");
					} else {
						ws_warning("Timeout or response was too long\n");
					}
					return false;
			}
			len = 0;
			ws_debug("loop end detection1 %d %d", *processed_packets, count);
		} while (!extcap_end_application && !loop_end);
		ws_debug("loop end detection2 %d %d %d", extcap_end_application, *processed_packets, count);
	} while (!extcap_end_application && (*processed_packets < *current_max) && ((*processed_packets < count)));

	return true;
}

/* ASA: Queries buffer content and reads it */
static void ssh_loop_read_asa(ssh_channel channel, FILE* fp, const uint32_t count)
{
	char line[SSH_READ_BLOCK_SIZE + 1];
	uint8_t* packet;
	uint32_t processed_packets = 0;
	uint32_t current_max = 0;
	bool running = true;
	uint32_t new_max;

	/* This is big enough to put on the heap */
	packet = (uint8_t*)g_malloc(PACKET_MAX_SIZE);

	do {
		uint32_t len = 0;

		/* Query count of available packets in buffer */
		if (ssh_channel_printf(channel, "show cap %s packet-number 0 | inc packets captured\nshow cap | inc %s\n", WIRESHARK_CAPTURE, WIRESHARK_CAPTURE) == EXIT_FAILURE) {
			g_free(packet);
			return;
		}
		if (!ssh_channel_wait_prompt_check_error(channel, line, &len, SSH_READ_BLOCK_SIZE, NULL)) {
			g_free(packet);
			return;
		}
		ws_debug("Read: %s", line);
		packets_captured_count_asa(line, &new_max, &running);
		ws_debug("Max counts %d %d", current_max, new_max);
		if (new_max == current_max) {
			/* There is no change in count of available packets, repeat the loop */
			continue;
		} else if (new_max < current_max) {
			/* Buffer was cleared, stop */
			g_free(packet);
			return;
		}
		current_max = new_max;
		ws_debug("New packet count %d\n", current_max);

		/* Process buffer */
		if (!process_buffer_response_asa(channel, packet, fp, count, &processed_packets, &current_max)) {
			g_free(packet);
			return;
		}
	} while (!extcap_end_application && running && (processed_packets < count));

	g_free(packet);

	/* Discard any subsequent messages */
	read_output_bytes_any(channel, -1, NULL);
}


static void ssh_loop_read(ssh_channel channel, FILE* fp, const uint32_t count _U_, CISCO_SW_TYPE sw_type)
{
	ws_debug("Starting reading loop");
	switch (sw_type) {
		case CISCO_IOS:
			ssh_loop_read_ios(channel, fp, count);
			break;
		case CISCO_IOS_XE_16:
			ssh_loop_read_ios_xe_16(channel, fp, count);
			break;
		case CISCO_IOS_XE_17:
			ssh_loop_read_ios_xe_17(channel, fp, count);
			break;
		case CISCO_ASA:
			ssh_loop_read_asa(channel, fp, count);
			break;
		case CISCO_UNKNOWN:
			break;
	}
	ws_debug("Reading loop finished");
}

static int detect_host_prompt(ssh_channel channel)
{
	char line[SSH_READ_BLOCK_SIZE + 1];
	int len = 0;
	char prompt_2[SSH_READ_BLOCK_SIZE + 1];

	/* Discard any login message */
	if (read_output_bytes(channel, -1, NULL) == EXIT_FAILURE)
		return EXIT_FAILURE;

	if (ssh_channel_printf(channel, "\n") == EXIT_FAILURE)
		return EXIT_FAILURE;

	/* Check if there is any response to empty line */
	switch (ssh_channel_read_line_timeout(channel, line, &len, SSH_READ_BLOCK_SIZE)) {
		case READ_LINE_EOLN:
			break;
		default:
			return EXIT_FAILURE;
	}

	if (ssh_channel_printf(channel, "\n") == EXIT_FAILURE)
		return EXIT_FAILURE;

	/* Read prompt_str and level char */
	switch (ssh_channel_read_line_timeout(channel, line, &len, SSH_READ_BLOCK_SIZE)) {
		case READ_LINE_EOLN:
			break;
		default:
			return EXIT_FAILURE;
	}
	if (len > 0) {
		g_strlcpy(prompt_str, line, SSH_READ_BLOCK_SIZE + 1);

		/* Is there hashtag at the end => enabled mode? */
		if (prompt_str[strlen(prompt_str)-1] != '#') {
			/* Is there hashtag and space (ASA) at the end => enabled mode? */
			if ((prompt_str[strlen(prompt_str)-2] != '#') || (prompt_str[strlen(prompt_str)-1] != ' ')) {
				return EXIT_FAILURE;
			}
		}
		prompt_len = (int32_t)strlen(prompt_str);
	} else {
		return EXIT_FAILURE;
        }

	if (ssh_channel_printf(channel, "\n") == EXIT_FAILURE)
		return EXIT_FAILURE;

	/* Read prompt_str and level char again */
	switch (ssh_channel_read_line_timeout(channel, line, &len, SSH_READ_BLOCK_SIZE)) {
		case READ_LINE_EOLN:
			break;
		default:
			return EXIT_FAILURE;
	}
	if (len > 0) {
		g_strlcpy(prompt_2, line, SSH_READ_BLOCK_SIZE + 1);
		/* Does second prompt_str match first one? */
		if (0 == g_strcmp0(prompt_str, prompt_2)) {
			ws_debug("Detected prompt %s", prompt_str);
			return true;
		}
	}

	return EXIT_FAILURE;
}

static int check_ios_version(ssh_channel channel, CISCO_SW_TYPE *sw_type)
{
	char* cmdline_version = "show version | include Version\n";
	const char* msg_ios = "Cisco IOS Software";
	const char* msg_ios_xe = "Cisco IOS XE Software";
	const char* msg_asa = "Cisco Adaptive Security Appliance Software";
	const char* msg_version = "Version ";
	char version[255];
	int sw_major = 0;
	int sw_minor = 0;
	char* cur;

	memset(version, 0x0, 255);

	/* Discard any login message */
	if (read_output_bytes(channel, -1, NULL) == EXIT_FAILURE)
		return false;

	if (ssh_channel_write(channel, cmdline_version, (uint32_t)strlen(cmdline_version)) == SSH_ERROR)
		return false;
	if (read_output_bytes(channel, 255, version) == EXIT_FAILURE)
		return false;

	/* Discard any subsequent text */
	if (read_output_bytes(channel, -1, NULL) == EXIT_FAILURE)
		return false;

	/* We should check IOS XE first as its version contains IOS string too */
	cur = g_strstr_len(version, strlen(version), msg_ios_xe);
	if (cur) {
		*sw_type = CISCO_IOS_XE_16;
		cur += strlen(msg_ios_xe);
	} else {
		cur = g_strstr_len(version, strlen(version), msg_ios);
		if (cur) {
			*sw_type = CISCO_IOS;
			cur += strlen(msg_ios);
		} else {
			cur = g_strstr_len(version, strlen(version), msg_asa);
			if (cur) {
				*sw_type = CISCO_ASA;
			cur += strlen(msg_asa);
			}
		}
	}

	if (*sw_type != CISCO_UNKNOWN && cur) {
		cur = g_strstr_len(cur, 255-strlen(cur), msg_version);
		if (cur) {
			cur += strlen(msg_version);
			if (sscanf(cur, "%u.%u", &sw_major, &sw_minor) != 2)
				return false;

			switch (*sw_type) {
				case CISCO_IOS:
					ws_debug("Current IOS version: %u.%u", sw_major, sw_minor);
					if ((sw_major > MINIMUM_IOS_MAJOR) || (sw_major == MINIMUM_IOS_MAJOR && sw_minor >= MINIMUM_IOS_MINOR)) {
						return true;
					}
					break;
				case CISCO_IOS_XE_16:
					ws_debug("Current IOS XE version: %u.%u", sw_major, sw_minor);
					if ((sw_major > MINIMUM_IOS_XE_MAJOR_17) || (sw_major == MINIMUM_IOS_XE_MAJOR_17 && sw_minor >= MINIMUM_IOS_XE_MINOR_17)) {
						*sw_type = CISCO_IOS_XE_17;
						return true;
					}
					if ((sw_major > MINIMUM_IOS_XE_MAJOR_16) || (sw_major == MINIMUM_IOS_XE_MAJOR_16 && sw_minor >= MINIMUM_IOS_XE_MINOR_16)) {
						*sw_type = CISCO_IOS_XE_16;
						return true;
					}
					break;
				case CISCO_ASA:
					ws_debug("Current ASA version: %u.%u", sw_major, sw_minor);
					if ((sw_major > MINIMUM_ASA_MAJOR) || (sw_major == MINIMUM_ASA_MAJOR && sw_minor >= MINIMUM_ASA_MINOR)) {
						return true;
					}
					break;
				default:
					return false;
			}
			ws_warning("Recognized software type, but minimal version requirements were not met\n");
			return false;
		} else {
			ws_warning("Recognized software type %d, but unrecognized version\n", *sw_type);
		}
	} else {
		ws_warning("Unrecognized type of control software.");
	}

	return false;
}

static bool run_capture_ios(ssh_channel channel, const char* iface, const char* cfilter, const uint32_t count)
{
	char* cmdline = NULL;
	int ret = 0;
	char line[SSH_READ_BLOCK_SIZE + 1];
	uint32_t len;
	char* iface_copy = g_strdup(iface);
	char* iface_one;
	char* str = NULL;
	int wscp_cnt = 1;
	char* wscp_str = NULL;

	if (ssh_channel_printf(channel, "terminal length 0\n") == EXIT_FAILURE)
		goto error;

	if (!ssh_channel_wait_prompt_check_error(channel, line, &len, SSH_READ_BLOCK_SIZE, NULL)) {
		ws_warning("Received response: %s", crtoln(line));
		goto error;
	}

	if (ssh_channel_printf(channel, "monitor capture buffer %s max-size 9500\n", WIRESHARK_CAPTURE_BUFFER) == EXIT_FAILURE)
		goto error;

	if (!ssh_channel_wait_prompt_check_error(channel, line, &len, SSH_READ_BLOCK_SIZE, NULL)) {
		ws_warning("Received response: %s", crtoln(line));
		goto error;
	}

	if (count > 0) {
		if (ssh_channel_printf(channel, "monitor capture buffer %s limit packet-count %u\n", WIRESHARK_CAPTURE_BUFFER, count) == EXIT_FAILURE)
			goto error;

		if (!ssh_channel_wait_prompt_check_error(channel, line, &len, SSH_READ_BLOCK_SIZE, NULL)) {
			ws_warning("Received response: %s", crtoln(line));
			goto error;
		}
        }

	if (cfilter) {
		char* multiline_filter;
		char* chr;

		if (ssh_channel_printf(channel, "configure terminal\n") == EXIT_FAILURE)
			goto error;

		if (ssh_channel_printf(channel, "ip access-list extended %s\n", WIRESHARK_CAPTURE_ACCESSLIST) == EXIT_FAILURE)
			goto error;

		multiline_filter = g_strdup(cfilter);
		chr = multiline_filter;
		while((chr = g_strstr_len(chr, strlen(chr), ",")) != NULL) {
			chr[0] = '\n';
			ws_debug("Splitting filter into multiline");
		}
		ret = ssh_channel_write(channel, multiline_filter, (uint32_t)strlen(multiline_filter));
		g_free(multiline_filter);
		if (ret == SSH_ERROR)
			goto error;

		if (ssh_channel_printf(channel, "\nend\n") == EXIT_FAILURE)
			goto error;

		if (!ssh_channel_wait_prompt_check_error(channel, line, &len, SSH_READ_BLOCK_SIZE, NULL)) {
			ws_warning("Received response: %s", crtoln(line));
			goto error;
		}

		if (ssh_channel_printf(channel, "monitor capture buffer %s filter access-list %s\n",
				WIRESHARK_CAPTURE_BUFFER, WIRESHARK_CAPTURE_ACCESSLIST) == EXIT_FAILURE)
			goto error;

		if (!ssh_channel_wait_prompt_check_error(channel, line, &len, SSH_READ_BLOCK_SIZE, NULL)) {
			ws_warning("Received response: %s", crtoln(line));
			goto error;
		}
	}

	wscp_cnt = 1;
	for (str = iface_copy; ; str = NULL) {
		iface_one = strtok(str, ",");
		if (iface_one == NULL)
			break;

		wscp_str = g_strdup_printf("%s_%d", WIRESHARK_CAPTURE_POINT, wscp_cnt);
		wscp_cnt++;

		if (0 == g_strcmp0(iface_one, "process-switched")) {
			cmdline = g_strdup_printf("monitor capture point ip process-switched %s both", wscp_str);
		} else if (0 == g_strcmp0(iface_one, "from-us")) {
			cmdline = g_strdup_printf("monitor capture point ip process-switched %s from-us", wscp_str);
		} else {
			cmdline = g_strdup_printf("monitor capture point ip cef %s %s both", wscp_str, iface_one);
		}

		if (ssh_channel_printf(channel, "%s\n", cmdline) == EXIT_FAILURE)
			goto error;

		if (!ssh_channel_wait_prompt_check_error(channel, line, &len, SSH_READ_BLOCK_SIZE, NULL)) {
			ws_warning("Received response: %s", crtoln(line));
			goto error;
		}

		if (ssh_channel_printf(channel, "monitor capture point associate %s %s \n", wscp_str,
				WIRESHARK_CAPTURE_BUFFER) == EXIT_FAILURE)
			goto error;

		if (!ssh_channel_wait_prompt_check_error(channel, line, &len, SSH_READ_BLOCK_SIZE, NULL)) {
			ws_warning("Received response: %s", crtoln(line));
			goto error;
		}

		g_free(cmdline);
		cmdline = NULL;
	}

	wscp_cnt = 1;
	for (str = iface_copy; ; str = NULL) {
		iface_one = strtok(str, ",");
		if (iface_one == NULL)
			break;

		wscp_str = g_strdup_printf("%s_%d", WIRESHARK_CAPTURE_POINT, wscp_cnt);
		wscp_cnt++;

		if (ssh_channel_printf(channel, "monitor capture point start %s\n", wscp_str) == EXIT_FAILURE)
			goto error;

		if (!ssh_channel_wait_prompt_check_error(channel, line, &len, SSH_READ_BLOCK_SIZE, NULL)) {
			ws_warning("Received response: %s", crtoln(line));
			goto error;
		}

		g_free(wscp_str);
		wscp_str = NULL;
	}

	g_free(iface_copy);
	return true;
error:
	g_free(wscp_str);
	g_free(iface_copy);
	g_free(cmdline);
	ws_warning("Error running ssh remote command");

	ssh_channel_close(channel);
	ssh_channel_free(channel);
	return false;
}

static bool run_capture_ios_xe_16(ssh_channel channel, const char* iface, const char* cfilter, const uint32_t count)
{
	int ret = 0;
	char line[SSH_READ_BLOCK_SIZE + 1];
	uint32_t len;
	char* iface_copy = g_strdup(iface);
	char* iface_one;
	char* str = NULL;

	if (ssh_channel_printf(channel, "terminal length 0\n") == EXIT_FAILURE)
		goto error;

	if (!ssh_channel_wait_prompt_check_error(channel, line, &len, SSH_READ_BLOCK_SIZE, NULL)) {
		ws_warning("Received response: %s", crtoln(line));
		goto error;
	}

	if (ssh_channel_printf(channel, "monitor capture %s limit packet-len 9500\n", WIRESHARK_CAPTURE) == EXIT_FAILURE)
		goto error;

	if (!ssh_channel_wait_prompt_check_error(channel, line, &len, SSH_READ_BLOCK_SIZE, NULL)) {
		ws_warning("Received response: %s", crtoln(line));
		goto error;
	}

	if (count > 0) {
		if (ssh_channel_printf(channel, "monitor capture %s limit packets %u\n", WIRESHARK_CAPTURE, count) == EXIT_FAILURE)
			goto error;

		if (!ssh_channel_wait_prompt_check_error(channel, line, &len, SSH_READ_BLOCK_SIZE, NULL)) {
			ws_warning("Received response: %s", crtoln(line));
			goto error;
		}
	}

	if (cfilter) {
		char* multiline_filter;
		char* chr;

		if (ssh_channel_printf(channel, "configure terminal\n") == EXIT_FAILURE)
			goto error;

		if (ssh_channel_printf(channel, "ip access-list extended %s\n", WIRESHARK_CAPTURE_ACCESSLIST) == EXIT_FAILURE)
			goto error;

		multiline_filter = g_strdup(cfilter);
		chr = multiline_filter;
		while((chr = g_strstr_len(chr, strlen(chr), ",")) != NULL) {
			chr[0] = '\n';
			ws_debug("Splitting filter into multiline");
		}
		ret = ssh_channel_write(channel, multiline_filter, (uint32_t)strlen(multiline_filter));
		g_free(multiline_filter);
		if (ret == SSH_ERROR)
			goto error;

		if (ssh_channel_printf(channel, "\nend\n") == EXIT_FAILURE)
			goto error;

		if (!ssh_channel_wait_prompt_check_error(channel, line, &len, SSH_READ_BLOCK_SIZE, NULL)) {
			ws_warning("Received response: %s", crtoln(line));
			goto error;
		}

		if (ssh_channel_printf(channel, "monitor capture %s access-list %s\n",
				WIRESHARK_CAPTURE, WIRESHARK_CAPTURE_ACCESSLIST) == EXIT_FAILURE)
			goto error;
	} else {
		if (ssh_channel_printf(channel, "monitor capture %s match any\n",
				WIRESHARK_CAPTURE) == EXIT_FAILURE)
			goto error;
	}

	if (!ssh_channel_wait_prompt_check_error(channel, line, &len, SSH_READ_BLOCK_SIZE, NULL)) {
		ws_warning("Received response: %s", crtoln(line));
		goto error;
	}

	for (str = iface_copy; ; str = NULL) {
		iface_one = strtok(str, ",");
		if (iface_one == NULL)
			break;

		if (0 == g_strcmp0(iface_one, "control-plane")) {
			if (ssh_channel_printf(channel, "monitor capture %s control-plane both\n", WIRESHARK_CAPTURE
					) == EXIT_FAILURE)
				goto error;
		} else {
			if (ssh_channel_printf(channel, "monitor capture %s interface %s both\n", WIRESHARK_CAPTURE,
					iface_one) == EXIT_FAILURE)
				goto error;
		}
	}

	if (!ssh_channel_wait_prompt_check_error(channel, line, &len, SSH_READ_BLOCK_SIZE, NULL)) {
		ws_warning("Received response: %s", crtoln(line));
		goto error;
	}

	if (ssh_channel_printf(channel, "monitor capture %s start\n", WIRESHARK_CAPTURE) == EXIT_FAILURE)
		goto error;

	if (!ssh_channel_wait_prompt_check_error(channel, line, &len, SSH_READ_BLOCK_SIZE,
	    "(Capture is not Supported|Unable to activate Capture)")
	   ) {
		ws_warning("Received response: %s", crtoln(line));
		goto error;
	}

	g_free(iface_copy);
	return true;
error:
	g_free(iface_copy);
	ws_warning("Error running ssh remote command");

	ssh_channel_close(channel);
	ssh_channel_free(channel);
	return false;
}

static bool run_capture_ios_xe_17(ssh_channel channel, const char* iface, const char* cfilter, const uint32_t count)
{
	int ret = 0;
	char line[SSH_READ_BLOCK_SIZE + 1];
	uint32_t len;
	char* iface_copy = g_strdup(iface);
	char* iface_one;
	char* str = NULL;

	if (ssh_channel_printf(channel, "terminal length 0\n") == EXIT_FAILURE)
		goto error;

	if (!ssh_channel_wait_prompt_check_error(channel, line, &len, SSH_READ_BLOCK_SIZE, NULL)) {
		ws_warning("Received response: %s", crtoln(line));
		goto error;
	}

	if (ssh_channel_printf(channel, "monitor capture %s limit packet-len 9500\n", WIRESHARK_CAPTURE) == EXIT_FAILURE)
		goto error;

	if (!ssh_channel_wait_prompt_check_error(channel, line, &len, SSH_READ_BLOCK_SIZE, NULL)) {
		ws_warning("Received response: %s", crtoln(line));
		goto error;
	}

	if (count > 0) {
		if (ssh_channel_printf(channel, "monitor capture %s limit packets %u\n", WIRESHARK_CAPTURE, count) == EXIT_FAILURE)
			goto error;

		if (!ssh_channel_wait_prompt_check_error(channel, line, &len, SSH_READ_BLOCK_SIZE, NULL)) {
			ws_warning("Received response: %s", crtoln(line));
			goto error;
		}
	}

	if (cfilter) {
		char* multiline_filter;
		char* chr;

		if (ssh_channel_printf(channel, "configure terminal\n") == EXIT_FAILURE)
			goto error;

		if (ssh_channel_printf(channel, "ip access-list extended %s\n", WIRESHARK_CAPTURE_ACCESSLIST) == EXIT_FAILURE)
			goto error;

		multiline_filter = g_strdup(cfilter);
		chr = multiline_filter;
		while((chr = g_strstr_len(chr, strlen(chr), ",")) != NULL) {
			chr[0] = '\n';
			ws_debug("Splitting filter into multiline");
		}
		ret = ssh_channel_write(channel, multiline_filter, (uint32_t)strlen(multiline_filter));
		g_free(multiline_filter);
		if (ret == SSH_ERROR)
			goto error;

		if (ssh_channel_printf(channel, "\nend\n") == EXIT_FAILURE)
			goto error;

		if (!ssh_channel_wait_prompt_check_error(channel, line, &len, SSH_READ_BLOCK_SIZE, NULL)) {
			ws_warning("Received response: %s", crtoln(line));
			goto error;
		}

		if (ssh_channel_printf(channel, "monitor capture %s access-list %s\n",
				WIRESHARK_CAPTURE, WIRESHARK_CAPTURE_ACCESSLIST) == EXIT_FAILURE)
			goto error;
	} else {
		if (ssh_channel_printf(channel, "monitor capture %s match any\n",
				WIRESHARK_CAPTURE) == EXIT_FAILURE)
			goto error;
	}

	if (!ssh_channel_wait_prompt_check_error(channel, line, &len, SSH_READ_BLOCK_SIZE, NULL)) {
		ws_warning("Received response: %s", crtoln(line));
		goto error;
	}

	for (str = iface_copy; ; str = NULL) {
		iface_one = strtok(str, ",");
		if (iface_one == NULL)
			break;

		if (0 == g_strcmp0(iface_one, "control-plane")) {
			if (ssh_channel_printf(channel, "monitor capture %s control-plane both\n", WIRESHARK_CAPTURE
					) == EXIT_FAILURE)
				goto error;
		} else {
			if (ssh_channel_printf(channel, "monitor capture %s interface %s both\n", WIRESHARK_CAPTURE,
					iface_one) == EXIT_FAILURE)
				goto error;
		}
	}

	if (!ssh_channel_wait_prompt_check_error(channel, line, &len, SSH_READ_BLOCK_SIZE, NULL)) {
		ws_warning("Received response: %s", crtoln(line));
		goto error;
	}

	if (ssh_channel_printf(channel, "monitor capture %s start display dump\n", WIRESHARK_CAPTURE) == EXIT_FAILURE)
		goto error;

/*
	if (!ssh_channel_wait_prompt_check_error(channel, line, &len, SSH_READ_BLOCK_SIZE,
	    "(Capture is not Supported|Unable to activate Capture)")
	   ) {
		ws_warning("Received response: %s", crtoln(line));
		goto error;
	}
*/

	g_free(iface_copy);
	return true;
error:
	g_free(iface_copy);
	ws_warning("Error running ssh remote command");

	ssh_channel_close(channel);
	ssh_channel_free(channel);
	return false;
}

static bool run_capture_asa(ssh_channel channel, const char* iface, const char* cfilter)
{
	char* cmdline = NULL;
	char line[SSH_READ_BLOCK_SIZE + 1];
	uint32_t len;
	char *sep;
	bool process_filter = true;
	char* iface_copy = g_strdup(iface);
	char* iface_one;
	char* str = NULL;

	if (ssh_channel_printf(channel, "terminal pager 0\n") == EXIT_FAILURE)
		goto error;

	if (!ssh_channel_wait_prompt_check_error(channel, line, &len, SSH_READ_BLOCK_SIZE, NULL)) {
		ws_warning("Received response: %s", crtoln(line));
		goto error;
	}

	for (str = iface_copy; ; str = NULL) {
		iface_one = strtok(str, ",");
		if (iface_one == NULL)
			break;

		if (0 == g_strcmp0(iface_one, "asp-drop")) {
			/* asp-drop: asp-drop: capture %s type asp-drop all packet-length 9216 !INCLUDE-DECRYPTED
			 */
			cmdline = g_strdup_printf("capture %s type asp-drop all packet-length 9216", WIRESHARK_CAPTURE);
		} else if (NULL != (sep = g_strstr_len(iface_one, -1, "---"))) {
			/* Interface type separator found. We support:
			 * isakmp---ifname: capture %s type isakmp packet-length 32810 interface %s
			 * // webvpn---ifname: capture %s type webvpn user %s !NO FILTER !INCLUDE-DECRYPTED
			 * lacp---ifname: capture %s type lacp interface %s packet-length 9216 !NO FILTER !INCLUDE-DECRYPTED
			 * tls-proxy---ifname: capture %s type tls-proxy packet-length 9216 interface %s
			 * inline-tag---ifname: capture %s type inline-tag packet-length 9216 interface %s
			 * raw-data---ifname: capture %s type rawdata packet-length 9216 interface %s
			 *
			 * We support /decrypted for some of it:
			 * isakmp/decrypted---ifname
			 * tls-proxy/decrypted---ifname
			 * inline-tag/decrypted---ifname
			 * raw-data/decrypted---ifname
			 */
			char* ifname = sep+3;

			if (strstr(iface_one,  "isakmp")) {
				if (strstr(iface_one,  "/decrypted")) {
					cmdline = g_strdup_printf("capture %s type isakmp include-decrypted packet-length 32810 interface %s", WIRESHARK_CAPTURE, ifname);
				} else {
					cmdline = g_strdup_printf("capture %s type isakmp packet-length 32810 interface %s", WIRESHARK_CAPTURE, ifname);
				}
				/* Completelly different output
				} else if (strstr(iface_one,  "webvpn")) {
					cmdline = g_strdup_printf("capture %s type webvpn user %s", WIRESHARK_CAPTURE, ifname);
					process_filter = false;
				*/
			} else if (strstr(iface_one,  "lacp")) {
				cmdline = g_strdup_printf("capture %s type lacp interface %s packet-length 9216", WIRESHARK_CAPTURE, ifname);
				process_filter = false;
			} else if (strstr(iface_one,  "tls-proxy")) {
				if (strstr(iface_one,  "/decrypted")) {
					cmdline = g_strdup_printf("capture %s type tls-proxy include-decrypted packet-length 9216 interface %s", WIRESHARK_CAPTURE, ifname);
				} else {
					cmdline = g_strdup_printf("capture %s type tls-proxy packet-length 9216 interface %s", WIRESHARK_CAPTURE, ifname);
				}
			} else if (strstr(iface_one,  "inline-tag")) {
				if (strstr(iface_one,  "/decrypted")) {
					cmdline = g_strdup_printf("capture %s type inline-tag include-decrypted packet-length 9216 interface %s", WIRESHARK_CAPTURE, ifname);
				} else {
					cmdline = g_strdup_printf("capture %s type inline-tag packet-length 9216 interface %s", WIRESHARK_CAPTURE, ifname);
				}
			} else if (strstr(iface_one,  "raw-data")) {
				if (strstr(iface_one,  "/decrypted")) {
					cmdline = g_strdup_printf("capture %s type raw-data include-decrypted packet-length 9216 interface %s", WIRESHARK_CAPTURE, ifname);
				} else {
					cmdline = g_strdup_printf("capture %s type raw-data packet-length 9216 interface %s", WIRESHARK_CAPTURE, ifname);
				}
			} else {
				ws_warning("Unknown interface type : %s", iface_one);
				goto error;
			}
		} else {
			/* Just interface name */
			cmdline = g_strdup_printf("capture %s type raw-data packet-length 9216 interface %s", WIRESHARK_CAPTURE, iface_one);
		}

		if (ssh_channel_printf(channel, "%s\n", cmdline) == EXIT_FAILURE)
			goto error;

		if (!ssh_channel_wait_prompt_check_error(channel, line, &len, SSH_READ_BLOCK_SIZE, NULL)) {
			ws_warning("Received response: %s", crtoln(line));
			goto error;
		}

		g_free(cmdline);
		cmdline = NULL;
	}

	if (process_filter && cfilter) {
		char* multiline_filter;
		char* chr;
		char* start;

		if (ssh_channel_printf(channel, "configure terminal\n") == EXIT_FAILURE)
			goto error;

		multiline_filter = g_strdup(cfilter);
		start = multiline_filter;
		while((chr = g_strstr_len(start, strlen(start), ",")) != NULL) {
			chr[0] = '\0';
			ws_debug("Splitting filter into multiline");
			if (ssh_channel_printf(channel, "access-list %s %s\n", WIRESHARK_CAPTURE_ACCESSLIST, start) == EXIT_FAILURE)
				goto error;
			start = chr+1;
		}

		if (ssh_channel_printf(channel, "access-list %s %s\n", WIRESHARK_CAPTURE_ACCESSLIST, start) == EXIT_FAILURE)
			goto error;

		if (ssh_channel_printf(channel, "\nend\n") == EXIT_FAILURE)
			goto error;

		if (!ssh_channel_wait_prompt_check_error(channel, line, &len, SSH_READ_BLOCK_SIZE, NULL)) {
			ws_warning("Received response: %s", crtoln(line));
			goto error;
		}

		if (ssh_channel_printf(channel, "capture %s access-list %s\n", WIRESHARK_CAPTURE, WIRESHARK_CAPTURE_ACCESSLIST) == EXIT_FAILURE)
			goto error;

		if (!ssh_channel_wait_prompt_check_error(channel, line, &len, SSH_READ_BLOCK_SIZE, NULL)) {
			ws_warning("Received response: %s", crtoln(line));
			goto error;
		}
	}

	g_free(iface_copy);
	return true;
error:
	g_free(iface_copy);
	g_free(cmdline);
	ws_warning("Error running ssh remote command");

	ssh_channel_close(channel);
	ssh_channel_free(channel);
	return false;
}

static ssh_channel open_channel(ssh_session sshs)
{
	ssh_channel channel;

	channel = ssh_channel_new(sshs);
	if (!channel)
		return NULL;

	if (ssh_channel_open_session(channel) != SSH_OK)
		goto error;

	if (ssh_channel_request_pty(channel) != SSH_OK)
		goto error;

	if (ssh_channel_change_pty_size(channel, 80, 24) != SSH_OK)
		goto error;

	if (ssh_channel_request_shell(channel) != SSH_OK)
		goto error;

	return channel;

error:
	ws_warning("Error running ssh remote command");

	ssh_channel_close(channel);
	ssh_channel_free(channel);
	return NULL;
}

static bool run_capture(ssh_channel channel, const char* iface, const char* cfilter, const uint32_t count, CISCO_SW_TYPE sw_type)
{
	switch (sw_type) {
		case CISCO_IOS:
			return run_capture_ios(channel, iface, cfilter, count);
		case CISCO_IOS_XE_16:
			return run_capture_ios_xe_16(channel, iface, cfilter, count);
		case CISCO_IOS_XE_17:
			return run_capture_ios_xe_17(channel, iface, cfilter, count);
		case CISCO_ASA:
			return run_capture_asa(channel, iface, cfilter);
		case CISCO_UNKNOWN:
			ws_warning("Unsupported cisco software. It will not collect any data most probably!");
			return false;
	}

	return false;
}

static int ssh_open_remote_connection(const ssh_params_t* ssh_params, const char* iface, const char* cfilter,
	const uint32_t count, const char* fifo)
{
	ssh_session sshs;
	ssh_channel channel;
	FILE* fp = stdout;
	uint64_t bytes_written = 0;
	int err;
	int ret = EXIT_FAILURE;
	char* err_info = NULL;

	if (g_strcmp0(fifo, "-")) {
		/* Open or create the output file */
		fp = fopen(fifo, "wb");
		if (!fp) {
			ws_warning("Error creating output file: %s", g_strerror(errno));
			return EXIT_FAILURE;
		}
	}

	if (!libpcap_write_file_header(fp, 1, PCAP_SNAPLEN, false, &bytes_written, &err)) {
		ws_warning("Can't write pcap file header");
		goto cleanup;
	}

	fflush(fp);

	ws_debug("Create first ssh session");
	sshs = create_ssh_connection(ssh_params, &err_info);
	if (!sshs) {
		ws_warning("Error creating connection: %s", err_info);
		goto cleanup;
	}

	channel = open_channel(sshs);
	if (!channel) {
		ret = EXIT_FAILURE;
		goto cleanup;
	}

	if (!detect_host_prompt(channel))
		goto cleanup;

	if (!check_ios_version(channel, &global_sw_type))
		goto cleanup;

	/* clean up and exit */
	ciscodump_cleanup(channel, iface, cfilter, global_sw_type);

	if (!run_capture(channel, iface, cfilter, count, global_sw_type)) {
		ret = EXIT_FAILURE;
		goto cleanup;
	}

	/* read from channel and write into fp */
	ssh_loop_read(channel, fp, count, global_sw_type);

	/* Read loop can be terminated by signal or QUIT command in
	 * mid of long "show" command and its reading can take really
	 * long time. So we terminate ssh session and then
	 * create new one to cleanup configuration
	 */
	ws_debug("Disconnect first ssh session");
	ssh_channel_close(channel);
	ssh_channel_free(channel);
	ssh_cleanup(&sshs, &channel);

	ws_debug("Create second ssh session");
	sshs = create_ssh_connection(ssh_params, &err_info);
	if (!sshs) {
		ws_warning("Error creating connection: %s", err_info);
		goto cleanup;
	}

	channel = open_channel(sshs);
	if (!channel) {
		ret = EXIT_FAILURE;
		goto cleanup;
	}

	/* clean up and exit */
	ciscodump_cleanup(channel, iface, cfilter, global_sw_type);

	ssh_channel_close(channel);
	ssh_channel_free(channel);
	ssh_cleanup(&sshs, &channel);

	ret = EXIT_SUCCESS;
cleanup:
	if (fp != stdout)
		fclose(fp);

	return ret;
}

static int list_config(char *interface, unsigned int remote_port)
{
	unsigned inc = 0;
	char* ipfilter;

	if (!interface) {
		ws_warning("No interface specified.");
		return EXIT_FAILURE;
	}

	if (g_strcmp0(interface, CISCODUMP_EXTCAP_INTERFACE)) {
		ws_warning("interface must be %s", CISCODUMP_EXTCAP_INTERFACE);
		return EXIT_FAILURE;
	}

	ipfilter = local_interfaces_to_filter(remote_port);

	printf("arg {number=%u}{call=--remote-host}{display=Remote SSH server address}"
		"{type=string}{tooltip=The remote SSH host. It can be both "
		"an IP address or a hostname}{required=true}{group=Server}\n", inc++);
	printf("arg {number=%u}{call=--remote-port}{display=Remote SSH server port}"
		"{type=unsigned}{default=22}{tooltip=The remote SSH host port (1-65535)}"
		"{range=1,65535}{group=Server}\n", inc++);
	printf("arg {number=%u}{call=--remote-username}{display=Remote SSH server username}"
		"{type=string}{default=%s}{tooltip=The remote SSH username. If not provided, "
		"the current user will be used}{group=Authentication}\n", inc++, g_get_user_name());
	printf("arg {number=%u}{call=--remote-password}{display=Remote SSH server password}"
		"{type=password}{tooltip=The SSH password, used when other methods (SSH agent "
		"or key files) are unavailable.}{group=Authentication}\n", inc++);
	printf("arg {number=%u}{call=--sshkey}{display=Path to SSH private key}"
		"{type=fileselect}{tooltip=The path on the local filesystem of the private ssh key}"
		"{group=Authentication}\n", inc++);
	printf("arg {number=%u}{call=--proxycommand}{display=ProxyCommand}"
		"{type=string}{tooltip=The command to use as proxy for the SSH connection}"
		"{group=Authentication}\n", inc++);
	printf("arg {number=%u}{call--sshkey-passphrase}{display=SSH key passphrase}"
		"{type=password}{tooltip=Passphrase to unlock the SSH private key}"
		"{group=Authentication\n", inc++);
	printf("arg {number=%u}{call=--ssh-sha1}{display=Support SHA-1 keys (deprecated)}"
	       "{type=boolflag}{tooltip=Support keys and key exchange algorithms using SHA-1 (deprecated)}{group=Authentication}"
	       "\n", inc++);
	printf("arg {number=%u}{call=--remote-interface}{display=Remote interface}"
		"{type=string}{required=true}{tooltip=The remote network interface used for capture"
		"}{group=Capture}\n", inc++);
	printf("arg {number=%u}{call=--remote-filter}{display=Remote capture filter}"
		"{type=string}{tooltip=The remote capture filter}", inc++);
	if (ipfilter)
		printf("{default=%s}", ipfilter);
	printf("{group=Capture}\n");
	printf("arg {number=%u}{call=--remote-count}{display=Packets to capture}"
		"{type=unsigned}{required=true}{tooltip=The number of remote packets to capture.}"
		"{group=Capture}\n", inc++);

	extcap_config_debug(&inc);

	g_free(ipfilter);

	return EXIT_SUCCESS;
}

int main(int argc, char *argv[])
{
	char* err_msg;
	int result;
	int option_idx = 0;
	ssh_params_t* ssh_params = ssh_params_new();
	char* remote_interface = NULL;
	char* remote_filter = NULL;
	uint32_t count = 0;
	int ret = EXIT_FAILURE;
	extcap_parameters * extcap_conf = g_new0(extcap_parameters, 1);
	char* help_url;
	char* help_header = NULL;

	/* Initialize log handler early so we can have proper logging during startup. */
	extcap_log_init("ciscodump");

	/*
	 * Get credential information for later use.
	 */
	init_process_policies();

	/*
	 * Attempt to get the pathname of the directory containing the
	 * executable file.
	 */
	err_msg = configuration_init(argv[0], NULL);
	if (err_msg != NULL) {
		ws_warning("Can't get pathname of directory containing the extcap program: %s.",
			err_msg);
		g_free(err_msg);
	}

	help_url = data_file_url("ciscodump.html");
	extcap_base_set_util_info(extcap_conf, argv[0], CISCODUMP_VERSION_MAJOR, CISCODUMP_VERSION_MINOR,
		CISCODUMP_VERSION_RELEASE, help_url);
	add_libssh_info(extcap_conf);
	g_free(help_url);
	extcap_base_register_interface(extcap_conf, CISCODUMP_EXTCAP_INTERFACE, "Cisco remote capture", 147, "Remote capture dependent DLT");
	if (!extcap_base_register_graceful_shutdown_cb(extcap_conf, graceful_shutdown_cb)) {
		ret = EXIT_FAILURE;
		goto end;
	}

	help_header = ws_strdup_printf(
		" %s --extcap-interfaces\n"
		" %s --extcap-interface=%s --extcap-dlts\n"
		" %s --extcap-interface=%s --extcap-config\n"
		" %s --extcap-interface=%s --remote-host myhost --remote-port 22222 "
		"--remote-username myuser --remote-interface gigabit0/0 "
		"--fifo=FILENAME --capture\n", argv[0], argv[0], CISCODUMP_EXTCAP_INTERFACE, argv[0],
		CISCODUMP_EXTCAP_INTERFACE, argv[0], CISCODUMP_EXTCAP_INTERFACE);
	extcap_help_add_header(extcap_conf, help_header);
	g_free(help_header);

	extcap_help_add_option(extcap_conf, "--help", "print this help");
	extcap_help_add_option(extcap_conf, "--version", "print the version");
	extcap_help_add_option(extcap_conf, "--remote-host <host>", "the remote SSH host");
	extcap_help_add_option(extcap_conf, "--remote-port <port>", "the remote SSH port (default: 22)");
	extcap_help_add_option(extcap_conf, "--remote-username <username>", "the remote SSH username (default: the current user)");
	extcap_help_add_option(extcap_conf, "--remote-password <password>", "the remote SSH password. "
		"If not specified, ssh-agent and ssh-key are used");
	extcap_help_add_option(extcap_conf, "--sshkey <public key path>", "the path of the ssh key");
	extcap_help_add_option(extcap_conf, "--sshkey-passphrase <public key passphrase>", "the passphrase to unlock public ssh");
	extcap_help_add_option(extcap_conf, "--proxycommand <proxy command>", "the command to use as proxy for the ssh connection");
	extcap_help_add_option(extcap_conf, "--ssh-sha1", "support keys and key exchange using SHA-1 (deprecated)");
	extcap_help_add_option(extcap_conf, "--remote-interface <iface>", "the remote capture interface");
	extcap_help_add_option(extcap_conf, "--remote-filter <filter>", "a filter for remote capture "
		"(default: don't capture data for all interfaces IPs)");

	ws_opterr = 0;
	ws_optind = 0;

	if (argc == 1) {
		extcap_help_print(extcap_conf);
		goto end;
	}

	while ((result = ws_getopt_long(argc, argv, ":", longopts, &option_idx)) != -1) {

		switch (result) {

		case OPT_HELP:
			extcap_help_print(extcap_conf);
			ret = EXIT_SUCCESS;
			goto end;

		case OPT_VERSION:
			extcap_version_print(extcap_conf);
			goto end;

		case OPT_REMOTE_HOST:
			g_free(ssh_params->host);
			ssh_params->host = g_strdup(ws_optarg);
			break;

		case OPT_REMOTE_PORT:
			if (!ws_strtou16(ws_optarg, NULL, &ssh_params->port) || ssh_params->port == 0) {
				ws_warning("Invalid port: %s", ws_optarg);
				goto end;
			}
			break;

		case OPT_REMOTE_USERNAME:
			g_free(ssh_params->username);
			ssh_params->username = g_strdup(ws_optarg);
			break;

		case OPT_REMOTE_PASSWORD:
			g_free(ssh_params->password);
			ssh_params->password = g_strdup(ws_optarg);
			memset(ws_optarg, 'X', strlen(ws_optarg));
			break;

		case OPT_SSHKEY:
			g_free(ssh_params->sshkey_path);
			ssh_params->sshkey_path = g_strdup(ws_optarg);
			break;

		case OPT_SSHKEY_PASSPHRASE:
			g_free(ssh_params->sshkey_passphrase);
			ssh_params->sshkey_passphrase = g_strdup(ws_optarg);
			memset(ws_optarg, 'X', strlen(ws_optarg));
			break;

		case OPT_PROXYCOMMAND:
			g_free(ssh_params->proxycommand);
			ssh_params->proxycommand = g_strdup(ws_optarg);
			break;

		case OPT_SSH_SHA1:
			ssh_params->ssh_sha1 = true;
			break;

		case OPT_REMOTE_INTERFACE:
			g_free(remote_interface);
			remote_interface = g_strdup(ws_optarg);
			break;

		case OPT_REMOTE_FILTER:
			g_free(remote_filter);
			remote_filter = g_strdup(ws_optarg);
			break;

		case OPT_REMOTE_COUNT:
			if (!ws_strtou32(ws_optarg, NULL, &count)) {
				ws_warning("Invalid packet count: %s", ws_optarg);
				goto end;
			}
			break;

		case ':':
			/* missing option argument */
			ws_warning("Option '%s' requires an argument", argv[ws_optind - 1]);
			break;

		default:
			if (!extcap_base_parse_options(extcap_conf, result - EXTCAP_OPT_LIST_INTERFACES, ws_optarg)) {
				ws_warning("Invalid option: %s", argv[ws_optind - 1]);
				goto end;
			}
		}
	}

	extcap_cmdline_debug(argv, argc);

	if (ws_optind != argc) {
		ws_warning("Unexpected extra option: %s", argv[ws_optind]);
		goto end;
	}

	if (extcap_base_handle_interface(extcap_conf)) {
		ret = EXIT_SUCCESS;
		goto end;
	}

	if (extcap_conf->show_config) {
		unsigned int port;
		if (!ws_strtou16(ws_optarg, NULL, &ssh_params->port) || ssh_params->port == 0) {
			port = 22;
		} else {
			port = ssh_params->port;
		}
		ret = list_config(extcap_conf->interface, port);
		goto end;
	}

	err_msg = ws_init_sockets();
	if (err_msg != NULL) {
		ws_warning("ERROR: %s", err_msg);
                g_free(err_msg);
		ws_warning("%s", please_report_bug());
		goto end;
	}

	if (extcap_conf->capture) {
		if (!ssh_params->host) {
			ws_warning("Missing parameter: --remote-host");
			goto end;
		}

		if (!remote_interface) {
			ws_warning("ERROR: No interface specified (--remote-interface)");
			goto end;
		}
		if (count == 0) {
			ws_warning("ERROR: count of packets must be specified (--remote-count)");
			goto end;
		}
		ssh_params_set_log_level(ssh_params, extcap_conf->debug);
		ret = ssh_open_remote_connection(ssh_params, remote_interface,
			remote_filter, count, extcap_conf->fifo);
	} else {
		ws_debug("You should not come here... maybe some parameter missing?");
		ret = EXIT_FAILURE;
	}

end:
	ssh_params_free(ssh_params);
	g_free(remote_interface);
	g_free(remote_filter);
	extcap_base_cleanup(&extcap_conf);
	return ret;
}

/*
 * Editor modelines  -  https://www.wireshark.org/tools/modelines.html
 *
 * Local variables:
 * c-basic-offset: 8
 * tab-width: 8
 * indent-tabs-mode: t
 * End:
 *
 * vi: set shiftwidth=8 tabstop=8 noexpandtab:
 * :indentSize=8:tabSize=8:noTabs=false:
 */