Class: Dnsruby::Message
- Inherits:
-
Object
- Object
- Dnsruby::Message
- Defined in:
- lib/Dnsruby/message.rb
Overview
Defines a DNS packet.
RFC 1035 Section 4.1, RFC 2136 Section 2, RFC 2845
Sections
Message objects have five sections:
-
The header section, a Dnsruby::Header object.
msg.header=Header.new(...) header = msg.header -
The question section, an array of Dnsruby::Question objects.
msg.add_question(Question.new(domain, type, klass)) msg.each_question do |question| .... end -
The answer section, an array of Dnsruby::RR objects.
msg.add_answer(RR.create({:name => "a2.example.com",
:type => “A”, :address => “10.0.0.2”}))
msg.each_answer {|answer| ... }
-
The authority section, an array of Dnsruby::RR objects.
msg.add_authority(rr) msg.each_authority {|rr| ... } -
The additional section, an array of Dnsruby::RR objects.
msg.add_additional(rr) msg.each_additional {|rr| ... }
In addition, each_resource iterates the answer, additional and authority sections :
msg.each_resource {|rr| ... }
Packet format encoding
Dnsruby::Message#encode
Dnsruby::Message::decode(data)
Additional information
security_level records the current DNSSEC status of this Message. answerfrom records the server which this Message was received from. cached records whether this response came from the cache.
Direct Known Subclasses
Defined Under Namespace
Classes: Section, SecurityLevel
Instance Attribute Summary collapse
-
#additional ⇒ Object
readonly
The additional section, an array of Dnsruby::RR objects.
-
#answer ⇒ Object
(also: #pre)
readonly
The answer section, an array of Dnsruby::RR objects.
-
#answerfrom ⇒ Object
If this Message is a response from a server, then answerfrom contains the address of the server.
-
#answerip ⇒ Object
If this Message is a response from a server, then answerfrom contains the IP address of the server.
-
#answersize ⇒ Object
If this Message is a response from a server, then answersize contains the size of the response.
-
#authority ⇒ Object
(also: #update)
readonly
The authority section, an array of Dnsruby::RR objects.
-
#cached ⇒ Object
If the Message was returned from the cache, the cached flag will be set true.
-
#do_caching ⇒ Object
do_caching is set by default.
-
#do_validation ⇒ Object
do_validation is set by default.
-
#header ⇒ Object
The header section, a Dnsruby::Header object.
-
#question ⇒ Object
(also: #zone)
readonly
The question section, an array of Dnsruby::Question objects.
-
#security_error ⇒ Object
If there was a problem verifying this message with DNSSEC, then securiy_error will hold a description of the problem.
-
#security_level ⇒ Object
If dnssec is set on, then each message will have the security level set To find the precise error (if any), call Dnsruby::Dnssec::validate(msg) - the resultant exception will define the error.
-
#send_raw ⇒ Object
Set send_raw if you wish to send and receive the response to this Message with no additional processing.
-
#tsigerror ⇒ Object
If this message has been verified using a TSIG RR then tsigerror contains the error code returned by the TSIG verification.
-
#tsigstart ⇒ Object
–.
-
#tsigstate ⇒ Object
Can be * :Unsigned - the default state * :Signed - the outgoing message has been signed * :Verified - the incoming message has been verified by TSIG * :Intermediate - the incoming message is an intermediate envelope in a TCP session in which only every 100th envelope must be signed * :Failed - the incoming response failed verification.
Class Method Summary collapse
-
.decode(m) ⇒ Object
Decode the encoded message.
Instance Method Summary collapse
- #==(other) ⇒ Object
-
#add_additional(rr) ⇒ Object
:nodoc: all.
-
#add_answer(rr) ⇒ Object
(also: #add_pre)
:nodoc: all.
-
#add_authority(rr) ⇒ Object
(also: #add_update)
:nodoc: all.
-
#add_question(question, type = Types.A, klass = Classes.IN) ⇒ Object
(also: #add_zone)
Add a new Question to the Message.
- #each_additional ⇒ Object
- #each_answer ⇒ Object (also: #each_pre)
- #each_authority ⇒ Object (also: #each_update)
- #each_question ⇒ Object (also: #each_zone)
-
#each_resource ⇒ Object
Calls each_answer, each_authority, each_additional.
-
#each_section ⇒ Object
Yields each section (question, answer, authority, additional).
-
#encode ⇒ Object
Return the encoded form of the message If there is a TSIG record present and the record has not been signed then sign it.
- #get_exception ⇒ Object
- #get_opt ⇒ Object
-
#initialize(*args) ⇒ Message
constructor
Create a new Message.
- #rcode ⇒ Object
-
#rrset(name, type, klass = Classes::IN) ⇒ Object
Return the first rrset of the specified attributes in the message.
-
#rrsets(type, klass = Classes::IN) ⇒ Object
Return the rrsets of the specified type in the message.
-
#section_rrsets(type = nil, include_opt = false) ⇒ Object
Return a hash, with the section as key, and the RRSets in that section as the data : => section_rrs.
-
#set_tsig(*args) ⇒ Object
Sets the TSIG to sign this message with.
-
#sign!(*args) ⇒ Object
Signs the message.
-
#signed? ⇒ Boolean
Was this message signed by a TSIG?.
- #to_s ⇒ Object
-
#tsig ⇒ Object
Returns the TSIG record from the ADDITIONAL section, if one is present.
-
#update_counts ⇒ Object
:nodoc:all.
-
#verified? ⇒ Boolean
If this message was signed by a TSIG, was the TSIG verified?.
Constructor Details
#initialize(*args) ⇒ Message
Create a new Message. Takes optional name, type and class
type defaults to A, and klass defaults to IN
-
Dnsruby::Message.new(“example.com”) # defaults to A, IN
-
Dnsruby::Message.new(“example.com”, ‘AAAA’)
-
Dnsruby::Message.new(“example.com”, Dnsruby::Types.PTR, “HS”)
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 |
# File 'lib/Dnsruby/message.rb', line 186 def initialize(*args) @header = Header.new() # @question = Section.new(self) @question = [] @answer = Section.new(self) @authority = Section.new(self) @additional = Section.new(self) @tsigstate = :Unsigned @signing = false @tsigkey = nil @answerfrom = nil @answerip = nil @send_raw = false @do_validation = true @do_caching = true @security_level = SecurityLevel.UNCHECKED @security_error = nil @cached = false type = Types::A klass = Classes::IN if (args.length > 0) name = args[0] if (args.length > 1) type = Types.new(args[1]) if (args.length > 2) klass = Classes.new(args[2]) end end add_question(name, type, klass) end end |
Instance Attribute Details
#additional ⇒ Object (readonly)
The additional section, an array of Dnsruby::RR objects.
226 227 228 |
# File 'lib/Dnsruby/message.rb', line 226 def additional @additional end |
#answer ⇒ Object (readonly) Also known as: pre
The answer section, an array of Dnsruby::RR objects.
222 223 224 |
# File 'lib/Dnsruby/message.rb', line 222 def answer @answer end |
#answerfrom ⇒ Object
If this Message is a response from a server, then answerfrom contains the address of the server
231 232 233 |
# File 'lib/Dnsruby/message.rb', line 231 def answerfrom @answerfrom end |
#answerip ⇒ Object
If this Message is a response from a server, then answerfrom contains the IP address of the server
234 235 236 |
# File 'lib/Dnsruby/message.rb', line 234 def answerip @answerip end |
#answersize ⇒ Object
If this Message is a response from a server, then answersize contains the size of the response
237 238 239 |
# File 'lib/Dnsruby/message.rb', line 237 def answersize @answersize end |
#authority ⇒ Object (readonly) Also known as: update
The authority section, an array of Dnsruby::RR objects.
224 225 226 |
# File 'lib/Dnsruby/message.rb', line 224 def @authority end |
#cached ⇒ Object
If the Message was returned from the cache, the cached flag will be set true. It will be false otherwise.
87 88 89 |
# File 'lib/Dnsruby/message.rb', line 87 def cached @cached end |
#do_caching ⇒ Object
do_caching is set by default. If you do not wish dnsruby to inspect the cache before sending the query, nor cache the result of the query, then set do_caching to false.
272 273 274 |
# File 'lib/Dnsruby/message.rb', line 272 def do_caching @do_caching end |
#do_validation ⇒ Object
do_validation is set by default. If you do not wish dnsruby to validate this message (on a Resolver with @dnssec==true), then set do_validation to false. This option does not affect caching, or the header options
267 268 269 |
# File 'lib/Dnsruby/message.rb', line 267 def do_validation @do_validation end |
#header ⇒ Object
The header section, a Dnsruby::Header object.
228 229 230 |
# File 'lib/Dnsruby/message.rb', line 228 def header @header end |
#question ⇒ Object (readonly) Also known as: zone
The question section, an array of Dnsruby::Question objects.
219 220 221 |
# File 'lib/Dnsruby/message.rb', line 219 def question @question end |
#security_error ⇒ Object
If there was a problem verifying this message with DNSSEC, then securiy_error will hold a description of the problem. It defaults to “”
83 84 85 |
# File 'lib/Dnsruby/message.rb', line 83 def security_error @security_error end |
#security_level ⇒ Object
If dnssec is set on, then each message will have the security level set To find the precise error (if any), call Dnsruby::Dnssec::validate(msg) - the resultant exception will define the error.
80 81 82 |
# File 'lib/Dnsruby/message.rb', line 80 def security_level @security_level end |
#send_raw ⇒ Object
Set send_raw if you wish to send and receive the response to this Message with no additional processing. In other words, if set, then Dnsruby will not touch the Header of the outgoing Message. This option does not affect caching or dnssec validation
This option should not normally be set.
262 263 264 |
# File 'lib/Dnsruby/message.rb', line 262 def send_raw @send_raw end |
#tsigerror ⇒ Object
If this message has been verified using a TSIG RR then tsigerror contains the error code returned by the TSIG verification. The error will be an RCode
241 242 243 |
# File 'lib/Dnsruby/message.rb', line 241 def tsigerror @tsigerror end |
#tsigstart ⇒ Object
–
253 254 255 |
# File 'lib/Dnsruby/message.rb', line 253 def tsigstart @tsigstart end |
#tsigstate ⇒ Object
Can be
-
:Unsigned - the default state
-
:Signed - the outgoing message has been signed
-
:Verified - the incoming message has been verified by TSIG
-
:Intermediate - the incoming message is an intermediate envelope in a TCP session
in which only every 100th envelope must be signed
-
:Failed - the incoming response failed verification
250 251 252 |
# File 'lib/Dnsruby/message.rb', line 250 def tsigstate @tsigstate end |
Class Method Details
.decode(m) ⇒ Object
Decode the encoded message
587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 |
# File 'lib/Dnsruby/message.rb', line 587 def Message.decode(m) o = Message.new() MessageDecoder.new(m) {|msg| o.header = Header.new(msg) o.header.qdcount.times { question = msg.get_question o.question << question } o.header.ancount.times { rr = msg.get_rr o.answer << rr } o.header.nscount.times { rr = msg.get_rr o. << rr } o.header.arcount.times { |count| start = msg.index rr = msg.get_rr if (rr.type == Types::TSIG) if (count!=o.header.arcount-1) Dnsruby.log.Error("Incoming message has TSIG record before last record") raise DecodeError.new("TSIG record present before last record") end o.tsigstart = start # needed for TSIG verification end o.additional << rr } } return o end |
Instance Method Details
#==(other) ⇒ Object
302 303 304 305 306 307 308 309 310 311 312 |
# File 'lib/Dnsruby/message.rb', line 302 def ==(other) ret = false if (other.kind_of?Message) ret = @header == other.header && @question[0] == other.question[0] && @answer == other.answer && @authority == other. && @additional == other.additional end return ret end |
#add_additional(rr) ⇒ Object
:nodoc: all
399 400 401 402 403 404 |
# File 'lib/Dnsruby/message.rb', line 399 def add_additional(rr) #:nodoc: all if (!@additional.include?rr) @additional << rr update_counts end end |
#add_answer(rr) ⇒ Object Also known as: add_pre
:nodoc: all
373 374 375 376 377 378 |
# File 'lib/Dnsruby/message.rb', line 373 def add_answer(rr) #:nodoc: all if (!@answer.include?rr) @answer << rr update_counts end end |
#add_authority(rr) ⇒ Object Also known as: add_update
:nodoc: all
386 387 388 389 390 391 |
# File 'lib/Dnsruby/message.rb', line 386 def (rr) #:nodoc: all if (!@authority.include?rr) @authority << rr update_counts end end |
#add_question(question, type = Types.A, klass = Classes.IN) ⇒ Object Also known as: add_zone
Add a new Question to the Message. Takes either a Question, or a name, and an optional type and class.
-
msg.add_question(Question.new(“example.com”, ‘MX’))
-
msg.add_question(“example.com”) # defaults to Types.A, Classes.IN
-
msg.add_question(“example.com”, Types.LOC)
351 352 353 354 355 356 357 |
# File 'lib/Dnsruby/message.rb', line 351 def add_question(question, type=Types.A, klass=Classes.IN) if (!question.kind_of?Question) question = Question.new(question, type, klass) end @question << question update_counts end |
#each_additional ⇒ Object
406 407 408 409 410 |
# File 'lib/Dnsruby/message.rb', line 406 def each_additional @additional.each {|rec| yield rec } end |
#each_answer ⇒ Object Also known as: each_pre
380 381 382 383 384 |
# File 'lib/Dnsruby/message.rb', line 380 def each_answer @answer.each {|rec| yield rec } end |
#each_authority ⇒ Object Also known as: each_update
393 394 395 396 397 |
# File 'lib/Dnsruby/message.rb', line 393 def @authority.each {|rec| yield rec } end |
#each_question ⇒ Object Also known as: each_zone
359 360 361 362 363 |
# File 'lib/Dnsruby/message.rb', line 359 def each_question @question.each {|rec| yield rec } end |
#each_resource ⇒ Object
Calls each_answer, each_authority, each_additional
418 419 420 421 422 |
# File 'lib/Dnsruby/message.rb', line 418 def each_resource each_answer {|rec| yield rec} {|rec| yield rec} each_additional {|rec| yield rec} end |
#each_section ⇒ Object
Yields each section (question, answer, authority, additional)
413 414 415 |
# File 'lib/Dnsruby/message.rb', line 413 def each_section [@answer, @authority, @additional].each {|section| yield section} end |
#encode ⇒ Object
Return the encoded form of the message If there is a TSIG record present and the record has not been signed then sign it
565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 |
# File 'lib/Dnsruby/message.rb', line 565 def encode if ((@tsigkey) && @tsigstate == :Unsigned && !@signing) @signing = true sign! @signing = false end return MessageEncoder.new {|msg| header = @header header.encode(msg) @question.each {|q| msg.put_name(q.qname) msg.put_pack('nn', q.qtype.code, q.qclass.code) } [@answer, @authority, @additional].each {|rr| rr.each { |r| msg.put_rr(r) } } }.to_s end |
#get_exception ⇒ Object
274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 |
# File 'lib/Dnsruby/message.rb', line 274 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 elsif (rcode==RCode.NOTZONE) exception = NotZone.new elsif (rcode==RCode.NOTAUTH) exception = NotAuth.new elsif (rcode==RCode.NXRRSET) exception = NXRRSet.new elsif (rcode==RCode.YXRRSET) exception = YXRRSet.new elsif (rcode==RCode.YXDOMAIN) exception = YXDomain.new elsif (rcode >= RCode.BADSIG && rcode <= RCode.BADALG) return VerifyError.new # @TODO@ end return exception end |
#get_opt ⇒ Object
465 466 467 468 469 470 471 472 |
# File 'lib/Dnsruby/message.rb', line 465 def get_opt each_additional do |r| if (r.type == Types::OPT) return r end end return nil end |
#rcode ⇒ Object
474 475 476 477 478 479 480 481 482 |
# File 'lib/Dnsruby/message.rb', line 474 def rcode rcode = @header.get_header_rcode opt = get_opt if (opt != nil) rcode = rcode.code + (opt.xrcode.code << 4) rcode = RCode.new(rcode) end return rcode; end |
#rrset(name, type, klass = Classes::IN) ⇒ Object
Return the first rrset of the specified attributes in the message
315 316 317 318 319 320 321 322 |
# File 'lib/Dnsruby/message.rb', line 315 def rrset(name, type, klass = Classes::IN) [@answer, @authority, @additional].each do |section| if ((rrset = section.rrset(name, type, klass)).length > 0) return rrset end end return RRSet.new end |
#rrsets(type, klass = Classes::IN) ⇒ Object
Return the rrsets of the specified type in the message
325 326 327 328 329 330 331 332 333 |
# File 'lib/Dnsruby/message.rb', line 325 def rrsets(type, klass=Classes::IN) rrsets = [] [@answer, @authority, @additional].each do |section| if ((rrset = section.rrsets(type, klass)).length > 0) rrsets.push(rrset) end end return rrsets end |
#section_rrsets(type = nil, include_opt = false) ⇒ Object
Return a hash, with the section as key, and the RRSets in that section as the data : => section_rrs
337 338 339 340 341 342 343 |
# File 'lib/Dnsruby/message.rb', line 337 def section_rrsets(type = nil, include_opt = false) ret = {} ["answer", "authority", "additional"].each do |section| ret[section] = self.send(section).rrsets(type, include_opt) end return ret end |
#set_tsig(*args) ⇒ Object
Sets the TSIG to sign this message with. Can either be a Dnsruby::RR::TSIG object, or it can be a (name, key) tuple, or it can be a hash which takes Dnsruby::RR::TSIG attributes (e.g. name, key, fudge, etc.)
437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 |
# File 'lib/Dnsruby/message.rb', line 437 def set_tsig(*args) if (args.length == 1) if (args[0].instance_of?RR::TSIG) @tsigkey = args[0] elsif (args[0].instance_of?Hash) @tsigkey = RR.create({:type=>'TSIG', :klass=>'ANY'}.merge(args[0])) else raise ArgumentError.new("Wrong type of argument to Dnsruby::Message#set_tsig - should be TSIG or Hash") end elsif (args.length == 2) @tsigkey = RR.create({:type=>'TSIG', :klass=>'ANY', :name=>args[0], :key=>args[1]}) else raise ArgumentError.new("Wrong number of arguments to Dnsruby::Message#set_tsig") end end |
#sign!(*args) ⇒ Object
Signs the message. If used with no arguments, then the message must have already been set (set_tsig). Otherwise, the arguments can either be a Dnsruby::RR::TSIG object, or a (name, key) tuple, or a hash which takes Dnsruby::RR::TSIG attributes (e.g. name, key, fudge, etc.)
NOTE that this method should only be called by the resolver, rather than the client code. To use signing from the client, call Dnsruby::Resolver#tsig=
551 552 553 554 555 556 557 558 559 560 |
# File 'lib/Dnsruby/message.rb', line 551 def sign!(*args) #:nodoc: all if (args.length > 0) set_tsig(*args) sign! else if ((@tsigkey) && @tsigstate == :Unsigned) @tsigkey.apply(self) end end end |
#signed? ⇒ Boolean
Was this message signed by a TSIG?
454 455 456 457 458 |
# File 'lib/Dnsruby/message.rb', line 454 def signed? return (@tsigstate == :Signed || @tsigstate == :Verified || @tsigstate == :Failed) end |
#to_s ⇒ Object
484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 |
# File 'lib/Dnsruby/message.rb', line 484 def to_s retval = ""; if (@answerfrom != nil && @answerfrom != "") retval = retval + ";; Answer received from #{@answerfrom} (#{@answersize} bytes)\n;;\n"; end retval = retval + ";; Security Level : #{@security_level.string}\n" retval = retval + ";; HEADER SECTION\n" # OPT pseudosection? EDNS flags, udpsize opt = get_opt if (!opt) retval = retval + @header.to_s else retval = retval + @header.to_s_with_rcode(rcode()) end retval = retval + "\n" if (opt) retval = retval + opt.to_s retval = retval + "\n" end section = (@header.opcode == OpCode.UPDATE) ? "ZONE" : "QUESTION"; retval = retval + ";; #{section} SECTION (#{@header.qdcount} record#{@header.qdcount == 1 ? '' : 's'})\n"; each_question { |qr| retval = retval + ";; #{qr.to_s}\n"; } if (@answer.size > 0) retval = retval + "\n"; section = (@header.opcode == OpCode.UPDATE) ? "PREREQUISITE" : "ANSWER"; retval = retval + ";; #{section} SECTION (#{@header.ancount} record#{@header.ancount == 1 ? '' : 's'})\n"; each_answer { |rr| retval = retval + rr.to_s + "\n"; } end if (@authority.size > 0) retval = retval + "\n"; section = (@header.opcode == OpCode.UPDATE) ? "UPDATE" : "AUTHORITY"; retval = retval + ";; #{section} SECTION (#{@header.nscount} record#{@header.nscount == 1 ? '' : 's'})\n"; { |rr| retval = retval + rr.to_s + "\n"; } end if ((@additional.size > 0 && !opt) || (@additional.size > 1)) retval = retval + "\n"; retval = retval + ";; ADDITIONAL SECTION (#{@header.arcount} record#{@header.arcount == 1 ? '' : 's'})\n"; each_additional { |rr| if (rr.type != Types::OPT) retval = retval + rr.to_s+ "\n" end } end return retval; end |
#tsig ⇒ Object
Returns the TSIG record from the ADDITIONAL section, if one is present.
425 426 427 428 429 430 431 432 |
# File 'lib/Dnsruby/message.rb', line 425 def tsig if (@additional.last) if (@additional.last.rr_type == Types.TSIG) return @additional.last end end return nil end |
#update_counts ⇒ Object
:nodoc:all
365 366 367 368 369 370 |
# File 'lib/Dnsruby/message.rb', line 365 def update_counts # :nodoc:all @header.ancount = @answer.length @header.arcount = @additional.length @header.qdcount = @question.length @header.nscount = @authority.length end |
#verified? ⇒ Boolean
If this message was signed by a TSIG, was the TSIG verified?
461 462 463 |
# File 'lib/Dnsruby/message.rb', line 461 def verified? return (@tsigstate == :Verified) end |