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:



1545
1546
1547
1548
1549
# File 'lib/resolv.rb', line 1545

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

Instance Method Details

#put_bytes(d) ⇒ Object



1555
1556
1557
# File 'lib/resolv.rb', line 1555

def put_bytes(d)
  @data << d
end

#put_label(d) ⇒ Object



1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
# File 'lib/resolv.rb', line 1610

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



1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
# File 'lib/resolv.rb', line 1594

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_length16Object



1563
1564
1565
1566
1567
1568
1569
1570
# File 'lib/resolv.rb', line 1563

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



1590
1591
1592
# File 'lib/resolv.rb', line 1590

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

#put_pack(template, *d) ⇒ Object



1559
1560
1561
# File 'lib/resolv.rb', line 1559

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

#put_string(d) ⇒ Object



1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
# File 'lib/resolv.rb', line 1572

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



1584
1585
1586
1587
1588
# File 'lib/resolv.rb', line 1584

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

#to_sObject



1551
1552
1553
# File 'lib/resolv.rb', line 1551

def to_s
  return @data
end