Tristan Matthews | 0a329cc | 2013-07-17 13:20:14 -0400 | [diff] [blame] | 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_ua_layer.h> |
| 21 | #include <pjsip/sip_module.h> |
| 22 | #include <pjsip/sip_dialog.h> |
| 23 | #include <pjsip/sip_endpoint.h> |
| 24 | #include <pjsip/sip_errno.h> |
| 25 | #include <pjsip/sip_transaction.h> |
| 26 | #include <pj/os.h> |
| 27 | #include <pj/hash.h> |
| 28 | #include <pj/assert.h> |
| 29 | #include <pj/string.h> |
| 30 | #include <pj/pool.h> |
| 31 | #include <pj/log.h> |
| 32 | |
| 33 | |
| 34 | #define THIS_FILE "sip_ua_layer.c" |
| 35 | |
| 36 | /* |
| 37 | * Static prototypes. |
| 38 | */ |
| 39 | static pj_status_t mod_ua_load(pjsip_endpoint *endpt); |
| 40 | static pj_status_t mod_ua_unload(void); |
| 41 | static pj_bool_t mod_ua_on_rx_request(pjsip_rx_data *rdata); |
| 42 | static pj_bool_t mod_ua_on_rx_response(pjsip_rx_data *rdata); |
| 43 | static void mod_ua_on_tsx_state(pjsip_transaction*, pjsip_event*); |
| 44 | |
| 45 | |
| 46 | extern long pjsip_dlg_lock_tls_id; /* defined in sip_dialog.c */ |
| 47 | |
| 48 | /* This struct is used to represent list of dialog inside a dialog set. |
| 49 | * We don't want to use pjsip_dialog for this purpose, to save some |
| 50 | * memory (about 100 bytes per dialog set). |
| 51 | */ |
| 52 | struct dlg_set_head |
| 53 | { |
| 54 | PJ_DECL_LIST_MEMBER(pjsip_dialog); |
| 55 | }; |
| 56 | |
| 57 | /* This struct represents a dialog set. |
| 58 | * This is the value that will be put in the UA's hash table. |
| 59 | */ |
| 60 | struct dlg_set |
| 61 | { |
| 62 | /* To put this node in free dlg_set nodes in UA. */ |
| 63 | PJ_DECL_LIST_MEMBER(struct dlg_set); |
| 64 | |
| 65 | /* This is the buffer to store this entry in the hash table. */ |
| 66 | pj_hash_entry_buf ht_entry; |
| 67 | |
| 68 | /* List of dialog in this dialog set. */ |
| 69 | struct dlg_set_head dlg_list; |
| 70 | }; |
| 71 | |
| 72 | |
| 73 | /* |
| 74 | * Module interface. |
| 75 | */ |
| 76 | static struct user_agent |
| 77 | { |
| 78 | pjsip_module mod; |
| 79 | pj_pool_t *pool; |
| 80 | pjsip_endpoint *endpt; |
| 81 | pj_mutex_t *mutex; |
| 82 | pj_hash_table_t *dlg_table; |
| 83 | pjsip_ua_init_param param; |
| 84 | struct dlg_set free_dlgset_nodes; |
| 85 | |
| 86 | } mod_ua = |
| 87 | { |
| 88 | { |
| 89 | NULL, NULL, /* prev, next. */ |
| 90 | { "mod-ua", 6 }, /* Name. */ |
| 91 | -1, /* Id */ |
| 92 | PJSIP_MOD_PRIORITY_UA_PROXY_LAYER, /* Priority */ |
| 93 | &mod_ua_load, /* load() */ |
| 94 | NULL, /* start() */ |
| 95 | NULL, /* stop() */ |
| 96 | &mod_ua_unload, /* unload() */ |
| 97 | &mod_ua_on_rx_request, /* on_rx_request() */ |
| 98 | &mod_ua_on_rx_response, /* on_rx_response() */ |
| 99 | NULL, /* on_tx_request. */ |
| 100 | NULL, /* on_tx_response() */ |
| 101 | &mod_ua_on_tsx_state, /* on_tsx_state() */ |
| 102 | } |
| 103 | }; |
| 104 | |
| 105 | /* |
| 106 | * mod_ua_load() |
| 107 | * |
| 108 | * Called when module is being loaded by endpoint. |
| 109 | */ |
| 110 | static pj_status_t mod_ua_load(pjsip_endpoint *endpt) |
| 111 | { |
| 112 | pj_status_t status; |
| 113 | |
| 114 | /* Initialize the user agent. */ |
| 115 | mod_ua.endpt = endpt; |
| 116 | mod_ua.pool = pjsip_endpt_create_pool( endpt, "ua%p", PJSIP_POOL_LEN_UA, |
| 117 | PJSIP_POOL_INC_UA); |
| 118 | if (mod_ua.pool == NULL) |
| 119 | return PJ_ENOMEM; |
| 120 | |
| 121 | status = pj_mutex_create_recursive(mod_ua.pool, " ua%p", &mod_ua.mutex); |
| 122 | if (status != PJ_SUCCESS) |
| 123 | return status; |
| 124 | |
| 125 | mod_ua.dlg_table = pj_hash_create(mod_ua.pool, PJSIP_MAX_DIALOG_COUNT); |
| 126 | if (mod_ua.dlg_table == NULL) |
| 127 | return PJ_ENOMEM; |
| 128 | |
| 129 | pj_list_init(&mod_ua.free_dlgset_nodes); |
| 130 | |
| 131 | /* Initialize dialog lock. */ |
| 132 | status = pj_thread_local_alloc(&pjsip_dlg_lock_tls_id); |
| 133 | if (status != PJ_SUCCESS) |
| 134 | return status; |
| 135 | |
| 136 | pj_thread_local_set(pjsip_dlg_lock_tls_id, NULL); |
| 137 | |
| 138 | return PJ_SUCCESS; |
| 139 | |
| 140 | } |
| 141 | |
| 142 | /* |
| 143 | * mod_ua_unload() |
| 144 | * |
| 145 | * Called when module is being unloaded. |
| 146 | */ |
| 147 | static pj_status_t mod_ua_unload(void) |
| 148 | { |
| 149 | pj_thread_local_free(pjsip_dlg_lock_tls_id); |
| 150 | pj_mutex_destroy(mod_ua.mutex); |
| 151 | |
| 152 | /* Release pool */ |
| 153 | if (mod_ua.pool) { |
| 154 | pjsip_endpt_release_pool( mod_ua.endpt, mod_ua.pool ); |
| 155 | } |
| 156 | return PJ_SUCCESS; |
| 157 | } |
| 158 | |
| 159 | /* |
| 160 | * mod_ua_on_tsx_stats() |
| 161 | * |
| 162 | * Called on changed on transaction state. |
| 163 | */ |
| 164 | static void mod_ua_on_tsx_state( pjsip_transaction *tsx, pjsip_event *e) |
| 165 | { |
| 166 | pjsip_dialog *dlg; |
| 167 | |
| 168 | /* Get the dialog where this transaction belongs. */ |
| 169 | dlg = (pjsip_dialog*) tsx->mod_data[mod_ua.mod.id]; |
| 170 | |
| 171 | /* If dialog instance has gone, it could mean that the dialog |
| 172 | * may has been destroyed. |
| 173 | */ |
| 174 | if (dlg == NULL) |
| 175 | return; |
| 176 | |
| 177 | /* Hand over the event to the dialog. */ |
| 178 | pjsip_dlg_on_tsx_state(dlg, tsx, e); |
| 179 | } |
| 180 | |
| 181 | |
| 182 | /* |
| 183 | * Init user agent module and register it to the endpoint. |
| 184 | */ |
| 185 | PJ_DEF(pj_status_t) pjsip_ua_init_module( pjsip_endpoint *endpt, |
| 186 | const pjsip_ua_init_param *prm) |
| 187 | { |
| 188 | pj_status_t status; |
| 189 | |
| 190 | /* Check if module already registered. */ |
| 191 | PJ_ASSERT_RETURN(mod_ua.mod.id == -1, PJ_EINVALIDOP); |
| 192 | |
| 193 | /* Copy param, if exists. */ |
| 194 | if (prm) |
| 195 | pj_memcpy(&mod_ua.param, prm, sizeof(pjsip_ua_init_param)); |
| 196 | |
| 197 | /* Register the module. */ |
| 198 | status = pjsip_endpt_register_module(endpt, &mod_ua.mod); |
| 199 | |
| 200 | return status; |
| 201 | } |
| 202 | |
| 203 | /* |
| 204 | * Get the instance of the user agent. |
| 205 | * |
| 206 | */ |
| 207 | PJ_DEF(pjsip_user_agent*) pjsip_ua_instance(void) |
| 208 | { |
| 209 | return &mod_ua.mod; |
| 210 | } |
| 211 | |
| 212 | |
| 213 | /* |
| 214 | * Get the endpoint where this UA is currently registered. |
| 215 | */ |
| 216 | PJ_DEF(pjsip_endpoint*) pjsip_ua_get_endpt(pjsip_user_agent *ua) |
| 217 | { |
| 218 | PJ_UNUSED_ARG(ua); |
| 219 | pj_assert(ua == &mod_ua.mod); |
| 220 | return mod_ua.endpt; |
| 221 | } |
| 222 | |
| 223 | |
| 224 | /* |
| 225 | * Destroy the user agent layer. |
| 226 | */ |
| 227 | PJ_DEF(pj_status_t) pjsip_ua_destroy(void) |
| 228 | { |
| 229 | /* Check if module already destroyed. */ |
| 230 | PJ_ASSERT_RETURN(mod_ua.mod.id != -1, PJ_EINVALIDOP); |
| 231 | |
| 232 | return pjsip_endpt_unregister_module(mod_ua.endpt, &mod_ua.mod); |
| 233 | } |
| 234 | |
| 235 | |
| 236 | |
| 237 | /* |
| 238 | * Create key to identify dialog set. |
| 239 | */ |
| 240 | /* |
| 241 | PJ_DEF(void) pjsip_ua_create_dlg_set_key( pj_pool_t *pool, |
| 242 | pj_str_t *set_key, |
| 243 | const pj_str_t *call_id, |
| 244 | const pj_str_t *local_tag) |
| 245 | { |
| 246 | PJ_ASSERT_ON_FAIL(pool && set_key && call_id && local_tag, return;); |
| 247 | |
| 248 | set_key->slen = call_id->slen + local_tag->slen + 1; |
| 249 | set_key->ptr = (char*) pj_pool_alloc(pool, set_key->slen); |
| 250 | pj_assert(set_key->ptr != NULL); |
| 251 | |
| 252 | pj_memcpy(set_key->ptr, call_id->ptr, call_id->slen); |
| 253 | set_key->ptr[call_id->slen] = '$'; |
| 254 | pj_memcpy(set_key->ptr + call_id->slen + 1, |
| 255 | local_tag->ptr, local_tag->slen); |
| 256 | } |
| 257 | */ |
| 258 | |
| 259 | /* |
| 260 | * Acquire one dlg_set node to be put in the hash table. |
| 261 | * This will first look in the free nodes list, then allocate |
| 262 | * a new one from UA's pool when one is not available. |
| 263 | */ |
| 264 | static struct dlg_set *alloc_dlgset_node(void) |
| 265 | { |
| 266 | struct dlg_set *set; |
| 267 | |
| 268 | if (!pj_list_empty(&mod_ua.free_dlgset_nodes)) { |
| 269 | set = mod_ua.free_dlgset_nodes.next; |
| 270 | pj_list_erase(set); |
| 271 | return set; |
| 272 | } else { |
| 273 | set = PJ_POOL_ALLOC_T(mod_ua.pool, struct dlg_set); |
| 274 | return set; |
| 275 | } |
| 276 | } |
| 277 | |
| 278 | /* |
| 279 | * Register new dialog. Called by pjsip_dlg_create_uac() and |
| 280 | * pjsip_dlg_create_uas(); |
| 281 | */ |
| 282 | PJ_DEF(pj_status_t) pjsip_ua_register_dlg( pjsip_user_agent *ua, |
| 283 | pjsip_dialog *dlg ) |
| 284 | { |
| 285 | /* Sanity check. */ |
| 286 | PJ_ASSERT_RETURN(ua && dlg, PJ_EINVAL); |
| 287 | |
| 288 | /* For all dialogs, local tag (inc hash) must has been initialized. */ |
| 289 | PJ_ASSERT_RETURN(dlg->local.info && dlg->local.info->tag.slen && |
| 290 | dlg->local.tag_hval != 0, PJ_EBUG); |
| 291 | |
| 292 | /* For UAS dialog, remote tag (inc hash) must have been initialized. */ |
| 293 | //PJ_ASSERT_RETURN(dlg->role==PJSIP_ROLE_UAC || |
| 294 | // (dlg->role==PJSIP_ROLE_UAS && dlg->remote.info->tag.slen |
| 295 | // && dlg->remote.tag_hval != 0), PJ_EBUG); |
| 296 | |
| 297 | /* Lock the user agent. */ |
| 298 | pj_mutex_lock(mod_ua.mutex); |
| 299 | |
| 300 | /* For UAC, check if there is existing dialog in the same set. */ |
| 301 | if (dlg->role == PJSIP_ROLE_UAC) { |
| 302 | struct dlg_set *dlg_set; |
| 303 | |
| 304 | dlg_set = (struct dlg_set*) |
| 305 | pj_hash_get_lower( mod_ua.dlg_table, |
| 306 | dlg->local.info->tag.ptr, |
| 307 | (unsigned)dlg->local.info->tag.slen, |
| 308 | &dlg->local.tag_hval); |
| 309 | |
| 310 | if (dlg_set) { |
| 311 | /* This is NOT the first dialog in the dialog set. |
| 312 | * Just add this dialog in the list. |
| 313 | */ |
| 314 | pj_assert(dlg_set->dlg_list.next != (void*)&dlg_set->dlg_list); |
| 315 | pj_list_push_back(&dlg_set->dlg_list, dlg); |
| 316 | |
| 317 | dlg->dlg_set = dlg_set; |
| 318 | |
| 319 | } else { |
| 320 | /* This is the first dialog in the dialog set. |
| 321 | * Create the dialog set and add this dialog to it. |
| 322 | */ |
| 323 | dlg_set = alloc_dlgset_node(); |
| 324 | pj_list_init(&dlg_set->dlg_list); |
| 325 | pj_list_push_back(&dlg_set->dlg_list, dlg); |
| 326 | |
| 327 | dlg->dlg_set = dlg_set; |
| 328 | |
| 329 | /* Register the dialog set in the hash table. */ |
| 330 | pj_hash_set_np_lower(mod_ua.dlg_table, |
| 331 | dlg->local.info->tag.ptr, |
| 332 | (unsigned)dlg->local.info->tag.slen, |
| 333 | dlg->local.tag_hval, dlg_set->ht_entry, |
| 334 | dlg_set); |
| 335 | } |
| 336 | |
| 337 | } else { |
| 338 | /* For UAS, create the dialog set with a single dialog as member. */ |
| 339 | struct dlg_set *dlg_set; |
| 340 | |
| 341 | dlg_set = alloc_dlgset_node(); |
| 342 | pj_list_init(&dlg_set->dlg_list); |
| 343 | pj_list_push_back(&dlg_set->dlg_list, dlg); |
| 344 | |
| 345 | dlg->dlg_set = dlg_set; |
| 346 | |
| 347 | pj_hash_set_np_lower(mod_ua.dlg_table, |
| 348 | dlg->local.info->tag.ptr, |
| 349 | (unsigned)dlg->local.info->tag.slen, |
| 350 | dlg->local.tag_hval, dlg_set->ht_entry, dlg_set); |
| 351 | } |
| 352 | |
| 353 | /* Unlock user agent. */ |
| 354 | pj_mutex_unlock(mod_ua.mutex); |
| 355 | |
| 356 | /* Done. */ |
| 357 | return PJ_SUCCESS; |
| 358 | } |
| 359 | |
| 360 | |
| 361 | PJ_DEF(pj_status_t) pjsip_ua_unregister_dlg( pjsip_user_agent *ua, |
| 362 | pjsip_dialog *dlg ) |
| 363 | { |
| 364 | struct dlg_set *dlg_set; |
| 365 | pjsip_dialog *d; |
| 366 | |
| 367 | /* Sanity-check arguments. */ |
| 368 | PJ_ASSERT_RETURN(ua && dlg, PJ_EINVAL); |
| 369 | |
| 370 | /* Check that dialog has been registered. */ |
| 371 | PJ_ASSERT_RETURN(dlg->dlg_set, PJ_EINVALIDOP); |
| 372 | |
| 373 | /* Lock user agent. */ |
| 374 | pj_mutex_lock(mod_ua.mutex); |
| 375 | |
| 376 | /* Find this dialog from the dialog set. */ |
| 377 | dlg_set = (struct dlg_set*) dlg->dlg_set; |
| 378 | d = dlg_set->dlg_list.next; |
| 379 | while (d != (pjsip_dialog*)&dlg_set->dlg_list && d != dlg) { |
| 380 | d = d->next; |
| 381 | } |
| 382 | |
| 383 | if (d != dlg) { |
| 384 | pj_assert(!"Dialog is not registered!"); |
| 385 | pj_mutex_unlock(mod_ua.mutex); |
| 386 | return PJ_EINVALIDOP; |
| 387 | } |
| 388 | |
| 389 | /* Remove this dialog from the list. */ |
| 390 | pj_list_erase(dlg); |
| 391 | |
| 392 | /* If dialog list is empty, remove the dialog set from the hash table. */ |
| 393 | if (pj_list_empty(&dlg_set->dlg_list)) { |
| 394 | pj_hash_set_lower(NULL, mod_ua.dlg_table, dlg->local.info->tag.ptr, |
| 395 | (unsigned)dlg->local.info->tag.slen, |
| 396 | dlg->local.tag_hval, NULL); |
| 397 | |
| 398 | /* Return dlg_set to free nodes. */ |
| 399 | pj_list_push_back(&mod_ua.free_dlgset_nodes, dlg_set); |
| 400 | } |
| 401 | |
| 402 | /* Unlock user agent. */ |
| 403 | pj_mutex_unlock(mod_ua.mutex); |
| 404 | |
| 405 | /* Done. */ |
| 406 | return PJ_SUCCESS; |
| 407 | } |
| 408 | |
| 409 | |
| 410 | PJ_DEF(pjsip_dialog*) pjsip_rdata_get_dlg( pjsip_rx_data *rdata ) |
| 411 | { |
| 412 | return (pjsip_dialog*) rdata->endpt_info.mod_data[mod_ua.mod.id]; |
| 413 | } |
| 414 | |
| 415 | PJ_DEF(pjsip_dialog*) pjsip_tsx_get_dlg( pjsip_transaction *tsx ) |
| 416 | { |
| 417 | return (pjsip_dialog*) tsx->mod_data[mod_ua.mod.id]; |
| 418 | } |
| 419 | |
| 420 | |
| 421 | /* |
| 422 | * Retrieve the current number of dialog-set currently registered |
| 423 | * in the hash table. |
| 424 | */ |
| 425 | PJ_DEF(unsigned) pjsip_ua_get_dlg_set_count(void) |
| 426 | { |
| 427 | unsigned count; |
| 428 | |
| 429 | PJ_ASSERT_RETURN(mod_ua.endpt, 0); |
| 430 | |
| 431 | pj_mutex_lock(mod_ua.mutex); |
| 432 | count = pj_hash_count(mod_ua.dlg_table); |
| 433 | pj_mutex_unlock(mod_ua.mutex); |
| 434 | |
| 435 | return count; |
| 436 | } |
| 437 | |
| 438 | |
| 439 | /* |
| 440 | * Find a dialog. |
| 441 | */ |
| 442 | PJ_DEF(pjsip_dialog*) pjsip_ua_find_dialog(const pj_str_t *call_id, |
| 443 | const pj_str_t *local_tag, |
| 444 | const pj_str_t *remote_tag, |
| 445 | pj_bool_t lock_dialog) |
| 446 | { |
| 447 | struct dlg_set *dlg_set; |
| 448 | pjsip_dialog *dlg; |
| 449 | |
| 450 | PJ_ASSERT_RETURN(call_id && local_tag && remote_tag, NULL); |
| 451 | |
| 452 | /* Lock user agent. */ |
| 453 | pj_mutex_lock(mod_ua.mutex); |
| 454 | |
| 455 | /* Lookup the dialog set. */ |
| 456 | dlg_set = (struct dlg_set*) |
| 457 | pj_hash_get_lower(mod_ua.dlg_table, local_tag->ptr, |
| 458 | (unsigned)local_tag->slen, NULL); |
| 459 | if (dlg_set == NULL) { |
| 460 | /* Not found */ |
| 461 | pj_mutex_unlock(mod_ua.mutex); |
| 462 | return NULL; |
| 463 | } |
| 464 | |
| 465 | /* Dialog set is found, now find the matching dialog based on the |
| 466 | * remote tag. |
| 467 | */ |
| 468 | dlg = dlg_set->dlg_list.next; |
| 469 | while (dlg != (pjsip_dialog*)&dlg_set->dlg_list) { |
| 470 | if (pj_stricmp(&dlg->remote.info->tag, remote_tag) == 0) |
| 471 | break; |
| 472 | dlg = dlg->next; |
| 473 | } |
| 474 | |
| 475 | if (dlg == (pjsip_dialog*)&dlg_set->dlg_list) { |
| 476 | /* Not found */ |
| 477 | pj_mutex_unlock(mod_ua.mutex); |
| 478 | return NULL; |
| 479 | } |
| 480 | |
| 481 | /* Dialog has been found. It SHOULD have the right Call-ID!! */ |
| 482 | PJ_ASSERT_ON_FAIL(pj_strcmp(&dlg->call_id->id, call_id)==0, |
| 483 | {pj_mutex_unlock(mod_ua.mutex); return NULL;}); |
| 484 | |
| 485 | if (lock_dialog) { |
| 486 | if (pjsip_dlg_try_inc_lock(dlg) != PJ_SUCCESS) { |
| 487 | |
| 488 | /* |
| 489 | * Unable to acquire dialog's lock while holding the user |
| 490 | * agent's mutex. Release the UA mutex before retrying once |
| 491 | * more. |
| 492 | * |
| 493 | * THIS MAY CAUSE RACE CONDITION! |
| 494 | */ |
| 495 | |
| 496 | /* Unlock user agent. */ |
| 497 | pj_mutex_unlock(mod_ua.mutex); |
| 498 | /* Lock dialog */ |
| 499 | pjsip_dlg_inc_lock(dlg); |
| 500 | |
| 501 | } else { |
| 502 | /* Unlock user agent. */ |
| 503 | pj_mutex_unlock(mod_ua.mutex); |
| 504 | } |
| 505 | |
| 506 | } else { |
| 507 | /* Unlock user agent. */ |
| 508 | pj_mutex_unlock(mod_ua.mutex); |
| 509 | } |
| 510 | |
| 511 | return dlg; |
| 512 | } |
| 513 | |
| 514 | |
| 515 | /* |
| 516 | * Find the first dialog in dialog set in hash table for an incoming message. |
| 517 | */ |
| 518 | static struct dlg_set *find_dlg_set_for_msg( pjsip_rx_data *rdata ) |
| 519 | { |
| 520 | /* CANCEL message doesn't have To tag, so we must lookup the dialog |
| 521 | * by finding the INVITE UAS transaction being cancelled. |
| 522 | */ |
| 523 | if (rdata->msg_info.cseq->method.id == PJSIP_CANCEL_METHOD) { |
| 524 | |
| 525 | pjsip_dialog *dlg; |
| 526 | |
| 527 | /* Create key for the rdata, but this time, use INVITE as the |
| 528 | * method. |
| 529 | */ |
| 530 | pj_str_t key; |
| 531 | pjsip_role_e role; |
| 532 | pjsip_transaction *tsx; |
| 533 | |
| 534 | if (rdata->msg_info.msg->type == PJSIP_REQUEST_MSG) |
| 535 | role = PJSIP_ROLE_UAS; |
| 536 | else |
| 537 | role = PJSIP_ROLE_UAC; |
| 538 | |
| 539 | pjsip_tsx_create_key(rdata->tp_info.pool, &key, role, |
| 540 | pjsip_get_invite_method(), rdata); |
| 541 | |
| 542 | /* Lookup the INVITE transaction */ |
| 543 | tsx = pjsip_tsx_layer_find_tsx(&key, PJ_TRUE); |
| 544 | |
| 545 | /* We should find the dialog attached to the INVITE transaction */ |
| 546 | if (tsx) { |
| 547 | dlg = (pjsip_dialog*) tsx->mod_data[mod_ua.mod.id]; |
| 548 | pj_grp_lock_release(tsx->grp_lock); |
| 549 | |
| 550 | /* Dlg may be NULL on some extreme condition |
| 551 | * (e.g. during debugging where initially there is a dialog) |
| 552 | */ |
| 553 | return dlg ? (struct dlg_set*) dlg->dlg_set : NULL; |
| 554 | |
| 555 | } else { |
| 556 | return NULL; |
| 557 | } |
| 558 | |
| 559 | |
| 560 | } else { |
| 561 | pj_str_t *tag; |
| 562 | struct dlg_set *dlg_set; |
| 563 | |
| 564 | if (rdata->msg_info.msg->type == PJSIP_REQUEST_MSG) |
| 565 | tag = &rdata->msg_info.to->tag; |
| 566 | else |
| 567 | tag = &rdata->msg_info.from->tag; |
| 568 | |
| 569 | /* Lookup the dialog set. */ |
| 570 | dlg_set = (struct dlg_set*) |
| 571 | pj_hash_get_lower(mod_ua.dlg_table, tag->ptr, |
| 572 | (unsigned)tag->slen, NULL); |
| 573 | return dlg_set; |
| 574 | } |
| 575 | } |
| 576 | |
| 577 | /* On received requests. */ |
| 578 | static pj_bool_t mod_ua_on_rx_request(pjsip_rx_data *rdata) |
| 579 | { |
| 580 | struct dlg_set *dlg_set; |
| 581 | pj_str_t *from_tag; |
| 582 | pjsip_dialog *dlg; |
| 583 | pj_status_t status; |
| 584 | |
| 585 | /* Optimized path: bail out early if request is not CANCEL and it doesn't |
| 586 | * have To tag |
| 587 | */ |
| 588 | if (rdata->msg_info.to->tag.slen == 0 && |
| 589 | rdata->msg_info.msg->line.req.method.id != PJSIP_CANCEL_METHOD) |
| 590 | { |
| 591 | return PJ_FALSE; |
| 592 | } |
| 593 | |
| 594 | /* Incoming REGISTER may have tags in it */ |
| 595 | if (rdata->msg_info.msg->line.req.method.id == PJSIP_REGISTER_METHOD) |
| 596 | return PJ_FALSE; |
| 597 | |
| 598 | retry_on_deadlock: |
| 599 | |
| 600 | /* Lock user agent before looking up the dialog hash table. */ |
| 601 | pj_mutex_lock(mod_ua.mutex); |
| 602 | |
| 603 | /* Lookup the dialog set, based on the To tag header. */ |
| 604 | dlg_set = find_dlg_set_for_msg(rdata); |
| 605 | |
| 606 | /* If dialog is not found, respond with 481 (Call/Transaction |
| 607 | * Does Not Exist). |
| 608 | */ |
| 609 | if (dlg_set == NULL) { |
| 610 | /* Unable to find dialog. */ |
| 611 | pj_mutex_unlock(mod_ua.mutex); |
| 612 | |
| 613 | if (rdata->msg_info.msg->line.req.method.id != PJSIP_ACK_METHOD) { |
| 614 | PJ_LOG(5,(THIS_FILE, |
| 615 | "Unable to find dialogset for %s, answering with 481", |
| 616 | pjsip_rx_data_get_info(rdata))); |
| 617 | |
| 618 | /* Respond with 481 . */ |
| 619 | pjsip_endpt_respond_stateless( mod_ua.endpt, rdata, 481, NULL, |
| 620 | NULL, NULL ); |
| 621 | } |
| 622 | return PJ_TRUE; |
| 623 | } |
| 624 | |
| 625 | /* Dialog set has been found. |
| 626 | * Find the dialog in the dialog set based on the content of the remote |
| 627 | * tag. |
| 628 | */ |
| 629 | from_tag = &rdata->msg_info.from->tag; |
| 630 | dlg = dlg_set->dlg_list.next; |
| 631 | while (dlg != (pjsip_dialog*)&dlg_set->dlg_list) { |
| 632 | |
| 633 | if (pj_stricmp(&dlg->remote.info->tag, from_tag) == 0) |
| 634 | break; |
| 635 | |
| 636 | dlg = dlg->next; |
| 637 | } |
| 638 | |
| 639 | /* Dialog may not be found, e.g. in this case: |
| 640 | * - UAC sends SUBSCRIBE, then UAS sends NOTIFY before answering |
| 641 | * SUBSCRIBE request with 2xx. |
| 642 | * |
| 643 | * In this case, we can accept the request ONLY when the original |
| 644 | * dialog still has empty To tag. |
| 645 | */ |
| 646 | if (dlg == (pjsip_dialog*)&dlg_set->dlg_list) { |
| 647 | |
| 648 | pjsip_dialog *first_dlg = dlg_set->dlg_list.next; |
| 649 | |
| 650 | if (first_dlg->remote.info->tag.slen != 0) { |
| 651 | /* Not found. Mulfunction UAC? */ |
| 652 | pj_mutex_unlock(mod_ua.mutex); |
| 653 | |
| 654 | if (rdata->msg_info.msg->line.req.method.id != PJSIP_ACK_METHOD) { |
| 655 | PJ_LOG(5,(THIS_FILE, |
| 656 | "Unable to find dialog for %s, answering with 481", |
| 657 | pjsip_rx_data_get_info(rdata))); |
| 658 | |
| 659 | pjsip_endpt_respond_stateless(mod_ua.endpt, rdata, |
| 660 | PJSIP_SC_CALL_TSX_DOES_NOT_EXIST, |
| 661 | NULL, NULL, NULL); |
| 662 | } else { |
| 663 | PJ_LOG(5,(THIS_FILE, |
| 664 | "Unable to find dialog for %s", |
| 665 | pjsip_rx_data_get_info(rdata))); |
| 666 | } |
| 667 | return PJ_TRUE; |
| 668 | } |
| 669 | |
| 670 | dlg = first_dlg; |
| 671 | } |
| 672 | |
| 673 | /* Mark the dialog id of the request. */ |
| 674 | rdata->endpt_info.mod_data[mod_ua.mod.id] = dlg; |
| 675 | |
| 676 | /* Try to lock the dialog */ |
| 677 | PJ_LOG(6,(dlg->obj_name, "UA layer acquiring dialog lock for request")); |
| 678 | status = pjsip_dlg_try_inc_lock(dlg); |
| 679 | if (status != PJ_SUCCESS) { |
| 680 | /* Failed to acquire dialog mutex immediately, this could be |
| 681 | * because of deadlock. Release UA mutex, yield, and retry |
| 682 | * the whole thing once again. |
| 683 | */ |
| 684 | pj_mutex_unlock(mod_ua.mutex); |
| 685 | pj_thread_sleep(0); |
| 686 | goto retry_on_deadlock; |
| 687 | } |
| 688 | |
| 689 | /* Done with processing in UA layer, release lock */ |
| 690 | pj_mutex_unlock(mod_ua.mutex); |
| 691 | |
| 692 | /* Pass to dialog. */ |
| 693 | pjsip_dlg_on_rx_request(dlg, rdata); |
| 694 | |
| 695 | /* Unlock the dialog. This may destroy the dialog */ |
| 696 | pjsip_dlg_dec_lock(dlg); |
| 697 | |
| 698 | /* Report as handled. */ |
| 699 | return PJ_TRUE; |
| 700 | } |
| 701 | |
| 702 | |
| 703 | /* On rx response notification. |
| 704 | */ |
| 705 | static pj_bool_t mod_ua_on_rx_response(pjsip_rx_data *rdata) |
| 706 | { |
| 707 | pjsip_transaction *tsx; |
| 708 | struct dlg_set *dlg_set; |
| 709 | pjsip_dialog *dlg; |
| 710 | pj_status_t status; |
| 711 | |
| 712 | /* |
| 713 | * Find the dialog instance for the response. |
| 714 | * All outgoing dialog requests are sent statefully, which means |
| 715 | * there will be an UAC transaction associated with this response, |
| 716 | * and the dialog instance will be recorded in that transaction. |
| 717 | * |
| 718 | * But even when transaction is found, there is possibility that |
| 719 | * the response is a forked response. |
| 720 | */ |
| 721 | |
| 722 | retry_on_deadlock: |
| 723 | |
| 724 | dlg = NULL; |
| 725 | |
| 726 | /* Lock user agent dlg table before we're doing anything. */ |
| 727 | pj_mutex_lock(mod_ua.mutex); |
| 728 | |
| 729 | /* Check if transaction is present. */ |
| 730 | tsx = pjsip_rdata_get_tsx(rdata); |
| 731 | if (tsx) { |
| 732 | /* Check if dialog is present in the transaction. */ |
| 733 | dlg = pjsip_tsx_get_dlg(tsx); |
| 734 | if (!dlg) { |
| 735 | /* Unlock dialog hash table. */ |
| 736 | pj_mutex_unlock(mod_ua.mutex); |
| 737 | return PJ_FALSE; |
| 738 | } |
| 739 | |
| 740 | /* Get the dialog set. */ |
| 741 | dlg_set = (struct dlg_set*) dlg->dlg_set; |
| 742 | |
| 743 | /* Even if transaction is found and (candidate) dialog has been |
| 744 | * identified, it's possible that the request has forked. |
| 745 | */ |
| 746 | |
| 747 | } else { |
| 748 | /* Transaction is not present. |
| 749 | * Check if this is a 2xx/OK response to INVITE, which in this |
| 750 | * case the response will be handled directly by the |
| 751 | * dialog. |
| 752 | */ |
| 753 | pjsip_cseq_hdr *cseq_hdr = rdata->msg_info.cseq; |
| 754 | |
| 755 | if (cseq_hdr->method.id != PJSIP_INVITE_METHOD || |
| 756 | rdata->msg_info.msg->line.status.code / 100 != 2) |
| 757 | { |
| 758 | /* Not a 2xx response to INVITE. |
| 759 | * This must be some stateless response sent by other modules, |
| 760 | * or a very late response. |
| 761 | */ |
| 762 | /* Unlock dialog hash table. */ |
| 763 | pj_mutex_unlock(mod_ua.mutex); |
| 764 | return PJ_FALSE; |
| 765 | } |
| 766 | |
| 767 | |
| 768 | /* Get the dialog set. */ |
| 769 | dlg_set = (struct dlg_set*) |
| 770 | pj_hash_get_lower(mod_ua.dlg_table, |
| 771 | rdata->msg_info.from->tag.ptr, |
| 772 | (unsigned)rdata->msg_info.from->tag.slen, |
| 773 | NULL); |
| 774 | |
| 775 | if (!dlg_set) { |
| 776 | /* Unlock dialog hash table. */ |
| 777 | pj_mutex_unlock(mod_ua.mutex); |
| 778 | |
| 779 | /* Strayed 2xx response!! */ |
| 780 | PJ_LOG(4,(THIS_FILE, |
| 781 | "Received strayed 2xx response (no dialog is found)" |
| 782 | " from %s:%d: %s", |
| 783 | rdata->pkt_info.src_name, rdata->pkt_info.src_port, |
| 784 | pjsip_rx_data_get_info(rdata))); |
| 785 | |
| 786 | return PJ_TRUE; |
| 787 | } |
| 788 | } |
| 789 | |
| 790 | /* At this point, we must have the dialog set, and the dialog set |
| 791 | * must have a dialog in the list. |
| 792 | */ |
| 793 | pj_assert(dlg_set && !pj_list_empty(&dlg_set->dlg_list)); |
| 794 | |
| 795 | /* Check for forked response. |
| 796 | * Request will fork only for the initial INVITE request. |
| 797 | */ |
| 798 | |
| 799 | //This doesn't work when there is authentication challenge, since |
| 800 | //first_cseq evaluation will yield false. |
| 801 | //if (rdata->msg_info.cseq->method.id == PJSIP_INVITE_METHOD && |
| 802 | // rdata->msg_info.cseq->cseq == dlg_set->dlg_list.next->local.first_cseq) |
| 803 | |
| 804 | if (rdata->msg_info.cseq->method.id == PJSIP_INVITE_METHOD) { |
| 805 | |
| 806 | int st_code = rdata->msg_info.msg->line.status.code; |
| 807 | pj_str_t *to_tag = &rdata->msg_info.to->tag; |
| 808 | |
| 809 | dlg = dlg_set->dlg_list.next; |
| 810 | |
| 811 | while (dlg != (pjsip_dialog*)&dlg_set->dlg_list) { |
| 812 | |
| 813 | /* If there is dialog with no remote tag (i.e. dialog has not |
| 814 | * been established yet), then send this response to that |
| 815 | * dialog. |
| 816 | */ |
| 817 | if (dlg->remote.info->tag.slen == 0) |
| 818 | break; |
| 819 | |
| 820 | /* Otherwise find the one with matching To tag. */ |
| 821 | if (pj_stricmp(to_tag, &dlg->remote.info->tag) == 0) |
| 822 | break; |
| 823 | |
| 824 | dlg = dlg->next; |
| 825 | } |
| 826 | |
| 827 | /* If no dialog with matching remote tag is found, this must be |
| 828 | * a forked response. Respond to this ONLY when response is non-100 |
| 829 | * provisional response OR a 2xx response. |
| 830 | */ |
| 831 | if (dlg == (pjsip_dialog*)&dlg_set->dlg_list && |
| 832 | ((st_code/100==1 && st_code!=100) || st_code/100==2)) |
| 833 | { |
| 834 | |
| 835 | PJ_LOG(5,(THIS_FILE, |
| 836 | "Received forked %s for existing dialog %s", |
| 837 | pjsip_rx_data_get_info(rdata), |
| 838 | dlg_set->dlg_list.next->obj_name)); |
| 839 | |
| 840 | /* Report to application about forked condition. |
| 841 | * Application can either create a dialog or ignore the response. |
| 842 | */ |
| 843 | if (mod_ua.param.on_dlg_forked) { |
| 844 | dlg = (*mod_ua.param.on_dlg_forked)(dlg_set->dlg_list.next, |
| 845 | rdata); |
| 846 | if (dlg == NULL) { |
| 847 | pj_mutex_unlock(mod_ua.mutex); |
| 848 | return PJ_TRUE; |
| 849 | } |
| 850 | } else { |
| 851 | dlg = dlg_set->dlg_list.next; |
| 852 | |
| 853 | PJ_LOG(4,(THIS_FILE, |
| 854 | "Unhandled forked %s from %s:%d, response will be " |
| 855 | "handed over to the first dialog", |
| 856 | pjsip_rx_data_get_info(rdata), |
| 857 | rdata->pkt_info.src_name, rdata->pkt_info.src_port)); |
| 858 | } |
| 859 | |
| 860 | } else if (dlg == (pjsip_dialog*)&dlg_set->dlg_list) { |
| 861 | |
| 862 | /* For 100 or non-2xx response which has different To tag, |
| 863 | * pass the response to the first dialog. |
| 864 | */ |
| 865 | |
| 866 | dlg = dlg_set->dlg_list.next; |
| 867 | |
| 868 | } |
| 869 | |
| 870 | } else { |
| 871 | /* Either this is a non-INVITE response, or subsequent INVITE |
| 872 | * within dialog. The dialog should have been identified when |
| 873 | * the transaction was found. |
| 874 | */ |
| 875 | pj_assert(tsx != NULL); |
| 876 | pj_assert(dlg != NULL); |
| 877 | } |
| 878 | |
| 879 | /* The dialog must have been found. */ |
| 880 | pj_assert(dlg != NULL); |
| 881 | |
| 882 | /* Put the dialog instance in the rdata. */ |
| 883 | rdata->endpt_info.mod_data[mod_ua.mod.id] = dlg; |
| 884 | |
| 885 | /* Attempt to acquire lock to the dialog. */ |
| 886 | PJ_LOG(6,(dlg->obj_name, "UA layer acquiring dialog lock for response")); |
| 887 | status = pjsip_dlg_try_inc_lock(dlg); |
| 888 | if (status != PJ_SUCCESS) { |
| 889 | /* Failed to acquire dialog mutex. This could indicate a deadlock |
| 890 | * situation, and for safety, try to avoid deadlock by releasing |
| 891 | * UA mutex, yield, and retry the whole processing once again. |
| 892 | */ |
| 893 | pj_mutex_unlock(mod_ua.mutex); |
| 894 | pj_thread_sleep(0); |
| 895 | goto retry_on_deadlock; |
| 896 | } |
| 897 | |
| 898 | /* We're done with processing in the UA layer, we can release the mutex */ |
| 899 | pj_mutex_unlock(mod_ua.mutex); |
| 900 | |
| 901 | /* Pass the response to the dialog. */ |
| 902 | pjsip_dlg_on_rx_response(dlg, rdata); |
| 903 | |
| 904 | /* Unlock the dialog. This may destroy the dialog. */ |
| 905 | pjsip_dlg_dec_lock(dlg); |
| 906 | |
| 907 | /* Done. */ |
| 908 | return PJ_TRUE; |
| 909 | } |
| 910 | |
| 911 | |
| 912 | #if PJ_LOG_MAX_LEVEL >= 3 |
| 913 | static void print_dialog( const char *title, |
| 914 | pjsip_dialog *dlg, char *buf, pj_size_t size) |
| 915 | { |
| 916 | int len; |
| 917 | char userinfo[128]; |
| 918 | |
| 919 | len = pjsip_hdr_print_on(dlg->remote.info, userinfo, sizeof(userinfo)); |
| 920 | if (len < 0) |
| 921 | pj_ansi_strcpy(userinfo, "<--uri too long-->"); |
| 922 | else |
| 923 | userinfo[len] = '\0'; |
| 924 | |
| 925 | len = pj_ansi_snprintf(buf, size, "%s[%s] %s", |
| 926 | title, |
| 927 | (dlg->state==PJSIP_DIALOG_STATE_NULL ? " - " : |
| 928 | "est"), |
| 929 | userinfo); |
| 930 | if (len < 1 || len >= (int)size) { |
| 931 | pj_ansi_strcpy(buf, "<--uri too long-->"); |
| 932 | } else |
| 933 | buf[len] = '\0'; |
| 934 | } |
| 935 | #endif |
| 936 | |
| 937 | /* |
| 938 | * Dump user agent contents (e.g. all dialogs). |
| 939 | */ |
| 940 | PJ_DEF(void) pjsip_ua_dump(pj_bool_t detail) |
| 941 | { |
| 942 | #if PJ_LOG_MAX_LEVEL >= 3 |
| 943 | pj_hash_iterator_t itbuf, *it; |
| 944 | char dlginfo[128]; |
| 945 | |
| 946 | pj_mutex_lock(mod_ua.mutex); |
| 947 | |
| 948 | PJ_LOG(3, (THIS_FILE, "Number of dialog sets: %u", |
| 949 | pj_hash_count(mod_ua.dlg_table))); |
| 950 | |
| 951 | if (detail && pj_hash_count(mod_ua.dlg_table)) { |
| 952 | PJ_LOG(3, (THIS_FILE, "Dumping dialog sets:")); |
| 953 | it = pj_hash_first(mod_ua.dlg_table, &itbuf); |
| 954 | for (; it != NULL; it = pj_hash_next(mod_ua.dlg_table, it)) { |
| 955 | struct dlg_set *dlg_set; |
| 956 | pjsip_dialog *dlg; |
| 957 | const char *title; |
| 958 | |
| 959 | dlg_set = (struct dlg_set*) pj_hash_this(mod_ua.dlg_table, it); |
| 960 | if (!dlg_set || pj_list_empty(&dlg_set->dlg_list)) continue; |
| 961 | |
| 962 | /* First dialog in dialog set. */ |
| 963 | dlg = dlg_set->dlg_list.next; |
| 964 | if (dlg->role == PJSIP_ROLE_UAC) |
| 965 | title = " [out] "; |
| 966 | else |
| 967 | title = " [in] "; |
| 968 | |
| 969 | print_dialog(title, dlg, dlginfo, sizeof(dlginfo)); |
| 970 | PJ_LOG(3,(THIS_FILE, "%s", dlginfo)); |
| 971 | |
| 972 | /* Next dialog in dialog set (forked) */ |
| 973 | dlg = dlg->next; |
| 974 | while (dlg != (pjsip_dialog*) &dlg_set->dlg_list) { |
| 975 | print_dialog(" [forked] ", dlg, dlginfo, sizeof(dlginfo)); |
| 976 | dlg = dlg->next; |
| 977 | } |
| 978 | } |
| 979 | } |
| 980 | |
| 981 | pj_mutex_unlock(mod_ua.mutex); |
| 982 | #endif |
| 983 | } |
| 984 | |