Class: Net::DNS::Header
- Inherits:
-
Object
- Object
- Net::DNS::Header
- Defined in:
- lib/Net/DNS/Header.rb
Overview
NAME
Net::DNS::Header - DNS packet header class
DESCRIPTION
A Net::DNS::Header object represents the header portion of a DNS packet.
COPYRIGHT
Copyright © 1997-2002 Michael Fuhr.
Portions Copyright © 2002-2004 Chris Reinhardt.
Portions Copyright © 2006 AlexD (Nominet UK)
All rights reserved. This program is free software; you may redistribute it and/or modify it under the same terms as Perl itself.
SEE ALSO
Net::DNS, Net::DNS::Resolver, Net::DNS::Packet, Net::DNS::Update, Net::DNS::Question, Net::DNS::RR, RFC 1035 Section 4.1.1
Constant Summary collapse
- MAX_ID =
65535
- @@next_id =
rand(MAX_ID)
Instance Attribute Summary collapse
-
#aa ⇒ Object
print “answer is ”, (header.aa!=0) ? “” : “non-”, “authoritativen” header.aa=(0).
-
#ad ⇒ Object
print “The result has ”, header.ad!=0 ? “” : “not”, “been verifiedn”.
-
#ancount ⇒ Object
(also: #prcount)
print “# of answer records: ”, header.ancount, “n” header.ancount=(5).
-
#arcount ⇒ Object
(also: #adcount)
print “# of additional records: ”, header.arcount, “n” header.arcount=(3).
-
#cd ⇒ Object
print “checking was ”, header.cd!=0 ? “not” : “”, “desiredn” header.cd=(0).
-
#id ⇒ Object
The query identification number.
-
#nscount ⇒ Object
(also: #upcount)
print “# of authority records: ”, header.nscount, “n” header.nscount=(2).
-
#opcode ⇒ Object
print “query opcode = ”, header.opcode, “n” header.opcode(“UPDATE”).
-
#qdcount ⇒ Object
(also: #zocount)
print “# of question records: ”, header.qdcount, “n” header.qdcount=(2).
-
#qr ⇒ Object
Gets or sets the query response flag.
-
#ra ⇒ Object
print “recursion is ”, header.ra!=0 ? “” : “not ”, “availablen” header.ra=(0).
-
#rcode ⇒ Object
print “query response code = ”, header.rcode, “n” header.rcode=(“SERVFAIL”).
-
#rd ⇒ Object
print “recursion was ”, header.rd!=0 ? “” : “not ”, “desiredn” header.rd=(0).
-
#tc ⇒ Object
print “packet is ”, header.tc!=0 ? “” : “not ”, “truncatedn” header.tc=(0).
Instance Method Summary collapse
- #==(other) ⇒ Object
-
#data ⇒ Object
Returns the header data in binary format, appropriate for use in a DNS query packet.
-
#initialize(*args) ⇒ Header
constructor
header = Net::DNS::Header.new header = Net::DNS::Header.new(data).
-
#inspect ⇒ Object
Returns a string representation of the header object.
-
#nextid ⇒ Object
Get the next Header ID.
Constructor Details
#initialize(*args) ⇒ Header
header = Net::DNS::Header.new
header = Net::DNS::Header.new(data)
Without an argument, new creates a header object appropriate for making a DNS query.
If new is passed a reference to a scalar containing DNS packet data, it creates a header object from that data.
Returns nil if unable to create a header object (e.g., if the data is incomplete).
168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 |
# File 'lib/Net/DNS/Header.rb', line 168 def initialize(*args) @qr = 0 @opcode = 0 @aa = 0 @tc = 0 @rd = 1 @ra = 0 @ad = 0 @cd = 0 @rcode = 0 @qdcount = 0 @ancount = 0 @nscount = 0 @arcount = 0 if (args != nil && args.length > 0) data = args[0]; if (data) if (data.length < Net::DNS::HFIXEDSZ ) return nil; end a = data.unpack("n C2 n4"); @id = a[0] @qr = (a[1] >> 7) & 0x1 @opcode = (a[1] >> 3) & 0xf @aa = (a[1] >> 2) & 0x1 @tc = (a[1] >> 1) & 0x1 @rd = a[1] & 0x1 @ra = (a[2] >> 7) & 0x1 @ad = (a[2] >> 5) & 0x1 @cd = (a[2] >> 4) & 0x1 @rcode = a[2] & 0xf @qdcount = a[3] @ancount = a[4] @nscount = a[5] @arcount = a[6] else @id = nextid() end else @id = nextid() end hasKey = Net::DNS::Opcodesbyval.has_key?@opcode temp = Net::DNS::Opcodesbyval[@opcode] temp2 = Net::DNS::Opcodesbyval if (Net::DNS::Opcodesbyval[@opcode] != nil) @opcode = Net::DNS::Opcodesbyval[@opcode] end if (Net::DNS::Rcodesbyval[@rcode]!=nil) @rcode = Net::DNS::Rcodesbyval[@rcode] end end |
Instance Attribute Details
#aa ⇒ Object
print “answer is ”, (header.aa!=0) ? “” : “non-”, “authoritativen”
header.aa=(0)
Gets or sets the authoritative answer flag.
61 62 63 |
# File 'lib/Net/DNS/Header.rb', line 61 def aa @aa end |
#ad ⇒ Object
print “The result has ”, header.ad!=0 ? “” : “not”, “been verifiedn”
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.)
94 95 96 |
# File 'lib/Net/DNS/Header.rb', line 94 def ad @ad end |
#ancount ⇒ Object Also known as: prcount
print “# of answer records: ”, header.ancount, “n”
header.ancount=(5)
Gets or sets the number of records in the answer section of the packet. In dynamic update packets, this field is known as prcount and refers to the number of RRs in the prerequisite section.
116 117 118 |
# File 'lib/Net/DNS/Header.rb', line 116 def ancount @ancount end |
#arcount ⇒ Object Also known as: adcount
print “# of additional records: ”, header.arcount, “n”
header.arcount=(3)
Gets or sets the number of records in the additional section of the packet. In dynamic update packets, this field is known as adcount.
131 132 133 |
# File 'lib/Net/DNS/Header.rb', line 131 def arcount @arcount end |
#cd ⇒ Object
print “checking was ”, header.cd!=0 ? “not” : “”, “desiredn”
header.cd=(0)
Gets or sets the checking disabled flag.
79 80 81 |
# File 'lib/Net/DNS/Header.rb', line 79 def cd @cd end |
#id ⇒ Object
The query identification number.
46 47 48 |
# File 'lib/Net/DNS/Header.rb', line 46 def id @id end |
#nscount ⇒ Object Also known as: upcount
print “# of authority records: ”, header.nscount, “n”
header.nscount=(2)
Gets or sets the number of records in the authority section of the packet. In dynamic update packets, this field is known as upcount and refers to the number of RRs in the update section.
124 125 126 |
# File 'lib/Net/DNS/Header.rb', line 124 def nscount @nscount end |
#opcode ⇒ Object
print “query opcode = ”, header.opcode, “n”
header.opcode("UPDATE")
Gets or sets the query opcode (the purpose of the query).
55 56 57 |
# File 'lib/Net/DNS/Header.rb', line 55 def opcode @opcode end |
#qdcount ⇒ Object Also known as: zocount
print “# of question records: ”, header.qdcount, “n”
header.qdcount=(2)
Gets or sets the number of records in the question section of the packet. In dynamic update packets, this field is known as zocount and refers to the number of RRs in the zone section.
108 109 110 |
# File 'lib/Net/DNS/Header.rb', line 108 def qdcount @qdcount end |
#qr ⇒ Object
Gets or sets the query response flag.
49 50 51 |
# File 'lib/Net/DNS/Header.rb', line 49 def qr @qr end |
#ra ⇒ Object
print “recursion is ”, header.ra!=0 ? “” : “not ”, “availablen”
header.ra=(0)
Gets or sets the recursion available flag.
85 86 87 |
# File 'lib/Net/DNS/Header.rb', line 85 def ra @ra end |
#rcode ⇒ Object
print “query response code = ”, header.rcode, “n”
header.rcode=("SERVFAIL")
Gets or sets the query response code (the status of the query).
100 101 102 |
# File 'lib/Net/DNS/Header.rb', line 100 def rcode @rcode end |
#rd ⇒ Object
print “recursion was ”, header.rd!=0 ? “” : “not ”, “desiredn”
header.rd=(0)
Gets or sets the recursion desired flag.
73 74 75 |
# File 'lib/Net/DNS/Header.rb', line 73 def rd @rd end |
#tc ⇒ Object
print “packet is ”, header.tc!=0 ? “” : “not ”, “truncatedn”
header.tc=(0)
Gets or sets the truncated packet flag.
67 68 69 |
# File 'lib/Net/DNS/Header.rb', line 67 def tc @tc end |
Instance Method Details
#==(other) ⇒ Object
278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 |
# File 'lib/Net/DNS/Header.rb', line 278 def ==(other) if (other != nil) && (other.is_a?(Header)) return false if @qr!=other.qr; return false if @opcode!=other.opcode; return false if @aa!=other.aa; return false if @tc!=other.tc; return false if @rd!=other.rd; return false if @ra!=other.ra; return false if @ad!=other.ad; return false if @cd!=other.cd; return false if @rcode!=other.rcode; return false if @ancount!=other.ancount; return false if @nscount!=other.nscount; return false if @qdcount!=other.qdcount; return false if @id!=other.id; return false if @arcount!=other.arcount; return true; else return false end end |
#data ⇒ Object
Returns the header data in binary format, appropriate for use in a DNS query packet.
hdata = header.data
267 268 269 270 271 272 273 274 275 276 |
# File 'lib/Net/DNS/Header.rb', line 267 def data opcode = Net::DNS::Opcodesbyname[@opcode]; rcode = Net::DNS::Rcodesbyname[@rcode]; byte2 = (@qr << 7) | (opcode << 3) | (@aa << 2) | (@tc << 1) | @rd; byte3 = (@ra << 7) | (@ad << 5) | (@cd << 4) | rcode; return [@id, byte2, byte3, @qdcount, @ancount, @nscount, @arcount].pack("n C2 n4"); end |
#inspect ⇒ Object
Returns a string representation of the header object.
print header.inspect
229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 |
# File 'lib/Net/DNS/Header.rb', line 229 def inspect retval = ";; id = #{@id}\n"; if (@opcode == "UPDATE") retval += ";; qr = #{@qr} " +\ "opcode = #{@opcode} "+\ "rcode = #{@rcode}\n"; retval += ";; zocount = #{@qdcount} "+\ "prcount = #{@ancount} " +\ "upcount = #{@nscount} " +\ "adcount = #{@arcount}\n"; else retval += ";; qr = #{@qr} " +\ "opcode = #{@opcode} " +\ "aa = #{@aa} " +\ "tc = #{@tc} " +\ "rd = #{@rd}\n"; retval += ";; ra = #{@ra} " +\ "ad = #{@ad} " +\ "cd = #{@cd} " +\ "rcode = #{@rcode}\n"; retval += ";; qdcount = #{@qdcount} " +\ "ancount = #{@ancount} " +\ "nscount = #{@nscount} " +\ "arcount = #{@arcount}\n"; end return retval; end |
#nextid ⇒ Object
Get the next Header ID
149 150 151 152 153 154 155 |
# File 'lib/Net/DNS/Header.rb', line 149 def nextid() @@next_id += 1 if (@@next_id > MAX_ID) @@next_id = 0 end return @@next_id end |