~ubuntu-branches/ubuntu/wily/sflphone/wily

« back to all changes in this revision

Viewing changes to daemon/libs/pjproject/pjsip/src/pjsip-ua/sip_inv.c

  • Committer: Package Import Robot
  • Author(s): Francois Marier
  • Date: 2011-11-25 13:24:12 UTC
  • mfrom: (4.1.10 sid)
  • Revision ID: package-import@ubuntu.com-20111125132412-dc4qvhyosk74cd42
Tags: 1.0.1-4
Don't assume that arch:all packages will get built (closes: #649726)

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/* $Id: sip_inv.c 3553 2011-05-05 06:14:19Z nanang $ */
 
2
/* 
 
3
 * Copyright (C) 2008-2011 Teluu Inc. (http://www.teluu.com)
 
4
 * Copyright (C) 2003-2008 Benny Prijono <benny@prijono.org>
 
5
 *
 
6
 * This program is free software; you can redistribute it and/or modify
 
7
 * it under the terms of the GNU General Public License as published by
 
8
 * the Free Software Foundation; either version 2 of the License, or
 
9
 * (at your option) any later version.
 
10
 *
 
11
 * This program is distributed in the hope that it will be useful,
 
12
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 
13
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
14
 * GNU General Public License for more details.
 
15
 *
 
16
 * You should have received a copy of the GNU General Public License
 
17
 * along with this program; if not, write to the Free Software
 
18
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA 
 
19
 */
 
20
#include <pjsip-ua/sip_inv.h>
 
21
#include <pjsip-ua/sip_100rel.h>
 
22
#include <pjsip-ua/sip_timer.h>
 
23
#include <pjsip/sip_module.h>
 
24
#include <pjsip/sip_endpoint.h>
 
25
#include <pjsip/sip_event.h>
 
26
#include <pjsip/sip_multipart.h>
 
27
#include <pjsip/sip_transaction.h>
 
28
#include <pjmedia/sdp.h>
 
29
#include <pjmedia/sdp_neg.h>
 
30
#include <pjmedia/errno.h>
 
31
#include <pj/string.h>
 
32
#include <pj/pool.h>
 
33
#include <pj/assert.h>
 
34
#include <pj/os.h>
 
35
#include <pj/log.h>
 
36
#include <pj/rand.h>
 
37
 
 
38
/* 
 
39
 * Note on offer/answer:
 
40
 *
 
41
 * The offer/answer framework in this implementation assumes the occurence
 
42
 * of SDP in a particular request/response according to this table:
 
43
 
 
44
                  offer   answer    Note:
 
45
    ========================================================================
 
46
    INVITE          X               INVITE may contain offer
 
47
    18x/INVITE      X       X       Response may contain offer or answer
 
48
    2xx/INVITE      X       X       Response may contain offer or answer
 
49
    ACK                     X       ACK may contain answer
 
50
 
 
51
    PRACK                   X       PRACK can only contain answer
 
52
    2xx/PRACK                       Response may not have offer nor answer
 
53
 
 
54
    UPDATE          X               UPDATE may only contain offer
 
55
    2xx/UPDATE              X       Response may only contain answer
 
56
    ========================================================================
 
57
 
 
58
  *
 
59
  */
 
60
 
 
61
#define THIS_FILE       "sip_inv.c"
 
62
 
 
63
static const char *inv_state_names[] =
 
64
{
 
65
    "NULL",
 
66
    "CALLING",
 
67
    "INCOMING",
 
68
    "EARLY",
 
69
    "CONNECTING",
 
70
    "CONFIRMED",
 
71
    "DISCONNCTD",
 
72
    "TERMINATED",
 
73
};
 
74
 
 
75
/* UPDATE method */
 
76
static const pjsip_method pjsip_update_method =
 
77
{
 
78
    PJSIP_OTHER_METHOD,
 
79
    { "UPDATE", 6 }
 
80
};
 
81
 
 
82
#define POOL_INIT_SIZE  256
 
83
#define POOL_INC_SIZE   256
 
84
 
 
85
/*
 
86
 * Static prototypes.
 
87
 */
 
88
static pj_status_t mod_inv_load(pjsip_endpoint *endpt);
 
89
static pj_status_t mod_inv_unload(void);
 
90
static pj_bool_t   mod_inv_on_rx_request(pjsip_rx_data *rdata);
 
91
static pj_bool_t   mod_inv_on_rx_response(pjsip_rx_data *rdata);
 
92
static void        mod_inv_on_tsx_state(pjsip_transaction*, pjsip_event*);
 
93
 
 
94
static void inv_on_state_null( pjsip_inv_session *inv, pjsip_event *e);
 
95
static void inv_on_state_calling( pjsip_inv_session *inv, pjsip_event *e);
 
96
static void inv_on_state_incoming( pjsip_inv_session *inv, pjsip_event *e);
 
97
static void inv_on_state_early( pjsip_inv_session *inv, pjsip_event *e);
 
98
static void inv_on_state_connecting( pjsip_inv_session *inv, pjsip_event *e);
 
99
static void inv_on_state_confirmed( pjsip_inv_session *inv, pjsip_event *e);
 
100
static void inv_on_state_disconnected( pjsip_inv_session *inv, pjsip_event *e);
 
101
 
 
102
static pj_status_t inv_check_sdp_in_incoming_msg( pjsip_inv_session *inv,
 
103
                                                  pjsip_transaction *tsx,
 
104
                                                  pjsip_rx_data *rdata);
 
105
static pj_status_t inv_negotiate_sdp( pjsip_inv_session *inv );
 
106
static pjsip_msg_body *create_sdp_body(pj_pool_t *pool,
 
107
                                       const pjmedia_sdp_session *c_sdp);
 
108
static pj_status_t process_answer( pjsip_inv_session *inv,
 
109
                                   int st_code,
 
110
                                   pjsip_tx_data *tdata,
 
111
                                   const pjmedia_sdp_session *local_sdp);
 
112
 
 
113
static pj_status_t handle_timer_response(pjsip_inv_session *inv,
 
114
                                         const pjsip_rx_data *rdata,
 
115
                                         pj_bool_t end_sess_on_failure);
 
116
 
 
117
static void (*inv_state_handler[])( pjsip_inv_session *inv, pjsip_event *e) = 
 
118
{
 
119
    &inv_on_state_null,
 
120
    &inv_on_state_calling,
 
121
    &inv_on_state_incoming,
 
122
    &inv_on_state_early,
 
123
    &inv_on_state_connecting,
 
124
    &inv_on_state_confirmed,
 
125
    &inv_on_state_disconnected,
 
126
};
 
127
 
 
128
static struct mod_inv
 
129
{
 
130
    pjsip_module         mod;
 
131
    pjsip_endpoint      *endpt;
 
132
    pjsip_inv_callback   cb;
 
133
} mod_inv = 
 
134
{
 
135
    {
 
136
        NULL, NULL,                         /* prev, next.              */
 
137
        { "mod-invite", 10 },               /* Name.                    */
 
138
        -1,                                 /* Id                       */
 
139
        PJSIP_MOD_PRIORITY_DIALOG_USAGE,    /* Priority                 */
 
140
        &mod_inv_load,                      /* load()                   */
 
141
        NULL,                               /* start()                  */
 
142
        NULL,                               /* stop()                   */
 
143
        &mod_inv_unload,                    /* unload()                 */
 
144
        &mod_inv_on_rx_request,             /* on_rx_request()          */
 
145
        &mod_inv_on_rx_response,            /* on_rx_response()         */
 
146
        NULL,                               /* on_tx_request.           */
 
147
        NULL,                               /* on_tx_response()         */
 
148
        &mod_inv_on_tsx_state,              /* on_tsx_state()           */
 
149
    }
 
150
};
 
151
 
 
152
 
 
153
/* Invite session data to be attached to transaction. */
 
154
struct tsx_inv_data
 
155
{
 
156
    pjsip_inv_session   *inv;       /* The invite session                   */
 
157
    pj_bool_t            sdp_done;  /* SDP negotiation done for this tsx?   */
 
158
    pj_str_t             done_tag;  /* To tag in RX response with answer    */
 
159
    pj_bool_t            done_early;/* Negotiation was done for early med?  */
 
160
};
 
161
 
 
162
/*
 
163
 * Module load()
 
164
 */
 
165
static pj_status_t mod_inv_load(pjsip_endpoint *endpt)
 
166
{
 
167
    pj_str_t allowed[] = {{"INVITE", 6}, {"ACK",3}, {"BYE",3}, {"CANCEL",6},
 
168
                            { "UPDATE", 6}};
 
169
    pj_str_t accepted = { "application/sdp", 15 };
 
170
 
 
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);
 
174
 
 
175
    /* Register "application/sdp" in Accept header */
 
176
    pjsip_endpt_add_capability(endpt, &mod_inv.mod, PJSIP_H_ACCEPT, NULL,
 
177
                               1, &accepted);
 
178
 
 
179
    return PJ_SUCCESS;
 
180
}
 
181
 
 
182
/*
 
183
 * Module unload()
 
184
 */
 
185
static pj_status_t mod_inv_unload(void)
 
186
{
 
187
    /* Should remove capability here */
 
188
    return PJ_SUCCESS;
 
189
}
 
190
 
 
191
/*
 
192
 * Set session state.
 
193
 */
 
194
void inv_set_state(pjsip_inv_session *inv, pjsip_inv_state state,
 
195
                   pjsip_event *e)
 
196
{
 
197
    pjsip_inv_state prev_state = inv->state;
 
198
    pj_status_t status;
 
199
 
 
200
 
 
201
    /* If state is confirmed, check that SDP negotiation is done,
 
202
     * otherwise disconnect the session.
 
203
     */
 
204
    if (state == PJSIP_INV_STATE_CONFIRMED) {
 
205
        if (pjmedia_sdp_neg_get_state(inv->neg)!=PJMEDIA_SDP_NEG_STATE_DONE) {
 
206
            pjsip_tx_data *bye;
 
207
 
 
208
            PJ_LOG(4,(inv->obj_name, "SDP offer/answer incomplete, ending the "
 
209
                      "session"));
 
210
 
 
211
            status = pjsip_inv_end_session(inv, PJSIP_SC_NOT_ACCEPTABLE, 
 
212
                                           NULL, &bye);
 
213
            if (status == PJ_SUCCESS && bye)
 
214
                status = pjsip_inv_send_msg(inv, bye);
 
215
 
 
216
            return;
 
217
        }
 
218
    }
 
219
 
 
220
    /* Set state. */
 
221
    inv->state = state;
 
222
 
 
223
    /* If state is DISCONNECTED, cause code MUST have been set. */
 
224
    pj_assert(inv->state != PJSIP_INV_STATE_DISCONNECTED ||
 
225
              inv->cause != 0);
 
226
 
 
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);
 
230
 
 
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) 
 
234
    {
 
235
        if (inv->last_ack) {
 
236
            pjsip_tx_data_dec_ref(inv->last_ack);
 
237
            inv->last_ack = NULL;
 
238
        }
 
239
        if (inv->invite_req) {
 
240
            pjsip_tx_data_dec_ref(inv->invite_req);
 
241
            inv->invite_req = NULL;
 
242
        }
 
243
        pjsip_100rel_end_session(inv);
 
244
        pjsip_timer_end_session(inv);
 
245
        pjsip_dlg_dec_session(inv->dlg, &mod_inv.mod);
 
246
 
 
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;
 
252
    }
 
253
}
 
254
 
 
255
 
 
256
/*
 
257
 * Set cause code.
 
258
 */
 
259
void inv_set_cause(pjsip_inv_session *inv, int cause_code,
 
260
                   const pj_str_t *cause_text)
 
261
{
 
262
    if (cause_code > inv->cause) {
 
263
        inv->cause = (pjsip_status_code) cause_code;
 
264
        if (cause_text)
 
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");
 
268
        else
 
269
            inv->cause_text = *pjsip_get_status_text(cause_code);
 
270
    }
 
271
}
 
272
 
 
273
 
 
274
/*
 
275
 * Check if outgoing request needs to have SDP answer.
 
276
 * This applies for both ACK and PRACK requests.
 
277
 */
 
278
static const pjmedia_sdp_session *inv_has_pending_answer(pjsip_inv_session *inv,
 
279
                                                         pjsip_transaction *tsx)
 
280
{
 
281
    pjmedia_sdp_neg_state neg_state;
 
282
    const pjmedia_sdp_session *sdp = NULL;
 
283
    pj_status_t status;
 
284
 
 
285
    /* If SDP negotiator is ready, start negotiation. */
 
286
 
 
287
    /* Start nego when appropriate. */
 
288
    neg_state = inv->neg ? pjmedia_sdp_neg_get_state(inv->neg) :
 
289
                PJMEDIA_SDP_NEG_STATE_NULL;
 
290
 
 
291
    if (neg_state == PJMEDIA_SDP_NEG_STATE_DONE) {
 
292
 
 
293
        /* Nothing to do */
 
294
 
 
295
    } else if (neg_state == PJMEDIA_SDP_NEG_STATE_WAIT_NEGO &&
 
296
               pjmedia_sdp_neg_has_local_answer(inv->neg) )
 
297
    {
 
298
        struct tsx_inv_data *tsx_inv_data;
 
299
        struct tsx_inv_data dummy;
 
300
 
 
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.
 
305
         */
 
306
        if (tsx) {
 
307
            tsx_inv_data = (struct tsx_inv_data*)tsx->mod_data[mod_inv.mod.id];
 
308
        } else {
 
309
            tsx_inv_data = &dummy;
 
310
            pj_bzero(&dummy, sizeof(dummy));
 
311
            dummy.inv = inv;
 
312
        }
 
313
 
 
314
        status = inv_negotiate_sdp(inv);
 
315
        if (status != PJ_SUCCESS)
 
316
            return NULL;
 
317
        
 
318
        /* Mark this transaction has having SDP offer/answer done. */
 
319
        tsx_inv_data->sdp_done = 1;
 
320
 
 
321
        status = pjmedia_sdp_neg_get_active_local(inv->neg, &sdp);
 
322
 
 
323
    } else {
 
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)));
 
329
         */
 
330
    }
 
331
 
 
332
    return sdp;
 
333
}
 
334
 
 
335
 
 
336
/*
 
337
 * Send ACK for 2xx response.
 
338
 */
 
339
static pj_status_t inv_send_ack(pjsip_inv_session *inv, pjsip_event *e)
 
340
{
 
341
    pjsip_rx_data *rdata;
 
342
    pj_status_t status;
 
343
 
 
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;
 
348
    else {
 
349
        pj_assert(!"Unsupported event type");
 
350
        return PJ_EBUG;
 
351
    }
 
352
 
 
353
    PJ_LOG(5,(inv->obj_name, "Received %s, sending ACK",
 
354
              pjsip_rx_data_get_info(rdata)));
 
355
 
 
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)
 
358
     */
 
359
    if (inv->last_ack && rdata->msg_info.cseq->cseq == inv->last_ack_cseq &&
 
360
        !inv->last_ack->is_pending)
 
361
    {
 
362
        pjsip_tx_data_add_ref(inv->last_ack);
 
363
 
 
364
    } else if (mod_inv.cb.on_send_ack) {
 
365
        /* If application handles ACK transmission manually, just notify the
 
366
         * callback
 
367
         */
 
368
        PJ_LOG(5,(inv->obj_name, "Received %s, notifying application callback",
 
369
                  pjsip_rx_data_get_info(rdata)));
 
370
 
 
371
        (*mod_inv.cb.on_send_ack)(inv, rdata);
 
372
        return PJ_SUCCESS;
 
373
 
 
374
    } else {
 
375
        status = pjsip_inv_create_ack(inv, rdata->msg_info.cseq->cseq,
 
376
                                      &inv->last_ack);
 
377
    }
 
378
 
 
379
    /* Send ACK */
 
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!");
 
384
        return status;
 
385
    }
 
386
 
 
387
 
 
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.
 
391
     */
 
392
    if (inv->state < PJSIP_INV_STATE_CONFIRMED) {
 
393
        inv_set_state(inv, PJSIP_INV_STATE_CONFIRMED, e);
 
394
    }
 
395
 
 
396
    return PJ_SUCCESS;
 
397
}
 
398
 
 
399
/*
 
400
 * Module on_rx_request()
 
401
 *
 
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.
 
406
 */
 
407
static pj_bool_t mod_inv_on_rx_request(pjsip_rx_data *rdata)
 
408
{
 
409
    pjsip_method *method;
 
410
    pjsip_dialog *dlg;
 
411
    pjsip_inv_session *inv;
 
412
 
 
413
    /* Only wants to receive request from a dialog. */
 
414
    dlg = pjsip_rdata_get_dlg(rdata);
 
415
    if (dlg == NULL)
 
416
        return PJ_FALSE;
 
417
 
 
418
    inv = (pjsip_inv_session*) dlg->mod_data[mod_inv.mod.id];
 
419
 
 
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
 
422
     * handlers.
 
423
     */
 
424
    method = &rdata->msg_info.msg->line.req.method;
 
425
 
 
426
    if (method->id == PJSIP_INVITE_METHOD) {
 
427
        return PJ_TRUE;
 
428
    }
 
429
 
 
430
    /* BYE and CANCEL must have existing invite session */
 
431
    if (method->id == PJSIP_BYE_METHOD ||
 
432
        method->id == PJSIP_CANCEL_METHOD)
 
433
    {
 
434
        if (inv == NULL)
 
435
            return PJ_FALSE;
 
436
 
 
437
        return PJ_TRUE;
 
438
    }
 
439
 
 
440
    /* On receipt ACK request, when state is CONNECTING,
 
441
     * move state to CONFIRMED.
 
442
     */
 
443
    if (method->id == PJSIP_ACK_METHOD && inv) {
 
444
 
 
445
        /* Ignore ACK if pending INVITE transaction has not finished. */
 
446
        if (inv->invite_tsx && 
 
447
            inv->invite_tsx->state < PJSIP_TSX_STATE_COMPLETED)
 
448
        {
 
449
            return PJ_TRUE;
 
450
        }
 
451
 
 
452
        /* Terminate INVITE transaction, if it's still present. */
 
453
        if (inv->invite_tsx && 
 
454
            inv->invite_tsx->state <= PJSIP_TSX_STATE_COMPLETED)
 
455
        {
 
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).
 
460
             */
 
461
            if (inv->state < PJSIP_INV_STATE_DISCONNECTED) {
 
462
                inv_check_sdp_in_incoming_msg(inv, inv->invite_tsx, rdata);
 
463
 
 
464
                /* Check if local offer got no SDP answer and INVITE session
 
465
                 * is in CONFIRMED state.
 
466
                 */
 
467
                if (pjmedia_sdp_neg_get_state(inv->neg)==
 
468
                    PJMEDIA_SDP_NEG_STATE_LOCAL_OFFER &&
 
469
                    inv->state==PJSIP_INV_STATE_CONFIRMED)
 
470
                {
 
471
                    pjmedia_sdp_neg_cancel_offer(inv->neg);
 
472
                }
 
473
            }
 
474
 
 
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;
 
483
            }
 
484
        }
 
485
 
 
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!)
 
489
         */
 
490
        if (inv->state == PJSIP_INV_STATE_CONNECTING) {
 
491
            pjsip_event event;
 
492
 
 
493
            PJSIP_EVENT_INIT_RX_MSG(event, rdata);
 
494
            inv_set_state(inv, PJSIP_INV_STATE_CONFIRMED, &event);
 
495
        }
 
496
    }
 
497
 
 
498
    return PJ_FALSE;
 
499
}
 
500
 
 
501
/* This function will process Session Timer headers in received 
 
502
 * 2xx or 422 response of INVITE/UPDATE request.
 
503
 */
 
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)
 
507
{
 
508
    pjsip_status_code st_code;
 
509
    pj_status_t status;
 
510
 
 
511
    status = pjsip_timer_process_resp(inv, rdata, &st_code);
 
512
    if (status != PJ_SUCCESS && end_sess_on_failure) {
 
513
        pjsip_tx_data *tdata;
 
514
        pj_status_t status2;
 
515
 
 
516
        status2 = pjsip_inv_end_session(inv, st_code, NULL, &tdata);
 
517
        if (tdata && status2 == PJ_SUCCESS)
 
518
            pjsip_inv_send_msg(inv, tdata);
 
519
    }
 
520
 
 
521
    return status;
 
522
}
 
523
 
 
524
/*
 
525
 * Module on_rx_response().
 
526
 *
 
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.
 
531
 */
 
532
static pj_bool_t mod_inv_on_rx_response(pjsip_rx_data *rdata)
 
533
{
 
534
    pjsip_dialog *dlg;
 
535
    pjsip_inv_session *inv;
 
536
    pjsip_msg *msg = rdata->msg_info.msg;
 
537
 
 
538
    dlg = pjsip_rdata_get_dlg(rdata);
 
539
 
 
540
    /* Ignore responses outside dialog */
 
541
    if (dlg == NULL)
 
542
        return PJ_FALSE;
 
543
 
 
544
    /* Ignore responses not belonging to invite session */
 
545
    inv = pjsip_dlg_get_inv_session(dlg);
 
546
    if (inv == NULL)
 
547
        return PJ_FALSE;
 
548
 
 
549
    /* This MAY be retransmission of 2xx response to INVITE. 
 
550
     * If it is, we need to send ACK.
 
551
     */
 
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) 
 
555
    {
 
556
        pjsip_event e;
 
557
 
 
558
        PJSIP_EVENT_INIT_RX_MSG(e, rdata);
 
559
        inv_send_ack(inv, &e);
 
560
        return PJ_TRUE;
 
561
 
 
562
    }
 
563
 
 
564
    /* No other processing needs to be done here. */
 
565
    return PJ_FALSE;
 
566
}
 
567
 
 
568
/*
 
569
 * Module on_tsx_state()
 
570
 *
 
571
 * This callback is called by dialog framework for all transactions
 
572
 * inside the dialog for all its dialog usages.
 
573
 */
 
574
static void mod_inv_on_tsx_state(pjsip_transaction *tsx, pjsip_event *e)
 
575
{
 
576
    pjsip_dialog *dlg;
 
577
    pjsip_inv_session *inv;
 
578
 
 
579
    dlg = pjsip_tsx_get_dlg(tsx);
 
580
    if (dlg == NULL)
 
581
        return;
 
582
 
 
583
    inv = pjsip_dlg_get_inv_session(dlg);
 
584
    if (inv == NULL)
 
585
        return;
 
586
 
 
587
    /* Call state handler for the invite session. */
 
588
    (*inv_state_handler[inv->state])(inv, e);
 
589
 
 
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);
 
593
 
 
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)
 
598
     */
 
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;
 
604
        }
 
605
    }
 
606
}
 
607
 
 
608
 
 
609
/*
 
610
 * Initialize the invite module.
 
611
 */
 
612
PJ_DEF(pj_status_t) pjsip_inv_usage_init( pjsip_endpoint *endpt,
 
613
                                          const pjsip_inv_callback *cb)
 
614
{
 
615
    pj_status_t status;
 
616
 
 
617
    /* Check arguments. */
 
618
    PJ_ASSERT_RETURN(endpt && cb, PJ_EINVAL);
 
619
 
 
620
    /* Some callbacks are mandatory */
 
621
    PJ_ASSERT_RETURN(cb->on_state_changed && cb->on_new_session, PJ_EINVAL);
 
622
 
 
623
    /* Check if module already registered. */
 
624
    PJ_ASSERT_RETURN(mod_inv.mod.id == -1, PJ_EINVALIDOP);
 
625
 
 
626
    /* Copy param. */
 
627
    pj_memcpy(&mod_inv.cb, cb, sizeof(pjsip_inv_callback));
 
628
 
 
629
    mod_inv.endpt = endpt;
 
630
 
 
631
    /* Register the module. */
 
632
    status = pjsip_endpt_register_module(endpt, &mod_inv.mod);
 
633
    if (status != PJ_SUCCESS)
 
634
        return status;
 
635
 
 
636
    return PJ_SUCCESS;
 
637
}
 
638
 
 
639
/*
 
640
 * Get the instance of invite module.
 
641
 */
 
642
PJ_DEF(pjsip_module*) pjsip_inv_usage_instance(void)
 
643
{
 
644
    return &mod_inv.mod;
 
645
}
 
646
 
 
647
 
 
648
 
 
649
/*
 
650
 * Return the invite session for the specified dialog.
 
651
 */
 
652
PJ_DEF(pjsip_inv_session*) pjsip_dlg_get_inv_session(pjsip_dialog *dlg)
 
653
{
 
654
    return (pjsip_inv_session*) dlg->mod_data[mod_inv.mod.id];
 
655
}
 
656
 
 
657
 
 
658
/*
 
659
 * Get INVITE state name.
 
660
 */
 
661
PJ_DEF(const char *) pjsip_inv_state_name(pjsip_inv_state state)
 
662
{
 
663
    PJ_ASSERT_RETURN(state >= PJSIP_INV_STATE_NULL && 
 
664
                     state <= PJSIP_INV_STATE_DISCONNECTED,
 
665
                     "??");
 
666
 
 
667
    return inv_state_names[state];
 
668
}
 
669
 
 
670
/*
 
671
 * Create UAC invite session.
 
672
 */
 
673
PJ_DEF(pj_status_t) pjsip_inv_create_uac( pjsip_dialog *dlg,
 
674
                                          const pjmedia_sdp_session *local_sdp,
 
675
                                          unsigned options,
 
676
                                          pjsip_inv_session **p_inv)
 
677
{
 
678
    pjsip_inv_session *inv;
 
679
    pj_status_t status;
 
680
 
 
681
    /* Verify arguments. */
 
682
    PJ_ASSERT_RETURN(dlg && p_inv, PJ_EINVAL);
 
683
 
 
684
    /* Must lock dialog first */
 
685
    pjsip_dlg_inc_lock(dlg);
 
686
 
 
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;
 
692
 
 
693
    /* Create the session */
 
694
    inv = PJ_POOL_ZALLOC_T(dlg->pool, pjsip_inv_session);
 
695
    pj_assert(inv != NULL);
 
696
 
 
697
    inv->pool = dlg->pool;
 
698
    inv->role = PJSIP_ROLE_UAC;
 
699
    inv->state = PJSIP_INV_STATE_NULL;
 
700
    inv->dlg = dlg;
 
701
    inv->options = options;
 
702
    inv->notify = PJ_TRUE;
 
703
    inv->cause = (pjsip_status_code) 0;
 
704
 
 
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);
 
712
 
 
713
    /* Object name will use the same dialog pointer. */
 
714
    pj_ansi_snprintf(inv->obj_name, PJ_MAX_OBJ_NAME, "inv%p", dlg);
 
715
 
 
716
    /* Create negotiator if local_sdp is specified. */
 
717
    if (local_sdp) {
 
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);
 
722
            return status;
 
723
        }
 
724
    }
 
725
 
 
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);
 
730
        return status;
 
731
    }
 
732
 
 
733
    /* Increment dialog session */
 
734
    pjsip_dlg_inc_session(dlg, &mod_inv.mod);
 
735
 
 
736
    /* Create 100rel handler */
 
737
    pjsip_100rel_attach(inv);
 
738
 
 
739
    /* Done */
 
740
    *p_inv = inv;
 
741
 
 
742
    pjsip_dlg_dec_lock(dlg);
 
743
 
 
744
    PJ_LOG(5,(inv->obj_name, "UAC invite session created for dialog %s",
 
745
              dlg->obj_name));
 
746
 
 
747
    return PJ_SUCCESS;
 
748
}
 
749
 
 
750
PJ_DEF(pjsip_rdata_sdp_info*) pjsip_rdata_get_sdp_info(pjsip_rx_data *rdata)
 
751
{
 
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;
 
756
 
 
757
    sdp_info = (pjsip_rdata_sdp_info*)
 
758
               rdata->endpt_info.mod_data[mod_inv.mod.id];
 
759
    if (sdp_info)
 
760
        return sdp_info;
 
761
 
 
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;
 
766
 
 
767
    pjsip_media_type_init2(&app_sdp, "application", "sdp");
 
768
 
 
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)
 
772
    {
 
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))
 
779
    {
 
780
        pjsip_multipart_part *part;
 
781
 
 
782
        part = pjsip_multipart_find_part(body, &app_sdp, NULL);
 
783
        if (part) {
 
784
            sdp_info->body.ptr = (char*)part->body->data;
 
785
            sdp_info->body.slen = part->body->len;
 
786
        }
 
787
    }
 
788
 
 
789
    if (sdp_info->body.ptr) {
 
790
        pj_status_t status;
 
791
        status = pjmedia_sdp_parse(rdata->tp_info.pool,
 
792
                                   sdp_info->body.ptr,
 
793
                                   sdp_info->body.slen,
 
794
                                   &sdp_info->sdp);
 
795
        if (status == PJ_SUCCESS)
 
796
            status = pjmedia_sdp_validate(sdp_info->sdp);
 
797
 
 
798
        if (status != PJ_SUCCESS) {
 
799
            sdp_info->sdp = NULL;
 
800
            PJ_PERROR(1,(THIS_FILE, status,
 
801
                         "Error parsing/validating SDP body"));
 
802
        }
 
803
 
 
804
        sdp_info->sdp_err = status;
 
805
    }
 
806
 
 
807
    return sdp_info;
 
808
}
 
809
 
 
810
 
 
811
/*
 
812
 * Verify incoming INVITE request.
 
813
 */
 
814
PJ_DEF(pj_status_t) pjsip_inv_verify_request2(pjsip_rx_data *rdata,
 
815
                                              unsigned *options,
 
816
                                              const pjmedia_sdp_session *r_sdp,
 
817
                                              const pjmedia_sdp_session *l_sdp,
 
818
                                              pjsip_dialog *dlg,
 
819
                                              pjsip_endpoint *endpt,
 
820
                                              pjsip_tx_data **p_tdata)
 
821
{
 
822
    pjsip_msg *msg;
 
823
    pjsip_allow_hdr *allow;
 
824
    pjsip_supported_hdr *sup_hdr;
 
825
    pjsip_require_hdr *req_hdr;
 
826
    pjsip_contact_hdr *c_hdr;
 
827
    int code = 200;
 
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;
 
832
 
 
833
    /* Init return arguments. */
 
834
    if (p_tdata) *p_tdata = NULL;
 
835
 
 
836
    /* Verify arguments. */
 
837
    PJ_ASSERT_RETURN(rdata != NULL && options != NULL, PJ_EINVAL);
 
838
 
 
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;
 
846
 
 
847
    /* Get the message in rdata */
 
848
    msg = rdata->msg_info.msg;
 
849
 
 
850
    /* Must be INVITE request. */
 
851
    PJ_ASSERT_RETURN(msg->type == PJSIP_REQUEST_MSG &&
 
852
                     msg->line.req.method.id == PJSIP_INVITE_METHOD,
 
853
                     PJ_EINVAL);
 
854
 
 
855
    /* If tdata is specified, then either dlg or endpt must be specified */
 
856
    PJ_ASSERT_RETURN((!p_tdata) || (endpt || dlg), PJ_EINVAL);
 
857
 
 
858
    /* Get the endpoint */
 
859
    endpt = endpt ? endpt : dlg->endpt;
 
860
 
 
861
    /* Init response header list */
 
862
    pj_list_init(&res_hdr_list);
 
863
 
 
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;
 
870
        pj_str_t warn_text;
 
871
 
 
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),
 
875
                                     &warn_text);
 
876
        if (w) {
 
877
            pj_list_push_back(&res_hdr_list, w);
 
878
        }
 
879
 
 
880
        code = PJSIP_SC_BAD_REQUEST;
 
881
        status = PJSIP_ERRNO_FROM_SIP_STATUS(code);
 
882
        goto on_return;
 
883
    }
 
884
 
 
885
    /* Check the request body, see if it's something that we support,
 
886
     * only when the body hasn't been parsed before.
 
887
     */
 
888
    if (r_sdp == NULL) {
 
889
        sdp_info = pjsip_rdata_get_sdp_info(rdata);
 
890
    } else {
 
891
        sdp_info = NULL;
 
892
    }
 
893
 
 
894
    if (r_sdp==NULL && msg->body) {
 
895
 
 
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);
 
901
 
 
902
            if (p_tdata) {
 
903
                /* Add Accept header to response */
 
904
                pjsip_accept_hdr *acc;
 
905
 
 
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);
 
910
            }
 
911
 
 
912
            goto on_return;
 
913
        }
 
914
 
 
915
        if (sdp_info->sdp_err != PJ_SUCCESS) {
 
916
            /* Unparseable or invalid SDP */
 
917
            code = PJSIP_SC_BAD_REQUEST;
 
918
 
 
919
            if (p_tdata) {
 
920
                /* Add Warning header. */
 
921
                pjsip_warning_hdr *w;
 
922
 
 
923
                w = pjsip_warning_hdr_create_from_status(rdata->tp_info.pool,
 
924
                                                         pjsip_endpt_name(endpt),
 
925
                                                         sdp_info->sdp_err);
 
926
                PJ_ASSERT_RETURN(w, PJ_ENOMEM);
 
927
 
 
928
                pj_list_push_back(&res_hdr_list, w);
 
929
            }
 
930
 
 
931
            goto on_return;
 
932
        }
 
933
 
 
934
        r_sdp = sdp_info->sdp;
 
935
    }
 
936
 
 
937
    if (r_sdp) {
 
938
        /* Negotiate with local SDP */
 
939
        if (l_sdp) {
 
940
            pjmedia_sdp_neg *neg;
 
941
 
 
942
            /* Local SDP must be valid! */
 
943
            PJ_ASSERT_RETURN((status=pjmedia_sdp_validate(l_sdp))==PJ_SUCCESS,
 
944
                             status);
 
945
 
 
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);
 
950
 
 
951
            /* Negotiate SDP */
 
952
            status = pjmedia_sdp_neg_negotiate(rdata->tp_info.pool, neg, 0);
 
953
            if (status != PJ_SUCCESS) {
 
954
 
 
955
                /* Incompatible media */
 
956
                code = PJSIP_SC_NOT_ACCEPTABLE_HERE;
 
957
 
 
958
                if (p_tdata) {
 
959
                    pjsip_accept_hdr *acc;
 
960
                    pjsip_warning_hdr *w;
 
961
 
 
962
                    /* Add Warning header. */
 
963
                    w = pjsip_warning_hdr_create_from_status(
 
964
                                            rdata->tp_info.pool, 
 
965
                                            pjsip_endpt_name(endpt), status);
 
966
                    PJ_ASSERT_RETURN(w, PJ_ENOMEM);
 
967
 
 
968
                    pj_list_push_back(&res_hdr_list, w);
 
969
 
 
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);
 
975
 
 
976
                }
 
977
 
 
978
                goto on_return;
 
979
            }
 
980
        }
 
981
    }
 
982
 
 
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.
 
986
     */
 
987
    allow = (pjsip_allow_hdr*) pjsip_msg_find_hdr(msg, PJSIP_H_ALLOW, NULL);
 
988
    if (allow) {
 
989
        unsigned i;
 
990
        const pj_str_t STR_UPDATE = { "UPDATE", 6 };
 
991
 
 
992
        for (i=0; i<allow->count; ++i) {
 
993
            if (pj_stricmp(&allow->values[i], &STR_UPDATE)==0)
 
994
                break;
 
995
        }
 
996
 
 
997
        if (i != allow->count) {
 
998
            /* UPDATE is present in Allow */
 
999
            rem_option |= PJSIP_INV_SUPPORT_UPDATE;
 
1000
        }
 
1001
 
 
1002
    }
 
1003
 
 
1004
    /* Check Supported header */
 
1005
    sup_hdr = (pjsip_supported_hdr*)
 
1006
              pjsip_msg_find_hdr(msg, PJSIP_H_SUPPORTED, NULL);
 
1007
    if (sup_hdr) {
 
1008
        unsigned i;
 
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 };
 
1012
 
 
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;
 
1020
        }
 
1021
    }
 
1022
 
 
1023
    /* Check Require header */
 
1024
    req_hdr = (pjsip_require_hdr*)
 
1025
              pjsip_msg_find_hdr(msg, PJSIP_H_REQUIRE, NULL);
 
1026
    if (req_hdr) {
 
1027
        unsigned i;
 
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];
 
1034
        
 
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)
 
1038
            {
 
1039
                rem_option |= PJSIP_INV_REQUIRE_100REL;
 
1040
 
 
1041
            } else if ((*options & PJSIP_INV_SUPPORT_TIMER) && 
 
1042
                pj_stricmp(&req_hdr->values[i], &STR_TIMER)==0)
 
1043
            {
 
1044
                rem_option |= PJSIP_INV_REQUIRE_TIMER;
 
1045
 
 
1046
            } else if (pj_stricmp(&req_hdr->values[i], &STR_REPLACES)==0) {
 
1047
                pj_bool_t supp;
 
1048
                
 
1049
                supp = pjsip_endpt_has_capability(endpt, PJSIP_H_SUPPORTED, 
 
1050
                                                  NULL, &STR_REPLACES);
 
1051
                if (!supp)
 
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)
 
1055
            {
 
1056
                rem_option |= PJSIP_INV_REQUIRE_ICE;
 
1057
 
 
1058
            } else if (!pjsip_endpt_has_capability(endpt, PJSIP_H_SUPPORTED,
 
1059
                                                   NULL, &req_hdr->values[i]))
 
1060
            {
 
1061
                /* Unknown/unsupported extension tag!  */
 
1062
                unsupp_tags[unsupp_cnt++] = req_hdr->values[i];
 
1063
            }
 
1064
        }
 
1065
 
 
1066
        /* Check if there are required tags that we don't support */
 
1067
        if (unsupp_cnt) {
 
1068
 
 
1069
            code = PJSIP_SC_BAD_EXTENSION;
 
1070
            status = PJSIP_ERRNO_FROM_SIP_STATUS(code);
 
1071
 
 
1072
            if (p_tdata) {
 
1073
                pjsip_unsupported_hdr *unsupp_hdr;
 
1074
                const pjsip_hdr *h;
 
1075
 
 
1076
                /* Add Unsupported header. */
 
1077
                unsupp_hdr = pjsip_unsupported_hdr_create(rdata->tp_info.pool);
 
1078
                PJ_ASSERT_RETURN(unsupp_hdr != NULL, PJ_ENOMEM);
 
1079
 
 
1080
                unsupp_hdr->count = unsupp_cnt;
 
1081
                for (i=0; i<unsupp_cnt; ++i)
 
1082
                    unsupp_hdr->values[i] = unsupp_tags[i];
 
1083
 
 
1084
                pj_list_push_back(&res_hdr_list, unsupp_hdr);
 
1085
 
 
1086
                /* Add Supported header. */
 
1087
                h = pjsip_endpt_get_capability(endpt, PJSIP_H_SUPPORTED, 
 
1088
                                               NULL);
 
1089
                pj_assert(h);
 
1090
                if (h) {
 
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);
 
1094
                }
 
1095
            }
 
1096
 
 
1097
            goto on_return;
 
1098
        }
 
1099
    }
 
1100
 
 
1101
    /* Check if there are local requirements that are not supported
 
1102
     * by peer.
 
1103
     */
 
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))
 
1108
    {
 
1109
        code = PJSIP_SC_EXTENSION_REQUIRED;
 
1110
        status = PJSIP_ERRNO_FROM_SIP_STATUS(code);
 
1111
 
 
1112
        if (p_tdata) {
 
1113
            const pjsip_hdr *h;
 
1114
 
 
1115
            /* Add Require header. */
 
1116
            req_hdr = pjsip_require_hdr_create(rdata->tp_info.pool);
 
1117
            PJ_ASSERT_RETURN(req_hdr != NULL, PJ_ENOMEM);
 
1118
 
 
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");
 
1123
 
 
1124
            pj_list_push_back(&res_hdr_list, req_hdr);
 
1125
 
 
1126
            /* Add Supported header. */
 
1127
            h = pjsip_endpt_get_capability(endpt, PJSIP_H_SUPPORTED, 
 
1128
                                           NULL);
 
1129
            pj_assert(h);
 
1130
            if (h) {
 
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);
 
1134
            }
 
1135
 
 
1136
        }
 
1137
 
 
1138
        goto on_return;
 
1139
    }
 
1140
 
 
1141
    /* If remote Require something that we support, make us Require
 
1142
     * that feature too.
 
1143
     */
 
1144
    if (rem_option & PJSIP_INV_REQUIRE_100REL) {
 
1145
            pj_assert(*options & PJSIP_INV_SUPPORT_100REL);
 
1146
            *options |= PJSIP_INV_REQUIRE_100REL;
 
1147
    }
 
1148
    if (rem_option & PJSIP_INV_REQUIRE_TIMER) {
 
1149
            pj_assert(*options & PJSIP_INV_SUPPORT_TIMER);
 
1150
            *options |= PJSIP_INV_REQUIRE_TIMER;
 
1151
    }
 
1152
 
 
1153
on_return:
 
1154
 
 
1155
    /* Create response if necessary */
 
1156
    if (code != 200 && p_tdata) {
 
1157
        pjsip_tx_data *tdata;
 
1158
        const pjsip_hdr *h;
 
1159
 
 
1160
        if (dlg) {
 
1161
            status = pjsip_dlg_create_response(dlg, rdata, code, NULL, 
 
1162
                                               &tdata);
 
1163
        } else {
 
1164
            status = pjsip_endpt_create_response(endpt, rdata, code, NULL, 
 
1165
                                                 &tdata);
 
1166
        }
 
1167
 
 
1168
        if (status != PJ_SUCCESS)
 
1169
            return status;
 
1170
 
 
1171
        /* Add response headers. */
 
1172
        h = res_hdr_list.next;
 
1173
        while (h != &res_hdr_list) {
 
1174
            pjsip_hdr *cloned;
 
1175
 
 
1176
            cloned = (pjsip_hdr*) pjsip_hdr_clone(tdata->pool, h);
 
1177
            PJ_ASSERT_RETURN(cloned, PJ_ENOMEM);
 
1178
 
 
1179
            pjsip_msg_add_hdr(tdata->msg, cloned);
 
1180
 
 
1181
            h = h->next;
 
1182
        }
 
1183
 
 
1184
        *p_tdata = tdata;
 
1185
 
 
1186
        /* Can not return PJ_SUCCESS when response message is produced.
 
1187
         * Ref: PROTOS test ~#2490
 
1188
         */
 
1189
        if (status == PJ_SUCCESS)
 
1190
            status = PJSIP_ERRNO_FROM_SIP_STATUS(code);
 
1191
 
 
1192
    }
 
1193
 
 
1194
    return status;
 
1195
}
 
1196
 
 
1197
 
 
1198
/*
 
1199
 * Verify incoming INVITE request.
 
1200
 */
 
1201
PJ_DEF(pj_status_t) pjsip_inv_verify_request( pjsip_rx_data *rdata,
 
1202
                                              unsigned *options,
 
1203
                                              const pjmedia_sdp_session *l_sdp,
 
1204
                                              pjsip_dialog *dlg,
 
1205
                                              pjsip_endpoint *endpt,
 
1206
                                              pjsip_tx_data **p_tdata)
 
1207
{
 
1208
    return pjsip_inv_verify_request2(rdata, options, NULL, l_sdp, dlg, 
 
1209
                                     endpt, p_tdata);
 
1210
}
 
1211
 
 
1212
/*
 
1213
 * Create UAS invite session.
 
1214
 */
 
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,
 
1218
                                          unsigned options,
 
1219
                                          pjsip_inv_session **p_inv)
 
1220
{
 
1221
    pjsip_inv_session *inv;
 
1222
    struct tsx_inv_data *tsx_inv_data;
 
1223
    pjsip_msg *msg;
 
1224
    pjsip_rdata_sdp_info *sdp_info;
 
1225
    pj_status_t status;
 
1226
 
 
1227
    /* Verify arguments. */
 
1228
    PJ_ASSERT_RETURN(dlg && rdata && p_inv, PJ_EINVAL);
 
1229
 
 
1230
    /* Dialog MUST have been initialised. */
 
1231
    PJ_ASSERT_RETURN(pjsip_rdata_get_tsx(rdata) != NULL, PJ_EINVALIDOP);
 
1232
 
 
1233
    msg = rdata->msg_info.msg;
 
1234
 
 
1235
    /* rdata MUST contain INVITE request */
 
1236
    PJ_ASSERT_RETURN(msg->type == PJSIP_REQUEST_MSG &&
 
1237
                     msg->line.req.method.id == PJSIP_INVITE_METHOD,
 
1238
                     PJ_EINVALIDOP);
 
1239
 
 
1240
    /* Lock dialog */
 
1241
    pjsip_dlg_inc_lock(dlg);
 
1242
 
 
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;
 
1248
 
 
1249
    /* Create the session */
 
1250
    inv = PJ_POOL_ZALLOC_T(dlg->pool, pjsip_inv_session);
 
1251
    pj_assert(inv != NULL);
 
1252
 
 
1253
    inv->pool = dlg->pool;
 
1254
    inv->role = PJSIP_ROLE_UAS;
 
1255
    inv->state = PJSIP_INV_STATE_NULL;
 
1256
    inv->dlg = dlg;
 
1257
    inv->options = options;
 
1258
    inv->notify = PJ_TRUE;
 
1259
    inv->cause = (pjsip_status_code) 0;
 
1260
 
 
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);
 
1268
 
 
1269
    /* Object name will use the same dialog pointer. */
 
1270
    pj_ansi_snprintf(inv->obj_name, PJ_MAX_OBJ_NAME, "inv%p", dlg);
 
1271
 
 
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;
 
1277
    }
 
1278
 
 
1279
    /* Create negotiator. */
 
1280
    if (sdp_info->sdp) {
 
1281
        status = pjmedia_sdp_neg_create_w_remote_offer(inv->pool, local_sdp,
 
1282
                                                       sdp_info->sdp,
 
1283
                                                       &inv->neg);
 
1284
                                                
 
1285
    } else if (local_sdp) {
 
1286
        status = pjmedia_sdp_neg_create_w_local_offer(inv->pool, 
 
1287
                                                      local_sdp, &inv->neg);
 
1288
    } else {
 
1289
        status = PJ_SUCCESS;
 
1290
    }
 
1291
 
 
1292
    if (status != PJ_SUCCESS) {
 
1293
        pjsip_dlg_dec_lock(dlg);
 
1294
        return status;
 
1295
    }
 
1296
 
 
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);
 
1301
        return status;
 
1302
    }
 
1303
 
 
1304
    /* Increment session in the dialog. */
 
1305
    pjsip_dlg_inc_session(dlg, &mod_inv.mod);
 
1306
 
 
1307
    /* Save the invite transaction. */
 
1308
    inv->invite_tsx = pjsip_rdata_get_tsx(rdata);
 
1309
 
 
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;
 
1314
 
 
1315
    /* Create 100rel handler */
 
1316
    if (inv->options & PJSIP_INV_REQUIRE_100REL) {
 
1317
        pjsip_100rel_attach(inv);
 
1318
    }
 
1319
 
 
1320
    /* Done */
 
1321
    pjsip_dlg_dec_lock(dlg);
 
1322
    *p_inv = inv;
 
1323
 
 
1324
    PJ_LOG(5,(inv->obj_name, "UAS invite session created for dialog %s",
 
1325
              dlg->obj_name));
 
1326
 
 
1327
    return PJ_SUCCESS;
 
1328
}
 
1329
 
 
1330
/*
 
1331
 * Forcefully terminate the session.
 
1332
 */
 
1333
PJ_DEF(pj_status_t) pjsip_inv_terminate( pjsip_inv_session *inv,
 
1334
                                         int st_code,
 
1335
                                         pj_bool_t notify)
 
1336
{
 
1337
    PJ_ASSERT_RETURN(inv, PJ_EINVAL);
 
1338
 
 
1339
    /* Lock dialog. */
 
1340
    pjsip_dlg_inc_lock(inv->dlg);
 
1341
 
 
1342
    /* Set callback notify flag. */
 
1343
    inv->notify = notify;
 
1344
 
 
1345
    /* If there's pending transaction, terminate the transaction. 
 
1346
     * This may subsequently set the INVITE session state to
 
1347
     * disconnected.
 
1348
     */
 
1349
    if (inv->invite_tsx && 
 
1350
        inv->invite_tsx->state <= PJSIP_TSX_STATE_COMPLETED)
 
1351
    {
 
1352
        pjsip_tsx_terminate(inv->invite_tsx, st_code);
 
1353
 
 
1354
    }
 
1355
 
 
1356
    /* Set cause. */
 
1357
    inv_set_cause(inv, st_code, NULL);
 
1358
 
 
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);
 
1362
    }
 
1363
 
 
1364
    /* Done.
 
1365
     * The dec_lock() below will actually destroys the dialog if it
 
1366
     * has no other session.
 
1367
     */
 
1368
    pjsip_dlg_dec_lock(inv->dlg);
 
1369
 
 
1370
    return PJ_SUCCESS;
 
1371
}
 
1372
 
 
1373
 
 
1374
/*
 
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.
 
1377
 */
 
1378
PJ_DEF(pj_status_t) pjsip_inv_uac_restart(pjsip_inv_session *inv,
 
1379
                                          pj_bool_t new_offer)
 
1380
{
 
1381
    PJ_ASSERT_RETURN(inv, PJ_EINVAL);
 
1382
 
 
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;
 
1388
    }
 
1389
 
 
1390
    if (new_offer && inv->neg) {
 
1391
        pjmedia_sdp_neg_state neg_state;
 
1392
 
 
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);
 
1396
        }
 
1397
    }
 
1398
 
 
1399
    return PJ_SUCCESS;
 
1400
}
 
1401
 
 
1402
 
 
1403
static void *clone_sdp(pj_pool_t *pool, const void *data, unsigned len)
 
1404
{
 
1405
    PJ_UNUSED_ARG(len);
 
1406
    return pjmedia_sdp_session_clone(pool, (const pjmedia_sdp_session*)data);
 
1407
}
 
1408
 
 
1409
static int print_sdp(pjsip_msg_body *body, char *buf, pj_size_t len)
 
1410
{
 
1411
    return pjmedia_sdp_print((const pjmedia_sdp_session*)body->data, buf, len);
 
1412
}
 
1413
 
 
1414
 
 
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)
 
1418
{
 
1419
    const pj_str_t STR_APPLICATION = { "application", 11};
 
1420
    const pj_str_t STR_SDP = { "sdp", 3 };
 
1421
    pjsip_msg_body *body;
 
1422
 
 
1423
    body = PJ_POOL_ZALLOC_T(pool, pjsip_msg_body);
 
1424
    PJ_ASSERT_RETURN(body != NULL, PJ_ENOMEM);
 
1425
 
 
1426
    pjsip_media_type_init(&body->content_type, (pj_str_t*)&STR_APPLICATION,
 
1427
                          (pj_str_t*)&STR_SDP);
 
1428
    body->data = sdp;
 
1429
    body->len = 0;
 
1430
    body->clone_data = &clone_sdp;
 
1431
    body->print_body = &print_sdp;
 
1432
 
 
1433
    *p_body = body;
 
1434
 
 
1435
    return PJ_SUCCESS;
 
1436
}
 
1437
 
 
1438
static pjsip_msg_body *create_sdp_body(pj_pool_t *pool,
 
1439
                                       const pjmedia_sdp_session *c_sdp)
 
1440
{
 
1441
    pjsip_msg_body *body;
 
1442
    pj_status_t status;
 
1443
 
 
1444
    status = pjsip_create_sdp_body(pool, 
 
1445
                                   pjmedia_sdp_session_clone(pool, c_sdp),
 
1446
                                   &body);
 
1447
 
 
1448
    if (status != PJ_SUCCESS)
 
1449
        return NULL;
 
1450
 
 
1451
    return body;
 
1452
}
 
1453
 
 
1454
/*
 
1455
 * Create initial INVITE request.
 
1456
 */
 
1457
PJ_DEF(pj_status_t) pjsip_inv_invite( pjsip_inv_session *inv,
 
1458
                                      pjsip_tx_data **p_tdata )
 
1459
{
 
1460
    pjsip_tx_data *tdata;
 
1461
    const pjsip_hdr *hdr;
 
1462
    pj_bool_t has_sdp;
 
1463
    pj_status_t status;
 
1464
 
 
1465
    /* Verify arguments. */
 
1466
    PJ_ASSERT_RETURN(inv && p_tdata, PJ_EINVAL);
 
1467
 
 
1468
    /* State MUST be NULL or CONFIRMED. */
 
1469
    PJ_ASSERT_RETURN(inv->state == PJSIP_INV_STATE_NULL ||
 
1470
                     inv->state == PJSIP_INV_STATE_CONFIRMED, 
 
1471
                     PJ_EINVALIDOP);
 
1472
 
 
1473
    /* Lock dialog. */
 
1474
    pjsip_dlg_inc_lock(inv->dlg);
 
1475
 
 
1476
    /* Create the INVITE request. */
 
1477
    status = pjsip_dlg_create_request(inv->dlg, pjsip_get_invite_method(), -1,
 
1478
                                      &tdata);
 
1479
    if (status != PJ_SUCCESS)
 
1480
        goto on_return;
 
1481
 
 
1482
 
 
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.
 
1486
     */
 
1487
    if (inv->state == PJSIP_INV_STATE_NULL) {
 
1488
        hdr = inv->dlg->inv_hdr.next;
 
1489
 
 
1490
        while (hdr != &inv->dlg->inv_hdr) {
 
1491
            pjsip_msg_add_hdr(tdata->msg, (pjsip_hdr*)
 
1492
                              pjsip_hdr_shallow_clone(tdata->pool, hdr));
 
1493
            hdr = hdr->next;
 
1494
        }
 
1495
    }
 
1496
 
 
1497
    /* See if we have SDP to send. */
 
1498
    if (inv->neg) {
 
1499
        pjmedia_sdp_neg_state neg_state;
 
1500
 
 
1501
        neg_state = pjmedia_sdp_neg_get_state(inv->neg);
 
1502
 
 
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)));
 
1506
 
 
1507
 
 
1508
    } else {
 
1509
        has_sdp = PJ_FALSE;
 
1510
    }
 
1511
 
 
1512
    /* Add SDP, if any. */
 
1513
    if (has_sdp) {
 
1514
        const pjmedia_sdp_session *offer;
 
1515
 
 
1516
        status = pjmedia_sdp_neg_get_neg_local(inv->neg, &offer);
 
1517
        if (status != PJ_SUCCESS) {
 
1518
            pjsip_tx_data_dec_ref(tdata);
 
1519
            goto on_return;
 
1520
        }
 
1521
 
 
1522
        tdata->msg->body = create_sdp_body(tdata->pool, offer);
 
1523
    }
 
1524
 
 
1525
    /* Add Allow header. */
 
1526
    if (inv->dlg->add_allow) {
 
1527
        hdr = pjsip_endpt_get_capability(inv->dlg->endpt, PJSIP_H_ALLOW, NULL);
 
1528
        if (hdr) {
 
1529
            pjsip_msg_add_hdr(tdata->msg, (pjsip_hdr*)
 
1530
                              pjsip_hdr_shallow_clone(tdata->pool, hdr));
 
1531
        }
 
1532
    }
 
1533
 
 
1534
    /* Add Supported header */
 
1535
    hdr = pjsip_endpt_get_capability(inv->dlg->endpt, PJSIP_H_SUPPORTED, NULL);
 
1536
    if (hdr) {
 
1537
        pjsip_msg_add_hdr(tdata->msg, (pjsip_hdr*)
 
1538
                          pjsip_hdr_shallow_clone(tdata->pool, hdr));
 
1539
    }
 
1540
 
 
1541
    /* Add Require header. */
 
1542
    if ((inv->options & PJSIP_INV_REQUIRE_100REL) ||
 
1543
        (inv->options & PJSIP_INV_REQUIRE_TIMER)) 
 
1544
    {
 
1545
        pjsip_require_hdr *hreq;
 
1546
 
 
1547
        hreq = pjsip_require_hdr_create(tdata->pool);
 
1548
 
 
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");
 
1553
 
 
1554
        pjsip_msg_add_hdr(tdata->msg, (pjsip_hdr*) hreq);
 
1555
    }
 
1556
 
 
1557
    status = pjsip_timer_update_req(inv, tdata);
 
1558
    if (status != PJ_SUCCESS)
 
1559
        goto on_return;
 
1560
 
 
1561
    /* Done. */
 
1562
    *p_tdata = tdata;
 
1563
 
 
1564
 
 
1565
on_return:
 
1566
    pjsip_dlg_dec_lock(inv->dlg);
 
1567
    return status;
 
1568
}
 
1569
 
 
1570
 
 
1571
/* Util: swap pool */
 
1572
static void swap_pool(pj_pool_t **p1, pj_pool_t **p2)
 
1573
{
 
1574
    pj_pool_t *tmp = *p1;
 
1575
    *p1 = *p2;
 
1576
    *p2 = tmp;
 
1577
}
 
1578
 
 
1579
 
 
1580
/*
 
1581
 * Initiate SDP negotiation in the SDP negotiator.
 
1582
 */
 
1583
static pj_status_t inv_negotiate_sdp( pjsip_inv_session *inv )
 
1584
{
 
1585
    pj_status_t status;
 
1586
 
 
1587
    PJ_ASSERT_RETURN(pjmedia_sdp_neg_get_state(inv->neg) ==
 
1588
                     PJMEDIA_SDP_NEG_STATE_WAIT_NEGO, 
 
1589
                     PJMEDIA_SDPNEG_EINSTATE);
 
1590
 
 
1591
    status = pjmedia_sdp_neg_negotiate(inv->pool_prov, inv->neg, 0);
 
1592
 
 
1593
    PJ_LOG(5,(inv->obj_name, "SDP negotiation done, status=%d", status));
 
1594
 
 
1595
    if (mod_inv.cb.on_media_update && inv->notify)
 
1596
        (*mod_inv.cb.on_media_update)(inv, status);
 
1597
 
 
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).
 
1601
     */
 
1602
    if (inv->state != PJSIP_INV_STATE_DISCONNECTED) {
 
1603
 
 
1604
        /* Swap the flip-flop pool when SDP negotiation success. */
 
1605
        if (status == PJ_SUCCESS) {
 
1606
            swap_pool(&inv->pool_prov, &inv->pool_active);
 
1607
        }
 
1608
 
 
1609
        /* Reset the provisional pool regardless SDP negotiation result. */
 
1610
        pj_pool_reset(inv->pool_prov);
 
1611
 
 
1612
    } else {
 
1613
 
 
1614
        status = PJSIP_ERRNO_FROM_SIP_STATUS(inv->cause);
 
1615
    }
 
1616
 
 
1617
    return status;
 
1618
}
 
1619
 
 
1620
/*
 
1621
 * Check in incoming message for SDP offer/answer.
 
1622
 */
 
1623
static pj_status_t inv_check_sdp_in_incoming_msg( pjsip_inv_session *inv,
 
1624
                                                  pjsip_transaction *tsx,
 
1625
                                                  pjsip_rx_data *rdata)
 
1626
{
 
1627
    struct tsx_inv_data *tsx_inv_data;
 
1628
    pj_status_t status;
 
1629
    pjsip_msg *msg;
 
1630
    pjsip_rdata_sdp_info *sdp_info;
 
1631
 
 
1632
    /* Check if SDP is present in the message. */
 
1633
 
 
1634
    msg = rdata->msg_info.msg;
 
1635
    if (msg->body == NULL) {
 
1636
        /* Message doesn't have body. */
 
1637
        return PJ_SUCCESS;
 
1638
    }
 
1639
 
 
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;
 
1644
    }
 
1645
 
 
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;
 
1652
    }
 
1653
 
 
1654
    /* MUST NOT do multiple SDP offer/answer in a single transaction,
 
1655
     * EXCEPT if:
 
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).
 
1661
     *
 
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
 
1665
     */
 
1666
    if (tsx_inv_data->sdp_done) {
 
1667
        pj_str_t res_tag;
 
1668
 
 
1669
        res_tag = rdata->msg_info.to->tag;
 
1670
 
 
1671
        /* Allow final response after SDP has been negotiated in early
 
1672
         * media, IF this response is a final response with different
 
1673
         * tag.
 
1674
         */
 
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))
 
1679
        {
 
1680
            const pjmedia_sdp_session *reoffer_sdp = NULL;
 
1681
 
 
1682
            PJ_LOG(4,(inv->obj_name, "Received forked final response "
 
1683
                      "after SDP negotiation has been done in early "
 
1684
                      "media. Renegotiating SDP.."));
 
1685
 
 
1686
            /* Retrieve original SDP offer from INVITE request */
 
1687
            reoffer_sdp = (const pjmedia_sdp_session*) 
 
1688
                          tsx->last_tx->msg->body->data;
 
1689
 
 
1690
            /* Feed the original offer to negotiator */
 
1691
            status = pjmedia_sdp_neg_modify_local_offer(inv->pool_prov, 
 
1692
                                                        inv->neg,
 
1693
                                                        reoffer_sdp);
 
1694
            if (status != PJ_SUCCESS) {
 
1695
                PJ_LOG(1,(inv->obj_name, "Error updating local offer for "
 
1696
                          "forked 2xx response (err=%d)", status));
 
1697
                return status;
 
1698
            }
 
1699
 
 
1700
        } else {
 
1701
 
 
1702
            if (rdata->msg_info.msg->body) {
 
1703
                PJ_LOG(4,(inv->obj_name, "SDP negotiation done, message "
 
1704
                          "body is ignored"));
 
1705
            }
 
1706
            return PJ_SUCCESS;
 
1707
        }
 
1708
    }
 
1709
 
 
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;
 
1716
    }
 
1717
 
 
1718
    pj_assert(sdp_info->sdp != NULL);
 
1719
 
 
1720
    /* The SDP can be an offer or answer, depending on negotiator's state */
 
1721
 
 
1722
    if (inv->neg == NULL ||
 
1723
        pjmedia_sdp_neg_get_state(inv->neg) == PJMEDIA_SDP_NEG_STATE_DONE) 
 
1724
    {
 
1725
 
 
1726
        /* This is an offer. */
 
1727
 
 
1728
        PJ_LOG(5,(inv->obj_name, "Got SDP offer in %s", 
 
1729
                  pjsip_rx_data_get_info(rdata)));
 
1730
 
 
1731
        if (inv->neg == NULL) {
 
1732
            status=pjmedia_sdp_neg_create_w_remote_offer(inv->pool, NULL,
 
1733
                                                         sdp_info->sdp,
 
1734
                                                         &inv->neg);
 
1735
        } else {
 
1736
            status=pjmedia_sdp_neg_set_remote_offer(inv->pool_prov, inv->neg, 
 
1737
                                                    sdp_info->sdp);
 
1738
        }
 
1739
 
 
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;
 
1744
        }
 
1745
 
 
1746
        /* Inform application about remote offer. */
 
1747
        if (mod_inv.cb.on_rx_offer && inv->notify) {
 
1748
 
 
1749
            (*mod_inv.cb.on_rx_offer)(inv, sdp_info->sdp);
 
1750
 
 
1751
        }
 
1752
 
 
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)
 
1756
        {
 
1757
            return PJ_EINVALIDOP;
 
1758
        }
 
1759
 
 
1760
    } else if (pjmedia_sdp_neg_get_state(inv->neg) == 
 
1761
                PJMEDIA_SDP_NEG_STATE_LOCAL_OFFER) 
 
1762
    {
 
1763
        int status_code;
 
1764
 
 
1765
        /* This is an answer. 
 
1766
         * Process and negotiate remote answer.
 
1767
         */
 
1768
 
 
1769
        PJ_LOG(5,(inv->obj_name, "Got SDP answer in %s", 
 
1770
                  pjsip_rx_data_get_info(rdata)));
 
1771
 
 
1772
        status = pjmedia_sdp_neg_set_remote_answer(inv->pool_prov, inv->neg,
 
1773
                                                   sdp_info->sdp);
 
1774
 
 
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;
 
1779
        }
 
1780
 
 
1781
        /* Negotiate SDP */
 
1782
 
 
1783
        inv_negotiate_sdp(inv);
 
1784
 
 
1785
        /* Mark this transaction has having SDP offer/answer done, and
 
1786
         * save the reference to the To tag
 
1787
         */
 
1788
 
 
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);
 
1794
 
 
1795
    } else {
 
1796
        
 
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))));
 
1800
    }
 
1801
 
 
1802
    return PJ_SUCCESS;
 
1803
}
 
1804
 
 
1805
 
 
1806
/*
 
1807
 * Process INVITE answer, for both initial and subsequent re-INVITE
 
1808
 */
 
1809
static pj_status_t process_answer( pjsip_inv_session *inv,
 
1810
                                   int st_code,
 
1811
                                   pjsip_tx_data *tdata,
 
1812
                                   const pjmedia_sdp_session *local_sdp)
 
1813
{
 
1814
    pj_status_t status;
 
1815
    const pjmedia_sdp_session *sdp = NULL;
 
1816
 
 
1817
    /* If local_sdp is specified, then we MUST NOT have answered the
 
1818
     * offer before. 
 
1819
     */
 
1820
    if (local_sdp && (st_code/100==1 || st_code/100==2)) {
 
1821
 
 
1822
        if (inv->neg == NULL) {
 
1823
            status = pjmedia_sdp_neg_create_w_local_offer(inv->pool, 
 
1824
                                                          local_sdp,
 
1825
                                                          &inv->neg);
 
1826
        } else if (pjmedia_sdp_neg_get_state(inv->neg)==
 
1827
                   PJMEDIA_SDP_NEG_STATE_REMOTE_OFFER)
 
1828
        {
 
1829
            status = pjmedia_sdp_neg_set_local_answer(inv->pool_prov, inv->neg,
 
1830
                                                      local_sdp);
 
1831
        } else {
 
1832
 
 
1833
            /* Can not specify local SDP at this state. */
 
1834
            pj_assert(0);
 
1835
            status = PJMEDIA_SDPNEG_EINSTATE;
 
1836
        }
 
1837
 
 
1838
        if (status != PJ_SUCCESS)
 
1839
            return status;
 
1840
 
 
1841
    }
 
1842
 
 
1843
 
 
1844
     /* If SDP negotiator is ready, start negotiation. */
 
1845
    if (st_code/100==2 || (st_code/10==18 && st_code!=180)) {
 
1846
 
 
1847
        pjmedia_sdp_neg_state neg_state;
 
1848
 
 
1849
        /* Start nego when appropriate. */
 
1850
        neg_state = inv->neg ? pjmedia_sdp_neg_get_state(inv->neg) :
 
1851
                    PJMEDIA_SDP_NEG_STATE_NULL;
 
1852
 
 
1853
        if (neg_state == PJMEDIA_SDP_NEG_STATE_LOCAL_OFFER) {
 
1854
 
 
1855
            status = pjmedia_sdp_neg_get_neg_local(inv->neg, &sdp);
 
1856
 
 
1857
        } else if (neg_state == PJMEDIA_SDP_NEG_STATE_WAIT_NEGO &&
 
1858
                   pjmedia_sdp_neg_has_local_answer(inv->neg) )
 
1859
        {
 
1860
            struct tsx_inv_data *tsx_inv_data;
 
1861
 
 
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];
 
1865
 
 
1866
            status = inv_negotiate_sdp(inv);
 
1867
            if (status != PJ_SUCCESS)
 
1868
                return status;
 
1869
            
 
1870
            /* Mark this transaction has having SDP offer/answer done. */
 
1871
            tsx_inv_data->sdp_done = 1;
 
1872
 
 
1873
            status = pjmedia_sdp_neg_get_active_local(inv->neg, &sdp);
 
1874
        }
 
1875
    }
 
1876
 
 
1877
    /* Include SDP when it's available for 2xx and 18x (but not 180) response.
 
1878
     * Subsequent response will include this SDP.
 
1879
     *
 
1880
     * Note note:
 
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 
 
1884
     *    session.
 
1885
     */
 
1886
    if (sdp) {
 
1887
        tdata->msg->body = create_sdp_body(tdata->pool, sdp);
 
1888
    } else {
 
1889
        if (inv->options & PJSIP_INV_REQUIRE_100REL) {
 
1890
            tdata->msg->body = NULL;
 
1891
        }
 
1892
    }
 
1893
 
 
1894
 
 
1895
    return PJ_SUCCESS;
 
1896
}
 
1897
 
 
1898
 
 
1899
/*
 
1900
 * Create first response to INVITE
 
1901
 */
 
1902
PJ_DEF(pj_status_t) pjsip_inv_initial_answer(   pjsip_inv_session *inv,
 
1903
                                                pjsip_rx_data *rdata,
 
1904
                                                int st_code,
 
1905
                                                const pj_str_t *st_text,
 
1906
                                                const pjmedia_sdp_session *sdp,
 
1907
                                                pjsip_tx_data **p_tdata)
 
1908
{
 
1909
    pjsip_tx_data *tdata;
 
1910
    pj_status_t status;
 
1911
    pjsip_status_code st_code2;
 
1912
 
 
1913
    /* Verify arguments. */
 
1914
    PJ_ASSERT_RETURN(inv && p_tdata, PJ_EINVAL);
 
1915
 
 
1916
    /* Must have INVITE transaction. */
 
1917
    PJ_ASSERT_RETURN(inv->invite_tsx, PJ_EBUG);
 
1918
 
 
1919
    pjsip_dlg_inc_lock(inv->dlg);
 
1920
 
 
1921
    /* Create response */
 
1922
    status = pjsip_dlg_create_response(inv->dlg, rdata, st_code, st_text,
 
1923
                                       &tdata);
 
1924
    if (status != PJ_SUCCESS)
 
1925
        goto on_return;
 
1926
 
 
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;
 
1931
 
 
1932
        status2 = pjsip_dlg_modify_response(inv->dlg, tdata, st_code2, NULL);
 
1933
        if (status2 != PJ_SUCCESS) {
 
1934
            pjsip_tx_data_dec_ref(tdata);
 
1935
            goto on_return;
 
1936
        }
 
1937
        status2 = pjsip_timer_update_resp(inv, tdata);
 
1938
        if (status2 == PJ_SUCCESS)
 
1939
            *p_tdata = tdata;
 
1940
        else
 
1941
            pjsip_tx_data_dec_ref(tdata);
 
1942
 
 
1943
        goto on_return;
 
1944
    }
 
1945
 
 
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);
 
1950
        goto on_return;
 
1951
    }
 
1952
 
 
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)));
 
1958
 
 
1959
    /* Invoke Session Timers */
 
1960
    pjsip_timer_update_resp(inv, tdata);
 
1961
 
 
1962
    *p_tdata = tdata;
 
1963
 
 
1964
on_return:
 
1965
    pjsip_dlg_dec_lock(inv->dlg);
 
1966
    return status;
 
1967
}
 
1968
 
 
1969
 
 
1970
/*
 
1971
 * Answer initial INVITE
 
1972
 * Re-INVITE will be answered automatically, and will not use this function.
 
1973
 */ 
 
1974
PJ_DEF(pj_status_t) pjsip_inv_answer(   pjsip_inv_session *inv,
 
1975
                                        int st_code,
 
1976
                                        const pj_str_t *st_text,
 
1977
                                        const pjmedia_sdp_session *local_sdp,
 
1978
                                        pjsip_tx_data **p_tdata )
 
1979
{
 
1980
    pjsip_tx_data *last_res;
 
1981
    pj_status_t status;
 
1982
 
 
1983
    /* Verify arguments. */
 
1984
    PJ_ASSERT_RETURN(inv && p_tdata, PJ_EINVAL);
 
1985
 
 
1986
    /* Must have INVITE transaction. */
 
1987
    PJ_ASSERT_RETURN(inv->invite_tsx, PJ_EBUG);
 
1988
 
 
1989
    /* Must have created an answer before */
 
1990
    PJ_ASSERT_RETURN(inv->last_answer, PJ_EINVALIDOP);
 
1991
 
 
1992
    pjsip_dlg_inc_lock(inv->dlg);
 
1993
 
 
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)
 
1998
        goto on_return;
 
1999
 
 
2000
    /* For non-2xx final response, strip message body */
 
2001
    if (st_code >= 300) {
 
2002
        last_res->msg->body = NULL;
 
2003
    }
 
2004
 
 
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);
 
2009
        goto on_return;
 
2010
    }
 
2011
 
 
2012
    /* Invoke Session Timers */
 
2013
    pjsip_timer_update_resp(inv, last_res);
 
2014
 
 
2015
    *p_tdata = last_res;
 
2016
 
 
2017
on_return:
 
2018
    pjsip_dlg_dec_lock(inv->dlg);
 
2019
    return status;
 
2020
}
 
2021
 
 
2022
 
 
2023
/*
 
2024
 * Set SDP answer.
 
2025
 */
 
2026
PJ_DEF(pj_status_t) pjsip_inv_set_sdp_answer( pjsip_inv_session *inv,
 
2027
                                              const pjmedia_sdp_session *sdp )
 
2028
{
 
2029
    pj_status_t status;
 
2030
 
 
2031
    PJ_ASSERT_RETURN(inv && sdp, PJ_EINVAL);
 
2032
 
 
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);
 
2036
 
 
2037
    return status;
 
2038
}
 
2039
 
 
2040
 
 
2041
/*
 
2042
 * End session.
 
2043
 */
 
2044
PJ_DEF(pj_status_t) pjsip_inv_end_session(  pjsip_inv_session *inv,
 
2045
                                            int st_code,
 
2046
                                            const pj_str_t *st_text,
 
2047
                                            pjsip_tx_data **p_tdata )
 
2048
{
 
2049
    pjsip_tx_data *tdata;
 
2050
    pj_status_t status;
 
2051
 
 
2052
    /* Verify arguments. */
 
2053
    PJ_ASSERT_RETURN(inv && p_tdata, PJ_EINVAL);
 
2054
 
 
2055
    /* Set cause code. */
 
2056
    inv_set_cause(inv, st_code, st_text);
 
2057
 
 
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:
 
2063
 
 
2064
        if (inv->role == PJSIP_ROLE_UAC) {
 
2065
 
 
2066
            /* For UAC when session has not been confirmed, create CANCEL. */
 
2067
 
 
2068
            /* MUST have the original UAC INVITE transaction. */
 
2069
            PJ_ASSERT_RETURN(inv->invite_tsx != NULL, PJ_EBUG);
 
2070
 
 
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.
 
2074
             */
 
2075
            if (inv->invite_tsx->status_code < 100) {
 
2076
 
 
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;
 
2081
                *p_tdata = NULL;
 
2082
                PJ_LOG(4, (inv->obj_name, "Delaying CANCEL since no "
 
2083
                           "provisional response is received yet"));
 
2084
                return PJ_SUCCESS;
 
2085
            }
 
2086
 
 
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.
 
2090
             * So fix this!
 
2091
             */
 
2092
            status = pjsip_endpt_create_cancel(inv->dlg->endpt, 
 
2093
                                               inv->invite_tsx->last_tx,
 
2094
                                               &tdata);
 
2095
            if (status != PJ_SUCCESS)
 
2096
                return status;
 
2097
 
 
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.
 
2101
             */
 
2102
            pjsip_tsx_set_timeout(inv->invite_tsx, 64 * pjsip_cfg()->tsx.t1);
 
2103
 
 
2104
        } else {
 
2105
 
 
2106
            /* For UAS, send a final response. */
 
2107
            tdata = inv->invite_tsx->last_tx;
 
2108
            PJ_ASSERT_RETURN(tdata != NULL, PJ_EINVALIDOP);
 
2109
 
 
2110
            //status = pjsip_dlg_modify_response(inv->dlg, tdata, st_code,
 
2111
            //                                 st_text);
 
2112
            status = pjsip_inv_answer(inv, st_code, st_text, NULL, &tdata);
 
2113
        }
 
2114
        break;
 
2115
 
 
2116
    case PJSIP_INV_STATE_CONNECTING:
 
2117
    case PJSIP_INV_STATE_CONFIRMED:
 
2118
        /* End Session Timer */
 
2119
        pjsip_timer_end_session(inv);
 
2120
 
 
2121
        /* For established dialog, send BYE */
 
2122
        status = pjsip_dlg_create_request(inv->dlg, pjsip_get_bye_method(), 
 
2123
                                          -1, &tdata);
 
2124
        break;
 
2125
 
 
2126
    case PJSIP_INV_STATE_DISCONNECTED:
 
2127
        /* No need to do anything. */
 
2128
        return PJSIP_ESESSIONTERMINATED;
 
2129
 
 
2130
    default:
 
2131
        pj_assert("!Invalid operation!");
 
2132
        return PJ_EINVALIDOP;
 
2133
    }
 
2134
 
 
2135
    if (status != PJ_SUCCESS)
 
2136
        return status;
 
2137
 
 
2138
 
 
2139
    /* Done */
 
2140
 
 
2141
    inv->cancelling = PJ_TRUE;
 
2142
    *p_tdata = tdata;
 
2143
 
 
2144
    return PJ_SUCCESS;
 
2145
}
 
2146
 
 
2147
/* Following redirection recursion, get next target from the target set and
 
2148
 * notify user.
 
2149
 *
 
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.
 
2153
 *
 
2154
 * Note:
 
2155
 *   the event 'e' argument may be NULL.
 
2156
 */
 
2157
static pj_bool_t inv_uac_recurse(pjsip_inv_session *inv, int code,
 
2158
                                 const pj_str_t *reason, pjsip_event *e)
 
2159
{
 
2160
    pjsip_redirect_op op;
 
2161
    pjsip_target *target;
 
2162
 
 
2163
    /* Won't redirect if the callback is not implemented. */
 
2164
    if (mod_inv.cb.on_redirected == NULL)
 
2165
        return PJ_FALSE;
 
2166
 
 
2167
    if (reason == NULL)
 
2168
        reason = pjsip_get_status_text(code);
 
2169
 
 
2170
    /* Set status of current target */
 
2171
    pjsip_target_assign_status(inv->dlg->target_set.current, inv->dlg->pool,
 
2172
                               code, reason);
 
2173
 
 
2174
    /* Fetch next target from the target set. We only want to
 
2175
     * process SIP/SIPS URI for now.
 
2176
     */
 
2177
    for (;;) {
 
2178
        target = pjsip_target_set_get_next(&inv->dlg->target_set);
 
2179
        if (target == NULL) {
 
2180
            /* No more target. */
 
2181
            return PJ_FALSE;
 
2182
        }
 
2183
 
 
2184
        if (!PJSIP_URI_SCHEME_IS_SIP(target->uri) &&
 
2185
            !PJSIP_URI_SCHEME_IS_SIPS(target->uri))
 
2186
        {
 
2187
            code = PJSIP_SC_UNSUPPORTED_URI_SCHEME;
 
2188
            reason = pjsip_get_status_text(code);
 
2189
 
 
2190
            /* Mark this target as unusable and fetch next target. */
 
2191
            pjsip_target_assign_status(target, inv->dlg->pool, code, reason);
 
2192
        } else {
 
2193
            /* Found a target */
 
2194
            break;
 
2195
        }
 
2196
    }
 
2197
 
 
2198
    /* We have target in 'target'. Set this target as current target
 
2199
     * and notify callback. 
 
2200
     */
 
2201
    pjsip_target_set_set_current(&inv->dlg->target_set, target);
 
2202
 
 
2203
    op = (*mod_inv.cb.on_redirected)(inv, target->uri, e);
 
2204
 
 
2205
 
 
2206
    /* Check what the application wants to do now */
 
2207
    switch (op) {
 
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().
 
2212
         */
 
2213
        pjsip_dlg_inc_session(inv->dlg, &mod_inv.mod);
 
2214
 
 
2215
        /* Act on the recursion */
 
2216
        pjsip_inv_process_redirect(inv, op, e);
 
2217
        return PJ_TRUE;
 
2218
 
 
2219
    case PJSIP_REDIRECT_PENDING:
 
2220
        /* Increment session so that the dialog/session is not destroyed 
 
2221
         * while we're waiting for user confirmation.
 
2222
         */
 
2223
        pjsip_dlg_inc_session(inv->dlg, &mod_inv.mod);
 
2224
 
 
2225
        /* Also clear the invite_tsx variable, otherwise when this tsx is
 
2226
         * terminated, it will also terminate the session.
 
2227
         */
 
2228
        inv->invite_tsx = NULL;
 
2229
 
 
2230
        /* Done. The processing will continue once the application calls
 
2231
         * pjsip_inv_process_redirect().
 
2232
         */
 
2233
        return PJ_TRUE;
 
2234
 
 
2235
    case PJSIP_REDIRECT_REJECT:
 
2236
        /* Recursively call  this function again to fetch next target, if any.
 
2237
         */
 
2238
        return inv_uac_recurse(inv, PJSIP_SC_REQUEST_TERMINATED, NULL, e);
 
2239
 
 
2240
    }
 
2241
 
 
2242
    pj_assert(!"Should not reach here");
 
2243
    return PJ_FALSE;
 
2244
}
 
2245
 
 
2246
 
 
2247
/* Process redirection/recursion */
 
2248
PJ_DEF(pj_status_t) pjsip_inv_process_redirect( pjsip_inv_session *inv,
 
2249
                                                pjsip_redirect_op op,
 
2250
                                                pjsip_event *e)
 
2251
{
 
2252
    const pjsip_status_code cancel_code = PJSIP_SC_REQUEST_TERMINATED;
 
2253
    pjsip_event usr_event;
 
2254
    pj_status_t status = PJ_SUCCESS;
 
2255
 
 
2256
    PJ_ASSERT_RETURN(inv && op != PJSIP_REDIRECT_PENDING, PJ_EINVAL);
 
2257
 
 
2258
    if (e == NULL) {
 
2259
        PJSIP_EVENT_INIT_USER(usr_event, NULL, NULL, NULL, NULL);
 
2260
        e = &usr_event;
 
2261
    }
 
2262
 
 
2263
    pjsip_dlg_inc_lock(inv->dlg);
 
2264
 
 
2265
    /* Decrement session. That's the convention here to prevent the dialog 
 
2266
     * or session from being destroyed while we're waiting for user
 
2267
     * confirmation.
 
2268
     */
 
2269
    pjsip_dlg_dec_session(inv->dlg, &mod_inv.mod);
 
2270
 
 
2271
    /* See what the application wants to do now */
 
2272
    switch (op) {
 
2273
    case PJSIP_REDIRECT_ACCEPT:
 
2274
        /* User accept the redirection. Reset the session and resend the 
 
2275
         * INVITE request.
 
2276
         */
 
2277
        {
 
2278
            pjsip_tx_data *tdata;
 
2279
            pjsip_via_hdr *via;
 
2280
 
 
2281
            /* Get the original INVITE request. */
 
2282
            tdata = inv->invite_req;
 
2283
            pjsip_tx_data_add_ref(tdata);
 
2284
 
 
2285
            /* Restore strict route set.
 
2286
             * See http://trac.pjsip.org/repos/ticket/492
 
2287
             */
 
2288
            pjsip_restore_strict_route_set(tdata);
 
2289
 
 
2290
            /* Set target */
 
2291
            tdata->msg->line.req.uri = (pjsip_uri*)
 
2292
               pjsip_uri_clone(tdata->pool, inv->dlg->target_set.current->uri);
 
2293
 
 
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;
 
2298
 
 
2299
            /* Reset message destination info (see #1248). */
 
2300
            pj_bzero(&tdata->dest_info, sizeof(tdata->dest_info));
 
2301
 
 
2302
            /* Must invalidate the message! */
 
2303
            pjsip_tx_data_invalidate_msg(tdata);
 
2304
 
 
2305
            /* Reset the session */
 
2306
            pjsip_inv_uac_restart(inv, PJ_FALSE);
 
2307
 
 
2308
            /* (re)Send the INVITE request */
 
2309
            status = pjsip_inv_send_msg(inv, tdata);
 
2310
        }
 
2311
        break;
 
2312
 
 
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);
 
2317
 
 
2318
        /* Caller should expect that the invite session is gone now, so
 
2319
         * we don't need to set status to PJSIP_ESESSIONTERMINATED here.
 
2320
         */
 
2321
        break;
 
2322
 
 
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);
 
2329
 
 
2330
            /* Tell caller that the invite session is gone now */
 
2331
            status = PJSIP_ESESSIONTERMINATED;
 
2332
        }
 
2333
        break;
 
2334
 
 
2335
 
 
2336
    case PJSIP_REDIRECT_PENDING:
 
2337
        pj_assert(!"Should not happen");
 
2338
        break;
 
2339
    }
 
2340
 
 
2341
 
 
2342
    pjsip_dlg_dec_lock(inv->dlg);
 
2343
 
 
2344
    return status;
 
2345
}
 
2346
 
 
2347
 
 
2348
/*
 
2349
 * Create re-INVITE.
 
2350
 */
 
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 )
 
2355
{
 
2356
    pj_status_t status;
 
2357
    pjsip_contact_hdr *contact_hdr = NULL;
 
2358
 
 
2359
    /* Check arguments. */
 
2360
    PJ_ASSERT_RETURN(inv && p_tdata, PJ_EINVAL);
 
2361
 
 
2362
    /* Must NOT have a pending INVITE transaction */
 
2363
    if (inv->invite_tsx!=NULL)
 
2364
        return PJ_EINVALIDOP;
 
2365
 
 
2366
 
 
2367
    pjsip_dlg_inc_lock(inv->dlg);
 
2368
 
 
2369
    if (new_contact) {
 
2370
        pj_str_t tmp;
 
2371
        const pj_str_t STR_CONTACT = { "Contact", 7 };
 
2372
 
 
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);
 
2377
        if (!contact_hdr) {
 
2378
            status = PJSIP_EINVALIDURI;
 
2379
            goto on_return;
 
2380
        }
 
2381
    }
 
2382
 
 
2383
 
 
2384
    if (new_offer) {
 
2385
        if (!inv->neg) {
 
2386
            status = pjmedia_sdp_neg_create_w_local_offer(inv->pool, 
 
2387
                                                          new_offer,
 
2388
                                                          &inv->neg);
 
2389
            if (status != PJ_SUCCESS)
 
2390
                goto on_return;
 
2391
 
 
2392
        } else switch (pjmedia_sdp_neg_get_state(inv->neg)) {
 
2393
 
 
2394
            case PJMEDIA_SDP_NEG_STATE_NULL:
 
2395
                pj_assert(!"Unexpected SDP neg state NULL");
 
2396
                status = PJ_EBUG;
 
2397
                goto on_return;
 
2398
 
 
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"));
 
2403
                break;
 
2404
 
 
2405
            case PJMEDIA_SDP_NEG_STATE_REMOTE_OFFER:
 
2406
                status = pjmedia_sdp_neg_set_local_answer(inv->pool_prov, 
 
2407
                                                          inv->neg,
 
2408
                                                          new_offer);
 
2409
                if (status != PJ_SUCCESS)
 
2410
                    goto on_return;
 
2411
                break;
 
2412
 
 
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"));
 
2417
                break;
 
2418
 
 
2419
            case PJMEDIA_SDP_NEG_STATE_DONE:
 
2420
                status = pjmedia_sdp_neg_modify_local_offer(inv->pool_prov,
 
2421
                                                            inv->neg,
 
2422
                                                            new_offer);
 
2423
                if (status != PJ_SUCCESS)
 
2424
                    goto on_return;
 
2425
                break;
 
2426
        }
 
2427
    }
 
2428
 
 
2429
    if (contact_hdr)
 
2430
        inv->dlg->local.contact = contact_hdr;
 
2431
 
 
2432
    status = pjsip_inv_invite(inv, p_tdata);
 
2433
 
 
2434
on_return:
 
2435
    pjsip_dlg_dec_lock(inv->dlg);
 
2436
    return status;
 
2437
}
 
2438
 
 
2439
/*
 
2440
 * Create UPDATE.
 
2441
 */
 
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 )
 
2446
{
 
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;
 
2451
 
 
2452
    /* Verify arguments. */
 
2453
    PJ_ASSERT_RETURN(inv && p_tdata, PJ_EINVAL);
 
2454
 
 
2455
    /* Dialog must have been established */
 
2456
    PJ_ASSERT_RETURN(inv->dlg->state == PJSIP_DIALOG_STATE_ESTABLISHED,
 
2457
                     PJ_EINVALIDOP);
 
2458
 
 
2459
    /* Invite session must not have been disconnected */
 
2460
    PJ_ASSERT_RETURN(inv->state < PJSIP_INV_STATE_DISCONNECTED,
 
2461
                     PJ_EINVALIDOP);
 
2462
 
 
2463
    /* Lock dialog. */
 
2464
    pjsip_dlg_inc_lock(inv->dlg);
 
2465
 
 
2466
    /* Process offer, if any */
 
2467
    if (offer) {
 
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;
 
2472
            goto on_error;
 
2473
        }
 
2474
 
 
2475
        /* Notify negotiator about the new offer. This will fix the offer
 
2476
         * with correct SDP origin.
 
2477
         */
 
2478
        status = pjmedia_sdp_neg_modify_local_offer(inv->pool_prov, inv->neg,
 
2479
                                                    offer);
 
2480
        if (status != PJ_SUCCESS)
 
2481
            goto on_error;
 
2482
 
 
2483
        /* Retrieve the "fixed" offer from negotiator */
 
2484
        pjmedia_sdp_neg_get_neg_local(inv->neg, &offer);
 
2485
    }
 
2486
 
 
2487
    /* Update Contact if required */
 
2488
    if (new_contact) {
 
2489
        pj_str_t tmp;
 
2490
        const pj_str_t STR_CONTACT = { "Contact", 7 };
 
2491
 
 
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);
 
2496
        if (!contact_hdr) {
 
2497
            status = PJSIP_EINVALIDURI;
 
2498
            goto on_error;
 
2499
        }
 
2500
 
 
2501
        inv->dlg->local.contact = contact_hdr;
 
2502
    }
 
2503
 
 
2504
    /* Create request */
 
2505
    status = pjsip_dlg_create_request(inv->dlg, &pjsip_update_method,
 
2506
                                      -1, &tdata);
 
2507
    if (status != PJ_SUCCESS)
 
2508
            goto on_error;
 
2509
 
 
2510
    /* Attach SDP body */
 
2511
    if (offer) {
 
2512
        sdp_copy = pjmedia_sdp_session_clone(tdata->pool, offer);
 
2513
        pjsip_create_sdp_body(tdata->pool, sdp_copy, &tdata->msg->body);
 
2514
    }
 
2515
 
 
2516
    /* Unlock dialog. */
 
2517
    pjsip_dlg_dec_lock(inv->dlg);
 
2518
 
 
2519
    status = pjsip_timer_update_req(inv, tdata);
 
2520
    if (status != PJ_SUCCESS)
 
2521
        goto on_error;
 
2522
 
 
2523
    *p_tdata = tdata;
 
2524
 
 
2525
    return PJ_SUCCESS;
 
2526
 
 
2527
on_error:
 
2528
    if (tdata)
 
2529
        pjsip_tx_data_dec_ref(tdata);
 
2530
 
 
2531
    /* Unlock dialog. */
 
2532
    pjsip_dlg_dec_lock(inv->dlg);
 
2533
 
 
2534
    return status;
 
2535
}
 
2536
 
 
2537
/*
 
2538
 * Create an ACK request.
 
2539
 */
 
2540
PJ_DEF(pj_status_t) pjsip_inv_create_ack(pjsip_inv_session *inv,
 
2541
                                         int cseq,
 
2542
                                         pjsip_tx_data **p_tdata)
 
2543
{
 
2544
    const pjmedia_sdp_session *sdp = NULL;
 
2545
    pj_status_t status;
 
2546
 
 
2547
    PJ_ASSERT_RETURN(inv && p_tdata, PJ_EINVAL);
 
2548
 
 
2549
    /* Lock dialog. */
 
2550
    pjsip_dlg_inc_lock(inv->dlg);
 
2551
 
 
2552
    /* Destroy last_ack */
 
2553
    if (inv->last_ack) {
 
2554
        pjsip_tx_data_dec_ref(inv->last_ack);
 
2555
        inv->last_ack = NULL;
 
2556
    }
 
2557
 
 
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);
 
2563
        return status;
 
2564
    }
 
2565
 
 
2566
    /* See if we have pending SDP answer to send */
 
2567
    sdp = inv_has_pending_answer(inv, inv->invite_tsx);
 
2568
    if (sdp) {
 
2569
        inv->last_ack->msg->body = create_sdp_body(inv->last_ack->pool, sdp);
 
2570
    }
 
2571
 
 
2572
    /* Keep this for subsequent response retransmission */
 
2573
    inv->last_ack_cseq = cseq;
 
2574
    pjsip_tx_data_add_ref(inv->last_ack);
 
2575
 
 
2576
    /* Done */
 
2577
    *p_tdata = inv->last_ack;
 
2578
 
 
2579
    /* Unlock dialog. */
 
2580
    pjsip_dlg_dec_lock(inv->dlg);
 
2581
 
 
2582
    return PJ_SUCCESS;
 
2583
}
 
2584
 
 
2585
/*
 
2586
 * Send a request or response message.
 
2587
 */
 
2588
PJ_DEF(pj_status_t) pjsip_inv_send_msg( pjsip_inv_session *inv,
 
2589
                                        pjsip_tx_data *tdata)
 
2590
{
 
2591
    pj_status_t status;
 
2592
 
 
2593
    /* Verify arguments. */
 
2594
    PJ_ASSERT_RETURN(inv && tdata, PJ_EINVAL);
 
2595
 
 
2596
    PJ_LOG(5,(inv->obj_name, "Sending %s", 
 
2597
              pjsip_tx_data_get_info(tdata)));
 
2598
 
 
2599
    if (tdata->msg->type == PJSIP_REQUEST_MSG) {
 
2600
        struct tsx_inv_data *tsx_inv_data;
 
2601
 
 
2602
        pjsip_dlg_inc_lock(inv->dlg);
 
2603
 
 
2604
        /* Check again that we didn't receive incoming re-INVITE */
 
2605
        if (tdata->msg->line.req.method.id==PJSIP_INVITE_METHOD && 
 
2606
            inv->invite_tsx) 
 
2607
        {
 
2608
            pjsip_tx_data_dec_ref(tdata);
 
2609
            pjsip_dlg_dec_lock(inv->dlg);
 
2610
            return PJ_EINVALIDOP;
 
2611
        }
 
2612
 
 
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;
 
2616
 
 
2617
        pjsip_dlg_dec_lock(inv->dlg);
 
2618
 
 
2619
        status = pjsip_dlg_send_request(inv->dlg, tdata, mod_inv.mod.id, 
 
2620
                                        tsx_inv_data);
 
2621
        if (status != PJ_SUCCESS)
 
2622
            return status;
 
2623
 
 
2624
    } else {
 
2625
        pjsip_cseq_hdr *cseq;
 
2626
 
 
2627
        /* Can only do this to send response to original INVITE
 
2628
         * request.
 
2629
         */
 
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),
 
2632
                         PJ_EINVALIDOP);
 
2633
 
 
2634
        if (inv->options & PJSIP_INV_REQUIRE_100REL) {
 
2635
            status = pjsip_100rel_tx_response(inv, tdata);
 
2636
        } else 
 
2637
        {
 
2638
            status = pjsip_dlg_send_response(inv->dlg, inv->invite_tsx, tdata);
 
2639
        }
 
2640
 
 
2641
        if (status != PJ_SUCCESS)
 
2642
            return status;
 
2643
    }
 
2644
 
 
2645
    /* Done (?) */
 
2646
    return PJ_SUCCESS;
 
2647
}
 
2648
 
 
2649
 
 
2650
/*
 
2651
 * Respond to incoming CANCEL request.
 
2652
 */
 
2653
static void inv_respond_incoming_cancel(pjsip_inv_session *inv,
 
2654
                                        pjsip_transaction *cancel_tsx,
 
2655
                                        pjsip_rx_data *rdata)
 
2656
{
 
2657
    pjsip_tx_data *tdata;
 
2658
    pjsip_transaction *invite_tsx;
 
2659
    pj_str_t key;
 
2660
    pj_status_t status;
 
2661
 
 
2662
    /* See if we have matching INVITE server transaction: */
 
2663
 
 
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);
 
2667
 
 
2668
    if (invite_tsx == NULL) {
 
2669
 
 
2670
        /* Invite transaction not found! 
 
2671
         * Respond CANCEL with 481 (RFC 3261 Section 9.2 page 55)
 
2672
         */
 
2673
        status = pjsip_dlg_create_response( inv->dlg, rdata, 481, NULL, 
 
2674
                                            &tdata);
 
2675
 
 
2676
    } else {
 
2677
        /* Always answer CANCEL will 200 (OK) regardless of
 
2678
         * the state of the INVITE transaction.
 
2679
         */
 
2680
        status = pjsip_dlg_create_response( inv->dlg, rdata, 200, NULL, 
 
2681
                                            &tdata);
 
2682
    }
 
2683
 
 
2684
    /* See if we have created the response successfully. */
 
2685
    if (status != PJ_SUCCESS) return;
 
2686
 
 
2687
    /* Send the CANCEL response */
 
2688
    status = pjsip_dlg_send_response(inv->dlg, cancel_tsx, tdata);
 
2689
    if (status != PJ_SUCCESS) return;
 
2690
 
 
2691
 
 
2692
    /* See if we need to terminate the UAS INVITE transaction
 
2693
     * with 487 (Request Terminated) response. 
 
2694
     */
 
2695
    if (invite_tsx && invite_tsx->status_code < 200) {
 
2696
 
 
2697
        pj_assert(invite_tsx->last_tx != NULL);
 
2698
 
 
2699
        tdata = invite_tsx->last_tx;
 
2700
 
 
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);
 
2707
            } else {
 
2708
                status = pjsip_dlg_send_response(inv->dlg, invite_tsx, 
 
2709
                                                 tdata);
 
2710
            }
 
2711
        }
 
2712
    }
 
2713
 
 
2714
    if (invite_tsx)
 
2715
        pj_mutex_unlock(invite_tsx->mutex);
 
2716
}
 
2717
 
 
2718
 
 
2719
/*
 
2720
 * Respond to incoming BYE request.
 
2721
 */
 
2722
static void inv_respond_incoming_bye( pjsip_inv_session *inv,
 
2723
                                      pjsip_transaction *bye_tsx,
 
2724
                                      pjsip_rx_data *rdata,
 
2725
                                      pjsip_event *e )
 
2726
{
 
2727
    pj_status_t status;
 
2728
    pjsip_tx_data *tdata;
 
2729
 
 
2730
    /* Respond BYE with 200: */
 
2731
 
 
2732
    status = pjsip_dlg_create_response(inv->dlg, rdata, 200, NULL, &tdata);
 
2733
    if (status != PJ_SUCCESS) return;
 
2734
 
 
2735
    status = pjsip_dlg_send_response(inv->dlg, bye_tsx, tdata);
 
2736
    if (status != PJ_SUCCESS) return;
 
2737
 
 
2738
    /* Terminate session: */
 
2739
 
 
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);
 
2743
    }
 
2744
}
 
2745
 
 
2746
/*
 
2747
 * Respond to BYE request.
 
2748
 */
 
2749
static void inv_handle_bye_response( pjsip_inv_session *inv,
 
2750
                                     pjsip_transaction *tsx,
 
2751
                                     pjsip_rx_data *rdata,
 
2752
                                     pjsip_event *e )
 
2753
{
 
2754
    pj_status_t status;
 
2755
    
 
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);
 
2759
        return;
 
2760
    }
 
2761
 
 
2762
    /* Handle 401/407 challenge. */
 
2763
    if (tsx->status_code == 401 || tsx->status_code == 407) {
 
2764
 
 
2765
        pjsip_tx_data *tdata;
 
2766
        
 
2767
        status = pjsip_auth_clt_reinit_req( &inv->dlg->auth_sess, 
 
2768
                                            rdata,
 
2769
                                            tsx->last_tx,
 
2770
                                            &tdata);
 
2771
        
 
2772
        if (status != PJ_SUCCESS) {
 
2773
            
 
2774
            /* Does not have proper credentials. 
 
2775
             * End the session anyway.
 
2776
             */
 
2777
            inv_set_cause(inv, PJSIP_SC_OK, NULL);
 
2778
            inv_set_state(inv, PJSIP_INV_STATE_DISCONNECTED, e);
 
2779
            
 
2780
        } else {
 
2781
            /* Re-send BYE. */
 
2782
            status = pjsip_inv_send_msg(inv, tdata);
 
2783
        }
 
2784
 
 
2785
    } else {
 
2786
 
 
2787
        /* End the session. */
 
2788
        inv_set_cause(inv, PJSIP_SC_OK, NULL);
 
2789
        inv_set_state(inv, PJSIP_INV_STATE_DISCONNECTED, e);
 
2790
    }
 
2791
 
 
2792
}
 
2793
 
 
2794
/*
 
2795
 * Respond to incoming UPDATE request.
 
2796
 */
 
2797
static void inv_respond_incoming_update(pjsip_inv_session *inv,
 
2798
                                        pjsip_rx_data *rdata)
 
2799
{
 
2800
    pjmedia_sdp_neg_state neg_state;
 
2801
    pj_status_t status;
 
2802
    pjsip_tx_data *tdata = NULL;
 
2803
    pjsip_status_code st_code;
 
2804
 
 
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,
 
2809
                                           NULL, &tdata);
 
2810
        goto on_return;
 
2811
    }
 
2812
 
 
2813
    neg_state = pjmedia_sdp_neg_get_state(inv->neg);
 
2814
 
 
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,
 
2819
                                           &tdata);
 
2820
    }
 
2821
    /* Send 500 with Retry-After header set randomly between 0 and 10 if we 
 
2822
     * receive UPDATE while we haven't sent answer.
 
2823
     */
 
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,
 
2828
                                           NULL, &tdata);
 
2829
 
 
2830
    /* If UPDATE doesn't contain SDP, just respond with 200/OK.
 
2831
     * This is a valid scenario according to session-timer draft.
 
2832
     */
 
2833
    } else if (rdata->msg_info.msg->body == NULL) {
 
2834
 
 
2835
        status = pjsip_dlg_create_response(inv->dlg, rdata, 
 
2836
                                           200, NULL, &tdata);
 
2837
 
 
2838
    } else {
 
2839
        /* We receive new offer from remote */
 
2840
        inv_check_sdp_in_incoming_msg(inv, pjsip_rdata_get_tsx(rdata), rdata);
 
2841
 
 
2842
        /* Application MUST have supplied the answer by now.
 
2843
         * If so, negotiate the SDP.
 
2844
         */
 
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)
 
2848
        {
 
2849
            /* Negotiation has failed. If negotiator is still
 
2850
             * stuck at non-DONE state, cancel any ongoing offer.
 
2851
             */
 
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);
 
2855
            }
 
2856
 
 
2857
            status = pjsip_dlg_create_response(inv->dlg, rdata, 
 
2858
                                               PJSIP_SC_NOT_ACCEPTABLE_HERE,
 
2859
                                               NULL, &tdata);
 
2860
        } else {
 
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);
 
2869
            }
 
2870
        }
 
2871
    }
 
2872
 
 
2873
on_return:
 
2874
    /* Invoke Session Timers */
 
2875
    if (status == PJ_SUCCESS)
 
2876
        status = pjsip_timer_update_resp(inv, tdata);
 
2877
 
 
2878
    if (status != PJ_SUCCESS) {
 
2879
        if (tdata != NULL) {
 
2880
            pjsip_tx_data_dec_ref(tdata);
 
2881
            tdata = NULL;
 
2882
        }
 
2883
        return;
 
2884
    }
 
2885
 
 
2886
    pjsip_dlg_send_response(inv->dlg, pjsip_rdata_get_tsx(rdata), tdata);
 
2887
}
 
2888
 
 
2889
 
 
2890
/*
 
2891
 * Handle incoming response to UAC UPDATE request.
 
2892
 */
 
2893
static void inv_handle_update_response( pjsip_inv_session *inv,
 
2894
                                        pjsip_event *e)
 
2895
{
 
2896
    pjsip_transaction *tsx = e->body.tsx_state.tsx;
 
2897
    struct tsx_inv_data *tsx_inv_data = NULL;
 
2898
    pj_status_t status = -1;
 
2899
 
 
2900
    /* Handle 401/407 challenge. */
 
2901
    if (tsx->state == PJSIP_TSX_STATE_COMPLETED &&
 
2902
        (tsx->status_code == 401 || tsx->status_code == 407))
 
2903
    {
 
2904
        pjsip_tx_data *tdata;
 
2905
        
 
2906
        status = pjsip_auth_clt_reinit_req( &inv->dlg->auth_sess, 
 
2907
                                            e->body.tsx_state.src.rdata,
 
2908
                                            tsx->last_tx,
 
2909
                                            &tdata);
 
2910
        
 
2911
        if (status != PJ_SUCCESS) {
 
2912
            
 
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.
 
2916
             */
 
2917
            /*
 
2918
            inv_set_cause(inv, PJSIP_SC_OK, NULL);
 
2919
            inv_set_state(inv, PJSIP_INV_STATE_DISCONNECTED, e);
 
2920
            */
 
2921
            
 
2922
        } else {
 
2923
            /* Re-send request. */
 
2924
            status = pjsip_inv_send_msg(inv, tdata);
 
2925
        }
 
2926
    }
 
2927
 
 
2928
    /* Process 422 response */
 
2929
    else if (tsx->state == PJSIP_TSX_STATE_COMPLETED &&
 
2930
             tsx->status_code == 422)
 
2931
    {
 
2932
        status = handle_timer_response(inv, e->body.tsx_state.src.rdata,
 
2933
                                       PJ_FALSE);
 
2934
    }
 
2935
 
 
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)
 
2940
    {
 
2941
        status = handle_timer_response(inv, e->body.tsx_state.src.rdata,
 
2942
                                       PJ_FALSE);
 
2943
        status = inv_check_sdp_in_incoming_msg(inv, tsx, 
 
2944
                                             e->body.tsx_state.src.rdata);
 
2945
    }
 
2946
    
 
2947
    /* Get/attach invite session's transaction data */
 
2948
    else 
 
2949
    {
 
2950
        /* Session-Timer needs to see any error responses, to determine
 
2951
         * whether peer supports UPDATE with empty body.
 
2952
         */
 
2953
        if (tsx->state == PJSIP_TSX_STATE_COMPLETED &&
 
2954
            tsx->role == PJSIP_ROLE_UAC)
 
2955
        {
 
2956
            status = handle_timer_response(inv, e->body.tsx_state.src.rdata,
 
2957
                                           PJ_FALSE);
 
2958
        }
 
2959
 
 
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;
 
2965
        }
 
2966
    }
 
2967
 
 
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) 
 
2972
    {
 
2973
        pjmedia_sdp_neg_cancel_offer(inv->neg);
 
2974
 
 
2975
        /* Prevent from us cancelling different offer! */
 
2976
        tsx_inv_data->sdp_done = PJ_TRUE;
 
2977
    }
 
2978
}
 
2979
 
 
2980
 
 
2981
/*
 
2982
 * Handle incoming reliable response.
 
2983
 */
 
2984
static void inv_handle_incoming_reliable_response(pjsip_inv_session *inv,
 
2985
                                                  pjsip_rx_data *rdata)
 
2986
{
 
2987
    pjsip_tx_data *tdata;
 
2988
    const pjmedia_sdp_session *sdp;
 
2989
    pj_status_t status;
 
2990
 
 
2991
    /* Create PRACK */
 
2992
    status = pjsip_100rel_create_prack(inv, rdata, &tdata);
 
2993
    if (status != PJ_SUCCESS)
 
2994
        return;
 
2995
 
 
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));
 
2998
    if (sdp) {
 
2999
        tdata->msg->body = create_sdp_body(tdata->pool, sdp);
 
3000
    }
 
3001
 
 
3002
    /* Send PRACK (must be using 100rel module!) */
 
3003
    pjsip_100rel_send_prack(inv, tdata);
 
3004
}
 
3005
 
 
3006
 
 
3007
/*
 
3008
 * Handle incoming PRACK.
 
3009
 */
 
3010
static void inv_respond_incoming_prack(pjsip_inv_session *inv,
 
3011
                                       pjsip_rx_data *rdata)
 
3012
{
 
3013
    pj_status_t status;
 
3014
 
 
3015
    /* Run through 100rel module to see if we can accept this
 
3016
     * PRACK request. The 100rel will send 200/OK to PRACK request.
 
3017
     */
 
3018
    status = pjsip_100rel_on_rx_prack(inv, rdata);
 
3019
    if (status != PJ_SUCCESS)
 
3020
        return;
 
3021
 
 
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);
 
3026
    } else {
 
3027
        /* No SDP body */
 
3028
        status = -1;
 
3029
    }
 
3030
 
 
3031
    /* If SDP negotiation has been successful, also mark the
 
3032
     * SDP negotiation flag in the invite transaction to be
 
3033
     * done too.
 
3034
     */
 
3035
    if (status == PJ_SUCCESS && inv->invite_tsx) {
 
3036
        struct tsx_inv_data *tsx_inv_data;
 
3037
 
 
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;
 
3046
        }
 
3047
        
 
3048
        tsx_inv_data->sdp_done = PJ_TRUE;
 
3049
    }
 
3050
}
 
3051
 
 
3052
 
 
3053
/*
 
3054
 * State NULL is before anything is sent/received.
 
3055
 */
 
3056
static void inv_on_state_null( pjsip_inv_session *inv, pjsip_event *e)
 
3057
{
 
3058
    pjsip_transaction *tsx = e->body.tsx_state.tsx;
 
3059
    pjsip_dialog *dlg = pjsip_tsx_get_dlg(tsx);
 
3060
 
 
3061
    PJ_ASSERT_ON_FAIL(tsx && dlg, return);
 
3062
 
 
3063
    if (tsx->method.id == PJSIP_INVITE_METHOD) {
 
3064
 
 
3065
        /* Keep the initial INVITE transaction. */
 
3066
        if (inv->invite_tsx == NULL)
 
3067
            inv->invite_tsx = tsx;
 
3068
 
 
3069
        if (dlg->role == PJSIP_ROLE_UAC) {
 
3070
 
 
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.
 
3074
             */
 
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;
 
3079
                }
 
3080
                inv->invite_req = tsx->last_tx;
 
3081
                pjsip_tx_data_add_ref(inv->invite_req);
 
3082
            }
 
3083
 
 
3084
            switch (tsx->state) {
 
3085
            case PJSIP_TSX_STATE_CALLING:
 
3086
                inv_set_state(inv, PJSIP_INV_STATE_CALLING, e);
 
3087
                break;
 
3088
            default:
 
3089
                inv_on_state_calling(inv, e);
 
3090
                break;
 
3091
            }
 
3092
 
 
3093
        } else {
 
3094
            switch (tsx->state) {
 
3095
            case PJSIP_TSX_STATE_TRYING:
 
3096
                inv_set_state(inv, PJSIP_INV_STATE_INCOMING, e);
 
3097
                break;
 
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);
 
3102
                break;
 
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);
 
3107
                break;
 
3108
            default:
 
3109
                inv_on_state_incoming(inv, e);
 
3110
                break;
 
3111
            }
 
3112
        }
 
3113
 
 
3114
    } else {
 
3115
        pj_assert(!"Unexpected transaction type");
 
3116
    }
 
3117
}
 
3118
 
 
3119
/*
 
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.
 
3124
 */
 
3125
static pj_bool_t handle_uac_tsx_response(pjsip_inv_session *inv, 
 
3126
                                         pjsip_event *e)
 
3127
{
 
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.)
 
3134
     * 
 
3135
     *  For INVITE initiated dialogs, terminating the dialog consists of
 
3136
     *  sending a BYE.
 
3137
     *
 
3138
     * Note:
 
3139
     *  according to X, this should terminate dialog usage only, not the 
 
3140
     *  dialog.
 
3141
     */
 
3142
    pjsip_transaction *tsx = e->body.tsx_state.tsx;
 
3143
 
 
3144
    pj_assert(tsx->role == PJSIP_UAC_ROLE);
 
3145
 
 
3146
    /* Note that 481 response to CANCEL does not terminate dialog usage,
 
3147
     * but only the transaction.
 
3148
     */
 
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))
 
3155
    {
 
3156
        pjsip_tx_data *bye;
 
3157
        pj_status_t status;
 
3158
 
 
3159
        inv_set_cause(inv, tsx->status_code, &tsx->status_text);
 
3160
        inv_set_state(inv, PJSIP_INV_STATE_DISCONNECTED, e);
 
3161
 
 
3162
        /* Send BYE */
 
3163
        status = pjsip_dlg_create_request(inv->dlg, pjsip_get_bye_method(), 
 
3164
                                          -1, &bye);
 
3165
        if (status == PJ_SUCCESS) {
 
3166
            pjsip_inv_send_msg(inv, bye);
 
3167
        }
 
3168
 
 
3169
        return PJ_TRUE; /* Handled */
 
3170
 
 
3171
    } 
 
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)) 
 
3176
    {
 
3177
 
 
3178
        pjsip_tx_data *tdata;
 
3179
        pj_status_t status;
 
3180
 
 
3181
        if (tsx->method.id == PJSIP_INVITE_METHOD)
 
3182
            inv->invite_tsx = NULL;
 
3183
 
 
3184
        status = pjsip_auth_clt_reinit_req( &inv->dlg->auth_sess, 
 
3185
                                            e->body.tsx_state.src.rdata,
 
3186
                                            tsx->last_tx, &tdata);
 
3187
    
 
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.
 
3192
             */
 
3193
            /*
 
3194
            inv_set_cause(inv, PJSIP_SC_OK, NULL);
 
3195
            inv_set_state(inv, PJSIP_INV_STATE_DISCONNECTED, e);
 
3196
            */
 
3197
            
 
3198
        } else {
 
3199
            /* Re-send request. */
 
3200
            status = pjsip_inv_send_msg(inv, tdata);
 
3201
        }
 
3202
 
 
3203
        return PJ_TRUE; /* Handled */
 
3204
    }
 
3205
 
 
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) 
 
3209
    {
 
3210
        handle_timer_response(inv, e->body.tsx_state.src.rdata, 
 
3211
                              PJ_FALSE);
 
3212
 
 
3213
        return PJ_TRUE; /* Handled */
 
3214
 
 
3215
    } else {
 
3216
        return PJ_FALSE; /* Unhandled */
 
3217
    }
 
3218
}
 
3219
 
 
3220
 
 
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()
 
3228
 *    callback.
 
3229
 *  - for other cases -- disconnect the session.
 
3230
 */
 
3231
static void handle_uac_call_rejection(pjsip_inv_session *inv, pjsip_event *e)
 
3232
{
 
3233
    pjsip_transaction *tsx = e->body.tsx_state.tsx;
 
3234
    pj_status_t status;
 
3235
    
 
3236
    if (PJSIP_IS_STATUS_IN_CLASS(tsx->status_code, 300)) {
 
3237
 
 
3238
        if (mod_inv.cb.on_redirected == NULL) {
 
3239
 
 
3240
            /* Redirection callback is not implemented, disconnect the
 
3241
             * call.
 
3242
             */
 
3243
            goto terminate_session;
 
3244
 
 
3245
        } else {
 
3246
            const pjsip_msg *res_msg;
 
3247
 
 
3248
            res_msg = e->body.tsx_state.src.rdata->msg_info.msg;
 
3249
 
 
3250
            /* Gather all Contact URI's in the response and add them
 
3251
             * to target set. The function will take care of removing
 
3252
             * duplicate URI's.
 
3253
             */
 
3254
            pjsip_target_set_add_from_msg(&inv->dlg->target_set, 
 
3255
                                          inv->dlg->pool, res_msg);
 
3256
 
 
3257
            /* Recurse to alternate targets if application allows us */
 
3258
            if (!inv_uac_recurse(inv, tsx->status_code, &tsx->status_text, e))
 
3259
            {
 
3260
                /* Recursion fails, terminate session now */
 
3261
                goto terminate_session;
 
3262
            }
 
3263
 
 
3264
            /* Done */
 
3265
        }
 
3266
 
 
3267
    } else if ((tsx->status_code==401 || tsx->status_code==407) &&
 
3268
                !inv->cancelling) 
 
3269
    {
 
3270
 
 
3271
        /* Handle authentication failure:
 
3272
         * Resend the request with Authorization header.
 
3273
         */
 
3274
        pjsip_tx_data *tdata;
 
3275
 
 
3276
        status = pjsip_auth_clt_reinit_req(&inv->dlg->auth_sess, 
 
3277
                                           e->body.tsx_state.src.rdata,
 
3278
                                           tsx->last_tx,
 
3279
                                           &tdata);
 
3280
 
 
3281
        if (status != PJ_SUCCESS) {
 
3282
 
 
3283
            /* Does not have proper credentials. If we are currently 
 
3284
             * recursing, try the next target. Otherwise end the session.
 
3285
             */
 
3286
            if (!inv_uac_recurse(inv, tsx->status_code, &tsx->status_text, e))
 
3287
            {
 
3288
                /* Recursion fails, terminate session now */
 
3289
                goto terminate_session;
 
3290
            }
 
3291
 
 
3292
        } else {
 
3293
 
 
3294
            /* Restart session. */
 
3295
            pjsip_inv_uac_restart(inv, PJ_FALSE);
 
3296
 
 
3297
            /* Send the request. */
 
3298
            status = pjsip_inv_send_msg(inv, tdata);
 
3299
        }
 
3300
 
 
3301
    } else if (tsx->state == PJSIP_TSX_STATE_COMPLETED &&
 
3302
               tsx->status_code == PJSIP_SC_SESSION_TIMER_TOO_SMALL) 
 
3303
    {
 
3304
        /* Handle session timer 422 response:
 
3305
         * Resend the request with requested session timer setting.
 
3306
         */
 
3307
        status = handle_timer_response(inv, e->body.tsx_state.src.rdata,
 
3308
                                       PJ_TRUE);
 
3309
 
 
3310
    } else if (PJSIP_IS_STATUS_IN_CLASS(tsx->status_code, 600)) {
 
3311
        /* Global error */
 
3312
        goto terminate_session;
 
3313
 
 
3314
    } else {
 
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;
 
3319
        }
 
3320
    }
 
3321
    return;
 
3322
 
 
3323
terminate_session:
 
3324
    inv_set_cause(inv, tsx->status_code, &tsx->status_text);
 
3325
    inv_set_state(inv, PJSIP_INV_STATE_DISCONNECTED, e);
 
3326
}
 
3327
 
 
3328
 
 
3329
/*
 
3330
 * State CALLING is after sending initial INVITE request but before
 
3331
 * any response (with tag) is received.
 
3332
 */
 
3333
static void inv_on_state_calling( pjsip_inv_session *inv, pjsip_event *e)
 
3334
{
 
3335
    pjsip_transaction *tsx = e->body.tsx_state.tsx;
 
3336
    pjsip_dialog *dlg = pjsip_tsx_get_dlg(tsx);
 
3337
    pj_status_t status;
 
3338
 
 
3339
    PJ_ASSERT_ON_FAIL(tsx && dlg, return);
 
3340
    
 
3341
    if (tsx == inv->invite_tsx) {
 
3342
 
 
3343
        switch (tsx->state) {
 
3344
 
 
3345
        case PJSIP_TSX_STATE_CALLING:
 
3346
            inv_set_state(inv, PJSIP_INV_STATE_CALLING, e);
 
3347
            break;
 
3348
 
 
3349
        case PJSIP_TSX_STATE_PROCEEDING:
 
3350
            if (inv->pending_cancel) {
 
3351
                pjsip_tx_data *cancel;
 
3352
 
 
3353
                inv->pending_cancel = PJ_FALSE;
 
3354
 
 
3355
                status = pjsip_inv_end_session(inv, 487, NULL, &cancel);
 
3356
                if (status == PJ_SUCCESS && cancel)
 
3357
                    status = pjsip_inv_send_msg(inv, cancel);
 
3358
            }
 
3359
 
 
3360
            if (dlg->remote.info->tag.slen) {
 
3361
 
 
3362
                inv_set_state(inv, PJSIP_INV_STATE_EARLY, e);
 
3363
 
 
3364
                inv_check_sdp_in_incoming_msg(inv, tsx, 
 
3365
                                              e->body.tsx_state.src.rdata);
 
3366
 
 
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);
 
3370
                }
 
3371
 
 
3372
            } else {
 
3373
                /* Ignore 100 (Trying) response, as it doesn't change
 
3374
                 * session state. It only ceases retransmissions.
 
3375
                 */
 
3376
            }
 
3377
            break;
 
3378
 
 
3379
        case PJSIP_TSX_STATE_COMPLETED:
 
3380
            if (tsx->status_code/100 == 2) {
 
3381
 
 
3382
                /* This should not happen.
 
3383
                 * When transaction receives 2xx, it should be terminated
 
3384
                 */
 
3385
                pj_assert(0);
 
3386
 
 
3387
                /* Process session timer response. */
 
3388
                status = handle_timer_response(inv,
 
3389
                                               e->body.tsx_state.src.rdata,
 
3390
                                               PJ_TRUE);
 
3391
                if (status != PJ_SUCCESS)
 
3392
                    break;
 
3393
 
 
3394
                inv_set_state(inv, PJSIP_INV_STATE_CONNECTING, e);
 
3395
    
 
3396
                inv_check_sdp_in_incoming_msg(inv, tsx, 
 
3397
                                              e->body.tsx_state.src.rdata);
 
3398
 
 
3399
            } else {
 
3400
                handle_uac_call_rejection(inv, e);
 
3401
            }
 
3402
            break;
 
3403
 
 
3404
        case PJSIP_TSX_STATE_TERMINATED:
 
3405
            /* INVITE transaction can be terminated either because UAC
 
3406
             * transaction received 2xx response or because of transport
 
3407
             * error.
 
3408
             */
 
3409
            if (tsx->status_code/100 == 2) {
 
3410
                /* This must be receipt of 2xx response */
 
3411
 
 
3412
                /* Process session timer response. */
 
3413
                status = handle_timer_response(inv,
 
3414
                                               e->body.tsx_state.src.rdata,
 
3415
                                               PJ_TRUE);
 
3416
                if (status != PJ_SUCCESS)
 
3417
                    break;
 
3418
 
 
3419
                /* Set state to CONNECTING */
 
3420
                inv_set_state(inv, PJSIP_INV_STATE_CONNECTING, e);
 
3421
 
 
3422
                inv_check_sdp_in_incoming_msg(inv, tsx, 
 
3423
                                              e->body.tsx_state.src.rdata);
 
3424
 
 
3425
                /* Send ACK */
 
3426
                pj_assert(e->body.tsx_state.type == PJSIP_EVENT_RX_MSG);
 
3427
 
 
3428
                inv_send_ack(inv, e);
 
3429
 
 
3430
            } else  {
 
3431
                inv_set_cause(inv, tsx->status_code, &tsx->status_text);
 
3432
                inv_set_state(inv, PJSIP_INV_STATE_DISCONNECTED, e);
 
3433
            }
 
3434
            break;
 
3435
 
 
3436
        default:
 
3437
            break;
 
3438
        }
 
3439
 
 
3440
    } else if (tsx->role == PJSIP_ROLE_UAC) {
 
3441
        /*
 
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.
 
3447
         */
 
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)
 
3453
        {
 
3454
            inv_set_cause(inv, tsx->status_code, &tsx->status_text);
 
3455
            inv_set_state(inv, PJSIP_INV_STATE_DISCONNECTED, e);
 
3456
        }
 
3457
    }
 
3458
}
 
3459
 
 
3460
/*
 
3461
 * State INCOMING is after we received the request, but before
 
3462
 * responses with tag are sent.
 
3463
 */
 
3464
static void inv_on_state_incoming( pjsip_inv_session *inv, pjsip_event *e)
 
3465
{
 
3466
    pjsip_transaction *tsx = e->body.tsx_state.tsx;
 
3467
    pjsip_dialog *dlg = pjsip_tsx_get_dlg(tsx);
 
3468
 
 
3469
    PJ_ASSERT_ON_FAIL(tsx && dlg, return);
 
3470
 
 
3471
    if (tsx == inv->invite_tsx) {
 
3472
 
 
3473
        /*
 
3474
         * Handle the INVITE state transition.
 
3475
         */
 
3476
 
 
3477
        switch (tsx->state) {
 
3478
 
 
3479
        case PJSIP_TSX_STATE_TRYING:
 
3480
            inv_set_state(inv, PJSIP_INV_STATE_INCOMING, e);
 
3481
            break;
 
3482
 
 
3483
        case PJSIP_TSX_STATE_PROCEEDING:
 
3484
            /*
 
3485
             * Transaction sent provisional response.
 
3486
             */
 
3487
            if (tsx->status_code > 100)
 
3488
                inv_set_state(inv, PJSIP_INV_STATE_EARLY, e);
 
3489
            break;
 
3490
 
 
3491
        case PJSIP_TSX_STATE_COMPLETED:
 
3492
            /*
 
3493
             * Transaction sent final response.
 
3494
             */
 
3495
            if (tsx->status_code/100 == 2) {
 
3496
                inv_set_state(inv, PJSIP_INV_STATE_CONNECTING, e);
 
3497
            } else {
 
3498
                inv_set_cause(inv, tsx->status_code, &tsx->status_text);
 
3499
                inv_set_state(inv, PJSIP_INV_STATE_DISCONNECTED, e);
 
3500
            }
 
3501
            break;
 
3502
 
 
3503
        case PJSIP_TSX_STATE_TERMINATED:
 
3504
            /* 
 
3505
             * This happens on transport error (e.g. failed to send
 
3506
             * response)
 
3507
             */
 
3508
            inv_set_cause(inv, tsx->status_code, &tsx->status_text);
 
3509
            inv_set_state(inv, PJSIP_INV_STATE_DISCONNECTED, e);
 
3510
            break;
 
3511
 
 
3512
        default:
 
3513
            pj_assert(!"Unexpected INVITE state");
 
3514
            break;
 
3515
        }
 
3516
 
 
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 )
 
3521
    {
 
3522
 
 
3523
        /*
 
3524
         * Handle incoming CANCEL request.
 
3525
         */
 
3526
 
 
3527
        inv_respond_incoming_cancel(inv, tsx, e->body.tsx_state.src.rdata);
 
3528
 
 
3529
    }
 
3530
}
 
3531
 
 
3532
/*
 
3533
 * State EARLY is for both UAS and UAC, after response with To tag
 
3534
 * is sent/received.
 
3535
 */
 
3536
static void inv_on_state_early( pjsip_inv_session *inv, pjsip_event *e)
 
3537
{
 
3538
    pjsip_transaction *tsx = e->body.tsx_state.tsx;
 
3539
    pjsip_dialog *dlg = pjsip_tsx_get_dlg(tsx);
 
3540
 
 
3541
    PJ_ASSERT_ON_FAIL(tsx && dlg, return);
 
3542
 
 
3543
    if (tsx == inv->invite_tsx) {
 
3544
 
 
3545
        /*
 
3546
         * Handle the INVITE state progress.
 
3547
         */
 
3548
 
 
3549
        switch (tsx->state) {
 
3550
 
 
3551
        case PJSIP_TSX_STATE_PROCEEDING:
 
3552
            /* Send/received another provisional response. */
 
3553
            inv_set_state(inv, PJSIP_INV_STATE_EARLY, e);
 
3554
 
 
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);
 
3558
 
 
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);
 
3562
                }
 
3563
            }
 
3564
            break;
 
3565
 
 
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) {
 
3570
                    pj_status_t status;
 
3571
 
 
3572
                    /* Process session timer response. */
 
3573
                    status = handle_timer_response(inv, 
 
3574
                                                   e->body.tsx_state.src.rdata,
 
3575
                                                   PJ_TRUE);
 
3576
                    if (status != PJ_SUCCESS)
 
3577
                        break;
 
3578
 
 
3579
                    inv_check_sdp_in_incoming_msg(inv, tsx, 
 
3580
                                                  e->body.tsx_state.src.rdata);
 
3581
                }
 
3582
 
 
3583
            } else if (tsx->role == PJSIP_ROLE_UAC) {
 
3584
 
 
3585
                handle_uac_call_rejection(inv, e);
 
3586
 
 
3587
            } else {
 
3588
                inv_set_cause(inv, tsx->status_code, &tsx->status_text);
 
3589
                inv_set_state(inv, PJSIP_INV_STATE_DISCONNECTED, e);
 
3590
            }
 
3591
            break;
 
3592
 
 
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) */
 
3596
 
 
3597
        case PJSIP_TSX_STATE_TERMINATED:
 
3598
            /* INVITE transaction can be terminated either because UAC
 
3599
             * transaction received 2xx response or because of transport
 
3600
             * error.
 
3601
             */
 
3602
            if (tsx->status_code/100 == 2) {
 
3603
 
 
3604
                /* This must be receipt of 2xx response */
 
3605
 
 
3606
                /* Set state to CONNECTING */
 
3607
                inv_set_state(inv, PJSIP_INV_STATE_CONNECTING, e);
 
3608
 
 
3609
                if (e->body.tsx_state.type == PJSIP_EVENT_RX_MSG) {
 
3610
                    pj_status_t status;
 
3611
                    
 
3612
                    /* Process session timer response. */
 
3613
                    status = handle_timer_response(inv, 
 
3614
                                                   e->body.tsx_state.src.rdata,
 
3615
                                                   PJ_TRUE);
 
3616
                    if (status != PJ_SUCCESS)
 
3617
                        break;
 
3618
 
 
3619
                    inv_check_sdp_in_incoming_msg(inv, tsx, 
 
3620
                                                  e->body.tsx_state.src.rdata);
 
3621
                }
 
3622
 
 
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);
 
3626
 
 
3627
                    inv_send_ack(inv, e);
 
3628
                }
 
3629
 
 
3630
            } else  {
 
3631
                inv_set_cause(inv, tsx->status_code, &tsx->status_text);
 
3632
                inv_set_state(inv, PJSIP_INV_STATE_DISCONNECTED, e);
 
3633
            }
 
3634
            break;
 
3635
 
 
3636
        default:
 
3637
            pj_assert(!"Unexpected INVITE tsx state");
 
3638
        }
 
3639
 
 
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 )
 
3645
    {
 
3646
 
 
3647
        /*
 
3648
         * Handle incoming CANCEL request.
 
3649
         */
 
3650
 
 
3651
        inv_respond_incoming_cancel(inv, tsx, e->body.tsx_state.src.rdata);
 
3652
 
 
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)
 
3656
    {
 
3657
        /*
 
3658
         * Handle incoming UPDATE
 
3659
         */
 
3660
        inv_respond_incoming_update(inv, e->body.tsx_state.src.rdata);
 
3661
 
 
3662
 
 
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)
 
3667
    {
 
3668
        /*
 
3669
         * Handle response to outgoing UPDATE request.
 
3670
         */
 
3671
        inv_handle_update_response(inv, e);
 
3672
 
 
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)
 
3676
    {
 
3677
        /*
 
3678
         * Handle incoming PRACK
 
3679
         */
 
3680
        inv_respond_incoming_prack(inv, e->body.tsx_state.src.rdata);
 
3681
 
 
3682
    } else if (tsx->role == PJSIP_ROLE_UAC) {
 
3683
        
 
3684
        /* Generic handling for UAC tsx completion */
 
3685
        handle_uac_tsx_response(inv, e);
 
3686
 
 
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)
 
3691
    {
 
3692
        /* Received BYE before the 2xx/OK response to INVITE.
 
3693
         * Assume that the 2xx/OK response is lost and the BYE
 
3694
         * arrives earlier.
 
3695
         */
 
3696
        inv_respond_incoming_bye(inv, tsx, e->body.tsx_state.src.rdata, e);
 
3697
 
 
3698
        if (inv->invite_tsx->role == PJSIP_ROLE_UAC) {
 
3699
            /* Set timer just in case we will never get the final response
 
3700
             * for INVITE.
 
3701
             */
 
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;
 
3705
            pjsip_msg *msg;
 
3706
 
 
3707
            /* For UAS, send a final response. */
 
3708
            tdata = inv->invite_tsx->last_tx;
 
3709
            PJ_ASSERT_ON_FAIL(tdata != NULL, return);
 
3710
 
 
3711
            msg = tdata->msg;
 
3712
            msg->line.status.code = PJSIP_SC_REQUEST_TERMINATED;
 
3713
            msg->line.status.reason =
 
3714
                    *pjsip_get_status_text(PJSIP_SC_REQUEST_TERMINATED);
 
3715
            msg->body = NULL;
 
3716
 
 
3717
            pjsip_tx_data_invalidate_msg(tdata);
 
3718
            pjsip_tx_data_add_ref(tdata);
 
3719
 
 
3720
            pjsip_dlg_send_response(inv->dlg, inv->invite_tsx, tdata);
 
3721
        }
 
3722
    }
 
3723
}
 
3724
 
 
3725
/*
 
3726
 * State CONNECTING is after 2xx response to INVITE is sent/received.
 
3727
 */
 
3728
static void inv_on_state_connecting( pjsip_inv_session *inv, pjsip_event *e)
 
3729
{
 
3730
    pjsip_transaction *tsx = e->body.tsx_state.tsx;
 
3731
    pjsip_dialog *dlg = pjsip_tsx_get_dlg(tsx);
 
3732
 
 
3733
    PJ_ASSERT_ON_FAIL(tsx && dlg, return);
 
3734
 
 
3735
    if (tsx == inv->invite_tsx) {
 
3736
 
 
3737
        /*
 
3738
         * Handle INVITE state progression.
 
3739
         */
 
3740
        switch (tsx->state) {
 
3741
 
 
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.
 
3745
             */
 
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);
 
3750
                }
 
3751
 
 
3752
                inv_set_state(inv, PJSIP_INV_STATE_CONFIRMED, e);
 
3753
            }
 
3754
            break;
 
3755
 
 
3756
        case PJSIP_TSX_STATE_TERMINATED:
 
3757
            /* INVITE transaction can be terminated either because UAC
 
3758
             * transaction received 2xx response or because of transport
 
3759
             * error.
 
3760
             */
 
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);
 
3765
                } else {
 
3766
                    pjsip_tx_data *bye;
 
3767
                    pj_status_t status;
 
3768
 
 
3769
                    /* Send BYE */
 
3770
                    status = pjsip_dlg_create_request(inv->dlg,
 
3771
                                                      pjsip_get_bye_method(),
 
3772
                                                      -1, &bye);
 
3773
                    if (status == PJ_SUCCESS) {
 
3774
                        pjsip_inv_send_msg(inv, bye);
 
3775
                    }
 
3776
                }
 
3777
            }
 
3778
            break;
 
3779
 
 
3780
        case PJSIP_TSX_STATE_DESTROYED:
 
3781
            /* Do nothing. */
 
3782
            break;
 
3783
 
 
3784
        default:
 
3785
            pj_assert(!"Unexpected state");
 
3786
        }
 
3787
 
 
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) 
 
3792
    {
 
3793
 
 
3794
        /*
 
3795
         * Handle incoming BYE.
 
3796
         */
 
3797
 
 
3798
        inv_respond_incoming_bye( inv, tsx, e->body.tsx_state.src.rdata, e );
 
3799
 
 
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))
 
3804
    {
 
3805
 
 
3806
        /*
 
3807
         * Outgoing BYE
 
3808
         */
 
3809
        inv_handle_bye_response( inv, tsx, e->body.tsx_state.src.rdata, e);
 
3810
 
 
3811
    }
 
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) 
 
3816
    {
 
3817
 
 
3818
        /*
 
3819
         * Handle strandled incoming CANCEL.
 
3820
         */
 
3821
        pjsip_rx_data *rdata = e->body.tsx_state.src.rdata;
 
3822
        pjsip_tx_data *tdata;
 
3823
        pj_status_t status;
 
3824
 
 
3825
        status = pjsip_dlg_create_response(dlg, rdata, 200, NULL, &tdata);
 
3826
        if (status != PJ_SUCCESS) return;
 
3827
 
 
3828
        status = pjsip_dlg_send_response(dlg, tsx, tdata);
 
3829
        if (status != PJ_SUCCESS) return;
 
3830
 
 
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)
 
3834
    {
 
3835
        /*
 
3836
         * Handle incoming UPDATE
 
3837
         */
 
3838
        inv_respond_incoming_update(inv, e->body.tsx_state.src.rdata);
 
3839
 
 
3840
 
 
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)
 
3845
    {
 
3846
        /*
 
3847
         * Handle response to outgoing UPDATE request.
 
3848
         */
 
3849
        if (handle_uac_tsx_response(inv, e) == PJ_FALSE)
 
3850
            inv_handle_update_response(inv, e);
 
3851
 
 
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)
 
3855
    {
 
3856
        /*
 
3857
         * Handle incoming PRACK
 
3858
         */
 
3859
        inv_respond_incoming_prack(inv, e->body.tsx_state.src.rdata);
 
3860
 
 
3861
    } else if (tsx->role == PJSIP_ROLE_UAC) {
 
3862
        
 
3863
        /* Generic handling for UAC tsx completion */
 
3864
        handle_uac_tsx_response(inv, e);
 
3865
    }
 
3866
 
 
3867
}
 
3868
 
 
3869
/*
 
3870
 * State CONFIRMED is after ACK is sent/received.
 
3871
 */
 
3872
static void inv_on_state_confirmed( pjsip_inv_session *inv, pjsip_event *e)
 
3873
{
 
3874
    pjsip_transaction *tsx = e->body.tsx_state.tsx;
 
3875
    pjsip_dialog *dlg = pjsip_tsx_get_dlg(tsx);
 
3876
 
 
3877
    PJ_ASSERT_ON_FAIL(tsx && dlg, return);
 
3878
 
 
3879
 
 
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))
 
3884
    {
 
3885
 
 
3886
        /*
 
3887
         * Outgoing BYE
 
3888
         */
 
3889
 
 
3890
        inv_handle_bye_response( inv, tsx, e->body.tsx_state.src.rdata, e);
 
3891
 
 
3892
    }
 
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) 
 
3897
    {
 
3898
 
 
3899
        /*
 
3900
         * Handle incoming BYE.
 
3901
         */
 
3902
 
 
3903
        inv_respond_incoming_bye( inv, tsx, e->body.tsx_state.src.rdata, e );
 
3904
 
 
3905
    }
 
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) 
 
3910
    {
 
3911
 
 
3912
        /*
 
3913
         * Handle strandled incoming CANCEL.
 
3914
         */
 
3915
        pjsip_rx_data *rdata = e->body.tsx_state.src.rdata;
 
3916
        pjsip_tx_data *tdata;
 
3917
        pj_status_t status;
 
3918
 
 
3919
        status = pjsip_dlg_create_response(dlg, rdata, 200, NULL, &tdata);
 
3920
        if (status != PJ_SUCCESS) return;
 
3921
 
 
3922
        status = pjsip_dlg_send_response(dlg, tsx, tdata);
 
3923
        if (status != PJ_SUCCESS) return;
 
3924
 
 
3925
    }
 
3926
    else if (tsx->method.id == PJSIP_INVITE_METHOD &&
 
3927
             tsx->role == PJSIP_ROLE_UAS)
 
3928
    {
 
3929
 
 
3930
        /*
 
3931
         * Handle incoming re-INVITE
 
3932
         */
 
3933
        if (tsx->state == PJSIP_TSX_STATE_TRYING) {
 
3934
            
 
3935
            pjsip_rx_data *rdata = e->body.tsx_state.src.rdata;
 
3936
            pjsip_tx_data *tdata;
 
3937
            pj_status_t status;
 
3938
            pjsip_rdata_sdp_info *sdp_info;
 
3939
            pjsip_status_code st_code;
 
3940
 
 
3941
            /* Check if we have INVITE pending. */
 
3942
            if (inv->invite_tsx && inv->invite_tsx!=tsx) {
 
3943
                int code;
 
3944
                pj_str_t reason;
 
3945
 
 
3946
                reason = pj_str("Another INVITE transaction in progress");
 
3947
 
 
3948
                if (inv->invite_tsx->role == PJSIP_ROLE_UAC)
 
3949
                    code = 491;
 
3950
                else
 
3951
                    code = 500;
 
3952
 
 
3953
                /* Can not receive re-INVITE while another one is pending. */
 
3954
                status = pjsip_dlg_create_response( inv->dlg, rdata, code,
 
3955
                                                    &reason, &tdata);
 
3956
                if (status != PJ_SUCCESS)
 
3957
                    return;
 
3958
 
 
3959
                if (code == 500) {
 
3960
                    /* MUST include Retry-After header with random value
 
3961
                     * between 0-10.
 
3962
                     */
 
3963
                    pjsip_retry_after_hdr *ra_hdr;
 
3964
                    int val = (pj_rand() % 10);
 
3965
 
 
3966
                    ra_hdr = pjsip_retry_after_hdr_create(tdata->pool, val);
 
3967
                    pjsip_msg_add_hdr(tdata->msg, (pjsip_hdr*)ra_hdr);
 
3968
                }
 
3969
 
 
3970
                status = pjsip_dlg_send_response( inv->dlg, tsx, tdata);
 
3971
                
 
3972
 
 
3973
                return;
 
3974
            }
 
3975
 
 
3976
            /* Save the invite transaction. */
 
3977
            inv->invite_tsx = tsx;
 
3978
 
 
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,
 
3983
                                                   NULL, &tdata);
 
3984
                if (status != PJ_SUCCESS)
 
3985
                    return;
 
3986
 
 
3987
                pjsip_timer_update_resp(inv, tdata);
 
3988
                status = pjsip_dlg_send_response(dlg, tsx, tdata);
 
3989
                return;
 
3990
            }
 
3991
 
 
3992
            /* Send 491 if we receive re-INVITE while another offer/answer
 
3993
             * negotiation is in progress
 
3994
             */
 
3995
            if (pjmedia_sdp_neg_get_state(inv->neg) !=
 
3996
                    PJMEDIA_SDP_NEG_STATE_DONE)
 
3997
            {
 
3998
                status = pjsip_dlg_create_response(inv->dlg, rdata,
 
3999
                                                   PJSIP_SC_REQUEST_PENDING,
 
4000
                                                   NULL, &tdata);
 
4001
                if (status != PJ_SUCCESS)
 
4002
                    return;
 
4003
                pjsip_timer_update_resp(inv, tdata);
 
4004
                status = pjsip_dlg_send_response(dlg, tsx, tdata);
 
4005
                return;
 
4006
            }
 
4007
 
 
4008
            /* Process SDP in incoming message. */
 
4009
            status = inv_check_sdp_in_incoming_msg(inv, tsx, rdata);
 
4010
 
 
4011
            if (status != PJ_SUCCESS) {
 
4012
 
 
4013
                /* Not Acceptable */
 
4014
                const pjsip_hdr *accept;
 
4015
 
 
4016
                status = pjsip_dlg_create_response(inv->dlg, rdata, 
 
4017
                                                   488, NULL, &tdata);
 
4018
                if (status != PJ_SUCCESS)
 
4019
                    return;
 
4020
 
 
4021
 
 
4022
                accept = pjsip_endpt_get_capability(dlg->endpt, PJSIP_H_ACCEPT,
 
4023
                                                    NULL);
 
4024
                if (accept) {
 
4025
                    pjsip_msg_add_hdr(tdata->msg, (pjsip_hdr*)
 
4026
                                      pjsip_hdr_clone(tdata->pool, accept));
 
4027
                }
 
4028
 
 
4029
                status = pjsip_dlg_send_response(dlg, tsx, tdata);
 
4030
 
 
4031
                return;
 
4032
            }
 
4033
 
 
4034
            /* Create 2xx ANSWER */
 
4035
            status = pjsip_dlg_create_response(dlg, rdata, 200, NULL, &tdata);
 
4036
            if (status != PJ_SUCCESS)
 
4037
                return;
 
4038
 
 
4039
            /* If the INVITE request has SDP body, send answer.
 
4040
             * Otherwise generate offer from local active SDP.
 
4041
             */
 
4042
            sdp_info = pjsip_rdata_get_sdp_info(rdata);
 
4043
            if (sdp_info->sdp != NULL) {
 
4044
                status = process_answer(inv, 200, tdata, NULL);
 
4045
            } else {
 
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.
 
4050
                 */
 
4051
                pjmedia_sdp_session *sdp = NULL;
 
4052
 
 
4053
                if (mod_inv.cb.on_create_offer)  {
 
4054
                    (*mod_inv.cb.on_create_offer)(inv, &sdp);
 
4055
                    if (sdp) {
 
4056
                        /* Notify negotiator about the new offer. This will
 
4057
                         * fix the offer with correct SDP origin.
 
4058
                         */
 
4059
                        status = 
 
4060
                            pjmedia_sdp_neg_modify_local_offer(inv->pool_prov,
 
4061
                                                               inv->neg,
 
4062
                                                               sdp);
 
4063
 
 
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;
 
4069
                        }
 
4070
                    }
 
4071
                } 
 
4072
                
 
4073
                if (sdp == NULL) {
 
4074
                    const pjmedia_sdp_session *active_sdp = NULL;
 
4075
                    status = pjmedia_sdp_neg_send_local_offer(inv->pool_prov,
 
4076
                                                              inv->neg, 
 
4077
                                                              &active_sdp);
 
4078
                    if (status == PJ_SUCCESS)
 
4079
                        sdp = (pjmedia_sdp_session*) active_sdp;
 
4080
                }
 
4081
 
 
4082
                if (sdp) {
 
4083
                    tdata->msg->body = create_sdp_body(tdata->pool, sdp);
 
4084
                }
 
4085
            }
 
4086
 
 
4087
            if (status != PJ_SUCCESS) {
 
4088
                /*
 
4089
                 * SDP negotiation has failed.
 
4090
                 */
 
4091
                pj_status_t rc;
 
4092
                pj_str_t reason;
 
4093
 
 
4094
                /* Delete the 2xx answer */
 
4095
                pjsip_tx_data_dec_ref(tdata);
 
4096
                
 
4097
                /* Create 500 response */
 
4098
                reason = pj_str("SDP negotiation failed");
 
4099
                rc = pjsip_dlg_create_response(dlg, rdata, 500, &reason, 
 
4100
                                               &tdata);
 
4101
                if (rc == PJ_SUCCESS) {
 
4102
                    pjsip_warning_hdr *w;
 
4103
                    const pj_str_t *endpt_name;
 
4104
 
 
4105
                    endpt_name = pjsip_endpt_name(dlg->endpt);
 
4106
                    w = pjsip_warning_hdr_create_from_status(tdata->pool, 
 
4107
                                                             endpt_name,
 
4108
                                                             status);
 
4109
                    if (w)
 
4110
                        pjsip_msg_add_hdr(tdata->msg, (pjsip_hdr*)w);
 
4111
 
 
4112
                    pjsip_inv_send_msg(inv, tdata);
 
4113
                }
 
4114
                return;
 
4115
            }
 
4116
 
 
4117
            /* Invoke Session Timers */
 
4118
            pjsip_timer_update_resp(inv, tdata);
 
4119
 
 
4120
            /* Send 2xx regardless of the status of negotiation */
 
4121
            status = pjsip_inv_send_msg(inv, tdata);
 
4122
 
 
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.
 
4126
             */
 
4127
            if (tsx->status_code/100 == 2 &&
 
4128
                e->body.tsx_state.type == PJSIP_EVENT_RX_MSG) 
 
4129
            {
 
4130
                inv_check_sdp_in_incoming_msg(inv, tsx,
 
4131
                                              e->body.tsx_state.src.rdata);
 
4132
 
 
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)
 
4136
                {
 
4137
                    pjmedia_sdp_neg_cancel_offer(inv->neg);
 
4138
                }
 
4139
            }
 
4140
 
 
4141
        }
 
4142
 
 
4143
    }
 
4144
    else if (tsx->method.id == PJSIP_INVITE_METHOD &&
 
4145
             tsx->role == PJSIP_ROLE_UAC)
 
4146
    {
 
4147
 
 
4148
        /*
 
4149
         * Handle outgoing re-INVITE
 
4150
         */
 
4151
        if (tsx->state == PJSIP_TSX_STATE_CALLING) {
 
4152
 
 
4153
            /* Must not have other pending INVITE transaction */
 
4154
            pj_assert(inv->invite_tsx==NULL || tsx==inv->invite_tsx);
 
4155
 
 
4156
            /* Save pending invite transaction */
 
4157
            inv->invite_tsx = tsx;
 
4158
 
 
4159
        } else if (tsx->state == PJSIP_TSX_STATE_TERMINATED &&
 
4160
                   tsx->status_code/100 == 2) 
 
4161
        {
 
4162
            pj_status_t status;
 
4163
 
 
4164
            /* Re-INVITE was accepted. */
 
4165
 
 
4166
            /* Process session timer response. */
 
4167
            status = handle_timer_response(inv, 
 
4168
                                           e->body.tsx_state.src.rdata,
 
4169
                                           PJ_TRUE);
 
4170
            if (status != PJ_SUCCESS)
 
4171
                return;
 
4172
 
 
4173
            /* Process SDP */
 
4174
            inv_check_sdp_in_incoming_msg(inv, tsx, 
 
4175
                                          e->body.tsx_state.src.rdata);
 
4176
 
 
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)
 
4180
            {
 
4181
                pjmedia_sdp_neg_cancel_offer(inv->neg);
 
4182
            }
 
4183
 
 
4184
            /* Send ACK */
 
4185
            inv_send_ack(inv, e);
 
4186
 
 
4187
        } else if (handle_uac_tsx_response(inv, e)) {
 
4188
 
 
4189
            /* Handle response that terminates dialog */
 
4190
            /* Nothing to do (already handled) */
 
4191
 
 
4192
        } else if (tsx->status_code >= 300 && tsx->status_code < 700) {
 
4193
 
 
4194
            pjmedia_sdp_neg_state neg_state;
 
4195
 
 
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);
 
4200
            }
 
4201
 
 
4202
            if (tsx == inv->invite_tsx)
 
4203
                inv->invite_tsx = NULL;
 
4204
        }
 
4205
 
 
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)
 
4209
    {
 
4210
        /*
 
4211
         * Handle incoming UPDATE
 
4212
         */
 
4213
        inv_respond_incoming_update(inv, e->body.tsx_state.src.rdata);
 
4214
 
 
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)
 
4219
    {
 
4220
        /*
 
4221
         * Handle response to outgoing UPDATE request.
 
4222
         */
 
4223
        if (handle_uac_tsx_response(inv, e) == PJ_FALSE)
 
4224
            inv_handle_update_response(inv, e);
 
4225
 
 
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)
 
4229
    {
 
4230
        /*
 
4231
         * Handle strandled incoming PRACK
 
4232
         */
 
4233
        inv_respond_incoming_prack(inv, e->body.tsx_state.src.rdata);
 
4234
 
 
4235
    } else if (tsx->role == PJSIP_ROLE_UAC) {
 
4236
        /*
 
4237
         * Handle 401/407/408/481/422 response
 
4238
         */
 
4239
        handle_uac_tsx_response(inv, e);
 
4240
    }
 
4241
 
 
4242
}
 
4243
 
 
4244
/*
 
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).
 
4248
 */
 
4249
static void inv_on_state_disconnected( pjsip_inv_session *inv, pjsip_event *e)
 
4250
{
 
4251
    pjsip_transaction *tsx = e->body.tsx_state.tsx;
 
4252
    pjsip_dialog *dlg = pjsip_tsx_get_dlg(tsx);
 
4253
 
 
4254
    PJ_ASSERT_ON_FAIL(tsx && dlg, return);
 
4255
 
 
4256
    if (tsx->role == PJSIP_ROLE_UAS &&
 
4257
        tsx->status_code < 200 &&
 
4258
        e->body.tsx_state.type == PJSIP_EVENT_RX_MSG) 
 
4259
    {
 
4260
        pjsip_rx_data *rdata = e->body.tsx_state.src.rdata;
 
4261
 
 
4262
        /*
 
4263
         * Respond BYE with 200/OK
 
4264
         */
 
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) {
 
4268
            /*
 
4269
             * Respond CANCEL with 200/OK too.
 
4270
             */
 
4271
            pjsip_tx_data *tdata;
 
4272
            pj_status_t status;
 
4273
 
 
4274
            status = pjsip_dlg_create_response(dlg, rdata, 200, NULL, &tdata);
 
4275
            if (status != PJ_SUCCESS) return;
 
4276
 
 
4277
            status = pjsip_dlg_send_response(dlg, tsx, tdata);
 
4278
            if (status != PJ_SUCCESS) return;
 
4279
 
 
4280
        }
 
4281
 
 
4282
    } else if (tsx->role == PJSIP_ROLE_UAC) {
 
4283
        /*
 
4284
         * Handle 401/407/408/481/422 response
 
4285
         */
 
4286
        handle_uac_tsx_response(inv, e);
 
4287
    }
 
4288
}
 
4289