Class: Dnsruby::MessageDecoder
- Inherits:
-
Object
- Object
- Dnsruby::MessageDecoder
- Defined in:
- lib/Dnsruby/message.rb
Overview
:nodoc: all
Instance Attribute Summary collapse
-
#index ⇒ Object
readonly
Returns the value of attribute index.
Instance Method Summary collapse
- #get_bytes(len = @limit - @index) ⇒ Object
- #get_label ⇒ Object
- #get_labels(limit = nil) ⇒ Object
- #get_length16 ⇒ Object
- #get_name ⇒ Object
- #get_question ⇒ Object
- #get_rr ⇒ Object
- #get_string ⇒ Object
- #get_string_list ⇒ Object
- #get_unpack(template) ⇒ Object
- #has_remaining ⇒ Object
-
#initialize(data) {|_self| ... } ⇒ MessageDecoder
constructor
A new instance of MessageDecoder.
Constructor Details
#initialize(data) {|_self| ... } ⇒ MessageDecoder
Returns a new instance of MessageDecoder.
870 871 872 873 874 875 |
# File 'lib/Dnsruby/message.rb', line 870 def initialize(data) @data = data @index = 0 @limit = data.length yield self end |
Instance Attribute Details
#index ⇒ Object (readonly)
Returns the value of attribute index.
869 870 871 |
# File 'lib/Dnsruby/message.rb', line 869 def index @index end |
Instance Method Details
#get_bytes(len = @limit - @index) ⇒ Object
895 896 897 898 899 |
# File 'lib/Dnsruby/message.rb', line 895 def get_bytes(len = @limit - @index) d = @data[@index, len] @index += len return d end |
#get_label ⇒ Object
994 995 996 997 998 999 1000 1001 1002 1003 |
# File 'lib/Dnsruby/message.rb', line 994 def get_label begin # label = Name::Label.new(Name::decode(self.get_string)) label = Name::Label.new(self.get_string) return label # return Name::Label::Str.new(self.get_string) rescue ResolvError => e raise DecodeError.new(e) # Turn it into something more suitable end end |
#get_labels(limit = nil) ⇒ Object
965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 |
# File 'lib/Dnsruby/message.rb', line 965 def get_labels(limit=nil) limit = @index if !limit || @index < limit d = [] while true temp = @data[@index] if (temp.class == String) temp = (temp.getbyte(0)) end case temp # @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_length16 ⇒ Object
881 882 883 884 885 886 887 888 889 890 891 892 893 |
# File 'lib/Dnsruby/message.rb', line 881 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_name ⇒ Object
961 962 963 |
# File 'lib/Dnsruby/message.rb', line 961 def get_name return Name.new(self.get_labels) end |
#get_question ⇒ Object
1005 1006 1007 1008 1009 1010 |
# File 'lib/Dnsruby/message.rb', line 1005 def get_question name = self.get_name type, klass = self.get_unpack("nn") q = Question.new(name, type, klass) return q end |
#get_rr ⇒ Object
1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 |
# File 'lib/Dnsruby/message.rb', line 1012 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_string ⇒ Object
942 943 944 945 946 947 948 949 950 951 |
# File 'lib/Dnsruby/message.rb', line 942 def get_string len = @data[@index] if (len.class == String) len = len.getbyte(0) end raise DecodeError.new("limit exceeded\nlimit = #{@limit}, index = #{@index}, len = #{len}\n") if @limit < @index + 1 + (len ? len : 0) d = @data[@index + 1, len] @index += 1 + len return d end |
#get_string_list ⇒ Object
953 954 955 956 957 958 959 |
# File 'lib/Dnsruby/message.rb', line 953 def get_string_list strings = [] while @index < @limit strings << self.get_string end strings end |
#get_unpack(template) ⇒ Object
901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 |
# File 'lib/Dnsruby/message.rb', line 901 def get_unpack(template) len = 0 littlec = ?c bigc = ?C littleh = ?h bigh = ?H littlen = ?n bign = ?N star = ?* if (littlec.class != Fixnum) # We're using Ruby 1.9 - convert the codes littlec = littlec.getbyte(0) bigc = bigc.getbyte(0) littleh = littleh.getbyte(0) bigh = bigh.getbyte(0) littlen = littlen.getbyte(0) bign = bign.getbyte(0) star = star.getbyte(0) end template.each_byte {|byte| case byte when littlec, bigc len += 1 when littleh, bigh len += 1 when littlen len += 2 when bign len += 4 when star 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_remaining ⇒ Object
877 878 879 |
# File 'lib/Dnsruby/message.rb', line 877 def has_remaining return @limit-@index > 0 end |