-
Notifications
You must be signed in to change notification settings - Fork 1.1k
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
base: main
Are you sure you want to change the base?
Changes from 5 commits
43343a6
af17461
2b2acc0
7035ef0
422849c
81da7df
7896295
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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}; | ||
|
||
|
@@ -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"))] | ||
garwahl marked this conversation as resolved.
Show resolved
Hide resolved
|
||
NetIf, | ||
/// Resolve nothing | ||
None, | ||
} | ||
|
@@ -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"), | ||
} | ||
} | ||
|
@@ -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
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @emhane Alternatively we could There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. this should just return None if this fails There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yes this is the behavior that I had originally #10922 (comment) There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
doesn't hurt but not strictly required There was a problem hiding this comment. Choose a reason for hiding this commentThe 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, | ||
} | ||
} | ||
|
There was a problem hiding this comment.
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
There was a problem hiding this comment.
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
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
https://docs.rs/if-addrs/0.13.3/if_addrs/fn.get_if_addrs.html
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
https://docs.rs/if-addrs/0.13.3/x86_64-pc-windows-msvc/if_addrs/fn.get_if_addrs.html
this also exists for windows,