1
/* $Id: sip_transaction.c 3553 2011-05-05 06:14:19Z nanang $ */
3
* Copyright (C) 2008-2011 Teluu Inc. (http://www.teluu.com)
4
* Copyright (C) 2003-2008 Benny Prijono <benny@prijono.org>
6
* This program is free software; you can redistribute it and/or modify
7
* it under the terms of the GNU General Public License as published by
8
* the Free Software Foundation; either version 2 of the License, or
9
* (at your option) any later version.
11
* This program is distributed in the hope that it will be useful,
12
* but WITHOUT ANY WARRANTY; without even the implied warranty of
13
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14
* GNU General Public License for more details.
16
* You should have received a copy of the GNU General Public License
17
* along with this program; if not, write to the Free Software
18
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
20
#include <pjsip/sip_transaction.h>
21
#include <pjsip/sip_util.h>
22
#include <pjsip/sip_module.h>
23
#include <pjsip/sip_endpoint.h>
24
#include <pjsip/sip_errno.h>
25
#include <pjsip/sip_event.h>
26
#include <pjlib-util/errno.h>
30
#include <pj/string.h>
31
#include <pj/assert.h>
35
#define THIS_FILE "sip_transaction.c"
38
#define TSX_TRACE_(expr) PJ_LOG(3,expr)
40
#define TSX_TRACE_(expr)
43
/* When this macro is set, transaction will keep the hashed value
44
* so that future lookup (to unregister transaction) does not need
45
* to recalculate the hash again. It should gains a little bit of
46
* performance, so generally we'd want this.
51
/* Defined in sip_util_statefull.c */
52
extern pjsip_module mod_stateful_util;
55
/*****************************************************************************
57
** Declarations and static variable definitions section.
59
*****************************************************************************
62
static pj_status_t mod_tsx_layer_load(pjsip_endpoint *endpt);
63
static pj_status_t mod_tsx_layer_start(void);
64
static pj_status_t mod_tsx_layer_stop(void);
65
static pj_status_t mod_tsx_layer_unload(void);
66
static pj_bool_t mod_tsx_layer_on_rx_request(pjsip_rx_data *rdata);
67
static pj_bool_t mod_tsx_layer_on_rx_response(pjsip_rx_data *rdata);
69
/* Transaction layer module definition. */
70
static struct mod_tsx_layer
72
struct pjsip_module mod;
74
pjsip_endpoint *endpt;
76
pj_hash_table_t *htable;
79
NULL, NULL, /* List's prev and next. */
80
{ "mod-tsx-layer", 13 }, /* Module name. */
82
PJSIP_MOD_PRIORITY_TSX_LAYER, /* Priority. */
83
mod_tsx_layer_load, /* load(). */
84
mod_tsx_layer_start, /* start() */
85
mod_tsx_layer_stop, /* stop() */
86
mod_tsx_layer_unload, /* unload() */
87
mod_tsx_layer_on_rx_request, /* on_rx_request() */
88
mod_tsx_layer_on_rx_response, /* on_rx_response() */
93
/* Thread Local Storage ID for transaction lock */
94
static long pjsip_tsx_lock_tls_id;
96
/* Transaction state names */
97
static const char *state_str[] =
110
static const char *role_name[] =
116
/* Transport flag. */
119
TSX_HAS_PENDING_TRANSPORT = 1,
120
TSX_HAS_PENDING_RESCHED = 2,
121
TSX_HAS_PENDING_SEND = 4,
122
TSX_HAS_PENDING_DESTROY = 8,
123
TSX_HAS_RESOLVED_SERVER = 16,
126
/* Transaction lock. */
127
typedef struct tsx_lock_data {
128
struct tsx_lock_data *prev;
129
pjsip_transaction *tsx;
134
/* Timer timeout value constants */
135
static pj_time_val t1_timer_val = { PJSIP_T1_TIMEOUT/1000,
136
PJSIP_T1_TIMEOUT%1000 };
137
static pj_time_val t2_timer_val = { PJSIP_T2_TIMEOUT/1000,
138
PJSIP_T2_TIMEOUT%1000 };
139
static pj_time_val t4_timer_val = { PJSIP_T4_TIMEOUT/1000,
140
PJSIP_T4_TIMEOUT%1000 };
141
static pj_time_val td_timer_val = { PJSIP_TD_TIMEOUT/1000,
142
PJSIP_TD_TIMEOUT%1000 };
143
static pj_time_val timeout_timer_val = { (64*PJSIP_T1_TIMEOUT)/1000,
144
(64*PJSIP_T1_TIMEOUT)%1000 };
146
#define TIMER_INACTIVE 0
147
#define TIMER_ACTIVE 1
149
/* Delay for 1xx retransmission (should be 60 seconds).
150
* Specify 0 to disable this feature
152
#ifndef PJSIP_TSX_1XX_RETRANS_DELAY
153
# define PJSIP_TSX_1XX_RETRANS_DELAY 60
158
static void lock_tsx(pjsip_transaction *tsx, struct tsx_lock_data *lck);
159
static pj_status_t unlock_tsx( pjsip_transaction *tsx,
160
struct tsx_lock_data *lck);
161
static pj_status_t tsx_on_state_null( pjsip_transaction *tsx,
163
static pj_status_t tsx_on_state_calling( pjsip_transaction *tsx,
165
static pj_status_t tsx_on_state_trying( pjsip_transaction *tsx,
167
static pj_status_t tsx_on_state_proceeding_uas( pjsip_transaction *tsx,
169
static pj_status_t tsx_on_state_proceeding_uac( pjsip_transaction *tsx,
171
static pj_status_t tsx_on_state_completed_uas( pjsip_transaction *tsx,
173
static pj_status_t tsx_on_state_completed_uac( pjsip_transaction *tsx,
175
static pj_status_t tsx_on_state_confirmed( pjsip_transaction *tsx,
177
static pj_status_t tsx_on_state_terminated( pjsip_transaction *tsx,
179
static pj_status_t tsx_on_state_destroyed( pjsip_transaction *tsx,
181
static void tsx_timer_callback( pj_timer_heap_t *theap,
182
pj_timer_entry *entry);
183
static void tsx_tp_state_callback(
185
pjsip_transport_state state,
186
const pjsip_transport_state_info *info);
187
static pj_status_t tsx_create( pjsip_module *tsx_user,
188
pjsip_transaction **p_tsx);
189
static pj_status_t tsx_destroy( pjsip_transaction *tsx );
190
static void tsx_resched_retransmission( pjsip_transaction *tsx );
191
static pj_status_t tsx_retransmit( pjsip_transaction *tsx, int resched);
192
static int tsx_send_msg( pjsip_transaction *tsx,
193
pjsip_tx_data *tdata);
194
static void tsx_update_transport( pjsip_transaction *tsx,
195
pjsip_transport *tp);
198
/* State handlers for UAC, indexed by state */
199
static int (*tsx_state_handler_uac[PJSIP_TSX_STATE_MAX])(pjsip_transaction *,
203
&tsx_on_state_calling,
205
&tsx_on_state_proceeding_uac,
206
&tsx_on_state_completed_uac,
207
&tsx_on_state_confirmed,
208
&tsx_on_state_terminated,
209
&tsx_on_state_destroyed,
212
/* State handlers for UAS */
213
static int (*tsx_state_handler_uas[PJSIP_TSX_STATE_MAX])(pjsip_transaction *,
218
&tsx_on_state_trying,
219
&tsx_on_state_proceeding_uas,
220
&tsx_on_state_completed_uas,
221
&tsx_on_state_confirmed,
222
&tsx_on_state_terminated,
223
&tsx_on_state_destroyed,
226
/*****************************************************************************
230
*****************************************************************************
233
* Get transaction state name.
235
PJ_DEF(const char *) pjsip_tsx_state_str(pjsip_tsx_state_e state)
237
return state_str[state];
243
PJ_DEF(const char *) pjsip_role_name(pjsip_role_e role)
245
return role_name[role];
250
* Create transaction key for RFC2543 compliant messages, which don't have
251
* unique branch parameter in the top most Via header.
253
* INVITE requests matches a transaction if the following attributes
254
* match the original request:
262
* CANCEL matching is done similarly as INVITE, except:
263
* - CSeq method will differ
264
* - To tag is not matched.
266
* ACK matching is done similarly, except that:
267
* - method of the CSeq will differ,
268
* - To tag is matched to the response sent by the server transaction.
270
* The transaction key is constructed from the common components of above
271
* components. Additional comparison is needed to fully match a transaction.
273
static pj_status_t create_tsx_key_2543( pj_pool_t *pool,
276
const pjsip_method *method,
277
const pjsip_rx_data *rdata )
279
#define SEPARATOR '$'
282
pj_size_t len_required;
286
PJ_ASSERT_RETURN(pool && str && method && rdata, PJ_EINVAL);
287
PJ_ASSERT_RETURN(rdata->msg_info.msg, PJ_EINVAL);
288
PJ_ASSERT_RETURN(rdata->msg_info.via, PJSIP_EMISSINGHDR);
289
PJ_ASSERT_RETURN(rdata->msg_info.cseq, PJSIP_EMISSINGHDR);
290
PJ_ASSERT_RETURN(rdata->msg_info.from, PJSIP_EMISSINGHDR);
292
host = &rdata->msg_info.via->sent_by.host;
293
req_uri = (pjsip_uri*)rdata->msg_info.msg->line.req.uri;
295
/* Calculate length required. */
296
len_required = 9 + /* CSeq number */
297
rdata->msg_info.from->tag.slen + /* From tag. */
298
rdata->msg_info.cid->id.slen + /* Call-ID */
299
host->slen + /* Via host. */
301
16; /* Separator+Allowance. */
302
key = p = (char*) pj_pool_alloc(pool, len_required);
303
end = p + len_required;
306
*p++ = (char)(role==PJSIP_ROLE_UAC ? 'c' : 's');
309
/* Add method, except when method is INVITE or ACK. */
310
if (method->id != PJSIP_INVITE_METHOD && method->id != PJSIP_ACK_METHOD) {
311
pj_memcpy(p, method->name.ptr, method->name.slen);
312
p += method->name.slen;
316
/* Add CSeq (only the number). */
317
len = pj_utoa(rdata->msg_info.cseq->cseq, p);
322
len = rdata->msg_info.from->tag.slen;
323
pj_memcpy( p, rdata->msg_info.from->tag.ptr, len);
328
len = rdata->msg_info.cid->id.slen;
329
pj_memcpy( p, rdata->msg_info.cid->id.ptr, len );
333
/* Add top Via header.
334
* We don't really care whether the port contains the real port (because
335
* it can be omited if default port is used). Anyway this function is
336
* only used to match request retransmission, and we expect that the
337
* request retransmissions will contain the same port.
339
pj_memcpy(p, host->ptr, host->slen);
343
len = pj_utoa(rdata->msg_info.via->sent_by.port, p);
357
* Create transaction key for RFC3161 compliant system.
359
static pj_status_t create_tsx_key_3261( pj_pool_t *pool,
362
const pjsip_method *method,
363
const pj_str_t *branch)
367
PJ_ASSERT_RETURN(pool && key && method && branch, PJ_EINVAL);
369
p = key->ptr = (char*)
370
pj_pool_alloc(pool, branch->slen + method->name.slen + 4 );
373
*p++ = (char)(role==PJSIP_ROLE_UAC ? 'c' : 's');
376
/* Add method, except when method is INVITE or ACK. */
377
if (method->id != PJSIP_INVITE_METHOD && method->id != PJSIP_ACK_METHOD) {
378
pj_memcpy(p, method->name.ptr, method->name.slen);
379
p += method->name.slen;
384
pj_memcpy(p, branch->ptr, branch->slen);
388
key->slen = p - key->ptr;
394
* Create key from the incoming data, to be used to search the transaction
395
* in the transaction hash table.
397
PJ_DEF(pj_status_t) pjsip_tsx_create_key( pj_pool_t *pool, pj_str_t *key,
399
const pjsip_method *method,
400
const pjsip_rx_data *rdata)
402
pj_str_t rfc3261_branch = {PJSIP_RFC3261_BRANCH_ID,
403
PJSIP_RFC3261_BRANCH_LEN};
406
/* Get the branch parameter in the top-most Via.
407
* If branch parameter is started with "z9hG4bK", then the message was
408
* generated by agent compliant with RFC3261. Otherwise, it will be
409
* handled as RFC2543.
411
const pj_str_t *branch = &rdata->msg_info.via->branch_param;
413
if (pj_strncmp(branch,&rfc3261_branch,PJSIP_RFC3261_BRANCH_LEN)==0) {
415
/* Create transaction key. */
416
return create_tsx_key_3261(pool, key, role, method, branch);
419
/* Create the key for the message. This key will be matched up
420
* with the transaction key. For RFC2563 transactions, the
421
* transaction key was created by the same function, so it will
424
return create_tsx_key_2543( pool, key, role, method, rdata );
428
/*****************************************************************************
430
** Transaction layer module
432
*****************************************************************************
435
* Create transaction layer module and registers it to the endpoint.
437
PJ_DEF(pj_status_t) pjsip_tsx_layer_init_module(pjsip_endpoint *endpt)
443
PJ_ASSERT_RETURN(mod_tsx_layer.endpt==NULL, PJ_EINVALIDOP);
445
/* Initialize timer values */
446
t1_timer_val.sec = pjsip_cfg()->tsx.t1 / 1000;
447
t1_timer_val.msec = pjsip_cfg()->tsx.t1 % 1000;
448
t2_timer_val.sec = pjsip_cfg()->tsx.t2 / 1000;
449
t2_timer_val.msec = pjsip_cfg()->tsx.t2 % 1000;
450
t4_timer_val.sec = pjsip_cfg()->tsx.t4 / 1000;
451
t4_timer_val.msec = pjsip_cfg()->tsx.t4 % 1000;
452
td_timer_val.sec = pjsip_cfg()->tsx.td / 1000;
453
td_timer_val.msec = pjsip_cfg()->tsx.td % 1000;
454
/* Changed the initialization below to use td_timer_val instead, to enable
455
* customization to the timeout value.
457
//timeout_timer_val.sec = (64 * pjsip_cfg()->tsx.t1) / 1000;
458
//timeout_timer_val.msec = (64 * pjsip_cfg()->tsx.t1) % 1000;
459
timeout_timer_val = td_timer_val;
461
/* Initialize TLS ID for transaction lock. */
462
status = pj_thread_local_alloc(&pjsip_tsx_lock_tls_id);
463
if (status != PJ_SUCCESS)
466
pj_thread_local_set(pjsip_tsx_lock_tls_id, NULL);
469
* Initialize transaction layer structure.
472
/* Create pool for the module. */
473
pool = pjsip_endpt_create_pool(endpt, "tsxlayer",
474
PJSIP_POOL_TSX_LAYER_LEN,
475
PJSIP_POOL_TSX_LAYER_INC );
480
/* Initialize some attributes. */
481
mod_tsx_layer.pool = pool;
482
mod_tsx_layer.endpt = endpt;
485
/* Create hash table. */
486
mod_tsx_layer.htable = pj_hash_create( pool, pjsip_cfg()->tsx.max_count );
487
if (!mod_tsx_layer.htable) {
488
pjsip_endpt_release_pool(endpt, pool);
493
status = pj_mutex_create_recursive(pool, "tsxlayer", &mod_tsx_layer.mutex);
494
if (status != PJ_SUCCESS) {
495
pjsip_endpt_release_pool(endpt, pool);
500
* Register transaction layer module to endpoint.
502
status = pjsip_endpt_register_module( endpt, &mod_tsx_layer.mod );
503
if (status != PJ_SUCCESS) {
504
pj_mutex_destroy(mod_tsx_layer.mutex);
505
pjsip_endpt_release_pool(endpt, pool);
509
/* Register mod_stateful_util module (sip_util_statefull.c) */
510
status = pjsip_endpt_register_module(endpt, &mod_stateful_util);
511
if (status != PJ_SUCCESS) {
520
* Get the instance of transaction layer module.
522
PJ_DEF(pjsip_module*) pjsip_tsx_layer_instance(void)
524
return &mod_tsx_layer.mod;
529
* Unregister and destroy transaction layer module.
531
PJ_DEF(pj_status_t) pjsip_tsx_layer_destroy(void)
533
/* Are we registered? */
534
PJ_ASSERT_RETURN(mod_tsx_layer.endpt!=NULL, PJ_EINVALIDOP);
536
/* Unregister from endpoint.
537
* Clean-ups will be done in the unload() module callback.
539
return pjsip_endpt_unregister_module( mod_tsx_layer.endpt,
545
* Register the transaction to the hash table.
547
static pj_status_t mod_tsx_layer_register_tsx( pjsip_transaction *tsx)
549
pj_assert(tsx->transaction_key.slen != 0);
551
/* Lock hash table mutex. */
552
pj_mutex_lock(mod_tsx_layer.mutex);
554
/* Check if no transaction with the same key exists.
555
* Do not use PJ_ASSERT_RETURN since it evaluates the expression
558
if(pj_hash_get(mod_tsx_layer.htable,
559
tsx->transaction_key.ptr,
560
tsx->transaction_key.slen,
563
pj_mutex_unlock(mod_tsx_layer.mutex);
565
"Unable to register %.*s transaction (key exists)",
566
(int)tsx->method.name.slen,
567
tsx->method.name.ptr));
571
TSX_TRACE_((THIS_FILE,
572
"Transaction %p registered with hkey=0x%p and key=%.*s",
573
tsx, tsx->hashed_key, tsx->transaction_key.slen,
574
tsx->transaction_key.ptr));
576
/* Register the transaction to the hash table. */
578
pj_hash_set( tsx->pool, mod_tsx_layer.htable, tsx->transaction_key.ptr,
579
tsx->transaction_key.slen, tsx->hashed_key, tsx);
581
pj_hash_set( tsx->pool, mod_tsx_layer.htable, tsx->transaction_key.ptr,
582
tsx->transaction_key.slen, 0, tsx);
586
pj_mutex_unlock(mod_tsx_layer.mutex);
593
* Unregister the transaction from the hash table.
595
static void mod_tsx_layer_unregister_tsx( pjsip_transaction *tsx)
597
if (mod_tsx_layer.mod.id == -1) {
598
/* The transaction layer has been unregistered. This could happen
599
* if the transaction was pending on transport and the application
600
* is shutdown. See http://trac.pjsip.org/repos/ticket/1033. In
601
* this case just do nothing.
606
pj_assert(tsx->transaction_key.slen != 0);
607
//pj_assert(tsx->state != PJSIP_TSX_STATE_NULL);
609
/* Lock hash table mutex. */
610
pj_mutex_lock(mod_tsx_layer.mutex);
612
/* Register the transaction to the hash table. */
614
pj_hash_set( NULL, mod_tsx_layer.htable, tsx->transaction_key.ptr,
615
tsx->transaction_key.slen, tsx->hashed_key, NULL);
617
pj_hash_set( NULL, mod_tsx_layer.htable, tsx->transaction_key.ptr,
618
tsx->transaction_key.slen, 0, NULL);
621
TSX_TRACE_((THIS_FILE,
622
"Transaction %p unregistered, hkey=0x%p and key=%.*s",
623
tsx, tsx->hashed_key, tsx->transaction_key.slen,
624
tsx->transaction_key.ptr));
627
pj_mutex_unlock(mod_tsx_layer.mutex);
632
* Retrieve the current number of transactions currently registered in
635
PJ_DEF(unsigned) pjsip_tsx_layer_get_tsx_count(void)
639
/* Are we registered? */
640
PJ_ASSERT_RETURN(mod_tsx_layer.endpt!=NULL, 0);
642
pj_mutex_lock(mod_tsx_layer.mutex);
643
count = pj_hash_count(mod_tsx_layer.htable);
644
pj_mutex_unlock(mod_tsx_layer.mutex);
651
* Find a transaction.
653
PJ_DEF(pjsip_transaction*) pjsip_tsx_layer_find_tsx( const pj_str_t *key,
656
pjsip_transaction *tsx;
657
pj_uint32_t hval = 0;
659
pj_mutex_lock(mod_tsx_layer.mutex);
660
tsx = (pjsip_transaction*)
661
pj_hash_get( mod_tsx_layer.htable, key->ptr, key->slen, &hval );
662
pj_mutex_unlock(mod_tsx_layer.mutex);
664
TSX_TRACE_((THIS_FILE,
665
"Finding tsx with hkey=0x%p and key=%.*s: found %p",
666
hval, key->slen, key->ptr, tsx));
669
* Transaction may gets deleted before we have chance to lock it.
671
PJ_TODO(FIX_RACE_CONDITION_HERE);
673
pj_mutex_lock(tsx->mutex);
679
/* This module callback is called when module is being loaded by
680
* endpoint. It does nothing for this module.
682
static pj_status_t mod_tsx_layer_load(pjsip_endpoint *endpt)
684
PJ_UNUSED_ARG(endpt);
689
/* This module callback is called when module is being started by
690
* endpoint. It does nothing for this module.
692
static pj_status_t mod_tsx_layer_start(void)
698
/* This module callback is called when module is being stopped by
701
static pj_status_t mod_tsx_layer_stop(void)
703
pj_hash_iterator_t it_buf, *it;
705
PJ_LOG(4,(THIS_FILE, "Stopping transaction layer module"));
707
pj_mutex_lock(mod_tsx_layer.mutex);
709
/* Destroy all transactions. */
710
it = pj_hash_first(mod_tsx_layer.htable, &it_buf);
712
pjsip_transaction *tsx = (pjsip_transaction*)
713
pj_hash_this(mod_tsx_layer.htable, it);
714
pj_hash_iterator_t *next = pj_hash_next(mod_tsx_layer.htable, it);
716
pjsip_tsx_terminate(tsx, PJSIP_SC_SERVICE_UNAVAILABLE);
717
mod_tsx_layer_unregister_tsx(tsx);
723
pj_mutex_unlock(mod_tsx_layer.mutex);
728
/* This module callback is called when module is being unloaded by
731
static pj_status_t mod_tsx_layer_unload(void)
733
/* Only self destroy when there's no transaction in the table.
734
* Transaction may refuse to destroy when it has pending
735
* transmission. If we destroy the module now, application will
736
* crash when the pending transaction finally got error response
737
* from transport and when it tries to unregister itself.
739
if (pj_hash_count(mod_tsx_layer.htable) != 0)
743
pj_mutex_destroy(mod_tsx_layer.mutex);
746
pjsip_endpt_release_pool(mod_tsx_layer.endpt, mod_tsx_layer.pool);
749
pj_thread_local_free(pjsip_tsx_lock_tls_id);
751
/* Mark as unregistered. */
752
mod_tsx_layer.endpt = NULL;
754
PJ_LOG(4,(THIS_FILE, "Transaction layer module destroyed"));
760
/* This module callback is called when endpoint has received an
761
* incoming request message.
763
static pj_bool_t mod_tsx_layer_on_rx_request(pjsip_rx_data *rdata)
766
pj_uint32_t hval = 0;
767
pjsip_transaction *tsx;
769
pjsip_tsx_create_key(rdata->tp_info.pool, &key, PJSIP_ROLE_UAS,
770
&rdata->msg_info.cseq->method, rdata);
772
/* Find transaction. */
773
pj_mutex_lock( mod_tsx_layer.mutex );
775
tsx = (pjsip_transaction*)
776
pj_hash_get( mod_tsx_layer.htable, key.ptr, key.slen, &hval );
779
TSX_TRACE_((THIS_FILE,
780
"Finding tsx for request, hkey=0x%p and key=%.*s, found %p",
781
hval, key.slen, key.ptr, tsx));
784
if (tsx == NULL || tsx->state == PJSIP_TSX_STATE_TERMINATED) {
785
/* Transaction not found.
786
* Reject the request so that endpoint passes the request to
787
* upper layer modules.
789
pj_mutex_unlock( mod_tsx_layer.mutex);
793
/* Unlock hash table. */
794
pj_mutex_unlock( mod_tsx_layer.mutex );
797
* Transaction may gets deleted before we have chance to lock it
798
* in pjsip_tsx_recv_msg().
800
PJ_TODO(FIX_RACE_CONDITION_HERE);
802
/* Pass the message to the transaction. */
803
pjsip_tsx_recv_msg(tsx, rdata );
809
/* This module callback is called when endpoint has received an
810
* incoming response message.
812
static pj_bool_t mod_tsx_layer_on_rx_response(pjsip_rx_data *rdata)
815
pj_uint32_t hval = 0;
816
pjsip_transaction *tsx;
818
pjsip_tsx_create_key(rdata->tp_info.pool, &key, PJSIP_ROLE_UAC,
819
&rdata->msg_info.cseq->method, rdata);
821
/* Find transaction. */
822
pj_mutex_lock( mod_tsx_layer.mutex );
824
tsx = (pjsip_transaction*)
825
pj_hash_get( mod_tsx_layer.htable, key.ptr, key.slen, &hval );
828
TSX_TRACE_((THIS_FILE,
829
"Finding tsx for response, hkey=0x%p and key=%.*s, found %p",
830
hval, key.slen, key.ptr, tsx));
833
if (tsx == NULL || tsx->state == PJSIP_TSX_STATE_TERMINATED) {
834
/* Transaction not found.
835
* Reject the request so that endpoint passes the request to
836
* upper layer modules.
838
pj_mutex_unlock( mod_tsx_layer.mutex);
842
/* Unlock hash table. */
843
pj_mutex_unlock( mod_tsx_layer.mutex );
846
* Transaction may gets deleted before we have chance to lock it
847
* in pjsip_tsx_recv_msg().
849
PJ_TODO(FIX_RACE_CONDITION_HERE);
851
/* Pass the message to the transaction. */
852
pjsip_tsx_recv_msg(tsx, rdata );
859
* Get transaction instance in the rdata.
861
PJ_DEF(pjsip_transaction*) pjsip_rdata_get_tsx( pjsip_rx_data *rdata )
863
return (pjsip_transaction*)
864
rdata->endpt_info.mod_data[mod_tsx_layer.mod.id];
869
* Dump transaction layer.
871
PJ_DEF(void) pjsip_tsx_layer_dump(pj_bool_t detail)
873
#if PJ_LOG_MAX_LEVEL >= 3
874
pj_hash_iterator_t itbuf, *it;
877
pj_mutex_lock(mod_tsx_layer.mutex);
879
PJ_LOG(3, (THIS_FILE, "Dumping transaction table:"));
880
PJ_LOG(3, (THIS_FILE, " Total %d transactions",
881
pj_hash_count(mod_tsx_layer.htable)));
884
it = pj_hash_first(mod_tsx_layer.htable, &itbuf);
886
PJ_LOG(3, (THIS_FILE, " - none - "));
889
pjsip_transaction *tsx = (pjsip_transaction*)
890
pj_hash_this(mod_tsx_layer.htable,it);
892
PJ_LOG(3, (THIS_FILE, " %s %s|%d|%s",
895
pjsip_tx_data_get_info(tsx->last_tx):
898
pjsip_tsx_state_str(tsx->state)));
900
it = pj_hash_next(mod_tsx_layer.htable, it);
906
pj_mutex_unlock(mod_tsx_layer.mutex);
910
/*****************************************************************************
914
*****************************************************************************
917
* Lock transaction and set the value of Thread Local Storage.
919
static void lock_tsx(pjsip_transaction *tsx, struct tsx_lock_data *lck)
921
struct tsx_lock_data *prev_data;
923
pj_mutex_lock(tsx->mutex);
924
prev_data = (struct tsx_lock_data *)
925
pj_thread_local_get(pjsip_tsx_lock_tls_id);
926
lck->prev = prev_data;
929
pj_thread_local_set(pjsip_tsx_lock_tls_id, lck);
934
* Unlock transaction.
935
* This will selectively unlock the mutex ONLY IF the transaction has not been
936
* destroyed. The function knows whether the transaction has been destroyed
937
* because when transaction is destroyed the is_alive flag for the transaction
938
* will be set to zero.
940
static pj_status_t unlock_tsx( pjsip_transaction *tsx,
941
struct tsx_lock_data *lck)
943
pj_assert( (void*)pj_thread_local_get(pjsip_tsx_lock_tls_id) == lck);
944
pj_assert( lck->tsx == tsx );
945
pj_thread_local_set(pjsip_tsx_lock_tls_id, lck->prev);
947
pj_mutex_unlock(tsx->mutex);
949
return lck->is_alive ? PJ_SUCCESS : PJSIP_ETSXDESTROYED;
953
/* Lock transaction for accessing the timeout timer only. */
954
static void lock_timer(pjsip_transaction *tsx)
956
pj_mutex_lock(tsx->mutex_b);
960
static void unlock_timer(pjsip_transaction *tsx)
962
pj_mutex_unlock(tsx->mutex_b);
965
/* Create and initialize basic transaction structure.
966
* This function is called by both UAC and UAS creation.
968
static pj_status_t tsx_create( pjsip_module *tsx_user,
969
pjsip_transaction **p_tsx)
972
pjsip_transaction *tsx;
975
pool = pjsip_endpt_create_pool( mod_tsx_layer.endpt, "tsx",
976
PJSIP_POOL_TSX_LEN, PJSIP_POOL_TSX_INC );
980
tsx = PJ_POOL_ZALLOC_T(pool, pjsip_transaction);
982
tsx->tsx_user = tsx_user;
983
tsx->endpt = mod_tsx_layer.endpt;
985
pj_ansi_snprintf(tsx->obj_name, sizeof(tsx->obj_name),
987
pj_memcpy(pool->obj_name, tsx->obj_name, sizeof(pool->obj_name));
989
tsx->handle_200resp = 1;
990
tsx->retransmit_timer.id = 0;
991
tsx->retransmit_timer.user_data = tsx;
992
tsx->retransmit_timer.cb = &tsx_timer_callback;
993
tsx->timeout_timer.id = 0;
994
tsx->timeout_timer.user_data = tsx;
995
tsx->timeout_timer.cb = &tsx_timer_callback;
997
status = pj_mutex_create_recursive(pool, tsx->obj_name, &tsx->mutex);
998
if (status != PJ_SUCCESS) {
999
pjsip_endpt_release_pool(mod_tsx_layer.endpt, pool);
1003
status = pj_mutex_create_simple(pool, tsx->obj_name, &tsx->mutex_b);
1004
if (status != PJ_SUCCESS) {
1005
pj_mutex_destroy(tsx->mutex);
1006
pjsip_endpt_release_pool(mod_tsx_layer.endpt, pool);
1015
/* Destroy transaction. */
1016
static pj_status_t tsx_destroy( pjsip_transaction *tsx )
1018
struct tsx_lock_data *lck;
1020
/* Release the transport */
1021
tsx_update_transport(tsx, NULL);
1023
/* Decrement reference counter in transport selector */
1024
pjsip_tpselector_dec_ref(&tsx->tp_sel);
1026
/* Free last transmitted message. */
1028
pjsip_tx_data_dec_ref( tsx->last_tx );
1029
tsx->last_tx = NULL;
1031
/* Cancel timeout timer. */
1032
if (tsx->timeout_timer.id != 0) {
1033
pjsip_endpt_cancel_timer(tsx->endpt, &tsx->timeout_timer);
1034
tsx->timeout_timer.id = 0;
1036
/* Cancel retransmission timer. */
1037
if (tsx->retransmit_timer.id != 0) {
1038
pjsip_endpt_cancel_timer(tsx->endpt, &tsx->retransmit_timer);
1039
tsx->retransmit_timer.id = 0;
1042
/* Clear some pending flags. */
1043
tsx->transport_flag &= ~(TSX_HAS_PENDING_RESCHED | TSX_HAS_PENDING_SEND);
1045
/* Refuse to destroy transaction if it has pending resolving. */
1046
if (tsx->transport_flag & TSX_HAS_PENDING_TRANSPORT) {
1047
tsx->transport_flag |= TSX_HAS_PENDING_DESTROY;
1048
tsx->tsx_user = NULL;
1049
PJ_LOG(4,(tsx->obj_name, "Will destroy later because transport is "
1054
/* Clear TLS, so that mutex will not be unlocked */
1055
lck = (struct tsx_lock_data*) pj_thread_local_get(pjsip_tsx_lock_tls_id);
1057
if (lck->tsx == tsx) {
1063
pj_mutex_destroy(tsx->mutex_b);
1064
pj_mutex_destroy(tsx->mutex);
1066
PJ_LOG(5,(tsx->obj_name, "Transaction destroyed!"));
1068
pjsip_endpt_release_pool(tsx->endpt, tsx->pool);
1075
* Callback when timer expires.
1077
static void tsx_timer_callback( pj_timer_heap_t *theap, pj_timer_entry *entry)
1080
pjsip_transaction *tsx = (pjsip_transaction*) entry->user_data;
1081
struct tsx_lock_data lck;
1083
PJ_UNUSED_ARG(theap);
1087
PJ_LOG(5,(tsx->obj_name, "%s timer event",
1088
(entry==&tsx->retransmit_timer ? "Retransmit":"Timeout")));
1091
PJSIP_EVENT_INIT_TIMER(event, entry);
1093
/* Dispatch event to transaction. */
1094
lock_tsx(tsx, &lck);
1095
(*tsx->state_handler)(tsx, &event);
1096
unlock_tsx(tsx, &lck);
1101
* Set transaction state, and inform TU about the transaction state change.
1103
static void tsx_set_state( pjsip_transaction *tsx,
1104
pjsip_tsx_state_e state,
1105
pjsip_event_id_e event_src_type,
1108
pjsip_tsx_state_e prev_state = tsx->state;
1110
/* New state must be greater than previous state */
1111
pj_assert(state >= tsx->state);
1113
PJ_LOG(5, (tsx->obj_name, "State changed from %s to %s, event=%s",
1114
state_str[tsx->state], state_str[state],
1115
pjsip_event_str(event_src_type)));
1120
/* Update the state handlers. */
1121
if (tsx->role == PJSIP_ROLE_UAC) {
1122
tsx->state_handler = tsx_state_handler_uac[state];
1124
tsx->state_handler = tsx_state_handler_uas[state];
1127
/* Before informing TU about state changed, inform TU about
1130
if (event_src_type==PJSIP_EVENT_RX_MSG && tsx->tsx_user) {
1131
pjsip_rx_data *rdata = (pjsip_rx_data*) event_src;
1133
pj_assert(rdata != NULL);
1135
if (rdata->msg_info.msg->type == PJSIP_RESPONSE_MSG &&
1136
tsx->tsx_user->on_rx_response)
1138
(*tsx->tsx_user->on_rx_response)(rdata);
1143
/* Inform TU about state changed. */
1144
if (tsx->tsx_user && tsx->tsx_user->on_tsx_state) {
1146
PJSIP_EVENT_INIT_TSX_STATE(e, tsx, event_src_type, event_src,
1148
(*tsx->tsx_user->on_tsx_state)(tsx, &e);
1152
/* When the transaction is terminated, release transport, and free the
1153
* saved last transmitted message.
1155
if (state == PJSIP_TSX_STATE_TERMINATED) {
1156
pj_time_val timeout = {0, 0};
1158
/* If we're still waiting for a message to be sent.. */
1159
if (tsx->transport_flag & TSX_HAS_PENDING_TRANSPORT) {
1160
/* Disassociate ourselves from the outstanding transmit data
1161
* so that when the send callback is called we will be able
1162
* to ignore that (otherwise we'll get assertion, see
1163
* http://trac.pjsip.org/repos/ticket/1033)
1165
if (tsx->pending_tx) {
1166
tsx->pending_tx->mod_data[mod_tsx_layer.mod.id] = NULL;
1167
tsx->pending_tx = NULL;
1169
tsx->transport_flag &= ~(TSX_HAS_PENDING_TRANSPORT);
1174
/* Cancel timeout timer. */
1175
if (tsx->timeout_timer.id != 0) {
1176
pjsip_endpt_cancel_timer(tsx->endpt, &tsx->timeout_timer);
1177
tsx->timeout_timer.id = 0;
1180
tsx->timeout_timer.id = TIMER_ACTIVE;
1181
pjsip_endpt_schedule_timer( tsx->endpt, &tsx->timeout_timer,
1186
} else if (state == PJSIP_TSX_STATE_DESTROYED) {
1188
/* Unregister transaction. */
1189
mod_tsx_layer_unregister_tsx(tsx);
1191
/* Destroy transaction. */
1198
* Create, initialize, and register UAC transaction.
1200
PJ_DEF(pj_status_t) pjsip_tsx_create_uac( pjsip_module *tsx_user,
1201
pjsip_tx_data *tdata,
1202
pjsip_transaction **p_tsx)
1204
pjsip_transaction *tsx;
1206
pjsip_cseq_hdr *cseq;
1208
pjsip_host_info dst_info;
1209
struct tsx_lock_data lck;
1212
/* Validate arguments. */
1213
PJ_ASSERT_RETURN(tdata && tdata->msg && p_tsx, PJ_EINVAL);
1214
PJ_ASSERT_RETURN(tdata->msg->type == PJSIP_REQUEST_MSG,
1215
PJSIP_ENOTREQUESTMSG);
1217
/* Method MUST NOT be ACK! */
1218
PJ_ASSERT_RETURN(tdata->msg->line.req.method.id != PJSIP_ACK_METHOD,
1224
/* Make sure CSeq header is present. */
1225
cseq = (pjsip_cseq_hdr*) pjsip_msg_find_hdr(msg, PJSIP_H_CSEQ, NULL);
1227
pj_assert(!"CSeq header not present in outgoing message!");
1228
return PJSIP_EMISSINGHDR;
1232
/* Create transaction instance. */
1233
status = tsx_create( tsx_user, &tsx);
1234
if (status != PJ_SUCCESS)
1238
/* Lock transaction. */
1239
lock_tsx(tsx, &lck);
1242
tsx->role = PJSIP_ROLE_UAC;
1245
pjsip_method_copy( tsx->pool, &tsx->method, &msg->line.req.method);
1248
tsx->cseq = cseq->cseq;
1250
/* Generate Via header if it doesn't exist. */
1251
via = (pjsip_via_hdr*) pjsip_msg_find_hdr(msg, PJSIP_H_VIA, NULL);
1253
via = pjsip_via_hdr_create(tdata->pool);
1254
pjsip_msg_insert_first_hdr(msg, (pjsip_hdr*) via);
1257
/* Generate branch parameter if it doesn't exist. */
1258
if (via->branch_param.slen == 0) {
1260
via->branch_param.ptr = (char*)
1261
pj_pool_alloc(tsx->pool, PJSIP_MAX_BRANCH_LEN);
1262
via->branch_param.slen = PJSIP_MAX_BRANCH_LEN;
1263
pj_memcpy(via->branch_param.ptr, PJSIP_RFC3261_BRANCH_ID,
1264
PJSIP_RFC3261_BRANCH_LEN);
1265
tmp.ptr = via->branch_param.ptr + PJSIP_RFC3261_BRANCH_LEN + 2;
1266
*(tmp.ptr-2) = 80; *(tmp.ptr-1) = 106;
1267
pj_generate_unique_string( &tmp );
1269
/* Save branch parameter. */
1270
tsx->branch = via->branch_param;
1273
/* Copy branch parameter. */
1274
pj_strdup(tsx->pool, &tsx->branch, &via->branch_param);
1277
/* Generate transaction key. */
1278
create_tsx_key_3261( tsx->pool, &tsx->transaction_key,
1279
PJSIP_ROLE_UAC, &tsx->method,
1280
&via->branch_param);
1282
/* Calculate hashed key value. */
1284
tsx->hashed_key = pj_hash_calc(0, tsx->transaction_key.ptr,
1285
tsx->transaction_key.slen);
1288
PJ_LOG(6, (tsx->obj_name, "tsx_key=%.*s", tsx->transaction_key.slen,
1289
tsx->transaction_key.ptr));
1291
/* Begin with State_Null.
1292
* Manually set-up the state becase we don't want to call the callback.
1294
tsx->state = PJSIP_TSX_STATE_NULL;
1295
tsx->state_handler = &tsx_on_state_null;
1297
/* Save the message. */
1298
tsx->last_tx = tdata;
1299
pjsip_tx_data_add_ref(tsx->last_tx);
1301
/* Determine whether reliable transport should be used initially.
1302
* This will be updated whenever transport has changed.
1304
status = pjsip_get_request_dest(tdata, &dst_info);
1305
if (status != PJ_SUCCESS) {
1306
unlock_tsx(tsx, &lck);
1310
tsx->is_reliable = (dst_info.flag & PJSIP_TRANSPORT_RELIABLE);
1312
/* Register transaction to hash table. */
1313
status = mod_tsx_layer_register_tsx(tsx);
1314
if (status != PJ_SUCCESS) {
1315
/* The assertion is removed by #1090:
1316
pj_assert(!"Bug in branch_param generator (i.e. not unique)");
1318
unlock_tsx(tsx, &lck);
1324
/* Unlock transaction and return. */
1325
unlock_tsx(tsx, &lck);
1327
PJ_LOG(5,(tsx->obj_name, "Transaction created for %s",
1328
pjsip_tx_data_get_info(tdata)));
1336
* Create, initialize, and register UAS transaction.
1338
PJ_DEF(pj_status_t) pjsip_tsx_create_uas( pjsip_module *tsx_user,
1339
pjsip_rx_data *rdata,
1340
pjsip_transaction **p_tsx)
1342
pjsip_transaction *tsx;
1345
pjsip_cseq_hdr *cseq;
1347
struct tsx_lock_data lck;
1349
/* Validate arguments. */
1350
PJ_ASSERT_RETURN(rdata && rdata->msg_info.msg && p_tsx, PJ_EINVAL);
1352
/* Keep shortcut to message */
1353
msg = rdata->msg_info.msg;
1355
/* Make sure this is a request message. */
1356
PJ_ASSERT_RETURN(msg->type == PJSIP_REQUEST_MSG, PJSIP_ENOTREQUESTMSG);
1358
/* Make sure method is not ACK */
1359
PJ_ASSERT_RETURN(msg->line.req.method.id != PJSIP_ACK_METHOD,
1362
/* Make sure CSeq header is present. */
1363
cseq = rdata->msg_info.cseq;
1365
return PJSIP_EMISSINGHDR;
1367
/* Make sure Via header is present. */
1368
if (rdata->msg_info.via == NULL)
1369
return PJSIP_EMISSINGHDR;
1371
/* Check that method in CSeq header match request method.
1372
* Reference: PROTOS #1922
1374
if (pjsip_method_cmp(&msg->line.req.method,
1375
&rdata->msg_info.cseq->method) != 0)
1377
PJ_LOG(4,(THIS_FILE, "Error: CSeq header contains different "
1378
"method than the request line"));
1379
return PJSIP_EINVALIDHDR;
1383
* Create transaction instance.
1385
status = tsx_create( tsx_user, &tsx);
1386
if (status != PJ_SUCCESS)
1390
/* Lock transaction. */
1391
lock_tsx(tsx, &lck);
1394
tsx->role = PJSIP_ROLE_UAS;
1397
pjsip_method_copy( tsx->pool, &tsx->method, &msg->line.req.method);
1400
tsx->cseq = cseq->cseq;
1402
/* Get transaction key either from branch for RFC3261 message, or
1403
* create transaction key.
1405
status = pjsip_tsx_create_key(tsx->pool, &tsx->transaction_key,
1406
PJSIP_ROLE_UAS, &tsx->method, rdata);
1407
if (status != PJ_SUCCESS) {
1408
unlock_tsx(tsx, &lck);
1413
/* Calculate hashed key value. */
1415
tsx->hashed_key = pj_hash_calc(0, tsx->transaction_key.ptr,
1416
tsx->transaction_key.slen);
1419
/* Duplicate branch parameter for transaction. */
1420
branch = &rdata->msg_info.via->branch_param;
1421
pj_strdup(tsx->pool, &tsx->branch, branch);
1423
PJ_LOG(6, (tsx->obj_name, "tsx_key=%.*s", tsx->transaction_key.slen,
1424
tsx->transaction_key.ptr));
1427
/* Begin with state NULL.
1428
* Manually set-up the state becase we don't want to call the callback.
1430
tsx->state = PJSIP_TSX_STATE_NULL;
1431
tsx->state_handler = &tsx_on_state_null;
1433
/* Get response address. */
1434
status = pjsip_get_response_addr( tsx->pool, rdata, &tsx->res_addr );
1435
if (status != PJ_SUCCESS) {
1436
unlock_tsx(tsx, &lck);
1441
/* If it's decided that we should use current transport, keep the
1444
if (tsx->res_addr.transport) {
1445
tsx_update_transport(tsx, tsx->res_addr.transport);
1446
pj_memcpy(&tsx->addr, &tsx->res_addr.addr, tsx->res_addr.addr_len);
1447
tsx->addr_len = tsx->res_addr.addr_len;
1448
tsx->is_reliable = PJSIP_TRANSPORT_IS_RELIABLE(tsx->transport);
1451
(tsx->res_addr.dst_host.flag & PJSIP_TRANSPORT_RELIABLE);
1455
/* Register the transaction. */
1456
status = mod_tsx_layer_register_tsx(tsx);
1457
if (status != PJ_SUCCESS) {
1458
unlock_tsx(tsx, &lck);
1463
/* Put this transaction in rdata's mod_data. */
1464
rdata->endpt_info.mod_data[mod_tsx_layer.mod.id] = tsx;
1466
/* Unlock transaction and return. */
1467
unlock_tsx(tsx, &lck);
1470
PJ_LOG(5,(tsx->obj_name, "Transaction created for %s",
1471
pjsip_rx_data_get_info(rdata)));
1480
* Bind transaction to a specific transport/listener.
1482
PJ_DEF(pj_status_t) pjsip_tsx_set_transport(pjsip_transaction *tsx,
1483
const pjsip_tpselector *sel)
1485
struct tsx_lock_data lck;
1487
/* Must be UAC transaction */
1488
PJ_ASSERT_RETURN(tsx && sel, PJ_EINVAL);
1490
/* Start locking the transaction. */
1491
lock_tsx(tsx, &lck);
1493
/* Decrement reference counter of previous transport selector */
1494
pjsip_tpselector_dec_ref(&tsx->tp_sel);
1496
/* Copy transport selector structure .*/
1497
pj_memcpy(&tsx->tp_sel, sel, sizeof(*sel));
1499
/* Increment reference counter */
1500
pjsip_tpselector_add_ref(&tsx->tp_sel);
1502
/* Unlock transaction. */
1503
unlock_tsx(tsx, &lck);
1510
* Set transaction status code and reason.
1512
static void tsx_set_status_code(pjsip_transaction *tsx,
1513
int code, const pj_str_t *reason)
1515
tsx->status_code = code;
1517
pj_strdup(tsx->pool, &tsx->status_text, reason);
1519
tsx->status_text = *pjsip_get_status_text(code);
1524
* Forcely terminate transaction.
1526
PJ_DEF(pj_status_t) pjsip_tsx_terminate( pjsip_transaction *tsx, int code )
1528
struct tsx_lock_data lck;
1530
PJ_ASSERT_RETURN(tsx != NULL, PJ_EINVAL);
1532
PJ_LOG(5,(tsx->obj_name, "Request to terminate transaction"));
1534
PJ_ASSERT_RETURN(code >= 200, PJ_EINVAL);
1536
if (tsx->state >= PJSIP_TSX_STATE_TERMINATED)
1539
lock_tsx(tsx, &lck);
1540
tsx_set_status_code(tsx, code, NULL);
1541
tsx_set_state( tsx, PJSIP_TSX_STATE_TERMINATED, PJSIP_EVENT_USER, NULL);
1542
unlock_tsx(tsx, &lck);
1549
* Cease retransmission on the UAC transaction. The UAC transaction is
1550
* still considered running, and it will complete when either final
1551
* response is received or the transaction times out.
1553
PJ_DEF(pj_status_t) pjsip_tsx_stop_retransmit(pjsip_transaction *tsx)
1555
struct tsx_lock_data lck;
1557
PJ_ASSERT_RETURN(tsx != NULL, PJ_EINVAL);
1558
PJ_ASSERT_RETURN(tsx->role == PJSIP_ROLE_UAC &&
1559
tsx->method.id == PJSIP_INVITE_METHOD,
1562
PJ_LOG(5,(tsx->obj_name, "Request to stop retransmission"));
1564
lock_tsx(tsx, &lck);
1565
/* Cancel retransmission timer. */
1566
if (tsx->retransmit_timer.id != 0) {
1567
pjsip_endpt_cancel_timer(tsx->endpt, &tsx->retransmit_timer);
1568
tsx->retransmit_timer.id = 0;
1570
unlock_tsx(tsx, &lck);
1577
* Start a timer to terminate transaction after the specified time
1580
PJ_DEF(pj_status_t) pjsip_tsx_set_timeout( pjsip_transaction *tsx,
1583
pj_time_val timeout;
1585
PJ_ASSERT_RETURN(tsx != NULL, PJ_EINVAL);
1586
PJ_ASSERT_RETURN(tsx->role == PJSIP_ROLE_UAC &&
1587
tsx->method.id == PJSIP_INVITE_METHOD,
1590
/* Note: must not call lock_tsx() as that would introduce deadlock.
1595
/* Transaction should normally not have final response, but as
1596
* #1121 says there is a (tolerable) window of race condition
1597
* where this might happen.
1599
if (tsx->status_code >= 200 && tsx->timeout_timer.id != 0) {
1600
/* Timeout is already set */
1605
if (tsx->timeout_timer.id != 0) {
1606
pjsip_endpt_cancel_timer(tsx->endpt, &tsx->timeout_timer);
1607
tsx->timeout_timer.id = 0;
1611
timeout.msec = millisec;
1612
pj_time_val_normalize(&timeout);
1614
tsx->timeout_timer.id = TIMER_ACTIVE;
1615
pjsip_endpt_schedule_timer(tsx->endpt, &tsx->timeout_timer,
1626
* This function is called by TU to send a message.
1628
PJ_DEF(pj_status_t) pjsip_tsx_send_msg( pjsip_transaction *tsx,
1629
pjsip_tx_data *tdata )
1632
struct tsx_lock_data lck;
1636
tdata = tsx->last_tx;
1638
PJ_ASSERT_RETURN(tdata != NULL, PJ_EINVALIDOP);
1640
PJ_LOG(5,(tsx->obj_name, "Sending %s in state %s",
1641
pjsip_tx_data_get_info(tdata),
1642
state_str[tsx->state]));
1644
PJSIP_EVENT_INIT_TX_MSG(event, tdata);
1646
/* Dispatch to transaction. */
1647
lock_tsx(tsx, &lck);
1649
/* Set transport selector to tdata */
1650
pjsip_tx_data_set_transport(tdata, &tsx->tp_sel);
1652
/* Dispatch to state handler */
1653
status = (*tsx->state_handler)(tsx, &event);
1655
unlock_tsx(tsx, &lck);
1657
/* Only decrement reference counter when it returns success.
1658
* (This is the specification from the .PDF design document).
1660
if (status == PJ_SUCCESS) {
1661
pjsip_tx_data_dec_ref(tdata);
1669
* This function is called by endpoint when incoming message for the
1670
* transaction is received.
1672
PJ_DEF(void) pjsip_tsx_recv_msg( pjsip_transaction *tsx,
1673
pjsip_rx_data *rdata)
1676
struct tsx_lock_data lck;
1679
PJ_LOG(5,(tsx->obj_name, "Incoming %s in state %s",
1680
pjsip_rx_data_get_info(rdata), state_str[tsx->state]));
1682
/* Put the transaction in the rdata's mod_data. */
1683
rdata->endpt_info.mod_data[mod_tsx_layer.mod.id] = tsx;
1686
PJSIP_EVENT_INIT_RX_MSG(event, rdata);
1688
/* Dispatch to transaction. */
1689
lock_tsx(tsx, &lck);
1690
status = (*tsx->state_handler)(tsx, &event);
1691
unlock_tsx(tsx, &lck);
1695
/* Callback called by send message framework */
1696
static void send_msg_callback( pjsip_send_state *send_state,
1697
pj_ssize_t sent, pj_bool_t *cont )
1699
pjsip_transaction *tsx = (pjsip_transaction*) send_state->token;
1700
pjsip_tx_data *tdata = send_state->tdata;
1701
struct tsx_lock_data lck;
1703
/* Check if transaction has cancelled itself from this transmit
1704
* notification (https://trac.pjsip.org/repos/ticket/1033).
1706
if (tdata->mod_data[mod_tsx_layer.mod.id] == NULL) {
1712
tdata->mod_data[mod_tsx_layer.mod.id] = NULL;
1713
tsx->pending_tx = NULL;
1715
lock_tsx(tsx, &lck);
1718
/* Successfully sent! */
1719
pj_assert(send_state->cur_transport != NULL);
1721
if (tsx->transport != send_state->cur_transport) {
1722
/* Update transport. */
1723
tsx_update_transport(tsx, send_state->cur_transport);
1725
/* Update remote address. */
1726
tsx->addr_len = tdata->dest_info.addr.entry[tdata->dest_info.cur_addr].addr_len;
1727
pj_memcpy(&tsx->addr,
1728
&tdata->dest_info.addr.entry[tdata->dest_info.cur_addr].addr,
1731
/* Update is_reliable flag. */
1732
tsx->is_reliable = PJSIP_TRANSPORT_IS_RELIABLE(tsx->transport);
1735
/* Clear pending transport flag. */
1736
tsx->transport_flag &= ~(TSX_HAS_PENDING_TRANSPORT);
1738
/* Mark that we have resolved the addresses. */
1739
tsx->transport_flag |= TSX_HAS_RESOLVED_SERVER;
1741
/* Pending destroy? */
1742
if (tsx->transport_flag & TSX_HAS_PENDING_DESTROY) {
1743
tsx_set_state( tsx, PJSIP_TSX_STATE_DESTROYED,
1744
PJSIP_EVENT_UNKNOWN, NULL );
1745
unlock_tsx(tsx, &lck);
1749
/* Need to transmit a message? */
1750
if (tsx->transport_flag & TSX_HAS_PENDING_SEND) {
1751
tsx->transport_flag &= ~(TSX_HAS_PENDING_SEND);
1752
tsx_send_msg(tsx, tsx->last_tx);
1755
/* Need to reschedule retransmission? */
1756
if (tsx->transport_flag & TSX_HAS_PENDING_RESCHED) {
1757
tsx->transport_flag &= ~(TSX_HAS_PENDING_RESCHED);
1759
/* Only update when transport turns out to be unreliable. */
1760
if (!tsx->is_reliable) {
1761
tsx_resched_retransmission(tsx);
1766
/* Failed to send! */
1767
pj_assert(sent != 0);
1769
/* If transaction is using the same transport as the failed one,
1770
* release the transport.
1772
if (send_state->cur_transport==tsx->transport)
1773
tsx_update_transport(tsx, NULL);
1775
/* Also stop processing if transaction has been flagged with
1776
* pending destroy (http://trac.pjsip.org/repos/ticket/906)
1778
if ((!*cont) || (tsx->transport_flag & TSX_HAS_PENDING_DESTROY)) {
1779
char errmsg[PJ_ERR_MSG_SIZE];
1780
pjsip_status_code sc;
1783
tsx->transport_err = -sent;
1785
err =pj_strerror(-sent, errmsg, sizeof(errmsg));
1787
PJ_LOG(2,(tsx->obj_name,
1788
"Failed to send %s! err=%d (%s)",
1789
pjsip_tx_data_get_info(send_state->tdata), -sent,
1792
/* Clear pending transport flag. */
1793
tsx->transport_flag &= ~(TSX_HAS_PENDING_TRANSPORT);
1795
/* Mark that we have resolved the addresses. */
1796
tsx->transport_flag |= TSX_HAS_RESOLVED_SERVER;
1798
/* Server resolution error is now mapped to 502 instead of 503,
1799
* since with 503 normally client should try again.
1800
* See http://trac.pjsip.org/repos/ticket/870
1802
if (-sent==PJ_ERESOLVE || -sent==PJLIB_UTIL_EDNS_NXDOMAIN)
1803
sc = PJSIP_SC_BAD_GATEWAY;
1805
sc = PJSIP_SC_TSX_TRANSPORT_ERROR;
1807
/* Terminate transaction, if it's not already terminated. */
1808
tsx_set_status_code(tsx, sc, &err);
1809
if (tsx->state != PJSIP_TSX_STATE_TERMINATED &&
1810
tsx->state != PJSIP_TSX_STATE_DESTROYED)
1812
tsx_set_state( tsx, PJSIP_TSX_STATE_TERMINATED,
1813
PJSIP_EVENT_TRANSPORT_ERROR, send_state->tdata);
1815
/* Don't forget to destroy if we have pending destroy flag
1816
* (http://trac.pjsip.org/repos/ticket/906)
1818
else if (tsx->transport_flag & TSX_HAS_PENDING_DESTROY)
1820
tsx_set_state( tsx, PJSIP_TSX_STATE_DESTROYED,
1821
PJSIP_EVENT_TRANSPORT_ERROR, send_state->tdata);
1825
char errmsg[PJ_ERR_MSG_SIZE];
1827
PJ_LOG(2,(tsx->obj_name,
1828
"Temporary failure in sending %s, "
1829
"will try next server. Err=%d (%s)",
1830
pjsip_tx_data_get_info(send_state->tdata), -sent,
1831
pj_strerror(-sent, errmsg, sizeof(errmsg)).ptr));
1833
/* Reset retransmission count */
1834
tsx->retransmit_count = 0;
1836
/* And reset timeout timer */
1837
if (tsx->timeout_timer.id) {
1840
pjsip_endpt_cancel_timer(tsx->endpt, &tsx->timeout_timer);
1841
tsx->timeout_timer.id = TIMER_INACTIVE;
1843
tsx->timeout_timer.id = TIMER_ACTIVE;
1844
pjsip_endpt_schedule_timer( tsx->endpt, &tsx->timeout_timer,
1845
&timeout_timer_val);
1850
/* Put again pending tdata */
1851
tdata->mod_data[mod_tsx_layer.mod.id] = tsx;
1852
tsx->pending_tx = tdata;
1856
unlock_tsx(tsx, &lck);
1860
/* Transport callback. */
1861
static void transport_callback(void *token, pjsip_tx_data *tdata,
1865
pjsip_transaction *tsx = (pjsip_transaction*) token;
1866
struct tsx_lock_data lck;
1867
char errmsg[PJ_ERR_MSG_SIZE];
1870
tsx->transport_err = -sent;
1872
err = pj_strerror(-sent, errmsg, sizeof(errmsg));
1874
PJ_LOG(2,(tsx->obj_name, "Transport failed to send %s! Err=%d (%s)",
1875
pjsip_tx_data_get_info(tdata), -sent, errmsg));
1877
lock_tsx(tsx, &lck);
1879
/* Release transport. */
1880
tsx_update_transport(tsx, NULL);
1882
/* Terminate transaction. */
1883
tsx_set_status_code(tsx, PJSIP_SC_TSX_TRANSPORT_ERROR, &err);
1884
tsx_set_state( tsx, PJSIP_TSX_STATE_TERMINATED,
1885
PJSIP_EVENT_TRANSPORT_ERROR, tdata );
1887
unlock_tsx(tsx, &lck);
1893
* Callback when transport state changes.
1895
static void tsx_tp_state_callback( pjsip_transport *tp,
1896
pjsip_transport_state state,
1897
const pjsip_transport_state_info *info)
1901
if (state == PJSIP_TP_STATE_DISCONNECTED) {
1902
pjsip_transaction *tsx;
1903
struct tsx_lock_data lck;
1905
pj_assert(tp && info && info->user_data);
1907
tsx = (pjsip_transaction*)info->user_data;
1909
lock_tsx(tsx, &lck);
1911
/* Terminate transaction when transport disconnected */
1912
if (tsx->state < PJSIP_TSX_STATE_TERMINATED) {
1914
char errmsg[PJ_ERR_MSG_SIZE];
1916
err = pj_strerror(info->status, errmsg, sizeof(errmsg));
1917
tsx_set_status_code(tsx, PJSIP_SC_TSX_TRANSPORT_ERROR, &err);
1918
tsx_set_state( tsx, PJSIP_TSX_STATE_TERMINATED,
1919
PJSIP_EVENT_TRANSPORT_ERROR, NULL);
1922
unlock_tsx(tsx, &lck);
1928
* Send message to the transport.
1930
static pj_status_t tsx_send_msg( pjsip_transaction *tsx,
1931
pjsip_tx_data *tdata)
1933
pj_status_t status = PJ_SUCCESS;
1935
PJ_ASSERT_RETURN(tsx && tdata, PJ_EINVAL);
1937
/* Send later if transport is still pending. */
1938
if (tsx->transport_flag & TSX_HAS_PENDING_TRANSPORT) {
1939
tsx->transport_flag |= TSX_HAS_PENDING_SEND;
1943
/* If we have the transport, send the message using that transport.
1944
* Otherwise perform full transport resolution.
1946
if (tsx->transport) {
1947
status = pjsip_transport_send( tsx->transport, tdata, &tsx->addr,
1949
&transport_callback);
1950
if (status == PJ_EPENDING)
1951
status = PJ_SUCCESS;
1953
if (status != PJ_SUCCESS) {
1954
char errmsg[PJ_ERR_MSG_SIZE];
1956
PJ_LOG(2,(tsx->obj_name,
1957
"Error sending %s: Err=%d (%s)",
1958
pjsip_tx_data_get_info(tdata), status,
1959
pj_strerror(status, errmsg, sizeof(errmsg)).ptr));
1961
/* On error, release transport to force using full transport
1962
* resolution procedure.
1964
tsx_update_transport(tsx, NULL);
1967
tsx->res_addr.transport = NULL;
1968
tsx->res_addr.addr_len = 0;
1974
/* We are here because we don't have transport, or we failed to send
1975
* the message using existing transport. If we haven't resolved the
1976
* server before, then begin the long process of resolving the server
1977
* and send the message with possibly new server.
1979
pj_assert(status != PJ_SUCCESS || tsx->transport == NULL);
1981
/* If we have resolved the server, we treat the error as permanent error.
1982
* Terminate transaction with transport error failure.
1984
if (tsx->transport_flag & TSX_HAS_RESOLVED_SERVER) {
1986
char errmsg[PJ_ERR_MSG_SIZE];
1989
if (status == PJ_SUCCESS) {
1990
pj_assert(!"Unexpected status!");
1991
status = PJ_EUNKNOWN;
1994
/* We have resolved the server!.
1995
* Treat this as permanent transport error.
1997
err = pj_strerror(status, errmsg, sizeof(errmsg));
1999
PJ_LOG(2,(tsx->obj_name,
2000
"Transport error, terminating transaction. "
2004
tsx_set_status_code(tsx, PJSIP_SC_TSX_TRANSPORT_ERROR, &err);
2005
tsx_set_state( tsx, PJSIP_TSX_STATE_TERMINATED,
2006
PJSIP_EVENT_TRANSPORT_ERROR, NULL );
2011
/* Must add reference counter because the send request functions
2012
* decrement the reference counter.
2014
pjsip_tx_data_add_ref(tdata);
2016
/* Also attach ourselves to the transmit data so that we'll be able
2017
* to unregister ourselves from the send notification of this
2020
tdata->mod_data[mod_tsx_layer.mod.id] = tsx;
2021
tsx->pending_tx = tdata;
2023
/* Begin resolving destination etc to send the message. */
2024
if (tdata->msg->type == PJSIP_REQUEST_MSG) {
2026
tsx->transport_flag |= TSX_HAS_PENDING_TRANSPORT;
2027
status = pjsip_endpt_send_request_stateless(tsx->endpt, tdata, tsx,
2028
&send_msg_callback);
2029
if (status == PJ_EPENDING)
2030
status = PJ_SUCCESS;
2031
if (status != PJ_SUCCESS) {
2032
pjsip_tx_data_dec_ref(tdata);
2033
tdata->mod_data[mod_tsx_layer.mod.id] = NULL;
2034
tsx->pending_tx = NULL;
2037
/* Check if transaction is terminated. */
2038
if (status==PJ_SUCCESS && tsx->state == PJSIP_TSX_STATE_TERMINATED)
2039
status = tsx->transport_err;
2043
tsx->transport_flag |= TSX_HAS_PENDING_TRANSPORT;
2044
status = pjsip_endpt_send_response( tsx->endpt, &tsx->res_addr,
2046
&send_msg_callback);
2047
if (status == PJ_EPENDING)
2048
status = PJ_SUCCESS;
2049
if (status != PJ_SUCCESS) {
2050
pjsip_tx_data_dec_ref(tdata);
2051
tdata->mod_data[mod_tsx_layer.mod.id] = NULL;
2052
tsx->pending_tx = NULL;
2055
/* Check if transaction is terminated. */
2056
if (status==PJ_SUCCESS && tsx->state == PJSIP_TSX_STATE_TERMINATED)
2057
status = tsx->transport_err;
2067
* Manually retransmit the last messagewithout updating the transaction state.
2069
PJ_DEF(pj_status_t) pjsip_tsx_retransmit_no_state(pjsip_transaction *tsx,
2070
pjsip_tx_data *tdata)
2072
struct tsx_lock_data lck;
2075
lock_tsx(tsx, &lck);
2076
if (tdata == NULL) {
2077
tdata = tsx->last_tx;
2079
status = tsx_send_msg(tsx, tdata);
2080
unlock_tsx(tsx, &lck);
2082
/* Only decrement reference counter when it returns success.
2083
* (This is the specification from the .PDF design document).
2085
if (status == PJ_SUCCESS) {
2086
pjsip_tx_data_dec_ref(tdata);
2094
* Retransmit last message sent.
2096
static void tsx_resched_retransmission( pjsip_transaction *tsx )
2098
pj_time_val timeout;
2099
pj_uint32_t msec_time;
2101
pj_assert((tsx->transport_flag & TSX_HAS_PENDING_TRANSPORT) == 0);
2103
if (tsx->role==PJSIP_ROLE_UAC && tsx->status_code >= 100)
2104
msec_time = pjsip_cfg()->tsx.t2;
2106
msec_time = (1 << (tsx->retransmit_count)) * pjsip_cfg()->tsx.t1;
2108
if (tsx->role == PJSIP_ROLE_UAC) {
2109
pj_assert(tsx->status_code < 200);
2110
/* Retransmission for non-INVITE transaction caps-off at T2 */
2111
if (msec_time > pjsip_cfg()->tsx.t2 &&
2112
tsx->method.id != PJSIP_INVITE_METHOD)
2114
msec_time = pjsip_cfg()->tsx.t2;
2117
/* For UAS, this can be retransmission of 2xx response for INVITE
2118
* or non-100 1xx response.
2120
if (tsx->status_code < 200) {
2121
/* non-100 1xx retransmission is at 60 seconds */
2122
msec_time = PJSIP_TSX_1XX_RETRANS_DELAY * 1000;
2124
/* Retransmission of INVITE final response also caps-off at T2 */
2125
pj_assert(tsx->status_code >= 200);
2126
if (msec_time > pjsip_cfg()->tsx.t2)
2127
msec_time = pjsip_cfg()->tsx.t2;
2131
timeout.sec = msec_time / 1000;
2132
timeout.msec = msec_time % 1000;
2133
tsx->retransmit_timer.id = TIMER_ACTIVE;
2134
pjsip_endpt_schedule_timer( tsx->endpt, &tsx->retransmit_timer,
2139
* Retransmit last message sent.
2141
static pj_status_t tsx_retransmit( pjsip_transaction *tsx, int resched)
2145
PJ_ASSERT_RETURN(tsx->last_tx!=NULL, PJ_EBUG);
2147
PJ_LOG(5,(tsx->obj_name, "Retransmiting %s, count=%d, restart?=%d",
2148
pjsip_tx_data_get_info(tsx->last_tx),
2149
tsx->retransmit_count, resched));
2151
++tsx->retransmit_count;
2153
/* Restart timer T1 first before sending the message to ensure that
2154
* retransmission timer is not engaged when loop transport is used.
2157
pj_assert(tsx->state != PJSIP_TSX_STATE_CONFIRMED);
2158
if (tsx->transport_flag & TSX_HAS_PENDING_TRANSPORT) {
2159
tsx->transport_flag |= TSX_HAS_PENDING_RESCHED;
2161
tsx_resched_retransmission(tsx);
2165
status = tsx_send_msg( tsx, tsx->last_tx);
2166
if (status != PJ_SUCCESS) {
2173
static void tsx_update_transport( pjsip_transaction *tsx,
2174
pjsip_transport *tp)
2178
if (tsx->transport) {
2179
pjsip_transport_remove_state_listener(tsx->transport,
2180
tsx->tp_st_key, tsx);
2181
pjsip_transport_dec_ref( tsx->transport );
2182
tsx->transport = NULL;
2186
tsx->transport = tp;
2187
pjsip_transport_add_ref(tp);
2188
pjsip_transport_add_state_listener(tp, &tsx_tp_state_callback, tsx,
2195
* Handler for events in state Null.
2197
static pj_status_t tsx_on_state_null( pjsip_transaction *tsx,
2198
pjsip_event *event )
2202
pj_assert(tsx->state == PJSIP_TSX_STATE_NULL);
2204
if (tsx->role == PJSIP_ROLE_UAS) {
2206
/* Set state to Trying. */
2207
pj_assert(event->type == PJSIP_EVENT_RX_MSG &&
2208
event->body.rx_msg.rdata->msg_info.msg->type ==
2210
tsx_set_state( tsx, PJSIP_TSX_STATE_TRYING, PJSIP_EVENT_RX_MSG,
2211
event->body.rx_msg.rdata);
2214
pjsip_tx_data *tdata;
2216
/* Must be transmit event.
2217
* You may got this assertion when using loop transport with delay
2218
* set to zero. That would cause on_rx_response() callback to be
2219
* called before tsx_send_msg() has completed.
2221
PJ_ASSERT_RETURN(event->type == PJSIP_EVENT_TX_MSG, PJ_EBUG);
2223
/* Get the txdata */
2224
tdata = event->body.tx_msg.tdata;
2226
/* Save the message for retransmission. */
2227
if (tsx->last_tx && tsx->last_tx != tdata) {
2228
pjsip_tx_data_dec_ref(tsx->last_tx);
2229
tsx->last_tx = NULL;
2231
if (tsx->last_tx != tdata) {
2232
tsx->last_tx = tdata;
2233
pjsip_tx_data_add_ref(tdata);
2236
/* Send the message. */
2237
status = tsx_send_msg( tsx, tdata);
2238
if (status != PJ_SUCCESS) {
2242
/* Start Timer B (or called timer F for non-INVITE) for transaction
2246
tsx->timeout_timer.id = TIMER_ACTIVE;
2247
pjsip_endpt_schedule_timer( tsx->endpt, &tsx->timeout_timer,
2248
&timeout_timer_val);
2251
/* Start Timer A (or timer E) for retransmission only if unreliable
2252
* transport is being used.
2254
if (!tsx->is_reliable) {
2255
tsx->retransmit_count = 0;
2256
if (tsx->transport_flag & TSX_HAS_PENDING_TRANSPORT) {
2257
tsx->transport_flag |= TSX_HAS_PENDING_RESCHED;
2259
tsx->retransmit_timer.id = TIMER_ACTIVE;
2260
pjsip_endpt_schedule_timer(tsx->endpt, &tsx->retransmit_timer,
2266
tsx_set_state( tsx, PJSIP_TSX_STATE_CALLING,
2267
PJSIP_EVENT_TX_MSG, tdata);
2275
* State Calling is for UAC after it sends request but before any responses
2278
static pj_status_t tsx_on_state_calling( pjsip_transaction *tsx,
2279
pjsip_event *event )
2281
pj_assert(tsx->state == PJSIP_TSX_STATE_CALLING);
2282
pj_assert(tsx->role == PJSIP_ROLE_UAC);
2284
if (event->type == PJSIP_EVENT_TIMER &&
2285
event->body.timer.entry == &tsx->retransmit_timer)
2289
/* Retransmit the request. */
2290
status = tsx_retransmit( tsx, 1 );
2291
if (status != PJ_SUCCESS) {
2295
} else if (event->type == PJSIP_EVENT_TIMER &&
2296
event->body.timer.entry == &tsx->timeout_timer)
2299
/* Cancel retransmission timer. */
2300
if (tsx->retransmit_timer.id != 0) {
2301
pjsip_endpt_cancel_timer(tsx->endpt, &tsx->retransmit_timer);
2302
tsx->retransmit_timer.id = 0;
2304
tsx->transport_flag &= ~(TSX_HAS_PENDING_RESCHED);
2306
/* Set status code */
2307
tsx_set_status_code(tsx, PJSIP_SC_TSX_TIMEOUT, NULL);
2310
tsx_set_state( tsx, PJSIP_TSX_STATE_TERMINATED,
2311
PJSIP_EVENT_TIMER, &tsx->timeout_timer);
2313
/* Transaction is destroyed */
2314
//return PJSIP_ETSXDESTROYED;
2316
} else if (event->type == PJSIP_EVENT_RX_MSG) {
2320
/* Get message instance */
2321
msg = event->body.rx_msg.rdata->msg_info.msg;
2323
/* Better be a response message. */
2324
if (msg->type != PJSIP_RESPONSE_MSG)
2325
return PJSIP_ENOTRESPONSEMSG;
2327
code = msg->line.status.code;
2329
/* If the response is final, cancel both retransmission and timeout
2333
if (tsx->retransmit_timer.id != 0) {
2334
pjsip_endpt_cancel_timer(tsx->endpt, &tsx->retransmit_timer);
2335
tsx->retransmit_timer.id = 0;
2338
if (tsx->timeout_timer.id != 0) {
2340
pjsip_endpt_cancel_timer(tsx->endpt, &tsx->timeout_timer);
2341
tsx->timeout_timer.id = 0;
2346
/* Cancel retransmit timer (for non-INVITE transaction, the
2347
* retransmit timer will be rescheduled at T2.
2349
if (tsx->retransmit_timer.id != 0) {
2350
pjsip_endpt_cancel_timer(tsx->endpt, &tsx->retransmit_timer);
2351
tsx->retransmit_timer.id = 0;
2354
/* For provisional response, only cancel retransmit when this
2355
* is an INVITE transaction. For non-INVITE, section 17.1.2.1
2356
* of RFC 3261 says that:
2357
* - retransmit timer is set to T2
2358
* - timeout timer F is not deleted.
2360
if (tsx->method.id == PJSIP_INVITE_METHOD) {
2362
/* Cancel timeout timer */
2364
pjsip_endpt_cancel_timer(tsx->endpt, &tsx->timeout_timer);
2368
if (!tsx->is_reliable) {
2369
tsx->retransmit_timer.id = TIMER_ACTIVE;
2370
pjsip_endpt_schedule_timer(tsx->endpt,
2371
&tsx->retransmit_timer,
2377
tsx->transport_flag &= ~(TSX_HAS_PENDING_RESCHED);
2380
/* Discard retransmission message if it is not INVITE.
2381
* The INVITE tdata is needed in case we have to generate ACK for
2382
* the final response.
2384
/* Keep last_tx for authorization. */
2385
//blp: always keep last_tx until transaction is destroyed
2386
//code = msg->line.status.code;
2387
//if (tsx->method.id != PJSIP_INVITE_METHOD && code!=401 && code!=407) {
2388
// pjsip_tx_data_dec_ref(tsx->last_tx);
2389
// tsx->last_tx = NULL;
2392
/* Processing is similar to state Proceeding. */
2393
tsx_on_state_proceeding_uac( tsx, event);
2396
pj_assert(!"Unexpected event");
2405
* State Trying is for UAS after it received request but before any responses
2407
* Note: this is different than RFC3261, which can use Trying state for
2408
* non-INVITE client transaction (bug in RFC?).
2410
static pj_status_t tsx_on_state_trying( pjsip_transaction *tsx,
2415
pj_assert(tsx->state == PJSIP_TSX_STATE_TRYING);
2417
/* This state is only for UAS */
2418
pj_assert(tsx->role == PJSIP_ROLE_UAS);
2420
/* Better be transmission of response message.
2421
* If we've got request retransmission, this means that the TU hasn't
2422
* transmitted any responses within 500 ms, which is not allowed. If
2423
* this happens, just ignore the event (we couldn't retransmit last
2424
* response because we haven't sent any!).
2426
if (event->type != PJSIP_EVENT_TX_MSG) {
2430
/* The rest of the processing of the event is exactly the same as in
2431
* "Proceeding" state.
2433
status = tsx_on_state_proceeding_uas( tsx, event);
2435
/* Inform the TU of the state transision if state is still State_Trying */
2436
if (status==PJ_SUCCESS && tsx->state == PJSIP_TSX_STATE_TRYING) {
2438
tsx_set_state( tsx, PJSIP_TSX_STATE_PROCEEDING,
2439
PJSIP_EVENT_TX_MSG, event->body.tx_msg.tdata);
2448
* Handler for events in Proceeding for UAS
2449
* This state happens after the TU sends provisional response.
2451
static pj_status_t tsx_on_state_proceeding_uas( pjsip_transaction *tsx,
2454
pj_assert(tsx->state == PJSIP_TSX_STATE_PROCEEDING ||
2455
tsx->state == PJSIP_TSX_STATE_TRYING);
2457
/* This state is only for UAS. */
2458
pj_assert(tsx->role == PJSIP_ROLE_UAS);
2460
/* Receive request retransmission. */
2461
if (event->type == PJSIP_EVENT_RX_MSG) {
2465
/* Must have last response sent. */
2466
PJ_ASSERT_RETURN(tsx->last_tx != NULL, PJ_EBUG);
2468
/* Send last response */
2469
if (tsx->transport_flag & TSX_HAS_PENDING_TRANSPORT) {
2470
tsx->transport_flag |= TSX_HAS_PENDING_SEND;
2472
status = tsx_send_msg(tsx, tsx->last_tx);
2473
if (status != PJ_SUCCESS)
2477
} else if (event->type == PJSIP_EVENT_TX_MSG ) {
2478
pjsip_tx_data *tdata = event->body.tx_msg.tdata;
2481
/* The TU sends response message to the request. Save this message so
2482
* that we can retransmit the last response in case we receive request
2485
pjsip_msg *msg = tdata->msg;
2487
/* This can only be a response message. */
2488
PJ_ASSERT_RETURN(msg->type==PJSIP_RESPONSE_MSG, PJSIP_ENOTRESPONSEMSG);
2490
/* Update last status */
2491
tsx_set_status_code(tsx, msg->line.status.code,
2492
&msg->line.status.reason);
2494
/* Discard the saved last response (it will be updated later as
2497
if (tsx->last_tx && tsx->last_tx != tdata) {
2498
pjsip_tx_data_dec_ref( tsx->last_tx );
2499
tsx->last_tx = NULL;
2502
/* Send the message. */
2503
status = tsx_send_msg(tsx, tdata);
2504
if (status != PJ_SUCCESS) {
2508
// Update To tag header for RFC2543 transaction.
2511
/* Update transaction state */
2512
if (PJSIP_IS_STATUS_IN_CLASS(tsx->status_code, 100)) {
2514
if (tsx->last_tx != tdata) {
2515
tsx->last_tx = tdata;
2516
pjsip_tx_data_add_ref( tdata );
2519
tsx_set_state( tsx, PJSIP_TSX_STATE_PROCEEDING,
2520
PJSIP_EVENT_TX_MSG, tdata );
2522
/* Retransmit provisional response every 1 minute if this is
2523
* an INVITE provisional response greater than 100.
2525
if (PJSIP_TSX_1XX_RETRANS_DELAY > 0 &&
2526
tsx->method.id==PJSIP_INVITE_METHOD && tsx->status_code>100)
2529
/* Stop 1xx retransmission timer, if any */
2530
if (tsx->retransmit_timer.id) {
2531
pjsip_endpt_cancel_timer(tsx->endpt,
2532
&tsx->retransmit_timer);
2533
tsx->retransmit_timer.id = 0;
2536
/* Schedule retransmission */
2537
tsx->retransmit_count = 0;
2538
if (tsx->transport_flag & TSX_HAS_PENDING_TRANSPORT) {
2539
tsx->transport_flag |= TSX_HAS_PENDING_RESCHED;
2541
pj_time_val delay = {PJSIP_TSX_1XX_RETRANS_DELAY, 0};
2543
tsx->retransmit_timer.id = TIMER_ACTIVE;
2544
pjsip_endpt_schedule_timer( tsx->endpt,
2545
&tsx->retransmit_timer,
2550
} else if (PJSIP_IS_STATUS_IN_CLASS(tsx->status_code, 200)) {
2552
/* Stop 1xx retransmission timer, if any */
2553
if (tsx->retransmit_timer.id) {
2554
pjsip_endpt_cancel_timer(tsx->endpt, &tsx->retransmit_timer);
2555
tsx->retransmit_timer.id = 0;
2558
if (tsx->method.id == PJSIP_INVITE_METHOD && tsx->handle_200resp==0) {
2560
/* 2xx class message is not saved, because retransmission
2563
tsx_set_state( tsx, PJSIP_TSX_STATE_TERMINATED,
2564
PJSIP_EVENT_TX_MSG, tdata );
2566
/* Transaction is destroyed. */
2567
//return PJSIP_ETSXDESTROYED;
2570
pj_time_val timeout;
2572
if (tsx->method.id == PJSIP_INVITE_METHOD) {
2573
tsx->retransmit_count = 0;
2574
if (tsx->transport_flag & TSX_HAS_PENDING_TRANSPORT) {
2575
tsx->transport_flag |= TSX_HAS_PENDING_RESCHED;
2577
tsx->retransmit_timer.id = TIMER_ACTIVE;
2578
pjsip_endpt_schedule_timer( tsx->endpt,
2579
&tsx->retransmit_timer,
2584
/* Save last response sent for retransmission when request
2585
* retransmission is received.
2587
if (tsx->last_tx != tdata) {
2588
tsx->last_tx = tdata;
2589
pjsip_tx_data_add_ref(tdata);
2592
/* Setup timeout timer: */
2594
if (tsx->method.id == PJSIP_INVITE_METHOD) {
2596
/* Start Timer H at 64*T1 for INVITE server transaction,
2597
* regardless of transport.
2599
timeout = timeout_timer_val;
2601
} else if (!tsx->is_reliable) {
2603
/* For non-INVITE, start timer J at 64*T1 for unreliable
2606
timeout = timeout_timer_val;
2610
/* Transaction terminates immediately for non-INVITE when
2611
* reliable transport is used.
2613
timeout.sec = timeout.msec = 0;
2617
tsx->timeout_timer.id = TIMER_ACTIVE;
2618
pjsip_endpt_schedule_timer( tsx->endpt, &tsx->timeout_timer,
2622
/* Set state to "Completed" */
2623
tsx_set_state( tsx, PJSIP_TSX_STATE_COMPLETED,
2624
PJSIP_EVENT_TX_MSG, tdata );
2627
} else if (tsx->status_code >= 300) {
2629
/* Stop 1xx retransmission timer, if any */
2630
if (tsx->retransmit_timer.id) {
2631
pjsip_endpt_cancel_timer(tsx->endpt, &tsx->retransmit_timer);
2632
tsx->retransmit_timer.id = 0;
2635
/* 3xx-6xx class message causes transaction to move to
2636
* "Completed" state.
2638
if (tsx->last_tx != tdata) {
2639
tsx->last_tx = tdata;
2640
pjsip_tx_data_add_ref( tdata );
2643
/* For INVITE, start timer H for transaction termination
2644
* regardless whether transport is reliable or not.
2645
* For non-INVITE, start timer J with the value of 64*T1 for
2646
* non-reliable transports, and zero for reliable transports.
2649
if (tsx->method.id == PJSIP_INVITE_METHOD) {
2650
/* Start timer H for INVITE */
2651
tsx->timeout_timer.id = TIMER_ACTIVE;
2652
pjsip_endpt_schedule_timer(tsx->endpt,&tsx->timeout_timer,
2653
&timeout_timer_val);
2654
} else if (!tsx->is_reliable) {
2655
/* Start timer J on 64*T1 seconds for non-INVITE */
2656
tsx->timeout_timer.id = TIMER_ACTIVE;
2657
pjsip_endpt_schedule_timer(tsx->endpt,&tsx->timeout_timer,
2658
&timeout_timer_val);
2660
/* Start timer J on zero seconds for non-INVITE */
2661
pj_time_val zero_time = { 0, 0 };
2662
tsx->timeout_timer.id = TIMER_ACTIVE;
2663
pjsip_endpt_schedule_timer(tsx->endpt,&tsx->timeout_timer,
2668
/* For INVITE, if unreliable transport is used, retransmission
2669
* timer G will be scheduled (retransmission).
2671
if (!tsx->is_reliable) {
2672
pjsip_cseq_hdr *cseq = (pjsip_cseq_hdr*)
2673
pjsip_msg_find_hdr( msg, PJSIP_H_CSEQ,
2675
if (cseq->method.id == PJSIP_INVITE_METHOD) {
2676
tsx->retransmit_count = 0;
2677
if (tsx->transport_flag & TSX_HAS_PENDING_TRANSPORT) {
2678
tsx->transport_flag |= TSX_HAS_PENDING_RESCHED;
2680
tsx->retransmit_timer.id = TIMER_ACTIVE;
2681
pjsip_endpt_schedule_timer(tsx->endpt,
2682
&tsx->retransmit_timer,
2689
tsx_set_state( tsx, PJSIP_TSX_STATE_COMPLETED,
2690
PJSIP_EVENT_TX_MSG, tdata );
2697
} else if (event->type == PJSIP_EVENT_TIMER &&
2698
event->body.timer.entry == &tsx->retransmit_timer) {
2700
/* Retransmission timer elapsed. */
2703
/* Must not be triggered while transport is pending. */
2704
pj_assert((tsx->transport_flag & TSX_HAS_PENDING_TRANSPORT) == 0);
2706
/* Must have last response to retransmit. */
2707
pj_assert(tsx->last_tx != NULL);
2709
/* Retransmit the last response. */
2710
status = tsx_retransmit( tsx, 1 );
2711
if (status != PJ_SUCCESS) {
2715
} else if (event->type == PJSIP_EVENT_TIMER &&
2716
event->body.timer.entry == &tsx->timeout_timer) {
2718
/* Timeout timer. should not happen? */
2719
pj_assert(!"Should not happen(?)");
2721
tsx_set_status_code(tsx, PJSIP_SC_TSX_TIMEOUT, NULL);
2723
tsx_set_state( tsx, PJSIP_TSX_STATE_TERMINATED,
2724
PJSIP_EVENT_TIMER, &tsx->timeout_timer);
2729
pj_assert(!"Unexpected event");
2738
* Handler for events in Proceeding for UAC
2739
* This state happens after provisional response(s) has been received from
2742
static pj_status_t tsx_on_state_proceeding_uac(pjsip_transaction *tsx,
2746
pj_assert(tsx->state == PJSIP_TSX_STATE_PROCEEDING ||
2747
tsx->state == PJSIP_TSX_STATE_CALLING);
2749
if (event->type != PJSIP_EVENT_TIMER) {
2752
/* Must be incoming response, because we should not retransmit
2753
* request once response has been received.
2755
pj_assert(event->type == PJSIP_EVENT_RX_MSG);
2756
if (event->type != PJSIP_EVENT_RX_MSG) {
2757
return PJ_EINVALIDOP;
2760
msg = event->body.rx_msg.rdata->msg_info.msg;
2762
/* Must be a response message. */
2763
if (msg->type != PJSIP_RESPONSE_MSG) {
2764
pj_assert(!"Expecting response message!");
2765
return PJSIP_ENOTRESPONSEMSG;
2768
tsx_set_status_code(tsx, msg->line.status.code,
2769
&msg->line.status.reason);
2772
if (event->body.timer.entry == &tsx->retransmit_timer) {
2773
/* Retransmit message. */
2776
status = tsx_retransmit( tsx, 1 );
2781
tsx_set_status_code(tsx, PJSIP_SC_TSX_TIMEOUT, NULL);
2785
if (PJSIP_IS_STATUS_IN_CLASS(tsx->status_code, 100)) {
2787
/* Inform the message to TU. */
2788
tsx_set_state( tsx, PJSIP_TSX_STATE_PROCEEDING,
2789
PJSIP_EVENT_RX_MSG, event->body.rx_msg.rdata );
2791
} else if (PJSIP_IS_STATUS_IN_CLASS(tsx->status_code,200)) {
2793
/* Stop timeout timer B/F. */
2795
pjsip_endpt_cancel_timer( tsx->endpt, &tsx->timeout_timer );
2798
/* For INVITE, the state moves to Terminated state (because ACK is
2799
* handled in TU). For non-INVITE, state moves to Completed.
2801
if (tsx->method.id == PJSIP_INVITE_METHOD) {
2802
tsx_set_state( tsx, PJSIP_TSX_STATE_TERMINATED,
2803
PJSIP_EVENT_RX_MSG, event->body.rx_msg.rdata );
2804
//return PJSIP_ETSXDESTROYED;
2807
pj_time_val timeout;
2809
/* For unreliable transport, start timer D (for INVITE) or
2810
* timer K for non-INVITE. */
2811
if (!tsx->is_reliable) {
2812
if (tsx->method.id == PJSIP_INVITE_METHOD) {
2813
timeout = td_timer_val;
2815
timeout = t4_timer_val;
2818
timeout.sec = timeout.msec = 0;
2821
tsx->timeout_timer.id = TIMER_ACTIVE;
2822
pjsip_endpt_schedule_timer( tsx->endpt, &tsx->timeout_timer,
2826
/* Cancel retransmission timer */
2827
if (tsx->retransmit_timer.id != 0) {
2828
pjsip_endpt_cancel_timer(tsx->endpt, &tsx->retransmit_timer);
2829
tsx->retransmit_timer.id = 0;
2832
/* Move state to Completed, inform TU. */
2833
tsx_set_state( tsx, PJSIP_TSX_STATE_COMPLETED,
2834
PJSIP_EVENT_RX_MSG, event->body.rx_msg.rdata );
2837
} else if (event->type == PJSIP_EVENT_TIMER &&
2838
event->body.timer.entry == &tsx->timeout_timer) {
2841
tsx_set_state( tsx, PJSIP_TSX_STATE_TERMINATED,
2842
PJSIP_EVENT_TIMER, &tsx->timeout_timer);
2845
} else if (tsx->status_code >= 300 && tsx->status_code <= 699) {
2849
/* XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX */
2851
* This is the old code; it's broken for authentication.
2853
pj_time_val timeout;
2857
pjsip_endpt_cancel_timer( tsx->endpt, &tsx->timeout_timer );
2859
/* Generate and send ACK for INVITE. */
2860
if (tsx->method.id == PJSIP_INVITE_METHOD) {
2863
status = pjsip_endpt_create_ack( tsx->endpt, tsx->last_tx,
2864
event->body.rx_msg.rdata,
2866
if (status != PJ_SUCCESS)
2869
if (ack != tsx->last_tx) {
2870
pjsip_tx_data_dec_ref(tsx->last_tx);
2874
status = tsx_send_msg( tsx, tsx->last_tx);
2875
if (status != PJ_SUCCESS) {
2880
/* Start Timer D with TD/T4 timer if unreliable transport is used. */
2881
if (!tsx->is_reliable) {
2882
if (tsx->method.id == PJSIP_INVITE_METHOD) {
2883
timeout = td_timer_val;
2885
timeout = t4_timer_val;
2888
timeout.sec = timeout.msec = 0;
2890
tsx->timeout->timer.id = TSX_TIMER_TIMEOUT;
2891
pjsip_endpt_schedule_timer( tsx->endpt, &tsx->timeout_timer, &timeout);
2894
* blp: You might be tempted to move this notification before
2895
* sending ACK, but I think you shouldn't. Better set-up
2896
* everything before calling tsx_user's callback to avoid
2899
tsx_set_state( tsx, PJSIP_TSX_STATE_COMPLETED,
2900
PJSIP_EVENT_RX_MSG, event->body.rx_msg.rdata );
2902
/* XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX */
2905
/* New code, taken from 0.2.9.x branch */
2906
pj_time_val timeout;
2907
pjsip_tx_data *ack_tdata = NULL;
2909
/* Cancel retransmission timer */
2910
if (tsx->retransmit_timer.id != 0) {
2911
pjsip_endpt_cancel_timer(tsx->endpt, &tsx->retransmit_timer);
2912
tsx->retransmit_timer.id = 0;
2917
tsx->timeout_timer.id = 0;
2918
pjsip_endpt_cancel_timer( tsx->endpt, &tsx->timeout_timer );
2921
/* Generate and send ACK (for INVITE) */
2922
if (tsx->method.id == PJSIP_INVITE_METHOD) {
2925
status = pjsip_endpt_create_ack( tsx->endpt, tsx->last_tx,
2926
event->body.rx_msg.rdata,
2928
if (status != PJ_SUCCESS)
2931
status = tsx_send_msg( tsx, ack_tdata);
2932
if (status != PJ_SUCCESS)
2937
tsx_set_state( tsx, PJSIP_TSX_STATE_COMPLETED,
2938
PJSIP_EVENT_RX_MSG, event->body.rx_msg.rdata);
2940
/* Generate and send ACK for INVITE. */
2941
if (tsx->method.id == PJSIP_INVITE_METHOD) {
2942
if (ack_tdata != tsx->last_tx) {
2943
pjsip_tx_data_dec_ref(tsx->last_tx);
2944
tsx->last_tx = ack_tdata;
2947
tsx_send_msg() does NOT decrement tdata's reference counter,
2948
so if we add the reference counter here, tdata will have
2949
reference counter 2, causing it to leak.
2950
pjsip_tx_data_add_ref(ack_tdata);
2955
/* Start Timer D with TD/T4 timer if unreliable transport is used. */
2956
/* Note: tsx->transport may be NULL! */
2957
if (!tsx->is_reliable) {
2958
if (tsx->method.id == PJSIP_INVITE_METHOD) {
2959
timeout = td_timer_val;
2961
timeout = t4_timer_val;
2964
timeout.sec = timeout.msec = 0;
2967
tsx->timeout_timer.id = TIMER_ACTIVE;
2968
pjsip_endpt_schedule_timer( tsx->endpt, &tsx->timeout_timer, &timeout);
2972
// Shouldn't happen because there's no timer for this state.
2973
pj_assert(!"Unexpected event");
2982
* Handler for events in Completed state for UAS
2984
static pj_status_t tsx_on_state_completed_uas( pjsip_transaction *tsx,
2987
pj_assert(tsx->state == PJSIP_TSX_STATE_COMPLETED);
2989
if (event->type == PJSIP_EVENT_RX_MSG) {
2990
pjsip_msg *msg = event->body.rx_msg.rdata->msg_info.msg;
2992
/* This must be a request message retransmission. */
2993
if (msg->type != PJSIP_REQUEST_MSG)
2994
return PJSIP_ENOTREQUESTMSG;
2996
/* On receive request retransmission, retransmit last response. */
2997
if (msg->line.req.method.id != PJSIP_ACK_METHOD) {
3000
status = tsx_retransmit( tsx, 0 );
3001
if (status != PJ_SUCCESS) {
3006
pj_time_val timeout;
3008
/* Process incoming ACK request. */
3010
/* Verify that this is an INVITE transaction */
3011
if (tsx->method.id != PJSIP_INVITE_METHOD) {
3012
PJ_LOG(2, (tsx->obj_name,
3013
"Received illegal ACK for %.*s transaction",
3014
(int)tsx->method.name.slen,
3015
tsx->method.name.ptr));
3016
return PJSIP_EINVALIDMETHOD;
3019
/* Cease retransmission. */
3020
if (tsx->retransmit_timer.id != 0) {
3021
pjsip_endpt_cancel_timer(tsx->endpt, &tsx->retransmit_timer);
3022
tsx->retransmit_timer.id = 0;
3024
tsx->transport_flag &= ~(TSX_HAS_PENDING_RESCHED);
3026
/* Reschedule timeout timer. */
3028
pjsip_endpt_cancel_timer( tsx->endpt, &tsx->timeout_timer );
3029
tsx->timeout_timer.id = TIMER_ACTIVE;
3031
/* Timer I is T4 timer for unreliable transports, and
3032
* zero seconds for reliable transports.
3034
if (!tsx->is_reliable) {
3038
timeout.sec = t4_timer_val.sec;
3039
timeout.msec = t4_timer_val.msec;
3041
pjsip_endpt_schedule_timer( tsx->endpt, &tsx->timeout_timer,
3045
/* Move state to "Confirmed" */
3046
tsx_set_state( tsx, PJSIP_TSX_STATE_CONFIRMED,
3047
PJSIP_EVENT_RX_MSG, event->body.rx_msg.rdata );
3050
} else if (event->type == PJSIP_EVENT_TIMER) {
3052
if (event->body.timer.entry == &tsx->retransmit_timer) {
3053
/* Retransmit message. */
3056
status = tsx_retransmit( tsx, 1 );
3057
if (status != PJ_SUCCESS) {
3062
if (tsx->method.id == PJSIP_INVITE_METHOD) {
3064
/* For INVITE, this means that ACK was never received.
3065
* Set state to Terminated, and inform TU.
3068
tsx_set_status_code(tsx, PJSIP_SC_TSX_TIMEOUT, NULL);
3070
tsx_set_state( tsx, PJSIP_TSX_STATE_TERMINATED,
3071
PJSIP_EVENT_TIMER, &tsx->timeout_timer );
3073
//return PJSIP_ETSXDESTROYED;
3076
/* Transaction terminated, it can now be deleted. */
3077
tsx_set_state( tsx, PJSIP_TSX_STATE_TERMINATED,
3078
PJSIP_EVENT_TIMER, &tsx->timeout_timer );
3079
//return PJSIP_ETSXDESTROYED;
3084
/* Ignore request to transmit. */
3085
PJ_ASSERT_RETURN(event->type == PJSIP_EVENT_TX_MSG &&
3086
event->body.tx_msg.tdata == tsx->last_tx,
3095
* Handler for events in Completed state for UAC transaction.
3097
static pj_status_t tsx_on_state_completed_uac( pjsip_transaction *tsx,
3100
pj_assert(tsx->state == PJSIP_TSX_STATE_COMPLETED);
3102
if (event->type == PJSIP_EVENT_TIMER) {
3103
/* Must be the timeout timer. */
3104
pj_assert(event->body.timer.entry == &tsx->timeout_timer);
3106
/* Move to Terminated state. */
3107
tsx_set_state( tsx, PJSIP_TSX_STATE_TERMINATED,
3108
PJSIP_EVENT_TIMER, event->body.timer.entry );
3110
/* Transaction has been destroyed. */
3111
//return PJSIP_ETSXDESTROYED;
3113
} else if (event->type == PJSIP_EVENT_RX_MSG) {
3114
if (tsx->method.id == PJSIP_INVITE_METHOD) {
3115
/* On received of final response retransmission, retransmit the ACK.
3116
* TU doesn't need to be informed.
3118
pjsip_msg *msg = event->body.rx_msg.rdata->msg_info.msg;
3119
pj_assert(msg->type == PJSIP_RESPONSE_MSG);
3120
if (msg->type==PJSIP_RESPONSE_MSG &&
3121
msg->line.status.code >= 200)
3125
status = tsx_retransmit( tsx, 0 );
3126
if (status != PJ_SUCCESS) {
3130
/* Very late retransmission of privisional response. */
3131
pj_assert( msg->type == PJSIP_RESPONSE_MSG );
3134
/* Just drop the response. */
3138
pj_assert(!"Unexpected event");
3139
return PJ_EINVALIDOP;
3147
* Handler for events in state Confirmed.
3149
static pj_status_t tsx_on_state_confirmed( pjsip_transaction *tsx,
3152
pj_assert(tsx->state == PJSIP_TSX_STATE_CONFIRMED);
3154
/* This state is only for UAS for INVITE. */
3155
pj_assert(tsx->role == PJSIP_ROLE_UAS);
3156
pj_assert(tsx->method.id == PJSIP_INVITE_METHOD);
3158
/* Absorb any ACK received. */
3159
if (event->type == PJSIP_EVENT_RX_MSG) {
3161
pjsip_msg *msg = event->body.rx_msg.rdata->msg_info.msg;
3163
/* Only expecting request message. */
3164
if (msg->type != PJSIP_REQUEST_MSG)
3165
return PJSIP_ENOTREQUESTMSG;
3167
/* Must be an ACK request or a late INVITE retransmission. */
3168
pj_assert(msg->line.req.method.id == PJSIP_ACK_METHOD ||
3169
msg->line.req.method.id == PJSIP_INVITE_METHOD);
3171
} else if (event->type == PJSIP_EVENT_TIMER) {
3172
/* Must be from timeout_timer_. */
3173
pj_assert(event->body.timer.entry == &tsx->timeout_timer);
3175
/* Move to Terminated state. */
3176
tsx_set_state( tsx, PJSIP_TSX_STATE_TERMINATED,
3177
PJSIP_EVENT_TIMER, &tsx->timeout_timer );
3179
/* Transaction has been destroyed. */
3180
//return PJSIP_ETSXDESTROYED;
3183
pj_assert(!"Unexpected event");
3192
* Handler for events in state Terminated.
3194
static pj_status_t tsx_on_state_terminated( pjsip_transaction *tsx,
3197
pj_assert(tsx->state == PJSIP_TSX_STATE_TERMINATED);
3198
pj_assert(event->type == PJSIP_EVENT_TIMER);
3200
/* Destroy this transaction */
3201
tsx_set_state(tsx, PJSIP_TSX_STATE_DESTROYED,
3202
event->type, event->body.user.user1 );
3209
* Handler for events in state Destroyed.
3212
static pj_status_t tsx_on_state_destroyed(pjsip_transaction *tsx,
3216
PJ_UNUSED_ARG(event);
3217
pj_assert(!"Not expecting any events!!");