AsyncDNS-cr  resolver.cr at [baf27b579c]

File src/resolver.cr artifact e08367333f part of check-in baf27b579c


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

module AsyncDNS
  class Resolver
    getter :settings

    enum Error
      NO_NAME_SERVER
    end

    private struct Context
      def initialize(@id : UInt16, @settings : Settings,
                     @block : Response | Error ->)
      end
    end

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

    def resolve(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

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

      # TODO
    end

    def stop
      # TODO
    end
  end
end