AsyncDNS-cr  Check-in [5578a30548]

Overview
Comment:Initial import
Downloads: Tarball | ZIP archive | SQL archive
Timelines: family | ancestors | descendants | both | trunk
Files: files | file ages | folders
SHA3-256: 5578a3054825664070e0edf9cc9eb94db3e56c6bce1d0cedb1d3662289e7145d
User & Date: js on 2021-03-03 00:54:03
Other Links: manifest | tags
Context
2021-03-03
01:29
Parse resolv.conf check-in: 070b3339e0 user: js tags: trunk
00:54
Initial import check-in: 5578a30548 user: js tags: trunk
00:45
initial empty check-in check-in: 779bc58d82 user: js tags: trunk
Changes

Added src/asyncdns.cr version [af9da1ad85].















>
>
>
>
>
>
>
1
2
3
4
5
6
7
require "./query"
require "./resolver"

# AsyncDNS is an asynchronous DNS resolver.
module AsyncDNS
  VERSION = "0.1.0"
end

Added src/dns_class.cr version [0f5180ff44].













>
>
>
>
>
>
1
2
3
4
5
6
module AsyncDNS
  enum DNSClass
    IN  =   1
    ANY = 255
  end
end

Added src/query.cr version [ca3771abae].

























>
>
>
>
>
>
>
>
>
>
>
>
1
2
3
4
5
6
7
8
9
10
11
12
require "./dns_class"
require "./rr_type"

module AsyncDNS
  class Query
    getter :domain_name, :dns_class, :rr_type

    def initialize(@domain_name : String, @dns_class : DNSClass,
                   @rr_type : RRType)
    end
  end
end

Added src/resolver.cr version [fe98c6126e].

























>
>
>
>
>
>
>
>
>
>
>
>
1
2
3
4
5
6
7
8
9
10
11
12
require "./settings"

module AsyncDNS
  class Resolver
    getter :settings
    setter :settings

    def initialize
      @settings = Settings.new
    end
  end
end

Added src/rr_type.cr version [2a8d0f9899].

































>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
module AsyncDNS
  enum RRType
    A     =   1
    NS    =   2
    CNAME =   5
    SOA   =   6
    PTR   =  12
    HINFO =  13
    MX    =  15
    TXT   =  16
    RP    =  17
    AAAA  =  28
    SRV   =  33
    ALL   = 255
  end
end

Added src/settings.cr version [09281d3bc1].



















































































































>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
require "time"

module AsyncDNS
  class Settings
    @local_domain : String?
    @last_reload : Time?

    getter :static_hosts, :name_servers, :local_domain, :search_domains,
      :timeout, :max_attempts, :abs_num_dots, :uses_tcp
    setter :static_hosts, :name_servers, :local_domain, :search_domains,
      :uses_tcp

    def initialize
      @static_hosts = Hash(String, Array(String)).new
      @name_servers = [] of String
      @search_domains = [] of String
      @timeout = Time::Span.new(seconds: 2)
      @max_attempts = 2
      @abs_num_dots = 1
      @uses_tcp = false

      self.reload
    end

    def reload
      {% if flag?(:unix) %}
      self.parse_hosts "/etc/hosts"
      {% else %}
      {% raise "Your OS is not supported by AsyncDNS" %}
      {% end %}
    end

    def parse_hosts(path)
      file = File.open(path, "r")

      @static_hosts.clear
      file.each_line do |line|
        pos = line.index('#')
        line = line[0, pos] if pos

        split = line.split(/[ \t]/, remove_empty: true)
        next unless split.size >= 2

        address = split[0]
        split[1, split.size - 1].each do |host|
          addresses = @static_hosts[host]?
          if addresses.nil?
            addresses = [] of String
            @static_hosts[host] = addresses
          end

          addresses << address
        end
      end
    end
  end
end

Added tests/example.cr version [c90f102b63].















>
>
>
>
>
>
>
1
2
3
4
5
6
7
require "../src/asyncdns"

AsyncDNS::Query.new("crystal-lang.org", AsyncDNS::DNSClass::IN,
  AsyncDNS::RRType::A)

settings = AsyncDNS::Settings.new
p settings.static_hosts