Class: Resolv::DNS::Message

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

Overview

:nodoc:

Defined Under Namespace

Classes: MessageDecoder, MessageEncoder

Constant Summary collapse

@@identifier =
-1

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(id = (@@identifier += 1) & 0xffff) ⇒ Message

Returns a new instance of Message.



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

def initialize(id = (@@identifier += 1) & 0xffff)
  @id = id
  @qr = 0
  @opcode = 0
  @aa = 0
  @tc = 0
  @rd = 0 # recursion desired
  @ra = 0 # recursion available
  @rcode = 0
  @question = []
  @answer = []
  @authority = []
  @additional = []
end

Instance Attribute Details

#aaObject

Returns the value of attribute aa



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

def aa
  @aa
end

#additionalObject (readonly)

Returns the value of attribute additional



1307
1308
1309
# File 'lib/resolv.rb', line 1307

def additional
  @additional
end

#answerObject (readonly)

Returns the value of attribute answer



1307
1308
1309
# File 'lib/resolv.rb', line 1307

def answer
  @answer
end

#authorityObject (readonly)

Returns the value of attribute authority



1307
1308
1309
# File 'lib/resolv.rb', line 1307

def authority
  @authority
end

#idObject

Returns the value of attribute id



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

def id
  @id
end

#opcodeObject

Returns the value of attribute opcode



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

def opcode
  @opcode
end

#qrObject

Returns the value of attribute qr



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

def qr
  @qr
end

#questionObject (readonly)

Returns the value of attribute question



1307
1308
1309
# File 'lib/resolv.rb', line 1307

def question
  @question
end

#raObject

Returns the value of attribute ra



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

def ra
  @ra
end

#rcodeObject

Returns the value of attribute rcode



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

def rcode
  @rcode
end

#rdObject

Returns the value of attribute rd



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

def rd
  @rd
end

#tcObject

Returns the value of attribute tc



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

def tc
  @tc
end

Class Method Details

.decode(m) ⇒ Object



1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
# File 'lib/resolv.rb', line 1463

def Message.decode(m)
  o = Message.new(0)
  MessageDecoder.new(m) {|msg|
    id, flag, qdcount, ancount, nscount, arcount =
      msg.get_unpack('nnnnnn')
    o.id = id
    o.qr = (flag >> 15) & 1
    o.opcode = (flag >> 11) & 15
    o.aa = (flag >> 10) & 1
    o.tc = (flag >> 9) & 1
    o.rd = (flag >> 8) & 1
    o.ra = (flag >> 7) & 1
    o.rcode = flag & 15
    (1..qdcount).each {
      name, typeclass = msg.get_question
      o.add_question(name, typeclass)
    }
    (1..ancount).each {
      name, ttl, data = msg.get_rr
      o.add_answer(name, ttl, data)
    }
    (1..nscount).each {
      name, ttl, data = msg.get_rr
      o.add_authority(name, ttl, data)
    }
    (1..arcount).each {
      name, ttl, data = msg.get_rr
      o.add_additional(name, ttl, data)
    }
  }
  return o
end

Instance Method Details

#==(other) ⇒ Object



1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
# File 'lib/resolv.rb', line 1309

def ==(other)
  return @id == other.id &&
         @qr == other.qr &&
         @opcode == other.opcode &&
         @aa == other.aa &&
         @tc == other.tc &&
         @rd == other.rd &&
         @ra == other.ra &&
         @rcode == other.rcode &&
         @question == other.question &&
         @answer == other.answer &&
         @authority == other.authority &&
         @additional == other.additional
end

#add_additional(name, ttl, data) ⇒ Object



1354
1355
1356
# File 'lib/resolv.rb', line 1354

def add_additional(name, ttl, data)
  @additional << [Name.create(name), ttl, data]
end

#add_answer(name, ttl, data) ⇒ Object



1334
1335
1336
# File 'lib/resolv.rb', line 1334

def add_answer(name, ttl, data)
  @answer << [Name.create(name), ttl, data]
end

#add_authority(name, ttl, data) ⇒ Object



1344
1345
1346
# File 'lib/resolv.rb', line 1344

def add_authority(name, ttl, data)
  @authority << [Name.create(name), ttl, data]
end

#add_question(name, typeclass) ⇒ Object



1324
1325
1326
# File 'lib/resolv.rb', line 1324

def add_question(name, typeclass)
  @question << [Name.create(name), typeclass]
end

#each_additionalObject



1358
1359
1360
1361
1362
# File 'lib/resolv.rb', line 1358

def each_additional
  @additional.each {|name, ttl, data|
    yield name, ttl, data
  }
end

#each_answerObject



1338
1339
1340
1341
1342
# File 'lib/resolv.rb', line 1338

def each_answer
  @answer.each {|name, ttl, data|
    yield name, ttl, data
  }
end

#each_authorityObject



1348
1349
1350
1351
1352
# File 'lib/resolv.rb', line 1348

def each_authority
  @authority.each {|name, ttl, data|
    yield name, ttl, data
  }
end

#each_questionObject



1328
1329
1330
1331
1332
# File 'lib/resolv.rb', line 1328

def each_question
  @question.each {|name, typeclass|
    yield name, typeclass
  }
end

#each_resourceObject



1364
1365
1366
1367
1368
# File 'lib/resolv.rb', line 1364

def each_resource
  each_answer {|name, ttl, data| yield name, ttl, data}
  each_authority {|name, ttl, data| yield name, ttl, data}
  each_additional {|name, ttl, data| yield name, ttl, data}
end

#encodeObject



1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
# File 'lib/resolv.rb', line 1370

def encode
  return MessageEncoder.new {|msg|
    msg.put_pack('nnnnnn',
      @id,
      (@qr & 1) << 15 |
      (@opcode & 15) << 11 |
      (@aa & 1) << 10 |
      (@tc & 1) << 9 |
      (@rd & 1) << 8 |
      (@ra & 1) << 7 |
      (@rcode & 15),
      @question.length,
      @answer.length,
      @authority.length,
      @additional.length)
    @question.each {|q|
      name, typeclass = q
      msg.put_name(name)
      msg.put_pack('nn', typeclass::TypeValue, typeclass::ClassValue)
    }
    [@answer, @authority, @additional].each {|rr|
      rr.each {|r|
        name, ttl, data = r
        msg.put_name(name)
        msg.put_pack('nnN', data.class::TypeValue, data.class::ClassValue, ttl)
        msg.put_length16 {data.encode_rdata(msg)}
      }
    }
  }.to_s
end