Skip to content

Commit

Permalink
Support IPv6 in async-dns (#25687)
Browse files Browse the repository at this point in the history
* Support IPv6 in async-dns

* Expect AAAA records for IPv6
  • Loading branch information
raboof authored and chbatey committed Sep 28, 2018
1 parent 6464dea commit 9a54ae9
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 9 deletions.
Expand Up @@ -4,9 +4,12 @@

package akka.io.dns.internal

import java.net.InetAddress
import java.net.{ Inet6Address, InetAddress }

import scala.collection.immutable.Seq
import akka.io.Dns
import akka.io.dns.{ AAAARecord, ResourceRecord }
import akka.io.dns.DnsProtocol.{ Resolve, Resolved }
import akka.testkit.{ AkkaSpec, ImplicitSender }

class AsyncDnsManagerSpec extends AkkaSpec(
Expand All @@ -24,6 +27,18 @@ class AsyncDnsManagerSpec extends AkkaSpec(
val oldProtocolReply = akka.io.Dns.Resolved("127.0.0.1", InetAddress.getByName("127.0.0.1") :: Nil)
expectMsg(oldProtocolReply)
}

"support ipv6" in {
dns ! Resolve("::1") // ::1 will short circuit the resolution
val Resolved("::1", Seq(AAAARecord("::1", Int.MaxValue, _)), Nil) = expectMsgType[Resolved]
}

"support ipv6 also using the old protocol" in {
dns ! akka.io.Dns.Resolve("::1") // ::1 will short circuit the resolution
val resolved = expectMsgType[akka.io.Dns.Resolved]
resolved.ipv4 should be(Nil)
resolved.ipv6.length should be(1)
}
}

}
Expand Up @@ -114,9 +114,7 @@ class AsyncDnsResolverSpec extends AkkaSpec(
r ! Resolve(name)
dnsClient1.expectNoMessage(50.millis)
val answer = expectMsgType[Resolved]
answer.records.collect { case r: ARecord r }.toSet shouldEqual Set(
ARecord("1:2:3:0:0:0:0:0", Int.MaxValue, InetAddress.getByName("1:2:3:0:0:0:0:0"))
)
val Seq(AAAARecord("1:2:3:0:0:0:0:0", Int.MaxValue, _)) = answer.records.collect { case r: AAAARecord r }
}

"return additional records for SRV requests" in {
Expand Down
Expand Up @@ -10,7 +10,7 @@ import java.util.concurrent.TimeUnit
import akka.actor.{ Actor, ActorLogging, ActorRefFactory, Deploy, Props, Timers }
import akka.annotation.InternalApi
import akka.dispatch.{ RequiresMessageQueue, UnboundedMessageQueueSemantics }
import akka.io.dns.{ ARecord, DnsProtocol, DnsSettings }
import akka.io.dns.{ AAAARecord, ARecord, DnsProtocol, DnsSettings }
import akka.io.dns.internal.AsyncDnsManager.CacheCleanup
import akka.io.{ Dns, DnsExt, PeriodicCacheCleanup }
import akka.routing.FromConfig
Expand Down Expand Up @@ -74,7 +74,10 @@ private[io] final class AsyncDnsManager(val ext: DnsExt) extends Actor
val adapted = DnsProtocol.Resolve(name)
val reply = (resolver ? adapted).mapTo[DnsProtocol.Resolved]
.map { asyncResolved
val ips = asyncResolved.records.collect { case a: ARecord a.ip }
val ips = asyncResolved.records.collect {
case a: ARecord a.ip
case a: AAAARecord a.ip
}
Dns.Resolved(asyncResolved.name, ips)
}
reply pipeTo sender
Expand Down
Expand Up @@ -4,14 +4,14 @@

package akka.io.dns.internal

import java.net.{ InetAddress, InetSocketAddress }
import java.net.{ Inet4Address, Inet6Address, InetAddress, InetSocketAddress }
import java.nio.charset.StandardCharsets

import akka.actor.{ Actor, ActorLogging, ActorRef, ActorRefFactory, Props }
import akka.annotation.InternalApi
import akka.io.dns.DnsProtocol.{ Ip, RequestType, Srv }
import akka.io.dns.internal.DnsClient._
import akka.io.dns.{ ARecord, DnsProtocol, DnsSettings, ResourceRecord }
import akka.io.dns.{ AAAARecord, ARecord, DnsProtocol, DnsSettings, ResourceRecord }
import akka.pattern.{ ask, pipe }
import akka.util.{ Helpers, Timeout }

Expand Down Expand Up @@ -59,7 +59,10 @@ private[io] final class AsyncDnsResolver(
Future.fromTry {
Try {
val address = InetAddress.getByName(name) // only checks validity, since known to be IP address
val record = ARecord(name, Int.MaxValue, address)
val record = address match {
case _: Inet4Address ARecord(name, Int.MaxValue, address)
case ipv6address: Inet6Address AAAARecord(name, Int.MaxValue, ipv6address)
}
DnsProtocol.Resolved(name, record :: Nil)
}
}
Expand Down

0 comments on commit 9a54ae9

Please sign in to comment.