AsyncDNS-cr  resolver.cr at [c210ddc089]

File src/resolver.cr artifact 389b0620fd part of check-in c210ddc089


require "./rr"
require "./settings"
require "./response"

module AsyncDNS
  class Resolver
    getter settings : Settings

    enum Error
      NO_NAME_SERVER
    end

    private record Context, id : UInt16, settings : Settings,
      block : Response | Error ->

    def initialize
      @settings = Settings.new
      @queries = Hash(UInt16, Context).new
      @tcp_queries = Hash(Socket, Context).new
    end

    def resolve(query : Query, &block : Response | Error ->)
      id : UInt16
      while true
        id = Random::Secure.rand(UInt16::MIN..UInt16::MAX)
        break unless @queries.has_key?(id)
      end

      if @settings.nameservers.empty?
        yield Error::NO_NAME_SERVER
        return
      end

      send(Context.new(id, settings.dup, block))
    end

    private def send(context : Context)
      @queries[context.@id] = context

      # TODO
    end

    def stop
      # TODO
    end
  end
end