class Dnsruby::RR::TXT

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

Constants

ESCAPE_CHARS
ESCAPE_CODES

Attributes

strings[RW]

List of the individual elements

Public Class Methods

display(str, do_escapes = true) click to toggle source
# 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) click to toggle source
# 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.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

Public Instance Methods

data() click to toggle source
# File lib/dnsruby/resource/TXT.rb, line 31
def data
  @strings.join
end
from_data(data) click to toggle source
# File lib/dnsruby/resource/TXT.rb, line 35
def from_data(data)
  @strings = data
end
from_hash(hash) click to toggle source
# 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) click to toggle source
# File lib/dnsruby/resource/TXT.rb, line 48
def from_string(input)
  @strings = TXT.parse(input)
end
rdata_to_string() click to toggle source
# 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