Class: Rex::Proto::IAX2::Call
- Inherits:
-
Object
- Object
- Rex::Proto::IAX2::Call
- Defined in:
- lib/rex/proto/iax2/call.rb
Instance Attribute Summary collapse
-
#audio_buff ⇒ Object
Returns the value of attribute audio_buff.
-
#audio_hook ⇒ Object
Returns the value of attribute audio_hook.
-
#busy ⇒ Object
Returns the value of attribute busy.
-
#caller_name ⇒ Object
Returns the value of attribute caller_name.
-
#caller_number ⇒ Object
Returns the value of attribute caller_number.
-
#client ⇒ Object
Returns the value of attribute client.
-
#codec ⇒ Object
Returns the value of attribute codec.
-
#dcall ⇒ Object
Returns the value of attribute dcall.
-
#dtmf ⇒ Object
Returns the value of attribute dtmf.
-
#iseq ⇒ Object
Returns the value of attribute iseq.
-
#itime ⇒ Object
Returns the value of attribute itime.
-
#oseq ⇒ Object
Returns the value of attribute oseq.
-
#queue ⇒ Object
Returns the value of attribute queue.
-
#ring_finish ⇒ Object
Returns the value of attribute ring_finish.
-
#ring_start ⇒ Object
Returns the value of attribute ring_start.
-
#scall ⇒ Object
Returns the value of attribute scall.
-
#state ⇒ Object
Returns the value of attribute state.
-
#time_limit ⇒ Object
Returns the value of attribute time_limit.
Instance Method Summary collapse
- #audio_packet_data(pkt) ⇒ Object
- #decode_audio_frame(buff) ⇒ Object
- #dial(number) ⇒ Object
- #dprint(msg) ⇒ Object
- #each_audio_frame(&block) ⇒ Object
-
#handle_audio(pkt) ⇒ Object
Encoded audio from the client.
-
#handle_control(pkt) ⇒ Object
Handling incoming control packets TODO: Enforce sequence order to prevent duplicates from breaking our state.
- #hangup ⇒ Object
-
#initialize(client, src_id) ⇒ Call
constructor
A new instance of Call.
- #process_elements(data, off = 0) ⇒ Object
-
#register ⇒ Object
Register with the IAX endpoint.
- #ring_time ⇒ Object
- #timestamp ⇒ Object
- #wait_for(*stypes) ⇒ Object
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_buff ⇒ Object
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_hook ⇒ Object
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 |
#busy ⇒ Object
Returns the value of attribute busy.
17 18 19 |
# File 'lib/rex/proto/iax2/call.rb', line 17 def busy @busy end |
#caller_name ⇒ Object
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_number ⇒ Object
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 |
#client ⇒ Object
Returns the value of attribute client.
7 8 9 |
# File 'lib/rex/proto/iax2/call.rb', line 7 def client @client end |
#codec ⇒ Object
Returns the value of attribute codec.
10 11 12 |
# File 'lib/rex/proto/iax2/call.rb', line 10 def codec @codec end |
#dcall ⇒ Object
Returns the value of attribute dcall.
9 10 11 |
# File 'lib/rex/proto/iax2/call.rb', line 9 def dcall @dcall end |
#dtmf ⇒ Object
Returns the value of attribute dtmf.
21 22 23 |
# File 'lib/rex/proto/iax2/call.rb', line 21 def dtmf @dtmf end |
#iseq ⇒ Object
Returns the value of attribute iseq.
8 9 10 |
# File 'lib/rex/proto/iax2/call.rb', line 8 def iseq @iseq end |
#itime ⇒ Object
Returns the value of attribute itime.
12 13 14 |
# File 'lib/rex/proto/iax2/call.rb', line 12 def itime @itime end |
#oseq ⇒ Object
Returns the value of attribute oseq.
8 9 10 |
# File 'lib/rex/proto/iax2/call.rb', line 8 def oseq @oseq end |
#queue ⇒ Object
Returns the value of attribute queue.
13 14 15 |
# File 'lib/rex/proto/iax2/call.rb', line 13 def queue @queue end |
#ring_finish ⇒ Object
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_start ⇒ Object
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 |
#scall ⇒ Object
Returns the value of attribute scall.
9 10 11 |
# File 'lib/rex/proto/iax2/call.rb', line 9 def scall @scall end |
#state ⇒ Object
Returns the value of attribute state.
10 11 12 |
# File 'lib/rex/proto/iax2/call.rb', line 10 def state @state end |
#time_limit ⇒ Object
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 |
#hangup ⇒ Object
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 |
#register ⇒ Object
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_time ⇒ Object
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 |
#timestamp ⇒ Object
163 164 165 |
# File 'lib/rex/proto/iax2/call.rb', line 163 def (( ::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 |