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:



1452
1453
1454
1455
1456
# File 'lib/resolv.rb', line 1452

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

Instance Method Details

#put_bytes(d) ⇒ Object



1462
1463
1464
# File 'lib/resolv.rb', line 1462

def put_bytes(d)
  @data << d
end

#put_label(d) ⇒ Object



1510
1511
1512
# File 'lib/resolv.rb', line 1510

def put_label(d)
  self.put_string(d.to_s)
end

#put_labels(d) ⇒ Object



1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
# File 'lib/resolv.rb', line 1494

def put_labels(d)
  d.each_index {|i|
    domain = d[i..-1]
    if 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



1470
1471
1472
1473
1474
1475
1476
1477
# File 'lib/resolv.rb', line 1470

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) ⇒ Object



1490
1491
1492
# File 'lib/resolv.rb', line 1490

def put_name(d)
  put_labels(d.to_a)
end

#put_pack(template, *d) ⇒ Object



1466
1467
1468
# File 'lib/resolv.rb', line 1466

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

#put_string(d) ⇒ Object



1479
1480
1481
1482
# File 'lib/resolv.rb', line 1479

def put_string(d)
  self.put_pack("C", d.length)
  @data << d
end

#put_string_list(ds) ⇒ Object



1484
1485
1486
1487
1488
# File 'lib/resolv.rb', line 1484

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

#to_sObject



1458
1459
1460
# File 'lib/resolv.rb', line 1458

def to_s
  return @data
end