Class: Dnsruby::MessageDecoder

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

Overview

:nodoc: all

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

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

Returns a new instance of MessageDecoder.

Yields:

  • (_self)

Yield Parameters:



613
614
615
616
617
618
# File 'lib/Dnsruby/message.rb', line 613

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

Instance Attribute Details

#indexObject (readonly)

Returns the value of attribute index.



612
613
614
# File 'lib/Dnsruby/message.rb', line 612

def index
  @index
end

Instance Method Details

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



638
639
640
641
642
# File 'lib/Dnsruby/message.rb', line 638

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

#get_labelObject



713
714
715
# File 'lib/Dnsruby/message.rb', line 713

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

#get_labels(limit = nil) ⇒ Object



688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
# File 'lib/Dnsruby/message.rb', line 688

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



624
625
626
627
628
629
630
631
632
633
634
635
636
# File 'lib/Dnsruby/message.rb', line 624

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



684
685
686
# File 'lib/Dnsruby/message.rb', line 684

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

#get_questionObject



717
718
719
720
721
722
# File 'lib/Dnsruby/message.rb', line 717

def get_question
  name = self.get_name
  type, klass = self.get_unpack("nn")
  q = Question.new(name, type, klass)
  return q
end

#get_rrObject



724
725
726
727
728
729
730
731
732
733
734
735
736
737
# File 'lib/Dnsruby/message.rb', line 724

def get_rr
  name = self.get_name
  type, klass, ttl = self.get_unpack('nnN')
  klass = Classes.new(klass)
  typeclass = RR.get_class(type, klass)
  # @TODO@ Trap decode errors here, and somehow mark the record as bad.
  # Need some way to represent raw data only
  rec = self.get_length16 {typeclass.decode_rdata(self)}
  rec.name=name
  rec.ttl=ttl
  rec.type = type
  rec.klass = klass
  return rec
end

#get_stringObject

Raises:



668
669
670
671
672
673
674
# File 'lib/Dnsruby/message.rb', line 668

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



676
677
678
679
680
681
682
# File 'lib/Dnsruby/message.rb', line 676

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

#get_unpack(template) ⇒ Object

Raises:



644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
# File 'lib/Dnsruby/message.rb', line 644

def get_unpack(template)
  len = 0
  template.each_byte {|byte|
    case byte
    when ?c, ?C
      len += 1
    when ?h, ?H
      len += 1          
    when ?n
      len += 2
    when ?N
      len += 4
    when ?*
      len = @limit-@index
    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

#has_remainingObject



620
621
622
# File 'lib/Dnsruby/message.rb', line 620

def has_remaining
  return @limit-@index > 0
end