Skip to content

Commit

Permalink
feat: default resolve both IPv4 and IPv6 addresses
Browse files Browse the repository at this point in the history
This is useful when target host only has a IPv6 address and the user
has not forced particular address family.

Fixes: fujiapple852#864
  • Loading branch information
artizirk committed Dec 15, 2023
1 parent ec1f888 commit 76b2496
Show file tree
Hide file tree
Showing 5 changed files with 29 additions and 1 deletion.
7 changes: 7 additions & 0 deletions src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,8 @@ pub enum AddressFamily {
Ipv4,
/// Internet Protocol V6
Ipv6,
/// Both Internet Protocol V4 or V6
Any,
}

/// The strategy Equal-cost Multi-Path routing strategy.
Expand Down Expand Up @@ -496,6 +498,7 @@ impl TrippyConfig {
.transpose()?;
let addr_family = match (args.ipv4, args.ipv6, cfg_file_strategy.addr_family) {
(false, false, None) => addr_family(constants::DEFAULT_ADDRESS_FAMILY),
(false, false, Some(AddressFamily::Any)) => addr_family(constants::DEFAULT_ADDRESS_FAMILY),
(false, false, Some(AddressFamily::Ipv4)) | (true, _, _) => TracerAddrFamily::Ipv4,
(false, false, Some(AddressFamily::Ipv6)) | (_, true, _) => TracerAddrFamily::Ipv6,
};
Expand All @@ -508,6 +511,9 @@ impl TrippyConfig {
(MultipathStrategyConfig::Dublin, TracerAddrFamily::Ipv6) => Err(anyhow!(
"Dublin multipath strategy not implemented for IPv6 yet!"
)),
(MultipathStrategyConfig::Dublin, TracerAddrFamily::Any) => Err(anyhow!(
"Dublin multipath strategy not implemented for IPv6 yet! You need to force IPv4."
)),
}?;
let port_direction = match (protocol, source_port, target_port, multipath_strategy_cfg) {
(TracerProtocol::Icmp, _, _, _) => PortDirection::None,
Expand Down Expand Up @@ -722,6 +728,7 @@ fn addr_family(addr_family: AddressFamily) -> TracerAddrFamily {
match addr_family {
AddressFamily::Ipv4 => TracerAddrFamily::Ipv4,
AddressFamily::Ipv6 => TracerAddrFamily::Ipv6,
AddressFamily::Any => TracerAddrFamily::Any,
}
}

Expand Down
2 changes: 1 addition & 1 deletion src/config/constants.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ pub const DEFAULT_LOG_FILTER: &str = "trippy=debug";
pub const DEFAULT_STRATEGY_PROTOCOL: Protocol = Protocol::Icmp;

/// The default value for `addr-family`.
pub const DEFAULT_ADDRESS_FAMILY: AddressFamily = AddressFamily::Ipv4;
pub const DEFAULT_ADDRESS_FAMILY: AddressFamily = AddressFamily::Any;

/// The default value for `min-round-duration`.
pub const DEFAULT_STRATEGY_MIN_ROUND_DURATION: &str = "1s";
Expand Down
15 changes: 15 additions & 0 deletions src/dns/lazy_resolver.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,8 @@ pub enum IpAddrFamily {
Ipv4,
/// Internet Protocol v6.
Ipv6,
/// Both Internet Protocol V4 or V6
Any,
}

impl Config {
Expand All @@ -56,6 +58,16 @@ impl Config {
timeout,
}
}

/// Create an Any IP version `Config`
#[must_use]
pub fn new_any(resolve_method: ResolveMethod, timeout: Duration) -> Self {
Self {
resolve_method,
addr_family: IpAddrFamily::Any,
timeout,
}
}
}

/// A cheaply cloneable, non-blocking, caching, forward and reverse DNS resolver.
Expand Down Expand Up @@ -170,6 +182,7 @@ mod inner {
options.ip_strategy = match config.addr_family {
IpAddrFamily::Ipv4 => LookupIpStrategy::Ipv4Only,
IpAddrFamily::Ipv6 => LookupIpStrategy::Ipv6Only,
IpAddrFamily::Any => LookupIpStrategy::default(),
};
let res = match config.resolve_method {
ResolveMethod::Resolv => Resolver::from_system_conf(),
Expand Down Expand Up @@ -216,6 +229,8 @@ mod inner {
(self.config.addr_family, addr),
(IpAddrFamily::Ipv4, IpAddr::V4(_))
| (IpAddrFamily::Ipv6, IpAddr::V6(_))
| (IpAddrFamily::Any, IpAddr::V4(_))
| (IpAddrFamily::Any, IpAddr::V6(_))
)
})
.collect::<Vec<_>>()),
Expand Down
3 changes: 3 additions & 0 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,9 @@ fn start_dns_resolver(cfg: &TrippyConfig) -> anyhow::Result<DnsResolver> {
TracerAddrFamily::Ipv6 => {
DnsResolver::start(Config::new_ipv6(cfg.dns_resolve_method, cfg.dns_timeout))?
}
TracerAddrFamily::Any => {
DnsResolver::start(Config::new_any(cfg.dns_resolve_method, cfg.dns_timeout))?
}
})
}

Expand Down
3 changes: 3 additions & 0 deletions src/tracing/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,13 +33,16 @@ pub enum TracerAddrFamily {
Ipv4,
/// Internet Protocol V6
Ipv6,
/// Both Internet Protocol V4 or V6
Any
}

impl Display for TracerAddrFamily {
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
match self {
Self::Ipv4 => write!(f, "v4"),
Self::Ipv6 => write!(f, "v6"),
Self::Any => write!(f, "v4v6"),
}
}
}
Expand Down

0 comments on commit 76b2496

Please sign in to comment.