Class: Resolv::DNS::Message

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

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.



1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
# File 'lib/resolv.rb', line 1091

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



1106
1107
1108
# File 'lib/resolv.rb', line 1106

def aa
  @aa
end

#additionalObject (readonly)

Returns the value of attribute additional



1107
1108
1109
# File 'lib/resolv.rb', line 1107

def additional
  @additional
end

#answerObject (readonly)

Returns the value of attribute answer



1107
1108
1109
# File 'lib/resolv.rb', line 1107

def answer
  @answer
end

#authorityObject (readonly)

Returns the value of attribute authority



1107
1108
1109
# File 'lib/resolv.rb', line 1107

def authority
  @authority
end

#idObject

Returns the value of attribute id



1106
1107
1108
# File 'lib/resolv.rb', line 1106

def id
  @id
end

#opcodeObject

Returns the value of attribute opcode



1106
1107
1108
# File 'lib/resolv.rb', line 1106

def opcode
  @opcode
end

#qrObject

Returns the value of attribute qr



1106
1107
1108
# File 'lib/resolv.rb', line 1106

def qr
  @qr
end

#questionObject (readonly)

Returns the value of attribute question



1107
1108
1109
# File 'lib/resolv.rb', line 1107

def question
  @question
end

#raObject

Returns the value of attribute ra



1106
1107
1108
# File 'lib/resolv.rb', line 1106

def ra
  @ra
end

#rcodeObject

Returns the value of attribute rcode



1106
1107
1108
# File 'lib/resolv.rb', line 1106

def rcode
  @rcode
end

#rdObject

Returns the value of attribute rd



1106
1107
1108
# File 'lib/resolv.rb', line 1106

def rd
  @rd
end

#tcObject

Returns the value of attribute tc



1106
1107
1108
# File 'lib/resolv.rb', line 1106

def tc
  @tc
end

Class Method Details

.decode(m) ⇒ Object



1263
1264
1265
1266
1267
1268
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
# File 'lib/resolv.rb', line 1263

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



1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
# File 'lib/resolv.rb', line 1109

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



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

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

#add_answer(name, ttl, data) ⇒ Object



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

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

#add_authority(name, ttl, data) ⇒ Object



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

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

#add_question(name, typeclass) ⇒ Object



1124
1125
1126
# File 'lib/resolv.rb', line 1124

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

#each_additionalObject



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

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

#each_answerObject



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

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

#each_authorityObject



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

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

#each_questionObject



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

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

#each_resourceObject



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

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



1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
# File 'lib/resolv.rb', line 1170

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