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
use crate::error::NtraceError;
use crate::protocol::{Protocol, Target};
use log::{debug, info, warn};
use rand::Rng;
use serde::{Deserialize, Serialize};
use std::collections::HashMap;
use std::net::{IpAddr, Ipv4Addr, Ipv6Addr, SocketAddr};
use std::sync::Arc;
use std::time::{Duration, Instant};
use tokio::sync::Mutex;
use trust_dns_resolver::TokioAsyncResolver;
use trust_dns_resolver::config::{ResolverConfig, ResolverOpts};
/// Configuration for traceroute
#[derive(Debug, Clone)]
pub struct TraceConfig {
/// Target to trace
pub target: Target,
/// Protocol to use (TCP, UDP, ICMP)
pub protocol: Protocol,
/// Port to use for TCP/UDP
pub port: u16,
/// Maximum number of hops to try
pub max_hops: u8,
/// Minimum TTL to start with
pub min_ttl: u8,
/// Number of queries per hop
pub queries: u8,
/// Timeout for each probe in milliseconds
pub timeout_ms: u64,
/// Whether to perform reverse DNS lookups
pub resolve_hostnames: bool,
/// Number of parallel requests
pub parallel_requests: u8,
/// Time between sending packets in milliseconds
pub send_time_ms: u64,
/// Time between sending packets for different TTLs in milliseconds
pub ttl_time_ms: u64,
/// Payload size for probe packets
pub payload_size: usize,
/// Whether to use fast mode (less accurate but faster)
pub fast_mode: bool,
/// Whether to perform MTU discovery
pub discover_mtu: bool,
/// Whether to detect path asymmetry
pub detect_asymmetry: bool,
/// Whether to perform AS path lookup
pub lookup_asn: bool,
/// Whether to perform geolocation lookup
pub lookup_geo: bool,
/// Whether to detect MPLS tunnels
pub detect_mpls: bool,
/// Source IP address to use (if None, system default is used)
pub source_ip: Option<IpAddr>,
/// Source port to use (if None, system assigned)
pub source_port: Option<u16>,
/// Type of Service (ToS) / DSCP value
pub tos: Option<u8>,
/// Interface to use
pub interface: Option<String>,
/// First hop timeout in milliseconds (can be higher than regular timeout)
pub first_hop_timeout_ms: Option<u64>,
/// Whether to use adaptive timing
pub adaptive_timing: bool,
}
impl Default for TraceConfig {
fn default() -> Self {
Self {
target: Target::Ip(IpAddr::V4(Ipv4Addr::new(1, 1, 1, 1))),
// Default to TCP which doesn't require root privileges
protocol: Protocol::Tcp,
// Use HTTPS port for better results
port: 443,
// Standard 30 hops max
max_hops: 30,
// Start from TTL 1
min_ttl: 1,
queries: 3,
// Balanced timeout for reliability
timeout_ms: 800,
resolve_hostnames: true,
parallel_requests: 24,
// Optimized delay between packets
send_time_ms: 5,
// Optimized delay between TTLs
ttl_time_ms: 5,
payload_size: 64,
// Default to standard mode
fast_mode: false,
// Don't perform MTU discovery by default
discover_mtu: false,
// Don't detect path asymmetry by default
detect_asymmetry: false,
// Don't perform AS path lookup by default
lookup_asn: false,
// Don't perform geolocation lookup by default
lookup_geo: false,
// Don't detect MPLS tunnels by default
detect_mpls: false,
// Use system default source IP
source_ip: None,
// Use system assigned source port
source_port: None,
// No ToS/DSCP value by default
tos: None,
// Use system default interface
interface: None,
// Use slightly higher timeout for first hop
first_hop_timeout_ms: Some(1200),
// Use adaptive timing by default
adaptive_timing: true,
}
}
}
/// Result for a single hop in the traceroute
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct HopResult {
/// Hop number (TTL)
pub hop: u8,
/// IP address of the hop
pub ip: Option<String>,
/// Hostname of the hop (if resolved)
pub hostname: Option<String>,
/// Latency for each query
pub latencies: Vec<Option<Duration>>,
/// Average latency
pub avg_latency: Option<Duration>,
/// Minimum latency
pub min_latency: Option<Duration>,
/// Maximum latency
pub max_latency: Option<Duration>,
/// Standard deviation of latencies
pub std_dev_latency: Option<f64>,
/// Packet loss percentage (0.0 - 100.0)
pub packet_loss: f64,
/// Whether this hop is the final destination
pub is_destination: bool,
/// ASN information (if available)
pub asn: Option<String>,
/// Organization name (if available)
pub org: Option<String>,
/// Location information (if available)
pub location: Option<String>,
/// MPLS labels (if available)
pub mpls_labels: Option<Vec<String>>,
/// Timestamp when this hop was recorded
pub timestamp: Option<chrono::DateTime<chrono::Utc>>,
}
/// Result of a complete traceroute
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct TraceResult {
/// Target that was traced
pub target: String,
/// Protocol used
pub protocol: String,
/// Port used (for TCP/UDP)
pub port: Option<u16>,
/// Hops discovered
pub hops: Vec<HopResult>,
/// Total time taken
pub duration: Duration,
/// Whether the trace reached the destination
pub reached_destination: bool,
/// Average round trip time
pub avg_rtt: Option<Duration>,
/// Minimum round trip time
pub min_rtt: Option<Duration>,
/// Maximum round trip time
pub max_rtt: Option<Duration>,
/// Standard deviation of round trip times
pub std_dev_rtt: Option<f64>,
/// Total packet loss percentage
pub packet_loss: f64,
/// Timestamp when the trace was started
pub timestamp: chrono::DateTime<chrono::Utc>,
/// Network path asymmetry detection
pub path_asymmetry: Option<f64>,
/// Route stability score (0.0-1.0)
pub route_stability: Option<f64>,
/// MTU discovery results
pub path_mtu: Option<u16>,
}
/// Tracer for performing traceroute operations
///
/// The `Tracer` struct is responsible for executing traceroute operations
/// using the specified configuration. It supports multiple protocols:
///
/// - TCP: Works without root privileges on all platforms
/// - UDP: Requires root privileges on most Unix like systems
/// - ICMP: Requires root privileges on all platforms
///
/// When using protocols that require root privileges, the implementation
/// will automatically fall back to TCP if the necessary privileges are
/// not available.
pub struct Tracer {
config: TraceConfig,
/// Statistics and results
results: Arc<Mutex<HashMap<u8, HopResult>>>,
}
impl Tracer {
/// Creates a new tracer with the given configuration
///
/// # Arguments
///
/// * `config` - The configuration to use for the traceroute operation
///
/// # Example
///
/// ```no_run
/// use ntrace::traceroute::{TraceConfig, Tracer};
/// use ntrace::protocol::{Protocol, Target};
/// use std::net::IpAddr;
///
/// let config = TraceConfig::default();
/// let tracer = Tracer::new(config);
/// ```
pub fn new(config: TraceConfig) -> Self {
Self {
config,
results: Arc::new(Mutex::new(HashMap::new())),
}
}
/// Detect path asymmetry by analyzing round trip times
///
/// This method attempts to detect if the network path is asymmetric
/// (different forward and return paths) by analyzing the variance in
/// round trip times and looking for patterns.
///
/// # Arguments
///
/// * `hops` - The hop results from a completed trace
///
/// # Returns
///
/// A value between 0.0 and 1.0 indicating the likelihood of path asymmetry,
/// or None if detection failed or there's insufficient data
fn detect_path_asymmetry(&self, hops: &[HopResult]) -> Option<f64> {
if !self.config.detect_asymmetry || hops.len() < 3 {
return None;
}
// Calculate the coefficient of variation (CV) for each hop's latencies
let mut cv_values = Vec::new();
for hop in hops {
if let (Some(avg), Some(std_dev)) = (hop.avg_latency, hop.std_dev_latency) {
let avg_micros = avg.as_micros() as f64;
if avg_micros > 0.0 {
let cv = std_dev / avg_micros;
cv_values.push(cv);
}
}
}
if cv_values.len() < 3 {
return None;
}
// Calculate the average CV
let avg_cv = cv_values.iter().sum::<f64>() / cv_values.len() as f64;
// Calculate the standard deviation of CVs
let variance = cv_values
.iter()
.map(|x| {
let diff = x - avg_cv;
diff * diff
})
.sum::<f64>()
/ cv_values.len() as f64;
let std_dev_cv = variance.sqrt();
// Look for patterns in latency jumps
let mut latency_jumps = 0;
for i in 1..hops.len() {
if let (Some(prev_avg), Some(curr_avg)) = (hops[i - 1].avg_latency, hops[i].avg_latency)
{
let prev_micros = prev_avg.as_micros() as f64;
let curr_micros = curr_avg.as_micros() as f64;
if curr_micros > 0.0 && prev_micros > 0.0 {
// Calculate the relative jump
let jump = (curr_micros - prev_micros).abs() / prev_micros;
// Count significant jumps
if jump > 0.5 {
latency_jumps += 1;
}
}
}
}
// Combine metrics to estimate asymmetry
// High CV variation and many latency jumps suggest asymmetry
let asymmetry_score = std_dev_cv * 0.7 + (latency_jumps as f64 / hops.len() as f64) * 0.3;
// Normalize to 0.0-1.0 range
Some((asymmetry_score * 2.0).min(1.0))
}
/// Perform reverse DNS lookup using trust-dns-resolver
async fn resolve_hostname(&self, ip: IpAddr) -> Option<String> {
if !self.config.resolve_hostnames {
return None;
}
debug!("Hostname resolution requested for {}", ip);
// Create a new resolver with default configuration
let mut opts = ResolverOpts::default();
// Set a reasonable timeout for DNS lookups
opts.timeout = Duration::from_secs(2);
// Enable caching for better performance
opts.cache_size = 100;
// Create the resolver - this returns the resolver directly, not a Result
let resolver = TokioAsyncResolver::tokio(ResolverConfig::default(), opts);
// Perform reverse lookup with timeout to prevent hanging
let lookup_future = resolver.reverse_lookup(ip);
let timeout_duration = Duration::from_millis(self.config.timeout_ms);
match tokio::time::timeout(timeout_duration, lookup_future).await {
Ok(Ok(response)) => {
let names: Vec<String> = response.iter().map(|name| name.to_string()).collect();
if names.is_empty() {
None
} else {
Some(names[0].trim_end_matches(".").to_string())
}
}
Ok(Err(e)) => {
debug!("Reverse lookup failed for {}: {}", ip, e);
None
}
Err(_) => {
debug!("Reverse lookup timed out for {}", ip);
None
}
}
}
/// Perform a parallel traceroute for faster results
///
/// This method sends multiple TTL probes in parallel to speed up the traceroute process.
/// It's more efficient than sequential probing but may be less accurate in some cases.
///
/// # Arguments
///
/// * `target_ip` - The IP address of the target
///
/// # Returns
///
/// A `Result` indicating success or failure of the operation
async fn trace_parallel(&self, target_ip: IpAddr) -> Result<(), NtraceError> {
info!("Performing parallel traceroute to {}", target_ip);
// Create progress indicator
let progress = if cfg!(not(test)) {
use indicatif::{ProgressBar, ProgressStyle};
let pb = ProgressBar::new(self.config.max_hops as u64);
pb.set_style(
ProgressStyle::default_bar()
.template("[{elapsed_precise}] [{bar:40.cyan/blue}] {pos}/{len} hops - {msg}")
.unwrap()
.progress_chars("█▓▒░ "),
);
pb.set_message(format!("Parallel tracing route to {}", target_ip));
Some(pb)
} else {
None
};
// Determine the batch size based on parallel_requests
let batch_size = self.config.parallel_requests.min(self.config.max_hops) as usize;
// Process TTLs in batches
for ttl_batch_start in (self.config.min_ttl..=self.config.max_hops).step_by(batch_size) {
let ttl_batch_end = (ttl_batch_start + batch_size as u8 - 1).min(self.config.max_hops);
// Create a vector of futures for this batch
let mut probe_futures = Vec::new();
for ttl in ttl_batch_start..=ttl_batch_end {
// Create a hop result entry
let hop_result = HopResult {
hop: ttl,
ip: None,
hostname: None,
latencies: vec![None; self.config.queries as usize],
avg_latency: None,
min_latency: None,
max_latency: None,
std_dev_latency: None,
packet_loss: 0.0,
is_destination: false,
asn: None,
org: None,
location: None,
mpls_labels: None,
timestamp: Some(chrono::Utc::now()),
};
// Store initial hop result
{
let mut results = self.results.lock().await;
results.insert(ttl, hop_result);
}
// Create a future for probing this TTL
let probe_future = self.probe_ttl(ttl, target_ip);
probe_futures.push(probe_future);
}
// Execute all probes in this batch concurrently
futures::future::join_all(probe_futures).await;
// Update progress
if let Some(pb) = &progress {
pb.set_position(ttl_batch_end as u64);
pb.set_message(format!(
"Processed hops {} to {}",
ttl_batch_start, ttl_batch_end
));
}
// Check if we've reached the destination
let results = self.results.lock().await;
let destination_reached = results.values().any(|hop| hop.is_destination);
if destination_reached {
break;
}
}
// Finish progress
if let Some(pb) = &progress {
pb.finish_with_message("Parallel traceroute completed");
}
Ok(())
}
/// Probe a single TTL
async fn probe_ttl(&self, ttl: u8, target_ip: IpAddr) -> Result<(), NtraceError> {
match self.config.protocol {
Protocol::Tcp => self.probe_ttl_tcp(ttl, target_ip).await,
Protocol::Udp => self.probe_ttl_udp(ttl, target_ip).await,
Protocol::Icmp => self.probe_ttl_icmp(ttl, target_ip).await,
}
}
/// Probe a single TTL using TCP
async fn probe_ttl_tcp(&self, ttl: u8, target_ip: IpAddr) -> Result<(), NtraceError> {
let mut responses = 0;
let mut total_latency = Duration::new(0, 0);
let mut latencies = vec![None; self.config.queries as usize];
let mut router_ip = None;
let mut is_destination = false;
for q in 0..self.config.queries {
// Send TCP SYN packet with TTL set
let start_time = Instant::now();
// Create a TCP socket
let socket = match std::net::TcpStream::connect_timeout(
&SocketAddr::new(target_ip, self.config.port),
Duration::from_millis(self.config.timeout_ms),
) {
Ok(s) => {
// Set TTL
if let Err(e) = s.set_ttl(ttl.into()) {
warn!("Failed to set TTL: {}", e);
continue;
}
s
}
Err(e) => {
// Connection failed, check if it's due to TTL exceeded
if let Some(addr) = Self::extract_router_ip_from_error(&e) {
let latency = start_time.elapsed();
latencies[q as usize] = Some(latency);
router_ip = Some(addr.to_string());
responses += 1;
total_latency += latency;
}
continue;
}
};
// Try to get socket error to determine if we got a response
match socket.take_error() {
Ok(Some(e)) => {
// Check if this is a TTL exceeded error
if let Some(addr) = Self::extract_router_ip_from_error(&e) {
let latency = start_time.elapsed();
latencies[q as usize] = Some(latency);
router_ip = Some(addr.to_string());
responses += 1;
total_latency += latency;
}
}
Ok(None) => {
// Connection succeeded - we reached the destination
let latency = start_time.elapsed();
latencies[q as usize] = Some(latency);
router_ip = Some(target_ip.to_string());
is_destination = true;
responses += 1;
total_latency += latency;
}
Err(e) => {
warn!("Error getting socket error: {}", e);
}
}
// Wait between queries
if q < self.config.queries - 1 {
tokio::time::sleep(Duration::from_millis(self.config.send_time_ms)).await;
}
}
// Update the hop result
let mut results = self.results.lock().await;
if let Some(hop_result) = results.get_mut(&ttl) {
hop_result.ip = router_ip;
hop_result.latencies = latencies;
hop_result.is_destination = is_destination;
// Calculate statistics
if responses > 0 {
// Average latency
hop_result.avg_latency = Some(total_latency / responses as u32);
// Calculate packet loss
hop_result.packet_loss = 100.0 * (self.config.queries as usize - responses) as f64
/ self.config.queries as f64;
// Find min and max latencies
let mut min_latency = Duration::from_secs(u64::MAX);
let mut max_latency = Duration::from_secs(0);
let mut latency_values = Vec::new();
for latency in &hop_result.latencies {
if let Some(lat) = latency {
min_latency = min_latency.min(*lat);
max_latency = max_latency.max(*lat);
latency_values.push(lat.as_micros() as f64);
}
}
hop_result.min_latency = Some(min_latency);
hop_result.max_latency = Some(max_latency);
// Calculate standard deviation
if latency_values.len() > 1 {
let avg = latency_values.iter().sum::<f64>() / latency_values.len() as f64;
let variance = latency_values
.iter()
.map(|x| {
let diff = *x - avg;
diff * diff
})
.sum::<f64>()
/ latency_values.len() as f64;
hop_result.std_dev_latency = Some(variance.sqrt());
}
// Resolve hostname if we have an IP
if let Some(ip_str) = &hop_result.ip {
if self.config.resolve_hostnames {
if let Ok(ip) = ip_str.parse::<IpAddr>() {
hop_result.hostname = self.resolve_hostname(ip).await;
}
}
}
}
}
Ok(())
}
/// Probe a single TTL using UDP
async fn probe_ttl_udp(&self, _ttl: u8, _target_ip: IpAddr) -> Result<(), NtraceError> {
// Similar to probe_ttl_tcp but using UDP
// Implementation would be similar to trace_udp but focused on a single TTL
Ok(())
}
/// Probe a single TTL using ICMP
async fn probe_ttl_icmp(&self, _ttl: u8, _target_ip: IpAddr) -> Result<(), NtraceError> {
// Similar to probe_ttl_tcp but using ICMP
// Implementation would be similar to trace_icmp_raw but focused on a single TTL
Ok(())
}
/// Perform a traceroute to the target
///
/// This method executes the traceroute operation using the configured protocol.
/// If the requested protocol requires root privileges and they are not available,
/// it will automatically fall back to TCP traceroute which works without privileges.
///
/// # Returns
///
/// A `Result` containing either a `TraceResult` with the traceroute information
/// or an `NtraceError` if the operation failed.
///
/// # Example
///
/// ```no_run
/// use ntrace::traceroute::{TraceConfig, Tracer};
///
/// #[tokio::main]
/// async fn main() {
/// let mut tracer = Tracer::new(TraceConfig::default());
/// let result = tracer.trace().await.unwrap();
/// println!("Found {} hops to destination", result.hops.len());
/// }
/// ```
pub async fn trace(&mut self) -> Result<TraceResult, NtraceError> {
// Start timing
let start_time = Instant::now();
// Resolve target to IP if it's a domain
let target_ip = match &self.config.target {
Target::Ip(ip) => *ip,
Target::Domain(domain) => {
// Resolve domain to IP address using tokio's lookup_host
use tokio::net::lookup_host;
let addr_iter = lookup_host(format!("{}:{}", domain, 80))
.await
.map_err(|e| {
NtraceError::DnsError(format!("Failed to resolve {}: {}", domain, e))
})?;
// Get the first IP address
let addr = addr_iter.into_iter().next().ok_or_else(|| {
NtraceError::DnsError(format!("No IP addresses found for {}", domain))
})?;
addr.ip()
}
};
// Display target information
info!(
"Tracing route to {} ({})",
match &self.config.target {
Target::Ip(ip) => ip.to_string(),
Target::Domain(domain) => domain.clone(),
},
target_ip
);
// Check if fast mode is enabled - if so, use parallel tracing
if self.config.fast_mode {
info!("Fast mode enabled - using parallel traceroute");
self.trace_parallel(target_ip).await?
} else {
// Use standard sequential tracing based on protocol
match self.config.protocol {
Protocol::Tcp => self.trace_tcp(target_ip).await?,
Protocol::Udp => {
// For UDP, check if we have root privileges or CAP_NET_RAW capability on Unix
if !has_root_privileges() && cfg!(target_family = "unix") {
// Try to ensure we have the CAP_NET_RAW capability
use crate::capability::ensure_cap_net_raw;
if !ensure_cap_net_raw() {
warn!(
"UDP traceroute requires root privileges or CAP_NET_RAW capability on Unix-like systems"
);
info!("Using TCP traceroute which doesn't require special privileges");
self.trace_tcp(target_ip).await?
} else {
// We should now have the capability, try UDP traceroute
match self.trace_udp(target_ip).await {
Ok(_) => {}
Err(e) => {
warn!(
"UDP traceroute failed: {}. Falling back to TCP traceroute.",
e
);
self.trace_tcp(target_ip).await?
}
}
}
} else {
match self.trace_udp(target_ip).await {
Ok(_) => {}
Err(e) => {
warn!(
"UDP traceroute failed: {}. Falling back to TCP traceroute.",
e
);
self.trace_tcp(target_ip).await?
}
}
}
}
Protocol::Icmp => {
// For ICMP, check if we have root privileges or CAP_NET_RAW capability
if !has_root_privileges() {
// Try to ensure we have the CAP_NET_RAW capability
use crate::capability::ensure_cap_net_raw;
if !ensure_cap_net_raw() {
warn!(
"The selected protocol {:?} requires root privileges or CAP_NET_RAW capability",
self.config.protocol
);
info!("Using TCP traceroute which doesn't require special privileges");
self.trace_tcp(target_ip).await?
} else {
// We should now have the capability, try ICMP traceroute
match self.trace_icmp_raw(target_ip).await {
Ok(_) => {}
Err(e) => {
warn!(
"Raw socket ICMP traceroute failed: {}. Falling back to TCP traceroute.",
e
);
self.trace_tcp(target_ip).await?
}
}
}
} else {
// ICMP protocol with root privileges - use raw socket implementation
match self.trace_icmp_raw(target_ip).await {
Ok(_) => {}
Err(e) => {
warn!(
"Raw socket ICMP traceroute failed: {}. Falling back to TCP traceroute.",
e
);
self.trace_tcp(target_ip).await?
}
}
}
}
}
}
// Calculate total duration
let duration = start_time.elapsed();
// Collect and sort results
let results = self.results.lock().await;
let mut hops: Vec<HopResult> = results.values().cloned().collect();
hops.sort_by_key(|h| h.hop);
// Determine if we reached the destination
let reached_destination = hops
.iter()
.any(|hop| hop.is_destination || (hop.ip.as_ref() == Some(&target_ip.to_string())));
// Calculate overall statistics
let mut all_latencies = Vec::new();
let mut total_packets = 0;
let mut received_packets = 0;
for hop in &hops {
total_packets += hop.latencies.len();
for latency in &hop.latencies {
if let Some(lat) = latency {
all_latencies.push(lat.as_micros() as f64);
received_packets += 1;
}
}
}
// Calculate overall RTT statistics
let (avg_rtt, min_rtt, max_rtt, std_dev_rtt) = if !all_latencies.is_empty() {
let sum: f64 = all_latencies.iter().sum();
let avg = sum / all_latencies.len() as f64;
let min = *all_latencies
.iter()
.min_by(|a, b| a.partial_cmp(b).unwrap_or(std::cmp::Ordering::Equal))
.unwrap();
let max = *all_latencies
.iter()
.max_by(|a, b| a.partial_cmp(b).unwrap_or(std::cmp::Ordering::Equal))
.unwrap();
// Calculate standard deviation
let variance = all_latencies
.iter()
.map(|x| {
let diff = *x - avg;
diff * diff
})
.sum::<f64>()
/ all_latencies.len() as f64;
let std_dev = variance.sqrt();
(
Some(Duration::from_micros(avg as u64)),
Some(Duration::from_micros(min as u64)),
Some(Duration::from_micros(max as u64)),
Some(std_dev),
)
} else {
(None, None, None, None)
};
// Calculate overall packet loss
let packet_loss = if total_packets > 0 {
100.0 * (total_packets - received_packets) as f64 / total_packets as f64
} else {
0.0
};
// Perform path MTU discovery if enabled
let path_mtu = if self.config.discover_mtu {
self.discover_path_mtu(target_ip).await
} else {
None
};
// Detect path asymmetry if enabled
let path_asymmetry = if self.config.detect_asymmetry {
self.detect_path_asymmetry(&hops)
} else {
None
};
// Create the final result
let trace_result = TraceResult {
target: match &self.config.target {
Target::Ip(ip) => ip.to_string(),
Target::Domain(domain) => domain.clone(),
},
protocol: format!("{:?}", self.config.protocol),
port: match self.config.protocol {
Protocol::Tcp | Protocol::Udp => Some(self.config.port),
_ => None,
},
hops,
duration,
reached_destination,
avg_rtt,
min_rtt,
max_rtt,
std_dev_rtt,
packet_loss,
timestamp: chrono::Utc::now(),
path_asymmetry,
route_stability: None, // Would need multiple traces to calculate stability
path_mtu,
};
Ok(trace_result)
}
/// Perform a TCP traceroute (doesn't require root privileges)
///
/// This method implements traceroute using TCP connections. It works by:
/// 1. Setting the TTL value on outgoing TCP packets
/// 2. Attempting to connect to the target
/// 3. Analyzing connection errors to determine the router IP addresses
///
/// TCP traceroute is the most reliable method for unprivileged users as it
/// doesn't require raw socket access.
///
/// # Arguments
///
/// * `target_ip` - The IP address of the target to trace
///
/// # Returns
///
/// A `Result` indicating success or failure of the operation
async fn trace_tcp(&self, target_ip: IpAddr) -> Result<(), NtraceError> {
// Both IPv4 and IPv6 are supported
// Create a more informative progress indicator
let progress = if cfg!(not(test)) {
use indicatif::{ProgressBar, ProgressStyle};
let pb = ProgressBar::new(self.config.max_hops as u64);
pb.set_style(
ProgressStyle::default_bar()
.template("[{elapsed_precise}] [{bar:40.cyan/blue}] {pos}/{len} hops - {msg}")
.unwrap()
.progress_chars("█▓▒░ "),
);
pb.set_message(format!("Tracing route to {} via TCP", target_ip));
Some(pb)
} else {
None
};
// Determine if we're using IPv4 or IPv6
let _is_ipv6 = matches!(target_ip, IpAddr::V6(_));
// Trace each TTL - continue until we reach the max_hops or the destination
let mut destination_reached = false;
for ttl in self.config.min_ttl..=self.config.max_hops {
// Create a hop result entry
let mut hop_result = HopResult {
hop: ttl,
ip: None,
hostname: None,
latencies: vec![None; self.config.queries as usize],
avg_latency: None,
min_latency: None,
max_latency: None,
std_dev_latency: None,
packet_loss: 0.0,
is_destination: false,
asn: None,
org: None,
location: None,
mpls_labels: None,
timestamp: Some(chrono::Utc::now()),
};
// Send multiple queries for this hop
let mut responses = 0;
let mut total_latency = Duration::new(0, 0);
for q in 0..self.config.queries {
// Adjust timeout for first hop if configured
let timeout_ms =
if ttl == self.config.min_ttl && self.config.first_hop_timeout_ms.is_some() {
self.config.first_hop_timeout_ms.unwrap()
} else {
self.config.timeout_ms
};
// Send TCP SYN packet with TTL set
let start_time = Instant::now();
// Create socket with TTL set
let _start_time = Instant::now();
// Create a TCP socket with the specified source IP if provided
let socket = match if let Some(source_ip) = self.config.source_ip {
// Bind to specific source IP if provided
let socket = std::net::TcpStream::connect_timeout(
&SocketAddr::new(target_ip, self.config.port),
Duration::from_millis(timeout_ms),
)?;
// Set source IP using socket options (platform specific)
#[cfg(target_family = "unix")]
{
use std::os::unix::io::AsRawFd;
let fd = socket.as_raw_fd();
match source_ip {
IpAddr::V4(ipv4) => {
#[cfg(any(target_os = "freebsd", target_os = "macos"))]
let addr = libc::sockaddr_in {
sin_len: std::mem::size_of::<libc::sockaddr_in>() as u8,
sin_family: libc::AF_INET as u8,
sin_port: 0,
sin_addr: libc::in_addr {
s_addr: u32::from_ne_bytes(ipv4.octets()),
},
sin_zero: [0; 8],
};
#[cfg(target_os = "linux")]
let addr = libc::sockaddr_in {
sin_family: libc::AF_INET as u16,
// Let the OS choose
sin_port: 0,
sin_addr: libc::in_addr {
s_addr: u32::from_ne_bytes(ipv4.octets()),
},
sin_zero: [0; 8],
};
let res = unsafe {
libc::bind(
fd,
&addr as *const _ as *const libc::sockaddr,
std::mem::size_of::<libc::sockaddr_in>() as u32,
)
};
if res != 0 {
warn!(
"Failed to bind to source IP: {}",
std::io::Error::last_os_error()
);
}
}
IpAddr::V6(ipv6) => {
#[cfg(any(target_os = "freebsd", target_os = "macos"))]
let addr = libc::sockaddr_in6 {
sin6_len: std::mem::size_of::<libc::sockaddr_in6>() as u8,
sin6_family: libc::AF_INET6 as u8,
sin6_port: 0,
sin6_flowinfo: 0,
sin6_addr: libc::in6_addr {
s6_addr: ipv6.octets(),
},
sin6_scope_id: 0,
};
#[cfg(target_os = "linux")]
let addr = libc::sockaddr_in6 {
sin6_family: libc::AF_INET6 as u16,
sin6_port: 0,
sin6_flowinfo: 0,
sin6_addr: libc::in6_addr {
s6_addr: ipv6.octets(),
},
sin6_scope_id: 0,
};
let res = unsafe {
libc::bind(
fd,
&addr as *const _ as *const libc::sockaddr,
std::mem::size_of::<libc::sockaddr_in6>() as u32,
)
};
if res != 0 {
warn!(
"Failed to bind to source IPv6: {}",
std::io::Error::last_os_error()
);
}
}
}
}
Ok(socket)
} else {
std::net::TcpStream::connect_timeout(
&SocketAddr::new(target_ip, self.config.port),
Duration::from_millis(timeout_ms),
)
} {
Ok(s) => {
// Set TTL
if let Err(e) = s.set_ttl(ttl.into()) {
warn!("Failed to set TTL: {}", e);
continue;
}
// Set ToS/DSCP if provided
if let Some(tos) = self.config.tos {
#[cfg(target_family = "unix")]
{
use std::os::unix::io::AsRawFd;
let fd = s.as_raw_fd();
let res = unsafe {
libc::setsockopt(
fd,
libc::IPPROTO_IP,
libc::IP_TOS,
&tos as *const _ as *const libc::c_void,
std::mem::size_of::<u8>() as u32,
)
};
if res != 0 {
warn!("Failed to set ToS: {}", std::io::Error::last_os_error());
}
}
}
s
}
Err(e) => {
// Connection failed, check if it's due to TTL exceeded
if let Some(addr) = Self::extract_router_ip_from_error(&e) {
let latency = start_time.elapsed();
hop_result.latencies[q as usize] = Some(latency);
hop_result.ip = Some(addr.to_string());
responses += 1;
total_latency += latency;
} else {
// Log the specific error for debugging
debug!(
"TCP connection error: {} (errno: {:?})",
e,
e.raw_os_error()
);
}
continue;
}
};
// Try to get socket error to determine if we got a response
match socket.take_error() {
Ok(Some(e)) => {
// Check if this is a TTL exceeded error
if let Some(addr) = Self::extract_router_ip_from_error(&e) {
let latency = start_time.elapsed();
hop_result.latencies[q as usize] = Some(latency);
hop_result.ip = Some(addr.to_string());
responses += 1;
total_latency += latency;
}
}
Ok(None) => {
// Connection succeeded - we reached the destination
let latency = start_time.elapsed();
hop_result.latencies[q as usize] = Some(latency);
hop_result.ip = Some(target_ip.to_string());
hop_result.is_destination = true;
responses += 1;
total_latency += latency;
}
Err(e) => {
warn!("Error getting socket error: {}", e);
}
}
// Wait between queries, use adaptive timing if enabled
if q < self.config.queries - 1 {
let wait_time = if self.config.adaptive_timing {
// Adjust wait time based on latency of previous response
if let Some(Some(latency)) = hop_result.latencies.get(q as usize) {
// Use a fraction of the last latency as wait time, but not less than send_time_ms
let adaptive_time = latency.as_millis() as u64 / 4;
std::cmp::max(self.config.send_time_ms, adaptive_time)
} else {
self.config.send_time_ms
}
} else {
self.config.send_time_ms
};
tokio::time::sleep(Duration::from_millis(wait_time)).await;
}
}
// Calculate latency statistics if we got responses
if responses > 0 {
// Calculate packet loss percentage
hop_result.packet_loss = 100.0
* (self.config.queries as usize - responses as usize) as f64
/ self.config.queries as f64;
// Average latency
hop_result.avg_latency = Some(total_latency / responses as u32);
// Find min and max latencies
let mut min_latency = Duration::from_secs(u64::MAX);
let mut max_latency = Duration::from_secs(0);
let mut latency_values = Vec::new();
for latency in &hop_result.latencies {
if let Some(lat) = latency {
min_latency = min_latency.min(*lat);
max_latency = max_latency.max(*lat);
latency_values.push(lat.as_micros() as f64);
}
}
hop_result.min_latency = Some(min_latency);
hop_result.max_latency = Some(max_latency);
// Calculate standard deviation if we have enough samples
if latency_values.len() > 1 {
let avg = latency_values.iter().sum::<f64>() / latency_values.len() as f64;
let variance = latency_values
.iter()
.map(|x| {
let diff = *x - avg;
diff * diff
})
.sum::<f64>()
/ latency_values.len() as f64;
hop_result.std_dev_latency = Some(variance.sqrt());
}
// Perform ASN lookup if enabled
if self.config.lookup_asn && hop_result.ip.is_some() {
// This would be implemented with a GeoIP or ASN database
// For now, we'll leave it as None
}
// Perform geolocation lookup if enabled
if self.config.lookup_geo && hop_result.ip.is_some() {
// This would be implemented with a GeoIP database
// For now, we'll leave it as None
}
// Detect MPLS tunnels if enabled
if self.config.detect_mpls {
// This would require analyzing ICMP responses for MPLS labels
// For now, we'll leave it as None
}
}
// Resolve hostname if we have an IP and hostname resolution is enabled
if let Some(ip_str) = &hop_result.ip {
if self.config.resolve_hostnames {
if let Ok(ip) = ip_str.parse::<IpAddr>() {
hop_result.hostname = self.resolve_hostname(ip).await;
}
}
}
// Store the result
{
let mut results = self.results.lock().await;
results.insert(ttl, hop_result.clone());
}
// Update progress with more information
if let Some(pb) = &progress {
let msg = match &hop_result.ip {
Some(ip) => match &hop_result.hostname {
Some(hostname) => format!("Found hop {}: {} ({})", ttl, ip, hostname),
None => format!("Found hop {}: {}", ttl, ip),
},
None => format!("No response at hop {}", ttl),
};
pb.set_message(msg);
pb.inc(1);
}
// If we reached the destination, mark it but continue to collect all hops
if let Some(ip) = &hop_result.ip {
if ip == &target_ip.to_string() {
hop_result.is_destination = true;
destination_reached = true;
}
}
// If we've reached the destination and collected a few more hops, we can stop
if destination_reached && ttl > 5 {
break;
}
// Wait between TTLs
if ttl < self.config.max_hops {
tokio::time::sleep(Duration::from_millis(self.config.ttl_time_ms)).await;
}
}
// Finish progress with summary
if let Some(pb) = &progress {
let results = self.results.lock().await;
let hop_count = results.len();
let destination_reached = results.values().any(|hop| hop.is_destination);
let msg = if destination_reached {
format!("Completed trace to {} in {} hops", target_ip, hop_count)
} else {
format!("Trace to {} incomplete after {} hops", target_ip, hop_count)
};
pb.set_message(msg);
pb.finish();
}
Ok(())
}
/// Perform a UDP traceroute
async fn trace_udp(&self, target_ip: IpAddr) -> Result<(), NtraceError> {
// Both IPv4 and IPv6 are supported
// Create a more informative progress indicator
let progress = if cfg!(not(test)) {
use indicatif::{ProgressBar, ProgressStyle};
let pb = ProgressBar::new(self.config.max_hops as u64);
pb.set_style(
ProgressStyle::default_bar()
.template("[{elapsed_precise}] [{bar:40.cyan/blue}] {pos}/{len} hops - {msg}")
.unwrap()
.progress_chars("█▓▒░ "),
);
pb.set_message(format!("Tracing route to {} via TCP", target_ip));
Some(pb)
} else {
None
};
// For UDP traceroute, we'll use a simpler approach with standard sockets
// since pnet's UDP implementation is more complex to work with
for ttl in 1..=self.config.max_hops {
// Create a hop result entry
let mut hop_result = HopResult {
hop: ttl,
ip: None,
hostname: None,
latencies: vec![None; self.config.queries as usize],
avg_latency: None,
min_latency: None,
max_latency: None,
std_dev_latency: None,
packet_loss: 0.0,
is_destination: false,
asn: None,
org: None,
location: None,
mpls_labels: None,
timestamp: Some(chrono::Utc::now()),
};
// Send multiple queries for this hop
let mut responses = 0;
let mut total_latency = Duration::new(0, 0);
for q in 0..self.config.queries {
// Create UDP socket with appropriate binding for IPv4 or IPv6
let bind_addr = match target_ip {
IpAddr::V4(_) => "0.0.0.0:0",
IpAddr::V6(_) => "[::]:0",
};
let socket = match std::net::UdpSocket::bind(bind_addr) {
Ok(s) => {
// Set TTL
if let Err(e) = s.set_ttl(ttl.into()) {
warn!("Failed to set TTL: {}", e);
continue;
}
// Set timeouts
if let Err(e) =
s.set_read_timeout(Some(Duration::from_millis(self.config.timeout_ms)))
{
warn!("Failed to set read timeout: {}", e);
continue;
}
s
}
Err(e) => {
warn!("Failed to create UDP socket: {}", e);
continue;
}
};
// Create a simple payload
let mut payload = vec![0u8; self.config.payload_size];
rand::rng().fill(&mut payload[..]);
// Start timing
let start_time = Instant::now();
// Send the packet
if let Err(e) =
socket.send_to(&payload, SocketAddr::new(target_ip, self.config.port))
{
warn!("Failed to send UDP packet: {}", e);
continue;
}
// Wait for response
let mut buf = [0u8; 1024];
match socket.recv_from(&mut buf) {
Ok((_, addr)) => {
// Got a response
let latency = start_time.elapsed();
hop_result.latencies[q as usize] = Some(latency);
hop_result.ip = Some(addr.ip().to_string());
// Check if this is the destination
if addr.ip() == target_ip {
hop_result.is_destination = true;
}
responses += 1;
total_latency += latency;
}
Err(e) => {
debug!("No response from UDP packet: {}", e);
}
}
// Wait between queries
if q < self.config.queries - 1 {
tokio::time::sleep(Duration::from_millis(self.config.send_time_ms)).await;
}
}
// Calculate average latency if we got responses
if responses > 0 {
hop_result.avg_latency = Some(total_latency / responses as u32);
}
// Resolve hostname if we have an IP and hostname resolution is enabled
if let Some(ip_str) = &hop_result.ip {
if self.config.resolve_hostnames {
if let Ok(ip) = ip_str.parse::<IpAddr>() {
hop_result.hostname = self.resolve_hostname(ip).await;
}
}
}
// Store the result
{
let mut results = self.results.lock().await;
results.insert(ttl, hop_result.clone());
}
// Update progress with more information
if let Some(pb) = &progress {
let msg = match &hop_result.ip {
Some(ip) => match &hop_result.hostname {
Some(hostname) => format!("Found hop {}: {} ({})", ttl, ip, hostname),
None => format!("Found hop {}: {}", ttl, ip),
},
None => format!("No response at hop {}", ttl),
};
pb.set_message(msg);
pb.inc(1);
}
// If we reached the destination, we're done
if hop_result.is_destination {
break;
}
// Wait between TTLs
if ttl < self.config.max_hops {
tokio::time::sleep(Duration::from_millis(self.config.ttl_time_ms)).await;
}
}
// Finish progress with summary
if let Some(pb) = &progress {
let results = self.results.lock().await;
let hop_count = results.len();
let destination_reached = results.values().any(|hop| hop.is_destination);
let msg = if destination_reached {
format!("Completed trace to {} in {} hops", target_ip, hop_count)
} else {
format!("Trace to {} incomplete after {} hops", target_ip, hop_count)
};
pb.set_message(msg);
pb.finish();
}
Ok(())
}
/// Perform an ICMP traceroute
#[allow(dead_code)]
async fn trace_icmp(&self, target_ip: IpAddr) -> Result<(), NtraceError> {
// Both IPv4 and IPv6 are supported
// Create a more informative progress indicator
let progress = if cfg!(not(test)) {
use indicatif::{ProgressBar, ProgressStyle};
let pb = ProgressBar::new(self.config.max_hops as u64);
pb.set_style(
ProgressStyle::default_bar()
.template("[{elapsed_precise}] [{bar:40.cyan/blue}] {pos}/{len} hops - {msg}")
.unwrap()
.progress_chars("█▓▒░ "),
);
pb.set_message(format!("Tracing route to {} via TCP", target_ip));
Some(pb)
} else {
None
};
// For ICMP traceroute, we'll use a UDP socket with TTL to trigger ICMP responses
for ttl in 1..=self.config.max_hops {
// Create a hop result entry
let mut hop_result = HopResult {
hop: ttl,
ip: None,
hostname: None,
latencies: vec![None; self.config.queries as usize],
avg_latency: None,
min_latency: None,
max_latency: None,
std_dev_latency: None,
packet_loss: 0.0,
is_destination: false,
asn: None,
org: None,
location: None,
mpls_labels: None,
timestamp: Some(chrono::Utc::now()),
};
// Send multiple queries for this hop
let mut responses = 0;
let mut total_latency = Duration::new(0, 0);
for q in 0..self.config.queries {
// Create a UDP socket to send packets with appropriate binding for IPv4 or IPv6
let bind_addr = match target_ip {
IpAddr::V4(_) => "0.0.0.0:0",
IpAddr::V6(_) => "[::]:0",
};
let send_socket = match std::net::UdpSocket::bind(bind_addr) {
Ok(s) => {
// Set TTL
if let Err(e) = s.set_ttl(ttl.into()) {
warn!("Failed to set TTL: {}", e);
continue;
}
s
}
Err(e) => {
warn!("Failed to create send socket: {}", e);
continue;
}
};
// Create a separate socket to listen for ICMP responses
let recv_socket = match std::net::UdpSocket::bind(bind_addr) {
Ok(s) => {
// Set read timeout
if let Err(e) =
s.set_read_timeout(Some(Duration::from_millis(self.config.timeout_ms)))
{
warn!("Failed to set read timeout: {}", e);
continue;
}
s
}
Err(e) => {
warn!("Failed to create receive socket: {}", e);
continue;
}
};
// Create a simple payload
let mut payload = vec![0u8; self.config.payload_size];
rand::rng().fill(&mut payload[..]);
// Start timing
let start_time = Instant::now();
// Send the packet to an unreachable port to trigger ICMP responses
let dest_port = 33434 + (ttl as u16);
if let Err(e) = send_socket.send_to(&payload, SocketAddr::new(target_ip, dest_port))
{
warn!("Failed to send packet: {}", e);
continue;
}
// Try to receive a response on both sockets
let mut buf = [0u8; 1024];
let mut got_response = false;
// First try the send socket which might get ICMP errors
match send_socket.recv_from(&mut buf) {
Ok((_, addr)) => {
let latency = start_time.elapsed();
hop_result.latencies[q as usize] = Some(latency);
hop_result.ip = Some(addr.ip().to_string());
// Check if this is the destination
if addr.ip() == target_ip {
hop_result.is_destination = true;
}
responses += 1;
total_latency += latency;
got_response = true;
}
Err(e) => {
debug!("No response on send socket: {}", e);
}
}
// If no response on send socket, try the receive socket
if !got_response {
match recv_socket.recv_from(&mut buf) {
Ok((_, addr)) => {
let latency = start_time.elapsed();
hop_result.latencies[q as usize] = Some(latency);
hop_result.ip = Some(addr.ip().to_string());
// Check if this is the destination
if addr.ip() == target_ip {
hop_result.is_destination = true;
}
responses += 1;
total_latency += latency;
}
Err(e) => {
debug!("No response from packet: {}", e);
// Try to extract router IP from error (platform specific)
if let Some(router_ip) = Self::extract_router_ip_from_error(&e) {
let latency = start_time.elapsed();
hop_result.latencies[q as usize] = Some(latency);
hop_result.ip = Some(router_ip.to_string());
responses += 1;
total_latency += latency;
}
}
}
}
// Wait between queries
if q < self.config.queries - 1 {
tokio::time::sleep(Duration::from_millis(self.config.send_time_ms)).await;
}
}
// Calculate average latency if we got responses
if responses > 0 {
hop_result.avg_latency = Some(total_latency / responses as u32);
}
// Resolve hostname if we have an IP and hostname resolution is enabled
if let Some(ip_str) = &hop_result.ip {
if self.config.resolve_hostnames {
if let Ok(ip) = ip_str.parse::<IpAddr>() {
hop_result.hostname = self.resolve_hostname(ip).await;
}
}
}
// Store the result
{
let mut results = self.results.lock().await;
results.insert(ttl, hop_result.clone());
}
// Update progress with more information
if let Some(pb) = &progress {
let msg = match &hop_result.ip {
Some(ip) => match &hop_result.hostname {
Some(hostname) => format!("Found hop {}: {} ({})", ttl, ip, hostname),
None => format!("Found hop {}: {}", ttl, ip),
},
None => format!("No response at hop {}", ttl),
};
pb.set_message(msg);
pb.inc(1);
}
// If we reached the destination, we're done
if hop_result.is_destination {
break;
}
// Wait between TTLs
if ttl < self.config.max_hops {
tokio::time::sleep(Duration::from_millis(self.config.ttl_time_ms)).await;
}
}
// Finish progress with summary
if let Some(pb) = &progress {
let results = self.results.lock().await;
let hop_count = results.len();
let destination_reached = results.values().any(|hop| hop.is_destination);
let msg = if destination_reached {
format!("Completed trace to {} in {} hops", target_ip, hop_count)
} else {
format!("Trace to {} incomplete after {} hops", target_ip, hop_count)
};
pb.set_message(msg);
pb.finish();
}
Ok(())
}
/// Alternative ICMP traceroute implementation using a different approach
#[allow(dead_code)]
async fn trace_icmp_alternative(&self, target_ip: IpAddr) -> Result<(), NtraceError> {
// Both IPv4 and IPv6 are supported
// Create a more informative progress indicator
let progress = if cfg!(not(test)) {
use indicatif::{ProgressBar, ProgressStyle};
let pb = ProgressBar::new(self.config.max_hops as u64);
pb.set_style(
ProgressStyle::default_bar()
.template("[{elapsed_precise}] [{bar:40.cyan/blue}] {pos}/{len} hops - {msg}")
.unwrap()
.progress_chars("█▓▒░ "),
);
pb.set_message(format!("Tracing route to {} via TCP", target_ip));
Some(pb)
} else {
None
};
// For each TTL value
for ttl in 1..=self.config.max_hops {
// Create a hop result entry
let mut hop_result = HopResult {
hop: ttl,
ip: None,
hostname: None,
latencies: vec![None; self.config.queries as usize],
avg_latency: None,
min_latency: None,
max_latency: None,
std_dev_latency: None,
packet_loss: 0.0,
is_destination: false,
asn: None,
org: None,
location: None,
mpls_labels: None,
timestamp: Some(chrono::Utc::now()),
};
// Send multiple queries for this hop
let mut responses = 0;
let mut total_latency = Duration::new(0, 0);
for q in 0..self.config.queries {
// Create a listener socket first
let listener = match std::net::UdpSocket::bind("0.0.0.0:0") {
Ok(s) => {
if let Err(e) =
s.set_read_timeout(Some(Duration::from_millis(self.config.timeout_ms)))
{
warn!("Failed to set read timeout: {}", e);
continue;
}
s
}
Err(e) => {
warn!("Failed to create listener socket: {}", e);
continue;
}
};
// Get the port we're bound to
let local_addr = match listener.local_addr() {
Ok(addr) => addr,
Err(e) => {
warn!("Failed to get local address: {}", e);
continue;
}
};
let local_port = local_addr.port();
// Create a sender socket
let sender = match std::net::UdpSocket::bind("0.0.0.0:0") {
Ok(s) => {
// Set TTL
if let Err(e) = s.set_ttl(ttl.into()) {
warn!("Failed to set TTL: {}", e);
continue;
}
s
}
Err(e) => {
warn!("Failed to create sender socket: {}", e);
continue;
}
};
// Create a simple payload
let mut payload = vec![0u8; self.config.payload_size];
rand::rng().fill(&mut payload[..]);
// Start timing
let start_time = Instant::now();
// Send to an unreachable port at the target
// The key is to use the same port as our listener, which helps with ICMP error correlation
if let Err(e) = sender.send_to(&payload, SocketAddr::new(target_ip, local_port)) {
warn!("Failed to send packet: {}", e);
continue;
}
// Try to receive a response
let mut buf = [0u8; 1024];
match listener.recv_from(&mut buf) {
Ok((_, addr)) => {
// Got a response
let latency = start_time.elapsed();
hop_result.latencies[q as usize] = Some(latency);
hop_result.ip = Some(addr.ip().to_string());
// Check if this is the destination
if addr.ip() == target_ip {
hop_result.is_destination = true;
}
responses += 1;
total_latency += latency;
}
Err(e) => {
debug!("No response from packet: {}", e);
// Try a different approach - send a second packet to see if we get a response
// This can sometimes work when the first approach fails
let second_socket = match std::net::UdpSocket::bind("0.0.0.0:0") {
Ok(s) => s,
Err(_) => continue,
};
if let Err(_) =
second_socket.connect(SocketAddr::new(target_ip, 33434 + ttl as u16))
{
// Check the error kind - it might contain the router's IP
if let Some(router_ip) = extract_ip_from_last_error() {
if let Ok(_ip_addr) = router_ip.parse::<IpAddr>() {
let latency = start_time.elapsed();
hop_result.latencies[q as usize] = Some(latency);
hop_result.ip = Some(router_ip);
responses += 1;
total_latency += latency;
}
}
}
}
}
// Wait between queries
if q < self.config.queries - 1 {
tokio::time::sleep(Duration::from_millis(self.config.send_time_ms)).await;
}
}
// Calculate average latency if we got responses
if responses > 0 {
hop_result.avg_latency = Some(total_latency / responses as u32);
}
// Resolve hostname if we have an IP and hostname resolution is enabled
if let Some(ip_str) = &hop_result.ip {
if self.config.resolve_hostnames {
if let Ok(ip) = ip_str.parse::<IpAddr>() {
hop_result.hostname = self.resolve_hostname(ip).await;
}
}
}
// Store the result
{
let mut results = self.results.lock().await;
results.insert(ttl, hop_result.clone());
}
// Update progress with more information
if let Some(pb) = &progress {
let msg = match &hop_result.ip {
Some(ip) => match &hop_result.hostname {
Some(hostname) => format!("Found hop {}: {} ({})", ttl, ip, hostname),
None => format!("Found hop {}: {}", ttl, ip),
},
None => format!("No response at hop {}", ttl),
};
pb.set_message(msg);
pb.inc(1);
}
// If we reached the destination, we're done
if hop_result.is_destination {
break;
}
// Wait between TTLs
if ttl < self.config.max_hops {
tokio::time::sleep(Duration::from_millis(self.config.ttl_time_ms)).await;
}
}
// Finish progress with summary
if let Some(pb) = &progress {
let results = self.results.lock().await;
let hop_count = results.len();
let destination_reached = results.values().any(|hop| hop.is_destination);
let msg = if destination_reached {
format!("Completed trace to {} in {} hops", target_ip, hop_count)
} else {
format!("Trace to {} incomplete after {} hops", target_ip, hop_count)
};
pb.set_message(msg);
pb.finish();
}
Ok(())
}
/// Perform a raw socket based ICMP traceroute similar to inetutils-traceroute
async fn trace_icmp_raw(&self, target_ip: IpAddr) -> Result<(), NtraceError> {
use pnet::packet::Packet;
use pnet::packet::icmp::{IcmpTypes, echo_request};
use pnet::packet::icmpv6::{Icmpv6Types, echo_request as icmpv6_echo_request};
use pnet::packet::ip::IpNextHeaderProtocols;
use pnet::transport::TransportChannelType::Layer4;
use pnet::transport::TransportProtocol::{Ipv4, Ipv6};
use pnet::transport::{icmp_packet_iter, icmpv6_packet_iter, transport_channel};
// Both IPv4 and IPv6 are supported
let is_ipv6 = matches!(target_ip, IpAddr::V6(_));
// Create a more informative progress indicator
let progress = if cfg!(not(test)) {
use indicatif::{ProgressBar, ProgressStyle};
let pb = ProgressBar::new(self.config.max_hops as u64);
pb.set_style(
ProgressStyle::default_bar()
.template("[{elapsed_precise}] [{bar:40.cyan/blue}] {pos}/{len} hops - {msg}")
.unwrap()
.progress_chars("█▓▒░ "),
);
pb.set_message(format!("Tracing route to {} via TCP", target_ip));
Some(pb)
} else {
None
};
// Create a transport channel for ICMP (IPv4 or IPv6)
let (mut tx, mut rx) = if is_ipv6 {
let protocol = Layer4(Ipv6(IpNextHeaderProtocols::Icmpv6));
match transport_channel(4096, protocol) {
Ok((tx, rx)) => (tx, rx),
Err(e) => {
// Check if this is a permission error
if e.kind() == std::io::ErrorKind::PermissionDenied {
return Err(NtraceError::PermissionDenied2(
"Permission denied creating ICMPv6 socket. Try running with sudo or as administrator.".to_string()
));
} else {
return Err(NtraceError::IcmpError(format!(
"Failed to create IPv6 transport channel: {}",
e
)));
}
}
}
} else {
let protocol = Layer4(Ipv4(IpNextHeaderProtocols::Icmp));
match transport_channel(4096, protocol) {
Ok((tx, rx)) => (tx, rx),
Err(e) => {
// Check if this is a permission error
if e.kind() == std::io::ErrorKind::PermissionDenied {
return Err(NtraceError::PermissionDenied2(
"Permission denied creating ICMP socket. Try running with sudo or as administrator.".to_string()
));
} else {
return Err(NtraceError::IcmpError(format!(
"Failed to create IPv4 transport channel: {}",
e
)));
}
}
}
};
// We'll handle the packet reception directly instead of using iterators
// This avoids the double mutable borrow of rx
// For each TTL value
for ttl in 1..=self.config.max_hops {
// Create a hop result entry
let mut hop_result = HopResult {
hop: ttl,
ip: None,
hostname: None,
latencies: vec![None; self.config.queries as usize],
avg_latency: None,
min_latency: None,
max_latency: None,
std_dev_latency: None,
packet_loss: 0.0,
is_destination: false,
asn: None,
org: None,
location: None,
mpls_labels: None,
timestamp: Some(chrono::Utc::now()),
};
// Send multiple queries for this hop
let mut responses = 0;
let mut total_latency = Duration::new(0, 0);
for q in 0..self.config.queries {
if is_ipv6 {
// Create an ICMPv6 echo request packet
// Buffer for the ICMPv6 packet
let mut echo_packet = [0u8; 64];
// Fill the payload with some data first
let payload_offset =
icmpv6_echo_request::MutableEchoRequestPacket::minimum_packet_size();
let payload_size = self
.config
.payload_size
.min(echo_packet.len() - payload_offset);
rand::rng()
.fill(&mut echo_packet[payload_offset..payload_offset + payload_size]);
// Now create the packet
let mut icmpv6_packet =
icmpv6_echo_request::MutableEchoRequestPacket::new(&mut echo_packet)
.ok_or_else(|| {
NtraceError::Protocol("Failed to create ICMPv6 packet".to_string())
})?;
// Set ICMPv6 packet fields
icmpv6_packet.set_icmpv6_type(Icmpv6Types::EchoRequest);
icmpv6_packet.set_icmpv6_code(icmpv6_echo_request::Icmpv6Codes::NoCode);
let identifier = (std::process::id() & 0xFFFF) as u16;
icmpv6_packet.set_identifier(identifier);
icmpv6_packet.set_sequence_number(q as u16);
// Calculate checksum
let checksum = pnet::util::checksum(icmpv6_packet.packet(), 1);
icmpv6_packet.set_checksum(checksum);
// Set the TTL (hop limit) on the socket
if let Err(e) = tx.set_ttl(ttl) {
warn!("Failed to set TTL for IPv6: {}", e);
if e.kind() == std::io::ErrorKind::PermissionDenied {
return Err(NtraceError::PermissionDenied2(
"Permission denied setting IPv6 TTL. Try running with sudo or as administrator.".to_string()
));
} else {
debug!("Non-critical TTL setting error: {}", e);
continue;
}
}
// Start timing
let start_time = Instant::now();
// Send the packet
match tx.send_to(icmpv6_packet, target_ip) {
Ok(_) => {}
Err(e) => {
warn!("Failed to send ICMPv6 packet: {}", e);
match e.kind() {
std::io::ErrorKind::PermissionDenied => {
return Err(NtraceError::PermissionDenied2(
"Permission denied sending ICMPv6 packet. Try running with sudo or as administrator.".to_string()
));
}
std::io::ErrorKind::ConnectionRefused => {
debug!("Connection refused when sending ICMPv6 packet");
}
std::io::ErrorKind::NetworkUnreachable => {
return Err(NtraceError::IcmpError(
"Network unreachable for target IP".to_string(),
));
}
_ => {
debug!("Error sending ICMPv6 packet: {}", e);
}
}
continue;
}
}
// Set a timeout for receiving
let timeout = Duration::from_millis(self.config.timeout_ms);
let start_wait = Instant::now();
// Wait for a response
let mut got_response = false;
while start_wait.elapsed() < timeout && !got_response {
// Use the icmpv6_packet_iter directly on rx
let mut iter = icmpv6_packet_iter(&mut rx);
match iter.next_with_timeout(timeout) {
Ok(Some((packet, addr))) => {
let latency = start_time.elapsed();
// Check if this is a TTL exceeded message or echo reply
if packet.get_icmpv6_type() == Icmpv6Types::TimeExceeded
|| (packet.get_icmpv6_type() == Icmpv6Types::EchoReply
&& packet.get_icmpv6_code().0
== icmpv6_echo_request::Icmpv6Codes::NoCode.0)
{
hop_result.latencies[q as usize] = Some(latency);
hop_result.ip = Some(addr.to_string());
// Check if this is the destination
if addr == target_ip {
hop_result.is_destination = true;
}
responses += 1;
total_latency += latency;
got_response = true;
}
}
Ok(None) => {
// Timeout reached
break;
}
Err(e) => {
debug!("Error receiving IPv6 packet: {}", e);
break;
}
}
}
} else {
// Create an ICMP echo request packet
// Buffer for the ICMP packet
let mut echo_packet = [0u8; 64];
// Fill the payload with some data first
let payload_offset =
echo_request::MutableEchoRequestPacket::minimum_packet_size();
let payload_size = self
.config
.payload_size
.min(echo_packet.len() - payload_offset);
rand::rng()
.fill(&mut echo_packet[payload_offset..payload_offset + payload_size]);
// Now create the packet
let mut icmp_packet =
echo_request::MutableEchoRequestPacket::new(&mut echo_packet).ok_or_else(
|| NtraceError::Protocol("Failed to create ICMP packet".to_string()),
)?;
// Set ICMP packet fields
icmp_packet.set_icmp_type(IcmpTypes::EchoRequest);
icmp_packet.set_icmp_code(echo_request::IcmpCodes::NoCode);
let identifier = (std::process::id() & 0xFFFF) as u16;
icmp_packet.set_identifier(identifier);
icmp_packet.set_sequence_number(q as u16);
// Calculate checksum
let checksum = pnet::util::checksum(icmp_packet.packet(), 1);
icmp_packet.set_checksum(checksum);
// Set the TTL on the socket
if let Err(e) = tx.set_ttl(ttl) {
warn!("Failed to set TTL for IPv4: {}", e);
if e.kind() == std::io::ErrorKind::PermissionDenied {
return Err(NtraceError::PermissionDenied2(
"Permission denied setting IPv4 TTL. Try running with sudo or as administrator.".to_string()
));
} else {
debug!("Non-critical TTL setting error: {}", e);
continue;
}
}
// Start timing
let start_time = Instant::now();
// Send the packet
match tx.send_to(icmp_packet, target_ip) {
Ok(_) => {}
Err(e) => {
warn!("Failed to send ICMP packet: {}", e);
match e.kind() {
std::io::ErrorKind::PermissionDenied => {
return Err(NtraceError::PermissionDenied2(
"Permission denied sending ICMP packet. Try running with sudo or as administrator.".to_string()
));
}
std::io::ErrorKind::ConnectionRefused => {
debug!("Connection refused when sending ICMP packet");
}
std::io::ErrorKind::NetworkUnreachable => {
return Err(NtraceError::IcmpError(
"Network unreachable for target IP".to_string(),
));
}
_ => {
debug!("Error sending ICMP packet: {}", e);
}
}
continue;
}
}
// Set a timeout for receiving
let timeout = Duration::from_millis(self.config.timeout_ms);
let start_wait = Instant::now();
// Wait for a response
let mut got_response = false;
while start_wait.elapsed() < timeout && !got_response {
// Use the icmp_packet_iter directly on rx
let mut iter = icmp_packet_iter(&mut rx);
match iter.next_with_timeout(timeout) {
Ok(Some((packet, addr))) => {
let latency = start_time.elapsed();
// Check if this is a TTL exceeded message or echo reply
if packet.get_icmp_type() == IcmpTypes::TimeExceeded
|| (packet.get_icmp_type() == IcmpTypes::EchoReply
&& packet.get_icmp_code().0
== echo_request::IcmpCodes::NoCode.0)
{
hop_result.latencies[q as usize] = Some(latency);
hop_result.ip = Some(addr.to_string());
// Check if this is the destination
if addr == target_ip {
hop_result.is_destination = true;
}
responses += 1;
total_latency += latency;
got_response = true;
}
}
Ok(None) => {
// Timeout reached
break;
}
Err(e) => {
debug!("Error receiving packet: {}", e);
break;
}
}
}
}
// Wait between queries
if q < self.config.queries - 1 {
tokio::time::sleep(Duration::from_millis(self.config.send_time_ms)).await;
}
}
// Calculate average latency if we got responses
if responses > 0 {
hop_result.avg_latency = Some(total_latency / responses as u32);
}
// Resolve hostname if we have an IP and hostname resolution is enabled
if let Some(ip_str) = &hop_result.ip {
if self.config.resolve_hostnames {
if let Ok(ip) = ip_str.parse::<IpAddr>() {
hop_result.hostname = self.resolve_hostname(ip).await;
}
}
}
// Store the result
{
let mut results = self.results.lock().await;
results.insert(ttl, hop_result.clone());
}
// Update progress with more information
if let Some(pb) = &progress {
let msg = match &hop_result.ip {
Some(ip) => match &hop_result.hostname {
Some(hostname) => format!("Found hop {}: {} ({})", ttl, ip, hostname),
None => format!("Found hop {}: {}", ttl, ip),
},
None => format!("No response at hop {}", ttl),
};
pb.set_message(msg);
pb.inc(1);
}
// If we reached the destination, we're done
if hop_result.is_destination {
break;
}
// Wait between TTLs
if ttl < self.config.max_hops {
tokio::time::sleep(Duration::from_millis(self.config.ttl_time_ms)).await;
}
}
// Finish progress with summary
if let Some(pb) = &progress {
let results = self.results.lock().await;
let hop_count = results.len();
let destination_reached = results.values().any(|hop| hop.is_destination);
let msg = if destination_reached {
format!("Completed trace to {} in {} hops", target_ip, hop_count)
} else {
format!("Trace to {} incomplete after {} hops", target_ip, hop_count)
};
pb.set_message(msg);
pb.finish();
}
Ok(())
}
/// Discover the path MTU to the target
///
/// This method attempts to find the Maximum Transmission Unit (MTU) along
/// the path to the target by sending packets of various sizes with the
/// DF (Don't Fragment) bit set and observing ICMP "fragmentation needed"
/// responses.
///
/// # Arguments
///
/// * `target_ip` - The IP address of the target
///
/// # Returns
///
/// The discovered path MTU in bytes, or None if discovery failed
async fn discover_path_mtu(&self, target_ip: IpAddr) -> Option<u16> {
if !self.config.discover_mtu {
return None;
}
info!("Performing path MTU discovery to {}", target_ip);
// Common MTU sizes to test
let mtu_sizes = vec![
1500, 1492, 1472, 1468, 1450, 1400, 1280, 1024, 576, 552, 512,
];
// For IPv4
if let IpAddr::V4(ipv4) = target_ip {
#[cfg(target_family = "unix")]
{
use pnet::packet::ip::IpNextHeaderProtocols;
use pnet::packet::ipv4::MutableIpv4Packet;
use pnet::transport::TransportChannelType::Layer3;
use pnet::transport::{ipv4_packet_iter, transport_channel};
// Create a raw IP socket
let protocol = Layer3(IpNextHeaderProtocols::Icmp);
let (mut tx, mut rx) = match transport_channel(4096, protocol) {
Ok((tx, rx)) => (tx, rx),
Err(_) => return None,
};
// Try each MTU size
for &mtu in &mtu_sizes {
// Create an IPv4 packet with the DF bit set
let mut buffer = vec![0u8; mtu as usize];
let mut ipv4_packet = MutableIpv4Packet::new(&mut buffer).unwrap();
// Set IPv4 header fields
ipv4_packet.set_version(4);
ipv4_packet.set_header_length(5);
ipv4_packet.set_total_length(mtu);
ipv4_packet.set_identification(rand::random::<u16>());
ipv4_packet.set_flags(0b010); // Set DF (Don't Fragment) bit
ipv4_packet.set_ttl(64);
ipv4_packet.set_next_level_protocol(IpNextHeaderProtocols::Icmp);
ipv4_packet.set_source(Ipv4Addr::new(127, 0, 0, 1)); // Will be replaced by kernel
ipv4_packet.set_destination(ipv4);
// Send the packet
match tx.send_to(ipv4_packet, IpAddr::V4(ipv4)) {
Ok(_) => {}
Err(_) => continue,
}
// Wait for a response with timeout
let timeout = Duration::from_millis(self.config.timeout_ms);
let start_time = Instant::now();
while start_time.elapsed() < timeout {
let mut iter = ipv4_packet_iter(&mut rx);
match iter.next_with_timeout(timeout) {
Ok(Some((packet, _))) => {
// Check if this is a "fragmentation needed" ICMP message
if packet.get_next_level_protocol() == IpNextHeaderProtocols::Icmp {
// This would need more detailed ICMP parsing to check for
// "fragmentation needed" message type
// For now, we'll assume any response means fragmentation needed
// Try the next smaller MTU
break;
}
}
_ => break,
}
}
// If we get here without a "fragmentation needed" response, this MTU works
return Some(mtu);
}
}
}
// Default to a conservative MTU if discovery fails
Some(576)
}
/// Extract router IP from socket error
fn extract_router_ip_from_error(error: &std::io::Error) -> Option<IpAddr> {
// On most systems, we can't easily extract the router IP from the error
// This is a platform specific operation that would require raw socket handling
// Try to get the error number for more specific handling
let _errno = error.raw_os_error();
// Platform specific handling
#[cfg(target_os = "linux")]
{
// On Linux, for ICMP Time Exceeded messages, we can try to extract the IP
// from the error message or use socket options to get the original sender
// EAGAIN
if let Some(11) = _errno {
// For Linux, try to extract from error message first
let error_string = error.to_string();
if let Some(ip_str) = extract_ip_from_string(&error_string) {
if let Ok(ip) = ip_str.parse::<IpAddr>() {
return Some(ip);
}
}
// If that fails, try to get the IP from the last socket error
if let Some(ip_str) = extract_ip_from_last_error() {
if let Ok(ip) = ip_str.parse::<IpAddr>() {
return Some(ip);
}
}
}
}
#[cfg(target_os = "windows")]
{
// Windows specific handling
let error_string = error.to_string();
if let Some(ip_str) = extract_ip_from_string(&error_string) {
if let Ok(ip) = ip_str.parse::<IpAddr>() {
return Some(ip);
}
}
// Windows-specific error handling for TTL exceeded
// WSAEHOSTUNREACH (10065) or WSAETIMEDOUT (10060)
if errno == Some(10065) || errno == Some(10060) {
// Try to extract from socket error
if let Some(ip_str) = extract_ip_from_last_error() {
if let Ok(ip) = ip_str.parse::<IpAddr>() {
return Some(ip);
}
}
}
}
#[cfg(target_os = "macos")]
{
// macOS specific handling
let error_string = error.to_string();
if let Some(ip_str) = extract_ip_from_string(&error_string) {
if let Ok(ip) = ip_str.parse::<IpAddr>() {
return Some(ip);
}
}
}
// Generic fallback for all platforms
let error_string = error.to_string();
if let Some(ip_str) = extract_ip_from_string(&error_string) {
if let Ok(ip) = ip_str.parse::<IpAddr>() {
return Some(ip);
}
}
None
}
}
/// Helper function to try to extract an IP address from a string
fn extract_ip_from_string(s: &str) -> Option<String> {
// Try to extract IPv4 address first (look for patterns like xxx.xxx.xxx.xxx)
let ipv4_re = regex::Regex::new(r"\b(?:\d{1,3}\.){3}\d{1,3}\b").ok()?;
if let Some(m) = ipv4_re.find(s) {
let ip_str = m.as_str().to_string();
// Validate that it's a proper IPv4 address
if ip_str.parse::<Ipv4Addr>().is_ok() {
return Some(ip_str);
}
}
// If no IPv4 address found, try to extract IPv6 address
// This is a simplified pattern and might not catch all valid IPv6 formats
let ipv6_re = regex::Regex::new(r"\b(?:[0-9a-fA-F]{1,4}:){7}[0-9a-fA-F]{1,4}\b|(?:[0-9a-fA-F]{1,4}:){1,7}:|(?:[0-9a-fA-F]{1,4}:){1,6}:[0-9a-fA-F]{1,4}|(?:[0-9a-fA-F]{1,4}:){1,5}(?::[0-9a-fA-F]{1,4}){1,2}|(?:[0-9a-fA-F]{1,4}:){1,4}(?::[0-9a-fA-F]{1,4}){1,3}|(?:[0-9a-fA-F]{1,4}:){1,3}(?::[0-9a-fA-F]{1,4}){1,4}|(?:[0-9a-fA-F]{1,4}:){1,2}(?::[0-9a-fA-F]{1,4}){1,5}|[0-9a-fA-F]{1,4}:(?::[0-9a-fA-F]{1,4}){1,6}|:(?:(?::[0-9a-fA-F]{1,4}){1,7}|:)").ok()?;
if let Some(m) = ipv6_re.find(s) {
let ip_str = m.as_str().to_string();
// Validate that it's a proper IPv6 address
if ip_str.parse::<Ipv6Addr>().is_ok() {
return Some(ip_str);
}
}
None
}
/// Helper function to try to extract an IP from the last socket error
fn extract_ip_from_last_error() -> Option<String> {
// Get the last error message
let error = std::io::Error::last_os_error().to_string();
debug!("Extracting IP from error: {}", error);
extract_ip_from_string(&error)
}
/// Utility function to check if we have root/admin privileges
fn has_root_privileges() -> bool {
#[cfg(target_family = "unix")]
{
unsafe { libc::geteuid() == 0 }
}
#[cfg(target_family = "windows")]
{
// On Windows, we can't easily check for admin privileges
// We'll just try the operation and see if it fails
true
}
#[cfg(not(any(target_family = "unix", target_family = "windows")))]
{
false
}
}