whatvn / dns4s

Scala DNS implementation with Akka and Netty extension

Home Page:https://mkroli.github.io/dns4s

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

dns4s

Build Status Coverage Status

dns4s is an implementation of the DNS protocol in Scala. It consists of the following parts:

  • Core
  • Akka IO Extension
  • Netty Codec

Core

The core part contains the functionality used for en-/decoding of DNS messages as well as an inner-DSL to con-/destruct DNS messages.

For a detailed explanation of the DSL have a look at its documentation.

Akka IO Extension

The akka part contains an akka-io extension.

Usage

If you're using sbt just add the following to your build definition:

resolvers += "bintray" at "http://jcenter.bintray.com"

libraryDependencies ++= Seq(
  "com.typesafe.akka" %% "akka-actor" % "2.4.12",
  "com.github.mkroli" %% "dns4s-akka" % "0.10")

Imports

Use the following imports to get started:

import akka.actor._
import akka.io.IO
import com.github.mkroli.dns4s.dsl._
import com.github.mkroli.dns4s.akka._
import scala.concurrent.duration._

Server

The following is an excerpt from examples/simple/../DnsServer.scala:

class DnsHandlerActor extends Actor {
  override def receive = {
    case Query(q) ~ Questions(QName(host) ~ TypeA() :: Nil) =>
      sender ! Response(q) ~ Answers(RRName(host) ~ ARecord("1.2.3.4"))
  }
}

object DnsServer extends App {
  implicit val system = ActorSystem("DnsServer")
  implicit val timeout = Timeout(5 seconds)
  IO(Dns) ? Dns.Bind(system.actorOf(Props[DnsHandlerActor]), 5354)
}

Client

The following is an excerpt from examples/simple-client/../DnsClient.scala:

implicit val system = ActorSystem("DnsServer")
implicit val timeout = Timeout(5 seconds)
import system.dispatcher

IO(Dns) ? Dns.DnsPacket(Query ~ Questions(QName("google.de")), new InetSocketAddress("8.8.8.8", 53)) onSuccess {
  case Response(Answers(answers)) =>
    answers.collect {
      case ARecord(arecord) => println(arecord.address.getHostAddress)
    }
}

Netty Codec

The netty part contains the MessageToMessageCodec DnsCodec. See the examples simple-netty and simple-netty-client for more information.

About

Scala DNS implementation with Akka and Netty extension

https://mkroli.github.io/dns4s

License:Apache License 2.0


Languages

Language:Scala 100.0%