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