Class: Rex::Proto::IAX2::Client

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(uopts = {}) ⇒ Client

Returns a new instance of Client.



20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/rex/proto/iax2/client.rb', line 20

def initialize(uopts={})
  opts = {
    :caller_number => '15555555555',
    :caller_name   => '',
    :server_port   => Constants::IAX2_DEFAULT_PORT,
    :context       => { }
  }.merge(uopts)

  self.caller_name   = opts[:caller_name]
  self.caller_number = opts[:caller_number]
  self.server_host   = opts[:server_host]
  self.server_port   = opts[:server_port]
  self.username      = opts[:username]
  self.password      = opts[:password]
  self.debugging     = opts[:debugging]

  self.sock = Rex::Socket::Udp.create(
    'PeerHost' => self.server_host,
    'PeerPort' => self.server_port,
    'Context'  => opts[:context]
  )

  self.monitor   = ::Thread.new { monitor_socket }

  self.src_call_idx = 0
  self.calls = {}

end

Instance Attribute Details

#caller_nameObject

Returns the value of attribute caller_name.



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

def caller_name
  @caller_name
end

#caller_numberObject

Returns the value of attribute caller_number.



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

def caller_number
  @caller_number
end

#callsObject

Returns the value of attribute calls.



18
19
20
# File 'lib/rex/proto/iax2/client.rb', line 18

def calls
  @calls
end

#debuggingObject

Returns the value of attribute debugging.



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

def debugging
  @debugging
end

#monitorObject

Returns the value of attribute monitor.



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

def monitor
  @monitor
end

#passwordObject

Returns the value of attribute password.



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

def password
  @password
end

#server_hostObject

Returns the value of attribute server_host.



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

def server_host
  @server_host
end

#server_portObject

Returns the value of attribute server_port.



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

def server_port
  @server_port
end

#sockObject

Returns the value of attribute sock.



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

def sock
  @sock
end

#src_call_idxObject

Returns the value of attribute src_call_idx.



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

def src_call_idx
  @src_call_idx
end

#usernameObject

Returns the value of attribute username.



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

def username
  @username
end

Instance Method Details

#allocate_call_idObject



106
107
108
109
110
111
112
113
# File 'lib/rex/proto/iax2/client.rb', line 106

def allocate_call_id
  res = ( self.src_call_idx += 1 )
  if ( res > 0x8000 )
    self.src_call_idx = 1
    res = 1
  end
  res
end

#create_callObject



53
54
55
56
# File 'lib/rex/proto/iax2/client.rb', line 53

def create_call
  cid  = allocate_call_id()
  self.calls[ cid ] = IAX2::Call.new(self, cid)
end

#create_ie(ie_type, ie_data) ⇒ Object



197
198
199
# File 'lib/rex/proto/iax2/client.rb', line 197

def create_ie(ie_type, ie_data)
  [ie_type, ie_data.length].pack('CC') + ie_data
end

#create_pkt(src_call, dst_call, tstamp, out_seq, inp_seq, itype, data) ⇒ Object



201
202
203
204
205
206
207
208
209
210
# File 'lib/rex/proto/iax2/client.rb', line 201

def create_pkt(src_call, dst_call, tstamp, out_seq, inp_seq, itype, data)
  [
    src_call | 0x8000,  # High bit indicates a full packet
    dst_call,
    tstamp,
    out_seq & 0xff,     # Sequence numbers wrap at 8-bits
    inp_seq & 0xff,     # Sequence numbers wrap at 8-bits
    itype
  ].pack('nnNCCC') + data
end

#dprint(msg) ⇒ Object



115
116
117
118
# File 'lib/rex/proto/iax2/client.rb', line 115

def dprint(msg)
  return if not self.debugging
  $stderr.puts "[#{::Time.now.to_s}] #{msg}"
end

#matching_call(pkt) ⇒ Object



86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
# File 'lib/rex/proto/iax2/client.rb', line 86

def matching_call(pkt)
  src_call = pkt[0,2].unpack('n')[0]
  dst_call = nil

  if (src_call & 0x8000 != 0)
    dst_call = pkt[2,2].unpack('n')[0]
    dst_call ^= 0x8000 if (dst_call & 0x8000 != 0)
  end

  src_call ^= 0x8000 if (src_call & 0x8000 != 0)

  # Find a matching call in our list
  mcall = self.calls.values.select {|x| x.dcall == src_call or (dst_call and x.scall == dst_call) }.first
  if not mcall
    dprint("Packet received for non-existent call #{[src_call, dst_call].inspect}  vs #{self.calls.values.map{|x| [x.dcall, x.scall]}.inspect}")
    return
  end
  mcall
end

#monitor_socketObject

Transport



62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
# File 'lib/rex/proto/iax2/client.rb', line 62

def monitor_socket
  while true
    begin
      pkt, src = self.sock.recvfrom(65535)
      next if not pkt

      # Find the matching call object
      mcall = matching_call(pkt)
      next if not mcall

      if (pkt[0,1].unpack("C")[0] & 0x80) != 0
        mcall.handle_control(pkt)
      else
        # Dispatch the buffer via the call handler
        mcall.handle_audio(pkt)
      end
    rescue ::Exception => e
      dprint("monitor_socket: #{e.class} #{e} #{e.backtrace}")
      break
    end
  end
  self.sock.close rescue nil
end

#send_ack(call) ⇒ Object



128
129
130
131
# File 'lib/rex/proto/iax2/client.rb', line 128

def send_ack(call)
  data =	[ Constants::IAX_SUBTYPE_ACK ].pack('C')
  send_data( call, create_pkt( call.scall, call.dcall, call.timestamp, call.oseq, call.iseq, Constants::IAX_TYPE_IAX, data ), false )
end

#send_authrep_chall_response(call, chall) ⇒ Object



171
172
173
174
175
176
177
# File 'lib/rex/proto/iax2/client.rb', line 171

def send_authrep_chall_response(call, chall)
  data =
    [ Constants::IAX_SUBTYPE_AUTHREP ].pack('C') +
    create_ie(Constants::IAX_IE_CHALLENGE_RESP, ::Digest::MD5.hexdigest( chall + self.password ))

  send_data( call, create_pkt( call.scall, call.dcall, call.timestamp, call.oseq, call.iseq, Constants::IAX_TYPE_IAX, data ) )
end

#send_data(call, data, inc_seq = true) ⇒ Object



120
121
122
123
124
125
126
# File 'lib/rex/proto/iax2/client.rb', line 120

def send_data(call, data, inc_seq = true )
  r = self.sock.sendto(data, self.server_host, self.server_port, 0)
  if inc_seq
    call.oseq = (call.oseq + 1) & 0xff
  end
  r
end

#send_hangup(call) ⇒ Object



149
150
151
152
# File 'lib/rex/proto/iax2/client.rb', line 149

def send_hangup(call)
  data =	[ Constants::IAX_SUBTYPE_HANGUP ].pack('C')
  send_data( call, create_pkt( call.scall, call.dcall, call.timestamp, call.oseq, call.iseq, Constants::IAX_TYPE_IAX, data ) )
end

#send_invalid(call) ⇒ Object



144
145
146
147
# File 'lib/rex/proto/iax2/client.rb', line 144

def send_invalid(call)
  data =	[ Constants::IAX_SUBTYPE_INVAL ].pack('C')
  send_data( call, create_pkt( call.scall, call.dcall, call.timestamp, call.oseq, call.iseq, Constants::IAX_TYPE_IAX, data ) )
end

#send_lagrp(call, stamp) ⇒ Object



138
139
140
141
# File 'lib/rex/proto/iax2/client.rb', line 138

def send_lagrp(call, stamp)
  data =	[ Constants::IAX_SUBTYPE_LAGRP ].pack('C')
  send_data( call, create_pkt( call.scall, call.dcall, stamp, call.oseq, call.iseq, Constants::IAX_TYPE_IAX, data ) )
end

#send_new(call, number) ⇒ Object



154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
# File 'lib/rex/proto/iax2/client.rb', line 154

def send_new(call, number)
  data = [ Constants::IAX_SUBTYPE_NEW ].pack('C')

  cid = call.caller_number || self.caller_number
  cid = number if cid == 'SELF'

  data << create_ie(Constants::IAX_IE_CALLING_NUMBER, cid )
  data << create_ie(Constants::IAX_IE_CALLING_NAME, call.caller_name || self.caller_name)
  data << create_ie(Constants::IAX_IE_DESIRED_CODEC, [Constants::IAX_SUPPORTED_CODECS].pack("N") )
  data << create_ie(Constants::IAX_IE_ACTUAL_CODECS, [Constants::IAX_SUPPORTED_CODECS].pack("N") )
  data << create_ie(Constants::IAX_IE_USERNAME, self.username) if self.username
  data << create_ie(Constants::IAX_IE_CALLED_NUMBER, number)
  data << create_ie(Constants::IAX_IE_ORIGINAL_DID, number)

  send_data( call, create_pkt( call.scall, call.dcall, call.timestamp, call.oseq, call.iseq, Constants::IAX_TYPE_IAX, data ) )
end

#send_pong(call, stamp) ⇒ Object



133
134
135
136
# File 'lib/rex/proto/iax2/client.rb', line 133

def send_pong(call, stamp)
  data =	[ Constants::IAX_SUBTYPE_PONG ].pack('C')
  send_data( call, create_pkt( call.scall, call.dcall, stamp, call.oseq, call.iseq, Constants::IAX_TYPE_IAX, data ) )
end

#send_regreq(call) ⇒ Object



179
180
181
182
183
184
185
# File 'lib/rex/proto/iax2/client.rb', line 179

def send_regreq(call)
  data = [ Constants::IAX_SUBTYPE_REGREQ ].pack('C')
  data << create_ie(Constants::IAX_IE_USERNAME, self.username) if self.username
  data << create_ie(Constants::IAX_IE_REG_REFRESH, [Constants::IAX_DEFAULT_REG_REFRESH].pack('n'))

  send_data( call, create_pkt( call.scall, call.dcall, call.timestamp, call.oseq, call.iseq, Constants::IAX_TYPE_IAX, data ) )
end

#send_regreq_chall_response(call, chall) ⇒ Object



187
188
189
190
191
192
193
194
195
# File 'lib/rex/proto/iax2/client.rb', line 187

def send_regreq_chall_response(call, chall)
  data =
    [ Constants::IAX_SUBTYPE_REGREQ ].pack('C') +
    create_ie(Constants::IAX_IE_USERNAME, self.username) +
    create_ie(Constants::IAX_IE_CHALLENGE_RESP, ::Digest::MD5.hexdigest( chall + self.password )) +
    create_ie(Constants::IAX_IE_REG_REFRESH, [Constants::IAX_DEFAULT_REG_REFRESH].pack('n'))

  send_data( call, create_pkt( call.scall, call.dcall, call.timestamp, call.oseq, call.iseq, Constants::IAX_TYPE_IAX, data ) )
end

#shutdownObject



49
50
51
# File 'lib/rex/proto/iax2/client.rb', line 49

def shutdown
  self.monitor.kill rescue nil
end