Class: Resolv::DNS::Message::MessageEncoder

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

Overview

:nodoc:

Instance Method Summary collapse

Constructor Details

#initialize {|_self| ... } ⇒ MessageEncoder

Returns a new instance of MessageEncoder.

Yields:

  • (_self)

Yield Parameters:



1642
1643
1644
1645
1646
# File 'lib/resolv.rb', line 1642

def initialize
  @data = ''.dup
  @names = {}
  yield self
end

Instance Method Details

#put_bytes(d) ⇒ Object



1652
1653
1654
# File 'lib/resolv.rb', line 1652

def put_bytes(d)
  @data << d
end

#put_label(d) ⇒ Object



1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
# File 'lib/resolv.rb', line 1707

def put_label(d)
  s = d.to_s
  # Label::Str applies this limit when a label is built, so what is left
  # for here is a raw string handed straight to put_labels. The two ways
  # an over-long label goes wrong differ: 64 to 255 octets write a length
  # octet in the reserved or compression pointer range, and 256 or more
  # wrap it mod 256. Either way the encoded name stops being the name the
  # caller asked for. [RFC 1035 2.3.4, 4.1.4]
  if s.bytesize > 63
    raise ArgumentError, "DNS label is too long (#{s.bytesize} bytes, max 63): #{s.inspect}"
  end
  self.put_string(s)
end

#put_labels(d, compress: true) ⇒ Object



1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
# File 'lib/resolv.rb', line 1691

def put_labels(d, compress: true)
  d.each_index {|i|
    domain = d[i..-1]
    if compress && idx = @names[domain]
      self.put_pack("n", 0xc000 | idx)
      return
    else
      if @data.length < 0x4000
        @names[domain] = @data.length
      end
      self.put_label(d[i])
    end
  }
  @data << "\0"
end

#put_length16 ⇒ Object



1660
1661
1662
1663
1664
1665
1666
1667
# File 'lib/resolv.rb', line 1660

def put_length16
  length_index = @data.length
  @data << "\0\0"
  data_start = @data.length
  yield
  data_end = @data.length
  @data[length_index, 2] = [data_end - data_start].pack("n")
end

#put_name(d, compress: true) ⇒ Object



1687
1688
1689
# File 'lib/resolv.rb', line 1687

def put_name(d, compress: true)
  put_labels(d.to_a, compress: compress)
end

#put_pack(template, *d) ⇒ Object



1656
1657
1658
# File 'lib/resolv.rb', line 1656

def put_pack(template, *d)
  @data << d.pack(template)
end

#put_string(d) ⇒ Object



1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
# File 'lib/resolv.rb', line 1669

def put_string(d)
  s = d.to_s
  # A character-string is prefixed by a single length octet, so it can
  # hold at most 255 octets. [RFC 1035 3.3] Reject anything longer to
  # avoid silently truncating the length to its low 8 bits (mod 256).
  if s.bytesize > 255
    raise ArgumentError, "character-string is too long (#{s.bytesize} bytes, max 255): #{s.inspect}"
  end
  self.put_pack("C", s.bytesize)
  @data << s
end

#put_string_list(ds) ⇒ Object



1681
1682
1683
1684
1685
# File 'lib/resolv.rb', line 1681

def put_string_list(ds)
  ds.each {|d|
    self.put_string(d)
  }
end

#to_s ⇒ Object



1648
1649
1650
# File 'lib/resolv.rb', line 1648

def to_s
  return @data
end