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