Skip to content

Instantly share code, notes, and snippets.

@dacr
Last active April 2, 2023 10:10
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dacr/80d8ac3abdc52c017a9aa65cfa176acd to your computer and use it in GitHub Desktop.
Save dacr/80d8ac3abdc52c017a9aa65cfa176acd to your computer and use it in GitHub Desktop.
playing with dnsjava api / published by https://github.com/dacr/code-examples-manager #e298e2b1-c0a7-4d21-87dc-db6409929c1e/d52db5438445c5d2417d584031e21fa861b38981
// summary : playing with dnsjava api
// keywords : scala, dns, dnsjava, resolution, @testable
// publish : gist
// authors : David Crosson
// license : Apache NON-AI License Version 2.0 (https://raw.githubusercontent.com/non-ai-licenses/non-ai-licenses/main/NON-AI-APACHE2)
// id : e298e2b1-c0a7-4d21-87dc-db6409929c1e
// created-on : 2020-07-01T05:11:43Z
// managed-by : https://github.com/dacr/code-examples-manager
// run-with : scala-cli $file
// ---------------------
//> using scala "3.1.1"
//> using dep "org.scalatest::scalatest:3.2.10"
//> using dep "dnsjava:dnsjava:3.4.3"
//> using dep "org.slf4j:slf4j-nop:1.7.32"
// ---------------------
import org.scalatest.*, flatspec.*, matchers.*
import org.xbill.DNS.*
import java.net.{InetAddress, Inet4Address, Inet6Address}
import java.time.Duration
def getAllByName(hostname:String):List[InetAddress] = {
org.xbill.DNS.Address.getAllByName(hostname).toList
}
def host(hostname:String):List[String] = {
getAllByName(hostname).map(_.getHostAddress)
}
def host4(hostname:String):List[String] = {
getAllByName(hostname).collect {case ip:Inet4Address => ip.getHostAddress}
}
def host6(hostname:String):List[String] = {
getAllByName(hostname).collect {case ip:Inet6Address => ip.getHostAddress}
}
object UnitTests extends AnyFlatSpec with should.Matchers {
override def suiteName: String = "UnitTests"
"host functions using dnsjava" should "resolve localhost address" in {
host("www.mapland.fr") should contain only("176.9.65.81", "2a01:4f8:150:834b:0:0:0:2")
}
it should "resolve remote host name" in {
host("www.mapland.fr") should contain only("176.9.65.81", "2a01:4f8:150:834b:0:0:0:2")
host4("www.mapland.fr") should contain only("176.9.65.81")
host6("www.mapland.fr") should contain only("2a01:4f8:150:834b:0:0:0:2")
}
"dnsjava" should "resolve all associated addresses" in {
val resolver = new SimpleResolver("8.8.8.8")
resolver.setTimeout(Duration.ofSeconds(5))
val lookup = new Lookup("www.mapland.fr", Type.ANY)
lookup.setResolver(resolver)
val records = Option(lookup.run()).getOrElse(Array.empty[Record])
val results = records.collect {
case r:ARecord => r.getAddress.getHostAddress
case r:AAAARecord => r.getAddress.getHostAddress
}
results should contain only("176.9.65.81", "2a01:4f8:150:834b:0:0:0:2")
}
}
UnitTests.execute()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment