1
/* $Id: sip_inv.c 3553 2011-05-05 06:14:19Z nanang $ */
3
* Copyright (C) 2008-2011 Teluu Inc. (http://www.teluu.com)
4
* Copyright (C) 2003-2008 Benny Prijono <benny@prijono.org>
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.
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.
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
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>
33
#include <pj/assert.h>
39
* Note on offer/answer:
41
* The offer/answer framework in this implementation assumes the occurence
42
* of SDP in a particular request/response according to this table:
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
51
PRACK X PRACK can only contain answer
52
2xx/PRACK Response may not have offer nor answer
54
UPDATE X UPDATE may only contain offer
55
2xx/UPDATE X Response may only contain answer
56
========================================================================
61
#define THIS_FILE "sip_inv.c"
63
static const char *inv_state_names[] =
76
static const pjsip_method pjsip_update_method =
82
#define POOL_INIT_SIZE 256
83
#define POOL_INC_SIZE 256
88
static pj_status_t mod_inv_load(pjsip_endpoint *endpt);
89
static pj_status_t mod_inv_unload(void);
90
static pj_bool_t mod_inv_on_rx_request(pjsip_rx_data *rdata);
91
static pj_bool_t mod_inv_on_rx_response(pjsip_rx_data *rdata);
92
static void mod_inv_on_tsx_state(pjsip_transaction*, pjsip_event*);
94
static void inv_on_state_null( pjsip_inv_session *inv, pjsip_event *e);
95
static void inv_on_state_calling( pjsip_inv_session *inv, pjsip_event *e);
96
static void inv_on_state_incoming( pjsip_inv_session *inv, pjsip_event *e);
97
static void inv_on_state_early( pjsip_inv_session *inv, pjsip_event *e);
98
static void inv_on_state_connecting( pjsip_inv_session *inv, pjsip_event *e);
99
static void inv_on_state_confirmed( pjsip_inv_session *inv, pjsip_event *e);
100
static void inv_on_state_disconnected( pjsip_inv_session *inv, pjsip_event *e);
102
static pj_status_t inv_check_sdp_in_incoming_msg( pjsip_inv_session *inv,
103
pjsip_transaction *tsx,
104
pjsip_rx_data *rdata);
105
static pj_status_t inv_negotiate_sdp( pjsip_inv_session *inv );
106
static pjsip_msg_body *create_sdp_body(pj_pool_t *pool,
107
const pjmedia_sdp_session *c_sdp);
108
static pj_status_t process_answer( pjsip_inv_session *inv,
110
pjsip_tx_data *tdata,
111
const pjmedia_sdp_session *local_sdp);
113
static pj_status_t handle_timer_response(pjsip_inv_session *inv,
114
const pjsip_rx_data *rdata,
115
pj_bool_t end_sess_on_failure);
117
static void (*inv_state_handler[])( pjsip_inv_session *inv, pjsip_event *e) =
120
&inv_on_state_calling,
121
&inv_on_state_incoming,
123
&inv_on_state_connecting,
124
&inv_on_state_confirmed,
125
&inv_on_state_disconnected,
128
static struct mod_inv
131
pjsip_endpoint *endpt;
132
pjsip_inv_callback cb;
136
NULL, NULL, /* prev, next. */
137
{ "mod-invite", 10 }, /* Name. */
139
PJSIP_MOD_PRIORITY_DIALOG_USAGE, /* Priority */
140
&mod_inv_load, /* load() */
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() */
153
/* Invite session data to be attached to transaction. */
156
pjsip_inv_session *inv; /* The invite session */
157
pj_bool_t sdp_done; /* SDP negotiation done for this tsx? */
158
pj_str_t done_tag; /* To tag in RX response with answer */
159
pj_bool_t done_early;/* Negotiation was done for early med? */
165
static pj_status_t mod_inv_load(pjsip_endpoint *endpt)
167
pj_str_t allowed[] = {{"INVITE", 6}, {"ACK",3}, {"BYE",3}, {"CANCEL",6},
169
pj_str_t accepted = { "application/sdp", 15 };
171
/* Register supported methods: INVITE, ACK, BYE, CANCEL, UPDATE */
172
pjsip_endpt_add_capability(endpt, &mod_inv.mod, PJSIP_H_ALLOW, NULL,
173
PJ_ARRAY_SIZE(allowed), allowed);
175
/* Register "application/sdp" in Accept header */
176
pjsip_endpt_add_capability(endpt, &mod_inv.mod, PJSIP_H_ACCEPT, NULL,
185
static pj_status_t mod_inv_unload(void)
187
/* Should remove capability here */
194
void inv_set_state(pjsip_inv_session *inv, pjsip_inv_state state,
197
pjsip_inv_state prev_state = inv->state;
201
/* If state is confirmed, check that SDP negotiation is done,
202
* otherwise disconnect the session.
204
if (state == PJSIP_INV_STATE_CONFIRMED) {
205
if (pjmedia_sdp_neg_get_state(inv->neg)!=PJMEDIA_SDP_NEG_STATE_DONE) {
208
PJ_LOG(4,(inv->obj_name, "SDP offer/answer incomplete, ending the "
211
status = pjsip_inv_end_session(inv, PJSIP_SC_NOT_ACCEPTABLE,
213
if (status == PJ_SUCCESS && bye)
214
status = pjsip_inv_send_msg(inv, bye);
223
/* If state is DISCONNECTED, cause code MUST have been set. */
224
pj_assert(inv->state != PJSIP_INV_STATE_DISCONNECTED ||
227
/* Call on_state_changed() callback. */
228
if (mod_inv.cb.on_state_changed && inv->notify)
229
(*mod_inv.cb.on_state_changed)(inv, e);
231
/* Only decrement when previous state is not already DISCONNECTED */
232
if (inv->state == PJSIP_INV_STATE_DISCONNECTED &&
233
prev_state != PJSIP_INV_STATE_DISCONNECTED)
236
pjsip_tx_data_dec_ref(inv->last_ack);
237
inv->last_ack = NULL;
239
if (inv->invite_req) {
240
pjsip_tx_data_dec_ref(inv->invite_req);
241
inv->invite_req = NULL;
243
pjsip_100rel_end_session(inv);
244
pjsip_timer_end_session(inv);
245
pjsip_dlg_dec_session(inv->dlg, &mod_inv.mod);
247
/* Release the flip-flop pools */
248
pj_pool_release(inv->pool_prov);
249
inv->pool_prov = NULL;
250
pj_pool_release(inv->pool_active);
251
inv->pool_active = NULL;
259
void inv_set_cause(pjsip_inv_session *inv, int cause_code,
260
const pj_str_t *cause_text)
262
if (cause_code > inv->cause) {
263
inv->cause = (pjsip_status_code) cause_code;
265
pj_strdup(inv->pool, &inv->cause_text, cause_text);
266
else if (cause_code/100 == 2)
267
inv->cause_text = pj_str("Normal call clearing");
269
inv->cause_text = *pjsip_get_status_text(cause_code);
275
* Check if outgoing request needs to have SDP answer.
276
* This applies for both ACK and PRACK requests.
278
static const pjmedia_sdp_session *inv_has_pending_answer(pjsip_inv_session *inv,
279
pjsip_transaction *tsx)
281
pjmedia_sdp_neg_state neg_state;
282
const pjmedia_sdp_session *sdp = NULL;
285
/* If SDP negotiator is ready, start negotiation. */
287
/* Start nego when appropriate. */
288
neg_state = inv->neg ? pjmedia_sdp_neg_get_state(inv->neg) :
289
PJMEDIA_SDP_NEG_STATE_NULL;
291
if (neg_state == PJMEDIA_SDP_NEG_STATE_DONE) {
295
} else if (neg_state == PJMEDIA_SDP_NEG_STATE_WAIT_NEGO &&
296
pjmedia_sdp_neg_has_local_answer(inv->neg) )
298
struct tsx_inv_data *tsx_inv_data;
299
struct tsx_inv_data dummy;
301
/* Get invite session's transaction data.
302
* Note that tsx may be NULL, for example when application sends
303
* delayed ACK request (at this time, the original INVITE
304
* transaction may have been destroyed.
307
tsx_inv_data = (struct tsx_inv_data*)tsx->mod_data[mod_inv.mod.id];
309
tsx_inv_data = &dummy;
310
pj_bzero(&dummy, sizeof(dummy));
314
status = inv_negotiate_sdp(inv);
315
if (status != PJ_SUCCESS)
318
/* Mark this transaction has having SDP offer/answer done. */
319
tsx_inv_data->sdp_done = 1;
321
status = pjmedia_sdp_neg_get_active_local(inv->neg, &sdp);
324
/* This remark is only valid for ACK.
325
PJ_LOG(4,(inv->dlg->obj_name,
326
"FYI, the SDP negotiator state (%s) is in a mess "
327
"when sending this ACK/PRACK request",
328
pjmedia_sdp_neg_state_str(neg_state)));
337
* Send ACK for 2xx response.
339
static pj_status_t inv_send_ack(pjsip_inv_session *inv, pjsip_event *e)
341
pjsip_rx_data *rdata;
344
if (e->type == PJSIP_EVENT_TSX_STATE)
345
rdata = e->body.tsx_state.src.rdata;
346
else if (e->type == PJSIP_EVENT_RX_MSG)
347
rdata = e->body.rx_msg.rdata;
349
pj_assert(!"Unsupported event type");
353
PJ_LOG(5,(inv->obj_name, "Received %s, sending ACK",
354
pjsip_rx_data_get_info(rdata)));
356
/* Check if we have cached ACK request. Must not use the cached ACK
357
* if it's still marked as pending by transport (#1011)
359
if (inv->last_ack && rdata->msg_info.cseq->cseq == inv->last_ack_cseq &&
360
!inv->last_ack->is_pending)
362
pjsip_tx_data_add_ref(inv->last_ack);
364
} else if (mod_inv.cb.on_send_ack) {
365
/* If application handles ACK transmission manually, just notify the
368
PJ_LOG(5,(inv->obj_name, "Received %s, notifying application callback",
369
pjsip_rx_data_get_info(rdata)));
371
(*mod_inv.cb.on_send_ack)(inv, rdata);
375
status = pjsip_inv_create_ack(inv, rdata->msg_info.cseq->cseq,
380
status = pjsip_dlg_send_request(inv->dlg, inv->last_ack, -1, NULL);
381
if (status != PJ_SUCCESS) {
382
/* Better luck next time */
383
pj_assert(!"Unable to send ACK!");
388
/* Set state to CONFIRMED (if we're not in CONFIRMED yet).
389
* But don't set it to CONFIRMED if we're already DISCONNECTED
390
* (this may have been a late 200/OK response.
392
if (inv->state < PJSIP_INV_STATE_CONFIRMED) {
393
inv_set_state(inv, PJSIP_INV_STATE_CONFIRMED, e);
400
* Module on_rx_request()
402
* This callback is called for these events:
403
* - endpoint receives request which was unhandled by higher priority
404
* modules (e.g. transaction layer, dialog layer).
405
* - dialog distributes incoming request to its usages.
407
static pj_bool_t mod_inv_on_rx_request(pjsip_rx_data *rdata)
409
pjsip_method *method;
411
pjsip_inv_session *inv;
413
/* Only wants to receive request from a dialog. */
414
dlg = pjsip_rdata_get_dlg(rdata);
418
inv = (pjsip_inv_session*) dlg->mod_data[mod_inv.mod.id];
420
/* Report to dialog that we handle INVITE, CANCEL, BYE, ACK.
421
* If we need to send response, it will be sent in the state
424
method = &rdata->msg_info.msg->line.req.method;
426
if (method->id == PJSIP_INVITE_METHOD) {
430
/* BYE and CANCEL must have existing invite session */
431
if (method->id == PJSIP_BYE_METHOD ||
432
method->id == PJSIP_CANCEL_METHOD)
440
/* On receipt ACK request, when state is CONNECTING,
441
* move state to CONFIRMED.
443
if (method->id == PJSIP_ACK_METHOD && inv) {
445
/* Ignore ACK if pending INVITE transaction has not finished. */
446
if (inv->invite_tsx &&
447
inv->invite_tsx->state < PJSIP_TSX_STATE_COMPLETED)
452
/* Terminate INVITE transaction, if it's still present. */
453
if (inv->invite_tsx &&
454
inv->invite_tsx->state <= PJSIP_TSX_STATE_COMPLETED)
456
/* Before we terminate INVITE transaction, process the SDP
457
* in the ACK request, if any.
458
* Only do this when invite state is not already disconnected
459
* (http://trac.pjsip.org/repos/ticket/640).
461
if (inv->state < PJSIP_INV_STATE_DISCONNECTED) {
462
inv_check_sdp_in_incoming_msg(inv, inv->invite_tsx, rdata);
464
/* Check if local offer got no SDP answer and INVITE session
465
* is in CONFIRMED state.
467
if (pjmedia_sdp_neg_get_state(inv->neg)==
468
PJMEDIA_SDP_NEG_STATE_LOCAL_OFFER &&
469
inv->state==PJSIP_INV_STATE_CONFIRMED)
471
pjmedia_sdp_neg_cancel_offer(inv->neg);
475
/* Now we can terminate the INVITE transaction */
476
pj_assert(inv->invite_tsx->status_code >= 200);
477
pjsip_tsx_terminate(inv->invite_tsx,
478
inv->invite_tsx->status_code);
479
inv->invite_tsx = NULL;
480
if (inv->last_answer) {
481
pjsip_tx_data_dec_ref(inv->last_answer);
482
inv->last_answer = NULL;
486
/* On receipt of ACK, only set state to confirmed when state
487
* is CONNECTING (e.g. we don't want to set the state to confirmed
488
* when we receive ACK retransmission after sending non-2xx!)
490
if (inv->state == PJSIP_INV_STATE_CONNECTING) {
493
PJSIP_EVENT_INIT_RX_MSG(event, rdata);
494
inv_set_state(inv, PJSIP_INV_STATE_CONFIRMED, &event);
501
/* This function will process Session Timer headers in received
502
* 2xx or 422 response of INVITE/UPDATE request.
504
static pj_status_t handle_timer_response(pjsip_inv_session *inv,
505
const pjsip_rx_data *rdata,
506
pj_bool_t end_sess_on_failure)
508
pjsip_status_code st_code;
511
status = pjsip_timer_process_resp(inv, rdata, &st_code);
512
if (status != PJ_SUCCESS && end_sess_on_failure) {
513
pjsip_tx_data *tdata;
516
status2 = pjsip_inv_end_session(inv, st_code, NULL, &tdata);
517
if (tdata && status2 == PJ_SUCCESS)
518
pjsip_inv_send_msg(inv, tdata);
525
* Module on_rx_response().
527
* This callback is called for these events:
528
* - dialog distributes incoming 2xx response to INVITE (outside
529
* transaction) to its usages.
530
* - endpoint distributes strayed responses.
532
static pj_bool_t mod_inv_on_rx_response(pjsip_rx_data *rdata)
535
pjsip_inv_session *inv;
536
pjsip_msg *msg = rdata->msg_info.msg;
538
dlg = pjsip_rdata_get_dlg(rdata);
540
/* Ignore responses outside dialog */
544
/* Ignore responses not belonging to invite session */
545
inv = pjsip_dlg_get_inv_session(dlg);
549
/* This MAY be retransmission of 2xx response to INVITE.
550
* If it is, we need to send ACK.
552
if (msg->type == PJSIP_RESPONSE_MSG && msg->line.status.code/100==2 &&
553
rdata->msg_info.cseq->method.id == PJSIP_INVITE_METHOD &&
554
inv->invite_tsx == NULL)
558
PJSIP_EVENT_INIT_RX_MSG(e, rdata);
559
inv_send_ack(inv, &e);
564
/* No other processing needs to be done here. */
569
* Module on_tsx_state()
571
* This callback is called by dialog framework for all transactions
572
* inside the dialog for all its dialog usages.
574
static void mod_inv_on_tsx_state(pjsip_transaction *tsx, pjsip_event *e)
577
pjsip_inv_session *inv;
579
dlg = pjsip_tsx_get_dlg(tsx);
583
inv = pjsip_dlg_get_inv_session(dlg);
587
/* Call state handler for the invite session. */
588
(*inv_state_handler[inv->state])(inv, e);
590
/* Call on_tsx_state */
591
if (mod_inv.cb.on_tsx_state_changed && inv->notify)
592
(*mod_inv.cb.on_tsx_state_changed)(inv, tsx, e);
594
/* Clear invite transaction when tsx is confirmed.
595
* Previously we set invite_tsx to NULL only when transaction has
596
* terminated, but this didn't work when ACK has the same Via branch
597
* value as the INVITE (see http://www.pjsip.org/trac/ticket/113)
599
if (tsx->state>=PJSIP_TSX_STATE_CONFIRMED && tsx == inv->invite_tsx) {
600
inv->invite_tsx = NULL;
601
if (inv->last_answer) {
602
pjsip_tx_data_dec_ref(inv->last_answer);
603
inv->last_answer = NULL;
610
* Initialize the invite module.
612
PJ_DEF(pj_status_t) pjsip_inv_usage_init( pjsip_endpoint *endpt,
613
const pjsip_inv_callback *cb)
617
/* Check arguments. */
618
PJ_ASSERT_RETURN(endpt && cb, PJ_EINVAL);
620
/* Some callbacks are mandatory */
621
PJ_ASSERT_RETURN(cb->on_state_changed && cb->on_new_session, PJ_EINVAL);
623
/* Check if module already registered. */
624
PJ_ASSERT_RETURN(mod_inv.mod.id == -1, PJ_EINVALIDOP);
627
pj_memcpy(&mod_inv.cb, cb, sizeof(pjsip_inv_callback));
629
mod_inv.endpt = endpt;
631
/* Register the module. */
632
status = pjsip_endpt_register_module(endpt, &mod_inv.mod);
633
if (status != PJ_SUCCESS)
640
* Get the instance of invite module.
642
PJ_DEF(pjsip_module*) pjsip_inv_usage_instance(void)
650
* Return the invite session for the specified dialog.
652
PJ_DEF(pjsip_inv_session*) pjsip_dlg_get_inv_session(pjsip_dialog *dlg)
654
return (pjsip_inv_session*) dlg->mod_data[mod_inv.mod.id];
659
* Get INVITE state name.
661
PJ_DEF(const char *) pjsip_inv_state_name(pjsip_inv_state state)
663
PJ_ASSERT_RETURN(state >= PJSIP_INV_STATE_NULL &&
664
state <= PJSIP_INV_STATE_DISCONNECTED,
667
return inv_state_names[state];
671
* Create UAC invite session.
673
PJ_DEF(pj_status_t) pjsip_inv_create_uac( pjsip_dialog *dlg,
674
const pjmedia_sdp_session *local_sdp,
676
pjsip_inv_session **p_inv)
678
pjsip_inv_session *inv;
681
/* Verify arguments. */
682
PJ_ASSERT_RETURN(dlg && p_inv, PJ_EINVAL);
684
/* Must lock dialog first */
685
pjsip_dlg_inc_lock(dlg);
687
/* Normalize options */
688
if (options & PJSIP_INV_REQUIRE_100REL)
689
options |= PJSIP_INV_SUPPORT_100REL;
690
if (options & PJSIP_INV_REQUIRE_TIMER)
691
options |= PJSIP_INV_SUPPORT_TIMER;
693
/* Create the session */
694
inv = PJ_POOL_ZALLOC_T(dlg->pool, pjsip_inv_session);
695
pj_assert(inv != NULL);
697
inv->pool = dlg->pool;
698
inv->role = PJSIP_ROLE_UAC;
699
inv->state = PJSIP_INV_STATE_NULL;
701
inv->options = options;
702
inv->notify = PJ_TRUE;
703
inv->cause = (pjsip_status_code) 0;
705
/* Create flip-flop pool (see ticket #877) */
706
/* (using inv->obj_name as temporary variable for pool names */
707
pj_ansi_snprintf(inv->obj_name, PJ_MAX_OBJ_NAME, "inv%p", dlg->pool);
708
inv->pool_prov = pjsip_endpt_create_pool(dlg->endpt, inv->obj_name,
709
POOL_INIT_SIZE, POOL_INC_SIZE);
710
inv->pool_active = pjsip_endpt_create_pool(dlg->endpt, inv->obj_name,
711
POOL_INIT_SIZE, POOL_INC_SIZE);
713
/* Object name will use the same dialog pointer. */
714
pj_ansi_snprintf(inv->obj_name, PJ_MAX_OBJ_NAME, "inv%p", dlg);
716
/* Create negotiator if local_sdp is specified. */
718
status = pjmedia_sdp_neg_create_w_local_offer(inv->pool,
719
local_sdp, &inv->neg);
720
if (status != PJ_SUCCESS) {
721
pjsip_dlg_dec_lock(dlg);
726
/* Register invite as dialog usage. */
727
status = pjsip_dlg_add_usage(dlg, &mod_inv.mod, inv);
728
if (status != PJ_SUCCESS) {
729
pjsip_dlg_dec_lock(dlg);
733
/* Increment dialog session */
734
pjsip_dlg_inc_session(dlg, &mod_inv.mod);
736
/* Create 100rel handler */
737
pjsip_100rel_attach(inv);
742
pjsip_dlg_dec_lock(dlg);
744
PJ_LOG(5,(inv->obj_name, "UAC invite session created for dialog %s",
750
PJ_DEF(pjsip_rdata_sdp_info*) pjsip_rdata_get_sdp_info(pjsip_rx_data *rdata)
752
pjsip_rdata_sdp_info *sdp_info;
753
pjsip_msg_body *body = rdata->msg_info.msg->body;
754
pjsip_ctype_hdr *ctype_hdr = rdata->msg_info.ctype;
755
pjsip_media_type app_sdp;
757
sdp_info = (pjsip_rdata_sdp_info*)
758
rdata->endpt_info.mod_data[mod_inv.mod.id];
762
sdp_info = PJ_POOL_ZALLOC_T(rdata->tp_info.pool,
763
pjsip_rdata_sdp_info);
764
PJ_ASSERT_RETURN(mod_inv.mod.id >= 0, sdp_info);
765
rdata->endpt_info.mod_data[mod_inv.mod.id] = sdp_info;
767
pjsip_media_type_init2(&app_sdp, "application", "sdp");
769
if (body && ctype_hdr &&
770
pj_stricmp(&ctype_hdr->media.type, &app_sdp.type)==0 &&
771
pj_stricmp(&ctype_hdr->media.subtype, &app_sdp.subtype)==0)
773
sdp_info->body.ptr = (char*)body->data;
774
sdp_info->body.slen = body->len;
775
} else if (body && ctype_hdr &&
776
pj_stricmp2(&ctype_hdr->media.type, "multipart")==0 &&
777
(pj_stricmp2(&ctype_hdr->media.subtype, "mixed")==0 ||
778
pj_stricmp2(&ctype_hdr->media.subtype, "alternative")==0))
780
pjsip_multipart_part *part;
782
part = pjsip_multipart_find_part(body, &app_sdp, NULL);
784
sdp_info->body.ptr = (char*)part->body->data;
785
sdp_info->body.slen = part->body->len;
789
if (sdp_info->body.ptr) {
791
status = pjmedia_sdp_parse(rdata->tp_info.pool,
795
if (status == PJ_SUCCESS)
796
status = pjmedia_sdp_validate(sdp_info->sdp);
798
if (status != PJ_SUCCESS) {
799
sdp_info->sdp = NULL;
800
PJ_PERROR(1,(THIS_FILE, status,
801
"Error parsing/validating SDP body"));
804
sdp_info->sdp_err = status;
812
* Verify incoming INVITE request.
814
PJ_DEF(pj_status_t) pjsip_inv_verify_request2(pjsip_rx_data *rdata,
816
const pjmedia_sdp_session *r_sdp,
817
const pjmedia_sdp_session *l_sdp,
819
pjsip_endpoint *endpt,
820
pjsip_tx_data **p_tdata)
823
pjsip_allow_hdr *allow;
824
pjsip_supported_hdr *sup_hdr;
825
pjsip_require_hdr *req_hdr;
826
pjsip_contact_hdr *c_hdr;
828
unsigned rem_option = 0;
829
pj_status_t status = PJ_SUCCESS;
830
pjsip_hdr res_hdr_list;
831
pjsip_rdata_sdp_info *sdp_info;
833
/* Init return arguments. */
834
if (p_tdata) *p_tdata = NULL;
836
/* Verify arguments. */
837
PJ_ASSERT_RETURN(rdata != NULL && options != NULL, PJ_EINVAL);
839
/* Normalize options */
840
if (*options & PJSIP_INV_REQUIRE_100REL)
841
*options |= PJSIP_INV_SUPPORT_100REL;
842
if (*options & PJSIP_INV_REQUIRE_TIMER)
843
*options |= PJSIP_INV_SUPPORT_TIMER;
844
if (*options & PJSIP_INV_REQUIRE_ICE)
845
*options |= PJSIP_INV_SUPPORT_ICE;
847
/* Get the message in rdata */
848
msg = rdata->msg_info.msg;
850
/* Must be INVITE request. */
851
PJ_ASSERT_RETURN(msg->type == PJSIP_REQUEST_MSG &&
852
msg->line.req.method.id == PJSIP_INVITE_METHOD,
855
/* If tdata is specified, then either dlg or endpt must be specified */
856
PJ_ASSERT_RETURN((!p_tdata) || (endpt || dlg), PJ_EINVAL);
858
/* Get the endpoint */
859
endpt = endpt ? endpt : dlg->endpt;
861
/* Init response header list */
862
pj_list_init(&res_hdr_list);
864
/* Check the Contact header */
865
c_hdr = (pjsip_contact_hdr*)
866
pjsip_msg_find_hdr(msg, PJSIP_H_CONTACT, NULL);
867
if (!c_hdr || !c_hdr->uri) {
868
/* Missing Contact header or Contact contains "*" */
869
pjsip_warning_hdr *w;
872
warn_text = pj_str("Bad/missing Contact header");
873
w = pjsip_warning_hdr_create(rdata->tp_info.pool, 399,
874
pjsip_endpt_name(endpt),
877
pj_list_push_back(&res_hdr_list, w);
880
code = PJSIP_SC_BAD_REQUEST;
881
status = PJSIP_ERRNO_FROM_SIP_STATUS(code);
885
/* Check the request body, see if it's something that we support,
886
* only when the body hasn't been parsed before.
889
sdp_info = pjsip_rdata_get_sdp_info(rdata);
894
if (r_sdp==NULL && msg->body) {
896
/* Check if body really contains SDP. */
897
if (sdp_info->body.ptr == NULL) {
898
/* Couldn't find "application/sdp" */
899
code = PJSIP_SC_UNSUPPORTED_MEDIA_TYPE;
900
status = PJSIP_ERRNO_FROM_SIP_STATUS(code);
903
/* Add Accept header to response */
904
pjsip_accept_hdr *acc;
906
acc = pjsip_accept_hdr_create(rdata->tp_info.pool);
907
PJ_ASSERT_RETURN(acc, PJ_ENOMEM);
908
acc->values[acc->count++] = pj_str("application/sdp");
909
pj_list_push_back(&res_hdr_list, acc);
915
if (sdp_info->sdp_err != PJ_SUCCESS) {
916
/* Unparseable or invalid SDP */
917
code = PJSIP_SC_BAD_REQUEST;
920
/* Add Warning header. */
921
pjsip_warning_hdr *w;
923
w = pjsip_warning_hdr_create_from_status(rdata->tp_info.pool,
924
pjsip_endpt_name(endpt),
926
PJ_ASSERT_RETURN(w, PJ_ENOMEM);
928
pj_list_push_back(&res_hdr_list, w);
934
r_sdp = sdp_info->sdp;
938
/* Negotiate with local SDP */
940
pjmedia_sdp_neg *neg;
942
/* Local SDP must be valid! */
943
PJ_ASSERT_RETURN((status=pjmedia_sdp_validate(l_sdp))==PJ_SUCCESS,
946
/* Create SDP negotiator */
947
status = pjmedia_sdp_neg_create_w_remote_offer(
948
rdata->tp_info.pool, l_sdp, r_sdp, &neg);
949
PJ_ASSERT_RETURN(status == PJ_SUCCESS, status);
952
status = pjmedia_sdp_neg_negotiate(rdata->tp_info.pool, neg, 0);
953
if (status != PJ_SUCCESS) {
955
/* Incompatible media */
956
code = PJSIP_SC_NOT_ACCEPTABLE_HERE;
959
pjsip_accept_hdr *acc;
960
pjsip_warning_hdr *w;
962
/* Add Warning header. */
963
w = pjsip_warning_hdr_create_from_status(
965
pjsip_endpt_name(endpt), status);
966
PJ_ASSERT_RETURN(w, PJ_ENOMEM);
968
pj_list_push_back(&res_hdr_list, w);
970
/* Add Accept header to response */
971
acc = pjsip_accept_hdr_create(rdata->tp_info.pool);
972
PJ_ASSERT_RETURN(acc, PJ_ENOMEM);
973
acc->values[acc->count++] = pj_str("application/sdp");
974
pj_list_push_back(&res_hdr_list, acc);
983
/* Check supported methods, see if peer supports UPDATE.
984
* We just assume that peer supports standard INVITE, ACK, CANCEL, and BYE
985
* implicitly by sending this INVITE.
987
allow = (pjsip_allow_hdr*) pjsip_msg_find_hdr(msg, PJSIP_H_ALLOW, NULL);
990
const pj_str_t STR_UPDATE = { "UPDATE", 6 };
992
for (i=0; i<allow->count; ++i) {
993
if (pj_stricmp(&allow->values[i], &STR_UPDATE)==0)
997
if (i != allow->count) {
998
/* UPDATE is present in Allow */
999
rem_option |= PJSIP_INV_SUPPORT_UPDATE;
1004
/* Check Supported header */
1005
sup_hdr = (pjsip_supported_hdr*)
1006
pjsip_msg_find_hdr(msg, PJSIP_H_SUPPORTED, NULL);
1009
const pj_str_t STR_100REL = { "100rel", 6};
1010
const pj_str_t STR_TIMER = { "timer", 5};
1011
const pj_str_t STR_ICE = { "ice", 3 };
1013
for (i=0; i<sup_hdr->count; ++i) {
1014
if (pj_stricmp(&sup_hdr->values[i], &STR_100REL)==0)
1015
rem_option |= PJSIP_INV_SUPPORT_100REL;
1016
else if (pj_stricmp(&sup_hdr->values[i], &STR_TIMER)==0)
1017
rem_option |= PJSIP_INV_SUPPORT_TIMER;
1018
else if (pj_stricmp(&sup_hdr->values[i], &STR_ICE)==0)
1019
rem_option |= PJSIP_INV_SUPPORT_ICE;
1023
/* Check Require header */
1024
req_hdr = (pjsip_require_hdr*)
1025
pjsip_msg_find_hdr(msg, PJSIP_H_REQUIRE, NULL);
1028
const pj_str_t STR_100REL = { "100rel", 6};
1029
const pj_str_t STR_REPLACES = { "replaces", 8 };
1030
const pj_str_t STR_TIMER = { "timer", 5 };
1031
const pj_str_t STR_ICE = { "ice", 3 };
1032
unsigned unsupp_cnt = 0;
1033
pj_str_t unsupp_tags[PJSIP_GENERIC_ARRAY_MAX_COUNT];
1035
for (i=0; i<req_hdr->count; ++i) {
1036
if ((*options & PJSIP_INV_SUPPORT_100REL) &&
1037
pj_stricmp(&req_hdr->values[i], &STR_100REL)==0)
1039
rem_option |= PJSIP_INV_REQUIRE_100REL;
1041
} else if ((*options & PJSIP_INV_SUPPORT_TIMER) &&
1042
pj_stricmp(&req_hdr->values[i], &STR_TIMER)==0)
1044
rem_option |= PJSIP_INV_REQUIRE_TIMER;
1046
} else if (pj_stricmp(&req_hdr->values[i], &STR_REPLACES)==0) {
1049
supp = pjsip_endpt_has_capability(endpt, PJSIP_H_SUPPORTED,
1050
NULL, &STR_REPLACES);
1052
unsupp_tags[unsupp_cnt++] = req_hdr->values[i];
1053
} else if ((*options & PJSIP_INV_SUPPORT_ICE) &&
1054
pj_stricmp(&req_hdr->values[i], &STR_ICE)==0)
1056
rem_option |= PJSIP_INV_REQUIRE_ICE;
1058
} else if (!pjsip_endpt_has_capability(endpt, PJSIP_H_SUPPORTED,
1059
NULL, &req_hdr->values[i]))
1061
/* Unknown/unsupported extension tag! */
1062
unsupp_tags[unsupp_cnt++] = req_hdr->values[i];
1066
/* Check if there are required tags that we don't support */
1069
code = PJSIP_SC_BAD_EXTENSION;
1070
status = PJSIP_ERRNO_FROM_SIP_STATUS(code);
1073
pjsip_unsupported_hdr *unsupp_hdr;
1076
/* Add Unsupported header. */
1077
unsupp_hdr = pjsip_unsupported_hdr_create(rdata->tp_info.pool);
1078
PJ_ASSERT_RETURN(unsupp_hdr != NULL, PJ_ENOMEM);
1080
unsupp_hdr->count = unsupp_cnt;
1081
for (i=0; i<unsupp_cnt; ++i)
1082
unsupp_hdr->values[i] = unsupp_tags[i];
1084
pj_list_push_back(&res_hdr_list, unsupp_hdr);
1086
/* Add Supported header. */
1087
h = pjsip_endpt_get_capability(endpt, PJSIP_H_SUPPORTED,
1091
sup_hdr = (pjsip_supported_hdr*)
1092
pjsip_hdr_clone(rdata->tp_info.pool, h);
1093
pj_list_push_back(&res_hdr_list, sup_hdr);
1101
/* Check if there are local requirements that are not supported
1104
if ( ((*options & PJSIP_INV_REQUIRE_100REL)!=0 &&
1105
(rem_option & PJSIP_INV_SUPPORT_100REL)==0) ||
1106
((*options & PJSIP_INV_REQUIRE_TIMER)!=0 &&
1107
(rem_option & PJSIP_INV_SUPPORT_TIMER)==0))
1109
code = PJSIP_SC_EXTENSION_REQUIRED;
1110
status = PJSIP_ERRNO_FROM_SIP_STATUS(code);
1115
/* Add Require header. */
1116
req_hdr = pjsip_require_hdr_create(rdata->tp_info.pool);
1117
PJ_ASSERT_RETURN(req_hdr != NULL, PJ_ENOMEM);
1119
if (*options & PJSIP_INV_REQUIRE_100REL)
1120
req_hdr->values[req_hdr->count++] = pj_str("100rel");
1121
if (*options & PJSIP_INV_REQUIRE_TIMER)
1122
req_hdr->values[req_hdr->count++] = pj_str("timer");
1124
pj_list_push_back(&res_hdr_list, req_hdr);
1126
/* Add Supported header. */
1127
h = pjsip_endpt_get_capability(endpt, PJSIP_H_SUPPORTED,
1131
sup_hdr = (pjsip_supported_hdr*)
1132
pjsip_hdr_clone(rdata->tp_info.pool, h);
1133
pj_list_push_back(&res_hdr_list, sup_hdr);
1141
/* If remote Require something that we support, make us Require
1144
if (rem_option & PJSIP_INV_REQUIRE_100REL) {
1145
pj_assert(*options & PJSIP_INV_SUPPORT_100REL);
1146
*options |= PJSIP_INV_REQUIRE_100REL;
1148
if (rem_option & PJSIP_INV_REQUIRE_TIMER) {
1149
pj_assert(*options & PJSIP_INV_SUPPORT_TIMER);
1150
*options |= PJSIP_INV_REQUIRE_TIMER;
1155
/* Create response if necessary */
1156
if (code != 200 && p_tdata) {
1157
pjsip_tx_data *tdata;
1161
status = pjsip_dlg_create_response(dlg, rdata, code, NULL,
1164
status = pjsip_endpt_create_response(endpt, rdata, code, NULL,
1168
if (status != PJ_SUCCESS)
1171
/* Add response headers. */
1172
h = res_hdr_list.next;
1173
while (h != &res_hdr_list) {
1176
cloned = (pjsip_hdr*) pjsip_hdr_clone(tdata->pool, h);
1177
PJ_ASSERT_RETURN(cloned, PJ_ENOMEM);
1179
pjsip_msg_add_hdr(tdata->msg, cloned);
1186
/* Can not return PJ_SUCCESS when response message is produced.
1187
* Ref: PROTOS test ~#2490
1189
if (status == PJ_SUCCESS)
1190
status = PJSIP_ERRNO_FROM_SIP_STATUS(code);
1199
* Verify incoming INVITE request.
1201
PJ_DEF(pj_status_t) pjsip_inv_verify_request( pjsip_rx_data *rdata,
1203
const pjmedia_sdp_session *l_sdp,
1205
pjsip_endpoint *endpt,
1206
pjsip_tx_data **p_tdata)
1208
return pjsip_inv_verify_request2(rdata, options, NULL, l_sdp, dlg,
1213
* Create UAS invite session.
1215
PJ_DEF(pj_status_t) pjsip_inv_create_uas( pjsip_dialog *dlg,
1216
pjsip_rx_data *rdata,
1217
const pjmedia_sdp_session *local_sdp,
1219
pjsip_inv_session **p_inv)
1221
pjsip_inv_session *inv;
1222
struct tsx_inv_data *tsx_inv_data;
1224
pjsip_rdata_sdp_info *sdp_info;
1227
/* Verify arguments. */
1228
PJ_ASSERT_RETURN(dlg && rdata && p_inv, PJ_EINVAL);
1230
/* Dialog MUST have been initialised. */
1231
PJ_ASSERT_RETURN(pjsip_rdata_get_tsx(rdata) != NULL, PJ_EINVALIDOP);
1233
msg = rdata->msg_info.msg;
1235
/* rdata MUST contain INVITE request */
1236
PJ_ASSERT_RETURN(msg->type == PJSIP_REQUEST_MSG &&
1237
msg->line.req.method.id == PJSIP_INVITE_METHOD,
1241
pjsip_dlg_inc_lock(dlg);
1243
/* Normalize options */
1244
if (options & PJSIP_INV_REQUIRE_100REL)
1245
options |= PJSIP_INV_SUPPORT_100REL;
1246
if (options & PJSIP_INV_REQUIRE_TIMER)
1247
options |= PJSIP_INV_SUPPORT_TIMER;
1249
/* Create the session */
1250
inv = PJ_POOL_ZALLOC_T(dlg->pool, pjsip_inv_session);
1251
pj_assert(inv != NULL);
1253
inv->pool = dlg->pool;
1254
inv->role = PJSIP_ROLE_UAS;
1255
inv->state = PJSIP_INV_STATE_NULL;
1257
inv->options = options;
1258
inv->notify = PJ_TRUE;
1259
inv->cause = (pjsip_status_code) 0;
1261
/* Create flip-flop pool (see ticket #877) */
1262
/* (using inv->obj_name as temporary variable for pool names */
1263
pj_ansi_snprintf(inv->obj_name, PJ_MAX_OBJ_NAME, "inv%p", dlg->pool);
1264
inv->pool_prov = pjsip_endpt_create_pool(dlg->endpt, inv->obj_name,
1265
POOL_INIT_SIZE, POOL_INC_SIZE);
1266
inv->pool_active = pjsip_endpt_create_pool(dlg->endpt, inv->obj_name,
1267
POOL_INIT_SIZE, POOL_INC_SIZE);
1269
/* Object name will use the same dialog pointer. */
1270
pj_ansi_snprintf(inv->obj_name, PJ_MAX_OBJ_NAME, "inv%p", dlg);
1272
/* Process SDP in message body, if present. */
1273
sdp_info = pjsip_rdata_get_sdp_info(rdata);
1274
if (sdp_info->sdp_err) {
1275
pjsip_dlg_dec_lock(dlg);
1276
return sdp_info->sdp_err;
1279
/* Create negotiator. */
1280
if (sdp_info->sdp) {
1281
status = pjmedia_sdp_neg_create_w_remote_offer(inv->pool, local_sdp,
1285
} else if (local_sdp) {
1286
status = pjmedia_sdp_neg_create_w_local_offer(inv->pool,
1287
local_sdp, &inv->neg);
1289
status = PJ_SUCCESS;
1292
if (status != PJ_SUCCESS) {
1293
pjsip_dlg_dec_lock(dlg);
1297
/* Register invite as dialog usage. */
1298
status = pjsip_dlg_add_usage(dlg, &mod_inv.mod, inv);
1299
if (status != PJ_SUCCESS) {
1300
pjsip_dlg_dec_lock(dlg);
1304
/* Increment session in the dialog. */
1305
pjsip_dlg_inc_session(dlg, &mod_inv.mod);
1307
/* Save the invite transaction. */
1308
inv->invite_tsx = pjsip_rdata_get_tsx(rdata);
1310
/* Attach our data to the transaction. */
1311
tsx_inv_data = PJ_POOL_ZALLOC_T(inv->invite_tsx->pool, struct tsx_inv_data);
1312
tsx_inv_data->inv = inv;
1313
inv->invite_tsx->mod_data[mod_inv.mod.id] = tsx_inv_data;
1315
/* Create 100rel handler */
1316
if (inv->options & PJSIP_INV_REQUIRE_100REL) {
1317
pjsip_100rel_attach(inv);
1321
pjsip_dlg_dec_lock(dlg);
1324
PJ_LOG(5,(inv->obj_name, "UAS invite session created for dialog %s",
1331
* Forcefully terminate the session.
1333
PJ_DEF(pj_status_t) pjsip_inv_terminate( pjsip_inv_session *inv,
1337
PJ_ASSERT_RETURN(inv, PJ_EINVAL);
1340
pjsip_dlg_inc_lock(inv->dlg);
1342
/* Set callback notify flag. */
1343
inv->notify = notify;
1345
/* If there's pending transaction, terminate the transaction.
1346
* This may subsequently set the INVITE session state to
1349
if (inv->invite_tsx &&
1350
inv->invite_tsx->state <= PJSIP_TSX_STATE_COMPLETED)
1352
pjsip_tsx_terminate(inv->invite_tsx, st_code);
1357
inv_set_cause(inv, st_code, NULL);
1359
/* Forcefully terminate the session if state is not DISCONNECTED */
1360
if (inv->state != PJSIP_INV_STATE_DISCONNECTED) {
1361
inv_set_state(inv, PJSIP_INV_STATE_DISCONNECTED, NULL);
1365
* The dec_lock() below will actually destroys the dialog if it
1366
* has no other session.
1368
pjsip_dlg_dec_lock(inv->dlg);
1375
* Restart UAC session, possibly because app or us wants to re-send the
1376
* INVITE request due to 401/407 challenge or 3xx response.
1378
PJ_DEF(pj_status_t) pjsip_inv_uac_restart(pjsip_inv_session *inv,
1379
pj_bool_t new_offer)
1381
PJ_ASSERT_RETURN(inv, PJ_EINVAL);
1383
inv->state = PJSIP_INV_STATE_NULL;
1384
inv->invite_tsx = NULL;
1385
if (inv->last_answer) {
1386
pjsip_tx_data_dec_ref(inv->last_answer);
1387
inv->last_answer = NULL;
1390
if (new_offer && inv->neg) {
1391
pjmedia_sdp_neg_state neg_state;
1393
neg_state = pjmedia_sdp_neg_get_state(inv->neg);
1394
if (neg_state == PJMEDIA_SDP_NEG_STATE_LOCAL_OFFER) {
1395
pjmedia_sdp_neg_cancel_offer(inv->neg);
1403
static void *clone_sdp(pj_pool_t *pool, const void *data, unsigned len)
1406
return pjmedia_sdp_session_clone(pool, (const pjmedia_sdp_session*)data);
1409
static int print_sdp(pjsip_msg_body *body, char *buf, pj_size_t len)
1411
return pjmedia_sdp_print((const pjmedia_sdp_session*)body->data, buf, len);
1415
PJ_DEF(pj_status_t) pjsip_create_sdp_body( pj_pool_t *pool,
1416
pjmedia_sdp_session *sdp,
1417
pjsip_msg_body **p_body)
1419
const pj_str_t STR_APPLICATION = { "application", 11};
1420
const pj_str_t STR_SDP = { "sdp", 3 };
1421
pjsip_msg_body *body;
1423
body = PJ_POOL_ZALLOC_T(pool, pjsip_msg_body);
1424
PJ_ASSERT_RETURN(body != NULL, PJ_ENOMEM);
1426
pjsip_media_type_init(&body->content_type, (pj_str_t*)&STR_APPLICATION,
1427
(pj_str_t*)&STR_SDP);
1430
body->clone_data = &clone_sdp;
1431
body->print_body = &print_sdp;
1438
static pjsip_msg_body *create_sdp_body(pj_pool_t *pool,
1439
const pjmedia_sdp_session *c_sdp)
1441
pjsip_msg_body *body;
1444
status = pjsip_create_sdp_body(pool,
1445
pjmedia_sdp_session_clone(pool, c_sdp),
1448
if (status != PJ_SUCCESS)
1455
* Create initial INVITE request.
1457
PJ_DEF(pj_status_t) pjsip_inv_invite( pjsip_inv_session *inv,
1458
pjsip_tx_data **p_tdata )
1460
pjsip_tx_data *tdata;
1461
const pjsip_hdr *hdr;
1465
/* Verify arguments. */
1466
PJ_ASSERT_RETURN(inv && p_tdata, PJ_EINVAL);
1468
/* State MUST be NULL or CONFIRMED. */
1469
PJ_ASSERT_RETURN(inv->state == PJSIP_INV_STATE_NULL ||
1470
inv->state == PJSIP_INV_STATE_CONFIRMED,
1474
pjsip_dlg_inc_lock(inv->dlg);
1476
/* Create the INVITE request. */
1477
status = pjsip_dlg_create_request(inv->dlg, pjsip_get_invite_method(), -1,
1479
if (status != PJ_SUCCESS)
1483
/* If this is the first INVITE, then copy the headers from inv_hdr.
1484
* These are the headers parsed from the request URI when the
1485
* dialog was created.
1487
if (inv->state == PJSIP_INV_STATE_NULL) {
1488
hdr = inv->dlg->inv_hdr.next;
1490
while (hdr != &inv->dlg->inv_hdr) {
1491
pjsip_msg_add_hdr(tdata->msg, (pjsip_hdr*)
1492
pjsip_hdr_shallow_clone(tdata->pool, hdr));
1497
/* See if we have SDP to send. */
1499
pjmedia_sdp_neg_state neg_state;
1501
neg_state = pjmedia_sdp_neg_get_state(inv->neg);
1503
has_sdp = (neg_state == PJMEDIA_SDP_NEG_STATE_LOCAL_OFFER ||
1504
(neg_state == PJMEDIA_SDP_NEG_STATE_WAIT_NEGO &&
1505
pjmedia_sdp_neg_has_local_answer(inv->neg)));
1512
/* Add SDP, if any. */
1514
const pjmedia_sdp_session *offer;
1516
status = pjmedia_sdp_neg_get_neg_local(inv->neg, &offer);
1517
if (status != PJ_SUCCESS) {
1518
pjsip_tx_data_dec_ref(tdata);
1522
tdata->msg->body = create_sdp_body(tdata->pool, offer);
1525
/* Add Allow header. */
1526
if (inv->dlg->add_allow) {
1527
hdr = pjsip_endpt_get_capability(inv->dlg->endpt, PJSIP_H_ALLOW, NULL);
1529
pjsip_msg_add_hdr(tdata->msg, (pjsip_hdr*)
1530
pjsip_hdr_shallow_clone(tdata->pool, hdr));
1534
/* Add Supported header */
1535
hdr = pjsip_endpt_get_capability(inv->dlg->endpt, PJSIP_H_SUPPORTED, NULL);
1537
pjsip_msg_add_hdr(tdata->msg, (pjsip_hdr*)
1538
pjsip_hdr_shallow_clone(tdata->pool, hdr));
1541
/* Add Require header. */
1542
if ((inv->options & PJSIP_INV_REQUIRE_100REL) ||
1543
(inv->options & PJSIP_INV_REQUIRE_TIMER))
1545
pjsip_require_hdr *hreq;
1547
hreq = pjsip_require_hdr_create(tdata->pool);
1549
if (inv->options & PJSIP_INV_REQUIRE_100REL)
1550
hreq->values[hreq->count++] = pj_str("100rel");
1551
if (inv->options & PJSIP_INV_REQUIRE_TIMER)
1552
hreq->values[hreq->count++] = pj_str("timer");
1554
pjsip_msg_add_hdr(tdata->msg, (pjsip_hdr*) hreq);
1557
status = pjsip_timer_update_req(inv, tdata);
1558
if (status != PJ_SUCCESS)
1566
pjsip_dlg_dec_lock(inv->dlg);
1571
/* Util: swap pool */
1572
static void swap_pool(pj_pool_t **p1, pj_pool_t **p2)
1574
pj_pool_t *tmp = *p1;
1581
* Initiate SDP negotiation in the SDP negotiator.
1583
static pj_status_t inv_negotiate_sdp( pjsip_inv_session *inv )
1587
PJ_ASSERT_RETURN(pjmedia_sdp_neg_get_state(inv->neg) ==
1588
PJMEDIA_SDP_NEG_STATE_WAIT_NEGO,
1589
PJMEDIA_SDPNEG_EINSTATE);
1591
status = pjmedia_sdp_neg_negotiate(inv->pool_prov, inv->neg, 0);
1593
PJ_LOG(5,(inv->obj_name, "SDP negotiation done, status=%d", status));
1595
if (mod_inv.cb.on_media_update && inv->notify)
1596
(*mod_inv.cb.on_media_update)(inv, status);
1598
/* Invite session may have been terminated by the application even
1599
* after a successful SDP negotiation, for example when no audio
1600
* codec is present in the offer (see ticket #1034).
1602
if (inv->state != PJSIP_INV_STATE_DISCONNECTED) {
1604
/* Swap the flip-flop pool when SDP negotiation success. */
1605
if (status == PJ_SUCCESS) {
1606
swap_pool(&inv->pool_prov, &inv->pool_active);
1609
/* Reset the provisional pool regardless SDP negotiation result. */
1610
pj_pool_reset(inv->pool_prov);
1614
status = PJSIP_ERRNO_FROM_SIP_STATUS(inv->cause);
1621
* Check in incoming message for SDP offer/answer.
1623
static pj_status_t inv_check_sdp_in_incoming_msg( pjsip_inv_session *inv,
1624
pjsip_transaction *tsx,
1625
pjsip_rx_data *rdata)
1627
struct tsx_inv_data *tsx_inv_data;
1630
pjsip_rdata_sdp_info *sdp_info;
1632
/* Check if SDP is present in the message. */
1634
msg = rdata->msg_info.msg;
1635
if (msg->body == NULL) {
1636
/* Message doesn't have body. */
1640
sdp_info = pjsip_rdata_get_sdp_info(rdata);
1641
if (sdp_info->body.ptr == NULL) {
1642
/* Message body is not "application/sdp" */
1643
return PJMEDIA_SDP_EINSDP;
1646
/* Get/attach invite session's transaction data */
1647
tsx_inv_data = (struct tsx_inv_data*) tsx->mod_data[mod_inv.mod.id];
1648
if (tsx_inv_data == NULL) {
1649
tsx_inv_data = PJ_POOL_ZALLOC_T(tsx->pool, struct tsx_inv_data);
1650
tsx_inv_data->inv = inv;
1651
tsx->mod_data[mod_inv.mod.id] = tsx_inv_data;
1654
/* MUST NOT do multiple SDP offer/answer in a single transaction,
1656
* - this is an initial UAC INVITE transaction (i.e. not re-INVITE), and
1657
* - the previous negotiation was done on an early media (18x) and
1658
* this response is a final/2xx response, and
1659
* - the 2xx response has different To tag than the 18x response
1660
* (i.e. the request has forked).
1662
* The exception above is to add a rudimentary support for early media
1663
* forking (sample case: custom ringback). See this ticket for more
1664
* info: http://trac.pjsip.org/repos/ticket/657
1666
if (tsx_inv_data->sdp_done) {
1669
res_tag = rdata->msg_info.to->tag;
1671
/* Allow final response after SDP has been negotiated in early
1672
* media, IF this response is a final response with different
1675
if (tsx->role == PJSIP_ROLE_UAC &&
1676
rdata->msg_info.msg->line.status.code/100 == 2 &&
1677
tsx_inv_data->done_early &&
1678
pj_strcmp(&tsx_inv_data->done_tag, &res_tag))
1680
const pjmedia_sdp_session *reoffer_sdp = NULL;
1682
PJ_LOG(4,(inv->obj_name, "Received forked final response "
1683
"after SDP negotiation has been done in early "
1684
"media. Renegotiating SDP.."));
1686
/* Retrieve original SDP offer from INVITE request */
1687
reoffer_sdp = (const pjmedia_sdp_session*)
1688
tsx->last_tx->msg->body->data;
1690
/* Feed the original offer to negotiator */
1691
status = pjmedia_sdp_neg_modify_local_offer(inv->pool_prov,
1694
if (status != PJ_SUCCESS) {
1695
PJ_LOG(1,(inv->obj_name, "Error updating local offer for "
1696
"forked 2xx response (err=%d)", status));
1702
if (rdata->msg_info.msg->body) {
1703
PJ_LOG(4,(inv->obj_name, "SDP negotiation done, message "
1704
"body is ignored"));
1710
/* Process the SDP body. */
1711
if (sdp_info->sdp_err) {
1712
PJ_PERROR(4,(THIS_FILE, sdp_info->sdp_err,
1713
"Error parsing SDP in %s",
1714
pjsip_rx_data_get_info(rdata)));
1715
return PJMEDIA_SDP_EINSDP;
1718
pj_assert(sdp_info->sdp != NULL);
1720
/* The SDP can be an offer or answer, depending on negotiator's state */
1722
if (inv->neg == NULL ||
1723
pjmedia_sdp_neg_get_state(inv->neg) == PJMEDIA_SDP_NEG_STATE_DONE)
1726
/* This is an offer. */
1728
PJ_LOG(5,(inv->obj_name, "Got SDP offer in %s",
1729
pjsip_rx_data_get_info(rdata)));
1731
if (inv->neg == NULL) {
1732
status=pjmedia_sdp_neg_create_w_remote_offer(inv->pool, NULL,
1736
status=pjmedia_sdp_neg_set_remote_offer(inv->pool_prov, inv->neg,
1740
if (status != PJ_SUCCESS) {
1741
PJ_PERROR(4,(THIS_FILE, status, "Error processing SDP offer in %",
1742
pjsip_rx_data_get_info(rdata)));
1743
return PJMEDIA_SDP_EINSDP;
1746
/* Inform application about remote offer. */
1747
if (mod_inv.cb.on_rx_offer && inv->notify) {
1749
(*mod_inv.cb.on_rx_offer)(inv, sdp_info->sdp);
1753
/* application must have supplied an answer at this point. */
1754
if (pjmedia_sdp_neg_get_state(inv->neg) !=
1755
PJMEDIA_SDP_NEG_STATE_WAIT_NEGO)
1757
return PJ_EINVALIDOP;
1760
} else if (pjmedia_sdp_neg_get_state(inv->neg) ==
1761
PJMEDIA_SDP_NEG_STATE_LOCAL_OFFER)
1765
/* This is an answer.
1766
* Process and negotiate remote answer.
1769
PJ_LOG(5,(inv->obj_name, "Got SDP answer in %s",
1770
pjsip_rx_data_get_info(rdata)));
1772
status = pjmedia_sdp_neg_set_remote_answer(inv->pool_prov, inv->neg,
1775
if (status != PJ_SUCCESS) {
1776
PJ_PERROR(4,(THIS_FILE, status, "Error processing SDP answer in %s",
1777
pjsip_rx_data_get_info(rdata)));
1778
return PJMEDIA_SDP_EINSDP;
1783
inv_negotiate_sdp(inv);
1785
/* Mark this transaction has having SDP offer/answer done, and
1786
* save the reference to the To tag
1789
tsx_inv_data->sdp_done = 1;
1790
status_code = rdata->msg_info.msg->line.status.code;
1791
tsx_inv_data->done_early = (status_code/100==1);
1792
pj_strdup(tsx->pool, &tsx_inv_data->done_tag,
1793
&rdata->msg_info.to->tag);
1797
PJ_LOG(5,(THIS_FILE, "Ignored SDP in %s: negotiator state is %s",
1798
pjsip_rx_data_get_info(rdata),
1799
pjmedia_sdp_neg_state_str(pjmedia_sdp_neg_get_state(inv->neg))));
1807
* Process INVITE answer, for both initial and subsequent re-INVITE
1809
static pj_status_t process_answer( pjsip_inv_session *inv,
1811
pjsip_tx_data *tdata,
1812
const pjmedia_sdp_session *local_sdp)
1815
const pjmedia_sdp_session *sdp = NULL;
1817
/* If local_sdp is specified, then we MUST NOT have answered the
1820
if (local_sdp && (st_code/100==1 || st_code/100==2)) {
1822
if (inv->neg == NULL) {
1823
status = pjmedia_sdp_neg_create_w_local_offer(inv->pool,
1826
} else if (pjmedia_sdp_neg_get_state(inv->neg)==
1827
PJMEDIA_SDP_NEG_STATE_REMOTE_OFFER)
1829
status = pjmedia_sdp_neg_set_local_answer(inv->pool_prov, inv->neg,
1833
/* Can not specify local SDP at this state. */
1835
status = PJMEDIA_SDPNEG_EINSTATE;
1838
if (status != PJ_SUCCESS)
1844
/* If SDP negotiator is ready, start negotiation. */
1845
if (st_code/100==2 || (st_code/10==18 && st_code!=180)) {
1847
pjmedia_sdp_neg_state neg_state;
1849
/* Start nego when appropriate. */
1850
neg_state = inv->neg ? pjmedia_sdp_neg_get_state(inv->neg) :
1851
PJMEDIA_SDP_NEG_STATE_NULL;
1853
if (neg_state == PJMEDIA_SDP_NEG_STATE_LOCAL_OFFER) {
1855
status = pjmedia_sdp_neg_get_neg_local(inv->neg, &sdp);
1857
} else if (neg_state == PJMEDIA_SDP_NEG_STATE_WAIT_NEGO &&
1858
pjmedia_sdp_neg_has_local_answer(inv->neg) )
1860
struct tsx_inv_data *tsx_inv_data;
1862
/* Get invite session's transaction data */
1863
tsx_inv_data = (struct tsx_inv_data*)
1864
inv->invite_tsx->mod_data[mod_inv.mod.id];
1866
status = inv_negotiate_sdp(inv);
1867
if (status != PJ_SUCCESS)
1870
/* Mark this transaction has having SDP offer/answer done. */
1871
tsx_inv_data->sdp_done = 1;
1873
status = pjmedia_sdp_neg_get_active_local(inv->neg, &sdp);
1877
/* Include SDP when it's available for 2xx and 18x (but not 180) response.
1878
* Subsequent response will include this SDP.
1881
* - When offer/answer has been completed in reliable 183, we MUST NOT
1882
* send SDP in 2xx response. So if we don't have SDP to send, clear
1883
* the SDP in the message body ONLY if 100rel is active in this
1887
tdata->msg->body = create_sdp_body(tdata->pool, sdp);
1889
if (inv->options & PJSIP_INV_REQUIRE_100REL) {
1890
tdata->msg->body = NULL;
1900
* Create first response to INVITE
1902
PJ_DEF(pj_status_t) pjsip_inv_initial_answer( pjsip_inv_session *inv,
1903
pjsip_rx_data *rdata,
1905
const pj_str_t *st_text,
1906
const pjmedia_sdp_session *sdp,
1907
pjsip_tx_data **p_tdata)
1909
pjsip_tx_data *tdata;
1911
pjsip_status_code st_code2;
1913
/* Verify arguments. */
1914
PJ_ASSERT_RETURN(inv && p_tdata, PJ_EINVAL);
1916
/* Must have INVITE transaction. */
1917
PJ_ASSERT_RETURN(inv->invite_tsx, PJ_EBUG);
1919
pjsip_dlg_inc_lock(inv->dlg);
1921
/* Create response */
1922
status = pjsip_dlg_create_response(inv->dlg, rdata, st_code, st_text,
1924
if (status != PJ_SUCCESS)
1927
/* Invoke Session Timers module */
1928
status = pjsip_timer_process_req(inv, rdata, &st_code2);
1929
if (status != PJ_SUCCESS) {
1930
pj_status_t status2;
1932
status2 = pjsip_dlg_modify_response(inv->dlg, tdata, st_code2, NULL);
1933
if (status2 != PJ_SUCCESS) {
1934
pjsip_tx_data_dec_ref(tdata);
1937
status2 = pjsip_timer_update_resp(inv, tdata);
1938
if (status2 == PJ_SUCCESS)
1941
pjsip_tx_data_dec_ref(tdata);
1946
/* Process SDP in answer */
1947
status = process_answer(inv, st_code, tdata, sdp);
1948
if (status != PJ_SUCCESS) {
1949
pjsip_tx_data_dec_ref(tdata);
1953
/* Save this answer */
1954
inv->last_answer = tdata;
1955
pjsip_tx_data_add_ref(inv->last_answer);
1956
PJ_LOG(5,(inv->dlg->obj_name, "Initial answer %s",
1957
pjsip_tx_data_get_info(inv->last_answer)));
1959
/* Invoke Session Timers */
1960
pjsip_timer_update_resp(inv, tdata);
1965
pjsip_dlg_dec_lock(inv->dlg);
1971
* Answer initial INVITE
1972
* Re-INVITE will be answered automatically, and will not use this function.
1974
PJ_DEF(pj_status_t) pjsip_inv_answer( pjsip_inv_session *inv,
1976
const pj_str_t *st_text,
1977
const pjmedia_sdp_session *local_sdp,
1978
pjsip_tx_data **p_tdata )
1980
pjsip_tx_data *last_res;
1983
/* Verify arguments. */
1984
PJ_ASSERT_RETURN(inv && p_tdata, PJ_EINVAL);
1986
/* Must have INVITE transaction. */
1987
PJ_ASSERT_RETURN(inv->invite_tsx, PJ_EBUG);
1989
/* Must have created an answer before */
1990
PJ_ASSERT_RETURN(inv->last_answer, PJ_EINVALIDOP);
1992
pjsip_dlg_inc_lock(inv->dlg);
1994
/* Modify last response. */
1995
last_res = inv->last_answer;
1996
status = pjsip_dlg_modify_response(inv->dlg, last_res, st_code, st_text);
1997
if (status != PJ_SUCCESS)
2000
/* For non-2xx final response, strip message body */
2001
if (st_code >= 300) {
2002
last_res->msg->body = NULL;
2005
/* Process SDP in answer */
2006
status = process_answer(inv, st_code, last_res, local_sdp);
2007
if (status != PJ_SUCCESS) {
2008
pjsip_tx_data_dec_ref(last_res);
2012
/* Invoke Session Timers */
2013
pjsip_timer_update_resp(inv, last_res);
2015
*p_tdata = last_res;
2018
pjsip_dlg_dec_lock(inv->dlg);
2026
PJ_DEF(pj_status_t) pjsip_inv_set_sdp_answer( pjsip_inv_session *inv,
2027
const pjmedia_sdp_session *sdp )
2031
PJ_ASSERT_RETURN(inv && sdp, PJ_EINVAL);
2033
pjsip_dlg_inc_lock(inv->dlg);
2034
status = pjmedia_sdp_neg_set_local_answer( inv->pool_prov, inv->neg, sdp);
2035
pjsip_dlg_dec_lock(inv->dlg);
2044
PJ_DEF(pj_status_t) pjsip_inv_end_session( pjsip_inv_session *inv,
2046
const pj_str_t *st_text,
2047
pjsip_tx_data **p_tdata )
2049
pjsip_tx_data *tdata;
2052
/* Verify arguments. */
2053
PJ_ASSERT_RETURN(inv && p_tdata, PJ_EINVAL);
2055
/* Set cause code. */
2056
inv_set_cause(inv, st_code, st_text);
2058
/* Create appropriate message. */
2059
switch (inv->state) {
2060
case PJSIP_INV_STATE_CALLING:
2061
case PJSIP_INV_STATE_EARLY:
2062
case PJSIP_INV_STATE_INCOMING:
2064
if (inv->role == PJSIP_ROLE_UAC) {
2066
/* For UAC when session has not been confirmed, create CANCEL. */
2068
/* MUST have the original UAC INVITE transaction. */
2069
PJ_ASSERT_RETURN(inv->invite_tsx != NULL, PJ_EBUG);
2071
/* But CANCEL should only be called when we have received a
2072
* provisional response. If we haven't received any responses,
2073
* just destroy the transaction.
2075
if (inv->invite_tsx->status_code < 100) {
2077
/* Do not stop INVITE retransmission, see ticket #506 */
2078
//pjsip_tsx_stop_retransmit(inv->invite_tsx);
2079
inv->cancelling = PJ_TRUE;
2080
inv->pending_cancel = PJ_TRUE;
2082
PJ_LOG(4, (inv->obj_name, "Delaying CANCEL since no "
2083
"provisional response is received yet"));
2087
/* The CSeq here assumes that the dialog is started with an
2088
* INVITE session. This may not be correct; dialog can be
2089
* started as SUBSCRIBE session.
2092
status = pjsip_endpt_create_cancel(inv->dlg->endpt,
2093
inv->invite_tsx->last_tx,
2095
if (status != PJ_SUCCESS)
2098
/* Set timeout for the INVITE transaction, in case UAS is not
2099
* able to respond the INVITE with 487 final response. The
2100
* timeout value is 64*T1.
2102
pjsip_tsx_set_timeout(inv->invite_tsx, 64 * pjsip_cfg()->tsx.t1);
2106
/* For UAS, send a final response. */
2107
tdata = inv->invite_tsx->last_tx;
2108
PJ_ASSERT_RETURN(tdata != NULL, PJ_EINVALIDOP);
2110
//status = pjsip_dlg_modify_response(inv->dlg, tdata, st_code,
2112
status = pjsip_inv_answer(inv, st_code, st_text, NULL, &tdata);
2116
case PJSIP_INV_STATE_CONNECTING:
2117
case PJSIP_INV_STATE_CONFIRMED:
2118
/* End Session Timer */
2119
pjsip_timer_end_session(inv);
2121
/* For established dialog, send BYE */
2122
status = pjsip_dlg_create_request(inv->dlg, pjsip_get_bye_method(),
2126
case PJSIP_INV_STATE_DISCONNECTED:
2127
/* No need to do anything. */
2128
return PJSIP_ESESSIONTERMINATED;
2131
pj_assert("!Invalid operation!");
2132
return PJ_EINVALIDOP;
2135
if (status != PJ_SUCCESS)
2141
inv->cancelling = PJ_TRUE;
2147
/* Following redirection recursion, get next target from the target set and
2150
* Returns PJ_FALSE if recursion fails (either because there's no more target
2151
* or user rejects the recursion). If we return PJ_FALSE, caller should
2152
* disconnect the session.
2155
* the event 'e' argument may be NULL.
2157
static pj_bool_t inv_uac_recurse(pjsip_inv_session *inv, int code,
2158
const pj_str_t *reason, pjsip_event *e)
2160
pjsip_redirect_op op;
2161
pjsip_target *target;
2163
/* Won't redirect if the callback is not implemented. */
2164
if (mod_inv.cb.on_redirected == NULL)
2168
reason = pjsip_get_status_text(code);
2170
/* Set status of current target */
2171
pjsip_target_assign_status(inv->dlg->target_set.current, inv->dlg->pool,
2174
/* Fetch next target from the target set. We only want to
2175
* process SIP/SIPS URI for now.
2178
target = pjsip_target_set_get_next(&inv->dlg->target_set);
2179
if (target == NULL) {
2180
/* No more target. */
2184
if (!PJSIP_URI_SCHEME_IS_SIP(target->uri) &&
2185
!PJSIP_URI_SCHEME_IS_SIPS(target->uri))
2187
code = PJSIP_SC_UNSUPPORTED_URI_SCHEME;
2188
reason = pjsip_get_status_text(code);
2190
/* Mark this target as unusable and fetch next target. */
2191
pjsip_target_assign_status(target, inv->dlg->pool, code, reason);
2193
/* Found a target */
2198
/* We have target in 'target'. Set this target as current target
2199
* and notify callback.
2201
pjsip_target_set_set_current(&inv->dlg->target_set, target);
2203
op = (*mod_inv.cb.on_redirected)(inv, target->uri, e);
2206
/* Check what the application wants to do now */
2208
case PJSIP_REDIRECT_ACCEPT:
2209
case PJSIP_REDIRECT_STOP:
2210
/* Must increment session counter, that's the convention of the
2211
* pjsip_inv_process_redirect().
2213
pjsip_dlg_inc_session(inv->dlg, &mod_inv.mod);
2215
/* Act on the recursion */
2216
pjsip_inv_process_redirect(inv, op, e);
2219
case PJSIP_REDIRECT_PENDING:
2220
/* Increment session so that the dialog/session is not destroyed
2221
* while we're waiting for user confirmation.
2223
pjsip_dlg_inc_session(inv->dlg, &mod_inv.mod);
2225
/* Also clear the invite_tsx variable, otherwise when this tsx is
2226
* terminated, it will also terminate the session.
2228
inv->invite_tsx = NULL;
2230
/* Done. The processing will continue once the application calls
2231
* pjsip_inv_process_redirect().
2235
case PJSIP_REDIRECT_REJECT:
2236
/* Recursively call this function again to fetch next target, if any.
2238
return inv_uac_recurse(inv, PJSIP_SC_REQUEST_TERMINATED, NULL, e);
2242
pj_assert(!"Should not reach here");
2247
/* Process redirection/recursion */
2248
PJ_DEF(pj_status_t) pjsip_inv_process_redirect( pjsip_inv_session *inv,
2249
pjsip_redirect_op op,
2252
const pjsip_status_code cancel_code = PJSIP_SC_REQUEST_TERMINATED;
2253
pjsip_event usr_event;
2254
pj_status_t status = PJ_SUCCESS;
2256
PJ_ASSERT_RETURN(inv && op != PJSIP_REDIRECT_PENDING, PJ_EINVAL);
2259
PJSIP_EVENT_INIT_USER(usr_event, NULL, NULL, NULL, NULL);
2263
pjsip_dlg_inc_lock(inv->dlg);
2265
/* Decrement session. That's the convention here to prevent the dialog
2266
* or session from being destroyed while we're waiting for user
2269
pjsip_dlg_dec_session(inv->dlg, &mod_inv.mod);
2271
/* See what the application wants to do now */
2273
case PJSIP_REDIRECT_ACCEPT:
2274
/* User accept the redirection. Reset the session and resend the
2278
pjsip_tx_data *tdata;
2281
/* Get the original INVITE request. */
2282
tdata = inv->invite_req;
2283
pjsip_tx_data_add_ref(tdata);
2285
/* Restore strict route set.
2286
* See http://trac.pjsip.org/repos/ticket/492
2288
pjsip_restore_strict_route_set(tdata);
2291
tdata->msg->line.req.uri = (pjsip_uri*)
2292
pjsip_uri_clone(tdata->pool, inv->dlg->target_set.current->uri);
2294
/* Remove branch param in Via header. */
2295
via = (pjsip_via_hdr*)
2296
pjsip_msg_find_hdr(tdata->msg, PJSIP_H_VIA, NULL);
2297
via->branch_param.slen = 0;
2299
/* Reset message destination info (see #1248). */
2300
pj_bzero(&tdata->dest_info, sizeof(tdata->dest_info));
2302
/* Must invalidate the message! */
2303
pjsip_tx_data_invalidate_msg(tdata);
2305
/* Reset the session */
2306
pjsip_inv_uac_restart(inv, PJ_FALSE);
2308
/* (re)Send the INVITE request */
2309
status = pjsip_inv_send_msg(inv, tdata);
2313
case PJSIP_REDIRECT_STOP:
2314
/* User doesn't want the redirection. Disconnect the session now. */
2315
inv_set_cause(inv, cancel_code, pjsip_get_status_text(cancel_code));
2316
inv_set_state(inv, PJSIP_INV_STATE_DISCONNECTED, e);
2318
/* Caller should expect that the invite session is gone now, so
2319
* we don't need to set status to PJSIP_ESESSIONTERMINATED here.
2323
case PJSIP_REDIRECT_REJECT:
2324
/* Current target is rejected. Fetch next target if any. */
2325
if (inv_uac_recurse(inv, cancel_code, NULL, NULL) == PJ_FALSE) {
2326
inv_set_cause(inv, cancel_code,
2327
pjsip_get_status_text(cancel_code));
2328
inv_set_state(inv, PJSIP_INV_STATE_DISCONNECTED, e);
2330
/* Tell caller that the invite session is gone now */
2331
status = PJSIP_ESESSIONTERMINATED;
2336
case PJSIP_REDIRECT_PENDING:
2337
pj_assert(!"Should not happen");
2342
pjsip_dlg_dec_lock(inv->dlg);
2351
PJ_DEF(pj_status_t) pjsip_inv_reinvite( pjsip_inv_session *inv,
2352
const pj_str_t *new_contact,
2353
const pjmedia_sdp_session *new_offer,
2354
pjsip_tx_data **p_tdata )
2357
pjsip_contact_hdr *contact_hdr = NULL;
2359
/* Check arguments. */
2360
PJ_ASSERT_RETURN(inv && p_tdata, PJ_EINVAL);
2362
/* Must NOT have a pending INVITE transaction */
2363
if (inv->invite_tsx!=NULL)
2364
return PJ_EINVALIDOP;
2367
pjsip_dlg_inc_lock(inv->dlg);
2371
const pj_str_t STR_CONTACT = { "Contact", 7 };
2373
pj_strdup_with_null(inv->dlg->pool, &tmp, new_contact);
2374
contact_hdr = (pjsip_contact_hdr*)
2375
pjsip_parse_hdr(inv->dlg->pool, &STR_CONTACT,
2376
tmp.ptr, tmp.slen, NULL);
2378
status = PJSIP_EINVALIDURI;
2386
status = pjmedia_sdp_neg_create_w_local_offer(inv->pool,
2389
if (status != PJ_SUCCESS)
2392
} else switch (pjmedia_sdp_neg_get_state(inv->neg)) {
2394
case PJMEDIA_SDP_NEG_STATE_NULL:
2395
pj_assert(!"Unexpected SDP neg state NULL");
2399
case PJMEDIA_SDP_NEG_STATE_LOCAL_OFFER:
2400
PJ_LOG(4,(inv->obj_name,
2401
"pjsip_inv_reinvite: already have an offer, new "
2402
"offer is ignored"));
2405
case PJMEDIA_SDP_NEG_STATE_REMOTE_OFFER:
2406
status = pjmedia_sdp_neg_set_local_answer(inv->pool_prov,
2409
if (status != PJ_SUCCESS)
2413
case PJMEDIA_SDP_NEG_STATE_WAIT_NEGO:
2414
PJ_LOG(4,(inv->obj_name,
2415
"pjsip_inv_reinvite: SDP in WAIT_NEGO state, new "
2416
"offer is ignored"));
2419
case PJMEDIA_SDP_NEG_STATE_DONE:
2420
status = pjmedia_sdp_neg_modify_local_offer(inv->pool_prov,
2423
if (status != PJ_SUCCESS)
2430
inv->dlg->local.contact = contact_hdr;
2432
status = pjsip_inv_invite(inv, p_tdata);
2435
pjsip_dlg_dec_lock(inv->dlg);
2442
PJ_DEF(pj_status_t) pjsip_inv_update ( pjsip_inv_session *inv,
2443
const pj_str_t *new_contact,
2444
const pjmedia_sdp_session *offer,
2445
pjsip_tx_data **p_tdata )
2447
pjsip_contact_hdr *contact_hdr = NULL;
2448
pjsip_tx_data *tdata = NULL;
2449
pjmedia_sdp_session *sdp_copy;
2450
pj_status_t status = PJ_SUCCESS;
2452
/* Verify arguments. */
2453
PJ_ASSERT_RETURN(inv && p_tdata, PJ_EINVAL);
2455
/* Dialog must have been established */
2456
PJ_ASSERT_RETURN(inv->dlg->state == PJSIP_DIALOG_STATE_ESTABLISHED,
2459
/* Invite session must not have been disconnected */
2460
PJ_ASSERT_RETURN(inv->state < PJSIP_INV_STATE_DISCONNECTED,
2464
pjsip_dlg_inc_lock(inv->dlg);
2466
/* Process offer, if any */
2468
if (pjmedia_sdp_neg_get_state(inv->neg)!=PJMEDIA_SDP_NEG_STATE_DONE) {
2469
PJ_LOG(4,(inv->dlg->obj_name,
2470
"Invalid SDP offer/answer state for UPDATE"));
2471
status = PJ_EINVALIDOP;
2475
/* Notify negotiator about the new offer. This will fix the offer
2476
* with correct SDP origin.
2478
status = pjmedia_sdp_neg_modify_local_offer(inv->pool_prov, inv->neg,
2480
if (status != PJ_SUCCESS)
2483
/* Retrieve the "fixed" offer from negotiator */
2484
pjmedia_sdp_neg_get_neg_local(inv->neg, &offer);
2487
/* Update Contact if required */
2490
const pj_str_t STR_CONTACT = { "Contact", 7 };
2492
pj_strdup_with_null(inv->dlg->pool, &tmp, new_contact);
2493
contact_hdr = (pjsip_contact_hdr*)
2494
pjsip_parse_hdr(inv->dlg->pool, &STR_CONTACT,
2495
tmp.ptr, tmp.slen, NULL);
2497
status = PJSIP_EINVALIDURI;
2501
inv->dlg->local.contact = contact_hdr;
2504
/* Create request */
2505
status = pjsip_dlg_create_request(inv->dlg, &pjsip_update_method,
2507
if (status != PJ_SUCCESS)
2510
/* Attach SDP body */
2512
sdp_copy = pjmedia_sdp_session_clone(tdata->pool, offer);
2513
pjsip_create_sdp_body(tdata->pool, sdp_copy, &tdata->msg->body);
2516
/* Unlock dialog. */
2517
pjsip_dlg_dec_lock(inv->dlg);
2519
status = pjsip_timer_update_req(inv, tdata);
2520
if (status != PJ_SUCCESS)
2529
pjsip_tx_data_dec_ref(tdata);
2531
/* Unlock dialog. */
2532
pjsip_dlg_dec_lock(inv->dlg);
2538
* Create an ACK request.
2540
PJ_DEF(pj_status_t) pjsip_inv_create_ack(pjsip_inv_session *inv,
2542
pjsip_tx_data **p_tdata)
2544
const pjmedia_sdp_session *sdp = NULL;
2547
PJ_ASSERT_RETURN(inv && p_tdata, PJ_EINVAL);
2550
pjsip_dlg_inc_lock(inv->dlg);
2552
/* Destroy last_ack */
2553
if (inv->last_ack) {
2554
pjsip_tx_data_dec_ref(inv->last_ack);
2555
inv->last_ack = NULL;
2558
/* Create new ACK request */
2559
status = pjsip_dlg_create_request(inv->dlg, pjsip_get_ack_method(),
2560
cseq, &inv->last_ack);
2561
if (status != PJ_SUCCESS) {
2562
pjsip_dlg_dec_lock(inv->dlg);
2566
/* See if we have pending SDP answer to send */
2567
sdp = inv_has_pending_answer(inv, inv->invite_tsx);
2569
inv->last_ack->msg->body = create_sdp_body(inv->last_ack->pool, sdp);
2572
/* Keep this for subsequent response retransmission */
2573
inv->last_ack_cseq = cseq;
2574
pjsip_tx_data_add_ref(inv->last_ack);
2577
*p_tdata = inv->last_ack;
2579
/* Unlock dialog. */
2580
pjsip_dlg_dec_lock(inv->dlg);
2586
* Send a request or response message.
2588
PJ_DEF(pj_status_t) pjsip_inv_send_msg( pjsip_inv_session *inv,
2589
pjsip_tx_data *tdata)
2593
/* Verify arguments. */
2594
PJ_ASSERT_RETURN(inv && tdata, PJ_EINVAL);
2596
PJ_LOG(5,(inv->obj_name, "Sending %s",
2597
pjsip_tx_data_get_info(tdata)));
2599
if (tdata->msg->type == PJSIP_REQUEST_MSG) {
2600
struct tsx_inv_data *tsx_inv_data;
2602
pjsip_dlg_inc_lock(inv->dlg);
2604
/* Check again that we didn't receive incoming re-INVITE */
2605
if (tdata->msg->line.req.method.id==PJSIP_INVITE_METHOD &&
2608
pjsip_tx_data_dec_ref(tdata);
2609
pjsip_dlg_dec_lock(inv->dlg);
2610
return PJ_EINVALIDOP;
2613
/* Associate our data in outgoing invite transaction */
2614
tsx_inv_data = PJ_POOL_ZALLOC_T(inv->pool, struct tsx_inv_data);
2615
tsx_inv_data->inv = inv;
2617
pjsip_dlg_dec_lock(inv->dlg);
2619
status = pjsip_dlg_send_request(inv->dlg, tdata, mod_inv.mod.id,
2621
if (status != PJ_SUCCESS)
2625
pjsip_cseq_hdr *cseq;
2627
/* Can only do this to send response to original INVITE
2630
PJ_ASSERT_RETURN((cseq=(pjsip_cseq_hdr*)pjsip_msg_find_hdr(tdata->msg, PJSIP_H_CSEQ, NULL))!=NULL
2631
&& (cseq->cseq == inv->invite_tsx->cseq),
2634
if (inv->options & PJSIP_INV_REQUIRE_100REL) {
2635
status = pjsip_100rel_tx_response(inv, tdata);
2638
status = pjsip_dlg_send_response(inv->dlg, inv->invite_tsx, tdata);
2641
if (status != PJ_SUCCESS)
2651
* Respond to incoming CANCEL request.
2653
static void inv_respond_incoming_cancel(pjsip_inv_session *inv,
2654
pjsip_transaction *cancel_tsx,
2655
pjsip_rx_data *rdata)
2657
pjsip_tx_data *tdata;
2658
pjsip_transaction *invite_tsx;
2662
/* See if we have matching INVITE server transaction: */
2664
pjsip_tsx_create_key(rdata->tp_info.pool, &key, PJSIP_ROLE_UAS,
2665
pjsip_get_invite_method(), rdata);
2666
invite_tsx = pjsip_tsx_layer_find_tsx(&key, PJ_TRUE);
2668
if (invite_tsx == NULL) {
2670
/* Invite transaction not found!
2671
* Respond CANCEL with 481 (RFC 3261 Section 9.2 page 55)
2673
status = pjsip_dlg_create_response( inv->dlg, rdata, 481, NULL,
2677
/* Always answer CANCEL will 200 (OK) regardless of
2678
* the state of the INVITE transaction.
2680
status = pjsip_dlg_create_response( inv->dlg, rdata, 200, NULL,
2684
/* See if we have created the response successfully. */
2685
if (status != PJ_SUCCESS) return;
2687
/* Send the CANCEL response */
2688
status = pjsip_dlg_send_response(inv->dlg, cancel_tsx, tdata);
2689
if (status != PJ_SUCCESS) return;
2692
/* See if we need to terminate the UAS INVITE transaction
2693
* with 487 (Request Terminated) response.
2695
if (invite_tsx && invite_tsx->status_code < 200) {
2697
pj_assert(invite_tsx->last_tx != NULL);
2699
tdata = invite_tsx->last_tx;
2701
status = pjsip_dlg_modify_response(inv->dlg, tdata, 487, NULL);
2702
if (status == PJ_SUCCESS) {
2703
/* Remove the message body */
2704
tdata->msg->body = NULL;
2705
if (inv->options & PJSIP_INV_REQUIRE_100REL) {
2706
status = pjsip_100rel_tx_response(inv, tdata);
2708
status = pjsip_dlg_send_response(inv->dlg, invite_tsx,
2715
pj_mutex_unlock(invite_tsx->mutex);
2720
* Respond to incoming BYE request.
2722
static void inv_respond_incoming_bye( pjsip_inv_session *inv,
2723
pjsip_transaction *bye_tsx,
2724
pjsip_rx_data *rdata,
2728
pjsip_tx_data *tdata;
2730
/* Respond BYE with 200: */
2732
status = pjsip_dlg_create_response(inv->dlg, rdata, 200, NULL, &tdata);
2733
if (status != PJ_SUCCESS) return;
2735
status = pjsip_dlg_send_response(inv->dlg, bye_tsx, tdata);
2736
if (status != PJ_SUCCESS) return;
2738
/* Terminate session: */
2740
if (inv->state != PJSIP_INV_STATE_DISCONNECTED) {
2741
inv_set_cause(inv, PJSIP_SC_OK, NULL);
2742
inv_set_state(inv, PJSIP_INV_STATE_DISCONNECTED, e);
2747
* Respond to BYE request.
2749
static void inv_handle_bye_response( pjsip_inv_session *inv,
2750
pjsip_transaction *tsx,
2751
pjsip_rx_data *rdata,
2756
if (e->body.tsx_state.type != PJSIP_EVENT_RX_MSG) {
2757
inv_set_cause(inv, PJSIP_SC_OK, NULL);
2758
inv_set_state(inv, PJSIP_INV_STATE_DISCONNECTED, e);
2762
/* Handle 401/407 challenge. */
2763
if (tsx->status_code == 401 || tsx->status_code == 407) {
2765
pjsip_tx_data *tdata;
2767
status = pjsip_auth_clt_reinit_req( &inv->dlg->auth_sess,
2772
if (status != PJ_SUCCESS) {
2774
/* Does not have proper credentials.
2775
* End the session anyway.
2777
inv_set_cause(inv, PJSIP_SC_OK, NULL);
2778
inv_set_state(inv, PJSIP_INV_STATE_DISCONNECTED, e);
2782
status = pjsip_inv_send_msg(inv, tdata);
2787
/* End the session. */
2788
inv_set_cause(inv, PJSIP_SC_OK, NULL);
2789
inv_set_state(inv, PJSIP_INV_STATE_DISCONNECTED, e);
2795
* Respond to incoming UPDATE request.
2797
static void inv_respond_incoming_update(pjsip_inv_session *inv,
2798
pjsip_rx_data *rdata)
2800
pjmedia_sdp_neg_state neg_state;
2802
pjsip_tx_data *tdata = NULL;
2803
pjsip_status_code st_code;
2805
/* Invoke Session Timers module */
2806
status = pjsip_timer_process_req(inv, rdata, &st_code);
2807
if (status != PJ_SUCCESS) {
2808
status = pjsip_dlg_create_response(inv->dlg, rdata, st_code,
2813
neg_state = pjmedia_sdp_neg_get_state(inv->neg);
2815
/* Send 491 if we receive UPDATE while we're waiting for an answer */
2816
if (neg_state == PJMEDIA_SDP_NEG_STATE_LOCAL_OFFER) {
2817
status = pjsip_dlg_create_response(inv->dlg, rdata,
2818
PJSIP_SC_REQUEST_PENDING, NULL,
2821
/* Send 500 with Retry-After header set randomly between 0 and 10 if we
2822
* receive UPDATE while we haven't sent answer.
2824
else if (neg_state == PJMEDIA_SDP_NEG_STATE_REMOTE_OFFER ||
2825
neg_state == PJMEDIA_SDP_NEG_STATE_WAIT_NEGO) {
2826
status = pjsip_dlg_create_response(inv->dlg, rdata,
2827
PJSIP_SC_INTERNAL_SERVER_ERROR,
2830
/* If UPDATE doesn't contain SDP, just respond with 200/OK.
2831
* This is a valid scenario according to session-timer draft.
2833
} else if (rdata->msg_info.msg->body == NULL) {
2835
status = pjsip_dlg_create_response(inv->dlg, rdata,
2839
/* We receive new offer from remote */
2840
inv_check_sdp_in_incoming_msg(inv, pjsip_rdata_get_tsx(rdata), rdata);
2842
/* Application MUST have supplied the answer by now.
2843
* If so, negotiate the SDP.
2845
neg_state = pjmedia_sdp_neg_get_state(inv->neg);
2846
if (neg_state != PJMEDIA_SDP_NEG_STATE_WAIT_NEGO ||
2847
(status=inv_negotiate_sdp(inv)) != PJ_SUCCESS)
2849
/* Negotiation has failed. If negotiator is still
2850
* stuck at non-DONE state, cancel any ongoing offer.
2852
neg_state = pjmedia_sdp_neg_get_state(inv->neg);
2853
if (neg_state != PJMEDIA_SDP_NEG_STATE_DONE) {
2854
pjmedia_sdp_neg_cancel_offer(inv->neg);
2857
status = pjsip_dlg_create_response(inv->dlg, rdata,
2858
PJSIP_SC_NOT_ACCEPTABLE_HERE,
2861
/* New media has been negotiated successfully, send 200/OK */
2862
status = pjsip_dlg_create_response(inv->dlg, rdata,
2863
PJSIP_SC_OK, NULL, &tdata);
2864
if (status == PJ_SUCCESS) {
2865
const pjmedia_sdp_session *sdp;
2866
status = pjmedia_sdp_neg_get_active_local(inv->neg, &sdp);
2867
if (status == PJ_SUCCESS)
2868
tdata->msg->body = create_sdp_body(tdata->pool, sdp);
2874
/* Invoke Session Timers */
2875
if (status == PJ_SUCCESS)
2876
status = pjsip_timer_update_resp(inv, tdata);
2878
if (status != PJ_SUCCESS) {
2879
if (tdata != NULL) {
2880
pjsip_tx_data_dec_ref(tdata);
2886
pjsip_dlg_send_response(inv->dlg, pjsip_rdata_get_tsx(rdata), tdata);
2891
* Handle incoming response to UAC UPDATE request.
2893
static void inv_handle_update_response( pjsip_inv_session *inv,
2896
pjsip_transaction *tsx = e->body.tsx_state.tsx;
2897
struct tsx_inv_data *tsx_inv_data = NULL;
2898
pj_status_t status = -1;
2900
/* Handle 401/407 challenge. */
2901
if (tsx->state == PJSIP_TSX_STATE_COMPLETED &&
2902
(tsx->status_code == 401 || tsx->status_code == 407))
2904
pjsip_tx_data *tdata;
2906
status = pjsip_auth_clt_reinit_req( &inv->dlg->auth_sess,
2907
e->body.tsx_state.src.rdata,
2911
if (status != PJ_SUCCESS) {
2913
/* Somehow failed. Probably it's not a good idea to terminate
2914
* the session since this is just a request within dialog. And
2915
* even if we terminate we should send BYE.
2918
inv_set_cause(inv, PJSIP_SC_OK, NULL);
2919
inv_set_state(inv, PJSIP_INV_STATE_DISCONNECTED, e);
2923
/* Re-send request. */
2924
status = pjsip_inv_send_msg(inv, tdata);
2928
/* Process 422 response */
2929
else if (tsx->state == PJSIP_TSX_STATE_COMPLETED &&
2930
tsx->status_code == 422)
2932
status = handle_timer_response(inv, e->body.tsx_state.src.rdata,
2936
/* Process 2xx response */
2937
else if (tsx->state == PJSIP_TSX_STATE_COMPLETED &&
2938
tsx->status_code/100 == 2 &&
2939
e->body.tsx_state.src.rdata->msg_info.msg->body)
2941
status = handle_timer_response(inv, e->body.tsx_state.src.rdata,
2943
status = inv_check_sdp_in_incoming_msg(inv, tsx,
2944
e->body.tsx_state.src.rdata);
2947
/* Get/attach invite session's transaction data */
2950
/* Session-Timer needs to see any error responses, to determine
2951
* whether peer supports UPDATE with empty body.
2953
if (tsx->state == PJSIP_TSX_STATE_COMPLETED &&
2954
tsx->role == PJSIP_ROLE_UAC)
2956
status = handle_timer_response(inv, e->body.tsx_state.src.rdata,
2960
tsx_inv_data = (struct tsx_inv_data*)tsx->mod_data[mod_inv.mod.id];
2961
if (tsx_inv_data == NULL) {
2962
tsx_inv_data=PJ_POOL_ZALLOC_T(tsx->pool, struct tsx_inv_data);
2963
tsx_inv_data->inv = inv;
2964
tsx->mod_data[mod_inv.mod.id] = tsx_inv_data;
2968
/* Cancel the negotiation if we don't get successful negotiation by now */
2969
if (pjmedia_sdp_neg_get_state(inv->neg) ==
2970
PJMEDIA_SDP_NEG_STATE_LOCAL_OFFER &&
2971
tsx_inv_data && tsx_inv_data->sdp_done == PJ_FALSE)
2973
pjmedia_sdp_neg_cancel_offer(inv->neg);
2975
/* Prevent from us cancelling different offer! */
2976
tsx_inv_data->sdp_done = PJ_TRUE;
2982
* Handle incoming reliable response.
2984
static void inv_handle_incoming_reliable_response(pjsip_inv_session *inv,
2985
pjsip_rx_data *rdata)
2987
pjsip_tx_data *tdata;
2988
const pjmedia_sdp_session *sdp;
2992
status = pjsip_100rel_create_prack(inv, rdata, &tdata);
2993
if (status != PJ_SUCCESS)
2996
/* See if we need to attach SDP answer on the PRACK request */
2997
sdp = inv_has_pending_answer(inv, pjsip_rdata_get_tsx(rdata));
2999
tdata->msg->body = create_sdp_body(tdata->pool, sdp);
3002
/* Send PRACK (must be using 100rel module!) */
3003
pjsip_100rel_send_prack(inv, tdata);
3008
* Handle incoming PRACK.
3010
static void inv_respond_incoming_prack(pjsip_inv_session *inv,
3011
pjsip_rx_data *rdata)
3015
/* Run through 100rel module to see if we can accept this
3016
* PRACK request. The 100rel will send 200/OK to PRACK request.
3018
status = pjsip_100rel_on_rx_prack(inv, rdata);
3019
if (status != PJ_SUCCESS)
3022
/* Now check for SDP answer in the PRACK request */
3023
if (rdata->msg_info.msg->body) {
3024
status = inv_check_sdp_in_incoming_msg(inv,
3025
pjsip_rdata_get_tsx(rdata), rdata);
3031
/* If SDP negotiation has been successful, also mark the
3032
* SDP negotiation flag in the invite transaction to be
3035
if (status == PJ_SUCCESS && inv->invite_tsx) {
3036
struct tsx_inv_data *tsx_inv_data;
3038
/* Get/attach invite session's transaction data */
3039
tsx_inv_data = (struct tsx_inv_data*)
3040
inv->invite_tsx->mod_data[mod_inv.mod.id];
3041
if (tsx_inv_data == NULL) {
3042
tsx_inv_data = PJ_POOL_ZALLOC_T(inv->invite_tsx->pool,
3043
struct tsx_inv_data);
3044
tsx_inv_data->inv = inv;
3045
inv->invite_tsx->mod_data[mod_inv.mod.id] = tsx_inv_data;
3048
tsx_inv_data->sdp_done = PJ_TRUE;
3054
* State NULL is before anything is sent/received.
3056
static void inv_on_state_null( pjsip_inv_session *inv, pjsip_event *e)
3058
pjsip_transaction *tsx = e->body.tsx_state.tsx;
3059
pjsip_dialog *dlg = pjsip_tsx_get_dlg(tsx);
3061
PJ_ASSERT_ON_FAIL(tsx && dlg, return);
3063
if (tsx->method.id == PJSIP_INVITE_METHOD) {
3065
/* Keep the initial INVITE transaction. */
3066
if (inv->invite_tsx == NULL)
3067
inv->invite_tsx = tsx;
3069
if (dlg->role == PJSIP_ROLE_UAC) {
3071
/* Save the original INVITE request, if on_redirected() callback
3072
* is implemented. We may need to resend the INVITE if we receive
3073
* redirection response.
3075
if (mod_inv.cb.on_redirected) {
3076
if (inv->invite_req) {
3077
pjsip_tx_data_dec_ref(inv->invite_req);
3078
inv->invite_req = NULL;
3080
inv->invite_req = tsx->last_tx;
3081
pjsip_tx_data_add_ref(inv->invite_req);
3084
switch (tsx->state) {
3085
case PJSIP_TSX_STATE_CALLING:
3086
inv_set_state(inv, PJSIP_INV_STATE_CALLING, e);
3089
inv_on_state_calling(inv, e);
3094
switch (tsx->state) {
3095
case PJSIP_TSX_STATE_TRYING:
3096
inv_set_state(inv, PJSIP_INV_STATE_INCOMING, e);
3098
case PJSIP_TSX_STATE_PROCEEDING:
3099
inv_set_state(inv, PJSIP_INV_STATE_INCOMING, e);
3100
if (tsx->status_code > 100)
3101
inv_set_state(inv, PJSIP_INV_STATE_EARLY, e);
3103
case PJSIP_TSX_STATE_TERMINATED:
3104
/* there is a failure in sending response. */
3105
inv_set_cause(inv, tsx->status_code, &tsx->status_text);
3106
inv_set_state(inv, PJSIP_INV_STATE_DISCONNECTED, e);
3109
inv_on_state_incoming(inv, e);
3115
pj_assert(!"Unexpected transaction type");
3120
* Generic UAC transaction handler:
3121
* - resend request on 401 or 407 response.
3122
* - terminate dialog on 408 and 481 response.
3123
* - resend request on 422 response.
3125
static pj_bool_t handle_uac_tsx_response(pjsip_inv_session *inv,
3128
/* RFC 3261 Section 12.2.1.2:
3129
* If the response for a request within a dialog is a 481
3130
* (Call/Transaction Does Not Exist) or a 408 (Request Timeout), the UAC
3131
* SHOULD terminate the dialog. A UAC SHOULD also terminate a dialog if
3132
* no response at all is received for the request (the client
3133
* transaction would inform the TU about the timeout.)
3135
* For INVITE initiated dialogs, terminating the dialog consists of
3139
* according to X, this should terminate dialog usage only, not the
3142
pjsip_transaction *tsx = e->body.tsx_state.tsx;
3144
pj_assert(tsx->role == PJSIP_UAC_ROLE);
3146
/* Note that 481 response to CANCEL does not terminate dialog usage,
3147
* but only the transaction.
3149
if (inv->state != PJSIP_INV_STATE_DISCONNECTED &&
3150
((tsx->status_code == PJSIP_SC_CALL_TSX_DOES_NOT_EXIST &&
3151
tsx->method.id != PJSIP_CANCEL_METHOD) ||
3152
tsx->status_code == PJSIP_SC_REQUEST_TIMEOUT ||
3153
tsx->status_code == PJSIP_SC_TSX_TIMEOUT ||
3154
tsx->status_code == PJSIP_SC_TSX_TRANSPORT_ERROR))
3159
inv_set_cause(inv, tsx->status_code, &tsx->status_text);
3160
inv_set_state(inv, PJSIP_INV_STATE_DISCONNECTED, e);
3163
status = pjsip_dlg_create_request(inv->dlg, pjsip_get_bye_method(),
3165
if (status == PJ_SUCCESS) {
3166
pjsip_inv_send_msg(inv, bye);
3169
return PJ_TRUE; /* Handled */
3172
/* Handle 401/407 challenge. */
3173
else if (tsx->state == PJSIP_TSX_STATE_COMPLETED &&
3174
(tsx->status_code == PJSIP_SC_UNAUTHORIZED ||
3175
tsx->status_code == PJSIP_SC_PROXY_AUTHENTICATION_REQUIRED))
3178
pjsip_tx_data *tdata;
3181
if (tsx->method.id == PJSIP_INVITE_METHOD)
3182
inv->invite_tsx = NULL;
3184
status = pjsip_auth_clt_reinit_req( &inv->dlg->auth_sess,
3185
e->body.tsx_state.src.rdata,
3186
tsx->last_tx, &tdata);
3188
if (status != PJ_SUCCESS) {
3189
/* Somehow failed. Probably it's not a good idea to terminate
3190
* the session since this is just a request within dialog. And
3191
* even if we terminate we should send BYE.
3194
inv_set_cause(inv, PJSIP_SC_OK, NULL);
3195
inv_set_state(inv, PJSIP_INV_STATE_DISCONNECTED, e);
3199
/* Re-send request. */
3200
status = pjsip_inv_send_msg(inv, tdata);
3203
return PJ_TRUE; /* Handled */
3206
/* Handle session timer 422 response. */
3207
else if (tsx->state == PJSIP_TSX_STATE_COMPLETED &&
3208
tsx->status_code == PJSIP_SC_SESSION_TIMER_TOO_SMALL)
3210
handle_timer_response(inv, e->body.tsx_state.src.rdata,
3213
return PJ_TRUE; /* Handled */
3216
return PJ_FALSE; /* Unhandled */
3221
/* Handle call rejection, especially with regard to processing call
3222
* redirection. We need to handle the following scenarios:
3223
* - 3xx response is received -- see if on_redirected() callback is
3224
* implemented. If so, add the Contact URIs in the response to the
3225
* target set and notify user.
3226
* - 4xx - 6xx resposne is received -- see if we're currently recursing,
3227
* if so fetch the next target if any and notify the on_redirected()
3229
* - for other cases -- disconnect the session.
3231
static void handle_uac_call_rejection(pjsip_inv_session *inv, pjsip_event *e)
3233
pjsip_transaction *tsx = e->body.tsx_state.tsx;
3236
if (PJSIP_IS_STATUS_IN_CLASS(tsx->status_code, 300)) {
3238
if (mod_inv.cb.on_redirected == NULL) {
3240
/* Redirection callback is not implemented, disconnect the
3243
goto terminate_session;
3246
const pjsip_msg *res_msg;
3248
res_msg = e->body.tsx_state.src.rdata->msg_info.msg;
3250
/* Gather all Contact URI's in the response and add them
3251
* to target set. The function will take care of removing
3254
pjsip_target_set_add_from_msg(&inv->dlg->target_set,
3255
inv->dlg->pool, res_msg);
3257
/* Recurse to alternate targets if application allows us */
3258
if (!inv_uac_recurse(inv, tsx->status_code, &tsx->status_text, e))
3260
/* Recursion fails, terminate session now */
3261
goto terminate_session;
3267
} else if ((tsx->status_code==401 || tsx->status_code==407) &&
3271
/* Handle authentication failure:
3272
* Resend the request with Authorization header.
3274
pjsip_tx_data *tdata;
3276
status = pjsip_auth_clt_reinit_req(&inv->dlg->auth_sess,
3277
e->body.tsx_state.src.rdata,
3281
if (status != PJ_SUCCESS) {
3283
/* Does not have proper credentials. If we are currently
3284
* recursing, try the next target. Otherwise end the session.
3286
if (!inv_uac_recurse(inv, tsx->status_code, &tsx->status_text, e))
3288
/* Recursion fails, terminate session now */
3289
goto terminate_session;
3294
/* Restart session. */
3295
pjsip_inv_uac_restart(inv, PJ_FALSE);
3297
/* Send the request. */
3298
status = pjsip_inv_send_msg(inv, tdata);
3301
} else if (tsx->state == PJSIP_TSX_STATE_COMPLETED &&
3302
tsx->status_code == PJSIP_SC_SESSION_TIMER_TOO_SMALL)
3304
/* Handle session timer 422 response:
3305
* Resend the request with requested session timer setting.
3307
status = handle_timer_response(inv, e->body.tsx_state.src.rdata,
3310
} else if (PJSIP_IS_STATUS_IN_CLASS(tsx->status_code, 600)) {
3312
goto terminate_session;
3315
/* See if we have alternate target to try */
3316
if (!inv_uac_recurse(inv, tsx->status_code, &tsx->status_text, e)) {
3317
/* Recursion fails, terminate session now */
3318
goto terminate_session;
3324
inv_set_cause(inv, tsx->status_code, &tsx->status_text);
3325
inv_set_state(inv, PJSIP_INV_STATE_DISCONNECTED, e);
3330
* State CALLING is after sending initial INVITE request but before
3331
* any response (with tag) is received.
3333
static void inv_on_state_calling( pjsip_inv_session *inv, pjsip_event *e)
3335
pjsip_transaction *tsx = e->body.tsx_state.tsx;
3336
pjsip_dialog *dlg = pjsip_tsx_get_dlg(tsx);
3339
PJ_ASSERT_ON_FAIL(tsx && dlg, return);
3341
if (tsx == inv->invite_tsx) {
3343
switch (tsx->state) {
3345
case PJSIP_TSX_STATE_CALLING:
3346
inv_set_state(inv, PJSIP_INV_STATE_CALLING, e);
3349
case PJSIP_TSX_STATE_PROCEEDING:
3350
if (inv->pending_cancel) {
3351
pjsip_tx_data *cancel;
3353
inv->pending_cancel = PJ_FALSE;
3355
status = pjsip_inv_end_session(inv, 487, NULL, &cancel);
3356
if (status == PJ_SUCCESS && cancel)
3357
status = pjsip_inv_send_msg(inv, cancel);
3360
if (dlg->remote.info->tag.slen) {
3362
inv_set_state(inv, PJSIP_INV_STATE_EARLY, e);
3364
inv_check_sdp_in_incoming_msg(inv, tsx,
3365
e->body.tsx_state.src.rdata);
3367
if (pjsip_100rel_is_reliable(e->body.tsx_state.src.rdata)) {
3368
inv_handle_incoming_reliable_response(
3369
inv, e->body.tsx_state.src.rdata);
3373
/* Ignore 100 (Trying) response, as it doesn't change
3374
* session state. It only ceases retransmissions.
3379
case PJSIP_TSX_STATE_COMPLETED:
3380
if (tsx->status_code/100 == 2) {
3382
/* This should not happen.
3383
* When transaction receives 2xx, it should be terminated
3387
/* Process session timer response. */
3388
status = handle_timer_response(inv,
3389
e->body.tsx_state.src.rdata,
3391
if (status != PJ_SUCCESS)
3394
inv_set_state(inv, PJSIP_INV_STATE_CONNECTING, e);
3396
inv_check_sdp_in_incoming_msg(inv, tsx,
3397
e->body.tsx_state.src.rdata);
3400
handle_uac_call_rejection(inv, e);
3404
case PJSIP_TSX_STATE_TERMINATED:
3405
/* INVITE transaction can be terminated either because UAC
3406
* transaction received 2xx response or because of transport
3409
if (tsx->status_code/100 == 2) {
3410
/* This must be receipt of 2xx response */
3412
/* Process session timer response. */
3413
status = handle_timer_response(inv,
3414
e->body.tsx_state.src.rdata,
3416
if (status != PJ_SUCCESS)
3419
/* Set state to CONNECTING */
3420
inv_set_state(inv, PJSIP_INV_STATE_CONNECTING, e);
3422
inv_check_sdp_in_incoming_msg(inv, tsx,
3423
e->body.tsx_state.src.rdata);
3426
pj_assert(e->body.tsx_state.type == PJSIP_EVENT_RX_MSG);
3428
inv_send_ack(inv, e);
3431
inv_set_cause(inv, tsx->status_code, &tsx->status_text);
3432
inv_set_state(inv, PJSIP_INV_STATE_DISCONNECTED, e);
3440
} else if (tsx->role == PJSIP_ROLE_UAC) {
3442
* Handle case when outgoing request is answered with 481 (Call/
3443
* Transaction Does Not Exist), 408, or when it's timed out. In these
3444
* cases, disconnect session (i.e. dialog usage only).
3445
* Note that 481 response to CANCEL does not terminate dialog usage,
3446
* but only the transaction.
3448
if ((tsx->status_code == PJSIP_SC_CALL_TSX_DOES_NOT_EXIST &&
3449
tsx->method.id != PJSIP_CANCEL_METHOD) ||
3450
tsx->status_code == PJSIP_SC_REQUEST_TIMEOUT ||
3451
tsx->status_code == PJSIP_SC_TSX_TIMEOUT ||
3452
tsx->status_code == PJSIP_SC_TSX_TRANSPORT_ERROR)
3454
inv_set_cause(inv, tsx->status_code, &tsx->status_text);
3455
inv_set_state(inv, PJSIP_INV_STATE_DISCONNECTED, e);
3461
* State INCOMING is after we received the request, but before
3462
* responses with tag are sent.
3464
static void inv_on_state_incoming( pjsip_inv_session *inv, pjsip_event *e)
3466
pjsip_transaction *tsx = e->body.tsx_state.tsx;
3467
pjsip_dialog *dlg = pjsip_tsx_get_dlg(tsx);
3469
PJ_ASSERT_ON_FAIL(tsx && dlg, return);
3471
if (tsx == inv->invite_tsx) {
3474
* Handle the INVITE state transition.
3477
switch (tsx->state) {
3479
case PJSIP_TSX_STATE_TRYING:
3480
inv_set_state(inv, PJSIP_INV_STATE_INCOMING, e);
3483
case PJSIP_TSX_STATE_PROCEEDING:
3485
* Transaction sent provisional response.
3487
if (tsx->status_code > 100)
3488
inv_set_state(inv, PJSIP_INV_STATE_EARLY, e);
3491
case PJSIP_TSX_STATE_COMPLETED:
3493
* Transaction sent final response.
3495
if (tsx->status_code/100 == 2) {
3496
inv_set_state(inv, PJSIP_INV_STATE_CONNECTING, e);
3498
inv_set_cause(inv, tsx->status_code, &tsx->status_text);
3499
inv_set_state(inv, PJSIP_INV_STATE_DISCONNECTED, e);
3503
case PJSIP_TSX_STATE_TERMINATED:
3505
* This happens on transport error (e.g. failed to send
3508
inv_set_cause(inv, tsx->status_code, &tsx->status_text);
3509
inv_set_state(inv, PJSIP_INV_STATE_DISCONNECTED, e);
3513
pj_assert(!"Unexpected INVITE state");
3517
} else if (tsx->method.id == PJSIP_CANCEL_METHOD &&
3518
tsx->role == PJSIP_ROLE_UAS &&
3519
tsx->state < PJSIP_TSX_STATE_COMPLETED &&
3520
e->body.tsx_state.type == PJSIP_EVENT_RX_MSG )
3524
* Handle incoming CANCEL request.
3527
inv_respond_incoming_cancel(inv, tsx, e->body.tsx_state.src.rdata);
3533
* State EARLY is for both UAS and UAC, after response with To tag
3536
static void inv_on_state_early( pjsip_inv_session *inv, pjsip_event *e)
3538
pjsip_transaction *tsx = e->body.tsx_state.tsx;
3539
pjsip_dialog *dlg = pjsip_tsx_get_dlg(tsx);
3541
PJ_ASSERT_ON_FAIL(tsx && dlg, return);
3543
if (tsx == inv->invite_tsx) {
3546
* Handle the INVITE state progress.
3549
switch (tsx->state) {
3551
case PJSIP_TSX_STATE_PROCEEDING:
3552
/* Send/received another provisional response. */
3553
inv_set_state(inv, PJSIP_INV_STATE_EARLY, e);
3555
if (e->body.tsx_state.type == PJSIP_EVENT_RX_MSG) {
3556
inv_check_sdp_in_incoming_msg(inv, tsx,
3557
e->body.tsx_state.src.rdata);
3559
if (pjsip_100rel_is_reliable(e->body.tsx_state.src.rdata)) {
3560
inv_handle_incoming_reliable_response(
3561
inv, e->body.tsx_state.src.rdata);
3566
case PJSIP_TSX_STATE_COMPLETED:
3567
if (tsx->status_code/100 == 2) {
3568
inv_set_state(inv, PJSIP_INV_STATE_CONNECTING, e);
3569
if (e->body.tsx_state.type == PJSIP_EVENT_RX_MSG) {
3572
/* Process session timer response. */
3573
status = handle_timer_response(inv,
3574
e->body.tsx_state.src.rdata,
3576
if (status != PJ_SUCCESS)
3579
inv_check_sdp_in_incoming_msg(inv, tsx,
3580
e->body.tsx_state.src.rdata);
3583
} else if (tsx->role == PJSIP_ROLE_UAC) {
3585
handle_uac_call_rejection(inv, e);
3588
inv_set_cause(inv, tsx->status_code, &tsx->status_text);
3589
inv_set_state(inv, PJSIP_INV_STATE_DISCONNECTED, e);
3593
case PJSIP_TSX_STATE_CONFIRMED:
3594
/* For some reason can go here (maybe when ACK for 2xx has
3595
* the same branch value as the INVITE transaction) */
3597
case PJSIP_TSX_STATE_TERMINATED:
3598
/* INVITE transaction can be terminated either because UAC
3599
* transaction received 2xx response or because of transport
3602
if (tsx->status_code/100 == 2) {
3604
/* This must be receipt of 2xx response */
3606
/* Set state to CONNECTING */
3607
inv_set_state(inv, PJSIP_INV_STATE_CONNECTING, e);
3609
if (e->body.tsx_state.type == PJSIP_EVENT_RX_MSG) {
3612
/* Process session timer response. */
3613
status = handle_timer_response(inv,
3614
e->body.tsx_state.src.rdata,
3616
if (status != PJ_SUCCESS)
3619
inv_check_sdp_in_incoming_msg(inv, tsx,
3620
e->body.tsx_state.src.rdata);
3623
/* if UAC, send ACK and move state to confirmed. */
3624
if (tsx->role == PJSIP_ROLE_UAC) {
3625
pj_assert(e->body.tsx_state.type == PJSIP_EVENT_RX_MSG);
3627
inv_send_ack(inv, e);
3631
inv_set_cause(inv, tsx->status_code, &tsx->status_text);
3632
inv_set_state(inv, PJSIP_INV_STATE_DISCONNECTED, e);
3637
pj_assert(!"Unexpected INVITE tsx state");
3640
} else if (inv->role == PJSIP_ROLE_UAS &&
3641
tsx->role == PJSIP_ROLE_UAS &&
3642
tsx->method.id == PJSIP_CANCEL_METHOD &&
3643
tsx->state < PJSIP_TSX_STATE_COMPLETED &&
3644
e->body.tsx_state.type == PJSIP_EVENT_RX_MSG )
3648
* Handle incoming CANCEL request.
3651
inv_respond_incoming_cancel(inv, tsx, e->body.tsx_state.src.rdata);
3653
} else if (tsx->role == PJSIP_ROLE_UAS &&
3654
tsx->state == PJSIP_TSX_STATE_TRYING &&
3655
pjsip_method_cmp(&tsx->method, &pjsip_update_method)==0)
3658
* Handle incoming UPDATE
3660
inv_respond_incoming_update(inv, e->body.tsx_state.src.rdata);
3663
} else if (tsx->role == PJSIP_ROLE_UAC &&
3664
(tsx->state == PJSIP_TSX_STATE_COMPLETED ||
3665
tsx->state == PJSIP_TSX_STATE_TERMINATED) &&
3666
pjsip_method_cmp(&tsx->method, &pjsip_update_method)==0)
3669
* Handle response to outgoing UPDATE request.
3671
inv_handle_update_response(inv, e);
3673
} else if (tsx->role == PJSIP_ROLE_UAS &&
3674
tsx->state == PJSIP_TSX_STATE_TRYING &&
3675
pjsip_method_cmp(&tsx->method, &pjsip_prack_method)==0)
3678
* Handle incoming PRACK
3680
inv_respond_incoming_prack(inv, e->body.tsx_state.src.rdata);
3682
} else if (tsx->role == PJSIP_ROLE_UAC) {
3684
/* Generic handling for UAC tsx completion */
3685
handle_uac_tsx_response(inv, e);
3687
} else if (tsx->role == PJSIP_ROLE_UAS &&
3688
tsx->method.id == PJSIP_BYE_METHOD &&
3689
tsx->status_code < 200 &&
3690
e->body.tsx_state.type == PJSIP_EVENT_RX_MSG)
3692
/* Received BYE before the 2xx/OK response to INVITE.
3693
* Assume that the 2xx/OK response is lost and the BYE
3696
inv_respond_incoming_bye(inv, tsx, e->body.tsx_state.src.rdata, e);
3698
if (inv->invite_tsx->role == PJSIP_ROLE_UAC) {
3699
/* Set timer just in case we will never get the final response
3702
pjsip_tsx_set_timeout(inv->invite_tsx, 64*pjsip_cfg()->tsx.t1);
3703
} else if (inv->invite_tsx->status_code < 200) {
3704
pjsip_tx_data *tdata;
3707
/* For UAS, send a final response. */
3708
tdata = inv->invite_tsx->last_tx;
3709
PJ_ASSERT_ON_FAIL(tdata != NULL, return);
3712
msg->line.status.code = PJSIP_SC_REQUEST_TERMINATED;
3713
msg->line.status.reason =
3714
*pjsip_get_status_text(PJSIP_SC_REQUEST_TERMINATED);
3717
pjsip_tx_data_invalidate_msg(tdata);
3718
pjsip_tx_data_add_ref(tdata);
3720
pjsip_dlg_send_response(inv->dlg, inv->invite_tsx, tdata);
3726
* State CONNECTING is after 2xx response to INVITE is sent/received.
3728
static void inv_on_state_connecting( pjsip_inv_session *inv, pjsip_event *e)
3730
pjsip_transaction *tsx = e->body.tsx_state.tsx;
3731
pjsip_dialog *dlg = pjsip_tsx_get_dlg(tsx);
3733
PJ_ASSERT_ON_FAIL(tsx && dlg, return);
3735
if (tsx == inv->invite_tsx) {
3738
* Handle INVITE state progression.
3740
switch (tsx->state) {
3742
case PJSIP_TSX_STATE_CONFIRMED:
3743
/* It can only go here if incoming ACK request has the same Via
3744
* branch parameter as the INVITE transaction.
3746
if (tsx->status_code/100 == 2) {
3747
if (e->body.tsx_state.type == PJSIP_EVENT_RX_MSG) {
3748
inv_check_sdp_in_incoming_msg(inv, tsx,
3749
e->body.tsx_state.src.rdata);
3752
inv_set_state(inv, PJSIP_INV_STATE_CONFIRMED, e);
3756
case PJSIP_TSX_STATE_TERMINATED:
3757
/* INVITE transaction can be terminated either because UAC
3758
* transaction received 2xx response or because of transport
3761
if (tsx->status_code/100 != 2) {
3762
if (tsx->role == PJSIP_ROLE_UAC) {
3763
inv_set_cause(inv, tsx->status_code, &tsx->status_text);
3764
inv_set_state(inv, PJSIP_INV_STATE_DISCONNECTED, e);
3770
status = pjsip_dlg_create_request(inv->dlg,
3771
pjsip_get_bye_method(),
3773
if (status == PJ_SUCCESS) {
3774
pjsip_inv_send_msg(inv, bye);
3780
case PJSIP_TSX_STATE_DESTROYED:
3785
pj_assert(!"Unexpected state");
3788
} else if (tsx->role == PJSIP_ROLE_UAS &&
3789
tsx->method.id == PJSIP_BYE_METHOD &&
3790
tsx->status_code < 200 &&
3791
e->body.tsx_state.type == PJSIP_EVENT_RX_MSG)
3795
* Handle incoming BYE.
3798
inv_respond_incoming_bye( inv, tsx, e->body.tsx_state.src.rdata, e );
3800
} else if (tsx->method.id == PJSIP_BYE_METHOD &&
3801
tsx->role == PJSIP_ROLE_UAC &&
3802
(tsx->state == PJSIP_TSX_STATE_COMPLETED ||
3803
tsx->state == PJSIP_TSX_STATE_TERMINATED))
3809
inv_handle_bye_response( inv, tsx, e->body.tsx_state.src.rdata, e);
3812
else if (tsx->method.id == PJSIP_CANCEL_METHOD &&
3813
tsx->role == PJSIP_ROLE_UAS &&
3814
tsx->status_code < 200 &&
3815
e->body.tsx_state.type == PJSIP_EVENT_RX_MSG)
3819
* Handle strandled incoming CANCEL.
3821
pjsip_rx_data *rdata = e->body.tsx_state.src.rdata;
3822
pjsip_tx_data *tdata;
3825
status = pjsip_dlg_create_response(dlg, rdata, 200, NULL, &tdata);
3826
if (status != PJ_SUCCESS) return;
3828
status = pjsip_dlg_send_response(dlg, tsx, tdata);
3829
if (status != PJ_SUCCESS) return;
3831
} else if (tsx->role == PJSIP_ROLE_UAS &&
3832
tsx->state == PJSIP_TSX_STATE_TRYING &&
3833
pjsip_method_cmp(&tsx->method, &pjsip_update_method)==0)
3836
* Handle incoming UPDATE
3838
inv_respond_incoming_update(inv, e->body.tsx_state.src.rdata);
3841
} else if (tsx->role == PJSIP_ROLE_UAC &&
3842
(tsx->state == PJSIP_TSX_STATE_COMPLETED ||
3843
tsx->state == PJSIP_TSX_STATE_TERMINATED) &&
3844
pjsip_method_cmp(&tsx->method, &pjsip_update_method)==0)
3847
* Handle response to outgoing UPDATE request.
3849
if (handle_uac_tsx_response(inv, e) == PJ_FALSE)
3850
inv_handle_update_response(inv, e);
3852
} else if (tsx->role == PJSIP_ROLE_UAS &&
3853
tsx->state == PJSIP_TSX_STATE_TRYING &&
3854
pjsip_method_cmp(&tsx->method, &pjsip_prack_method)==0)
3857
* Handle incoming PRACK
3859
inv_respond_incoming_prack(inv, e->body.tsx_state.src.rdata);
3861
} else if (tsx->role == PJSIP_ROLE_UAC) {
3863
/* Generic handling for UAC tsx completion */
3864
handle_uac_tsx_response(inv, e);
3870
* State CONFIRMED is after ACK is sent/received.
3872
static void inv_on_state_confirmed( pjsip_inv_session *inv, pjsip_event *e)
3874
pjsip_transaction *tsx = e->body.tsx_state.tsx;
3875
pjsip_dialog *dlg = pjsip_tsx_get_dlg(tsx);
3877
PJ_ASSERT_ON_FAIL(tsx && dlg, return);
3880
if (tsx->method.id == PJSIP_BYE_METHOD &&
3881
tsx->role == PJSIP_ROLE_UAC &&
3882
(tsx->state == PJSIP_TSX_STATE_COMPLETED ||
3883
tsx->state == PJSIP_TSX_STATE_TERMINATED))
3890
inv_handle_bye_response( inv, tsx, e->body.tsx_state.src.rdata, e);
3893
else if (tsx->method.id == PJSIP_BYE_METHOD &&
3894
tsx->role == PJSIP_ROLE_UAS &&
3895
tsx->status_code < 200 &&
3896
e->body.tsx_state.type == PJSIP_EVENT_RX_MSG)
3900
* Handle incoming BYE.
3903
inv_respond_incoming_bye( inv, tsx, e->body.tsx_state.src.rdata, e );
3906
else if (tsx->method.id == PJSIP_CANCEL_METHOD &&
3907
tsx->role == PJSIP_ROLE_UAS &&
3908
tsx->status_code < 200 &&
3909
e->body.tsx_state.type == PJSIP_EVENT_RX_MSG)
3913
* Handle strandled incoming CANCEL.
3915
pjsip_rx_data *rdata = e->body.tsx_state.src.rdata;
3916
pjsip_tx_data *tdata;
3919
status = pjsip_dlg_create_response(dlg, rdata, 200, NULL, &tdata);
3920
if (status != PJ_SUCCESS) return;
3922
status = pjsip_dlg_send_response(dlg, tsx, tdata);
3923
if (status != PJ_SUCCESS) return;
3926
else if (tsx->method.id == PJSIP_INVITE_METHOD &&
3927
tsx->role == PJSIP_ROLE_UAS)
3931
* Handle incoming re-INVITE
3933
if (tsx->state == PJSIP_TSX_STATE_TRYING) {
3935
pjsip_rx_data *rdata = e->body.tsx_state.src.rdata;
3936
pjsip_tx_data *tdata;
3938
pjsip_rdata_sdp_info *sdp_info;
3939
pjsip_status_code st_code;
3941
/* Check if we have INVITE pending. */
3942
if (inv->invite_tsx && inv->invite_tsx!=tsx) {
3946
reason = pj_str("Another INVITE transaction in progress");
3948
if (inv->invite_tsx->role == PJSIP_ROLE_UAC)
3953
/* Can not receive re-INVITE while another one is pending. */
3954
status = pjsip_dlg_create_response( inv->dlg, rdata, code,
3956
if (status != PJ_SUCCESS)
3960
/* MUST include Retry-After header with random value
3963
pjsip_retry_after_hdr *ra_hdr;
3964
int val = (pj_rand() % 10);
3966
ra_hdr = pjsip_retry_after_hdr_create(tdata->pool, val);
3967
pjsip_msg_add_hdr(tdata->msg, (pjsip_hdr*)ra_hdr);
3970
status = pjsip_dlg_send_response( inv->dlg, tsx, tdata);
3976
/* Save the invite transaction. */
3977
inv->invite_tsx = tsx;
3979
/* Process session timers headers in the re-INVITE */
3980
status = pjsip_timer_process_req(inv, rdata, &st_code);
3981
if (status != PJ_SUCCESS) {
3982
status = pjsip_dlg_create_response(inv->dlg, rdata, st_code,
3984
if (status != PJ_SUCCESS)
3987
pjsip_timer_update_resp(inv, tdata);
3988
status = pjsip_dlg_send_response(dlg, tsx, tdata);
3992
/* Send 491 if we receive re-INVITE while another offer/answer
3993
* negotiation is in progress
3995
if (pjmedia_sdp_neg_get_state(inv->neg) !=
3996
PJMEDIA_SDP_NEG_STATE_DONE)
3998
status = pjsip_dlg_create_response(inv->dlg, rdata,
3999
PJSIP_SC_REQUEST_PENDING,
4001
if (status != PJ_SUCCESS)
4003
pjsip_timer_update_resp(inv, tdata);
4004
status = pjsip_dlg_send_response(dlg, tsx, tdata);
4008
/* Process SDP in incoming message. */
4009
status = inv_check_sdp_in_incoming_msg(inv, tsx, rdata);
4011
if (status != PJ_SUCCESS) {
4013
/* Not Acceptable */
4014
const pjsip_hdr *accept;
4016
status = pjsip_dlg_create_response(inv->dlg, rdata,
4018
if (status != PJ_SUCCESS)
4022
accept = pjsip_endpt_get_capability(dlg->endpt, PJSIP_H_ACCEPT,
4025
pjsip_msg_add_hdr(tdata->msg, (pjsip_hdr*)
4026
pjsip_hdr_clone(tdata->pool, accept));
4029
status = pjsip_dlg_send_response(dlg, tsx, tdata);
4034
/* Create 2xx ANSWER */
4035
status = pjsip_dlg_create_response(dlg, rdata, 200, NULL, &tdata);
4036
if (status != PJ_SUCCESS)
4039
/* If the INVITE request has SDP body, send answer.
4040
* Otherwise generate offer from local active SDP.
4042
sdp_info = pjsip_rdata_get_sdp_info(rdata);
4043
if (sdp_info->sdp != NULL) {
4044
status = process_answer(inv, 200, tdata, NULL);
4046
/* INVITE does not have SDP.
4047
* If on_create_offer() callback is implemented, ask app.
4048
* to generate an offer, otherwise just send active local
4049
* SDP to signal that nothing gets modified.
4051
pjmedia_sdp_session *sdp = NULL;
4053
if (mod_inv.cb.on_create_offer) {
4054
(*mod_inv.cb.on_create_offer)(inv, &sdp);
4056
/* Notify negotiator about the new offer. This will
4057
* fix the offer with correct SDP origin.
4060
pjmedia_sdp_neg_modify_local_offer(inv->pool_prov,
4064
/* Retrieve the "fixed" offer from negotiator */
4065
if (status==PJ_SUCCESS) {
4066
const pjmedia_sdp_session *lsdp = NULL;
4067
pjmedia_sdp_neg_get_neg_local(inv->neg, &lsdp);
4068
sdp = (pjmedia_sdp_session*)lsdp;
4074
const pjmedia_sdp_session *active_sdp = NULL;
4075
status = pjmedia_sdp_neg_send_local_offer(inv->pool_prov,
4078
if (status == PJ_SUCCESS)
4079
sdp = (pjmedia_sdp_session*) active_sdp;
4083
tdata->msg->body = create_sdp_body(tdata->pool, sdp);
4087
if (status != PJ_SUCCESS) {
4089
* SDP negotiation has failed.
4094
/* Delete the 2xx answer */
4095
pjsip_tx_data_dec_ref(tdata);
4097
/* Create 500 response */
4098
reason = pj_str("SDP negotiation failed");
4099
rc = pjsip_dlg_create_response(dlg, rdata, 500, &reason,
4101
if (rc == PJ_SUCCESS) {
4102
pjsip_warning_hdr *w;
4103
const pj_str_t *endpt_name;
4105
endpt_name = pjsip_endpt_name(dlg->endpt);
4106
w = pjsip_warning_hdr_create_from_status(tdata->pool,
4110
pjsip_msg_add_hdr(tdata->msg, (pjsip_hdr*)w);
4112
pjsip_inv_send_msg(inv, tdata);
4117
/* Invoke Session Timers */
4118
pjsip_timer_update_resp(inv, tdata);
4120
/* Send 2xx regardless of the status of negotiation */
4121
status = pjsip_inv_send_msg(inv, tdata);
4123
} else if (tsx->state == PJSIP_TSX_STATE_CONFIRMED) {
4124
/* This is the case where ACK has the same branch as
4125
* the INVITE request.
4127
if (tsx->status_code/100 == 2 &&
4128
e->body.tsx_state.type == PJSIP_EVENT_RX_MSG)
4130
inv_check_sdp_in_incoming_msg(inv, tsx,
4131
e->body.tsx_state.src.rdata);
4133
/* Check if local offer got no SDP answer */
4134
if (pjmedia_sdp_neg_get_state(inv->neg)==
4135
PJMEDIA_SDP_NEG_STATE_LOCAL_OFFER)
4137
pjmedia_sdp_neg_cancel_offer(inv->neg);
4144
else if (tsx->method.id == PJSIP_INVITE_METHOD &&
4145
tsx->role == PJSIP_ROLE_UAC)
4149
* Handle outgoing re-INVITE
4151
if (tsx->state == PJSIP_TSX_STATE_CALLING) {
4153
/* Must not have other pending INVITE transaction */
4154
pj_assert(inv->invite_tsx==NULL || tsx==inv->invite_tsx);
4156
/* Save pending invite transaction */
4157
inv->invite_tsx = tsx;
4159
} else if (tsx->state == PJSIP_TSX_STATE_TERMINATED &&
4160
tsx->status_code/100 == 2)
4164
/* Re-INVITE was accepted. */
4166
/* Process session timer response. */
4167
status = handle_timer_response(inv,
4168
e->body.tsx_state.src.rdata,
4170
if (status != PJ_SUCCESS)
4174
inv_check_sdp_in_incoming_msg(inv, tsx,
4175
e->body.tsx_state.src.rdata);
4177
/* Check if local offer got no SDP answer */
4178
if (pjmedia_sdp_neg_get_state(inv->neg)==
4179
PJMEDIA_SDP_NEG_STATE_LOCAL_OFFER)
4181
pjmedia_sdp_neg_cancel_offer(inv->neg);
4185
inv_send_ack(inv, e);
4187
} else if (handle_uac_tsx_response(inv, e)) {
4189
/* Handle response that terminates dialog */
4190
/* Nothing to do (already handled) */
4192
} else if (tsx->status_code >= 300 && tsx->status_code < 700) {
4194
pjmedia_sdp_neg_state neg_state;
4196
/* Outgoing INVITE transaction has failed, cancel SDP nego */
4197
neg_state = pjmedia_sdp_neg_get_state(inv->neg);
4198
if (neg_state == PJMEDIA_SDP_NEG_STATE_LOCAL_OFFER) {
4199
pjmedia_sdp_neg_cancel_offer(inv->neg);
4202
if (tsx == inv->invite_tsx)
4203
inv->invite_tsx = NULL;
4206
} else if (tsx->role == PJSIP_ROLE_UAS &&
4207
tsx->state == PJSIP_TSX_STATE_TRYING &&
4208
pjsip_method_cmp(&tsx->method, &pjsip_update_method)==0)
4211
* Handle incoming UPDATE
4213
inv_respond_incoming_update(inv, e->body.tsx_state.src.rdata);
4215
} else if (tsx->role == PJSIP_ROLE_UAC &&
4216
(tsx->state == PJSIP_TSX_STATE_COMPLETED ||
4217
tsx->state == PJSIP_TSX_STATE_TERMINATED) &&
4218
pjsip_method_cmp(&tsx->method, &pjsip_update_method)==0)
4221
* Handle response to outgoing UPDATE request.
4223
if (handle_uac_tsx_response(inv, e) == PJ_FALSE)
4224
inv_handle_update_response(inv, e);
4226
} else if (tsx->role == PJSIP_ROLE_UAS &&
4227
tsx->state == PJSIP_TSX_STATE_TRYING &&
4228
pjsip_method_cmp(&tsx->method, &pjsip_prack_method)==0)
4231
* Handle strandled incoming PRACK
4233
inv_respond_incoming_prack(inv, e->body.tsx_state.src.rdata);
4235
} else if (tsx->role == PJSIP_ROLE_UAC) {
4237
* Handle 401/407/408/481/422 response
4239
handle_uac_tsx_response(inv, e);
4245
* After session has been terminated, but before dialog is destroyed
4246
* (because dialog has other usages, or because dialog is waiting for
4247
* the last transaction to terminate).
4249
static void inv_on_state_disconnected( pjsip_inv_session *inv, pjsip_event *e)
4251
pjsip_transaction *tsx = e->body.tsx_state.tsx;
4252
pjsip_dialog *dlg = pjsip_tsx_get_dlg(tsx);
4254
PJ_ASSERT_ON_FAIL(tsx && dlg, return);
4256
if (tsx->role == PJSIP_ROLE_UAS &&
4257
tsx->status_code < 200 &&
4258
e->body.tsx_state.type == PJSIP_EVENT_RX_MSG)
4260
pjsip_rx_data *rdata = e->body.tsx_state.src.rdata;
4263
* Respond BYE with 200/OK
4265
if (tsx->method.id == PJSIP_BYE_METHOD) {
4266
inv_respond_incoming_bye( inv, tsx, rdata, e );
4267
} else if (tsx->method.id == PJSIP_CANCEL_METHOD) {
4269
* Respond CANCEL with 200/OK too.
4271
pjsip_tx_data *tdata;
4274
status = pjsip_dlg_create_response(dlg, rdata, 200, NULL, &tdata);
4275
if (status != PJ_SUCCESS) return;
4277
status = pjsip_dlg_send_response(dlg, tsx, tdata);
4278
if (status != PJ_SUCCESS) return;
4282
} else if (tsx->role == PJSIP_ROLE_UAC) {
4284
* Handle 401/407/408/481/422 response
4286
handle_uac_tsx_response(inv, e);