Skip to content

Commit

Permalink
Select right IP address based on Java preferences. (akka#22336)
Browse files Browse the repository at this point in the history
* Change priority of DNS resolved addresses based on java.net.preferIPv6Addresses property. akka#22330

* Include test specs for IPv6 resolver selection. akka#22330

* Use Java system properties to retrieve the IPv6 preference. akka#22330

(cherry picked from commit 6406c0e)
  • Loading branch information
jpuerto authored and patriknw committed Feb 23, 2017
1 parent 0a47610 commit 8a4d976
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 1 deletion.
38 changes: 38 additions & 0 deletions akka-actor-tests/src/test/scala/akka/io/DnsSpec.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
package akka.io

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

import scala.collection.immutable.Seq
import org.scalatest.{ BeforeAndAfterAll, Matchers, WordSpec }

class DnsSpec extends WordSpec with BeforeAndAfterAll with Matchers {

val ip4Address = InetAddress.getByAddress("localhost", Array[Byte](127, 0, 0, 1)) match {
case address: Inet4Address address
}
val ipv6Address = InetAddress.getByAddress("localhost", Array[Byte](0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1)) match {
case address: Inet6Address address
}

var temporaryValue: Option[String] = None

override def beforeAll() = {
temporaryValue = sys.props.get("java.net.preferIPv6Addresses")
}

override def afterAll() = temporaryValue match {
case Some(value) sys.props.put("java.net.preferIPv6Addresses", value)
case _ sys.props.remove("java.net.preferIPv6Addresses")
}

"Dns" should {
"resolve to a IPv6 address if it is the preferred network stack" in {
sys.props.put("java.net.preferIPv6Addresses", true.toString)
Dns.Resolved("test", Seq(ip4Address), Seq(ipv6Address)).addr should ===(ipv6Address)
}
"resolve to a IPv4 address if IPv6 is not the preferred network stack" in {
sys.props.remove("java.net.preferIPv6Addresses")
Dns.Resolved("test", Seq(ip4Address), Seq(ipv6Address)).addr should ===(ip4Address)
}
}
}
10 changes: 9 additions & 1 deletion akka-actor/src/main/scala/akka/io/Dns.scala
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ object Dns extends ExtensionId[DnsExt] with ExtensionIdProvider {
}

case class Resolved(name: String, ipv4: immutable.Seq[Inet4Address], ipv6: immutable.Seq[Inet6Address]) extends Command {
val addrOption: Option[InetAddress] = ipv4.headOption orElse ipv6.headOption
val addrOption: Option[InetAddress] = IpVersionSelector.getInetAddress(ipv4.headOption, ipv6.headOption)

@throws[UnknownHostException]
def addr: InetAddress = addrOption match {
Expand Down Expand Up @@ -89,3 +89,11 @@ class DnsExt(system: ExtendedActorSystem) extends IO.Extension {

def getResolver: ActorRef = manager
}

object IpVersionSelector {
def getInetAddress(ipv4: Option[Inet4Address], ipv6: Option[Inet6Address]): Option[InetAddress] =
System.getProperty("java.net.preferIPv6Addresses") match {
case "true" ipv6 orElse ipv4
case _ ipv4 orElse ipv6
}
}

0 comments on commit 8a4d976

Please sign in to comment.