Class: RRs::Message

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



5
6
7
8
9
10
11
12
13
14
15
16
17
18
# File 'lib/rrs/message.rb', line 5

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.



20
21
22
# File 'lib/rrs/message.rb', line 20

def aa
  @aa
end

#additionalObject (readonly)

Returns the value of attribute additional.



21
22
23
# File 'lib/rrs/message.rb', line 21

def additional
  @additional
end

#answerObject (readonly)

Returns the value of attribute answer.



21
22
23
# File 'lib/rrs/message.rb', line 21

def answer
  @answer
end

#authorityObject (readonly)

Returns the value of attribute authority.



21
22
23
# File 'lib/rrs/message.rb', line 21

def authority
  @authority
end

#idObject

Returns the value of attribute id.



20
21
22
# File 'lib/rrs/message.rb', line 20

def id
  @id
end

#opcodeObject

Returns the value of attribute opcode.



20
21
22
# File 'lib/rrs/message.rb', line 20

def opcode
  @opcode
end

#qrObject

Returns the value of attribute qr.



20
21
22
# File 'lib/rrs/message.rb', line 20

def qr
  @qr
end

#questionObject (readonly)

Returns the value of attribute question.



21
22
23
# File 'lib/rrs/message.rb', line 21

def question
  @question
end

#raObject

Returns the value of attribute ra.



20
21
22
# File 'lib/rrs/message.rb', line 20

def ra
  @ra
end

#rcodeObject

Returns the value of attribute rcode.



20
21
22
# File 'lib/rrs/message.rb', line 20

def rcode
  @rcode
end

#rdObject

Returns the value of attribute rd.



20
21
22
# File 'lib/rrs/message.rb', line 20

def rd
  @rd
end

#tcObject

Returns the value of attribute tc.



20
21
22
# File 'lib/rrs/message.rb', line 20

def tc
  @tc
end

Class Method Details

.decode(m) ⇒ Object



179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
# File 'lib/rrs/message.rb', line 179

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.tc = (flag >> 9) & 1
    o.rcode = flag & 15
    return o unless o.tc.zero?
  
    o.qr = (flag >> 15) & 1
    o.opcode = (flag >> 11) & 15
    o.aa = (flag >> 10) & 1
    o.rd = (flag >> 8) & 1
    o.ra = (flag >> 7) & 1
    (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



23
24
25
26
27
28
29
30
31
32
33
34
35
36
# File 'lib/rrs/message.rb', line 23

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



68
69
70
# File 'lib/rrs/message.rb', line 68

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

#add_answer(name, ttl, data) ⇒ Object



48
49
50
# File 'lib/rrs/message.rb', line 48

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

#add_authority(name, ttl, data) ⇒ Object



58
59
60
# File 'lib/rrs/message.rb', line 58

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

#add_question(name, typeclass) ⇒ Object



38
39
40
# File 'lib/rrs/message.rb', line 38

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

#each_additionalObject



72
73
74
75
76
# File 'lib/rrs/message.rb', line 72

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

#each_answerObject



52
53
54
55
56
# File 'lib/rrs/message.rb', line 52

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

#each_authorityObject



62
63
64
65
66
# File 'lib/rrs/message.rb', line 62

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

#each_questionObject



42
43
44
45
46
# File 'lib/rrs/message.rb', line 42

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

#each_resourceObject



78
79
80
81
82
# File 'lib/rrs/message.rb', line 78

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



84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
# File 'lib/rrs/message.rb', line 84

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