Class: Resolv::DNS::Message

Inherits:
Object
  • Object
show all
Defined in:
lib/fresolv.rb

Overview

:nodoc:

Defined Under Namespace

Classes: MessageDecoder, MessageEncoder

Constant Summary collapse

@@identifier =
-1

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(id = (@@identifier += 1) & 0xffff) ⇒ Message

Returns a new instance of Message.



1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
# File 'lib/fresolv.rb', line 1228

def initialize(id = (@@identifier += 1) & 0xffff)
  @id = id
  @qr = 0
  @opcode = 0
  @aa = 0
  @tc = 0
  @rd = 0 # recursion desired
  @ra = 0 # recursion available
  @rcode = 0
  @question = []
  @answer = []
  @authority = []
  @additional = []
end

Instance Attribute Details

#aaObject

Returns the value of attribute aa.



1243
1244
1245
# File 'lib/fresolv.rb', line 1243

def aa
  @aa
end

#additionalObject (readonly)

Returns the value of attribute additional.



1244
1245
1246
# File 'lib/fresolv.rb', line 1244

def additional
  @additional
end

#answerObject (readonly)

Returns the value of attribute answer.



1244
1245
1246
# File 'lib/fresolv.rb', line 1244

def answer
  @answer
end

#authorityObject (readonly)

Returns the value of attribute authority.



1244
1245
1246
# File 'lib/fresolv.rb', line 1244

def authority
  @authority
end

#idObject

Returns the value of attribute id.



1243
1244
1245
# File 'lib/fresolv.rb', line 1243

def id
  @id
end

#opcodeObject

Returns the value of attribute opcode.



1243
1244
1245
# File 'lib/fresolv.rb', line 1243

def opcode
  @opcode
end

#qrObject

Returns the value of attribute qr.



1243
1244
1245
# File 'lib/fresolv.rb', line 1243

def qr
  @qr
end

#questionObject (readonly)

Returns the value of attribute question.



1244
1245
1246
# File 'lib/fresolv.rb', line 1244

def question
  @question
end

#raObject

Returns the value of attribute ra.



1243
1244
1245
# File 'lib/fresolv.rb', line 1243

def ra
  @ra
end

#rcodeObject

Returns the value of attribute rcode.



1243
1244
1245
# File 'lib/fresolv.rb', line 1243

def rcode
  @rcode
end

#rdObject

Returns the value of attribute rd.



1243
1244
1245
# File 'lib/fresolv.rb', line 1243

def rd
  @rd
end

#tcObject

Returns the value of attribute tc.



1243
1244
1245
# File 'lib/fresolv.rb', line 1243

def tc
  @tc
end

Class Method Details

.decode(m) ⇒ Object



1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
# File 'lib/fresolv.rb', line 1400

def Message.decode(m)
  o = Message.new(0)
  MessageDecoder.new(m) {|msg|
    id, flag, qdcount, ancount, nscount, arcount =
      msg.get_unpack('nnnnnn')
    o.id = id
    o.qr = (flag >> 15) & 1
    o.opcode = (flag >> 11) & 15
    o.aa = (flag >> 10) & 1
    o.tc = (flag >> 9) & 1
    o.rd = (flag >> 8) & 1
    o.ra = (flag >> 7) & 1
    o.rcode = flag & 15
    (1..qdcount).each {
      name, typeclass = msg.get_question
      o.add_question(name, typeclass)
    }
    (1..ancount).each {
      name, ttl, data = msg.get_rr
      o.add_answer(name, ttl, data)
    }
    (1..nscount).each {
      name, ttl, data = msg.get_rr
      o.add_authority(name, ttl, data)
    }
    (1..arcount).each {
      name, ttl, data = msg.get_rr
      o.add_additional(name, ttl, data)
    }
  }
  return o
end

Instance Method Details

#==(other) ⇒ Object



1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
# File 'lib/fresolv.rb', line 1246

def ==(other)
  return @id == other.id &&
         @qr == other.qr &&
         @opcode == other.opcode &&
         @aa == other.aa &&
         @tc == other.tc &&
         @rd == other.rd &&
         @ra == other.ra &&
         @rcode == other.rcode &&
         @question == other.question &&
         @answer == other.answer &&
         @authority == other.authority &&
         @additional == other.additional
end

#add_additional(name, ttl, data) ⇒ Object



1291
1292
1293
# File 'lib/fresolv.rb', line 1291

def add_additional(name, ttl, data)
  @additional << [Name.create(name), ttl, data]
end

#add_answer(name, ttl, data) ⇒ Object



1271
1272
1273
# File 'lib/fresolv.rb', line 1271

def add_answer(name, ttl, data)
  @answer << [Name.create(name), ttl, data]
end

#add_authority(name, ttl, data) ⇒ Object



1281
1282
1283
# File 'lib/fresolv.rb', line 1281

def add_authority(name, ttl, data)
  @authority << [Name.create(name), ttl, data]
end

#add_question(name, typeclass) ⇒ Object



1261
1262
1263
# File 'lib/fresolv.rb', line 1261

def add_question(name, typeclass)
  @question << [Name.create(name), typeclass]
end

#each_additionalObject



1295
1296
1297
1298
1299
# File 'lib/fresolv.rb', line 1295

def each_additional
  @additional.each {|name, ttl, data|
    yield name, ttl, data
  }
end

#each_answerObject



1275
1276
1277
1278
1279
# File 'lib/fresolv.rb', line 1275

def each_answer
  @answer.each {|name, ttl, data|
    yield name, ttl, data
  }
end

#each_authorityObject



1285
1286
1287
1288
1289
# File 'lib/fresolv.rb', line 1285

def each_authority
  @authority.each {|name, ttl, data|
    yield name, ttl, data
  }
end

#each_questionObject



1265
1266
1267
1268
1269
# File 'lib/fresolv.rb', line 1265

def each_question
  @question.each {|name, typeclass|
    yield name, typeclass
  }
end

#each_resourceObject



1301
1302
1303
1304
1305
# File 'lib/fresolv.rb', line 1301

def each_resource
  each_answer {|name, ttl, data| yield name, ttl, data}
  each_authority {|name, ttl, data| yield name, ttl, data}
  each_additional {|name, ttl, data| yield name, ttl, data}
end

#encodeObject



1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
# File 'lib/fresolv.rb', line 1307

def encode
  return MessageEncoder.new {|msg|
    msg.put_pack('nnnnnn',
      @id,
      (@qr & 1) << 15 |
      (@opcode & 15) << 11 |
      (@aa & 1) << 10 |
      (@tc & 1) << 9 |
      (@rd & 1) << 8 |
      (@ra & 1) << 7 |
      (@rcode & 15),
      @question.length,
      @answer.length,
      @authority.length,
      @additional.length)
    @question.each {|q|
      name, typeclass = q
      msg.put_name(name)
      msg.put_pack('nn', typeclass::TypeValue, typeclass::ClassValue)
    }
    [@answer, @authority, @additional].each {|rr|
      rr.each {|r|
        name, ttl, data = r
        msg.put_name(name)
        msg.put_pack('nnN', data.class::TypeValue, data.class::ClassValue, ttl)
        msg.put_length16 {data.encode_rdata(msg)}
      }
    }
  }.to_s
end