Skip to content

Commit

Permalink
Improve dnsjava algorithm
Browse files Browse the repository at this point in the history
  • Loading branch information
slandelle committed Apr 17, 2015
1 parent c0dc2e6 commit 464c2a3
Show file tree
Hide file tree
Showing 2 changed files with 62 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -29,12 +29,11 @@ import io.gatling.http.ahc.ChannelPoolPartitioning
import io.gatling.http.config.HttpProtocol
import io.gatling.http.cookie.CookieSupport
import io.gatling.http.referer.RefererHandling
import io.gatling.http.util.HttpHelper
import io.gatling.http.util.{ DnsHelper, HttpHelper }

import scala.util.control.NonFatal

import com.typesafe.scalalogging.LazyLogging
import org.xbill.DNS.Address

object RequestExpressionBuilder {
val BuildRequestErrorMapper = "Failed to build request: " + _
Expand Down Expand Up @@ -71,7 +70,7 @@ abstract class RequestExpressionBuilder(commonAttributes: CommonAttributes, prot
case Some(address) => address
case None =>
try {
Address.getByName(name)
DnsHelper.getAddressByName(name)
} catch {
case NonFatal(e) =>
logger.warn(s"Failed to resolve address of name $name")
Expand Down
60 changes: 60 additions & 0 deletions gatling-http/src/main/scala/io/gatling/http/util/DnsHelper.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
/**
* Copyright 2011-2015 eBusiness Information, Groupe Excilys (www.ebusinessinformation.fr)
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package io.gatling.http.util

import java.net.{ UnknownHostException, InetAddress }

import org.xbill.DNS.Address._
import org.xbill.DNS._

object DnsHelper {

@throws(classOf[UnknownHostException])
def getAddressByName(name: String): InetAddress =
toByteArray(name, IPv4) match {
case null =>
toByteArray(name, IPv6) match {
case null =>
val address = lookupHostNameAddress(name)
InetAddress.getByAddress(name, address.getAddress)

case ipV6Bytes => InetAddress.getByAddress(name, ipV6Bytes)
}

case ipV4Bytes => InetAddress.getByAddress(name, ipV4Bytes)
}

@throws(classOf[UnknownHostException])
private def lookupHostNameAddress(name: String): InetAddress =
try {
val lookup: Lookup = new Lookup(name, Type.A)
lookup.run match {
case null =>
if (lookup.getResult == Lookup.TYPE_NOT_FOUND) {
new Lookup(name, Type.AAAA).run match {
case null => throw new UnknownHostException("unknown host")
case aaaa => aaaa(0).asInstanceOf[AAAARecord].getAddress
}
} else {
throw new UnknownHostException("unknown host")
}

case a => a(0).asInstanceOf[ARecord].getAddress
}
} catch {
case e: TextParseException => throw new UnknownHostException("invalid name")
}
}

0 comments on commit 464c2a3

Please sign in to comment.