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
The Authenticated Data flag 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.
-
#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.
-
#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_header_rcode ⇒ Object
This new get_header_rcode method is intended for use only by the Message class.
-
#initialize(*args) ⇒ Header
constructor
A new instance of Header.
- #rcode=(rcode) ⇒ Object
- #to_s ⇒ Object
- #to_s_with_rcode(rcode) ⇒ Object
Constructor Details
#initialize(*args) ⇒ Header
Returns a new instance of Header.
731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 |
# File 'lib/Dnsruby/message.rb', line 731 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
683 684 685 |
# File 'lib/Dnsruby/message.rb', line 683 def aa @aa end |
#ad ⇒ Object
The Authenticated Data flag 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.)
699 700 701 |
# File 'lib/Dnsruby/message.rb', line 699 def ad @ad end |
#ancount ⇒ Object Also known as: prcount
The number of records in the answer section of the message
727 728 729 |
# File 'lib/Dnsruby/message.rb', line 727 def ancount @ancount end |
#arcount ⇒ Object Also known as: adcount
The number of records in the additional record section og the message
729 730 731 |
# File 'lib/Dnsruby/message.rb', line 729 def arcount @arcount end |
#cd ⇒ Object
The Checking Disabled flag
692 693 694 |
# File 'lib/Dnsruby/message.rb', line 692 def cd @cd end |
#id ⇒ Object
The header ID
677 678 679 |
# File 'lib/Dnsruby/message.rb', line 677 def id @id end |
#nscount ⇒ Object Also known as: upcount
The number of records in the authoriy section of the message
725 726 727 |
# File 'lib/Dnsruby/message.rb', line 725 def nscount @nscount end |
#opcode ⇒ Object
The header opcode
720 721 722 |
# File 'lib/Dnsruby/message.rb', line 720 def opcode @opcode end |
#qdcount ⇒ Object Also known as: zocount
The number of records in the question section of the message
723 724 725 |
# File 'lib/Dnsruby/message.rb', line 723 def qdcount @qdcount end |
#qr ⇒ Object
The query response flag
680 681 682 |
# File 'lib/Dnsruby/message.rb', line 680 def qr @qr end |
#ra ⇒ Object
Recursion available flag
705 706 707 |
# File 'lib/Dnsruby/message.rb', line 705 def ra @ra end |
#rd ⇒ Object
Recursion Desired flag
689 690 691 |
# File 'lib/Dnsruby/message.rb', line 689 def rd @rd end |
#tc ⇒ Object
Truncated flag
686 687 688 |
# File 'lib/Dnsruby/message.rb', line 686 def tc @tc end |
Class Method Details
.decrement_arcount_encoded(bytes) ⇒ Object
791 792 793 794 795 796 797 798 799 800 801 802 |
# File 'lib/Dnsruby/message.rb', line 791 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
760 761 762 763 764 765 |
# File 'lib/Dnsruby/message.rb', line 760 def Header.new_from_data(data) header = Header.new MessageDecoder.new(data) {|msg| header.decode(msg)} return header end |
Instance Method Details
#==(other) ⇒ Object
804 805 806 807 808 809 810 811 812 813 814 |
# File 'lib/Dnsruby/message.rb', line 804 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.get_header_rcode end |
#data ⇒ Object
767 768 769 770 771 |
# File 'lib/Dnsruby/message.rb', line 767 def data return MessageEncoder.new {|msg| self.encode(msg) }.to_s end |
#decode(msg) ⇒ Object
853 854 855 856 857 858 859 860 861 862 863 864 865 |
# File 'lib/Dnsruby/message.rb', line 853 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
773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 |
# File 'lib/Dnsruby/message.rb', line 773 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_header_rcode ⇒ Object
This new get_header_rcode method is intended for use only by the Message class. This is because the Message OPT section may contain an extended rcode (see RFC 2671 section 4.6). Using the header rcode only ignores this extension, and is not recommended.
715 716 717 |
# File 'lib/Dnsruby/message.rb', line 715 def get_header_rcode @rcode end |
#rcode=(rcode) ⇒ Object
756 757 758 |
# File 'lib/Dnsruby/message.rb', line 756 def rcode=(rcode) @rcode = RCode.new(rcode) end |
#to_s ⇒ Object
816 817 818 |
# File 'lib/Dnsruby/message.rb', line 816 def to_s to_s_with_rcode(@rcode) end |
#to_s_with_rcode(rcode) ⇒ Object
820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 |
# File 'lib/Dnsruby/message.rb', line 820 def to_s_with_rcode(rcode) 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 |