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