Class: Dnsruby::RR::TXT

Inherits:
Dnsruby::RR show all
Defined in:
lib/dnsruby/resource/TXT.rb

Overview

Class for DNS Text (TXT) resource records. RFC 1035 Section 3.3.14

Direct Known Subclasses

SPF

Constant Summary collapse

ClassValue =

:nodoc: all

nil
TypeValue =

:nodoc: all

Types::TXT
ESCAPE_CHARS =
{"b" => 8, "t" => 9, "n" => 10, "v" => 11, "f" => 12, "r" => 13}
ESCAPE_CODES =
ESCAPE_CHARS.invert

Constants inherited from Dnsruby::RR

ClassInsensitiveTypes

Instance Attribute Summary collapse

Attributes inherited from Dnsruby::RR

#klass, #name, #rdata, #ttl, #type

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Dnsruby::RR

#<=>, #==, #clone, create, #eql?, find_class, get_class, get_num, #hash, implemented_rrs, #init_defaults, new_from_data, new_from_hash, new_from_string, #rdlength, #sameRRset, #to_s

Instance Attribute Details

#stringsObject

List of the individual elements



29
30
31
# File 'lib/dnsruby/resource/TXT.rb', line 29

def strings
  @strings
end

Class Method Details

.decode_rdata(msg) ⇒ Object

:nodoc: all



196
197
198
199
# File 'lib/dnsruby/resource/TXT.rb', line 196

def self.decode_rdata(msg) #:nodoc: all
  strings = msg.get_string_list
  return self.new(strings)
end

.display(str, do_escapes = true) ⇒ Object



150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
# File 'lib/dnsruby/resource/TXT.rb', line 150

def TXT.display(str, do_escapes = true)
  output = ""
  #  Probably need to scan through each string manually
  #  Make sure to remember to escape binary characters.
  #  Go through copying to output, and adding "\" characters as necessary?
  str.each_byte {|c|
    if (c == 34) || (c == 92) # || (c == 59)
      if (do_escapes)
      output+='\\'
      end
      output+=c.chr
    elsif (c < 32) # c is binary
      if (ESCAPE_CODES[c])
        output +=  c.chr
      else
        output+= '\\'
        num = c.to_i.to_s
        (3-num.length).times {|i|
          num="0"+num
        }
        output+= num # Need a 3 digit number here.
      end

    else
      output += c.chr
    end
  }
  return output
end

.parse(input) ⇒ Object



52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
# File 'lib/dnsruby/resource/TXT.rb', line 52

def TXT.parse(input)
  #  Need to look out for special characters.
  #  Need to split the input up into strings (which are defined by non-escaped " characters)
  #  Then need to fix up any \ escape characters (should just be " and ; and binary?)
  #  Sadly, it's going to be easiest just to scan through this character by character...
  in_escaped = false
  in_string = false
  count = -1
  strings = []
  current_binary = ""
  current_quote_char = '"'
  unquoted = false
  seen_strings = false
  pos = 0
  input = input.sub(/^\s*\(\s*/, "")
  input.sub!(/\s*\)\s*$/, "")
  input.each_char {|c|
    if (((c == "'") || (c == '"')) && (!in_escaped) && (!unquoted))
      if (!in_string)
        seen_strings = true
        current_quote_char = c
        in_string = true
        count+=1
        strings[count] = ""
      else
        if (c == current_quote_char)
          in_string = false
        else
          strings[count]+=c
        end
      end
    else
      if (seen_strings && !in_string)
        if (c == ";")
           # Comment in zone file!
           return strings
        end
        if (c != " " && c != "\t")
          in_string = true
          count+=1
          strings[count] = ""
        else
          next
        end
      end
      if (pos == 0)
        unquoted = true
        count+=1
        strings[count] = ""
      elsif (unquoted)
        if (c == " ")
          count+=1
          strings[count] = ""
          pos += 1
          next
        end
      end

      if (c == "\\")
        if (in_escaped)
          in_escaped = false
          strings[count]+=(c)
        else
          in_escaped = true
        end
      else
        if (in_escaped)
          #  Build up the binary
          if (c == ";") || (c == '"')
            strings[count]+=c
            in_escaped = false
          elsif (ESCAPE_CHARS[c])
            in_escaped=false
            strings[count]+=ESCAPE_CHARS[c].chr
          elsif (c<"0" || c>"9")
            in_escaped = false
            strings[count]+=c
          else
            #  Must be building up three digit string to identify binary value?
#                  if (c >= "0" && c <= "9")
              current_binary += c
#                  end
            if ((current_binary.length == 3) ) # || (c < "0" || c > "9"))
              strings[count]+=current_binary.to_i.chr
              in_escaped = false
              current_binary = ""
            end
          end
        else
          strings[count]+=(c)
        end
      end
    end
    pos += 1
  }
  return strings
end

Instance Method Details

#dataObject



31
32
33
# File 'lib/dnsruby/resource/TXT.rb', line 31

def data
  @strings.join
end

#encode_rdata(msg, canonical = false) ⇒ Object

:nodoc: all



192
193
194
# File 'lib/dnsruby/resource/TXT.rb', line 192

def encode_rdata(msg, canonical=false) #:nodoc: all
  msg.put_string_list(@strings)
end

#from_data(data) ⇒ Object



35
36
37
# File 'lib/dnsruby/resource/TXT.rb', line 35

def from_data(data)
  @strings = data
end

#from_hash(hash) ⇒ Object



39
40
41
42
43
# File 'lib/dnsruby/resource/TXT.rb', line 39

def from_hash(hash)
  if (hash.has_key?:strings)
    from_string(hash[:strings])
  end
end

#from_string(input) ⇒ Object



48
49
50
# File 'lib/dnsruby/resource/TXT.rb', line 48

def from_string(input)
  @strings = TXT.parse(input)
end

#rdata_to_stringObject



180
181
182
183
184
185
186
187
188
189
190
# File 'lib/dnsruby/resource/TXT.rb', line 180

def rdata_to_string
  if (defined?@strings)
    temp = []
    @strings.each {|str|
      output = TXT.display(str)
      temp.push("\"#{output}\"")
    }
    return temp.join(' ')
  end
  return ''
end