Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add new NetIf variant to NatResolver for IP resolution via network interface #10922

Open
wants to merge 7 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions crates/net/nat/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ serde_with = { workspace = true, optional = true }
thiserror.workspace = true
tokio = { workspace = true, features = ["time"] }
if-addrs.workspace = true
tracing.workspace = true

[dev-dependencies]
reth-tracing.workspace = true
Expand Down
13 changes: 13 additions & 0 deletions crates/net/nat/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,9 @@ use std::{
task::{Context, Poll},
time::Duration,
};
use tracing::error;

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

Expand All @@ -48,6 +50,9 @@ pub enum NatResolver {
PublicIp,
/// Use the given [`IpAddr`]
ExternalIp(IpAddr),
/// Resolve external IP via the network interface.
#[cfg(not(target_os = "windows"))]
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd like to not feature gate this variant

Copy link
Member

@emhane emhane Sep 18, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

it only works on non-windows

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

garwahl marked this conversation as resolved.
Show resolved Hide resolved
NetIf,
/// Resolve nothing
None,
}
Expand All @@ -66,6 +71,7 @@ impl fmt::Display for NatResolver {
Self::Upnp => f.write_str("upnp"),
Self::PublicIp => f.write_str("publicip"),
Self::ExternalIp(ip) => write!(f, "extip:{ip}"),
Self::NetIf => f.write_str("networkinterface"),
garwahl marked this conversation as resolved.
Show resolved Hide resolved
Self::None => f.write_str("none"),
}
}
Expand Down Expand Up @@ -185,6 +191,13 @@ pub async fn external_addr_with(resolver: NatResolver) -> Option<IpAddr> {
match resolver {
NatResolver::Any | NatResolver::Upnp | NatResolver::PublicIp => resolve_external_ip().await,
NatResolver::ExternalIp(ip) => Some(ip),
NatResolver::NetIf => match resolve_net_if_ip(DEFAULT_NET_IF_NAME) {
Ok(ip) => Some(ip),
Err(err) => {
error!("Failed to resolve network interface IP: {}", err);
None
Comment on lines +197 to +198
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@emhane Alternatively we could panic!(....) instead here if I'm understanding #10922 (comment) correctly. Have decided to just log an error here but LMKWYT

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this should just return None if this fails

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes this is the behavior that I had originally #10922 (comment)

@mattsse @emhane Do we want to log an error if this fails?

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do we want to log an error if this fails?

doesn't hurt but not strictly required

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Right so does the above current diff SGTY? We log an error and return None on failure. Just checking your comment here #10922 (comment) wasn't referring to something else

}
},
NatResolver::None => None,
}
}
Expand Down
Loading