Class: Resolv::DNS::Message

Inherits:
Object
  • Object
show all
Defined in:
lib/resolv.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.



1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
# File 'lib/resolv.rb', line 1097

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.



1112
1113
1114
# File 'lib/resolv.rb', line 1112

def aa
  @aa
end

#additionalObject (readonly)

Returns the value of attribute additional.



1113
1114
1115
# File 'lib/resolv.rb', line 1113

def additional
  @additional
end

#answerObject (readonly)

Returns the value of attribute answer.



1113
1114
1115
# File 'lib/resolv.rb', line 1113

def answer
  @answer
end

#authorityObject (readonly)

Returns the value of attribute authority.



1113
1114
1115
# File 'lib/resolv.rb', line 1113

def authority
  @authority
end

#idObject

Returns the value of attribute id.



1112
1113
1114
# File 'lib/resolv.rb', line 1112

def id
  @id
end

#opcodeObject

Returns the value of attribute opcode.



1112
1113
1114
# File 'lib/resolv.rb', line 1112

def opcode
  @opcode
end

#qrObject

Returns the value of attribute qr.



1112
1113
1114
# File 'lib/resolv.rb', line 1112

def qr
  @qr
end

#questionObject (readonly)

Returns the value of attribute question.



1113
1114
1115
# File 'lib/resolv.rb', line 1113

def question
  @question
end

#raObject

Returns the value of attribute ra.



1112
1113
1114
# File 'lib/resolv.rb', line 1112

def ra
  @ra
end

#rcodeObject

Returns the value of attribute rcode.



1112
1113
1114
# File 'lib/resolv.rb', line 1112

def rcode
  @rcode
end

#rdObject

Returns the value of attribute rd.



1112
1113
1114
# File 'lib/resolv.rb', line 1112

def rd
  @rd
end

#tcObject

Returns the value of attribute tc.



1112
1113
1114
# File 'lib/resolv.rb', line 1112

def tc
  @tc
end

Class Method Details

.decode(m) ⇒ Object



1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
# File 'lib/resolv.rb', line 1269

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



1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
# File 'lib/resolv.rb', line 1115

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



1160
1161
1162
# File 'lib/resolv.rb', line 1160

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

#add_answer(name, ttl, data) ⇒ Object



1140
1141
1142
# File 'lib/resolv.rb', line 1140

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

#add_authority(name, ttl, data) ⇒ Object



1150
1151
1152
# File 'lib/resolv.rb', line 1150

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

#add_question(name, typeclass) ⇒ Object



1130
1131
1132
# File 'lib/resolv.rb', line 1130

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

#each_additionalObject



1164
1165
1166
1167
1168
# File 'lib/resolv.rb', line 1164

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

#each_answerObject



1144
1145
1146
1147
1148
# File 'lib/resolv.rb', line 1144

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

#each_authorityObject



1154
1155
1156
1157
1158
# File 'lib/resolv.rb', line 1154

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

#each_questionObject



1134
1135
1136
1137
1138
# File 'lib/resolv.rb', line 1134

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

#each_resourceObject



1170
1171
1172
1173
1174
# File 'lib/resolv.rb', line 1170

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



1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
# File 'lib/resolv.rb', line 1176

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