Class: DNS::Header

Inherits:
Object
  • Object
show all
Defined in:
lib/faildns/header.rb,
lib/faildns/header/type.rb,
lib/faildns/header/opcode.rb,
lib/faildns/header/status.rb

Overview

– The header contains the following fields:

                                1  1  1  1  1  1
  0  1  2  3  4  5  6  7  8  9  0  1  2  3  4  5
+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+
|                      ID                       |
+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+
|QR|   Opcode  |AA|TC|RD|RA|   Z    |   RCODE   |
+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+
|                    QDCOUNT                    |
+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+
|                    ANCOUNT                    |
+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+
|                    NSCOUNT                    |
+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+
|                    ARCOUNT                    |
+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+

where:

ID A 16 bit identifier assigned by the program that

generates any kind of query.  This identifier is copied
the corresponding reply and can be used by the requester
to match up replies to outstanding queries.

QR A one bit field that specifies whether this message is a

query (0), or a response (1).

OPCODE A four bit field that specifies kind of query in this

message.  This value is set by the originator of a query
and copied into the response.  The values are:

0               a standard query (QUERY)

1               an inverse query (IQUERY)

2               a server status request (STATUS)

3-15            reserved for future use

AA Authoritative Answer - this bit is valid in responses,

and specifies that the responding name server is an
authority for the domain name in question section.

Note that the contents of the answer section may have
multiple owner names because of aliases.  The AA bit
corresponds to the name which matches the query name, or
the first owner name in the answer section.

TC TrunCation - specifies that this message was truncated

due to length greater than that permitted on the
transmission channel.

RD Recursion Desired - this bit may be set in a query and

is copied into the response.  If RD is set, it directs
the name server to pursue the query recursively.
Recursive query support is optional.

RA Recursion Available - this be is set or cleared in a

response, and denotes whether recursive query support is
available in the name server.

Z Reserved for future use. Must be zero in all queries

and responses.

RCODE Response code - this 4 bit field is set as part of

responses.  The values have the following
interpretation:

0               No error condition

1               Format error - The name server was
                unable to interpret the query.

2               Server failure - The name server was
                unable to process this query due to a
                problem with the name server.

3               Name Error - Meaningful only for
                responses from an authoritative name
                server, this code signifies that the
                domain name referenced in the query does
                not exist.

4               Not Implemented - The name server does
                not support the requested kind of query.

5               Refused - The name server refuses to
                perform the specified operation for
                policy reasons.  For example, a name
                server may not wish to provide the
                information to the particular requester,
                or a name server may not wish to perform
                a particular operation (e.g., zone
                transfer) for particular data.

6-15            Reserved for future use.

QDCOUNT an unsigned 16 bit integer specifying the number of

entries in the question section.

ANCOUNT an unsigned 16 bit integer specifying the number of

resource records in the answer section.

NSCOUNT an unsigned 16 bit integer specifying the number of name

server resource records in the authority records
section.

ARCOUNT an unsigned 16 bit integer specifying the number of

resource records in the additional records section.

++

Defined Under Namespace

Classes: Opcode, Status, Type

Constant Summary collapse

@@default =
{
  :AA => false, :TC => false, :RD => false, :RA => false, :AD => false, :CD => true,
   :RCODE => Status.new(:NOERROR),
   :QDCOUNT => 0,
  :ANCOUNT => 0,
  :NSCOUNT => 0,
  :ARCOUNT => 0
}

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(what = {}) ⇒ Header

Returns a new instance of Header.



186
187
188
189
190
191
192
193
194
195
196
# File 'lib/faildns/header.rb', line 186

def initialize (what={})
  if !what.is_a? Hash
    raise ArgumentError.new('You have to pass a Hash.')
  end

  @data = @@default.merge(what)

  if block_given?
    yield self
  end
end

Class Method Details

.length(string = nil) ⇒ Object



182
183
184
# File 'lib/faildns/header.rb', line 182

def self.length (string=nil)
  12
end

.parse(string) ⇒ Object



151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
# File 'lib/faildns/header.rb', line 151

def self.parse (string)
  data = string.unpack('nnnnnn')

  string[0, Header.length] = ''

  return Header.new(
    :ID => data[0],

    :QR => Type.new((data[1] & 0x8000) >> 15),

    :OPCODE => Opcode.new((data[1] & 0x7800) >> 11),

    :AA => (data[1] & 0x400 != 0),
    :TC => (data[1] & 0x200 != 0),
    :RD => (data[1] & 0x100 != 0),
    :RA => (data[1] & 0x80  != 0),
    :AD => (data[1] & 0x40  != 0),
    :CD => (data[1] & 0x20  != 0),

    :RCODE  => Status.new(data[1] & 0xf),

    :QDCOUNT => data[2],

    :ANCOUNT => data[3],

    :NSCOUNT => data[4],

    :ARCOUNT => data[5]
  )
end

Instance Method Details

#additionalsObject



211
# File 'lib/faildns/header.rb', line 211

def additionals;    @data[:ARCOUNT] end

#additionals=(val) ⇒ Object



232
# File 'lib/faildns/header.rb', line 232

def additionals= (val); @data[:ARCOUNT] = val             end

#answersObject



209
# File 'lib/faildns/header.rb', line 209

def answers;        @data[:ANCOUNT] end

#answers=(val) ⇒ Object



230
# File 'lib/faildns/header.rb', line 230

def answers= (val);     @data[:ANCOUNT] = val             end

#authentic!Object



220
# File 'lib/faildns/header.rb', line 220

def authentic!;         @data[:AD]      = true            end

#authentic?Boolean

Returns:

  • (Boolean)


205
# File 'lib/faildns/header.rb', line 205

def authentic?;     @data[:AD]      end

#authoritative!Object



216
# File 'lib/faildns/header.rb', line 216

def authoritative!;     @data[:AA]      = true            end

#authoritative?Boolean

Returns:

  • (Boolean)


201
# File 'lib/faildns/header.rb', line 201

def authoritative?; @data[:AA]      end

#authoritiesObject



210
# File 'lib/faildns/header.rb', line 210

def authorities;    @data[:NSCOUNT] end

#authorities=(val) ⇒ Object



231
# File 'lib/faildns/header.rb', line 231

def authorities= (val); @data[:NSCOUNT] = val             end

#checking!Object



221
# File 'lib/faildns/header.rb', line 221

def checking!;          @data[:CD]      = true            end

#checking?Boolean

Returns:

  • (Boolean)


206
# File 'lib/faildns/header.rb', line 206

def checking?;      @data[:CD]      end

#classObject



200
# File 'lib/faildns/header.rb', line 200

def class;          @data[:OPCODE]  end

#class=(val) ⇒ Object



215
# File 'lib/faildns/header.rb', line 215

def class= (val);       @data[:OPCODE]  = Opcode.new(val) end

#idObject



198
# File 'lib/faildns/header.rb', line 198

def id;             @data[:ID]      end

#id=(val) ⇒ Object



213
# File 'lib/faildns/header.rb', line 213

def id= (val);          @data[:ID]      = val             end

#not_authentic!Object



226
# File 'lib/faildns/header.rb', line 226

def not_authentic!;     @data[:AD]      = false           end

#not_authoritative!Object



222
# File 'lib/faildns/header.rb', line 222

def not_authoritative!; @data[:AA]      = false           end

#not_checking!Object



227
# File 'lib/faildns/header.rb', line 227

def not_checking!;      @data[:CD]      = false           end

#not_recursivable!Object



225
# File 'lib/faildns/header.rb', line 225

def not_recursivable!;  @data[:RA]      = false           end

#not_recursive!Object



224
# File 'lib/faildns/header.rb', line 224

def not_recursive!;     @data[:RD]      = false           end

#not_truncated!Object



223
# File 'lib/faildns/header.rb', line 223

def not_truncated!;     @data[:TC]      = false           end

#packObject



234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
# File 'lib/faildns/header.rb', line 234

def pack
  [
    self.id,

    ( (self.type.value << 15) \
    | (self.class.value << 14) \
    | ((self.authoritative?) ? (1 << 10) : 0) \
    | ((self.truncated?) ? (1 << 9) : 0) \
    | ((self.recursive?) ? (1 << 8) : 0) \
    | ((self.recursivable?) ? (1 << 7) : 0) \
    | (self.status.value)),

    self.questions,
    self.answers,
    self.authorities,
    self.additionals
  ].pack('nnnnnn')
end

#questionsObject



208
# File 'lib/faildns/header.rb', line 208

def questions;      @data[:QDCOUNT] end

#questions=(val) ⇒ Object



229
# File 'lib/faildns/header.rb', line 229

def questions= (val);   @data[:QDCOUNT] = val             end

#recursivable!Object



219
# File 'lib/faildns/header.rb', line 219

def recursivable!;      @data[:RA]      = true            end

#recursivable?Boolean

Returns:

  • (Boolean)


204
# File 'lib/faildns/header.rb', line 204

def recursivable?;  @data[:RA]      end

#recursive!Object



218
# File 'lib/faildns/header.rb', line 218

def recursive!;         @data[:RD]      = true            end

#recursive?Boolean

Returns:

  • (Boolean)


203
# File 'lib/faildns/header.rb', line 203

def recursive?;     @data[:RD]      end

#statusObject



207
# File 'lib/faildns/header.rb', line 207

def status;         @data[:RCODE]   end

#status=(val) ⇒ Object



228
# File 'lib/faildns/header.rb', line 228

def status= (val);      @data[:RCODE]   = Status.new(val) end

#truncated!Object



217
# File 'lib/faildns/header.rb', line 217

def truncated!;         @data[:TC]      = true            end

#truncated?Boolean

Returns:

  • (Boolean)


202
# File 'lib/faildns/header.rb', line 202

def truncated?;     @data[:TC]      end

#typeObject



199
# File 'lib/faildns/header.rb', line 199

def type;           @data[:QR]      end

#type=(val) ⇒ Object



214
# File 'lib/faildns/header.rb', line 214

def type= (val);        @data[:QR]      = Type.new(val)   end