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.


1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
# File 'lib/resolv.rb', line 1340

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


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

def aa
  @aa
end

#additionalObject (readonly)

Returns the value of attribute additional


1356
1357
1358
# File 'lib/resolv.rb', line 1356

def additional
  @additional
end

#answerObject (readonly)

Returns the value of attribute answer


1356
1357
1358
# File 'lib/resolv.rb', line 1356

def answer
  @answer
end

#authorityObject (readonly)

Returns the value of attribute authority


1356
1357
1358
# File 'lib/resolv.rb', line 1356

def authority
  @authority
end

#idObject

Returns the value of attribute id


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

def id
  @id
end

#opcodeObject

Returns the value of attribute opcode


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

def opcode
  @opcode
end

#qrObject

Returns the value of attribute qr


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

def qr
  @qr
end

#questionObject (readonly)

Returns the value of attribute question


1356
1357
1358
# File 'lib/resolv.rb', line 1356

def question
  @question
end

#raObject

Returns the value of attribute ra


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

def ra
  @ra
end

#rcodeObject

Returns the value of attribute rcode


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

def rcode
  @rcode
end

#rdObject

Returns the value of attribute rd


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

def rd
  @rd
end

#tcObject

Returns the value of attribute tc


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

def tc
  @tc
end

Class Method Details

.decode(m) ⇒ Object


1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
# File 'lib/resolv.rb', line 1514

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


1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
# File 'lib/resolv.rb', line 1358

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


1403
1404
1405
# File 'lib/resolv.rb', line 1403

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

#add_answer(name, ttl, data) ⇒ Object


1383
1384
1385
# File 'lib/resolv.rb', line 1383

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

#add_authority(name, ttl, data) ⇒ Object


1393
1394
1395
# File 'lib/resolv.rb', line 1393

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

#add_question(name, typeclass) ⇒ Object


1373
1374
1375
# File 'lib/resolv.rb', line 1373

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

#each_additionalObject


1407
1408
1409
1410
1411
# File 'lib/resolv.rb', line 1407

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

#each_answerObject


1387
1388
1389
1390
1391
# File 'lib/resolv.rb', line 1387

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

#each_authorityObject


1397
1398
1399
1400
1401
# File 'lib/resolv.rb', line 1397

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

#each_questionObject


1377
1378
1379
1380
1381
# File 'lib/resolv.rb', line 1377

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

#each_resourceObject


1413
1414
1415
1416
1417
# File 'lib/resolv.rb', line 1413

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


1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
# File 'lib/resolv.rb', line 1419

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