pistol 4.0.18

A Rust Library about Cybersecurity
Documentation
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
/* Scan */
#[cfg(any(feature = "scan", feature = "ping"))]
use chrono::DateTime;
#[cfg(any(feature = "scan", feature = "ping"))]
use chrono::Local;
#[cfg(feature = "scan")]
use pnet::datalink::MacAddr;
#[cfg(any(feature = "scan", feature = "ping"))]
use prettytable::Cell;
#[cfg(any(feature = "scan", feature = "ping"))]
use prettytable::Row;
#[cfg(any(feature = "scan", feature = "ping"))]
use prettytable::Table;
#[cfg(any(feature = "scan", feature = "ping"))]
use prettytable::row;
#[cfg(any(feature = "scan", feature = "ping"))]
use std::collections::BTreeMap;
#[cfg(any(feature = "scan", feature = "ping"))]
use std::fmt;
#[cfg(any(feature = "scan", feature = "ping"))]
use std::net::IpAddr;
#[cfg(any(feature = "scan", feature = "ping"))]
use std::net::Ipv4Addr;
#[cfg(any(feature = "scan", feature = "ping"))]
use std::net::Ipv6Addr;
#[cfg(any(feature = "scan", feature = "ping"))]
use std::sync::mpsc::channel;
#[cfg(any(feature = "scan", feature = "ping"))]
use std::time::Duration;
#[cfg(any(feature = "scan", feature = "ping"))]
use std::time::Instant;
#[cfg(any(feature = "scan", feature = "ping"))]
use tracing::debug;
#[cfg(any(feature = "scan", feature = "ping"))]
use tracing::error;
#[cfg(any(feature = "scan", feature = "ping"))]
use tracing::warn;

pub mod arp;
pub mod ndp_ns;
#[cfg(any(feature = "scan", feature = "ping"))]
pub mod tcp;
#[cfg(any(feature = "scan", feature = "ping"))]
pub mod tcp6;
#[cfg(any(feature = "scan", feature = "ping"))]
pub mod udp;
#[cfg(any(feature = "scan", feature = "ping"))]
pub mod udp6;

#[cfg(any(feature = "scan", feature = "ping"))]
use crate::Target;
#[cfg(any(feature = "scan", feature = "ping"))]
use crate::error::PistolError;
#[cfg(any(feature = "scan", feature = "ping"))]
#[cfg(feature = "scan")]
use crate::layer::find_interface_by_src;
#[cfg(any(feature = "scan", feature = "ping"))]
use crate::layer::infer_addr;
#[cfg(feature = "scan")]
use crate::scan::arp::send_arp_scan_packet;
#[cfg(any(feature = "scan", feature = "ping"))]
use crate::utils::get_threads_pool;
#[cfg(any(feature = "scan", feature = "ping"))]
use crate::utils::num_threads_check;
#[cfg(any(feature = "scan", feature = "ping"))]
use crate::utils::random_port;
#[cfg(any(feature = "scan", feature = "ping"))]
use crate::utils::time_sec_to_string;

/// This structure is used to indicate whether the status
/// is a conclusion drawn from data received or from no data received.
/// For example, in UDP scan, if no data is received, the status returned is open_or_filtered.
/// So when UDP scan returns to the open_or_filtered state, DataRecvStatus should be set to No.
/// This structure is used to indicate whether the program needs to be retried or terminated directly.
#[cfg(any(feature = "scan", feature = "ping"))]
#[derive(Debug, Clone, Copy)]
pub enum DataRecvStatus {
    Yes,
    No,
}

#[cfg(feature = "scan")]
#[derive(Debug, Clone)]
pub struct MacReport {
    pub addr: IpAddr,
    pub mac: Option<MacAddr>,
    pub ouis: String, // productions organization name
    pub rtt: Duration,
}

#[cfg(feature = "scan")]
#[derive(Debug, Clone)]
pub struct PistolMacScans {
    pub mac_reports: Vec<MacReport>,
    pub start_time: DateTime<Local>,
    pub end_time: DateTime<Local>,
    max_attempts: usize,
}

#[cfg(feature = "scan")]
impl PistolMacScans {
    pub fn new(max_attempts: usize) -> PistolMacScans {
        PistolMacScans {
            mac_reports: Vec::new(),
            start_time: Local::now(),
            end_time: Local::now(),
            max_attempts,
        }
    }
    pub fn value(&self) -> Vec<MacReport> {
        self.mac_reports.clone()
    }
    pub fn finish(&mut self, mac_reports: Vec<MacReport>) {
        self.end_time = Local::now();
        self.mac_reports = mac_reports;
    }
}

#[cfg(feature = "scan")]
impl fmt::Display for PistolMacScans {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        let total_cost = self.end_time - self.start_time;
        let total_cost_str =
            time_sec_to_string(Duration::from_secs_f64(total_cost.as_seconds_f64()));
        let mut table = Table::new();
        table.add_row(Row::new(vec![
            Cell::new(&format!(
                "Mac Scan Results (max_attempts:{})",
                self.max_attempts
            ))
            .style_spec("c")
            .with_hspan(5),
        ]));

        table.add_row(row![c -> "seq", c -> "addr", c -> "mac", c -> "oui", c-> "rtt"]);

        // sorted the results
        let mut btm_addr: BTreeMap<IpAddr, MacReport> = BTreeMap::new();
        for report in &self.mac_reports {
            btm_addr.insert(report.addr, report.clone());
        }

        let mut i = 1;
        for (_addr, report) in btm_addr {
            let rtt_str = time_sec_to_string(report.rtt);
            match report.mac {
                Some(mac) => {
                    table.add_row(
                        row![c -> i, c -> report.addr, c -> mac, c -> report.ouis, c -> rtt_str],
                    );
                    i += 1;
                }
                None => (),
            }
        }

        let avg_cost = total_cost.as_seconds_f64() / self.mac_reports.len() as f64;
        let summary = format!(
            "total cost: {}\navg cost: {:.3}s\nalive hosts: {}",
            total_cost_str,
            avg_cost,
            self.mac_reports.len(),
        );
        table.add_row(Row::new(vec![Cell::new(&summary).with_hspan(5)]));

        write!(f, "{}", table)
    }
}

#[cfg(feature = "scan")]
#[derive(Debug, Clone)]
pub struct NmapMacPrefix {
    pub prefix: String,
    pub ouis: String,
}

#[cfg(feature = "scan")]
fn get_nmap_mac_prefixes() -> Vec<NmapMacPrefix> {
    let nmap_mac_prefixes_file = include_str!("./db/nmap-mac-prefixes");
    let mut nmap_mac_prefixes = Vec::new();
    for l in nmap_mac_prefixes_file.lines() {
        nmap_mac_prefixes.push(l.to_string());
    }

    let mut ret = Vec::new();
    for p in nmap_mac_prefixes {
        if !p.contains("#") {
            let p_split: Vec<String> = p.split(" ").map(|s| s.to_string()).collect();
            if p_split.len() >= 2 {
                let ouis_slice = p_split[1..].to_vec();
                let n = NmapMacPrefix {
                    prefix: p_split[0].to_string(),
                    ouis: ouis_slice.join(" "),
                };
                ret.push(n);
            }
        }
    }
    ret
}

#[cfg(feature = "scan")]
pub fn arp_scan_raw(
    dst_ipv4: Ipv4Addr,
    src_addr: Option<IpAddr>,
    timeout: Option<Duration>,
) -> Result<(Option<MacAddr>, Duration), PistolError> {
    let dst_mac = MacAddr::broadcast();
    let (dst_ipv4, src_ipv4) = match infer_addr(dst_ipv4.into(), src_addr)? {
        Some(ia) => ia.ipv4_addr()?,
        None => return Err(PistolError::CanNotFoundSourceAddress),
    };
    let interface = match find_interface_by_src(src_ipv4.into()) {
        Some(i) => i,
        None => return Err(PistolError::CanNotFoundInterface),
    };
    let src_mac = match interface.mac {
        Some(m) => m,
        None => return Err(PistolError::CanNotFoundMacAddress),
    };
    match send_arp_scan_packet(dst_ipv4, dst_mac, src_ipv4, src_mac, interface, timeout) {
        Ok((mac, rtt)) => Ok((mac, rtt)),
        Err(e) => Err(e),
    }
}

#[cfg(feature = "scan")]
pub fn ndp_ns_scan_raw(
    dst_ipv6: Ipv6Addr,
    src_addr: Option<IpAddr>,
    timeout: Option<Duration>,
) -> Result<(Option<MacAddr>, Duration), PistolError> {
    use crate::scan::ndp_ns::send_ndp_ns_scan_packet;
    let (dst_ipv6, src_ipv6) = match infer_addr(dst_ipv6.into(), src_addr)? {
        Some(ia) => ia.ipv6_addr()?,
        None => return Err(PistolError::CanNotFoundSourceAddress),
    };
    let interface = match find_interface_by_src(src_ipv6.into()) {
        Some(i) => i,
        None => return Err(PistolError::CanNotFoundInterface),
    };
    let src_mac = match interface.mac {
        Some(m) => m,
        None => return Err(PistolError::CanNotFoundMacAddress),
    };
    match send_ndp_ns_scan_packet(dst_ipv6, src_ipv6, src_mac, interface, timeout) {
        Ok((mac, rtt)) => Ok((mac, rtt)),
        Err(e) => Err(e),
    }
}

/// ARP Scan (IPv4) or NDP NS Scan (IPv6).
/// This will sends ARP packet or NDP NS packet to hosts on the local network and displays any responses that are received.
/// ```rust
/// use pistol::PistolRunner;
/// use pistol::PistolLogger;
/// use pistol::Target;
/// use std::time::Duration;
///
/// fn main() {
///     // you cannot use `_` here because it will be automatically optimized and ignored by the compiler
///     let _pr = PistolRunner::init(
///         PistolLogger::None,
///         Some(String::from("arp_scan.pcapng")),
///         Some(Duration::from_secs_f64(0.001)),
///     )
///     .unwrap();
///     let targets = Target::from_subnet("192.168.5.0/24", None).unwrap();
///     // set the timeout same as `arp-scan`
///     let timeout = Some(Duration::from_secs_f64(0.5));
///     let src_ipv4 = None;
///     let num_threads = Some(512);
///     let max_attempts = 2;
///     let ret = mac_scan(&targets, num_threads, src_ipv4, timeout, max_attempts).unwrap();
///     println!("{}", ret);
/// }
/// ```
/// Compare the speed with arp-scan.
/// pistol:
/// ```
/// +--------+---------------+-------------------+--------+---------+
/// |                Mac Scan Results (max_attempts:2)                 |
/// +--------+---------------+-------------------+--------+---------+
/// |  seq   |     addr      |        mac        |  oui   |   rtt   |
/// +--------+---------------+-------------------+--------+---------+
/// |   1    |  192.168.5.2  | 00:50:56:ff:a6:97 | VMware | 0.84ms  |
/// +--------+---------------+-------------------+--------+---------+
/// |   2    |  192.168.5.1  | 00:50:56:c0:00:08 | VMware | 19.50ms |
/// +--------+---------------+-------------------+--------+---------+
/// |   3    | 192.168.5.254 | 00:50:56:f8:c4:8f | VMware | 9.89ms  |
/// +--------+---------------+-------------------+--------+---------+
/// | total cost: 1.13s                                             |
/// | avg cost: 0.376s                                              |
/// | alive hosts: 3                                                |
/// +--------+---------------+-------------------+--------+---------+
/// ```
/// arp-scan:
/// ```
/// ➜  pistol-rs git:(main) ✗ sudo arp-scan 192.168.5.0/24
/// Interface: ens33, type: EN10MB, MAC: 00:0c:29:5b:bd:5c, IPv4: 192.168.5.3
/// Starting arp-scan 1.10.0 with 256 hosts (https://github.com/royhills/arp-scan)
/// 192.168.5.1     00:50:56:c0:00:08       VMware, Inc.
/// 192.168.5.2     00:50:56:ff:a6:97       VMware, Inc.
/// 192.168.5.254   00:50:56:f8:c4:8f       VMware, Inc.
///
/// 3 packets received by filter, 0 packets dropped by kernel
/// Ending arp-scan 1.10.0: 256 hosts scanned in 2.001 seconds (127.94 hosts/sec). 3 responded
/// ```
#[cfg(feature = "scan")]
pub fn mac_scan(
    targets: &[Target],
    num_threads: Option<usize>,
    src_addr: Option<IpAddr>,
    timeout: Option<Duration>,
    max_attempts: usize,
) -> Result<PistolMacScans, PistolError> {
    let nmap_mac_prefixes = get_nmap_mac_prefixes();
    let mut ret = PistolMacScans::new(max_attempts);

    let num_threads = match num_threads {
        Some(t) => t,
        None => {
            let num_threads = targets.len();
            let num_threads = num_threads_check(num_threads);
            num_threads
        }
    };

    let pool = get_threads_pool(num_threads);
    let (tx, rx) = channel();
    let mut recv_size = 0;
    for target in targets {
        let dst_addr = target.addr;
        match dst_addr {
            IpAddr::V4(dst_ipv4) => {
                let tx = tx.clone();
                recv_size += 1;
                pool.execute(move || {
                    for i in 0..max_attempts {
                        debug!("arp scan packets: #{}/{}", i + 1, max_attempts);
                        let scan_ret = arp_scan_raw(dst_ipv4, src_addr, timeout);
                        if i == max_attempts - 1 {
                            let _ = tx.send((dst_addr, scan_ret));
                        } else {
                            match scan_ret {
                                Ok((value, _)) => match value {
                                    Some(_) => {
                                        let _ = tx.send((dst_addr, scan_ret));
                                        break;
                                    }
                                    None => (),
                                },
                                Err(_) => {
                                    let _ = tx.send((dst_addr, scan_ret));
                                    break;
                                }
                            }
                        }
                    }
                });
            }
            IpAddr::V6(dst_ipv6) => {
                let tx = tx.clone();
                recv_size += 1;
                pool.execute(move || {
                    for i in 0..max_attempts {
                        debug!("ndp_ns scan packets: #{}/{}", i + 1, max_attempts);
                        let scan_ret = ndp_ns_scan_raw(dst_ipv6, src_addr, timeout);
                        if i == max_attempts - 1 {
                            let _ = tx.send((dst_addr, scan_ret));
                        } else {
                            match scan_ret {
                                Ok((value, _)) => match value {
                                    Some(_) => {
                                        let _ = tx.send((dst_addr, scan_ret));
                                        break;
                                    }
                                    None => (),
                                },
                                Err(_) => {
                                    // println!("{}", e);
                                    let _ = tx.send((dst_addr, scan_ret));
                                    break;
                                }
                            }
                        }
                    }
                });
            }
        }
    }

    let mut mac_scan_reports = Vec::new();
    let iter = rx.into_iter().take(recv_size);
    for (target_addr, values) in iter {
        match values? {
            (Some(target_mac), rtt) => {
                let mut target_ouis = String::new();
                let mut mac_prefix = String::new();
                let m0 = format!("{:X}", target_mac.0);
                let m1 = format!("{:X}", target_mac.1);
                let m2 = format!("{:X}", target_mac.2);
                // m0
                let i = if m0.len() < 2 { 2 - m0.len() } else { 0 };
                if i > 0 {
                    for _ in 0..i {
                        mac_prefix += "0";
                    }
                }
                mac_prefix += &m0;
                // m1
                let i = if m1.len() < 2 { 2 - m1.len() } else { 0 };
                if i > 0 {
                    for _ in 0..i {
                        mac_prefix += "0";
                    }
                }
                mac_prefix += &m1;
                // m2
                let i = if m2.len() < 2 { 2 - m2.len() } else { 0 };
                if i > 0 {
                    for _ in 0..i {
                        mac_prefix += "0";
                    }
                }
                mac_prefix += &m2;
                // println!("{}", mac_prefix);
                for p in &nmap_mac_prefixes {
                    if mac_prefix == p.prefix {
                        target_ouis = p.ouis.to_string();
                    }
                }
                let mr = MacReport {
                    addr: target_addr,
                    mac: Some(target_mac),
                    ouis: target_ouis,
                    rtt,
                };
                mac_scan_reports.push(mr);
            }
            (None, rtt) => {
                let mr = MacReport {
                    addr: target_addr,
                    mac: None,
                    ouis: String::new(),
                    rtt,
                };
                mac_scan_reports.push(mr);
            }
        }
    }
    ret.finish(mac_scan_reports);
    Ok(ret)
}

#[cfg(any(feature = "scan", feature = "ping"))]
#[derive(Debug, Clone, Copy, PartialEq)]
pub enum ScanMethod {
    Connect,
    Syn,
    Fin,
    Ack,
    Null,
    Xmas,
    Window,
    Maimon,
    Idle, // need ipv4 ip id and ipv4 only
    Udp,
}

#[cfg(any(feature = "scan", feature = "ping"))]
#[derive(Debug, Clone, Copy, PartialEq)]
pub enum PortStatus {
    Open,
    Closed,
    Filtered,
    OpenOrFiltered,
    Unfiltered,
    Unreachable,
    ClosedOrFiltered,
    Error,
    // pistol new, for offline host
    Offline,
}

#[cfg(any(feature = "scan", feature = "ping"))]
impl fmt::Display for PortStatus {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        let s = match self {
            PortStatus::Open => "open",
            PortStatus::Closed => "closed",
            PortStatus::Filtered => "filtered",
            PortStatus::OpenOrFiltered => "open_or_filtered",
            PortStatus::Unfiltered => "unfiltered",
            PortStatus::Unreachable => "unreachable",
            PortStatus::ClosedOrFiltered => "closed_or_filtered",
            PortStatus::Error => "error",
            PortStatus::Offline => "offline",
        };
        write!(f, "{}", s)
    }
}

#[cfg(any(feature = "scan", feature = "ping"))]
#[derive(Debug, Clone)]
pub struct PortReport {
    pub addr: IpAddr,
    pub port: u16,
    pub origin: Option<String>,
    pub status: PortStatus,
    pub cost: Duration, // from layer2
}

#[cfg(any(feature = "scan", feature = "ping"))]
#[derive(Debug, Clone)]
pub struct PistolPortScans {
    // The order of this Vec is the same as the order in which the data packets are received.
    // The detection that receives the data first is in the front.
    pub port_reports: Vec<PortReport>,
    pub start_time: DateTime<Local>,
    pub end_time: DateTime<Local>,
    pub max_attempts: usize,
}

#[cfg(any(feature = "scan", feature = "ping"))]
impl PistolPortScans {
    pub fn new(max_attempts: usize) -> PistolPortScans {
        PistolPortScans {
            port_reports: Vec::new(),
            start_time: Local::now(),
            end_time: Local::now(),
            max_attempts,
        }
    }
    pub fn value(&self) -> Vec<PortReport> {
        self.port_reports.clone()
    }
    pub fn finish(&mut self, port_reports: Vec<PortReport>) {
        self.end_time = Local::now();
        self.port_reports = port_reports;
    }
}

#[cfg(any(feature = "scan", feature = "ping"))]
impl fmt::Display for PistolPortScans {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        let total_cost = self.end_time - self.start_time;

        let mut table = Table::new();
        table.add_row(Row::new(vec![
            Cell::new(&format!(
                "Port Scan Results (max_attempts:{})",
                self.max_attempts,
            ))
            .style_spec("c")
            .with_hspan(5),
        ]));

        table.add_row(row![c -> "id", c -> "addr", c -> "port", c-> "status", c -> "time cost"]);

        // sorted the resutls
        let mut btm_addr: BTreeMap<IpAddr, BTreeMap<u16, PortReport>> = BTreeMap::new();
        for report in &self.port_reports {
            if let Some(btm_port) = btm_addr.get_mut(&report.addr) {
                btm_port.insert(report.port, report.clone());
            } else {
                let mut btm_port = BTreeMap::new();
                btm_port.insert(report.port, report.clone());
                btm_addr.insert(report.addr, btm_port);
            }
        }

        let mut open_ports_num = 0;
        let mut i = 1;
        for (_addr, bt_port) in btm_addr {
            for (_port, report) in bt_port {
                match report.status {
                    PortStatus::Open => open_ports_num += 1,
                    _ => (),
                }
                let addr_str = match report.origin {
                    Some(o) => format!("{}({})", report.addr, o),
                    None => format!("{}", report.addr),
                };
                let status_str = format!("{}", report.status);
                let time_cost_str = time_sec_to_string(report.cost);
                table.add_row(
                    row![c -> i, c -> addr_str, c -> report.port, c -> status_str, c -> time_cost_str],
                );
                i += 1;
            }
        }

        // let help_info = format!(
        //     "NOTE:\nO: OPEN, OF: OPEN_OR_FILTERED, F: FILTERED,\nUF: UNFILTERED, C: CLOSED, UR: UNREACHABLE,\nCF: CLOSE_OF_FILTERED, E: ERROR, OL: OFFLINE."
        // );
        // table.add_row(Row::new(vec![Cell::new(&help_info).with_hspan(5)]));

        let avg_cost = total_cost.as_seconds_f64() / self.port_reports.len() as f64;
        let summary = format!(
            "total cost: {:.3}s, avg cost: {:.3}s, open ports: {}",
            total_cost.as_seconds_f64(),
            avg_cost,
            open_ports_num,
        );
        table.add_row(Row::new(vec![Cell::new(&summary).with_hspan(5)]));
        write!(f, "{}", table)
    }
}

#[cfg(any(feature = "scan", feature = "ping"))]
#[derive(Debug, Clone, Copy)]
pub struct TcpIdleScans {
    pub zombie_ip_id_1: u16,
    pub zombie_ip_id_2: u16,
}

#[cfg(any(feature = "scan", feature = "ping"))]
fn scan_thread(
    method: ScanMethod,
    dst_ipv4: Ipv4Addr,
    dst_port: u16,
    src_ipv4: Ipv4Addr,
    src_port: u16,
    zombie_ipv4: Option<Ipv4Addr>,
    zombie_port: Option<u16>,
    timeout: Option<Duration>,
) -> Result<(PortStatus, DataRecvStatus, Duration), PistolError> {
    let (port_status, data_return, rtt) = match method {
        ScanMethod::Connect => tcp::send_connect_scan_packet(dst_ipv4.into(), dst_port, timeout)?,
        ScanMethod::Syn => {
            tcp::send_syn_scan_packet(dst_ipv4, dst_port, src_ipv4, src_port, timeout)?
        }
        ScanMethod::Fin => {
            tcp::send_fin_scan_packet(dst_ipv4, dst_port, src_ipv4, src_port, timeout)?
        }
        ScanMethod::Ack => {
            tcp::send_ack_scan_packet(dst_ipv4, dst_port, src_ipv4, src_port, timeout)?
        }
        ScanMethod::Null => {
            tcp::send_null_scan_packet(dst_ipv4, dst_port, src_ipv4, src_port, timeout)?
        }
        ScanMethod::Xmas => {
            tcp::send_xmas_scan_packet(dst_ipv4, dst_port, src_ipv4, src_port, timeout)?
        }
        ScanMethod::Window => {
            tcp::send_window_scan_packet(dst_ipv4, dst_port, src_ipv4, src_port, timeout)?
        }
        ScanMethod::Maimon => {
            tcp::send_maimon_scan_packet(dst_ipv4, dst_port, src_ipv4, src_port, timeout)?
        }
        ScanMethod::Idle => {
            // here is always has value, so just use unwrap is fine
            let zombie_ipv4 = zombie_ipv4.unwrap();
            let zombie_port = zombie_port.unwrap();
            match tcp::send_idle_scan_packet(
                dst_ipv4,
                dst_port,
                src_ipv4,
                src_port,
                zombie_ipv4,
                zombie_port,
                timeout,
            ) {
                Ok((port_status, data_return, _idel_rets, rtt)) => (port_status, data_return, rtt),
                Err(e) => return Err(e.into()),
            }
        }
        ScanMethod::Udp => {
            udp::send_udp_scan_packet(dst_ipv4, dst_port, src_ipv4, src_port, timeout)?
        }
    };

    Ok((port_status, data_return, rtt))
}

#[cfg(any(feature = "scan", feature = "ping"))]
fn scan_thread6(
    method: ScanMethod,
    dst_ipv6: Ipv6Addr,
    dst_port: u16,
    src_ipv6: Ipv6Addr,
    src_port: u16,
    timeout: Option<Duration>,
) -> Result<(PortStatus, DataRecvStatus, Duration), PistolError> {
    let (port_status, data_return, rtt) = match method {
        ScanMethod::Connect => tcp::send_connect_scan_packet(dst_ipv6.into(), dst_port, timeout)?,
        ScanMethod::Syn => {
            tcp6::send_syn_scan_packet(dst_ipv6, dst_port, src_ipv6, src_port, timeout)?
        }
        ScanMethod::Fin => {
            tcp6::send_fin_scan_packet(dst_ipv6, dst_port, src_ipv6, src_port, timeout)?
        }
        ScanMethod::Ack => {
            tcp6::send_ack_scan_packet(dst_ipv6, dst_port, src_ipv6, src_port, timeout)?
        }
        ScanMethod::Null => {
            tcp6::send_null_scan_packet(dst_ipv6, dst_port, src_ipv6, src_port, timeout)?
        }
        ScanMethod::Xmas => {
            tcp6::send_xmas_scan_packet(dst_ipv6, dst_port, src_ipv6, src_port, timeout)?
        }
        ScanMethod::Window => {
            tcp6::send_window_scan_packet(dst_ipv6, dst_port, src_ipv6, src_port, timeout)?
        }
        ScanMethod::Maimon => {
            tcp6::send_maimon_scan_packet(dst_ipv6, dst_port, src_ipv6, src_port, timeout)?
        }
        ScanMethod::Udp => {
            udp6::send_udp_scan_packet(dst_ipv6, dst_port, src_ipv6, src_port, timeout)?
        }
        ScanMethod::Idle => {
            warn!("idel scan not supported the ipv6 address, use connect scan instead now");
            tcp::send_connect_scan_packet(dst_ipv6.into(), dst_port, timeout)?
        }
    };

    Ok((port_status, data_return, rtt))
}

/// General scan function.
#[cfg(any(feature = "scan", feature = "ping"))]
fn scan(
    targets: &[Target],
    num_threads: Option<usize>,
    method: ScanMethod,
    src_addr: Option<IpAddr>,
    src_port: Option<u16>,
    zombie_ipv4: Option<Ipv4Addr>,
    zombie_port: Option<u16>,
    timeout: Option<Duration>,
    max_attempts: usize,
) -> Result<PistolPortScans, PistolError> {
    let mut pistol_port_scans = PistolPortScans::new(max_attempts);

    let num_threads = match num_threads {
        Some(t) => t,
        None => {
            let mut init_numm_threads = 0;
            for t in targets {
                init_numm_threads += t.ports.len();
            }
            let num_threads = num_threads_check(init_numm_threads);
            num_threads
        }
    };

    debug!("scan will create {} threads to do the jobs", num_threads);
    let pool = get_threads_pool(num_threads);
    let (tx, rx) = channel();
    let mut recv_size = 0;

    for target in targets {
        let dst_addr = target.addr;
        match dst_addr {
            IpAddr::V4(dst_ipv4) => {
                for &dst_port in &target.ports {
                    let origin = target.origin.clone();
                    let src_port = match src_port {
                        Some(s) => s,
                        None => {
                            warn!("can not found src port, use random port instead");
                            random_port()
                        }
                    };
                    // debug!(
                    //     "sending scan packet to [{}] port [{}] and src port [{}]",
                    //     dst_addr, dst_port, src_port
                    // );
                    let tx = tx.clone();
                    recv_size += 1;
                    let (dst_ipv4, src_ipv4) = match infer_addr(dst_ipv4.into(), src_addr)? {
                        Some(ia) => ia.ipv4_addr()?,
                        None => return Err(PistolError::CanNotFoundSourceAddress),
                    };

                    pool.execute(move || {
                        for ind in 0..max_attempts {
                            let start_time = Instant::now();
                            debug!(
                                "sending scan packet to [{}] port [{}] try [{}]",
                                dst_addr, dst_port, ind
                            );
                            let scan_ret = scan_thread(
                                method,
                                dst_ipv4,
                                dst_port,
                                src_ipv4,
                                src_port,
                                zombie_ipv4,
                                zombie_port,
                                timeout,
                            );
                            if ind == max_attempts - 1 {
                                // last attempt
                                let _ = tx.send((
                                    dst_addr,
                                    dst_port,
                                    origin.clone(),
                                    scan_ret,
                                    start_time.elapsed(),
                                ));
                            } else {
                                match scan_ret {
                                    Ok((_port_status, data_return, _)) => {
                                        match data_return {
                                            DataRecvStatus::Yes => {
                                                // conclusions drawn from the returned data
                                                let _ = tx.send((
                                                    dst_addr,
                                                    dst_port,
                                                    origin.clone(),
                                                    scan_ret,
                                                    start_time.elapsed(),
                                                ));
                                                break; // quit loop now
                                            }
                                            // conclusions from the default policy
                                            DataRecvStatus::No => (), // continue probing
                                        }
                                    }
                                    Err(_) => {
                                        // stop probe immediately if an error occurs
                                        let _ = tx.send((
                                            dst_addr,
                                            dst_port,
                                            origin.clone(),
                                            scan_ret,
                                            start_time.elapsed(),
                                        ));
                                        break;
                                    }
                                }
                            }
                        }
                    });
                }
            }
            IpAddr::V6(dst_ipv6) => {
                for &dst_port in &target.ports {
                    let origin = target.origin.clone();
                    let src_port = match src_port {
                        Some(s) => s,
                        None => {
                            warn!("can not found src port, use random port instead");
                            random_port()
                        }
                    };
                    let tx = tx.clone();
                    recv_size += 1;
                    let (dst_ipv6, src_ipv6) = match infer_addr(dst_ipv6.into(), src_addr)? {
                        Some(ia) => ia.ipv6_addr()?,
                        None => return Err(PistolError::CanNotFoundSourceAddress),
                    };
                    pool.execute(move || {
                        for ind in 0..max_attempts {
                            let start_time = Instant::now();
                            let scan_ret = scan_thread6(
                                method, dst_ipv6, dst_port, src_ipv6, src_port, timeout,
                            );
                            if ind == max_attempts - 1 {
                                let _ = tx.send((
                                    dst_addr,
                                    dst_port,
                                    origin.clone(),
                                    scan_ret,
                                    start_time.elapsed(),
                                ));
                            } else {
                                match scan_ret {
                                    Ok((_port_status, data_return, _)) => {
                                        match data_return {
                                            DataRecvStatus::Yes => {
                                                // conclusions drawn from the returned data
                                                let _ = tx.send((
                                                    dst_addr,
                                                    dst_port,
                                                    origin.clone(),
                                                    scan_ret,
                                                    start_time.elapsed(),
                                                ));
                                                break; // quit loop now
                                            }
                                            // conclusions from the default policy
                                            DataRecvStatus::No => (), // continue probing
                                        }
                                    }
                                    Err(_) => {
                                        // stop probe immediately if an error occurs
                                        let _ = tx.send((
                                            dst_addr,
                                            dst_port,
                                            origin.clone(),
                                            scan_ret,
                                            start_time.elapsed(),
                                        ));
                                        break;
                                    }
                                }
                            }
                        }
                    });
                }
            }
        }
    }

    let iter = rx.into_iter().take(recv_size);
    let mut reports = Vec::new();
    for (dst_addr, dst_port, origin, v, elapsed) in iter {
        match v {
            Ok((port_status, _data_return, rtt)) => {
                // println!("rtt: {:.3}", rtt.as_secs_f64());
                let scan_report = PortReport {
                    addr: dst_addr,
                    port: dst_port,
                    origin,
                    status: port_status,
                    cost: rtt,
                };
                reports.push(scan_report);
            }
            Err(e) => match e {
                PistolError::CanNotFoundMacAddress => {
                    let scan_report = PortReport {
                        addr: dst_addr,
                        port: dst_port,
                        origin,
                        status: PortStatus::Offline,
                        cost: elapsed,
                    };
                    reports.push(scan_report);
                }
                _ => {
                    error!("scan error: {}", e);
                    let scan_report = PortReport {
                        addr: dst_addr,
                        port: dst_port,
                        origin,
                        status: PortStatus::Error,
                        cost: elapsed,
                    };
                    reports.push(scan_report);
                }
            },
        }
    }
    pistol_port_scans.finish(reports);
    Ok(pistol_port_scans)
}

/// TCP Connect() Scan.
/// This is the most basic form of TCP scanning.
/// The connect() system call provided by your operating system is used to open a connection to every interesting port on the machine.
/// If the port is listening, connect() will succeed, otherwise the port isn't reachable.
/// One strong advantage to this technique is that you don't need any special privileges.
/// Any user on most UNIX boxes is free to use this call.
/// Another advantage is speed.
/// While making a separate connect() call for every targeted port in a linear fashion would take ages over a slow connection,
/// you can hasten the scan by using many sockets in parallel.
/// Using non-blocking I/O allows you to set a low time-out period and watch all the sockets at once.
/// This is the fastest scanning method supported by nmap, and is available with the -t (TCP) option.
/// The big downside is that this sort of scan is easily detectable and filterable.
/// The target hosts logs will show a bunch of connection and error messages for the services which take the connection and then have it immediately shutdown.
#[cfg(feature = "scan")]
pub fn tcp_connect_scan(
    targets: &[Target],
    num_threads: Option<usize>,
    src_addr: Option<IpAddr>,
    src_port: Option<u16>,
    timeout: Option<Duration>,
    max_attempts: usize,
) -> Result<PistolPortScans, PistolError> {
    scan(
        targets,
        num_threads,
        ScanMethod::Connect,
        src_addr,
        src_port,
        None,
        None,
        timeout,
        max_attempts,
    )
}

/// TCP connect() Scan, raw version.
#[cfg(feature = "scan")]
pub fn tcp_connect_scan_raw(
    dst_addr: IpAddr,
    dst_port: u16,
    src_addr: Option<IpAddr>,
    src_port: Option<u16>,
    timeout: Option<Duration>,
) -> Result<(PortStatus, Duration), PistolError> {
    scan_raw(
        ScanMethod::Connect,
        dst_addr,
        dst_port,
        src_addr,
        src_port,
        None,
        None,
        timeout,
    )
}

/// TCP SYN Scan.
/// This technique is often referred to as "half-open" scanning, because you don't open a full TCP connection.
/// You send a SYN packet, as if you are going to open a real connection and wait for a response.
/// A SYN|ACK indicates the port is listening.
/// A RST is indicative of a non-listener.
/// If a SYN|ACK is received, you immediately send a RST to tear down the connection (actually the kernel does this for us).
/// The primary advantage to this scanning technique is that fewer sites will log it.
/// Unfortunately you need root privileges to build these custom SYN packets.
/// SYN scan is the default and most popular scan option for good reason.
/// It can be performed quickly,
/// scanning thousands of ports per second on a fast network not hampered by intrusive firewalls.
/// SYN scan is relatively unobtrusive and stealthy, since it never completes TCP connections.
#[cfg(any(feature = "scan", feature = "ping"))]
pub fn tcp_syn_scan(
    targets: &[Target],
    num_threads: Option<usize>,
    src_addr: Option<IpAddr>,
    src_port: Option<u16>,
    timeout: Option<Duration>,
    max_attempts: usize,
) -> Result<PistolPortScans, PistolError> {
    scan(
        targets,
        num_threads,
        ScanMethod::Syn,
        src_addr,
        src_port,
        None,
        None,
        timeout,
        max_attempts,
    )
}

/// TCP SYN Scan, raw version.
#[cfg(feature = "scan")]
pub fn tcp_syn_scan_raw(
    dst_addr: IpAddr,
    dst_port: u16,
    src_addr: Option<IpAddr>,
    src_port: Option<u16>,
    timeout: Option<Duration>,
) -> Result<(PortStatus, Duration), PistolError> {
    scan_raw(
        ScanMethod::Syn,
        dst_addr,
        dst_port,
        src_addr,
        src_port,
        None,
        None,
        timeout,
    )
}

/// TCP FIN Scan.
/// There are times when even SYN scanning isn't clandestine enough.
/// Some firewalls and packet filters watch for SYNs to an unallowed port,
/// and programs like synlogger and Courtney are available to detect these scans.
/// FIN packets, on the other hand, may be able to pass through unmolested.
/// This scanning technique was featured in detail by Uriel Maimon in Phrack 49, article 15.
/// The idea is that closed ports tend to reply to your FIN packet with the proper RST.
/// Open ports, on the other hand, tend to ignore the packet in question.
/// This is a bug in TCP implementations and so it isn't 100% reliable
/// (some systems, notably Micro$oft boxes, seem to be immune).
/// When scanning systems compliant with this RFC text,
/// any packet not containing SYN, RST, or ACK bits will result in a returned RST if the port is closed and no response at all if the port is open.
/// As long as none of those three bits are included, any combination of the other three (FIN, PSH, and URG) are OK.
#[cfg(feature = "scan")]
pub fn tcp_fin_scan(
    targets: &[Target],
    num_threads: Option<usize>,
    src_addr: Option<IpAddr>,
    src_port: Option<u16>,
    timeout: Option<Duration>,
    max_attempts: usize,
) -> Result<PistolPortScans, PistolError> {
    scan(
        targets,
        num_threads,
        ScanMethod::Fin,
        src_addr,
        src_port,
        None,
        None,
        timeout,
        max_attempts,
    )
}

/// TCP FIN Scan, raw version.
#[cfg(feature = "scan")]
pub fn tcp_fin_scan_raw(
    dst_addr: IpAddr,
    dst_port: u16,
    src_addr: Option<IpAddr>,
    src_port: Option<u16>,
    timeout: Option<Duration>,
) -> Result<(PortStatus, Duration), PistolError> {
    scan_raw(
        ScanMethod::Fin,
        dst_addr,
        dst_port,
        src_addr,
        src_port,
        None,
        None,
        timeout,
    )
}

/// TCP ACK Scan.
/// This scan is different than the others discussed so far in that it never determines open (or even open|filtered) ports.
/// It is used to map out firewall rulesets, determining whether they are stateful or not and which ports are filtered.
/// When scanning unfiltered systems, open and closed ports will both return a RST packet.
/// We then labels them as unfiltered, meaning that they are reachable by the ACK packet, but whether they are open or closed is undetermined.
/// Ports that don't respond, or send certain ICMP error messages back, are labeled filtered.
#[cfg(feature = "scan")]
pub fn tcp_ack_scan(
    targets: &[Target],
    num_threads: Option<usize>,
    src_addr: Option<IpAddr>,
    src_port: Option<u16>,
    timeout: Option<Duration>,
    max_attempts: usize,
) -> Result<PistolPortScans, PistolError> {
    scan(
        targets,
        num_threads,
        ScanMethod::Ack,
        src_addr,
        src_port,
        None,
        None,
        timeout,
        max_attempts,
    )
}

/// TCP ACK Scan, raw version.
#[cfg(feature = "scan")]
pub fn tcp_ack_scan_raw(
    dst_addr: IpAddr,
    dst_port: u16,
    src_addr: Option<IpAddr>,
    src_port: Option<u16>,
    timeout: Option<Duration>,
) -> Result<(PortStatus, Duration), PistolError> {
    scan_raw(
        ScanMethod::Ack,
        dst_addr,
        dst_port,
        src_addr,
        src_port,
        None,
        None,
        timeout,
    )
}

/// TCP Null Scan.
/// Does not set any bits (TCP flag header is 0).
/// When scanning systems compliant with this RFC text,
/// any packet not containing SYN, RST, or ACK bits will result in a returned RST if the port is closed and no response at all if the port is open.
/// As long as none of those three bits are included, any combination of the other three (FIN, PSH, and URG) are OK.
#[cfg(feature = "scan")]
pub fn tcp_null_scan(
    targets: &[Target],
    num_threads: Option<usize>,
    src_addr: Option<IpAddr>,
    src_port: Option<u16>,
    timeout: Option<Duration>,
    max_attempts: usize,
) -> Result<PistolPortScans, PistolError> {
    scan(
        targets,
        num_threads,
        ScanMethod::Null,
        src_addr,
        src_port,
        None,
        None,
        timeout,
        max_attempts,
    )
}

/// TCP Null Scan, raw version.
#[cfg(feature = "scan")]
pub fn tcp_null_scan_raw(
    dst_addr: IpAddr,
    dst_port: u16,
    src_addr: Option<IpAddr>,
    src_port: Option<u16>,
    timeout: Option<Duration>,
) -> Result<(PortStatus, Duration), PistolError> {
    scan_raw(
        ScanMethod::Null,
        dst_addr,
        dst_port,
        src_addr,
        src_port,
        None,
        None,
        timeout,
    )
}

/// TCP Xmas Scan.
/// Sets the FIN, PSH, and URG flags, lighting the packet up like a Christmas tree.
/// When scanning systems compliant with this RFC text,
/// any packet not containing SYN, RST, or ACK bits will result in a returned RST if the port is closed and no response at all if the port is open.
/// As long as none of those three bits are included, any combination of the other three (FIN, PSH, and URG) are OK.
#[cfg(feature = "scan")]
pub fn tcp_xmas_scan(
    targets: &[Target],
    num_threads: Option<usize>,
    src_addr: Option<IpAddr>,
    src_port: Option<u16>,
    timeout: Option<Duration>,
    max_attempts: usize,
) -> Result<PistolPortScans, PistolError> {
    scan(
        targets,
        num_threads,
        ScanMethod::Xmas,
        src_addr,
        src_port,
        None,
        None,
        timeout,
        max_attempts,
    )
}

/// TCP Xmas Scan, raw version.
#[cfg(feature = "scan")]
pub fn tcp_xmas_scan_raw(
    dst_addr: IpAddr,
    dst_port: u16,
    src_addr: Option<IpAddr>,
    src_port: Option<u16>,
    timeout: Option<Duration>,
) -> Result<(PortStatus, Duration), PistolError> {
    scan_raw(
        ScanMethod::Xmas,
        dst_addr,
        dst_port,
        src_addr,
        src_port,
        None,
        None,
        timeout,
    )
}

/// TCP Window Scan.
/// Window scan is exactly the same as ACK scan except that it exploits an implementation detail of certain systems to differentiate open ports from closed ones,
/// rather than always printing unfiltered when a RST is returned.
/// It does this by examining the TCP Window value of the RST packets returned.
/// On some systems, open ports use a positive window size (even for RST packets) while closed ones have a zero window.
/// Window scan sends the same bare ACK probe as ACK scan.
#[cfg(feature = "scan")]
pub fn tcp_window_scan(
    targets: &[Target],
    num_threads: Option<usize>,
    src_addr: Option<IpAddr>,
    src_port: Option<u16>,
    timeout: Option<Duration>,
    max_attempts: usize,
) -> Result<PistolPortScans, PistolError> {
    scan(
        targets,
        num_threads,
        ScanMethod::Window,
        src_addr,
        src_port,
        None,
        None,
        timeout,
        max_attempts,
    )
}

/// TCP Window Scan, raw version.
#[cfg(feature = "scan")]
pub fn tcp_window_scan_raw(
    dst_addr: IpAddr,
    dst_port: u16,
    src_addr: Option<IpAddr>,
    src_port: Option<u16>,
    timeout: Option<Duration>,
) -> Result<(PortStatus, Duration), PistolError> {
    scan_raw(
        ScanMethod::Window,
        dst_addr,
        dst_port,
        src_addr,
        src_port,
        None,
        None,
        timeout,
    )
}

/// TCP Maimon Scan.
/// The Maimon scan is named after its discoverer, Uriel Maimon.
/// He described the technique in Phrack Magazine issue #49 (November 1996).
/// This technique is exactly the same as NULL, FIN, and Xmas scan, except that the probe is FIN/ACK.
/// According to RFC 793 (TCP), a RST packet should be generated in response to such a probe whether the port is open or closed.
/// However, Uriel noticed that many BSD-derived systems simply drop the packet if the port is open.
#[cfg(feature = "scan")]
pub fn tcp_maimon_scan(
    targets: &[Target],
    num_threads: Option<usize>,
    src_addr: Option<IpAddr>,
    src_port: Option<u16>,
    timeout: Option<Duration>,
    max_attempts: usize,
) -> Result<PistolPortScans, PistolError> {
    scan(
        targets,
        num_threads,
        ScanMethod::Maimon,
        src_addr,
        src_port,
        None,
        None,
        timeout,
        max_attempts,
    )
}

/// TCP Maimon Scan, raw version.
#[cfg(feature = "scan")]
pub fn tcp_maimon_scan_raw(
    dst_addr: IpAddr,
    dst_port: u16,
    src_addr: Option<IpAddr>,
    src_port: Option<u16>,
    timeout: Option<Duration>,
) -> Result<(PortStatus, Duration), PistolError> {
    scan_raw(
        ScanMethod::Maimon,
        dst_addr,
        dst_port,
        src_addr,
        src_port,
        None,
        None,
        timeout,
    )
}

/// TCP Idle Scan.
/// In 1998, security researcher Antirez (who also wrote the hping2 tool used in parts of this book)
/// posted to the Bugtraq mailing list an ingenious new port scanning technique.
/// Idle scan, as it has become known, allows for completely blind port scanning.
/// Attackers can actually scan a target without sending a single packet to the target from their own IP address!
/// Instead, a clever side-channel attack allows for the scan to be bounced off a dumb "zombie host".
/// Intrusion detection system (IDS) reports will finger the innocent zombie as the attacker.
/// Besides being extraordinarily stealthy, this scan type permits discovery of IP-based trust relationships between machines.
#[cfg(feature = "scan")]
pub fn tcp_idle_scan(
    targets: &[Target],
    num_threads: Option<usize>,
    src_addr: Option<IpAddr>,
    src_port: Option<u16>,
    zombie_ipv4: Option<Ipv4Addr>,
    zombie_port: Option<u16>,
    timeout: Option<Duration>,
    max_attempts: usize,
) -> Result<PistolPortScans, PistolError> {
    scan(
        targets,
        num_threads,
        ScanMethod::Idle,
        src_addr,
        src_port,
        zombie_ipv4,
        zombie_port,
        timeout,
        max_attempts,
    )
}

/// TCP Idle Scan, raw version.
#[cfg(feature = "scan")]
pub fn tcp_idle_scan_raw(
    dst_addr: IpAddr,
    dst_port: u16,
    src_addr: Option<IpAddr>,
    src_port: Option<u16>,
    zombie_ipv4: Option<Ipv4Addr>,
    zombie_port: Option<u16>,
    timeout: Option<Duration>,
) -> Result<(PortStatus, Duration), PistolError> {
    scan_raw(
        ScanMethod::Idle,
        dst_addr,
        dst_port,
        src_addr,
        src_port,
        zombie_ipv4,
        zombie_port,
        timeout,
    )
}

/// UDP Scan.
/// While most popular services on the Internet run over the TCP protocol, UDP services are widely deployed.
/// DNS, SNMP, and DHCP (registered ports 53, 161/162, and 67/68) are three of the most common.
/// Because UDP scanning is generally slower and more difficult than TCP, some security auditors ignore these ports.
/// This is a mistake, as exploitable UDP services are quite common and attackers certainly don't ignore the whole protocol.
/// UDP scan works by sending a UDP packet to every targeted port.
/// For most ports, this packet will be empty (no payload), but for a few of the more common ports a protocol-specific payload will be sent.
/// Based on the response, or lack thereof, the port is assigned to one of four states.
#[cfg(feature = "scan")]
pub fn udp_scan(
    targets: &[Target],
    num_threads: Option<usize>,
    src_addr: Option<IpAddr>,
    src_port: Option<u16>,
    timeout: Option<Duration>,
    max_attempts: usize,
) -> Result<PistolPortScans, PistolError> {
    scan(
        targets,
        num_threads,
        ScanMethod::Udp,
        src_addr,
        src_port,
        None,
        None,
        timeout,
        max_attempts,
    )
}

/// UDP Scan, raw version.
#[cfg(feature = "scan")]
pub fn udp_scan_raw(
    dst_addr: IpAddr,
    dst_port: u16,
    src_addr: Option<IpAddr>,
    src_port: Option<u16>,
    timeout: Option<Duration>,
) -> Result<(PortStatus, Duration), PistolError> {
    scan_raw(
        ScanMethod::Udp,
        dst_addr,
        dst_port,
        src_addr,
        src_port,
        None,
        None,
        timeout,
    )
}

#[cfg(feature = "scan")]
fn scan_raw(
    method: ScanMethod,
    dst_addr: IpAddr,
    dst_port: u16,
    src_addr: Option<IpAddr>,
    src_port: Option<u16>,
    zombie_ipv4: Option<Ipv4Addr>,
    zombie_port: Option<u16>,
    timeout: Option<Duration>,
) -> Result<(PortStatus, Duration), PistolError> {
    let src_port = match src_port {
        Some(s) => s,
        None => random_port(),
    };

    let ia = match infer_addr(dst_addr, src_addr)? {
        Some(ia) => ia,
        None => return Err(PistolError::CanNotFoundSourceAddress),
    };

    // dst_addr may change during the processing.
    // It is only used here to determine whether the target is ipv4 or ipv6.
    // The real dst_addr is inferred from infer_addr.
    match dst_addr {
        IpAddr::V4(_) => {
            let (dst_ipv4, src_ipv4) = ia.ipv4_addr()?;
            let (port_status, _data_return, rtt) = scan_thread(
                method,
                dst_ipv4,
                dst_port,
                src_ipv4,
                src_port,
                zombie_ipv4,
                zombie_port,
                timeout,
            )?;
            Ok((port_status, rtt))
        }
        IpAddr::V6(_) => {
            let (dst_ipv6, src_ipv6) = ia.ipv6_addr()?;
            let (port_status, _data_return, rtt) =
                scan_thread6(method, dst_ipv6, dst_port, src_ipv6, src_port, timeout)?;
            Ok((port_status, rtt))
        }
    }
}

#[cfg(feature = "scan")]
#[cfg(test)]
mod tests {
    use super::*;
    use crate::PistolLogger;
    use crate::PistolRunner;
    use crate::Target;
    use std::str::FromStr;
    #[test]
    fn test_arp_scan_subnet() {
        let _pr = PistolRunner::init(
            PistolLogger::None,
            Some(String::from("arp_scan.pcapng")),
            None, // use default value
        )
        .unwrap();
        let targets = Target::from_subnet("192.168.5.0/24", None).unwrap();
        // println!("{}", targets.len());
        // let target1 = Target::new(IpAddr::V4(Ipv4Addr::new(192, 168, 5, 1)), None);
        // let target2 = Target::new(IpAddr::V4(Ipv4Addr::new(192, 168, 5, 2)), None);
        // let target3 = Target::new(IpAddr::V4(Ipv4Addr::new(192, 168, 5, 3)), None);
        // let target4 = Target::new(IpAddr::V4(Ipv4Addr::new(192, 168, 5, 4)), None);
        // let targets = vec![target1, target2, target3, target4];q
        let timeout = Some(Duration::from_secs_f64(0.5));
        let src_ipv4 = None;
        let num_threads = Some(512);
        let max_attempts = 2;
        let ret = mac_scan(&targets, num_threads, src_ipv4, timeout, max_attempts).unwrap();
        println!("{}", ret);
    }
    #[test]
    fn test_ndp_ns_scan_subnet() {
        let _pr = PistolRunner::init(
            PistolLogger::None,
            Some(String::from("ndp_ns_scan.pcapng")),
            None, // use default value
        )
        .unwrap();
        let targets = Target::from_subnet6("fe80::20c:29ff:fe5b:bd5c/126", None).unwrap();
        let timeout = Some(Duration::from_secs_f64(0.5));
        let src_ipv6 = None;
        let num_threads = Some(512);
        let max_attempts = 2;
        let ret = mac_scan(&targets, num_threads, src_ipv6, timeout, max_attempts).unwrap();
        println!("{}", ret);
    }
    #[test]
    fn test_ndp_ns_scan_single() {
        let _pr = PistolRunner::init(
            PistolLogger::None,
            Some(String::from("ndp_ns_scan_single.pcapng")),
            None, // use default value
        )
        .unwrap();
        let ipv6 = Ipv6Addr::from_str("fe80::20c:29ff:fe2c:9e4").unwrap();
        let target = Target::new(ipv6.into(), None);
        let timeout = Some(Duration::from_secs_f64(0.5));
        let src_ipv6 = None;
        let num_threads = Some(512);
        let max_attempts = 2;
        let ret = mac_scan(&[target], num_threads, src_ipv6, timeout, max_attempts).unwrap();
        println!("{}", ret);
    }
    #[test]
    fn test_tcp_connect_scan() {
        let _pr = PistolRunner::init(
            PistolLogger::None,
            Some(String::from("tcp_connnect_scan.pcapng")),
            None,
        )
        .unwrap();

        let src_ipv4 = None;
        let src_port = None;
        let timeout = Some(Duration::new(1, 0));
        let dst_ipv4 = Ipv4Addr::new(192, 168, 5, 5);
        // let dst_ipv4 = Ipv4Addr::new(192, 168, 31, 1);
        let target = Target::new(dst_ipv4.into(), Some(vec![22, 80, 443]));
        // let host = Host::new(TEST_IPV4_LOCAL.into(), Some(vec![22, 99]));
        let max_attempts = 2;
        let num_threads = Some(8);
        let ret = tcp_connect_scan(
            &[target],
            num_threads,
            src_ipv4,
            src_port,
            timeout,
            max_attempts,
        )
        .unwrap();
        println!("{}", ret);

        let src_ipv4 = None;
        let src_port = None;
        let timeout = Some(Duration::new(1, 0));
        let dst_ipv4 = Ipv4Addr::new(127, 0, 0, 1);
        // let dst_ipv4 = Ipv4Addr::new(192, 168, 31, 1);
        let target = Target::new(dst_ipv4.into(), Some(vec![22, 80, 443]));
        // let host = Host::new(TEST_IPV4_LOCAL.into(), Some(vec![22, 99]));
        let max_attempts = 2;
        let num_threads = Some(8);
        let ret = tcp_connect_scan(
            &[target],
            num_threads,
            src_ipv4,
            src_port,
            timeout,
            max_attempts,
        )
        .unwrap();
        println!("{}", ret);
    }
    #[test]
    fn test_tcp_syn_scan() {
        let _pr = PistolRunner::init(
            PistolLogger::Debug,
            Some(String::from("tcp_syn_scan.pcapng")),
            None, // use default value
        )
        .unwrap();

        let src_ipv4 = None;
        let src_port = None;
        let timeout = Some(Duration::new(1, 0));
        #[cfg(all(target_os = "linux", target_arch = "aarch64"))]
        let addr1 = IpAddr::V4(Ipv4Addr::new(10, 179, 252, 233));
        #[cfg(all(target_os = "linux", target_arch = "x86_64"))]
        let addr1 = IpAddr::V4(Ipv4Addr::new(192, 168, 5, 129));
        #[cfg(target_os = "linux")]
        let ports = vec![22, 80, 5432, 8080];
        #[cfg(target_os = "windows")]
        let addr1 = IpAddr::V4(Ipv4Addr::new(192, 168, 5, 129));
        #[cfg(target_os = "windows")]
        let ports = vec![22, 80, 3389, 8080];
        #[cfg(target_os = "freebsd")]
        let addr1 = IpAddr::V4(Ipv4Addr::new(192, 168, 1, 4));
        #[cfg(target_os = "freebsd")]
        let ports = vec![22, 80, 3389, 8080];

        let target1 = Target::new(addr1, Some(ports));
        let max_attempts = 2;
        let num_threads = None;
        let ret = tcp_syn_scan(
            &[target1],
            num_threads,
            src_ipv4,
            src_port,
            timeout,
            max_attempts,
        )
        .unwrap();
        println!("{}", ret);
    }
    #[test]
    fn test_tcp_fin_scan() {
        let _pr = PistolRunner::init(
            PistolLogger::None,
            Some(String::from("tcp_fin_scan.pcapng")),
            None, // use default value
        )
        .unwrap();

        let src_ipv4 = None;
        let src_port = None;
        let timeout = Some(Duration::new(1, 0));
        let dst_ipv4 = IpAddr::V4(Ipv4Addr::new(192, 168, 5, 5));
        let target = Target::new(dst_ipv4, Some(vec![22, 80, 443]));
        let max_attempts = 2;
        let num_threads = Some(8);
        let ret = tcp_fin_scan(
            &[target],
            num_threads,
            src_ipv4,
            src_port,
            timeout,
            max_attempts,
        )
        .unwrap();
        println!("{}", ret);
    }
    #[test]
    fn test_tcp_ack_scan() {
        let _pr = PistolRunner::init(
            PistolLogger::None,
            Some(String::from("tcp_ack_scan.pcapng")),
            None, // use default value
        )
        .unwrap();

        let src_ipv4 = None;
        let src_port = None;
        let timeout = Some(Duration::new(1, 0));
        let dst_ipv4 = IpAddr::V4(Ipv4Addr::new(192, 168, 5, 5));
        let target = Target::new(dst_ipv4, Some(vec![22, 80, 443]));
        let max_attempts = 2;
        let num_threads = Some(8);
        let ret = tcp_ack_scan(
            &[target],
            num_threads,
            src_ipv4,
            src_port,
            timeout,
            max_attempts,
        )
        .unwrap();
        println!("{}", ret);
    }
    #[test]
    fn test_tcp_null_scan() {
        let _pr = PistolRunner::init(
            PistolLogger::None,
            Some(String::from("tcp_null_scan.pcapng")),
            None, // use default value
        )
        .unwrap();

        let src_ipv4 = None;
        let src_port = None;
        let timeout = Some(Duration::new(1, 0));
        let dst_ipv4 = IpAddr::V4(Ipv4Addr::new(192, 168, 5, 5));
        let target = Target::new(dst_ipv4, Some(vec![22, 80, 443]));
        let max_attempts = 2;
        let num_threads = Some(8);
        let ret = tcp_null_scan(
            &[target],
            num_threads,
            src_ipv4,
            src_port,
            timeout,
            max_attempts,
        )
        .unwrap();
        println!("{}", ret);
    }
    #[test]
    fn test_udp_scan() {
        let _pr = PistolRunner::init(
            PistolLogger::None,
            Some(String::from("tcp_udp_scan.pcapng")),
            None, // use default value
        )
        .unwrap();

        let src_ipv4 = None;
        let src_port = None;
        let timeout = Some(Duration::new(1, 0));
        let dst_ipv4 = IpAddr::V4(Ipv4Addr::new(192, 168, 5, 5));
        let target = Target::new(dst_ipv4, Some(vec![22, 80, 443, 8080, 8081]));
        let max_attempts = 2;
        let num_threads = Some(8);
        let ret = udp_scan(
            &[target],
            num_threads,
            src_ipv4,
            src_port,
            timeout,
            max_attempts,
        )
        .unwrap();
        println!("{}", ret);
    }
}