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