Debian Codegraph @

Xpjproject/pjsip/src/pjsip-ua/sip_inv.c

raw symbols SCIP
1 /*
2 * Copyright (C) 2008-2011 Teluu Inc. (http://www.teluu.com)
3 * Copyright (C) 2003-2008 Benny Prijono <[email protected]>
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18 */
19 #include <pjsip-ua/sip_inv.h>
20 #include <pjsip-ua/sip_100rel.h>
21 #include <pjsip-ua/sip_timer.h>
22 #include <pjsip-ua/sip_siprec.h>
23 #include <pjsip/print_util.h>
24 #include <pjsip/sip_module.h>
25 #include <pjsip/sip_endpoint.h>
26 #include <pjsip/sip_event.h>
27 #include <pjsip/sip_multipart.h>
28 #include <pjsip/sip_transaction.h>
29 #include <pjmedia/sdp.h>
30 #include <pjmedia/sdp_neg.h>
31 #include <pjmedia/errno.h>
32 #include <pj/array.h>
33 #include <pj/string.h>
34 #include <pj/pool.h>
35 #include <pj/assert.h>
36 #include <pj/os.h>
37 #include <pj/log.h>
38 #include <pj/rand.h>
39
40 /*
41 * Note on offer/answer:
42 *
43 * The offer/answer framework in this implementation assumes the occurence
44 * of SDP in a particular request/response according to this table:
45
46 offer answer Note:
47 ========================================================================
48 INVITE X INVITE may contain offer
49 18x/INVITE X X Response may contain offer or answer
50 2xx/INVITE X X Response may contain offer or answer
51 ACK X ACK may contain answer
52
53 PRACK X X PRACK may contain offer or answer
54 2xx/PRACK X Response may contain answer
55
56 UPDATE X UPDATE may only contain offer
57 2xx/UPDATE X Response may only contain answer
58 ========================================================================
59
60 *
61 */
62
63 #define THIS_FILE "sip_inv.c"
64
65 static const char *inv_state_names[] =
66 {
67 "NULL",
68 "CALLING",
69 "INCOMING",
70 "EARLY",
71 "CONNECTING",
72 "CONFIRMED",
73 "DISCONNECTED",
74 "TERMINATED",
75 };
76
77 /* UPDATE method */
78 static const pjsip_method pjsip_update_method =
79 {
80 PJSIP_OTHER_METHOD,
81 { "UPDATE", 6 }
82 };
83
84 #define POOL_INIT_SIZE 1000
85 #define POOL_INC_SIZE 1000
86
87 /* Process header parameter in redirection target. */
88 void pjsip_inv_process_hparam(pjsip_inv_session *sess,
89 const pj_str_t *hname,
90 const pj_str_t *hvalue,
91 pjsip_tx_data *tdata);
92
93 /* Application can override this if it wants to implement custom handler. */
94 void (*pjsip_inv_process_hparam_ptr)(pjsip_inv_session *sess,
95 const pj_str_t *hname,
96 const pj_str_t *hvalue,
97 pjsip_tx_data *tdata) =
98 &pjsip_inv_process_hparam;
99
100 /*
101 * Static prototypes.
102 */
103 static pj_status_t mod_inv_load(pjsip_endpoint *endpt);
104 static pj_status_t mod_inv_unload(void);
105 static pj_bool_t mod_inv_on_rx_request(pjsip_rx_data *rdata);
106 static pj_bool_t mod_inv_on_rx_response(pjsip_rx_data *rdata);
107 static void mod_inv_on_tsx_state(pjsip_transaction*, pjsip_event*);
108
109 static void inv_on_state_null( pjsip_inv_session *inv, pjsip_event *e);
110 static void inv_on_state_calling( pjsip_inv_session *inv, pjsip_event *e);
111 static void inv_on_state_incoming( pjsip_inv_session *inv, pjsip_event *e);
112 static void inv_on_state_early( pjsip_inv_session *inv, pjsip_event *e);
113 static void inv_on_state_connecting( pjsip_inv_session *inv, pjsip_event *e);
114 static void inv_on_state_confirmed( pjsip_inv_session *inv, pjsip_event *e);
115 static void inv_on_state_disconnected( pjsip_inv_session *inv, pjsip_event *e);
116
117 static pj_status_t inv_check_sdp_in_incoming_msg( pjsip_inv_session *inv,
118 pjsip_transaction *tsx,
119 pjsip_rx_data *rdata);
120 static pj_status_t inv_negotiate_sdp( pjsip_inv_session *inv );
121 static pjsip_msg_body *create_sdp_body(pj_pool_t *pool,
122 const pjmedia_sdp_session *c_sdp);
123 static pj_status_t process_answer( pjsip_inv_session *inv,
124 int st_code,
125 pjsip_tx_data *tdata,
126 const pjmedia_sdp_session *local_sdp);
127
128 static pj_status_t handle_timer_response(pjsip_inv_session *inv,
129 const pjsip_rx_data *rdata,
130 pj_bool_t end_sess_on_failure);
131
132 static pj_bool_t inv_check_secure_dlg(pjsip_inv_session *inv,
133 pjsip_event *e);
134
135 static int print_sdp(pjsip_msg_body *body, char *buf, pj_size_t len);
136
137 /* Add Reason header to request (rfc3326) or Warning header to error response
138 * (rfc3261 20.43).
139 */
140 static pj_status_t add_reason_warning_hdr(pjsip_tx_data *tdata,
141 unsigned code,
142 const pj_str_t *reason);
143
144 static pj_status_t inv_async_auth_send_impl(
145 pjsip_auth_clt_sess *auth_sess,
146 void *user_data,
147 pjsip_tx_data *tdata);
148 static void inv_bye_auth_abandon_impl(
149 pjsip_auth_clt_sess *auth_sess,
150 void *user_data);
151 static void inv_initial_invite_auth_abandon_impl(
152 pjsip_auth_clt_sess *auth_sess,
153 void *user_data);
154 static void inv_set_state(pjsip_inv_session *inv, pjsip_inv_state state,
155 pjsip_event *e);
156 static void inv_set_cause(pjsip_inv_session *inv, int cause_code,
157 const pj_str_t *cause_text);
158
159 static void (*inv_state_handler[])( pjsip_inv_session *inv, pjsip_event *e) =
160 {
161 &inv_on_state_null,
162 &inv_on_state_calling,
163 &inv_on_state_incoming,
164 &inv_on_state_early,
165 &inv_on_state_connecting,
166 &inv_on_state_confirmed,
167 &inv_on_state_disconnected,
168 };
169
170 static struct mod_inv
171 {
172 pjsip_module mod;
173 pjsip_endpoint *endpt;
174 pjsip_inv_callback cb;
175 } mod_inv =
176 {
177 {
178 NULL, NULL, /* prev, next. */
179 { "mod-invite", 10 }, /* Name. */
180 -1, /* Id */
181 PJSIP_MOD_PRIORITY_DIALOG_USAGE, /* Priority */
182 &mod_inv_load, /* load() */
183 NULL, /* start() */
184 NULL, /* stop() */
185 &mod_inv_unload, /* unload() */
186 &mod_inv_on_rx_request, /* on_rx_request() */
187 &mod_inv_on_rx_response, /* on_rx_response() */
188 NULL, /* on_tx_request. */
189 NULL, /* on_tx_response() */
190 &mod_inv_on_tsx_state, /* on_tsx_state() */
191 }
192 };
193
194
195 /* Invite session data to be attached to transaction. */
196 struct tsx_inv_data
197 {
198 pjsip_inv_session *inv; /* The invite session */
199 pj_bool_t sdp_done; /* SDP negotiation done for this tsx? */
200 pj_bool_t retrying; /* Resend (e.g. due to 401/407) */
201 pj_str_t done_tag; /* To tag in RX response with answer */
202 pj_bool_t done_early;/* Negotiation was done for early med? */
203 pj_bool_t done_early_rel;/* Early med was realiable? */
204 pj_bool_t has_sdp; /* Message with SDP? */
205 };
206
207
208 /* Add Reason header to request (rfc3326) or Warning header to error response
209 * (rfc3261 20.43).
210 */
211 static pj_status_t add_reason_warning_hdr(pjsip_tx_data *tdata,
212 unsigned code,
213 const pj_str_t *reason)
214 {
215 PJ_ASSERT_RETURN(tdata && reason, PJ_EINVAL);
216
217 if (tdata->msg->type == PJSIP_REQUEST_MSG) {
218
219 /* Add Reason header in request messages */
220 pjsip_hdr *hdr;
221 const pj_str_t hname = { "Reason", 6 };
222 pj_str_t hvalue;
223 pj_size_t hval_len;
224
225 PJ_ASSERT_RETURN(tdata && reason && reason->slen >= 0, PJ_EINVAL);
226 PJ_ASSERT_RETURN(code < 1000, PJ_EINVAL);
227
228 hval_len = 3 + /* 'SIP' */
229 11 + /* ' ;cause=3-digit-code' */
230 (pj_size_t)reason->slen + 10; /* ' ;text=".."' */
231 hvalue.ptr = (char*)pj_pool_alloc(tdata->pool, hval_len);
232 if (!hvalue.ptr)
233 return PJ_ENOMEM;
234
235 hvalue.slen = pj_ansi_snprintf(hvalue.ptr, hval_len,
236 "SIP ;cause=%u ;text=\"%.*s\"",
237 code, (int)reason->slen, reason->ptr);
238 hdr = (pjsip_hdr*)
239 pjsip_generic_string_hdr_create(tdata->pool, &hname, &hvalue);
240 if (hdr)
241 pjsip_msg_add_hdr(tdata->msg, (pjsip_hdr*)hdr);
242 else
243 return PJ_ENOMEM;
244
245 } else {
246
247 /* Add Warning header in response messages */
248 pjsip_warning_hdr *hdr;
249 pjsip_dialog *dlg = pjsip_tdata_get_dlg(tdata);
250
251 // Let's always use warning code = 390 for now.
252 unsigned warn_code = 390;
253 // PJ_ASSERT_RETURN(!code || (code >= 300 && code < 400), PJ_EINVAL);
254 // if (code != 0) warn_code = code;
255
256 PJ_ASSERT_RETURN(dlg, PJ_EINVAL);
257
258 hdr = pjsip_warning_hdr_create(tdata->pool, warn_code,
259 pjsip_endpt_name(dlg->endpt), reason);
260 if (hdr)
261 pjsip_msg_add_hdr(tdata->msg, (pjsip_hdr*)hdr);
262 else
263 return PJ_ENOMEM;
264 }
265
266 return PJ_SUCCESS;
267 }
268
269
270 /*
271 * Module load()
272 */
273 static pj_status_t mod_inv_load(pjsip_endpoint *endpt)
274 {
275 pj_str_t allowed[] = {{"INVITE", 6}, {"ACK",3}, {"BYE",3}, {"CANCEL",6},
276 { "UPDATE", 6}};
277 pj_str_t accepted = { "application/sdp", 15 };
278
279 /* Register supported methods: INVITE, ACK, BYE, CANCEL, UPDATE */
280 pjsip_endpt_add_capability(endpt, &mod_inv.mod, PJSIP_H_ALLOW, NULL,
281 PJ_ARRAY_SIZE(allowed), allowed);
282
283 /* Register "application/sdp" in Accept header */
284 pjsip_endpt_add_capability(endpt, &mod_inv.mod, PJSIP_H_ACCEPT, NULL,
285 1, &accepted);
286
287 return PJ_SUCCESS;
288 }
289
290 /*
291 * Module unload()
292 */
293 static pj_status_t mod_inv_unload(void)
294 {
295 /* Should remove capability here */
296 return PJ_SUCCESS;
297 }
298
299 /*
300 * Add reference to INVITE session.
301 */
302 PJ_DEF(pj_status_t) pjsip_inv_add_ref( pjsip_inv_session *inv )
303 {
304 PJ_ASSERT_RETURN(inv && inv->ref_cnt, PJ_EINVAL);
305
306 pj_atomic_inc(inv->ref_cnt);
307
308 return PJ_SUCCESS;
309 }
310
311 static void inv_session_destroy(pjsip_inv_session *inv)
312 {
313 if (inv->last_ack) {
314 pjsip_tx_data_dec_ref(inv->last_ack);
315 inv->last_ack = NULL;
316 }
317 if (inv->invite_req) {
318 pjsip_tx_data_dec_ref(inv->invite_req);
319 inv->invite_req = NULL;
320 }
321 if (inv->pending_bye) {
322 pjsip_tx_data_dec_ref(inv->pending_bye);
323 inv->pending_bye = NULL;
324 }
325 pjsip_100rel_end_session(inv);
326 pjsip_timer_end_session(inv);
327 pjsip_dlg_dec_session(inv->dlg, &mod_inv.mod);
328
329 /* Release the flip-flop pools */
330 pj_pool_release(inv->pool_prov);
331 inv->pool_prov = NULL;
332 pj_pool_release(inv->pool_active);
333 inv->pool_active = NULL;
334
335 pj_atomic_destroy(inv->ref_cnt);
336 inv->ref_cnt = NULL;
337 }
338
339 static pj_status_t inv_async_auth_send_impl(
340 pjsip_auth_clt_sess *auth_sess,
341 void *user_data,
342 pjsip_tx_data *tdata)
343 {
344 pjsip_inv_session *inv = (pjsip_inv_session *)user_data;
345 PJ_UNUSED_ARG(auth_sess);
346 /* For initial INVITE auth retry the session must be restarted
347 * so that pjsip_inv_send_msg() can accept the new INVITE.
348 * In-dialog re-INVITE only needs invite_tsx cleared, which
349 * the call site handles before dispatching the challenge.
350 */
351 if (tdata->msg->line.req.method.id == PJSIP_INVITE_METHOD &&
352 inv->state != PJSIP_INV_STATE_CONFIRMED)
353 {
354 pjsip_inv_uac_restart(inv, PJ_FALSE);
355 }
356 return pjsip_inv_send_msg(inv, tdata);
357 }
358
359 /* Abandon callback for BYE 401/407: disconnect the session. */
360 static void inv_bye_auth_abandon_impl(pjsip_auth_clt_sess *auth_sess,
361 void *user_data)
362 {
363 pjsip_inv_session *inv = (pjsip_inv_session *)user_data;
364 pjsip_event usr_event;
365
366 PJ_UNUSED_ARG(auth_sess);
367 PJSIP_EVENT_INIT_USER(usr_event, NULL, NULL, NULL, NULL);
368 inv_set_cause(inv, PJSIP_SC_OK, NULL);
369 inv_set_state(inv, PJSIP_INV_STATE_DISCONNECTED, &usr_event);
370 }
371
372 /* Abandon callback for initial INVITE 401/407: terminate the session. */
373 static void inv_initial_invite_auth_abandon_impl(
374 pjsip_auth_clt_sess *auth_sess,
375 void *user_data)
376 {
377 pjsip_inv_session *inv = (pjsip_inv_session *)user_data;
378 pjsip_event usr_event;
379
380 PJ_UNUSED_ARG(auth_sess);
381 PJSIP_EVENT_INIT_USER(usr_event, NULL, NULL, NULL, NULL);
382 inv_set_cause(inv, PJSIP_SC_UNAUTHORIZED, NULL);
383 inv_set_state(inv, PJSIP_INV_STATE_DISCONNECTED, &usr_event);
384 }
385
386 /*
387 * Decrease INVITE session reference, destroy it when the reference count
388 * reaches zero.
389 */
390 PJ_DEF(pj_status_t) pjsip_inv_dec_ref( pjsip_inv_session *inv )
391 {
392 pj_atomic_value_t ref_cnt;
393
394 PJ_ASSERT_RETURN(inv && inv->ref_cnt, PJ_EINVAL);
395
396 ref_cnt = pj_atomic_dec_and_get(inv->ref_cnt);
397 pj_assert( ref_cnt >= 0);
398 if (ref_cnt == 0) {
399 inv_session_destroy(inv);
400 return PJ_EGONE;
401 }
402 return PJ_SUCCESS;
403 }
404
405 /*
406 * Set session state.
407 */
408 static void inv_set_state(pjsip_inv_session *inv, pjsip_inv_state state,
409 pjsip_event *e)
410 {
411 pjsip_inv_state prev_state = inv->state;
412 pj_bool_t dont_notify = PJ_FALSE;
413 pj_status_t status;
414
415 /* Prevent STATE_CALLING from being reported more than once because
416 * of authentication
417 * https://github.com/pjsip/pjproject/issues/1318
418 */
419 if (state==PJSIP_INV_STATE_CALLING &&
420 (inv->cb_called & (1 << PJSIP_INV_STATE_CALLING)) != 0)
421 {
422 dont_notify = PJ_TRUE;
423 }
424
425 /* If state is confirmed, check that SDP negotiation is done,
426 * otherwise disconnect the session.
427 */
428 if (state == PJSIP_INV_STATE_CONFIRMED) {
429 struct tsx_inv_data *tsx_inv_data = NULL;
430
431 if (inv->invite_tsx) {
432 tsx_inv_data = (struct tsx_inv_data*)
433 inv->invite_tsx->mod_data[mod_inv.mod.id];
434 }
435
436 if ((tsx_inv_data && !tsx_inv_data->sdp_done) &&
437 (!inv->neg || pjmedia_sdp_neg_get_state(inv->neg)!=
438 PJMEDIA_SDP_NEG_STATE_DONE))
439 {
440 pjsip_tx_data *bye;
441 pj_str_t reason = pj_str("SDP offer/answer incomplete");
442
443 PJ_LOG(4,(inv->obj_name, "SDP offer/answer incomplete, ending the "
444 "session"));
445
446 status = pjsip_inv_end_session(inv, PJSIP_SC_NOT_ACCEPTABLE,
447 &reason, &bye);
448 if (status == PJ_SUCCESS && bye)
449 status = pjsip_inv_send_msg(inv, bye);
450
451 return;
452 }
453 }
454
455 PJ_LOG(5, (inv->obj_name, "State changed from %s to %s, event=%s",
456 inv_state_names[inv->state], inv_state_names[state],
457 pjsip_event_str(e->type)));
458
459 /* Set state. */
460 inv->state = state;
461
462 /* If state is DISCONNECTED, cause code MUST have been set. */
463 pj_assert(inv->state != PJSIP_INV_STATE_DISCONNECTED ||
464 inv->cause != 0);
465
466 /* Mark the callback as called for this state */
467 inv->cb_called |= (1 << state);
468
469 /* Call on_state_changed() callback.
470 * While in the callback, can the state shift to DISCONNECTED? Perhaps
471 * yes, so better avoid premature destroy of the invite session by
472 * temporarily increase its ref counter.
473 */
474 pjsip_inv_add_ref(inv);
475 if (mod_inv.cb.on_state_changed && inv->notify && !dont_notify)
476 (*mod_inv.cb.on_state_changed)(inv, e);
477 pjsip_inv_dec_ref(inv);
478
479 /* The above callback may change the state, so we need to be careful here
480 * and only decrement inv under the following conditions:
481 * 1. If the state parameter is DISCONNECTED, and previous state is not
482 * already DISCONNECTED.
483 * This is to make sure that dec_ref() is not called more than once.
484 * 2. If current state is PJSIP_INV_STATE_DISCONNECTED.
485 * This is to make sure that dec_ref() is not called if user restarts
486 * inv within the callback. Note that this check must be last since
487 * inv may have already been destroyed.
488 */
489 if (state == PJSIP_INV_STATE_DISCONNECTED &&
490 prev_state != PJSIP_INV_STATE_DISCONNECTED &&
491 inv->state == PJSIP_INV_STATE_DISCONNECTED)
492 {
493 pjsip_inv_dec_ref(inv);
494 }
495 }
496
497
498 /*
499 * Set cause code.
500 */
501 static void inv_set_cause(pjsip_inv_session *inv, int cause_code,
502 const pj_str_t *cause_text)
503 {
504 if ((cause_code > (int)inv->cause) || inv->pending_bye) {
505 inv->cause = (pjsip_status_code) cause_code;
506 if (cause_text)
507 pj_strdup(inv->pool, &inv->cause_text, cause_text);
508 else if (cause_code/100 == 2)
509 inv->cause_text = pj_str("Normal call clearing");
510 else
511 inv->cause_text = *pjsip_get_status_text(cause_code);
512 }
513 }
514
515
516 /*
517 * Check if outgoing request needs to have SDP answer.
518 * This applies for both ACK and PRACK requests.
519 */
520 static const pjmedia_sdp_session *inv_has_pending_answer(pjsip_inv_session *inv,
521 pjsip_transaction *tsx)
522 {
523 pjmedia_sdp_neg_state neg_state;
524 const pjmedia_sdp_session *sdp = NULL;
525 pj_status_t status;
526
527 /* If SDP negotiator is ready, start negotiation. */
528
529 /* Start nego when appropriate. */
530 neg_state = inv->neg ? pjmedia_sdp_neg_get_state(inv->neg) :
531 PJMEDIA_SDP_NEG_STATE_NULL;
532
533 if (neg_state == PJMEDIA_SDP_NEG_STATE_DONE) {
534
535 /* Nothing to do */
536
537 } else if (neg_state == PJMEDIA_SDP_NEG_STATE_WAIT_NEGO &&
538 pjmedia_sdp_neg_has_local_answer(inv->neg) )
539 {
540 struct tsx_inv_data *tsx_inv_data;
541 struct tsx_inv_data dummy;
542
543 /* Get invite session's transaction data.
544 * Note that tsx may be NULL, for example when application sends
545 * delayed ACK request (at this time, the original INVITE
546 * transaction may have been destroyed.
547 */
548 if (tsx) {
549 tsx_inv_data = (struct tsx_inv_data*)tsx->mod_data[mod_inv.mod.id];
550 } else {
551 tsx_inv_data = &dummy;
552 pj_bzero(&dummy, sizeof(dummy));
553 dummy.inv = inv;
554 }
555
556 status = inv_negotiate_sdp(inv);
557 if (status != PJ_SUCCESS)
558 return NULL;
559
560 /* Mark this transaction has having SDP offer/answer done. */
561 tsx_inv_data->sdp_done = 1;
562
563 status = pjmedia_sdp_neg_get_active_local(inv->neg, &sdp);
564
565 } else {
566 /* This remark is only valid for ACK.
567 PJ_LOG(4,(inv->dlg->obj_name,
568 "FYI, the SDP negotiator state (%s) is in a mess "
569 "when sending this ACK/PRACK request",
570 pjmedia_sdp_neg_state_str(neg_state)));
571 */
572 }
573
574 return sdp;
575 }
576
577 /* Process pending disconnection
578 * https://github.com/pjsip/pjproject/issues/1712
579 */
580 static void inv_perform_pending_bye(pjsip_inv_session *inv)
581 {
582 if (inv->pending_bye) {
583 pjsip_tx_data *bye = inv->pending_bye;
584 pj_status_t status;
585
586 PJ_LOG(4,(inv->dlg->obj_name, "Sending pending BYE"));
587
588 inv->pending_bye = NULL;
589 status = pjsip_inv_send_msg(inv, bye);
590
591 if (status != PJ_SUCCESS) {
592 PJ_PERROR(1,(inv->dlg->obj_name, status,
593 "Failed sending pending BYE"));
594 }
595 }
596 }
597
598 /*
599 * Send ACK for 2xx response.
600 */
601 static pj_status_t inv_send_ack(pjsip_inv_session *inv, pjsip_event *e)
602 {
603 pjsip_rx_data *rdata;
604 pjsip_event ack_e;
605 pj_status_t status;
606
607 if (e->type == PJSIP_EVENT_TSX_STATE)
608 rdata = e->body.tsx_state.src.rdata;
609 else if (e->type == PJSIP_EVENT_RX_MSG)
610 rdata = e->body.rx_msg.rdata;
611 else {
612 pj_assert(!"Unsupported event type");
613 return PJ_EBUG;
614 }
615
616 /* Note that with https://github.com/pjsip/pjproject/issues/1725, this
617 * function can be called to send ACK for previous INVITE 200/OK
618 * retransmission
619 */
620
621 PJ_LOG(5,(inv->obj_name, "Received %s, sending ACK",
622 pjsip_rx_data_get_info(rdata)));
623
624 /* Check if we have cached ACK request and if we have sent it.
625 * Must not use the cached ACK if it's still marked as pending
626 * by transport (#1011).
627 */
628 if (inv->last_ack && rdata->msg_info.cseq->cseq == inv->last_ack_cseq &&
629 inv->last_ack->tp_info.transport != NULL &&
630 !inv->last_ack->is_pending)
631 {
632 pjsip_tx_data_add_ref(inv->last_ack);
633
634 } else if (mod_inv.cb.on_send_ack) {
635 /* If application handles ACK transmission manually, just notify the
636 * callback
637 */
638 PJ_LOG(5,(inv->obj_name, "Received %s, notifying application callback",
639 pjsip_rx_data_get_info(rdata)));
640
641 (*mod_inv.cb.on_send_ack)(inv, rdata);
642 return PJ_SUCCESS;
643
644 } else {
645 status = pjsip_inv_create_ack(inv, rdata->msg_info.cseq->cseq,
646 &inv->last_ack);
647 if (status != PJ_SUCCESS)
648 return status;
649 }
650
651 PJSIP_EVENT_INIT_TX_MSG(ack_e, inv->last_ack);
652
653 /* Send ACK */
654 status = pjsip_dlg_send_request(inv->dlg, inv->last_ack, -1, NULL);
655 if (status != PJ_SUCCESS) {
656 /* Better luck next time */
657 pj_assert(!"Unable to send ACK!");
658 return status;
659 }
660
661
662 /* Set state to CONFIRMED (if we're not in CONFIRMED yet).
663 * But don't set it to CONFIRMED if we're already DISCONNECTED
664 * (this may have been a late 200/OK response.
665 */
666 if (inv->state < PJSIP_INV_STATE_CONFIRMED) {
667 inv_set_state(inv, PJSIP_INV_STATE_CONFIRMED, &ack_e);
668 } else if (inv->state == PJSIP_INV_STATE_DISCONNECTED && inv->last_ack) {
669 /* Avoid possible leaked tdata when invite session is already
670 * destroyed.
671 * https://github.com/pjsip/pjproject/pull/2432
672 */
673 pjsip_tx_data_dec_ref(inv->last_ack);
674 inv->last_ack = NULL;
675 }
676
677 return PJ_SUCCESS;
678 }
679
680 /*
681 * Module on_rx_request()
682 *
683 * This callback is called for these events:
684 * - endpoint receives request which was unhandled by higher priority
685 * modules (e.g. transaction layer, dialog layer).
686 * - dialog distributes incoming request to its usages.
687 */
688 static pj_bool_t mod_inv_on_rx_request(pjsip_rx_data *rdata)
689 {
690 pjsip_method *method;
691 pjsip_dialog *dlg;
692 pjsip_inv_session *inv;
693
694 /* Only wants to receive request from a dialog. */
695 dlg = pjsip_rdata_get_dlg(rdata);
696 if (dlg == NULL)
697 return PJ_FALSE;
698
699 inv = (pjsip_inv_session*) dlg->mod_data[mod_inv.mod.id];
700
701 /* Report to dialog that we handle INVITE, CANCEL, BYE, ACK.
702 * If we need to send response, it will be sent in the state
703 * handlers.
704 */
705 method = &rdata->msg_info.msg->line.req.method;
706
707 if (method->id == PJSIP_INVITE_METHOD) {
708 return PJ_TRUE;
709 }
710
711 /* BYE and CANCEL must have existing invite session */
712 if (method->id == PJSIP_BYE_METHOD ||
713 method->id == PJSIP_CANCEL_METHOD)
714 {
715 if (inv == NULL)
716 return PJ_FALSE;
717
718 return PJ_TRUE;
719 }
720
721 /* On receipt ACK request, when state is CONNECTING,
722 * move state to CONFIRMED.
723 */
724 if (method->id == PJSIP_ACK_METHOD && inv) {
725
726 /* Ignore if we don't have INVITE in progress */
727 if (!inv->invite_tsx) {
728 return PJ_TRUE;
729 }
730
731 /* Ignore ACK if pending INVITE transaction has not finished. */
732 if (inv->invite_tsx->state < PJSIP_TSX_STATE_COMPLETED) {
733 return PJ_TRUE;
734 }
735
736 /* Ignore ACK with different CSeq
737 * https://github.com/pjsip/pjproject/issues/1391
738 */
739 if (rdata->msg_info.cseq->cseq != inv->invite_tsx->cseq) {
740 return PJ_TRUE;
741 }
742
743 /* Terminate INVITE transaction, if it's still present. */
744 if (inv->invite_tsx->state <= PJSIP_TSX_STATE_COMPLETED) {
745 /* Before we terminate INVITE transaction, process the SDP
746 * in the ACK request, if any.
747 * Only do this when invite state is not already disconnected
748 * (https://github.com/pjsip/pjproject/issues/640).
749 */
750 if (inv->state < PJSIP_INV_STATE_DISCONNECTED) {
751 inv_check_sdp_in_incoming_msg(inv, inv->invite_tsx, rdata);
752
753 /* Check if local offer got no SDP answer and INVITE session
754 * is in CONFIRMED state.
755 */
756 if (pjmedia_sdp_neg_get_state(inv->neg)==
757 PJMEDIA_SDP_NEG_STATE_LOCAL_OFFER &&
758 inv->state==PJSIP_INV_STATE_CONFIRMED)
759 {
760 pjmedia_sdp_neg_cancel_offer(inv->neg);
761 }
762 }
763
764 /* Now we can terminate the INVITE transaction */
765 if (inv->invite_tsx->status_code/100 == 2) {
766 /* Stop retransmissions of 200 response */
767 pjsip_tsx_stop_retransmit(inv->invite_tsx);
768
769 /* Schedule INVITE tsx termination to absorb request
770 * retransmissions for about 64*T1 (~32 seconds).
771 * See also #4765.
772 */
773 pjsip_tsx_terminate_async2(inv->invite_tsx,
774 inv->invite_tsx->status_code,
775 NULL,
776 pjsip_cfg()->tsx.td);
777 } else {
778 /* If the response was not 2xx, the ACK is considered part of
779 * the INVITE transaction, so should have been handled by
780 * the transaction.
781 * But for best effort, we will also attempt to terminate
782 * the tsx here. However, we need to do it asynchronously
783 * to avoid deadlock.
784 */
785 pjsip_tsx_terminate_async(inv->invite_tsx,
786 inv->invite_tsx->status_code);
787 }
788 inv->invite_tsx = NULL;
789
790 if (inv->last_answer) {
791 pjsip_tx_data_dec_ref(inv->last_answer);
792 inv->last_answer = NULL;
793 }
794 }
795
796 /* On receipt of ACK, only set state to confirmed when state
797 * is CONNECTING (e.g. we don't want to set the state to confirmed
798 * when we receive ACK retransmission after sending non-2xx!)
799 */
800 if (inv->state == PJSIP_INV_STATE_CONNECTING) {
801 pjsip_event event;
802
803 PJSIP_EVENT_INIT_RX_MSG(event, rdata);
804 inv_set_state(inv, PJSIP_INV_STATE_CONFIRMED, &event);
805
806 /* Send pending BYE if any:
807 * https://github.com/pjsip/pjproject/issues/1712
808 * Do this after setting the state to CONFIRMED, so that we
809 * have consistent CONFIRMED state between caller and callee.
810 */
811 if (inv->pending_bye)
812 inv_perform_pending_bye(inv);
813 }
814 }
815
816 return PJ_FALSE;
817 }
818
819 /* This function will process Session Timer headers in received
820 * 2xx or 422 response of INVITE/UPDATE request.
821 */
822 static pj_status_t handle_timer_response(pjsip_inv_session *inv,
823 const pjsip_rx_data *rdata,
824 pj_bool_t end_sess_on_failure)
825 {
826 pjsip_status_code st_code;
827 pj_status_t status;
828
829 status = pjsip_timer_process_resp(inv, rdata, &st_code);
830 if (status != PJ_SUCCESS && end_sess_on_failure) {
831 pjsip_tx_data *tdata;
832 pj_status_t status2;
833 pj_str_t reason = pj_str("Failed processing session timer response");
834
835 status2 = pjsip_inv_end_session(inv, st_code, &reason, &tdata);
836 if (tdata && status2 == PJ_SUCCESS)
837 pjsip_inv_send_msg(inv, tdata);
838 }
839
840 return status;
841 }
842
843 /*
844 * Module on_rx_response().
845 *
846 * This callback is called for these events:
847 * - dialog distributes incoming 2xx response to INVITE (outside
848 * transaction) to its usages.
849 * - endpoint distributes strayed responses.
850 */
851 static pj_bool_t mod_inv_on_rx_response(pjsip_rx_data *rdata)
852 {
853 pjsip_dialog *dlg;
854 pjsip_inv_session *inv;
855 pjsip_msg *msg = rdata->msg_info.msg;
856
857 dlg = pjsip_rdata_get_dlg(rdata);
858
859 /* Ignore responses outside dialog */
860 if (dlg == NULL)
861 return PJ_FALSE;
862
863 /* Ignore responses not belonging to invite session */
864 inv = pjsip_dlg_get_inv_session(dlg);
865 if (inv == NULL)
866 return PJ_FALSE;
867
868 /* This MAY be retransmission of 2xx response to INVITE.
869 * If it is, we need to send ACK.
870 */
871 if (msg->type == PJSIP_RESPONSE_MSG && msg->line.status.code/100==2 &&
872 rdata->msg_info.cseq->method.id == PJSIP_INVITE_METHOD)
873 {
874 /* The code inside "if" is called the second time 200/OK
875 * retransmission is received. Also handle the situation
876 * when we have another re-INVITE on going and 200/OK
877 * retransmission is received. See:
878 * https://github.com/pjsip/pjproject/issues/1725.
879 * Also send ACK for 200/OK of pending re-INVITE after call is
880 * disconnected (see https://github.com/pjsip/pjproject/issues/1755).
881 */
882 if (inv->invite_tsx == NULL ||
883 inv->state == PJSIP_INV_STATE_DISCONNECTED ||
884 (inv->last_ack && inv->last_ack_cseq==rdata->msg_info.cseq->cseq))
885 {
886 pjsip_event e;
887
888 PJSIP_EVENT_INIT_RX_MSG(e, rdata);
889 inv_send_ack(inv, &e);
890 return PJ_TRUE;
891 }
892 }
893
894 /* No other processing needs to be done here. */
895 return PJ_FALSE;
896 }
897
898 /*
899 * Module on_tsx_state()
900 *
901 * This callback is called by dialog framework for all transactions
902 * inside the dialog for all its dialog usages.
903 */
904 static void mod_inv_on_tsx_state(pjsip_transaction *tsx, pjsip_event *e)
905 {
906 pjsip_dialog *dlg;
907 pjsip_inv_session *inv;
908 pj_bool_t cb_called = PJ_FALSE;
909
910 dlg = pjsip_tsx_get_dlg(tsx);
911 if (dlg == NULL)
912 return;
913
914 inv = pjsip_dlg_get_inv_session(dlg);
915 if (inv == NULL)
916 return;
917
918 /* Call on_tsx_state_changed() upon receipt of request (tsx state is
919 * PJSIP_TSX_STATE_TRYING). We need to do this before calling the state
920 * handler since the handler will send response and change the tsx state.
921 */
922 if (mod_inv.cb.on_tsx_state_changed && inv->notify &&
923 e->body.tsx_state.tsx->state == PJSIP_TSX_STATE_TRYING)
924 {
925 cb_called = PJ_TRUE;
926 (*mod_inv.cb.on_tsx_state_changed)(inv, tsx, e);
927 }
928
929 /* Call state handler for the invite session. */
930 (*inv_state_handler[inv->state])(inv, e);
931
932 /* Clear invite transaction when tsx is terminated.
933 * Necessary for app that wants to send a new re-INVITE request immediately
934 * after the transaction is terminated.
935 */
936 if (tsx->state==PJSIP_TSX_STATE_TERMINATED && tsx == inv->invite_tsx) {
937 inv->invite_tsx = NULL;
938
939 if (inv->last_answer) {
940 pjsip_tx_data_dec_ref(inv->last_answer);
941 inv->last_answer = NULL;
942 }
943 }
944
945 /* Call on_tsx_state. */
946 if (mod_inv.cb.on_tsx_state_changed && inv->notify && !cb_called) {
947 (*mod_inv.cb.on_tsx_state_changed)(inv, tsx, e);
948 }
949
950 /* Clear invite transaction when tsx is confirmed.
951 * Previously we set invite_tsx to NULL only when transaction has
952 * terminated, but this didn't work when ACK has the same Via branch
953 * value as the INVITE (see https://github.com/pjsip/pjproject/issues/113)
954 */
955 if (tsx->state>=PJSIP_TSX_STATE_CONFIRMED && tsx == inv->invite_tsx) {
956 inv->invite_tsx = NULL;
957
958 if (inv->last_answer) {
959 pjsip_tx_data_dec_ref(inv->last_answer);
960 inv->last_answer = NULL;
961 }
962 }
963 }
964
965 /*
966 * Check if tx_data has sdp.
967 */
968 static pj_bool_t tx_data_has_sdp(const pjsip_tx_data *tdata)
969 {
970 pjsip_msg_body *body = tdata->msg->body;
971 pjsip_media_type app_sdp;
972
973 PJ_ASSERT_RETURN(tdata, PJ_FALSE);
974
975 pjsip_media_type_init2(&app_sdp, "application", "sdp");
976
977 if (body &&
978 pj_stricmp(&body->content_type.type, &app_sdp.type)==0 &&
979 pj_stricmp(&body->content_type.subtype, &app_sdp.subtype)==0)
980 {
981 return PJ_TRUE;
982
983 } else if (body &&
984 pj_stricmp2(&body->content_type.type, "multipart") &&
985 (pj_stricmp2(&body->content_type.subtype, "mixed")==0 ||
986 pj_stricmp2(&body->content_type.subtype, "alternative")==0))
987 {
988 pjsip_multipart_part *part;
989
990 part = pjsip_multipart_find_part(body, &app_sdp, NULL);
991 if (part) {
992 return PJ_TRUE;
993 }
994 }
995
996 return PJ_FALSE;
997 }
998
999
1000 /*
1001 * Initialize the invite module.
1002 */
1003 PJ_DEF(pj_status_t) pjsip_inv_usage_init( pjsip_endpoint *endpt,
1004 const pjsip_inv_callback *cb)
1005 {
1006 pj_status_t status;
1007
1008 /* Check arguments. */
1009 PJ_ASSERT_RETURN(endpt && cb, PJ_EINVAL);
1010
1011 /* Some callbacks are mandatory */
1012 PJ_ASSERT_RETURN(cb->on_state_changed, PJ_EINVAL);
1013
1014 /* Check if module already registered. */
1015 PJ_ASSERT_RETURN(mod_inv.mod.id == -1, PJ_EINVALIDOP);
1016
1017 /* Copy param. */
1018 pj_memcpy(&mod_inv.cb, cb, sizeof(pjsip_inv_callback));
1019
1020 mod_inv.endpt = endpt;
1021
1022 /* Register the module. */
1023 status = pjsip_endpt_register_module(endpt, &mod_inv.mod);
1024 if (status != PJ_SUCCESS)
1025 return status;
1026
1027 return PJ_SUCCESS;
1028 }
1029
1030 /*
1031 * Get the instance of invite module.
1032 */
1033 PJ_DEF(pjsip_module*) pjsip_inv_usage_instance(void)
1034 {
1035 return &mod_inv.mod;
1036 }
1037
1038
1039
1040 /*
1041 * Return the invite session for the specified dialog.
1042 */
1043 PJ_DEF(pjsip_inv_session*) pjsip_dlg_get_inv_session(pjsip_dialog *dlg)
1044 {
1045 return (pjsip_inv_session*) dlg->mod_data[mod_inv.mod.id];
1046 }
1047
1048
1049 /*
1050 * Get INVITE state name.
1051 */
1052 PJ_DEF(const char *) pjsip_inv_state_name(pjsip_inv_state state)
1053 {
1054 PJ_ASSERT_RETURN(state >= PJSIP_INV_STATE_NULL &&
1055 state <= PJSIP_INV_STATE_DISCONNECTED,
1056 "??");
1057
1058 return inv_state_names[state];
1059 }
1060
1061 /*
1062 * Create UAC invite session.
1063 */
1064 PJ_DEF(pj_status_t) pjsip_inv_create_uac( pjsip_dialog *dlg,
1065 const pjmedia_sdp_session *local_sdp,
1066 unsigned options,
1067 pjsip_inv_session **p_inv)
1068 {
1069 pjsip_inv_session *inv;
1070 pj_status_t status;
1071
1072 /* Verify arguments. */
1073 PJ_ASSERT_RETURN(dlg && p_inv, PJ_EINVAL);
1074
1075 /* Must lock dialog first */
1076 pjsip_dlg_inc_lock(dlg);
1077
1078 /* Normalize options */
1079 if (options & PJSIP_INV_REQUIRE_100REL)
1080 options |= PJSIP_INV_SUPPORT_100REL;
1081 if (options & PJSIP_INV_REQUIRE_TIMER)
1082 options |= PJSIP_INV_SUPPORT_TIMER;
1083 if (options & PJSIP_INV_REQUIRE_TRICKLE_ICE)
1084 options |= PJSIP_INV_SUPPORT_TRICKLE_ICE;
1085
1086 /* Create the session */
1087 inv = PJ_POOL_ZALLOC_T(dlg->pool, pjsip_inv_session);
1088 pj_assert(inv != NULL);
1089
1090 status = pj_atomic_create(dlg->pool, 0, &inv->ref_cnt);
1091 if (status != PJ_SUCCESS) {
1092 pjsip_dlg_dec_lock(dlg);
1093 return status;
1094 }
1095
1096 inv->pool = dlg->pool;
1097 inv->role = PJSIP_ROLE_UAC;
1098 inv->state = PJSIP_INV_STATE_NULL;
1099 inv->dlg = dlg;
1100 inv->options = options;
1101 inv->notify = PJ_TRUE;
1102 inv->cause = (pjsip_status_code) 0;
1103
1104 /* Create flip-flop pool (see ticket #877) */
1105 /* (using inv->obj_name as temporary variable for pool names */
1106 pj_ansi_snprintf(inv->obj_name, PJ_MAX_OBJ_NAME, "inv%p", dlg->pool);
1107 inv->pool_prov = pjsip_endpt_create_pool(dlg->endpt, inv->obj_name,
1108 POOL_INIT_SIZE, POOL_INC_SIZE);
1109 inv->pool_active = pjsip_endpt_create_pool(dlg->endpt, inv->obj_name,
1110 POOL_INIT_SIZE, POOL_INC_SIZE);
1111
1112 /* Object name will use the same dialog pointer. */
1113 pj_ansi_snprintf(inv->obj_name, PJ_MAX_OBJ_NAME, "inv%p", dlg);
1114
1115 /* Create negotiator if local_sdp is specified. */
1116 if (local_sdp) {
1117 status = pjmedia_sdp_neg_create_w_local_offer(inv->pool,
1118 local_sdp, &inv->neg);
1119 if (status != PJ_SUCCESS) {
1120 pjsip_dlg_dec_lock(dlg);
1121 return status;
1122 }
1123 }
1124
1125 /* Register invite as dialog usage. */
1126 status = pjsip_dlg_add_usage(dlg, &mod_inv.mod, inv);
1127 if (status != PJ_SUCCESS) {
1128 pjsip_dlg_dec_lock(dlg);
1129 return status;
1130 }
1131
1132 /* Increment dialog session */
1133 pjsip_dlg_inc_session(dlg, &mod_inv.mod);
1134
1135 /* Create 100rel handler */
1136 pjsip_100rel_attach(inv);
1137
1138 /* Done */
1139 pjsip_inv_add_ref(inv);
1140 *p_inv = inv;
1141
1142 pjsip_dlg_dec_lock(dlg);
1143
1144 PJ_LOG(5,(inv->obj_name, "UAC invite session created for dialog %s",
1145 dlg->obj_name));
1146
1147 return PJ_SUCCESS;
1148 }
1149
1150 PJ_DEF(pjsip_sdp_info*) pjsip_get_sdp_info(pj_pool_t *pool,
1151 pjsip_msg_body *body,
1152 pjsip_media_type *msg_media_type,
1153 const pjsip_media_type *search_media_type)
1154 {
1155 pjsip_sdp_info *sdp_info;
1156 pjsip_media_type search_type;
1157 pjsip_media_type multipart_mixed;
1158 pjsip_media_type multipart_alternative;
1159 pjsip_media_type *msg_type;
1160 pj_status_t status;
1161
1162 sdp_info = PJ_POOL_ZALLOC_T(pool,
1163 pjsip_sdp_info);
1164
1165 PJ_ASSERT_RETURN(mod_inv.mod.id >= 0, sdp_info);
1166
1167 if (!body) {
1168 return sdp_info;
1169 }
1170
1171 if (msg_media_type) {
1172 msg_type = msg_media_type;
1173 } else {
1174 if (body->content_type.type.slen == 0) {
1175 return sdp_info;
1176 }
1177 msg_type = &body->content_type;
1178 }
1179
1180 if (!search_media_type) {
1181 pjsip_media_type_init2(&search_type, "application", "sdp");
1182 } else {
1183 pj_memcpy(&search_type, search_media_type, sizeof(search_type));
1184 }
1185
1186 pjsip_media_type_init2(&multipart_mixed, "multipart", "mixed");
1187 pjsip_media_type_init2(&multipart_alternative, "multipart", "alternative");
1188
1189 if (pjsip_media_type_cmp(msg_type, &search_type, PJ_FALSE) == 0)
1190 {
1191 /*
1192 * If the print_body function is print_sdp, we know that
1193 * body->data is a pjmedia_sdp_session object and came from
1194 * a tx_data. If not, it's the text representation of the
1195 * sdp from an rx_data.
1196 */
1197 if (body->print_body == print_sdp) {
1198 sdp_info->sdp = body->data;
1199 } else {
1200 sdp_info->body.ptr = (char*)body->data;
1201 sdp_info->body.slen = body->len;
1202 }
1203 } else if (pjsip_media_type_cmp(&multipart_mixed, msg_type, PJ_FALSE) == 0 ||
1204 pjsip_media_type_cmp(&multipart_alternative, msg_type, PJ_FALSE) == 0)
1205 {
1206 pjsip_multipart_part *part;
1207 part = pjsip_multipart_find_part(body, &search_type, NULL);
1208 if (part) {
1209 if (part->body->print_body == print_sdp) {
1210 sdp_info->sdp = part->body->data;
1211 } else {
1212 sdp_info->body.ptr = (char*)part->body->data;
1213 sdp_info->body.slen = part->body->len;
1214 }
1215 }
1216 }
1217
1218 /*
1219 * If the body was already a pjmedia_sdp_session, we can just
1220 * return it. If not and there wasn't a text representation
1221 * of the sdp either, we can also just return.
1222 */
1223 if (sdp_info->sdp || !sdp_info->body.ptr) {
1224 return sdp_info;
1225 }
1226
1227 /*
1228 * If the body was the text representation of teh SDP, we need
1229 * to parse it to create a pjmedia_sdp_session object.
1230 */
1231 status = pjmedia_sdp_parse(pool,
1232 sdp_info->body.ptr,
1233 sdp_info->body.slen,
1234 &sdp_info->sdp);
1235 if (status == PJ_SUCCESS)
1236 status = pjmedia_sdp_validate2(sdp_info->sdp, PJ_FALSE);
1237
1238 if (status != PJ_SUCCESS) {
1239 sdp_info->sdp = NULL;
1240 PJ_PERROR(1, (THIS_FILE, status,
1241 "Error parsing/validating SDP body"));
1242 }
1243
1244 sdp_info->sdp_err = status;
1245
1246 return sdp_info;
1247 }
1248
1249 PJ_DEF(pjsip_rdata_sdp_info*) pjsip_rdata_get_sdp_info2(
1250 pjsip_rx_data *rdata,
1251 const pjsip_media_type *search_media_type)
1252 {
1253 pjsip_media_type *msg_media_type = NULL;
1254 pjsip_rdata_sdp_info *sdp_info;
1255
1256 if (rdata->endpt_info.mod_data[mod_inv.mod.id]) {
1257 return (pjsip_rdata_sdp_info *)rdata->endpt_info.mod_data[mod_inv.mod.id];
1258 }
1259
1260 /*
1261 * rdata should have a Content-Type header at this point but we'll
1262 * make sure.
1263 */
1264 if (rdata->msg_info.ctype) {
1265 msg_media_type = &rdata->msg_info.ctype->media;
1266 }
1267 sdp_info = pjsip_get_sdp_info(rdata->tp_info.pool,
1268 rdata->msg_info.msg->body,
1269 msg_media_type,
1270 search_media_type);
1271 rdata->endpt_info.mod_data[mod_inv.mod.id] = sdp_info;
1272
1273 return sdp_info;
1274 }
1275
1276 PJ_DEF(pjsip_rdata_sdp_info*) pjsip_rdata_get_sdp_info(pjsip_rx_data *rdata)
1277 {
1278 return pjsip_rdata_get_sdp_info2(rdata, NULL);
1279 }
1280
1281 PJ_DEF(pjsip_tdata_sdp_info*) pjsip_tdata_get_sdp_info2(
1282 pjsip_tx_data *tdata,
1283 const pjsip_media_type *search_media_type)
1284 {
1285 pjsip_ctype_hdr *ctype_hdr = NULL;
1286 pjsip_media_type *msg_media_type = NULL;
1287 pjsip_tdata_sdp_info *sdp_info;
1288
1289 if (tdata->mod_data[mod_inv.mod.id]) {
1290 return (pjsip_tdata_sdp_info *)tdata->mod_data[mod_inv.mod.id];
1291 }
1292 /*
1293 * tdata won't usually have a Content-Type header at this point
1294 * but we'll check just the same,
1295 */
1296 ctype_hdr = pjsip_msg_find_hdr(tdata->msg, PJSIP_H_CONTENT_TYPE, NULL);
1297 if (ctype_hdr) {
1298 msg_media_type = &ctype_hdr->media;
1299 }
1300
1301 sdp_info = pjsip_get_sdp_info(tdata->pool,
1302 tdata->msg->body,
1303 msg_media_type,
1304 search_media_type);
1305 tdata->mod_data[mod_inv.mod.id] = sdp_info;
1306
1307 return sdp_info;
1308 }
1309
1310 PJ_DEF(pjsip_tdata_sdp_info*) pjsip_tdata_get_sdp_info(pjsip_tx_data *tdata)
1311 {
1312 return pjsip_tdata_get_sdp_info2(tdata, NULL);
1313 }
1314
1315 /*
1316 * Verify incoming INVITE request.
1317 */
1318 PJ_DEF(pj_status_t) pjsip_inv_verify_request3(pjsip_rx_data *rdata,
1319 pj_pool_t *tmp_pool,
1320 unsigned *options,
1321 const pjmedia_sdp_session *r_sdp,
1322 const pjmedia_sdp_session *l_sdp,
1323 pjsip_dialog *dlg,
1324 pjsip_endpoint *endpt,
1325 pjsip_tx_data **p_tdata)
1326 {
1327 pjsip_msg *msg = NULL;
1328 pjsip_allow_hdr *allow = NULL;
1329 pjsip_supported_hdr *sup_hdr = NULL;
1330 pjsip_require_hdr *req_hdr = NULL;
1331 pjsip_contact_hdr *c_hdr = NULL;
1332 int code = 200;
1333 unsigned rem_option = 0;
1334 pj_status_t status = PJ_SUCCESS;
1335 pjsip_hdr res_hdr_list;
1336 pjsip_rdata_sdp_info *sdp_info;
1337
1338 /* Init return arguments. */
1339 if (p_tdata) *p_tdata = NULL;
1340
1341 /* Verify arguments. */
1342 PJ_ASSERT_RETURN(tmp_pool != NULL && options != NULL, PJ_EINVAL);
1343
1344 /* Normalize options */
1345 if (*options & PJSIP_INV_REQUIRE_100REL)
1346 *options |= PJSIP_INV_SUPPORT_100REL;
1347 if (*options & PJSIP_INV_REQUIRE_TIMER)
1348 *options |= PJSIP_INV_SUPPORT_TIMER;
1349 if (*options & PJSIP_INV_REQUIRE_ICE)
1350 *options |= PJSIP_INV_SUPPORT_ICE;
1351 if (*options & PJSIP_INV_REQUIRE_TRICKLE_ICE)
1352 *options |= PJSIP_INV_SUPPORT_TRICKLE_ICE;
1353 if (*options & PJSIP_INV_REQUIRE_SIPREC)
1354 *options |= PJSIP_INV_SUPPORT_SIPREC;
1355
1356 if (rdata) {
1357 /* Get the message in rdata */
1358 msg = rdata->msg_info.msg;
1359
1360 /* Must be INVITE request. */
1361 PJ_ASSERT_RETURN(msg && msg->type == PJSIP_REQUEST_MSG &&
1362 msg->line.req.method.id == PJSIP_INVITE_METHOD,
1363 PJ_EINVAL);
1364 }
1365
1366 /* If tdata is specified, then either dlg or endpt must be specified */
1367 PJ_ASSERT_RETURN((!p_tdata) || (endpt || dlg), PJ_EINVAL);
1368
1369 /* Get the endpoint */
1370 endpt = endpt ? endpt : dlg->endpt;
1371
1372 /* Init response header list */
1373 pj_list_init(&res_hdr_list);
1374
1375 /* Check the Contact header */
1376 if (msg) {
1377 c_hdr = (pjsip_contact_hdr*)
1378 pjsip_msg_find_hdr(msg, PJSIP_H_CONTACT, NULL);
1379 }
1380 if (msg && (!c_hdr || !c_hdr->uri)) {
1381 /* Missing Contact header or Contact contains "*" */
1382 pjsip_warning_hdr *w;
1383 pj_str_t warn_text;
1384
1385 warn_text = pj_str("Bad/missing Contact header");
1386 w = pjsip_warning_hdr_create(tmp_pool, 399,
1387 pjsip_endpt_name(endpt),
1388 &warn_text);
1389 if (w) {
1390 pj_list_push_back(&res_hdr_list, w);
1391 }
1392
1393 code = PJSIP_SC_BAD_REQUEST;
1394 status = PJSIP_ERRNO_FROM_SIP_STATUS(code);
1395 goto on_return;
1396 }
1397
1398 /* Ticket #1735: Check Contact/Record-Route header in a secure dialog. */
1399 if (pjsip_cfg()->endpt.disable_secure_dlg_check == PJ_FALSE &&
1400 msg && PJSIP_URI_SCHEME_IS_SIPS(msg->line.req.uri))
1401 {
1402 /* Check Contact header */
1403 if (!PJSIP_URI_SCHEME_IS_SIPS(c_hdr->uri))
1404 status = PJSIP_ESESSIONINSECURE;
1405
1406 /* Check top Record-Route header */
1407 if (status == PJ_SUCCESS) {
1408 pjsip_rr_hdr *r = (pjsip_rr_hdr*)
1409 pjsip_msg_find_hdr(msg, PJSIP_H_RECORD_ROUTE,
1410 NULL);
1411 if (r && !PJSIP_URI_SCHEME_IS_SIPS(&r->name_addr)) {
1412 /* Not "sips", check if it is "sip" and has param
1413 * "transport=tls".
1414 */
1415 if (PJSIP_URI_SCHEME_IS_SIP(&r->name_addr)) {
1416 pjsip_sip_uri *sip_uri = (pjsip_sip_uri*)
1417 pjsip_uri_get_uri(r->name_addr.uri);
1418 if (pj_stricmp2(&sip_uri->transport_param, "tls")!=0)
1419 status = PJSIP_ESESSIONINSECURE;
1420 } else {
1421 /* Not "sips" nor "sip", treat it as insecure? */
1422 status = PJSIP_ESESSIONINSECURE;
1423 }
1424 }
1425 }
1426
1427 if (status != PJ_SUCCESS) {
1428 pjsip_warning_hdr *w;
1429 pj_str_t warn_text = pj_str("SIPS Required");
1430 w = pjsip_warning_hdr_create(tmp_pool, 381,
1431 pjsip_endpt_name(endpt),
1432 &warn_text);
1433 if (w) {
1434 pj_list_push_back(&res_hdr_list, w);
1435 }
1436 code = PJSIP_SC_TEMPORARILY_UNAVAILABLE;
1437 goto on_return;
1438 }
1439 }
1440
1441 /* Check the request body, see if it's something that we support,
1442 * only when the body hasn't been parsed before.
1443 */
1444 if (r_sdp == NULL && rdata) {
1445 sdp_info = pjsip_rdata_get_sdp_info(rdata);
1446 } else {
1447 sdp_info = NULL;
1448 }
1449
1450 if (r_sdp==NULL && msg && msg->body) {
1451
1452 /* Check if body really contains SDP. */
1453 if (sdp_info->body.ptr == NULL && !PJSIP_INV_ACCEPT_UNKNOWN_BODY) {
1454 /* Couldn't find "application/sdp" */
1455 code = PJSIP_SC_UNSUPPORTED_MEDIA_TYPE;
1456 status = PJSIP_ERRNO_FROM_SIP_STATUS(code);
1457
1458 if (p_tdata) {
1459 /* Add Accept header to response */
1460 pjsip_accept_hdr *acc;
1461
1462 acc = pjsip_accept_hdr_create(tmp_pool);
1463 PJ_ASSERT_RETURN(acc, PJ_ENOMEM);
1464 acc->values[acc->count++] = pj_str("application/sdp");
1465 pj_list_push_back(&res_hdr_list, acc);
1466 }
1467
1468 goto on_return;
1469 }
1470
1471 if (sdp_info->sdp_err != PJ_SUCCESS) {
1472 /* Unparseable or invalid SDP */
1473 code = PJSIP_SC_BAD_REQUEST;
1474
1475 if (p_tdata) {
1476 /* Add Warning header. */
1477 pjsip_warning_hdr *w;
1478
1479 w = pjsip_warning_hdr_create_from_status(tmp_pool,
1480 pjsip_endpt_name(endpt),
1481 sdp_info->sdp_err);
1482 PJ_ASSERT_RETURN(w, PJ_ENOMEM);
1483
1484 pj_list_push_back(&res_hdr_list, w);
1485 }
1486
1487 goto on_return;
1488 }
1489
1490 r_sdp = sdp_info->sdp;
1491 }
1492
1493 if (r_sdp) {
1494 /* Negotiate with local SDP */
1495 if (l_sdp) {
1496 pjmedia_sdp_neg *neg;
1497
1498 /* Local SDP must be valid! */
1499 status = pjmedia_sdp_validate(l_sdp);
1500 if (status != PJ_SUCCESS) {
1501 pj_assert(!"Invalid local SDP");
1502 code = PJSIP_SC_INTERNAL_SERVER_ERROR;
1503 goto on_return;
1504 }
1505
1506 /* Create SDP negotiator */
1507 status = pjmedia_sdp_neg_create_w_remote_offer(
1508 tmp_pool, l_sdp, r_sdp, &neg);
1509 if (status != PJ_SUCCESS) {
1510 pj_assert(!"Failed creating SDP negotiator");
1511 code = PJSIP_SC_INTERNAL_SERVER_ERROR;
1512 goto on_return;
1513 }
1514
1515 /* Negotiate SDP */
1516 status = pjmedia_sdp_neg_negotiate(tmp_pool, neg, 0);
1517 if (status != PJ_SUCCESS) {
1518
1519 /* Incompatible media */
1520 code = PJSIP_SC_NOT_ACCEPTABLE_HERE;
1521
1522 if (p_tdata) {
1523 pjsip_accept_hdr *acc;
1524 pjsip_warning_hdr *w;
1525
1526 /* Add Warning header. */
1527 w = pjsip_warning_hdr_create_from_status(
1528 tmp_pool,
1529 pjsip_endpt_name(endpt), status);
1530 PJ_ASSERT_RETURN(w, PJ_ENOMEM);
1531
1532 pj_list_push_back(&res_hdr_list, w);
1533
1534 /* Add Accept header to response */
1535 acc = pjsip_accept_hdr_create(tmp_pool);
1536 PJ_ASSERT_RETURN(acc, PJ_ENOMEM);
1537 acc->values[acc->count++] = pj_str("application/sdp");
1538 pj_list_push_back(&res_hdr_list, acc);
1539
1540 }
1541
1542 goto on_return;
1543 }
1544 }
1545 }
1546
1547 /* Check supported methods, see if peer supports UPDATE.
1548 * We just assume that peer supports standard INVITE, ACK, CANCEL, and BYE
1549 * implicitly by sending this INVITE.
1550 */
1551 if (msg) {
1552 allow = (pjsip_allow_hdr*) pjsip_msg_find_hdr(msg, PJSIP_H_ALLOW,
1553 NULL);
1554 }
1555 if (allow) {
1556 unsigned i;
1557 const pj_str_t STR_UPDATE = { "UPDATE", 6 };
1558
1559 for (i=0; i<allow->count; ++i) {
1560 if (pj_stricmp(&allow->values[i], &STR_UPDATE)==0)
1561 break;
1562 }
1563
1564 if (i != allow->count) {
1565 /* UPDATE is present in Allow */
1566 *options |= PJSIP_INV_SUPPORT_UPDATE;
1567 }
1568
1569 }
1570
1571 /* Check Supported header */
1572 if (msg) {
1573 sup_hdr = (pjsip_supported_hdr*)
1574 pjsip_msg_find_hdr(msg, PJSIP_H_SUPPORTED, NULL);
1575 }
1576 if (sup_hdr) {
1577 unsigned i;
1578 const pj_str_t STR_100REL = { "100rel", 6};
1579 const pj_str_t STR_TIMER = { "timer", 5};
1580 const pj_str_t STR_ICE = { "ice", 3 };
1581 const pj_str_t STR_TRICKLE_ICE = { "trickle-ice", 11 };
1582
1583 for (i=0; i<sup_hdr->count; ++i) {
1584 if (pj_stricmp(&sup_hdr->values[i], &STR_100REL)==0)
1585 rem_option |= PJSIP_INV_SUPPORT_100REL;
1586 else if (pj_stricmp(&sup_hdr->values[i], &STR_TIMER)==0)
1587 rem_option |= PJSIP_INV_SUPPORT_TIMER;
1588 else if (pj_stricmp(&sup_hdr->values[i], &STR_ICE)==0)
1589 rem_option |= PJSIP_INV_SUPPORT_ICE;
1590 else if (pj_stricmp(&sup_hdr->values[i], &STR_TRICKLE_ICE)==0)
1591 rem_option |= PJSIP_INV_SUPPORT_TRICKLE_ICE;
1592 }
1593 }
1594
1595 /* Check Require header */
1596 if (msg) {
1597 req_hdr = (pjsip_require_hdr*)
1598 pjsip_msg_find_hdr(msg, PJSIP_H_REQUIRE, NULL);
1599 }
1600 if (req_hdr) {
1601 unsigned i;
1602 const pj_str_t STR_100REL = { "100rel", 6};
1603 const pj_str_t STR_REPLACES = { "replaces", 8 };
1604 const pj_str_t STR_TIMER = { "timer", 5 };
1605 const pj_str_t STR_ICE = { "ice", 3 };
1606 const pj_str_t STR_TRICKLE_ICE = { "trickle-ice", 11 };
1607 const pj_str_t STR_SIPREC = { "siprec", 6 };
1608 unsigned unsupp_cnt = 0;
1609 pj_str_t unsupp_tags[PJSIP_GENERIC_ARRAY_MAX_COUNT];
1610
1611 for (i=0; i<req_hdr->count; ++i) {
1612 if ((*options & PJSIP_INV_SUPPORT_100REL) &&
1613 pj_stricmp(&req_hdr->values[i], &STR_100REL)==0)
1614 {
1615 rem_option |= PJSIP_INV_REQUIRE_100REL;
1616
1617 } else if (!(*options & PJSIP_INV_SUPPORT_SIPREC) &&
1618 pj_stricmp(&req_hdr->values[i], &STR_SIPREC)==0)
1619 {
1620 unsupp_tags[unsupp_cnt++] = req_hdr->values[i];
1621
1622 } else if ((*options & PJSIP_INV_SUPPORT_TIMER) &&
1623 pj_stricmp(&req_hdr->values[i], &STR_TIMER)==0)
1624 {
1625 rem_option |= PJSIP_INV_REQUIRE_TIMER;
1626
1627 } else if (pj_stricmp(&req_hdr->values[i], &STR_REPLACES)==0) {
1628 pj_bool_t supp;
1629
1630 supp = pjsip_endpt_has_capability(endpt, PJSIP_H_SUPPORTED,
1631 NULL, &STR_REPLACES);
1632 if (!supp)
1633 unsupp_tags[unsupp_cnt++] = req_hdr->values[i];
1634 } else if ((*options & PJSIP_INV_SUPPORT_ICE) &&
1635 pj_stricmp(&req_hdr->values[i], &STR_ICE)==0)
1636 {
1637 rem_option |= PJSIP_INV_REQUIRE_ICE;
1638
1639 } else if ((*options & PJSIP_INV_SUPPORT_TRICKLE_ICE) &&
1640 pj_stricmp(&req_hdr->values[i], &STR_TRICKLE_ICE)==0)
1641 {
1642 rem_option |= PJSIP_INV_REQUIRE_TRICKLE_ICE;
1643
1644 } else if (!pjsip_endpt_has_capability(endpt, PJSIP_H_SUPPORTED,
1645 NULL, &req_hdr->values[i]))
1646 {
1647 /* Unknown/unsupported extension tag! */
1648 unsupp_tags[unsupp_cnt++] = req_hdr->values[i];
1649 }
1650 }
1651
1652 /* Check if there are required tags that we don't support */
1653 if (unsupp_cnt) {
1654
1655 code = PJSIP_SC_BAD_EXTENSION;
1656 status = PJSIP_ERRNO_FROM_SIP_STATUS(code);
1657
1658 if (p_tdata) {
1659 pjsip_unsupported_hdr *unsupp_hdr;
1660 const pjsip_hdr *h;
1661
1662 /* Add Unsupported header. */
1663 unsupp_hdr = pjsip_unsupported_hdr_create(tmp_pool);
1664 PJ_ASSERT_RETURN(unsupp_hdr != NULL, PJ_ENOMEM);
1665
1666 unsupp_hdr->count = unsupp_cnt;
1667 for (i=0; i<unsupp_cnt; ++i)
1668 unsupp_hdr->values[i] = unsupp_tags[i];
1669
1670 pj_list_push_back(&res_hdr_list, unsupp_hdr);
1671
1672 /* Add Supported header. */
1673 h = pjsip_endpt_get_capability(endpt, PJSIP_H_SUPPORTED,
1674 NULL);
1675 pj_assert(h);
1676 if (h) {
1677 sup_hdr = (pjsip_supported_hdr*)
1678 pjsip_hdr_clone(tmp_pool, h);
1679 pj_list_push_back(&res_hdr_list, sup_hdr);
1680 }
1681 }
1682
1683 goto on_return;
1684 }
1685 }
1686
1687 /* Check if there are local requirements that are not supported
1688 * by peer.
1689 */
1690 if ( msg && (((*options & PJSIP_INV_REQUIRE_100REL)!=0 &&
1691 (rem_option & PJSIP_INV_SUPPORT_100REL)==0) ||
1692 ((*options & PJSIP_INV_REQUIRE_TIMER)!=0 &&
1693 (rem_option & PJSIP_INV_SUPPORT_TIMER)==0) ||
1694 ((*options & PJSIP_INV_REQUIRE_TRICKLE_ICE)!=0 &&
1695 (rem_option & PJSIP_INV_SUPPORT_TRICKLE_ICE)==0)))
1696 {
1697 code = PJSIP_SC_EXTENSION_REQUIRED;
1698 status = PJSIP_ERRNO_FROM_SIP_STATUS(code);
1699
1700 if (p_tdata) {
1701 const pjsip_hdr *h;
1702
1703 /* Add Require header. */
1704 req_hdr = pjsip_require_hdr_create(tmp_pool);
1705 PJ_ASSERT_RETURN(req_hdr != NULL, PJ_ENOMEM);
1706
1707 if (*options & PJSIP_INV_REQUIRE_100REL)
1708 req_hdr->values[req_hdr->count++] = pj_str("100rel");
1709 if (*options & PJSIP_INV_REQUIRE_TIMER)
1710 req_hdr->values[req_hdr->count++] = pj_str("timer");
1711 if (*options & PJSIP_INV_REQUIRE_TRICKLE_ICE)
1712 req_hdr->values[req_hdr->count++] = pj_str("trickle-ice");
1713
1714 pj_list_push_back(&res_hdr_list, req_hdr);
1715
1716 /* Add Supported header. */
1717 h = pjsip_endpt_get_capability(endpt, PJSIP_H_SUPPORTED,
1718 NULL);
1719 pj_assert(h);
1720 if (h) {
1721 sup_hdr = (pjsip_supported_hdr*)
1722 pjsip_hdr_clone(tmp_pool, h);
1723 pj_list_push_back(&res_hdr_list, sup_hdr);
1724 }
1725
1726 }
1727
1728 goto on_return;
1729 }
1730
1731 /* If remote Require something that we support, make us Require
1732 * that feature too.
1733 */
1734 if (rem_option & PJSIP_INV_REQUIRE_100REL) {
1735 pj_assert(*options & PJSIP_INV_SUPPORT_100REL);
1736 *options |= PJSIP_INV_REQUIRE_100REL;
1737 }
1738 if (rem_option & PJSIP_INV_REQUIRE_TIMER) {
1739 pj_assert(*options & PJSIP_INV_SUPPORT_TIMER);
1740 *options |= PJSIP_INV_REQUIRE_TIMER;
1741 }
1742 if (rem_option & PJSIP_INV_REQUIRE_TRICKLE_ICE) {
1743 pj_assert(*options & PJSIP_INV_SUPPORT_TRICKLE_ICE);
1744 *options |= PJSIP_INV_REQUIRE_TRICKLE_ICE;
1745 }
1746
1747 on_return:
1748
1749 /* Create response if necessary */
1750 if (code != 200 && p_tdata) {
1751 pjsip_tx_data *tdata;
1752 const pjsip_hdr *h;
1753
1754 if (!rdata) {
1755 return PJSIP_ERRNO_FROM_SIP_STATUS(code);
1756 }
1757
1758 if (dlg) {
1759 status = pjsip_dlg_create_response(dlg, rdata, code, NULL,
1760 &tdata);
1761 } else {
1762 status = pjsip_endpt_create_response(endpt, rdata, code, NULL,
1763 &tdata);
1764 }
1765
1766 if (status != PJ_SUCCESS)
1767 return status;
1768
1769 /* Add response headers. */
1770 h = res_hdr_list.next;
1771 while (h != &res_hdr_list) {
1772 pjsip_hdr *cloned;
1773
1774 cloned = (pjsip_hdr*) pjsip_hdr_clone(tdata->pool, h);
1775 PJ_ASSERT_RETURN(cloned, PJ_ENOMEM);
1776
1777 pjsip_msg_add_hdr(tdata->msg, cloned);
1778
1779 h = h->next;
1780 }
1781
1782 *p_tdata = tdata;
1783
1784 /* Can not return PJ_SUCCESS when response message is produced.
1785 * Ref: PROTOS test ~#2490
1786 */
1787 if (status == PJ_SUCCESS)
1788 status = PJSIP_ERRNO_FROM_SIP_STATUS(code);
1789
1790 }
1791
1792 return status;
1793 }
1794
1795
1796 /*
1797 * Verify incoming INVITE request.
1798 */
1799 PJ_DEF(pj_status_t) pjsip_inv_verify_request2(pjsip_rx_data *rdata,
1800 unsigned *options,
1801 const pjmedia_sdp_session *r_sdp,
1802 const pjmedia_sdp_session *l_sdp,
1803 pjsip_dialog *dlg,
1804 pjsip_endpoint *endpt,
1805 pjsip_tx_data **p_tdata)
1806 {
1807 return pjsip_inv_verify_request3(rdata, rdata->tp_info.pool,
1808 options, r_sdp, l_sdp, dlg,
1809 endpt, p_tdata);
1810 }
1811
1812
1813 /*
1814 * Verify incoming INVITE request.
1815 */
1816 PJ_DEF(pj_status_t) pjsip_inv_verify_request( pjsip_rx_data *rdata,
1817 unsigned *options,
1818 const pjmedia_sdp_session *l_sdp,
1819 pjsip_dialog *dlg,
1820 pjsip_endpoint *endpt,
1821 pjsip_tx_data **p_tdata)
1822 {
1823 return pjsip_inv_verify_request3(rdata, rdata->tp_info.pool,
1824 options, NULL, l_sdp, dlg,
1825 endpt, p_tdata);
1826 }
1827
1828 /*
1829 * Create UAS invite session.
1830 */
1831 PJ_DEF(pj_status_t) pjsip_inv_create_uas( pjsip_dialog *dlg,
1832 pjsip_rx_data *rdata,
1833 const pjmedia_sdp_session *local_sdp,
1834 unsigned options,
1835 pjsip_inv_session **p_inv)
1836 {
1837 pjsip_inv_session *inv;
1838 struct tsx_inv_data *tsx_inv_data;
1839 pjsip_msg *msg;
1840 pjsip_rdata_sdp_info *sdp_info;
1841 pj_status_t status;
1842
1843 /* Verify arguments. */
1844 PJ_ASSERT_RETURN(dlg && rdata && p_inv, PJ_EINVAL);
1845
1846 /* Dialog MUST have been initialised. */
1847 PJ_ASSERT_RETURN(pjsip_rdata_get_tsx(rdata) != NULL, PJ_EINVALIDOP);
1848
1849 msg = rdata->msg_info.msg;
1850
1851 /* rdata MUST contain INVITE request */
1852 PJ_ASSERT_RETURN(msg->type == PJSIP_REQUEST_MSG &&
1853 msg->line.req.method.id == PJSIP_INVITE_METHOD,
1854 PJ_EINVALIDOP);
1855
1856 /* Lock dialog */
1857 pjsip_dlg_inc_lock(dlg);
1858
1859 /* Normalize options */
1860 if (options & PJSIP_INV_REQUIRE_100REL)
1861 options |= PJSIP_INV_SUPPORT_100REL;
1862 if (options & PJSIP_INV_REQUIRE_TIMER)
1863 options |= PJSIP_INV_SUPPORT_TIMER;
1864
1865 /* Create the session */
1866 inv = PJ_POOL_ZALLOC_T(dlg->pool, pjsip_inv_session);
1867 pj_assert(inv != NULL);
1868
1869 status = pj_atomic_create(dlg->pool, 0, &inv->ref_cnt);
1870 if (status != PJ_SUCCESS) {
1871 pjsip_dlg_dec_lock(dlg);
1872 return status;
1873 }
1874
1875 inv->pool = dlg->pool;
1876 inv->role = PJSIP_ROLE_UAS;
1877 inv->state = PJSIP_INV_STATE_NULL;
1878 inv->dlg = dlg;
1879 inv->options = options;
1880 inv->notify = PJ_TRUE;
1881 inv->cause = (pjsip_status_code) 0;
1882
1883 /* Create flip-flop pool (see ticket #877) */
1884 /* (using inv->obj_name as temporary variable for pool names */
1885 pj_ansi_snprintf(inv->obj_name, PJ_MAX_OBJ_NAME, "inv%p", dlg->pool);
1886 inv->pool_prov = pjsip_endpt_create_pool(dlg->endpt, inv->obj_name,
1887 POOL_INIT_SIZE, POOL_INC_SIZE);
1888 inv->pool_active = pjsip_endpt_create_pool(dlg->endpt, inv->obj_name,
1889 POOL_INIT_SIZE, POOL_INC_SIZE);
1890
1891 /* Object name will use the same dialog pointer. */
1892 pj_ansi_snprintf(inv->obj_name, PJ_MAX_OBJ_NAME, "inv%p", dlg);
1893
1894 /* Process SDP in message body, if present. */
1895 sdp_info = pjsip_rdata_get_sdp_info(rdata);
1896 if (sdp_info->sdp_err) {
1897 pjsip_dlg_dec_lock(dlg);
1898 return sdp_info->sdp_err;
1899 }
1900
1901 /* Create negotiator. */
1902 if (sdp_info->sdp) {
1903 status = pjmedia_sdp_neg_create_w_remote_offer(inv->pool, local_sdp,
1904 sdp_info->sdp,
1905 &inv->neg);
1906
1907 } else if (local_sdp) {
1908 status = pjmedia_sdp_neg_create_w_local_offer(inv->pool,
1909 local_sdp, &inv->neg);
1910 } else {
1911 status = PJ_SUCCESS;
1912 }
1913
1914 if (status != PJ_SUCCESS) {
1915 pjsip_dlg_dec_lock(dlg);
1916 return status;
1917 }
1918
1919 /* Register invite as dialog usage. */
1920 status = pjsip_dlg_add_usage(dlg, &mod_inv.mod, inv);
1921 if (status != PJ_SUCCESS) {
1922 pjsip_dlg_dec_lock(dlg);
1923 return status;
1924 }
1925
1926 /* Increment session in the dialog. */
1927 pjsip_dlg_inc_session(dlg, &mod_inv.mod);
1928
1929 /* Save the invite transaction. */
1930 inv->invite_tsx = pjsip_rdata_get_tsx(rdata);
1931
1932 /* Attach our data to the transaction. */
1933 tsx_inv_data = PJ_POOL_ZALLOC_T(inv->invite_tsx->pool, struct tsx_inv_data);
1934 tsx_inv_data->inv = inv;
1935 tsx_inv_data->has_sdp = (sdp_info->sdp!=NULL);
1936 inv->invite_tsx->mod_data[mod_inv.mod.id] = tsx_inv_data;
1937
1938 /* Create 100rel handler */
1939 if (inv->options & PJSIP_INV_REQUIRE_100REL) {
1940 pjsip_100rel_attach(inv);
1941 }
1942
1943 /* Done */
1944 pjsip_inv_add_ref(inv);
1945 pjsip_dlg_dec_lock(dlg);
1946 *p_inv = inv;
1947
1948 PJ_LOG(5,(inv->obj_name, "UAS invite session created for dialog %s",
1949 dlg->obj_name));
1950
1951 return PJ_SUCCESS;
1952 }
1953
1954 /*
1955 * Forcefully terminate the session.
1956 */
1957 PJ_DEF(pj_status_t) pjsip_inv_terminate( pjsip_inv_session *inv,
1958 int st_code,
1959 pj_bool_t notify)
1960 {
1961 PJ_ASSERT_RETURN(inv, PJ_EINVAL);
1962
1963 /* Lock dialog. */
1964 pjsip_dlg_inc_lock(inv->dlg);
1965
1966 /* Set callback notify flag. */
1967 inv->notify = notify;
1968
1969 /* If there's pending transaction, terminate the transaction.
1970 * This may subsequently set the INVITE session state to
1971 * disconnected.
1972 */
1973 if (inv->invite_tsx &&
1974 inv->invite_tsx->state <= PJSIP_TSX_STATE_COMPLETED)
1975 {
1976 pjsip_tsx_terminate(inv->invite_tsx, st_code);
1977
1978 }
1979
1980 /* Set cause. */
1981 inv_set_cause(inv, st_code, NULL);
1982
1983 /* Forcefully terminate the session if state is not DISCONNECTED */
1984 if (inv->state != PJSIP_INV_STATE_DISCONNECTED) {
1985 pjsip_event usr_event;
1986
1987 PJSIP_EVENT_INIT_USER(usr_event, NULL, NULL, NULL, NULL);
1988 inv_set_state(inv, PJSIP_INV_STATE_DISCONNECTED, &usr_event);
1989 }
1990
1991 /* Done.
1992 * The dec_lock() below will actually destroys the dialog if it
1993 * has no other session.
1994 */
1995 pjsip_dlg_dec_lock(inv->dlg);
1996
1997 return PJ_SUCCESS;
1998 }
1999
2000
2001 /*
2002 * Restart UAC session, possibly because app or us wants to re-send the
2003 * INVITE request due to 401/407 challenge or 3xx response.
2004 */
2005 PJ_DEF(pj_status_t) pjsip_inv_uac_restart(pjsip_inv_session *inv,
2006 pj_bool_t new_offer)
2007 {
2008 PJ_ASSERT_RETURN(inv, PJ_EINVAL);
2009
2010 pjsip_inv_add_ref(inv);
2011
2012 inv->state = PJSIP_INV_STATE_NULL;
2013 inv->invite_tsx = NULL;
2014 if (inv->last_answer) {
2015 pjsip_tx_data_dec_ref(inv->last_answer);
2016 inv->last_answer = NULL;
2017 }
2018
2019 if (new_offer && inv->neg) {
2020 pjmedia_sdp_neg_state neg_state;
2021
2022 neg_state = pjmedia_sdp_neg_get_state(inv->neg);
2023 if (neg_state == PJMEDIA_SDP_NEG_STATE_LOCAL_OFFER) {
2024 pjmedia_sdp_neg_cancel_offer(inv->neg);
2025 }
2026 }
2027
2028 pjsip_inv_dec_ref(inv);
2029
2030 return PJ_SUCCESS;
2031 }
2032
2033
2034 static void *clone_sdp(pj_pool_t *pool, const void *data, unsigned len)
2035 {
2036 PJ_UNUSED_ARG(len);
2037 return pjmedia_sdp_session_clone(pool, (const pjmedia_sdp_session*)data);
2038 }
2039
2040 static int print_sdp(pjsip_msg_body *body, char *buf, pj_size_t len)
2041 {
2042 return pjmedia_sdp_print((const pjmedia_sdp_session*)body->data, buf, len);
2043 }
2044
2045
2046 PJ_DEF(pj_status_t) pjsip_create_sdp_body( pj_pool_t *pool,
2047 pjmedia_sdp_session *sdp,
2048 pjsip_msg_body **p_body)
2049 {
2050 const pj_str_t STR_APPLICATION = { "application", 11};
2051 const pj_str_t STR_SDP = { "sdp", 3 };
2052 pjsip_msg_body *body;
2053
2054 body = PJ_POOL_ZALLOC_T(pool, pjsip_msg_body);
2055 PJ_ASSERT_RETURN(body != NULL, PJ_ENOMEM);
2056
2057 pjsip_media_type_init(&body->content_type, (pj_str_t*)&STR_APPLICATION,
2058 (pj_str_t*)&STR_SDP);
2059 body->data = sdp;
2060 body->len = 0;
2061 body->clone_data = &clone_sdp;
2062 body->print_body = &print_sdp;
2063
2064 *p_body = body;
2065
2066 return PJ_SUCCESS;
2067 }
2068
2069 static pjsip_multipart_part* create_sdp_part(pj_pool_t *pool, pjmedia_sdp_session *sdp)
2070 {
2071 pjsip_multipart_part *sdp_part;
2072 pjsip_media_type media_type;
2073
2074 pjsip_media_type_init2(&media_type, "application", "sdp");
2075
2076 sdp_part = pjsip_multipart_create_part(pool);
2077 PJ_ASSERT_RETURN(sdp_part != NULL, NULL);
2078
2079 sdp_part->body = PJ_POOL_ZALLOC_T(pool, pjsip_msg_body);
2080 PJ_ASSERT_RETURN(sdp_part->body != NULL, NULL);
2081
2082 pjsip_media_type_cp(pool, &sdp_part->body->content_type, &media_type);
2083
2084 sdp_part->body->data = sdp;
2085 sdp_part->body->clone_data = clone_sdp;
2086 sdp_part->body->print_body = print_sdp;
2087
2088 return sdp_part;
2089 }
2090
2091 PJ_DEF(pj_status_t) pjsip_create_multipart_sdp_body(pj_pool_t *pool,
2092 pjmedia_sdp_session *sdp,
2093 pjsip_msg_body **p_body)
2094 {
2095 pjsip_media_type media_type;
2096 pjsip_msg_body *multipart;
2097 pjsip_multipart_part *sdp_part;
2098
2099 pjsip_media_type_init2(&media_type, "multipart", "mixed");
2100 multipart = pjsip_multipart_create(pool, &media_type, NULL);
2101 PJ_ASSERT_RETURN(multipart != NULL, PJ_ENOMEM);
2102
2103 sdp_part = create_sdp_part(pool, sdp);
2104 PJ_ASSERT_RETURN(sdp_part != NULL, PJ_ENOMEM);
2105 pjsip_multipart_add_part(pool, multipart, sdp_part);
2106 *p_body = multipart;
2107
2108 return PJ_SUCCESS;
2109 }
2110
2111 static pjsip_msg_body *create_sdp_body(pj_pool_t *pool,
2112 const pjmedia_sdp_session *c_sdp)
2113 {
2114 pjsip_msg_body *body;
2115 pj_status_t status;
2116
2117 status = pjsip_create_sdp_body(pool,
2118 pjmedia_sdp_session_clone(pool, c_sdp),
2119 &body);
2120
2121 if (status != PJ_SUCCESS)
2122 return NULL;
2123
2124 return body;
2125 }
2126
2127 /* Utility to remove a string value from generic array header */
2128 static void remove_val_from_array_hdr(pjsip_generic_array_hdr *arr_hdr,
2129 const pj_str_t *val)
2130 {
2131 unsigned i;
2132 for (i=0; i<arr_hdr->count; ++i) {
2133 if (pj_stricmp(&arr_hdr->values[i], val)==0) {
2134 pj_array_erase(arr_hdr->values, sizeof(arr_hdr->values[0]),
2135 arr_hdr->count, i);
2136 --arr_hdr->count;
2137 break;
2138 }
2139 }
2140 }
2141
2142
2143 /* Remove disabled extensions, e.g: timer & 100rel, from Allow/Supported
2144 * headers (see ticket #1858).
2145 */
2146 static void cleanup_allow_sup_hdr(unsigned inv_option,
2147 pjsip_tx_data *tdata,
2148 pjsip_allow_hdr *allow_hdr,
2149 pjsip_supported_hdr *sup_hdr)
2150 {
2151 /* If all extensions are enabled, nothing to do */
2152 if ((inv_option & PJSIP_INV_SUPPORT_100REL) &&
2153 (inv_option & PJSIP_INV_SUPPORT_TIMER) &&
2154 (inv_option & PJSIP_INV_SUPPORT_TRICKLE_ICE) &&
2155 (inv_option & PJSIP_INV_SUPPORT_SIPREC))
2156 {
2157 return;
2158 }
2159
2160 if (!allow_hdr && tdata) {
2161 allow_hdr = (pjsip_allow_hdr*) pjsip_msg_find_hdr(tdata->msg,
2162 PJSIP_H_ALLOW,
2163 NULL);
2164 }
2165 if (!sup_hdr && tdata) {
2166 sup_hdr = (pjsip_supported_hdr*) pjsip_msg_find_hdr(tdata->msg,
2167 PJSIP_H_SUPPORTED,
2168 NULL);
2169 }
2170
2171 /* Remove "timer" from Supported header if Session-Timers is
2172 * disabled (https://github.com/pjsip/pjproject/issues/1761)
2173 */
2174 if ((inv_option & PJSIP_INV_SUPPORT_TIMER) == 0 && sup_hdr) {
2175 const pj_str_t STR_TIMER = { "timer", 5 };
2176 remove_val_from_array_hdr(sup_hdr, &STR_TIMER);
2177 }
2178
2179 if ((inv_option & PJSIP_INV_SUPPORT_TRICKLE_ICE) == 0 && sup_hdr) {
2180 const pj_str_t STR_TRICKLE_ICE = { "trickle-ice", 11 };
2181 remove_val_from_array_hdr(sup_hdr, &STR_TRICKLE_ICE);
2182 }
2183
2184 if ((inv_option & PJSIP_INV_SUPPORT_100REL) == 0) {
2185 const pj_str_t STR_PRACK = { "PRACK", 5 };
2186 const pj_str_t STR_100REL = { "100rel", 6 };
2187
2188 if (allow_hdr)
2189 remove_val_from_array_hdr(allow_hdr, &STR_PRACK);
2190 if (sup_hdr)
2191 remove_val_from_array_hdr(sup_hdr, &STR_100REL);
2192 }
2193
2194 if ((inv_option & PJSIP_INV_SUPPORT_SIPREC) == 0 && sup_hdr) {
2195 const pj_str_t STR_SIPREC = { "siprec", 6 };
2196 remove_val_from_array_hdr(sup_hdr, &STR_SIPREC);
2197 }
2198 }
2199
2200 /*
2201 * Create initial INVITE request.
2202 */
2203 PJ_DEF(pj_status_t) pjsip_inv_invite( pjsip_inv_session *inv,
2204 pjsip_tx_data **p_tdata )
2205 {
2206 pjsip_tx_data *tdata;
2207 const pjsip_hdr *hdr;
2208 pjsip_allow_hdr *allow_hdr = NULL;
2209 pjsip_supported_hdr *sup_hdr = NULL;
2210 pj_bool_t has_sdp;
2211 pj_status_t status;
2212
2213 /* Verify arguments. */
2214 PJ_ASSERT_RETURN(inv && p_tdata, PJ_EINVAL);
2215
2216 /* State MUST be NULL or CONFIRMED. */
2217 PJ_ASSERT_RETURN(inv->state == PJSIP_INV_STATE_NULL ||
2218 inv->state == PJSIP_INV_STATE_CONFIRMED,
2219 PJ_EINVALIDOP);
2220
2221 /* Lock dialog. */
2222 pjsip_dlg_inc_lock(inv->dlg);
2223
2224 /* Create the INVITE request. */
2225 status = pjsip_dlg_create_request(inv->dlg, pjsip_get_invite_method(), -1,
2226 &tdata);
2227 if (status != PJ_SUCCESS)
2228 goto on_return;
2229
2230
2231 /* If this is the first INVITE, then copy the headers from inv_hdr.
2232 * These are the headers parsed from the request URI when the
2233 * dialog was created.
2234 */
2235 if (inv->state == PJSIP_INV_STATE_NULL) {
2236 hdr = inv->dlg->inv_hdr.next;
2237
2238 while (hdr != &inv->dlg->inv_hdr) {
2239 pjsip_msg_add_hdr(tdata->msg, (pjsip_hdr*)
2240 pjsip_hdr_shallow_clone(tdata->pool, hdr));
2241 hdr = hdr->next;
2242 }
2243 }
2244
2245 /* See if we have SDP to send. */
2246 if (inv->neg) {
2247 pjmedia_sdp_neg_state neg_state;
2248
2249 neg_state = pjmedia_sdp_neg_get_state(inv->neg);
2250
2251 has_sdp = (neg_state == PJMEDIA_SDP_NEG_STATE_LOCAL_OFFER ||
2252 (neg_state == PJMEDIA_SDP_NEG_STATE_WAIT_NEGO &&
2253 pjmedia_sdp_neg_has_local_answer(inv->neg)));
2254
2255
2256 } else {
2257 has_sdp = PJ_FALSE;
2258 }
2259
2260 /* Add SDP, if any. */
2261 if (has_sdp) {
2262 const pjmedia_sdp_session *offer;
2263
2264 status = pjmedia_sdp_neg_get_neg_local(inv->neg, &offer);
2265 if (status != PJ_SUCCESS) {
2266 pjsip_tx_data_dec_ref(tdata);
2267 goto on_return;
2268 }
2269
2270 tdata->msg->body = create_sdp_body(tdata->pool, offer);
2271 }
2272
2273 /* Add Allow header. */
2274 if (inv->dlg->add_allow) {
2275 hdr = pjsip_endpt_get_capability(inv->dlg->endpt, PJSIP_H_ALLOW, NULL);
2276 if (hdr) {
2277 allow_hdr = (pjsip_allow_hdr*)
2278 pjsip_hdr_shallow_clone(tdata->pool, hdr);
2279 pjsip_msg_add_hdr(tdata->msg, (pjsip_hdr*)allow_hdr);
2280 }
2281 }
2282
2283 /* Add Supported header */
2284 hdr = pjsip_endpt_get_capability(inv->dlg->endpt, PJSIP_H_SUPPORTED, NULL);
2285 if (hdr) {
2286 sup_hdr = (pjsip_supported_hdr*)
2287 pjsip_hdr_shallow_clone(tdata->pool, hdr);
2288 pjsip_msg_add_hdr(tdata->msg, (pjsip_hdr*)sup_hdr);
2289 }
2290
2291 /* Cleanup Allow & Supported headers from disabled extensions */
2292 cleanup_allow_sup_hdr(inv->options, NULL, allow_hdr, sup_hdr);
2293
2294 /* Add Require header. */
2295 if ((inv->options & PJSIP_INV_REQUIRE_100REL) ||
2296 (inv->options & PJSIP_INV_REQUIRE_TIMER) ||
2297 (inv->options & PJSIP_INV_REQUIRE_TRICKLE_ICE))
2298 {
2299 pjsip_require_hdr *hreq;
2300
2301 hreq = pjsip_require_hdr_create(tdata->pool);
2302
2303 if (inv->options & PJSIP_INV_REQUIRE_100REL)
2304 hreq->values[hreq->count++] = pj_str("100rel");
2305 if (inv->options & PJSIP_INV_REQUIRE_TIMER)
2306 hreq->values[hreq->count++] = pj_str("timer");
2307 if (inv->options & PJSIP_INV_REQUIRE_TRICKLE_ICE)
2308 hreq->values[hreq->count++] = pj_str("trickle-ice");
2309
2310 pjsip_msg_add_hdr(tdata->msg, (pjsip_hdr*) hreq);
2311 }
2312
2313 status = pjsip_timer_update_req(inv, tdata);
2314 if (status != PJ_SUCCESS)
2315 goto on_return;
2316
2317 /* Done. */
2318 *p_tdata = tdata;
2319
2320
2321 on_return:
2322 pjsip_dlg_dec_lock(inv->dlg);
2323 return status;
2324 }
2325
2326
2327 /* Util: swap pool */
2328 static void swap_pool(pj_pool_t **p1, pj_pool_t **p2)
2329 {
2330 pj_pool_t *tmp = *p1;
2331 *p1 = *p2;
2332 *p2 = tmp;
2333 }
2334
2335
2336 /*
2337 * Initiate SDP negotiation in the SDP negotiator.
2338 */
2339 static pj_status_t inv_negotiate_sdp( pjsip_inv_session *inv )
2340 {
2341 pj_status_t status;
2342
2343 PJ_ASSERT_RETURN(pjmedia_sdp_neg_get_state(inv->neg) ==
2344 PJMEDIA_SDP_NEG_STATE_WAIT_NEGO,
2345 PJMEDIA_SDPNEG_EINSTATE);
2346
2347 status = pjmedia_sdp_neg_negotiate(inv->pool_prov, inv->neg, 0);
2348
2349 PJ_PERROR(4,(inv->obj_name, status, "SDP negotiation done"));
2350
2351 if (mod_inv.cb.on_media_update && inv->notify)
2352 (*mod_inv.cb.on_media_update)(inv, status);
2353
2354 /* Invite session may have been terminated by the application even
2355 * after a successful SDP negotiation, for example when no audio
2356 * codec is present in the offer (see ticket #1034).
2357 */
2358 if (inv->state != PJSIP_INV_STATE_DISCONNECTED) {
2359
2360 /* Swap the flip-flop pool when SDP negotiation success. */
2361 if (status == PJ_SUCCESS) {
2362 swap_pool(&inv->pool_prov, &inv->pool_active);
2363 }
2364
2365 /* Reset the provisional pool regardless SDP negotiation result. */
2366 pj_pool_reset(inv->pool_prov);
2367
2368 } else {
2369
2370 status = PJSIP_ERRNO_FROM_SIP_STATUS(inv->cause);
2371 }
2372
2373 return status;
2374 }
2375
2376 /*
2377 * Check in incoming message for SDP offer/answer.
2378 */
2379 static pj_status_t inv_check_sdp_in_incoming_msg( pjsip_inv_session *inv,
2380 pjsip_transaction *tsx,
2381 pjsip_rx_data *rdata)
2382 {
2383 struct tsx_inv_data *tsx_inv_data;
2384 pj_status_t status = PJ_SUCCESS;
2385 pjsip_msg *msg;
2386 pjsip_rdata_sdp_info *sdp_info;
2387
2388 /* Check if SDP is present in the message. */
2389
2390 msg = rdata->msg_info.msg;
2391 if (msg->body == NULL) {
2392 /* Message doesn't have body. */
2393 return PJ_SUCCESS;
2394 }
2395
2396 sdp_info = pjsip_rdata_get_sdp_info(rdata);
2397 if (sdp_info->body.ptr == NULL) {
2398 /* Message body is not "application/sdp" */
2399 status = PJMEDIA_SDP_EINSDP;
2400 if (PJSIP_INV_ACCEPT_UNKNOWN_BODY)
2401 return status;
2402 goto on_return;
2403 }
2404
2405 /* Process the SDP body. */
2406 if (sdp_info->sdp_err) {
2407 PJ_PERROR(4,(THIS_FILE, sdp_info->sdp_err,
2408 "Error parsing SDP in %s",
2409 pjsip_rx_data_get_info(rdata)));
2410 status = PJMEDIA_SDP_EINSDP;
2411 goto on_return;
2412 }
2413
2414 /* Get/attach invite session's transaction data */
2415 tsx_inv_data = (struct tsx_inv_data*) tsx->mod_data[mod_inv.mod.id];
2416 if (tsx_inv_data == NULL) {
2417 tsx_inv_data = PJ_POOL_ZALLOC_T(tsx->pool, struct tsx_inv_data);
2418 tsx_inv_data->inv = inv;
2419 tsx_inv_data->has_sdp = (sdp_info->sdp!=NULL);
2420 tsx->mod_data[mod_inv.mod.id] = tsx_inv_data;
2421 }
2422
2423 /* Initialize info that we are following forked media */
2424 inv->following_fork = PJ_FALSE;
2425 inv->updated_sdp_answer = PJ_FALSE;
2426
2427 /* MUST NOT do multiple SDP offer/answer in a single transaction,
2428 * EXCEPT previous nego was in 18x (early media) and any of the following
2429 * condition is met:
2430 * - Non-forking scenario:
2431 * - 'accept_multiple_sdp_answers' is set, and
2432 * - previous early response was not reliable (rfc6337 section 3.1.1).
2433 * - Forking scenario:
2434 * - This response has different To tag than the previous response, and
2435 * - This response is 18x/2xx (early or final). If this is 18x,
2436 * only do multiple SDP nego if 'follow_early_media_fork' is set.
2437 *
2438 * See also tickets #657, #1644, #1764, and #2123 for more info.
2439 */
2440 if (tsx_inv_data->sdp_done) {
2441 pj_str_t res_tag;
2442 int st_code;
2443
2444 res_tag = rdata->msg_info.to->tag;
2445 st_code = rdata->msg_info.msg->line.status.code;
2446
2447 if (tsx->role == PJSIP_ROLE_UAC && tsx_inv_data->done_early &&
2448 (
2449 /* Non-forking scenario */
2450 (
2451 !tsx_inv_data->done_early_rel &&
2452 (st_code/100 == 2 || st_code/10 == 18) &&
2453 pjsip_cfg()->endpt.accept_multiple_sdp_answers &&
2454 !pj_stricmp(&tsx_inv_data->done_tag, &res_tag)
2455 )
2456 ||
2457 /* Forking scenario */
2458 (
2459 (st_code/100 == 2 ||
2460 (st_code/10 == 18 &&
2461 pjsip_cfg()->endpt.follow_early_media_fork)) &&
2462 pj_stricmp(&tsx_inv_data->done_tag, &res_tag)
2463 )
2464 )
2465 )
2466 {
2467 pjsip_sdp_info *tdata_sdp_info;
2468 const pjmedia_sdp_session *reoffer_sdp = NULL;
2469
2470 if (pjmedia_sdp_neg_get_state(inv->neg) !=
2471 PJMEDIA_SDP_NEG_STATE_DONE)
2472 {
2473 PJ_LOG(4,(inv->obj_name, "SDP negotiation in progress, "
2474 "message body in %s response is ignored",
2475 (st_code/10==18? "early" : "final" )));
2476 return PJ_SUCCESS;
2477 }
2478
2479 PJ_LOG(4,(inv->obj_name, "Received %s response "
2480 "after SDP negotiation has been done in early "
2481 "media. Renegotiating SDP..",
2482 (st_code/10==18? "early" : "final" )));
2483
2484 /* Retrieve original SDP offer from INVITE request */
2485 tdata_sdp_info = pjsip_tdata_get_sdp_info(tsx->last_tx);
2486 reoffer_sdp = tdata_sdp_info->sdp;
2487
2488 /* Feed the original offer to negotiator */
2489 status = pjmedia_sdp_neg_modify_local_offer2(inv->pool_prov,
2490 inv->neg,
2491 inv->sdp_neg_flags,
2492 reoffer_sdp);
2493
2494 if (status != PJ_SUCCESS) {
2495 PJ_LOG(1,(inv->obj_name, "Error updating local offer for "
2496 "forked 2xx/18x response (err=%d)", status));
2497 return status;
2498 }
2499
2500 inv->following_fork = !!pj_stricmp(&tsx_inv_data->done_tag,
2501 &res_tag);
2502 inv->updated_sdp_answer = PJ_TRUE;
2503
2504 } else {
2505
2506 if (rdata->msg_info.msg->body) {
2507 PJ_LOG(4,(inv->obj_name, "SDP negotiation done, message "
2508 "body is ignored"));
2509 }
2510 return PJ_SUCCESS;
2511 }
2512 }
2513
2514 pj_assert(sdp_info->sdp != NULL);
2515
2516 /* The SDP can be an offer or answer, depending on negotiator's state */
2517
2518 if (inv->neg == NULL ||
2519 pjmedia_sdp_neg_get_state(inv->neg) == PJMEDIA_SDP_NEG_STATE_DONE)
2520 {
2521
2522 /* This is an offer. */
2523
2524 PJ_LOG(5,(inv->obj_name, "Got SDP offer in %s",
2525 pjsip_rx_data_get_info(rdata)));
2526
2527 if (inv->neg == NULL) {
2528 status=pjmedia_sdp_neg_create_w_remote_offer(inv->pool, NULL,
2529 sdp_info->sdp,
2530 &inv->neg);
2531 } else {
2532 status=pjmedia_sdp_neg_set_remote_offer(inv->pool_prov, inv->neg,
2533 sdp_info->sdp);
2534 }
2535
2536 if (status != PJ_SUCCESS) {
2537 PJ_PERROR(4,(THIS_FILE, status, "Error processing SDP offer in %s",
2538 pjsip_rx_data_get_info(rdata)));
2539 return PJMEDIA_SDP_EINSDP;
2540 }
2541
2542 /* Inform application about remote offer. */
2543 if (mod_inv.cb.on_rx_offer2 && inv->notify) {
2544 struct pjsip_inv_on_rx_offer_cb_param param;
2545
2546 param.offer = sdp_info->sdp;
2547 param.rdata = rdata;
2548 (*mod_inv.cb.on_rx_offer2)(inv, &param);
2549 } else if (mod_inv.cb.on_rx_offer && inv->notify) {
2550 (*mod_inv.cb.on_rx_offer)(inv, sdp_info->sdp);
2551 }
2552
2553 /* application must have supplied an answer at this point. */
2554 if (pjmedia_sdp_neg_get_state(inv->neg) !=
2555 PJMEDIA_SDP_NEG_STATE_WAIT_NEGO)
2556 {
2557 if (mod_inv.cb.on_rx_reinvite && inv->notify &&
2558 msg->type == PJSIP_REQUEST_MSG &&
2559 msg->line.req.method.id == PJSIP_INVITE_METHOD)
2560 {
2561 /* Do not return failure first, allow the application
2562 * to set the answer in the on_rx_reinvite() callback.
2563 */
2564 PJ_LOG(5,(inv->obj_name, "Ignoring on_rx_offer() status "
2565 "because on_rx_reinvite() is implemented"));
2566 return PJ_SUCCESS;
2567 }
2568 return PJMEDIA_SDPNEG_EINSTATE;
2569 }
2570
2571 } else if (pjmedia_sdp_neg_get_state(inv->neg) ==
2572 PJMEDIA_SDP_NEG_STATE_LOCAL_OFFER)
2573 {
2574 int status_code;
2575
2576 /* This is an answer.
2577 * Process and negotiate remote answer.
2578 */
2579
2580 PJ_LOG(5,(inv->obj_name, "Got SDP answer in %s",
2581 pjsip_rx_data_get_info(rdata)));
2582
2583 status = pjmedia_sdp_neg_set_remote_answer(inv->pool_prov, inv->neg,
2584 sdp_info->sdp);
2585
2586 if (status != PJ_SUCCESS) {
2587 PJ_PERROR(4,(THIS_FILE, status, "Error processing SDP answer in %s",
2588 pjsip_rx_data_get_info(rdata)));
2589 status = PJMEDIA_SDP_EINSDP;
2590 goto on_return;
2591 }
2592
2593 /* Negotiate SDP */
2594
2595 inv_negotiate_sdp(inv);
2596
2597 /* Mark this transaction has having SDP offer/answer done, and
2598 * save the reference to the To tag
2599 */
2600
2601 tsx_inv_data->sdp_done = 1;
2602 status_code = rdata->msg_info.msg->line.status.code;
2603 tsx_inv_data->done_early = (status_code/100==1);
2604 tsx_inv_data->done_early_rel = tsx_inv_data->done_early &&
2605 pjsip_100rel_is_reliable(rdata);
2606 if (!inv->sdp_done_early_rel)
2607 inv->sdp_done_early_rel = tsx_inv_data->done_early_rel;
2608 pj_strdup(tsx->pool, &tsx_inv_data->done_tag,
2609 &rdata->msg_info.to->tag);
2610
2611 } else {
2612
2613 PJ_LOG(5,(THIS_FILE, "Ignored SDP in %s: negotiator state is %s",
2614 pjsip_rx_data_get_info(rdata),
2615 pjmedia_sdp_neg_state_str(pjmedia_sdp_neg_get_state(inv->neg))));
2616 }
2617
2618 on_return:
2619 if (status != PJ_SUCCESS && inv->neg &&
2620 pjmedia_sdp_neg_get_state(inv->neg) ==
2621 PJMEDIA_SDP_NEG_STATE_LOCAL_OFFER)
2622 {
2623 if (mod_inv.cb.on_media_update && inv->notify)
2624 (*mod_inv.cb.on_media_update)(inv, status);
2625 }
2626
2627 return status;
2628 }
2629
2630
2631 /*
2632 * Process INVITE answer, for both initial and subsequent re-INVITE
2633 */
2634 static pj_status_t process_answer( pjsip_inv_session *inv,
2635 int st_code,
2636 pjsip_tx_data *tdata,
2637 const pjmedia_sdp_session *local_sdp)
2638 {
2639 pj_status_t status;
2640 const pjmedia_sdp_session *sdp = NULL;
2641
2642 /* If local_sdp is specified, then we MUST NOT have answered the
2643 * offer before.
2644 */
2645 if (local_sdp && (st_code/100==1 || st_code/100==2)) {
2646
2647 if (inv->neg == NULL) {
2648 status = pjmedia_sdp_neg_create_w_local_offer(inv->pool,
2649 local_sdp,
2650 &inv->neg);
2651 } else if (pjmedia_sdp_neg_get_state(inv->neg)==
2652 PJMEDIA_SDP_NEG_STATE_REMOTE_OFFER)
2653 {
2654 status = pjmedia_sdp_neg_set_local_answer(inv->pool_prov, inv->neg,
2655 local_sdp);
2656 } else {
2657
2658 /* Can not specify local SDP at this state. */
2659 pj_assert(0);
2660 status = PJMEDIA_SDPNEG_EINSTATE;
2661 }
2662
2663 if (status != PJ_SUCCESS)
2664 return status;
2665
2666 }
2667
2668
2669 /* If SDP negotiator is ready, start negotiation. */
2670 if (st_code/100==2 || (st_code/10==18 && st_code!=180 && st_code!=181)) {
2671
2672 pjmedia_sdp_neg_state neg_state;
2673
2674 /* Start nego when appropriate. */
2675 neg_state = inv->neg ? pjmedia_sdp_neg_get_state(inv->neg) :
2676 PJMEDIA_SDP_NEG_STATE_NULL;
2677
2678 if (neg_state == PJMEDIA_SDP_NEG_STATE_LOCAL_OFFER) {
2679
2680 status = pjmedia_sdp_neg_get_neg_local(inv->neg, &sdp);
2681
2682 } else if (neg_state == PJMEDIA_SDP_NEG_STATE_WAIT_NEGO &&
2683 pjmedia_sdp_neg_has_local_answer(inv->neg) )
2684 {
2685 struct tsx_inv_data *tsx_inv_data;
2686
2687 /* Get invite session's transaction data */
2688 tsx_inv_data = (struct tsx_inv_data*)
2689 inv->invite_tsx->mod_data[mod_inv.mod.id];
2690
2691 status = inv_negotiate_sdp(inv);
2692 if (status != PJ_SUCCESS)
2693 return status;
2694
2695 /* Mark this transaction has having SDP offer/answer done. */
2696 tsx_inv_data->sdp_done = 1;
2697
2698 status = pjmedia_sdp_neg_get_active_local(inv->neg, &sdp);
2699 }
2700 }
2701
2702 /* Include SDP when it's available for 2xx and 18x (but not 180 and 181)
2703 * response. Subsequent response will include this SDP.
2704 *
2705 * Note note:
2706 * - When offer/answer has been completed in reliable 183, we MUST NOT
2707 * send SDP in 2xx response. So if we don't have SDP to send, clear
2708 * the SDP in the message body ONLY if 100rel is active in this
2709 * session.
2710 */
2711 if (sdp) {
2712 tdata->msg->body = create_sdp_body(tdata->pool, sdp);
2713 } else {
2714 if (inv->options & PJSIP_INV_REQUIRE_100REL) {
2715 tdata->msg->body = NULL;
2716 }
2717 }
2718
2719 /* Cancel SDP negotiation if this is a negative reply to a re-INVITE */
2720 if (st_code >= 300 && inv->neg != NULL &&
2721 inv->state == PJSIP_INV_STATE_CONFIRMED)
2722 {
2723 pjmedia_sdp_neg_state neg_state;
2724 neg_state = pjmedia_sdp_neg_get_state(inv->neg);
2725 if (neg_state == PJMEDIA_SDP_NEG_STATE_REMOTE_OFFER) {
2726 pjmedia_sdp_neg_cancel_offer(inv->neg);
2727 }
2728 }
2729
2730 return PJ_SUCCESS;
2731 }
2732
2733
2734 /*
2735 * Create first response to INVITE
2736 */
2737 PJ_DEF(pj_status_t) pjsip_inv_initial_answer( pjsip_inv_session *inv,
2738 pjsip_rx_data *rdata,
2739 int st_code,
2740 const pj_str_t *st_text,
2741 const pjmedia_sdp_session *sdp,
2742 pjsip_tx_data **p_tdata)
2743 {
2744 pjsip_tx_data *tdata;
2745 pj_status_t status;
2746 pjsip_status_code st_code2;
2747
2748 /* Verify arguments. */
2749 PJ_ASSERT_RETURN(inv && p_tdata, PJ_EINVAL);
2750
2751 /* Must have INVITE transaction. */
2752 PJ_ASSERT_RETURN(inv->invite_tsx, PJ_EBUG);
2753
2754 pj_log_push_indent();
2755
2756 pjsip_dlg_inc_lock(inv->dlg);
2757
2758 /* Create response */
2759 status = pjsip_dlg_create_response(inv->dlg, rdata, st_code, st_text,
2760 &tdata);
2761 if (status != PJ_SUCCESS)
2762 goto on_return;
2763
2764 /* Invoke Session Timers module */
2765 status = pjsip_timer_process_req(inv, rdata, &st_code2);
2766 if (status != PJ_SUCCESS) {
2767 pj_status_t status2;
2768
2769 status2 = pjsip_dlg_modify_response(inv->dlg, tdata, st_code2, NULL);
2770 if (status2 != PJ_SUCCESS) {
2771 pjsip_tx_data_dec_ref(tdata);
2772 goto on_return;
2773 }
2774 status2 = pjsip_timer_update_resp(inv, tdata);
2775 if (status2 == PJ_SUCCESS) {
2776 inv->last_answer = tdata;
2777 *p_tdata = tdata;
2778 } else {
2779 /* To avoid leak, we need to decrement 2 times since
2780 * pjsip_dlg_modify_response() increment tdata ref count.
2781 */
2782 pjsip_tx_data_dec_ref(tdata);
2783 pjsip_tx_data_dec_ref(tdata);
2784 }
2785
2786 goto on_return;
2787 }
2788
2789 /* Process SDP in answer */
2790 status = process_answer(inv, st_code, tdata, sdp);
2791 if (status != PJ_SUCCESS) {
2792 pjsip_tx_data_dec_ref(tdata);
2793 goto on_return;
2794 }
2795
2796 /* Cleanup Allow & Supported headers from disabled extensions */
2797 cleanup_allow_sup_hdr(inv->options, tdata, NULL, NULL);
2798
2799 /* Save this answer */
2800 inv->last_answer = tdata;
2801 pjsip_tx_data_add_ref(inv->last_answer);
2802 PJ_LOG(5,(inv->dlg->obj_name, "Initial answer %s",
2803 pjsip_tx_data_get_info(inv->last_answer)));
2804
2805 /* Invoke Session Timers */
2806 pjsip_timer_update_resp(inv, tdata);
2807
2808 *p_tdata = tdata;
2809
2810 on_return:
2811 pjsip_dlg_dec_lock(inv->dlg);
2812 pj_log_pop_indent();
2813 return status;
2814 }
2815
2816
2817 /*
2818 * Answer INVITE request.
2819 */
2820 PJ_DEF(pj_status_t) pjsip_inv_answer( pjsip_inv_session *inv,
2821 int st_code,
2822 const pj_str_t *st_text,
2823 const pjmedia_sdp_session *local_sdp,
2824 pjsip_tx_data **p_tdata )
2825 {
2826 pjsip_tx_data *last_res;
2827 pjsip_tx_data *old_res;
2828 pj_status_t status;
2829
2830 /* Verify arguments. */
2831 PJ_ASSERT_RETURN(inv && p_tdata, PJ_EINVAL);
2832
2833 /* Must have INVITE transaction. */
2834 PJ_ASSERT_RETURN(inv->invite_tsx, PJ_EBUG);
2835
2836 /* Must have created an answer before */
2837 PJ_ASSERT_RETURN(inv->last_answer, PJ_EINVALIDOP);
2838
2839 pj_log_push_indent();
2840
2841 pjsip_dlg_inc_lock(inv->dlg);
2842
2843 /* Clone last response.
2844 * The tdata (last_answer) is a shared object used by the transaction.
2845 * Modifying a shared object might lead to a deadlock.
2846 * Refer to ticket #2137 for more detail.
2847 */
2848 status = pjsip_tx_data_clone(inv->last_answer, 0, &last_res);
2849 if (status != PJ_SUCCESS)
2850 goto on_return;
2851
2852 /* Put the dialog of the session in tdata's mod_data */
2853 last_res->mod_data[inv->dlg->ua->id] = inv->dlg;
2854
2855 /* Modify last response. */
2856 status = pjsip_dlg_modify_response(inv->dlg, last_res, st_code, st_text);
2857 if (status != PJ_SUCCESS) {
2858 pjsip_tx_data_dec_ref(last_res);
2859 goto on_return;
2860 }
2861
2862 /* For non-2xx final response, strip message body */
2863 if (st_code >= 300) {
2864 last_res->msg->body = NULL;
2865 }
2866
2867 /* Process SDP in answer */
2868 status = process_answer(inv, st_code, last_res, local_sdp);
2869 if (status != PJ_SUCCESS) {
2870 /* To avoid leak, we need to decrement 2 times since
2871 * pjsip_dlg_modify_response() increment tdata ref count.
2872 */
2873 pjsip_tx_data_dec_ref(last_res);
2874 pjsip_tx_data_dec_ref(last_res);
2875 goto on_return;
2876 }
2877
2878 /* Invoke Session Timers */
2879 pjsip_timer_update_resp(inv, last_res);
2880
2881 /* Cleanup Allow & Supported headers from disabled extensions */
2882 cleanup_allow_sup_hdr(inv->options, last_res, NULL, NULL);
2883
2884 /* Update last_answer */
2885 old_res = inv->last_answer;
2886 inv->last_answer = last_res;
2887
2888 /* Release old answer */
2889 pjsip_tx_data_dec_ref(old_res);
2890
2891 *p_tdata = last_res;
2892
2893 on_return:
2894 pjsip_dlg_dec_lock(inv->dlg);
2895 pj_log_pop_indent();
2896 return status;
2897 }
2898
2899
2900 /*
2901 * Set local SDP offer/answer.
2902 */
2903 PJ_DEF(pj_status_t) pjsip_inv_set_local_sdp(pjsip_inv_session *inv,
2904 const pjmedia_sdp_session *sdp )
2905 {
2906 const pjmedia_sdp_session *offer;
2907 pj_status_t status;
2908
2909 PJ_ASSERT_RETURN(inv && sdp, PJ_EINVAL);
2910
2911 pjsip_inv_add_ref(inv);
2912
2913 /* If we have remote SDP offer, set local answer to respond to the offer,
2914 * otherwise we set/modify our local offer (and create an SDP negotiator
2915 * if we don't have one yet).
2916 */
2917 if (inv->neg) {
2918 pjmedia_sdp_neg_state neg_state = pjmedia_sdp_neg_get_state(inv->neg);
2919
2920 if ((neg_state == PJMEDIA_SDP_NEG_STATE_REMOTE_OFFER ||
2921 neg_state == PJMEDIA_SDP_NEG_STATE_WAIT_NEGO) &&
2922 pjmedia_sdp_neg_get_neg_remote(inv->neg, &offer) == PJ_SUCCESS)
2923 {
2924 status = pjsip_inv_set_sdp_answer(inv, sdp);
2925 } else if (neg_state == PJMEDIA_SDP_NEG_STATE_DONE) {
2926 status = pjmedia_sdp_neg_modify_local_offer2(inv->pool, inv->neg,
2927 inv->sdp_neg_flags,
2928 sdp);
2929 } else {
2930 pjsip_inv_dec_ref(inv);
2931 return PJMEDIA_SDPNEG_EINSTATE;
2932 }
2933 } else {
2934 status = pjmedia_sdp_neg_create_w_local_offer(inv->pool,
2935 sdp, &inv->neg);
2936 }
2937
2938 pjsip_inv_dec_ref(inv);
2939
2940 return status;
2941 }
2942
2943
2944 /*
2945 * Set SDP answer.
2946 */
2947 PJ_DEF(pj_status_t) pjsip_inv_set_sdp_answer( pjsip_inv_session *inv,
2948 const pjmedia_sdp_session *sdp )
2949 {
2950 pj_status_t status;
2951
2952 PJ_ASSERT_RETURN(inv && sdp, PJ_EINVAL);
2953
2954 pjsip_dlg_inc_lock(inv->dlg);
2955 status = pjmedia_sdp_neg_set_local_answer( inv->pool_prov, inv->neg, sdp);
2956 pjsip_dlg_dec_lock(inv->dlg);
2957
2958 return status;
2959 }
2960
2961
2962 /*
2963 * End session.
2964 */
2965 PJ_DEF(pj_status_t) pjsip_inv_end_session( pjsip_inv_session *inv,
2966 int st_code,
2967 const pj_str_t *st_text,
2968 pjsip_tx_data **p_tdata )
2969 {
2970 pjsip_tx_data *tdata;
2971 pj_status_t status;
2972
2973 /* Verify arguments. */
2974 PJ_ASSERT_RETURN(inv && p_tdata, PJ_EINVAL);
2975
2976 pj_log_push_indent();
2977 pjsip_inv_add_ref(inv);
2978
2979 /* Set cause code. */
2980 inv_set_cause(inv, st_code, st_text);
2981
2982 /* Create appropriate message. */
2983 switch (inv->state) {
2984 case PJSIP_INV_STATE_NULL:
2985 case PJSIP_INV_STATE_CALLING:
2986 case PJSIP_INV_STATE_EARLY:
2987 case PJSIP_INV_STATE_INCOMING:
2988
2989 if (inv->role == PJSIP_ROLE_UAC) {
2990
2991 /* For UAC when session has not been confirmed, create CANCEL. */
2992
2993 /* MUST have the original UAC INVITE transaction. */
2994 PJ_ASSERT_ON_FAIL(inv->invite_tsx != NULL,
2995 { pjsip_inv_dec_ref(inv); return PJ_EBUG; });
2996
2997 /* But CANCEL should only be called when we have received a
2998 * provisional response. If we haven't received any responses,
2999 * just destroy the transaction.
3000 */
3001 if (inv->invite_tsx->status_code < 100) {
3002
3003 /* Do not stop INVITE retransmission, see ticket #506 */
3004 //pjsip_tsx_stop_retransmit(inv->invite_tsx);
3005 inv->cancelling = PJ_TRUE;
3006 inv->pending_cancel = PJ_TRUE;
3007 *p_tdata = NULL;
3008 PJ_LOG(4, (inv->obj_name, "Delaying CANCEL since no "
3009 "provisional response is received yet"));
3010 pjsip_inv_dec_ref(inv);
3011 pj_log_pop_indent();
3012 return PJ_SUCCESS;
3013 }
3014
3015 /* The CSeq here assumes that the dialog is started with an
3016 * INVITE session. This may not be correct; dialog can be
3017 * started as SUBSCRIBE session.
3018 * So fix this!
3019 */
3020 status = pjsip_endpt_create_cancel(inv->dlg->endpt,
3021 inv->invite_tsx->last_tx,
3022 &tdata);
3023 if (status != PJ_SUCCESS) {
3024 pjsip_inv_dec_ref(inv);
3025 pj_log_pop_indent();
3026 return status;
3027 }
3028
3029 if (st_text && st_text->slen)
3030 add_reason_warning_hdr(tdata, st_code, st_text);
3031
3032 /* Set timeout for the INVITE transaction, in case UAS is not
3033 * able to respond the INVITE with 487 final response. The
3034 * timeout value is 64*T1.
3035 */
3036 pjsip_tsx_set_timeout(inv->invite_tsx, 64 * pjsip_cfg()->tsx.t1);
3037
3038 } else {
3039
3040 /* For UAS, send a final response. */
3041 tdata = inv->invite_tsx->last_tx;
3042 if (tdata == NULL)
3043 tdata = inv->last_answer;
3044
3045 PJ_ASSERT_ON_FAIL(tdata != NULL,
3046 { pjsip_inv_dec_ref(inv);
3047 return PJ_EINVALIDOP; });
3048
3049 //status = pjsip_dlg_modify_response(inv->dlg, tdata, st_code,
3050 // st_text);
3051 status = pjsip_inv_answer(inv, st_code, st_text, NULL, &tdata);
3052 }
3053 break;
3054
3055 case PJSIP_INV_STATE_CONNECTING:
3056 case PJSIP_INV_STATE_CONFIRMED:
3057 /* End Session Timer */
3058 pjsip_timer_end_session(inv);
3059
3060 /* For established dialog, send BYE */
3061 status = pjsip_dlg_create_request(inv->dlg, pjsip_get_bye_method(),
3062 -1, &tdata);
3063 if (status == PJ_SUCCESS && st_text && st_text->slen)
3064 add_reason_warning_hdr(tdata, st_code, st_text);
3065 break;
3066
3067 case PJSIP_INV_STATE_DISCONNECTED:
3068 /* No need to do anything. */
3069 pjsip_inv_dec_ref(inv);
3070 pj_log_pop_indent();
3071 return PJSIP_ESESSIONTERMINATED;
3072
3073 default:
3074 pj_assert(!"Invalid operation!");
3075 pjsip_inv_dec_ref(inv);
3076 pj_log_pop_indent();
3077 return PJ_EINVALIDOP;
3078 }
3079
3080 if (status != PJ_SUCCESS) {
3081 pjsip_inv_dec_ref(inv);
3082 pj_log_pop_indent();
3083 return status;
3084 }
3085
3086
3087 /* Done */
3088
3089 inv->cancelling = PJ_TRUE;
3090 *p_tdata = tdata;
3091
3092 pjsip_inv_dec_ref(inv);
3093 pj_log_pop_indent();
3094 return PJ_SUCCESS;
3095 }
3096
3097 /*
3098 * Cancel re-INVITE transaction.
3099 */
3100 PJ_DEF(pj_status_t) pjsip_inv_cancel_reinvite( pjsip_inv_session *inv,
3101 pjsip_tx_data **p_tdata )
3102 {
3103 pjsip_tx_data *tdata;
3104 pj_status_t status;
3105
3106 /* Verify arguments. */
3107 PJ_ASSERT_RETURN(inv && p_tdata, PJ_EINVAL);
3108
3109 pj_log_push_indent();
3110 pjsip_inv_add_ref(inv);
3111
3112 /* Create appropriate message. */
3113 switch (inv->state) {
3114 case PJSIP_INV_STATE_CONFIRMED:
3115 /* MUST have the original UAC INVITE transaction */
3116 PJ_ASSERT_RETURN(inv->invite_tsx != NULL, PJ_EBUG);
3117
3118 /* CANCEL should only be called when we have received a
3119 * provisional response.
3120 */
3121 if (inv->invite_tsx->status_code < 100) {
3122 inv->cancelling = PJ_TRUE;
3123 inv->pending_cancel = PJ_TRUE;
3124 *p_tdata = NULL;
3125 PJ_LOG(4, (inv->obj_name, "Delaying CANCEL since no "
3126 "provisional response is received yet"));
3127 pjsip_inv_dec_ref(inv);
3128 pj_log_pop_indent();
3129 return PJ_SUCCESS;
3130 }
3131
3132 status = pjsip_endpt_create_cancel(inv->dlg->endpt,
3133 inv->invite_tsx->last_tx,
3134 &tdata);
3135 if (status != PJ_SUCCESS) {
3136 pjsip_inv_dec_ref(inv);
3137 pj_log_pop_indent();
3138 return status;
3139 }
3140 break;
3141
3142 default:
3143 /* We cannot send CANCEL to a re-INVITE if the INVITE session is
3144 * not confirmed.
3145 */
3146 pjsip_inv_dec_ref(inv);
3147 pj_log_pop_indent();
3148 return PJ_EINVALIDOP;
3149 }
3150
3151 pjsip_inv_dec_ref(inv);
3152 pj_log_pop_indent();
3153
3154 *p_tdata = tdata;
3155 return PJ_SUCCESS;
3156 }
3157
3158 /* Following redirection recursion, get next target from the target set and
3159 * notify user.
3160 *
3161 * Returns PJ_FALSE if recursion fails (either because there's no more target
3162 * or user rejects the recursion). If we return PJ_FALSE, caller should
3163 * disconnect the session.
3164 *
3165 * Note:
3166 * the event 'e' argument may be NULL.
3167 */
3168 static pj_bool_t inv_uac_recurse(pjsip_inv_session *inv, int code,
3169 const pj_str_t *reason, pjsip_event *e)
3170 {
3171 pjsip_redirect_op op;
3172 pjsip_target *target;
3173
3174 /* Won't redirect if the callback is not implemented. */
3175 if (mod_inv.cb.on_redirected == NULL)
3176 return PJ_FALSE;
3177
3178 if (reason == NULL)
3179 reason = pjsip_get_status_text(code);
3180
3181 /* Set status of current target */
3182 pjsip_target_assign_status(inv->dlg->target_set.current, inv->dlg->pool,
3183 code, reason);
3184
3185 /* Fetch next target from the target set. We only want to
3186 * process SIP/SIPS URI for now.
3187 */
3188 for (;;) {
3189 target = pjsip_target_set_get_next(&inv->dlg->target_set);
3190 if (target == NULL) {
3191 /* No more target. */
3192 return PJ_FALSE;
3193 }
3194
3195 if (!PJSIP_URI_SCHEME_IS_SIP(target->uri) &&
3196 !PJSIP_URI_SCHEME_IS_SIPS(target->uri))
3197 {
3198 code = PJSIP_SC_UNSUPPORTED_URI_SCHEME;
3199 reason = pjsip_get_status_text(code);
3200
3201 /* Mark this target as unusable and fetch next target. */
3202 pjsip_target_assign_status(target, inv->dlg->pool, code, reason);
3203 } else {
3204 /* Found a target */
3205 break;
3206 }
3207 }
3208
3209 /* We have target in 'target'. Set this target as current target
3210 * and notify callback.
3211 */
3212 pjsip_target_set_set_current(&inv->dlg->target_set, target);
3213
3214 op = (*mod_inv.cb.on_redirected)(inv, target->uri, e);
3215
3216
3217 /* Check what the application wants to do now */
3218 switch (op) {
3219 case PJSIP_REDIRECT_ACCEPT:
3220 case PJSIP_REDIRECT_ACCEPT_REPLACE:
3221 case PJSIP_REDIRECT_STOP:
3222 /* Must increment session counter, that's the convention of the
3223 * pjsip_inv_process_redirect().
3224 */
3225 pjsip_dlg_inc_session(inv->dlg, &mod_inv.mod);
3226
3227 /* Act on the recursion */
3228 pjsip_inv_process_redirect(inv, op, e);
3229 return PJ_TRUE;
3230
3231 case PJSIP_REDIRECT_PENDING:
3232 /* Increment session so that the dialog/session is not destroyed
3233 * while we're waiting for user confirmation.
3234 */
3235 pjsip_dlg_inc_session(inv->dlg, &mod_inv.mod);
3236
3237 /* Also clear the invite_tsx variable, otherwise when this tsx is
3238 * terminated, it will also terminate the session.
3239 */
3240 inv->invite_tsx = NULL;
3241
3242 /* Done. The processing will continue once the application calls
3243 * pjsip_inv_process_redirect().
3244 */
3245 return PJ_TRUE;
3246
3247 case PJSIP_REDIRECT_REJECT:
3248 /* Recursively call this function again to fetch next target, if any.
3249 */
3250 return inv_uac_recurse(inv, PJSIP_SC_REQUEST_TERMINATED, NULL, e);
3251
3252 }
3253
3254 pj_assert(!"Should not reach here");
3255 return PJ_FALSE;
3256 }
3257
3258 PJ_DEF(pj_bool_t) pjsip_inv_uac_try_next_target(pjsip_inv_session *inv)
3259 {
3260 PJ_ASSERT_RETURN(inv, PJ_FALSE);
3261 return inv_uac_recurse(inv, PJSIP_SC_UNAUTHORIZED,
3262 pjsip_get_status_text(PJSIP_SC_UNAUTHORIZED),
3263 NULL);
3264 }
3265
3266
3267 /* Process header parameter in redirection target. */
3268 void pjsip_inv_process_hparam(pjsip_inv_session *sess,
3269 const pj_str_t *hname,
3270 const pj_str_t *hvalue,
3271 pjsip_tx_data *tdata)
3272 {
3273 const pjsip_hdr_e ignored_hdrs[] = {
3274 /* According to RFC 3261 section 19.1.5, we should not honor
3275 * the following headers as they can potentially be malicious
3276 * or misleading.
3277 */
3278 PJSIP_H_ACCEPT, PJSIP_H_ACCEPT_ENCODING_UNIMP,
3279 PJSIP_H_ACCEPT_LANGUAGE_UNIMP, PJSIP_H_ALLOW,
3280 PJSIP_H_CALL_ID, PJSIP_H_CONTACT, PJSIP_H_CSEQ,
3281 PJSIP_H_FROM, PJSIP_H_ORGANIZATION_UNIMP,
3282 PJSIP_H_RECORD_ROUTE, PJSIP_H_ROUTE,
3283 PJSIP_H_SUPPORTED, PJSIP_H_TO,
3284 PJSIP_H_USER_AGENT_UNIMP, PJSIP_H_VIA,
3285 /* We opt to ignore the following headers as
3286 * they will be automatically populated by PJSIP.
3287 */
3288 PJSIP_H_CONTENT_LENGTH, PJSIP_H_CONTENT_TYPE,
3289 /* We will reject these headers as well as they serve
3290 * for proxy authentication purposes and should not
3291 * be overwritten by user.
3292 */
3293 PJSIP_H_PROXY_AUTHENTICATE, PJSIP_H_PROXY_AUTHORIZATION
3294 };
3295 /* According to RFC 3261 section 19.1.5, we should verify
3296 * the accuracy of the header fields.
3297 * Application will need to do its own verification by
3298 * overriding the handler function.
3299 */
3300 const pjsip_hdr_e verify_hdrs[] = {
3301 PJSIP_H_CONTENT_DISPOSITION_UNIMP,
3302 PJSIP_H_CONTENT_ENCODING_UNIMP,
3303 PJSIP_H_CONTENT_LANGUAGE_UNIMP,
3304 PJSIP_H_DATE_UNIMP, PJSIP_H_MIME_VERSION_UNIMP,
3305 PJSIP_H_TIMESTAMP_UNIMP
3306 /* We move this to the ignored headers above since
3307 * PJSIP will automatically populate it with the correct
3308 * information.
3309 */
3310 // PJSIP_H_CONTENT_LENGTH, PJSIP_H_CONTENT_TYPE
3311 };
3312 const pjsip_hdr_e append_hdrs[] = {
3313 /* According to the example in RFC 3261 section 8.1.3.4, for Call-Info,
3314 * we can append the new value to existing ones.
3315 *
3316 * Important note: Be careful when adding a new header
3317 * to this list as we currently assume that it must be
3318 * a generic string header.
3319 */
3320 PJSIP_H_CALL_INFO_UNIMP
3321 };
3322
3323 unsigned i;
3324 pjsip_hdr *hdr;
3325
3326 PJ_UNUSED_ARG(sess);
3327 PJ_UNUSED_ARG(verify_hdrs);
3328
3329 /* Check if we should reject/ignore the header. */
3330 for (i = 0; i < PJ_ARRAY_SIZE(ignored_hdrs); i++) {
3331 if (!pj_stricmp2(hname, pjsip_hdr_names[ignored_hdrs[i]].name)) {
3332 PJ_LOG(4, (THIS_FILE, "Redirection header %.*s ignored",
3333 (int)hname->slen,
3334 hname->ptr));
3335 return;
3336 }
3337 }
3338
3339 hdr = pjsip_msg_find_hdr_by_name(tdata->msg, hname, NULL);
3340
3341 /* Check if the header exists. */
3342 if (hdr) {
3343 /* Yes, now check if we can append the header. */
3344 for (i = 0; i < PJ_ARRAY_SIZE(append_hdrs); i++) {
3345 if (!pj_stricmp2(hname, pjsip_hdr_names[append_hdrs[i]].name)) {
3346 pjsip_generic_string_hdr *shdr;
3347 pj_str_t old_hval;
3348
3349 shdr = (pjsip_generic_string_hdr *)hdr;
3350 old_hval = shdr->hvalue;
3351
3352 /* Allocate the buffer enough to contain the old hvalue, new one,
3353 * and the separator (", ").
3354 */
3355 pj_bzero(&shdr->hvalue, sizeof(pj_str_t));
3356 shdr->hvalue.ptr = (char *) pj_pool_alloc(tdata->pool,
3357 old_hval.slen +
3358 hvalue->slen + 2);
3359 pj_strcat(&shdr->hvalue, &old_hval);
3360 pj_strcat2(&shdr->hvalue, ", ");
3361 pj_strcat(&shdr->hvalue, hvalue);
3362
3363 return;
3364 }
3365 }
3366
3367 /* We can't append to the existing header, so remove it. */
3368 pjsip_msg_find_remove_hdr(tdata->msg, PJSIP_H_OTHER, hdr);
3369 }
3370
3371 /* Add the header */
3372 hdr = (pjsip_hdr *)pjsip_generic_string_hdr_create(tdata->pool, hname,
3373 hvalue);
3374 pjsip_msg_add_hdr(tdata->msg, hdr);
3375 }
3376
3377
3378 /* Process redirection/recursion */
3379 PJ_DEF(pj_status_t) pjsip_inv_process_redirect( pjsip_inv_session *inv,
3380 pjsip_redirect_op op,
3381 pjsip_event *e)
3382 {
3383 const pjsip_status_code cancel_code = PJSIP_SC_REQUEST_TERMINATED;
3384 pjsip_event usr_event;
3385 pj_status_t status = PJ_SUCCESS;
3386
3387 PJ_ASSERT_RETURN(inv && op != PJSIP_REDIRECT_PENDING, PJ_EINVAL);
3388
3389 if (e == NULL) {
3390 PJSIP_EVENT_INIT_USER(usr_event, NULL, NULL, NULL, NULL);
3391 e = &usr_event;
3392 }
3393
3394 pjsip_dlg_inc_lock(inv->dlg);
3395
3396 /* Decrement session. That's the convention here to prevent the dialog
3397 * or session from being destroyed while we're waiting for user
3398 * confirmation.
3399 */
3400 pjsip_dlg_dec_session(inv->dlg, &mod_inv.mod);
3401
3402 /* See what the application wants to do now */
3403 switch (op) {
3404 case PJSIP_REDIRECT_ACCEPT:
3405 case PJSIP_REDIRECT_ACCEPT_REPLACE:
3406 /* User accept the redirection. Reset the session and resend the
3407 * INVITE request.
3408 */
3409 {
3410 pjsip_tx_data *tdata;
3411 pjsip_via_hdr *via;
3412 pjsip_uri *req_uri;
3413
3414 /* Get the original INVITE request. */
3415 tdata = inv->invite_req;
3416 pjsip_tx_data_add_ref(tdata);
3417
3418 /* Restore strict route set.
3419 * See https://github.com/pjsip/pjproject/issues/492
3420 */
3421 pjsip_restore_strict_route_set(tdata);
3422
3423 /* Set target */
3424 tdata->msg->line.req.uri = req_uri = (pjsip_uri*)
3425 pjsip_uri_clone(tdata->pool, inv->dlg->target_set.current->uri);
3426
3427 if (PJSIP_URI_SCHEME_IS_SIP(req_uri) ||
3428 PJSIP_URI_SCHEME_IS_SIPS(req_uri))
3429 {
3430 pjsip_sip_uri *uri;
3431 pjsip_param *param;
3432
3433 uri = (pjsip_sip_uri *)pjsip_uri_get_uri(req_uri);
3434
3435 /* According to RFC 3261 section 19.1.5, the method parameter
3436 * MUST NOT be placed in the Request-URI
3437 */
3438 if (uri->method_param.slen > 0) {
3439 /* If the URI contains a method parameter, its value MUST
3440 * be used as the method of the request.
3441 * We only support if the method is INVITE
3442 */
3443 if (pj_stricmp(&uri->method_param,
3444 &pjsip_get_invite_method()->name))
3445 {
3446 PJ_LOG(3, (THIS_FILE, "Redirection using %.*s "
3447 "method unsupported, terminating session",
3448 (int)uri->method_param.slen,
3449 uri->method_param.ptr));
3450
3451 inv_set_cause(inv, cancel_code,
3452 pjsip_get_status_text(cancel_code));
3453 inv_set_state(inv, PJSIP_INV_STATE_DISCONNECTED, e);
3454
3455 status = PJSIP_EINVALIDMETHOD;
3456 break;
3457 }
3458 pj_bzero(&uri->method_param, sizeof(pj_str_t));
3459 }
3460
3461 /* Process the header parameters */
3462 param = uri->header_param.next;
3463 while (param != &uri->header_param) {
3464 (*pjsip_inv_process_hparam_ptr)(inv, &param->name,
3465 &param->value,
3466 tdata);
3467 param = param->next;
3468 }
3469
3470 /* Remove all header params */
3471 if (!pj_list_empty(&uri->header_param)) {
3472 pj_list_init(&uri->header_param);
3473 }
3474 }
3475
3476 /* Remove branch param in Via header. */
3477 via = (pjsip_via_hdr*)
3478 pjsip_msg_find_hdr(tdata->msg, PJSIP_H_VIA, NULL);
3479 via->branch_param.slen = 0;
3480
3481 /* Process PJSIP_REDIRECT_ACCEPT_REPLACE */
3482 if (op == PJSIP_REDIRECT_ACCEPT_REPLACE) {
3483 pjsip_to_hdr *to;
3484 pjsip_dialog *dlg = inv->dlg;
3485 enum { TMP_LEN = PJSIP_MAX_URL_SIZE };
3486 char tmp[TMP_LEN];
3487 int len;
3488
3489 /* Replace To header */
3490 to = PJSIP_MSG_TO_HDR(tdata->msg);
3491 to->uri = (pjsip_uri*)
3492 pjsip_uri_clone(tdata->pool,
3493 dlg->target_set.current->uri);
3494 to->tag.slen = 0;
3495 pj_list_init(&to->other_param);
3496
3497 /* Re-init dialog remote info */
3498 dlg->remote.info = (pjsip_to_hdr*)
3499 pjsip_hdr_clone(dlg->pool, to);
3500
3501 /* Remove header param from remote info */
3502 if (PJSIP_URI_SCHEME_IS_SIP(dlg->remote.info->uri) ||
3503 PJSIP_URI_SCHEME_IS_SIPS(dlg->remote.info->uri))
3504 {
3505 pjsip_sip_uri *sip_uri = (pjsip_sip_uri *)
3506 pjsip_uri_get_uri(dlg->remote.info->uri);
3507 if (!pj_list_empty(&sip_uri->header_param)) {
3508 /* Remove all header param */
3509 pj_list_init(&sip_uri->header_param);
3510 }
3511 }
3512
3513 /* Print the remote info. */
3514 len = pjsip_uri_print(PJSIP_URI_IN_FROMTO_HDR,
3515 dlg->remote.info->uri, tmp, TMP_LEN);
3516 if (len < 1) {
3517 pj_ansi_strxcpy(tmp, "<-error: uri too long->",
3518 sizeof(tmp));
3519 len = (int)pj_ansi_strlen(tmp);
3520 }
3521 pj_strdup2_with_null(dlg->pool, &dlg->remote.info_str, tmp);
3522
3523 /* Secure? */
3524 dlg->secure = PJSIP_URI_SCHEME_IS_SIPS(to->uri);
3525 }
3526
3527 /* Reset message destination info (see #1248). */
3528 pj_bzero(&tdata->dest_info, sizeof(tdata->dest_info));
3529
3530 /* Must invalidate the message! */
3531 pjsip_tx_data_invalidate_msg(tdata);
3532
3533 /* Reset the session */
3534 pjsip_inv_uac_restart(inv, PJ_FALSE);
3535
3536 /* (re)Send the INVITE request */
3537 status = pjsip_inv_send_msg(inv, tdata);
3538 }
3539 break;
3540
3541 case PJSIP_REDIRECT_STOP:
3542 /* User doesn't want the redirection. Disconnect the session now. */
3543 inv_set_cause(inv, cancel_code, pjsip_get_status_text(cancel_code));
3544 inv_set_state(inv, PJSIP_INV_STATE_DISCONNECTED, e);
3545
3546 /* Caller should expect that the invite session is gone now, so
3547 * we don't need to set status to PJSIP_ESESSIONTERMINATED here.
3548 */
3549 break;
3550
3551 case PJSIP_REDIRECT_REJECT:
3552 /* Current target is rejected. Fetch next target if any. */
3553 if (inv_uac_recurse(inv, cancel_code, NULL, NULL) == PJ_FALSE) {
3554 inv_set_cause(inv, cancel_code,
3555 pjsip_get_status_text(cancel_code));
3556 inv_set_state(inv, PJSIP_INV_STATE_DISCONNECTED, e);
3557
3558 /* Tell caller that the invite session is gone now */
3559 status = PJSIP_ESESSIONTERMINATED;
3560 }
3561 break;
3562
3563
3564 case PJSIP_REDIRECT_PENDING:
3565 pj_assert(!"Should not happen");
3566 break;
3567 }
3568
3569
3570 pjsip_dlg_dec_lock(inv->dlg);
3571
3572 return status;
3573 }
3574
3575
3576 /*
3577 * Create re-INVITE.
3578 */
3579 PJ_DEF(pj_status_t) pjsip_inv_reinvite( pjsip_inv_session *inv,
3580 const pj_str_t *new_contact,
3581 const pjmedia_sdp_session *new_offer,
3582 pjsip_tx_data **p_tdata )
3583 {
3584 pj_status_t status;
3585 pjsip_contact_hdr *contact_hdr = NULL;
3586
3587 /* Check arguments. */
3588 PJ_ASSERT_RETURN(inv && p_tdata, PJ_EINVAL);
3589
3590 /* Must NOT have a pending INVITE transaction */
3591 if (inv->invite_tsx!=NULL)
3592 return PJ_EINVALIDOP;
3593
3594 pj_log_push_indent();
3595
3596 pjsip_dlg_inc_lock(inv->dlg);
3597
3598 if (new_contact) {
3599 pj_str_t tmp;
3600 const pj_str_t STR_CONTACT = { "Contact", 7 };
3601
3602 pj_strdup_with_null(inv->dlg->pool, &tmp, new_contact);
3603 contact_hdr = (pjsip_contact_hdr*)
3604 pjsip_parse_hdr(inv->dlg->pool, &STR_CONTACT,
3605 tmp.ptr, tmp.slen, NULL);
3606 if (!contact_hdr) {
3607 status = PJSIP_EINVALIDURI;
3608 goto on_return;
3609 }
3610 }
3611
3612
3613 if (new_offer) {
3614 if (!inv->neg) {
3615 status = pjmedia_sdp_neg_create_w_local_offer(inv->pool,
3616 new_offer,
3617 &inv->neg);
3618 if (status != PJ_SUCCESS)
3619 goto on_return;
3620
3621 } else switch (pjmedia_sdp_neg_get_state(inv->neg)) {
3622
3623 case PJMEDIA_SDP_NEG_STATE_NULL:
3624 pj_assert(!"Unexpected SDP neg state NULL");
3625 status = PJ_EBUG;
3626 goto on_return;
3627
3628 case PJMEDIA_SDP_NEG_STATE_LOCAL_OFFER:
3629 PJ_LOG(4,(inv->obj_name,
3630 "pjsip_inv_reinvite: already have an offer, new "
3631 "offer is ignored"));
3632 break;
3633
3634 case PJMEDIA_SDP_NEG_STATE_REMOTE_OFFER:
3635 status = pjmedia_sdp_neg_set_local_answer(inv->pool_prov,
3636 inv->neg,
3637 new_offer);
3638 if (status != PJ_SUCCESS)
3639 goto on_return;
3640 break;
3641
3642 case PJMEDIA_SDP_NEG_STATE_WAIT_NEGO:
3643 PJ_LOG(4,(inv->obj_name,
3644 "pjsip_inv_reinvite: SDP in WAIT_NEGO state, new "
3645 "offer is ignored"));
3646 break;
3647
3648 case PJMEDIA_SDP_NEG_STATE_DONE:
3649 status = pjmedia_sdp_neg_modify_local_offer2(
3650 inv->pool_prov, inv->neg,
3651 inv->sdp_neg_flags, new_offer);
3652 if (status != PJ_SUCCESS)
3653 goto on_return;
3654 break;
3655 }
3656 }
3657
3658 if (contact_hdr)
3659 inv->dlg->local.contact = contact_hdr;
3660
3661 status = pjsip_inv_invite(inv, p_tdata);
3662
3663 on_return:
3664 pjsip_dlg_dec_lock(inv->dlg);
3665 pj_log_pop_indent();
3666 return status;
3667 }
3668
3669 /*
3670 * Create UPDATE.
3671 */
3672 PJ_DEF(pj_status_t) pjsip_inv_update ( pjsip_inv_session *inv,
3673 const pj_str_t *new_contact,
3674 const pjmedia_sdp_session *offer,
3675 pjsip_tx_data **p_tdata )
3676 {
3677 pjsip_contact_hdr *contact_hdr = NULL;
3678 pjsip_tx_data *tdata = NULL;
3679 pjmedia_sdp_session *sdp_copy;
3680 const pjsip_hdr *hdr;
3681 pjsip_supported_hdr *sup_hdr = NULL;
3682 pj_status_t status = PJ_SUCCESS;
3683
3684 /* Verify arguments. */
3685 PJ_ASSERT_RETURN(inv && p_tdata, PJ_EINVAL);
3686
3687 /* Dialog must have been established */
3688 PJ_ASSERT_RETURN(inv->dlg->state == PJSIP_DIALOG_STATE_ESTABLISHED,
3689 PJ_EINVALIDOP);
3690
3691 /* Invite session must not have been disconnected */
3692 PJ_ASSERT_RETURN(inv->state < PJSIP_INV_STATE_DISCONNECTED,
3693 PJ_EINVALIDOP);
3694
3695 pj_log_push_indent();
3696
3697 /* Lock dialog. */
3698 pjsip_dlg_inc_lock(inv->dlg);
3699
3700 /* Process offer, if any */
3701 if (offer) {
3702 if (inv->state == PJSIP_INV_STATE_EARLY && !inv->sdp_done_early_rel) {
3703 PJ_LOG(4,(inv->dlg->obj_name,
3704 "RFC 3311 section 5.1 recommends against sending UPDATE"
3705 " without reliable prov response"));
3706 #if PJSIP_INV_UPDATE_EARLY_CHECK_RELIABLE
3707 status = PJ_EINVALIDOP;
3708 goto on_error;
3709 #endif
3710 }
3711
3712 if (pjmedia_sdp_neg_get_state(inv->neg)!=PJMEDIA_SDP_NEG_STATE_DONE) {
3713 PJ_LOG(4,(inv->dlg->obj_name,
3714 "Invalid SDP offer/answer state for UPDATE"));
3715 status = PJ_EINVALIDOP;
3716 goto on_error;
3717 }
3718
3719 /* Notify negotiator about the new offer. This will fix the offer
3720 * with correct SDP origin.
3721 */
3722 status = pjmedia_sdp_neg_modify_local_offer2(inv->pool_prov, inv->neg,
3723 inv->sdp_neg_flags, offer);
3724 if (status != PJ_SUCCESS)
3725 goto on_error;
3726
3727 /* Retrieve the "fixed" offer from negotiator */
3728 pjmedia_sdp_neg_get_neg_local(inv->neg, &offer);
3729 }
3730
3731 /* Update Contact if required */
3732 if (new_contact) {
3733 pj_str_t tmp;
3734 const pj_str_t STR_CONTACT = { "Contact", 7 };
3735
3736 pj_strdup_with_null(inv->dlg->pool, &tmp, new_contact);
3737 contact_hdr = (pjsip_contact_hdr*)
3738 pjsip_parse_hdr(inv->dlg->pool, &STR_CONTACT,
3739 tmp.ptr, tmp.slen, NULL);
3740 if (!contact_hdr) {
3741 status = PJSIP_EINVALIDURI;
3742 goto on_error;
3743 }
3744
3745 inv->dlg->local.contact = contact_hdr;
3746 }
3747
3748 /* Create request */
3749 status = pjsip_dlg_create_request(inv->dlg, &pjsip_update_method,
3750 -1, &tdata);
3751 if (status != PJ_SUCCESS)
3752 goto on_error;
3753
3754 /* Attach SDP body */
3755 if (offer) {
3756 sdp_copy = pjmedia_sdp_session_clone(tdata->pool, offer);
3757 pjsip_create_sdp_body(tdata->pool, sdp_copy, &tdata->msg->body);
3758 }
3759
3760 /* Session Timers spec (RFC 4028) says that Supported header MUST be put
3761 * in refresh requests. So here we'll just put the Supported header in
3762 * all cases regardless of whether session timers is used or not, just
3763 * in case this is a common behavior.
3764 */
3765 hdr = pjsip_endpt_get_capability(inv->dlg->endpt, PJSIP_H_SUPPORTED, NULL);
3766 if (hdr) {
3767 sup_hdr = (pjsip_supported_hdr*)
3768 pjsip_hdr_shallow_clone(tdata->pool, hdr);
3769 pjsip_msg_add_hdr(tdata->msg, (pjsip_hdr*)sup_hdr);
3770 }
3771
3772 status = pjsip_timer_update_req(inv, tdata);
3773 if (status != PJ_SUCCESS)
3774 goto on_error;
3775
3776 /* Cleanup Allow & Supported headers from disabled extensions */
3777 cleanup_allow_sup_hdr(inv->options, NULL, NULL, sup_hdr);
3778
3779 /* Unlock dialog. */
3780 pjsip_dlg_dec_lock(inv->dlg);
3781
3782 *p_tdata = tdata;
3783
3784 pj_log_pop_indent();
3785 return PJ_SUCCESS;
3786
3787 on_error:
3788 if (tdata)
3789 pjsip_tx_data_dec_ref(tdata);
3790
3791 /* Unlock dialog. */
3792 pjsip_dlg_dec_lock(inv->dlg);
3793
3794 pj_log_pop_indent();
3795 return status;
3796 }
3797
3798 /*
3799 * Create an ACK request.
3800 */
3801 PJ_DEF(pj_status_t) pjsip_inv_create_ack(pjsip_inv_session *inv,
3802 int cseq,
3803 pjsip_tx_data **p_tdata)
3804 {
3805 const pjmedia_sdp_session *sdp = NULL;
3806 pj_status_t status;
3807
3808 PJ_ASSERT_RETURN(inv && p_tdata, PJ_EINVAL);
3809
3810 /* Lock dialog. */
3811 pjsip_dlg_inc_lock(inv->dlg);
3812
3813 /* Destroy last_ack */
3814 if (inv->last_ack) {
3815 pjsip_tx_data_dec_ref(inv->last_ack);
3816 inv->last_ack = NULL;
3817 }
3818
3819 /* Create new ACK request */
3820 status = pjsip_dlg_create_request(inv->dlg, pjsip_get_ack_method(),
3821 cseq, &inv->last_ack);
3822 if (status != PJ_SUCCESS) {
3823 pjsip_dlg_dec_lock(inv->dlg);
3824 return status;
3825 }
3826
3827 /* See if we have pending SDP answer to send */
3828 sdp = inv_has_pending_answer(inv, inv->invite_tsx);
3829 if (sdp) {
3830 inv->last_ack->msg->body = create_sdp_body(inv->last_ack->pool, sdp);
3831 }
3832
3833 /* Keep this for subsequent response retransmission */
3834 inv->last_ack_cseq = cseq;
3835 pjsip_tx_data_add_ref(inv->last_ack);
3836
3837 /* Done */
3838 *p_tdata = inv->last_ack;
3839
3840 /* Unlock dialog. */
3841 pjsip_dlg_dec_lock(inv->dlg);
3842
3843 return PJ_SUCCESS;
3844 }
3845
3846 /*
3847 * Send a request or response message.
3848 */
3849 PJ_DEF(pj_status_t) pjsip_inv_send_msg( pjsip_inv_session *inv,
3850 pjsip_tx_data *tdata)
3851 {
3852 pj_status_t status;
3853
3854 /* Verify arguments. */
3855 PJ_ASSERT_RETURN(inv && tdata, PJ_EINVAL);
3856
3857 pj_log_push_indent();
3858 pjsip_inv_add_ref(inv);
3859
3860 PJ_LOG(5,(inv->obj_name, "Sending %s",
3861 pjsip_tx_data_get_info(tdata)));
3862
3863 if (tdata->msg->type == PJSIP_REQUEST_MSG) {
3864 struct tsx_inv_data *tsx_inv_data;
3865
3866 pjsip_dlg_inc_lock(inv->dlg);
3867
3868 /* Check again that we didn't receive incoming re-INVITE */
3869 if (tdata->msg->line.req.method.id==PJSIP_INVITE_METHOD &&
3870 inv->invite_tsx)
3871 {
3872 pjsip_tx_data_dec_ref(tdata);
3873 pjsip_dlg_dec_lock(inv->dlg);
3874 status = PJ_EINVALIDOP;
3875 goto on_error;
3876 }
3877
3878 /* Don't send BYE before ACK is received
3879 * https://github.com/pjsip/pjproject/issues/1712
3880 */
3881 if (tdata->msg->line.req.method.id == PJSIP_BYE_METHOD &&
3882 inv->role == PJSIP_ROLE_UAS &&
3883 inv->state == PJSIP_INV_STATE_CONNECTING &&
3884 inv->cause != PJSIP_SC_REQUEST_TIMEOUT &&
3885 inv->cause != PJSIP_SC_TSX_TRANSPORT_ERROR)
3886 {
3887 if (inv->pending_bye)
3888 pjsip_tx_data_dec_ref(inv->pending_bye);
3889
3890 inv->pending_bye = tdata;
3891 PJ_LOG(4, (inv->obj_name, "Delaying BYE request until "
3892 "ACK is received"));
3893 pjsip_dlg_dec_lock(inv->dlg);
3894 goto on_return;
3895 }
3896
3897 /* Associate our data in outgoing invite transaction */
3898 tsx_inv_data = PJ_POOL_ZALLOC_T(inv->pool, struct tsx_inv_data);
3899 tsx_inv_data->inv = inv;
3900 tsx_inv_data->has_sdp = tx_data_has_sdp(tdata);
3901
3902 pjsip_dlg_dec_lock(inv->dlg);
3903
3904 status = pjsip_dlg_send_request(inv->dlg, tdata, mod_inv.mod.id,
3905 tsx_inv_data);
3906 if (status != PJ_SUCCESS) {
3907 goto on_error;
3908 }
3909
3910 /* Check if this is delayed manual ACK (see #416) */
3911 if (mod_inv.cb.on_send_ack &&
3912 tdata->msg->line.req.method.id == PJSIP_ACK_METHOD &&
3913 tdata == inv->last_ack)
3914 {
3915 pjsip_dlg_inc_lock(inv->dlg);
3916
3917 /* Set state to CONFIRMED (if we're not in CONFIRMED yet).
3918 * But don't set it to CONFIRMED if we're already DISCONNECTED
3919 * (this may have been a late 200/OK response.
3920 */
3921 if (inv->state < PJSIP_INV_STATE_CONFIRMED) {
3922 pjsip_event ack_e;
3923 PJSIP_EVENT_INIT_TX_MSG(ack_e, inv->last_ack);
3924 inv_set_state(inv, PJSIP_INV_STATE_CONFIRMED, &ack_e);
3925 } else if (inv->state == PJSIP_INV_STATE_DISCONNECTED) {
3926 /* Avoid possible leaked tdata when invite session is
3927 * already destroyed.
3928 * https://github.com/pjsip/pjproject/pull/2432
3929 */
3930 pjsip_tx_data_dec_ref(inv->last_ack);
3931 inv->last_ack = NULL;
3932 }
3933
3934 pjsip_dlg_dec_lock(inv->dlg);
3935 }
3936
3937 } else {
3938 pjsip_cseq_hdr *cseq;
3939
3940 /* Can only do this to send response to original INVITE
3941 * request.
3942 */
3943 cseq = (pjsip_cseq_hdr*)pjsip_msg_find_hdr(tdata->msg, PJSIP_H_CSEQ, NULL);
3944 PJ_ASSERT_ON_FAIL(cseq != NULL
3945 && (inv->invite_tsx && cseq->cseq == inv->invite_tsx->cseq),
3946 { pjsip_tx_data_dec_ref(tdata);
3947 pjsip_inv_dec_ref(inv);
3948 return PJ_EINVALIDOP; });
3949
3950 if (inv->options & PJSIP_INV_REQUIRE_100REL) {
3951 status = pjsip_100rel_tx_response(inv, tdata);
3952 } else
3953 {
3954 status = pjsip_dlg_send_response(inv->dlg, inv->invite_tsx, tdata);
3955 }
3956
3957 if (status != PJ_SUCCESS) {
3958 goto on_error;
3959 }
3960 }
3961
3962 /* Done */
3963 on_return:
3964 pjsip_inv_dec_ref(inv);
3965 pj_log_pop_indent();
3966 return PJ_SUCCESS;
3967
3968 on_error:
3969 pjsip_inv_dec_ref(inv);
3970 pj_log_pop_indent();
3971 return status;
3972 }
3973
3974
3975 /*
3976 * Respond to incoming CANCEL request.
3977 */
3978 static void inv_respond_incoming_cancel(pjsip_inv_session *inv,
3979 pjsip_transaction *cancel_tsx,
3980 pjsip_event *e)
3981 {
3982 pjsip_tx_data *tdata;
3983 pjsip_transaction *invite_tsx;
3984 pjsip_rx_data *rdata;
3985 pj_str_t key;
3986 pj_status_t status;
3987
3988 pj_assert(e->body.tsx_state.type == PJSIP_EVENT_RX_MSG);
3989 rdata = e->body.tsx_state.src.rdata;
3990
3991 /* https://github.com/pjsip/pjproject/issues/1651
3992 * Special treatment for CANCEL. Since here we're responding to CANCEL
3993 * automatically (including 487 to INVITE), application will see the
3994 * 200/OK response to CANCEL first in the callback, and then 487 to
3995 * INVITE, before the CANCEL request itself. And worse, pjsua application
3996 * may not see the CANCEL request at all because by the time the CANCEL
3997 * request is reported, call has been disconnected and further events
3998 * from the INVITE session has been suppressed.
3999 *
4000 * Update: we have called this in mod_inv_on_tsx_state()
4001 */
4002 // if (mod_inv.cb.on_tsx_state_changed && inv->notify)
4003 // (*mod_inv.cb.on_tsx_state_changed)(inv, cancel_tsx, e);
4004
4005 /* See if we have matching INVITE server transaction: */
4006
4007 pjsip_tsx_create_key(rdata->tp_info.pool, &key, PJSIP_ROLE_UAS,
4008 pjsip_get_invite_method(), rdata);
4009 invite_tsx = pjsip_tsx_layer_find_tsx2(&key, PJ_TRUE);
4010
4011 if (invite_tsx == NULL) {
4012
4013 /* Invite transaction not found!
4014 * Respond CANCEL with 481 (RFC 3261 Section 9.2 page 55)
4015 */
4016 status = pjsip_dlg_create_response( inv->dlg, rdata, 481, NULL,
4017 &tdata);
4018
4019 } else {
4020 inv->cancelling = PJ_TRUE;
4021
4022 /* Always answer CANCEL will 200 (OK) regardless of
4023 * the state of the INVITE transaction.
4024 */
4025 status = pjsip_dlg_create_response( inv->dlg, rdata, 200, NULL,
4026 &tdata);
4027 }
4028
4029 /* See if we have created the response successfully. */
4030 if (status != PJ_SUCCESS) return;
4031
4032 /* Send the CANCEL response */
4033 status = pjsip_dlg_send_response(inv->dlg, cancel_tsx, tdata);
4034 if (status != PJ_SUCCESS) return;
4035
4036
4037 /* See if we need to terminate the UAS INVITE transaction
4038 * with 487 (Request Terminated) response.
4039 */
4040 if (invite_tsx && invite_tsx->status_code < 200) {
4041
4042 pj_assert(invite_tsx->last_tx != NULL);
4043
4044 tdata = invite_tsx->last_tx;
4045
4046 status = pjsip_dlg_modify_response(inv->dlg, tdata, 487, NULL);
4047 if (status == PJ_SUCCESS) {
4048 /* Remove the message body */
4049 tdata->msg->body = NULL;
4050 if (inv->options & PJSIP_INV_REQUIRE_100REL) {
4051 status = pjsip_100rel_tx_response(inv, tdata);
4052 } else {
4053 status = pjsip_dlg_send_response(inv->dlg, invite_tsx,
4054 tdata);
4055 }
4056 }
4057 }
4058
4059 if (invite_tsx)
4060 pj_grp_lock_dec_ref(invite_tsx->grp_lock);
4061 }
4062
4063
4064 /*
4065 * Respond to incoming BYE request.
4066 */
4067 static void inv_respond_incoming_bye( pjsip_inv_session *inv,
4068 pjsip_transaction *bye_tsx,
4069 pjsip_rx_data *rdata,
4070 pjsip_event *e )
4071 {
4072 pj_status_t status;
4073 pjsip_tx_data *tdata;
4074
4075 /* Respond BYE with 200: */
4076
4077 status = pjsip_dlg_create_response(inv->dlg, rdata, 200, NULL, &tdata);
4078 if (status != PJ_SUCCESS) return;
4079
4080 status = pjsip_dlg_send_response(inv->dlg, bye_tsx, tdata);
4081 if (status != PJ_SUCCESS) return;
4082
4083 /* End Session Timer */
4084 pjsip_timer_end_session(inv);
4085
4086 /* Terminate session: */
4087
4088 if (inv->state != PJSIP_INV_STATE_DISCONNECTED) {
4089 inv_set_cause(inv, PJSIP_SC_OK, NULL);
4090 inv_set_state(inv, PJSIP_INV_STATE_DISCONNECTED, e);
4091 }
4092 }
4093
4094 /*
4095 * Respond to BYE request.
4096 */
4097 static void inv_handle_bye_response( pjsip_inv_session *inv,
4098 pjsip_transaction *tsx,
4099 pjsip_rx_data *rdata,
4100 pjsip_event *e )
4101 {
4102 pj_status_t status;
4103
4104 if (e->body.tsx_state.type != PJSIP_EVENT_RX_MSG) {
4105 inv_set_cause(inv, PJSIP_SC_OK, NULL);
4106 inv_set_state(inv, PJSIP_INV_STATE_DISCONNECTED, e);
4107 return;
4108 }
4109
4110 /* Handle 401/407 challenge. */
4111 if (tsx->status_code == 401 || tsx->status_code == 407) {
4112
4113 pjsip_tx_data *tdata;
4114 pjsip_auth_clt_async_on_chal_param chal_param = { 0 };
4115 struct tsx_inv_data *tsx_inv_data;
4116
4117 /* Dialog grp_lock is held by the caller (pjsip_dlg_on_tsx_state).
4118 * We do NOT release it here because:
4119 * (a) grp_lock is recursive, so sync send from callback works, and
4120 * (b) releasing a lock held by a parent caller risks state changes
4121 * from another thread processing a different event on this dialog.
4122 */
4123 pj_bzero(&chal_param, sizeof(chal_param));
4124 {
4125 pjsip_auth_clt_async_impl_token *token;
4126 token = PJ_POOL_ZALLOC_T(tsx->pool,
4127 pjsip_auth_clt_async_impl_token);
4128 token->user_data = inv;
4129 token->send_impl = &inv_async_auth_send_impl;
4130 token->abandon_impl = &inv_bye_auth_abandon_impl;
4131 token->grp_lock = tsx->grp_lock;
4132 pj_grp_lock_add_ref(tsx->grp_lock);
4133
4134 chal_param.rdata = rdata;
4135 chal_param.tdata = tsx->last_tx;
4136 status = pjsip_auth_clt_async_impl_on_challenge(
4137 &inv->dlg->auth_sess,
4138 token, &chal_param);
4139 if (status != PJ_SUCCESS)
4140 pj_grp_lock_dec_ref(tsx->grp_lock);
4141 }
4142 if (status == PJ_SUCCESS) {
4143 tsx_inv_data =
4144 (struct tsx_inv_data*)tsx->mod_data[mod_inv.mod.id];
4145 if (tsx_inv_data)
4146 tsx_inv_data->retrying = PJ_TRUE;
4147 } else {
4148 status = pjsip_auth_clt_reinit_req(&inv->dlg->auth_sess,
4149 rdata, tsx->last_tx,
4150 &tdata);
4151 if (status != PJ_SUCCESS) {
4152
4153 /* Does not have proper credentials.
4154 * End the session anyway.
4155 */
4156 inv_set_cause(inv, PJSIP_SC_OK, NULL);
4157 inv_set_state(inv, PJSIP_INV_STATE_DISCONNECTED, e);
4158
4159 } else {
4160 tsx_inv_data =
4161 (struct tsx_inv_data*)tsx->mod_data[mod_inv.mod.id];
4162 if (tsx_inv_data)
4163 tsx_inv_data->retrying = PJ_TRUE;
4164
4165 /* Re-send BYE. */
4166 status = pjsip_inv_send_msg(inv, tdata);
4167 }
4168 }
4169
4170 } else {
4171
4172 /* End the session. */
4173 inv_set_cause(inv, PJSIP_SC_OK, NULL);
4174 inv_set_state(inv, PJSIP_INV_STATE_DISCONNECTED, e);
4175 }
4176
4177 }
4178
4179 /*
4180 * Respond to incoming UPDATE request.
4181 */
4182 static void inv_respond_incoming_update(pjsip_inv_session *inv,
4183 pjsip_event *e)
4184 {
4185 pjmedia_sdp_neg_state neg_state;
4186 pj_status_t status;
4187 pjsip_tx_data *tdata = NULL;
4188 pjsip_rx_data *rdata;
4189 pjsip_status_code st_code;
4190
4191 pj_assert(e->type == PJSIP_EVENT_TSX_STATE &&
4192 e->body.tsx_state.type == PJSIP_EVENT_RX_MSG);
4193 rdata = e->body.tsx_state.src.rdata;
4194
4195 /* Check routing URI scheme for secure dialog */
4196 if (!inv_check_secure_dlg(inv, e))
4197 return;
4198
4199 /* Invoke Session Timers module */
4200 status = pjsip_timer_process_req(inv, rdata, &st_code);
4201 if (status != PJ_SUCCESS) {
4202 status = pjsip_dlg_create_response(inv->dlg, rdata, st_code,
4203 NULL, &tdata);
4204 goto on_return;
4205 }
4206
4207 neg_state = pjmedia_sdp_neg_get_state(inv->neg);
4208
4209 /* If UPDATE doesn't contain SDP, just respond with 200/OK.
4210 * This is a valid scenario according to session-timer draft.
4211 */
4212 if (rdata->msg_info.msg->body == NULL) {
4213
4214 status = pjsip_dlg_create_response(inv->dlg, rdata,
4215 200, NULL, &tdata);
4216 }
4217 /* Send 491 if we receive UPDATE while we're waiting for an answer */
4218 else if (neg_state == PJMEDIA_SDP_NEG_STATE_LOCAL_OFFER) {
4219 status = pjsip_dlg_create_response(inv->dlg, rdata,
4220 PJSIP_SC_REQUEST_PENDING, NULL,
4221 &tdata);
4222 }
4223 /* Send 500 with Retry-After header set randomly between 0 and 10 if we
4224 * receive UPDATE while we haven't sent answer.
4225 */
4226 else if (neg_state == PJMEDIA_SDP_NEG_STATE_REMOTE_OFFER ||
4227 neg_state == PJMEDIA_SDP_NEG_STATE_WAIT_NEGO)
4228 {
4229 pjsip_retry_after_hdr *ra_hdr;
4230 int val;
4231
4232 status = pjsip_dlg_create_response(inv->dlg, rdata,
4233 PJSIP_SC_INTERNAL_SERVER_ERROR,
4234 NULL, &tdata);
4235
4236 if (status == PJ_SUCCESS) {
4237 val = (pj_rand() % 10);
4238 ra_hdr = pjsip_retry_after_hdr_create(tdata->pool, val);
4239 pjsip_msg_add_hdr(tdata->msg, (pjsip_hdr*)ra_hdr);
4240 }
4241
4242 } else {
4243 /* We receive new offer from remote */
4244 inv_check_sdp_in_incoming_msg(inv, pjsip_rdata_get_tsx(rdata), rdata);
4245
4246 /* Application MUST have supplied the answer by now.
4247 * If so, negotiate the SDP.
4248 */
4249 neg_state = pjmedia_sdp_neg_get_state(inv->neg);
4250 if (neg_state != PJMEDIA_SDP_NEG_STATE_WAIT_NEGO ||
4251 (status=inv_negotiate_sdp(inv)) != PJ_SUCCESS)
4252 {
4253 /* Negotiation has failed. If negotiator is still
4254 * stuck at non-DONE state, cancel any ongoing offer.
4255 */
4256 neg_state = pjmedia_sdp_neg_get_state(inv->neg);
4257 if (neg_state != PJMEDIA_SDP_NEG_STATE_DONE) {
4258 pjmedia_sdp_neg_cancel_offer(inv->neg);
4259 }
4260
4261 status = pjsip_dlg_create_response(inv->dlg, rdata,
4262 PJSIP_SC_NOT_ACCEPTABLE_HERE,
4263 NULL, &tdata);
4264 } else {
4265 /* New media has been negotiated successfully, send 200/OK */
4266 status = pjsip_dlg_create_response(inv->dlg, rdata,
4267 PJSIP_SC_OK, NULL, &tdata);
4268 if (status == PJ_SUCCESS) {
4269 const pjmedia_sdp_session *sdp;
4270 status = pjmedia_sdp_neg_get_active_local(inv->neg, &sdp);
4271 if (status == PJ_SUCCESS)
4272 tdata->msg->body = create_sdp_body(tdata->pool, sdp);
4273 }
4274 }
4275 }
4276
4277 on_return:
4278 /* Invoke Session Timers */
4279 if (status == PJ_SUCCESS)
4280 status = pjsip_timer_update_resp(inv, tdata);
4281
4282 if (status != PJ_SUCCESS) {
4283 if (tdata != NULL) {
4284 pjsip_tx_data_dec_ref(tdata);
4285 tdata = NULL;
4286 }
4287 return;
4288 }
4289
4290 pjsip_dlg_send_response(inv->dlg, pjsip_rdata_get_tsx(rdata), tdata);
4291 }
4292
4293
4294 /*
4295 * Handle incoming response to UAC UPDATE request.
4296 */
4297 static pj_bool_t inv_handle_update_response( pjsip_inv_session *inv,
4298 pjsip_event *e)
4299 {
4300 pjsip_transaction *tsx = e->body.tsx_state.tsx;
4301 struct tsx_inv_data *tsx_inv_data;
4302 pj_bool_t handled = PJ_FALSE;
4303 pj_status_t status = -1;
4304
4305 tsx_inv_data = (struct tsx_inv_data*)tsx->mod_data[mod_inv.mod.id];
4306 pj_assert(tsx_inv_data);
4307
4308 /* Handle 481/408 response. */
4309 if (inv->state != PJSIP_INV_STATE_DISCONNECTED &&
4310 tsx->state == PJSIP_TSX_STATE_COMPLETED &&
4311 ((tsx->status_code == PJSIP_SC_CALL_TSX_DOES_NOT_EXIST) ||
4312 (tsx->status_code == PJSIP_SC_REQUEST_TIMEOUT &&
4313 !pjsip_cfg()->endpt.keep_inv_after_tsx_timeout)))
4314 {
4315 pjsip_tx_data *bye = NULL;
4316
4317 /* End session */
4318 status = pjsip_inv_end_session(inv, tsx->status_code,
4319 &tsx->status_text, &bye);
4320 if (status == PJ_SUCCESS && bye) {
4321 status = pjsip_inv_send_msg(inv, bye);
4322 }
4323
4324 handled = PJ_TRUE;
4325 }
4326
4327 /* Handle 401/407 challenge. */
4328 else if (tsx->state == PJSIP_TSX_STATE_COMPLETED &&
4329 (tsx->status_code == 401 || tsx->status_code == 407))
4330 {
4331 pjsip_tx_data *tdata;
4332 pjsip_auth_clt_async_on_chal_param chal_param = { 0 };
4333
4334 /* UPDATE auth: abandon_impl is NULL because abandoning a
4335 * mid-dialog UPDATE does not terminate the session.
4336 * Dialog grp_lock is held by the caller; see BYE handler comment.
4337 */
4338 pj_bzero(&chal_param, sizeof(chal_param));
4339 {
4340 pjsip_auth_clt_async_impl_token *token;
4341 token = PJ_POOL_ZALLOC_T(tsx->pool,
4342 pjsip_auth_clt_async_impl_token);
4343 token->user_data = inv;
4344 token->send_impl = &inv_async_auth_send_impl;
4345 token->abandon_impl = NULL;
4346 token->grp_lock = tsx->grp_lock;
4347 pj_grp_lock_add_ref(tsx->grp_lock);
4348
4349 chal_param.rdata = e->body.tsx_state.src.rdata;
4350 chal_param.tdata = tsx->last_tx;
4351 status = pjsip_auth_clt_async_impl_on_challenge(
4352 &inv->dlg->auth_sess,
4353 token, &chal_param);
4354 if (status != PJ_SUCCESS)
4355 pj_grp_lock_dec_ref(tsx->grp_lock);
4356 }
4357 if (status == PJ_SUCCESS) {
4358 if (tsx_inv_data)
4359 tsx_inv_data->retrying = PJ_TRUE;
4360 } else {
4361 status = pjsip_auth_clt_reinit_req(
4362 &inv->dlg->auth_sess,
4363 e->body.tsx_state.src.rdata,
4364 tsx->last_tx, &tdata);
4365 if (status == PJ_SUCCESS) {
4366 if (tsx_inv_data)
4367 tsx_inv_data->retrying = PJ_TRUE;
4368
4369 /* Re-send request. */
4370 status = pjsip_inv_send_msg(inv, tdata);
4371 }
4372 /* Somehow failed. Probably it's not a good idea to
4373 * terminate the session since this is just a request
4374 * within dialog. And even if we terminate we should
4375 * send BYE.
4376 */
4377 }
4378
4379 handled = PJ_TRUE;
4380 }
4381
4382 /* Process 422 response */
4383 else if (tsx->state == PJSIP_TSX_STATE_COMPLETED &&
4384 tsx->status_code == 422)
4385 {
4386 status = handle_timer_response(inv, e->body.tsx_state.src.rdata,
4387 PJ_FALSE);
4388 handled = PJ_TRUE;
4389 }
4390
4391 /* Process 2xx response */
4392 else if (tsx->state == PJSIP_TSX_STATE_COMPLETED &&
4393 tsx->status_code/100 == 2)
4394 {
4395 pjsip_rx_data *rdata = e->body.tsx_state.src.rdata;
4396
4397 /* Check routing URI scheme for secure dialog */
4398 if (inv_check_secure_dlg(inv, e)) {
4399
4400 status = handle_timer_response(inv, rdata, PJ_FALSE);
4401
4402 if (rdata->msg_info.msg->body) {
4403 /* Only process remote SDP if we have sent local offer */
4404 if (inv->neg && pjmedia_sdp_neg_get_state(inv->neg) ==
4405 PJMEDIA_SDP_NEG_STATE_LOCAL_OFFER)
4406 {
4407 status = inv_check_sdp_in_incoming_msg(inv, tsx, rdata);
4408 } else {
4409 PJ_LOG(5,(THIS_FILE, "Ignored message body in %s as no "
4410 "local offer was sent",
4411 pjsip_rx_data_get_info(rdata)));
4412 }
4413 }
4414 }
4415
4416 handled = PJ_TRUE;
4417 }
4418
4419 /* Process 502/503 error */
4420 else if ((tsx->state == PJSIP_TSX_STATE_TERMINATED) &&
4421 (tsx->status_code == 503 || tsx->status_code == 502))
4422 {
4423 status = pjsip_timer_handle_refresh_error(inv, e);
4424
4425 handled = PJ_TRUE;
4426 }
4427
4428 /* Get/attach invite session's transaction data */
4429 else
4430 {
4431 /* Session-Timer needs to see any error responses, to determine
4432 * whether peer supports UPDATE with empty body.
4433 */
4434 if (tsx->state == PJSIP_TSX_STATE_COMPLETED &&
4435 tsx->role == PJSIP_ROLE_UAC)
4436 {
4437 status = handle_timer_response(inv, e->body.tsx_state.src.rdata,
4438 PJ_FALSE);
4439 handled = PJ_TRUE;
4440 }
4441 }
4442
4443 /* Cancel the negotiation if we don't get successful negotiation by now,
4444 * unless it's authentication challenge and the request is being retried.
4445 */
4446 if (pjmedia_sdp_neg_get_state(inv->neg) ==
4447 PJMEDIA_SDP_NEG_STATE_LOCAL_OFFER &&
4448 tsx_inv_data && tsx_inv_data->sdp_done == PJ_FALSE &&
4449 !tsx_inv_data->retrying && tsx_inv_data->has_sdp)
4450 {
4451 pjmedia_sdp_neg_cancel_offer(inv->neg);
4452
4453 /* Prevent from us cancelling different offer! */
4454 tsx_inv_data->sdp_done = PJ_TRUE;
4455 }
4456
4457 return handled;
4458 }
4459
4460
4461 /*
4462 * Handle incoming reliable response.
4463 */
4464 static void inv_handle_incoming_reliable_response(pjsip_inv_session *inv,
4465 pjsip_rx_data *rdata)
4466 {
4467 pjsip_tx_data *tdata;
4468 const pjmedia_sdp_session *sdp;
4469 pj_status_t status;
4470
4471 /* Create PRACK */
4472 status = pjsip_100rel_create_prack(inv, rdata, &tdata);
4473 if (status != PJ_SUCCESS)
4474 return;
4475
4476 /* See if we need to attach SDP answer on the PRACK request */
4477 sdp = inv_has_pending_answer(inv, pjsip_rdata_get_tsx(rdata));
4478 if (sdp) {
4479 tdata->msg->body = create_sdp_body(tdata->pool, sdp);
4480 }
4481
4482 /* Send PRACK (must be using 100rel module!) */
4483 pjsip_100rel_send_prack(inv, tdata);
4484 }
4485
4486
4487 /*
4488 * Handle incoming PRACK.
4489 */
4490 static void inv_respond_incoming_prack(pjsip_inv_session *inv,
4491 pjsip_rx_data *rdata)
4492 {
4493 pjmedia_sdp_neg_state neg_state;
4494 pj_status_t status, prack_sdp_status;
4495 pjsip_tx_data *tdata = NULL;
4496
4497 /* Run through 100rel module to see if we can accept this
4498 * PRACK request. The 100rel will not send 200/OK to PRACK request.
4499 */
4500 status = pjsip_100rel_on_rx_prack(inv, rdata);
4501
4502 /* Already replied 400 "Unexpected PRACK" */
4503 if (status == PJSIP_ENOTINITIALIZED)
4504 return;
4505
4506 /* Even if something wrong, anyway reply with 200/OK for PRACK
4507 * Keeping the same behaviour, that was in sip_100rel.c
4508 */
4509 if (status != PJ_SUCCESS) {
4510 status = pjsip_dlg_create_response(inv->dlg, rdata, PJSIP_SC_OK, NULL,
4511 &tdata);
4512 goto on_return;
4513 }
4514
4515 /* No SDP in the PRACK request, respond with 200/OK without SDP */
4516 if (rdata->msg_info.msg->body == NULL) {
4517 status = pjsip_dlg_create_response(inv->dlg, rdata, PJSIP_SC_OK, NULL,
4518 &tdata);
4519 goto on_return;
4520 }
4521
4522 neg_state = pjmedia_sdp_neg_get_state(inv->neg);
4523 /* If we're waiting for an answer, respond with 200/OK without SDP */
4524 if (neg_state == PJMEDIA_SDP_NEG_STATE_LOCAL_OFFER) {
4525 status = inv_check_sdp_in_incoming_msg(inv, pjsip_rdata_get_tsx(rdata),
4526 rdata);
4527 /* If SDP negotiation has been successful, also mark the
4528 * SDP negotiation flag in the invite transaction to be
4529 * done too.
4530 */
4531 if (status == PJ_SUCCESS && inv->invite_tsx) {
4532 struct tsx_inv_data *tsx_inv_data;
4533
4534 /* Get/attach invite session's transaction data */
4535 tsx_inv_data = (struct tsx_inv_data*)
4536 inv->invite_tsx->mod_data[mod_inv.mod.id];
4537 if (tsx_inv_data == NULL) {
4538 tsx_inv_data = PJ_POOL_ZALLOC_T(inv->invite_tsx->pool,
4539 struct tsx_inv_data);
4540 tsx_inv_data->inv = inv;
4541 tsx_inv_data->has_sdp = PJ_TRUE;
4542 inv->invite_tsx->mod_data[mod_inv.mod.id] = tsx_inv_data;
4543 }
4544
4545 tsx_inv_data->sdp_done = PJ_TRUE;
4546 }
4547
4548 status = pjsip_dlg_create_response(inv->dlg, rdata, PJSIP_SC_OK,
4549 NULL, &tdata);
4550 if (pjmedia_sdp_neg_get_state(inv->neg) == PJMEDIA_SDP_NEG_STATE_DONE)
4551 {
4552 inv->sdp_done_early_rel = PJ_TRUE;
4553 }
4554 goto on_return;
4555 }
4556
4557 /* We receive new offer from remote */
4558 inv_check_sdp_in_incoming_msg(inv, pjsip_rdata_get_tsx(rdata), rdata);
4559
4560 /* Please check RFC 6337 section 2.3 Rejection of an Offer.
4561 * And RFC 3262 Section 5.
4562 * We MUST answer to PRACK with 200/OK with SDP answer
4563 * if PRACK contains SDP offer.
4564 * So if SDP negotiation failed we terminate INVITE transaction
4565 * with errror 488 "Not Acceptable Here"
4566 */
4567 prack_sdp_status = inv_negotiate_sdp(inv);
4568 inv->sdp_done_early_rel = PJ_TRUE;
4569 status = pjsip_dlg_create_response(inv->dlg, rdata, PJSIP_SC_OK, NULL,
4570 &tdata);
4571 if (status == PJ_SUCCESS) {
4572 const pjmedia_sdp_session *sdp;
4573 status = pjmedia_sdp_neg_get_active_local(inv->neg, &sdp);
4574 if (status == PJ_SUCCESS)
4575 tdata->msg->body = create_sdp_body(tdata->pool, sdp);
4576 pjsip_dlg_send_response(inv->dlg, pjsip_rdata_get_tsx(rdata), tdata);
4577 }
4578 if (prack_sdp_status != PJ_SUCCESS) {
4579 pjsip_tx_data *resp;
4580 pj_str_t st_reason = pj_str("PRACK SDP negotiation failed");
4581 /* SDP negotiation has failed. If negotiator is still
4582 * stuck at non-DONE state, cancel any ongoing offer.
4583 */
4584 neg_state = pjmedia_sdp_neg_get_state(inv->neg);
4585 if (neg_state != PJMEDIA_SDP_NEG_STATE_DONE)
4586 pjmedia_sdp_neg_cancel_offer(inv->neg);
4587
4588 /* Terminate the call as we have no way
4589 * to reply with error code to PRACK
4590 */
4591 PJ_LOG(4,(THIS_FILE, "Call terminated. PRACK SDP not acceptable"));
4592 status = pjsip_inv_end_session(inv, PJSIP_SC_NOT_ACCEPTABLE_HERE,
4593 &st_reason, &resp);
4594 if (status == PJ_SUCCESS && resp)
4595 status = pjsip_inv_send_msg(inv, resp);
4596 }
4597 return;
4598
4599 on_return:
4600 if (status == PJ_SUCCESS)
4601 pjsip_dlg_send_response(inv->dlg, pjsip_rdata_get_tsx(rdata), tdata);
4602 }
4603
4604
4605 /* Ticket #1735: If this is a secure dialog, make sure that any incoming
4606 * initial/subsequent INVITE/UPDATE request or the 2xx response to INVITE/
4607 * UPDATE specifies secure Contact and Record-Route headers.
4608 */
4609 static pj_bool_t inv_check_secure_dlg(pjsip_inv_session *inv,
4610 pjsip_event *e)
4611 {
4612 pjsip_transaction *tsx = e->body.tsx_state.tsx;
4613 pjsip_dialog *dlg = pjsip_tsx_get_dlg(tsx);
4614
4615 if (pjsip_cfg()->endpt.disable_secure_dlg_check == PJ_FALSE &&
4616 dlg->secure && e->body.tsx_state.type==PJSIP_EVENT_RX_MSG &&
4617 ((tsx->role==PJSIP_ROLE_UAC && tsx->status_code/100 == 2) ||
4618 (tsx->role==PJSIP_ROLE_UAS && tsx->state == PJSIP_TSX_STATE_TRYING))
4619 &&
4620 (tsx->method.id==PJSIP_INVITE_METHOD ||
4621 pjsip_method_cmp(&tsx->method, &pjsip_update_method)==0))
4622 {
4623 const pjsip_msg *msg = e->body.tsx_state.src.rdata->msg_info.msg;
4624 pj_status_t status = PJ_SUCCESS;
4625 pjsip_contact_hdr *c;
4626
4627 /* Check Contact header */
4628 c = (pjsip_contact_hdr*)
4629 pjsip_msg_find_hdr(msg,PJSIP_H_CONTACT, NULL);
4630 if (!(c && c->uri && PJSIP_URI_SCHEME_IS_SIPS(c->uri)))
4631 status = PJSIP_ESESSIONINSECURE;
4632
4633 /* Check top Record-Route header */
4634 if (status == PJ_SUCCESS) {
4635 pjsip_rr_hdr *r = (pjsip_rr_hdr*)
4636 pjsip_msg_find_hdr(msg, PJSIP_H_RECORD_ROUTE,
4637 NULL);
4638 if (r && !PJSIP_URI_SCHEME_IS_SIPS(&r->name_addr)) {
4639 /* Not "sips", check if it is "sip" and has param
4640 * "transport=tls".
4641 */
4642 if (PJSIP_URI_SCHEME_IS_SIP(&r->name_addr)) {
4643 pjsip_sip_uri *sip_uri = (pjsip_sip_uri*)
4644 pjsip_uri_get_uri(r->name_addr.uri);
4645 if (pj_stricmp2(&sip_uri->transport_param, "tls")!=0)
4646 status = PJSIP_ESESSIONINSECURE;
4647 } else {
4648 /* Not "sips" nor "sip", treat it as insecure? */
4649 status = PJSIP_ESESSIONINSECURE;
4650 }
4651 }
4652 }
4653
4654 if (status == PJSIP_ESESSIONINSECURE) {
4655 /* Found non-SIPS scheme in Contact/Record-Route header */
4656
4657 pj_str_t warn_text = pj_str("SIPS Required");
4658
4659 if (tsx->role == PJSIP_ROLE_UAC) {
4660
4661 /* If we are UAC, terminate the session */
4662 pjsip_tx_data *bye;
4663
4664 PJ_LOG(4,(inv->obj_name,
4665 "Secure dialog requires SIPS scheme in Contact and "
4666 "Record-Route headers, ending the session"));
4667
4668 status = pjsip_inv_end_session(inv, 480, &warn_text, &bye);
4669 if (status == PJ_SUCCESS && bye) {
4670 pjsip_warning_hdr *w;
4671 w = pjsip_warning_hdr_create(bye->pool, 381,
4672 pjsip_endpt_name(dlg->endpt),
4673 &warn_text);
4674 if (w)
4675 pjsip_msg_add_hdr(bye->msg, (pjsip_hdr*)w);
4676
4677 status = pjsip_inv_send_msg(inv, bye);
4678 }
4679
4680 } else {
4681
4682 /* If we are UAS, reject the request */
4683 pjsip_rx_data *rdata = e->body.tsx_state.src.rdata;
4684 pjsip_tx_data *tdata;
4685
4686 PJ_LOG(4,(inv->obj_name,
4687 "Secure dialog requires SIPS scheme in Contact and "
4688 "Route headers, rejecting the request"));
4689
4690 status = pjsip_dlg_create_response(inv->dlg, rdata, 480,
4691 NULL, &tdata);
4692 if (status == PJ_SUCCESS) {
4693 pjsip_warning_hdr *w;
4694 w = pjsip_warning_hdr_create(tdata->pool, 381,
4695 pjsip_endpt_name(dlg->endpt),
4696 &warn_text);
4697 if (w)
4698 pjsip_msg_add_hdr(tdata->msg, (pjsip_hdr*)w);
4699
4700 pjsip_dlg_send_response(dlg, tsx, tdata);
4701 }
4702
4703 }
4704
4705 return PJ_FALSE;
4706 }
4707 }
4708
4709 return PJ_TRUE;
4710 }
4711
4712
4713
4714 /*
4715 * State NULL is before anything is sent/received.
4716 */
4717 static void inv_on_state_null( pjsip_inv_session *inv, pjsip_event *e)
4718 {
4719 pjsip_transaction *tsx = e->body.tsx_state.tsx;
4720 pjsip_dialog *dlg = pjsip_tsx_get_dlg(tsx);
4721
4722 PJ_ASSERT_ON_FAIL(tsx && dlg, return);
4723
4724 if (tsx->method.id == PJSIP_INVITE_METHOD) {
4725
4726 /* Keep the initial INVITE transaction. */
4727 if (inv->invite_tsx == NULL)
4728 inv->invite_tsx = tsx;
4729
4730 if (dlg->role == PJSIP_ROLE_UAC) {
4731
4732 /* Save the original INVITE request.
4733 * We may need to resend the INVITE if we receive redirection
4734 * or session timer too small response.
4735 */
4736 if (1) {
4737 if (inv->invite_req) {
4738 pjsip_tx_data_dec_ref(inv->invite_req);
4739 inv->invite_req = NULL;
4740 }
4741 inv->invite_req = tsx->last_tx;
4742 pjsip_tx_data_add_ref(inv->invite_req);
4743 }
4744
4745 switch (tsx->state) {
4746 case PJSIP_TSX_STATE_CALLING:
4747 inv_set_state(inv, PJSIP_INV_STATE_CALLING, e);
4748 break;
4749 default:
4750 inv_on_state_calling(inv, e);
4751 break;
4752 }
4753
4754 } else {
4755 switch (tsx->state) {
4756 case PJSIP_TSX_STATE_TRYING:
4757 inv_set_state(inv, PJSIP_INV_STATE_INCOMING, e);
4758 break;
4759 case PJSIP_TSX_STATE_PROCEEDING:
4760 inv_set_state(inv, PJSIP_INV_STATE_INCOMING, e);
4761 if (tsx->status_code > 100)
4762 inv_set_state(inv, PJSIP_INV_STATE_EARLY, e);
4763 break;
4764 case PJSIP_TSX_STATE_TERMINATED:
4765 /* there is a failure in sending response. */
4766 inv_set_cause(inv, tsx->status_code, &tsx->status_text);
4767 inv_set_state(inv, PJSIP_INV_STATE_DISCONNECTED, e);
4768 break;
4769 default:
4770 inv_on_state_incoming(inv, e);
4771 break;
4772 }
4773 }
4774
4775 } else {
4776 pj_assert(!"Unexpected transaction type");
4777 }
4778 }
4779
4780 /*
4781 * Generic UAC transaction handler:
4782 * - resend request on 401 or 407 response.
4783 * - terminate dialog on 408 and 481 response.
4784 * - resend request on 422 response.
4785 */
4786 static pj_bool_t handle_uac_tsx_response(pjsip_inv_session *inv,
4787 pjsip_event *e)
4788 {
4789 /* RFC 3261 Section 12.2.1.2:
4790 * If the response for a request within a dialog is a 481
4791 * (Call/Transaction Does Not Exist) or a 408 (Request Timeout), the UAC
4792 * SHOULD terminate the dialog. A UAC SHOULD also terminate a dialog if
4793 * no response at all is received for the request (the client
4794 * transaction would inform the TU about the timeout.)
4795 *
4796 * For INVITE initiated dialogs, terminating the dialog consists of
4797 * sending a BYE.
4798 *
4799 * Note:
4800 * according to X, this should terminate dialog usage only, not the
4801 * dialog.
4802 */
4803 pjsip_transaction *tsx = e->body.tsx_state.tsx;
4804
4805 pj_assert(tsx->role == PJSIP_UAC_ROLE);
4806
4807 /* Note that 481 response to CANCEL does not terminate dialog usage,
4808 * but only the transaction.
4809 *
4810 * Avoid acting on the subsequent COMPLETED->TERMINATED transition
4811 * (e.g. Timer D), which can happen long after the response/timeout
4812 * was already processed.
4813 */
4814 if (inv->state != PJSIP_INV_STATE_DISCONNECTED &&
4815 e->body.tsx_state.prev_state != PJSIP_TSX_STATE_COMPLETED &&
4816 ((tsx->status_code == PJSIP_SC_CALL_TSX_DOES_NOT_EXIST &&
4817 tsx->method.id != PJSIP_CANCEL_METHOD) ||
4818 (tsx->status_code == PJSIP_SC_REQUEST_TIMEOUT &&
4819 !pjsip_cfg()->endpt.keep_inv_after_tsx_timeout)))
4820 {
4821 pjsip_tx_data *bye;
4822 pj_status_t status;
4823
4824 inv_set_cause(inv, tsx->status_code, &tsx->status_text);
4825
4826 /* Do not shift state to DISCONNECTED here, as it will destroy the
4827 * invite session and the BYE sending below will raise an assertion.
4828 */
4829 //inv_set_state(inv, PJSIP_INV_STATE_DISCONNECTED, e);
4830
4831 /* Send BYE */
4832 status = pjsip_dlg_create_request(inv->dlg, pjsip_get_bye_method(),
4833 -1, &bye);
4834 if (status == PJ_SUCCESS && tsx->status_text.slen) {
4835 status = add_reason_warning_hdr(bye, tsx->status_code,
4836 &tsx->status_text);
4837 }
4838 if (status == PJ_SUCCESS) {
4839 pjsip_inv_send_msg(inv, bye);
4840 }
4841
4842 return PJ_TRUE; /* Handled */
4843
4844 }
4845 /* Handle 401/407 challenge. */
4846 else if (tsx->state == PJSIP_TSX_STATE_COMPLETED &&
4847 (tsx->status_code == PJSIP_SC_UNAUTHORIZED ||
4848 tsx->status_code == PJSIP_SC_PROXY_AUTHENTICATION_REQUIRED))
4849 {
4850 pjsip_tx_data *tdata;
4851 pj_status_t status;
4852 pjsip_auth_clt_async_on_chal_param chal_param = { 0 };
4853 struct tsx_inv_data *tsx_inv_data;
4854
4855 /* Clear invite_tsx so pjsip_inv_send_msg() can accept the
4856 * retried INVITE. Note: if another re-INVITE arrives before
4857 * the deferred send_impl fires, invite_tsx will be overwritten.
4858 * This is acceptable because tsx_inv_data->retrying prevents
4859 * the old tsx from affecting the inv state machine.
4860 */
4861 if (tsx->method.id == PJSIP_INVITE_METHOD)
4862 inv->invite_tsx = NULL;
4863
4864 /* In-dialog request auth: abandon_impl is NULL because
4865 * abandoning a mid-dialog request does not terminate the session.
4866 * Dialog grp_lock is held by the caller; see BYE handler comment.
4867 */
4868 pj_bzero(&chal_param, sizeof(chal_param));
4869 {
4870 pjsip_auth_clt_async_impl_token *token;
4871 token = PJ_POOL_ZALLOC_T(tsx->pool,
4872 pjsip_auth_clt_async_impl_token);
4873 token->user_data = inv;
4874 token->send_impl = &inv_async_auth_send_impl;
4875 token->abandon_impl = NULL;
4876 token->grp_lock = tsx->grp_lock;
4877 pj_grp_lock_add_ref(tsx->grp_lock);
4878
4879 chal_param.rdata = e->body.tsx_state.src.rdata;
4880 chal_param.tdata = tsx->last_tx;
4881 status = pjsip_auth_clt_async_impl_on_challenge(
4882 &inv->dlg->auth_sess,
4883 token, &chal_param);
4884 if (status != PJ_SUCCESS)
4885 pj_grp_lock_dec_ref(tsx->grp_lock);
4886 }
4887 if (status == PJ_SUCCESS) {
4888 tsx_inv_data =
4889 (struct tsx_inv_data*)tsx->mod_data[mod_inv.mod.id];
4890 if (tsx_inv_data)
4891 tsx_inv_data->retrying = PJ_TRUE;
4892 } else {
4893 status = pjsip_auth_clt_reinit_req(
4894 &inv->dlg->auth_sess,
4895 e->body.tsx_state.src.rdata,
4896 tsx->last_tx, &tdata);
4897 if (status == PJ_SUCCESS) {
4898 tsx_inv_data =
4899 (struct tsx_inv_data*)tsx->mod_data[mod_inv.mod.id];
4900 if (tsx_inv_data)
4901 tsx_inv_data->retrying = PJ_TRUE;
4902
4903 /* Re-send request. */
4904 status = pjsip_inv_send_msg(inv, tdata);
4905 }
4906 /* Somehow failed. Probably it's not a good idea to
4907 * terminate the session since this is just a request
4908 * within dialog. And even if we terminate we should
4909 * send BYE.
4910 */
4911 }
4912
4913 return PJ_TRUE; /* Handled */
4914 }
4915
4916 /* Handle session timer 422 response. */
4917 else if (tsx->state == PJSIP_TSX_STATE_COMPLETED &&
4918 tsx->status_code == PJSIP_SC_SESSION_TIMER_TOO_SMALL)
4919 {
4920 handle_timer_response(inv, e->body.tsx_state.src.rdata,
4921 PJ_FALSE);
4922
4923 return PJ_TRUE; /* Handled */
4924
4925 }
4926
4927 else {
4928 return PJ_FALSE; /* Unhandled */
4929 }
4930 }
4931
4932
4933 /* Handle call rejection, especially with regard to processing call
4934 * redirection. We need to handle the following scenarios:
4935 * - 3xx response is received -- see if on_redirected() callback is
4936 * implemented. If so, add the Contact URIs in the response to the
4937 * target set and notify user.
4938 * - 4xx - 6xx resposne is received -- see if we're currently recursing,
4939 * if so fetch the next target if any and notify the on_redirected()
4940 * callback.
4941 * - for other cases -- disconnect the session.
4942 */
4943 static void handle_uac_call_rejection(pjsip_inv_session *inv, pjsip_event *e)
4944 {
4945 pjsip_transaction *tsx = e->body.tsx_state.tsx;
4946 pj_status_t status;
4947
4948 if (PJSIP_IS_STATUS_IN_CLASS(tsx->status_code, 300)) {
4949
4950 if (mod_inv.cb.on_redirected == NULL) {
4951
4952 /* Redirection callback is not implemented, disconnect the
4953 * call.
4954 */
4955 goto terminate_session;
4956
4957 } else {
4958 const pjsip_msg *res_msg;
4959
4960 res_msg = e->body.tsx_state.src.rdata->msg_info.msg;
4961
4962 /* Gather all Contact URI's in the response and add them
4963 * to target set. The function will take care of removing
4964 * duplicate URI's.
4965 */
4966 pjsip_target_set_add_from_msg(&inv->dlg->target_set,
4967 inv->dlg->pool, res_msg);
4968
4969 /* Recurse to alternate targets if application allows us */
4970 if (!inv_uac_recurse(inv, tsx->status_code, &tsx->status_text, e))
4971 {
4972 /* Recursion fails, terminate session now */
4973 goto terminate_session;
4974 }
4975
4976 /* Done */
4977 }
4978
4979 } else if ((tsx->status_code==401 || tsx->status_code==407) &&
4980 !inv->cancelling)
4981 {
4982 /* Handle authentication failure:
4983 * Resend the request with Authorization header.
4984 */
4985 pjsip_tx_data *tdata;
4986 pjsip_auth_clt_async_on_chal_param chal_param = { 0 };
4987
4988 /* Initial INVITE auth: abandoning terminates the session.
4989 * Dialog grp_lock is held by the caller; see BYE handler comment.
4990 */
4991 pj_bzero(&chal_param, sizeof(chal_param));
4992 {
4993 pjsip_auth_clt_async_impl_token *token;
4994 token = PJ_POOL_ZALLOC_T(tsx->pool,
4995 pjsip_auth_clt_async_impl_token);
4996 token->user_data = inv;
4997 token->send_impl = &inv_async_auth_send_impl;
4998 token->abandon_impl = &inv_initial_invite_auth_abandon_impl;
4999 token->grp_lock = tsx->grp_lock;
5000 pj_grp_lock_add_ref(tsx->grp_lock);
5001
5002 chal_param.rdata = e->body.tsx_state.src.rdata;
5003 chal_param.tdata = tsx->last_tx;
5004 status = pjsip_auth_clt_async_impl_on_challenge(
5005 &inv->dlg->auth_sess,
5006 token, &chal_param);
5007 if (status != PJ_SUCCESS)
5008 pj_grp_lock_dec_ref(tsx->grp_lock);
5009 }
5010 if (status != PJ_SUCCESS) {
5011 status = pjsip_auth_clt_reinit_req(
5012 &inv->dlg->auth_sess,
5013 e->body.tsx_state.src.rdata,
5014 tsx->last_tx, &tdata);
5015 if (status != PJ_SUCCESS) {
5016
5017 /* Does not have proper credentials. If we are currently
5018 * recursing, try the next target. Otherwise end the
5019 * session.
5020 */
5021 if (!inv_uac_recurse(inv, tsx->status_code,
5022 &tsx->status_text, e))
5023 {
5024 /* Recursion fails, terminate session now */
5025 goto terminate_session;
5026 }
5027
5028 } else {
5029
5030 /* Restart session. */
5031 pjsip_inv_uac_restart(inv, PJ_FALSE);
5032
5033 /* Send the request. */
5034 status = pjsip_inv_send_msg(inv, tdata);
5035 }
5036 }
5037
5038 } else if (tsx->state == PJSIP_TSX_STATE_COMPLETED &&
5039 tsx->status_code == PJSIP_SC_SESSION_TIMER_TOO_SMALL)
5040 {
5041 /* Handle session timer 422 response:
5042 * Resend the request with requested session timer setting.
5043 */
5044 status = handle_timer_response(inv, e->body.tsx_state.src.rdata,
5045 PJ_FALSE);
5046 if (status != PJ_SUCCESS)
5047 goto terminate_session;
5048
5049 } else if (PJSIP_IS_STATUS_IN_CLASS(tsx->status_code, 600)) {
5050 /* Global error */
5051 goto terminate_session;
5052
5053 } else {
5054 /* See if we have alternate target to try */
5055 if (!inv_uac_recurse(inv, tsx->status_code, &tsx->status_text, e)) {
5056 /* Recursion fails, terminate session now */
5057 goto terminate_session;
5058 }
5059 }
5060 return;
5061
5062 terminate_session:
5063 inv_set_cause(inv, tsx->status_code, &tsx->status_text);
5064 inv_set_state(inv, PJSIP_INV_STATE_DISCONNECTED, e);
5065 }
5066
5067
5068 /*
5069 * State CALLING is after sending initial INVITE request but before
5070 * any response (with tag) is received.
5071 */
5072 static void inv_on_state_calling( pjsip_inv_session *inv, pjsip_event *e)
5073 {
5074 pjsip_transaction *tsx = e->body.tsx_state.tsx;
5075 pjsip_dialog *dlg = pjsip_tsx_get_dlg(tsx);
5076 pj_status_t status;
5077
5078 PJ_ASSERT_ON_FAIL(tsx && dlg, return);
5079
5080 if (tsx == inv->invite_tsx) {
5081
5082 switch (tsx->state) {
5083
5084 case PJSIP_TSX_STATE_CALLING:
5085 inv_set_state(inv, PJSIP_INV_STATE_CALLING, e);
5086 break;
5087
5088 case PJSIP_TSX_STATE_PROCEEDING:
5089 if (inv->pending_cancel) {
5090 pjsip_tx_data *cancel;
5091
5092 inv->pending_cancel = PJ_FALSE;
5093
5094 status = pjsip_inv_end_session(inv, 487, NULL, &cancel);
5095 if (status == PJ_SUCCESS && cancel)
5096 status = pjsip_inv_send_msg(inv, cancel);
5097 }
5098
5099 if (tsx->status_code != 100) {
5100
5101 if (inv->role == PJSIP_ROLE_UAC) {
5102 pjsip_rx_data *rdata = e->body.tsx_state.src.rdata;
5103 pjsip_allow_hdr *allow = NULL;
5104 pjsip_msg *msg = rdata->msg_info.msg;
5105
5106 if (msg) {
5107 allow = (pjsip_allow_hdr*) pjsip_msg_find_hdr(msg,
5108 PJSIP_H_ALLOW, NULL);
5109 }
5110 if (allow) {
5111 unsigned i;
5112 const pj_str_t STR_UPDATE = { "UPDATE", 6 };
5113
5114 for (i=0; i<allow->count; ++i) {
5115 if (pj_stricmp(&allow->values[i], &STR_UPDATE)==0) {
5116 /* UPDATE is present in Allow */
5117 inv->options |= PJSIP_INV_SUPPORT_UPDATE;
5118 break;
5119 }
5120 }
5121 }
5122 }
5123
5124 if (dlg->remote.info->tag.slen)
5125 inv_set_state(inv, PJSIP_INV_STATE_EARLY, e);
5126
5127 inv_check_sdp_in_incoming_msg(inv, tsx,
5128 e->body.tsx_state.src.rdata);
5129
5130 if (pjsip_100rel_is_reliable(e->body.tsx_state.src.rdata)) {
5131 inv_handle_incoming_reliable_response(
5132 inv, e->body.tsx_state.src.rdata);
5133 }
5134
5135 } else {
5136 /* Ignore 100 (Trying) response, as it doesn't change
5137 * session state. It only ceases retransmissions.
5138 */
5139 }
5140 break;
5141
5142 case PJSIP_TSX_STATE_COMPLETED:
5143 if (tsx->status_code/100 == 2) {
5144
5145 /* This should not happen.
5146 * When transaction receives 2xx, it should be terminated
5147 */
5148 pj_assert(0);
5149
5150 inv_set_state(inv, PJSIP_INV_STATE_CONNECTING, e);
5151
5152 /* Check routing URI scheme for secure dialog */
5153 if (!inv_check_secure_dlg(inv, e))
5154 break;
5155
5156 /* Process session timer response. */
5157 status = handle_timer_response(inv,
5158 e->body.tsx_state.src.rdata,
5159 PJ_TRUE);
5160 if (status != PJ_SUCCESS)
5161 break;
5162
5163 inv_check_sdp_in_incoming_msg(inv, tsx,
5164 e->body.tsx_state.src.rdata);
5165
5166 } else {
5167 handle_uac_call_rejection(inv, e);
5168 }
5169 break;
5170
5171 case PJSIP_TSX_STATE_TERMINATED:
5172 /* INVITE transaction can be terminated either because UAC
5173 * transaction received 2xx response or because of transport
5174 * error.
5175 */
5176 if (tsx->status_code/100 == 2) {
5177 /* This must be receipt of 2xx response */
5178 pj_assert(e->body.tsx_state.type == PJSIP_EVENT_RX_MSG);
5179
5180 /* Set state to CONNECTING */
5181 inv_set_state(inv, PJSIP_INV_STATE_CONNECTING, e);
5182
5183 /* Check routing URI scheme for secure dialog */
5184 if (!inv_check_secure_dlg(inv, e))
5185 break;
5186
5187 /* Process session timer response. */
5188 status = handle_timer_response(inv,
5189 e->body.tsx_state.src.rdata,
5190 PJ_TRUE);
5191 if (status != PJ_SUCCESS)
5192 break;
5193
5194 inv_check_sdp_in_incoming_msg(inv, tsx,
5195 e->body.tsx_state.src.rdata);
5196 /* Send ACK */
5197 inv_send_ack(inv, e);
5198
5199 } else {
5200 inv_set_cause(inv, tsx->status_code, &tsx->status_text);
5201 inv_set_state(inv, PJSIP_INV_STATE_DISCONNECTED, e);
5202 }
5203 break;
5204
5205 default:
5206 break;
5207 }
5208
5209 } else if (tsx->role == PJSIP_ROLE_UAC) {
5210 /*
5211 * Handle case when outgoing request is answered with 481 (Call/
5212 * Transaction Does Not Exist), 408, or when it's timed out. In these
5213 * cases, disconnect session (i.e. dialog usage only).
5214 * Note that 481 response to CANCEL does not terminate dialog usage,
5215 * but only the transaction.
5216 */
5217 if ((tsx->status_code == PJSIP_SC_CALL_TSX_DOES_NOT_EXIST &&
5218 tsx->method.id != PJSIP_CANCEL_METHOD) ||
5219 tsx->status_code == PJSIP_SC_REQUEST_TIMEOUT ||
5220 tsx->status_code == PJSIP_SC_TSX_TIMEOUT)
5221 {
5222 inv_set_cause(inv, tsx->status_code, &tsx->status_text);
5223 inv_set_state(inv, PJSIP_INV_STATE_DISCONNECTED, e);
5224 }
5225 } else if (tsx->role == PJSIP_ROLE_UAS &&
5226 tsx->state == PJSIP_TSX_STATE_TRYING &&
5227 pjsip_method_cmp(&tsx->method, &pjsip_update_method)==0)
5228 {
5229 /*
5230 * Handle a very early UPDATE
5231 */
5232 inv_respond_incoming_update(inv, e);
5233
5234
5235 }
5236 }
5237
5238 /*
5239 * State INCOMING is after we received the request, but before
5240 * responses with tag are sent.
5241 */
5242 static void inv_on_state_incoming( pjsip_inv_session *inv, pjsip_event *e)
5243 {
5244 pjsip_transaction *tsx = e->body.tsx_state.tsx;
5245 pjsip_dialog *dlg = pjsip_tsx_get_dlg(tsx);
5246
5247 PJ_ASSERT_ON_FAIL(tsx && dlg, return);
5248
5249 if (tsx == inv->invite_tsx) {
5250
5251 /*
5252 * Handle the INVITE state transition.
5253 */
5254
5255 switch (tsx->state) {
5256
5257 case PJSIP_TSX_STATE_TRYING:
5258 inv_set_state(inv, PJSIP_INV_STATE_INCOMING, e);
5259 break;
5260
5261 case PJSIP_TSX_STATE_PROCEEDING:
5262 /*
5263 * Transaction sent provisional response.
5264 */
5265 if (tsx->status_code > 100)
5266 inv_set_state(inv, PJSIP_INV_STATE_EARLY, e);
5267 break;
5268
5269 case PJSIP_TSX_STATE_COMPLETED:
5270 /*
5271 * Transaction sent final response.
5272 */
5273 if (tsx->status_code/100 == 2) {
5274 inv_set_state(inv, PJSIP_INV_STATE_CONNECTING, e);
5275 } else {
5276 inv_set_cause(inv, tsx->status_code, &tsx->status_text);
5277 inv_set_state(inv, PJSIP_INV_STATE_DISCONNECTED, e);
5278 }
5279 break;
5280
5281 case PJSIP_TSX_STATE_TERMINATED:
5282 /*
5283 * This happens on transport error (e.g. failed to send
5284 * response)
5285 */
5286 inv_set_cause(inv, tsx->status_code, &tsx->status_text);
5287 inv_set_state(inv, PJSIP_INV_STATE_DISCONNECTED, e);
5288 break;
5289
5290 default:
5291 pj_assert(!"Unexpected INVITE state");
5292 break;
5293 }
5294
5295 } else if (tsx->method.id == PJSIP_CANCEL_METHOD &&
5296 tsx->role == PJSIP_ROLE_UAS &&
5297 tsx->state < PJSIP_TSX_STATE_COMPLETED &&
5298 e->body.tsx_state.type == PJSIP_EVENT_RX_MSG )
5299 {
5300
5301 /*
5302 * Handle incoming CANCEL request.
5303 */
5304
5305 inv_respond_incoming_cancel(inv, tsx, e);
5306
5307 }
5308 }
5309
5310 /*
5311 * State EARLY is for both UAS and UAC, after response with To tag
5312 * is sent/received.
5313 */
5314 static void inv_on_state_early( pjsip_inv_session *inv, pjsip_event *e)
5315 {
5316 pjsip_transaction *tsx = e->body.tsx_state.tsx;
5317 pjsip_dialog *dlg = pjsip_tsx_get_dlg(tsx);
5318
5319 PJ_ASSERT_ON_FAIL(tsx && dlg, return);
5320
5321 if (tsx == inv->invite_tsx) {
5322
5323 /*
5324 * Handle the INVITE state progress.
5325 */
5326
5327 switch (tsx->state) {
5328
5329 case PJSIP_TSX_STATE_PROCEEDING:
5330 /* Send/received another provisional response. */
5331 inv_set_state(inv, PJSIP_INV_STATE_EARLY, e);
5332
5333 if (e->body.tsx_state.type == PJSIP_EVENT_RX_MSG) {
5334 inv_check_sdp_in_incoming_msg(inv, tsx,
5335 e->body.tsx_state.src.rdata);
5336
5337 if (pjsip_100rel_is_reliable(e->body.tsx_state.src.rdata)) {
5338 inv_handle_incoming_reliable_response(
5339 inv, e->body.tsx_state.src.rdata);
5340 }
5341 }
5342 break;
5343
5344 case PJSIP_TSX_STATE_COMPLETED:
5345 if (tsx->status_code/100 == 2) {
5346 inv_set_state(inv, PJSIP_INV_STATE_CONNECTING, e);
5347 if (e->body.tsx_state.type == PJSIP_EVENT_RX_MSG) {
5348 pj_status_t status;
5349
5350 /* Check routing URI scheme for secure dialog */
5351 if (!inv_check_secure_dlg(inv, e))
5352 break;
5353
5354 /* Process session timer response. */
5355 status = handle_timer_response(inv,
5356 e->body.tsx_state.src.rdata,
5357 PJ_TRUE);
5358 if (status != PJ_SUCCESS)
5359 break;
5360
5361 inv_check_sdp_in_incoming_msg(inv, tsx,
5362 e->body.tsx_state.src.rdata);
5363 }
5364
5365 } else if (tsx->role == PJSIP_ROLE_UAC) {
5366
5367 handle_uac_call_rejection(inv, e);
5368
5369 } else {
5370 inv_set_cause(inv, tsx->status_code, &tsx->status_text);
5371 inv_set_state(inv, PJSIP_INV_STATE_DISCONNECTED, e);
5372 }
5373 break;
5374
5375 case PJSIP_TSX_STATE_CONFIRMED:
5376 /* For some reason can go here (maybe when ACK for 2xx has
5377 * the same branch value as the INVITE transaction) */
5378
5379 case PJSIP_TSX_STATE_TERMINATED:
5380 /* INVITE transaction can be terminated either because UAC
5381 * transaction received 2xx response or because of transport
5382 * error.
5383 */
5384 if (tsx->status_code/100 == 2) {
5385
5386 /* This must be receipt of 2xx response */
5387
5388 /* Set state to CONNECTING */
5389 inv_set_state(inv, PJSIP_INV_STATE_CONNECTING, e);
5390
5391 if (e->body.tsx_state.type == PJSIP_EVENT_RX_MSG) {
5392 pj_status_t status;
5393
5394 /* Check routing URI scheme for secure dialog */
5395 if (!inv_check_secure_dlg(inv, e))
5396 break;
5397
5398 /* Process session timer response. */
5399 status = handle_timer_response(inv,
5400 e->body.tsx_state.src.rdata,
5401 PJ_TRUE);
5402 if (status != PJ_SUCCESS)
5403 break;
5404
5405 inv_check_sdp_in_incoming_msg(inv, tsx,
5406 e->body.tsx_state.src.rdata);
5407 }
5408
5409 /* if UAC, send ACK and move state to confirmed. */
5410 if (tsx->role == PJSIP_ROLE_UAC) {
5411 pj_assert(e->body.tsx_state.type == PJSIP_EVENT_RX_MSG);
5412
5413 inv_send_ack(inv, e);
5414 }
5415
5416 } else {
5417 inv_set_cause(inv, tsx->status_code, &tsx->status_text);
5418 inv_set_state(inv, PJSIP_INV_STATE_DISCONNECTED, e);
5419 }
5420 break;
5421
5422 default:
5423 pj_assert(!"Unexpected INVITE tsx state");
5424 break;
5425 }
5426 } else if (tsx->method.id == PJSIP_INVITE_METHOD &&
5427 tsx->role == PJSIP_ROLE_UAS &&
5428 tsx->state == PJSIP_TSX_STATE_TRYING)
5429 {
5430 /* Handle incoming second INVITE. See RFC 3261 section 14.2. */
5431 pjsip_rx_data *rdata = e->body.tsx_state.src.rdata;
5432 pjsip_tx_data *tdata;
5433 pj_status_t status;
5434 int code;
5435 const pj_str_t *reason;
5436
5437 if (inv->invite_tsx->role == PJSIP_ROLE_UAC)
5438 code = PJSIP_SC_REQUEST_PENDING;
5439 else
5440 code = PJSIP_SC_INTERNAL_SERVER_ERROR;
5441
5442 reason = pjsip_get_status_text(code);
5443
5444 /* Can not receive second INVITE while another one is pending. */
5445 status = pjsip_dlg_create_response( inv->dlg, rdata, code,
5446 reason, &tdata);
5447 if (status != PJ_SUCCESS)
5448 return;
5449
5450 if (code == PJSIP_SC_INTERNAL_SERVER_ERROR) {
5451 /* MUST include Retry-After header with random value
5452 * between 0-10.
5453 */
5454 pjsip_retry_after_hdr *ra_hdr;
5455 int val = (pj_rand() % 10);
5456
5457 ra_hdr = pjsip_retry_after_hdr_create(tdata->pool, val);
5458 pjsip_msg_add_hdr(tdata->msg, (pjsip_hdr*)ra_hdr);
5459 }
5460
5461 pjsip_dlg_send_response( inv->dlg, tsx, tdata);
5462
5463 } else if (inv->role == PJSIP_ROLE_UAS &&
5464 tsx->role == PJSIP_ROLE_UAS &&
5465 tsx->method.id == PJSIP_CANCEL_METHOD &&
5466 tsx->state < PJSIP_TSX_STATE_COMPLETED &&
5467 e->body.tsx_state.type == PJSIP_EVENT_RX_MSG )
5468 {
5469
5470 /*
5471 * Handle incoming CANCEL request.
5472 */
5473
5474 inv_respond_incoming_cancel(inv, tsx, e);
5475
5476 } else if (tsx->role == PJSIP_ROLE_UAS &&
5477 tsx->state == PJSIP_TSX_STATE_TRYING &&
5478 pjsip_method_cmp(&tsx->method, &pjsip_update_method)==0)
5479 {
5480 /*
5481 * Handle incoming UPDATE
5482 */
5483 inv_respond_incoming_update(inv, e);
5484
5485
5486 } else if (tsx->role == PJSIP_ROLE_UAC &&
5487 (tsx->state == PJSIP_TSX_STATE_COMPLETED ||
5488 tsx->state == PJSIP_TSX_STATE_TERMINATED) &&
5489 pjsip_method_cmp(&tsx->method, &pjsip_update_method)==0)
5490 {
5491 /*
5492 * Handle response to outgoing UPDATE request.
5493 */
5494 inv_handle_update_response(inv, e);
5495
5496 } else if (tsx->role == PJSIP_ROLE_UAS &&
5497 tsx->state == PJSIP_TSX_STATE_TRYING &&
5498 pjsip_method_cmp(&tsx->method, &pjsip_prack_method)==0)
5499 {
5500 /*
5501 * Handle incoming PRACK
5502 */
5503 inv_respond_incoming_prack(inv, e->body.tsx_state.src.rdata);
5504
5505 } else if (tsx->role == PJSIP_ROLE_UAC) {
5506
5507 /* Generic handling for UAC tsx completion */
5508 handle_uac_tsx_response(inv, e);
5509
5510 } else if (tsx->role == PJSIP_ROLE_UAS &&
5511 tsx->method.id == PJSIP_BYE_METHOD &&
5512 tsx->status_code < 200 &&
5513 e->body.tsx_state.type == PJSIP_EVENT_RX_MSG)
5514 {
5515 /* Received BYE before the 2xx/OK response to INVITE.
5516 * Assume that the 2xx/OK response is lost and the BYE
5517 * arrives earlier.
5518 */
5519 inv_respond_incoming_bye(inv, tsx, e->body.tsx_state.src.rdata, e);
5520
5521 if (inv->invite_tsx->role == PJSIP_ROLE_UAC) {
5522 /* Set timer just in case we will never get the final response
5523 * for INVITE.
5524 */
5525 pjsip_tsx_set_timeout(inv->invite_tsx, 64*pjsip_cfg()->tsx.t1);
5526 } else if (inv->invite_tsx->status_code < 200) {
5527 pjsip_tx_data *tdata;
5528 pjsip_msg *msg;
5529
5530 /* For UAS, send a final response. */
5531 tdata = inv->invite_tsx->last_tx;
5532 PJ_ASSERT_ON_FAIL(tdata != NULL, return);
5533
5534 msg = tdata->msg;
5535 msg->line.status.code = PJSIP_SC_REQUEST_TERMINATED;
5536 msg->line.status.reason =
5537 *pjsip_get_status_text(PJSIP_SC_REQUEST_TERMINATED);
5538 msg->body = NULL;
5539
5540 pjsip_tx_data_invalidate_msg(tdata);
5541 pjsip_tx_data_add_ref(tdata);
5542
5543 pjsip_dlg_send_response(inv->dlg, inv->invite_tsx, tdata);
5544 }
5545 }
5546 }
5547
5548 /*
5549 * State CONNECTING is after 2xx response to INVITE is sent/received.
5550 */
5551 static void inv_on_state_connecting( pjsip_inv_session *inv, pjsip_event *e)
5552 {
5553 pjsip_transaction *tsx = e->body.tsx_state.tsx;
5554 pjsip_dialog *dlg = pjsip_tsx_get_dlg(tsx);
5555
5556 PJ_ASSERT_ON_FAIL(tsx && dlg, return);
5557
5558 if (tsx == inv->invite_tsx) {
5559
5560 /*
5561 * Handle INVITE state progression.
5562 */
5563 switch (tsx->state) {
5564
5565 case PJSIP_TSX_STATE_CONFIRMED:
5566 /* It can only go here if incoming ACK request has the same Via
5567 * branch parameter as the INVITE transaction.
5568 */
5569 if (tsx->status_code/100 == 2) {
5570 if (e->body.tsx_state.type == PJSIP_EVENT_RX_MSG) {
5571 inv_check_sdp_in_incoming_msg(inv, tsx,
5572 e->body.tsx_state.src.rdata);
5573 }
5574
5575 inv_set_state(inv, PJSIP_INV_STATE_CONFIRMED, e);
5576
5577 /* Send pending BYE if any:
5578 * https://github.com/pjsip/pjproject/issues/1712
5579 * Do this after setting the state to CONFIRMED, so that we
5580 * have consistent CONFIRMED state between caller and callee.
5581 */
5582 if (inv->pending_bye)
5583 inv_perform_pending_bye(inv);
5584
5585 }
5586 break;
5587
5588 case PJSIP_TSX_STATE_TERMINATED:
5589 /* INVITE transaction can be terminated either because UAC
5590 * transaction received 2xx response or because of transport
5591 * error.
5592 */
5593 if (tsx->status_code/100 != 2) {
5594 if (tsx->role == PJSIP_ROLE_UAC) {
5595 inv_set_cause(inv, tsx->status_code, &tsx->status_text);
5596 inv_set_state(inv, PJSIP_INV_STATE_DISCONNECTED, e);
5597 } else {
5598 pjsip_tx_data *bye;
5599 pj_status_t status;
5600
5601 inv_set_cause(inv, tsx->status_code, &tsx->status_text);
5602
5603 /* Send BYE */
5604 status = pjsip_dlg_create_request(inv->dlg,
5605 pjsip_get_bye_method(),
5606 -1, &bye);
5607 if (status == PJ_SUCCESS && tsx->status_text.slen) {
5608 status = add_reason_warning_hdr(bye, tsx->status_code,
5609 &tsx->status_text);
5610 }
5611 if (status == PJ_SUCCESS) {
5612 pjsip_inv_send_msg(inv, bye);
5613
5614 if (inv->pending_bye) {
5615 pjsip_tx_data_dec_ref(inv->pending_bye);
5616 inv->pending_bye = NULL;
5617 }
5618 }
5619 }
5620 }
5621 break;
5622
5623 case PJSIP_TSX_STATE_DESTROYED:
5624 /* Do nothing. */
5625 break;
5626
5627 default:
5628 pj_assert(!"Unexpected state");
5629 break;
5630 }
5631
5632 } else if (tsx->role == PJSIP_ROLE_UAS &&
5633 tsx->method.id == PJSIP_BYE_METHOD &&
5634 tsx->status_code < 200 &&
5635 e->body.tsx_state.type == PJSIP_EVENT_RX_MSG)
5636 {
5637
5638 /*
5639 * Handle incoming BYE.
5640 */
5641
5642 inv_respond_incoming_bye( inv, tsx, e->body.tsx_state.src.rdata, e );
5643
5644 } else if (tsx->method.id == PJSIP_BYE_METHOD &&
5645 tsx->role == PJSIP_ROLE_UAC &&
5646 (tsx->state == PJSIP_TSX_STATE_COMPLETED ||
5647 tsx->state == PJSIP_TSX_STATE_TERMINATED))
5648 {
5649
5650 /*
5651 * Outgoing BYE
5652 */
5653 inv_handle_bye_response( inv, tsx, e->body.tsx_state.src.rdata, e);
5654
5655 }
5656 else if (tsx->method.id == PJSIP_CANCEL_METHOD &&
5657 tsx->role == PJSIP_ROLE_UAS &&
5658 tsx->status_code < 200 &&
5659 e->body.tsx_state.type == PJSIP_EVENT_RX_MSG)
5660 {
5661
5662 /*
5663 * Handle strandled incoming CANCEL or CANCEL for re-INVITE
5664 */
5665 inv_respond_incoming_cancel(inv, tsx, e);
5666
5667 } else if (tsx->role == PJSIP_ROLE_UAS &&
5668 tsx->state == PJSIP_TSX_STATE_TRYING &&
5669 pjsip_method_cmp(&tsx->method, pjsip_get_invite_method())==0)
5670 {
5671 pjsip_rx_data *rdata = e->body.tsx_state.src.rdata;
5672 pjsip_tx_data *tdata;
5673 pj_status_t status;
5674
5675 /* See https://github.com/pjsip/pjproject/issues/1455
5676 * Handle incoming re-INVITE before current INVITE is confirmed.
5677 * According to RFC 5407:
5678 * - answer with 200 if we don't have pending offer-answer
5679 * - answer with 491 if we *have* pending offer-answer
5680 *
5681 * But unfortunately accepting the re-INVITE would mean we have
5682 * two outstanding INVITEs, and we don't support that because
5683 * we will get confused when we handle the ACK.
5684 */
5685 status = pjsip_dlg_create_response(inv->dlg, rdata,
5686 PJSIP_SC_REQUEST_PENDING,
5687 NULL, &tdata);
5688 if (status != PJ_SUCCESS)
5689 return;
5690 pjsip_timer_update_resp(inv, tdata);
5691 status = pjsip_dlg_send_response(dlg, tsx, tdata);
5692
5693 } else if (tsx->role == PJSIP_ROLE_UAS &&
5694 tsx->state == PJSIP_TSX_STATE_TRYING &&
5695 pjsip_method_cmp(&tsx->method, &pjsip_update_method)==0)
5696 {
5697 /*
5698 * Handle incoming UPDATE
5699 */
5700 inv_respond_incoming_update(inv, e);
5701
5702
5703 } else if (tsx->role == PJSIP_ROLE_UAC &&
5704 (tsx->state == PJSIP_TSX_STATE_COMPLETED ||
5705 tsx->state == PJSIP_TSX_STATE_TERMINATED) &&
5706 pjsip_method_cmp(&tsx->method, &pjsip_update_method)==0)
5707 {
5708 /*
5709 * Handle response to outgoing UPDATE request.
5710 */
5711 if (inv_handle_update_response(inv, e) == PJ_FALSE)
5712 handle_uac_tsx_response(inv, e);
5713
5714 } else if (tsx->role == PJSIP_ROLE_UAS &&
5715 tsx->state == PJSIP_TSX_STATE_TRYING &&
5716 pjsip_method_cmp(&tsx->method, &pjsip_prack_method)==0)
5717 {
5718 /*
5719 * Handle incoming PRACK
5720 */
5721 inv_respond_incoming_prack(inv, e->body.tsx_state.src.rdata);
5722
5723 } else if (tsx->role == PJSIP_ROLE_UAC) {
5724
5725 /* Generic handling for UAC tsx completion */
5726 handle_uac_tsx_response(inv, e);
5727
5728 }
5729
5730 }
5731
5732 /*
5733 * State CONFIRMED is after ACK is sent/received.
5734 */
5735 static void inv_on_state_confirmed( pjsip_inv_session *inv, pjsip_event *e)
5736 {
5737 pjsip_transaction *tsx = e->body.tsx_state.tsx;
5738 pjsip_dialog *dlg = pjsip_tsx_get_dlg(tsx);
5739
5740 PJ_ASSERT_ON_FAIL(tsx && dlg, return);
5741
5742
5743 if (tsx->method.id == PJSIP_BYE_METHOD &&
5744 tsx->role == PJSIP_ROLE_UAC &&
5745 (tsx->state == PJSIP_TSX_STATE_COMPLETED ||
5746 tsx->state == PJSIP_TSX_STATE_TERMINATED))
5747 {
5748
5749 /*
5750 * Outgoing BYE
5751 */
5752
5753 inv_handle_bye_response( inv, tsx, e->body.tsx_state.src.rdata, e);
5754
5755 }
5756 else if (tsx->method.id == PJSIP_BYE_METHOD &&
5757 tsx->role == PJSIP_ROLE_UAS &&
5758 tsx->status_code < 200 &&
5759 e->body.tsx_state.type == PJSIP_EVENT_RX_MSG)
5760 {
5761
5762 /*
5763 * Handle incoming BYE.
5764 */
5765
5766 inv_respond_incoming_bye( inv, tsx, e->body.tsx_state.src.rdata, e );
5767
5768 }
5769 else if (tsx->method.id == PJSIP_CANCEL_METHOD &&
5770 tsx->role == PJSIP_ROLE_UAS &&
5771 tsx->status_code < 200 &&
5772 e->body.tsx_state.type == PJSIP_EVENT_RX_MSG)
5773 {
5774
5775 /*
5776 * Handle strandled incoming CANCEL or CANCEL for re-INVITE
5777 */
5778 inv_respond_incoming_cancel(inv, tsx, e);
5779 }
5780 else if (tsx->method.id == PJSIP_INVITE_METHOD &&
5781 tsx->role == PJSIP_ROLE_UAS)
5782 {
5783
5784 /*
5785 * Handle incoming re-INVITE
5786 */
5787 if (tsx->state == PJSIP_TSX_STATE_TRYING) {
5788
5789 pjsip_rx_data *rdata = e->body.tsx_state.src.rdata;
5790 pjsip_tx_data *tdata;
5791 pj_status_t status;
5792 pjsip_rdata_sdp_info *sdp_info = NULL;
5793 pjsip_status_code st_code;
5794
5795 /* Check if we have INVITE pending. */
5796 if (inv->invite_tsx && inv->invite_tsx!=tsx) {
5797 int code;
5798 pj_str_t reason;
5799
5800 reason = pj_str("Another INVITE transaction in progress");
5801
5802 if (inv->invite_tsx->role == PJSIP_ROLE_UAC)
5803 code = 491;
5804 else
5805 code = 500;
5806
5807 /* Can not receive re-INVITE while another one is pending. */
5808 status = pjsip_dlg_create_response( inv->dlg, rdata, code,
5809 &reason, &tdata);
5810 if (status != PJ_SUCCESS)
5811 return;
5812
5813 if (code == 500) {
5814 /* MUST include Retry-After header with random value
5815 * between 0-10.
5816 */
5817 pjsip_retry_after_hdr *ra_hdr;
5818 int val = (pj_rand() % 10);
5819
5820 ra_hdr = pjsip_retry_after_hdr_create(tdata->pool, val);
5821 pjsip_msg_add_hdr(tdata->msg, (pjsip_hdr*)ra_hdr);
5822 }
5823
5824 status = pjsip_dlg_send_response( inv->dlg, tsx, tdata);
5825
5826
5827 return;
5828 }
5829
5830 /* Check routing URI scheme for secure dialog */
5831 if (!inv_check_secure_dlg(inv, e))
5832 return;
5833
5834 /* Save the invite transaction. */
5835 inv->invite_tsx = tsx;
5836
5837 /* Process session timers headers in the re-INVITE */
5838 status = pjsip_timer_process_req(inv, rdata, &st_code);
5839 if (status != PJ_SUCCESS) {
5840 status = pjsip_dlg_create_response(inv->dlg, rdata, st_code,
5841 NULL, &tdata);
5842 if (status != PJ_SUCCESS)
5843 return;
5844
5845 pjsip_timer_update_resp(inv, tdata);
5846 status = pjsip_dlg_send_response(dlg, tsx, tdata);
5847 return;
5848 }
5849
5850 /* Send 491 if we receive re-INVITE while another offer/answer
5851 * negotiation is in progress
5852 */
5853 if (pjmedia_sdp_neg_get_state(inv->neg) !=
5854 PJMEDIA_SDP_NEG_STATE_DONE)
5855 {
5856 status = pjsip_dlg_create_response(inv->dlg, rdata,
5857 PJSIP_SC_REQUEST_PENDING,
5858 NULL, &tdata);
5859 if (status != PJ_SUCCESS)
5860 return;
5861 pjsip_timer_update_resp(inv, tdata);
5862 status = pjsip_dlg_send_response(dlg, tsx, tdata);
5863 return;
5864 }
5865
5866 /* Process SDP in incoming message. */
5867 status = inv_check_sdp_in_incoming_msg(inv, tsx, rdata);
5868
5869 if (status == PJ_SUCCESS && mod_inv.cb.on_rx_reinvite &&
5870 inv->notify)
5871 {
5872 pj_status_t rc;
5873
5874 sdp_info = pjsip_rdata_get_sdp_info(rdata);
5875 rc = (*mod_inv.cb.on_rx_reinvite)(inv, sdp_info->sdp,
5876 rdata);
5877 if (rc == PJ_SUCCESS) {
5878 /* Application will send its own response.
5879 * Our job is done. */
5880 PJ_LOG(5,(inv->obj_name, "on_rx_reinvite() returns %d",
5881 rc));
5882 return;
5883 }
5884
5885 /* If application lets us answer the re-INVITE which contains
5886 * SDP, application must set the SDP answer with
5887 * #pjsip_inv_set_sdp_answer().
5888 */
5889 if (sdp_info->sdp &&
5890 (pjmedia_sdp_neg_get_state(inv->neg) !=
5891 PJMEDIA_SDP_NEG_STATE_WAIT_NEGO))
5892 {
5893 status = PJMEDIA_SDPNEG_EINSTATE;
5894 }
5895 }
5896
5897 if (status != PJ_SUCCESS) {
5898 pj_bool_t reject_message = PJ_TRUE;
5899
5900 if (status == PJMEDIA_SDP_EINSDP)
5901 {
5902 sdp_info = pjsip_rdata_get_sdp_info(rdata);
5903 if (sdp_info->body.ptr == NULL &&
5904 PJSIP_INV_ACCEPT_UNKNOWN_BODY)
5905 {
5906 /* Message body is not "application/sdp" */
5907 reject_message = PJ_FALSE;
5908 }
5909 }
5910
5911 if (reject_message) {
5912 /* Not Acceptable */
5913 const pjsip_hdr *accept;
5914 pj_str_t reason = pj_str("SDP negotiation failed");
5915
5916 if (status == PJMEDIA_SDP_EINSDP)
5917 reason = pj_str("Bad SDP");
5918 else if (status == PJMEDIA_SDPNEG_EINSTATE)
5919 reason = pj_str("No SDP answer");
5920
5921 /* The incoming SDP is unacceptable. If the SDP negotiator
5922 * state has just been changed, i.e: DONE -> REMOTE_OFFER,
5923 * revert it back.
5924 */
5925 if (pjmedia_sdp_neg_get_state(inv->neg) ==
5926 PJMEDIA_SDP_NEG_STATE_REMOTE_OFFER)
5927 {
5928 pjmedia_sdp_neg_cancel_offer(inv->neg);
5929 }
5930
5931 status = pjsip_dlg_create_response(inv->dlg, rdata,
5932 (status == PJMEDIA_SDP_EINSDP)?415:488,
5933 NULL, &tdata);
5934
5935 if (status != PJ_SUCCESS)
5936 return;
5937
5938 /* Add Warning header */
5939 add_reason_warning_hdr(tdata, 0, &reason);
5940
5941 accept = pjsip_endpt_get_capability(dlg->endpt,
5942 PJSIP_H_ACCEPT,
5943 NULL);
5944 if (accept) {
5945 pjsip_msg_add_hdr(tdata->msg, (pjsip_hdr*)
5946 pjsip_hdr_clone(tdata->pool, accept));
5947 }
5948
5949 status = pjsip_dlg_send_response(dlg, tsx, tdata);
5950
5951 return;
5952 }
5953 }
5954
5955 /* Create 2xx ANSWER */
5956 status = pjsip_dlg_create_response(dlg, rdata, 200, NULL, &tdata);
5957 if (status != PJ_SUCCESS)
5958 return;
5959
5960 /* If the INVITE request has SDP body, send answer.
5961 * Otherwise generate offer from local active SDP.
5962 */
5963 if (!sdp_info)
5964 sdp_info = pjsip_rdata_get_sdp_info(rdata);
5965 if (sdp_info->sdp != NULL) {
5966 status = process_answer(inv, 200, tdata, NULL);
5967 } else {
5968 /* INVITE does not have SDP.
5969 * If on_create_offer() callback is implemented, ask app.
5970 * to generate an offer, otherwise just send active local
5971 * SDP to signal that nothing gets modified.
5972 */
5973 pjmedia_sdp_session *sdp = NULL;
5974
5975 if (mod_inv.cb.on_create_offer) {
5976 (*mod_inv.cb.on_create_offer)(inv, &sdp);
5977 if (sdp) {
5978 /* Notify negotiator about the new offer. This will
5979 * fix the offer with correct SDP origin.
5980 */
5981 status =
5982 pjmedia_sdp_neg_modify_local_offer2(
5983 inv->pool_prov, inv->neg,
5984 inv->sdp_neg_flags, sdp);
5985
5986 /* Retrieve the "fixed" offer from negotiator */
5987 if (status==PJ_SUCCESS) {
5988 const pjmedia_sdp_session *lsdp = NULL;
5989 pjmedia_sdp_neg_get_neg_local(inv->neg, &lsdp);
5990 sdp = (pjmedia_sdp_session*)lsdp;
5991 }
5992 }
5993 }
5994
5995 if (sdp == NULL) {
5996 const pjmedia_sdp_session *active_sdp = NULL;
5997 status = pjmedia_sdp_neg_send_local_offer(inv->pool_prov,
5998 inv->neg,
5999 &active_sdp);
6000 if (status == PJ_SUCCESS)
6001 sdp = (pjmedia_sdp_session*) active_sdp;
6002 }
6003
6004 if (sdp) {
6005 tdata->msg->body = create_sdp_body(tdata->pool, sdp);
6006 }
6007 }
6008
6009 if (status != PJ_SUCCESS) {
6010 /*
6011 * SDP negotiation has failed.
6012 */
6013 pj_status_t rc;
6014 pj_str_t reason;
6015
6016 /* Delete the 2xx answer */
6017 pjsip_tx_data_dec_ref(tdata);
6018
6019 /* Create 500 response */
6020 reason = pj_str("SDP negotiation failed");
6021 rc = pjsip_dlg_create_response(dlg, rdata, 500, &reason,
6022 &tdata);
6023 if (rc == PJ_SUCCESS) {
6024 pjsip_warning_hdr *w;
6025 const pj_str_t *endpt_name;
6026
6027 endpt_name = pjsip_endpt_name(dlg->endpt);
6028 w = pjsip_warning_hdr_create_from_status(tdata->pool,
6029 endpt_name,
6030 status);
6031 if (w)
6032 pjsip_msg_add_hdr(tdata->msg, (pjsip_hdr*)w);
6033
6034 pjsip_inv_send_msg(inv, tdata);
6035 }
6036 return;
6037 }
6038
6039 /* Invoke Session Timers */
6040 pjsip_timer_update_resp(inv, tdata);
6041
6042 /* Add the Allow and Supported headers here.
6043 * If omitted, the dialog will insert them automatically,
6044 * but without removing entries from disabled extensions.
6045 */
6046 {
6047 const pjsip_hdr *hdr;
6048 pjsip_allow_hdr *allow_hdr = NULL;
6049 pjsip_supported_hdr *sup_hdr = NULL;
6050
6051 if (dlg->add_allow &&
6052 !pjsip_msg_find_hdr(tdata->msg, PJSIP_H_ALLOW, NULL))
6053 {
6054 hdr = pjsip_endpt_get_capability(dlg->endpt,
6055 PJSIP_H_ALLOW, NULL);
6056 if (hdr) {
6057 allow_hdr = (pjsip_allow_hdr*)
6058 pjsip_hdr_clone(tdata->pool, hdr);
6059 pjsip_msg_add_hdr(tdata->msg, (pjsip_hdr*)allow_hdr);
6060 }
6061 }
6062
6063 if (!pjsip_msg_find_hdr(tdata->msg, PJSIP_H_SUPPORTED, NULL))
6064 {
6065 hdr = pjsip_endpt_get_capability(inv->dlg->endpt,
6066 PJSIP_H_SUPPORTED, NULL);
6067 if (hdr) {
6068 sup_hdr = (pjsip_supported_hdr*)
6069 pjsip_hdr_clone(tdata->pool, hdr);
6070 pjsip_msg_add_hdr(tdata->msg, (pjsip_hdr*)sup_hdr);
6071 }
6072 }
6073
6074 /* Cleanup the Supported headers from disabled extensions */
6075 cleanup_allow_sup_hdr(inv->options, NULL, allow_hdr, sup_hdr);
6076 }
6077
6078 /* Send 2xx regardless of the status of negotiation */
6079 status = pjsip_inv_send_msg(inv, tdata);
6080
6081 } else if (tsx->state == PJSIP_TSX_STATE_CONFIRMED) {
6082 /* This is the case where ACK has the same branch as
6083 * the INVITE request.
6084 */
6085 if (tsx->status_code/100 == 2 &&
6086 e->body.tsx_state.type == PJSIP_EVENT_RX_MSG)
6087 {
6088 inv_check_sdp_in_incoming_msg(inv, tsx,
6089 e->body.tsx_state.src.rdata);
6090
6091 /* Check if local offer got no SDP answer */
6092 if (pjmedia_sdp_neg_get_state(inv->neg)==
6093 PJMEDIA_SDP_NEG_STATE_LOCAL_OFFER)
6094 {
6095 pjmedia_sdp_neg_cancel_offer(inv->neg);
6096 }
6097 }
6098
6099 }
6100
6101 }
6102 else if (tsx->method.id == PJSIP_INVITE_METHOD &&
6103 tsx->role == PJSIP_ROLE_UAC)
6104 {
6105
6106 /*
6107 * Handle outgoing re-INVITE
6108 */
6109 if (tsx->state == PJSIP_TSX_STATE_CALLING) {
6110
6111 /* Must not have other pending INVITE transaction */
6112 pj_assert(inv->invite_tsx==NULL || tsx==inv->invite_tsx);
6113
6114 /* Save pending invite transaction */
6115 inv->invite_tsx = tsx;
6116
6117 } else if (tsx->state == PJSIP_TSX_STATE_PROCEEDING) {
6118
6119 /* CANCEL the re-INVITE if necessary */
6120 if (inv->pending_cancel) {
6121 pj_status_t status;
6122 pjsip_tx_data *cancel;
6123
6124 inv->pending_cancel = PJ_FALSE;
6125
6126 status = pjsip_inv_cancel_reinvite(inv, &cancel);
6127 if (status == PJ_SUCCESS && cancel)
6128 status = pjsip_inv_send_msg(inv, cancel);
6129 }
6130
6131 } else if (tsx->state == PJSIP_TSX_STATE_TERMINATED &&
6132 tsx->status_code/100 == 2)
6133 {
6134 pj_status_t status;
6135
6136 /* Re-INVITE was accepted. */
6137
6138 /* Check routing URI scheme for secure dialog */
6139 if (!inv_check_secure_dlg(inv, e))
6140 return;
6141
6142 /* Process session timer response. */
6143 status = handle_timer_response(inv,
6144 e->body.tsx_state.src.rdata,
6145 PJ_TRUE);
6146 if (status != PJ_SUCCESS)
6147 return;
6148
6149 /* Process SDP */
6150 inv_check_sdp_in_incoming_msg(inv, tsx,
6151 e->body.tsx_state.src.rdata);
6152
6153 /* Check if local offer got no SDP answer */
6154 if (pjmedia_sdp_neg_get_state(inv->neg)==
6155 PJMEDIA_SDP_NEG_STATE_LOCAL_OFFER)
6156 {
6157 pjmedia_sdp_neg_cancel_offer(inv->neg);
6158 }
6159
6160 /* Send ACK */
6161 inv_send_ack(inv, e);
6162
6163 } else if (handle_uac_tsx_response(inv, e)) {
6164
6165 /* Handle response that terminates dialog */
6166 /* Nothing to do (already handled) */
6167
6168 } else if (tsx->status_code >= 300 && tsx->status_code < 700 &&
6169 e->body.tsx_state.prev_state != PJSIP_TSX_STATE_COMPLETED)
6170 {
6171 /* Ticket #1654: do not cancel SDP offer when tsx state changing
6172 * from 'completed' to 'terminated', as it should have already
6173 * been cancelled when tsx state is 'completed'.
6174 */
6175
6176 pjmedia_sdp_neg_state neg_state;
6177 struct tsx_inv_data *tsx_inv_data;
6178
6179 tsx_inv_data = (struct tsx_inv_data*)tsx->mod_data[mod_inv.mod.id];
6180
6181 /* Outgoing INVITE transaction has failed, cancel SDP nego */
6182 neg_state = pjmedia_sdp_neg_get_state(inv->neg);
6183 if (neg_state == PJMEDIA_SDP_NEG_STATE_LOCAL_OFFER &&
6184 tsx_inv_data->retrying == PJ_FALSE)
6185 {
6186 pjmedia_sdp_neg_cancel_offer(inv->neg);
6187 }
6188
6189 if (tsx == inv->invite_tsx)
6190 inv->invite_tsx = NULL;
6191
6192 /* Process 502/503 error for session timer refresh */
6193 if (tsx->status_code == 503 || tsx->status_code == 502) {
6194 pjsip_timer_handle_refresh_error(inv, e);
6195 }
6196 }
6197
6198 } else if (tsx->role == PJSIP_ROLE_UAS &&
6199 tsx->state == PJSIP_TSX_STATE_TRYING &&
6200 pjsip_method_cmp(&tsx->method, &pjsip_update_method)==0)
6201 {
6202 /*
6203 * Handle incoming UPDATE
6204 */
6205 inv_respond_incoming_update(inv, e);
6206
6207 } else if (tsx->role == PJSIP_ROLE_UAC &&
6208 (tsx->state == PJSIP_TSX_STATE_COMPLETED ||
6209 tsx->state == PJSIP_TSX_STATE_TERMINATED) &&
6210 pjsip_method_cmp(&tsx->method, &pjsip_update_method)==0)
6211 {
6212 /*
6213 * Handle response to outgoing UPDATE request.
6214 */
6215 if (inv_handle_update_response(inv, e) == PJ_FALSE)
6216 handle_uac_tsx_response(inv, e);
6217
6218 } else if (tsx->role == PJSIP_ROLE_UAS &&
6219 tsx->state == PJSIP_TSX_STATE_TRYING &&
6220 pjsip_method_cmp(&tsx->method, &pjsip_prack_method)==0)
6221 {
6222 /*
6223 * Handle strandled incoming PRACK
6224 */
6225 inv_respond_incoming_prack(inv, e->body.tsx_state.src.rdata);
6226
6227 } else if (tsx->role == PJSIP_ROLE_UAC) {
6228 /*
6229 * Handle 401/407/408/481/422 response
6230 */
6231 handle_uac_tsx_response(inv, e);
6232 }
6233
6234 }
6235
6236 /*
6237 * After session has been terminated, but before dialog is destroyed
6238 * (because dialog has other usages, or because dialog is waiting for
6239 * the last transaction to terminate).
6240 */
6241 static void inv_on_state_disconnected( pjsip_inv_session *inv, pjsip_event *e)
6242 {
6243 pjsip_transaction *tsx = e->body.tsx_state.tsx;
6244 pjsip_dialog *dlg = pjsip_tsx_get_dlg(tsx);
6245
6246 PJ_ASSERT_ON_FAIL(tsx && dlg, return);
6247
6248 if (tsx->role == PJSIP_ROLE_UAS &&
6249 tsx->status_code < 200 &&
6250 e->body.tsx_state.type == PJSIP_EVENT_RX_MSG)
6251 {
6252 pjsip_rx_data *rdata = e->body.tsx_state.src.rdata;
6253
6254 /*
6255 * Respond BYE with 200/OK
6256 */
6257 if (tsx->method.id == PJSIP_BYE_METHOD) {
6258 inv_respond_incoming_bye( inv, tsx, rdata, e );
6259 } else if (tsx->method.id == PJSIP_CANCEL_METHOD) {
6260 /*
6261 * Respond CANCEL with 200/OK too.
6262 */
6263 pjsip_tx_data *tdata;
6264 pj_status_t status;
6265
6266 status = pjsip_dlg_create_response(dlg, rdata, 200, NULL, &tdata);
6267 if (status != PJ_SUCCESS) return;
6268
6269 status = pjsip_dlg_send_response(dlg, tsx, tdata);
6270 if (status != PJ_SUCCESS) return;
6271
6272 }
6273
6274 } else if (tsx->role == PJSIP_ROLE_UAC) {
6275 /*
6276 * Handle 401/407/408/481/422 response
6277 */
6278 handle_uac_tsx_response(inv, e);
6279 }
6280 }
6281