Skip to content

Commit

Permalink
fix: IPv6 DNS
Browse files Browse the repository at this point in the history
  • Loading branch information
XOR-op committed Oct 22, 2023
1 parent 17f3ca5 commit 554b0fe
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 25 deletions.
65 changes: 42 additions & 23 deletions boltconn/src/network/dns/dns.rs
Original file line number Diff line number Diff line change
Expand Up @@ -91,40 +91,59 @@ impl<P: RuntimeProvider> GenericDns<P> {
})
}

pub async fn genuine_lookup(&self, domain_name: &str) -> Option<IpAddr> {
let mut low_priority_result = None;
async fn genuine_lookup_v4(&self, domain_name: &str) -> Option<IpAddr> {
for r in self.resolvers.load().iter() {
if let Ok(r) =
tokio::time::timeout(Duration::from_secs(5), r.ipv4_lookup(domain_name)).await
{
if let Ok(result) = r {
if let Some(i) = result.iter().next() {
return Some(i.0.into());
}
}
} else {
tracing::trace!("DNS lookup for {domain_name} timeout");
}
}
None
}

async fn genuine_lookup_v6(&self, domain_name: &str) -> Option<IpAddr> {
for r in self.resolvers.load().iter() {
if let Ok(r) =
tokio::time::timeout(Duration::from_secs(5), r.lookup_ip(domain_name)).await
tokio::time::timeout(Duration::from_secs(5), r.ipv6_lookup(domain_name)).await
{
if let Ok(result) = r {
if let Some(i) = result.iter().next() {
match (self.preference, i) {
(DnsPreference::Ipv4Only, IpAddr::V4(addr))
| (DnsPreference::PreferIpv4, IpAddr::V4(addr)) => {
return Some(addr.into())
}
(DnsPreference::Ipv6Only, IpAddr::V6(addr))
| (DnsPreference::PreferIpv6, IpAddr::V6(addr)) => {
return Some(addr.into())
}
(DnsPreference::PreferIpv4, IpAddr::V6(addr)) => {
low_priority_result = Some(addr.into());
}
(DnsPreference::PreferIpv6, IpAddr::V4(addr)) => {
low_priority_result = Some(addr.into());
}
_ => {
//pass
}
}
return Some(i.0.into());
}
}
} else {
tracing::trace!("DNS lookup for {domain_name} timeout");
}
}
low_priority_result
None
}

pub async fn genuine_lookup(&self, domain_name: &str) -> Option<IpAddr> {
match self.preference {
DnsPreference::Ipv4Only => self.genuine_lookup_v4(domain_name).await,
DnsPreference::Ipv6Only => self.genuine_lookup_v6(domain_name).await,
DnsPreference::PreferIpv4 => {
if let Some(a) = self.genuine_lookup_v4(domain_name).await {
Some(a)
} else {
self.genuine_lookup_v6(domain_name).await
}
}
DnsPreference::PreferIpv6 => {
if let Some(a) = self.genuine_lookup_v6(domain_name).await {
Some(a)
} else {
self.genuine_lookup_v4(domain_name).await
}
}
}
}

/// If no corresponding record, return fake ip itself.
Expand Down
5 changes: 3 additions & 2 deletions boltconn/src/transport/smol.rs
Original file line number Diff line number Diff line change
Expand Up @@ -286,7 +286,7 @@ impl SmolStack {
&mut self.iface,
&mut self.socket_set,
self.ip_addr
.matched_if_addr(local_addr.ip())
.matched_if_addr(remote_addr.ip())
.ok_or::<io::Error>(ErrorKind::AddrNotAvailable.into())?,
port,
remote_addr,
Expand All @@ -305,7 +305,7 @@ impl SmolStack {
&mut self.iface,
&mut self.socket_set,
self.ip_addr
.matched_if_addr(local_addr.ip())
.matched_if_addr(remote_addr.ip())
.ok_or::<io::Error>(ErrorKind::AddrNotAvailable.into())?,
local_addr.port(),
remote_addr,
Expand Down Expand Up @@ -350,6 +350,7 @@ impl SmolStack {
abort_handle: ConnAbortHandle,
notify: Arc<Notify>,
) -> io::Result<()> {
// todo: IPv6 support when local_addr is a V4 address
if local_addr.port() == 0 {
for _ in 0..10 {
let port = rand::thread_rng().gen_range(32768..65534);
Expand Down

0 comments on commit 554b0fe

Please sign in to comment.