ADDED src/asyncdns.cr Index: src/asyncdns.cr ================================================================== --- /dev/null +++ src/asyncdns.cr @@ -0,0 +1,7 @@ +require "./query" +require "./resolver" + +# AsyncDNS is an asynchronous DNS resolver. +module AsyncDNS + VERSION = "0.1.0" +end ADDED src/dns_class.cr Index: src/dns_class.cr ================================================================== --- /dev/null +++ src/dns_class.cr @@ -0,0 +1,6 @@ +module AsyncDNS + enum DNSClass + IN = 1 + ANY = 255 + end +end ADDED src/query.cr Index: src/query.cr ================================================================== --- /dev/null +++ src/query.cr @@ -0,0 +1,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 Index: src/resolver.cr ================================================================== --- /dev/null +++ src/resolver.cr @@ -0,0 +1,12 @@ +require "./settings" + +module AsyncDNS + class Resolver + getter :settings + setter :settings + + def initialize + @settings = Settings.new + end + end +end ADDED src/rr_type.cr Index: src/rr_type.cr ================================================================== --- /dev/null +++ src/rr_type.cr @@ -0,0 +1,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 Index: src/settings.cr ================================================================== --- /dev/null +++ src/settings.cr @@ -0,0 +1,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 Index: tests/example.cr ================================================================== --- /dev/null +++ tests/example.cr @@ -0,0 +1,7 @@ +require "../src/asyncdns" + +AsyncDNS::Query.new("crystal-lang.org", AsyncDNS::DNSClass::IN, + AsyncDNS::RRType::A) + +settings = AsyncDNS::Settings.new +p settings.static_hosts