Skip to content

Commit 6975177

Browse files
garwahlgarwahemhanemattsse
authored
Add new NetIf variant to NatResolver for IP resolution via network interface (paradigmxyz#10922)
Co-authored-by: garwah <garwah@garwah> Co-authored-by: Emilia Hane <[email protected]> Co-authored-by: Matthias Seitz <[email protected]>
1 parent 95d65dc commit 6975177

File tree

3 files changed

+15
-0
lines changed

3 files changed

+15
-0
lines changed

Cargo.lock

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

crates/net/nat/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ serde_with = { workspace = true, optional = true }
1818
thiserror.workspace = true
1919
tokio = { workspace = true, features = ["time"] }
2020
if-addrs.workspace = true
21+
tracing.workspace = true
2122

2223
[dev-dependencies]
2324
reth-tracing.workspace = true

crates/net/nat/src/lib.rs

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,9 @@ use std::{
2525
task::{Context, Poll},
2626
time::Duration,
2727
};
28+
use tracing::error;
2829

30+
use crate::net_if::resolve_net_if_ip;
2931
#[cfg(feature = "serde")]
3032
use serde_with::{DeserializeFromStr, SerializeDisplay};
3133

@@ -48,6 +50,8 @@ pub enum NatResolver {
4850
PublicIp,
4951
/// Use the given [`IpAddr`]
5052
ExternalIp(IpAddr),
53+
/// Resolve external IP via the network interface.
54+
NetIf,
5155
/// Resolve nothing
5256
None,
5357
}
@@ -66,6 +70,7 @@ impl fmt::Display for NatResolver {
6670
Self::Upnp => f.write_str("upnp"),
6771
Self::PublicIp => f.write_str("publicip"),
6872
Self::ExternalIp(ip) => write!(f, "extip:{ip}"),
73+
Self::NetIf => f.write_str("netif"),
6974
Self::None => f.write_str("none"),
7075
}
7176
}
@@ -91,6 +96,7 @@ impl FromStr for NatResolver {
9196
"upnp" => Self::Upnp,
9297
"none" => Self::None,
9398
"publicip" | "public-ip" => Self::PublicIp,
99+
"netif" => Self::NetIf,
94100
s => {
95101
let Some(ip) = s.strip_prefix("extip:") else {
96102
return Err(ParseNatResolverError::UnknownVariant(format!(
@@ -185,6 +191,13 @@ pub async fn external_addr_with(resolver: NatResolver) -> Option<IpAddr> {
185191
match resolver {
186192
NatResolver::Any | NatResolver::Upnp | NatResolver::PublicIp => resolve_external_ip().await,
187193
NatResolver::ExternalIp(ip) => Some(ip),
194+
NatResolver::NetIf => match resolve_net_if_ip(DEFAULT_NET_IF_NAME) {
195+
Ok(ip) => Some(ip),
196+
Err(err) => {
197+
error!("Failed to resolve network interface IP: {}", err);
198+
None
199+
}
200+
},
188201
NatResolver::None => None,
189202
}
190203
}

0 commit comments

Comments
 (0)