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.



1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
# File 'lib/resolv.rb', line 1045

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.



1060
1061
1062
# File 'lib/resolv.rb', line 1060

def aa
  @aa
end

#additionalObject (readonly)

Returns the value of attribute additional.



1061
1062
1063
# File 'lib/resolv.rb', line 1061

def additional
  @additional
end

#answerObject (readonly)

Returns the value of attribute answer.



1061
1062
1063
# File 'lib/resolv.rb', line 1061

def answer
  @answer
end

#authorityObject (readonly)

Returns the value of attribute authority.



1061
1062
1063
# File 'lib/resolv.rb', line 1061

def authority
  @authority
end

#idObject

Returns the value of attribute id.



1060
1061
1062
# File 'lib/resolv.rb', line 1060

def id
  @id
end

#opcodeObject

Returns the value of attribute opcode.



1060
1061
1062
# File 'lib/resolv.rb', line 1060

def opcode
  @opcode
end

#qrObject

Returns the value of attribute qr.



1060
1061
1062
# File 'lib/resolv.rb', line 1060

def qr
  @qr
end

#questionObject (readonly)

Returns the value of attribute question.



1061
1062
1063
# File 'lib/resolv.rb', line 1061

def question
  @question
end

#raObject

Returns the value of attribute ra.



1060
1061
1062
# File 'lib/resolv.rb', line 1060

def ra
  @ra
end

#rcodeObject

Returns the value of attribute rcode.



1060
1061
1062
# File 'lib/resolv.rb', line 1060

def rcode
  @rcode
end

#rdObject

Returns the value of attribute rd.



1060
1061
1062
# File 'lib/resolv.rb', line 1060

def rd
  @rd
end

#tcObject

Returns the value of attribute tc.



1060
1061
1062
# File 'lib/resolv.rb', line 1060

def tc
  @tc
end

Class Method Details

.decode(m) ⇒ Object



1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
# File 'lib/resolv.rb', line 1217

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



1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
# File 'lib/resolv.rb', line 1063

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



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

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

#add_answer(name, ttl, data) ⇒ Object



1088
1089
1090
# File 'lib/resolv.rb', line 1088

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

#add_authority(name, ttl, data) ⇒ Object



1098
1099
1100
# File 'lib/resolv.rb', line 1098

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

#add_question(name, typeclass) ⇒ Object



1078
1079
1080
# File 'lib/resolv.rb', line 1078

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

#each_additionalObject



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

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

#each_answerObject



1092
1093
1094
1095
1096
# File 'lib/resolv.rb', line 1092

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

#each_authorityObject



1102
1103
1104
1105
1106
# File 'lib/resolv.rb', line 1102

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

#each_questionObject



1082
1083
1084
1085
1086
# File 'lib/resolv.rb', line 1082

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

#each_resourceObject



1118
1119
1120
1121
1122
# File 'lib/resolv.rb', line 1118

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



1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
# File 'lib/resolv.rb', line 1124

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