Class: Dnsruby::Header
- Inherits:
-
Object
- Object
- Dnsruby::Header
- Defined in:
- lib/Dnsruby/message.rb
Overview
The header portion of a DNS packet
RFC 1035 Section 4.1.1
Constant Summary collapse
- MAX_ID =
65535
Instance Attribute Summary collapse
-
#aa ⇒ Object
Authoritative answer flag.
-
#ad ⇒ Object
Relevant in DNSSEC context.
-
#ancount ⇒ Object
(also: #prcount)
The number of records in the answer section of the message.
-
#arcount ⇒ Object
(also: #adcount)
The number of records in the additional record section og the message.
-
#cd ⇒ Object
The checking disabled flag.
-
#dnssec_ok ⇒ Object
The DO (dnssec OK) flag.
-
#id ⇒ Object
The header ID.
-
#nscount ⇒ Object
(also: #upcount)
The number of records in the authoriy section of the message.
-
#opcode ⇒ Object
The header opcode.
-
#qdcount ⇒ Object
(also: #zocount)
The number of records in the question section of the message.
-
#qr ⇒ Object
The query response flag.
-
#ra ⇒ Object
Recursion available flag.
-
#rcode ⇒ Object
Query response code.
-
#rd ⇒ Object
Recursion Desired flag.
-
#tc ⇒ Object
Truncated flag.
Class Method Summary collapse
Instance Method Summary collapse
- #==(other) ⇒ Object
- #data ⇒ Object
- #decode(msg) ⇒ Object
- #encode(msg) ⇒ Object
- #get_exception ⇒ Object
-
#initialize(*args) ⇒ Header
constructor
A new instance of Header.
- #to_s ⇒ Object
Constructor Details
#initialize(*args) ⇒ Header
Returns a new instance of Header.
449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 |
# File 'lib/Dnsruby/message.rb', line 449 def initialize(*args) if (args.length == 0) @id = rand(MAX_ID) @qr = false @opcode=OpCode.Query @aa = false @ad=false @tc = false @rd = false # recursion desired @ra = false # recursion available @cd=false @rcode=RCode.NoError @qdcount = 0 @nscount = 0 @ancount = 0 @arcount = 0 elsif (args.length == 1) decode(args[0]) end end |
Instance Attribute Details
#aa ⇒ Object
Authoritative answer flag
407 408 409 |
# File 'lib/Dnsruby/message.rb', line 407 def aa @aa end |
#ad ⇒ Object
Relevant in DNSSEC context.
(The AD bit is only set on answers where signatures have been cryptographically verified or the server is authoritative for the data and is allowed to set the bit by policy.)
423 424 425 |
# File 'lib/Dnsruby/message.rb', line 423 def ad @ad end |
#ancount ⇒ Object Also known as: prcount
The number of records in the answer section of the message
445 446 447 |
# File 'lib/Dnsruby/message.rb', line 445 def ancount @ancount end |
#arcount ⇒ Object Also known as: adcount
The number of records in the additional record section og the message
447 448 449 |
# File 'lib/Dnsruby/message.rb', line 447 def arcount @arcount end |
#cd ⇒ Object
The checking disabled flag
416 417 418 |
# File 'lib/Dnsruby/message.rb', line 416 def cd @cd end |
#dnssec_ok ⇒ Object
The DO (dnssec OK) flag
426 427 428 |
# File 'lib/Dnsruby/message.rb', line 426 def dnssec_ok @dnssec_ok end |
#id ⇒ Object
The header ID
401 402 403 |
# File 'lib/Dnsruby/message.rb', line 401 def id @id end |
#nscount ⇒ Object Also known as: upcount
The number of records in the authoriy section of the message
443 444 445 |
# File 'lib/Dnsruby/message.rb', line 443 def nscount @nscount end |
#opcode ⇒ Object
The header opcode
438 439 440 |
# File 'lib/Dnsruby/message.rb', line 438 def opcode @opcode end |
#qdcount ⇒ Object Also known as: zocount
The number of records in the question section of the message
441 442 443 |
# File 'lib/Dnsruby/message.rb', line 441 def qdcount @qdcount end |
#qr ⇒ Object
The query response flag
404 405 406 |
# File 'lib/Dnsruby/message.rb', line 404 def qr @qr end |
#ra ⇒ Object
Recursion available flag
432 433 434 |
# File 'lib/Dnsruby/message.rb', line 432 def ra @ra end |
#rcode ⇒ Object
Query response code
435 436 437 |
# File 'lib/Dnsruby/message.rb', line 435 def rcode @rcode end |
#rd ⇒ Object
Recursion Desired flag
413 414 415 |
# File 'lib/Dnsruby/message.rb', line 413 def rd @rd end |
#tc ⇒ Object
Truncated flag
410 411 412 |
# File 'lib/Dnsruby/message.rb', line 410 def tc @tc end |
Class Method Details
.decrement_arcount_encoded(bytes) ⇒ Object
509 510 511 512 513 514 515 516 517 518 519 520 |
# File 'lib/Dnsruby/message.rb', line 509 def Header.decrement_arcount_encoded(bytes) header = Header.new header_end = 0 MessageDecoder.new(bytes) {|msg| header.decode(msg) header_end = msg.index } header.arcount = header.arcount - 1 bytes[0,header_end]=MessageEncoder.new {|msg| header.encode(msg)}.to_s return bytes end |
.new_from_data(data) ⇒ Object
478 479 480 481 482 483 |
# File 'lib/Dnsruby/message.rb', line 478 def Header.new_from_data(data) header = Header.new MessageDecoder.new(data) {|msg| header.decode(msg)} return header end |
Instance Method Details
#==(other) ⇒ Object
522 523 524 525 526 527 528 529 530 531 532 |
# File 'lib/Dnsruby/message.rb', line 522 def ==(other) return @qr == other.qr && @opcode == other.opcode && @aa == other.aa && @tc == other.tc && @rd == other.rd && @ra == other.ra && @cd == other.cd && @ad == other.ad && @rcode == other.rcode end |
#data ⇒ Object
485 486 487 488 489 |
# File 'lib/Dnsruby/message.rb', line 485 def data return MessageEncoder.new {|msg| self.encode(msg) }.to_s end |
#decode(msg) ⇒ Object
567 568 569 570 571 572 573 574 575 576 577 578 579 |
# File 'lib/Dnsruby/message.rb', line 567 def decode(msg) @id, flag, @qdcount, @ancount, @nscount, @arcount = msg.get_unpack('nnnnnn') @qr = (((flag >> 15)&1)==1)?true:false @opcode = OpCode.new((flag >> 11) & 15) @aa = (((flag >> 10)&1)==1)?true:false @tc = (((flag >> 9)&1)==1)?true:false @rd = (((flag >> 8)&1)==1)?true:false @ra = (((flag >> 7)&1)==1)?true:false @ad = (((flag >> 5)&1)==1)?true:false @cd = (((flag >> 4)&1)==1)?true:false @rcode = RCode.new(flag & 15) end |
#encode(msg) ⇒ Object
491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 |
# File 'lib/Dnsruby/message.rb', line 491 def encode(msg) msg.put_pack('nnnnnn', @id, (@qr?1:0) << 15 | (@opcode.code & 15) << 11 | (@aa?1:0) << 10 | (@tc?1:0) << 9 | (@rd?1:0) << 8 | (@ra?1:0) << 7 | (@ad?1:0) << 5 | (@cd?1:0) << 4 | (@rcode.code & 15), @qdcount, @ancount, @nscount, @arcount) end |
#get_exception ⇒ Object
581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 |
# File 'lib/Dnsruby/message.rb', line 581 def get_exception exception = nil if (@rcode==RCode.NXDOMAIN) exception = NXDomain.new elsif (@rcode==RCode.SERVFAIL) exception = ServFail.new elsif (@rcode==RCode.FORMERR) exception = FormErr.new elsif (@rcode==RCode.NOTIMP) exception = NotImp.new elsif (@rcode==RCode.REFUSED) exception = Refused.new end return exception end |
#to_s ⇒ Object
534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 |
# File 'lib/Dnsruby/message.rb', line 534 def to_s retval = ";; id = #{@id}\n"; if (@opcode == OpCode::Update) retval += ";; qr = #{@qr} " +\ "opcode = #{@opcode.string} "+\ "rcode = #{@rcode.string}\n"; retval += ";; zocount = #{@qdcount} "+\ "prcount = #{@ancount} " +\ "upcount = #{@nscount} " +\ "adcount = #{@arcount}\n"; else retval += ";; qr = #{@qr} " +\ "opcode = #{@opcode.string} " +\ "aa = #{@aa} " +\ "tc = #{@tc} " +\ "rd = #{@rd}\n"; retval += ";; ra = #{@ra} " +\ "ad = #{@ad} " +\ "cd = #{@cd} " +\ "rcode = #{@rcode.string}\n"; retval += ";; qdcount = #{@qdcount} " +\ "ancount = #{@ancount} " +\ "nscount = #{@nscount} " +\ "arcount = #{@arcount}\n"; end return retval; end |