Class: Rex::Proto::IAX2::Call

Inherits:
Object
  • Object
show all
Defined in:
lib/rex/proto/iax2/call.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(client, src_id) ⇒ Call

Returns a new instance of Call.



24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/rex/proto/iax2/call.rb', line 24

def initialize(client, src_id)
  self.client = client
  self.scall  = src_id
  self.dcall  = 0
  self.iseq   = 0
  self.oseq   = 0
  self.state  = nil

  self.itime  = ::Time.now
  self.queue  = ::Queue.new

  self.audio_buff = []

  self.busy = false
  self.dtmf = ''
end

Instance Attribute Details

#audio_buffObject

Returns the value of attribute audio_buff.



15
16
17
# File 'lib/rex/proto/iax2/call.rb', line 15

def audio_buff
  @audio_buff
end

#audio_hookObject

Returns the value of attribute audio_hook.



14
15
16
# File 'lib/rex/proto/iax2/call.rb', line 14

def audio_hook
  @audio_hook
end

#busyObject

Returns the value of attribute busy.



17
18
19
# File 'lib/rex/proto/iax2/call.rb', line 17

def busy
  @busy
end

#caller_nameObject

Returns the value of attribute caller_name.



19
20
21
# File 'lib/rex/proto/iax2/call.rb', line 19

def caller_name
  @caller_name
end

#caller_numberObject

Returns the value of attribute caller_number.



20
21
22
# File 'lib/rex/proto/iax2/call.rb', line 20

def caller_number
  @caller_number
end

#clientObject

Returns the value of attribute client.



7
8
9
# File 'lib/rex/proto/iax2/call.rb', line 7

def client
  @client
end

#codecObject

Returns the value of attribute codec.



10
11
12
# File 'lib/rex/proto/iax2/call.rb', line 10

def codec
  @codec
end

#dcallObject

Returns the value of attribute dcall.



9
10
11
# File 'lib/rex/proto/iax2/call.rb', line 9

def dcall
  @dcall
end

#dtmfObject

Returns the value of attribute dtmf.



21
22
23
# File 'lib/rex/proto/iax2/call.rb', line 21

def dtmf
  @dtmf
end

#iseqObject

Returns the value of attribute iseq.



8
9
10
# File 'lib/rex/proto/iax2/call.rb', line 8

def iseq
  @iseq
end

#itimeObject

Returns the value of attribute itime.



12
13
14
# File 'lib/rex/proto/iax2/call.rb', line 12

def itime
  @itime
end

#oseqObject

Returns the value of attribute oseq.



8
9
10
# File 'lib/rex/proto/iax2/call.rb', line 8

def oseq
  @oseq
end

#queueObject

Returns the value of attribute queue.



13
14
15
# File 'lib/rex/proto/iax2/call.rb', line 13

def queue
  @queue
end

#ring_finishObject

Returns the value of attribute ring_finish.



11
12
13
# File 'lib/rex/proto/iax2/call.rb', line 11

def ring_finish
  @ring_finish
end

#ring_startObject

Returns the value of attribute ring_start.



11
12
13
# File 'lib/rex/proto/iax2/call.rb', line 11

def ring_start
  @ring_start
end

#scallObject

Returns the value of attribute scall.



9
10
11
# File 'lib/rex/proto/iax2/call.rb', line 9

def scall
  @scall
end

#stateObject

Returns the value of attribute state.



10
11
12
# File 'lib/rex/proto/iax2/call.rb', line 10

def state
  @state
end

#time_limitObject

Returns the value of attribute time_limit.



16
17
18
# File 'lib/rex/proto/iax2/call.rb', line 16

def time_limit
  @time_limit
end

Instance Method Details

#audio_packet_data(pkt) ⇒ Object



338
339
340
# File 'lib/rex/proto/iax2/call.rb', line 338

def audio_packet_data(pkt)
  (pkt[0,1].unpack("C")[0] & 0x80 == 0) ? pkt[4,pkt.length-4] : pkt[12,pkt.length-12]
end

#decode_audio_frame(buff) ⇒ Object



316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
# File 'lib/rex/proto/iax2/call.rb', line 316

def decode_audio_frame(buff)
  case self.codec

  # Convert u-law into signed PCM
  when Constants::IAX_CODEC_G711_MULAW
    Rex::Proto::IAX2::Codecs::MuLaw.decode(buff)

  # Convert a-law into signed PCM
  when Constants::IAX_CODEC_G711_ALAW
    Rex::Proto::IAX2::Codecs::ALaw.decode(buff)

  # Linear little-endian signed PCM is our native format
  when Constants::IAX_CODEC_LINEAR_PCM
    buff

  # Unsupported codec, return empty
  else
    dprint("UNKNOWN CODEC: #{self.codec.inspect}")
    ''
  end
end

#dial(number) ⇒ Object



116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
# File 'lib/rex/proto/iax2/call.rb', line 116

def dial(number)
  self.client.send_new(self, number)
  res = wait_for(Constants::IAX_SUBTYPE_AUTHREQ, Constants::IAX_SUBTYPE_ACCEPT)
  return if not res

  # Handle authentication if its requested
  if res[1] == Constants::IAX_SUBTYPE_AUTHREQ
    chall = nil

    # Look for IAX_AUTH_MD5 (2) as an available auth method
    if res[2][14].unpack("n")[0] & 2 <= 0
      dprint("REGAUTH: MD5 authentication is not enabled on the server")
      return
    end

    if res[2][Constants::IAX_IE_CHALLENGE_DATA]
      self.dcall = res[0][0]
      chall = res[2][Constants::IAX_IE_CHALLENGE_DATA]
    end

    if chall.nil?
      dprint("REGAUTH: No challenge data received")
      return
    end

    self.client.send_authrep_chall_response(self, chall)
    res = wait_for( Constants::IAX_SUBTYPE_ACCEPT)
    return if not res
  end

  self.codec = res[2][Constants::IAX_IE_DESIRED_CODEC].unpack("N")[0]
  self.state = :ringing
  self.ring_start = ::Time.now.to_i
  self.client.send_ack(self)
  true
end

#dprint(msg) ⇒ Object



42
43
44
# File 'lib/rex/proto/iax2/call.rb', line 42

def dprint(msg)
  self.client.dprint(msg)
end

#each_audio_frame(&block) ⇒ Object



310
311
312
313
314
# File 'lib/rex/proto/iax2/call.rb', line 310

def each_audio_frame(&block)
  self.audio_buff.each do |frame|
    block.call(frame)
  end
end

#handle_audio(pkt) ⇒ Object

Encoded audio from the client



291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
# File 'lib/rex/proto/iax2/call.rb', line 291

def handle_audio(pkt)
  # Ignore audio received before the call is answered (ring ring)
  return if self.state != :answered

  # Extract the data from the packet (full or mini)
  data = audio_packet_data(pkt)

  # Decode the data into linear PCM frames
  buff = decode_audio_frame(data)

  # Call the caller-provided hook if its exists
  if self.audio_hook
    self.audio_buff(buff)
  # Otherwise append the frame to the buffer
  else
    self.audio_buff << buff
  end
end

#handle_control(pkt) ⇒ Object

Handling incoming control packets TODO: Enforce sequence order to prevent duplicates from breaking our state



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
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
# File 'lib/rex/proto/iax2/call.rb', line 180

def handle_control(pkt)
  src_call, dst_call, tstamp, out_seq, inp_seq, itype = pkt.unpack('nnNCCC')

  # Scrub the high bits out of the call IDs
  src_call ^= 0x8000 if (src_call & 0x8000 != 0)
  dst_call ^= 0x8000 if (dst_call & 0x8000 != 0)

  phdr = [ src_call, dst_call, tstamp, out_seq, inp_seq, itype ]

  info  = nil
  stype = pkt[11,1].unpack("C")[0]
  info  = process_elements(pkt, 12) if [Constants::IAX_TYPE_IAX, Constants::IAX_TYPE_CONTROL].include?(itype)

  if dst_call != self.scall
    dprint("Incoming packet to inactive call: #{dst_call} vs #{self.scall}: #{phdr.inspect} #{stype.inspect} #{info.inspect}")
    return
  end

  # Increment the received sequence number
  self.iseq = (self.iseq + 1) & 0xff

  if self.state == :hangup
    dprint("Packet received after hangup, replying with invalid")
    self.client.send_invalid(self)
    return
  end

  # Technically these all require an ACK reply
  # NEW, HANGUP, REJECT, ACCEPT, PONG, AUTHREP, REGREL, REGACK, REGREJ, TXREL

  case itype
  when Constants::IAX_TYPE_DTMF_BEGIN
    self.dprint("DTMF BEG: #{pkt[11,1]}")
    self.dtmf << pkt[11,1]

  when Constants::IAX_TYPE_DTMF_END
    self.dprint("DTMF END: #{pkt[11,1]}")

  when Constants::IAX_TYPE_CONTROL
    case stype
    when Constants::IAX_CTRL_HANGUP
      dprint("HANGUP")
      self.client.send_ack(self)
      self.state = :hangup

    when Constants::IAX_CTRL_RINGING
      dprint("RINGING")
      self.client.send_ack(self)

    when Constants::IAX_CTRL_BUSY
      dprint("BUSY")
      self.busy  = true
      self.state = :hangup
      self.client.send_ack(self)

    when Constants::IAX_CTRL_ANSWER
      dprint("ANSWER")
      if self.state == :ringing
        self.state = :answered
        self.ring_finish = ::Time.now.to_i
      end
      self.client.send_ack(self)

    when Constants::IAX_CTRL_PROGRESS
      dprint("PROGRESS")

    when Constants::IAX_CTRL_PROCEED
      dprint("PROCEED")

    when 255
      dprint("STOP SOUNDS")
    end
    # Acknowledge all control packets
    # self.client.send_ack(self)

  when Constants::IAX_TYPE_IAX

    dprint( ["RECV", phdr, stype, info].inspect )
    case stype
    when Constants::IAX_SUBTYPE_HANGUP
      self.state = :hangup
      self.client.send_ack(self)
    when Constants::IAX_SUBTYPE_LAGRQ
      # Lagrps echo the timestamp
      self.client.send_lagrp(self, tstamp)
    when Constants::IAX_SUBTYPE_ACK
      # Nothing to do here
    when Constants::IAX_SUBTYPE_PING
      # Pongs echo the timestamp
      self.client.send_pong(self, tstamp)
    when Constants::IAX_SUBTYPE_PONG
      self.client.send_ack(self)
    else
      dprint( ["RECV-QUEUE", phdr, stype, info].inspect )
      self.queue.push( [phdr, stype, info ] )
    end

  when Constants::IAX_TYPE_VOICE
    v_codec = stype
    if self.state == :answered
      handle_audio(pkt)
    end
    self.client.send_ack(self)

  when nil
    dprint("Invalid control packet: #{pkt.unpack("H*")[0]}")
  end
end

#hangupObject



153
154
155
156
157
# File 'lib/rex/proto/iax2/call.rb', line 153

def hangup
  self.client.send_hangup(self)
  self.state = :hangup
  true
end

#process_elements(data, off = 0) ⇒ Object



167
168
169
170
171
172
173
174
175
176
# File 'lib/rex/proto/iax2/call.rb', line 167

def process_elements(data,off=0)
  res = {}
  while( off < data.length )
    ie_type = data[off    ,1].unpack("C")[0]
    ie_len  = data[off + 1,2].unpack("C")[0]
    res[ie_type] = data[off + 2, ie_len]
    off += ie_len + 2
  end
  res
end

#registerObject

Register with the IAX endpoint



61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
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
114
# File 'lib/rex/proto/iax2/call.rb', line 61

def register
  self.client.send_regreq(self)
  res = wait_for( Constants::IAX_SUBTYPE_REGAUTH, Constants::IAX_SUBTYPE_REGREJ )
  return if not res

  if res[1] == Constants::IAX_SUBTYPE_REGREJ
    reason = res[2][Constants::IAX_IE_REGREJ_CAUSE] || "Unknown Reason"
    dprint("REGREJ: #{reason}")
    # Acknowledge the REGREJ
    self.client.send_ack(self)
    return
  end

  chall = nil

  # Look for IAX_AUTH_MD5 (2) as an available auth method
  if res[2][14].unpack("n")[0] & 2 <= 0
    dprint("REGAUTH: MD5 authentication is not enabled on the server")
    return
  end

  if res[2][Constants::IAX_IE_CHALLENGE_DATA]
    self.dcall = res[0][0]
    chall = res[2][Constants::IAX_IE_CHALLENGE_DATA]
  end

  if chall.nil?
    dprint("REGAUTH: No challenge data received")
    return
  end

  self.client.send_regreq_chall_response(self, chall)
  res = wait_for( Constants::IAX_SUBTYPE_REGACK, Constants::IAX_SUBTYPE_REGREJ )
  return if not res

  if res[1] == Constants::IAX_SUBTYPE_REGREJ
    reason = res[2][Constants::IAX_IE_REGREJ_CAUSE] || "Unknown Reason"
    dprint("REGREJ: #{reason}")
    return
  end

  if res[2][Constants::IAX_IE_APPARENT_ADDR]
    r_fam, r_port, r_addr = res[2][Constants::IAX_IE_APPARENT_ADDR].unpack('nnA4')
    r_addr = r_addr.unpack("C*").map{|x| x.to_s }.join(".")
    dprint("REGACK: Registered from address #{r_addr}:#{r_port}")
  end

  # Acknowledge the REGACK
  self.client.send_ack(self)

  self.state = :registered

  true
end

#ring_timeObject



159
160
161
# File 'lib/rex/proto/iax2/call.rb', line 159

def ring_time
  (self.ring_finish || ::Time.now).to_i - self.ring_start.to_i
end

#timestampObject



163
164
165
# File 'lib/rex/proto/iax2/call.rb', line 163

def timestamp
  (( ::Time.now - self.itime) * 1000.0 ).to_i & 0xffffffff
end

#wait_for(*stypes) ⇒ Object



46
47
48
49
50
51
52
53
54
55
56
57
58
# File 'lib/rex/proto/iax2/call.rb', line 46

def wait_for(*stypes)
  begin
    ::Timeout.timeout( Constants::IAX_DEFAULT_TIMEOUT ) do
      while (res = self.queue.pop )
        if stypes.include?(res[1])
          return res
        end
      end
    end
  rescue ::Timeout::Error
    return nil
  end
end