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
Changes from 2 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
6 changes: 6 additions & 0 deletions crates/net/nat/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ use std::{
time::Duration,
};

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

Expand All @@ -48,6 +49,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 +70,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 +190,7 @@ 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 => resolve_net_if_ip(DEFAULT_NET_IF_NAME).ok(),
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 I've decided to return None here if resolution fails given IP resolution here is touted as best effort similarly to how both resolve_external_ip() above behaves in that there is no Err if that fails.

Don't think we should be changing the signature of this method to Result<Option<IpAddr>, NetInterfaceError> since that would leak resolver specific impl detail to consumers. LMKWYT

Copy link
Member

Choose a reason for hiding this comment

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

defo think sig should change. becomes hard to use this tool otherwise if error isn't printed to log. the error could be logged here too, but it's wrong to use thereth::cli target here, and users should be able to read the error without enabling target reth::net. @shekhirin will error log level be written to stdout regardless of target in default node logging setting?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yeah think I'd much rather log an error here or lower, than force consumers of external_addr_with to handle a NetInterfaceError even if their NatResolver impl is something unrelated WDYT

Similar reasoning to how resolve_external_ip() discards the error, would be nice to have it log an error instead

Copy link
Member

Choose a reason for hiding this comment

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

ye, would like it to panic already in cli as well if the error is logged, so if none is returned when the net if ip resolution flag is passed

please open an issue for resolve_external_ip() not logging error

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