Class: OverSIP::SIP::InviteClientTransaction

Inherits:
ClientTransaction show all
Defined in:
lib/oversip/sip/client_transaction.rb

Constant Summary

Constants included from Logger

Logger::SYSLOG_POSIXMQ_MAPPING

Instance Attribute Summary

Attributes inherited from ClientTransaction

#connection, #core, #request, #state

Instance Method Summary collapse

Methods inherited from ClientTransaction

get_class

Methods included from Logger

close, #fatal, fg_system_msg2str, init_logger_mq, load_methods, #log_id, syslog_system_msg2str, syslog_user_msg2str

Constructor Details

#initialize(core, request, transaction_conf, transport, ip = nil, ip_type = nil, port = nil) ⇒ InviteClientTransaction

Returns a new instance of InviteClientTransaction.



75
76
77
78
79
80
81
# File 'lib/oversip/sip/client_transaction.rb', line 75

def initialize core, request, transaction_conf, transport, ip=nil, ip_type=nil, port=nil
  super
  @log_id = "ICT #{@transaction_id}"

  # Can be :calling, :proceeding, :completed, :accepted or :terminated.
  @state = :calling
end

Instance Method Details

#connection_failedObject



256
257
258
259
260
261
262
263
264
265
266
267
268
# File 'lib/oversip/sip/client_transaction.rb', line 256

def connection_failed
  # This avoid the case in which the TCP connection timeout raises after the transaction timeout.
  # Neither we react if the transaction has been canceled and the CANCEL cannot be sent due to
  # TCP disconnection.
  return unless @state == :calling or not @cancel

  @timer_A.cancel  if @timer_A
  @timer_B.cancel
  @timer_C.cancel
  terminate_transaction

  @core.connection_failed
end

#do_cancel(cancel = nil) ⇒ Object

It receives the received CANCEL request as parameter so it can check the existence of Reason header and act according (RFC 3326). This method is also called (without argument) when Timer C expires (INVITE).



311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
# File 'lib/oversip/sip/client_transaction.rb', line 311

def do_cancel cancel=nil
  return if @cancel

  @cancel = "CANCEL #{@request.ruri} SIP/2.0\r\n"
  @cancel << "Via: #{@top_via}\r\n"

  @request.hdr_route.each do |route|
    @cancel << "Route: " << route << "\r\n"
  end  if @request.hdr_route

  # RFC 3326. Copy Reason headers if present in the received CANCEL.
  cancel.header_all("Reason").each do |reason|
    @cancel << "Reason: " << reason << "\r\n"
  end  if cancel

  @cancel << "From: " << @request.hdr_from << "\r\n"
  @cancel << "To: " << @request.hdr_to << "\r\n"
  @cancel << "Call-ID: " << @request.call_id << "\r\n"
  @cancel << "CSeq: " << @request.cseq.to_s << " CANCEL\r\n"
  @cancel << "Content-Length: 0\r\n"
  @cancel << HDR_USER_AGENT << "\r\n"
  @cancel << "\r\n"

  # Just send the ACK inmediately if the branch has replied a 1XX response.
  send_cancel  if @state == :proceeding
end

#receive_response(response) ⇒ Object



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
# File 'lib/oversip/sip/client_transaction.rb', line 183

def receive_response response
  # Set the request attribute to the response so we can access the related outgoing request.
  response.request = @request

  # Set server transaction variables to the response.
  response.tvars = @request.tvars

  # Provisional response
  if response.status_code < 200
    case @state
    when :calling
      @state = :proceeding
      @timer_A.cancel  if @timer_A
      @timer_B.cancel
      @core.receive_response(response) unless response.status_code == 100
      # RFC 3261 - 9.1 states that a CANCEL must be sent after receiving a 1XX response.
      send_cancel if @cancel
      return true
    when :proceeding
      @core.receive_response(response) unless response.status_code == 100
      return true
    else
      log_system_notice "received a provisional response #{response.status_code} while in #{@state} state"
      return false
    end

  # [3456]XX final response.
  elsif response.status_code >= 300
    case @state
    when :calling, :proceeding
      @state = :completed
      @timer_A.cancel  if @timer_A
      @timer_B.cancel
      @timer_C.cancel
      if @transport == :udp
        start_timer_D
      else
        terminate_transaction
      end
      send_ack(response)
      @core.receive_response(response)
      return true
    when :completed
      send_ack(response)
      return false
    when :accepted
      log_system_notice "received a [3456]XX response while in accepted state, ignoring it"
      return false
    end

  # 2XX final response.
  else
    case @state
    when :calling, :proceeding
      @state = :accepted
      @timer_A.cancel  if @timer_A
      @timer_B.cancel
      @timer_C.cancel
      start_timer_M
      @core.receive_response(response)
      return true
    when :accepted
      @core.receive_response(response)
      return true
    when :completed
      ### NOTE: It could be accepted and bypassed to the UAC, but makes no sense.
      log_system_notice "received 2XX response while in completed state, ignoring it"
      return false
    end

  end
end

#receive_response_to_cancel(response) ⇒ Object



370
371
372
373
374
375
376
377
378
379
380
# File 'lib/oversip/sip/client_transaction.rb', line 370

def receive_response_to_cancel(response)
  unless @state == :terminated
    log_system_debug "our CANCEL got a #{response.status_code} response, transaction terminated"  if $oversip_debug

    @timer_E_cancel.cancel  if @timer_E_cancel
    @timer_F_cancel.cancel
    # We MUST ensure that we end the client transaction, so after sending a CANCEL and get a response
    # for it, ensure the transaction is terminated after a while.
    ::EM.add_timer(4) { terminate_transaction }
  end
end

#retransmit_cancelObject



366
367
368
# File 'lib/oversip/sip/client_transaction.rb', line 366

def retransmit_cancel
  @connection.send_sip_msg @cancel, @ip, @port
end

#retransmit_requestObject



179
180
181
# File 'lib/oversip/sip/client_transaction.rb', line 179

def retransmit_request
  @connection.send_sip_msg @request_leg_b, @ip, @port
end

#send_ack(response) ⇒ Object



281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
# File 'lib/oversip/sip/client_transaction.rb', line 281

def send_ack response
  unless @ack
    @ack = "ACK #{@request.ruri} SIP/2.0\r\n"
    @ack << "Via: #{@top_via}\r\n"

    @request.hdr_route.each do |route|
      @ack << "Route: " << route << "\r\n"
    end  if @request.hdr_route

    @ack << "From: " << @request.hdr_from << "\r\n"
    @ack << "To: " << @request.hdr_to
    unless @request.to_tag
      @ack << ";tag=#{response.to_tag}"  if response.to_tag
    end
    @ack << "\r\n"

    @ack << "Call-ID: " << @request.call_id << "\r\n"
    @ack << "CSeq: " << @request.cseq.to_s << " ACK\r\n"
    @ack << "Content-Length: 0\r\n"
    @ack << HDR_USER_AGENT << "\r\n"
    @ack << "\r\n"
  end

  log_system_debug "sending ACK for [3456]XX response"  if $oversip_debug
  @connection.send_sip_msg @ack, @ip, @port
end

#send_cancelObject



338
339
340
341
342
343
344
345
# File 'lib/oversip/sip/client_transaction.rb', line 338

def send_cancel
  log_system_debug "sending CANCEL"  if $oversip_debug

  @connection.send_sip_msg @cancel, @ip, @port

  start_timer_E_cancel  if @transport == :udp
  start_timer_F_cancel
end

#send_requestObject



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
115
116
117
118
119
120
121
122
123
124
125
126
127
128
# File 'lib/oversip/sip/client_transaction.rb', line 83

def send_request
  @client_transactions = @server_klass.invite_client_transactions
  # Store the new client transaction.
  @client_transactions[@transaction_id] = self

  @top_via = "#{@server_klass.via_core};branch=z9hG4bK#{@transaction_id};rport"
  @request.insert_header "Via", @top_via

  case @request.in_rr
  # Add a second Record-Route just in case there is transport change.
  when :rr
    unless @request.connection.is_a?(@server_klass)
      @out_rr = :rr
      @request.insert_header "Record-Route", @server_klass.record_route
    end
  # When there is outgoing Outbound always add a second Record-Route header.
  when :outgoing_outbound_rr
    @out_rr = :rr
    @request.insert_header "Record-Route", @server_klass.record_route
  # When there is incoming Outbound always add a second Record-Route header containing the flow token.
  when :incoming_outbound_rr
    @out_rr = :rr
    @request.insert_header "Record-Route", "<sip:" << @request.route_outbound_flow_token << @server_klass.outbound_record_route_fragment
  # When there is both incoming and outgoing Outbound always add a second Record-Route header containing the flow token.
  when :both_outbound_rr
    @out_rr = :rr
    @request.insert_header "Record-Route", "<sip:" << @request.route_outbound_flow_token << @server_klass.outbound_record_route_fragment
  end

  @request_leg_b = @request.to_s

  # NOTE: This cannot return false as the connection has been retrieved from the corresponding hash,
  # and when a connection is terminated its value is automatically deleted from such hash.
  @connection.send_sip_msg @request_leg_b, @ip, @port

  @request.delete_header_top "Via"
  if @out_rr == :rr
    @request.delete_header_top "Record-Route"
  end

  start_timer_A  if @transport == :udp
  start_timer_B
  start_timer_C

  true
end

#start_timer_AObject



130
131
132
133
134
135
136
137
# File 'lib/oversip/sip/client_transaction.rb', line 130

def start_timer_A
  @timer_A_interval = TIMER_A
  @timer_A = ::EM::PeriodicTimer.new(@timer_A_interval) do
    log_system_debug "timer A expires, retransmitting request"  if $oversip_debug
    retransmit_request
    @timer_A_interval = @timer_A.interval = 2*@timer_A_interval
  end
end

#start_timer_BObject



139
140
141
142
143
144
145
146
147
# File 'lib/oversip/sip/client_transaction.rb', line 139

def start_timer_B
  @timer_B = ::EM::Timer.new(@transaction_conf[:timer_B] || TIMER_B) do
    log_system_debug "timer B expires, transaction timeout"  if $oversip_debug
    @timer_A.cancel  if @timer_A
    @timer_C.cancel
    terminate_transaction
    @core.client_timeout
  end
end

#start_timer_CObject



149
150
151
152
153
154
155
156
157
# File 'lib/oversip/sip/client_transaction.rb', line 149

def start_timer_C
  @timer_C = ::EM::Timer.new(@transaction_conf[:timer_C] || TIMER_C) do
    log_system_debug "timer C expires, transaction timeout"  if $oversip_debug
    @timer_A.cancel  if @timer_A
    @timer_B.cancel
    do_cancel
    @core.invite_timeout
  end
end

#start_timer_DObject



159
160
161
162
163
164
# File 'lib/oversip/sip/client_transaction.rb', line 159

def start_timer_D
  ::EM.add_timer(TIMER_D_UDP) do
    log_system_debug "timer D expires, transaction terminated"  if $oversip_debug
    terminate_transaction
  end
end

#start_timer_E_cancelObject



347
348
349
350
351
352
353
354
# File 'lib/oversip/sip/client_transaction.rb', line 347

def start_timer_E_cancel
  @timer_E_cancel_interval = TIMER_E
  @timer_E_cancel = ::EM::PeriodicTimer.new(@timer_E_cancel_interval) do
    log_system_debug "timer E expires, retransmitting CANCEL"  if $oversip_debug
    retransmit_cancel
    @timer_E_cancel_interval = @timer_E_cancel.interval = [2*@timer_E_cancel_interval, T2].min
  end
end

#start_timer_F_cancelObject



356
357
358
359
360
361
362
363
364
# File 'lib/oversip/sip/client_transaction.rb', line 356

def start_timer_F_cancel
  @timer_F_cancel = ::EM::Timer.new(@transaction_conf[:timer_F] || TIMER_F) do
    unless @state == :terminated
      log_system_debug "timer F expires, CANCEL timeout, transaction terminated"  if $oversip_debug
      @timer_E_cancel.cancel  if @timer_E_cancel
      terminate_transaction
    end
  end
end

#start_timer_MObject



166
167
168
169
170
171
# File 'lib/oversip/sip/client_transaction.rb', line 166

def start_timer_M
  ::EM.add_timer(TIMER_M) do
    log_system_debug "timer M expires, transaction terminated"  if $oversip_debug
    terminate_transaction
  end
end

#terminate_transactionObject

Terminate current transaction and delete from the list of transactions.



174
175
176
177
# File 'lib/oversip/sip/client_transaction.rb', line 174

def terminate_transaction
  @state = :terminated
  @client_transactions.delete(@transaction_id)
end

#tls_validation_failedObject



270
271
272
273
274
275
276
277
278
279
# File 'lib/oversip/sip/client_transaction.rb', line 270

def tls_validation_failed
  return unless @state == :calling or not @cancel

  @timer_A.cancel  if @timer_A
  @timer_B.cancel
  @timer_C.cancel
  terminate_transaction

  @core.tls_validation_failed
end