blob: ec18981569b8dc792223ba7f7afdd4ca8ac31239 [file] [log] [blame]
Tristan Matthews0a329cc2013-07-17 13:20:14 -04001/* $Id$ */
2/*
3 * Copyright (C) 2008-2011 Teluu Inc. (http://www.teluu.com)
4 * Copyright (C) 2003-2008 Benny Prijono <benny@prijono.org>
5 *
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.
10 *
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.
15 *
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
19 */
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>
27#include <pj/hash.h>
28#include <pj/pool.h>
29#include <pj/os.h>
30#include <pj/rand.h>
31#include <pj/string.h>
32#include <pj/assert.h>
33#include <pj/guid.h>
34#include <pj/log.h>
35
36#define THIS_FILE "sip_transaction.c"
37
38#if 0
39#define TSX_TRACE_(expr) PJ_LOG(3,expr)
40#else
41#define TSX_TRACE_(expr)
42#endif
43
44/* When this macro is set, transaction will keep the hashed value
45 * so that future lookup (to unregister transaction) does not need
46 * to recalculate the hash again. It should gains a little bit of
47 * performance, so generally we'd want this.
48 */
49#define PRECALC_HASH
50
51
52/* Defined in sip_util_statefull.c */
53extern pjsip_module mod_stateful_util;
54
55
56/*****************************************************************************
57 **
58 ** Declarations and static variable definitions section.
59 **
60 *****************************************************************************
61 **/
62/* Prototypes. */
63static pj_status_t mod_tsx_layer_load(pjsip_endpoint *endpt);
64static pj_status_t mod_tsx_layer_start(void);
65static pj_status_t mod_tsx_layer_stop(void);
66static pj_status_t mod_tsx_layer_unload(void);
67static pj_bool_t mod_tsx_layer_on_rx_request(pjsip_rx_data *rdata);
68static pj_bool_t mod_tsx_layer_on_rx_response(pjsip_rx_data *rdata);
69
70/* Transaction layer module definition. */
71static struct mod_tsx_layer
72{
73 struct pjsip_module mod;
74 pj_pool_t *pool;
75 pjsip_endpoint *endpt;
76 pj_mutex_t *mutex;
77 pj_hash_table_t *htable;
78} mod_tsx_layer =
79{ {
80 NULL, NULL, /* List's prev and next. */
81 { "mod-tsx-layer", 13 }, /* Module name. */
82 -1, /* Module ID */
83 PJSIP_MOD_PRIORITY_TSX_LAYER, /* Priority. */
84 mod_tsx_layer_load, /* load(). */
85 mod_tsx_layer_start, /* start() */
86 mod_tsx_layer_stop, /* stop() */
87 mod_tsx_layer_unload, /* unload() */
88 mod_tsx_layer_on_rx_request, /* on_rx_request() */
89 mod_tsx_layer_on_rx_response, /* on_rx_response() */
90 NULL
91 }
92};
93
94/* Transaction state names */
95static const char *state_str[] =
96{
97 "Null",
98 "Calling",
99 "Trying",
100 "Proceeding",
101 "Completed",
102 "Confirmed",
103 "Terminated",
104 "Destroyed",
105};
106
107/* Role names */
108static const char *role_name[] =
109{
110 "UAC",
111 "UAS"
112};
113
114/* Transport flag. */
115enum
116{
117 TSX_HAS_PENDING_TRANSPORT = 1,
118 TSX_HAS_PENDING_RESCHED = 2,
119 TSX_HAS_PENDING_SEND = 4,
120 TSX_HAS_PENDING_DESTROY = 8,
121 TSX_HAS_RESOLVED_SERVER = 16,
122};
123
124/* Timer timeout value constants */
125static pj_time_val t1_timer_val = { PJSIP_T1_TIMEOUT/1000,
126 PJSIP_T1_TIMEOUT%1000 };
127static pj_time_val t2_timer_val = { PJSIP_T2_TIMEOUT/1000,
128 PJSIP_T2_TIMEOUT%1000 };
129static pj_time_val t4_timer_val = { PJSIP_T4_TIMEOUT/1000,
130 PJSIP_T4_TIMEOUT%1000 };
131static pj_time_val td_timer_val = { PJSIP_TD_TIMEOUT/1000,
132 PJSIP_TD_TIMEOUT%1000 };
133static pj_time_val timeout_timer_val = { (64*PJSIP_T1_TIMEOUT)/1000,
134 (64*PJSIP_T1_TIMEOUT)%1000 };
135
136#define TIMER_INACTIVE 0
137#define RETRANSMIT_TIMER 1
138#define TIMEOUT_TIMER 2
139#define TRANSPORT_ERR_TIMER 3
140
141
142/* Prototypes. */
143static pj_status_t tsx_on_state_null( pjsip_transaction *tsx,
144 pjsip_event *event);
145static pj_status_t tsx_on_state_calling( pjsip_transaction *tsx,
146 pjsip_event *event);
147static pj_status_t tsx_on_state_trying( pjsip_transaction *tsx,
148 pjsip_event *event);
149static pj_status_t tsx_on_state_proceeding_uas( pjsip_transaction *tsx,
150 pjsip_event *event);
151static pj_status_t tsx_on_state_proceeding_uac( pjsip_transaction *tsx,
152 pjsip_event *event);
153static pj_status_t tsx_on_state_completed_uas( pjsip_transaction *tsx,
154 pjsip_event *event);
155static pj_status_t tsx_on_state_completed_uac( pjsip_transaction *tsx,
156 pjsip_event *event);
157static pj_status_t tsx_on_state_confirmed( pjsip_transaction *tsx,
158 pjsip_event *event);
159static pj_status_t tsx_on_state_terminated( pjsip_transaction *tsx,
160 pjsip_event *event);
161static pj_status_t tsx_on_state_destroyed( pjsip_transaction *tsx,
162 pjsip_event *event);
163static void tsx_timer_callback( pj_timer_heap_t *theap,
164 pj_timer_entry *entry);
165static void tsx_tp_state_callback(
166 pjsip_transport *tp,
167 pjsip_transport_state state,
168 const pjsip_transport_state_info *info);
169static void tsx_set_state( pjsip_transaction *tsx,
170 pjsip_tsx_state_e state,
171 pjsip_event_id_e event_src_type,
172 void *event_src );
173static void tsx_set_state_no_notify( pjsip_transaction *tsx,
174 pjsip_tsx_state_e state,
175 pjsip_event_id_e event_src_type,
176 void *event_src );
177static void tsx_set_status_code(pjsip_transaction *tsx,
178 int code, const pj_str_t *reason);
179static pj_status_t tsx_create( pjsip_module *tsx_user,
180 pj_grp_lock_t *grp_lock,
181 pjsip_transaction **p_tsx);
182static void tsx_on_destroy(void *arg);
183static pj_status_t tsx_shutdown( pjsip_transaction *tsx );
184static void tsx_resched_retransmission( pjsip_transaction *tsx );
185static pj_status_t tsx_retransmit( pjsip_transaction *tsx, int resched);
186static int tsx_send_msg( pjsip_transaction *tsx,
187 pjsip_tx_data *tdata);
188static void tsx_update_transport( pjsip_transaction *tsx,
189 pjsip_transport *tp);
190
191
192/* State handlers for UAC, indexed by state */
193static int (*tsx_state_handler_uac[PJSIP_TSX_STATE_MAX])(pjsip_transaction *,
194 pjsip_event *) =
195{
196 &tsx_on_state_null,
197 &tsx_on_state_calling,
198 NULL,
199 &tsx_on_state_proceeding_uac,
200 &tsx_on_state_completed_uac,
201 &tsx_on_state_confirmed,
202 &tsx_on_state_terminated,
203 &tsx_on_state_destroyed,
204};
205
206/* State handlers for UAS */
207static int (*tsx_state_handler_uas[PJSIP_TSX_STATE_MAX])(pjsip_transaction *,
208 pjsip_event *) =
209{
210 &tsx_on_state_null,
211 NULL,
212 &tsx_on_state_trying,
213 &tsx_on_state_proceeding_uas,
214 &tsx_on_state_completed_uas,
215 &tsx_on_state_confirmed,
216 &tsx_on_state_terminated,
217 &tsx_on_state_destroyed,
218};
219
220/*****************************************************************************
221 **
222 ** Utilities
223 **
224 *****************************************************************************
225 */
226/*
227 * Get transaction state name.
228 */
229PJ_DEF(const char *) pjsip_tsx_state_str(pjsip_tsx_state_e state)
230{
231 return state_str[state];
232}
233
234/*
235 * Get the role name.
236 */
237PJ_DEF(const char *) pjsip_role_name(pjsip_role_e role)
238{
239 return role_name[role];
240}
241
242
243/*
244 * Create transaction key for RFC2543 compliant messages, which don't have
245 * unique branch parameter in the top most Via header.
246 *
247 * INVITE requests matches a transaction if the following attributes
248 * match the original request:
249 * - Request-URI
250 * - To tag
251 * - From tag
252 * - Call-ID
253 * - CSeq
254 * - top Via header
255 *
256 * CANCEL matching is done similarly as INVITE, except:
257 * - CSeq method will differ
258 * - To tag is not matched.
259 *
260 * ACK matching is done similarly, except that:
261 * - method of the CSeq will differ,
262 * - To tag is matched to the response sent by the server transaction.
263 *
264 * The transaction key is constructed from the common components of above
265 * components. Additional comparison is needed to fully match a transaction.
266 */
267static pj_status_t create_tsx_key_2543( pj_pool_t *pool,
268 pj_str_t *str,
269 pjsip_role_e role,
270 const pjsip_method *method,
271 const pjsip_rx_data *rdata )
272{
273#define SEPARATOR '$'
274 char *key, *p;
275 pj_ssize_t len;
276 pj_size_t len_required;
277 pj_str_t *host;
278
279 PJ_ASSERT_RETURN(pool && str && method && rdata, PJ_EINVAL);
280 PJ_ASSERT_RETURN(rdata->msg_info.msg, PJ_EINVAL);
281 PJ_ASSERT_RETURN(rdata->msg_info.via, PJSIP_EMISSINGHDR);
282 PJ_ASSERT_RETURN(rdata->msg_info.cseq, PJSIP_EMISSINGHDR);
283 PJ_ASSERT_RETURN(rdata->msg_info.from, PJSIP_EMISSINGHDR);
284
285 host = &rdata->msg_info.via->sent_by.host;
286
287 /* Calculate length required. */
288 len_required = 9 + /* CSeq number */
289 rdata->msg_info.from->tag.slen + /* From tag. */
290 rdata->msg_info.cid->id.slen + /* Call-ID */
291 host->slen + /* Via host. */
292 9 + /* Via port. */
293 16; /* Separator+Allowance. */
294 key = p = (char*) pj_pool_alloc(pool, len_required);
295
296 /* Add role. */
297 *p++ = (char)(role==PJSIP_ROLE_UAC ? 'c' : 's');
298 *p++ = SEPARATOR;
299
300 /* Add method, except when method is INVITE or ACK. */
301 if (method->id != PJSIP_INVITE_METHOD && method->id != PJSIP_ACK_METHOD) {
302 pj_memcpy(p, method->name.ptr, method->name.slen);
303 p += method->name.slen;
304 *p++ = '$';
305 }
306
307 /* Add CSeq (only the number). */
308 len = pj_utoa(rdata->msg_info.cseq->cseq, p);
309 p += len;
310 *p++ = SEPARATOR;
311
312 /* Add From tag. */
313 len = rdata->msg_info.from->tag.slen;
314 pj_memcpy( p, rdata->msg_info.from->tag.ptr, len);
315 p += len;
316 *p++ = SEPARATOR;
317
318 /* Add Call-ID. */
319 len = rdata->msg_info.cid->id.slen;
320 pj_memcpy( p, rdata->msg_info.cid->id.ptr, len );
321 p += len;
322 *p++ = SEPARATOR;
323
324 /* Add top Via header.
325 * We don't really care whether the port contains the real port (because
326 * it can be omited if default port is used). Anyway this function is
327 * only used to match request retransmission, and we expect that the
328 * request retransmissions will contain the same port.
329 */
330 pj_memcpy(p, host->ptr, host->slen);
331 p += host->slen;
332 *p++ = ':';
333
334 len = pj_utoa(rdata->msg_info.via->sent_by.port, p);
335 p += len;
336 *p++ = SEPARATOR;
337
338 *p++ = '\0';
339
340 /* Done. */
341 str->ptr = key;
342 str->slen = p-key;
343
344 return PJ_SUCCESS;
345}
346
347/*
348 * Create transaction key for RFC3161 compliant system.
349 */
350static pj_status_t create_tsx_key_3261( pj_pool_t *pool,
351 pj_str_t *key,
352 pjsip_role_e role,
353 const pjsip_method *method,
354 const pj_str_t *branch)
355{
356 char *p;
357
358 PJ_ASSERT_RETURN(pool && key && method && branch, PJ_EINVAL);
359
360 p = key->ptr = (char*)
361 pj_pool_alloc(pool, branch->slen + method->name.slen + 4 );
362
363 /* Add role. */
364 *p++ = (char)(role==PJSIP_ROLE_UAC ? 'c' : 's');
365 *p++ = SEPARATOR;
366
367 /* Add method, except when method is INVITE or ACK. */
368 if (method->id != PJSIP_INVITE_METHOD && method->id != PJSIP_ACK_METHOD) {
369 pj_memcpy(p, method->name.ptr, method->name.slen);
370 p += method->name.slen;
371 *p++ = '$';
372 }
373
374 /* Add branch ID. */
375 pj_memcpy(p, branch->ptr, branch->slen);
376 p += branch->slen;
377
378 /* Set length */
379 key->slen = p - key->ptr;
380
381 return PJ_SUCCESS;
382}
383
384/*
385 * Create key from the incoming data, to be used to search the transaction
386 * in the transaction hash table.
387 */
388PJ_DEF(pj_status_t) pjsip_tsx_create_key( pj_pool_t *pool, pj_str_t *key,
389 pjsip_role_e role,
390 const pjsip_method *method,
391 const pjsip_rx_data *rdata)
392{
393 pj_str_t rfc3261_branch = {PJSIP_RFC3261_BRANCH_ID,
394 PJSIP_RFC3261_BRANCH_LEN};
395
396
397 /* Get the branch parameter in the top-most Via.
398 * If branch parameter is started with "z9hG4bK", then the message was
399 * generated by agent compliant with RFC3261. Otherwise, it will be
400 * handled as RFC2543.
401 */
402 const pj_str_t *branch = &rdata->msg_info.via->branch_param;
403
404 if (pj_strnicmp(branch,&rfc3261_branch,PJSIP_RFC3261_BRANCH_LEN)==0) {
405
406 /* Create transaction key. */
407 return create_tsx_key_3261(pool, key, role, method, branch);
408
409 } else {
410 /* Create the key for the message. This key will be matched up
411 * with the transaction key. For RFC2563 transactions, the
412 * transaction key was created by the same function, so it will
413 * match the message.
414 */
415 return create_tsx_key_2543( pool, key, role, method, rdata );
416 }
417}
418
419/*****************************************************************************
420 **
421 ** Transaction layer module
422 **
423 *****************************************************************************
424 **/
425/*
426 * Create transaction layer module and registers it to the endpoint.
427 */
428PJ_DEF(pj_status_t) pjsip_tsx_layer_init_module(pjsip_endpoint *endpt)
429{
430 pj_pool_t *pool;
431 pj_status_t status;
432
433
434 PJ_ASSERT_RETURN(mod_tsx_layer.endpt==NULL, PJ_EINVALIDOP);
435
436 /* Initialize timer values */
437 t1_timer_val.sec = pjsip_cfg()->tsx.t1 / 1000;
438 t1_timer_val.msec = pjsip_cfg()->tsx.t1 % 1000;
439 t2_timer_val.sec = pjsip_cfg()->tsx.t2 / 1000;
440 t2_timer_val.msec = pjsip_cfg()->tsx.t2 % 1000;
441 t4_timer_val.sec = pjsip_cfg()->tsx.t4 / 1000;
442 t4_timer_val.msec = pjsip_cfg()->tsx.t4 % 1000;
443 td_timer_val.sec = pjsip_cfg()->tsx.td / 1000;
444 td_timer_val.msec = pjsip_cfg()->tsx.td % 1000;
445 /* Changed the initialization below to use td_timer_val instead, to enable
446 * customization to the timeout value.
447 */
448 //timeout_timer_val.sec = (64 * pjsip_cfg()->tsx.t1) / 1000;
449 //timeout_timer_val.msec = (64 * pjsip_cfg()->tsx.t1) % 1000;
450 timeout_timer_val = td_timer_val;
451
452 /*
453 * Initialize transaction layer structure.
454 */
455
456 /* Create pool for the module. */
457 pool = pjsip_endpt_create_pool(endpt, "tsxlayer",
458 PJSIP_POOL_TSX_LAYER_LEN,
459 PJSIP_POOL_TSX_LAYER_INC );
460 if (!pool)
461 return PJ_ENOMEM;
462
463
464 /* Initialize some attributes. */
465 mod_tsx_layer.pool = pool;
466 mod_tsx_layer.endpt = endpt;
467
468
469 /* Create hash table. */
470 mod_tsx_layer.htable = pj_hash_create( pool, pjsip_cfg()->tsx.max_count );
471 if (!mod_tsx_layer.htable) {
472 pjsip_endpt_release_pool(endpt, pool);
473 return PJ_ENOMEM;
474 }
475
476 /* Create group lock. */
477 status = pj_mutex_create_recursive(pool, "tsxlayer", &mod_tsx_layer.mutex);
478 if (status != PJ_SUCCESS) {
479 pjsip_endpt_release_pool(endpt, pool);
480 return status;
481 }
482
483 /*
484 * Register transaction layer module to endpoint.
485 */
486 status = pjsip_endpt_register_module( endpt, &mod_tsx_layer.mod );
487 if (status != PJ_SUCCESS) {
488 pj_mutex_destroy(mod_tsx_layer.mutex);
489 pjsip_endpt_release_pool(endpt, pool);
490 return status;
491 }
492
493 /* Register mod_stateful_util module (sip_util_statefull.c) */
494 status = pjsip_endpt_register_module(endpt, &mod_stateful_util);
495 if (status != PJ_SUCCESS) {
496 return status;
497 }
498
499 return PJ_SUCCESS;
500}
501
502
503/*
504 * Get the instance of transaction layer module.
505 */
506PJ_DEF(pjsip_module*) pjsip_tsx_layer_instance(void)
507{
508 return &mod_tsx_layer.mod;
509}
510
511
512/*
513 * Unregister and destroy transaction layer module.
514 */
515PJ_DEF(pj_status_t) pjsip_tsx_layer_destroy(void)
516{
517 /* Are we registered? */
518 PJ_ASSERT_RETURN(mod_tsx_layer.endpt!=NULL, PJ_EINVALIDOP);
519
520 /* Unregister from endpoint.
521 * Clean-ups will be done in the unload() module callback.
522 */
523 return pjsip_endpt_unregister_module( mod_tsx_layer.endpt,
524 &mod_tsx_layer.mod);
525}
526
527
528/*
529 * Register the transaction to the hash table.
530 */
531static pj_status_t mod_tsx_layer_register_tsx( pjsip_transaction *tsx)
532{
533 pj_assert(tsx->transaction_key.slen != 0);
534
535 /* Lock hash table mutex. */
536 pj_mutex_lock(mod_tsx_layer.mutex);
537
538 /* Check if no transaction with the same key exists.
539 * Do not use PJ_ASSERT_RETURN since it evaluates the expression
540 * twice!
541 */
542 if(pj_hash_get_lower(mod_tsx_layer.htable,
543 tsx->transaction_key.ptr,
544 (unsigned)tsx->transaction_key.slen,
545 NULL))
546 {
547 pj_mutex_unlock(mod_tsx_layer.mutex);
548 PJ_LOG(2,(THIS_FILE,
549 "Unable to register %.*s transaction (key exists)",
550 (int)tsx->method.name.slen,
551 tsx->method.name.ptr));
552 return PJ_EEXISTS;
553 }
554
555 TSX_TRACE_((THIS_FILE,
556 "Transaction %p registered with hkey=0x%p and key=%.*s",
557 tsx, tsx->hashed_key, tsx->transaction_key.slen,
558 tsx->transaction_key.ptr));
559
560 /* Register the transaction to the hash table. */
561#ifdef PRECALC_HASH
562 pj_hash_set_lower( tsx->pool, mod_tsx_layer.htable,
563 tsx->transaction_key.ptr,
564 (unsigned)tsx->transaction_key.slen,
565 tsx->hashed_key, tsx);
566#else
567 pj_hash_set_lower( tsx->pool, mod_tsx_layer.htable,
568 tsx->transaction_key.ptr,
569 tsx->transaction_key.slen, 0, tsx);
570#endif
571
572 /* Unlock mutex. */
573 pj_mutex_unlock(mod_tsx_layer.mutex);
574
575 return PJ_SUCCESS;
576}
577
578
579/*
580 * Unregister the transaction from the hash table.
581 */
582static void mod_tsx_layer_unregister_tsx( pjsip_transaction *tsx)
583{
584 if (mod_tsx_layer.mod.id == -1) {
585 /* The transaction layer has been unregistered. This could happen
586 * if the transaction was pending on transport and the application
587 * is shutdown. See http://trac.pjsip.org/repos/ticket/1033. In
588 * this case just do nothing.
589 */
590 return;
591 }
592
593 pj_assert(tsx->transaction_key.slen != 0);
594 //pj_assert(tsx->state != PJSIP_TSX_STATE_NULL);
595
596 /* Lock hash table mutex. */
597 pj_mutex_lock(mod_tsx_layer.mutex);
598
599 /* Register the transaction to the hash table. */
600#ifdef PRECALC_HASH
601 pj_hash_set_lower( NULL, mod_tsx_layer.htable, tsx->transaction_key.ptr,
602 (unsigned)tsx->transaction_key.slen, tsx->hashed_key,
603 NULL);
604#else
605 pj_hash_set_lower( NULL, mod_tsx_layer.htable, tsx->transaction_key.ptr,
606 tsx->transaction_key.slen, 0, NULL);
607#endif
608
609 TSX_TRACE_((THIS_FILE,
610 "Transaction %p unregistered, hkey=0x%p and key=%.*s",
611 tsx, tsx->hashed_key, tsx->transaction_key.slen,
612 tsx->transaction_key.ptr));
613
614 /* Unlock mutex. */
615 pj_mutex_unlock(mod_tsx_layer.mutex);
616}
617
618
619/*
620 * Retrieve the current number of transactions currently registered in
621 * the hash table.
622 */
623PJ_DEF(unsigned) pjsip_tsx_layer_get_tsx_count(void)
624{
625 unsigned count;
626
627 /* Are we registered? */
628 PJ_ASSERT_RETURN(mod_tsx_layer.endpt!=NULL, 0);
629
630 pj_mutex_lock(mod_tsx_layer.mutex);
631 count = pj_hash_count(mod_tsx_layer.htable);
632 pj_mutex_unlock(mod_tsx_layer.mutex);
633
634 return count;
635}
636
637
638/*
639 * Find a transaction.
640 */
641PJ_DEF(pjsip_transaction*) pjsip_tsx_layer_find_tsx( const pj_str_t *key,
642 pj_bool_t lock )
643{
644 pjsip_transaction *tsx;
645 pj_uint32_t hval = 0;
646
647 pj_mutex_lock(mod_tsx_layer.mutex);
648 tsx = (pjsip_transaction*)
649 pj_hash_get_lower( mod_tsx_layer.htable, key->ptr,
650 (unsigned)key->slen, &hval );
651 pj_mutex_unlock(mod_tsx_layer.mutex);
652
653 TSX_TRACE_((THIS_FILE,
654 "Finding tsx with hkey=0x%p and key=%.*s: found %p",
655 hval, key->slen, key->ptr, tsx));
656
657 /* Race condition!
658 * Transaction may gets deleted before we have chance to lock it.
659 */
660 PJ_TODO(FIX_RACE_CONDITION_HERE);
661 PJ_RACE_ME(5);
662
663 if (tsx && lock)
664 pj_grp_lock_acquire(tsx->grp_lock);
665
666 return tsx;
667}
668
669
670/* This module callback is called when module is being loaded by
671 * endpoint. It does nothing for this module.
672 */
673static pj_status_t mod_tsx_layer_load(pjsip_endpoint *endpt)
674{
675 PJ_UNUSED_ARG(endpt);
676 return PJ_SUCCESS;
677}
678
679
680/* This module callback is called when module is being started by
681 * endpoint. It does nothing for this module.
682 */
683static pj_status_t mod_tsx_layer_start(void)
684{
685 return PJ_SUCCESS;
686}
687
688
689/* This module callback is called when module is being stopped by
690 * endpoint.
691 */
692static pj_status_t mod_tsx_layer_stop(void)
693{
694 pj_hash_iterator_t it_buf, *it;
695
696 PJ_LOG(4,(THIS_FILE, "Stopping transaction layer module"));
697
698 pj_mutex_lock(mod_tsx_layer.mutex);
699
700 /* Destroy all transactions. */
701 it = pj_hash_first(mod_tsx_layer.htable, &it_buf);
702 while (it) {
703 pjsip_transaction *tsx = (pjsip_transaction*)
704 pj_hash_this(mod_tsx_layer.htable, it);
705 pj_hash_iterator_t *next = pj_hash_next(mod_tsx_layer.htable, it);
706 if (tsx) {
707 pjsip_tsx_terminate(tsx, PJSIP_SC_SERVICE_UNAVAILABLE);
708 mod_tsx_layer_unregister_tsx(tsx);
709 tsx_shutdown(tsx);
710 }
711 it = next;
712 }
713
714 pj_mutex_unlock(mod_tsx_layer.mutex);
715
716 PJ_LOG(4,(THIS_FILE, "Stopped transaction layer module"));
717
718 return PJ_SUCCESS;
719}
720
721
722/* Destroy this module */
723static void tsx_layer_destroy(pjsip_endpoint *endpt)
724{
725 PJ_UNUSED_ARG(endpt);
726
727 /* Destroy mutex. */
728 pj_mutex_destroy(mod_tsx_layer.mutex);
729
730 /* Release pool. */
731 pjsip_endpt_release_pool(mod_tsx_layer.endpt, mod_tsx_layer.pool);
732
733 /* Mark as unregistered. */
734 mod_tsx_layer.endpt = NULL;
735
736 PJ_LOG(4,(THIS_FILE, "Transaction layer module destroyed"));
737}
738
739
740/* This module callback is called when module is being unloaded by
741 * endpoint.
742 */
743static pj_status_t mod_tsx_layer_unload(void)
744{
745 /* Only self destroy when there's no transaction in the table.
746 * Transaction may refuse to destroy when it has pending
747 * transmission. If we destroy the module now, application will
748 * crash when the pending transaction finally got error response
749 * from transport and when it tries to unregister itself.
750 */
751 if (pj_hash_count(mod_tsx_layer.htable) != 0) {
752 if (pjsip_endpt_atexit(mod_tsx_layer.endpt, &tsx_layer_destroy) !=
753 PJ_SUCCESS)
754 {
755 PJ_LOG(3,(THIS_FILE, "Failed to register transaction layer "
756 "module destroy."));
757 }
758 return PJ_EBUSY;
759 }
760
761 tsx_layer_destroy(mod_tsx_layer.endpt);
762
763 return PJ_SUCCESS;
764}
765
766
767/* This module callback is called when endpoint has received an
768 * incoming request message.
769 */
770static pj_bool_t mod_tsx_layer_on_rx_request(pjsip_rx_data *rdata)
771{
772 pj_str_t key;
773 pj_uint32_t hval = 0;
774 pjsip_transaction *tsx;
775
776 pjsip_tsx_create_key(rdata->tp_info.pool, &key, PJSIP_ROLE_UAS,
777 &rdata->msg_info.cseq->method, rdata);
778
779 /* Find transaction. */
780 pj_mutex_lock( mod_tsx_layer.mutex );
781
782 tsx = (pjsip_transaction*)
783 pj_hash_get_lower( mod_tsx_layer.htable, key.ptr, (unsigned)key.slen,
784 &hval );
785
786
787 TSX_TRACE_((THIS_FILE,
788 "Finding tsx for request, hkey=0x%p and key=%.*s, found %p",
789 hval, key.slen, key.ptr, tsx));
790
791
792 if (tsx == NULL || tsx->state == PJSIP_TSX_STATE_TERMINATED) {
793 /* Transaction not found.
794 * Reject the request so that endpoint passes the request to
795 * upper layer modules.
796 */
797 pj_mutex_unlock( mod_tsx_layer.mutex);
798 return PJ_FALSE;
799 }
800
801 /* Unlock hash table. */
802 pj_mutex_unlock( mod_tsx_layer.mutex );
803
804 /* Race condition!
805 * Transaction may gets deleted before we have chance to lock it
806 * in pjsip_tsx_recv_msg().
807 */
808 PJ_TODO(FIX_RACE_CONDITION_HERE);
809 PJ_RACE_ME(5);
810
811 /* Pass the message to the transaction. */
812 pjsip_tsx_recv_msg(tsx, rdata );
813
814 return PJ_TRUE;
815}
816
817
818/* This module callback is called when endpoint has received an
819 * incoming response message.
820 */
821static pj_bool_t mod_tsx_layer_on_rx_response(pjsip_rx_data *rdata)
822{
823 pj_str_t key;
824 pj_uint32_t hval = 0;
825 pjsip_transaction *tsx;
826
827 pjsip_tsx_create_key(rdata->tp_info.pool, &key, PJSIP_ROLE_UAC,
828 &rdata->msg_info.cseq->method, rdata);
829
830 /* Find transaction. */
831 pj_mutex_lock( mod_tsx_layer.mutex );
832
833 tsx = (pjsip_transaction*)
834 pj_hash_get_lower( mod_tsx_layer.htable, key.ptr, (unsigned)key.slen,
835 &hval );
836
837
838 TSX_TRACE_((THIS_FILE,
839 "Finding tsx for response, hkey=0x%p and key=%.*s, found %p",
840 hval, key.slen, key.ptr, tsx));
841
842
843 if (tsx == NULL || tsx->state == PJSIP_TSX_STATE_TERMINATED) {
844 /* Transaction not found.
845 * Reject the request so that endpoint passes the request to
846 * upper layer modules.
847 */
848 pj_mutex_unlock( mod_tsx_layer.mutex);
849 return PJ_FALSE;
850 }
851
852 /* Unlock hash table. */
853 pj_mutex_unlock( mod_tsx_layer.mutex );
854
855 /* Race condition!
856 * Transaction may gets deleted before we have chance to lock it
857 * in pjsip_tsx_recv_msg().
858 */
859 PJ_TODO(FIX_RACE_CONDITION_HERE);
860 PJ_RACE_ME(5);
861
862 /* Pass the message to the transaction. */
863 pjsip_tsx_recv_msg(tsx, rdata );
864
865 return PJ_TRUE;
866}
867
868
869/*
870 * Get transaction instance in the rdata.
871 */
872PJ_DEF(pjsip_transaction*) pjsip_rdata_get_tsx( pjsip_rx_data *rdata )
873{
874 return (pjsip_transaction*)
875 rdata->endpt_info.mod_data[mod_tsx_layer.mod.id];
876}
877
878
879/*
880 * Dump transaction layer.
881 */
882PJ_DEF(void) pjsip_tsx_layer_dump(pj_bool_t detail)
883{
884#if PJ_LOG_MAX_LEVEL >= 3
885 pj_hash_iterator_t itbuf, *it;
886
887 /* Lock mutex. */
888 pj_mutex_lock(mod_tsx_layer.mutex);
889
890 PJ_LOG(3, (THIS_FILE, "Dumping transaction table:"));
891 PJ_LOG(3, (THIS_FILE, " Total %d transactions",
892 pj_hash_count(mod_tsx_layer.htable)));
893
894 if (detail) {
895 it = pj_hash_first(mod_tsx_layer.htable, &itbuf);
896 if (it == NULL) {
897 PJ_LOG(3, (THIS_FILE, " - none - "));
898 } else {
899 while (it != NULL) {
900 pjsip_transaction *tsx = (pjsip_transaction*)
901 pj_hash_this(mod_tsx_layer.htable,it);
902
903 PJ_LOG(3, (THIS_FILE, " %s %s|%d|%s",
904 tsx->obj_name,
905 (tsx->last_tx?
906 pjsip_tx_data_get_info(tsx->last_tx):
907 "none"),
908 tsx->status_code,
909 pjsip_tsx_state_str(tsx->state)));
910
911 it = pj_hash_next(mod_tsx_layer.htable, it);
912 }
913 }
914 }
915
916 /* Unlock mutex. */
917 pj_mutex_unlock(mod_tsx_layer.mutex);
918#endif
919}
920
921/*****************************************************************************
922 **
923 ** Transaction
924 **
925 *****************************************************************************
926 **/
927/* Lock transaction for accessing the timeout timer only. */
928static void lock_timer(pjsip_transaction *tsx)
929{
930 pj_mutex_lock(tsx->mutex_b);
931}
932
933/* Unlock timer */
934static void unlock_timer(pjsip_transaction *tsx)
935{
936 pj_mutex_unlock(tsx->mutex_b);
937}
938
939/* Utility: schedule a timer */
940static pj_status_t tsx_schedule_timer(pjsip_transaction *tsx,
941 pj_timer_entry *entry,
942 const pj_time_val *delay,
943 int active_id)
944{
945 pj_timer_heap_t *timer_heap = pjsip_endpt_get_timer_heap(tsx->endpt);
946 pj_status_t status;
947
948 pj_assert(active_id != 0);
949 status = pj_timer_heap_schedule_w_grp_lock(timer_heap, entry,
950 delay, active_id,
951 tsx->grp_lock);
952
953 return status;
954}
955
956/* Utility: cancel a timer */
957static int tsx_cancel_timer(pjsip_transaction *tsx,
958 pj_timer_entry *entry)
959{
960 pj_timer_heap_t *timer_heap = pjsip_endpt_get_timer_heap(tsx->endpt);
961 return pj_timer_heap_cancel_if_active(timer_heap, entry, TIMER_INACTIVE);
962}
963
964/* Create and initialize basic transaction structure.
965 * This function is called by both UAC and UAS creation.
966 */
967static pj_status_t tsx_create( pjsip_module *tsx_user,
968 pj_grp_lock_t *grp_lock,
969 pjsip_transaction **p_tsx)
970{
971 pj_pool_t *pool;
972 pjsip_transaction *tsx;
973 pj_status_t status;
974
975 pool = pjsip_endpt_create_pool( mod_tsx_layer.endpt, "tsx",
976 PJSIP_POOL_TSX_LEN, PJSIP_POOL_TSX_INC );
977 if (!pool)
978 return PJ_ENOMEM;
979
980 tsx = PJ_POOL_ZALLOC_T(pool, pjsip_transaction);
981 tsx->pool = pool;
982 tsx->tsx_user = tsx_user;
983 tsx->endpt = mod_tsx_layer.endpt;
984
985 pj_ansi_snprintf(tsx->obj_name, sizeof(tsx->obj_name),
986 "tsx%p", tsx);
987 pj_memcpy(pool->obj_name, tsx->obj_name, sizeof(pool->obj_name));
988
989 tsx->handle_200resp = 1;
990 tsx->retransmit_timer.id = TIMER_INACTIVE;
991 tsx->retransmit_timer.user_data = tsx;
992 tsx->retransmit_timer.cb = &tsx_timer_callback;
993 tsx->timeout_timer.id = TIMER_INACTIVE;
994 tsx->timeout_timer.user_data = tsx;
995 tsx->timeout_timer.cb = &tsx_timer_callback;
996
997 if (grp_lock) {
998 tsx->grp_lock = grp_lock;
999 } else {
1000 status = pj_grp_lock_create(pool, NULL, &tsx->grp_lock);
1001 if (status != PJ_SUCCESS) {
1002 pjsip_endpt_release_pool(mod_tsx_layer.endpt, pool);
1003 return status;
1004 }
1005 }
1006
1007 pj_grp_lock_add_ref(tsx->grp_lock);
1008 pj_grp_lock_add_handler(tsx->grp_lock, tsx->pool, tsx, &tsx_on_destroy);
1009
1010 status = pj_mutex_create_simple(pool, tsx->obj_name, &tsx->mutex_b);
1011 if (status != PJ_SUCCESS) {
1012 tsx_shutdown(tsx);
1013 return status;
1014 }
1015
1016 *p_tsx = tsx;
1017 return PJ_SUCCESS;
1018}
1019
1020/* Really destroy transaction, when grp_lock reference is zero */
1021static void tsx_on_destroy( void *arg )
1022{
1023 pjsip_transaction *tsx = (pjsip_transaction*)arg;
1024
1025 PJ_LOG(5,(tsx->obj_name, "Transaction destroyed!"));
1026
1027 pj_mutex_destroy(tsx->mutex_b);
1028 pjsip_endpt_release_pool(tsx->endpt, tsx->pool);
1029}
1030
1031/* Shutdown transaction. */
1032static pj_status_t tsx_shutdown( pjsip_transaction *tsx )
1033{
1034 /* Release the transport */
1035 tsx_update_transport(tsx, NULL);
1036
1037 /* Decrement reference counter in transport selector, only if
1038 * we haven't been called before */
1039 if (!tsx->terminating) {
1040 pjsip_tpselector_dec_ref(&tsx->tp_sel);
1041 }
1042
1043 /* Free last transmitted message. */
1044 if (tsx->last_tx) {
1045 pjsip_tx_data_dec_ref( tsx->last_tx );
1046 tsx->last_tx = NULL;
1047 }
1048 /* Cancel timeout timer. */
1049 tsx_cancel_timer(tsx, &tsx->timeout_timer);
1050
1051 /* Cancel retransmission timer. */
1052 tsx_cancel_timer(tsx, &tsx->retransmit_timer);
1053
1054 /* Clear some pending flags. */
1055 tsx->transport_flag &= ~(TSX_HAS_PENDING_RESCHED | TSX_HAS_PENDING_SEND);
1056
1057
1058 /* Refuse to destroy transaction if it has pending resolving. */
1059 if (tsx->transport_flag & TSX_HAS_PENDING_TRANSPORT) {
1060 tsx->transport_flag |= TSX_HAS_PENDING_DESTROY;
1061 tsx->tsx_user = NULL;
1062 PJ_LOG(4,(tsx->obj_name, "Will destroy later because transport is "
1063 "in progress"));
1064 }
1065
1066 if (!tsx->terminating) {
1067 tsx->terminating = PJ_TRUE;
1068 pj_grp_lock_dec_ref(tsx->grp_lock);
1069 }
1070
1071 /* No acccess to tsx after this, it may have been destroyed */
1072
1073 return PJ_SUCCESS;
1074}
1075
1076
1077/*
1078 * Callback when timer expires. Transport error also piggybacks this event
1079 * to avoid deadlock (https://trac.pjsip.org/repos/ticket/1646).
1080 */
1081static void tsx_timer_callback( pj_timer_heap_t *theap, pj_timer_entry *entry)
1082{
1083 pjsip_transaction *tsx = (pjsip_transaction*) entry->user_data;
1084
1085 PJ_UNUSED_ARG(theap);
1086
1087 if (entry->id == TRANSPORT_ERR_TIMER) {
1088 /* Posted transport error event */
1089 entry->id = 0;
1090 if (tsx->state < PJSIP_TSX_STATE_TERMINATED) {
1091 pjsip_tsx_state_e prev_state;
1092
1093 pj_grp_lock_acquire(tsx->grp_lock);
1094 prev_state = tsx->state;
1095
1096 /* Release transport as it's no longer working. */
1097 tsx_update_transport(tsx, NULL);
1098
1099 if (tsx->status_code < 200) {
1100 pj_str_t err;
1101 char errmsg[PJ_ERR_MSG_SIZE];
1102
1103 err = pj_strerror(tsx->transport_err, errmsg, sizeof(errmsg));
1104 tsx_set_status_code(tsx, PJSIP_SC_TSX_TRANSPORT_ERROR, &err);
1105 }
1106
1107 /* Set transaction state etc, but don't notify TU now,
1108 * otherwise we'll get a deadlock. See:
1109 * https://trac.pjsip.org/repos/ticket/1646
1110 */
1111 tsx_set_state_no_notify( tsx, PJSIP_TSX_STATE_TERMINATED,
1112 PJSIP_EVENT_TRANSPORT_ERROR, NULL);
1113 pj_grp_lock_release(tsx->grp_lock);
1114
1115 /* Now notify TU about state change, WITHOUT holding the
1116 * group lock. It should be safe to do so; transaction will
1117 * not get destroyed because group lock reference counter
1118 * has been incremented by the timer heap.
1119 */
1120 if (tsx->tsx_user && tsx->tsx_user->on_tsx_state) {
1121 pjsip_event e;
1122 PJSIP_EVENT_INIT_TSX_STATE(e, tsx,
1123 PJSIP_EVENT_TRANSPORT_ERROR, NULL,
1124 prev_state);
1125 (*tsx->tsx_user->on_tsx_state)(tsx, &e);
1126 }
1127 }
1128 } else {
1129 pjsip_event event;
1130
1131 entry->id = 0;
1132
1133 PJ_LOG(5,(tsx->obj_name, "%s timer event",
1134 (entry==&tsx->retransmit_timer ? "Retransmit":"Timeout")));
1135 pj_log_push_indent();
1136
1137
1138 PJSIP_EVENT_INIT_TIMER(event, entry);
1139
1140 /* Dispatch event to transaction. */
1141 pj_grp_lock_acquire(tsx->grp_lock);
1142 (*tsx->state_handler)(tsx, &event);
1143 pj_grp_lock_release(tsx->grp_lock);
1144
1145 pj_log_pop_indent();
1146 }
1147}
1148
1149
1150/*
1151 * Set transaction state, and inform TU about the transaction state change.
1152 */
1153static void tsx_set_state( pjsip_transaction *tsx,
1154 pjsip_tsx_state_e state,
1155 pjsip_event_id_e event_src_type,
1156 void *event_src )
1157{
1158 pjsip_tsx_state_e prev_state = tsx->state;
1159
1160 /* New state must be greater than previous state */
1161 pj_assert(state >= tsx->state);
1162
1163 PJ_LOG(5, (tsx->obj_name, "State changed from %s to %s, event=%s",
1164 state_str[tsx->state], state_str[state],
1165 pjsip_event_str(event_src_type)));
1166 pj_log_push_indent();
1167
1168 /* Change state. */
1169 tsx->state = state;
1170
1171 /* Update the state handlers. */
1172 if (tsx->role == PJSIP_ROLE_UAC) {
1173 tsx->state_handler = tsx_state_handler_uac[state];
1174 } else {
1175 tsx->state_handler = tsx_state_handler_uas[state];
1176 }
1177
1178 /* Before informing TU about state changed, inform TU about
1179 * rx event.
1180 */
1181 if (event_src_type==PJSIP_EVENT_RX_MSG && tsx->tsx_user) {
1182 pjsip_rx_data *rdata = (pjsip_rx_data*) event_src;
1183
1184 pj_assert(rdata != NULL);
1185
1186 if (rdata->msg_info.msg->type == PJSIP_RESPONSE_MSG &&
1187 tsx->tsx_user->on_rx_response)
1188 {
1189 (*tsx->tsx_user->on_rx_response)(rdata);
1190 }
1191
1192 }
1193
1194 /* Inform TU about state changed. */
1195 if (tsx->tsx_user && tsx->tsx_user->on_tsx_state) {
1196 pjsip_event e;
1197 PJSIP_EVENT_INIT_TSX_STATE(e, tsx, event_src_type, event_src,
1198 prev_state);
1199 (*tsx->tsx_user->on_tsx_state)(tsx, &e);
1200 }
1201
1202
1203 /* When the transaction is terminated, release transport, and free the
1204 * saved last transmitted message.
1205 */
1206 if (state == PJSIP_TSX_STATE_TERMINATED) {
1207 pj_time_val timeout = {0, 0};
1208
1209 /* If we're still waiting for a message to be sent.. */
1210 if (tsx->transport_flag & TSX_HAS_PENDING_TRANSPORT) {
1211 /* Disassociate ourselves from the outstanding transmit data
1212 * so that when the send callback is called we will be able
1213 * to ignore that (otherwise we'll get assertion, see
1214 * http://trac.pjsip.org/repos/ticket/1033)
1215 */
1216 if (tsx->pending_tx) {
1217 tsx->pending_tx->mod_data[mod_tsx_layer.mod.id] = NULL;
1218 tsx->pending_tx = NULL;
1219 }
1220 tsx->transport_flag &= ~(TSX_HAS_PENDING_TRANSPORT);
1221 }
1222
1223 lock_timer(tsx);
1224 tsx_cancel_timer(tsx, &tsx->timeout_timer);
1225 tsx_schedule_timer( tsx, &tsx->timeout_timer, &timeout, TIMEOUT_TIMER);
1226 unlock_timer(tsx);
1227
1228 } else if (state == PJSIP_TSX_STATE_DESTROYED) {
1229
1230 /* Unregister transaction. */
1231 mod_tsx_layer_unregister_tsx(tsx);
1232
1233 /* Destroy transaction. */
1234 tsx_shutdown(tsx);
1235 }
1236
1237 pj_log_pop_indent();
1238}
1239
1240/* Set transaction state without notifying tsx_user */
1241static void tsx_set_state_no_notify( pjsip_transaction *tsx,
1242 pjsip_tsx_state_e state,
1243 pjsip_event_id_e event_src_type,
1244 void *event_src )
1245{
1246 pjsip_module *tsx_user = tsx->tsx_user;
1247 tsx->tsx_user = NULL;
1248 tsx_set_state(tsx, state, event_src_type, event_src);
1249 tsx->tsx_user = tsx_user;
1250}
1251
1252/*
1253 * Create, initialize, and register UAC transaction.
1254 */
1255PJ_DEF(pj_status_t) pjsip_tsx_create_uac( pjsip_module *tsx_user,
1256 pjsip_tx_data *tdata,
1257 pjsip_transaction **p_tsx)
1258{
1259 return pjsip_tsx_create_uac2(tsx_user, tdata, NULL, p_tsx);
1260}
1261
1262PJ_DEF(pj_status_t) pjsip_tsx_create_uac2(pjsip_module *tsx_user,
1263 pjsip_tx_data *tdata,
1264 pj_grp_lock_t *grp_lock,
1265 pjsip_transaction **p_tsx)
1266{
1267 pjsip_transaction *tsx;
1268 pjsip_msg *msg;
1269 pjsip_cseq_hdr *cseq;
1270 pjsip_via_hdr *via;
1271 pjsip_host_info dst_info;
1272 pj_status_t status;
1273
1274 /* Validate arguments. */
1275 PJ_ASSERT_RETURN(tdata && tdata->msg && p_tsx, PJ_EINVAL);
1276 PJ_ASSERT_RETURN(tdata->msg->type == PJSIP_REQUEST_MSG,
1277 PJSIP_ENOTREQUESTMSG);
1278
1279 /* Method MUST NOT be ACK! */
1280 PJ_ASSERT_RETURN(tdata->msg->line.req.method.id != PJSIP_ACK_METHOD,
1281 PJ_EINVALIDOP);
1282
1283 /* Keep shortcut */
1284 msg = tdata->msg;
1285
1286 /* Make sure CSeq header is present. */
1287 cseq = (pjsip_cseq_hdr*) pjsip_msg_find_hdr(msg, PJSIP_H_CSEQ, NULL);
1288 if (!cseq) {
1289 pj_assert(!"CSeq header not present in outgoing message!");
1290 return PJSIP_EMISSINGHDR;
1291 }
1292
1293
1294 /* Create transaction instance. */
1295 status = tsx_create( tsx_user, grp_lock, &tsx);
1296 if (status != PJ_SUCCESS)
1297 return status;
1298
1299
1300 /* Lock transaction. */
1301 pj_grp_lock_acquire(tsx->grp_lock);
1302
1303 /* Role is UAC. */
1304 tsx->role = PJSIP_ROLE_UAC;
1305
1306 /* Save method. */
1307 pjsip_method_copy( tsx->pool, &tsx->method, &msg->line.req.method);
1308
1309 /* Save CSeq. */
1310 tsx->cseq = cseq->cseq;
1311
1312 /* Generate Via header if it doesn't exist. */
1313 via = (pjsip_via_hdr*) pjsip_msg_find_hdr(msg, PJSIP_H_VIA, NULL);
1314 if (via == NULL) {
1315 via = pjsip_via_hdr_create(tdata->pool);
1316 pjsip_msg_insert_first_hdr(msg, (pjsip_hdr*) via);
1317 }
1318
1319 /* Generate branch parameter if it doesn't exist. */
1320 if (via->branch_param.slen == 0) {
1321 pj_str_t tmp;
1322 via->branch_param.ptr = (char*)
1323 pj_pool_alloc(tsx->pool, PJSIP_MAX_BRANCH_LEN);
1324 via->branch_param.slen = PJSIP_MAX_BRANCH_LEN;
1325 pj_memcpy(via->branch_param.ptr, PJSIP_RFC3261_BRANCH_ID,
1326 PJSIP_RFC3261_BRANCH_LEN);
1327 tmp.ptr = via->branch_param.ptr + PJSIP_RFC3261_BRANCH_LEN + 2;
1328 *(tmp.ptr-2) = 80; *(tmp.ptr-1) = 106;
1329 pj_generate_unique_string( &tmp );
1330
1331 /* Save branch parameter. */
1332 tsx->branch = via->branch_param;
1333
1334 } else {
1335 /* Copy branch parameter. */
1336 pj_strdup(tsx->pool, &tsx->branch, &via->branch_param);
1337 }
1338
1339 /* Generate transaction key. */
1340 create_tsx_key_3261( tsx->pool, &tsx->transaction_key,
1341 PJSIP_ROLE_UAC, &tsx->method,
1342 &via->branch_param);
1343
1344 /* Calculate hashed key value. */
1345#ifdef PRECALC_HASH
1346 tsx->hashed_key = pj_hash_calc_tolower(0, NULL, &tsx->transaction_key);
1347#endif
1348
1349 PJ_LOG(6, (tsx->obj_name, "tsx_key=%.*s", tsx->transaction_key.slen,
1350 tsx->transaction_key.ptr));
1351
1352 /* Begin with State_Null.
1353 * Manually set-up the state becase we don't want to call the callback.
1354 */
1355 tsx->state = PJSIP_TSX_STATE_NULL;
1356 tsx->state_handler = &tsx_on_state_null;
1357
1358 /* Save the message. */
1359 tsx->last_tx = tdata;
1360 pjsip_tx_data_add_ref(tsx->last_tx);
1361
1362 /* Determine whether reliable transport should be used initially.
1363 * This will be updated whenever transport has changed.
1364 */
1365 status = pjsip_get_request_dest(tdata, &dst_info);
1366 if (status != PJ_SUCCESS) {
1367 pj_grp_lock_release(tsx->grp_lock);
1368 tsx_shutdown(tsx);
1369 return status;
1370 }
1371 tsx->is_reliable = (dst_info.flag & PJSIP_TRANSPORT_RELIABLE);
1372
1373 /* Register transaction to hash table. */
1374 status = mod_tsx_layer_register_tsx(tsx);
1375 if (status != PJ_SUCCESS) {
1376 /* The assertion is removed by #1090:
1377 pj_assert(!"Bug in branch_param generator (i.e. not unique)");
1378 */
1379 pj_grp_lock_release(tsx->grp_lock);
1380 tsx_shutdown(tsx);
1381 return status;
1382 }
1383
1384
1385 /* Unlock transaction and return. */
1386 pj_grp_lock_release(tsx->grp_lock);
1387
1388 pj_log_push_indent();
1389 PJ_LOG(5,(tsx->obj_name, "Transaction created for %s",
1390 pjsip_tx_data_get_info(tdata)));
1391 pj_log_pop_indent();
1392
1393 *p_tsx = tsx;
1394 return PJ_SUCCESS;
1395}
1396
1397
1398/*
1399 * Create, initialize, and register UAS transaction.
1400 */
1401PJ_DEF(pj_status_t) pjsip_tsx_create_uas( pjsip_module *tsx_user,
1402 pjsip_rx_data *rdata,
1403 pjsip_transaction **p_tsx)
1404{
1405 return pjsip_tsx_create_uas2(tsx_user, rdata, NULL, p_tsx);
1406}
1407
1408PJ_DEF(pj_status_t) pjsip_tsx_create_uas2(pjsip_module *tsx_user,
1409 pjsip_rx_data *rdata,
1410 pj_grp_lock_t *grp_lock,
1411 pjsip_transaction **p_tsx)
1412{
1413 pjsip_transaction *tsx;
1414 pjsip_msg *msg;
1415 pj_str_t *branch;
1416 pjsip_cseq_hdr *cseq;
1417 pj_status_t status;
1418
1419 /* Validate arguments. */
1420 PJ_ASSERT_RETURN(rdata && rdata->msg_info.msg && p_tsx, PJ_EINVAL);
1421
1422 /* Keep shortcut to message */
1423 msg = rdata->msg_info.msg;
1424
1425 /* Make sure this is a request message. */
1426 PJ_ASSERT_RETURN(msg->type == PJSIP_REQUEST_MSG, PJSIP_ENOTREQUESTMSG);
1427
1428 /* Make sure method is not ACK */
1429 PJ_ASSERT_RETURN(msg->line.req.method.id != PJSIP_ACK_METHOD,
1430 PJ_EINVALIDOP);
1431
1432 /* Make sure CSeq header is present. */
1433 cseq = rdata->msg_info.cseq;
1434 if (!cseq)
1435 return PJSIP_EMISSINGHDR;
1436
1437 /* Make sure Via header is present. */
1438 if (rdata->msg_info.via == NULL)
1439 return PJSIP_EMISSINGHDR;
1440
1441 /* Check that method in CSeq header match request method.
1442 * Reference: PROTOS #1922
1443 */
1444 if (pjsip_method_cmp(&msg->line.req.method,
1445 &rdata->msg_info.cseq->method) != 0)
1446 {
1447 PJ_LOG(4,(THIS_FILE, "Error: CSeq header contains different "
1448 "method than the request line"));
1449 return PJSIP_EINVALIDHDR;
1450 }
1451
1452 /*
1453 * Create transaction instance.
1454 */
1455 status = tsx_create( tsx_user, grp_lock, &tsx);
1456 if (status != PJ_SUCCESS)
1457 return status;
1458
1459
1460 /* Lock transaction. */
1461 pj_grp_lock_acquire(tsx->grp_lock);
1462
1463 /* Role is UAS */
1464 tsx->role = PJSIP_ROLE_UAS;
1465
1466 /* Save method. */
1467 pjsip_method_copy( tsx->pool, &tsx->method, &msg->line.req.method);
1468
1469 /* Save CSeq */
1470 tsx->cseq = cseq->cseq;
1471
1472 /* Get transaction key either from branch for RFC3261 message, or
1473 * create transaction key.
1474 */
1475 status = pjsip_tsx_create_key(tsx->pool, &tsx->transaction_key,
1476 PJSIP_ROLE_UAS, &tsx->method, rdata);
1477 if (status != PJ_SUCCESS) {
1478 pj_grp_lock_release(tsx->grp_lock);
1479 tsx_shutdown(tsx);
1480 return status;
1481 }
1482
1483 /* Calculate hashed key value. */
1484#ifdef PRECALC_HASH
1485 tsx->hashed_key = pj_hash_calc_tolower(0, NULL, &tsx->transaction_key);
1486#endif
1487
1488 /* Duplicate branch parameter for transaction. */
1489 branch = &rdata->msg_info.via->branch_param;
1490 pj_strdup(tsx->pool, &tsx->branch, branch);
1491
1492 PJ_LOG(6, (tsx->obj_name, "tsx_key=%.*s", tsx->transaction_key.slen,
1493 tsx->transaction_key.ptr));
1494
1495
1496 /* Begin with state NULL.
1497 * Manually set-up the state becase we don't want to call the callback.
1498 */
1499 tsx->state = PJSIP_TSX_STATE_NULL;
1500 tsx->state_handler = &tsx_on_state_null;
1501
1502 /* Get response address. */
1503 status = pjsip_get_response_addr( tsx->pool, rdata, &tsx->res_addr );
1504 if (status != PJ_SUCCESS) {
1505 pj_grp_lock_release(tsx->grp_lock);
1506 tsx_shutdown(tsx);
1507 return status;
1508 }
1509
1510 /* If it's decided that we should use current transport, keep the
1511 * transport.
1512 */
1513 if (tsx->res_addr.transport) {
1514 tsx_update_transport(tsx, tsx->res_addr.transport);
1515 pj_memcpy(&tsx->addr, &tsx->res_addr.addr, tsx->res_addr.addr_len);
1516 tsx->addr_len = tsx->res_addr.addr_len;
1517 tsx->is_reliable = PJSIP_TRANSPORT_IS_RELIABLE(tsx->transport);
1518 } else {
1519 tsx->is_reliable =
1520 (tsx->res_addr.dst_host.flag & PJSIP_TRANSPORT_RELIABLE);
1521 }
1522
1523
1524 /* Register the transaction. */
1525 status = mod_tsx_layer_register_tsx(tsx);
1526 if (status != PJ_SUCCESS) {
1527 pj_grp_lock_release(tsx->grp_lock);
1528 tsx_shutdown(tsx);
1529 return status;
1530 }
1531
1532 /* Put this transaction in rdata's mod_data. */
1533 rdata->endpt_info.mod_data[mod_tsx_layer.mod.id] = tsx;
1534
1535 /* Unlock transaction and return. */
1536 pj_grp_lock_release(tsx->grp_lock);
1537
1538 pj_log_push_indent();
1539 PJ_LOG(5,(tsx->obj_name, "Transaction created for %s",
1540 pjsip_rx_data_get_info(rdata)));
1541 pj_log_pop_indent();
1542
1543
1544 *p_tsx = tsx;
1545 return PJ_SUCCESS;
1546}
1547
1548
1549/*
1550 * Bind transaction to a specific transport/listener.
1551 */
1552PJ_DEF(pj_status_t) pjsip_tsx_set_transport(pjsip_transaction *tsx,
1553 const pjsip_tpselector *sel)
1554{
1555 /* Must be UAC transaction */
1556 PJ_ASSERT_RETURN(tsx && sel, PJ_EINVAL);
1557
1558 /* Start locking the transaction. */
1559 pj_grp_lock_acquire(tsx->grp_lock);
1560
1561 /* Decrement reference counter of previous transport selector */
1562 pjsip_tpselector_dec_ref(&tsx->tp_sel);
1563
1564 /* Copy transport selector structure .*/
1565 pj_memcpy(&tsx->tp_sel, sel, sizeof(*sel));
1566
1567 /* Increment reference counter */
1568 pjsip_tpselector_add_ref(&tsx->tp_sel);
1569
1570 /* Unlock transaction. */
1571 pj_grp_lock_release(tsx->grp_lock);
1572
1573 return PJ_SUCCESS;
1574}
1575
1576
1577/*
1578 * Set transaction status code and reason.
1579 */
1580static void tsx_set_status_code(pjsip_transaction *tsx,
1581 int code, const pj_str_t *reason)
1582{
1583 tsx->status_code = code;
1584 if (reason)
1585 pj_strdup(tsx->pool, &tsx->status_text, reason);
1586 else
1587 tsx->status_text = *pjsip_get_status_text(code);
1588}
1589
1590
1591/*
1592 * Forcely terminate transaction.
1593 */
1594PJ_DEF(pj_status_t) pjsip_tsx_terminate( pjsip_transaction *tsx, int code )
1595{
1596 PJ_ASSERT_RETURN(tsx != NULL, PJ_EINVAL);
1597
1598 PJ_LOG(5,(tsx->obj_name, "Request to terminate transaction"));
1599
1600 PJ_ASSERT_RETURN(code >= 200, PJ_EINVAL);
1601
1602 if (tsx->state >= PJSIP_TSX_STATE_TERMINATED)
1603 return PJ_SUCCESS;
1604
1605 pj_log_push_indent();
1606
1607 pj_grp_lock_acquire(tsx->grp_lock);
1608 tsx_set_status_code(tsx, code, NULL);
1609 tsx_set_state( tsx, PJSIP_TSX_STATE_TERMINATED, PJSIP_EVENT_USER, NULL);
1610 pj_grp_lock_release(tsx->grp_lock);
1611
1612 pj_log_pop_indent();
1613
1614 return PJ_SUCCESS;
1615}
1616
1617
1618/*
1619 * Cease retransmission on the UAC transaction. The UAC transaction is
1620 * still considered running, and it will complete when either final
1621 * response is received or the transaction times out.
1622 */
1623PJ_DEF(pj_status_t) pjsip_tsx_stop_retransmit(pjsip_transaction *tsx)
1624{
1625 PJ_ASSERT_RETURN(tsx != NULL, PJ_EINVAL);
1626 PJ_ASSERT_RETURN(tsx->role == PJSIP_ROLE_UAC &&
1627 tsx->method.id == PJSIP_INVITE_METHOD,
1628 PJ_EINVALIDOP);
1629
1630 PJ_LOG(5,(tsx->obj_name, "Request to stop retransmission"));
1631
1632 pj_log_push_indent();
1633
1634 pj_grp_lock_acquire(tsx->grp_lock);
1635 /* Cancel retransmission timer. */
1636 tsx_cancel_timer(tsx, &tsx->retransmit_timer);
1637 pj_grp_lock_release(tsx->grp_lock);
1638
1639 pj_log_pop_indent();
1640
1641 return PJ_SUCCESS;
1642}
1643
1644
1645/*
1646 * Start a timer to terminate transaction after the specified time
1647 * has elapsed.
1648 */
1649PJ_DEF(pj_status_t) pjsip_tsx_set_timeout( pjsip_transaction *tsx,
1650 unsigned millisec)
1651{
1652 pj_time_val timeout;
1653
1654 PJ_ASSERT_RETURN(tsx != NULL, PJ_EINVAL);
1655 PJ_ASSERT_RETURN(tsx->role == PJSIP_ROLE_UAC &&
1656 tsx->method.id == PJSIP_INVITE_METHOD,
1657 PJ_EINVALIDOP);
1658
1659 /* Note: must not call pj_grp_lock_acquire(tsx->grp_lock) as
1660 * that would introduce deadlock. See #1121.
1661 */
1662 lock_timer(tsx);
1663
1664 /* Transaction should normally not have final response, but as
1665 * #1121 says there is a (tolerable) window of race condition
1666 * where this might happen.
1667 */
1668 if (tsx->status_code >= 200 && tsx->timeout_timer.id != 0) {
1669 /* Timeout is already set */
1670 unlock_timer(tsx);
1671 return PJ_EEXISTS;
1672 }
1673
1674 tsx_cancel_timer(tsx, &tsx->timeout_timer);
1675
1676 timeout.sec = 0;
1677 timeout.msec = millisec;
1678 pj_time_val_normalize(&timeout);
1679
1680 tsx_schedule_timer(tsx, &tsx->timeout_timer, &timeout, TIMEOUT_TIMER);
1681
1682 unlock_timer(tsx);
1683
1684 return PJ_SUCCESS;
1685}
1686
1687
1688/*
1689 * This function is called by TU to send a message.
1690 */
1691PJ_DEF(pj_status_t) pjsip_tsx_send_msg( pjsip_transaction *tsx,
1692 pjsip_tx_data *tdata )
1693{
1694 pjsip_event event;
1695 pj_status_t status;
1696
1697 if (tdata == NULL)
1698 tdata = tsx->last_tx;
1699
1700 PJ_ASSERT_RETURN(tdata != NULL, PJ_EINVALIDOP);
1701
1702 PJ_LOG(5,(tsx->obj_name, "Sending %s in state %s",
1703 pjsip_tx_data_get_info(tdata),
1704 state_str[tsx->state]));
1705 pj_log_push_indent();
1706
1707 PJSIP_EVENT_INIT_TX_MSG(event, tdata);
1708
1709 /* Dispatch to transaction. */
1710 pj_grp_lock_acquire(tsx->grp_lock);
1711
1712 /* Set transport selector to tdata */
1713 pjsip_tx_data_set_transport(tdata, &tsx->tp_sel);
1714
1715 /* Dispatch to state handler */
1716 status = (*tsx->state_handler)(tsx, &event);
1717
1718 pj_grp_lock_release(tsx->grp_lock);
1719
1720 /* Only decrement reference counter when it returns success.
1721 * (This is the specification from the .PDF design document).
1722 */
1723 if (status == PJ_SUCCESS) {
1724 pjsip_tx_data_dec_ref(tdata);
1725 }
1726
1727 pj_log_pop_indent();
1728
1729 return status;
1730}
1731
1732
1733/*
1734 * This function is called by endpoint when incoming message for the
1735 * transaction is received.
1736 */
1737PJ_DEF(void) pjsip_tsx_recv_msg( pjsip_transaction *tsx,
1738 pjsip_rx_data *rdata)
1739{
1740 pjsip_event event;
1741
1742 PJ_LOG(5,(tsx->obj_name, "Incoming %s in state %s",
1743 pjsip_rx_data_get_info(rdata), state_str[tsx->state]));
1744 pj_log_push_indent();
1745
1746 /* Put the transaction in the rdata's mod_data. */
1747 rdata->endpt_info.mod_data[mod_tsx_layer.mod.id] = tsx;
1748
1749 /* Init event. */
1750 PJSIP_EVENT_INIT_RX_MSG(event, rdata);
1751
1752 /* Dispatch to transaction. */
1753 pj_grp_lock_acquire(tsx->grp_lock);
1754 (*tsx->state_handler)(tsx, &event);
1755 pj_grp_lock_release(tsx->grp_lock);
1756
1757 pj_log_pop_indent();
1758}
1759
1760
1761/* Callback called by send message framework */
1762static void send_msg_callback( pjsip_send_state *send_state,
1763 pj_ssize_t sent, pj_bool_t *cont )
1764{
1765 pjsip_transaction *tsx = (pjsip_transaction*) send_state->token;
1766 pjsip_tx_data *tdata = send_state->tdata;
1767
1768 /* Check if transaction has cancelled itself from this transmit
1769 * notification (https://trac.pjsip.org/repos/ticket/1033).
1770 * Also check if the transaction layer itself may have been shutdown
1771 * (https://trac.pjsip.org/repos/ticket/1535)
1772 */
1773 if (mod_tsx_layer.mod.id < 0 ||
1774 tdata->mod_data[mod_tsx_layer.mod.id] == NULL)
1775 {
1776 *cont = PJ_FALSE;
1777 return;
1778 }
1779
1780 pj_grp_lock_acquire(tsx->grp_lock);
1781
1782 /* Reset */
1783 tdata->mod_data[mod_tsx_layer.mod.id] = NULL;
1784 tsx->pending_tx = NULL;
1785
1786 if (sent > 0) {
1787 /* Successfully sent! */
1788 pj_assert(send_state->cur_transport != NULL);
1789
1790 if (tsx->transport != send_state->cur_transport) {
1791 /* Update transport. */
1792 tsx_update_transport(tsx, send_state->cur_transport);
1793
1794 /* Update remote address. */
1795 tsx->addr_len = tdata->dest_info.addr.entry[tdata->dest_info.cur_addr].addr_len;
1796 pj_memcpy(&tsx->addr,
1797 &tdata->dest_info.addr.entry[tdata->dest_info.cur_addr].addr,
1798 tsx->addr_len);
1799
1800 /* Update is_reliable flag. */
1801 tsx->is_reliable = PJSIP_TRANSPORT_IS_RELIABLE(tsx->transport);
1802 }
1803
1804 /* Clear pending transport flag. */
1805 tsx->transport_flag &= ~(TSX_HAS_PENDING_TRANSPORT);
1806
1807 /* Mark that we have resolved the addresses. */
1808 tsx->transport_flag |= TSX_HAS_RESOLVED_SERVER;
1809
1810 /* Pending destroy? */
1811 if (tsx->transport_flag & TSX_HAS_PENDING_DESTROY) {
1812 tsx_set_state( tsx, PJSIP_TSX_STATE_DESTROYED,
1813 PJSIP_EVENT_UNKNOWN, NULL );
1814 pj_grp_lock_release(tsx->grp_lock);
1815 return;
1816 }
1817
1818 /* Need to transmit a message? */
1819 if (tsx->transport_flag & TSX_HAS_PENDING_SEND) {
1820 tsx->transport_flag &= ~(TSX_HAS_PENDING_SEND);
1821 tsx_send_msg(tsx, tsx->last_tx);
1822 }
1823
1824 /* Need to reschedule retransmission? */
1825 if (tsx->transport_flag & TSX_HAS_PENDING_RESCHED) {
1826 tsx->transport_flag &= ~(TSX_HAS_PENDING_RESCHED);
1827
1828 /* Only update when transport turns out to be unreliable. */
1829 if (!tsx->is_reliable) {
1830 tsx_resched_retransmission(tsx);
1831 }
1832 }
1833
1834 } else {
1835 /* Failed to send! */
1836 pj_assert(sent != 0);
1837
1838 /* If transaction is using the same transport as the failed one,
1839 * release the transport.
1840 */
1841 if (send_state->cur_transport==tsx->transport)
1842 tsx_update_transport(tsx, NULL);
1843
1844 /* Also stop processing if transaction has been flagged with
1845 * pending destroy (http://trac.pjsip.org/repos/ticket/906)
1846 */
1847 if ((!*cont) || (tsx->transport_flag & TSX_HAS_PENDING_DESTROY)) {
1848 char errmsg[PJ_ERR_MSG_SIZE];
1849 pjsip_status_code sc;
1850 pj_str_t err;
1851
1852 tsx->transport_err = (pj_status_t)-sent;
1853
1854 err =pj_strerror((pj_status_t)-sent, errmsg, sizeof(errmsg));
1855
1856 PJ_LOG(2,(tsx->obj_name,
1857 "Failed to send %s! err=%d (%s)",
1858 pjsip_tx_data_get_info(send_state->tdata), -sent,
1859 errmsg));
1860
1861 /* Clear pending transport flag. */
1862 tsx->transport_flag &= ~(TSX_HAS_PENDING_TRANSPORT);
1863
1864 /* Mark that we have resolved the addresses. */
1865 tsx->transport_flag |= TSX_HAS_RESOLVED_SERVER;
1866
1867 /* Server resolution error is now mapped to 502 instead of 503,
1868 * since with 503 normally client should try again.
1869 * See http://trac.pjsip.org/repos/ticket/870
1870 */
1871 if (-sent==PJ_ERESOLVE || -sent==PJLIB_UTIL_EDNS_NXDOMAIN)
1872 sc = PJSIP_SC_BAD_GATEWAY;
1873 else
1874 sc = PJSIP_SC_TSX_TRANSPORT_ERROR;
1875
1876 /* Terminate transaction, if it's not already terminated. */
1877 tsx_set_status_code(tsx, sc, &err);
1878 if (tsx->state != PJSIP_TSX_STATE_TERMINATED &&
1879 tsx->state != PJSIP_TSX_STATE_DESTROYED)
1880 {
1881 tsx_set_state( tsx, PJSIP_TSX_STATE_TERMINATED,
1882 PJSIP_EVENT_TRANSPORT_ERROR, send_state->tdata);
1883 }
1884 /* Don't forget to destroy if we have pending destroy flag
1885 * (http://trac.pjsip.org/repos/ticket/906)
1886 */
1887 else if (tsx->transport_flag & TSX_HAS_PENDING_DESTROY)
1888 {
1889 tsx_set_state( tsx, PJSIP_TSX_STATE_DESTROYED,
1890 PJSIP_EVENT_TRANSPORT_ERROR, send_state->tdata);
1891 }
1892
1893 } else {
1894 PJ_PERROR(2,(tsx->obj_name, (pj_status_t)-sent,
1895 "Temporary failure in sending %s, "
1896 "will try next server",
1897 pjsip_tx_data_get_info(send_state->tdata)));
1898
1899 /* Reset retransmission count */
1900 tsx->retransmit_count = 0;
1901
1902 /* And reset timeout timer */
1903 if (tsx->timeout_timer.id) {
1904 lock_timer(tsx);
1905 tsx_cancel_timer(tsx, &tsx->timeout_timer);
1906 tsx_schedule_timer( tsx, &tsx->timeout_timer,
1907 &timeout_timer_val, TIMEOUT_TIMER);
1908 unlock_timer(tsx);
1909 }
1910
1911 /* Put again pending tdata */
1912 tdata->mod_data[mod_tsx_layer.mod.id] = tsx;
1913 tsx->pending_tx = tdata;
1914 }
1915 }
1916
1917 pj_grp_lock_release(tsx->grp_lock);
1918}
1919
1920
1921/* Transport callback. */
1922static void transport_callback(void *token, pjsip_tx_data *tdata,
1923 pj_ssize_t sent)
1924{
1925 pjsip_transaction *tsx = (pjsip_transaction*) token;
1926
1927 /* In other circumstances, locking tsx->grp_lock AFTER transport mutex
1928 * will introduce deadlock if another thread is currently sending a
1929 * SIP message to the transport. But this should be safe as there should
1930 * be no way this callback could be called while another thread is
1931 * sending a message.
1932 */
1933 pj_grp_lock_acquire(tsx->grp_lock);
1934 tsx->transport_flag &= ~(TSX_HAS_PENDING_TRANSPORT);
1935 pj_grp_lock_release(tsx->grp_lock);
1936
1937 if (sent < 0) {
1938 pj_time_val delay = {0, 0};
1939 char errmsg[PJ_ERR_MSG_SIZE];
1940
1941 pj_strerror((pj_status_t)-sent, errmsg, sizeof(errmsg));
1942
1943 PJ_LOG(2,(tsx->obj_name, "Transport failed to send %s! Err=%d (%s)",
1944 pjsip_tx_data_get_info(tdata), -sent, errmsg));
1945
1946 /* Post the event for later processing, to avoid deadlock.
1947 * See https://trac.pjsip.org/repos/ticket/1646
1948 */
1949 lock_timer(tsx);
1950 tsx->transport_err = (pj_status_t)-sent;
1951 tsx_cancel_timer(tsx, &tsx->timeout_timer);
1952 tsx_schedule_timer(tsx, &tsx->timeout_timer, &delay,
1953 TRANSPORT_ERR_TIMER);
1954 unlock_timer(tsx);
1955 }
1956
1957 /* Decrease pending send counter */
1958 pj_grp_lock_dec_ref(tsx->grp_lock);
1959}
1960
1961
1962/*
1963 * Callback when transport state changes.
1964 */
1965static void tsx_tp_state_callback( pjsip_transport *tp,
1966 pjsip_transport_state state,
1967 const pjsip_transport_state_info *info)
1968{
1969 PJ_UNUSED_ARG(tp);
1970
1971 if (state == PJSIP_TP_STATE_DISCONNECTED) {
1972 pjsip_transaction *tsx;
1973 pj_time_val delay = {0, 0};
1974
1975 pj_assert(tp && info && info->user_data);
1976
1977 tsx = (pjsip_transaction*)info->user_data;
1978
1979 /* Post the event for later processing, to avoid deadlock.
1980 * See https://trac.pjsip.org/repos/ticket/1646
1981 */
1982 lock_timer(tsx);
1983 tsx->transport_err = info->status;
1984 tsx_cancel_timer(tsx, &tsx->timeout_timer);
1985 tsx_schedule_timer(tsx, &tsx->timeout_timer, &delay,
1986 TRANSPORT_ERR_TIMER);
1987 unlock_timer(tsx);
1988 }
1989}
1990
1991
1992/*
1993 * Send message to the transport.
1994 */
1995static pj_status_t tsx_send_msg( pjsip_transaction *tsx,
1996 pjsip_tx_data *tdata)
1997{
1998 pj_status_t status = PJ_SUCCESS;
1999
2000 PJ_ASSERT_RETURN(tsx && tdata, PJ_EINVAL);
2001
2002 /* Send later if transport is still pending. */
2003 if (tsx->transport_flag & TSX_HAS_PENDING_TRANSPORT) {
2004 tsx->transport_flag |= TSX_HAS_PENDING_SEND;
2005 return PJ_SUCCESS;
2006 }
2007
2008 /* Skip send if previous tdata transmission is pending (see #1665). */
2009 if (tdata->is_pending) {
2010 PJ_LOG(2,(THIS_FILE, "Unable to send %s: message is pending",
2011 pjsip_tx_data_get_info(tdata)));
2012 return PJ_SUCCESS;
2013 }
2014
2015 /* If we have the transport, send the message using that transport.
2016 * Otherwise perform full transport resolution.
2017 */
2018 if (tsx->transport) {
2019 /* Increment group lock while waiting for send operation to complete,
2020 * to prevent us from being destroyed prematurely. See
2021 * https://trac.pjsip.org/repos/ticket/1646
2022 */
2023 pj_grp_lock_add_ref(tsx->grp_lock);
2024 tsx->transport_flag |= TSX_HAS_PENDING_TRANSPORT;
2025
2026 status = pjsip_transport_send( tsx->transport, tdata, &tsx->addr,
2027 tsx->addr_len, tsx,
2028 &transport_callback);
2029 if (status == PJ_EPENDING)
2030 status = PJ_SUCCESS;
2031 else {
2032 /* Operation completes immediately */
2033 tsx->transport_flag &= ~(TSX_HAS_PENDING_TRANSPORT);
2034 pj_grp_lock_dec_ref(tsx->grp_lock);
2035 }
2036
2037 if (status != PJ_SUCCESS) {
2038 PJ_PERROR(2,(tsx->obj_name, status,
2039 "Error sending %s",
2040 pjsip_tx_data_get_info(tdata)));
2041
2042 /* On error, release transport to force using full transport
2043 * resolution procedure.
2044 */
2045 tsx_update_transport(tsx, NULL);
2046
2047 tsx->addr_len = 0;
2048 tsx->res_addr.transport = NULL;
2049 tsx->res_addr.addr_len = 0;
2050 } else {
2051 return PJ_SUCCESS;
2052 }
2053 }
2054
2055 /* We are here because we don't have transport, or we failed to send
2056 * the message using existing transport. If we haven't resolved the
2057 * server before, then begin the long process of resolving the server
2058 * and send the message with possibly new server.
2059 */
2060 pj_assert(status != PJ_SUCCESS || tsx->transport == NULL);
2061
2062 /* If we have resolved the server, we treat the error as permanent error.
2063 * Terminate transaction with transport error failure.
2064 */
2065 if (tsx->transport_flag & TSX_HAS_RESOLVED_SERVER) {
2066
2067 char errmsg[PJ_ERR_MSG_SIZE];
2068 pj_str_t err;
2069
2070 if (status == PJ_SUCCESS) {
2071 pj_assert(!"Unexpected status!");
2072 status = PJ_EUNKNOWN;
2073 }
2074
2075 /* We have resolved the server!.
2076 * Treat this as permanent transport error.
2077 */
2078 err = pj_strerror(status, errmsg, sizeof(errmsg));
2079
2080 PJ_LOG(2,(tsx->obj_name,
2081 "Transport error, terminating transaction. "
2082 "Err=%d (%s)",
2083 status, errmsg));
2084
2085 tsx_set_status_code(tsx, PJSIP_SC_TSX_TRANSPORT_ERROR, &err);
2086 tsx_set_state( tsx, PJSIP_TSX_STATE_TERMINATED,
2087 PJSIP_EVENT_TRANSPORT_ERROR, NULL );
2088
2089 return status;
2090 }
2091
2092 /* Must add reference counter because the send request functions
2093 * decrement the reference counter.
2094 */
2095 pjsip_tx_data_add_ref(tdata);
2096
2097 /* Also attach ourselves to the transmit data so that we'll be able
2098 * to unregister ourselves from the send notification of this
2099 * transmit data.
2100 */
2101 tdata->mod_data[mod_tsx_layer.mod.id] = tsx;
2102 tsx->pending_tx = tdata;
2103
2104 /* Begin resolving destination etc to send the message. */
2105 if (tdata->msg->type == PJSIP_REQUEST_MSG) {
2106
2107 tsx->transport_flag |= TSX_HAS_PENDING_TRANSPORT;
2108 status = pjsip_endpt_send_request_stateless(tsx->endpt, tdata, tsx,
2109 &send_msg_callback);
2110 if (status == PJ_EPENDING)
2111 status = PJ_SUCCESS;
2112 if (status != PJ_SUCCESS) {
2113 pjsip_tx_data_dec_ref(tdata);
2114 tdata->mod_data[mod_tsx_layer.mod.id] = NULL;
2115 tsx->pending_tx = NULL;
2116 }
2117
2118 /* Check if transaction is terminated. */
2119 if (status==PJ_SUCCESS && tsx->state == PJSIP_TSX_STATE_TERMINATED)
2120 status = tsx->transport_err;
2121
2122 } else {
2123
2124 tsx->transport_flag |= TSX_HAS_PENDING_TRANSPORT;
2125 status = pjsip_endpt_send_response( tsx->endpt, &tsx->res_addr,
2126 tdata, tsx,
2127 &send_msg_callback);
2128 if (status == PJ_EPENDING)
2129 status = PJ_SUCCESS;
2130 if (status != PJ_SUCCESS) {
2131 pjsip_tx_data_dec_ref(tdata);
2132 tdata->mod_data[mod_tsx_layer.mod.id] = NULL;
2133 tsx->pending_tx = NULL;
2134 }
2135
2136 /* Check if transaction is terminated. */
2137 if (status==PJ_SUCCESS && tsx->state == PJSIP_TSX_STATE_TERMINATED)
2138 status = tsx->transport_err;
2139
2140 }
2141
2142
2143 return status;
2144}
2145
2146
2147/*
2148 * Manually retransmit the last messagewithout updating the transaction state.
2149 */
2150PJ_DEF(pj_status_t) pjsip_tsx_retransmit_no_state(pjsip_transaction *tsx,
2151 pjsip_tx_data *tdata)
2152{
2153 pj_status_t status;
2154
2155 pj_grp_lock_acquire(tsx->grp_lock);
2156 if (tdata == NULL) {
2157 tdata = tsx->last_tx;
2158 }
2159 status = tsx_send_msg(tsx, tdata);
2160 pj_grp_lock_release(tsx->grp_lock);
2161
2162 /* Only decrement reference counter when it returns success.
2163 * (This is the specification from the .PDF design document).
2164 */
2165 if (status == PJ_SUCCESS) {
2166 pjsip_tx_data_dec_ref(tdata);
2167 }
2168
2169 return status;
2170}
2171
2172
2173/*
2174 * Retransmit last message sent.
2175 */
2176static void tsx_resched_retransmission( pjsip_transaction *tsx )
2177{
2178 pj_uint32_t msec_time;
2179
2180 pj_assert((tsx->transport_flag & TSX_HAS_PENDING_TRANSPORT) == 0);
2181
2182 if (tsx->role==PJSIP_ROLE_UAC && tsx->status_code >= 100)
2183 msec_time = pjsip_cfg()->tsx.t2;
2184 else
2185 msec_time = (1 << (tsx->retransmit_count)) * pjsip_cfg()->tsx.t1;
2186
2187 if (tsx->role == PJSIP_ROLE_UAC) {
2188 pj_assert(tsx->status_code < 200);
2189 /* Retransmission for non-INVITE transaction caps-off at T2 */
2190 if (msec_time > pjsip_cfg()->tsx.t2 &&
2191 tsx->method.id != PJSIP_INVITE_METHOD)
2192 {
2193 msec_time = pjsip_cfg()->tsx.t2;
2194 }
2195 } else {
2196 /* For UAS, this can be retransmission of 2xx response for INVITE
2197 * or non-100 1xx response.
2198 */
2199 if (tsx->status_code < 200) {
2200 /* non-100 1xx retransmission is at 60 seconds */
2201 msec_time = PJSIP_TSX_1XX_RETRANS_DELAY * 1000;
2202 } else {
2203 /* Retransmission of INVITE final response also caps-off at T2 */
2204 pj_assert(tsx->status_code >= 200);
2205 if (msec_time > pjsip_cfg()->tsx.t2)
2206 msec_time = pjsip_cfg()->tsx.t2;
2207 }
2208 }
2209
2210 if (msec_time != 0) {
2211 pj_time_val timeout;
2212
2213 timeout.sec = msec_time / 1000;
2214 timeout.msec = msec_time % 1000;
2215 tsx_schedule_timer( tsx, &tsx->retransmit_timer, &timeout,
2216 RETRANSMIT_TIMER);
2217 }
2218}
2219
2220/*
2221 * Retransmit last message sent.
2222 */
2223static pj_status_t tsx_retransmit( pjsip_transaction *tsx, int resched)
2224{
2225 pj_status_t status;
2226
2227 PJ_ASSERT_RETURN(tsx->last_tx!=NULL, PJ_EBUG);
2228
2229 PJ_LOG(5,(tsx->obj_name, "Retransmiting %s, count=%d, restart?=%d",
2230 pjsip_tx_data_get_info(tsx->last_tx),
2231 tsx->retransmit_count, resched));
2232
2233 ++tsx->retransmit_count;
2234
2235 /* Restart timer T1 first before sending the message to ensure that
2236 * retransmission timer is not engaged when loop transport is used.
2237 */
2238 if (resched) {
2239 pj_assert(tsx->state != PJSIP_TSX_STATE_CONFIRMED);
2240 if (tsx->transport_flag & TSX_HAS_PENDING_TRANSPORT) {
2241 tsx->transport_flag |= TSX_HAS_PENDING_RESCHED;
2242 } else {
2243 tsx_resched_retransmission(tsx);
2244 }
2245 }
2246
2247 status = tsx_send_msg( tsx, tsx->last_tx);
2248 if (status != PJ_SUCCESS) {
2249 return status;
2250 }
2251
2252 return PJ_SUCCESS;
2253}
2254
2255static void tsx_update_transport( pjsip_transaction *tsx,
2256 pjsip_transport *tp)
2257{
2258 pj_assert(tsx);
2259
2260 if (tsx->transport) {
2261 pjsip_transport_remove_state_listener(tsx->transport,
2262 tsx->tp_st_key, tsx);
2263 pjsip_transport_dec_ref( tsx->transport );
2264 tsx->transport = NULL;
2265 }
2266
2267 if (tp) {
2268 tsx->transport = tp;
2269 pjsip_transport_add_ref(tp);
2270 pjsip_transport_add_state_listener(tp, &tsx_tp_state_callback, tsx,
2271 &tsx->tp_st_key);
2272 if (tp->is_shutdown) {
2273 pjsip_transport_state_info info;
2274
2275 pj_bzero(&info, sizeof(info));
2276 info.user_data = tsx;
2277 info.status = PJSIP_SC_TSX_TRANSPORT_ERROR;
2278 tsx_tp_state_callback(tp, PJSIP_TP_STATE_DISCONNECTED, &info);
2279 }
2280 }
2281}
2282
2283/*
2284 * Handler for events in state Null.
2285 */
2286static pj_status_t tsx_on_state_null( pjsip_transaction *tsx,
2287 pjsip_event *event )
2288{
2289 pj_status_t status;
2290
2291 pj_assert(tsx->state == PJSIP_TSX_STATE_NULL);
2292
2293 if (tsx->role == PJSIP_ROLE_UAS) {
2294
2295 /* Set state to Trying. */
2296 pj_assert(event->type == PJSIP_EVENT_RX_MSG &&
2297 event->body.rx_msg.rdata->msg_info.msg->type ==
2298 PJSIP_REQUEST_MSG);
2299 tsx_set_state( tsx, PJSIP_TSX_STATE_TRYING, PJSIP_EVENT_RX_MSG,
2300 event->body.rx_msg.rdata);
2301
2302 } else {
2303 pjsip_tx_data *tdata;
2304
2305 /* Must be transmit event.
2306 * You may got this assertion when using loop transport with delay
2307 * set to zero. That would cause on_rx_response() callback to be
2308 * called before tsx_send_msg() has completed.
2309 */
2310 PJ_ASSERT_RETURN(event->type == PJSIP_EVENT_TX_MSG, PJ_EBUG);
2311
2312 /* Get the txdata */
2313 tdata = event->body.tx_msg.tdata;
2314
2315 /* Save the message for retransmission. */
2316 if (tsx->last_tx && tsx->last_tx != tdata) {
2317 pjsip_tx_data_dec_ref(tsx->last_tx);
2318 tsx->last_tx = NULL;
2319 }
2320 if (tsx->last_tx != tdata) {
2321 tsx->last_tx = tdata;
2322 pjsip_tx_data_add_ref(tdata);
2323 }
2324
2325 /* Send the message. */
2326 status = tsx_send_msg( tsx, tdata);
2327 if (status != PJ_SUCCESS) {
2328 return status;
2329 }
2330
2331 /* Start Timer B (or called timer F for non-INVITE) for transaction
2332 * timeout.
2333 */
2334 lock_timer(tsx);
2335 tsx_schedule_timer( tsx, &tsx->timeout_timer, &timeout_timer_val,
2336 TIMEOUT_TIMER);
2337 unlock_timer(tsx);
2338
2339 /* Start Timer A (or timer E) for retransmission only if unreliable
2340 * transport is being used.
2341 */
2342 if (!tsx->is_reliable) {
2343 tsx->retransmit_count = 0;
2344 if (tsx->transport_flag & TSX_HAS_PENDING_TRANSPORT) {
2345 tsx->transport_flag |= TSX_HAS_PENDING_RESCHED;
2346 } else {
2347 tsx_schedule_timer(tsx, &tsx->retransmit_timer,
2348 &t1_timer_val, RETRANSMIT_TIMER);
2349 }
2350 }
2351
2352 /* Move state. */
2353 tsx_set_state( tsx, PJSIP_TSX_STATE_CALLING,
2354 PJSIP_EVENT_TX_MSG, tdata);
2355 }
2356
2357 return PJ_SUCCESS;
2358}
2359
2360
2361/*
2362 * State Calling is for UAC after it sends request but before any responses
2363 * is received.
2364 */
2365static pj_status_t tsx_on_state_calling( pjsip_transaction *tsx,
2366 pjsip_event *event )
2367{
2368 pj_assert(tsx->state == PJSIP_TSX_STATE_CALLING);
2369 pj_assert(tsx->role == PJSIP_ROLE_UAC);
2370
2371 if (event->type == PJSIP_EVENT_TIMER &&
2372 event->body.timer.entry == &tsx->retransmit_timer)
2373 {
2374 pj_status_t status;
2375
2376 /* Retransmit the request. */
2377 status = tsx_retransmit( tsx, 1 );
2378 if (status != PJ_SUCCESS) {
2379 return status;
2380 }
2381
2382 } else if (event->type == PJSIP_EVENT_TIMER &&
2383 event->body.timer.entry == &tsx->timeout_timer)
2384 {
2385 /* Cancel retransmission timer. */
2386 tsx_cancel_timer(tsx, &tsx->retransmit_timer);
2387
2388 tsx->transport_flag &= ~(TSX_HAS_PENDING_RESCHED);
2389
2390 /* Set status code */
2391 tsx_set_status_code(tsx, PJSIP_SC_TSX_TIMEOUT, NULL);
2392
2393 /* Inform TU. */
2394 tsx_set_state( tsx, PJSIP_TSX_STATE_TERMINATED,
2395 PJSIP_EVENT_TIMER, &tsx->timeout_timer);
2396
2397 /* Transaction is destroyed */
2398 //return PJSIP_ETSXDESTROYED;
2399
2400 } else if (event->type == PJSIP_EVENT_RX_MSG) {
2401 pjsip_msg *msg;
2402 int code;
2403
2404 /* Get message instance */
2405 msg = event->body.rx_msg.rdata->msg_info.msg;
2406
2407 /* Better be a response message. */
2408 if (msg->type != PJSIP_RESPONSE_MSG)
2409 return PJSIP_ENOTRESPONSEMSG;
2410
2411 code = msg->line.status.code;
2412
2413 /* If the response is final, cancel both retransmission and timeout
2414 * timer.
2415 */
2416 if (code >= 200) {
2417 tsx_cancel_timer(tsx, &tsx->retransmit_timer);
2418
2419 if (tsx->timeout_timer.id != 0) {
2420 lock_timer(tsx);
2421 tsx_cancel_timer(tsx, &tsx->timeout_timer);
2422 unlock_timer(tsx);
2423 }
2424
2425 } else {
2426 /* Cancel retransmit timer (for non-INVITE transaction, the
2427 * retransmit timer will be rescheduled at T2.
2428 */
2429 tsx_cancel_timer(tsx, &tsx->retransmit_timer);
2430
2431 /* For provisional response, only cancel retransmit when this
2432 * is an INVITE transaction. For non-INVITE, section 17.1.2.1
2433 * of RFC 3261 says that:
2434 * - retransmit timer is set to T2
2435 * - timeout timer F is not deleted.
2436 */
2437 if (tsx->method.id == PJSIP_INVITE_METHOD) {
2438
2439 /* Cancel timeout timer */
2440 lock_timer(tsx);
2441 tsx_cancel_timer(tsx, &tsx->timeout_timer);
2442 unlock_timer(tsx);
2443
2444 } else {
2445 if (!tsx->is_reliable) {
2446 tsx_schedule_timer(tsx, &tsx->retransmit_timer,
2447 &t2_timer_val, RETRANSMIT_TIMER);
2448 }
2449 }
2450 }
2451
2452 tsx->transport_flag &= ~(TSX_HAS_PENDING_RESCHED);
2453
2454
2455 /* Discard retransmission message if it is not INVITE.
2456 * The INVITE tdata is needed in case we have to generate ACK for
2457 * the final response.
2458 */
2459 /* Keep last_tx for authorization. */
2460 //blp: always keep last_tx until transaction is destroyed
2461 //code = msg->line.status.code;
2462 //if (tsx->method.id != PJSIP_INVITE_METHOD && code!=401 && code!=407) {
2463 // pjsip_tx_data_dec_ref(tsx->last_tx);
2464 // tsx->last_tx = NULL;
2465 //}
2466
2467 /* Processing is similar to state Proceeding. */
2468 tsx_on_state_proceeding_uac( tsx, event);
2469
2470 } else {
2471 pj_assert(!"Unexpected event");
2472 return PJ_EBUG;
2473 }
2474
2475 return PJ_SUCCESS;
2476}
2477
2478
2479/*
2480 * State Trying is for UAS after it received request but before any responses
2481 * is sent.
2482 * Note: this is different than RFC3261, which can use Trying state for
2483 * non-INVITE client transaction (bug in RFC?).
2484 */
2485static pj_status_t tsx_on_state_trying( pjsip_transaction *tsx,
2486 pjsip_event *event)
2487{
2488 pj_status_t status;
2489
2490 pj_assert(tsx->state == PJSIP_TSX_STATE_TRYING);
2491
2492 /* This state is only for UAS */
2493 pj_assert(tsx->role == PJSIP_ROLE_UAS);
2494
2495 /* Better be transmission of response message.
2496 * If we've got request retransmission, this means that the TU hasn't
2497 * transmitted any responses within 500 ms, which is not allowed. If
2498 * this happens, just ignore the event (we couldn't retransmit last
2499 * response because we haven't sent any!).
2500 */
2501 if (event->type != PJSIP_EVENT_TX_MSG) {
2502 return PJ_SUCCESS;
2503 }
2504
2505 /* The rest of the processing of the event is exactly the same as in
2506 * "Proceeding" state.
2507 */
2508 status = tsx_on_state_proceeding_uas( tsx, event);
2509
2510 /* Inform the TU of the state transision if state is still State_Trying */
2511 if (status==PJ_SUCCESS && tsx->state == PJSIP_TSX_STATE_TRYING) {
2512
2513 tsx_set_state( tsx, PJSIP_TSX_STATE_PROCEEDING,
2514 PJSIP_EVENT_TX_MSG, event->body.tx_msg.tdata);
2515
2516 }
2517
2518 return status;
2519}
2520
2521
2522/*
2523 * Handler for events in Proceeding for UAS
2524 * This state happens after the TU sends provisional response.
2525 */
2526static pj_status_t tsx_on_state_proceeding_uas( pjsip_transaction *tsx,
2527 pjsip_event *event)
2528{
2529 pj_assert(tsx->state == PJSIP_TSX_STATE_PROCEEDING ||
2530 tsx->state == PJSIP_TSX_STATE_TRYING);
2531
2532 /* This state is only for UAS. */
2533 pj_assert(tsx->role == PJSIP_ROLE_UAS);
2534
2535 /* Receive request retransmission. */
2536 if (event->type == PJSIP_EVENT_RX_MSG) {
2537
2538 pj_status_t status;
2539
2540 /* Must have last response sent. */
2541 PJ_ASSERT_RETURN(tsx->last_tx != NULL, PJ_EBUG);
2542
2543 /* Send last response */
2544 if (tsx->transport_flag & TSX_HAS_PENDING_TRANSPORT) {
2545 tsx->transport_flag |= TSX_HAS_PENDING_SEND;
2546 } else {
2547 status = tsx_send_msg(tsx, tsx->last_tx);
2548 if (status != PJ_SUCCESS)
2549 return status;
2550 }
2551
2552 } else if (event->type == PJSIP_EVENT_TX_MSG ) {
2553 pjsip_tx_data *tdata = event->body.tx_msg.tdata;
2554 pj_status_t status;
2555
2556 /* The TU sends response message to the request. Save this message so
2557 * that we can retransmit the last response in case we receive request
2558 * retransmission.
2559 */
2560 pjsip_msg *msg = tdata->msg;
2561
2562 /* This can only be a response message. */
2563 PJ_ASSERT_RETURN(msg->type==PJSIP_RESPONSE_MSG, PJSIP_ENOTRESPONSEMSG);
2564
2565 /* Update last status */
2566 tsx_set_status_code(tsx, msg->line.status.code,
2567 &msg->line.status.reason);
2568
2569 /* Discard the saved last response (it will be updated later as
2570 * necessary).
2571 */
2572 if (tsx->last_tx && tsx->last_tx != tdata) {
2573 pjsip_tx_data_dec_ref( tsx->last_tx );
2574 tsx->last_tx = NULL;
2575 }
2576
2577 /* Send the message. */
2578 status = tsx_send_msg(tsx, tdata);
2579 if (status != PJ_SUCCESS) {
2580 return status;
2581 }
2582
2583 // Update To tag header for RFC2543 transaction.
2584 // TODO:
2585
2586 /* Update transaction state */
2587 if (PJSIP_IS_STATUS_IN_CLASS(tsx->status_code, 100)) {
2588
2589 if (tsx->last_tx != tdata) {
2590 tsx->last_tx = tdata;
2591 pjsip_tx_data_add_ref( tdata );
2592 }
2593
2594 tsx_set_state( tsx, PJSIP_TSX_STATE_PROCEEDING,
2595 PJSIP_EVENT_TX_MSG, tdata );
2596
2597 /* Retransmit provisional response every 1 minute if this is
2598 * an INVITE provisional response greater than 100.
2599 */
2600 if (PJSIP_TSX_1XX_RETRANS_DELAY > 0 &&
2601 tsx->method.id==PJSIP_INVITE_METHOD && tsx->status_code>100)
2602 {
2603
2604 /* Stop 1xx retransmission timer, if any */
2605 tsx_cancel_timer(tsx, &tsx->retransmit_timer);
2606
2607 /* Schedule retransmission */
2608 tsx->retransmit_count = 0;
2609 if (tsx->transport_flag & TSX_HAS_PENDING_TRANSPORT) {
2610 tsx->transport_flag |= TSX_HAS_PENDING_RESCHED;
2611 } else {
2612 pj_time_val delay = {PJSIP_TSX_1XX_RETRANS_DELAY, 0};
2613 tsx_schedule_timer( tsx, &tsx->retransmit_timer, &delay,
2614 RETRANSMIT_TIMER);
2615 }
2616 }
2617
2618 } else if (PJSIP_IS_STATUS_IN_CLASS(tsx->status_code, 200)) {
2619
2620 /* Stop 1xx retransmission timer, if any */
2621 tsx_cancel_timer(tsx, &tsx->retransmit_timer);
2622
2623 if (tsx->method.id == PJSIP_INVITE_METHOD && tsx->handle_200resp==0) {
2624
2625 /* 2xx class message is not saved, because retransmission
2626 * is handled by TU.
2627 */
2628 tsx_set_state( tsx, PJSIP_TSX_STATE_TERMINATED,
2629 PJSIP_EVENT_TX_MSG, tdata );
2630
2631 /* Transaction is destroyed. */
2632 //return PJSIP_ETSXDESTROYED;
2633
2634 } else {
2635 pj_time_val timeout;
2636
2637 if (tsx->method.id == PJSIP_INVITE_METHOD) {
2638 tsx->retransmit_count = 0;
2639 if (tsx->transport_flag & TSX_HAS_PENDING_TRANSPORT) {
2640 tsx->transport_flag |= TSX_HAS_PENDING_RESCHED;
2641 } else {
2642 tsx_schedule_timer( tsx, &tsx->retransmit_timer,
2643 &t1_timer_val, RETRANSMIT_TIMER);
2644 }
2645 }
2646
2647 /* Save last response sent for retransmission when request
2648 * retransmission is received.
2649 */
2650 if (tsx->last_tx != tdata) {
2651 tsx->last_tx = tdata;
2652 pjsip_tx_data_add_ref(tdata);
2653 }
2654
2655 /* Setup timeout timer: */
2656
2657 if (tsx->method.id == PJSIP_INVITE_METHOD) {
2658
2659 /* Start Timer H at 64*T1 for INVITE server transaction,
2660 * regardless of transport.
2661 */
2662 timeout = timeout_timer_val;
2663
2664 } else if (!tsx->is_reliable) {
2665
2666 /* For non-INVITE, start timer J at 64*T1 for unreliable
2667 * transport.
2668 */
2669 timeout = timeout_timer_val;
2670
2671 } else {
2672
2673 /* Transaction terminates immediately for non-INVITE when
2674 * reliable transport is used.
2675 */
2676 timeout.sec = timeout.msec = 0;
2677 }
2678
2679 lock_timer(tsx);
2680 tsx_schedule_timer( tsx, &tsx->timeout_timer,
2681 &timeout, TIMEOUT_TIMER);
2682 unlock_timer(tsx);
2683
2684 /* Set state to "Completed" */
2685 tsx_set_state( tsx, PJSIP_TSX_STATE_COMPLETED,
2686 PJSIP_EVENT_TX_MSG, tdata );
2687 }
2688
2689 } else if (tsx->status_code >= 300) {
2690
2691 /* Stop 1xx retransmission timer, if any */
2692 tsx_cancel_timer(tsx, &tsx->retransmit_timer);
2693
2694 /* 3xx-6xx class message causes transaction to move to
2695 * "Completed" state.
2696 */
2697 if (tsx->last_tx != tdata) {
2698 tsx->last_tx = tdata;
2699 pjsip_tx_data_add_ref( tdata );
2700 }
2701
2702 /* For INVITE, start timer H for transaction termination
2703 * regardless whether transport is reliable or not.
2704 * For non-INVITE, start timer J with the value of 64*T1 for
2705 * non-reliable transports, and zero for reliable transports.
2706 */
2707 lock_timer(tsx);
2708 if (tsx->method.id == PJSIP_INVITE_METHOD) {
2709 /* Start timer H for INVITE */
2710 tsx_schedule_timer(tsx, &tsx->timeout_timer,
2711 &timeout_timer_val, TIMEOUT_TIMER);
2712 } else if (!tsx->is_reliable) {
2713 /* Start timer J on 64*T1 seconds for non-INVITE */
2714 tsx_schedule_timer(tsx, &tsx->timeout_timer,
2715 &timeout_timer_val, TIMEOUT_TIMER);
2716 } else {
2717 /* Start timer J on zero seconds for non-INVITE */
2718 pj_time_val zero_time = { 0, 0 };
2719 tsx_schedule_timer(tsx, &tsx->timeout_timer,
2720 &zero_time, TIMEOUT_TIMER);
2721 }
2722 unlock_timer(tsx);
2723
2724 /* For INVITE, if unreliable transport is used, retransmission
2725 * timer G will be scheduled (retransmission).
2726 */
2727 if (!tsx->is_reliable) {
2728 pjsip_cseq_hdr *cseq = (pjsip_cseq_hdr*)
2729 pjsip_msg_find_hdr( msg, PJSIP_H_CSEQ,
2730 NULL);
2731 if (cseq->method.id == PJSIP_INVITE_METHOD) {
2732 tsx->retransmit_count = 0;
2733 if (tsx->transport_flag & TSX_HAS_PENDING_TRANSPORT) {
2734 tsx->transport_flag |= TSX_HAS_PENDING_RESCHED;
2735 } else {
2736 tsx_schedule_timer(tsx, &tsx->retransmit_timer,
2737 &t1_timer_val, RETRANSMIT_TIMER);
2738 }
2739 }
2740 }
2741
2742 /* Inform TU */
2743 tsx_set_state( tsx, PJSIP_TSX_STATE_COMPLETED,
2744 PJSIP_EVENT_TX_MSG, tdata );
2745
2746 } else {
2747 pj_assert(0);
2748 }
2749
2750
2751 } else if (event->type == PJSIP_EVENT_TIMER &&
2752 event->body.timer.entry == &tsx->retransmit_timer) {
2753
2754 /* Retransmission timer elapsed. */
2755 pj_status_t status;
2756
2757 /* Must not be triggered while transport is pending. */
2758 pj_assert((tsx->transport_flag & TSX_HAS_PENDING_TRANSPORT) == 0);
2759
2760 /* Must have last response to retransmit. */
2761 pj_assert(tsx->last_tx != NULL);
2762
2763 /* Retransmit the last response. */
2764 status = tsx_retransmit( tsx, 1 );
2765 if (status != PJ_SUCCESS) {
2766 return status;
2767 }
2768
2769 } else if (event->type == PJSIP_EVENT_TIMER &&
2770 event->body.timer.entry == &tsx->timeout_timer) {
2771
2772 /* Timeout timer. should not happen? */
2773 pj_assert(!"Should not happen(?)");
2774
2775 tsx_set_status_code(tsx, PJSIP_SC_TSX_TIMEOUT, NULL);
2776
2777 tsx_set_state( tsx, PJSIP_TSX_STATE_TERMINATED,
2778 PJSIP_EVENT_TIMER, &tsx->timeout_timer);
2779
2780 return PJ_EBUG;
2781
2782 } else {
2783 pj_assert(!"Unexpected event");
2784 return PJ_EBUG;
2785 }
2786
2787 return PJ_SUCCESS;
2788}
2789
2790
2791/*
2792 * Handler for events in Proceeding for UAC
2793 * This state happens after provisional response(s) has been received from
2794 * UAS.
2795 */
2796static pj_status_t tsx_on_state_proceeding_uac(pjsip_transaction *tsx,
2797 pjsip_event *event)
2798{
2799
2800 pj_assert(tsx->state == PJSIP_TSX_STATE_PROCEEDING ||
2801 tsx->state == PJSIP_TSX_STATE_CALLING);
2802
2803 if (event->type != PJSIP_EVENT_TIMER) {
2804 pjsip_msg *msg;
2805
2806 /* Must be incoming response, because we should not retransmit
2807 * request once response has been received.
2808 */
2809 pj_assert(event->type == PJSIP_EVENT_RX_MSG);
2810 if (event->type != PJSIP_EVENT_RX_MSG) {
2811 return PJ_EINVALIDOP;
2812 }
2813
2814 msg = event->body.rx_msg.rdata->msg_info.msg;
2815
2816 /* Must be a response message. */
2817 if (msg->type != PJSIP_RESPONSE_MSG) {
2818 pj_assert(!"Expecting response message!");
2819 return PJSIP_ENOTRESPONSEMSG;
2820 }
2821
2822 tsx_set_status_code(tsx, msg->line.status.code,
2823 &msg->line.status.reason);
2824
2825 } else {
2826 if (event->body.timer.entry == &tsx->retransmit_timer) {
2827 /* Retransmit message. */
2828 pj_status_t status;
2829
2830 status = tsx_retransmit( tsx, 1 );
2831
2832 return status;
2833
2834 } else {
2835 tsx_set_status_code(tsx, PJSIP_SC_TSX_TIMEOUT, NULL);
2836 }
2837 }
2838
2839 if (PJSIP_IS_STATUS_IN_CLASS(tsx->status_code, 100)) {
2840
2841 /* Inform the message to TU. */
2842 tsx_set_state( tsx, PJSIP_TSX_STATE_PROCEEDING,
2843 PJSIP_EVENT_RX_MSG, event->body.rx_msg.rdata );
2844
2845 } else if (PJSIP_IS_STATUS_IN_CLASS(tsx->status_code,200)) {
2846
2847 /* Stop timeout timer B/F. */
2848 lock_timer(tsx);
2849 tsx_cancel_timer( tsx, &tsx->timeout_timer );
2850 unlock_timer(tsx);
2851
2852 /* For INVITE, the state moves to Terminated state (because ACK is
2853 * handled in TU). For non-INVITE, state moves to Completed.
2854 */
2855 if (tsx->method.id == PJSIP_INVITE_METHOD) {
2856 tsx_set_state( tsx, PJSIP_TSX_STATE_TERMINATED,
2857 PJSIP_EVENT_RX_MSG, event->body.rx_msg.rdata );
2858 //return PJSIP_ETSXDESTROYED;
2859
2860 } else {
2861 pj_time_val timeout;
2862
2863 /* For unreliable transport, start timer D (for INVITE) or
2864 * timer K for non-INVITE. */
2865 if (!tsx->is_reliable) {
2866 if (tsx->method.id == PJSIP_INVITE_METHOD) {
2867 timeout = td_timer_val;
2868 } else {
2869 timeout = t4_timer_val;
2870 }
2871 } else {
2872 timeout.sec = timeout.msec = 0;
2873 }
2874 lock_timer(tsx);
2875 tsx_schedule_timer( tsx, &tsx->timeout_timer,
2876 &timeout, TIMEOUT_TIMER);
2877 unlock_timer(tsx);
2878
2879 /* Cancel retransmission timer */
2880 tsx_cancel_timer(tsx, &tsx->retransmit_timer);
2881
2882 /* Move state to Completed, inform TU. */
2883 tsx_set_state( tsx, PJSIP_TSX_STATE_COMPLETED,
2884 PJSIP_EVENT_RX_MSG, event->body.rx_msg.rdata );
2885 }
2886
2887 } else if (event->type == PJSIP_EVENT_TIMER &&
2888 event->body.timer.entry == &tsx->timeout_timer) {
2889
2890 /* Inform TU. */
2891 tsx_set_state( tsx, PJSIP_TSX_STATE_TERMINATED,
2892 PJSIP_EVENT_TIMER, &tsx->timeout_timer);
2893
2894
2895 } else if (tsx->status_code >= 300 && tsx->status_code <= 699) {
2896
2897
2898#if 0
2899 /* XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX */
2900 /*
2901 * This is the old code; it's broken for authentication.
2902 */
2903 pj_time_val timeout;
2904 pj_status_t status;
2905
2906 /* Stop timer B. */
2907 tsx_cancel_timer( tsx, &tsx->timeout_timer );
2908
2909 /* Generate and send ACK for INVITE. */
2910 if (tsx->method.id == PJSIP_INVITE_METHOD) {
2911 pjsip_tx_data *ack;
2912
2913 status = pjsip_endpt_create_ack( tsx->endpt, tsx->last_tx,
2914 event->body.rx_msg.rdata,
2915 &ack);
2916 if (status != PJ_SUCCESS)
2917 return status;
2918
2919 if (ack != tsx->last_tx) {
2920 pjsip_tx_data_dec_ref(tsx->last_tx);
2921 tsx->last_tx = ack;
2922 }
2923
2924 status = tsx_send_msg( tsx, tsx->last_tx);
2925 if (status != PJ_SUCCESS) {
2926 return status;
2927 }
2928 }
2929
2930 /* Start Timer D with TD/T4 timer if unreliable transport is used. */
2931 if (!tsx->is_reliable) {
2932 if (tsx->method.id == PJSIP_INVITE_METHOD) {
2933 timeout = td_timer_val;
2934 } else {
2935 timeout = t4_timer_val;
2936 }
2937 } else {
2938 timeout.sec = timeout.msec = 0;
2939 }
2940 tsx_schedule_timer( tsx, &tsx->timeout_timer, &timeout, TIMEOUT_TIMER);
2941
2942 /* Inform TU.
2943 * blp: You might be tempted to move this notification before
2944 * sending ACK, but I think you shouldn't. Better set-up
2945 * everything before calling tsx_user's callback to avoid
2946 * mess up.
2947 */
2948 tsx_set_state( tsx, PJSIP_TSX_STATE_COMPLETED,
2949 PJSIP_EVENT_RX_MSG, event->body.rx_msg.rdata );
2950
2951 /* XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX */
2952#endif
2953
2954 /* New code, taken from 0.2.9.x branch */
2955 pj_time_val timeout;
2956 pjsip_tx_data *ack_tdata = NULL;
2957
2958 /* Cancel retransmission timer */
2959 tsx_cancel_timer(tsx, &tsx->retransmit_timer);
2960
2961 /* Stop timer B. */
2962 lock_timer(tsx);
2963 tsx_cancel_timer( tsx, &tsx->timeout_timer );
2964 unlock_timer(tsx);
2965
2966 /* Generate and send ACK (for INVITE) */
2967 if (tsx->method.id == PJSIP_INVITE_METHOD) {
2968 pj_status_t status;
2969
2970 status = pjsip_endpt_create_ack( tsx->endpt, tsx->last_tx,
2971 event->body.rx_msg.rdata,
2972 &ack_tdata);
2973 if (status != PJ_SUCCESS)
2974 return status;
2975
2976 status = tsx_send_msg( tsx, ack_tdata);
2977 if (status != PJ_SUCCESS)
2978 return status;
2979 }
2980
2981 /* Inform TU. */
2982 tsx_set_state( tsx, PJSIP_TSX_STATE_COMPLETED,
2983 PJSIP_EVENT_RX_MSG, event->body.rx_msg.rdata);
2984
2985 /* Generate and send ACK for INVITE. */
2986 if (tsx->method.id == PJSIP_INVITE_METHOD) {
2987 if (ack_tdata != tsx->last_tx) {
2988 pjsip_tx_data_dec_ref(tsx->last_tx);
2989 tsx->last_tx = ack_tdata;
2990
2991 /* This is a bug.
2992 tsx_send_msg() does NOT decrement tdata's reference counter,
2993 so if we add the reference counter here, tdata will have
2994 reference counter 2, causing it to leak.
2995 pjsip_tx_data_add_ref(ack_tdata);
2996 */
2997 }
2998 }
2999
3000 /* Start Timer D with TD/T4 timer if unreliable transport is used. */
3001 /* Note: tsx->transport may be NULL! */
3002 if (!tsx->is_reliable) {
3003 if (tsx->method.id == PJSIP_INVITE_METHOD) {
3004 timeout = td_timer_val;
3005 } else {
3006 timeout = t4_timer_val;
3007 }
3008 } else {
3009 timeout.sec = timeout.msec = 0;
3010 }
3011 lock_timer(tsx);
3012 /* In the short period above timer may have been inserted
3013 * by set_timeout() (by CANCEL). Cancel it if necessary. See:
3014 * https://trac.pjsip.org/repos/ticket/1374
3015 */
3016 tsx_cancel_timer( tsx, &tsx->timeout_timer );
3017 tsx_schedule_timer( tsx, &tsx->timeout_timer, &timeout,
3018 TIMEOUT_TIMER);
3019 unlock_timer(tsx);
3020
3021 } else {
3022 // Shouldn't happen because there's no timer for this state.
3023 pj_assert(!"Unexpected event");
3024 return PJ_EBUG;
3025 }
3026
3027 return PJ_SUCCESS;
3028}
3029
3030
3031/*
3032 * Handler for events in Completed state for UAS
3033 */
3034static pj_status_t tsx_on_state_completed_uas( pjsip_transaction *tsx,
3035 pjsip_event *event)
3036{
3037 pj_assert(tsx->state == PJSIP_TSX_STATE_COMPLETED);
3038
3039 if (event->type == PJSIP_EVENT_RX_MSG) {
3040 pjsip_msg *msg = event->body.rx_msg.rdata->msg_info.msg;
3041
3042 /* This must be a request message retransmission. */
3043 if (msg->type != PJSIP_REQUEST_MSG)
3044 return PJSIP_ENOTREQUESTMSG;
3045
3046 /* On receive request retransmission, retransmit last response. */
3047 if (msg->line.req.method.id != PJSIP_ACK_METHOD) {
3048 pj_status_t status;
3049
3050 status = tsx_retransmit( tsx, 0 );
3051 if (status != PJ_SUCCESS) {
3052 return status;
3053 }
3054
3055 } else {
3056 pj_time_val timeout;
3057
3058 /* Process incoming ACK request. */
3059
3060 /* Verify that this is an INVITE transaction */
3061 if (tsx->method.id != PJSIP_INVITE_METHOD) {
3062 PJ_LOG(2, (tsx->obj_name,
3063 "Received illegal ACK for %.*s transaction",
3064 (int)tsx->method.name.slen,
3065 tsx->method.name.ptr));
3066 return PJSIP_EINVALIDMETHOD;
3067 }
3068
3069 /* Cease retransmission. */
3070 tsx_cancel_timer(tsx, &tsx->retransmit_timer);
3071
3072 tsx->transport_flag &= ~(TSX_HAS_PENDING_RESCHED);
3073
3074 /* Reschedule timeout timer. */
3075 lock_timer(tsx);
3076 tsx_cancel_timer( tsx, &tsx->timeout_timer );
3077
3078 /* Timer I is T4 timer for unreliable transports, and
3079 * zero seconds for reliable transports.
3080 */
3081 if (!tsx->is_reliable) {
3082 timeout.sec = 0;
3083 timeout.msec = 0;
3084 } else {
3085 timeout.sec = t4_timer_val.sec;
3086 timeout.msec = t4_timer_val.msec;
3087 }
3088 tsx_schedule_timer( tsx, &tsx->timeout_timer,
3089 &timeout, TIMEOUT_TIMER);
3090 unlock_timer(tsx);
3091
3092 /* Move state to "Confirmed" */
3093 tsx_set_state( tsx, PJSIP_TSX_STATE_CONFIRMED,
3094 PJSIP_EVENT_RX_MSG, event->body.rx_msg.rdata );
3095 }
3096
3097 } else if (event->type == PJSIP_EVENT_TIMER) {
3098
3099 if (event->body.timer.entry == &tsx->retransmit_timer) {
3100 /* Retransmit message. */
3101 pj_status_t status;
3102
3103 status = tsx_retransmit( tsx, 1 );
3104 if (status != PJ_SUCCESS) {
3105 return status;
3106 }
3107
3108 } else {
3109 if (tsx->method.id == PJSIP_INVITE_METHOD) {
3110
3111 /* For INVITE, this means that ACK was never received.
3112 * Set state to Terminated, and inform TU.
3113 */
3114
3115 tsx_set_status_code(tsx, PJSIP_SC_TSX_TIMEOUT, NULL);
3116
3117 tsx_set_state( tsx, PJSIP_TSX_STATE_TERMINATED,
3118 PJSIP_EVENT_TIMER, &tsx->timeout_timer );
3119
3120 //return PJSIP_ETSXDESTROYED;
3121
3122 } else {
3123 /* Transaction terminated, it can now be deleted. */
3124 tsx_set_state( tsx, PJSIP_TSX_STATE_TERMINATED,
3125 PJSIP_EVENT_TIMER, &tsx->timeout_timer );
3126 //return PJSIP_ETSXDESTROYED;
3127 }
3128 }
3129
3130 } else {
3131 /* Ignore request to transmit. */
3132 PJ_ASSERT_RETURN(event->type == PJSIP_EVENT_TX_MSG &&
3133 event->body.tx_msg.tdata == tsx->last_tx,
3134 PJ_EINVALIDOP);
3135 }
3136
3137 return PJ_SUCCESS;
3138}
3139
3140
3141/*
3142 * Handler for events in Completed state for UAC transaction.
3143 */
3144static pj_status_t tsx_on_state_completed_uac( pjsip_transaction *tsx,
3145 pjsip_event *event)
3146{
3147 pj_assert(tsx->state == PJSIP_TSX_STATE_COMPLETED);
3148
3149 if (event->type == PJSIP_EVENT_TIMER) {
3150 /* Must be the timeout timer. */
3151 pj_assert(event->body.timer.entry == &tsx->timeout_timer);
3152
3153 /* Move to Terminated state. */
3154 tsx_set_state( tsx, PJSIP_TSX_STATE_TERMINATED,
3155 PJSIP_EVENT_TIMER, event->body.timer.entry );
3156
3157 /* Transaction has been destroyed. */
3158 //return PJSIP_ETSXDESTROYED;
3159
3160 } else if (event->type == PJSIP_EVENT_RX_MSG) {
3161 if (tsx->method.id == PJSIP_INVITE_METHOD) {
3162 /* On received of final response retransmission, retransmit the ACK.
3163 * TU doesn't need to be informed.
3164 */
3165 pjsip_msg *msg = event->body.rx_msg.rdata->msg_info.msg;
3166 pj_assert(msg->type == PJSIP_RESPONSE_MSG);
3167 if (msg->type==PJSIP_RESPONSE_MSG &&
3168 msg->line.status.code >= 200)
3169 {
3170 pj_status_t status;
3171
3172 status = tsx_retransmit( tsx, 0 );
3173 if (status != PJ_SUCCESS) {
3174 return status;
3175 }
3176 } else {
3177 /* Very late retransmission of privisional response. */
3178 pj_assert( msg->type == PJSIP_RESPONSE_MSG );
3179 }
3180 } else {
3181 /* Just drop the response. */
3182 }
3183
3184 } else {
3185 pj_assert(!"Unexpected event");
3186 return PJ_EINVALIDOP;
3187 }
3188
3189 return PJ_SUCCESS;
3190}
3191
3192
3193/*
3194 * Handler for events in state Confirmed.
3195 */
3196static pj_status_t tsx_on_state_confirmed( pjsip_transaction *tsx,
3197 pjsip_event *event)
3198{
3199 pj_assert(tsx->state == PJSIP_TSX_STATE_CONFIRMED);
3200
3201 /* This state is only for UAS for INVITE. */
3202 pj_assert(tsx->role == PJSIP_ROLE_UAS);
3203 pj_assert(tsx->method.id == PJSIP_INVITE_METHOD);
3204
3205 /* Absorb any ACK received. */
3206 if (event->type == PJSIP_EVENT_RX_MSG) {
3207
3208 pjsip_msg *msg = event->body.rx_msg.rdata->msg_info.msg;
3209
3210 /* Only expecting request message. */
3211 if (msg->type != PJSIP_REQUEST_MSG)
3212 return PJSIP_ENOTREQUESTMSG;
3213
3214 /* Must be an ACK request or a late INVITE retransmission. */
3215 pj_assert(msg->line.req.method.id == PJSIP_ACK_METHOD ||
3216 msg->line.req.method.id == PJSIP_INVITE_METHOD);
3217
3218 } else if (event->type == PJSIP_EVENT_TIMER) {
3219 /* Must be from timeout_timer_. */
3220 pj_assert(event->body.timer.entry == &tsx->timeout_timer);
3221
3222 /* Move to Terminated state. */
3223 tsx_set_state( tsx, PJSIP_TSX_STATE_TERMINATED,
3224 PJSIP_EVENT_TIMER, &tsx->timeout_timer );
3225
3226 /* Transaction has been destroyed. */
3227 //return PJSIP_ETSXDESTROYED;
3228
3229 } else {
3230 pj_assert(!"Unexpected event");
3231 return PJ_EBUG;
3232 }
3233
3234 return PJ_SUCCESS;
3235}
3236
3237
3238/*
3239 * Handler for events in state Terminated.
3240 */
3241static pj_status_t tsx_on_state_terminated( pjsip_transaction *tsx,
3242 pjsip_event *event)
3243{
3244 pj_assert(tsx->state == PJSIP_TSX_STATE_TERMINATED);
3245
3246 /* Ignore events other than timer. This used to be an assertion but
3247 * events may genuinely arrive at this state.
3248 */
3249 if (event->type != PJSIP_EVENT_TIMER) {
3250 return PJ_EIGNORED;
3251 }
3252
3253 /* Destroy this transaction */
3254 tsx_set_state(tsx, PJSIP_TSX_STATE_DESTROYED,
3255 event->type, event->body.user.user1 );
3256
3257 return PJ_SUCCESS;
3258}
3259
3260
3261/*
3262 * Handler for events in state Destroyed.
3263 * Shouldn't happen!
3264 */
3265static pj_status_t tsx_on_state_destroyed(pjsip_transaction *tsx,
3266 pjsip_event *event)
3267{
3268 PJ_UNUSED_ARG(tsx);
3269 PJ_UNUSED_ARG(event);
3270
3271 // See https://trac.pjsip.org/repos/ticket/1432
3272 //pj_assert(!"Not expecting any events!!");
3273
3274 return PJ_EIGNORED;
3275}
3276