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

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

Overview

:nodoc: used to implement Message.encode

Instance Method Summary collapse

Constructor Details

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

Returns a new instance of MessageEncoder.

Yields:

  • (_self)

Yield Parameters:



1249
1250
1251
1252
1253
# File 'lib/net/dns/resolv.rb', line 1249

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

Instance Method Details

#put_bytes(d) ⇒ Object



1259
1260
1261
# File 'lib/net/dns/resolv.rb', line 1259

def put_bytes(d)
  @data << d
end

#put_label(d) ⇒ Object



1306
1307
1308
# File 'lib/net/dns/resolv.rb', line 1306

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

#put_labels(d) ⇒ Object



1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
# File 'lib/net/dns/resolv.rb', line 1292

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

#put_length16Object



1267
1268
1269
1270
1271
1272
1273
1274
# File 'lib/net/dns/resolv.rb', line 1267

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



1288
1289
1290
# File 'lib/net/dns/resolv.rb', line 1288

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

#put_pack(template, *d) ⇒ Object



1263
1264
1265
# File 'lib/net/dns/resolv.rb', line 1263

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

#put_string(d) ⇒ Object

Raises:

  • (ArgumentError)


1276
1277
1278
1279
1280
# File 'lib/net/dns/resolv.rb', line 1276

def put_string(d)
  raise ArgumentError, "strings longer than 255 characters cannot be encoded" if d.length > 255
  self.put_pack("C", d.length)
  @data << d
end

#put_string_list(ds) ⇒ Object



1282
1283
1284
1285
1286
# File 'lib/net/dns/resolv.rb', line 1282

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

#to_sObject



1255
1256
1257
# File 'lib/net/dns/resolv.rb', line 1255

def to_s
  return @data
end