Class: MockDnsServer::ActionFactory

Inherits:
Object
  • Object
show all
Includes:
MessageBuilder
Defined in:
lib/mock_dns_server/action_factory.rb

Overview

Creates and returns actions that will be run upon receiving incoming messages.

Instance Method Summary collapse

Methods included from MessageBuilder

axfr_request, dns_update, dummy_a_response, ixfr_request, ixfr_request_soa_rr, notify_message, ns, rr, serial_value, soa_answer, soa_request, soa_response, specified_a_response

Instance Method Details

#constant(constant_object) ⇒ Object

Responds with the same object regardless of the request content.



27
28
29
30
31
# File 'lib/mock_dns_server/action_factory.rb', line 27

def constant(constant_object)
  ->(_, sender, context, protocol) do
    context.server.send_response(sender, constant_object, protocol)
  end
end

#echoObject

Echos the request back to the sender.



11
12
13
14
15
# File 'lib/mock_dns_server/action_factory.rb', line 11

def echo
  ->(incoming_message, sender, context, protocol) do
    context.server.send_response(sender, incoming_message, protocol)
  end
end

#puts_and_echoObject



17
18
19
20
21
22
23
24
# File 'lib/mock_dns_server/action_factory.rb', line 17

def puts_and_echo
  ->(incoming_message, sender, context, protocol) do
    puts "Received #{protocol.to_s.upcase} message from #{sender}:\n#{incoming_message}\n\n"
    puts "Hex:\n\n"
    puts "#{incoming_message.encode.hexdump}\n\n"
    echo.(incoming_message, sender, context, protocol)
  end
end

#puts_messageObject

Outputs the string representation of the incoming message to stdout.



56
57
58
59
60
# File 'lib/mock_dns_server/action_factory.rb', line 56

def puts_message
  ->(incoming_message, sender, context, protocol) do
    puts incoming_message
  end
end

#send_message(response) ⇒ Object

Sends a fixed DNSRuby::Message.



42
43
44
45
46
47
48
49
50
51
52
53
# File 'lib/mock_dns_server/action_factory.rb', line 42

def send_message(response)
  ->(incoming_message, sender, context, protocol) do

    if [incoming_message, response].all? { |m| m.is_a?(Dnsruby::Message) }
      response.header.id = incoming_message.header.id
    end
    if response.is_a?(Dnsruby::Message)
      response.header.qr = true
    end
    context.server.send_response(sender, response, protocol)
  end
end

#send_soa(zone, serial, expire = nil, refresh = nil) ⇒ Object

Sends a SOA response.



35
36
37
38
# File 'lib/mock_dns_server/action_factory.rb', line 35

def send_soa(zone, serial, expire = nil, refresh = nil)
  send_message(soa_response(
      name: zone, serial: serial, expire: expire, refresh: refresh))
end

#zone_load(serial_history) ⇒ Object



63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
# File 'lib/mock_dns_server/action_factory.rb', line 63

def zone_load(serial_history)
  ->(incoming_message, sender, context, protocol) do

    mt = MessageTransformer.new(incoming_message)
    zone = mt.qname
    type = mt.qtype

    if serial_history.zone.downcase != zone.downcase
      raise "Zones differ (history: #{serial_history.zone}, request: #{zone}"
    end

    if %w(AXFR IXFR).include?(type)
      xfr_response = serial_history.xfr_response(incoming_message)
      send_message(xfr_response).(incoming_message, sender, context, :tcp)
    elsif type == 'SOA'
      send_soa(zone, serial_history.high_serial).(incoming_message, sender, context, protocol)
    end
  end
end