Class: Resolv::DNS::Message::MessageDecoder

Inherits:
Object
  • Object
show all
Defined in:
lib/net/dns/resolv.rb

Overview

:nodoc: used to implement Message.decode

Instance Method Summary collapse

Constructor Details

#initialize(data) {|_self| ... } ⇒ MessageDecoder

Returns a new instance of MessageDecoder.

Yields:

  • (_self)

Yield Parameters:



1345
1346
1347
1348
1349
1350
# File 'lib/net/dns/resolv.rb', line 1345

def initialize(data)
  @data = data
  @index = 0
  @limit = data.length
  yield self
end

Instance Method Details

#get_bytes(len = @limit - @index) ⇒ Object



1366
1367
1368
1369
1370
# File 'lib/net/dns/resolv.rb', line 1366

def get_bytes(len = @limit - @index)
  d = @data[@index, len]
  @index += len
  return d
end

#get_labelObject



1437
1438
1439
# File 'lib/net/dns/resolv.rb', line 1437

def get_label
  return Label::Str.new(self.get_string)
end

#get_labels(limit = nil) ⇒ Object



1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
# File 'lib/net/dns/resolv.rb', line 1412

def get_labels(limit=nil)
  limit = @index if !limit || @index < limit
  d = []
  while true
    case @data[@index]
    when 0
      @index += 1
      return d
    when 192..255
      idx = self.get_unpack('n')[0] & 0x3fff
      if limit <= idx
        raise DecodeError.new("non-backward name pointer")
      end
      save_index = @index
      @index = idx
      d += self.get_labels(limit)
      @index = save_index
      return d
    else
      d << self.get_label
    end
  end
  return d
end

#get_length16Object



1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
# File 'lib/net/dns/resolv.rb', line 1352

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_nameObject



1408
1409
1410
# File 'lib/net/dns/resolv.rb', line 1408

def get_name
  return Name.new(self.get_labels)
end

#get_questionObject



1441
1442
1443
1444
1445
1446
1447
# File 'lib/net/dns/resolv.rb', line 1441

def get_question
  name = self.get_name
  type, klass = self.get_unpack("nn")
  typeclass = Resource.get_class(type, klass % 0x8000)
  unicast = (klass >> 15) == 1
  return name, typeclass, unicast
end

#get_rrObject



1449
1450
1451
1452
1453
1454
1455
1456
# File 'lib/net/dns/resolv.rb', line 1449

def get_rr
  name = self.get_name
  type, klass, ttl = self.get_unpack('nnN')
  typeclass = Resource.get_class(type, klass % 0x8000)
  data = self.get_length16 {typeclass.decode_rdata(self)}
  cacheflush = (klass >> 15) == 1
  return name, ttl, data, cacheflush
end

#get_stringObject

Raises:



1392
1393
1394
1395
1396
1397
1398
# File 'lib/net/dns/resolv.rb', line 1392

def get_string
  len = @data[@index]
  raise DecodeError.new("limit exceeded") if @limit < @index + 1 + len
  d = @data[@index + 1, len]
  @index += 1 + len
  return d
end

#get_string_listObject



1400
1401
1402
1403
1404
1405
1406
# File 'lib/net/dns/resolv.rb', line 1400

def get_string_list
  strings = []
  while @index < @limit
    strings << self.get_string
  end
  strings
end

#get_unpack(template) ⇒ Object

Raises:



1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
# File 'lib/net/dns/resolv.rb', line 1372

def get_unpack(template)
  len = 0
  template.each_byte {|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