ftr 0.10.0

A fast, parallel ICMP traceroute with ASN lookup, reverse DNS, and ISP detection
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
//! Async high-level traceroute API
//!
//! This module provides the async API for performing traceroute operations
//! with immediate response processing using Tokio.

use crate::dns::resolver;
use crate::services::Services;
use crate::socket::factory::create_probe_socket_with_options_and_verbose;
use crate::traceroute::config::PreferredFamily;
use crate::traceroute::engine::TracerouteEngine;
use crate::traceroute::{TracerouteConfig, TracerouteError, TracerouteResult};
use std::net::IpAddr;

/// Async traceroute API
#[derive(Debug)]
pub struct Traceroute {
    config: TracerouteConfig,
    target_ip: IpAddr,
    services: Option<Services>,
}

impl Traceroute {
    /// Create a new async traceroute from configuration with injected services
    pub async fn new_with_services(
        mut config: TracerouteConfig,
        services: Services,
    ) -> Result<Self, TracerouteError> {
        let target_ip = Self::resolve_target(&mut config).await?;

        Ok(Self {
            config,
            target_ip,
            services: Some(services),
        })
    }

    /// Resolve the target hostname/IP to an IpAddr, honoring the
    /// configuration's family preference
    async fn resolve_target(config: &mut TracerouteConfig) -> Result<IpAddr, TracerouteError> {
        // IP literals and "localhost" resolve deterministically and take
        // precedence over a pre-set target_ip (pre-0.9 precedence order).
        let target_is_literal =
            config.target.parse::<IpAddr>().is_ok() || config.target == "localhost";

        if !target_is_literal {
            // Already resolved (skips DNS): validate the family preference
            // still holds, then use it.
            if let Some(ip) = config.target_ip {
                check_family(ip, config.preferred_family, &config.target)?;
                return Ok(ip);
            }
        }

        let ip = resolve_target_with_family(&config.target, config.preferred_family).await?;
        config.target_ip = Some(ip);
        Ok(ip)
    }

    /// Create a new async traceroute from configuration (uses global caches)
    pub async fn new(mut config: TracerouteConfig) -> Result<Self, TracerouteError> {
        let target_ip = Self::resolve_target(&mut config).await?;

        Ok(Self {
            config,
            target_ip,
            services: None,
        })
    }

    /// Run the async traceroute
    pub async fn run(self) -> Result<TracerouteResult, TracerouteError> {
        // Use timing config from traceroute config, but override socket_read_timeout with probe_timeout
        let mut timing_config = self.config.timing.clone();
        timing_config.socket_read_timeout = self.config.probe_timeout;

        // Create socket with protocol preference; verbosity is passed
        // explicitly (never via environment variables, which would race
        // between concurrent traces in the same process)
        let socket = create_probe_socket_with_options_and_verbose(
            self.target_ip,
            timing_config,
            self.config.protocol,
            self.config.socket_mode,
            self.config.verbose,
        )
        .await?;

        // Create and run fully parallel async engine. Engine errors are
        // already TracerouteError values; propagate them unchanged so typed
        // variants (e.g. Ipv6NotSupported, InsufficientPermissions) survive.
        let engine = if let Some(services) = self.services {
            TracerouteEngine::new_with_services(
                socket,
                self.config.clone(),
                self.target_ip,
                std::sync::Arc::new(services),
            )
            .await?
        } else {
            TracerouteEngine::new(socket, self.config.clone(), self.target_ip).await?
        };

        let result = engine.run().await?;

        // The fully parallel engine handles enrichment internally, so we can just return
        Ok(result)
    }
}

/// Validate that a resolved/literal address matches an explicit family
/// preference. `Auto` accepts either family; the family always lives in the
/// error *message* (context), never in a distinct error type.
fn check_family(ip: IpAddr, family: PreferredFamily, target: &str) -> Result<(), TracerouteError> {
    match (family, ip) {
        (PreferredFamily::V4, IpAddr::V6(_)) => Err(TracerouteError::ResolutionError(format!(
            "{target} is an IPv6 address but IPv4 was requested (-4)"
        ))),
        (PreferredFamily::V6, IpAddr::V4(_)) => Err(TracerouteError::ResolutionError(format!(
            "{target} is an IPv4 address but IPv6 was requested (-6)"
        ))),
        _ => Ok(()),
    }
}

/// Resolve a traceroute target (IP literal or hostname) to a single address
/// of the preferred family.
///
/// - **IP literals** are used directly; an explicit `V4`/`V6` preference
///   that contradicts the literal's family is a
///   [`TracerouteError::ResolutionError`].
/// - **`localhost`** short-circuits to `127.0.0.1` (or `::1` under
///   [`PreferredFamily::V6`]) without touching DNS.
/// - **Hostnames** resolve per the preference. `V4` uses ftr's own DNS
///   resolver (A query) exactly as previous releases did. `V6` uses the
///   system resolver (`getaddrinfo` via tokio's `lookup_host`) and keeps
///   only IPv6 addresses; the system resolver is used deliberately so OS
///   facilities like macOS NAT64/DNS64 synthesis apply. `Auto` prefers
///   IPv4 and only falls back to IPv6 when no A records exist (see
///   [`PreferredFamily`] for why this conservative default was chosen).
///
/// Zone-scoped literals (`fe80::1%en0`) are rejected with a clear error
/// rather than silently stripping the zone: `IpAddr` cannot carry a scope
/// id, and tracerouting a link-local neighbor is a single-hop affair.
/// First-class scoped-target support needs an API extension (deferred).
pub async fn resolve_target_with_family(
    target: &str,
    family: PreferredFamily,
) -> Result<IpAddr, TracerouteError> {
    // Zone-scoped IPv6 literal: refuse loudly, never strip the zone.
    if let Some((addr_part, zone)) = target.split_once('%') {
        if addr_part.parse::<std::net::Ipv6Addr>().is_ok() {
            return Err(TracerouteError::ResolutionError(format!(
                "zone-scoped IPv6 targets ({addr_part}%{zone}) are not yet supported"
            )));
        }
    }

    // IP literal: use as-is (after family validation).
    if let Ok(ip) = target.parse::<IpAddr>() {
        check_family(ip, family, target)?;
        return Ok(ip);
    }

    // Well-known hostname, no DNS needed.
    if target == "localhost" {
        return Ok(match family {
            PreferredFamily::V6 => IpAddr::V6(std::net::Ipv6Addr::LOCALHOST),
            _ => IpAddr::V4(std::net::Ipv4Addr::LOCALHOST),
        });
    }

    match family {
        PreferredFamily::V4 => resolve_first_a(target).await,
        PreferredFamily::V6 => resolve_first_aaaa(target).await,
        _ => {
            // Auto: IPv4 first — identical mechanism and result to pre-0.9
            // releases for any host with A records — then IPv6.
            match resolve_first_a(target).await {
                Ok(ip) => Ok(ip),
                Err(v4_err) => match resolve_first_aaaa(target).await {
                    Ok(ip) => Ok(ip),
                    Err(_) => Err(v4_err),
                },
            }
        }
    }
}

/// Resolve a hostname's first A record via ftr's own DNS resolver — the
/// exact IPv4 mechanism used by previous releases.
async fn resolve_first_a(target: &str) -> Result<IpAddr, TracerouteError> {
    let addrs = resolver::resolve_a(target)
        .await
        .map_err(|e| TracerouteError::ResolutionError(format!("{target}: {e}")))?;
    addrs
        .first()
        .map(|a| IpAddr::V4(*a))
        .ok_or_else(|| TracerouteError::ResolutionError(format!("{target}: no IPv4 addresses")))
}

/// Resolve a hostname's first IPv6 address via the system resolver
/// (`getaddrinfo`), so OS-level behaviors (NAT64/DNS64 synthesis,
/// /etc/hosts entries) apply to IPv6 targets.
async fn resolve_first_aaaa(target: &str) -> Result<IpAddr, TracerouteError> {
    let addrs = tokio::net::lookup_host((target, 0))
        .await
        .map_err(|e| TracerouteError::ResolutionError(format!("{target}: {e}")))?;
    addrs
        .into_iter()
        .find(std::net::SocketAddr::is_ipv6)
        .map(|a| a.ip())
        .ok_or_else(|| {
            TracerouteError::ResolutionError(format!("{target}: no IPv6 (AAAA) addresses"))
        })
}

/// Run an async traceroute with the given configuration
pub async fn trace_async(target: &str) -> Result<TracerouteResult, TracerouteError> {
    // ConfigError converts into TracerouteError::ConfigError via #[from]
    let config = TracerouteConfig::builder().target(target).build()?;
    trace_with_config_async(config).await
}

/// Run an async traceroute with a custom configuration
pub async fn trace_with_config_async(
    config: TracerouteConfig,
) -> Result<TracerouteResult, TracerouteError> {
    let traceroute = Traceroute::new(config).await?;
    traceroute.run().await
}

/// Run an async traceroute with injected services (internal API for Ftr struct)
pub(crate) async fn trace_with_services(
    config: TracerouteConfig,
    services: &Services,
) -> Result<TracerouteResult, TracerouteError> {
    let traceroute = Traceroute::new_with_services(config, services.clone()).await?;
    traceroute.run().await
}

#[cfg(test)]
mod tests {
    use super::*;
    use std::time::Duration;

    #[tokio::test]
    async fn test_async_traceroute_creation() {
        let config = TracerouteConfig::builder()
            .target("127.0.0.1")
            .build()
            .expect("failed to build traceroute config");

        let result = Traceroute::new(config).await;
        assert!(result.is_ok());

        let traceroute = result.expect("Traceroute creation should succeed");
        assert_eq!(
            traceroute.target_ip,
            IpAddr::V4("127.0.0.1".parse().expect("valid IPv4 address"))
        );
    }

    #[tokio::test]
    async fn test_async_traceroute_ipv6_literal_resolves() {
        // IPv6 literals now resolve at creation time; platforms without
        // IPv6 probe support surface Ipv6NotSupported later, from the
        // socket factory, when the trace runs.
        let config = TracerouteConfig::builder()
            .target("::1")
            .target_ip(IpAddr::V6("::1".parse().expect("valid IPv6 address")))
            .build()
            .expect("failed to build traceroute config");

        let traceroute = Traceroute::new(config)
            .await
            .expect("IPv6 literal must resolve");
        assert_eq!(
            traceroute.target_ip,
            IpAddr::V6("::1".parse().expect("valid IPv6 address"))
        );
    }

    #[tokio::test]
    async fn test_resolve_ip_literals_by_family() {
        use crate::traceroute::config::PreferredFamily;

        // Literals pass through untouched under Auto and their own family.
        for family in [PreferredFamily::Auto, PreferredFamily::V4] {
            let ip = resolve_target_with_family("8.8.8.8", family)
                .await
                .expect("v4 literal resolves");
            assert_eq!(ip, IpAddr::V4("8.8.8.8".parse().expect("valid IPv4")));
        }
        for family in [PreferredFamily::Auto, PreferredFamily::V6] {
            let ip = resolve_target_with_family("2001:4860:4860::8888", family)
                .await
                .expect("v6 literal resolves");
            assert_eq!(
                ip,
                IpAddr::V6("2001:4860:4860::8888".parse().expect("valid IPv6"))
            );
        }

        // Family/literal contradictions are resolution errors whose message
        // carries the family context (no dedicated error type).
        let err = resolve_target_with_family("8.8.8.8", PreferredFamily::V6)
            .await
            .expect_err("v4 literal under -6 must fail");
        assert!(matches!(&err, TracerouteError::ResolutionError(m) if m.contains("IPv6")));

        let err = resolve_target_with_family("2001:4860:4860::8888", PreferredFamily::V4)
            .await
            .expect_err("v6 literal under -4 must fail");
        assert!(matches!(&err, TracerouteError::ResolutionError(m) if m.contains("IPv4")));
    }

    #[tokio::test]
    async fn test_resolve_localhost_by_family() {
        use crate::traceroute::config::PreferredFamily;

        assert_eq!(
            resolve_target_with_family("localhost", PreferredFamily::Auto)
                .await
                .expect("localhost resolves"),
            IpAddr::V4(std::net::Ipv4Addr::LOCALHOST)
        );
        assert_eq!(
            resolve_target_with_family("localhost", PreferredFamily::V6)
                .await
                .expect("localhost -6 resolves"),
            IpAddr::V6(std::net::Ipv6Addr::LOCALHOST)
        );
    }

    #[tokio::test]
    async fn test_resolve_rejects_zone_scoped_literal() {
        use crate::traceroute::config::PreferredFamily;

        // The zone must never be silently stripped; scoped targets are a
        // clear, typed error until the API can carry a scope id end-to-end.
        let err = resolve_target_with_family("fe80::1%en0", PreferredFamily::Auto)
            .await
            .expect_err("zone-scoped literal must be rejected");
        assert!(
            matches!(&err, TracerouteError::ResolutionError(m) if m.contains("zone-scoped")),
            "unexpected error: {err:?}"
        );
    }

    #[tokio::test]
    async fn test_async_traceroute_with_ip() {
        let config = TracerouteConfig::builder()
            .target("8.8.8.8")
            .target_ip(IpAddr::V4("8.8.8.8".parse().expect("valid IPv4 address")))
            .build()
            .expect("failed to build traceroute config");

        let result = Traceroute::new(config).await;
        assert!(result.is_ok());

        let traceroute = result.expect("Traceroute creation should succeed");
        assert_eq!(
            traceroute.target_ip,
            IpAddr::V4("8.8.8.8".parse().expect("valid IPv4 address"))
        );
    }

    #[tokio::test]
    async fn test_trace_async_localhost() {
        // This may fail due to permissions, but should at least parse correctly
        let result = trace_async("127.0.0.1").await;

        // Either succeeds, fails with permissions, or fails creating a socket
        assert!(
            matches!(
                &result,
                Ok(_)
                    | Err(TracerouteError::InsufficientPermissions { .. })
                    | Err(TracerouteError::SocketError(_))
            ),
            "Unexpected error: {:?}",
            result
        );
        if let Ok(trace_result) = result {
            assert_eq!(trace_result.target, "127.0.0.1");
        }
    }

    #[tokio::test]
    async fn test_trace_with_config_async() {
        let config = TracerouteConfig::builder()
            .target("127.0.0.1")
            .max_hops(3)
            .probe_timeout(Duration::from_millis(100))
            .build()
            .expect("failed to build traceroute config");

        let result = trace_with_config_async(config).await;

        // Either succeeds, fails with permissions, or fails creating a socket
        assert!(
            matches!(
                &result,
                Ok(_)
                    | Err(TracerouteError::InsufficientPermissions { .. })
                    | Err(TracerouteError::SocketError(_))
            ),
            "Unexpected error: {:?}",
            result
        );
        if let Ok(trace_result) = result {
            assert_eq!(trace_result.target, "127.0.0.1");
            assert!(trace_result.hops.len() <= 3);
        }
    }

    #[tokio::test]
    async fn test_async_traceroute_hostname_resolution() {
        let config = TracerouteConfig::builder()
            .target("localhost")
            .build()
            .expect("failed to build traceroute config");

        let result = Traceroute::new(config).await;
        assert!(result.is_ok());

        let traceroute = result.expect("Traceroute creation should succeed");
        // localhost should resolve to 127.0.0.1
        assert_eq!(
            traceroute.target_ip,
            IpAddr::V4("127.0.0.1".parse().expect("valid IPv4 address"))
        );
    }

    #[tokio::test]
    async fn test_async_traceroute_invalid_hostname() {
        let config = TracerouteConfig::builder()
            .target("this.hostname.definitely.does.not.exist.invalid")
            .build()
            .expect("failed to build traceroute config");

        let result = Traceroute::new(config).await;
        assert!(
            matches!(&result, Err(TracerouteError::ResolutionError(_))),
            "Expected resolution error, got: {:?}",
            result
        );
    }

    #[tokio::test]
    async fn test_verbose_config_does_not_touch_environment() {
        let config = TracerouteConfig::builder()
            .target("127.0.0.1")
            .verbose(2)
            .max_hops(1) // Limit hops to make test faster
            .probe_timeout(Duration::from_millis(100))
            .build()
            .expect("failed to build traceroute config");
        assert_eq!(config.verbose, 2);

        let traceroute = Traceroute::new(config)
            .await
            .expect("Traceroute creation for localhost should succeed");

        // Verbosity is threaded through explicitly; running a trace must not
        // mutate process-global environment state (which would race between
        // concurrent traces)
        let before = std::env::var("FTR_VERBOSE").ok();
        let _ = tokio::time::timeout(Duration::from_secs(5), traceroute.run()).await;
        assert_eq!(std::env::var("FTR_VERBOSE").ok(), before);
    }
}