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-ua/sip_inv.h> |
| 21 | #include <pjsip-ua/sip_100rel.h> |
| 22 | #include <pjsip-ua/sip_timer.h> |
| 23 | #include <pjsip/sip_module.h> |
| 24 | #include <pjsip/sip_endpoint.h> |
| 25 | #include <pjsip/sip_event.h> |
| 26 | #include <pjsip/sip_multipart.h> |
| 27 | #include <pjsip/sip_transaction.h> |
| 28 | #include <pjmedia/sdp.h> |
| 29 | #include <pjmedia/sdp_neg.h> |
| 30 | #include <pjmedia/errno.h> |
| 31 | #include <pj/string.h> |
| 32 | #include <pj/pool.h> |
| 33 | #include <pj/assert.h> |
| 34 | #include <pj/os.h> |
| 35 | #include <pj/log.h> |
| 36 | #include <pj/rand.h> |
| 37 | |
| 38 | /* |
| 39 | * Note on offer/answer: |
| 40 | * |
| 41 | * The offer/answer framework in this implementation assumes the occurence |
| 42 | * of SDP in a particular request/response according to this table: |
| 43 | |
| 44 | offer answer Note: |
| 45 | ======================================================================== |
| 46 | INVITE X INVITE may contain offer |
| 47 | 18x/INVITE X X Response may contain offer or answer |
| 48 | 2xx/INVITE X X Response may contain offer or answer |
| 49 | ACK X ACK may contain answer |
| 50 | |
| 51 | PRACK X PRACK can only contain answer |
| 52 | 2xx/PRACK Response may not have offer nor answer |
| 53 | |
| 54 | UPDATE X UPDATE may only contain offer |
| 55 | 2xx/UPDATE X Response may only contain answer |
| 56 | ======================================================================== |
| 57 | |
| 58 | * |
| 59 | */ |
| 60 | |
| 61 | #define THIS_FILE "sip_inv.c" |
| 62 | |
| 63 | static const char *inv_state_names[] = |
| 64 | { |
| 65 | "NULL", |
| 66 | "CALLING", |
| 67 | "INCOMING", |
| 68 | "EARLY", |
| 69 | "CONNECTING", |
| 70 | "CONFIRMED", |
| 71 | "DISCONNCTD", |
| 72 | "TERMINATED", |
| 73 | }; |
| 74 | |
| 75 | /* UPDATE method */ |
| 76 | static const pjsip_method pjsip_update_method = |
| 77 | { |
| 78 | PJSIP_OTHER_METHOD, |
| 79 | { "UPDATE", 6 } |
| 80 | }; |
| 81 | |
| 82 | #define POOL_INIT_SIZE 256 |
| 83 | #define POOL_INC_SIZE 256 |
| 84 | |
| 85 | /* |
| 86 | * Static prototypes. |
| 87 | */ |
| 88 | static pj_status_t mod_inv_load(pjsip_endpoint *endpt); |
| 89 | static pj_status_t mod_inv_unload(void); |
| 90 | static pj_bool_t mod_inv_on_rx_request(pjsip_rx_data *rdata); |
| 91 | static pj_bool_t mod_inv_on_rx_response(pjsip_rx_data *rdata); |
| 92 | static void mod_inv_on_tsx_state(pjsip_transaction*, pjsip_event*); |
| 93 | |
| 94 | static void inv_on_state_null( pjsip_inv_session *inv, pjsip_event *e); |
| 95 | static void inv_on_state_calling( pjsip_inv_session *inv, pjsip_event *e); |
| 96 | static void inv_on_state_incoming( pjsip_inv_session *inv, pjsip_event *e); |
| 97 | static void inv_on_state_early( pjsip_inv_session *inv, pjsip_event *e); |
| 98 | static void inv_on_state_connecting( pjsip_inv_session *inv, pjsip_event *e); |
| 99 | static void inv_on_state_confirmed( pjsip_inv_session *inv, pjsip_event *e); |
| 100 | static void inv_on_state_disconnected( pjsip_inv_session *inv, pjsip_event *e); |
| 101 | |
| 102 | static pj_status_t inv_check_sdp_in_incoming_msg( pjsip_inv_session *inv, |
| 103 | pjsip_transaction *tsx, |
| 104 | pjsip_rx_data *rdata); |
| 105 | static pj_status_t inv_negotiate_sdp( pjsip_inv_session *inv ); |
| 106 | static pjsip_msg_body *create_sdp_body(pj_pool_t *pool, |
| 107 | const pjmedia_sdp_session *c_sdp); |
| 108 | static pj_status_t process_answer( pjsip_inv_session *inv, |
| 109 | int st_code, |
| 110 | pjsip_tx_data *tdata, |
| 111 | const pjmedia_sdp_session *local_sdp); |
| 112 | |
| 113 | static pj_status_t handle_timer_response(pjsip_inv_session *inv, |
| 114 | const pjsip_rx_data *rdata, |
| 115 | pj_bool_t end_sess_on_failure); |
| 116 | |
| 117 | static void (*inv_state_handler[])( pjsip_inv_session *inv, pjsip_event *e) = |
| 118 | { |
| 119 | &inv_on_state_null, |
| 120 | &inv_on_state_calling, |
| 121 | &inv_on_state_incoming, |
| 122 | &inv_on_state_early, |
| 123 | &inv_on_state_connecting, |
| 124 | &inv_on_state_confirmed, |
| 125 | &inv_on_state_disconnected, |
| 126 | }; |
| 127 | |
| 128 | static struct mod_inv |
| 129 | { |
| 130 | pjsip_module mod; |
| 131 | pjsip_endpoint *endpt; |
| 132 | pjsip_inv_callback cb; |
| 133 | } mod_inv = |
| 134 | { |
| 135 | { |
| 136 | NULL, NULL, /* prev, next. */ |
| 137 | { "mod-invite", 10 }, /* Name. */ |
| 138 | -1, /* Id */ |
| 139 | PJSIP_MOD_PRIORITY_DIALOG_USAGE, /* Priority */ |
| 140 | &mod_inv_load, /* load() */ |
| 141 | NULL, /* start() */ |
| 142 | NULL, /* stop() */ |
| 143 | &mod_inv_unload, /* unload() */ |
| 144 | &mod_inv_on_rx_request, /* on_rx_request() */ |
| 145 | &mod_inv_on_rx_response, /* on_rx_response() */ |
| 146 | NULL, /* on_tx_request. */ |
| 147 | NULL, /* on_tx_response() */ |
| 148 | &mod_inv_on_tsx_state, /* on_tsx_state() */ |
| 149 | } |
| 150 | }; |
| 151 | |
| 152 | |
| 153 | /* Invite session data to be attached to transaction. */ |
| 154 | struct tsx_inv_data |
| 155 | { |
| 156 | pjsip_inv_session *inv; /* The invite session */ |
| 157 | pj_bool_t sdp_done; /* SDP negotiation done for this tsx? */ |
| 158 | pj_bool_t retrying; /* Resend (e.g. due to 401/407) */ |
| 159 | pj_str_t done_tag; /* To tag in RX response with answer */ |
| 160 | pj_bool_t done_early;/* Negotiation was done for early med? */ |
| 161 | }; |
| 162 | |
| 163 | /* |
| 164 | * Module load() |
| 165 | */ |
| 166 | static pj_status_t mod_inv_load(pjsip_endpoint *endpt) |
| 167 | { |
| 168 | pj_str_t allowed[] = {{"INVITE", 6}, {"ACK",3}, {"BYE",3}, {"CANCEL",6}, |
| 169 | { "UPDATE", 6}}; |
| 170 | pj_str_t accepted = { "application/sdp", 15 }; |
| 171 | |
| 172 | /* Register supported methods: INVITE, ACK, BYE, CANCEL, UPDATE */ |
| 173 | pjsip_endpt_add_capability(endpt, &mod_inv.mod, PJSIP_H_ALLOW, NULL, |
| 174 | PJ_ARRAY_SIZE(allowed), allowed); |
| 175 | |
| 176 | /* Register "application/sdp" in Accept header */ |
| 177 | pjsip_endpt_add_capability(endpt, &mod_inv.mod, PJSIP_H_ACCEPT, NULL, |
| 178 | 1, &accepted); |
| 179 | |
| 180 | return PJ_SUCCESS; |
| 181 | } |
| 182 | |
| 183 | /* |
| 184 | * Module unload() |
| 185 | */ |
| 186 | static pj_status_t mod_inv_unload(void) |
| 187 | { |
| 188 | /* Should remove capability here */ |
| 189 | return PJ_SUCCESS; |
| 190 | } |
| 191 | |
| 192 | /* |
| 193 | * Set session state. |
| 194 | */ |
| 195 | void inv_set_state(pjsip_inv_session *inv, pjsip_inv_state state, |
| 196 | pjsip_event *e) |
| 197 | { |
| 198 | pjsip_inv_state prev_state = inv->state; |
| 199 | pj_bool_t dont_notify = PJ_FALSE; |
| 200 | pj_status_t status; |
| 201 | |
| 202 | /* Prevent STATE_CALLING from being reported more than once because |
| 203 | * of authentication |
| 204 | * https://trac.pjsip.org/repos/ticket/1318 |
| 205 | */ |
| 206 | if (state==PJSIP_INV_STATE_CALLING && |
| 207 | (inv->cb_called & (1 << PJSIP_INV_STATE_CALLING)) != 0) |
| 208 | { |
| 209 | dont_notify = PJ_TRUE; |
| 210 | } |
| 211 | |
| 212 | /* If state is confirmed, check that SDP negotiation is done, |
| 213 | * otherwise disconnect the session. |
| 214 | */ |
| 215 | if (state == PJSIP_INV_STATE_CONFIRMED) { |
| 216 | struct tsx_inv_data *tsx_inv_data = NULL; |
| 217 | |
| 218 | if (inv->invite_tsx) { |
| 219 | tsx_inv_data = (struct tsx_inv_data*) |
| 220 | inv->invite_tsx->mod_data[mod_inv.mod.id]; |
| 221 | } |
| 222 | |
| 223 | if (pjmedia_sdp_neg_get_state(inv->neg)!=PJMEDIA_SDP_NEG_STATE_DONE && |
| 224 | (tsx_inv_data && !tsx_inv_data->sdp_done) ) |
| 225 | { |
| 226 | pjsip_tx_data *bye; |
| 227 | |
| 228 | PJ_LOG(4,(inv->obj_name, "SDP offer/answer incomplete, ending the " |
| 229 | "session")); |
| 230 | |
| 231 | status = pjsip_inv_end_session(inv, PJSIP_SC_NOT_ACCEPTABLE, |
| 232 | NULL, &bye); |
| 233 | if (status == PJ_SUCCESS && bye) |
| 234 | status = pjsip_inv_send_msg(inv, bye); |
| 235 | |
| 236 | return; |
| 237 | } |
| 238 | } |
| 239 | |
| 240 | /* Set state. */ |
| 241 | inv->state = state; |
| 242 | |
| 243 | /* If state is DISCONNECTED, cause code MUST have been set. */ |
| 244 | pj_assert(inv->state != PJSIP_INV_STATE_DISCONNECTED || |
| 245 | inv->cause != 0); |
| 246 | |
| 247 | /* Mark the callback as called for this state */ |
| 248 | inv->cb_called |= (1 << state); |
| 249 | |
| 250 | /* Call on_state_changed() callback. */ |
| 251 | if (mod_inv.cb.on_state_changed && inv->notify && !dont_notify) |
| 252 | (*mod_inv.cb.on_state_changed)(inv, e); |
| 253 | |
| 254 | /* Only decrement when previous state is not already DISCONNECTED */ |
| 255 | if (inv->state == PJSIP_INV_STATE_DISCONNECTED && |
| 256 | prev_state != PJSIP_INV_STATE_DISCONNECTED) |
| 257 | { |
| 258 | if (inv->last_ack) { |
| 259 | pjsip_tx_data_dec_ref(inv->last_ack); |
| 260 | inv->last_ack = NULL; |
| 261 | } |
| 262 | if (inv->invite_req) { |
| 263 | pjsip_tx_data_dec_ref(inv->invite_req); |
| 264 | inv->invite_req = NULL; |
| 265 | } |
| 266 | pjsip_100rel_end_session(inv); |
| 267 | pjsip_timer_end_session(inv); |
| 268 | pjsip_dlg_dec_session(inv->dlg, &mod_inv.mod); |
| 269 | |
| 270 | /* Release the flip-flop pools */ |
| 271 | pj_pool_release(inv->pool_prov); |
| 272 | inv->pool_prov = NULL; |
| 273 | pj_pool_release(inv->pool_active); |
| 274 | inv->pool_active = NULL; |
| 275 | } |
| 276 | } |
| 277 | |
| 278 | |
| 279 | /* |
| 280 | * Set cause code. |
| 281 | */ |
| 282 | void inv_set_cause(pjsip_inv_session *inv, int cause_code, |
| 283 | const pj_str_t *cause_text) |
| 284 | { |
| 285 | if (cause_code > inv->cause) { |
| 286 | inv->cause = (pjsip_status_code) cause_code; |
| 287 | if (cause_text) |
| 288 | pj_strdup(inv->pool, &inv->cause_text, cause_text); |
| 289 | else if (cause_code/100 == 2) |
| 290 | inv->cause_text = pj_str("Normal call clearing"); |
| 291 | else |
| 292 | inv->cause_text = *pjsip_get_status_text(cause_code); |
| 293 | } |
| 294 | } |
| 295 | |
| 296 | |
| 297 | /* |
| 298 | * Check if outgoing request needs to have SDP answer. |
| 299 | * This applies for both ACK and PRACK requests. |
| 300 | */ |
| 301 | static const pjmedia_sdp_session *inv_has_pending_answer(pjsip_inv_session *inv, |
| 302 | pjsip_transaction *tsx) |
| 303 | { |
| 304 | pjmedia_sdp_neg_state neg_state; |
| 305 | const pjmedia_sdp_session *sdp = NULL; |
| 306 | pj_status_t status; |
| 307 | |
| 308 | /* If SDP negotiator is ready, start negotiation. */ |
| 309 | |
| 310 | /* Start nego when appropriate. */ |
| 311 | neg_state = inv->neg ? pjmedia_sdp_neg_get_state(inv->neg) : |
| 312 | PJMEDIA_SDP_NEG_STATE_NULL; |
| 313 | |
| 314 | if (neg_state == PJMEDIA_SDP_NEG_STATE_DONE) { |
| 315 | |
| 316 | /* Nothing to do */ |
| 317 | |
| 318 | } else if (neg_state == PJMEDIA_SDP_NEG_STATE_WAIT_NEGO && |
| 319 | pjmedia_sdp_neg_has_local_answer(inv->neg) ) |
| 320 | { |
| 321 | struct tsx_inv_data *tsx_inv_data; |
| 322 | struct tsx_inv_data dummy; |
| 323 | |
| 324 | /* Get invite session's transaction data. |
| 325 | * Note that tsx may be NULL, for example when application sends |
| 326 | * delayed ACK request (at this time, the original INVITE |
| 327 | * transaction may have been destroyed. |
| 328 | */ |
| 329 | if (tsx) { |
| 330 | tsx_inv_data = (struct tsx_inv_data*)tsx->mod_data[mod_inv.mod.id]; |
| 331 | } else { |
| 332 | tsx_inv_data = &dummy; |
| 333 | pj_bzero(&dummy, sizeof(dummy)); |
| 334 | dummy.inv = inv; |
| 335 | } |
| 336 | |
| 337 | status = inv_negotiate_sdp(inv); |
| 338 | if (status != PJ_SUCCESS) |
| 339 | return NULL; |
| 340 | |
| 341 | /* Mark this transaction has having SDP offer/answer done. */ |
| 342 | tsx_inv_data->sdp_done = 1; |
| 343 | |
| 344 | status = pjmedia_sdp_neg_get_active_local(inv->neg, &sdp); |
| 345 | |
| 346 | } else { |
| 347 | /* This remark is only valid for ACK. |
| 348 | PJ_LOG(4,(inv->dlg->obj_name, |
| 349 | "FYI, the SDP negotiator state (%s) is in a mess " |
| 350 | "when sending this ACK/PRACK request", |
| 351 | pjmedia_sdp_neg_state_str(neg_state))); |
| 352 | */ |
| 353 | } |
| 354 | |
| 355 | return sdp; |
| 356 | } |
| 357 | |
| 358 | |
| 359 | /* |
| 360 | * Send ACK for 2xx response. |
| 361 | */ |
| 362 | static pj_status_t inv_send_ack(pjsip_inv_session *inv, pjsip_event *e) |
| 363 | { |
| 364 | pjsip_rx_data *rdata; |
| 365 | pjsip_event ack_e; |
| 366 | pj_status_t status; |
| 367 | |
| 368 | if (e->type == PJSIP_EVENT_TSX_STATE) |
| 369 | rdata = e->body.tsx_state.src.rdata; |
| 370 | else if (e->type == PJSIP_EVENT_RX_MSG) |
| 371 | rdata = e->body.rx_msg.rdata; |
| 372 | else { |
| 373 | pj_assert(!"Unsupported event type"); |
| 374 | return PJ_EBUG; |
| 375 | } |
| 376 | |
| 377 | PJ_LOG(5,(inv->obj_name, "Received %s, sending ACK", |
| 378 | pjsip_rx_data_get_info(rdata))); |
| 379 | |
| 380 | /* Check if we have cached ACK request. Must not use the cached ACK |
| 381 | * if it's still marked as pending by transport (#1011) |
| 382 | */ |
| 383 | if (inv->last_ack && rdata->msg_info.cseq->cseq == inv->last_ack_cseq && |
| 384 | !inv->last_ack->is_pending) |
| 385 | { |
| 386 | pjsip_tx_data_add_ref(inv->last_ack); |
| 387 | |
| 388 | } else if (mod_inv.cb.on_send_ack) { |
| 389 | /* If application handles ACK transmission manually, just notify the |
| 390 | * callback |
| 391 | */ |
| 392 | PJ_LOG(5,(inv->obj_name, "Received %s, notifying application callback", |
| 393 | pjsip_rx_data_get_info(rdata))); |
| 394 | |
| 395 | (*mod_inv.cb.on_send_ack)(inv, rdata); |
| 396 | return PJ_SUCCESS; |
| 397 | |
| 398 | } else { |
| 399 | status = pjsip_inv_create_ack(inv, rdata->msg_info.cseq->cseq, |
| 400 | &inv->last_ack); |
| 401 | if (status != PJ_SUCCESS) |
| 402 | return status; |
| 403 | } |
| 404 | |
| 405 | PJSIP_EVENT_INIT_TX_MSG(ack_e, inv->last_ack); |
| 406 | |
| 407 | /* Send ACK */ |
| 408 | status = pjsip_dlg_send_request(inv->dlg, inv->last_ack, -1, NULL); |
| 409 | if (status != PJ_SUCCESS) { |
| 410 | /* Better luck next time */ |
| 411 | pj_assert(!"Unable to send ACK!"); |
| 412 | return status; |
| 413 | } |
| 414 | |
| 415 | |
| 416 | /* Set state to CONFIRMED (if we're not in CONFIRMED yet). |
| 417 | * But don't set it to CONFIRMED if we're already DISCONNECTED |
| 418 | * (this may have been a late 200/OK response. |
| 419 | */ |
| 420 | if (inv->state < PJSIP_INV_STATE_CONFIRMED) { |
| 421 | inv_set_state(inv, PJSIP_INV_STATE_CONFIRMED, &ack_e); |
| 422 | } |
| 423 | |
| 424 | return PJ_SUCCESS; |
| 425 | } |
| 426 | |
| 427 | /* |
| 428 | * Module on_rx_request() |
| 429 | * |
| 430 | * This callback is called for these events: |
| 431 | * - endpoint receives request which was unhandled by higher priority |
| 432 | * modules (e.g. transaction layer, dialog layer). |
| 433 | * - dialog distributes incoming request to its usages. |
| 434 | */ |
| 435 | static pj_bool_t mod_inv_on_rx_request(pjsip_rx_data *rdata) |
| 436 | { |
| 437 | pjsip_method *method; |
| 438 | pjsip_dialog *dlg; |
| 439 | pjsip_inv_session *inv; |
| 440 | |
| 441 | /* Only wants to receive request from a dialog. */ |
| 442 | dlg = pjsip_rdata_get_dlg(rdata); |
| 443 | if (dlg == NULL) |
| 444 | return PJ_FALSE; |
| 445 | |
| 446 | inv = (pjsip_inv_session*) dlg->mod_data[mod_inv.mod.id]; |
| 447 | |
| 448 | /* Report to dialog that we handle INVITE, CANCEL, BYE, ACK. |
| 449 | * If we need to send response, it will be sent in the state |
| 450 | * handlers. |
| 451 | */ |
| 452 | method = &rdata->msg_info.msg->line.req.method; |
| 453 | |
| 454 | if (method->id == PJSIP_INVITE_METHOD) { |
| 455 | return PJ_TRUE; |
| 456 | } |
| 457 | |
| 458 | /* BYE and CANCEL must have existing invite session */ |
| 459 | if (method->id == PJSIP_BYE_METHOD || |
| 460 | method->id == PJSIP_CANCEL_METHOD) |
| 461 | { |
| 462 | if (inv == NULL) |
| 463 | return PJ_FALSE; |
| 464 | |
| 465 | return PJ_TRUE; |
| 466 | } |
| 467 | |
| 468 | /* On receipt ACK request, when state is CONNECTING, |
| 469 | * move state to CONFIRMED. |
| 470 | */ |
| 471 | if (method->id == PJSIP_ACK_METHOD && inv) { |
| 472 | |
| 473 | /* Ignore if we don't have INVITE in progress */ |
| 474 | if (!inv->invite_tsx) { |
| 475 | return PJ_TRUE; |
| 476 | } |
| 477 | |
| 478 | /* Ignore ACK if pending INVITE transaction has not finished. */ |
| 479 | if (inv->invite_tsx->state < PJSIP_TSX_STATE_COMPLETED) { |
| 480 | return PJ_TRUE; |
| 481 | } |
| 482 | |
| 483 | /* Ignore ACK with different CSeq |
| 484 | * https://trac.pjsip.org/repos/ticket/1391 |
| 485 | */ |
| 486 | if (rdata->msg_info.cseq->cseq != inv->invite_tsx->cseq) { |
| 487 | return PJ_TRUE; |
| 488 | } |
| 489 | |
| 490 | /* Terminate INVITE transaction, if it's still present. */ |
| 491 | if (inv->invite_tsx->state <= PJSIP_TSX_STATE_COMPLETED) { |
| 492 | /* Before we terminate INVITE transaction, process the SDP |
| 493 | * in the ACK request, if any. |
| 494 | * Only do this when invite state is not already disconnected |
| 495 | * (http://trac.pjsip.org/repos/ticket/640). |
| 496 | */ |
| 497 | if (inv->state < PJSIP_INV_STATE_DISCONNECTED) { |
| 498 | inv_check_sdp_in_incoming_msg(inv, inv->invite_tsx, rdata); |
| 499 | |
| 500 | /* Check if local offer got no SDP answer and INVITE session |
| 501 | * is in CONFIRMED state. |
| 502 | */ |
| 503 | if (pjmedia_sdp_neg_get_state(inv->neg)== |
| 504 | PJMEDIA_SDP_NEG_STATE_LOCAL_OFFER && |
| 505 | inv->state==PJSIP_INV_STATE_CONFIRMED) |
| 506 | { |
| 507 | pjmedia_sdp_neg_cancel_offer(inv->neg); |
| 508 | } |
| 509 | } |
| 510 | |
| 511 | /* Now we can terminate the INVITE transaction */ |
| 512 | pj_assert(inv->invite_tsx->status_code >= 200); |
| 513 | pjsip_tsx_terminate(inv->invite_tsx, |
| 514 | inv->invite_tsx->status_code); |
| 515 | inv->invite_tsx = NULL; |
| 516 | if (inv->last_answer) { |
| 517 | pjsip_tx_data_dec_ref(inv->last_answer); |
| 518 | inv->last_answer = NULL; |
| 519 | } |
| 520 | } |
| 521 | |
| 522 | /* On receipt of ACK, only set state to confirmed when state |
| 523 | * is CONNECTING (e.g. we don't want to set the state to confirmed |
| 524 | * when we receive ACK retransmission after sending non-2xx!) |
| 525 | */ |
| 526 | if (inv->state == PJSIP_INV_STATE_CONNECTING) { |
| 527 | pjsip_event event; |
| 528 | |
| 529 | PJSIP_EVENT_INIT_RX_MSG(event, rdata); |
| 530 | inv_set_state(inv, PJSIP_INV_STATE_CONFIRMED, &event); |
| 531 | } |
| 532 | } |
| 533 | |
| 534 | return PJ_FALSE; |
| 535 | } |
| 536 | |
| 537 | /* This function will process Session Timer headers in received |
| 538 | * 2xx or 422 response of INVITE/UPDATE request. |
| 539 | */ |
| 540 | static pj_status_t handle_timer_response(pjsip_inv_session *inv, |
| 541 | const pjsip_rx_data *rdata, |
| 542 | pj_bool_t end_sess_on_failure) |
| 543 | { |
| 544 | pjsip_status_code st_code; |
| 545 | pj_status_t status; |
| 546 | |
| 547 | status = pjsip_timer_process_resp(inv, rdata, &st_code); |
| 548 | if (status != PJ_SUCCESS && end_sess_on_failure) { |
| 549 | pjsip_tx_data *tdata; |
| 550 | pj_status_t status2; |
| 551 | |
| 552 | status2 = pjsip_inv_end_session(inv, st_code, NULL, &tdata); |
| 553 | if (tdata && status2 == PJ_SUCCESS) |
| 554 | pjsip_inv_send_msg(inv, tdata); |
| 555 | } |
| 556 | |
| 557 | return status; |
| 558 | } |
| 559 | |
| 560 | /* |
| 561 | * Module on_rx_response(). |
| 562 | * |
| 563 | * This callback is called for these events: |
| 564 | * - dialog distributes incoming 2xx response to INVITE (outside |
| 565 | * transaction) to its usages. |
| 566 | * - endpoint distributes strayed responses. |
| 567 | */ |
| 568 | static pj_bool_t mod_inv_on_rx_response(pjsip_rx_data *rdata) |
| 569 | { |
| 570 | pjsip_dialog *dlg; |
| 571 | pjsip_inv_session *inv; |
| 572 | pjsip_msg *msg = rdata->msg_info.msg; |
| 573 | |
| 574 | dlg = pjsip_rdata_get_dlg(rdata); |
| 575 | |
| 576 | /* Ignore responses outside dialog */ |
| 577 | if (dlg == NULL) |
| 578 | return PJ_FALSE; |
| 579 | |
| 580 | /* Ignore responses not belonging to invite session */ |
| 581 | inv = pjsip_dlg_get_inv_session(dlg); |
| 582 | if (inv == NULL) |
| 583 | return PJ_FALSE; |
| 584 | |
| 585 | /* This MAY be retransmission of 2xx response to INVITE. |
| 586 | * If it is, we need to send ACK. |
| 587 | */ |
| 588 | if (msg->type == PJSIP_RESPONSE_MSG && msg->line.status.code/100==2 && |
| 589 | rdata->msg_info.cseq->method.id == PJSIP_INVITE_METHOD && |
| 590 | inv->invite_tsx == NULL) |
| 591 | { |
| 592 | pjsip_event e; |
| 593 | |
| 594 | PJSIP_EVENT_INIT_RX_MSG(e, rdata); |
| 595 | inv_send_ack(inv, &e); |
| 596 | return PJ_TRUE; |
| 597 | |
| 598 | } |
| 599 | |
| 600 | /* No other processing needs to be done here. */ |
| 601 | return PJ_FALSE; |
| 602 | } |
| 603 | |
| 604 | /* |
| 605 | * Module on_tsx_state() |
| 606 | * |
| 607 | * This callback is called by dialog framework for all transactions |
| 608 | * inside the dialog for all its dialog usages. |
| 609 | */ |
| 610 | static void mod_inv_on_tsx_state(pjsip_transaction *tsx, pjsip_event *e) |
| 611 | { |
| 612 | pjsip_dialog *dlg; |
| 613 | pjsip_inv_session *inv; |
| 614 | |
| 615 | dlg = pjsip_tsx_get_dlg(tsx); |
| 616 | if (dlg == NULL) |
| 617 | return; |
| 618 | |
| 619 | inv = pjsip_dlg_get_inv_session(dlg); |
| 620 | if (inv == NULL) |
| 621 | return; |
| 622 | |
| 623 | /* Call state handler for the invite session. */ |
| 624 | (*inv_state_handler[inv->state])(inv, e); |
| 625 | |
| 626 | /* Call on_tsx_state. CANCEL request is a special case and has been |
| 627 | * reported earlier in inv_respond_incoming_cancel() |
| 628 | */ |
| 629 | if (mod_inv.cb.on_tsx_state_changed && inv->notify && |
| 630 | !(tsx->method.id==PJSIP_CANCEL_METHOD && |
| 631 | e->body.tsx_state.type==PJSIP_EVENT_RX_MSG)) |
| 632 | { |
| 633 | (*mod_inv.cb.on_tsx_state_changed)(inv, tsx, e); |
| 634 | } |
| 635 | |
| 636 | /* Clear invite transaction when tsx is confirmed. |
| 637 | * Previously we set invite_tsx to NULL only when transaction has |
| 638 | * terminated, but this didn't work when ACK has the same Via branch |
| 639 | * value as the INVITE (see http://www.pjsip.org/trac/ticket/113) |
| 640 | */ |
| 641 | if (tsx->state>=PJSIP_TSX_STATE_CONFIRMED && tsx == inv->invite_tsx) { |
| 642 | inv->invite_tsx = NULL; |
| 643 | if (inv->last_answer) { |
| 644 | pjsip_tx_data_dec_ref(inv->last_answer); |
| 645 | inv->last_answer = NULL; |
| 646 | } |
| 647 | } |
| 648 | } |
| 649 | |
| 650 | |
| 651 | /* |
| 652 | * Initialize the invite module. |
| 653 | */ |
| 654 | PJ_DEF(pj_status_t) pjsip_inv_usage_init( pjsip_endpoint *endpt, |
| 655 | const pjsip_inv_callback *cb) |
| 656 | { |
| 657 | pj_status_t status; |
| 658 | |
| 659 | /* Check arguments. */ |
| 660 | PJ_ASSERT_RETURN(endpt && cb, PJ_EINVAL); |
| 661 | |
| 662 | /* Some callbacks are mandatory */ |
| 663 | PJ_ASSERT_RETURN(cb->on_state_changed && cb->on_new_session, PJ_EINVAL); |
| 664 | |
| 665 | /* Check if module already registered. */ |
| 666 | PJ_ASSERT_RETURN(mod_inv.mod.id == -1, PJ_EINVALIDOP); |
| 667 | |
| 668 | /* Copy param. */ |
| 669 | pj_memcpy(&mod_inv.cb, cb, sizeof(pjsip_inv_callback)); |
| 670 | |
| 671 | mod_inv.endpt = endpt; |
| 672 | |
| 673 | /* Register the module. */ |
| 674 | status = pjsip_endpt_register_module(endpt, &mod_inv.mod); |
| 675 | if (status != PJ_SUCCESS) |
| 676 | return status; |
| 677 | |
| 678 | return PJ_SUCCESS; |
| 679 | } |
| 680 | |
| 681 | /* |
| 682 | * Get the instance of invite module. |
| 683 | */ |
| 684 | PJ_DEF(pjsip_module*) pjsip_inv_usage_instance(void) |
| 685 | { |
| 686 | return &mod_inv.mod; |
| 687 | } |
| 688 | |
| 689 | |
| 690 | |
| 691 | /* |
| 692 | * Return the invite session for the specified dialog. |
| 693 | */ |
| 694 | PJ_DEF(pjsip_inv_session*) pjsip_dlg_get_inv_session(pjsip_dialog *dlg) |
| 695 | { |
| 696 | return (pjsip_inv_session*) dlg->mod_data[mod_inv.mod.id]; |
| 697 | } |
| 698 | |
| 699 | |
| 700 | /* |
| 701 | * Get INVITE state name. |
| 702 | */ |
| 703 | PJ_DEF(const char *) pjsip_inv_state_name(pjsip_inv_state state) |
| 704 | { |
| 705 | PJ_ASSERT_RETURN(state >= PJSIP_INV_STATE_NULL && |
| 706 | state <= PJSIP_INV_STATE_DISCONNECTED, |
| 707 | "??"); |
| 708 | |
| 709 | return inv_state_names[state]; |
| 710 | } |
| 711 | |
| 712 | /* |
| 713 | * Create UAC invite session. |
| 714 | */ |
| 715 | PJ_DEF(pj_status_t) pjsip_inv_create_uac( pjsip_dialog *dlg, |
| 716 | const pjmedia_sdp_session *local_sdp, |
| 717 | unsigned options, |
| 718 | pjsip_inv_session **p_inv) |
| 719 | { |
| 720 | pjsip_inv_session *inv; |
| 721 | pj_status_t status; |
| 722 | |
| 723 | /* Verify arguments. */ |
| 724 | PJ_ASSERT_RETURN(dlg && p_inv, PJ_EINVAL); |
| 725 | |
| 726 | /* Must lock dialog first */ |
| 727 | pjsip_dlg_inc_lock(dlg); |
| 728 | |
| 729 | /* Normalize options */ |
| 730 | if (options & PJSIP_INV_REQUIRE_100REL) |
| 731 | options |= PJSIP_INV_SUPPORT_100REL; |
| 732 | if (options & PJSIP_INV_REQUIRE_TIMER) |
| 733 | options |= PJSIP_INV_SUPPORT_TIMER; |
| 734 | |
| 735 | /* Create the session */ |
| 736 | inv = PJ_POOL_ZALLOC_T(dlg->pool, pjsip_inv_session); |
| 737 | pj_assert(inv != NULL); |
| 738 | |
| 739 | inv->pool = dlg->pool; |
| 740 | inv->role = PJSIP_ROLE_UAC; |
| 741 | inv->state = PJSIP_INV_STATE_NULL; |
| 742 | inv->dlg = dlg; |
| 743 | inv->options = options; |
| 744 | inv->notify = PJ_TRUE; |
| 745 | inv->cause = (pjsip_status_code) 0; |
| 746 | |
| 747 | /* Create flip-flop pool (see ticket #877) */ |
| 748 | /* (using inv->obj_name as temporary variable for pool names */ |
| 749 | pj_ansi_snprintf(inv->obj_name, PJ_MAX_OBJ_NAME, "inv%p", dlg->pool); |
| 750 | inv->pool_prov = pjsip_endpt_create_pool(dlg->endpt, inv->obj_name, |
| 751 | POOL_INIT_SIZE, POOL_INC_SIZE); |
| 752 | inv->pool_active = pjsip_endpt_create_pool(dlg->endpt, inv->obj_name, |
| 753 | POOL_INIT_SIZE, POOL_INC_SIZE); |
| 754 | |
| 755 | /* Object name will use the same dialog pointer. */ |
| 756 | pj_ansi_snprintf(inv->obj_name, PJ_MAX_OBJ_NAME, "inv%p", dlg); |
| 757 | |
| 758 | /* Create negotiator if local_sdp is specified. */ |
| 759 | if (local_sdp) { |
| 760 | status = pjmedia_sdp_neg_create_w_local_offer(inv->pool, |
| 761 | local_sdp, &inv->neg); |
| 762 | if (status != PJ_SUCCESS) { |
| 763 | pjsip_dlg_dec_lock(dlg); |
| 764 | return status; |
| 765 | } |
| 766 | } |
| 767 | |
| 768 | /* Register invite as dialog usage. */ |
| 769 | status = pjsip_dlg_add_usage(dlg, &mod_inv.mod, inv); |
| 770 | if (status != PJ_SUCCESS) { |
| 771 | pjsip_dlg_dec_lock(dlg); |
| 772 | return status; |
| 773 | } |
| 774 | |
| 775 | /* Increment dialog session */ |
| 776 | pjsip_dlg_inc_session(dlg, &mod_inv.mod); |
| 777 | |
| 778 | /* Create 100rel handler */ |
| 779 | pjsip_100rel_attach(inv); |
| 780 | |
| 781 | /* Done */ |
| 782 | *p_inv = inv; |
| 783 | |
| 784 | pjsip_dlg_dec_lock(dlg); |
| 785 | |
| 786 | PJ_LOG(5,(inv->obj_name, "UAC invite session created for dialog %s", |
| 787 | dlg->obj_name)); |
| 788 | |
| 789 | return PJ_SUCCESS; |
| 790 | } |
| 791 | |
| 792 | PJ_DEF(pjsip_rdata_sdp_info*) pjsip_rdata_get_sdp_info(pjsip_rx_data *rdata) |
| 793 | { |
| 794 | pjsip_rdata_sdp_info *sdp_info; |
| 795 | pjsip_msg_body *body = rdata->msg_info.msg->body; |
| 796 | pjsip_ctype_hdr *ctype_hdr = rdata->msg_info.ctype; |
| 797 | pjsip_media_type app_sdp; |
| 798 | |
| 799 | sdp_info = (pjsip_rdata_sdp_info*) |
| 800 | rdata->endpt_info.mod_data[mod_inv.mod.id]; |
| 801 | if (sdp_info) |
| 802 | return sdp_info; |
| 803 | |
| 804 | sdp_info = PJ_POOL_ZALLOC_T(rdata->tp_info.pool, |
| 805 | pjsip_rdata_sdp_info); |
| 806 | PJ_ASSERT_RETURN(mod_inv.mod.id >= 0, sdp_info); |
| 807 | rdata->endpt_info.mod_data[mod_inv.mod.id] = sdp_info; |
| 808 | |
| 809 | pjsip_media_type_init2(&app_sdp, "application", "sdp"); |
| 810 | |
| 811 | if (body && ctype_hdr && |
| 812 | pj_stricmp(&ctype_hdr->media.type, &app_sdp.type)==0 && |
| 813 | pj_stricmp(&ctype_hdr->media.subtype, &app_sdp.subtype)==0) |
| 814 | { |
| 815 | sdp_info->body.ptr = (char*)body->data; |
| 816 | sdp_info->body.slen = body->len; |
| 817 | } else if (body && ctype_hdr && |
| 818 | pj_stricmp2(&ctype_hdr->media.type, "multipart")==0 && |
| 819 | (pj_stricmp2(&ctype_hdr->media.subtype, "mixed")==0 || |
| 820 | pj_stricmp2(&ctype_hdr->media.subtype, "alternative")==0)) |
| 821 | { |
| 822 | pjsip_multipart_part *part; |
| 823 | |
| 824 | part = pjsip_multipart_find_part(body, &app_sdp, NULL); |
| 825 | if (part) { |
| 826 | sdp_info->body.ptr = (char*)part->body->data; |
| 827 | sdp_info->body.slen = part->body->len; |
| 828 | } |
| 829 | } |
| 830 | |
| 831 | if (sdp_info->body.ptr) { |
| 832 | pj_status_t status; |
| 833 | status = pjmedia_sdp_parse(rdata->tp_info.pool, |
| 834 | sdp_info->body.ptr, |
| 835 | sdp_info->body.slen, |
| 836 | &sdp_info->sdp); |
| 837 | if (status == PJ_SUCCESS) |
| 838 | status = pjmedia_sdp_validate2(sdp_info->sdp, PJ_FALSE); |
| 839 | |
| 840 | if (status != PJ_SUCCESS) { |
| 841 | sdp_info->sdp = NULL; |
| 842 | PJ_PERROR(1,(THIS_FILE, status, |
| 843 | "Error parsing/validating SDP body")); |
| 844 | } |
| 845 | |
| 846 | sdp_info->sdp_err = status; |
| 847 | } |
| 848 | |
| 849 | return sdp_info; |
| 850 | } |
| 851 | |
| 852 | |
| 853 | /* |
| 854 | * Verify incoming INVITE request. |
| 855 | */ |
| 856 | PJ_DEF(pj_status_t) pjsip_inv_verify_request3(pjsip_rx_data *rdata, |
| 857 | pj_pool_t *tmp_pool, |
| 858 | unsigned *options, |
| 859 | const pjmedia_sdp_session *r_sdp, |
| 860 | const pjmedia_sdp_session *l_sdp, |
| 861 | pjsip_dialog *dlg, |
| 862 | pjsip_endpoint *endpt, |
| 863 | pjsip_tx_data **p_tdata) |
| 864 | { |
| 865 | pjsip_msg *msg = NULL; |
| 866 | pjsip_allow_hdr *allow = NULL; |
| 867 | pjsip_supported_hdr *sup_hdr = NULL; |
| 868 | pjsip_require_hdr *req_hdr = NULL; |
| 869 | pjsip_contact_hdr *c_hdr = NULL; |
| 870 | int code = 200; |
| 871 | unsigned rem_option = 0; |
| 872 | pj_status_t status = PJ_SUCCESS; |
| 873 | pjsip_hdr res_hdr_list; |
| 874 | pjsip_rdata_sdp_info *sdp_info; |
| 875 | |
| 876 | /* Init return arguments. */ |
| 877 | if (p_tdata) *p_tdata = NULL; |
| 878 | |
| 879 | /* Verify arguments. */ |
| 880 | PJ_ASSERT_RETURN(tmp_pool != NULL && options != NULL, PJ_EINVAL); |
| 881 | |
| 882 | /* Normalize options */ |
| 883 | if (*options & PJSIP_INV_REQUIRE_100REL) |
| 884 | *options |= PJSIP_INV_SUPPORT_100REL; |
| 885 | if (*options & PJSIP_INV_REQUIRE_TIMER) |
| 886 | *options |= PJSIP_INV_SUPPORT_TIMER; |
| 887 | if (*options & PJSIP_INV_REQUIRE_ICE) |
| 888 | *options |= PJSIP_INV_SUPPORT_ICE; |
| 889 | |
| 890 | if (rdata) { |
| 891 | /* Get the message in rdata */ |
| 892 | msg = rdata->msg_info.msg; |
| 893 | |
| 894 | /* Must be INVITE request. */ |
| 895 | PJ_ASSERT_RETURN(msg && msg->type == PJSIP_REQUEST_MSG && |
| 896 | msg->line.req.method.id == PJSIP_INVITE_METHOD, |
| 897 | PJ_EINVAL); |
| 898 | } |
| 899 | |
| 900 | /* If tdata is specified, then either dlg or endpt must be specified */ |
| 901 | PJ_ASSERT_RETURN((!p_tdata) || (endpt || dlg), PJ_EINVAL); |
| 902 | |
| 903 | /* Get the endpoint */ |
| 904 | endpt = endpt ? endpt : dlg->endpt; |
| 905 | |
| 906 | /* Init response header list */ |
| 907 | pj_list_init(&res_hdr_list); |
| 908 | |
| 909 | /* Check the Contact header */ |
| 910 | if (msg) { |
| 911 | c_hdr = (pjsip_contact_hdr*) |
| 912 | pjsip_msg_find_hdr(msg, PJSIP_H_CONTACT, NULL); |
| 913 | } |
| 914 | if (msg && (!c_hdr || !c_hdr->uri)) { |
| 915 | /* Missing Contact header or Contact contains "*" */ |
| 916 | pjsip_warning_hdr *w; |
| 917 | pj_str_t warn_text; |
| 918 | |
| 919 | warn_text = pj_str("Bad/missing Contact header"); |
| 920 | w = pjsip_warning_hdr_create(tmp_pool, 399, |
| 921 | pjsip_endpt_name(endpt), |
| 922 | &warn_text); |
| 923 | if (w) { |
| 924 | pj_list_push_back(&res_hdr_list, w); |
| 925 | } |
| 926 | |
| 927 | code = PJSIP_SC_BAD_REQUEST; |
| 928 | status = PJSIP_ERRNO_FROM_SIP_STATUS(code); |
| 929 | goto on_return; |
| 930 | } |
| 931 | |
| 932 | /* Check the request body, see if it's something that we support, |
| 933 | * only when the body hasn't been parsed before. |
| 934 | */ |
| 935 | if (r_sdp == NULL && rdata) { |
| 936 | sdp_info = pjsip_rdata_get_sdp_info(rdata); |
| 937 | } else { |
| 938 | sdp_info = NULL; |
| 939 | } |
| 940 | |
| 941 | if (r_sdp==NULL && msg && msg->body) { |
| 942 | |
| 943 | /* Check if body really contains SDP. */ |
| 944 | if (sdp_info->body.ptr == NULL) { |
| 945 | /* Couldn't find "application/sdp" */ |
| 946 | code = PJSIP_SC_UNSUPPORTED_MEDIA_TYPE; |
| 947 | status = PJSIP_ERRNO_FROM_SIP_STATUS(code); |
| 948 | |
| 949 | if (p_tdata) { |
| 950 | /* Add Accept header to response */ |
| 951 | pjsip_accept_hdr *acc; |
| 952 | |
| 953 | acc = pjsip_accept_hdr_create(tmp_pool); |
| 954 | PJ_ASSERT_RETURN(acc, PJ_ENOMEM); |
| 955 | acc->values[acc->count++] = pj_str("application/sdp"); |
| 956 | pj_list_push_back(&res_hdr_list, acc); |
| 957 | } |
| 958 | |
| 959 | goto on_return; |
| 960 | } |
| 961 | |
| 962 | if (sdp_info->sdp_err != PJ_SUCCESS) { |
| 963 | /* Unparseable or invalid SDP */ |
| 964 | code = PJSIP_SC_BAD_REQUEST; |
| 965 | |
| 966 | if (p_tdata) { |
| 967 | /* Add Warning header. */ |
| 968 | pjsip_warning_hdr *w; |
| 969 | |
| 970 | w = pjsip_warning_hdr_create_from_status(tmp_pool, |
| 971 | pjsip_endpt_name(endpt), |
| 972 | sdp_info->sdp_err); |
| 973 | PJ_ASSERT_RETURN(w, PJ_ENOMEM); |
| 974 | |
| 975 | pj_list_push_back(&res_hdr_list, w); |
| 976 | } |
| 977 | |
| 978 | goto on_return; |
| 979 | } |
| 980 | |
| 981 | r_sdp = sdp_info->sdp; |
| 982 | } |
| 983 | |
| 984 | if (r_sdp) { |
| 985 | /* Negotiate with local SDP */ |
| 986 | if (l_sdp) { |
| 987 | pjmedia_sdp_neg *neg; |
| 988 | |
| 989 | /* Local SDP must be valid! */ |
| 990 | PJ_ASSERT_RETURN((status=pjmedia_sdp_validate(l_sdp))==PJ_SUCCESS, |
| 991 | status); |
| 992 | |
| 993 | /* Create SDP negotiator */ |
| 994 | status = pjmedia_sdp_neg_create_w_remote_offer( |
| 995 | tmp_pool, l_sdp, r_sdp, &neg); |
| 996 | PJ_ASSERT_RETURN(status == PJ_SUCCESS, status); |
| 997 | |
| 998 | /* Negotiate SDP */ |
| 999 | status = pjmedia_sdp_neg_negotiate(tmp_pool, neg, 0); |
| 1000 | if (status != PJ_SUCCESS) { |
| 1001 | |
| 1002 | /* Incompatible media */ |
| 1003 | code = PJSIP_SC_NOT_ACCEPTABLE_HERE; |
| 1004 | |
| 1005 | if (p_tdata) { |
| 1006 | pjsip_accept_hdr *acc; |
| 1007 | pjsip_warning_hdr *w; |
| 1008 | |
| 1009 | /* Add Warning header. */ |
| 1010 | w = pjsip_warning_hdr_create_from_status( |
| 1011 | tmp_pool, |
| 1012 | pjsip_endpt_name(endpt), status); |
| 1013 | PJ_ASSERT_RETURN(w, PJ_ENOMEM); |
| 1014 | |
| 1015 | pj_list_push_back(&res_hdr_list, w); |
| 1016 | |
| 1017 | /* Add Accept header to response */ |
| 1018 | acc = pjsip_accept_hdr_create(tmp_pool); |
| 1019 | PJ_ASSERT_RETURN(acc, PJ_ENOMEM); |
| 1020 | acc->values[acc->count++] = pj_str("application/sdp"); |
| 1021 | pj_list_push_back(&res_hdr_list, acc); |
| 1022 | |
| 1023 | } |
| 1024 | |
| 1025 | goto on_return; |
| 1026 | } |
| 1027 | } |
| 1028 | } |
| 1029 | |
| 1030 | /* Check supported methods, see if peer supports UPDATE. |
| 1031 | * We just assume that peer supports standard INVITE, ACK, CANCEL, and BYE |
| 1032 | * implicitly by sending this INVITE. |
| 1033 | */ |
| 1034 | if (msg) { |
| 1035 | allow = (pjsip_allow_hdr*) pjsip_msg_find_hdr(msg, PJSIP_H_ALLOW, |
| 1036 | NULL); |
| 1037 | } |
| 1038 | if (allow) { |
| 1039 | unsigned i; |
| 1040 | const pj_str_t STR_UPDATE = { "UPDATE", 6 }; |
| 1041 | |
| 1042 | for (i=0; i<allow->count; ++i) { |
| 1043 | if (pj_stricmp(&allow->values[i], &STR_UPDATE)==0) |
| 1044 | break; |
| 1045 | } |
| 1046 | |
| 1047 | if (i != allow->count) { |
| 1048 | /* UPDATE is present in Allow */ |
| 1049 | rem_option |= PJSIP_INV_SUPPORT_UPDATE; |
| 1050 | } |
| 1051 | |
| 1052 | } |
| 1053 | |
| 1054 | /* Check Supported header */ |
| 1055 | if (msg) { |
| 1056 | sup_hdr = (pjsip_supported_hdr*) |
| 1057 | pjsip_msg_find_hdr(msg, PJSIP_H_SUPPORTED, NULL); |
| 1058 | } |
| 1059 | if (sup_hdr) { |
| 1060 | unsigned i; |
| 1061 | const pj_str_t STR_100REL = { "100rel", 6}; |
| 1062 | const pj_str_t STR_TIMER = { "timer", 5}; |
| 1063 | const pj_str_t STR_ICE = { "ice", 3 }; |
| 1064 | |
| 1065 | for (i=0; i<sup_hdr->count; ++i) { |
| 1066 | if (pj_stricmp(&sup_hdr->values[i], &STR_100REL)==0) |
| 1067 | rem_option |= PJSIP_INV_SUPPORT_100REL; |
| 1068 | else if (pj_stricmp(&sup_hdr->values[i], &STR_TIMER)==0) |
| 1069 | rem_option |= PJSIP_INV_SUPPORT_TIMER; |
| 1070 | else if (pj_stricmp(&sup_hdr->values[i], &STR_ICE)==0) |
| 1071 | rem_option |= PJSIP_INV_SUPPORT_ICE; |
| 1072 | } |
| 1073 | } |
| 1074 | |
| 1075 | /* Check Require header */ |
| 1076 | if (msg) { |
| 1077 | req_hdr = (pjsip_require_hdr*) |
| 1078 | pjsip_msg_find_hdr(msg, PJSIP_H_REQUIRE, NULL); |
| 1079 | } |
| 1080 | if (req_hdr) { |
| 1081 | unsigned i; |
| 1082 | const pj_str_t STR_100REL = { "100rel", 6}; |
| 1083 | const pj_str_t STR_REPLACES = { "replaces", 8 }; |
| 1084 | const pj_str_t STR_TIMER = { "timer", 5 }; |
| 1085 | const pj_str_t STR_ICE = { "ice", 3 }; |
| 1086 | unsigned unsupp_cnt = 0; |
| 1087 | pj_str_t unsupp_tags[PJSIP_GENERIC_ARRAY_MAX_COUNT]; |
| 1088 | |
| 1089 | for (i=0; i<req_hdr->count; ++i) { |
| 1090 | if ((*options & PJSIP_INV_SUPPORT_100REL) && |
| 1091 | pj_stricmp(&req_hdr->values[i], &STR_100REL)==0) |
| 1092 | { |
| 1093 | rem_option |= PJSIP_INV_REQUIRE_100REL; |
| 1094 | |
| 1095 | } else if ((*options & PJSIP_INV_SUPPORT_TIMER) && |
| 1096 | pj_stricmp(&req_hdr->values[i], &STR_TIMER)==0) |
| 1097 | { |
| 1098 | rem_option |= PJSIP_INV_REQUIRE_TIMER; |
| 1099 | |
| 1100 | } else if (pj_stricmp(&req_hdr->values[i], &STR_REPLACES)==0) { |
| 1101 | pj_bool_t supp; |
| 1102 | |
| 1103 | supp = pjsip_endpt_has_capability(endpt, PJSIP_H_SUPPORTED, |
| 1104 | NULL, &STR_REPLACES); |
| 1105 | if (!supp) |
| 1106 | unsupp_tags[unsupp_cnt++] = req_hdr->values[i]; |
| 1107 | } else if ((*options & PJSIP_INV_SUPPORT_ICE) && |
| 1108 | pj_stricmp(&req_hdr->values[i], &STR_ICE)==0) |
| 1109 | { |
| 1110 | rem_option |= PJSIP_INV_REQUIRE_ICE; |
| 1111 | |
| 1112 | } else if (!pjsip_endpt_has_capability(endpt, PJSIP_H_SUPPORTED, |
| 1113 | NULL, &req_hdr->values[i])) |
| 1114 | { |
| 1115 | /* Unknown/unsupported extension tag! */ |
| 1116 | unsupp_tags[unsupp_cnt++] = req_hdr->values[i]; |
| 1117 | } |
| 1118 | } |
| 1119 | |
| 1120 | /* Check if there are required tags that we don't support */ |
| 1121 | if (unsupp_cnt) { |
| 1122 | |
| 1123 | code = PJSIP_SC_BAD_EXTENSION; |
| 1124 | status = PJSIP_ERRNO_FROM_SIP_STATUS(code); |
| 1125 | |
| 1126 | if (p_tdata) { |
| 1127 | pjsip_unsupported_hdr *unsupp_hdr; |
| 1128 | const pjsip_hdr *h; |
| 1129 | |
| 1130 | /* Add Unsupported header. */ |
| 1131 | unsupp_hdr = pjsip_unsupported_hdr_create(tmp_pool); |
| 1132 | PJ_ASSERT_RETURN(unsupp_hdr != NULL, PJ_ENOMEM); |
| 1133 | |
| 1134 | unsupp_hdr->count = unsupp_cnt; |
| 1135 | for (i=0; i<unsupp_cnt; ++i) |
| 1136 | unsupp_hdr->values[i] = unsupp_tags[i]; |
| 1137 | |
| 1138 | pj_list_push_back(&res_hdr_list, unsupp_hdr); |
| 1139 | |
| 1140 | /* Add Supported header. */ |
| 1141 | h = pjsip_endpt_get_capability(endpt, PJSIP_H_SUPPORTED, |
| 1142 | NULL); |
| 1143 | pj_assert(h); |
| 1144 | if (h) { |
| 1145 | sup_hdr = (pjsip_supported_hdr*) |
| 1146 | pjsip_hdr_clone(tmp_pool, h); |
| 1147 | pj_list_push_back(&res_hdr_list, sup_hdr); |
| 1148 | } |
| 1149 | } |
| 1150 | |
| 1151 | goto on_return; |
| 1152 | } |
| 1153 | } |
| 1154 | |
| 1155 | /* Check if there are local requirements that are not supported |
| 1156 | * by peer. |
| 1157 | */ |
| 1158 | if ( msg && (((*options & PJSIP_INV_REQUIRE_100REL)!=0 && |
| 1159 | (rem_option & PJSIP_INV_SUPPORT_100REL)==0) || |
| 1160 | ((*options & PJSIP_INV_REQUIRE_TIMER)!=0 && |
| 1161 | (rem_option & PJSIP_INV_SUPPORT_TIMER)==0))) |
| 1162 | { |
| 1163 | code = PJSIP_SC_EXTENSION_REQUIRED; |
| 1164 | status = PJSIP_ERRNO_FROM_SIP_STATUS(code); |
| 1165 | |
| 1166 | if (p_tdata) { |
| 1167 | const pjsip_hdr *h; |
| 1168 | |
| 1169 | /* Add Require header. */ |
| 1170 | req_hdr = pjsip_require_hdr_create(tmp_pool); |
| 1171 | PJ_ASSERT_RETURN(req_hdr != NULL, PJ_ENOMEM); |
| 1172 | |
| 1173 | if (*options & PJSIP_INV_REQUIRE_100REL) |
| 1174 | req_hdr->values[req_hdr->count++] = pj_str("100rel"); |
| 1175 | if (*options & PJSIP_INV_REQUIRE_TIMER) |
| 1176 | req_hdr->values[req_hdr->count++] = pj_str("timer"); |
| 1177 | |
| 1178 | pj_list_push_back(&res_hdr_list, req_hdr); |
| 1179 | |
| 1180 | /* Add Supported header. */ |
| 1181 | h = pjsip_endpt_get_capability(endpt, PJSIP_H_SUPPORTED, |
| 1182 | NULL); |
| 1183 | pj_assert(h); |
| 1184 | if (h) { |
| 1185 | sup_hdr = (pjsip_supported_hdr*) |
| 1186 | pjsip_hdr_clone(tmp_pool, h); |
| 1187 | pj_list_push_back(&res_hdr_list, sup_hdr); |
| 1188 | } |
| 1189 | |
| 1190 | } |
| 1191 | |
| 1192 | goto on_return; |
| 1193 | } |
| 1194 | |
| 1195 | /* If remote Require something that we support, make us Require |
| 1196 | * that feature too. |
| 1197 | */ |
| 1198 | if (rem_option & PJSIP_INV_REQUIRE_100REL) { |
| 1199 | pj_assert(*options & PJSIP_INV_SUPPORT_100REL); |
| 1200 | *options |= PJSIP_INV_REQUIRE_100REL; |
| 1201 | } |
| 1202 | if (rem_option & PJSIP_INV_REQUIRE_TIMER) { |
| 1203 | pj_assert(*options & PJSIP_INV_SUPPORT_TIMER); |
| 1204 | *options |= PJSIP_INV_REQUIRE_TIMER; |
| 1205 | } |
| 1206 | |
| 1207 | on_return: |
| 1208 | |
| 1209 | /* Create response if necessary */ |
| 1210 | if (code != 200 && p_tdata) { |
| 1211 | pjsip_tx_data *tdata; |
| 1212 | const pjsip_hdr *h; |
| 1213 | |
| 1214 | if (!rdata) { |
| 1215 | return PJSIP_ERRNO_FROM_SIP_STATUS(code); |
| 1216 | } |
| 1217 | |
| 1218 | if (dlg) { |
| 1219 | status = pjsip_dlg_create_response(dlg, rdata, code, NULL, |
| 1220 | &tdata); |
| 1221 | } else { |
| 1222 | status = pjsip_endpt_create_response(endpt, rdata, code, NULL, |
| 1223 | &tdata); |
| 1224 | } |
| 1225 | |
| 1226 | if (status != PJ_SUCCESS) |
| 1227 | return status; |
| 1228 | |
| 1229 | /* Add response headers. */ |
| 1230 | h = res_hdr_list.next; |
| 1231 | while (h != &res_hdr_list) { |
| 1232 | pjsip_hdr *cloned; |
| 1233 | |
| 1234 | cloned = (pjsip_hdr*) pjsip_hdr_clone(tdata->pool, h); |
| 1235 | PJ_ASSERT_RETURN(cloned, PJ_ENOMEM); |
| 1236 | |
| 1237 | pjsip_msg_add_hdr(tdata->msg, cloned); |
| 1238 | |
| 1239 | h = h->next; |
| 1240 | } |
| 1241 | |
| 1242 | *p_tdata = tdata; |
| 1243 | |
| 1244 | /* Can not return PJ_SUCCESS when response message is produced. |
| 1245 | * Ref: PROTOS test ~#2490 |
| 1246 | */ |
| 1247 | if (status == PJ_SUCCESS) |
| 1248 | status = PJSIP_ERRNO_FROM_SIP_STATUS(code); |
| 1249 | |
| 1250 | } |
| 1251 | |
| 1252 | return status; |
| 1253 | } |
| 1254 | |
| 1255 | |
| 1256 | /* |
| 1257 | * Verify incoming INVITE request. |
| 1258 | */ |
| 1259 | PJ_DEF(pj_status_t) pjsip_inv_verify_request2(pjsip_rx_data *rdata, |
| 1260 | unsigned *options, |
| 1261 | const pjmedia_sdp_session *r_sdp, |
| 1262 | const pjmedia_sdp_session *l_sdp, |
| 1263 | pjsip_dialog *dlg, |
| 1264 | pjsip_endpoint *endpt, |
| 1265 | pjsip_tx_data **p_tdata) |
| 1266 | { |
| 1267 | return pjsip_inv_verify_request3(rdata, rdata->tp_info.pool, |
| 1268 | options, r_sdp, l_sdp, dlg, |
| 1269 | endpt, p_tdata); |
| 1270 | } |
| 1271 | |
| 1272 | |
| 1273 | /* |
| 1274 | * Verify incoming INVITE request. |
| 1275 | */ |
| 1276 | PJ_DEF(pj_status_t) pjsip_inv_verify_request( pjsip_rx_data *rdata, |
| 1277 | unsigned *options, |
| 1278 | const pjmedia_sdp_session *l_sdp, |
| 1279 | pjsip_dialog *dlg, |
| 1280 | pjsip_endpoint *endpt, |
| 1281 | pjsip_tx_data **p_tdata) |
| 1282 | { |
| 1283 | return pjsip_inv_verify_request3(rdata, rdata->tp_info.pool, |
| 1284 | options, NULL, l_sdp, dlg, |
| 1285 | endpt, p_tdata); |
| 1286 | } |
| 1287 | |
| 1288 | /* |
| 1289 | * Create UAS invite session. |
| 1290 | */ |
| 1291 | PJ_DEF(pj_status_t) pjsip_inv_create_uas( pjsip_dialog *dlg, |
| 1292 | pjsip_rx_data *rdata, |
| 1293 | const pjmedia_sdp_session *local_sdp, |
| 1294 | unsigned options, |
| 1295 | pjsip_inv_session **p_inv) |
| 1296 | { |
| 1297 | pjsip_inv_session *inv; |
| 1298 | struct tsx_inv_data *tsx_inv_data; |
| 1299 | pjsip_msg *msg; |
| 1300 | pjsip_rdata_sdp_info *sdp_info; |
| 1301 | pj_status_t status; |
| 1302 | |
| 1303 | /* Verify arguments. */ |
| 1304 | PJ_ASSERT_RETURN(dlg && rdata && p_inv, PJ_EINVAL); |
| 1305 | |
| 1306 | /* Dialog MUST have been initialised. */ |
| 1307 | PJ_ASSERT_RETURN(pjsip_rdata_get_tsx(rdata) != NULL, PJ_EINVALIDOP); |
| 1308 | |
| 1309 | msg = rdata->msg_info.msg; |
| 1310 | |
| 1311 | /* rdata MUST contain INVITE request */ |
| 1312 | PJ_ASSERT_RETURN(msg->type == PJSIP_REQUEST_MSG && |
| 1313 | msg->line.req.method.id == PJSIP_INVITE_METHOD, |
| 1314 | PJ_EINVALIDOP); |
| 1315 | |
| 1316 | /* Lock dialog */ |
| 1317 | pjsip_dlg_inc_lock(dlg); |
| 1318 | |
| 1319 | /* Normalize options */ |
| 1320 | if (options & PJSIP_INV_REQUIRE_100REL) |
| 1321 | options |= PJSIP_INV_SUPPORT_100REL; |
| 1322 | if (options & PJSIP_INV_REQUIRE_TIMER) |
| 1323 | options |= PJSIP_INV_SUPPORT_TIMER; |
| 1324 | |
| 1325 | /* Create the session */ |
| 1326 | inv = PJ_POOL_ZALLOC_T(dlg->pool, pjsip_inv_session); |
| 1327 | pj_assert(inv != NULL); |
| 1328 | |
| 1329 | inv->pool = dlg->pool; |
| 1330 | inv->role = PJSIP_ROLE_UAS; |
| 1331 | inv->state = PJSIP_INV_STATE_NULL; |
| 1332 | inv->dlg = dlg; |
| 1333 | inv->options = options; |
| 1334 | inv->notify = PJ_TRUE; |
| 1335 | inv->cause = (pjsip_status_code) 0; |
| 1336 | |
| 1337 | /* Create flip-flop pool (see ticket #877) */ |
| 1338 | /* (using inv->obj_name as temporary variable for pool names */ |
| 1339 | pj_ansi_snprintf(inv->obj_name, PJ_MAX_OBJ_NAME, "inv%p", dlg->pool); |
| 1340 | inv->pool_prov = pjsip_endpt_create_pool(dlg->endpt, inv->obj_name, |
| 1341 | POOL_INIT_SIZE, POOL_INC_SIZE); |
| 1342 | inv->pool_active = pjsip_endpt_create_pool(dlg->endpt, inv->obj_name, |
| 1343 | POOL_INIT_SIZE, POOL_INC_SIZE); |
| 1344 | |
| 1345 | /* Object name will use the same dialog pointer. */ |
| 1346 | pj_ansi_snprintf(inv->obj_name, PJ_MAX_OBJ_NAME, "inv%p", dlg); |
| 1347 | |
| 1348 | /* Process SDP in message body, if present. */ |
| 1349 | sdp_info = pjsip_rdata_get_sdp_info(rdata); |
| 1350 | if (sdp_info->sdp_err) { |
| 1351 | pjsip_dlg_dec_lock(dlg); |
| 1352 | return sdp_info->sdp_err; |
| 1353 | } |
| 1354 | |
| 1355 | /* Create negotiator. */ |
| 1356 | if (sdp_info->sdp) { |
| 1357 | status = pjmedia_sdp_neg_create_w_remote_offer(inv->pool, local_sdp, |
| 1358 | sdp_info->sdp, |
| 1359 | &inv->neg); |
| 1360 | |
| 1361 | } else if (local_sdp) { |
| 1362 | status = pjmedia_sdp_neg_create_w_local_offer(inv->pool, |
| 1363 | local_sdp, &inv->neg); |
| 1364 | } else { |
| 1365 | status = PJ_SUCCESS; |
| 1366 | } |
| 1367 | |
| 1368 | if (status != PJ_SUCCESS) { |
| 1369 | pjsip_dlg_dec_lock(dlg); |
| 1370 | return status; |
| 1371 | } |
| 1372 | |
| 1373 | /* Register invite as dialog usage. */ |
| 1374 | status = pjsip_dlg_add_usage(dlg, &mod_inv.mod, inv); |
| 1375 | if (status != PJ_SUCCESS) { |
| 1376 | pjsip_dlg_dec_lock(dlg); |
| 1377 | return status; |
| 1378 | } |
| 1379 | |
| 1380 | /* Increment session in the dialog. */ |
| 1381 | pjsip_dlg_inc_session(dlg, &mod_inv.mod); |
| 1382 | |
| 1383 | /* Save the invite transaction. */ |
| 1384 | inv->invite_tsx = pjsip_rdata_get_tsx(rdata); |
| 1385 | |
| 1386 | /* Attach our data to the transaction. */ |
| 1387 | tsx_inv_data = PJ_POOL_ZALLOC_T(inv->invite_tsx->pool, struct tsx_inv_data); |
| 1388 | tsx_inv_data->inv = inv; |
| 1389 | inv->invite_tsx->mod_data[mod_inv.mod.id] = tsx_inv_data; |
| 1390 | |
| 1391 | /* Create 100rel handler */ |
| 1392 | if (inv->options & PJSIP_INV_REQUIRE_100REL) { |
| 1393 | pjsip_100rel_attach(inv); |
| 1394 | } |
| 1395 | |
| 1396 | /* Done */ |
| 1397 | pjsip_dlg_dec_lock(dlg); |
| 1398 | *p_inv = inv; |
| 1399 | |
| 1400 | PJ_LOG(5,(inv->obj_name, "UAS invite session created for dialog %s", |
| 1401 | dlg->obj_name)); |
| 1402 | |
| 1403 | return PJ_SUCCESS; |
| 1404 | } |
| 1405 | |
| 1406 | /* |
| 1407 | * Forcefully terminate the session. |
| 1408 | */ |
| 1409 | PJ_DEF(pj_status_t) pjsip_inv_terminate( pjsip_inv_session *inv, |
| 1410 | int st_code, |
| 1411 | pj_bool_t notify) |
| 1412 | { |
| 1413 | PJ_ASSERT_RETURN(inv, PJ_EINVAL); |
| 1414 | |
| 1415 | /* Lock dialog. */ |
| 1416 | pjsip_dlg_inc_lock(inv->dlg); |
| 1417 | |
| 1418 | /* Set callback notify flag. */ |
| 1419 | inv->notify = notify; |
| 1420 | |
| 1421 | /* If there's pending transaction, terminate the transaction. |
| 1422 | * This may subsequently set the INVITE session state to |
| 1423 | * disconnected. |
| 1424 | */ |
| 1425 | if (inv->invite_tsx && |
| 1426 | inv->invite_tsx->state <= PJSIP_TSX_STATE_COMPLETED) |
| 1427 | { |
| 1428 | pjsip_tsx_terminate(inv->invite_tsx, st_code); |
| 1429 | |
| 1430 | } |
| 1431 | |
| 1432 | /* Set cause. */ |
| 1433 | inv_set_cause(inv, st_code, NULL); |
| 1434 | |
| 1435 | /* Forcefully terminate the session if state is not DISCONNECTED */ |
| 1436 | if (inv->state != PJSIP_INV_STATE_DISCONNECTED) { |
| 1437 | inv_set_state(inv, PJSIP_INV_STATE_DISCONNECTED, NULL); |
| 1438 | } |
| 1439 | |
| 1440 | /* Done. |
| 1441 | * The dec_lock() below will actually destroys the dialog if it |
| 1442 | * has no other session. |
| 1443 | */ |
| 1444 | pjsip_dlg_dec_lock(inv->dlg); |
| 1445 | |
| 1446 | return PJ_SUCCESS; |
| 1447 | } |
| 1448 | |
| 1449 | |
| 1450 | /* |
| 1451 | * Restart UAC session, possibly because app or us wants to re-send the |
| 1452 | * INVITE request due to 401/407 challenge or 3xx response. |
| 1453 | */ |
| 1454 | PJ_DEF(pj_status_t) pjsip_inv_uac_restart(pjsip_inv_session *inv, |
| 1455 | pj_bool_t new_offer) |
| 1456 | { |
| 1457 | PJ_ASSERT_RETURN(inv, PJ_EINVAL); |
| 1458 | |
| 1459 | inv->state = PJSIP_INV_STATE_NULL; |
| 1460 | inv->invite_tsx = NULL; |
| 1461 | if (inv->last_answer) { |
| 1462 | pjsip_tx_data_dec_ref(inv->last_answer); |
| 1463 | inv->last_answer = NULL; |
| 1464 | } |
| 1465 | |
| 1466 | if (new_offer && inv->neg) { |
| 1467 | pjmedia_sdp_neg_state neg_state; |
| 1468 | |
| 1469 | neg_state = pjmedia_sdp_neg_get_state(inv->neg); |
| 1470 | if (neg_state == PJMEDIA_SDP_NEG_STATE_LOCAL_OFFER) { |
| 1471 | pjmedia_sdp_neg_cancel_offer(inv->neg); |
| 1472 | } |
| 1473 | } |
| 1474 | |
| 1475 | return PJ_SUCCESS; |
| 1476 | } |
| 1477 | |
| 1478 | |
| 1479 | static void *clone_sdp(pj_pool_t *pool, const void *data, unsigned len) |
| 1480 | { |
| 1481 | PJ_UNUSED_ARG(len); |
| 1482 | return pjmedia_sdp_session_clone(pool, (const pjmedia_sdp_session*)data); |
| 1483 | } |
| 1484 | |
| 1485 | static int print_sdp(pjsip_msg_body *body, char *buf, pj_size_t len) |
| 1486 | { |
| 1487 | return pjmedia_sdp_print((const pjmedia_sdp_session*)body->data, buf, len); |
| 1488 | } |
| 1489 | |
| 1490 | |
| 1491 | PJ_DEF(pj_status_t) pjsip_create_sdp_body( pj_pool_t *pool, |
| 1492 | pjmedia_sdp_session *sdp, |
| 1493 | pjsip_msg_body **p_body) |
| 1494 | { |
| 1495 | const pj_str_t STR_APPLICATION = { "application", 11}; |
| 1496 | const pj_str_t STR_SDP = { "sdp", 3 }; |
| 1497 | pjsip_msg_body *body; |
| 1498 | |
| 1499 | body = PJ_POOL_ZALLOC_T(pool, pjsip_msg_body); |
| 1500 | PJ_ASSERT_RETURN(body != NULL, PJ_ENOMEM); |
| 1501 | |
| 1502 | pjsip_media_type_init(&body->content_type, (pj_str_t*)&STR_APPLICATION, |
| 1503 | (pj_str_t*)&STR_SDP); |
| 1504 | body->data = sdp; |
| 1505 | body->len = 0; |
| 1506 | body->clone_data = &clone_sdp; |
| 1507 | body->print_body = &print_sdp; |
| 1508 | |
| 1509 | *p_body = body; |
| 1510 | |
| 1511 | return PJ_SUCCESS; |
| 1512 | } |
| 1513 | |
| 1514 | static pjsip_msg_body *create_sdp_body(pj_pool_t *pool, |
| 1515 | const pjmedia_sdp_session *c_sdp) |
| 1516 | { |
| 1517 | pjsip_msg_body *body; |
| 1518 | pj_status_t status; |
| 1519 | |
| 1520 | status = pjsip_create_sdp_body(pool, |
| 1521 | pjmedia_sdp_session_clone(pool, c_sdp), |
| 1522 | &body); |
| 1523 | |
| 1524 | if (status != PJ_SUCCESS) |
| 1525 | return NULL; |
| 1526 | |
| 1527 | return body; |
| 1528 | } |
| 1529 | |
| 1530 | /* |
| 1531 | * Create initial INVITE request. |
| 1532 | */ |
| 1533 | PJ_DEF(pj_status_t) pjsip_inv_invite( pjsip_inv_session *inv, |
| 1534 | pjsip_tx_data **p_tdata ) |
| 1535 | { |
| 1536 | pjsip_tx_data *tdata; |
| 1537 | const pjsip_hdr *hdr; |
| 1538 | pj_bool_t has_sdp; |
| 1539 | pj_status_t status; |
| 1540 | |
| 1541 | /* Verify arguments. */ |
| 1542 | PJ_ASSERT_RETURN(inv && p_tdata, PJ_EINVAL); |
| 1543 | |
| 1544 | /* State MUST be NULL or CONFIRMED. */ |
| 1545 | PJ_ASSERT_RETURN(inv->state == PJSIP_INV_STATE_NULL || |
| 1546 | inv->state == PJSIP_INV_STATE_CONFIRMED, |
| 1547 | PJ_EINVALIDOP); |
| 1548 | |
| 1549 | /* Lock dialog. */ |
| 1550 | pjsip_dlg_inc_lock(inv->dlg); |
| 1551 | |
| 1552 | /* Create the INVITE request. */ |
| 1553 | status = pjsip_dlg_create_request(inv->dlg, pjsip_get_invite_method(), -1, |
| 1554 | &tdata); |
| 1555 | if (status != PJ_SUCCESS) |
| 1556 | goto on_return; |
| 1557 | |
| 1558 | |
| 1559 | /* If this is the first INVITE, then copy the headers from inv_hdr. |
| 1560 | * These are the headers parsed from the request URI when the |
| 1561 | * dialog was created. |
| 1562 | */ |
| 1563 | if (inv->state == PJSIP_INV_STATE_NULL) { |
| 1564 | hdr = inv->dlg->inv_hdr.next; |
| 1565 | |
| 1566 | while (hdr != &inv->dlg->inv_hdr) { |
| 1567 | pjsip_msg_add_hdr(tdata->msg, (pjsip_hdr*) |
| 1568 | pjsip_hdr_shallow_clone(tdata->pool, hdr)); |
| 1569 | hdr = hdr->next; |
| 1570 | } |
| 1571 | } |
| 1572 | |
| 1573 | /* See if we have SDP to send. */ |
| 1574 | if (inv->neg) { |
| 1575 | pjmedia_sdp_neg_state neg_state; |
| 1576 | |
| 1577 | neg_state = pjmedia_sdp_neg_get_state(inv->neg); |
| 1578 | |
| 1579 | has_sdp = (neg_state == PJMEDIA_SDP_NEG_STATE_LOCAL_OFFER || |
| 1580 | (neg_state == PJMEDIA_SDP_NEG_STATE_WAIT_NEGO && |
| 1581 | pjmedia_sdp_neg_has_local_answer(inv->neg))); |
| 1582 | |
| 1583 | |
| 1584 | } else { |
| 1585 | has_sdp = PJ_FALSE; |
| 1586 | } |
| 1587 | |
| 1588 | /* Add SDP, if any. */ |
| 1589 | if (has_sdp) { |
| 1590 | const pjmedia_sdp_session *offer; |
| 1591 | |
| 1592 | status = pjmedia_sdp_neg_get_neg_local(inv->neg, &offer); |
| 1593 | if (status != PJ_SUCCESS) { |
| 1594 | pjsip_tx_data_dec_ref(tdata); |
| 1595 | goto on_return; |
| 1596 | } |
| 1597 | |
| 1598 | tdata->msg->body = create_sdp_body(tdata->pool, offer); |
| 1599 | } |
| 1600 | |
| 1601 | /* Add Allow header. */ |
| 1602 | if (inv->dlg->add_allow) { |
| 1603 | hdr = pjsip_endpt_get_capability(inv->dlg->endpt, PJSIP_H_ALLOW, NULL); |
| 1604 | if (hdr) { |
| 1605 | pjsip_msg_add_hdr(tdata->msg, (pjsip_hdr*) |
| 1606 | pjsip_hdr_shallow_clone(tdata->pool, hdr)); |
| 1607 | } |
| 1608 | } |
| 1609 | |
| 1610 | /* Add Supported header */ |
| 1611 | hdr = pjsip_endpt_get_capability(inv->dlg->endpt, PJSIP_H_SUPPORTED, NULL); |
| 1612 | if (hdr) { |
| 1613 | pjsip_msg_add_hdr(tdata->msg, (pjsip_hdr*) |
| 1614 | pjsip_hdr_shallow_clone(tdata->pool, hdr)); |
| 1615 | } |
| 1616 | |
| 1617 | /* Add Require header. */ |
| 1618 | if ((inv->options & PJSIP_INV_REQUIRE_100REL) || |
| 1619 | (inv->options & PJSIP_INV_REQUIRE_TIMER)) |
| 1620 | { |
| 1621 | pjsip_require_hdr *hreq; |
| 1622 | |
| 1623 | hreq = pjsip_require_hdr_create(tdata->pool); |
| 1624 | |
| 1625 | if (inv->options & PJSIP_INV_REQUIRE_100REL) |
| 1626 | hreq->values[hreq->count++] = pj_str("100rel"); |
| 1627 | if (inv->options & PJSIP_INV_REQUIRE_TIMER) |
| 1628 | hreq->values[hreq->count++] = pj_str("timer"); |
| 1629 | |
| 1630 | pjsip_msg_add_hdr(tdata->msg, (pjsip_hdr*) hreq); |
| 1631 | } |
| 1632 | |
| 1633 | status = pjsip_timer_update_req(inv, tdata); |
| 1634 | if (status != PJ_SUCCESS) |
| 1635 | goto on_return; |
| 1636 | |
| 1637 | /* Done. */ |
| 1638 | *p_tdata = tdata; |
| 1639 | |
| 1640 | |
| 1641 | on_return: |
| 1642 | pjsip_dlg_dec_lock(inv->dlg); |
| 1643 | return status; |
| 1644 | } |
| 1645 | |
| 1646 | |
| 1647 | /* Util: swap pool */ |
| 1648 | static void swap_pool(pj_pool_t **p1, pj_pool_t **p2) |
| 1649 | { |
| 1650 | pj_pool_t *tmp = *p1; |
| 1651 | *p1 = *p2; |
| 1652 | *p2 = tmp; |
| 1653 | } |
| 1654 | |
| 1655 | |
| 1656 | /* |
| 1657 | * Initiate SDP negotiation in the SDP negotiator. |
| 1658 | */ |
| 1659 | static pj_status_t inv_negotiate_sdp( pjsip_inv_session *inv ) |
| 1660 | { |
| 1661 | pj_status_t status; |
| 1662 | |
| 1663 | PJ_ASSERT_RETURN(pjmedia_sdp_neg_get_state(inv->neg) == |
| 1664 | PJMEDIA_SDP_NEG_STATE_WAIT_NEGO, |
| 1665 | PJMEDIA_SDPNEG_EINSTATE); |
| 1666 | |
| 1667 | status = pjmedia_sdp_neg_negotiate(inv->pool_prov, inv->neg, 0); |
| 1668 | |
| 1669 | PJ_LOG(5,(inv->obj_name, "SDP negotiation done, status=%d", status)); |
| 1670 | |
| 1671 | if (mod_inv.cb.on_media_update && inv->notify) |
| 1672 | (*mod_inv.cb.on_media_update)(inv, status); |
| 1673 | |
| 1674 | /* Invite session may have been terminated by the application even |
| 1675 | * after a successful SDP negotiation, for example when no audio |
| 1676 | * codec is present in the offer (see ticket #1034). |
| 1677 | */ |
| 1678 | if (inv->state != PJSIP_INV_STATE_DISCONNECTED) { |
| 1679 | |
| 1680 | /* Swap the flip-flop pool when SDP negotiation success. */ |
| 1681 | if (status == PJ_SUCCESS) { |
| 1682 | swap_pool(&inv->pool_prov, &inv->pool_active); |
| 1683 | } |
| 1684 | |
| 1685 | /* Reset the provisional pool regardless SDP negotiation result. */ |
| 1686 | pj_pool_reset(inv->pool_prov); |
| 1687 | |
| 1688 | } else { |
| 1689 | |
| 1690 | status = PJSIP_ERRNO_FROM_SIP_STATUS(inv->cause); |
| 1691 | } |
| 1692 | |
| 1693 | return status; |
| 1694 | } |
| 1695 | |
| 1696 | /* |
| 1697 | * Check in incoming message for SDP offer/answer. |
| 1698 | */ |
| 1699 | static pj_status_t inv_check_sdp_in_incoming_msg( pjsip_inv_session *inv, |
| 1700 | pjsip_transaction *tsx, |
| 1701 | pjsip_rx_data *rdata) |
| 1702 | { |
| 1703 | struct tsx_inv_data *tsx_inv_data; |
| 1704 | pj_status_t status; |
| 1705 | pjsip_msg *msg; |
| 1706 | pjsip_rdata_sdp_info *sdp_info; |
| 1707 | |
| 1708 | /* Check if SDP is present in the message. */ |
| 1709 | |
| 1710 | msg = rdata->msg_info.msg; |
| 1711 | if (msg->body == NULL) { |
| 1712 | /* Message doesn't have body. */ |
| 1713 | return PJ_SUCCESS; |
| 1714 | } |
| 1715 | |
| 1716 | sdp_info = pjsip_rdata_get_sdp_info(rdata); |
| 1717 | if (sdp_info->body.ptr == NULL) { |
| 1718 | /* Message body is not "application/sdp" */ |
| 1719 | return PJMEDIA_SDP_EINSDP; |
| 1720 | } |
| 1721 | |
| 1722 | /* Get/attach invite session's transaction data */ |
| 1723 | tsx_inv_data = (struct tsx_inv_data*) tsx->mod_data[mod_inv.mod.id]; |
| 1724 | if (tsx_inv_data == NULL) { |
| 1725 | tsx_inv_data = PJ_POOL_ZALLOC_T(tsx->pool, struct tsx_inv_data); |
| 1726 | tsx_inv_data->inv = inv; |
| 1727 | tsx->mod_data[mod_inv.mod.id] = tsx_inv_data; |
| 1728 | } |
| 1729 | |
| 1730 | /* MUST NOT do multiple SDP offer/answer in a single transaction, |
| 1731 | * EXCEPT if: |
| 1732 | * - this is an initial UAC INVITE transaction (i.e. not re-INVITE), and |
| 1733 | * - the previous negotiation was done on an early media (18x) and |
| 1734 | * this response is a final/2xx response, and |
| 1735 | * - the 2xx response has different To tag than the 18x response |
| 1736 | * (i.e. the request has forked). |
| 1737 | * |
| 1738 | * The exception above is to add a rudimentary support for early media |
| 1739 | * forking (sample case: custom ringback). See this ticket for more |
| 1740 | * info: http://trac.pjsip.org/repos/ticket/657 |
| 1741 | */ |
| 1742 | if (tsx_inv_data->sdp_done) { |
| 1743 | pj_str_t res_tag; |
| 1744 | int st_code; |
| 1745 | |
| 1746 | res_tag = rdata->msg_info.to->tag; |
| 1747 | st_code = rdata->msg_info.msg->line.status.code; |
| 1748 | |
| 1749 | /* Allow final/early response after SDP has been negotiated in early |
| 1750 | * media, IF this response is a final/early response with different |
| 1751 | * tag. |
| 1752 | */ |
| 1753 | if (tsx->role == PJSIP_ROLE_UAC && |
| 1754 | (st_code/100 == 2 || |
| 1755 | (st_code==183 && pjsip_cfg()->endpt.follow_early_media_fork)) && |
| 1756 | tsx_inv_data->done_early && |
| 1757 | pj_stricmp(&tsx_inv_data->done_tag, &res_tag)) |
| 1758 | { |
| 1759 | const pjmedia_sdp_session *reoffer_sdp = NULL; |
| 1760 | |
| 1761 | PJ_LOG(4,(inv->obj_name, "Received forked %s response " |
| 1762 | "after SDP negotiation has been done in early " |
| 1763 | "media. Renegotiating SDP..", |
| 1764 | (st_code==183? "early" : "final" ))); |
| 1765 | |
| 1766 | /* Retrieve original SDP offer from INVITE request */ |
| 1767 | reoffer_sdp = (const pjmedia_sdp_session*) |
| 1768 | tsx->last_tx->msg->body->data; |
| 1769 | |
| 1770 | /* Feed the original offer to negotiator */ |
| 1771 | status = pjmedia_sdp_neg_modify_local_offer2(inv->pool_prov, |
| 1772 | inv->neg, |
| 1773 | inv->sdp_neg_flags, |
| 1774 | reoffer_sdp); |
| 1775 | if (status != PJ_SUCCESS) { |
| 1776 | PJ_LOG(1,(inv->obj_name, "Error updating local offer for " |
| 1777 | "forked 2xx/183 response (err=%d)", status)); |
| 1778 | return status; |
| 1779 | } |
| 1780 | |
| 1781 | } else { |
| 1782 | |
| 1783 | if (rdata->msg_info.msg->body) { |
| 1784 | PJ_LOG(4,(inv->obj_name, "SDP negotiation done, message " |
| 1785 | "body is ignored")); |
| 1786 | } |
| 1787 | return PJ_SUCCESS; |
| 1788 | } |
| 1789 | } |
| 1790 | |
| 1791 | /* Process the SDP body. */ |
| 1792 | if (sdp_info->sdp_err) { |
| 1793 | PJ_PERROR(4,(THIS_FILE, sdp_info->sdp_err, |
| 1794 | "Error parsing SDP in %s", |
| 1795 | pjsip_rx_data_get_info(rdata))); |
| 1796 | return PJMEDIA_SDP_EINSDP; |
| 1797 | } |
| 1798 | |
| 1799 | pj_assert(sdp_info->sdp != NULL); |
| 1800 | |
| 1801 | /* The SDP can be an offer or answer, depending on negotiator's state */ |
| 1802 | |
| 1803 | if (inv->neg == NULL || |
| 1804 | pjmedia_sdp_neg_get_state(inv->neg) == PJMEDIA_SDP_NEG_STATE_DONE) |
| 1805 | { |
| 1806 | |
| 1807 | /* This is an offer. */ |
| 1808 | |
| 1809 | PJ_LOG(5,(inv->obj_name, "Got SDP offer in %s", |
| 1810 | pjsip_rx_data_get_info(rdata))); |
| 1811 | |
| 1812 | if (inv->neg == NULL) { |
| 1813 | status=pjmedia_sdp_neg_create_w_remote_offer(inv->pool, NULL, |
| 1814 | sdp_info->sdp, |
| 1815 | &inv->neg); |
| 1816 | } else { |
| 1817 | status=pjmedia_sdp_neg_set_remote_offer(inv->pool_prov, inv->neg, |
| 1818 | sdp_info->sdp); |
| 1819 | } |
| 1820 | |
| 1821 | if (status != PJ_SUCCESS) { |
| 1822 | PJ_PERROR(4,(THIS_FILE, status, "Error processing SDP offer in %", |
| 1823 | pjsip_rx_data_get_info(rdata))); |
| 1824 | return PJMEDIA_SDP_EINSDP; |
| 1825 | } |
| 1826 | |
| 1827 | /* Inform application about remote offer. */ |
| 1828 | if (mod_inv.cb.on_rx_offer && inv->notify) { |
| 1829 | |
| 1830 | (*mod_inv.cb.on_rx_offer)(inv, sdp_info->sdp); |
| 1831 | |
| 1832 | } |
| 1833 | |
| 1834 | /* application must have supplied an answer at this point. */ |
| 1835 | if (pjmedia_sdp_neg_get_state(inv->neg) != |
| 1836 | PJMEDIA_SDP_NEG_STATE_WAIT_NEGO) |
| 1837 | { |
| 1838 | if (mod_inv.cb.on_rx_reinvite && inv->notify && |
| 1839 | msg->type == PJSIP_REQUEST_MSG && |
| 1840 | msg->line.req.method.id == PJSIP_INVITE_METHOD) |
| 1841 | { |
| 1842 | /* Do not return failure first, allow the application |
| 1843 | * to set the answer in the on_rx_reinvite() callback. |
| 1844 | */ |
| 1845 | PJ_LOG(5,(inv->obj_name, "Ignoring on_rx_offer() status " |
| 1846 | "because on_rx_reinvite() is implemented")); |
| 1847 | return PJ_SUCCESS; |
| 1848 | } |
| 1849 | return PJ_EINVALIDOP; |
| 1850 | } |
| 1851 | |
| 1852 | } else if (pjmedia_sdp_neg_get_state(inv->neg) == |
| 1853 | PJMEDIA_SDP_NEG_STATE_LOCAL_OFFER) |
| 1854 | { |
| 1855 | int status_code; |
| 1856 | |
| 1857 | /* This is an answer. |
| 1858 | * Process and negotiate remote answer. |
| 1859 | */ |
| 1860 | |
| 1861 | PJ_LOG(5,(inv->obj_name, "Got SDP answer in %s", |
| 1862 | pjsip_rx_data_get_info(rdata))); |
| 1863 | |
| 1864 | status = pjmedia_sdp_neg_set_remote_answer(inv->pool_prov, inv->neg, |
| 1865 | sdp_info->sdp); |
| 1866 | |
| 1867 | if (status != PJ_SUCCESS) { |
| 1868 | PJ_PERROR(4,(THIS_FILE, status, "Error processing SDP answer in %s", |
| 1869 | pjsip_rx_data_get_info(rdata))); |
| 1870 | return PJMEDIA_SDP_EINSDP; |
| 1871 | } |
| 1872 | |
| 1873 | /* Negotiate SDP */ |
| 1874 | |
| 1875 | inv_negotiate_sdp(inv); |
| 1876 | |
| 1877 | /* Mark this transaction has having SDP offer/answer done, and |
| 1878 | * save the reference to the To tag |
| 1879 | */ |
| 1880 | |
| 1881 | tsx_inv_data->sdp_done = 1; |
| 1882 | status_code = rdata->msg_info.msg->line.status.code; |
| 1883 | tsx_inv_data->done_early = (status_code/100==1); |
| 1884 | pj_strdup(tsx->pool, &tsx_inv_data->done_tag, |
| 1885 | &rdata->msg_info.to->tag); |
| 1886 | |
| 1887 | } else { |
| 1888 | |
| 1889 | PJ_LOG(5,(THIS_FILE, "Ignored SDP in %s: negotiator state is %s", |
| 1890 | pjsip_rx_data_get_info(rdata), |
| 1891 | pjmedia_sdp_neg_state_str(pjmedia_sdp_neg_get_state(inv->neg)))); |
| 1892 | } |
| 1893 | |
| 1894 | return PJ_SUCCESS; |
| 1895 | } |
| 1896 | |
| 1897 | |
| 1898 | /* |
| 1899 | * Process INVITE answer, for both initial and subsequent re-INVITE |
| 1900 | */ |
| 1901 | static pj_status_t process_answer( pjsip_inv_session *inv, |
| 1902 | int st_code, |
| 1903 | pjsip_tx_data *tdata, |
| 1904 | const pjmedia_sdp_session *local_sdp) |
| 1905 | { |
| 1906 | pj_status_t status; |
| 1907 | const pjmedia_sdp_session *sdp = NULL; |
| 1908 | |
| 1909 | /* If local_sdp is specified, then we MUST NOT have answered the |
| 1910 | * offer before. |
| 1911 | */ |
| 1912 | if (local_sdp && (st_code/100==1 || st_code/100==2)) { |
| 1913 | |
| 1914 | if (inv->neg == NULL) { |
| 1915 | status = pjmedia_sdp_neg_create_w_local_offer(inv->pool, |
| 1916 | local_sdp, |
| 1917 | &inv->neg); |
| 1918 | } else if (pjmedia_sdp_neg_get_state(inv->neg)== |
| 1919 | PJMEDIA_SDP_NEG_STATE_REMOTE_OFFER) |
| 1920 | { |
| 1921 | status = pjmedia_sdp_neg_set_local_answer(inv->pool_prov, inv->neg, |
| 1922 | local_sdp); |
| 1923 | } else { |
| 1924 | |
| 1925 | /* Can not specify local SDP at this state. */ |
| 1926 | pj_assert(0); |
| 1927 | status = PJMEDIA_SDPNEG_EINSTATE; |
| 1928 | } |
| 1929 | |
| 1930 | if (status != PJ_SUCCESS) |
| 1931 | return status; |
| 1932 | |
| 1933 | } |
| 1934 | |
| 1935 | |
| 1936 | /* If SDP negotiator is ready, start negotiation. */ |
| 1937 | if (st_code/100==2 || (st_code/10==18 && st_code!=180)) { |
| 1938 | |
| 1939 | pjmedia_sdp_neg_state neg_state; |
| 1940 | |
| 1941 | /* Start nego when appropriate. */ |
| 1942 | neg_state = inv->neg ? pjmedia_sdp_neg_get_state(inv->neg) : |
| 1943 | PJMEDIA_SDP_NEG_STATE_NULL; |
| 1944 | |
| 1945 | if (neg_state == PJMEDIA_SDP_NEG_STATE_LOCAL_OFFER) { |
| 1946 | |
| 1947 | status = pjmedia_sdp_neg_get_neg_local(inv->neg, &sdp); |
| 1948 | |
| 1949 | } else if (neg_state == PJMEDIA_SDP_NEG_STATE_WAIT_NEGO && |
| 1950 | pjmedia_sdp_neg_has_local_answer(inv->neg) ) |
| 1951 | { |
| 1952 | struct tsx_inv_data *tsx_inv_data; |
| 1953 | |
| 1954 | /* Get invite session's transaction data */ |
| 1955 | tsx_inv_data = (struct tsx_inv_data*) |
| 1956 | inv->invite_tsx->mod_data[mod_inv.mod.id]; |
| 1957 | |
| 1958 | status = inv_negotiate_sdp(inv); |
| 1959 | if (status != PJ_SUCCESS) |
| 1960 | return status; |
| 1961 | |
| 1962 | /* Mark this transaction has having SDP offer/answer done. */ |
| 1963 | tsx_inv_data->sdp_done = 1; |
| 1964 | |
| 1965 | status = pjmedia_sdp_neg_get_active_local(inv->neg, &sdp); |
| 1966 | } |
| 1967 | } |
| 1968 | |
| 1969 | /* Include SDP when it's available for 2xx and 18x (but not 180) response. |
| 1970 | * Subsequent response will include this SDP. |
| 1971 | * |
| 1972 | * Note note: |
| 1973 | * - When offer/answer has been completed in reliable 183, we MUST NOT |
| 1974 | * send SDP in 2xx response. So if we don't have SDP to send, clear |
| 1975 | * the SDP in the message body ONLY if 100rel is active in this |
| 1976 | * session. |
| 1977 | */ |
| 1978 | if (sdp) { |
| 1979 | tdata->msg->body = create_sdp_body(tdata->pool, sdp); |
| 1980 | } else { |
| 1981 | if (inv->options & PJSIP_INV_REQUIRE_100REL) { |
| 1982 | tdata->msg->body = NULL; |
| 1983 | } |
| 1984 | } |
| 1985 | |
| 1986 | /* Cancel SDP negotiation if this is a negative reply to a re-INVITE */ |
| 1987 | if (st_code >= 300 && inv->neg != NULL && |
| 1988 | inv->state == PJSIP_INV_STATE_CONFIRMED) |
| 1989 | { |
| 1990 | pjmedia_sdp_neg_state neg_state; |
| 1991 | neg_state = pjmedia_sdp_neg_get_state(inv->neg); |
| 1992 | if (neg_state == PJMEDIA_SDP_NEG_STATE_REMOTE_OFFER) { |
| 1993 | pjmedia_sdp_neg_cancel_offer(inv->neg); |
| 1994 | } |
| 1995 | } |
| 1996 | |
| 1997 | return PJ_SUCCESS; |
| 1998 | } |
| 1999 | |
| 2000 | |
| 2001 | /* |
| 2002 | * Create first response to INVITE |
| 2003 | */ |
| 2004 | PJ_DEF(pj_status_t) pjsip_inv_initial_answer( pjsip_inv_session *inv, |
| 2005 | pjsip_rx_data *rdata, |
| 2006 | int st_code, |
| 2007 | const pj_str_t *st_text, |
| 2008 | const pjmedia_sdp_session *sdp, |
| 2009 | pjsip_tx_data **p_tdata) |
| 2010 | { |
| 2011 | pjsip_tx_data *tdata; |
| 2012 | pj_status_t status; |
| 2013 | pjsip_status_code st_code2; |
| 2014 | |
| 2015 | /* Verify arguments. */ |
| 2016 | PJ_ASSERT_RETURN(inv && p_tdata, PJ_EINVAL); |
| 2017 | |
| 2018 | /* Must have INVITE transaction. */ |
| 2019 | PJ_ASSERT_RETURN(inv->invite_tsx, PJ_EBUG); |
| 2020 | |
| 2021 | pj_log_push_indent(); |
| 2022 | |
| 2023 | pjsip_dlg_inc_lock(inv->dlg); |
| 2024 | |
| 2025 | /* Create response */ |
| 2026 | status = pjsip_dlg_create_response(inv->dlg, rdata, st_code, st_text, |
| 2027 | &tdata); |
| 2028 | if (status != PJ_SUCCESS) |
| 2029 | goto on_return; |
| 2030 | |
| 2031 | /* Invoke Session Timers module */ |
| 2032 | status = pjsip_timer_process_req(inv, rdata, &st_code2); |
| 2033 | if (status != PJ_SUCCESS) { |
| 2034 | pj_status_t status2; |
| 2035 | |
| 2036 | status2 = pjsip_dlg_modify_response(inv->dlg, tdata, st_code2, NULL); |
| 2037 | if (status2 != PJ_SUCCESS) { |
| 2038 | pjsip_tx_data_dec_ref(tdata); |
| 2039 | goto on_return; |
| 2040 | } |
| 2041 | status2 = pjsip_timer_update_resp(inv, tdata); |
| 2042 | if (status2 == PJ_SUCCESS) |
| 2043 | *p_tdata = tdata; |
| 2044 | else |
| 2045 | pjsip_tx_data_dec_ref(tdata); |
| 2046 | |
| 2047 | goto on_return; |
| 2048 | } |
| 2049 | |
| 2050 | /* Process SDP in answer */ |
| 2051 | status = process_answer(inv, st_code, tdata, sdp); |
| 2052 | if (status != PJ_SUCCESS) { |
| 2053 | pjsip_tx_data_dec_ref(tdata); |
| 2054 | goto on_return; |
| 2055 | } |
| 2056 | |
| 2057 | /* Save this answer */ |
| 2058 | inv->last_answer = tdata; |
| 2059 | pjsip_tx_data_add_ref(inv->last_answer); |
| 2060 | PJ_LOG(5,(inv->dlg->obj_name, "Initial answer %s", |
| 2061 | pjsip_tx_data_get_info(inv->last_answer))); |
| 2062 | |
| 2063 | /* Invoke Session Timers */ |
| 2064 | pjsip_timer_update_resp(inv, tdata); |
| 2065 | |
| 2066 | *p_tdata = tdata; |
| 2067 | |
| 2068 | on_return: |
| 2069 | pjsip_dlg_dec_lock(inv->dlg); |
| 2070 | pj_log_pop_indent(); |
| 2071 | return status; |
| 2072 | } |
| 2073 | |
| 2074 | |
| 2075 | /* |
| 2076 | * Answer INVITE request. |
| 2077 | */ |
| 2078 | PJ_DEF(pj_status_t) pjsip_inv_answer( pjsip_inv_session *inv, |
| 2079 | int st_code, |
| 2080 | const pj_str_t *st_text, |
| 2081 | const pjmedia_sdp_session *local_sdp, |
| 2082 | pjsip_tx_data **p_tdata ) |
| 2083 | { |
| 2084 | pjsip_tx_data *last_res; |
| 2085 | pj_status_t status; |
| 2086 | |
| 2087 | /* Verify arguments. */ |
| 2088 | PJ_ASSERT_RETURN(inv && p_tdata, PJ_EINVAL); |
| 2089 | |
| 2090 | /* Must have INVITE transaction. */ |
| 2091 | PJ_ASSERT_RETURN(inv->invite_tsx, PJ_EBUG); |
| 2092 | |
| 2093 | /* Must have created an answer before */ |
| 2094 | PJ_ASSERT_RETURN(inv->last_answer, PJ_EINVALIDOP); |
| 2095 | |
| 2096 | pj_log_push_indent(); |
| 2097 | |
| 2098 | pjsip_dlg_inc_lock(inv->dlg); |
| 2099 | |
| 2100 | /* Modify last response. */ |
| 2101 | last_res = inv->last_answer; |
| 2102 | status = pjsip_dlg_modify_response(inv->dlg, last_res, st_code, st_text); |
| 2103 | if (status != PJ_SUCCESS) |
| 2104 | goto on_return; |
| 2105 | |
| 2106 | /* For non-2xx final response, strip message body */ |
| 2107 | if (st_code >= 300) { |
| 2108 | last_res->msg->body = NULL; |
| 2109 | } |
| 2110 | |
| 2111 | /* Process SDP in answer */ |
| 2112 | status = process_answer(inv, st_code, last_res, local_sdp); |
| 2113 | if (status != PJ_SUCCESS) { |
| 2114 | pjsip_tx_data_dec_ref(last_res); |
| 2115 | goto on_return; |
| 2116 | } |
| 2117 | |
| 2118 | /* Invoke Session Timers */ |
| 2119 | pjsip_timer_update_resp(inv, last_res); |
| 2120 | |
| 2121 | *p_tdata = last_res; |
| 2122 | |
| 2123 | on_return: |
| 2124 | pjsip_dlg_dec_lock(inv->dlg); |
| 2125 | pj_log_pop_indent(); |
| 2126 | return status; |
| 2127 | } |
| 2128 | |
| 2129 | |
| 2130 | /* |
| 2131 | * Set local SDP offer/answer. |
| 2132 | */ |
| 2133 | PJ_DEF(pj_status_t) pjsip_inv_set_local_sdp(pjsip_inv_session *inv, |
| 2134 | const pjmedia_sdp_session *sdp ) |
| 2135 | { |
| 2136 | const pjmedia_sdp_session *offer; |
| 2137 | pj_status_t status; |
| 2138 | |
| 2139 | PJ_ASSERT_RETURN(inv && sdp, PJ_EINVAL); |
| 2140 | |
| 2141 | /* If we have remote SDP offer, set local answer to respond to the offer, |
| 2142 | * otherwise we set/modify our local offer (and create an SDP negotiator |
| 2143 | * if we don't have one yet). |
| 2144 | */ |
| 2145 | if (inv->neg) { |
| 2146 | pjmedia_sdp_neg_state neg_state = pjmedia_sdp_neg_get_state(inv->neg); |
| 2147 | |
| 2148 | if ((neg_state == PJMEDIA_SDP_NEG_STATE_REMOTE_OFFER || |
| 2149 | neg_state == PJMEDIA_SDP_NEG_STATE_WAIT_NEGO) && |
| 2150 | pjmedia_sdp_neg_get_neg_remote(inv->neg, &offer) == PJ_SUCCESS) |
| 2151 | { |
| 2152 | status = pjsip_inv_set_sdp_answer(inv, sdp); |
| 2153 | } else if (neg_state == PJMEDIA_SDP_NEG_STATE_DONE) { |
| 2154 | status = pjmedia_sdp_neg_modify_local_offer2(inv->pool, inv->neg, |
| 2155 | inv->sdp_neg_flags, |
| 2156 | sdp); |
| 2157 | } else |
| 2158 | return PJMEDIA_SDPNEG_EINSTATE; |
| 2159 | } else { |
| 2160 | status = pjmedia_sdp_neg_create_w_local_offer(inv->pool, |
| 2161 | sdp, &inv->neg); |
| 2162 | } |
| 2163 | |
| 2164 | return status; |
| 2165 | } |
| 2166 | |
| 2167 | |
| 2168 | /* |
| 2169 | * Set SDP answer. |
| 2170 | */ |
| 2171 | PJ_DEF(pj_status_t) pjsip_inv_set_sdp_answer( pjsip_inv_session *inv, |
| 2172 | const pjmedia_sdp_session *sdp ) |
| 2173 | { |
| 2174 | pj_status_t status; |
| 2175 | |
| 2176 | PJ_ASSERT_RETURN(inv && sdp, PJ_EINVAL); |
| 2177 | |
| 2178 | pjsip_dlg_inc_lock(inv->dlg); |
| 2179 | status = pjmedia_sdp_neg_set_local_answer( inv->pool_prov, inv->neg, sdp); |
| 2180 | pjsip_dlg_dec_lock(inv->dlg); |
| 2181 | |
| 2182 | return status; |
| 2183 | } |
| 2184 | |
| 2185 | |
| 2186 | /* |
| 2187 | * End session. |
| 2188 | */ |
| 2189 | PJ_DEF(pj_status_t) pjsip_inv_end_session( pjsip_inv_session *inv, |
| 2190 | int st_code, |
| 2191 | const pj_str_t *st_text, |
| 2192 | pjsip_tx_data **p_tdata ) |
| 2193 | { |
| 2194 | pjsip_tx_data *tdata; |
| 2195 | pj_status_t status; |
| 2196 | |
| 2197 | /* Verify arguments. */ |
| 2198 | PJ_ASSERT_RETURN(inv && p_tdata, PJ_EINVAL); |
| 2199 | |
| 2200 | pj_log_push_indent(); |
| 2201 | |
| 2202 | /* Set cause code. */ |
| 2203 | inv_set_cause(inv, st_code, st_text); |
| 2204 | |
| 2205 | /* Create appropriate message. */ |
| 2206 | switch (inv->state) { |
| 2207 | case PJSIP_INV_STATE_CALLING: |
| 2208 | case PJSIP_INV_STATE_EARLY: |
| 2209 | case PJSIP_INV_STATE_INCOMING: |
| 2210 | |
| 2211 | if (inv->role == PJSIP_ROLE_UAC) { |
| 2212 | |
| 2213 | /* For UAC when session has not been confirmed, create CANCEL. */ |
| 2214 | |
| 2215 | /* MUST have the original UAC INVITE transaction. */ |
| 2216 | PJ_ASSERT_RETURN(inv->invite_tsx != NULL, PJ_EBUG); |
| 2217 | |
| 2218 | /* But CANCEL should only be called when we have received a |
| 2219 | * provisional response. If we haven't received any responses, |
| 2220 | * just destroy the transaction. |
| 2221 | */ |
| 2222 | if (inv->invite_tsx->status_code < 100) { |
| 2223 | |
| 2224 | /* Do not stop INVITE retransmission, see ticket #506 */ |
| 2225 | //pjsip_tsx_stop_retransmit(inv->invite_tsx); |
| 2226 | inv->cancelling = PJ_TRUE; |
| 2227 | inv->pending_cancel = PJ_TRUE; |
| 2228 | *p_tdata = NULL; |
| 2229 | PJ_LOG(4, (inv->obj_name, "Delaying CANCEL since no " |
| 2230 | "provisional response is received yet")); |
| 2231 | pj_log_pop_indent(); |
| 2232 | return PJ_SUCCESS; |
| 2233 | } |
| 2234 | |
| 2235 | /* The CSeq here assumes that the dialog is started with an |
| 2236 | * INVITE session. This may not be correct; dialog can be |
| 2237 | * started as SUBSCRIBE session. |
| 2238 | * So fix this! |
| 2239 | */ |
| 2240 | status = pjsip_endpt_create_cancel(inv->dlg->endpt, |
| 2241 | inv->invite_tsx->last_tx, |
| 2242 | &tdata); |
| 2243 | if (status != PJ_SUCCESS) { |
| 2244 | pj_log_pop_indent(); |
| 2245 | return status; |
| 2246 | } |
| 2247 | |
| 2248 | /* Set timeout for the INVITE transaction, in case UAS is not |
| 2249 | * able to respond the INVITE with 487 final response. The |
| 2250 | * timeout value is 64*T1. |
| 2251 | */ |
| 2252 | pjsip_tsx_set_timeout(inv->invite_tsx, 64 * pjsip_cfg()->tsx.t1); |
| 2253 | |
| 2254 | } else { |
| 2255 | |
| 2256 | /* For UAS, send a final response. */ |
| 2257 | tdata = inv->invite_tsx->last_tx; |
| 2258 | PJ_ASSERT_RETURN(tdata != NULL, PJ_EINVALIDOP); |
| 2259 | |
| 2260 | //status = pjsip_dlg_modify_response(inv->dlg, tdata, st_code, |
| 2261 | // st_text); |
| 2262 | status = pjsip_inv_answer(inv, st_code, st_text, NULL, &tdata); |
| 2263 | } |
| 2264 | break; |
| 2265 | |
| 2266 | case PJSIP_INV_STATE_CONNECTING: |
| 2267 | case PJSIP_INV_STATE_CONFIRMED: |
| 2268 | /* End Session Timer */ |
| 2269 | pjsip_timer_end_session(inv); |
| 2270 | |
| 2271 | /* For established dialog, send BYE */ |
| 2272 | status = pjsip_dlg_create_request(inv->dlg, pjsip_get_bye_method(), |
| 2273 | -1, &tdata); |
| 2274 | break; |
| 2275 | |
| 2276 | case PJSIP_INV_STATE_DISCONNECTED: |
| 2277 | /* No need to do anything. */ |
| 2278 | pj_log_pop_indent(); |
| 2279 | return PJSIP_ESESSIONTERMINATED; |
| 2280 | |
| 2281 | default: |
| 2282 | pj_assert(!"Invalid operation!"); |
| 2283 | pj_log_pop_indent(); |
| 2284 | return PJ_EINVALIDOP; |
| 2285 | } |
| 2286 | |
| 2287 | if (status != PJ_SUCCESS) { |
| 2288 | pj_log_pop_indent(); |
| 2289 | return status; |
| 2290 | } |
| 2291 | |
| 2292 | |
| 2293 | /* Done */ |
| 2294 | |
| 2295 | inv->cancelling = PJ_TRUE; |
| 2296 | *p_tdata = tdata; |
| 2297 | |
| 2298 | pj_log_pop_indent(); |
| 2299 | return PJ_SUCCESS; |
| 2300 | } |
| 2301 | |
| 2302 | /* |
| 2303 | * Cancel re-INVITE transaction. |
| 2304 | */ |
| 2305 | PJ_DEF(pj_status_t) pjsip_inv_cancel_reinvite( pjsip_inv_session *inv, |
| 2306 | pjsip_tx_data **p_tdata ) |
| 2307 | { |
| 2308 | pjsip_tx_data *tdata; |
| 2309 | pj_status_t status; |
| 2310 | |
| 2311 | /* Verify arguments. */ |
| 2312 | PJ_ASSERT_RETURN(inv && p_tdata, PJ_EINVAL); |
| 2313 | |
| 2314 | pj_log_push_indent(); |
| 2315 | |
| 2316 | /* Create appropriate message. */ |
| 2317 | switch (inv->state) { |
| 2318 | case PJSIP_INV_STATE_CONFIRMED: |
| 2319 | /* MUST have the original UAC INVITE transaction */ |
| 2320 | PJ_ASSERT_RETURN(inv->invite_tsx != NULL, PJ_EBUG); |
| 2321 | |
| 2322 | /* CANCEL should only be called when we have received a |
| 2323 | * provisional response. |
| 2324 | */ |
| 2325 | if (inv->invite_tsx->status_code < 100) { |
| 2326 | inv->cancelling = PJ_TRUE; |
| 2327 | inv->pending_cancel = PJ_TRUE; |
| 2328 | *p_tdata = NULL; |
| 2329 | PJ_LOG(4, (inv->obj_name, "Delaying CANCEL since no " |
| 2330 | "provisional response is received yet")); |
| 2331 | pj_log_pop_indent(); |
| 2332 | return PJ_SUCCESS; |
| 2333 | } |
| 2334 | |
| 2335 | status = pjsip_endpt_create_cancel(inv->dlg->endpt, |
| 2336 | inv->invite_tsx->last_tx, |
| 2337 | &tdata); |
| 2338 | if (status != PJ_SUCCESS) { |
| 2339 | pj_log_pop_indent(); |
| 2340 | return status; |
| 2341 | } |
| 2342 | break; |
| 2343 | |
| 2344 | default: |
| 2345 | /* We cannot send CANCEL to a re-INVITE if the INVITE session is |
| 2346 | * not confirmed. |
| 2347 | */ |
| 2348 | pj_log_pop_indent(); |
| 2349 | return PJ_EINVALIDOP; |
| 2350 | } |
| 2351 | |
| 2352 | pj_log_pop_indent(); |
| 2353 | |
| 2354 | *p_tdata = tdata; |
| 2355 | return PJ_SUCCESS; |
| 2356 | } |
| 2357 | |
| 2358 | /* Following redirection recursion, get next target from the target set and |
| 2359 | * notify user. |
| 2360 | * |
| 2361 | * Returns PJ_FALSE if recursion fails (either because there's no more target |
| 2362 | * or user rejects the recursion). If we return PJ_FALSE, caller should |
| 2363 | * disconnect the session. |
| 2364 | * |
| 2365 | * Note: |
| 2366 | * the event 'e' argument may be NULL. |
| 2367 | */ |
| 2368 | static pj_bool_t inv_uac_recurse(pjsip_inv_session *inv, int code, |
| 2369 | const pj_str_t *reason, pjsip_event *e) |
| 2370 | { |
| 2371 | pjsip_redirect_op op; |
| 2372 | pjsip_target *target; |
| 2373 | |
| 2374 | /* Won't redirect if the callback is not implemented. */ |
| 2375 | if (mod_inv.cb.on_redirected == NULL) |
| 2376 | return PJ_FALSE; |
| 2377 | |
| 2378 | if (reason == NULL) |
| 2379 | reason = pjsip_get_status_text(code); |
| 2380 | |
| 2381 | /* Set status of current target */ |
| 2382 | pjsip_target_assign_status(inv->dlg->target_set.current, inv->dlg->pool, |
| 2383 | code, reason); |
| 2384 | |
| 2385 | /* Fetch next target from the target set. We only want to |
| 2386 | * process SIP/SIPS URI for now. |
| 2387 | */ |
| 2388 | for (;;) { |
| 2389 | target = pjsip_target_set_get_next(&inv->dlg->target_set); |
| 2390 | if (target == NULL) { |
| 2391 | /* No more target. */ |
| 2392 | return PJ_FALSE; |
| 2393 | } |
| 2394 | |
| 2395 | if (!PJSIP_URI_SCHEME_IS_SIP(target->uri) && |
| 2396 | !PJSIP_URI_SCHEME_IS_SIPS(target->uri)) |
| 2397 | { |
| 2398 | code = PJSIP_SC_UNSUPPORTED_URI_SCHEME; |
| 2399 | reason = pjsip_get_status_text(code); |
| 2400 | |
| 2401 | /* Mark this target as unusable and fetch next target. */ |
| 2402 | pjsip_target_assign_status(target, inv->dlg->pool, code, reason); |
| 2403 | } else { |
| 2404 | /* Found a target */ |
| 2405 | break; |
| 2406 | } |
| 2407 | } |
| 2408 | |
| 2409 | /* We have target in 'target'. Set this target as current target |
| 2410 | * and notify callback. |
| 2411 | */ |
| 2412 | pjsip_target_set_set_current(&inv->dlg->target_set, target); |
| 2413 | |
| 2414 | op = (*mod_inv.cb.on_redirected)(inv, target->uri, e); |
| 2415 | |
| 2416 | |
| 2417 | /* Check what the application wants to do now */ |
| 2418 | switch (op) { |
| 2419 | case PJSIP_REDIRECT_ACCEPT: |
| 2420 | case PJSIP_REDIRECT_ACCEPT_REPLACE: |
| 2421 | case PJSIP_REDIRECT_STOP: |
| 2422 | /* Must increment session counter, that's the convention of the |
| 2423 | * pjsip_inv_process_redirect(). |
| 2424 | */ |
| 2425 | pjsip_dlg_inc_session(inv->dlg, &mod_inv.mod); |
| 2426 | |
| 2427 | /* Act on the recursion */ |
| 2428 | pjsip_inv_process_redirect(inv, op, e); |
| 2429 | return PJ_TRUE; |
| 2430 | |
| 2431 | case PJSIP_REDIRECT_PENDING: |
| 2432 | /* Increment session so that the dialog/session is not destroyed |
| 2433 | * while we're waiting for user confirmation. |
| 2434 | */ |
| 2435 | pjsip_dlg_inc_session(inv->dlg, &mod_inv.mod); |
| 2436 | |
| 2437 | /* Also clear the invite_tsx variable, otherwise when this tsx is |
| 2438 | * terminated, it will also terminate the session. |
| 2439 | */ |
| 2440 | inv->invite_tsx = NULL; |
| 2441 | |
| 2442 | /* Done. The processing will continue once the application calls |
| 2443 | * pjsip_inv_process_redirect(). |
| 2444 | */ |
| 2445 | return PJ_TRUE; |
| 2446 | |
| 2447 | case PJSIP_REDIRECT_REJECT: |
| 2448 | /* Recursively call this function again to fetch next target, if any. |
| 2449 | */ |
| 2450 | return inv_uac_recurse(inv, PJSIP_SC_REQUEST_TERMINATED, NULL, e); |
| 2451 | |
| 2452 | } |
| 2453 | |
| 2454 | pj_assert(!"Should not reach here"); |
| 2455 | return PJ_FALSE; |
| 2456 | } |
| 2457 | |
| 2458 | |
| 2459 | /* Process redirection/recursion */ |
| 2460 | PJ_DEF(pj_status_t) pjsip_inv_process_redirect( pjsip_inv_session *inv, |
| 2461 | pjsip_redirect_op op, |
| 2462 | pjsip_event *e) |
| 2463 | { |
| 2464 | const pjsip_status_code cancel_code = PJSIP_SC_REQUEST_TERMINATED; |
| 2465 | pjsip_event usr_event; |
| 2466 | pj_status_t status = PJ_SUCCESS; |
| 2467 | |
| 2468 | PJ_ASSERT_RETURN(inv && op != PJSIP_REDIRECT_PENDING, PJ_EINVAL); |
| 2469 | |
| 2470 | if (e == NULL) { |
| 2471 | PJSIP_EVENT_INIT_USER(usr_event, NULL, NULL, NULL, NULL); |
| 2472 | e = &usr_event; |
| 2473 | } |
| 2474 | |
| 2475 | pjsip_dlg_inc_lock(inv->dlg); |
| 2476 | |
| 2477 | /* Decrement session. That's the convention here to prevent the dialog |
| 2478 | * or session from being destroyed while we're waiting for user |
| 2479 | * confirmation. |
| 2480 | */ |
| 2481 | pjsip_dlg_dec_session(inv->dlg, &mod_inv.mod); |
| 2482 | |
| 2483 | /* See what the application wants to do now */ |
| 2484 | switch (op) { |
| 2485 | case PJSIP_REDIRECT_ACCEPT: |
| 2486 | case PJSIP_REDIRECT_ACCEPT_REPLACE: |
| 2487 | /* User accept the redirection. Reset the session and resend the |
| 2488 | * INVITE request. |
| 2489 | */ |
| 2490 | { |
| 2491 | pjsip_tx_data *tdata; |
| 2492 | pjsip_via_hdr *via; |
| 2493 | |
| 2494 | /* Get the original INVITE request. */ |
| 2495 | tdata = inv->invite_req; |
| 2496 | pjsip_tx_data_add_ref(tdata); |
| 2497 | |
| 2498 | /* Restore strict route set. |
| 2499 | * See http://trac.pjsip.org/repos/ticket/492 |
| 2500 | */ |
| 2501 | pjsip_restore_strict_route_set(tdata); |
| 2502 | |
| 2503 | /* Set target */ |
| 2504 | tdata->msg->line.req.uri = (pjsip_uri*) |
| 2505 | pjsip_uri_clone(tdata->pool, inv->dlg->target_set.current->uri); |
| 2506 | |
| 2507 | /* Remove branch param in Via header. */ |
| 2508 | via = (pjsip_via_hdr*) |
| 2509 | pjsip_msg_find_hdr(tdata->msg, PJSIP_H_VIA, NULL); |
| 2510 | via->branch_param.slen = 0; |
| 2511 | |
| 2512 | /* Process PJSIP_REDIRECT_ACCEPT_REPLACE */ |
| 2513 | if (op == PJSIP_REDIRECT_ACCEPT_REPLACE) { |
| 2514 | pjsip_to_hdr *to; |
| 2515 | pjsip_dialog *dlg = inv->dlg; |
| 2516 | enum { TMP_LEN = 128 }; |
| 2517 | char tmp[TMP_LEN]; |
| 2518 | int len; |
| 2519 | |
| 2520 | /* Replace To header */ |
| 2521 | to = PJSIP_MSG_TO_HDR(tdata->msg); |
| 2522 | to->uri = (pjsip_uri*) |
| 2523 | pjsip_uri_clone(tdata->pool, |
| 2524 | dlg->target_set.current->uri); |
| 2525 | to->tag.slen = 0; |
| 2526 | pj_list_init(&to->other_param); |
| 2527 | |
| 2528 | /* Re-init dialog remote info */ |
| 2529 | dlg->remote.info = (pjsip_to_hdr*) |
| 2530 | pjsip_hdr_clone(dlg->pool, to); |
| 2531 | |
| 2532 | /* Remove header param from remote info */ |
| 2533 | if (PJSIP_URI_SCHEME_IS_SIP(dlg->remote.info->uri) || |
| 2534 | PJSIP_URI_SCHEME_IS_SIPS(dlg->remote.info->uri)) |
| 2535 | { |
| 2536 | pjsip_sip_uri *sip_uri = (pjsip_sip_uri *) |
| 2537 | pjsip_uri_get_uri(dlg->remote.info->uri); |
| 2538 | if (!pj_list_empty(&sip_uri->header_param)) { |
| 2539 | /* Remove all header param */ |
| 2540 | pj_list_init(&sip_uri->header_param); |
| 2541 | } |
| 2542 | } |
| 2543 | |
| 2544 | /* Print the remote info. */ |
| 2545 | len = pjsip_uri_print(PJSIP_URI_IN_FROMTO_HDR, |
| 2546 | dlg->remote.info->uri, tmp, TMP_LEN); |
| 2547 | if (len < 1) { |
| 2548 | pj_ansi_strcpy(tmp, "<-error: uri too long->"); |
| 2549 | len = (int)pj_ansi_strlen(tmp); |
| 2550 | } |
| 2551 | pj_strdup2_with_null(dlg->pool, &dlg->remote.info_str, tmp); |
| 2552 | |
| 2553 | /* Secure? */ |
| 2554 | dlg->secure = PJSIP_URI_SCHEME_IS_SIPS(to->uri); |
| 2555 | } |
| 2556 | |
| 2557 | /* Reset message destination info (see #1248). */ |
| 2558 | pj_bzero(&tdata->dest_info, sizeof(tdata->dest_info)); |
| 2559 | |
| 2560 | /* Must invalidate the message! */ |
| 2561 | pjsip_tx_data_invalidate_msg(tdata); |
| 2562 | |
| 2563 | /* Reset the session */ |
| 2564 | pjsip_inv_uac_restart(inv, PJ_FALSE); |
| 2565 | |
| 2566 | /* (re)Send the INVITE request */ |
| 2567 | status = pjsip_inv_send_msg(inv, tdata); |
| 2568 | } |
| 2569 | break; |
| 2570 | |
| 2571 | case PJSIP_REDIRECT_STOP: |
| 2572 | /* User doesn't want the redirection. Disconnect the session now. */ |
| 2573 | inv_set_cause(inv, cancel_code, pjsip_get_status_text(cancel_code)); |
| 2574 | inv_set_state(inv, PJSIP_INV_STATE_DISCONNECTED, e); |
| 2575 | |
| 2576 | /* Caller should expect that the invite session is gone now, so |
| 2577 | * we don't need to set status to PJSIP_ESESSIONTERMINATED here. |
| 2578 | */ |
| 2579 | break; |
| 2580 | |
| 2581 | case PJSIP_REDIRECT_REJECT: |
| 2582 | /* Current target is rejected. Fetch next target if any. */ |
| 2583 | if (inv_uac_recurse(inv, cancel_code, NULL, NULL) == PJ_FALSE) { |
| 2584 | inv_set_cause(inv, cancel_code, |
| 2585 | pjsip_get_status_text(cancel_code)); |
| 2586 | inv_set_state(inv, PJSIP_INV_STATE_DISCONNECTED, e); |
| 2587 | |
| 2588 | /* Tell caller that the invite session is gone now */ |
| 2589 | status = PJSIP_ESESSIONTERMINATED; |
| 2590 | } |
| 2591 | break; |
| 2592 | |
| 2593 | |
| 2594 | case PJSIP_REDIRECT_PENDING: |
| 2595 | pj_assert(!"Should not happen"); |
| 2596 | break; |
| 2597 | } |
| 2598 | |
| 2599 | |
| 2600 | pjsip_dlg_dec_lock(inv->dlg); |
| 2601 | |
| 2602 | return status; |
| 2603 | } |
| 2604 | |
| 2605 | |
| 2606 | /* |
| 2607 | * Create re-INVITE. |
| 2608 | */ |
| 2609 | PJ_DEF(pj_status_t) pjsip_inv_reinvite( pjsip_inv_session *inv, |
| 2610 | const pj_str_t *new_contact, |
| 2611 | const pjmedia_sdp_session *new_offer, |
| 2612 | pjsip_tx_data **p_tdata ) |
| 2613 | { |
| 2614 | pj_status_t status; |
| 2615 | pjsip_contact_hdr *contact_hdr = NULL; |
| 2616 | |
| 2617 | /* Check arguments. */ |
| 2618 | PJ_ASSERT_RETURN(inv && p_tdata, PJ_EINVAL); |
| 2619 | |
| 2620 | /* Must NOT have a pending INVITE transaction */ |
| 2621 | if (inv->invite_tsx!=NULL) |
| 2622 | return PJ_EINVALIDOP; |
| 2623 | |
| 2624 | pj_log_push_indent(); |
| 2625 | |
| 2626 | pjsip_dlg_inc_lock(inv->dlg); |
| 2627 | |
| 2628 | if (new_contact) { |
| 2629 | pj_str_t tmp; |
| 2630 | const pj_str_t STR_CONTACT = { "Contact", 7 }; |
| 2631 | |
| 2632 | pj_strdup_with_null(inv->dlg->pool, &tmp, new_contact); |
| 2633 | contact_hdr = (pjsip_contact_hdr*) |
| 2634 | pjsip_parse_hdr(inv->dlg->pool, &STR_CONTACT, |
| 2635 | tmp.ptr, tmp.slen, NULL); |
| 2636 | if (!contact_hdr) { |
| 2637 | status = PJSIP_EINVALIDURI; |
| 2638 | goto on_return; |
| 2639 | } |
| 2640 | } |
| 2641 | |
| 2642 | |
| 2643 | if (new_offer) { |
| 2644 | if (!inv->neg) { |
| 2645 | status = pjmedia_sdp_neg_create_w_local_offer(inv->pool, |
| 2646 | new_offer, |
| 2647 | &inv->neg); |
| 2648 | if (status != PJ_SUCCESS) |
| 2649 | goto on_return; |
| 2650 | |
| 2651 | } else switch (pjmedia_sdp_neg_get_state(inv->neg)) { |
| 2652 | |
| 2653 | case PJMEDIA_SDP_NEG_STATE_NULL: |
| 2654 | pj_assert(!"Unexpected SDP neg state NULL"); |
| 2655 | status = PJ_EBUG; |
| 2656 | goto on_return; |
| 2657 | |
| 2658 | case PJMEDIA_SDP_NEG_STATE_LOCAL_OFFER: |
| 2659 | PJ_LOG(4,(inv->obj_name, |
| 2660 | "pjsip_inv_reinvite: already have an offer, new " |
| 2661 | "offer is ignored")); |
| 2662 | break; |
| 2663 | |
| 2664 | case PJMEDIA_SDP_NEG_STATE_REMOTE_OFFER: |
| 2665 | status = pjmedia_sdp_neg_set_local_answer(inv->pool_prov, |
| 2666 | inv->neg, |
| 2667 | new_offer); |
| 2668 | if (status != PJ_SUCCESS) |
| 2669 | goto on_return; |
| 2670 | break; |
| 2671 | |
| 2672 | case PJMEDIA_SDP_NEG_STATE_WAIT_NEGO: |
| 2673 | PJ_LOG(4,(inv->obj_name, |
| 2674 | "pjsip_inv_reinvite: SDP in WAIT_NEGO state, new " |
| 2675 | "offer is ignored")); |
| 2676 | break; |
| 2677 | |
| 2678 | case PJMEDIA_SDP_NEG_STATE_DONE: |
| 2679 | status = pjmedia_sdp_neg_modify_local_offer2( |
| 2680 | inv->pool_prov, inv->neg, |
| 2681 | inv->sdp_neg_flags, new_offer); |
| 2682 | if (status != PJ_SUCCESS) |
| 2683 | goto on_return; |
| 2684 | break; |
| 2685 | } |
| 2686 | } |
| 2687 | |
| 2688 | if (contact_hdr) |
| 2689 | inv->dlg->local.contact = contact_hdr; |
| 2690 | |
| 2691 | status = pjsip_inv_invite(inv, p_tdata); |
| 2692 | |
| 2693 | on_return: |
| 2694 | pjsip_dlg_dec_lock(inv->dlg); |
| 2695 | pj_log_pop_indent(); |
| 2696 | return status; |
| 2697 | } |
| 2698 | |
| 2699 | /* |
| 2700 | * Create UPDATE. |
| 2701 | */ |
| 2702 | PJ_DEF(pj_status_t) pjsip_inv_update ( pjsip_inv_session *inv, |
| 2703 | const pj_str_t *new_contact, |
| 2704 | const pjmedia_sdp_session *offer, |
| 2705 | pjsip_tx_data **p_tdata ) |
| 2706 | { |
| 2707 | pjsip_contact_hdr *contact_hdr = NULL; |
| 2708 | pjsip_tx_data *tdata = NULL; |
| 2709 | pjmedia_sdp_session *sdp_copy; |
| 2710 | const pjsip_hdr *hdr; |
| 2711 | pj_status_t status = PJ_SUCCESS; |
| 2712 | |
| 2713 | /* Verify arguments. */ |
| 2714 | PJ_ASSERT_RETURN(inv && p_tdata, PJ_EINVAL); |
| 2715 | |
| 2716 | /* Dialog must have been established */ |
| 2717 | PJ_ASSERT_RETURN(inv->dlg->state == PJSIP_DIALOG_STATE_ESTABLISHED, |
| 2718 | PJ_EINVALIDOP); |
| 2719 | |
| 2720 | /* Invite session must not have been disconnected */ |
| 2721 | PJ_ASSERT_RETURN(inv->state < PJSIP_INV_STATE_DISCONNECTED, |
| 2722 | PJ_EINVALIDOP); |
| 2723 | |
| 2724 | pj_log_push_indent(); |
| 2725 | |
| 2726 | /* Lock dialog. */ |
| 2727 | pjsip_dlg_inc_lock(inv->dlg); |
| 2728 | |
| 2729 | /* Process offer, if any */ |
| 2730 | if (offer) { |
| 2731 | if (pjmedia_sdp_neg_get_state(inv->neg)!=PJMEDIA_SDP_NEG_STATE_DONE) { |
| 2732 | PJ_LOG(4,(inv->dlg->obj_name, |
| 2733 | "Invalid SDP offer/answer state for UPDATE")); |
| 2734 | status = PJ_EINVALIDOP; |
| 2735 | goto on_error; |
| 2736 | } |
| 2737 | |
| 2738 | /* Notify negotiator about the new offer. This will fix the offer |
| 2739 | * with correct SDP origin. |
| 2740 | */ |
| 2741 | status = pjmedia_sdp_neg_modify_local_offer2(inv->pool_prov, inv->neg, |
| 2742 | inv->sdp_neg_flags, offer); |
| 2743 | if (status != PJ_SUCCESS) |
| 2744 | goto on_error; |
| 2745 | |
| 2746 | /* Retrieve the "fixed" offer from negotiator */ |
| 2747 | pjmedia_sdp_neg_get_neg_local(inv->neg, &offer); |
| 2748 | } |
| 2749 | |
| 2750 | /* Update Contact if required */ |
| 2751 | if (new_contact) { |
| 2752 | pj_str_t tmp; |
| 2753 | const pj_str_t STR_CONTACT = { "Contact", 7 }; |
| 2754 | |
| 2755 | pj_strdup_with_null(inv->dlg->pool, &tmp, new_contact); |
| 2756 | contact_hdr = (pjsip_contact_hdr*) |
| 2757 | pjsip_parse_hdr(inv->dlg->pool, &STR_CONTACT, |
| 2758 | tmp.ptr, tmp.slen, NULL); |
| 2759 | if (!contact_hdr) { |
| 2760 | status = PJSIP_EINVALIDURI; |
| 2761 | goto on_error; |
| 2762 | } |
| 2763 | |
| 2764 | inv->dlg->local.contact = contact_hdr; |
| 2765 | } |
| 2766 | |
| 2767 | /* Create request */ |
| 2768 | status = pjsip_dlg_create_request(inv->dlg, &pjsip_update_method, |
| 2769 | -1, &tdata); |
| 2770 | if (status != PJ_SUCCESS) |
| 2771 | goto on_error; |
| 2772 | |
| 2773 | /* Attach SDP body */ |
| 2774 | if (offer) { |
| 2775 | sdp_copy = pjmedia_sdp_session_clone(tdata->pool, offer); |
| 2776 | pjsip_create_sdp_body(tdata->pool, sdp_copy, &tdata->msg->body); |
| 2777 | } |
| 2778 | |
| 2779 | /* Session Timers spec (RFC 4028) says that Supported header MUST be put |
| 2780 | * in refresh requests. So here we'll just put the Supported header in |
| 2781 | * all cases regardless of whether session timers is used or not, just |
| 2782 | * in case this is a common behavior. |
| 2783 | */ |
| 2784 | hdr = pjsip_endpt_get_capability(inv->dlg->endpt, PJSIP_H_SUPPORTED, NULL); |
| 2785 | if (hdr) { |
| 2786 | pjsip_msg_add_hdr(tdata->msg, (pjsip_hdr*) |
| 2787 | pjsip_hdr_shallow_clone(tdata->pool, hdr)); |
| 2788 | } |
| 2789 | |
| 2790 | status = pjsip_timer_update_req(inv, tdata); |
| 2791 | if (status != PJ_SUCCESS) |
| 2792 | goto on_error; |
| 2793 | |
| 2794 | /* Unlock dialog. */ |
| 2795 | pjsip_dlg_dec_lock(inv->dlg); |
| 2796 | |
| 2797 | *p_tdata = tdata; |
| 2798 | |
| 2799 | pj_log_pop_indent(); |
| 2800 | return PJ_SUCCESS; |
| 2801 | |
| 2802 | on_error: |
| 2803 | if (tdata) |
| 2804 | pjsip_tx_data_dec_ref(tdata); |
| 2805 | |
| 2806 | /* Unlock dialog. */ |
| 2807 | pjsip_dlg_dec_lock(inv->dlg); |
| 2808 | |
| 2809 | pj_log_pop_indent(); |
| 2810 | return status; |
| 2811 | } |
| 2812 | |
| 2813 | /* |
| 2814 | * Create an ACK request. |
| 2815 | */ |
| 2816 | PJ_DEF(pj_status_t) pjsip_inv_create_ack(pjsip_inv_session *inv, |
| 2817 | int cseq, |
| 2818 | pjsip_tx_data **p_tdata) |
| 2819 | { |
| 2820 | const pjmedia_sdp_session *sdp = NULL; |
| 2821 | pj_status_t status; |
| 2822 | |
| 2823 | PJ_ASSERT_RETURN(inv && p_tdata, PJ_EINVAL); |
| 2824 | |
| 2825 | /* Lock dialog. */ |
| 2826 | pjsip_dlg_inc_lock(inv->dlg); |
| 2827 | |
| 2828 | /* Destroy last_ack */ |
| 2829 | if (inv->last_ack) { |
| 2830 | pjsip_tx_data_dec_ref(inv->last_ack); |
| 2831 | inv->last_ack = NULL; |
| 2832 | } |
| 2833 | |
| 2834 | /* Create new ACK request */ |
| 2835 | status = pjsip_dlg_create_request(inv->dlg, pjsip_get_ack_method(), |
| 2836 | cseq, &inv->last_ack); |
| 2837 | if (status != PJ_SUCCESS) { |
| 2838 | pjsip_dlg_dec_lock(inv->dlg); |
| 2839 | return status; |
| 2840 | } |
| 2841 | |
| 2842 | /* See if we have pending SDP answer to send */ |
| 2843 | sdp = inv_has_pending_answer(inv, inv->invite_tsx); |
| 2844 | if (sdp) { |
| 2845 | inv->last_ack->msg->body = create_sdp_body(inv->last_ack->pool, sdp); |
| 2846 | } |
| 2847 | |
| 2848 | /* Keep this for subsequent response retransmission */ |
| 2849 | inv->last_ack_cseq = cseq; |
| 2850 | pjsip_tx_data_add_ref(inv->last_ack); |
| 2851 | |
| 2852 | /* Done */ |
| 2853 | *p_tdata = inv->last_ack; |
| 2854 | |
| 2855 | /* Unlock dialog. */ |
| 2856 | pjsip_dlg_dec_lock(inv->dlg); |
| 2857 | |
| 2858 | return PJ_SUCCESS; |
| 2859 | } |
| 2860 | |
| 2861 | /* |
| 2862 | * Send a request or response message. |
| 2863 | */ |
| 2864 | PJ_DEF(pj_status_t) pjsip_inv_send_msg( pjsip_inv_session *inv, |
| 2865 | pjsip_tx_data *tdata) |
| 2866 | { |
| 2867 | pj_status_t status; |
| 2868 | |
| 2869 | /* Verify arguments. */ |
| 2870 | PJ_ASSERT_RETURN(inv && tdata, PJ_EINVAL); |
| 2871 | |
| 2872 | pj_log_push_indent(); |
| 2873 | |
| 2874 | PJ_LOG(5,(inv->obj_name, "Sending %s", |
| 2875 | pjsip_tx_data_get_info(tdata))); |
| 2876 | |
| 2877 | if (tdata->msg->type == PJSIP_REQUEST_MSG) { |
| 2878 | struct tsx_inv_data *tsx_inv_data; |
| 2879 | |
| 2880 | pjsip_dlg_inc_lock(inv->dlg); |
| 2881 | |
| 2882 | /* Check again that we didn't receive incoming re-INVITE */ |
| 2883 | if (tdata->msg->line.req.method.id==PJSIP_INVITE_METHOD && |
| 2884 | inv->invite_tsx) |
| 2885 | { |
| 2886 | pjsip_tx_data_dec_ref(tdata); |
| 2887 | pjsip_dlg_dec_lock(inv->dlg); |
| 2888 | status = PJ_EINVALIDOP; |
| 2889 | goto on_error; |
| 2890 | } |
| 2891 | |
| 2892 | /* Associate our data in outgoing invite transaction */ |
| 2893 | tsx_inv_data = PJ_POOL_ZALLOC_T(inv->pool, struct tsx_inv_data); |
| 2894 | tsx_inv_data->inv = inv; |
| 2895 | |
| 2896 | pjsip_dlg_dec_lock(inv->dlg); |
| 2897 | |
| 2898 | status = pjsip_dlg_send_request(inv->dlg, tdata, mod_inv.mod.id, |
| 2899 | tsx_inv_data); |
| 2900 | if (status != PJ_SUCCESS) { |
| 2901 | goto on_error; |
| 2902 | } |
| 2903 | |
| 2904 | } else { |
| 2905 | pjsip_cseq_hdr *cseq; |
| 2906 | |
| 2907 | /* Can only do this to send response to original INVITE |
| 2908 | * request. |
| 2909 | */ |
| 2910 | PJ_ASSERT_RETURN((cseq=(pjsip_cseq_hdr*)pjsip_msg_find_hdr(tdata->msg, PJSIP_H_CSEQ, NULL))!=NULL |
| 2911 | && (cseq->cseq == inv->invite_tsx->cseq), |
| 2912 | PJ_EINVALIDOP); |
| 2913 | |
| 2914 | if (inv->options & PJSIP_INV_REQUIRE_100REL) { |
| 2915 | status = pjsip_100rel_tx_response(inv, tdata); |
| 2916 | } else |
| 2917 | { |
| 2918 | status = pjsip_dlg_send_response(inv->dlg, inv->invite_tsx, tdata); |
| 2919 | } |
| 2920 | |
| 2921 | if (status != PJ_SUCCESS) { |
| 2922 | goto on_error; |
| 2923 | } |
| 2924 | } |
| 2925 | |
| 2926 | /* Done */ |
| 2927 | pj_log_pop_indent(); |
| 2928 | return PJ_SUCCESS; |
| 2929 | |
| 2930 | on_error: |
| 2931 | pj_log_pop_indent(); |
| 2932 | return status; |
| 2933 | } |
| 2934 | |
| 2935 | |
| 2936 | /* |
| 2937 | * Respond to incoming CANCEL request. |
| 2938 | */ |
| 2939 | static void inv_respond_incoming_cancel(pjsip_inv_session *inv, |
| 2940 | pjsip_transaction *cancel_tsx, |
| 2941 | pjsip_event *e) |
| 2942 | { |
| 2943 | pjsip_tx_data *tdata; |
| 2944 | pjsip_transaction *invite_tsx; |
| 2945 | pjsip_rx_data *rdata; |
| 2946 | pj_str_t key; |
| 2947 | pj_status_t status; |
| 2948 | |
| 2949 | pj_assert(e->body.tsx_state.type == PJSIP_EVENT_RX_MSG); |
| 2950 | rdata = e->body.tsx_state.src.rdata; |
| 2951 | |
| 2952 | /* https://trac.pjsip.org/repos/ticket/1651 |
| 2953 | * Special treatment for CANCEL. Since here we're responding to CANCEL |
| 2954 | * automatically (including 487 to INVITE), application will see the |
| 2955 | * 200/OK response to CANCEL first in the callback, and then 487 to |
| 2956 | * INVITE, before the CANCEL request itself. And worse, pjsua application |
| 2957 | * may not see the CANCEL request at all because by the time the CANCEL |
| 2958 | * request is reported, call has been disconnected and further events |
| 2959 | * from the INVITE session has been suppressed. |
| 2960 | */ |
| 2961 | if (mod_inv.cb.on_tsx_state_changed && inv->notify) |
| 2962 | (*mod_inv.cb.on_tsx_state_changed)(inv, cancel_tsx, e); |
| 2963 | |
| 2964 | /* See if we have matching INVITE server transaction: */ |
| 2965 | |
| 2966 | pjsip_tsx_create_key(rdata->tp_info.pool, &key, PJSIP_ROLE_UAS, |
| 2967 | pjsip_get_invite_method(), rdata); |
| 2968 | invite_tsx = pjsip_tsx_layer_find_tsx(&key, PJ_TRUE); |
| 2969 | |
| 2970 | if (invite_tsx == NULL) { |
| 2971 | |
| 2972 | /* Invite transaction not found! |
| 2973 | * Respond CANCEL with 481 (RFC 3261 Section 9.2 page 55) |
| 2974 | */ |
| 2975 | status = pjsip_dlg_create_response( inv->dlg, rdata, 481, NULL, |
| 2976 | &tdata); |
| 2977 | |
| 2978 | } else { |
| 2979 | /* Always answer CANCEL will 200 (OK) regardless of |
| 2980 | * the state of the INVITE transaction. |
| 2981 | */ |
| 2982 | status = pjsip_dlg_create_response( inv->dlg, rdata, 200, NULL, |
| 2983 | &tdata); |
| 2984 | } |
| 2985 | |
| 2986 | /* See if we have created the response successfully. */ |
| 2987 | if (status != PJ_SUCCESS) return; |
| 2988 | |
| 2989 | /* Send the CANCEL response */ |
| 2990 | status = pjsip_dlg_send_response(inv->dlg, cancel_tsx, tdata); |
| 2991 | if (status != PJ_SUCCESS) return; |
| 2992 | |
| 2993 | |
| 2994 | /* See if we need to terminate the UAS INVITE transaction |
| 2995 | * with 487 (Request Terminated) response. |
| 2996 | */ |
| 2997 | if (invite_tsx && invite_tsx->status_code < 200) { |
| 2998 | |
| 2999 | pj_assert(invite_tsx->last_tx != NULL); |
| 3000 | |
| 3001 | tdata = invite_tsx->last_tx; |
| 3002 | |
| 3003 | status = pjsip_dlg_modify_response(inv->dlg, tdata, 487, NULL); |
| 3004 | if (status == PJ_SUCCESS) { |
| 3005 | /* Remove the message body */ |
| 3006 | tdata->msg->body = NULL; |
| 3007 | if (inv->options & PJSIP_INV_REQUIRE_100REL) { |
| 3008 | status = pjsip_100rel_tx_response(inv, tdata); |
| 3009 | } else { |
| 3010 | status = pjsip_dlg_send_response(inv->dlg, invite_tsx, |
| 3011 | tdata); |
| 3012 | } |
| 3013 | } |
| 3014 | } |
| 3015 | |
| 3016 | if (invite_tsx) |
| 3017 | pj_grp_lock_release(invite_tsx->grp_lock); |
| 3018 | } |
| 3019 | |
| 3020 | |
| 3021 | /* |
| 3022 | * Respond to incoming BYE request. |
| 3023 | */ |
| 3024 | static void inv_respond_incoming_bye( pjsip_inv_session *inv, |
| 3025 | pjsip_transaction *bye_tsx, |
| 3026 | pjsip_rx_data *rdata, |
| 3027 | pjsip_event *e ) |
| 3028 | { |
| 3029 | pj_status_t status; |
| 3030 | pjsip_tx_data *tdata; |
| 3031 | |
| 3032 | /* Respond BYE with 200: */ |
| 3033 | |
| 3034 | status = pjsip_dlg_create_response(inv->dlg, rdata, 200, NULL, &tdata); |
| 3035 | if (status != PJ_SUCCESS) return; |
| 3036 | |
| 3037 | status = pjsip_dlg_send_response(inv->dlg, bye_tsx, tdata); |
| 3038 | if (status != PJ_SUCCESS) return; |
| 3039 | |
| 3040 | /* Terminate session: */ |
| 3041 | |
| 3042 | if (inv->state != PJSIP_INV_STATE_DISCONNECTED) { |
| 3043 | inv_set_cause(inv, PJSIP_SC_OK, NULL); |
| 3044 | inv_set_state(inv, PJSIP_INV_STATE_DISCONNECTED, e); |
| 3045 | } |
| 3046 | } |
| 3047 | |
| 3048 | /* |
| 3049 | * Respond to BYE request. |
| 3050 | */ |
| 3051 | static void inv_handle_bye_response( pjsip_inv_session *inv, |
| 3052 | pjsip_transaction *tsx, |
| 3053 | pjsip_rx_data *rdata, |
| 3054 | pjsip_event *e ) |
| 3055 | { |
| 3056 | pj_status_t status; |
| 3057 | |
| 3058 | if (e->body.tsx_state.type != PJSIP_EVENT_RX_MSG) { |
| 3059 | inv_set_cause(inv, PJSIP_SC_OK, NULL); |
| 3060 | inv_set_state(inv, PJSIP_INV_STATE_DISCONNECTED, e); |
| 3061 | return; |
| 3062 | } |
| 3063 | |
| 3064 | /* Handle 401/407 challenge. */ |
| 3065 | if (tsx->status_code == 401 || tsx->status_code == 407) { |
| 3066 | |
| 3067 | pjsip_tx_data *tdata; |
| 3068 | |
| 3069 | status = pjsip_auth_clt_reinit_req( &inv->dlg->auth_sess, |
| 3070 | rdata, |
| 3071 | tsx->last_tx, |
| 3072 | &tdata); |
| 3073 | |
| 3074 | if (status != PJ_SUCCESS) { |
| 3075 | |
| 3076 | /* Does not have proper credentials. |
| 3077 | * End the session anyway. |
| 3078 | */ |
| 3079 | inv_set_cause(inv, PJSIP_SC_OK, NULL); |
| 3080 | inv_set_state(inv, PJSIP_INV_STATE_DISCONNECTED, e); |
| 3081 | |
| 3082 | } else { |
| 3083 | struct tsx_inv_data *tsx_inv_data; |
| 3084 | |
| 3085 | tsx_inv_data = (struct tsx_inv_data*)tsx->mod_data[mod_inv.mod.id]; |
| 3086 | if (tsx_inv_data) |
| 3087 | tsx_inv_data->retrying = PJ_TRUE; |
| 3088 | |
| 3089 | /* Re-send BYE. */ |
| 3090 | status = pjsip_inv_send_msg(inv, tdata); |
| 3091 | } |
| 3092 | |
| 3093 | } else { |
| 3094 | |
| 3095 | /* End the session. */ |
| 3096 | inv_set_cause(inv, PJSIP_SC_OK, NULL); |
| 3097 | inv_set_state(inv, PJSIP_INV_STATE_DISCONNECTED, e); |
| 3098 | } |
| 3099 | |
| 3100 | } |
| 3101 | |
| 3102 | /* |
| 3103 | * Respond to incoming UPDATE request. |
| 3104 | */ |
| 3105 | static void inv_respond_incoming_update(pjsip_inv_session *inv, |
| 3106 | pjsip_rx_data *rdata) |
| 3107 | { |
| 3108 | pjmedia_sdp_neg_state neg_state; |
| 3109 | pj_status_t status; |
| 3110 | pjsip_tx_data *tdata = NULL; |
| 3111 | pjsip_status_code st_code; |
| 3112 | |
| 3113 | /* Invoke Session Timers module */ |
| 3114 | status = pjsip_timer_process_req(inv, rdata, &st_code); |
| 3115 | if (status != PJ_SUCCESS) { |
| 3116 | status = pjsip_dlg_create_response(inv->dlg, rdata, st_code, |
| 3117 | NULL, &tdata); |
| 3118 | goto on_return; |
| 3119 | } |
| 3120 | |
| 3121 | neg_state = pjmedia_sdp_neg_get_state(inv->neg); |
| 3122 | |
| 3123 | /* If UPDATE doesn't contain SDP, just respond with 200/OK. |
| 3124 | * This is a valid scenario according to session-timer draft. |
| 3125 | */ |
| 3126 | if (rdata->msg_info.msg->body == NULL) { |
| 3127 | |
| 3128 | status = pjsip_dlg_create_response(inv->dlg, rdata, |
| 3129 | 200, NULL, &tdata); |
| 3130 | } |
| 3131 | /* Send 491 if we receive UPDATE while we're waiting for an answer */ |
| 3132 | else if (neg_state == PJMEDIA_SDP_NEG_STATE_LOCAL_OFFER) { |
| 3133 | status = pjsip_dlg_create_response(inv->dlg, rdata, |
| 3134 | PJSIP_SC_REQUEST_PENDING, NULL, |
| 3135 | &tdata); |
| 3136 | } |
| 3137 | /* Send 500 with Retry-After header set randomly between 0 and 10 if we |
| 3138 | * receive UPDATE while we haven't sent answer. |
| 3139 | */ |
| 3140 | else if (neg_state == PJMEDIA_SDP_NEG_STATE_REMOTE_OFFER || |
| 3141 | neg_state == PJMEDIA_SDP_NEG_STATE_WAIT_NEGO) |
| 3142 | { |
| 3143 | pjsip_retry_after_hdr *ra_hdr; |
| 3144 | int val; |
| 3145 | |
| 3146 | status = pjsip_dlg_create_response(inv->dlg, rdata, |
| 3147 | PJSIP_SC_INTERNAL_SERVER_ERROR, |
| 3148 | NULL, &tdata); |
| 3149 | |
| 3150 | val = (pj_rand() % 10); |
| 3151 | ra_hdr = pjsip_retry_after_hdr_create(tdata->pool, val); |
| 3152 | pjsip_msg_add_hdr(tdata->msg, (pjsip_hdr*)ra_hdr); |
| 3153 | |
| 3154 | } else { |
| 3155 | /* We receive new offer from remote */ |
| 3156 | inv_check_sdp_in_incoming_msg(inv, pjsip_rdata_get_tsx(rdata), rdata); |
| 3157 | |
| 3158 | /* Application MUST have supplied the answer by now. |
| 3159 | * If so, negotiate the SDP. |
| 3160 | */ |
| 3161 | neg_state = pjmedia_sdp_neg_get_state(inv->neg); |
| 3162 | if (neg_state != PJMEDIA_SDP_NEG_STATE_WAIT_NEGO || |
| 3163 | (status=inv_negotiate_sdp(inv)) != PJ_SUCCESS) |
| 3164 | { |
| 3165 | /* Negotiation has failed. If negotiator is still |
| 3166 | * stuck at non-DONE state, cancel any ongoing offer. |
| 3167 | */ |
| 3168 | neg_state = pjmedia_sdp_neg_get_state(inv->neg); |
| 3169 | if (neg_state != PJMEDIA_SDP_NEG_STATE_DONE) { |
| 3170 | pjmedia_sdp_neg_cancel_offer(inv->neg); |
| 3171 | } |
| 3172 | |
| 3173 | status = pjsip_dlg_create_response(inv->dlg, rdata, |
| 3174 | PJSIP_SC_NOT_ACCEPTABLE_HERE, |
| 3175 | NULL, &tdata); |
| 3176 | } else { |
| 3177 | /* New media has been negotiated successfully, send 200/OK */ |
| 3178 | status = pjsip_dlg_create_response(inv->dlg, rdata, |
| 3179 | PJSIP_SC_OK, NULL, &tdata); |
| 3180 | if (status == PJ_SUCCESS) { |
| 3181 | const pjmedia_sdp_session *sdp; |
| 3182 | status = pjmedia_sdp_neg_get_active_local(inv->neg, &sdp); |
| 3183 | if (status == PJ_SUCCESS) |
| 3184 | tdata->msg->body = create_sdp_body(tdata->pool, sdp); |
| 3185 | } |
| 3186 | } |
| 3187 | } |
| 3188 | |
| 3189 | on_return: |
| 3190 | /* Invoke Session Timers */ |
| 3191 | if (status == PJ_SUCCESS) |
| 3192 | status = pjsip_timer_update_resp(inv, tdata); |
| 3193 | |
| 3194 | if (status != PJ_SUCCESS) { |
| 3195 | if (tdata != NULL) { |
| 3196 | pjsip_tx_data_dec_ref(tdata); |
| 3197 | tdata = NULL; |
| 3198 | } |
| 3199 | return; |
| 3200 | } |
| 3201 | |
| 3202 | pjsip_dlg_send_response(inv->dlg, pjsip_rdata_get_tsx(rdata), tdata); |
| 3203 | } |
| 3204 | |
| 3205 | |
| 3206 | /* |
| 3207 | * Handle incoming response to UAC UPDATE request. |
| 3208 | */ |
| 3209 | static pj_bool_t inv_handle_update_response( pjsip_inv_session *inv, |
| 3210 | pjsip_event *e) |
| 3211 | { |
| 3212 | pjsip_transaction *tsx = e->body.tsx_state.tsx; |
| 3213 | struct tsx_inv_data *tsx_inv_data; |
| 3214 | pj_bool_t handled = PJ_FALSE; |
| 3215 | pj_status_t status = -1; |
| 3216 | |
| 3217 | tsx_inv_data = (struct tsx_inv_data*)tsx->mod_data[mod_inv.mod.id]; |
| 3218 | pj_assert(tsx_inv_data); |
| 3219 | |
| 3220 | /* Handle 401/407 challenge. */ |
| 3221 | if (tsx->state == PJSIP_TSX_STATE_COMPLETED && |
| 3222 | (tsx->status_code == 401 || tsx->status_code == 407)) |
| 3223 | { |
| 3224 | pjsip_tx_data *tdata; |
| 3225 | |
| 3226 | status = pjsip_auth_clt_reinit_req( &inv->dlg->auth_sess, |
| 3227 | e->body.tsx_state.src.rdata, |
| 3228 | tsx->last_tx, |
| 3229 | &tdata); |
| 3230 | |
| 3231 | if (status != PJ_SUCCESS) { |
| 3232 | |
| 3233 | /* Somehow failed. Probably it's not a good idea to terminate |
| 3234 | * the session since this is just a request within dialog. And |
| 3235 | * even if we terminate we should send BYE. |
| 3236 | */ |
| 3237 | /* |
| 3238 | inv_set_cause(inv, PJSIP_SC_OK, NULL); |
| 3239 | inv_set_state(inv, PJSIP_INV_STATE_DISCONNECTED, e); |
| 3240 | */ |
| 3241 | |
| 3242 | } else { |
| 3243 | if (tsx_inv_data) |
| 3244 | tsx_inv_data->retrying = PJ_TRUE; |
| 3245 | |
| 3246 | /* Re-send request. */ |
| 3247 | status = pjsip_inv_send_msg(inv, tdata); |
| 3248 | } |
| 3249 | |
| 3250 | handled = PJ_TRUE; |
| 3251 | } |
| 3252 | |
| 3253 | /* Process 422 response */ |
| 3254 | else if (tsx->state == PJSIP_TSX_STATE_COMPLETED && |
| 3255 | tsx->status_code == 422) |
| 3256 | { |
| 3257 | status = handle_timer_response(inv, e->body.tsx_state.src.rdata, |
| 3258 | PJ_FALSE); |
| 3259 | handled = PJ_TRUE; |
| 3260 | } |
| 3261 | |
| 3262 | /* Process 2xx response */ |
| 3263 | else if (tsx->state == PJSIP_TSX_STATE_COMPLETED && |
| 3264 | tsx->status_code/100 == 2 && |
| 3265 | e->body.tsx_state.src.rdata->msg_info.msg->body) |
| 3266 | { |
| 3267 | status = handle_timer_response(inv, e->body.tsx_state.src.rdata, |
| 3268 | PJ_FALSE); |
| 3269 | status = inv_check_sdp_in_incoming_msg(inv, tsx, |
| 3270 | e->body.tsx_state.src.rdata); |
| 3271 | handled = PJ_TRUE; |
| 3272 | } |
| 3273 | |
| 3274 | /* Get/attach invite session's transaction data */ |
| 3275 | else |
| 3276 | { |
| 3277 | /* Session-Timer needs to see any error responses, to determine |
| 3278 | * whether peer supports UPDATE with empty body. |
| 3279 | */ |
| 3280 | if (tsx->state == PJSIP_TSX_STATE_COMPLETED && |
| 3281 | tsx->role == PJSIP_ROLE_UAC) |
| 3282 | { |
| 3283 | status = handle_timer_response(inv, e->body.tsx_state.src.rdata, |
| 3284 | PJ_FALSE); |
| 3285 | handled = PJ_TRUE; |
| 3286 | } |
| 3287 | } |
| 3288 | |
| 3289 | /* Cancel the negotiation if we don't get successful negotiation by now, |
| 3290 | * unless it's authentication challenge and the request is being retried. |
| 3291 | */ |
| 3292 | if (pjmedia_sdp_neg_get_state(inv->neg) == |
| 3293 | PJMEDIA_SDP_NEG_STATE_LOCAL_OFFER && |
| 3294 | tsx_inv_data && tsx_inv_data->sdp_done == PJ_FALSE && |
| 3295 | !tsx_inv_data->retrying) |
| 3296 | { |
| 3297 | pjmedia_sdp_neg_cancel_offer(inv->neg); |
| 3298 | |
| 3299 | /* Prevent from us cancelling different offer! */ |
| 3300 | tsx_inv_data->sdp_done = PJ_TRUE; |
| 3301 | } |
| 3302 | |
| 3303 | return handled; |
| 3304 | } |
| 3305 | |
| 3306 | |
| 3307 | /* |
| 3308 | * Handle incoming reliable response. |
| 3309 | */ |
| 3310 | static void inv_handle_incoming_reliable_response(pjsip_inv_session *inv, |
| 3311 | pjsip_rx_data *rdata) |
| 3312 | { |
| 3313 | pjsip_tx_data *tdata; |
| 3314 | const pjmedia_sdp_session *sdp; |
| 3315 | pj_status_t status; |
| 3316 | |
| 3317 | /* Create PRACK */ |
| 3318 | status = pjsip_100rel_create_prack(inv, rdata, &tdata); |
| 3319 | if (status != PJ_SUCCESS) |
| 3320 | return; |
| 3321 | |
| 3322 | /* See if we need to attach SDP answer on the PRACK request */ |
| 3323 | sdp = inv_has_pending_answer(inv, pjsip_rdata_get_tsx(rdata)); |
| 3324 | if (sdp) { |
| 3325 | tdata->msg->body = create_sdp_body(tdata->pool, sdp); |
| 3326 | } |
| 3327 | |
| 3328 | /* Send PRACK (must be using 100rel module!) */ |
| 3329 | pjsip_100rel_send_prack(inv, tdata); |
| 3330 | } |
| 3331 | |
| 3332 | |
| 3333 | /* |
| 3334 | * Handle incoming PRACK. |
| 3335 | */ |
| 3336 | static void inv_respond_incoming_prack(pjsip_inv_session *inv, |
| 3337 | pjsip_rx_data *rdata) |
| 3338 | { |
| 3339 | pj_status_t status; |
| 3340 | |
| 3341 | /* Run through 100rel module to see if we can accept this |
| 3342 | * PRACK request. The 100rel will send 200/OK to PRACK request. |
| 3343 | */ |
| 3344 | status = pjsip_100rel_on_rx_prack(inv, rdata); |
| 3345 | if (status != PJ_SUCCESS) |
| 3346 | return; |
| 3347 | |
| 3348 | /* Now check for SDP answer in the PRACK request */ |
| 3349 | if (rdata->msg_info.msg->body) { |
| 3350 | status = inv_check_sdp_in_incoming_msg(inv, |
| 3351 | pjsip_rdata_get_tsx(rdata), rdata); |
| 3352 | } else { |
| 3353 | /* No SDP body */ |
| 3354 | status = -1; |
| 3355 | } |
| 3356 | |
| 3357 | /* If SDP negotiation has been successful, also mark the |
| 3358 | * SDP negotiation flag in the invite transaction to be |
| 3359 | * done too. |
| 3360 | */ |
| 3361 | if (status == PJ_SUCCESS && inv->invite_tsx) { |
| 3362 | struct tsx_inv_data *tsx_inv_data; |
| 3363 | |
| 3364 | /* Get/attach invite session's transaction data */ |
| 3365 | tsx_inv_data = (struct tsx_inv_data*) |
| 3366 | inv->invite_tsx->mod_data[mod_inv.mod.id]; |
| 3367 | if (tsx_inv_data == NULL) { |
| 3368 | tsx_inv_data = PJ_POOL_ZALLOC_T(inv->invite_tsx->pool, |
| 3369 | struct tsx_inv_data); |
| 3370 | tsx_inv_data->inv = inv; |
| 3371 | inv->invite_tsx->mod_data[mod_inv.mod.id] = tsx_inv_data; |
| 3372 | } |
| 3373 | |
| 3374 | tsx_inv_data->sdp_done = PJ_TRUE; |
| 3375 | } |
| 3376 | } |
| 3377 | |
| 3378 | |
| 3379 | /* |
| 3380 | * State NULL is before anything is sent/received. |
| 3381 | */ |
| 3382 | static void inv_on_state_null( pjsip_inv_session *inv, pjsip_event *e) |
| 3383 | { |
| 3384 | pjsip_transaction *tsx = e->body.tsx_state.tsx; |
| 3385 | pjsip_dialog *dlg = pjsip_tsx_get_dlg(tsx); |
| 3386 | |
| 3387 | PJ_ASSERT_ON_FAIL(tsx && dlg, return); |
| 3388 | |
| 3389 | if (tsx->method.id == PJSIP_INVITE_METHOD) { |
| 3390 | |
| 3391 | /* Keep the initial INVITE transaction. */ |
| 3392 | if (inv->invite_tsx == NULL) |
| 3393 | inv->invite_tsx = tsx; |
| 3394 | |
| 3395 | if (dlg->role == PJSIP_ROLE_UAC) { |
| 3396 | |
| 3397 | /* Save the original INVITE request. |
| 3398 | * We may need to resend the INVITE if we receive redirection |
| 3399 | * or session timer too small response. |
| 3400 | */ |
| 3401 | if (1) { |
| 3402 | if (inv->invite_req) { |
| 3403 | pjsip_tx_data_dec_ref(inv->invite_req); |
| 3404 | inv->invite_req = NULL; |
| 3405 | } |
| 3406 | inv->invite_req = tsx->last_tx; |
| 3407 | pjsip_tx_data_add_ref(inv->invite_req); |
| 3408 | } |
| 3409 | |
| 3410 | switch (tsx->state) { |
| 3411 | case PJSIP_TSX_STATE_CALLING: |
| 3412 | inv_set_state(inv, PJSIP_INV_STATE_CALLING, e); |
| 3413 | break; |
| 3414 | default: |
| 3415 | inv_on_state_calling(inv, e); |
| 3416 | break; |
| 3417 | } |
| 3418 | |
| 3419 | } else { |
| 3420 | switch (tsx->state) { |
| 3421 | case PJSIP_TSX_STATE_TRYING: |
| 3422 | inv_set_state(inv, PJSIP_INV_STATE_INCOMING, e); |
| 3423 | break; |
| 3424 | case PJSIP_TSX_STATE_PROCEEDING: |
| 3425 | inv_set_state(inv, PJSIP_INV_STATE_INCOMING, e); |
| 3426 | if (tsx->status_code > 100) |
| 3427 | inv_set_state(inv, PJSIP_INV_STATE_EARLY, e); |
| 3428 | break; |
| 3429 | case PJSIP_TSX_STATE_TERMINATED: |
| 3430 | /* there is a failure in sending response. */ |
| 3431 | inv_set_cause(inv, tsx->status_code, &tsx->status_text); |
| 3432 | inv_set_state(inv, PJSIP_INV_STATE_DISCONNECTED, e); |
| 3433 | break; |
| 3434 | default: |
| 3435 | inv_on_state_incoming(inv, e); |
| 3436 | break; |
| 3437 | } |
| 3438 | } |
| 3439 | |
| 3440 | } else { |
| 3441 | pj_assert(!"Unexpected transaction type"); |
| 3442 | } |
| 3443 | } |
| 3444 | |
| 3445 | /* |
| 3446 | * Generic UAC transaction handler: |
| 3447 | * - resend request on 401 or 407 response. |
| 3448 | * - terminate dialog on 408 and 481 response. |
| 3449 | * - resend request on 422 response. |
| 3450 | */ |
| 3451 | static pj_bool_t handle_uac_tsx_response(pjsip_inv_session *inv, |
| 3452 | pjsip_event *e) |
| 3453 | { |
| 3454 | /* RFC 3261 Section 12.2.1.2: |
| 3455 | * If the response for a request within a dialog is a 481 |
| 3456 | * (Call/Transaction Does Not Exist) or a 408 (Request Timeout), the UAC |
| 3457 | * SHOULD terminate the dialog. A UAC SHOULD also terminate a dialog if |
| 3458 | * no response at all is received for the request (the client |
| 3459 | * transaction would inform the TU about the timeout.) |
| 3460 | * |
| 3461 | * For INVITE initiated dialogs, terminating the dialog consists of |
| 3462 | * sending a BYE. |
| 3463 | * |
| 3464 | * Note: |
| 3465 | * according to X, this should terminate dialog usage only, not the |
| 3466 | * dialog. |
| 3467 | */ |
| 3468 | pjsip_transaction *tsx = e->body.tsx_state.tsx; |
| 3469 | |
| 3470 | pj_assert(tsx->role == PJSIP_UAC_ROLE); |
| 3471 | |
| 3472 | /* Note that 481 response to CANCEL does not terminate dialog usage, |
| 3473 | * but only the transaction. |
| 3474 | */ |
| 3475 | if (inv->state != PJSIP_INV_STATE_DISCONNECTED && |
| 3476 | ((tsx->status_code == PJSIP_SC_CALL_TSX_DOES_NOT_EXIST && |
| 3477 | tsx->method.id != PJSIP_CANCEL_METHOD) || |
| 3478 | tsx->status_code == PJSIP_SC_REQUEST_TIMEOUT || |
| 3479 | tsx->status_code == PJSIP_SC_TSX_TIMEOUT)) |
| 3480 | { |
| 3481 | pjsip_tx_data *bye; |
| 3482 | pj_status_t status; |
| 3483 | |
| 3484 | inv_set_cause(inv, tsx->status_code, &tsx->status_text); |
| 3485 | inv_set_state(inv, PJSIP_INV_STATE_DISCONNECTED, e); |
| 3486 | |
| 3487 | /* Send BYE */ |
| 3488 | status = pjsip_dlg_create_request(inv->dlg, pjsip_get_bye_method(), |
| 3489 | -1, &bye); |
| 3490 | if (status == PJ_SUCCESS) { |
| 3491 | pjsip_inv_send_msg(inv, bye); |
| 3492 | } |
| 3493 | |
| 3494 | return PJ_TRUE; /* Handled */ |
| 3495 | |
| 3496 | } |
| 3497 | /* Handle 401/407 challenge. */ |
| 3498 | else if (tsx->state == PJSIP_TSX_STATE_COMPLETED && |
| 3499 | (tsx->status_code == PJSIP_SC_UNAUTHORIZED || |
| 3500 | tsx->status_code == PJSIP_SC_PROXY_AUTHENTICATION_REQUIRED)) |
| 3501 | { |
| 3502 | pjsip_tx_data *tdata; |
| 3503 | pj_status_t status; |
| 3504 | |
| 3505 | if (tsx->method.id == PJSIP_INVITE_METHOD) |
| 3506 | inv->invite_tsx = NULL; |
| 3507 | |
| 3508 | status = pjsip_auth_clt_reinit_req( &inv->dlg->auth_sess, |
| 3509 | e->body.tsx_state.src.rdata, |
| 3510 | tsx->last_tx, &tdata); |
| 3511 | |
| 3512 | if (status != PJ_SUCCESS) { |
| 3513 | /* Somehow failed. Probably it's not a good idea to terminate |
| 3514 | * the session since this is just a request within dialog. And |
| 3515 | * even if we terminate we should send BYE. |
| 3516 | */ |
| 3517 | /* |
| 3518 | inv_set_cause(inv, PJSIP_SC_OK, NULL); |
| 3519 | inv_set_state(inv, PJSIP_INV_STATE_DISCONNECTED, e); |
| 3520 | */ |
| 3521 | |
| 3522 | } else { |
| 3523 | struct tsx_inv_data *tsx_inv_data; |
| 3524 | |
| 3525 | tsx_inv_data = (struct tsx_inv_data*)tsx->mod_data[mod_inv.mod.id]; |
| 3526 | if (tsx_inv_data) |
| 3527 | tsx_inv_data->retrying = PJ_TRUE; |
| 3528 | |
| 3529 | /* Re-send request. */ |
| 3530 | status = pjsip_inv_send_msg(inv, tdata); |
| 3531 | } |
| 3532 | |
| 3533 | return PJ_TRUE; /* Handled */ |
| 3534 | } |
| 3535 | |
| 3536 | /* Handle session timer 422 response. */ |
| 3537 | else if (tsx->state == PJSIP_TSX_STATE_COMPLETED && |
| 3538 | tsx->status_code == PJSIP_SC_SESSION_TIMER_TOO_SMALL) |
| 3539 | { |
| 3540 | handle_timer_response(inv, e->body.tsx_state.src.rdata, |
| 3541 | PJ_FALSE); |
| 3542 | |
| 3543 | return PJ_TRUE; /* Handled */ |
| 3544 | |
| 3545 | } else { |
| 3546 | return PJ_FALSE; /* Unhandled */ |
| 3547 | } |
| 3548 | } |
| 3549 | |
| 3550 | |
| 3551 | /* Handle call rejection, especially with regard to processing call |
| 3552 | * redirection. We need to handle the following scenarios: |
| 3553 | * - 3xx response is received -- see if on_redirected() callback is |
| 3554 | * implemented. If so, add the Contact URIs in the response to the |
| 3555 | * target set and notify user. |
| 3556 | * - 4xx - 6xx resposne is received -- see if we're currently recursing, |
| 3557 | * if so fetch the next target if any and notify the on_redirected() |
| 3558 | * callback. |
| 3559 | * - for other cases -- disconnect the session. |
| 3560 | */ |
| 3561 | static void handle_uac_call_rejection(pjsip_inv_session *inv, pjsip_event *e) |
| 3562 | { |
| 3563 | pjsip_transaction *tsx = e->body.tsx_state.tsx; |
| 3564 | pj_status_t status; |
| 3565 | |
| 3566 | if (PJSIP_IS_STATUS_IN_CLASS(tsx->status_code, 300)) { |
| 3567 | |
| 3568 | if (mod_inv.cb.on_redirected == NULL) { |
| 3569 | |
| 3570 | /* Redirection callback is not implemented, disconnect the |
| 3571 | * call. |
| 3572 | */ |
| 3573 | goto terminate_session; |
| 3574 | |
| 3575 | } else { |
| 3576 | const pjsip_msg *res_msg; |
| 3577 | |
| 3578 | res_msg = e->body.tsx_state.src.rdata->msg_info.msg; |
| 3579 | |
| 3580 | /* Gather all Contact URI's in the response and add them |
| 3581 | * to target set. The function will take care of removing |
| 3582 | * duplicate URI's. |
| 3583 | */ |
| 3584 | pjsip_target_set_add_from_msg(&inv->dlg->target_set, |
| 3585 | inv->dlg->pool, res_msg); |
| 3586 | |
| 3587 | /* Recurse to alternate targets if application allows us */ |
| 3588 | if (!inv_uac_recurse(inv, tsx->status_code, &tsx->status_text, e)) |
| 3589 | { |
| 3590 | /* Recursion fails, terminate session now */ |
| 3591 | goto terminate_session; |
| 3592 | } |
| 3593 | |
| 3594 | /* Done */ |
| 3595 | } |
| 3596 | |
| 3597 | } else if ((tsx->status_code==401 || tsx->status_code==407) && |
| 3598 | !inv->cancelling) |
| 3599 | { |
| 3600 | |
| 3601 | /* Handle authentication failure: |
| 3602 | * Resend the request with Authorization header. |
| 3603 | */ |
| 3604 | pjsip_tx_data *tdata; |
| 3605 | |
| 3606 | status = pjsip_auth_clt_reinit_req(&inv->dlg->auth_sess, |
| 3607 | e->body.tsx_state.src.rdata, |
| 3608 | tsx->last_tx, |
| 3609 | &tdata); |
| 3610 | |
| 3611 | if (status != PJ_SUCCESS) { |
| 3612 | |
| 3613 | /* Does not have proper credentials. If we are currently |
| 3614 | * recursing, try the next target. Otherwise end the session. |
| 3615 | */ |
| 3616 | if (!inv_uac_recurse(inv, tsx->status_code, &tsx->status_text, e)) |
| 3617 | { |
| 3618 | /* Recursion fails, terminate session now */ |
| 3619 | goto terminate_session; |
| 3620 | } |
| 3621 | |
| 3622 | } else { |
| 3623 | |
| 3624 | /* Restart session. */ |
| 3625 | pjsip_inv_uac_restart(inv, PJ_FALSE); |
| 3626 | |
| 3627 | /* Send the request. */ |
| 3628 | status = pjsip_inv_send_msg(inv, tdata); |
| 3629 | } |
| 3630 | |
| 3631 | } else if (tsx->state == PJSIP_TSX_STATE_COMPLETED && |
| 3632 | tsx->status_code == PJSIP_SC_SESSION_TIMER_TOO_SMALL) |
| 3633 | { |
| 3634 | /* Handle session timer 422 response: |
| 3635 | * Resend the request with requested session timer setting. |
| 3636 | */ |
| 3637 | status = handle_timer_response(inv, e->body.tsx_state.src.rdata, |
| 3638 | PJ_TRUE); |
| 3639 | |
| 3640 | } else if (PJSIP_IS_STATUS_IN_CLASS(tsx->status_code, 600)) { |
| 3641 | /* Global error */ |
| 3642 | goto terminate_session; |
| 3643 | |
| 3644 | } else { |
| 3645 | /* See if we have alternate target to try */ |
| 3646 | if (!inv_uac_recurse(inv, tsx->status_code, &tsx->status_text, e)) { |
| 3647 | /* Recursion fails, terminate session now */ |
| 3648 | goto terminate_session; |
| 3649 | } |
| 3650 | } |
| 3651 | return; |
| 3652 | |
| 3653 | terminate_session: |
| 3654 | inv_set_cause(inv, tsx->status_code, &tsx->status_text); |
| 3655 | inv_set_state(inv, PJSIP_INV_STATE_DISCONNECTED, e); |
| 3656 | } |
| 3657 | |
| 3658 | |
| 3659 | /* |
| 3660 | * State CALLING is after sending initial INVITE request but before |
| 3661 | * any response (with tag) is received. |
| 3662 | */ |
| 3663 | static void inv_on_state_calling( pjsip_inv_session *inv, pjsip_event *e) |
| 3664 | { |
| 3665 | pjsip_transaction *tsx = e->body.tsx_state.tsx; |
| 3666 | pjsip_dialog *dlg = pjsip_tsx_get_dlg(tsx); |
| 3667 | pj_status_t status; |
| 3668 | |
| 3669 | PJ_ASSERT_ON_FAIL(tsx && dlg, return); |
| 3670 | |
| 3671 | if (tsx == inv->invite_tsx) { |
| 3672 | |
| 3673 | switch (tsx->state) { |
| 3674 | |
| 3675 | case PJSIP_TSX_STATE_CALLING: |
| 3676 | inv_set_state(inv, PJSIP_INV_STATE_CALLING, e); |
| 3677 | break; |
| 3678 | |
| 3679 | case PJSIP_TSX_STATE_PROCEEDING: |
| 3680 | if (inv->pending_cancel) { |
| 3681 | pjsip_tx_data *cancel; |
| 3682 | |
| 3683 | inv->pending_cancel = PJ_FALSE; |
| 3684 | |
| 3685 | status = pjsip_inv_end_session(inv, 487, NULL, &cancel); |
| 3686 | if (status == PJ_SUCCESS && cancel) |
| 3687 | status = pjsip_inv_send_msg(inv, cancel); |
| 3688 | } |
| 3689 | |
| 3690 | if (dlg->remote.info->tag.slen) { |
| 3691 | |
| 3692 | inv_set_state(inv, PJSIP_INV_STATE_EARLY, e); |
| 3693 | |
| 3694 | inv_check_sdp_in_incoming_msg(inv, tsx, |
| 3695 | e->body.tsx_state.src.rdata); |
| 3696 | |
| 3697 | if (pjsip_100rel_is_reliable(e->body.tsx_state.src.rdata)) { |
| 3698 | inv_handle_incoming_reliable_response( |
| 3699 | inv, e->body.tsx_state.src.rdata); |
| 3700 | } |
| 3701 | |
| 3702 | } else { |
| 3703 | /* Ignore 100 (Trying) response, as it doesn't change |
| 3704 | * session state. It only ceases retransmissions. |
| 3705 | */ |
| 3706 | } |
| 3707 | break; |
| 3708 | |
| 3709 | case PJSIP_TSX_STATE_COMPLETED: |
| 3710 | if (tsx->status_code/100 == 2) { |
| 3711 | |
| 3712 | /* This should not happen. |
| 3713 | * When transaction receives 2xx, it should be terminated |
| 3714 | */ |
| 3715 | pj_assert(0); |
| 3716 | |
| 3717 | /* Process session timer response. */ |
| 3718 | status = handle_timer_response(inv, |
| 3719 | e->body.tsx_state.src.rdata, |
| 3720 | PJ_TRUE); |
| 3721 | if (status != PJ_SUCCESS) |
| 3722 | break; |
| 3723 | |
| 3724 | inv_set_state(inv, PJSIP_INV_STATE_CONNECTING, e); |
| 3725 | |
| 3726 | inv_check_sdp_in_incoming_msg(inv, tsx, |
| 3727 | e->body.tsx_state.src.rdata); |
| 3728 | |
| 3729 | } else { |
| 3730 | handle_uac_call_rejection(inv, e); |
| 3731 | } |
| 3732 | break; |
| 3733 | |
| 3734 | case PJSIP_TSX_STATE_TERMINATED: |
| 3735 | /* INVITE transaction can be terminated either because UAC |
| 3736 | * transaction received 2xx response or because of transport |
| 3737 | * error. |
| 3738 | */ |
| 3739 | if (tsx->status_code/100 == 2) { |
| 3740 | /* This must be receipt of 2xx response */ |
| 3741 | |
| 3742 | /* Process session timer response. */ |
| 3743 | status = handle_timer_response(inv, |
| 3744 | e->body.tsx_state.src.rdata, |
| 3745 | PJ_TRUE); |
| 3746 | if (status != PJ_SUCCESS) |
| 3747 | break; |
| 3748 | |
| 3749 | /* Set state to CONNECTING */ |
| 3750 | inv_set_state(inv, PJSIP_INV_STATE_CONNECTING, e); |
| 3751 | |
| 3752 | inv_check_sdp_in_incoming_msg(inv, tsx, |
| 3753 | e->body.tsx_state.src.rdata); |
| 3754 | |
| 3755 | /* Send ACK */ |
| 3756 | pj_assert(e->body.tsx_state.type == PJSIP_EVENT_RX_MSG); |
| 3757 | |
| 3758 | inv_send_ack(inv, e); |
| 3759 | |
| 3760 | } else { |
| 3761 | inv_set_cause(inv, tsx->status_code, &tsx->status_text); |
| 3762 | inv_set_state(inv, PJSIP_INV_STATE_DISCONNECTED, e); |
| 3763 | } |
| 3764 | break; |
| 3765 | |
| 3766 | default: |
| 3767 | break; |
| 3768 | } |
| 3769 | |
| 3770 | } else if (tsx->role == PJSIP_ROLE_UAC) { |
| 3771 | /* |
| 3772 | * Handle case when outgoing request is answered with 481 (Call/ |
| 3773 | * Transaction Does Not Exist), 408, or when it's timed out. In these |
| 3774 | * cases, disconnect session (i.e. dialog usage only). |
| 3775 | * Note that 481 response to CANCEL does not terminate dialog usage, |
| 3776 | * but only the transaction. |
| 3777 | */ |
| 3778 | if ((tsx->status_code == PJSIP_SC_CALL_TSX_DOES_NOT_EXIST && |
| 3779 | tsx->method.id != PJSIP_CANCEL_METHOD) || |
| 3780 | tsx->status_code == PJSIP_SC_REQUEST_TIMEOUT || |
| 3781 | tsx->status_code == PJSIP_SC_TSX_TIMEOUT || |
| 3782 | tsx->status_code == PJSIP_SC_TSX_TRANSPORT_ERROR) |
| 3783 | { |
| 3784 | inv_set_cause(inv, tsx->status_code, &tsx->status_text); |
| 3785 | inv_set_state(inv, PJSIP_INV_STATE_DISCONNECTED, e); |
| 3786 | } |
| 3787 | } else if (tsx->role == PJSIP_ROLE_UAS && |
| 3788 | tsx->state == PJSIP_TSX_STATE_TRYING && |
| 3789 | pjsip_method_cmp(&tsx->method, &pjsip_update_method)==0) |
| 3790 | { |
| 3791 | /* |
| 3792 | * Handle a very early UPDATE |
| 3793 | */ |
| 3794 | inv_respond_incoming_update(inv, e->body.tsx_state.src.rdata); |
| 3795 | |
| 3796 | |
| 3797 | } |
| 3798 | } |
| 3799 | |
| 3800 | /* |
| 3801 | * State INCOMING is after we received the request, but before |
| 3802 | * responses with tag are sent. |
| 3803 | */ |
| 3804 | static void inv_on_state_incoming( pjsip_inv_session *inv, pjsip_event *e) |
| 3805 | { |
| 3806 | pjsip_transaction *tsx = e->body.tsx_state.tsx; |
| 3807 | pjsip_dialog *dlg = pjsip_tsx_get_dlg(tsx); |
| 3808 | |
| 3809 | PJ_ASSERT_ON_FAIL(tsx && dlg, return); |
| 3810 | |
| 3811 | if (tsx == inv->invite_tsx) { |
| 3812 | |
| 3813 | /* |
| 3814 | * Handle the INVITE state transition. |
| 3815 | */ |
| 3816 | |
| 3817 | switch (tsx->state) { |
| 3818 | |
| 3819 | case PJSIP_TSX_STATE_TRYING: |
| 3820 | inv_set_state(inv, PJSIP_INV_STATE_INCOMING, e); |
| 3821 | break; |
| 3822 | |
| 3823 | case PJSIP_TSX_STATE_PROCEEDING: |
| 3824 | /* |
| 3825 | * Transaction sent provisional response. |
| 3826 | */ |
| 3827 | if (tsx->status_code > 100) |
| 3828 | inv_set_state(inv, PJSIP_INV_STATE_EARLY, e); |
| 3829 | break; |
| 3830 | |
| 3831 | case PJSIP_TSX_STATE_COMPLETED: |
| 3832 | /* |
| 3833 | * Transaction sent final response. |
| 3834 | */ |
| 3835 | if (tsx->status_code/100 == 2) { |
| 3836 | inv_set_state(inv, PJSIP_INV_STATE_CONNECTING, e); |
| 3837 | } else { |
| 3838 | inv_set_cause(inv, tsx->status_code, &tsx->status_text); |
| 3839 | inv_set_state(inv, PJSIP_INV_STATE_DISCONNECTED, e); |
| 3840 | } |
| 3841 | break; |
| 3842 | |
| 3843 | case PJSIP_TSX_STATE_TERMINATED: |
| 3844 | /* |
| 3845 | * This happens on transport error (e.g. failed to send |
| 3846 | * response) |
| 3847 | */ |
| 3848 | inv_set_cause(inv, tsx->status_code, &tsx->status_text); |
| 3849 | inv_set_state(inv, PJSIP_INV_STATE_DISCONNECTED, e); |
| 3850 | break; |
| 3851 | |
| 3852 | default: |
| 3853 | pj_assert(!"Unexpected INVITE state"); |
| 3854 | break; |
| 3855 | } |
| 3856 | |
| 3857 | } else if (tsx->method.id == PJSIP_CANCEL_METHOD && |
| 3858 | tsx->role == PJSIP_ROLE_UAS && |
| 3859 | tsx->state < PJSIP_TSX_STATE_COMPLETED && |
| 3860 | e->body.tsx_state.type == PJSIP_EVENT_RX_MSG ) |
| 3861 | { |
| 3862 | |
| 3863 | /* |
| 3864 | * Handle incoming CANCEL request. |
| 3865 | */ |
| 3866 | |
| 3867 | inv_respond_incoming_cancel(inv, tsx, e); |
| 3868 | |
| 3869 | } |
| 3870 | } |
| 3871 | |
| 3872 | /* |
| 3873 | * State EARLY is for both UAS and UAC, after response with To tag |
| 3874 | * is sent/received. |
| 3875 | */ |
| 3876 | static void inv_on_state_early( pjsip_inv_session *inv, pjsip_event *e) |
| 3877 | { |
| 3878 | pjsip_transaction *tsx = e->body.tsx_state.tsx; |
| 3879 | pjsip_dialog *dlg = pjsip_tsx_get_dlg(tsx); |
| 3880 | |
| 3881 | PJ_ASSERT_ON_FAIL(tsx && dlg, return); |
| 3882 | |
| 3883 | if (tsx == inv->invite_tsx) { |
| 3884 | |
| 3885 | /* |
| 3886 | * Handle the INVITE state progress. |
| 3887 | */ |
| 3888 | |
| 3889 | switch (tsx->state) { |
| 3890 | |
| 3891 | case PJSIP_TSX_STATE_PROCEEDING: |
| 3892 | /* Send/received another provisional response. */ |
| 3893 | inv_set_state(inv, PJSIP_INV_STATE_EARLY, e); |
| 3894 | |
| 3895 | if (e->body.tsx_state.type == PJSIP_EVENT_RX_MSG) { |
| 3896 | inv_check_sdp_in_incoming_msg(inv, tsx, |
| 3897 | e->body.tsx_state.src.rdata); |
| 3898 | |
| 3899 | if (pjsip_100rel_is_reliable(e->body.tsx_state.src.rdata)) { |
| 3900 | inv_handle_incoming_reliable_response( |
| 3901 | inv, e->body.tsx_state.src.rdata); |
| 3902 | } |
| 3903 | } |
| 3904 | break; |
| 3905 | |
| 3906 | case PJSIP_TSX_STATE_COMPLETED: |
| 3907 | if (tsx->status_code/100 == 2) { |
| 3908 | inv_set_state(inv, PJSIP_INV_STATE_CONNECTING, e); |
| 3909 | if (e->body.tsx_state.type == PJSIP_EVENT_RX_MSG) { |
| 3910 | pj_status_t status; |
| 3911 | |
| 3912 | /* Process session timer response. */ |
| 3913 | status = handle_timer_response(inv, |
| 3914 | e->body.tsx_state.src.rdata, |
| 3915 | PJ_TRUE); |
| 3916 | if (status != PJ_SUCCESS) |
| 3917 | break; |
| 3918 | |
| 3919 | inv_check_sdp_in_incoming_msg(inv, tsx, |
| 3920 | e->body.tsx_state.src.rdata); |
| 3921 | } |
| 3922 | |
| 3923 | } else if (tsx->role == PJSIP_ROLE_UAC) { |
| 3924 | |
| 3925 | handle_uac_call_rejection(inv, e); |
| 3926 | |
| 3927 | } else { |
| 3928 | inv_set_cause(inv, tsx->status_code, &tsx->status_text); |
| 3929 | inv_set_state(inv, PJSIP_INV_STATE_DISCONNECTED, e); |
| 3930 | } |
| 3931 | break; |
| 3932 | |
| 3933 | case PJSIP_TSX_STATE_CONFIRMED: |
| 3934 | /* For some reason can go here (maybe when ACK for 2xx has |
| 3935 | * the same branch value as the INVITE transaction) */ |
| 3936 | |
| 3937 | case PJSIP_TSX_STATE_TERMINATED: |
| 3938 | /* INVITE transaction can be terminated either because UAC |
| 3939 | * transaction received 2xx response or because of transport |
| 3940 | * error. |
| 3941 | */ |
| 3942 | if (tsx->status_code/100 == 2) { |
| 3943 | |
| 3944 | /* This must be receipt of 2xx response */ |
| 3945 | |
| 3946 | /* Set state to CONNECTING */ |
| 3947 | inv_set_state(inv, PJSIP_INV_STATE_CONNECTING, e); |
| 3948 | |
| 3949 | if (e->body.tsx_state.type == PJSIP_EVENT_RX_MSG) { |
| 3950 | pj_status_t status; |
| 3951 | |
| 3952 | /* Process session timer response. */ |
| 3953 | status = handle_timer_response(inv, |
| 3954 | e->body.tsx_state.src.rdata, |
| 3955 | PJ_TRUE); |
| 3956 | if (status != PJ_SUCCESS) |
| 3957 | break; |
| 3958 | |
| 3959 | inv_check_sdp_in_incoming_msg(inv, tsx, |
| 3960 | e->body.tsx_state.src.rdata); |
| 3961 | } |
| 3962 | |
| 3963 | /* if UAC, send ACK and move state to confirmed. */ |
| 3964 | if (tsx->role == PJSIP_ROLE_UAC) { |
| 3965 | pj_assert(e->body.tsx_state.type == PJSIP_EVENT_RX_MSG); |
| 3966 | |
| 3967 | inv_send_ack(inv, e); |
| 3968 | } |
| 3969 | |
| 3970 | } else { |
| 3971 | inv_set_cause(inv, tsx->status_code, &tsx->status_text); |
| 3972 | inv_set_state(inv, PJSIP_INV_STATE_DISCONNECTED, e); |
| 3973 | } |
| 3974 | break; |
| 3975 | |
| 3976 | default: |
| 3977 | pj_assert(!"Unexpected INVITE tsx state"); |
| 3978 | break; |
| 3979 | } |
| 3980 | |
| 3981 | } else if (inv->role == PJSIP_ROLE_UAS && |
| 3982 | tsx->role == PJSIP_ROLE_UAS && |
| 3983 | tsx->method.id == PJSIP_CANCEL_METHOD && |
| 3984 | tsx->state < PJSIP_TSX_STATE_COMPLETED && |
| 3985 | e->body.tsx_state.type == PJSIP_EVENT_RX_MSG ) |
| 3986 | { |
| 3987 | |
| 3988 | /* |
| 3989 | * Handle incoming CANCEL request. |
| 3990 | */ |
| 3991 | |
| 3992 | inv_respond_incoming_cancel(inv, tsx, e); |
| 3993 | |
| 3994 | } else if (tsx->role == PJSIP_ROLE_UAS && |
| 3995 | tsx->state == PJSIP_TSX_STATE_TRYING && |
| 3996 | pjsip_method_cmp(&tsx->method, &pjsip_update_method)==0) |
| 3997 | { |
| 3998 | /* |
| 3999 | * Handle incoming UPDATE |
| 4000 | */ |
| 4001 | inv_respond_incoming_update(inv, e->body.tsx_state.src.rdata); |
| 4002 | |
| 4003 | |
| 4004 | } else if (tsx->role == PJSIP_ROLE_UAC && |
| 4005 | (tsx->state == PJSIP_TSX_STATE_COMPLETED || |
| 4006 | tsx->state == PJSIP_TSX_STATE_TERMINATED) && |
| 4007 | pjsip_method_cmp(&tsx->method, &pjsip_update_method)==0) |
| 4008 | { |
| 4009 | /* |
| 4010 | * Handle response to outgoing UPDATE request. |
| 4011 | */ |
| 4012 | inv_handle_update_response(inv, e); |
| 4013 | |
| 4014 | } else if (tsx->role == PJSIP_ROLE_UAS && |
| 4015 | tsx->state == PJSIP_TSX_STATE_TRYING && |
| 4016 | pjsip_method_cmp(&tsx->method, &pjsip_prack_method)==0) |
| 4017 | { |
| 4018 | /* |
| 4019 | * Handle incoming PRACK |
| 4020 | */ |
| 4021 | inv_respond_incoming_prack(inv, e->body.tsx_state.src.rdata); |
| 4022 | |
| 4023 | } else if (tsx->role == PJSIP_ROLE_UAC) { |
| 4024 | |
| 4025 | /* Generic handling for UAC tsx completion */ |
| 4026 | handle_uac_tsx_response(inv, e); |
| 4027 | |
| 4028 | } else if (tsx->role == PJSIP_ROLE_UAS && |
| 4029 | tsx->method.id == PJSIP_BYE_METHOD && |
| 4030 | tsx->status_code < 200 && |
| 4031 | e->body.tsx_state.type == PJSIP_EVENT_RX_MSG) |
| 4032 | { |
| 4033 | /* Received BYE before the 2xx/OK response to INVITE. |
| 4034 | * Assume that the 2xx/OK response is lost and the BYE |
| 4035 | * arrives earlier. |
| 4036 | */ |
| 4037 | inv_respond_incoming_bye(inv, tsx, e->body.tsx_state.src.rdata, e); |
| 4038 | |
| 4039 | if (inv->invite_tsx->role == PJSIP_ROLE_UAC) { |
| 4040 | /* Set timer just in case we will never get the final response |
| 4041 | * for INVITE. |
| 4042 | */ |
| 4043 | pjsip_tsx_set_timeout(inv->invite_tsx, 64*pjsip_cfg()->tsx.t1); |
| 4044 | } else if (inv->invite_tsx->status_code < 200) { |
| 4045 | pjsip_tx_data *tdata; |
| 4046 | pjsip_msg *msg; |
| 4047 | |
| 4048 | /* For UAS, send a final response. */ |
| 4049 | tdata = inv->invite_tsx->last_tx; |
| 4050 | PJ_ASSERT_ON_FAIL(tdata != NULL, return); |
| 4051 | |
| 4052 | msg = tdata->msg; |
| 4053 | msg->line.status.code = PJSIP_SC_REQUEST_TERMINATED; |
| 4054 | msg->line.status.reason = |
| 4055 | *pjsip_get_status_text(PJSIP_SC_REQUEST_TERMINATED); |
| 4056 | msg->body = NULL; |
| 4057 | |
| 4058 | pjsip_tx_data_invalidate_msg(tdata); |
| 4059 | pjsip_tx_data_add_ref(tdata); |
| 4060 | |
| 4061 | pjsip_dlg_send_response(inv->dlg, inv->invite_tsx, tdata); |
| 4062 | } |
| 4063 | } |
| 4064 | } |
| 4065 | |
| 4066 | /* |
| 4067 | * State CONNECTING is after 2xx response to INVITE is sent/received. |
| 4068 | */ |
| 4069 | static void inv_on_state_connecting( pjsip_inv_session *inv, pjsip_event *e) |
| 4070 | { |
| 4071 | pjsip_transaction *tsx = e->body.tsx_state.tsx; |
| 4072 | pjsip_dialog *dlg = pjsip_tsx_get_dlg(tsx); |
| 4073 | |
| 4074 | PJ_ASSERT_ON_FAIL(tsx && dlg, return); |
| 4075 | |
| 4076 | if (tsx == inv->invite_tsx) { |
| 4077 | |
| 4078 | /* |
| 4079 | * Handle INVITE state progression. |
| 4080 | */ |
| 4081 | switch (tsx->state) { |
| 4082 | |
| 4083 | case PJSIP_TSX_STATE_CONFIRMED: |
| 4084 | /* It can only go here if incoming ACK request has the same Via |
| 4085 | * branch parameter as the INVITE transaction. |
| 4086 | */ |
| 4087 | if (tsx->status_code/100 == 2) { |
| 4088 | if (e->body.tsx_state.type == PJSIP_EVENT_RX_MSG) { |
| 4089 | inv_check_sdp_in_incoming_msg(inv, tsx, |
| 4090 | e->body.tsx_state.src.rdata); |
| 4091 | } |
| 4092 | |
| 4093 | inv_set_state(inv, PJSIP_INV_STATE_CONFIRMED, e); |
| 4094 | } |
| 4095 | break; |
| 4096 | |
| 4097 | case PJSIP_TSX_STATE_TERMINATED: |
| 4098 | /* INVITE transaction can be terminated either because UAC |
| 4099 | * transaction received 2xx response or because of transport |
| 4100 | * error. |
| 4101 | */ |
| 4102 | if (tsx->status_code/100 != 2) { |
| 4103 | if (tsx->role == PJSIP_ROLE_UAC) { |
| 4104 | inv_set_cause(inv, tsx->status_code, &tsx->status_text); |
| 4105 | inv_set_state(inv, PJSIP_INV_STATE_DISCONNECTED, e); |
| 4106 | } else { |
| 4107 | pjsip_tx_data *bye; |
| 4108 | pj_status_t status; |
| 4109 | |
| 4110 | /* Send BYE */ |
| 4111 | status = pjsip_dlg_create_request(inv->dlg, |
| 4112 | pjsip_get_bye_method(), |
| 4113 | -1, &bye); |
| 4114 | if (status == PJ_SUCCESS) { |
| 4115 | pjsip_inv_send_msg(inv, bye); |
| 4116 | } |
| 4117 | } |
| 4118 | } |
| 4119 | break; |
| 4120 | |
| 4121 | case PJSIP_TSX_STATE_DESTROYED: |
| 4122 | /* Do nothing. */ |
| 4123 | break; |
| 4124 | |
| 4125 | default: |
| 4126 | pj_assert(!"Unexpected state"); |
| 4127 | break; |
| 4128 | } |
| 4129 | |
| 4130 | } else if (tsx->role == PJSIP_ROLE_UAS && |
| 4131 | tsx->method.id == PJSIP_BYE_METHOD && |
| 4132 | tsx->status_code < 200 && |
| 4133 | e->body.tsx_state.type == PJSIP_EVENT_RX_MSG) |
| 4134 | { |
| 4135 | |
| 4136 | /* |
| 4137 | * Handle incoming BYE. |
| 4138 | */ |
| 4139 | |
| 4140 | inv_respond_incoming_bye( inv, tsx, e->body.tsx_state.src.rdata, e ); |
| 4141 | |
| 4142 | } else if (tsx->method.id == PJSIP_BYE_METHOD && |
| 4143 | tsx->role == PJSIP_ROLE_UAC && |
| 4144 | (tsx->state == PJSIP_TSX_STATE_COMPLETED || |
| 4145 | tsx->state == PJSIP_TSX_STATE_TERMINATED)) |
| 4146 | { |
| 4147 | |
| 4148 | /* |
| 4149 | * Outgoing BYE |
| 4150 | */ |
| 4151 | inv_handle_bye_response( inv, tsx, e->body.tsx_state.src.rdata, e); |
| 4152 | |
| 4153 | } |
| 4154 | else if (tsx->method.id == PJSIP_CANCEL_METHOD && |
| 4155 | tsx->role == PJSIP_ROLE_UAS && |
| 4156 | tsx->status_code < 200 && |
| 4157 | e->body.tsx_state.type == PJSIP_EVENT_RX_MSG) |
| 4158 | { |
| 4159 | |
| 4160 | /* |
| 4161 | * Handle strandled incoming CANCEL or CANCEL for re-INVITE |
| 4162 | */ |
| 4163 | inv_respond_incoming_cancel(inv, tsx, e); |
| 4164 | |
| 4165 | } else if (tsx->role == PJSIP_ROLE_UAS && |
| 4166 | tsx->state == PJSIP_TSX_STATE_TRYING && |
| 4167 | pjsip_method_cmp(&tsx->method, &pjsip_invite_method)==0) |
| 4168 | { |
| 4169 | pjsip_rx_data *rdata = e->body.tsx_state.src.rdata; |
| 4170 | pjsip_tx_data *tdata; |
| 4171 | pj_status_t status; |
| 4172 | |
| 4173 | /* See https://trac.pjsip.org/repos/ticket/1455 |
| 4174 | * Handle incoming re-INVITE before current INVITE is confirmed. |
| 4175 | * According to RFC 5407: |
| 4176 | * - answer with 200 if we don't have pending offer-answer |
| 4177 | * - answer with 491 if we *have* pending offer-answer |
| 4178 | * |
| 4179 | * But unfortunately accepting the re-INVITE would mean we have |
| 4180 | * two outstanding INVITEs, and we don't support that because |
| 4181 | * we will get confused when we handle the ACK. |
| 4182 | */ |
| 4183 | status = pjsip_dlg_create_response(inv->dlg, rdata, |
| 4184 | PJSIP_SC_REQUEST_PENDING, |
| 4185 | NULL, &tdata); |
| 4186 | if (status != PJ_SUCCESS) |
| 4187 | return; |
| 4188 | pjsip_timer_update_resp(inv, tdata); |
| 4189 | status = pjsip_dlg_send_response(dlg, tsx, tdata); |
| 4190 | |
| 4191 | } else if (tsx->role == PJSIP_ROLE_UAS && |
| 4192 | tsx->state == PJSIP_TSX_STATE_TRYING && |
| 4193 | pjsip_method_cmp(&tsx->method, &pjsip_update_method)==0) |
| 4194 | { |
| 4195 | /* |
| 4196 | * Handle incoming UPDATE |
| 4197 | */ |
| 4198 | inv_respond_incoming_update(inv, e->body.tsx_state.src.rdata); |
| 4199 | |
| 4200 | |
| 4201 | } else if (tsx->role == PJSIP_ROLE_UAC && |
| 4202 | (tsx->state == PJSIP_TSX_STATE_COMPLETED || |
| 4203 | tsx->state == PJSIP_TSX_STATE_TERMINATED) && |
| 4204 | pjsip_method_cmp(&tsx->method, &pjsip_update_method)==0) |
| 4205 | { |
| 4206 | /* |
| 4207 | * Handle response to outgoing UPDATE request. |
| 4208 | */ |
| 4209 | if (inv_handle_update_response(inv, e) == PJ_FALSE) |
| 4210 | handle_uac_tsx_response(inv, e); |
| 4211 | |
| 4212 | } else if (tsx->role == PJSIP_ROLE_UAS && |
| 4213 | tsx->state == PJSIP_TSX_STATE_TRYING && |
| 4214 | pjsip_method_cmp(&tsx->method, &pjsip_prack_method)==0) |
| 4215 | { |
| 4216 | /* |
| 4217 | * Handle incoming PRACK |
| 4218 | */ |
| 4219 | inv_respond_incoming_prack(inv, e->body.tsx_state.src.rdata); |
| 4220 | |
| 4221 | } else if (tsx->role == PJSIP_ROLE_UAC) { |
| 4222 | |
| 4223 | /* Generic handling for UAC tsx completion */ |
| 4224 | handle_uac_tsx_response(inv, e); |
| 4225 | |
| 4226 | } |
| 4227 | |
| 4228 | } |
| 4229 | |
| 4230 | /* |
| 4231 | * State CONFIRMED is after ACK is sent/received. |
| 4232 | */ |
| 4233 | static void inv_on_state_confirmed( pjsip_inv_session *inv, pjsip_event *e) |
| 4234 | { |
| 4235 | pjsip_transaction *tsx = e->body.tsx_state.tsx; |
| 4236 | pjsip_dialog *dlg = pjsip_tsx_get_dlg(tsx); |
| 4237 | |
| 4238 | PJ_ASSERT_ON_FAIL(tsx && dlg, return); |
| 4239 | |
| 4240 | |
| 4241 | if (tsx->method.id == PJSIP_BYE_METHOD && |
| 4242 | tsx->role == PJSIP_ROLE_UAC && |
| 4243 | (tsx->state == PJSIP_TSX_STATE_COMPLETED || |
| 4244 | tsx->state == PJSIP_TSX_STATE_TERMINATED)) |
| 4245 | { |
| 4246 | |
| 4247 | /* |
| 4248 | * Outgoing BYE |
| 4249 | */ |
| 4250 | |
| 4251 | inv_handle_bye_response( inv, tsx, e->body.tsx_state.src.rdata, e); |
| 4252 | |
| 4253 | } |
| 4254 | else if (tsx->method.id == PJSIP_BYE_METHOD && |
| 4255 | tsx->role == PJSIP_ROLE_UAS && |
| 4256 | tsx->status_code < 200 && |
| 4257 | e->body.tsx_state.type == PJSIP_EVENT_RX_MSG) |
| 4258 | { |
| 4259 | |
| 4260 | /* |
| 4261 | * Handle incoming BYE. |
| 4262 | */ |
| 4263 | |
| 4264 | inv_respond_incoming_bye( inv, tsx, e->body.tsx_state.src.rdata, e ); |
| 4265 | |
| 4266 | } |
| 4267 | else if (tsx->method.id == PJSIP_CANCEL_METHOD && |
| 4268 | tsx->role == PJSIP_ROLE_UAS && |
| 4269 | tsx->status_code < 200 && |
| 4270 | e->body.tsx_state.type == PJSIP_EVENT_RX_MSG) |
| 4271 | { |
| 4272 | |
| 4273 | /* |
| 4274 | * Handle strandled incoming CANCEL. |
| 4275 | */ |
| 4276 | pjsip_rx_data *rdata = e->body.tsx_state.src.rdata; |
| 4277 | pjsip_tx_data *tdata; |
| 4278 | pj_status_t status; |
| 4279 | |
| 4280 | status = pjsip_dlg_create_response(dlg, rdata, 200, NULL, &tdata); |
| 4281 | if (status != PJ_SUCCESS) return; |
| 4282 | |
| 4283 | status = pjsip_dlg_send_response(dlg, tsx, tdata); |
| 4284 | if (status != PJ_SUCCESS) return; |
| 4285 | |
| 4286 | } |
| 4287 | else if (tsx->method.id == PJSIP_INVITE_METHOD && |
| 4288 | tsx->role == PJSIP_ROLE_UAS) |
| 4289 | { |
| 4290 | |
| 4291 | /* |
| 4292 | * Handle incoming re-INVITE |
| 4293 | */ |
| 4294 | if (tsx->state == PJSIP_TSX_STATE_TRYING) { |
| 4295 | |
| 4296 | pjsip_rx_data *rdata = e->body.tsx_state.src.rdata; |
| 4297 | pjsip_tx_data *tdata; |
| 4298 | pj_status_t status; |
| 4299 | pjsip_rdata_sdp_info *sdp_info = NULL; |
| 4300 | pjsip_status_code st_code; |
| 4301 | |
| 4302 | /* Check if we have INVITE pending. */ |
| 4303 | if (inv->invite_tsx && inv->invite_tsx!=tsx) { |
| 4304 | int code; |
| 4305 | pj_str_t reason; |
| 4306 | |
| 4307 | reason = pj_str("Another INVITE transaction in progress"); |
| 4308 | |
| 4309 | if (inv->invite_tsx->role == PJSIP_ROLE_UAC) |
| 4310 | code = 491; |
| 4311 | else |
| 4312 | code = 500; |
| 4313 | |
| 4314 | /* Can not receive re-INVITE while another one is pending. */ |
| 4315 | status = pjsip_dlg_create_response( inv->dlg, rdata, code, |
| 4316 | &reason, &tdata); |
| 4317 | if (status != PJ_SUCCESS) |
| 4318 | return; |
| 4319 | |
| 4320 | if (code == 500) { |
| 4321 | /* MUST include Retry-After header with random value |
| 4322 | * between 0-10. |
| 4323 | */ |
| 4324 | pjsip_retry_after_hdr *ra_hdr; |
| 4325 | int val = (pj_rand() % 10); |
| 4326 | |
| 4327 | ra_hdr = pjsip_retry_after_hdr_create(tdata->pool, val); |
| 4328 | pjsip_msg_add_hdr(tdata->msg, (pjsip_hdr*)ra_hdr); |
| 4329 | } |
| 4330 | |
| 4331 | status = pjsip_dlg_send_response( inv->dlg, tsx, tdata); |
| 4332 | |
| 4333 | |
| 4334 | return; |
| 4335 | } |
| 4336 | |
| 4337 | /* Save the invite transaction. */ |
| 4338 | inv->invite_tsx = tsx; |
| 4339 | |
| 4340 | /* Process session timers headers in the re-INVITE */ |
| 4341 | status = pjsip_timer_process_req(inv, rdata, &st_code); |
| 4342 | if (status != PJ_SUCCESS) { |
| 4343 | status = pjsip_dlg_create_response(inv->dlg, rdata, st_code, |
| 4344 | NULL, &tdata); |
| 4345 | if (status != PJ_SUCCESS) |
| 4346 | return; |
| 4347 | |
| 4348 | pjsip_timer_update_resp(inv, tdata); |
| 4349 | status = pjsip_dlg_send_response(dlg, tsx, tdata); |
| 4350 | return; |
| 4351 | } |
| 4352 | |
| 4353 | /* Send 491 if we receive re-INVITE while another offer/answer |
| 4354 | * negotiation is in progress |
| 4355 | */ |
| 4356 | if (pjmedia_sdp_neg_get_state(inv->neg) != |
| 4357 | PJMEDIA_SDP_NEG_STATE_DONE) |
| 4358 | { |
| 4359 | status = pjsip_dlg_create_response(inv->dlg, rdata, |
| 4360 | PJSIP_SC_REQUEST_PENDING, |
| 4361 | NULL, &tdata); |
| 4362 | if (status != PJ_SUCCESS) |
| 4363 | return; |
| 4364 | pjsip_timer_update_resp(inv, tdata); |
| 4365 | status = pjsip_dlg_send_response(dlg, tsx, tdata); |
| 4366 | return; |
| 4367 | } |
| 4368 | |
| 4369 | /* Process SDP in incoming message. */ |
| 4370 | status = inv_check_sdp_in_incoming_msg(inv, tsx, rdata); |
| 4371 | |
| 4372 | if (status == PJ_SUCCESS && mod_inv.cb.on_rx_reinvite && |
| 4373 | inv->notify) |
| 4374 | { |
| 4375 | pj_status_t rc; |
| 4376 | |
| 4377 | sdp_info = pjsip_rdata_get_sdp_info(rdata); |
| 4378 | rc = (*mod_inv.cb.on_rx_reinvite)(inv, sdp_info->sdp, |
| 4379 | rdata); |
| 4380 | if (rc == PJ_SUCCESS) { |
| 4381 | /* Application will send its own response. |
| 4382 | * Our job is done. */ |
| 4383 | PJ_LOG(5,(inv->obj_name, "on_rx_reinvite() returns %d", |
| 4384 | rc)); |
| 4385 | return; |
| 4386 | } |
| 4387 | |
| 4388 | /* If application lets us answer the re-INVITE, |
| 4389 | * application must set the SDP answer with |
| 4390 | * #pjsip_inv_set_sdp_answer(). |
| 4391 | */ |
| 4392 | if (pjmedia_sdp_neg_get_state(inv->neg) != |
| 4393 | PJMEDIA_SDP_NEG_STATE_WAIT_NEGO) |
| 4394 | { |
| 4395 | status = PJ_EINVALIDOP; |
| 4396 | } |
| 4397 | } |
| 4398 | |
| 4399 | if (status != PJ_SUCCESS) { |
| 4400 | |
| 4401 | /* Not Acceptable */ |
| 4402 | const pjsip_hdr *accept; |
| 4403 | |
| 4404 | /* The incoming SDP is unacceptable. If the SDP negotiator |
| 4405 | * state has just been changed, i.e: DONE -> REMOTE_OFFER, |
| 4406 | * revert it back. |
| 4407 | */ |
| 4408 | if (pjmedia_sdp_neg_get_state(inv->neg) == |
| 4409 | PJMEDIA_SDP_NEG_STATE_REMOTE_OFFER) |
| 4410 | { |
| 4411 | pjmedia_sdp_neg_cancel_offer(inv->neg); |
| 4412 | } |
| 4413 | |
| 4414 | status = pjsip_dlg_create_response(inv->dlg, rdata, |
| 4415 | 488, NULL, &tdata); |
| 4416 | if (status != PJ_SUCCESS) |
| 4417 | return; |
| 4418 | |
| 4419 | |
| 4420 | accept = pjsip_endpt_get_capability(dlg->endpt, PJSIP_H_ACCEPT, |
| 4421 | NULL); |
| 4422 | if (accept) { |
| 4423 | pjsip_msg_add_hdr(tdata->msg, (pjsip_hdr*) |
| 4424 | pjsip_hdr_clone(tdata->pool, accept)); |
| 4425 | } |
| 4426 | |
| 4427 | status = pjsip_dlg_send_response(dlg, tsx, tdata); |
| 4428 | |
| 4429 | return; |
| 4430 | } |
| 4431 | |
| 4432 | /* Create 2xx ANSWER */ |
| 4433 | status = pjsip_dlg_create_response(dlg, rdata, 200, NULL, &tdata); |
| 4434 | if (status != PJ_SUCCESS) |
| 4435 | return; |
| 4436 | |
| 4437 | /* If the INVITE request has SDP body, send answer. |
| 4438 | * Otherwise generate offer from local active SDP. |
| 4439 | */ |
| 4440 | if (!sdp_info) |
| 4441 | sdp_info = pjsip_rdata_get_sdp_info(rdata); |
| 4442 | if (sdp_info->sdp != NULL) { |
| 4443 | status = process_answer(inv, 200, tdata, NULL); |
| 4444 | } else { |
| 4445 | /* INVITE does not have SDP. |
| 4446 | * If on_create_offer() callback is implemented, ask app. |
| 4447 | * to generate an offer, otherwise just send active local |
| 4448 | * SDP to signal that nothing gets modified. |
| 4449 | */ |
| 4450 | pjmedia_sdp_session *sdp = NULL; |
| 4451 | |
| 4452 | if (mod_inv.cb.on_create_offer) { |
| 4453 | (*mod_inv.cb.on_create_offer)(inv, &sdp); |
| 4454 | if (sdp) { |
| 4455 | /* Notify negotiator about the new offer. This will |
| 4456 | * fix the offer with correct SDP origin. |
| 4457 | */ |
| 4458 | status = |
| 4459 | pjmedia_sdp_neg_modify_local_offer2( |
| 4460 | inv->pool_prov, inv->neg, |
| 4461 | inv->sdp_neg_flags, sdp); |
| 4462 | |
| 4463 | /* Retrieve the "fixed" offer from negotiator */ |
| 4464 | if (status==PJ_SUCCESS) { |
| 4465 | const pjmedia_sdp_session *lsdp = NULL; |
| 4466 | pjmedia_sdp_neg_get_neg_local(inv->neg, &lsdp); |
| 4467 | sdp = (pjmedia_sdp_session*)lsdp; |
| 4468 | } |
| 4469 | } |
| 4470 | } |
| 4471 | |
| 4472 | if (sdp == NULL) { |
| 4473 | const pjmedia_sdp_session *active_sdp = NULL; |
| 4474 | status = pjmedia_sdp_neg_send_local_offer(inv->pool_prov, |
| 4475 | inv->neg, |
| 4476 | &active_sdp); |
| 4477 | if (status == PJ_SUCCESS) |
| 4478 | sdp = (pjmedia_sdp_session*) active_sdp; |
| 4479 | } |
| 4480 | |
| 4481 | if (sdp) { |
| 4482 | tdata->msg->body = create_sdp_body(tdata->pool, sdp); |
| 4483 | } |
| 4484 | } |
| 4485 | |
| 4486 | if (status != PJ_SUCCESS) { |
| 4487 | /* |
| 4488 | * SDP negotiation has failed. |
| 4489 | */ |
| 4490 | pj_status_t rc; |
| 4491 | pj_str_t reason; |
| 4492 | |
| 4493 | /* Delete the 2xx answer */ |
| 4494 | pjsip_tx_data_dec_ref(tdata); |
| 4495 | |
| 4496 | /* Create 500 response */ |
| 4497 | reason = pj_str("SDP negotiation failed"); |
| 4498 | rc = pjsip_dlg_create_response(dlg, rdata, 500, &reason, |
| 4499 | &tdata); |
| 4500 | if (rc == PJ_SUCCESS) { |
| 4501 | pjsip_warning_hdr *w; |
| 4502 | const pj_str_t *endpt_name; |
| 4503 | |
| 4504 | endpt_name = pjsip_endpt_name(dlg->endpt); |
| 4505 | w = pjsip_warning_hdr_create_from_status(tdata->pool, |
| 4506 | endpt_name, |
| 4507 | status); |
| 4508 | if (w) |
| 4509 | pjsip_msg_add_hdr(tdata->msg, (pjsip_hdr*)w); |
| 4510 | |
| 4511 | pjsip_inv_send_msg(inv, tdata); |
| 4512 | } |
| 4513 | return; |
| 4514 | } |
| 4515 | |
| 4516 | /* Invoke Session Timers */ |
| 4517 | pjsip_timer_update_resp(inv, tdata); |
| 4518 | |
| 4519 | /* Send 2xx regardless of the status of negotiation */ |
| 4520 | status = pjsip_inv_send_msg(inv, tdata); |
| 4521 | |
| 4522 | } else if (tsx->state == PJSIP_TSX_STATE_CONFIRMED) { |
| 4523 | /* This is the case where ACK has the same branch as |
| 4524 | * the INVITE request. |
| 4525 | */ |
| 4526 | if (tsx->status_code/100 == 2 && |
| 4527 | e->body.tsx_state.type == PJSIP_EVENT_RX_MSG) |
| 4528 | { |
| 4529 | inv_check_sdp_in_incoming_msg(inv, tsx, |
| 4530 | e->body.tsx_state.src.rdata); |
| 4531 | |
| 4532 | /* Check if local offer got no SDP answer */ |
| 4533 | if (pjmedia_sdp_neg_get_state(inv->neg)== |
| 4534 | PJMEDIA_SDP_NEG_STATE_LOCAL_OFFER) |
| 4535 | { |
| 4536 | pjmedia_sdp_neg_cancel_offer(inv->neg); |
| 4537 | } |
| 4538 | } |
| 4539 | |
| 4540 | } |
| 4541 | |
| 4542 | } |
| 4543 | else if (tsx->method.id == PJSIP_INVITE_METHOD && |
| 4544 | tsx->role == PJSIP_ROLE_UAC) |
| 4545 | { |
| 4546 | |
| 4547 | /* |
| 4548 | * Handle outgoing re-INVITE |
| 4549 | */ |
| 4550 | if (tsx->state == PJSIP_TSX_STATE_CALLING) { |
| 4551 | |
| 4552 | /* Must not have other pending INVITE transaction */ |
| 4553 | pj_assert(inv->invite_tsx==NULL || tsx==inv->invite_tsx); |
| 4554 | |
| 4555 | /* Save pending invite transaction */ |
| 4556 | inv->invite_tsx = tsx; |
| 4557 | |
| 4558 | } else if (tsx->state == PJSIP_TSX_STATE_PROCEEDING) { |
| 4559 | |
| 4560 | /* CANCEL the re-INVITE if necessary */ |
| 4561 | if (inv->pending_cancel) { |
| 4562 | pj_status_t status; |
| 4563 | pjsip_tx_data *cancel; |
| 4564 | |
| 4565 | inv->pending_cancel = PJ_FALSE; |
| 4566 | |
| 4567 | status = pjsip_inv_cancel_reinvite(inv, &cancel); |
| 4568 | if (status == PJ_SUCCESS && cancel) |
| 4569 | status = pjsip_inv_send_msg(inv, cancel); |
| 4570 | } |
| 4571 | |
| 4572 | } else if (tsx->state == PJSIP_TSX_STATE_TERMINATED && |
| 4573 | tsx->status_code/100 == 2) |
| 4574 | { |
| 4575 | pj_status_t status; |
| 4576 | |
| 4577 | /* Re-INVITE was accepted. */ |
| 4578 | |
| 4579 | /* Process session timer response. */ |
| 4580 | status = handle_timer_response(inv, |
| 4581 | e->body.tsx_state.src.rdata, |
| 4582 | PJ_TRUE); |
| 4583 | if (status != PJ_SUCCESS) |
| 4584 | return; |
| 4585 | |
| 4586 | /* Process SDP */ |
| 4587 | inv_check_sdp_in_incoming_msg(inv, tsx, |
| 4588 | e->body.tsx_state.src.rdata); |
| 4589 | |
| 4590 | /* Check if local offer got no SDP answer */ |
| 4591 | if (pjmedia_sdp_neg_get_state(inv->neg)== |
| 4592 | PJMEDIA_SDP_NEG_STATE_LOCAL_OFFER) |
| 4593 | { |
| 4594 | pjmedia_sdp_neg_cancel_offer(inv->neg); |
| 4595 | } |
| 4596 | |
| 4597 | /* Send ACK */ |
| 4598 | inv_send_ack(inv, e); |
| 4599 | |
| 4600 | } else if (handle_uac_tsx_response(inv, e)) { |
| 4601 | |
| 4602 | /* Handle response that terminates dialog */ |
| 4603 | /* Nothing to do (already handled) */ |
| 4604 | |
| 4605 | } else if (tsx->status_code >= 300 && tsx->status_code < 700 && |
| 4606 | e->body.tsx_state.prev_state != PJSIP_TSX_STATE_COMPLETED) |
| 4607 | { |
| 4608 | /* Ticket #1654: do not cancel SDP offer when tsx state changing |
| 4609 | * from 'completed' to 'terminated', as it should have already |
| 4610 | * been cancelled when tsx state is 'completed'. |
| 4611 | */ |
| 4612 | |
| 4613 | pjmedia_sdp_neg_state neg_state; |
| 4614 | struct tsx_inv_data *tsx_inv_data; |
| 4615 | |
| 4616 | tsx_inv_data = (struct tsx_inv_data*)tsx->mod_data[mod_inv.mod.id]; |
| 4617 | |
| 4618 | /* Outgoing INVITE transaction has failed, cancel SDP nego */ |
| 4619 | neg_state = pjmedia_sdp_neg_get_state(inv->neg); |
| 4620 | if (neg_state == PJMEDIA_SDP_NEG_STATE_LOCAL_OFFER && |
| 4621 | tsx_inv_data->retrying == PJ_FALSE) |
| 4622 | { |
| 4623 | pjmedia_sdp_neg_cancel_offer(inv->neg); |
| 4624 | } |
| 4625 | |
| 4626 | if (tsx == inv->invite_tsx) |
| 4627 | inv->invite_tsx = NULL; |
| 4628 | } |
| 4629 | |
| 4630 | } else if (tsx->role == PJSIP_ROLE_UAS && |
| 4631 | tsx->state == PJSIP_TSX_STATE_TRYING && |
| 4632 | pjsip_method_cmp(&tsx->method, &pjsip_update_method)==0) |
| 4633 | { |
| 4634 | /* |
| 4635 | * Handle incoming UPDATE |
| 4636 | */ |
| 4637 | inv_respond_incoming_update(inv, e->body.tsx_state.src.rdata); |
| 4638 | |
| 4639 | } else if (tsx->role == PJSIP_ROLE_UAC && |
| 4640 | (tsx->state == PJSIP_TSX_STATE_COMPLETED || |
| 4641 | tsx->state == PJSIP_TSX_STATE_TERMINATED) && |
| 4642 | pjsip_method_cmp(&tsx->method, &pjsip_update_method)==0) |
| 4643 | { |
| 4644 | /* |
| 4645 | * Handle response to outgoing UPDATE request. |
| 4646 | */ |
| 4647 | if (inv_handle_update_response(inv, e) == PJ_FALSE) |
| 4648 | handle_uac_tsx_response(inv, e); |
| 4649 | |
| 4650 | } else if (tsx->role == PJSIP_ROLE_UAS && |
| 4651 | tsx->state == PJSIP_TSX_STATE_TRYING && |
| 4652 | pjsip_method_cmp(&tsx->method, &pjsip_prack_method)==0) |
| 4653 | { |
| 4654 | /* |
| 4655 | * Handle strandled incoming PRACK |
| 4656 | */ |
| 4657 | inv_respond_incoming_prack(inv, e->body.tsx_state.src.rdata); |
| 4658 | |
| 4659 | } else if (tsx->role == PJSIP_ROLE_UAC) { |
| 4660 | /* |
| 4661 | * Handle 401/407/408/481/422 response |
| 4662 | */ |
| 4663 | handle_uac_tsx_response(inv, e); |
| 4664 | } |
| 4665 | |
| 4666 | } |
| 4667 | |
| 4668 | /* |
| 4669 | * After session has been terminated, but before dialog is destroyed |
| 4670 | * (because dialog has other usages, or because dialog is waiting for |
| 4671 | * the last transaction to terminate). |
| 4672 | */ |
| 4673 | static void inv_on_state_disconnected( pjsip_inv_session *inv, pjsip_event *e) |
| 4674 | { |
| 4675 | pjsip_transaction *tsx = e->body.tsx_state.tsx; |
| 4676 | pjsip_dialog *dlg = pjsip_tsx_get_dlg(tsx); |
| 4677 | |
| 4678 | PJ_ASSERT_ON_FAIL(tsx && dlg, return); |
| 4679 | |
| 4680 | if (tsx->role == PJSIP_ROLE_UAS && |
| 4681 | tsx->status_code < 200 && |
| 4682 | e->body.tsx_state.type == PJSIP_EVENT_RX_MSG) |
| 4683 | { |
| 4684 | pjsip_rx_data *rdata = e->body.tsx_state.src.rdata; |
| 4685 | |
| 4686 | /* |
| 4687 | * Respond BYE with 200/OK |
| 4688 | */ |
| 4689 | if (tsx->method.id == PJSIP_BYE_METHOD) { |
| 4690 | inv_respond_incoming_bye( inv, tsx, rdata, e ); |
| 4691 | } else if (tsx->method.id == PJSIP_CANCEL_METHOD) { |
| 4692 | /* |
| 4693 | * Respond CANCEL with 200/OK too. |
| 4694 | */ |
| 4695 | pjsip_tx_data *tdata; |
| 4696 | pj_status_t status; |
| 4697 | |
| 4698 | status = pjsip_dlg_create_response(dlg, rdata, 200, NULL, &tdata); |
| 4699 | if (status != PJ_SUCCESS) return; |
| 4700 | |
| 4701 | status = pjsip_dlg_send_response(dlg, tsx, tdata); |
| 4702 | if (status != PJ_SUCCESS) return; |
| 4703 | |
| 4704 | } |
| 4705 | |
| 4706 | } else if (tsx->role == PJSIP_ROLE_UAC) { |
| 4707 | /* |
| 4708 | * Handle 401/407/408/481/422 response |
| 4709 | */ |
| 4710 | handle_uac_tsx_response(inv, e); |
| 4711 | } |
| 4712 | } |
| 4713 | |