Class: RRs::Message::MessageDecoder
- Inherits:
-
Object
- Object
- RRs::Message::MessageDecoder
- Defined in:
- lib/rrs/message.rb
Overview
:nodoc:
Instance Method Summary collapse
- #get_bytes(len = @limit - @index) ⇒ Object
- #get_label ⇒ Object
- #get_labels ⇒ Object
- #get_length16 ⇒ Object
- #get_list ⇒ Object
- #get_name ⇒ Object
- #get_question ⇒ Object
- #get_rr ⇒ Object
- #get_string ⇒ Object
- #get_string_list ⇒ Object
- #get_unpack(template) ⇒ Object
-
#initialize(data) {|_self| ... } ⇒ MessageDecoder
constructor
A new instance of MessageDecoder.
- #inspect ⇒ Object
Constructor Details
#initialize(data) {|_self| ... } ⇒ MessageDecoder
Returns a new instance of MessageDecoder.
215 216 217 218 219 220 |
# File 'lib/rrs/message.rb', line 215 def initialize(data) @data = data @index = 0 @limit = data.bytesize yield self end |
Instance Method Details
#get_bytes(len = @limit - @index) ⇒ Object
240 241 242 243 244 245 |
# File 'lib/rrs/message.rb', line 240 def get_bytes(len = @limit - @index) raise DecodeError.new("limit exceeded") if @limit < @index + len d = @data.byteslice(@index, len) @index += len return d end |
#get_label ⇒ Object
326 327 328 |
# File 'lib/rrs/message.rb', line 326 def get_label return Label::Str.new(self.get_string) end |
#get_labels ⇒ Object
297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 |
# File 'lib/rrs/message.rb', line 297 def get_labels prev_index = @index save_index = nil d = [] while true raise DecodeError.new("limit exceeded") if @limit <= @index case @data.getbyte(@index) when 0 @index += 1 if save_index @index = save_index end return d when 192..255 idx = self.get_unpack('n')[0] & 0x3fff if prev_index <= idx raise DecodeError.new("non-backward name pointer") end prev_index = idx if !save_index save_index = @index end @index = idx else d << self.get_label end end end |
#get_length16 ⇒ Object
226 227 228 229 230 231 232 233 234 235 236 237 238 |
# File 'lib/rrs/message.rb', line 226 def get_length16 len, = self.get_unpack('n') save_limit = @limit @limit = @index + len d = yield(len) if @index < @limit raise DecodeError.new("junk exists") elsif @limit < @index raise DecodeError.new("limit exceeded") end @limit = save_limit return d end |
#get_list ⇒ Object
285 286 287 288 289 290 291 |
# File 'lib/rrs/message.rb', line 285 def get_list [].tap do |values| while @index < @limit values << yield end end end |
#get_name ⇒ Object
293 294 295 |
# File 'lib/rrs/message.rb', line 293 def get_name return Name.new(self.get_labels) end |
#get_question ⇒ Object
330 331 332 333 334 |
# File 'lib/rrs/message.rb', line 330 def get_question name = self.get_name type, klass = self.get_unpack("nn") return name, Resource.get_class(type, klass) end |
#get_rr ⇒ Object
336 337 338 339 340 341 342 343 344 345 346 347 348 349 |
# File 'lib/rrs/message.rb', line 336 def get_rr name = self.get_name type, klass, ttl = self.get_unpack('nnN') typeclass = Resource.get_class(type, klass) res = self.get_length16 do begin typeclass.decode_rdata self rescue => e raise DecodeError, e., e.backtrace end end res.instance_variable_set :@ttl, ttl return name, ttl, res end |
#get_string ⇒ Object
268 269 270 271 272 273 274 275 |
# File 'lib/rrs/message.rb', line 268 def get_string raise DecodeError.new("limit exceeded") if @limit <= @index len = @data.getbyte(@index) raise DecodeError.new("limit exceeded") if @limit < @index + 1 + len d = @data.byteslice(@index + 1, len) @index += 1 + len return d end |
#get_string_list ⇒ Object
277 278 279 280 281 282 283 |
# File 'lib/rrs/message.rb', line 277 def get_string_list strings = [] while @index < @limit strings << self.get_string end strings end |
#get_unpack(template) ⇒ Object
247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 |
# File 'lib/rrs/message.rb', line 247 def get_unpack(template) len = 0 template.each_byte {|byte| byte = "%c" % byte case byte when ?c, ?C len += 1 when ?n len += 2 when ?N len += 4 else raise StandardError.new("unsupported template: '#{byte.chr}' in '#{template}'") end } raise DecodeError.new("limit exceeded") if @limit < @index + len arr = @data.unpack("@#{@index}#{template}") @index += len return arr end |
#inspect ⇒ Object
222 223 224 |
# File 'lib/rrs/message.rb', line 222 def inspect "\#<#{self.class}: #{@data.byteslice(0, @index).inspect} #{@data.byteslice(@index..-1).inspect}>" end |