module DNS::ResolveHost::Controls::Server

Public Class Methods

start(hostname: nil, ip_addresses: nil, &block) click to toggle source
# File lib/dns/resolve_host/controls/server.rb, line 5
def self.start(hostname: nil, ip_addresses: nil, &block)
  hostname ||= Hostname.example
  ip_addresses ||= [IPAddress.example]

  bind_address = BindAddress.example
  port = Port.example

  interfaces = [[:udp, bind_address, port], [:tcp, bind_address, port]]

  Celluloid.logger = Log.get self

  dns_server = RubyDNS.run_server :listen => interfaces, :asynchronous => true do
    match %r{\A#{Regexp.escape hostname}\z} do |request|
      ip_addresses.each do |ip_address|
        request.respond! ip_address
      end
    end
  end

  block.(bind_address, port)

  # The rubydns library does not properly unbind its server socket when
  # the actor terminates. See the following github issue:
  #
  #     https://github.com/ioquatix/rubydns/issues/60
  #
  # The workaround discovered by the person who filed the issue is
  # pasted here. The proper way to stop the DNS server would be this:
  #
  #     dns_server.terminate
  #
  # [Nathan Ladd, Fri 30 Dec 2016]
  dns_server.actors.first.links.each(&:terminate)
end