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
@@identifier =
-1

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(*args) ⇒ Header

Returns a new instance of Header.



396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
# File 'lib/Dnsruby/message.rb', line 396

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



354
355
356
# File 'lib/Dnsruby/message.rb', line 354

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.)



370
371
372
# File 'lib/Dnsruby/message.rb', line 370

def ad
  @ad
end

#ancountObject Also known as: prcount

The number of records in the answer section of the message



392
393
394
# File 'lib/Dnsruby/message.rb', line 392

def ancount
  @ancount
end

#arcountObject Also known as: adcount

The number of records in the additional record section og the message



394
395
396
# File 'lib/Dnsruby/message.rb', line 394

def arcount
  @arcount
end

#cdObject

The checking disabled flag



363
364
365
# File 'lib/Dnsruby/message.rb', line 363

def cd
  @cd
end

#dnssec_okObject

The DO (dnssec OK) flag



373
374
375
# File 'lib/Dnsruby/message.rb', line 373

def dnssec_ok
  @dnssec_ok
end

#idObject

The header ID



348
349
350
# File 'lib/Dnsruby/message.rb', line 348

def id
  @id
end

#nscountObject Also known as: upcount

The number of records in the authoriy section of the message



390
391
392
# File 'lib/Dnsruby/message.rb', line 390

def nscount
  @nscount
end

#opcodeObject

The header opcode



385
386
387
# File 'lib/Dnsruby/message.rb', line 385

def opcode
  @opcode
end

#qdcountObject Also known as: zocount

The number of records in the question section of the message



388
389
390
# File 'lib/Dnsruby/message.rb', line 388

def qdcount
  @qdcount
end

#qrObject

The query response flag



351
352
353
# File 'lib/Dnsruby/message.rb', line 351

def qr
  @qr
end

#raObject

Recursion available flag



379
380
381
# File 'lib/Dnsruby/message.rb', line 379

def ra
  @ra
end

#rcodeObject

Query response code



382
383
384
# File 'lib/Dnsruby/message.rb', line 382

def rcode
  @rcode
end

#rdObject

Recursion Desired flag



360
361
362
# File 'lib/Dnsruby/message.rb', line 360

def rd
  @rd
end

#tcObject

Truncated flag



357
358
359
# File 'lib/Dnsruby/message.rb', line 357

def tc
  @tc
end

Class Method Details

.new_from_data(data) ⇒ Object



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

def Header.new_from_data(data)
  header = Header.new
  MessageDecoder.new(data) {|msg|
    header.decode(msg)}
  return header
end

Instance Method Details

#==(other) ⇒ Object



456
457
458
459
460
461
462
463
464
465
466
# File 'lib/Dnsruby/message.rb', line 456

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



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

def data
  return MessageEncoder.new {|msg|
    self.encode(msg)
  }.to_s
end

#decode(msg) ⇒ Object



501
502
503
504
505
506
507
508
509
510
511
512
513
# File 'lib/Dnsruby/message.rb', line 501

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



438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
# File 'lib/Dnsruby/message.rb', line 438

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

#getExceptionObject



523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
# File 'lib/Dnsruby/message.rb', line 523

def getException
  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



468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
# File 'lib/Dnsruby/message.rb', line 468

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