Skip to content
DNS library for Elixir
Branch: master
Clone or download
Latest commit 46f4fe8 May 19, 2019
Type Name Latest commit message Commit time
Failed to load latest commit information.
config extract server behavior Jan 31, 2016
example update sample server and readme, fix #27 May 2, 2019
lib Close the UDP socket after use May 17, 2019
test switch off failing tests Apr 14, 2018
.formatter.exs add behaviour Apr 14, 2018
.travis.yml switch to Travis Sep 8, 2018 add LICENSE Jan 30, 2016 update sample server and readme, fix #27 May 2, 2019
mix.exs version bump Sep 13, 2018
mix.lock add behaviour Apr 14, 2018

DNS Build Status

DNS library for Elixir. Currently, the package provides:

  • Elixir structs to interface with inet_dns module.
  • DNS.Server behavior
  • DNS client

Note: the inet_dns module is considered internal to Erlang and subject to change. If this happened this library will be updated to accommodate for that, but for now inet_dns is simple and worked for me.


The package is available in Hex and can be installed as:

  1. Make sure you have the Erlang/OTP source files installed, otherwise the compilation will fail with an {:error, :enoent} message. On Ubuntu, this can be done using apt-get install erlang-src.

  2. Add dns to your list of dependencies in mix.exs:

    def deps do
      [{:dns, "~> 2.1.2"}]


DNS client

iex> DNS.resolve("")
{:ok, [{216, 58, 221, 110}]}

iex> DNS.resolve("notfound.domain")
{:error, :not_found}

iex> DNS.query("")
%DNS.Record{anlist: [%DNS.Resource{bm: [], class: :in, cnt: 0,
   data: {216, 58, 221, 110}, domain: '', func: false, tm: :undefined,
   ttl: 129, type: :a}], arlist: [],
 header: %DNS.Header{aa: false, id: 0, opcode: :query, pr: false, qr: true,
  ra: true, rcode: 0, rd: false, tc: false}, nslist: [],
 qdlist: [%DNS.Query{class: :in, domain: '', type: :a}]}

iex> DNS.resolve("", :a, {"", 53})

DNS server

defmodule ServerExample do
  @moduledoc """
  Example implementing DNS.Server behaviour
  @behaviour DNS.Server
  use DNS.Server

  def handle(record, _cl) do -> "#{inspect(record)}" end)
    query = hd(record.qdlist)

    result =
      case query.type do
        :a -> {127, 0, 0, 1}
        :cname -> ''
        :txt -> ['your txt value']
        _ -> nil

    resource = %DNS.Resource{
      domain: query.domain,
      class: query.class,
      type: query.type,
      ttl: 0,
      data: result

    %{record | anlist: [resource], header: %{record.header | qr: true}}

To run the example server in iex:

iex(1)> c "example/test_server.ex"
iex(2)> {:ok, server_pid} = ServerExample.start_link 8000
Server listening at 8000
{:ok, #PID<0.180.0>}
iex(3)> Process.exit(server_pid, :normal)

For more information, see API Reference



You can’t perform that action at this time.