1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
2308
2309
2310
2311
2312
2313
2314
2315
2316
2317
2318
2319
2320
2321
2322
2323
2324
2325
2326
2327
2328
2329
2330
2331
2332
2333
2334
2335
2336
2337
2338
2339
2340
2341
2342
2343
2344
2345
2346
2347
2348
2349
2350
2351
2352
2353
2354
2355
2356
2357
2358
2359
2360
2361
2362
2363
2364
2365
2366
2367
2368
2369
2370
2371
2372
2373
2374
2375
2376
2377
2378
2379
2380
2381
2382
2383
2384
2385
2386
2387
2388
2389
2390
2391
2392
2393
2394
2395
2396
2397
2398
2399
2400
2401
2402
2403
2404
2405
2406
2407
2408
2409
2410
2411
2412
2413
2414
2415
2416
2417
2418
2419
2420
2421
2422
2423
2424
2425
2426
2427
2428
2429
2430
2431
2432
2433
2434
2435
2436
2437
2438
2439
2440
2441
2442
2443
2444
2445
2446
2447
2448
2449
2450
2451
2452
2453
2454
2455
2456
2457
2458
2459
2460
2461
2462
2463
2464
2465
2466
2467
2468
2469
2470
2471
2472
2473
2474
2475
2476
2477
2478
2479
2480
2481
2482
2483
2484
2485
2486
2487
2488
2489
2490
2491
2492
2493
2494
2495
2496
2497
2498
2499
2500
2501
2502
2503
2504
2505
2506
2507
2508
2509
2510
2511
2512
2513
2514
2515
2516
2517
2518
2519
2520
2521
2522
2523
2524
2525
2526
2527
2528
2529
2530
2531
2532
2533
2534
2535
2536
2537
2538
2539
2540
2541
2542
2543
2544
2545
2546
2547
2548
2549
2550
2551
2552
2553
2554
2555
2556
2557
2558
2559
2560
2561
2562
2563
2564
2565
2566
2567
2568
2569
2570
2571
2572
2573
2574
2575
2576
2577
2578
2579
2580
2581
2582
2583
2584
2585
2586
2587
2588
2589
2590
2591
2592
2593
2594
2595
2596
2597
2598
2599
2600
2601
2602
2603
2604
2605
2606
2607
2608
2609
2610
2611
2612
2613
2614
2615
2616
2617
2618
2619
2620
2621
2622
2623
2624
2625
2626
2627
2628
2629
2630
2631
2632
2633
2634
2635
2636
2637
2638
2639
2640
2641
2642
2643
2644
2645
2646
2647
2648
2649
2650
2651
2652
2653
2654
2655
2656
2657
2658
2659
2660
2661
2662
2663
2664
2665
2666
2667
2668
2669
2670
2671
2672
2673
2674
2675
2676
2677
2678
2679
2680
2681
2682
2683
2684
2685
2686
2687
2688
2689
2690
2691
2692
2693
2694
2695
2696
2697
2698
2699
2700
2701
2702
2703
2704
2705
2706
2707
2708
2709
2710
2711
2712
2713
2714
2715
2716
2717
2718
2719
2720
2721
2722
2723
2724
2725
2726
2727
2728
2729
2730
2731
2732
2733
2734
2735
2736
2737
2738
2739
2740
2741
2742
2743
2744
2745
2746
2747
2748
2749
2750
2751
2752
2753
2754
2755
2756
2757
2758
2759
2760
2761
2762
2763
2764
2765
2766
2767
2768
2769
2770
2771
2772
2773
2774
2775
2776
2777
2778
2779
2780
2781
2782
2783
2784
2785
2786
2787
2788
2789
2790
2791
2792
2793
2794
2795
2796
2797
2798
2799
2800
2801
2802
2803
2804
2805
2806
2807
2808
2809
2810
2811
2812
2813
2814
2815
2816
2817
2818
2819
2820
2821
2822
2823
2824
2825
2826
2827
2828
2829
2830
2831
2832
2833
2834
2835
2836
2837
2838
2839
2840
2841
2842
2843
2844
2845
2846
2847
2848
2849
2850
2851
2852
2853
2854
2855
2856
2857
2858
2859
2860
2861
2862
2863
2864
2865
2866
2867
2868
2869
2870
2871
2872
2873
2874
2875
2876
2877
2878
2879
2880
2881
2882
2883
2884
2885
2886
2887
2888
2889
2890
2891
2892
2893
2894
2895
2896
2897
2898
2899
2900
2901
2902
2903
2904
2905
2906
2907
2908
2909
2910
2911
2912
2913
2914
2915
2916
2917
2918
2919
2920
2921
2922
2923
2924
2925
2926
2927
2928
2929
2930
2931
2932
2933
2934
2935
2936
2937
2938
2939
2940
2941
2942
2943
2944
2945
2946
2947
2948
2949
2950
2951
2952
2953
2954
2955
2956
2957
2958
2959
2960
2961
2962
2963
2964
2965
2966
2967
2968
2969
2970
2971
2972
2973
2974
2975
2976
2977
2978
2979
2980
2981
2982
2983
2984
2985
2986
2987
2988
2989
2990
2991
2992
2993
2994
2995
2996
2997
2998
2999
3000
3001
3002
3003
3004
3005
3006
3007
3008
3009
3010
3011
3012
3013
3014
3015
3016
3017
3018
3019
3020
3021
3022
3023
3024
3025
3026
3027
3028
3029
3030
3031
3032
3033
3034
3035
3036
3037
3038
3039
3040
3041
3042
3043
3044
3045
3046
3047
3048
3049
3050
3051
3052
3053
3054
3055
3056
3057
3058
3059
3060
3061
3062
3063
3064
3065
3066
3067
3068
3069
3070
3071
3072
3073
3074
3075
3076
3077
3078
3079
3080
3081
3082
3083
3084
3085
3086
3087
3088
3089
3090
3091
3092
3093
3094
3095
3096
3097
3098
3099
3100
3101
3102
3103
3104
3105
3106
3107
3108
3109
3110
3111
3112
3113
3114
3115
3116
3117
3118
3119
3120
3121
3122
3123
3124
3125
3126
3127
3128
3129
3130
3131
3132
3133
3134
|
/***************************************************************************
* NpingOps.cc -- The NpingOps class contains global options, mostly based *
* on user-provided command-line settings. *
* *
***********************IMPORTANT NMAP LICENSE TERMS************************
*
* The Nmap Security Scanner is (C) 1996-2023 Nmap Software LLC ("The Nmap
* Project"). Nmap is also a registered trademark of the Nmap Project.
*
* This program is distributed under the terms of the Nmap Public Source
* License (NPSL). The exact license text applying to a particular Nmap
* release or source code control revision is contained in the LICENSE
* file distributed with that version of Nmap or source code control
* revision. More Nmap copyright/legal information is available from
* https://nmap.org/book/man-legal.html, and further information on the
* NPSL license itself can be found at https://nmap.org/npsl/ . This
* header summarizes some key points from the Nmap license, but is no
* substitute for the actual license text.
*
* Nmap is generally free for end users to download and use themselves,
* including commercial use. It is available from https://nmap.org.
*
* The Nmap license generally prohibits companies from using and
* redistributing Nmap in commercial products, but we sell a special Nmap
* OEM Edition with a more permissive license and special features for
* this purpose. See https://nmap.org/oem/
*
* If you have received a written Nmap license agreement or contract
* stating terms other than these (such as an Nmap OEM license), you may
* choose to use and redistribute Nmap under those terms instead.
*
* The official Nmap Windows builds include the Npcap software
* (https://npcap.com) for packet capture and transmission. It is under
* separate license terms which forbid redistribution without special
* permission. So the official Nmap Windows builds may not be redistributed
* without special permission (such as an Nmap OEM license).
*
* Source is provided to this software because we believe users have a
* right to know exactly what a program is going to do before they run it.
* This also allows you to audit the software for security holes.
*
* Source code also allows you to port Nmap to new platforms, fix bugs, and add
* new features. You are highly encouraged to submit your changes as a Github PR
* or by email to the dev@nmap.org mailing list for possible incorporation into
* the main distribution. Unless you specify otherwise, it is understood that
* you are offering us very broad rights to use your submissions as described in
* the Nmap Public Source License Contributor Agreement. This is important
* because we fund the project by selling licenses with various terms, and also
* because the inability to relicense code has caused devastating problems for
* other Free Software projects (such as KDE and NASM).
*
* The free version of Nmap is distributed in the hope that it will be
* useful, but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. Warranties,
* indemnification and commercial support are all available through the
* Npcap OEM program--see https://nmap.org/oem/
*
***************************************************************************/
#ifdef WIN32
#include "winfix.h"
#endif
#include "nping.h"
#include "nbase.h"
#include "NpingOps.h"
#include "utils.h"
#include "utils_net.h"
#include "ArgParser.h"
#include "output.h"
#include "common.h"
/******************************************************************************
* Constructors and destructors *
******************************************************************************/
/* Constructor */
NpingOps::NpingOps() {
/* Probe modes */
mode=0;
mode_set=false;
traceroute=false;
traceroute_set=false;
/* Output */
vb=DEFAULT_VERBOSITY;
vb_set=false;
dbg=DEFAULT_DEBUGGING;
dbg_set=false;
show_sent_pkts=true;
show_sent_pkts_set=false;
/* Operation and Performance */
pcount=0;
pcount_set=false;
sendpref=0;
sendpref_set=false;
send_eth=false;
send_eth_set=false;
delay=0;
delay_set=false;
memset(device, 0, MAX_DEV_LEN);
device_set=false;
spoofsource=false;
spoofsource_set=false;
bpf_filter_spec=NULL;
bpf_filter_spec_set=false;
current_round=0;
have_pcap=true;
disable_packet_capture=false;
disable_packet_capture_set=false;
/* Privileges */
/* If user did not specify --privileged or --unprivileged explicitly, try to
* determine if has root privileges. */
#if defined WIN32 || defined __amigaos__
/* TODO: Check this because although nmap does exactly the same, it has a this->have_pcap that may affect to this */
isr00t=true;
#else
if (getenv("NMAP_PRIVILEGED") || getenv("NPING_PRIVILEGED"))
isr00t=true;
else if (getenv("NMAP_UNPRIVILEGED") || getenv("NPING_UNPRIVILEGED"))
isr00t=false;
else
isr00t = !(geteuid());
#endif
/* Payloads */
payload_type=PL_NONE;
payload_type_set=false;
payload_buff=NULL;
payload_buff_set=false;
payload_len=0;
payload_len_set=false;
/* Roles */
role=0;
role_set=false;
/* IP */
ttl=0;
ttl_set=false;
tos=0;
tos_set=false;
identification=0;
identification_set=false;
mf=false;
mf_set=false;
df=false;
df_set=false;
mtu=0;
mtu_set=false;
badsum_ip=false;
badsum_ip_set=false;
ipversion=0;
ipversion_set=false;
memset(&ipv4_src_address, 0, sizeof(struct in_addr));
ipv4_src_address_set=false;
ip_options=NULL;
ip_options_set=false;
/* IPv6 */
ipv6_tclass=0;
ipv6_tclass_set=false;
ipv6_flowlabel=0;
ipv6_flowlabel_set=false;
memset(&ipv6_src_address, 0, sizeof(struct in6_addr));
ipv6_src_address_set=false;
/* TCP / UDP */
target_ports=NULL;
tportcount=0;
target_ports_set=false;
source_port=0;
source_port_set=false;
tcpseq=0;
tcpseq_set=false;
memset(tcpflags, 0, 8);
tcpflags_set=false;
tcpack=0;
tcpack_set=false;
tcpwin=0;
tcpwin_set=false;
badsum=false;
badsum_set=false;
/* ICMP */
icmp_type=0;
icmp_type_set=false;
icmp_code=0;
icmp_code_set=false;
badsum_icmp=false;
badsum_icmp_set=false;
icmp_redir_addr.s_addr=0;
icmp_redir_addr_set=false;
icmp_paramprob_pnt=0;
icmp_paramprob_pnt_set=false;
icmp_routeadv_ltime=0;
icmp_routeadv_ltime_set=false;
icmp_id=0;
icmp_id_set=false;
icmp_seq=0;
icmp_seq_set=false;
icmp_orig_time=0;
icmp_orig_time_set=false;
icmp_recv_time=0;
icmp_recv_time_set=false;
icmp_trans_time=0;
icmp_trans_time_set=false;
memset( icmp_advert_entry_addr, 0, sizeof(u32)*MAX_ICMP_ADVERT_ENTRIES );
memset( icmp_advert_entry_pref, 0, sizeof(u32)*MAX_ICMP_ADVERT_ENTRIES );
icmp_advert_entry_count=0;
icmp_advert_entry_set=false;
/* Ethernet */
memset(src_mac, 0, 6);
src_mac_set=false;
memset(dst_mac, 0, 6);
dst_mac_set=false;
eth_type=0;
eth_type_set=false;
arp_htype=0;
arp_htype_set=false;
/* ARP/RARP */
arp_ptype=0;
arp_ptype_set=false;
arp_hlen=0;
arp_hlen_set=false;
arp_plen=0;
arp_plen_set=false;
arp_opcode=0;
arp_opcode_set=false;
memset(arp_sha, 0, 6);
arp_sha_set=false;
memset(arp_tha, 0, 6);
arp_tha_set=false;
arp_spa.s_addr=0;
arp_spa_set=false;
arp_tpa.s_addr=0;
arp_tpa_set=false;
/* Echo mode */
echo_port=DEFAULT_ECHO_PORT;
echo_port_set=false;
do_crypto=true;
echo_payload=false;
echo_server_once=false;
echo_server_once_set=false;
memset(echo_passphrase, 0, sizeof(echo_passphrase));
echo_passphrase_set=false;
memset(&last_sent_pkt_time, 0, sizeof(struct timeval));
delayed_rcvd_str=NULL;
delayed_rcvd_str_set=false;
} /* End of NpingOps() */
/* Destructor */
NpingOps::~NpingOps() {
if (payload_buff!=NULL)
free(payload_buff);
if ( ip_options!=NULL )
free(ip_options);
if ( target_ports!=NULL )
free(target_ports);
if (delayed_rcvd_str_set)
free(delayed_rcvd_str);
return;
} /* End of ~NpingOps() */
/******************************************************************************
* Nping probe modes *
******************************************************************************/
/** Sets attribute "mode". Mode must be one of: TCP_CONNECT TCP, UDP, ICMP,
* ARP
* @return OP_SUCCESS on success.
* @return OP_FAILURE in case of error. */
int NpingOps::setMode(int md) {
if ( md!=TCP_CONNECT && md!=TCP && md!=UDP && md!=UDP_UNPRIV && md!=ICMP && md!=ARP )
return OP_FAILURE;
else{
this->mode=md;
this->mode_set=true;
}
return OP_SUCCESS;
} /* End of setMode() */
/** Returns value of attribute "mode". The value returned is supposed to be
* one of : TCP_CONNECT, TCP, UDP, UDP_UNPRIV, ICMP, ARP */
int NpingOps::getMode() {
return mode;
} /* End of getMode() */
/* Returns true if option has been set */
bool NpingOps::issetMode(){
return this->mode_set;
} /* End of isset() */
/** Takes a probe mode and returns an ASCII string with the name of the mode.
* @warning Returned pointer is a static buffer that subsequent calls
* will overwrite.
* @return Pointer to the appropriate string on success and pointer to a
* string containing "Unknown probe" in case of failure.
* */
char * NpingOps::mode2Ascii(int md) {
static char buff[24];
switch( md ){
case TCP_CONNECT:
sprintf(buff, "TCP-Connect");
break;
case TCP:
sprintf(buff, "TCP");
break;
case UDP:
sprintf(buff, "UDP");
break;
case UDP_UNPRIV:
sprintf(buff, "UDP-Unprivileged");
break;
case ICMP:
sprintf(buff, "ICMP");
break;
case ARP:
sprintf(buff, "ARP");
break;
default:
sprintf(buff, "Unknown mode");
break;
}
return buff;
} /* End of mode2Ascii() */
/** Returns value of attribute "traceroute" */
bool NpingOps::getTraceroute() {
return traceroute;
} /* End of getTraceroute() */
/** Sets attribute traceroute to "true".
* @return previous value of the attribute. */
bool NpingOps::enableTraceroute() {
bool prev = traceroute;
this->traceroute=true;
this->traceroute_set=true;
return prev;
} /* End of enableTraceroute() */
/** Sets attribute traceroute to "false".
* @return previous value of the attribute. */
bool NpingOps::disableTraceroute() {
bool prev = traceroute;
this->traceroute=false;
this->traceroute_set=true;
return prev;
} /* End of disableTraceroute() */
/* Returns true if option has been set */
bool NpingOps::issetTraceroute(){
return this->traceroute_set;
} /* End of issetTraceroute() */
/******************************************************************************
* Output *
******************************************************************************/
/** Sets verbosity level. Supplied level must be an integer between -4 and
* 4. Check man pages for details.
*
* The thing here is that what the argument parser gets from the user is
* number in the range [-4, 4]. However, in NpingOps we don't store negative
* verbosity values, we just convert the supplied level into our internal
* levels (QT_4, QT_3, ... , VB_0, VB_1, ..., VB_4)
* So the rest of the code in Nping should check for these defines, rather
* than checking for numbers. Check nping.h for more information on how to
* handle verbosity levels.
*
* @return OP_SUCCESS on success and OP_FAILURE in case of error. */
int NpingOps::setVerbosity(int level){
if( level < -4 || level > 4 ){
nping_fatal(QT_3,"setVerbosity(): Invalid verbosity level supplied\n");
return OP_FAILURE;
}else{
switch(level){
case -4: vb=QT_4; break;
case -3: vb=QT_3; break;
case -2: vb=QT_2; break;
case -1: vb=QT_1; break;
case 0: vb=VB_0; break;
case 1: vb=VB_1; break;
case 2: vb=VB_2; break;
case 3: vb=VB_3; break;
case 4: vb=VB_4; break;
default:
nping_fatal(QT_3,"setVerbosity():2: Invalid verbosity level supplied\n");
break;
}
}
this->vb_set=true;
return OP_SUCCESS;
} /* End of setVerbosity() */
/** Returns value of attribute vb (current verbosity level) */
int NpingOps::getVerbosity(){
return vb;
} /* End of getVerbosity() */
/* Returns true if option has been set */
bool NpingOps::issetVerbosity(){
return this->vb_set;
} /* End of issetVerbosity() */
/** Increments verbosity level by one. (When it reaches VB_4 it stops
* getting incremented)
* @return previous verbosity level */
int NpingOps::increaseVerbosity(){
this->vb_set=true;
if (vb < VB_4){
vb++;
return vb-1;
}else{
return vb;
}
} /* End of increaseVerbosity() */
/** Decreases verbosity level by one. (When it reaches QT_4 it stops
* getting incremented)
* @return previous verbosity level */
int NpingOps::decreaseVerbosity(){
this->vb_set=true;
if (vb > QT_4){
vb--;
return vb+1;
}else{
return vb;
}
} /* End of decreaseVerbosity() */
/** Sets debugging level. Supplied level must be an integer between DBG_0 and
* DBG_9. Check file nping.h for details
* @return OP_SUCCESS on success and OP_FAILURE in case of error. */
int NpingOps::setDebugging(int level){
if( level < 0 || level > 9){
nping_fatal(QT_3,"setDebugging(): Invalid debugging level supplied\n");
return OP_FAILURE;
}else{
this->dbg= DBG_0 + level;
}
this->dbg_set=true;
return OP_SUCCESS;
} /* End of setDebugging() */
/** Returns value of attribute dbg (current debugging level) */
int NpingOps::getDebugging(){
return dbg;
} /* End of getDebugging() */
/** Increments debugging level by one. (When it reaches DBG_9 it stops
* getting incremented)
* * @return previous verbosity level */
int NpingOps::increaseDebugging(){
this->dbg_set=true;
if (dbg < DBG_9){
dbg++;
return dbg-1;
}else{
return dbg;
}
} /* End of increaseDebugging() */
/* Returns true if option has been set */
bool NpingOps::issetDebugging(){
return this->dbg_set;
} /* End of issetDebugging() */
/** Sets ShowSentPackets.
* @return OP_SUCCESS on success and OP_FAILURE in case of error. */
int NpingOps::setShowSentPackets(bool val){
this->show_sent_pkts=val;
this->show_sent_pkts_set=true;
return OP_SUCCESS;
} /* End of setShowSentPackets() */
/** Returns value of attribute show_sent_pkts */
bool NpingOps::showSentPackets(){
return this->show_sent_pkts;
} /* End of showSentPackets() */
/* Returns true if option has been set */
bool NpingOps::issetShowSentPackets(){
return this->show_sent_pkts_set;
} /* End of issetShowSentPackets() */
/******************************************************************************
* Operation and Performance *
******************************************************************************/
/** Sets packet count (number of packets that should be sent to each target)
* Supplied parameter must be a non-negative integer.
* @return OP_SUCCESS on success and OP_FAILURE in case of error. */
int NpingOps::setPacketCount(u32 val){
/* If zero is supplied, use the highest possible value */
if( val==0 )
this->pcount=0xFFFFFFFF;
else
pcount=val;
this->pcount_set=true;
return OP_SUCCESS;
} /* End of setPacketCount() */
/** Returns value of attribute pcount (number of packets that should be sent
* to each target) */
u32 NpingOps::getPacketCount(){
return this->pcount;
} /* End of getPacketCount() */
/* Returns true if option has been set */
bool NpingOps::issetPacketCount(){
return this->pcount_set;
} /* End of issetPacketCount() */
/** Sets attribute sendpref which defines user's preference for packet
* sending. Supplied parameter must be an integer with one of these values:
* PACKET_SEND_NOPREF, PACKET_SEND_ETH_WEAK, PACKET_SEND_ETH_STRONG,
* PACKET_SEND_ETH, PACKET_SEND_IP_WEAK, PACKET_SEND_IP_STRONG, PACKET_SEND_IP
* @return OP_SUCCESS on success and OP_FAILURE in case of error. */
int NpingOps::setSendPreference(int v){
if( v!=PACKET_SEND_NOPREF && v!=PACKET_SEND_ETH_WEAK &&
v!=PACKET_SEND_ETH_STRONG && v!=PACKET_SEND_ETH &&
v!=PACKET_SEND_IP_WEAK && v!=PACKET_SEND_IP_STRONG &&
v!=PACKET_SEND_IP ){
nping_fatal(QT_3,"setSendPreference(): Invalid value supplied\n");
return OP_FAILURE;
}else{
sendpref=v;
}
this->sendpref_set=true;
return OP_SUCCESS;
} /* End of setSendPreference() */
/** Returns value of attribute sendpref */
int NpingOps::getSendPreference(){
return this->sendpref;
} /* End of getSendPreference() */
/* Returns true if option has been set */
bool NpingOps::issetSendPreference(){
return this->sendpref_set;
} /* End of issetSendPreference() */
/* Returns true if send preference is Ethernet */
bool NpingOps::sendPreferenceEthernet(){
if ( this->getSendPreference()==PACKET_SEND_ETH_WEAK )
return true;
else if (this->getSendPreference()==PACKET_SEND_ETH_STRONG)
return true;
else if (this->getSendPreference()==PACKET_SEND_ETH )
return true;
else
return false;
} /* End of sendPreferenceEthernet() */
/* Returns true if send preference is Ethernet */
bool NpingOps::sendPreferenceIP(){
if ( this->getSendPreference()==PACKET_SEND_IP_WEAK )
return true;
else if (this->getSendPreference()==PACKET_SEND_IP_STRONG)
return true;
else if (this->getSendPreference()==PACKET_SEND_IP )
return true;
else
return false;
} /* End of sendPreferenceIP() */
/** Sets SendEth.
* @return OP_SUCCESS on success and OP_FAILURE in case of error. */
int NpingOps::setSendEth(bool val){
this->send_eth=val;
this->send_eth_set=true;
return OP_SUCCESS;
} /* End of setSendEth() */
/** Returns value of attribute send_eth */
bool NpingOps::sendEth(){
return this->send_eth;
} /* End of getSendEth() */
/* Returns true if option has been set */
bool NpingOps::issetSendEth(){
return this->send_eth_set;
} /* End of issetSendEth() */
/** Sets inter-probe delay. Supplied parameter is assumed to be in milliseconds
* and must be a long integer greater than zero.
* @warning timeout is assumed to be in milliseconds. Use tval2msecs() from
* nbase to obtain a proper value.
* @return OP_SUCCESS on success and OP_FAILURE in case of error. */
int NpingOps::setDelay(long t){
if( t < 0 )
nping_fatal(QT_3,"setDelay(): Invalid time supplied\n");
this->delay=t;
this->delay_set=true;
return OP_SUCCESS;
} /* End of setDelay() */
/** Returns value of attribute delay */
long NpingOps::getDelay(){
return delay;
} /* End of getDelay() */
/* Returns true if option has been set */
bool NpingOps::issetDelay(){
return this->delay_set;
} /* End of issetDelay() */
/** Sets network device. Supplied parameter must be a valid network interface
* name.
* @return OP_SUCCESS on success and OP_FAILURE in case of error. */
int NpingOps::setDevice(char *n){
if( n==NULL ){
nping_fatal(QT_3,"setDevice(): Invalid value supplied\n");
}else{
Strncpy(this->device, n, MAX_DEV_LEN-1);
}
this->device_set=true;
return OP_SUCCESS;
} /* End of setDevice() */
char *NpingOps::getDevice(){
return this->device;
} /* End of getDevice() */
/* Returns true if option has been set */
bool NpingOps::issetDevice(){
return this->device_set;
} /* End of issetDevice() */
/** Returns true if user requested explicitly that he wants IP source
* spoofing */
bool NpingOps::spoofSource(){
return this->spoofsource;
} /* End of spoofSource() */
bool NpingOps::getSpoofSource(){
return this->spoofsource;
} /* End of getSpoofSource() */
int NpingOps::setSpoofSource(){
this->spoofsource=true;
this->spoofsource_set=true;
return OP_SUCCESS;
} /* End of spoofSource() */
/* Returns true if option has been set */
bool NpingOps::issetSpoofSource(){
return this->spoofsource_set;
} /* End of issetSpoofSource() */
/** Sets BPFFilterSpec.
* @return OP_SUCCESS on success and OP_FAILURE in case of error. */
int NpingOps::setBPFFilterSpec(char *val){
this->bpf_filter_spec=val;
this->bpf_filter_spec_set=true;
return OP_SUCCESS;
} /* End of setBPFFilterSpec() */
/** Returns value of attribute bpf_filter_spec */
char *NpingOps::getBPFFilterSpec(){
return this->bpf_filter_spec;
} /* End of getBPFFilterSpec() */
/* Returns true if option has been set */
bool NpingOps::issetBPFFilterSpec(){
return this->bpf_filter_spec_set;
} /* End of issetBPFFilterSpec() */
/** Sets CurrentRound.
* @return OP_SUCCESS on success and OP_FAILURE in case of error. */
int NpingOps::setCurrentRound(int val){
this->current_round=val;
return OP_SUCCESS;
} /* End of setCurrentRound() */
/** Returns value of attribute current_round */
int NpingOps::getCurrentRound(){
return this->current_round;
} /* End of getCurrentRound() */
bool NpingOps::havePcap(){
return this->have_pcap;
} /* End of havePcap() */
int NpingOps::setHavePcap(bool val){
this->have_pcap=val;
return OP_SUCCESS;
} /* End of setHavePcap() */
/** Sets DisablePacketCapture.
* @return OP_SUCCESS on success and OP_FAILURE in case of error. */
int NpingOps::setDisablePacketCapture(bool val){
this->disable_packet_capture=val;
this->disable_packet_capture_set=true;
return OP_SUCCESS;
} /* End of setDisablePacketCapture() */
/** Returns value of attribute disable_packet_capture */
bool NpingOps::disablePacketCapture(){
return this->disable_packet_capture;
} /* End of disablePacketCapture() */
/* Returns true if option has been set */
bool NpingOps::issetDisablePacketCapture(){
return this->disable_packet_capture_set;
} /* End of issetDisablePacketCapture() */
/** Sets the IP version that will be used in all packets. Supplied parameter
* must be either IP_VERSION_4 or IP_VERSION_&.
* @return OP_SUCCESS on success and OP_FAILURE in case of error. */
int NpingOps::setIPVersion(u8 val){
if( val!=IP_VERSION_4 && val!=IP_VERSION_6 ){
nping_fatal(QT_3,"setIPVersion(): Invalid value supplied\n");
return OP_FAILURE;
}else{
this-> ipversion=val;
}
this->ipversion_set=true;
return OP_SUCCESS;
} /* End of setIPVersion() */
/** Returns value of attribute ipversion. */
int NpingOps::getIPVersion(){
return ipversion;
} /* End of getIPVersion() */
/* Returns true if option has been set */
bool NpingOps::issetIPVersion(){
return this->ipversion_set;
} /* End of issetIPversion() */
/* Returns true if we are using IPv4 */
bool NpingOps::ipv4(){
if( this->getIPVersion() == IP_VERSION_4 )
return true;
else
return false;
} /* End of ipv4() */
/* Returns true if we are using IPv6 */
bool NpingOps::ipv6(){
if( this->getIPVersion() == IP_VERSION_6 )
return true;
else
return false;
} /* End of ipv6() */
/* Returns true if we are sending IPv6 packets at raw TCP level (using a
* useless and boring IPv6 socket that doesn't let us include our own IPv6
* header)*/
bool NpingOps::ipv6UsingSocket(){
if( this->getIPVersion() == IP_VERSION_6 && this->sendEth()==false)
return true;
else
return false;
} /* End of ipv6UsingSocket() */
/* Returns AF_INET or AF_INET6, depending on current configuration */
int NpingOps::af(){
if( this->getIPVersion() == IP_VERSION_6 )
return AF_INET6;
else
return AF_INET;
} /* End of af() */
/******************************************************************************
* User types and Privileges *
******************************************************************************/
/** Sets value of attribute isr00t.
* @returns previous isr00t value */
int NpingOps::setIsRoot(int v) {
int prev=this->isr00t;
this->isr00t = (v==0) ? 0 : 1;
return prev;
} /* End of setIsRoot() */
/** Sets attribute isr00t to value 1.
* @returns previous isr00t value */
int NpingOps::setIsRoot() {
int prev=this->isr00t;
this->isr00t=1;
return prev;
} /* End of setIsRoot() */
/* Returns the state of attribute isr00t */
bool NpingOps::isRoot() {
return (this->isr00t);
} /* End of isRoot() */
/******************************************************************************
* Payloads *
******************************************************************************/
/** Sets payload type. Supplied parameter must be one of: PL_RAND, PL_HEX or
* PL_FILE;
* @return OP_SUCCESS on success and OP_FAILURE in case of error. */
int NpingOps::setPayloadType(int t){
if( t!=PL_RAND && t!=PL_HEX && t!=PL_FILE && t!=PL_STRING){
nping_fatal(QT_3,"setPayloadType(): Invalid value supplied\n");
return OP_FAILURE;
}else{
payload_type=t;
}
this->payload_type_set=true;
return OP_SUCCESS;
} /* End of setPayloadType() */
/** Returns value of attribute payload_type */
int NpingOps::getPayloadType(){
return payload_type;
} /* End of getPayloadType() */
/* Returns true if option has been set */
bool NpingOps::issetPayloadType(){
return this->payload_type_set;
} /* End of issetPayloadType() */
/** Sets payload buffer pointer. Supplied pointer must be a free()able
* non-NULL pointer; Supplied length must be a positive integer.
* @return OP_SUCCESS on success and OP_FAILURE in case of error. */
int NpingOps::setPayloadBuffer(u8 *p, int len){
if( p==NULL || len < 0 ){
nping_fatal(QT_3,"setPayloadBuffer(): Invalid value supplied\n");
return OP_FAILURE;
}else{
this->payload_buff=p;
this->payload_len=len;
}
this->payload_buff_set=true;
this->payload_len_set=true;
return OP_SUCCESS;
} /* End of setPayloadBuffer() */
/** Returns value of attribute payload_type */
u8 *NpingOps::getPayloadBuffer(){
return this->payload_buff;
} /* End of getPayloadBuffer() */
/* Returns true if option has been set */
bool NpingOps::issetPayloadBuffer(){
return this->payload_buff_set;
} /* End of issetPayloadBuffer() */
/** Returns value of attribute payload_len */
int NpingOps::getPayloadLen(){
return this->payload_len;
} /* End of getPayloadLen() */
/* Returns true if option has been set */
bool NpingOps::issetPayloadLen(){
return this->payload_len_set;
} /* End of issetPayloadLen() */
/******************************************************************************
* Roles (normal, client, server... ) *
******************************************************************************/
/** Sets nping's role. Supplied argument must be one of: ROLE_NORMAL,
* ROLE_CLIENT or ROLE_SERVER.
* @return previous value of attribute "role" or OP_FAILURE in case of error.
* */
int NpingOps::setRole(int r){
int prev = this->role;
if (r!=ROLE_NORMAL && r!=ROLE_CLIENT && r!=ROLE_SERVER){
nping_warning(QT_2,"setRoleClient(): Invalid role supplied");
return OP_FAILURE;
}
else
this->role=r;
this->role_set=true;
return prev;
} /* End of setRole() */
/** Sets nping's role to ROLE_CLIENT.
* @return previous value of attribute "role". */
int NpingOps::setRoleClient(){
int prev = this->role;
this->role=ROLE_CLIENT;
this->role_set=true;
return prev;
} /* End of setRoleClient() */
/** Sets nping's role to ROLE_SERVER.
* @return previous value of attribute "role". */
int NpingOps::setRoleServer(){
int prev = this->role;
this->role=ROLE_SERVER;
this->role_set=true;
return prev;
} /* End of setRoleServer() */
/** Sets nping's role to ROLE_NORMAL.
* @return previous value of attribute "role". */
int NpingOps::setRoleNormal(){
int prev = this->role;
this->role=ROLE_NORMAL;
this->role_set=true;
return prev;
} /* End of setRoleNormal() */
/* Returns nping role. */
int NpingOps::getRole(){
return this->role;
} /* End of getRole() */
/* Returns true if option has been set */
bool NpingOps::issetRole(){
return this->role_set;
} /* End of issetRole() */
/******************************************************************************
* Internet Protocol Version 4 *
******************************************************************************/
/** Sets IPv4 TTL / IPv6 hop limit. Supplied parameter must be an integer
* between 0 and 255 (included).
* @return OP_SUCCESS */
int NpingOps::setTTL(u8 t){
this->ttl=t;
this->ttl_set=true;
return OP_SUCCESS;
} /* End of setTTL() */
/** Sets IPv4 TTL / IPv6 hop limit. This a wrapper for setTTL(). It is provided
* for consistency with IPv6 option setters.
* @return OP_SUCCESS on success and OP_FAILURE in case of error. */
int NpingOps::setHopLimit(u8 t){
return setTTL(t);
} /* End of setHopLimit() */
/** Returns value of attribute ttl */
u8 NpingOps::getTTL(){
return ttl;
} /* End of getTTL() */
/** Returns value of attribute ttl */
u8 NpingOps::getHopLimit(){
return getTTL();
} /* End of getHopLimit() */
/* Returns true if option has been set */
bool NpingOps::issetTTL(){
return this->ttl_set;
} /* End of issetTTL() */
/* Returns true if option has been set */
bool NpingOps::issetHopLimit(){
return issetTTL();
} /* End of issetHopLimit() */
/** Sets IP TOS. Supplied parameter must be 0<=n<=255
* @return OP_SUCCESS on success and OP_FAILURE in case of error. */
int NpingOps::setTOS(u8 val){
this->tos=val;
this->tos_set=true;
return OP_SUCCESS;
} /* End of setTOS() */
/** Returns value of attribute TOS */
u8 NpingOps::getTOS(){
return this->tos;
} /* End of getTOS() */
/* Returns true if option has been set */
bool NpingOps::issetTOS(){
return this->tos_set;
} /* End of isset() */
/** Sets IP Identification. Supplied parameter must be 0<=n<=255
* @return OP_SUCCESS on success and OP_FAILURE in case of error. */
int NpingOps::setIdentification(u16 val){
this->identification=val;
this->identification_set=true;
return OP_SUCCESS;
} /* End of setIdentification() */
/** Returns value of attribute Identification */
u16 NpingOps::getIdentification(){
return this->identification;
} /* End of getIdentification() */
/* Returns true if option has been set */
bool NpingOps::issetIdentification(){
return this->identification_set;
} /* End of issetIdentification() */
int NpingOps::setMF(){
this->mf = true;
this->mf_set=true;
return OP_SUCCESS;
} /* End of setMF() */
/* Get MF flag */
bool NpingOps::getMF(){
return this->mf;
} /* End of getMF() */
/** Set DF flag */
int NpingOps::setDF(){
this->df = true;
this->df_set=true;
return OP_SUCCESS;
} /* End of setDF() */
/** Get DF flag */
bool NpingOps::getDF(){
return this->df;
} /* End of getDF() */
/** Set Reserved / Evil flag */
int NpingOps::setRF(){
this->rf = true;
this->rf_set = true;
return OP_SUCCESS;
} /* End of setRF() */
/** Get Reserved / Evil flag */
bool NpingOps::getRF(){
return this->rf;
} /* End of getRF() */
/* Returns true if option has been set */
bool NpingOps::issetMF(){
return this->mf_set;
} /* End of isset() */
/* Returns true if option has been set */
bool NpingOps::issetDF(){
return this->df_set;
} /* End of isset() */
/* Returns true if option has been set */
bool NpingOps::issetRF(){
return this->rf_set;
} /* End of isset() */
/** Sets Maximum Transmission Unit length. Supplied parameter must be a positive
* integer and must be a multiple of 8.
* @return OP_SUCCESS on success and OP_FAILURE in case of error. */
int NpingOps::setMTU(u32 t){
if(t==0 || (t%8)!=0){
nping_fatal(QT_3,"setMTU(): Invalid mtu supplied\n");
}else{
this->mtu=t;
this->mtu_set=true;
}
return OP_SUCCESS;
} /* End of setMTU() */
/** Returns value of attribute mtu */
u32 NpingOps::getMTU(){
return this->mtu;
} /* End of getMTU() */
/* Returns true if option has been set */
bool NpingOps::issetMTU(){
return this->mtu_set;
} /* End of issetMTU() */
/** Sets attribute badsum_ip to "true". (Generate invalid checksums in IP
* packets)
* @return previous value of the attribute. */
bool NpingOps::enableBadsumIP() {
bool prev = this->badsum_ip;
this->badsum_ip=true;
this->badsum_ip_set=true;
return prev;
} /* End of enableBadsumIP() */
/** Sets attribute badsum_ip to "false". (Do NOT Generate invalid checksums
* in IP packets)
* @return previous value of the attribute. */
bool NpingOps::disableBadsumIP() {
bool prev = badsum_ip;
badsum_ip=false;
this->badsum_ip_set=true;
return prev;
} /* End of disableBadsumIP() */
/** Returns value of attribute badsum_ip */
bool NpingOps::getBadsumIP() {
return this->badsum_ip;
} /* End of getBadsumIP() */
/* Returns true if option has been set */
bool NpingOps::issetBadsumIP(){
return this->badsum_ip_set;
} /* End of issetBadsumIP() */
/** @warning Supplied parameter must be in NETWORK byte order */
int NpingOps::setIPv4SourceAddress(struct in_addr i){
this->ipv4_src_address=i;
this->ipv4_src_address_set=true;
return OP_SUCCESS;
} /* End of setIPv4SourceAddress() */
struct in_addr NpingOps::getIPv4SourceAddress(){
return ipv4_src_address;
} /* End of getIPv4SourceAddress() */
/* Returns true if option has been set */
bool NpingOps::issetIPv4SourceAddress(){
return this->ipv4_src_address_set;
} /* End of issetIPv4SourceAddress() */
/** @warning This method makes a copy of the supplied buffer. That copy will
* be free()ed by the NpingOps destructor. */
int NpingOps::setIPOptions(char *txt){
if (txt==NULL)
nping_fatal(QT_3,"setIPOptions(): NULL pointer supplied\n");
this->ip_options=strdup(txt) ;
this->ip_options_set=true;
return OP_SUCCESS;
} /* End of setIPOptions() */
char *NpingOps::getIPOptions(){
return this->ip_options;
} /* End of getIPOptions() */
bool NpingOps::issetIPOptions(){
return this->ip_options_set;
} /* End of issetIPOptions() */
/******************************************************************************
* Internet Protocol Version 6 *
******************************************************************************/
/** Sets TrafficClass.
* @return OP_SUCCESS on success and OP_FAILURE in case of error. */
int NpingOps::setTrafficClass(u8 val){
this->ipv6_tclass=val;
this->ipv6_tclass_set=true;
return OP_SUCCESS;
} /* End of setTrafficClass() */
/** Returns value of attribute ipv6_tclass */
u8 NpingOps::getTrafficClass(){
return this->ipv6_tclass;
} /* End of getTrafficClass() */
/* Returns true if option has been set */
bool NpingOps::issetTrafficClass(){
return this->ipv6_tclass_set;
} /* End of issetTrafficClass() */
/** Sets FlowLabel.
* @return OP_SUCCESS on success and OP_FAILURE in case of error. */
int NpingOps::setFlowLabel(u32 val){
this->ipv6_flowlabel=val;
this->ipv6_flowlabel_set=true;
return OP_SUCCESS;
} /* End of setFlowLabel() */
/** Returns value of attribute ipv6_flowlabel */
u32 NpingOps::getFlowLabel(){
return this->ipv6_flowlabel;
} /* End of getFlowLabel() */
/* Returns true if option has been set */
bool NpingOps::issetFlowLabel(){
return this->ipv6_flowlabel_set;
} /* End of issetFlowLabel() */
int NpingOps::setIPv6SourceAddress(u8 *val){
if(val==NULL)
nping_fatal(QT_3,"setIPv6SourceAddress(): NULL pointer supplied\n");
memcpy(this->ipv6_src_address.s6_addr, val, 16);
this->ipv6_src_address_set=true;
return OP_SUCCESS;
} /* End of setIPv6SourceAddress() */
int NpingOps::setIPv6SourceAddress(struct in6_addr val){
this->ipv6_src_address = val;
this->ipv6_src_address_set=true;
return OP_SUCCESS;
} /* End of setIPv6SourceAddress() */
struct in6_addr NpingOps::getIPv6SourceAddress(){
return ipv6_src_address;
} /* End of getIPv6SourceAddress() */
/* Returns true if option has been set */
bool NpingOps::issetIPv6SourceAddress(){
return this->ipv6_src_address_set;
} /* End of issetIPv6SourceAddress() */
/* Returns a pointer to a sockaddr_storage structure that contains the
* source IP address. This function takes into account this->getIPVersion()
* an returns an IPv4 sockaddr_in or an IPv6 sockaddr_in6 struct. */
struct sockaddr_storage *NpingOps::getSourceSockAddr(){
static struct sockaddr_storage ss;
return getSourceSockAddr(&ss);
} /* End of getSourceSockAddr() */
/* Returns a pointer to the supplied sockaddr_storage structure that now
* contains the source IP address. This function takes into account
* this->getIPVersion() an returns an IPv4 sockaddr_in or an IPv6
* sockaddr_in6 struct. */
struct sockaddr_storage *NpingOps::getSourceSockAddr(struct sockaddr_storage *ss){
struct sockaddr_in *s4 = (struct sockaddr_in*)ss;
struct sockaddr_in6 *s6 = (struct sockaddr_in6*)ss;
memset(ss, 0, sizeof(struct sockaddr_storage));
if( this->getIPVersion() == IP_VERSION_4){
if(this->spoofSource())
s4->sin_addr=getIPv4SourceAddress();
else
s4->sin_addr.s_addr=INADDR_ANY;
s4->sin_family=AF_INET;
if(this->issetSourcePort())
s4->sin_port=htons(this->getSourcePort());
else
s4->sin_port=0;
}
else if (this->getIPVersion() == IP_VERSION_6){
if(this->spoofSource())
s6->sin6_addr=this->getIPv6SourceAddress();
else
s6->sin6_addr=in6addr_any;
s6->sin6_addr=this->getIPv6SourceAddress();
s6->sin6_family=AF_INET6;
if(this->issetSourcePort())
s6->sin6_port=htons(this->getSourcePort());
else
s6->sin6_port=0;
}else{
nping_fatal(QT_3, "NpingOps::getSourceSockAddr(): IP version unset.");
}
return ss;
} /* End of getSourceSockAddr() */
/******************************************************************************
* Transmission Control Protocol and User Datagram Protocol *
******************************************************************************/
/** @warning Returned ports are in HOST byte order */
u16 *NpingOps::getTargetPorts( int *len ){
if( this->tportcount <= 0)
return NULL;
if(len!=NULL)
*len=this->tportcount;
return this->target_ports;
} /* End of getTargetPorts() */
/** @warning ports in the supplied array must be in HOST byte order */
int NpingOps::setTargetPorts( u16 *pnt, int n ){
if(this->tportcount>65536 || this->tportcount<0)
nping_fatal(QT_3, "setTargetPorts():: Invalid number of ports supplied.");
this->target_ports=pnt;
this->tportcount=n;
this->target_ports_set=true;
return OP_SUCCESS;
} /* End of setTargetPorts() */
/* Returns true if option has been set */
bool NpingOps::issetTargetPorts(){
return this->target_ports_set;
} /* End of issetTargetPorts() */
/*Returns true if the scan type can use the -p option*/
bool NpingOps::scan_mode_uses_target_ports(int mode){
return (mode==TCP_CONNECT || mode==TCP || mode == UDP || mode == UDP_UNPRIV);
} /*End of scan_mode_uses_target_ports*/
/** Sets TCP/UPD source port. Supplied parameter must be an integer >=0 &&
* <=65535
* @return OP_SUCCESS on success and OP_FAILURE in case of error. */
int NpingOps::setSourcePort(u16 val){
this->source_port=val;
this->source_port_set=true;
return OP_SUCCESS;
} /* End of setSourcePort() */
/** Returns value of attribute source_port */
u16 NpingOps::getSourcePort(){
return this->source_port;
} /* End of getSourcePort() */
/* Returns true if option has been set */
bool NpingOps::issetSourcePort(){
return this->source_port_set;
} /* End of issetSourcePort() */
/** Sets TCP Seq number. Supplied parameter must be a positive integer between
* 0 and 2^32 -1
* @return OP_SUCCESS on success and OP_FAILURE in case of error. */
int NpingOps::setTCPSequence(u32 val){
this->tcpseq=val;
this->tcpseq_set=true;
return OP_SUCCESS;
} /* End of setTCPSequence() */
/** Returns value of attribute tcpseq */
u32 NpingOps::getTCPSequence(){
return this->tcpseq;
} /* End of getTCPSequence() */
/** Returns true if option has been set */
bool NpingOps::issetTCPSequence(){
return this->tcpseq_set;
} /* End of issetTCPSequence() */
/** Sets TCP Ack. Supplied parameter must be a positive integer between 0 and
* 2^32 -1
* @return OP_SUCCESS */
int NpingOps::setTCPAck(u32 val){
this->tcpack=val;
this->tcpack_set=true;
return OP_SUCCESS;
} /* End of setTCPAck() */
/** Returns value of attribute tcpack */
u32 NpingOps::getTCPAck(){
return this->tcpack;
} /* End of getTCPAck() */
/** Returns true if option has been set */
bool NpingOps::issetTCPAck(){
return this->tcpack_set;
} /* End of issetTCPAck() */
int NpingOps::setFlagTCP(int flag){
if (flag < FLAG_CWR || flag > FLAG_FIN)
nping_fatal(QT_3,"setFlagTCP(): Invalid flag supplied\n");
else
this->tcpflags[flag]=1;
this->tcpflags_set=true;
return OP_SUCCESS;
} /* End of setFlagTCP() */
int NpingOps::setAllFlagsTCP(){
for(int i=FLAG_CWR; i<=FLAG_FIN; i++)
this->tcpflags[i]=1;
this->tcpflags_set=true;
return OP_SUCCESS;
} /* End of setFlagTCP() */
int NpingOps::unsetAllFlagsTCP(){
for(int i=FLAG_CWR; i<=FLAG_FIN; i++)
this->tcpflags[i]=0;
this->tcpflags_set=true;
return OP_SUCCESS;
} /* End of setFlagTCP() */
int NpingOps::getFlagTCP(int flag){
if (flag < FLAG_CWR || flag > FLAG_FIN)
nping_fatal(QT_3,"setFlagTCP(): Invalid flag supplied\n");
return this->tcpflags[flag];
} /* End of getFlagTCP() */
u8 NpingOps::getTCPFlags(){
u8 octet=0x00;
if(this->getFlagTCP(FLAG_CWR))
octet |= TH_CWR;
if(this->getFlagTCP(FLAG_ECN))
octet |= TH_ECN;
if(this->getFlagTCP(FLAG_URG))
octet |= TH_URG;
if(this->getFlagTCP(FLAG_ACK))
octet |= TH_ACK;
if(this->getFlagTCP(FLAG_PSH))
octet |= TH_PSH;
if(this->getFlagTCP(FLAG_RST))
octet |= TH_RST;
if(this->getFlagTCP(FLAG_SYN))
octet |= TH_SYN;
if(this->getFlagTCP(FLAG_FIN))
octet |= TH_FIN;
return octet;
} /* End of getTCPFlags() */
/* Returns true if option has been set */
bool NpingOps::issetTCPFlags(){
return this->tcpflags_set;
} /* End of isset() */
/** Sets TCP Window. Supplied parameter must be a positive integer between 0 and
* 2^32 -1
* @return OP_SUCCESS */
int NpingOps::setTCPWindow(u16 val){
this->tcpwin=val;
this->tcpwin_set=true;
return OP_SUCCESS;
} /* End of setTCPWindow() */
/** Returns value of attribute tcpwin */
u16 NpingOps::getTCPWindow(){
return this->tcpwin;
} /* End of getTCPWindow() */
/** Returns true if option has been set */
bool NpingOps::issetTCPWindow(){
return this->tcpwin_set;
} /* End of issetTCPWindow() */
/** Sets attribute badsum to "true". (Generate invalid checksums in UDP / TCP
* packets)
* @return previous value of the attribute. */
bool NpingOps::enableBadsum() {
bool prev = this->badsum;
this->badsum=true;
this->badsum_set=true;
return prev;
} /* End of enableBadsumTCP() */
/** Sets attribute traceroute to "false". (Do NOT Generate invalid checksums
* in UDP / TCP packets)
* @return previous value of the attribute. */
bool NpingOps::disableBadsum() {
bool prev = this->badsum;
this->badsum=false;
this->badsum_set=true;
return prev;
} /* End of disableBadsum() */
/** Returns value of attribute badsum */
bool NpingOps::getBadsum() {
return this->badsum;
} /* End of getBadsum() */
/* Returns true if option has been set */
bool NpingOps::issetBadsum(){
return this->badsum_set;
} /* End of issetBadsum() */
/******************************************************************************
* Internet Control Message Protocol *
******************************************************************************/
/** Sets ICMPType.
* @return OP_SUCCESS on success and OP_FAILURE in case of error. */
int NpingOps::setICMPType(u8 val){
this->icmp_type=val;
this->icmp_type_set=true;
return OP_SUCCESS;
} /* End of setICMPType() */
/** Returns value of attribute icmp_type */
u8 NpingOps::getICMPType(){
return this->icmp_type;
} /* End of getICMPType() */
/* Returns true if option has been set */
bool NpingOps::issetICMPType(){
return this->icmp_type_set;
} /* End of issetICMPType() */
/** Sets ICMPCode.
* @return OP_SUCCESS on success and OP_FAILURE in case of error. */
int NpingOps::setICMPCode(u8 val){
this->icmp_code=val;
this->icmp_code_set=true;
return OP_SUCCESS;
} /* End of setICMPCode() */
/** Returns value of attribute icmp_code */
u8 NpingOps::getICMPCode(){
return this->icmp_code;
} /* End of getICMPCode() */
/* Returns true if option has been set */
bool NpingOps::issetICMPCode(){
return this->icmp_code_set;
} /* End of issetICMPCode() */
/** Sets attribute badsum_icmp to "true". (Generate invalid checksums in ICMP
* packets)
* @return previous value of the attribute. */
bool NpingOps::enableBadsumICMP() {
bool prev = this->badsum_icmp;
this->badsum_icmp=true;
this->badsum_icmp_set=true;
return prev;
} /* End of enableBadsumICMPTCP() */
/** Sets attribute traceroute to "false". (Do NOT Generate invalid checksums
* in UDP / TCP packets)
* @return previous value of the attribute. */
bool NpingOps::disableBadsumICMP() {
bool prev = this->badsum_icmp;
this->badsum_icmp=false;
this->badsum_icmp_set=true;
return prev;
} /* End of disableBadsumICMP() */
/** Returns value of attribute badsum_icmp */
bool NpingOps::getBadsumICMP() {
return this->badsum_icmp;
} /* End of getBadsumICMP() */
/* Returns true if option has been set */
bool NpingOps::issetBadsumICMP(){
return this->badsum_icmp_set;
} /* End of issetBadsumICMP() */
/** Sets ICMPRedirectAddress.
* @return OP_SUCCESS on success and OP_FAILURE in case of error. */
int NpingOps::setICMPRedirectAddress(struct in_addr val){
this->icmp_redir_addr=val;
this->icmp_redir_addr_set=true;
return OP_SUCCESS;
} /* End of setICMPRedirectAddress() */
/** Returns value of attribute icmp_redir_addr */
struct in_addr NpingOps::getICMPRedirectAddress(){
return this->icmp_redir_addr;
} /* End of getICMPRedirectAddress() */
/* Returns true if option has been set */
bool NpingOps::issetICMPRedirectAddress(){
return this->icmp_redir_addr_set;
} /* End of issetICMPRedirectAddress() */
/** Sets ICMPParamProblemPointer.
* @return OP_SUCCESS on success and OP_FAILURE in case of error. */
int NpingOps::setICMPParamProblemPointer(u8 val){
this->icmp_paramprob_pnt=val;
this->icmp_paramprob_pnt_set=true;
return OP_SUCCESS;
} /* End of setICMPParamProblemPointer() */
/** Returns value of attribute icmp_paramprob_pnt */
u8 NpingOps::getICMPParamProblemPointer(){
return this->icmp_paramprob_pnt;
} /* End of getICMPParamProblemPointer() */
/* Returns true if option has been set */
bool NpingOps::issetICMPParamProblemPointer(){
return this->icmp_paramprob_pnt_set;
} /* End of issetICMPParamProblemPointer() */
/** Sets ICMPRouterAdvLifetime.
* @return OP_SUCCESS on success and OP_FAILURE in case of error. */
int NpingOps::setICMPRouterAdvLifetime(u16 val){
this->icmp_routeadv_ltime=val;
this->icmp_routeadv_ltime_set=true;
return OP_SUCCESS;
} /* End of setICMPRouterAdvLifetime() */
/** Returns value of attribute icmp_routeadv_ltime */
u16 NpingOps::getICMPRouterAdvLifetime(){
return this->icmp_routeadv_ltime;
} /* End of getICMPRouterAdvLifetime() */
/* Returns true if option has been set */
bool NpingOps::issetICMPRouterAdvLifetime(){
return this->icmp_routeadv_ltime_set;
} /* End of issetICMPRouterAdvLifetime() */
/** Sets ICMPIdentifier.
* @return OP_SUCCESS on success and OP_FAILURE in case of error. */
int NpingOps::setICMPIdentifier(u16 val){
this->icmp_id=val;
this->icmp_id_set=true;
return OP_SUCCESS;
} /* End of setICMPIdentifier() */
/** Returns value of attribute icmp_id */
u16 NpingOps::getICMPIdentifier(){
return this->icmp_id;
} /* End of getICMPIdentifier() */
/* Returns true if option has been set */
bool NpingOps::issetICMPIdentifier(){
return this->icmp_id_set;
} /* End of issetICMPIdentifier() */
/** Sets ICMPSequence.
* @return OP_SUCCESS on success and OP_FAILURE in case of error. */
int NpingOps::setICMPSequence(u16 val){
this->icmp_seq=val;
this->icmp_seq_set=true;
return OP_SUCCESS;
} /* End of setICMPSequence() */
/** Returns value of attribute icmp_seq */
u16 NpingOps::getICMPSequence(){
return this->icmp_seq;
} /* End of getICMPSequence() */
/* Returns true if option has been set */
bool NpingOps::issetICMPSequence(){
return this->icmp_seq_set;
} /* End of issetICMPSequence() */
/** Sets ICMPOriginateTimestamp.
* @return OP_SUCCESS on success and OP_FAILURE in case of error. */
int NpingOps::setICMPOriginateTimestamp(u32 val){
this->icmp_orig_time=val;
this->icmp_orig_time_set=true;
return OP_SUCCESS;
} /* End of setICMPOriginateTimestamp() */
/** Returns value of attribute icmp_orig_time */
u32 NpingOps::getICMPOriginateTimestamp(){
return this->icmp_orig_time;
} /* End of getICMPOriginateTimestamp() */
/* Returns true if option has been set */
bool NpingOps::issetICMPOriginateTimestamp(){
return this->icmp_orig_time_set;
} /* End of issetICMPOriginateTimestamp() */
/** Sets ICMPReceiveTimestamp.
* @return OP_SUCCESS on success and OP_FAILURE in case of error. */
int NpingOps::setICMPReceiveTimestamp(u32 val){
this->icmp_recv_time=val;
this->icmp_recv_time_set=true;
return OP_SUCCESS;
} /* End of setICMPReceiveTimestamp() */
/** Returns value of attribute icmp_recv_time */
u32 NpingOps::getICMPReceiveTimestamp(){
return this->icmp_recv_time;
} /* End of getICMPReceiveTimestamp() */
/* Returns true if option has been set */
bool NpingOps::issetICMPReceiveTimestamp(){
return this->icmp_recv_time_set;
} /* End of issetICMPReceiveTimestamp() */
/** Sets ICMPTransmitTimestamp.
* @return OP_SUCCESS on success and OP_FAILURE in case of error. */
int NpingOps::setICMPTransmitTimestamp(u32 val){
this->icmp_trans_time=val;
this->icmp_trans_time_set=true;
return OP_SUCCESS;
} /* End of setICMPTransmitTimestamp() */
/** Returns value of attribute icmp_trans_time */
u32 NpingOps::getICMPTransmitTimestamp(){
return this->icmp_trans_time;
} /* End of getICMPTransmitTimestamp() */
/* Returns true if option has been set */
bool NpingOps::issetICMPTransmitTimestamp(){
return this->icmp_trans_time_set;
} /* End of issetICMPTransmitTimestamp() */
int NpingOps::addICMPAdvertEntry(struct in_addr addr, u32 pref ){
if( this->icmp_advert_entry_count > MAX_ICMP_ADVERT_ENTRIES )
return OP_FAILURE;
this->icmp_advert_entry_addr[this->icmp_advert_entry_count] = addr;
this->icmp_advert_entry_pref[this->icmp_advert_entry_count] = pref;
this->icmp_advert_entry_count++;
this->icmp_advert_entry_set=true;
return OP_SUCCESS;
} /* End of addICMPAdvertEntry() */
/** @param num means that the caller wants to obtain the num-th entry.
* Count starts in 0 so the supplied value must be
* 0 <= num < getICMPAdvertEntryCount() */
int NpingOps::getICMPAdvertEntry(int num, struct in_addr *addr, u32 *pref){
if( num<0 || num>=icmp_advert_entry_count )
nping_fatal(QT_3,"getICMPAdvertEntry(): Supplied index is out of bounds.\n");
if( addr==NULL || pref==NULL)
nping_fatal(QT_3,"getICMPAdvertEntry(): NULL pointer supplied\n");
*addr = this->icmp_advert_entry_addr[num];
*pref = this->icmp_advert_entry_pref[num];
return OP_SUCCESS;
} /* End of getICMPAdvertEntry() */
int NpingOps::getICMPAdvertEntryCount(){
return this->icmp_advert_entry_count;
} /* End of getICMPAdvertEntryCount()*/
bool NpingOps::issetICMPAdvertEntry(){
return this->icmp_advert_entry_set;
} /* End of issetICMPAdvertEntry()*/
/******************************************************************************
* Ethernet *
******************************************************************************/
/** Sets SourceMAC.
* @return OP_SUCCESS on success and OP_FAILURE in case of error. */
int NpingOps::setSourceMAC(u8 * val){
memcpy(this->src_mac, val, 6);
this->src_mac_set=true;
return OP_SUCCESS;
} /* End of setSourceMAC() */
/** Returns value of attribute src_mac */
u8 * NpingOps::getSourceMAC(){
return this->src_mac;
} /* End of getSourceMAC() */
/* Returns true if option has been set */
bool NpingOps::issetSourceMAC(){
return this->src_mac_set;
} /* End of issetSourceMAC() */
/** Sets DestMAC.
* @return OP_SUCCESS on success and OP_FAILURE in case of error. */
int NpingOps::setDestMAC(u8 * val){
memcpy(this->dst_mac, val, 6);
this->dst_mac_set=true;
return OP_SUCCESS;
} /* End of setDestMAC() */
/** Returns value of attribute dst_mac */
u8 * NpingOps::getDestMAC(){
return this->dst_mac;
} /* End of getDestMAC() */
/* Returns true if option has been set */
bool NpingOps::issetDestMAC(){
return this->dst_mac_set;
} /* End of issetDestMAC() */
/** Sets EtherType.
* @return OP_SUCCESS on success and OP_FAILURE in case of error. */
int NpingOps::setEtherType(u16 val){
this->eth_type=val;
this->eth_type_set=true;
return OP_SUCCESS;
} /* End of setEtherType() */
/** Returns value of attribute eth_type */
u16 NpingOps::getEtherType(){
return this->eth_type;
} /* End of getEtherType() */
/* Returns true if option has been set */
bool NpingOps::issetEtherType(){
return this->eth_type_set;
} /* End of issetEtherType() */
/******************************************************************************
* Address Resolution Protocol / Reverse Address Resolution Protocol *
******************************************************************************/
/** Sets ARPHardwareType.
* @return OP_SUCCESS on success and OP_FAILURE in case of error. */
int NpingOps::setARPHardwareType(u16 val){
this->arp_htype=val;
this->arp_htype_set=true;
return OP_SUCCESS;
} /* End of setARPHardwareType() */
/** Returns value of attribute arp_htype */
u16 NpingOps::getARPHardwareType(){
return this->arp_htype;
} /* End of getARPHardwareType() */
/* Returns true if option has been set */
bool NpingOps::issetARPHardwareType(){
return this->arp_htype_set;
} /* End of issetARPHardwareType() */
/** Sets ARPProtocolType.
* @return OP_SUCCESS on success and OP_FAILURE in case of error. */
int NpingOps::setARPProtocolType(u16 val){
this->arp_ptype=val;
this->arp_ptype_set=true;
return OP_SUCCESS;
} /* End of setARPProtocolType() */
/** Returns value of attribute arp_ptype */
u16 NpingOps::getARPProtocolType(){
return this->arp_ptype;
} /* End of getARPProtocolType() */
/* Returns true if option has been set */
bool NpingOps::issetARPProtocolType(){
return this->arp_ptype_set;
} /* End of issetARPProtocolType() */
/** Sets ARPHwAddrLen.
* @return OP_SUCCESS on success and OP_FAILURE in case of error. */
int NpingOps::setARPHwAddrLen(u8 val){
this->arp_hlen=val;
this->arp_hlen_set=true;
return OP_SUCCESS;
} /* End of setARPHwAddrLen() */
/** Returns value of attribute arp_hlen */
u8 NpingOps::getARPHwAddrLen(){
return this->arp_hlen;
} /* End of getARPHwAddrLen() */
/* Returns true if option has been set */
bool NpingOps::issetARPHwAddrLen(){
return this->arp_hlen_set;
} /* End of issetARPHwAddrLen() */
/** Sets ARPProtoAddrLen.
* @return OP_SUCCESS on success and OP_FAILURE in case of error. */
int NpingOps::setARPProtoAddrLen(u8 val){
this->arp_plen=val;
this->arp_plen_set=true;
return OP_SUCCESS;
} /* End of setARPProtoAddrLen() */
/** Returns value of attribute arp_plen */
u8 NpingOps::getARPProtoAddrLen(){
return this->arp_plen;
} /* End of getARPProtoAddrLen() */
/* Returns true if option has been set */
bool NpingOps::issetARPProtoAddrLen(){
return this->arp_plen_set;
} /* End of issetARPProtoAddrLen() */
/** Sets ARPOpCode.
* @return OP_SUCCESS on success and OP_FAILURE in case of error. */
int NpingOps::setARPOpCode(u16 val){
this->arp_opcode=val;
this->arp_opcode_set=true;
return OP_SUCCESS;
} /* End of setARPOpCode() */
/** Returns value of attribute arp_opcode */
u16 NpingOps::getARPOpCode(){
return this->arp_opcode;
} /* End of getARPOpCode() */
/* Returns true if option has been set */
bool NpingOps::issetARPOpCode(){
return this->arp_opcode_set;
} /* End of issetARPOpCode() */
/** Sets ARPSenderHwAddr.
* @return OP_SUCCESS on success and OP_FAILURE in case of error. */
int NpingOps::setARPSenderHwAddr(u8 * val){
memcpy(this->arp_sha, val, 6); /* MAC Address (6 bytes) */
this->arp_sha_set=true;
return OP_SUCCESS;
} /* End of setARPSenderHwAddr() */
/** Returns value of attribute arp_sha */
u8 * NpingOps::getARPSenderHwAddr(){
return this->arp_sha;
} /* End of getARPSenderHwAddr() */
/* Returns true if option has been set */
bool NpingOps::issetARPSenderHwAddr(){
return this->arp_sha_set;
} /* End of issetARPSenderHwAddr() */
/** Sets ARPTargetHwAddr.
* @return OP_SUCCESS on success and OP_FAILURE in case of error. */
int NpingOps::setARPTargetHwAddr(u8 * val){
memcpy(this->arp_tha, val, 6); /* MAC Address (6 bytes) */
this->arp_tha_set=true;
return OP_SUCCESS;
} /* End of setARPTargetHwAddr() */
/** Returns value of attribute arp_tha */
u8 * NpingOps::getARPTargetHwAddr(){
return this->arp_tha;
} /* End of getARPTargetHwAddr() */
/* Returns true if option has been set */
bool NpingOps::issetARPTargetHwAddr(){
return this->arp_tha_set;
} /* End of issetARPTargetHwAddr() */
/** Sets ARPSenderProtoAddr.
* @return OP_SUCCESS on success and OP_FAILURE in case of error. */
int NpingOps::setARPSenderProtoAddr(struct in_addr val){
this->arp_spa=val;
this->arp_spa_set=true;
return OP_SUCCESS;
} /* End of setARPSenderProtoAddr() */
/** Returns value of attribute arp_spa */
struct in_addr NpingOps::getARPSenderProtoAddr(){
return this->arp_spa;
} /* End of getARPSenderProtoAddr() */
/* Returns true if option has been set */
bool NpingOps::issetARPSenderProtoAddr(){
return this->arp_spa_set;
} /* End of issetARPSenderProtoAddr() */
/** Sets ARPTargetProtoAddr.
* @return OP_SUCCESS on success and OP_FAILURE in case of error. */
int NpingOps::setARPTargetProtoAddr(struct in_addr val){
this->arp_tpa=val;
this->arp_tpa_set=true;
return OP_SUCCESS;
} /* End of setARPTargetProtoAddr() */
/** Returns value of attribute arp_tpa */
struct in_addr NpingOps::getARPTargetProtoAddr(){
return this->arp_tpa;
} /* End of getARPTargetProtoAddr() */
/* Returns true if option has been set */
bool NpingOps::issetARPTargetProtoAddr(){
return this->arp_tpa_set;
} /* End of issetARPTargetProtoAddr() */
/** Sets EchoPort.
* @return OP_SUCCESS on success and OP_FAILURE in case of error. */
int NpingOps::setEchoPort(u16 val){
this->echo_port=val;
this->echo_port_set=true;
return OP_SUCCESS;
} /* End of setEchoPort() */
/** Returns value of attribute echo_port */
u16 NpingOps::getEchoPort(){
return this->echo_port;
} /* End of getEchoPort() */
/* Returns true if option has been set */
bool NpingOps::issetEchoPort(){
return this->echo_port_set;
} /* End of issetEchoPort() */
int NpingOps::setEchoPassphrase(const char *str){
strncpy(this->echo_passphrase, str, sizeof(echo_passphrase)-1);
this->echo_passphrase_set=true;
return OP_SUCCESS;
} /* End of setEchoPassphrase() */
char *NpingOps::getEchoPassphrase(){
return this->echo_passphrase;
} /* End of getEchoPassphrase() */
bool NpingOps::issetEchoPassphrase(){
return this->echo_passphrase_set;
} /* End of issetEchoPassphrase() */
/** Sets value of this->echo_server_once. When true, the echo server
* will exit after attending one client. */
int NpingOps::setOnce(bool val){
this->echo_server_once=val;
this->echo_server_once_set=true;
return OP_SUCCESS;
} /* End of once() */
/** Returns value of attribute echo_port */
bool NpingOps::once(){
return this->echo_server_once;
} /* End of once() */
/******************************************************************************
* Option Validation *
******************************************************************************/
void NpingOps::validateOptions() {
/** DETERMINE ROOT PRIVILEGES ************************************************/
const char *privreq = "root privileges";
#ifdef WIN32
//if (!this->have_pcap)
privreq = "Npcap, but it seems to be missing.\n\
Npcap is available from https://npcap.com. The Npcap driver service must\n\
be started by an administrator before Npcap can be used. Running nping.exe\n\
will open a UAC dialog where you can start the service if you have\n\
administrator privileges.";
#endif
if (this->havePcap()==false){
#ifdef WIN32
nping_fatal(QT_3, "Nping requires %s", privreq);
#else
nping_fatal(QT_3, "Nping requires libpcap to be installed on your system.");
#endif
}
/** ROLE SELECTION ***********************************************************/
/* Ensure that at least one role is selected */
if ( !this->issetRole() ) {
this->setRoleNormal();
}
/** TARGET SPECIFICATION *****************************************************/
/* Check if user entered at least one target spec */
if( this->getRole() == ROLE_NORMAL ){
if ( this->targets.getTargetSpecCount() <= 0 )
nping_fatal(QT_3,"WARNING: No targets were specified, so 0 hosts pinged.");
}else if( this->getRole() == ROLE_CLIENT ){
if ( this->targets.getTargetSpecCount() <= 0 )
nping_fatal(QT_3,"No echo server was specified.");
}
/** IP VERSION ***************************************************************/
/* Default to IP version 4 */
if( !this->issetIPVersion() )
this->setIPVersion( IP_VERSION_4 );
/** PROBE MODE SELECTION *****************************************************/
/* Ensure that one probe mode is selected */
if( !this->issetMode() ){
if ( this->isRoot() ){
if( !this->ipv6() )
this->setMode(ICMP);
else
this->setMode(TCP);
}
else
this->setMode(TCP_CONNECT);
}
/** PACKET COUNT / ROUNDS ****************************************************/
if( !this->issetPacketCount() ){
/* If --traceroute is set, the packet count is higher */
if(this->issetTraceroute() )
this->setPacketCount( TRACEROUTE_PACKET_COUNT );
else
this->setPacketCount( DEFAULT_PACKET_COUNT );
}
if( !this->issetDelay() )
this->setDelay( DEFAULT_DELAY );
/** UDP UNPRIVILEGED MODE? ***************************************************/
/* If user is NOT root and specified UDP mode, check if he did not specify
* any option that requires privileges. In that case, we enter
* UDP-Unprivileged mode, where users can send UDP packets and read responses
* trough a normal UDP socket. */
if( !this->isRoot() && this->getMode()==UDP && canRunUDPWithoutPrivileges() )
this->setMode( UDP_UNPRIV );
/** CHECK PRIVILEGES FOR CURRENT ROLE ****************************************/
if( !this->isRoot() && (this->getRole()==ROLE_SERVER || this->getRole()==ROLE_CLIENT) )
nping_fatal(QT_3,"Echo mode requires %s.", privreq);
/** CHECK PRIVILEGES FOR CURRENT MODE ****************************************/
if( !this->isRoot() && this->getMode()!=UDP_UNPRIV && this->getMode()!=TCP_CONNECT )
nping_fatal(QT_3,"Mode %s requires %s.", this->mode2Ascii( this->getMode() ), privreq);
/** DEFAULT HEADER PARAMETERS *************************************************/
this->setDefaultHeaderValues();
/** ARP MODE RELATED PARAMETERS *********************************************/
if(this->getMode()==ARP && this->ipv6()) {
nping_fatal(QT_3, "Sorry, ARP does not support IPv6 and Nping does not yet support NDP.");
}
/** TCP CONNECT RELATED PARAMETERS *********************************************/
if(this->getMode()==TCP_CONNECT) {
if(this->issetPayloadBuffer())
nping_print(VB_0, "Warning: Payload supplied in TCP Connect mode. Payload will be ignored.");
}
/** SOURCE IP, SOURCE MAC and NETWORK DEVICE *********************************/
/* If we are in a mode where we need to craft IP packets, then we need to
* obtain a network interface name and a source IP address. There are three
* different possibilities:
* 1. User did NOT specify both network interface and source IP address.
* 2. User did specify a network interface but not a source IP address.
* 3. User did actually supply a source IP but not a network interface name
*
* I know the following code is ugly but the thing is that we want to determine
* interface and source IP without user intervention, so we try in many ways
* until either we succeed or we run out of possibilities and fatal().
*/
if( this->getMode()!=TCP_CONNECT && this->getMode()!=UDP_UNPRIV && this->getRole()!=ROLE_SERVER){
char devbuff[32];
char *dev;
struct sockaddr_storage ss, ifaddr;
struct sockaddr_in *s4=(struct sockaddr_in *)&ifaddr;
struct sockaddr_in6 *s6=(struct sockaddr_in6 *)&ifaddr;
size_t ss_len;
char hostname[128];
memset(&ss, 0, sizeof(struct sockaddr_storage));
memset(&ifaddr, 0, sizeof(struct sockaddr_storage));
/* CASE 1: User did not specify a device so we have to select one. */
if( !this->issetDevice() ){
if( this->ipv4() ){
/* Ugly hack. Get the first resolvable target and determine net interface. Let's
* hope user did not specify something that mixes localhost with
* other targets, like "nping localhost google.com playboy.com" */
for(int z=0; z<this->targets.getTargetSpecCount(); z++){
if( this->targets.getNextTargetAddressAndName(&ss, &ss_len, hostname, sizeof(hostname)) == OP_SUCCESS )
break;
else if( z>=(this->targets.getTargetSpecCount()-1) )
nping_fatal(QT_3,"Cannot find a valid target. Please make sure the specified hosts are either IP addresses in standard notation or hostnames that can be resolved with DNS");
}
this->targets.rewind();
/* Try to obtain a device name from the target IP */
if ( getNetworkInterfaceName( &ss , devbuff) != OP_SUCCESS ) {
/* If that didn't work, ask libpcap */
if ( (dev = this->select_network_iface()) == NULL)
nping_fatal(QT_3, "Cannot obtain device for packet capture");
else
this->setDevice( dev );
/* Libpcap gave us a device name, try to obtain it's IP */
if ( devname2ipaddr_alt(this->getDevice(), &ifaddr) != 0 ){
if( this->isRoot() )
nping_fatal(QT_3,"Cannot figure out what source address to use for device %s, does it even exist?", this->getDevice());
else
nping_fatal(QT_3,"Cannot figure out what source address to use for device %s, are you root?", this->getDevice());
}
else{
if( s4->sin_family==AF_INET )
this->setIPv4SourceAddress(s4->sin_addr);
else if ( s6->sin6_family==AF_INET6 )
this->setIPv6SourceAddress(s6->sin6_addr.s6_addr);
}
}else{
this->setDevice(devbuff);
}
}else{ /* In IPv6 we just select one in libpcap and hope is the right one */
char *selected_iface=this->select_network_iface();
if(selected_iface==NULL)
nping_fatal(QT_3, "Error trying to find a suitable network interface ");
else
this->setDevice( selected_iface );
}
} /* CASE 2: User did actually supply a device name */
else{
nping_print(DBG_2, "Using network interface \"%s\"", this->getDevice() );
}
/* The echo server needs to find out a network interface*/
}else if (this->getRole()==ROLE_SERVER && this->issetDevice()==false){
char *selected_iface=this->select_network_iface();
if(selected_iface==NULL)
nping_fatal(QT_3, "Error trying to find a suitable network interface ");
else
this->setDevice( selected_iface );
nping_print(DBG_2, "Using network interface \"%s\"", this->getDevice() );
}
/** RAW IP AND RAW ETHERNET TRANSMISSION MODES *******************************/
/* Determine if we need to send at raw ip level or at raw ethernet level */
if(this->getRole()!=ROLE_SERVER){
if (!this->issetSendPreference()) {
/* CASE 1: ARP requested. We have to do raw ethernet transmission */
if(this->getMode()==ARP ){
this->setSendEth(true);
this->setSendPreference( PACKET_SEND_ETH_STRONG );
}
/* CASE 2: If we are dealing with IPv6 we have two options: send at raw
* eth level or sent at raw transport layer level. So here, we check if the
* user has specified some IPv6 header specific options. If he has, we then
* have to use raw ethernet (since we cannot include our own IPv6 header in
* raw IPv6 sockets). If he hasn't, the best way is to send at raw TCP/UDP
* level so we disable sendEth() */
else if (this->ipv6() ){
/* CASE 2.A: If user did not specify custom IPv6 header or Ethernet
* field values go for raw transport layer level transmission */
if( this->canDoIPv6ThroughSocket() ){
this->setSendEth(false);
this->setSendPreference( PACKET_SEND_IP_STRONG );
}
/* CASE 2.B: User wants to set some IPv6 or Ethernet values. So here we
* check if enough parameters were supplied. */
else if (this->canDoIPv6Ethernet() ){
this->setSendEth(true);
this->setSendPreference( PACKET_SEND_ETH_STRONG );
}else{
nping_fatal(QT_3, "If you want to control some of the fields"
" in the IPv6 header you also have to supply source and"
" destination MAC address. However, you can always"
" choose to let the kernel create the IPv6 header"
" choosing not to pass --source-IP, --traffic-class"
" or --flow options. That should simplify things a bit");
}
}
/* CASE 3: We are dealing with regular, IPv4-based modes. In this case
* we just select transmission mode based on current OS. For Windows
* we choose raw eth level because MS has disable raw sockets support.
* For the rest of systems, we chose raw IP because it's easier for us
* as we don't have to deal with all the source MAC and next-hop MAC address
* determination process. */
else{
#ifdef WIN32
this->setSendPreference( PACKET_SEND_ETH_STRONG );
this->setSendEth(true);
#else
this->setSendPreference( PACKET_SEND_IP_WEAK );
this->setSendEth(false);
#endif
}
/* User did actually supplied his own sending preference. Let's check if we
* can actually send probes the way he wants. */
}else{
if( this->getMode()==ARP && !this->sendPreferenceEthernet() ){
this->setSendEth(true);
nping_warning(QT_2, "Warning: ARP mode requires raw ethernet frame transmission. Specified preference will be ignored.");
}
else if( this->ipv6() ){
/* CASE 1: User requested ethernet explicitly and supplied all
* necessary options. */
if( this->sendPreferenceEthernet() && this->canDoIPv6Ethernet() ){
this->setSendEth(true);
/* CASE 2: User requested Ethernet but did not really supplied all
* the information we need */
}else if( this->sendPreferenceEthernet() && !this->canDoIPv6Ethernet() ){
nping_fatal(QT_3, "You requested raw ethernet level transmission and IPv6."
" In this case, you need to supply source MAC address,"
" destination MAC address and IPv6 source address.");
/* CASE 3: User requested raw IP transmission and did not request
* any special IPv6 header options. */
}else if( this->sendPreferenceIP() && this->canDoIPv6ThroughSocket() ){
this->setSendEth(false);
/* CASE 4: User requested raw IP transmission but also wanted to
* set custom IPv6 header field values. */
}else if (this->sendPreferenceIP() && !this->canDoIPv6ThroughSocket()){
nping_fatal(QT_3, "You requested raw IP transmission mode for IPv6."
" Nping does not currently allow IPv6 header manipulation"
" when sending packets at raw IP level due to the limitations"
" on raw IPv6 sockets, imposed by RFC 2292. Please"
" use raw Ethernet transmission (option --send-eth)");
}
}
else if( this->sendPreferenceEthernet() ){
this->setSendEth(true);
}else{
this->setSendEth(false);
}
}
if( this->getMode()==TCP_CONNECT || this->getMode()==UDP_UNPRIV )
nping_print(DBG_2,"Nping will send packets in unprivileged mode using regular system calls");
else
nping_print(DBG_2,"Nping will send packets at %s", this->sendEth() ? "raw ethernet level" : "raw IP level" );
}
/** ECHO MODE ************************************************************/
if(this->getRole()==ROLE_CLIENT){
/* Make sure the nping echo client does not generate packets with tcp
* src port or tcp dst port 9929 (or --echo-port N, if that is set),
* because 1) the echo server does not capture those packets and 2) to
* avoid messing with the established side-channel tcp connection. */
if(this->getMode()==TCP){
for(int i=0; i<tportcount; i++){
if( this->target_ports[i]==this->getEchoPort())
nping_fatal(QT_3, "Packets can't be sent to the same port that is used to connect to the echo server (%d)", this->getEchoPort());
else if(this->getSourcePort()==this->getEchoPort())
nping_fatal(QT_3, "Packets can't be sent from the same port that is used to connect to the echo server (%d)", this->getEchoPort());
}
}
/* Check the echo client only produces TCP/UDP/ICMP packets */
switch( this->getMode() ){
case TCP:
case UDP:
case ICMP:
break;
default:
nping_fatal(QT_3, "The echo client can't be run with protocols other than TCP, UDP or ICMP.");
break;
}
}
#ifndef HAVE_OPENSSL
if(this->getRole()==ROLE_CLIENT || this->getRole()==ROLE_SERVER ){
if( this->doCrypto()==true ){
nping_fatal(QT_3, "Nping was compiled without OpenSSL so authentications need to be transmitted as cleartext. If you wish to continue, please specify --no-crypto.");
}
}
#endif
/** FRAGMENTATION ************************************************************/
#if !defined(LINUX) && !defined(OPENBSD) && !defined(FREEBSD) && !defined(NETBSD)
if (this->issetMTU()) {
error("Warning: Packet fragmentation selected on a host other than Linux, OpenBSD, FreeBSD, or NetBSD. This may or may not work.");
}
#endif
/** MISCELLANEOUS ************************************************************/
if( this->issetSourcePort() && this->getMode()==TCP_CONNECT && this->getPacketCount()>1 )
error("Warning: Setting a source port in TCP-Connect mode with %d rounds may not work after the first round. You may want to do just one round (use --count 1).", this->getPacketCount() );
} /* End of validateOptions() */
/** Returns true if requested mode is a simple TCP connect probe mode */
bool NpingOps::canRunUDPWithoutPrivileges(){
if( this->issetBadsumIP() ||
this->issetTTL() ||
this->issetHopLimit() ||
this->issetTOS() ||
this->issetIdentification() ||
this->issetMF() ||
this->issetDF() ||
this->issetRF() ||
this->issetIPv4SourceAddress() ||
this->issetIPv6SourceAddress() ||
this->issetIPOptions() ||
this->issetMTU() ||
this->issetSpoofSource() ||
this->issetSourceMAC() ||
this->issetDestMAC() ||
this->issetEtherType() ||
this->issetTraceroute() ||
this->issetBPFFilterSpec()
)
return false;
else
return true;
} /* End canRunUDPWithoutPrivileges() */
/** Returns true if user did not request any special ethernet or ipv6 header
* options */
bool NpingOps::canDoIPv6ThroughSocket(){
if( this->issetEtherType() ||
this->issetDestMAC() ||
this->issetSourceMAC() ||
this->issetHopLimit() ||
this->issetTrafficClass() ||
this->issetFlowLabel() ||
this->issetIPv6SourceAddress()
)
return false;
else
return true;
} /* End canDoIPv6ThroughSocket() */
/** Returns true if user supplied all necessary options to allow IPv6 at raw
* Ethernet level */
bool NpingOps::canDoIPv6Ethernet(){
if( this->issetDestMAC() && this->issetSourceMAC() && this->issetIPv6SourceAddress() )
return true;
else
return false;
} /* End canDoIPv6Ethernet() */
/******************************************************************************
* Miscellaneous *
******************************************************************************/
void NpingOps::displayNpingDoneMsg(){
if( this->getRole()==ROLE_SERVER ){
nping_print(QT_1, "Nping done: %lu %s served in %.2f seconds",
(unsigned long)this->stats.getEchoClientsServed(),
(this->stats.getEchoClientsServed() == 1)? "client" : "clients",
this->stats.elapsedRuntime()
);
}else{
nping_print(QT_1, "Nping done: %lu %s pinged in %.2f seconds",
this->targets.getTargetsFetched(),
(this->targets.getTargetsFetched() == 1)? "IP address" : "IP addresses",
this->stats.elapsedRuntime()
);
}
} /* End of displayNpingDoneMessage() */
/** @warning This method calls targets.rewind() */
void NpingOps::displayStatistics(){
char auxbuff[256];
memset(auxbuff, 0, 256);
NpingTarget *target=NULL;
this->targets.rewind();
nping_print(VB_0," "); /* Print newline */
/* Per-target statistics */
if( this->targets.getTargetsFetched() > 1){
while( (target=this->targets.getNextTarget()) != NULL )
target->printStats();
}else{
target=this->targets.getNextTarget();
if( target!= NULL)
target->printRTTs();
}
#ifdef WIN32
/* Sent/Recv/Echoed Packets */
if(this->getRole()==ROLE_CLIENT){
nping_print(QT_1|NO_NEWLINE, "Raw packets sent: %I64u ", this->stats.getSentPackets() );
nping_print(QT_1|NO_NEWLINE, "(%s) ", format_bytecount(this->stats.getSentBytes(), auxbuff, 256));
nping_print(QT_1|NO_NEWLINE,"| Rcvd: %I64u ", this->stats.getRecvPackets() );
nping_print(QT_1|NO_NEWLINE,"(%s) ", format_bytecount(this->stats.getRecvBytes(), auxbuff, 256));
nping_print(QT_1|NO_NEWLINE,"| Lost: %I64u ", this->stats.getLostPackets() );
nping_print(QT_1|NO_NEWLINE,"(%.2lf%%)", this->stats.getLostPacketPercentage100() );
nping_print(QT_1|NO_NEWLINE,"| Echoed: %I64u ", this->stats.getEchoedPackets() );
nping_print(QT_1,"(%s) ", format_bytecount(this->stats.getEchoedBytes(), auxbuff, 256));
}else if(this->getRole()==ROLE_SERVER){
nping_print(QT_1|NO_NEWLINE, "Raw packets captured: %I64u ", this->stats.getRecvPackets() );
nping_print(QT_1|NO_NEWLINE, "(%s) ", format_bytecount(this->stats.getRecvBytes(), auxbuff, 256));
nping_print(QT_1|NO_NEWLINE,"| Echoed: %I64u ", this->stats.getEchoedPackets() );
nping_print(QT_1|NO_NEWLINE,"(%s) ", format_bytecount(this->stats.getEchoedBytes(), auxbuff, 256));
nping_print(QT_1|NO_NEWLINE,"| Not Matched: %I64u ", this->stats.getUnmatchedPackets() );
nping_print(QT_1|NO_NEWLINE,"(%s) ", format_bytecount(this->stats.getRecvBytes()-this->stats.getEchoedBytes(), auxbuff, 256));
nping_print(QT_1,"(%.2lf%%)", this->stats.getUnmatchedPacketPercentage100() );
}else if(this->getMode()==TCP_CONNECT){
nping_print(QT_1|NO_NEWLINE, "TCP connection attempts: %I64u ", this->stats.getSentPackets() );
nping_print(QT_1|NO_NEWLINE,"| Successful connections: %I64u ", this->stats.getRecvPackets() );
nping_print(QT_1|NO_NEWLINE,"| Failed: %I64u ", this->stats.getLostPackets() );
nping_print(QT_1,"(%.2lf%%)", this->stats.getLostPacketPercentage100() );
} else if (this->getMode()==UDP_UNPRIV){
nping_print(QT_1|NO_NEWLINE, "UDP packets sent: %I64u ", this->stats.getSentPackets() );
nping_print(QT_1|NO_NEWLINE,"| Rcvd: %I64u ", this->stats.getRecvPackets() );
nping_print(QT_1|NO_NEWLINE,"| Lost: %I64u ", this->stats.getLostPackets() );
nping_print(QT_1,"(%.2lf%%)", this->stats.getLostPacketPercentage100() );
} else{
nping_print(QT_1|NO_NEWLINE, "Raw packets sent: %I64u ", this->stats.getSentPackets() );
nping_print(QT_1|NO_NEWLINE, "(%s) ", format_bytecount(this->stats.getSentBytes(), auxbuff, 256));
nping_print(QT_1|NO_NEWLINE,"| Rcvd: %I64u ", this->stats.getRecvPackets() );
nping_print(QT_1|NO_NEWLINE,"(%s) ", format_bytecount(this->stats.getRecvBytes(), auxbuff, 256));
nping_print(QT_1|NO_NEWLINE,"| Lost: %I64u ", this->stats.getLostPackets() );
nping_print(QT_1,"(%.2lf%%)", this->stats.getLostPacketPercentage100() );
}
#else
/* Sent/Recv/Echoed Packets */
if(this->getRole()==ROLE_CLIENT){
nping_print(QT_1|NO_NEWLINE, "Raw packets sent: %llu ", this->stats.getSentPackets() );
nping_print(QT_1|NO_NEWLINE, "(%s) ", format_bytecount(this->stats.getSentBytes(), auxbuff, 256));
nping_print(QT_1|NO_NEWLINE,"| Rcvd: %llu ", this->stats.getRecvPackets() );
nping_print(QT_1|NO_NEWLINE,"(%s) ", format_bytecount(this->stats.getRecvBytes(), auxbuff, 256));
nping_print(QT_1|NO_NEWLINE,"| Lost: %llu ", this->stats.getLostPackets() );
nping_print(QT_1|NO_NEWLINE,"(%.2lf%%)", this->stats.getLostPacketPercentage100() );
nping_print(QT_1|NO_NEWLINE,"| Echoed: %llu ", this->stats.getEchoedPackets() );
nping_print(QT_1,"(%s) ", format_bytecount(this->stats.getEchoedBytes(), auxbuff, 256));
}else if(this->getRole()==ROLE_SERVER){
nping_print(QT_1|NO_NEWLINE, "Raw packets captured: %llu ", this->stats.getRecvPackets() );
nping_print(QT_1|NO_NEWLINE, "(%s) ", format_bytecount(this->stats.getRecvBytes(), auxbuff, 256));
nping_print(QT_1|NO_NEWLINE,"| Echoed: %llu ", this->stats.getEchoedPackets() );
nping_print(QT_1|NO_NEWLINE,"(%s) ", format_bytecount(this->stats.getEchoedBytes(), auxbuff, 256));
nping_print(QT_1|NO_NEWLINE,"| Not Matched: %llu ", this->stats.getUnmatchedPackets() );
nping_print(QT_1|NO_NEWLINE,"(%s) ", format_bytecount(this->stats.getRecvBytes()-this->stats.getEchoedBytes(), auxbuff, 256));
nping_print(QT_1,"(%.2lf%%)", this->stats.getUnmatchedPacketPercentage100() );
}else if(this->getMode()==TCP_CONNECT){
nping_print(QT_1|NO_NEWLINE, "TCP connection attempts: %llu ", this->stats.getSentPackets() );
nping_print(QT_1|NO_NEWLINE,"| Successful connections: %llu ", this->stats.getRecvPackets() );
nping_print(QT_1|NO_NEWLINE,"| Failed: %llu ", this->stats.getLostPackets() );
nping_print(QT_1,"(%.2lf%%)", this->stats.getLostPacketPercentage100() );
} else if (this->getMode()==UDP_UNPRIV){
nping_print(QT_1|NO_NEWLINE, "UDP packets sent: %llu ", this->stats.getSentPackets() );
nping_print(QT_1|NO_NEWLINE,"| Rcvd: %llu ", this->stats.getRecvPackets() );
nping_print(QT_1|NO_NEWLINE,"| Lost: %llu ", this->stats.getLostPackets() );
nping_print(QT_1,"(%.2lf%%)", this->stats.getLostPacketPercentage100() );
} else{
nping_print(QT_1|NO_NEWLINE, "Raw packets sent: %llu ", this->stats.getSentPackets() );
nping_print(QT_1|NO_NEWLINE, "(%s) ", format_bytecount(this->stats.getSentBytes(), auxbuff, 256));
nping_print(QT_1|NO_NEWLINE,"| Rcvd: %llu ", this->stats.getRecvPackets() );
nping_print(QT_1|NO_NEWLINE,"(%s) ", format_bytecount(this->stats.getRecvBytes(), auxbuff, 256));
nping_print(QT_1|NO_NEWLINE,"| Lost: %llu ", this->stats.getLostPackets() );
nping_print(QT_1,"(%.2lf%%)", this->stats.getLostPacketPercentage100() );
}
#endif
/* Transmission times & rates */
nping_print(VB_1|NO_NEWLINE,"Tx time: %.5lfs ", this->stats.elapsedTx() );
nping_print(VB_1|NO_NEWLINE,"| Tx bytes/s: %.2lf ", this->stats.getOverallTxByteRate() );
nping_print(VB_1,"| Tx pkts/s: %.2lf", this->stats.getOverallTxPacketRate() );
nping_print(VB_1|NO_NEWLINE,"Rx time: %.5lfs ", this->stats.elapsedRx() );
nping_print(VB_1|NO_NEWLINE,"| Rx bytes/s: %.2lf ", this->stats.getOverallRxByteRate() );
nping_print(VB_1,"| Rx pkts/s: %.2lf", this->stats.getOverallRxPacketRate() );
} /* End of displayStatistics() */
/* Close open files, free allocated memory, etc. */
int NpingOps::cleanup(){
this->targets.freeTargets();
return OP_SUCCESS;
} /* End of cleanup() */
char *NpingOps::select_network_iface(){
char errbuf[PCAP_ERRBUF_SIZE];
pcap_if_t *pcap_ifaces=NULL;
/* Vars for the current interface in the loop */
pcap_if_t *curr=NULL; /* Current pcap pcap_if_t element */
bool current_has_address=false; /* Does it have an addr of any type? */
bool current_has_ipv6=false; /* Does it have an IPv6 address? */
bool current_has_ipv4=false; /* Does it have an IPv4 address? */
bool current_is_loopback=false; /* Is it a loopback interface? */
bool select_current=false; /* Is current better than candidate? */
struct sockaddr_in6 devaddr6; /* We store iface's IPv6 address */
struct sockaddr_in devaddr4; /* And also its IPv4 address */
/* Vars for our candidate interface */
pcap_if_t *candidate=NULL;
bool candidate_has_address=false;
bool candidate_has_ipv6=false;
bool candidate_has_ipv4=false;
bool candidate_is_loopback=false;
//struct sockaddr_in6 candidate_addr6;
//struct sockaddr_in candidate_addr4;
/* Ask libpcap for a list of network interfaces */
if( pcap_findalldevs(&pcap_ifaces, errbuf) != 0 )
nping_fatal(QT_3, "Cannot obtain device for packet capture --> %s. You may want to specify one explicitly using option -e", errbuf);
/* Iterate over the interface list and select the best one */
for(curr=pcap_ifaces; curr!=NULL; curr=curr->next){
current_has_address=false; candidate_has_ipv6=false;
candidate_is_loopback=false; candidate_has_ipv4=false;
select_current=false;
if( curr->flags==PCAP_IF_LOOPBACK)
current_is_loopback=true;
/* Loop through the list of addresses */
for(pcap_addr_t *curraddr=curr->addresses; curraddr!=NULL; curraddr=curraddr->next){
current_has_address=true;
if( curraddr->addr->sa_family==AF_INET){
current_has_ipv4=true;
memcpy( &devaddr4, curraddr->addr, sizeof(struct sockaddr_in));
} else if( curraddr->addr->sa_family==AF_INET6){
current_has_ipv6=true;
memcpy( &devaddr6, curraddr->addr, sizeof(struct sockaddr_in6));
}
}
/* If we still have no candidate, take the first one we find */
if( candidate==NULL){
select_current=true;
}
/* If we already have a candidate, check if the one we are
* processing right now is better than the one we've already got */
else{
/* If our candidate does not have an IPv6 address but this one does,
* select the new one. */
if( candidate_has_ipv6==false && current_has_ipv6==true ){
select_current=true;
}
/* If our candidate does not even have an IPv4 address but this
* one does, select the new one. */
else if( candidate_has_ipv4==false && candidate_has_ipv6==false && current_has_ipv4){
select_current=true;
}
/* If our candidate is a loopback iface, select the new one */
else if( candidate_is_loopback && !current_is_loopback){
/* Select the new one only if it has an IPv6 address
* and the old one didn't. If our old loopback iface
* has an IPv6 address and this one does not, we
* prefer to keep the loopback one, even though the
* other is not loopback */
if(current_has_ipv6==true){
select_current=true;
}
/* We also prefer IPv4 capable interfaces than */
else if(candidate_has_ipv6==false && current_has_ipv4==true){
select_current=true;
}
}
/* If both are loopback, select the best one. */
else if( candidate->flags==PCAP_IF_LOOPBACK && curr->flags==PCAP_IF_LOOPBACK){
if( candidate_has_ipv6==false && current_has_ipv6 )
select_current=true;
}
}
/* Did we determine that we should discard our old candidate? */
if( select_current ){
candidate=curr;
candidate_has_address=current_has_address;
candidate_has_ipv4=current_has_ipv4;
candidate_has_ipv6=current_has_ipv6;
candidate_is_loopback=current_is_loopback;
}
/* Let's see if we have the interface of our dreams... */
if( candidate_has_address && candidate_has_ipv6 && candidate_has_ipv4 && candidate_is_loopback==false){
break;
}
}
if(candidate==NULL)
return NULL;
else
return candidate->name;
} /* End of select_network_iface() */
int NpingOps::setDefaultHeaderValues(){
if(this->ipv6()){ /* IPv6 */
if(!this->issetTrafficClass())
this->ipv6_tclass=DEFAULT_IPv6_TRAFFIC_CLASS;
if(!this->issetFlowLabel())
this->ipv6_flowlabel=(get_random_u32() % 1048575);
if(!this->issetHopLimit() && !this->issetTraceroute())
this->ttl=DEFAULT_IPv6_TTL;
}else{ /* IPv4 */
if(!this->issetTOS())
this->tos=DEFAULT_IP_TOS;
if(!this->issetIdentification())
this->identification=get_random_u16();
if(!this->issetTTL() && !this->issetTraceroute())
this->ttl=DEFAULT_IP_TTL;
}
switch( this->getMode() ){
case TCP:
if(!this->issetTargetPorts()){
u16 *list = (u16 *)safe_zalloc( sizeof(u16) );
list[0]=DEFAULT_TCP_TARGET_PORT;
this->setTargetPorts(list, 1);
}
if(!this->issetSourcePort()){
/* Generate any source port higher than 1024 */
if(this->getRole()!=ROLE_CLIENT){
this->source_port=(1024 + ( get_random_u16()%(65535-1024) ));
}else{
/* For the echo client, avoid choosing the port used for the echo side channel */
while( (this->source_port=(1024 + ( get_random_u16()%(65535-1024) )))==this->echo_port );
}
}
if(!this->issetTCPSequence())
this->tcpseq=get_random_u32();
if(!this->issetTCPAck()){
if(this->getFlagTCP(FLAG_ACK))
this->tcpack=get_random_u32();
else
this->tcpack=0;
}
if(!this->issetTCPFlags())
this->setFlagTCP(FLAG_SYN);
if(!this->issetTCPWindow())
this->tcpwin=DEFAULT_TCP_WINDOW_SIZE;
/* @todo ADD urgent pointer handling here when it gets implemented */
break;
case UDP:
if(!this->issetTargetPorts()){
u16 *list = (u16 *)safe_zalloc( sizeof(u16) );
list[0]=DEFAULT_UDP_TARGET_PORT;
this->setTargetPorts(list, 1);
}
if(!this->issetSourcePort())
this->source_port=DEFAULT_UDP_SOURCE_PORT;
break;
case ICMP:
if(this->ipv6()){
if(!this->issetICMPType()) /* Default to ICMP Echo */
this->icmp_type=DEFAULT_ICMPv6_TYPE;
if(!this->issetICMPCode())
this->icmp_code=DEFAULT_ICMPv6_CODE;
}else{
if(!this->issetICMPType()) /* Default to ICMP Echo */
this->icmp_type=DEFAULT_ICMP_TYPE;
if(!this->issetICMPCode())
this->icmp_code=DEFAULT_ICMP_CODE;
}
break;
case ARP:
if(!this->issetARPOpCode())
this->arp_opcode=DEFAULT_ARP_OP;
break;
case UDP_UNPRIV:
if(!this->issetTargetPorts()){
u16 *list = (u16 *)safe_zalloc( sizeof(u16) );
list[0]=DEFAULT_UDP_TARGET_PORT;
this->setTargetPorts(list, 1);
}
if(!this->issetSourcePort())
this->source_port=DEFAULT_UDP_SOURCE_PORT;
break;
case TCP_CONNECT:
if( !this->issetTargetPorts() ) {
u16 *list = (u16 *)safe_zalloc( sizeof(u16) );
list[0]=DEFAULT_TCP_TARGET_PORT;
this->setTargetPorts(list, 1);
}
default:
return OP_FAILURE;
break;
}
return OP_SUCCESS;
} /* End of setDefaultHeaderValues() */
int NpingOps::setLastPacketSentTime(struct timeval t){
this->last_sent_pkt_time=t;
return OP_SUCCESS;
} /* End of setLastPacketSentTime() */
struct timeval NpingOps::getLastPacketSentTime(){
return this->last_sent_pkt_time;
} /* End of getLastPacketSentTime() */
/** Sets the RCVD output to be delayed. The supplied string is strdup()ed, so
* the caller may safely free() it or modify after calling this function.
* The "id" parameter is the nsock timer event scheduled for the output of
* the RCVD string (usually scheduled by ProbeMode). It is provided to allow
* other objects (like EchoClient) to cancel the event if they take care of
* printing the RCVD string before the timer goes off.*/
int NpingOps::setDelayedRcvd(const char *str, nsock_event_id id){
if(str==NULL)
return OP_FAILURE;
this->delayed_rcvd_str=strdup(str);
this->delayed_rcvd_event=id;
this->delayed_rcvd_str_set=true;
return OP_SUCCESS;
} /* End of setDelayedRcvd() */
/** Returns a pointer to a delayed RCVD output string. It returns non-NULL
* strings only once per prior setDelayedRcvd() call. This is, when a string
* has been set through a setDelayRcdv() call, the first time getDelayRcvd()
* is called, it returns that string. Subsequent calls will return NULL until
* another string is set, using setDelayRcdv() again.
* The "id" parameter will be filled with the timer event that was supposed
* to print the message. If getDelayedRcvd() is called by the timer handler
* itself, then NULL can be passed safely since the event id is not needed.
* If the caller is some other method that wants to print the RCVD string
* before the timer goes off, it may use the event ID to cancel the scheduled
* event since it's no longer necessary.
* @warning returned string is the strdup()ed version of the string passed
* in the call to setDelayedRcvd(), so the caller MUST free the returned
* pointer when it's done using it. */
char *NpingOps::getDelayedRcvd(nsock_event_id *id){
if(delayed_rcvd_str_set==false){
return NULL;
}else{
this->delayed_rcvd_str_set=false;
char *old=this->delayed_rcvd_str;
this->delayed_rcvd_str=NULL;
if(id!=NULL)
*id=this->delayed_rcvd_event;
return old;
}
} /* End of getDelayedRcvd() */
bool NpingOps::doCrypto(){
return this->do_crypto;
}
int NpingOps::doCrypto(bool value){
this->do_crypto=value;
return OP_SUCCESS;
}
/* Returns true if the echo server is allowed to include payloads in NEP_ECHO
* messages. */
bool NpingOps::echoPayload(){
return this->echo_payload;
}
/* Enables or disables payload echo for the echo server. Pass true to enable
* or false to disable. */
int NpingOps::echoPayload(bool value){
this->echo_payload=value;
this->echo_payload_set=true;
return OP_SUCCESS;
}
/** Returns the total number of probes to be sent (this takes into account
* the number of rounds, ports, and targets. It returns a positive integer
* on success and n<=0 in case of error. */
int NpingOps::getTotalProbes(){
int total_ports=0;
this->getTargetPorts(&total_ports);
u64 tmp = (u64) this->getPacketCount() * total_ports;
if (tmp > INT_MAX) {
return -1;
}
tmp *= this->targets.Targets.size();
if (tmp > INT_MAX) {
return -1;
}
return (int) tmp;
}
/******************************************************************************
* Code templates. *
******************************************************************************/
/*
Attributes for NpingOps:
TYPE ATTRNAME;
bool ATTRNAME_set;
Prototypes for NpingOps:
int setMETHNAME(TYPE val);
TYPE getMETHNAME();
bool issetMETHNAME();
Initialization for NpingOps::NpingOps()
ATTRNAME=0;
ATTRNAME_set=false;
*/
/** Sets METHNAME. Supplied parameter must be XXXXXXXX
* @return OP_SUCCESS on success and OP_FAILURE in case of error. */
/*int NpingOps::setMETHNAME(TYPE val){
if( 0 ){
nping_fatal(QT_3,"setMETHNAME(): Invalid value supplied\n");
return OP_FAILURE;
}else{
ATTRNAME=val;
ATTRNAME_set=true;
}
return OP_SUCCESS;
} *//* End of setMETHNAME() */
/** Returns value of attribute ATTRNAME */
/*TYPE NpingOps::getMETHNAME(){
return this->ATTRNAME;
} *//* End of getMETHNAME() */
/* Returns true if option has been set */
/*bool NpingOps::issetMETHNAME(){
return this->ATTRNAME_set;
} *//* End of issetMETHNAME() */
|