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

Instance Method Summary collapse

Constructor Details

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

Returns a new instance of Message.



1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
# File 'lib/resolv.rb', line 1422

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.



1437
1438
1439
# File 'lib/resolv.rb', line 1437

def aa
  @aa
end

#additionalObject (readonly)

Returns the value of attribute additional.



1438
1439
1440
# File 'lib/resolv.rb', line 1438

def additional
  @additional
end

#answerObject (readonly)

Returns the value of attribute answer.



1438
1439
1440
# File 'lib/resolv.rb', line 1438

def answer
  @answer
end

#authorityObject (readonly)

Returns the value of attribute authority.



1438
1439
1440
# File 'lib/resolv.rb', line 1438

def authority
  @authority
end

#idObject

Returns the value of attribute id.



1437
1438
1439
# File 'lib/resolv.rb', line 1437

def id
  @id
end

#opcodeObject

Returns the value of attribute opcode.



1437
1438
1439
# File 'lib/resolv.rb', line 1437

def opcode
  @opcode
end

#qrObject

Returns the value of attribute qr.



1437
1438
1439
# File 'lib/resolv.rb', line 1437

def qr
  @qr
end

#questionObject (readonly)

Returns the value of attribute question.



1438
1439
1440
# File 'lib/resolv.rb', line 1438

def question
  @question
end

#raObject

Returns the value of attribute ra.



1437
1438
1439
# File 'lib/resolv.rb', line 1437

def ra
  @ra
end

#rcodeObject

Returns the value of attribute rcode.



1437
1438
1439
# File 'lib/resolv.rb', line 1437

def rcode
  @rcode
end

#rdObject

Returns the value of attribute rd.



1437
1438
1439
# File 'lib/resolv.rb', line 1437

def rd
  @rd
end

#tcObject

Returns the value of attribute tc.



1437
1438
1439
# File 'lib/resolv.rb', line 1437

def tc
  @tc
end

Instance Method Details

#==(other) ⇒ Object



1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
# File 'lib/resolv.rb', line 1440

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_equal?(other.question) &&
         @answer == other.answer &&
         @authority == other.authority &&
         @additional == other.additional
end

#add_additional(name, ttl, data) ⇒ Object



1497
1498
1499
# File 'lib/resolv.rb', line 1497

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

#add_answer(name, ttl, data) ⇒ Object



1477
1478
1479
# File 'lib/resolv.rb', line 1477

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

#add_authority(name, ttl, data) ⇒ Object



1487
1488
1489
# File 'lib/resolv.rb', line 1487

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

#add_question(name, typeclass) ⇒ Object



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

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

#each_additionalObject



1501
1502
1503
1504
1505
# File 'lib/resolv.rb', line 1501

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

#each_answerObject



1481
1482
1483
1484
1485
# File 'lib/resolv.rb', line 1481

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

#each_authorityObject



1491
1492
1493
1494
1495
# File 'lib/resolv.rb', line 1491

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

#each_questionObject



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

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

#each_resourceObject



1507
1508
1509
1510
1511
# File 'lib/resolv.rb', line 1507

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



1513
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
# File 'lib/resolv.rb', line 1513

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