Giter Club home page Giter Club logo

elixir-dns's Introduction

DNS

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.

Installation

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

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

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

  2. Ensure dns is started before your application:

    def application do [applications: [:dns]] end

Usage

DNS client

iex> DNS.resolve("google.com")
{216, 58, 221, 110}

iex> DNS.query("google.com")
%DNS.Record{anlist: [%DNS.Resource{bm: [], class: :in, cnt: 0,
   data: {216, 58, 221, 110}, domain: 'google.com', 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: 'google.com', type: :a}]}

iex> DNS.resolve("google.com", {"8.8.8.8", 53})
...

DNS server

defmodule TestServer do
  use Application

  @behaviour DNS.Server

  def start(_args, _opts) do
    import Supervisor.Spec, warn: false

    port = Application.get_env(:dns, :server)[:port]
    children = [
      worker(Task, [DNS.Server, :accept, [port, __MODULE__]])
    ]

    opts = [strategy: :one_for_one, name: DNS.Server.Supervisor]
    Supervisor.start_link(children, opts)
  end

  def handle(record, {ip, _}) do
    query = hd(record.qdlist)

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

    %{record | anlist: [resource]}
  end
end

For more information, see API Reference

License

BSD-3-Clauses

elixir-dns's People

Contributors

tungd avatar

Watchers

James Cloos avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.