Class: Dnsruby::Header

Inherits:
Object
  • Object
show all
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

Class Method Summary collapse

Instance Method Summary collapse

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

#aaObject

Authoritative answer flag



407
408
409
# File 'lib/Dnsruby/message.rb', line 407

def aa
  @aa
end

#adObject

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

#ancountObject 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

#arcountObject 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

#cdObject

The checking disabled flag



416
417
418
# File 'lib/Dnsruby/message.rb', line 416

def cd
  @cd
end

#dnssec_okObject

The DO (dnssec OK) flag



426
427
428
# File 'lib/Dnsruby/message.rb', line 426

def dnssec_ok
  @dnssec_ok
end

#idObject

The header ID



401
402
403
# File 'lib/Dnsruby/message.rb', line 401

def id
  @id
end

#nscountObject 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

#opcodeObject

The header opcode



438
439
440
# File 'lib/Dnsruby/message.rb', line 438

def opcode
  @opcode
end

#qdcountObject 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

#qrObject

The query response flag



404
405
406
# File 'lib/Dnsruby/message.rb', line 404

def qr
  @qr
end

#raObject

Recursion available flag



432
433
434
# File 'lib/Dnsruby/message.rb', line 432

def ra
  @ra
end

#rcodeObject

Query response code



435
436
437
# File 'lib/Dnsruby/message.rb', line 435

def rcode
  @rcode
end

#rdObject

Recursion Desired flag



413
414
415
# File 'lib/Dnsruby/message.rb', line 413

def rd
  @rd
end

#tcObject

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

#dataObject



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_exceptionObject



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_sObject



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