Module: MockDnsServer::MessageBuilder
- Included in:
- ActionFactory, ServerContext
- Defined in:
- lib/mock_dns_server/message_builder.rb
Class Method Summary collapse
- .axfr_request(zone) ⇒ Object
-
.dns_update(zone, records) ⇒ Object
Creates a Dnsruby::Update object from a hash, as it would be generated from a Cucumber table with the following headings: | Action | Type | Domain | RDATA |.
- .dummy_a_response(record_count, domain, ttl = 86400) ⇒ Object
- .ixfr_request(zone, serial) ⇒ Object
-
.ixfr_request_soa_rr(zone, serial) ⇒ Object
Builds a SOA RR suitable for inclusion in the authority section of an IXFR query.
- .notify_message(options) ⇒ Object
-
.ns(owner, address) ⇒ Object
Creates an NS RR record.
-
.rr(type, name, rdata) ⇒ Object
Builds a Dnsruby::RR instance with the specified type, name, and rdata, with a hard coded TTL and class ‘IN’.
-
.serial_value(serial) ⇒ Object
Gets the serial value from the passed object; if it’s a SerialNumber, calls its value method; if not, we assume it’s a number and it’s returned unchanged.
- .soa_answer(options) ⇒ Object
- .soa_request(name) ⇒ Object
-
.soa_response(options) ⇒ Object
TODO: Eliminate duplication of soa_response and notify_message.
-
.specified_a_response(answer_string) ⇒ Object
Builds a response to an ‘A’ request from the encoded string passed.
Class Method Details
.axfr_request(zone) ⇒ Object
173 174 175 |
# File 'lib/mock_dns_server/message_builder.rb', line 173 def axfr_request(zone) Dnsruby::Message.new(zone, 'AXFR') end |
.dns_update(zone, records) ⇒ Object
Creates a Dnsruby::Update object from a hash, as it would be generated from a Cucumber table with the following headings: | Action | Type | Domain | RDATA |
120 121 122 123 124 125 126 127 128 129 130 131 132 |
# File 'lib/mock_dns_server/message_builder.rb', line 120 def dns_update(zone, records) update = Dnsruby::Update.new(zone) records.each do |r| if r.type.upcase == 'ADD' s = "#{Domain} 3600 #{Type} #{RDATA}" rr = Dnsruby::RR.create(s) update.add(rr) else update.delete(r['Domain'], r['Type'], r['RDATA']) end end update end |
.dummy_a_response(record_count, domain, ttl = 86400) ⇒ Object
56 57 58 59 60 61 62 63 64 65 66 67 |
# File 'lib/mock_dns_server/message_builder.rb', line 56 def dummy_a_response(record_count, domain, ttl = 86400) ip_dispenser = IpAddressDispenser.new = Dnsruby::Message.new .header.qr = true record_count.times do answer = Dnsruby::RR.new_from_hash( name: domain, ttl: ttl, type: 'A', address: ip_dispenser.next, klass: 'IN') .add_answer(answer) end end |
.ixfr_request(zone, serial) ⇒ Object
166 167 168 169 170 |
# File 'lib/mock_dns_server/message_builder.rb', line 166 def ixfr_request(zone, serial) query = Dnsruby::Message.new(zone, 'IXFR') query.(ixfr_request_soa_rr(zone, serial_value(serial))) query end |
.ixfr_request_soa_rr(zone, serial) ⇒ Object
Builds a SOA RR suitable for inclusion in the authority section of an IXFR query.
147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 |
# File 'lib/mock_dns_server/message_builder.rb', line 147 def ixfr_request_soa_rr(zone, serial) = { name: zone, type: 'SOA', ttl: 3600, klass: 'IN', mname: '.', rname: '.', serial: serial_value(serial), refresh: 0, retry: 0, expire: 0, minimum: 0 } Dnsruby::RR.new_from_hash() end |
.notify_message(options) ⇒ Object
178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 |
# File 'lib/mock_dns_server/message_builder.rb', line 178 def () = Dnsruby::Message.new([:name], 'SOA', 'IN') .header.opcode = Dnsruby::OpCode::Notify mname = [:mname] || 'default.com' .add_answer(Dnsruby::RR.new_from_hash( { name: [:name], type: 'SOA', serial: serial_value([:serial]), mname: mname, rname: 'admin.' + mname, refresh: 3600, retry: 3600, expire: 3600, minimum: 3600 } )) end |
.ns(owner, address) ⇒ Object
Creates an NS RR record.
111 112 113 |
# File 'lib/mock_dns_server/message_builder.rb', line 111 def ns(owner, address) rr('NS', owner, address) end |
.rr(type, name, rdata) ⇒ Object
Builds a Dnsruby::RR instance with the specified type, name, and rdata, with a hard coded TTL and class ‘IN’.
102 103 104 105 106 107 |
# File 'lib/mock_dns_server/message_builder.rb', line 102 def rr(type, name, rdata) ttl = 3600 klass = 'IN' string = [name, ttl, klass, type, rdata].join(' ') Dnsruby::RR.new_from_string(string) end |
.serial_value(serial) ⇒ Object
Gets the serial value from the passed object; if it’s a SerialNumber, calls its value method; if not, we assume it’s a number and it’s returned unchanged.
72 73 74 |
# File 'lib/mock_dns_server/message_builder.rb', line 72 def serial_value(serial) serial.is_a?(SerialNumber) ? serial.value : serial end |
.soa_answer(options) ⇒ Object
82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 |
# File 'lib/mock_dns_server/message_builder.rb', line 82 def soa_answer() mname = [:mname] || 'default.com' Dnsruby::RR.create( { name: [:name], ttl: [:ttl] || 3600, type: 'SOA', serial: serial_value([:serial]), mname: mname, rname: 'admin.' + mname, refresh: [:refresh] || 3600, retry: [:retry] || 3600, expire: [:expire] || 3600, minimum: [:minimum] || 3600 } ) end |
.soa_request(name) ⇒ Object
77 78 79 |
# File 'lib/mock_dns_server/message_builder.rb', line 77 def soa_request(name) Dnsruby::Message.new(name, 'SOA', 'IN') end |
.soa_response(options) ⇒ Object
TODO: Eliminate duplication of soa_response and notify_message
137 138 139 140 141 142 143 |
# File 'lib/mock_dns_server/message_builder.rb', line 137 def soa_response() raise "Must provide zone name as options[:name]." if [:name].nil? = Dnsruby::Message.new([:name], 'SOA', 'IN') .header.qr = true .add_answer(soa_answer()) end |
.specified_a_response(answer_string) ⇒ Object
Builds a response to an ‘A’ request from the encoded string passed. (see resource.rb, e.g. github.com/vertis/dnsruby/blob/master/lib/Dnsruby/resource/resource.rb, line 643 ff at the time of this writing):
a = Dnsruby::RR.create("foo.example.com. 86400 A 10.1.2.3")
47 48 49 50 51 52 53 |
# File 'lib/mock_dns_server/message_builder.rb', line 47 def specified_a_response(answer_string) = Dnsruby::Message.new .header.qr = true answer = Dnsruby::RR.create(answer_string) .add_answer(answer) end |