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

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

Overview

:nodoc:

Instance Method Summary collapse

Constructor Details

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

Returns a new instance of MessageDecoder.

Yields:

  • (_self)

Yield Parameters:


1497
1498
1499
1500
1501
1502
# File 'lib/resolv.rb', line 1497

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

Instance Method Details

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


1522
1523
1524
1525
1526
# File 'lib/resolv.rb', line 1522

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

#get_labelObject


1594
1595
1596
# File 'lib/resolv.rb', line 1594

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

#get_labels(limit = nil) ⇒ Object


1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
# File 'lib/resolv.rb', line 1569

def get_labels(limit=nil)
  limit = @index if !limit || @index < limit
  d = []
  while true
    case @data[@index].ord
    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


1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
# File 'lib/resolv.rb', line 1508

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


1565
1566
1567
# File 'lib/resolv.rb', line 1565

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

#get_questionObject


1598
1599
1600
1601
1602
# File 'lib/resolv.rb', line 1598

def get_question
  name = self.get_name
  type, klass = self.get_unpack("nn")
  return name, Resource.get_class(type, klass)
end

#get_rrObject


1604
1605
1606
1607
1608
1609
1610
1611
# File 'lib/resolv.rb', line 1604

def get_rr
  name = self.get_name
  type, klass, ttl = self.get_unpack('nnN')
  typeclass = Resource.get_class(type, klass)
  res = self.get_length16 { typeclass.decode_rdata self }
  res.instance_variable_set :@ttl, ttl
  return name, ttl, res
end

#get_stringObject

Raises:


1549
1550
1551
1552
1553
1554
1555
# File 'lib/resolv.rb', line 1549

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

#get_string_listObject


1557
1558
1559
1560
1561
1562
1563
# File 'lib/resolv.rb', line 1557

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

#get_unpack(template) ⇒ Object

Raises:


1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
# File 'lib/resolv.rb', line 1528

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

#inspectObject


1504
1505
1506
# File 'lib/resolv.rb', line 1504

def inspect
  "\#<#{self.class}: #{@data[0, @index].inspect} #{@data[@index..-1].inspect}>"
end