diff options
author | Benny Prijono <bennylp@teluu.com> | 2008-11-27 00:06:46 +0000 |
---|---|---|
committer | Benny Prijono <bennylp@teluu.com> | 2008-11-27 00:06:46 +0000 |
commit | c40accaad90c1ec16cf734e478d13247812c0e0c (patch) | |
tree | 49326bcc74cf57217fee661f888332279725d09e /pjsip/src | |
parent | 9b44f51f9e162f47a951d3e933e0b0e462db576b (diff) |
Ticket #10: handle redirection response in the invite session
git-svn-id: http://svn.pjsip.org/repos/pjproject/trunk@2370 74dad513-b988-da41-8d7b-12977e46ad98
Diffstat (limited to 'pjsip/src')
-rw-r--r-- | pjsip/src/pjsip-ua/sip_inv.c | 387 | ||||
-rw-r--r-- | pjsip/src/pjsip/sip_dialog.c | 4 | ||||
-rw-r--r-- | pjsip/src/pjsip/sip_util.c | 133 | ||||
-rw-r--r-- | pjsip/src/pjsua-lib/pjsua_call.c | 70 |
4 files changed, 552 insertions, 42 deletions
diff --git a/pjsip/src/pjsip-ua/sip_inv.c b/pjsip/src/pjsip-ua/sip_inv.c index 0ec82cef..46872f06 100644 --- a/pjsip/src/pjsip-ua/sip_inv.c +++ b/pjsip/src/pjsip-ua/sip_inv.c @@ -225,6 +225,10 @@ void inv_set_state(pjsip_inv_session *inv, pjsip_inv_state state, pjsip_tx_data_dec_ref(inv->last_ack); inv->last_ack = NULL; } + if (inv->invite_req) { + pjsip_tx_data_dec_ref(inv->invite_req); + inv->invite_req = NULL; + } pjsip_100rel_end_session(inv); pjsip_dlg_dec_session(inv->dlg, &mod_inv.mod); } @@ -1219,6 +1223,35 @@ PJ_DEF(pj_status_t) pjsip_inv_terminate( pjsip_inv_session *inv, } +/* + * Restart UAC session, possibly because app or us wants to re-send the + * INVITE request due to 401/407 challenge or 3xx response. + */ +PJ_DEF(pj_status_t) pjsip_inv_uac_restart(pjsip_inv_session *inv, + pj_bool_t new_offer) +{ + PJ_ASSERT_RETURN(inv, PJ_EINVAL); + + inv->state = PJSIP_INV_STATE_NULL; + inv->invite_tsx = NULL; + if (inv->last_answer) { + pjsip_tx_data_dec_ref(inv->last_answer); + inv->last_answer = NULL; + } + + if (new_offer && inv->neg) { + pjmedia_sdp_neg_state neg_state; + + neg_state = pjmedia_sdp_neg_get_state(inv->neg); + if (neg_state == PJMEDIA_SDP_NEG_STATE_LOCAL_OFFER) { + pjmedia_sdp_neg_cancel_offer(inv->neg); + } + } + + return PJ_SUCCESS; +} + + static void *clone_sdp(pj_pool_t *pool, const void *data, unsigned len) { PJ_UNUSED_ARG(len); @@ -1891,6 +1924,203 @@ PJ_DEF(pj_status_t) pjsip_inv_end_session( pjsip_inv_session *inv, return PJ_SUCCESS; } +/* Following redirection recursion, get next target from the target set and + * notify user. + * + * Returns PJ_FALSE if recursion fails (either because there's no more target + * or user rejects the recursion). If we return PJ_FALSE, caller should + * disconnect the session. + * + * Note: + * the event 'e' argument may be NULL. + */ +static pj_bool_t inv_uac_recurse(pjsip_inv_session *inv, int code, + const pj_str_t *reason, pjsip_event *e) +{ + pjsip_redirect_op op = PJSIP_REDIRECT_ACCEPT; + pjsip_target *target; + + /* Won't redirect if the callback is not implemented. */ + if (mod_inv.cb.on_redirected == NULL) + return PJ_FALSE; + + if (reason == NULL) + reason = pjsip_get_status_text(code); + + /* Set status of current target */ + pjsip_target_assign_status(inv->dlg->target_set.current, inv->dlg->pool, + code, reason); + + /* Fetch next target from the target set. We only want to + * process SIP/SIPS URI for now. + */ + for (;;) { + target = pjsip_target_set_get_next(&inv->dlg->target_set); + if (target == NULL) { + /* No more target. */ + return PJ_FALSE; + } + + if (!PJSIP_URI_SCHEME_IS_SIP(target->uri) && + !PJSIP_URI_SCHEME_IS_SIPS(target->uri)) + { + code = PJSIP_SC_UNSUPPORTED_URI_SCHEME; + reason = pjsip_get_status_text(code); + + /* Mark this target as unusable and fetch next target. */ + pjsip_target_assign_status(target, inv->dlg->pool, code, reason); + } else { + /* Found a target */ + break; + } + } + + /* We have target in 'target'. Set this target as current target + * and notify callback. + */ + pjsip_target_set_set_current(&inv->dlg->target_set, target); + + (*mod_inv.cb.on_redirected)(inv, target->uri, &op, e); + + + /* Check what the application wants to do now */ + switch (op) { + case PJSIP_REDIRECT_ACCEPT: + case PJSIP_REDIRECT_STOP: + /* Must increment session counter, that's the convention of the + * pjsip_inv_process_redirect(). + */ + pjsip_dlg_inc_session(inv->dlg, &mod_inv.mod); + + /* Act on the recursion */ + pjsip_inv_process_redirect(inv, op, e); + return PJ_TRUE; + + case PJSIP_REDIRECT_PENDING: + /* Increment session so that the dialog/session is not destroyed + * while we're waiting for user confirmation. + */ + pjsip_dlg_inc_session(inv->dlg, &mod_inv.mod); + + /* Also clear the invite_tsx variable, otherwise when this tsx is + * terminated, it will also terminate the session. + */ + inv->invite_tsx = NULL; + + /* Done. The processing will continue once the application calls + * pjsip_inv_process_redirect(). + */ + return PJ_TRUE; + + case PJSIP_REDIRECT_REJECT: + /* Recursively call this function again to fetch next target, if any. + */ + return inv_uac_recurse(inv, PJSIP_SC_REQUEST_TERMINATED, NULL, e); + + } + + pj_assert(!"Should not reach here"); + return PJ_FALSE; +} + + +/* Process redirection/recursion */ +PJ_DEF(pj_status_t) pjsip_inv_process_redirect( pjsip_inv_session *inv, + pjsip_redirect_op op, + pjsip_event *e) +{ + const pjsip_status_code cancel_code = PJSIP_SC_REQUEST_TERMINATED; + pjsip_event usr_event; + pj_status_t status = PJ_SUCCESS; + + PJ_ASSERT_RETURN(inv && op != PJSIP_REDIRECT_PENDING, PJ_EINVAL); + + if (e == NULL) { + PJSIP_EVENT_INIT_USER(usr_event, NULL, NULL, NULL, NULL); + e = &usr_event; + } + + pjsip_dlg_inc_lock(inv->dlg); + + /* Decrement session. That's the convention here to prevent the dialog + * or session from being destroyed while we're waiting for user + * confirmation. + */ + pjsip_dlg_dec_session(inv->dlg, &mod_inv.mod); + + /* See what the application wants to do now */ + switch (op) { + case PJSIP_REDIRECT_ACCEPT: + /* User accept the redirection. Reset the session and resend the + * INVITE request. + */ + { + pjsip_tx_data *tdata; + pjsip_via_hdr *via; + + /* Get the original INVITE request. */ + tdata = inv->invite_req; + pjsip_tx_data_add_ref(tdata); + + /* Restore strict route set. + * See http://trac.pjsip.org/repos/ticket/492 + */ + pjsip_restore_strict_route_set(tdata); + + /* Set target */ + tdata->msg->line.req.uri = + pjsip_uri_clone(tdata->pool, inv->dlg->target_set.current->uri); + + /* Remove branch param in Via header. */ + via = (pjsip_via_hdr*) + pjsip_msg_find_hdr(tdata->msg, PJSIP_H_VIA, NULL); + via->branch_param.slen = 0; + + /* Must invalidate the message! */ + pjsip_tx_data_invalidate_msg(tdata); + + /* Reset the session */ + pjsip_inv_uac_restart(inv, PJ_FALSE); + + /* (re)Send the INVITE request */ + status = pjsip_inv_send_msg(inv, tdata); + } + break; + + case PJSIP_REDIRECT_STOP: + /* User doesn't want the redirection. Disconnect the session now. */ + inv_set_cause(inv, cancel_code, pjsip_get_status_text(cancel_code)); + inv_set_state(inv, PJSIP_INV_STATE_DISCONNECTED, e); + + /* Caller should expect that the invite session is gone now, so + * we don't need to set status to PJSIP_ESESSIONTERMINATED here. + */ + break; + + case PJSIP_REDIRECT_REJECT: + /* Current target is rejected. Fetch next target if any. */ + if (inv_uac_recurse(inv, cancel_code, NULL, NULL) == PJ_FALSE) { + inv_set_cause(inv, cancel_code, + pjsip_get_status_text(cancel_code)); + inv_set_state(inv, PJSIP_INV_STATE_DISCONNECTED, e); + + /* Tell caller that the invite session is gone now */ + status = PJSIP_ESESSIONTERMINATED; + } + break; + + + case PJSIP_REDIRECT_PENDING: + pj_assert(!"Should not happen"); + break; + } + + + pjsip_dlg_dec_lock(inv->dlg); + + return status; +} + /* * Create re-INVITE. @@ -2552,6 +2782,19 @@ static void inv_on_state_null( pjsip_inv_session *inv, pjsip_event *e) if (dlg->role == PJSIP_ROLE_UAC) { + /* Save the original INVITE request, if on_redirected() callback + * is implemented. We may need to resend the INVITE if we receive + * redirection response. + */ + if (mod_inv.cb.on_redirected) { + if (inv->invite_req) { + pjsip_tx_data_dec_ref(inv->invite_req); + inv->invite_req = NULL; + } + inv->invite_req = tsx->last_tx; + pjsip_tx_data_add_ref(inv->invite_req); + } + switch (tsx->state) { case PJSIP_TSX_STATE_CALLING: inv_set_state(inv, PJSIP_INV_STATE_CALLING, e); @@ -2673,6 +2916,105 @@ static pj_bool_t handle_uac_tsx_response(pjsip_inv_session *inv, } +/* Handle call rejection, especially with regard to processing call + * redirection. We need to handle the following scenarios: + * - 3xx response is received -- see if on_redirected() callback is + * implemented. If so, add the Contact URIs in the response to the + * target set and notify user. + * - 4xx - 6xx resposne is received -- see if we're currently recursing, + * if so fetch the next target if any and notify the on_redirected() + * callback. + * - for other cases -- disconnect the session. + */ +static void handle_uac_call_rejection(pjsip_inv_session *inv, pjsip_event *e) +{ + pjsip_transaction *tsx = e->body.tsx_state.tsx; + pj_status_t status; + + if (PJSIP_IS_STATUS_IN_CLASS(tsx->status_code, 300)) { + + if (mod_inv.cb.on_redirected == NULL) { + + /* Redirection callback is not implemented, disconnect the + * call. + */ + goto terminate_session; + + } else { + const pjsip_msg *res_msg; + + res_msg = e->body.tsx_state.src.rdata->msg_info.msg; + + /* Gather all Contact URI's in the response and add them + * to target set. The function will take care of removing + * duplicate URI's. + */ + pjsip_target_set_add_from_msg(&inv->dlg->target_set, + inv->dlg->pool, res_msg); + + /* Recurse to alternate targets if application allows us */ + if (!inv_uac_recurse(inv, tsx->status_code, &tsx->status_text, e)) + { + /* Recursion fails, terminate session now */ + goto terminate_session; + } + + /* Done */ + } + + } else if ((tsx->status_code==401 || tsx->status_code==407) && + !inv->cancelling) + { + + /* Handle authentication failure: + * Resend the request with Authorization header. + */ + pjsip_tx_data *tdata; + + status = pjsip_auth_clt_reinit_req(&inv->dlg->auth_sess, + e->body.tsx_state.src.rdata, + tsx->last_tx, + &tdata); + + if (status != PJ_SUCCESS) { + + /* Does not have proper credentials. If we are currently + * recursing, try the next target. Otherwise end the session. + */ + if (!inv_uac_recurse(inv, tsx->status_code, &tsx->status_text, e)) + { + /* Recursion fails, terminate session now */ + goto terminate_session; + } + + } else { + + /* Restart session. */ + pjsip_inv_uac_restart(inv, PJ_FALSE); + + /* Send the request. */ + status = pjsip_inv_send_msg(inv, tdata); + } + + } else if (PJSIP_IS_STATUS_IN_CLASS(tsx->status_code, 600)) { + /* Global error */ + goto terminate_session; + + } else { + /* See if we have alternate target to try */ + if (!inv_uac_recurse(inv, tsx->status_code, &tsx->status_text, e)) { + /* Recursion fails, terminate session now */ + goto terminate_session; + } + } + return; + +terminate_session: + inv_set_cause(inv, tsx->status_code, &tsx->status_text); + inv_set_state(inv, PJSIP_INV_STATE_DISCONNECTED, e); +} + + /* * State CALLING is after sending initial INVITE request but before * any response (with tag) is received. @@ -2735,47 +3077,8 @@ static void inv_on_state_calling( pjsip_inv_session *inv, pjsip_event *e) inv_check_sdp_in_incoming_msg(inv, tsx, e->body.tsx_state.src.rdata); - } else if ((tsx->status_code==401 || tsx->status_code==407) && - !inv->cancelling) - { - - /* Handle authentication failure: - * Resend the request with Authorization header. - */ - pjsip_tx_data *tdata; - - status = pjsip_auth_clt_reinit_req(&inv->dlg->auth_sess, - e->body.tsx_state.src.rdata, - tsx->last_tx, - &tdata); - - if (status != PJ_SUCCESS) { - - /* Does not have proper credentials. - * End the session. - */ - inv_set_cause(inv, tsx->status_code, &tsx->status_text); - inv_set_state(inv, PJSIP_INV_STATE_DISCONNECTED, e); - - } else { - - /* Restart session. */ - inv->state = PJSIP_INV_STATE_NULL; - inv->invite_tsx = NULL; - if (inv->last_answer) { - pjsip_tx_data_dec_ref(inv->last_answer); - inv->last_answer = NULL; - } - - /* Send the request. */ - status = pjsip_inv_send_msg(inv, tdata); - } - } else { - - inv_set_cause(inv, tsx->status_code, &tsx->status_text); - inv_set_state(inv, PJSIP_INV_STATE_DISCONNECTED, e); - + handle_uac_call_rejection(inv, e); } break; @@ -2942,6 +3245,10 @@ static void inv_on_state_early( pjsip_inv_session *inv, pjsip_event *e) e->body.tsx_state.src.rdata); } + } else if (tsx->role == PJSIP_ROLE_UAC) { + + handle_uac_call_rejection(inv, e); + } else { inv_set_cause(inv, tsx->status_code, &tsx->status_text); inv_set_state(inv, PJSIP_INV_STATE_DISCONNECTED, e); diff --git a/pjsip/src/pjsip/sip_dialog.c b/pjsip/src/pjsip/sip_dialog.c index 4489cb8e..6c6b2b3a 100644 --- a/pjsip/src/pjsip/sip_dialog.c +++ b/pjsip/src/pjsip/sip_dialog.c @@ -91,6 +91,7 @@ static pj_status_t create_dialog( pjsip_user_agent *ua, if (status != PJ_SUCCESS) goto on_error; + pjsip_target_set_init(&dlg->target_set); *p_dlg = dlg; return PJ_SUCCESS; @@ -182,6 +183,9 @@ PJ_DEF(pj_status_t) pjsip_dlg_create_uac( pjsip_user_agent *ua, pj_list_init(&uri->header_param); } + /* Add target to the target set */ + pjsip_target_set_add_uri(&dlg->target_set, dlg->pool, dlg->target, 0); + /* Init local info. */ dlg->local.info = pjsip_from_hdr_create(dlg->pool); pj_strdup_with_null(dlg->pool, &dlg->local.info_str, local_uri); diff --git a/pjsip/src/pjsip/sip_util.c b/pjsip/src/pjsip/sip_util.c index f059d20e..3529313a 100644 --- a/pjsip/src/pjsip/sip_util.c +++ b/pjsip/src/pjsip/sip_util.c @@ -49,6 +49,139 @@ static const char *event_str[] = static pj_str_t str_TEXT = { "text", 4}, str_PLAIN = { "plain", 5 }; +/* Add URI to target-set */ +PJ_DEF(pj_status_t) pjsip_target_set_add_uri( pjsip_target_set *tset, + pj_pool_t *pool, + const pjsip_uri *uri, + int q1000) +{ + pjsip_target *t, *pos = NULL; + + PJ_ASSERT_RETURN(tset && pool && uri, PJ_EINVAL); + + /* Set q-value to 1 if it is not set */ + if (q1000 <= 0) + q1000 = 1000; + + /* Scan all the elements to see for duplicates, and at the same time + * get the position where the new element should be inserted to + * based on the q-value. + */ + t = tset->head.next; + while (t != &tset->head) { + if (pjsip_uri_cmp(PJSIP_URI_IN_REQ_URI, t->uri, uri)==PJ_SUCCESS) + return PJ_EEXISTS; + if (pos==NULL && t->q1000 < q1000) + pos = t; + t = t->next; + } + + /* Create new element */ + t = PJ_POOL_ZALLOC_T(pool, pjsip_target); + t->uri = pjsip_uri_clone(pool, uri); + t->q1000 = q1000; + + /* Insert */ + if (pos == NULL) + pj_list_push_back(&tset->head, t); + else + pj_list_insert_before(pos, t); + + /* Set current target if this is the first URI */ + if (tset->current == NULL) + tset->current = t; + + return PJ_SUCCESS; +} + +/* Add URI's in the Contact header in the message to target-set */ +PJ_DEF(pj_status_t) pjsip_target_set_add_from_msg( pjsip_target_set *tset, + pj_pool_t *pool, + const pjsip_msg *msg) +{ + const pjsip_hdr *hdr; + unsigned added = 0; + + PJ_ASSERT_RETURN(tset && pool && msg, PJ_EINVAL); + + /* Scan for Contact headers and add the URI */ + hdr = msg->hdr.next; + while (hdr != &msg->hdr) { + if (hdr->type == PJSIP_H_CONTACT) { + const pjsip_contact_hdr *cn_hdr = (const pjsip_contact_hdr*)hdr; + + if (!cn_hdr->star) { + pj_status_t rc; + rc = pjsip_target_set_add_uri(tset, pool, cn_hdr->uri, + cn_hdr->q1000); + if (rc == PJ_SUCCESS) + ++added; + } + } + hdr = hdr->next; + } + + return added ? PJ_SUCCESS : PJ_EEXISTS; +} + + +/* Get next target, if any */ +PJ_DEF(pjsip_target*) pjsip_target_set_get_next(const pjsip_target_set *tset) +{ + const pjsip_target *t, *next = NULL; + + t = tset->head.next; + while (t != &tset->head) { + if (PJSIP_IS_STATUS_IN_CLASS(t->code, 200)) { + /* No more target since one target has been successful */ + return NULL; + } + if (PJSIP_IS_STATUS_IN_CLASS(t->code, 600)) { + /* No more target since one target returned global error */ + return NULL; + } + if (t->code==0 && next==NULL) { + /* This would be the next target as long as we don't find + * targets with 2xx or 6xx status after this. + */ + next = t; + } + t = t->next; + } + + return (pjsip_target*)next; +} + + +/* Set current target */ +PJ_DEF(pj_status_t) pjsip_target_set_set_current( pjsip_target_set *tset, + pjsip_target *target) +{ + PJ_ASSERT_RETURN(tset && target, PJ_EINVAL); + PJ_ASSERT_RETURN(pj_list_find_node(tset, target) != NULL, PJ_ENOTFOUND); + + tset->current = target; + + return PJ_SUCCESS; +} + + +/* Assign status to a target */ +PJ_DEF(pj_status_t) pjsip_target_assign_status( pjsip_target *target, + pj_pool_t *pool, + int status_code, + const pj_str_t *reason) +{ + PJ_ASSERT_RETURN(target && pool && status_code && reason, PJ_EINVAL); + + target->code = status_code; + pj_strdup(pool, &target->reason, reason); + + return PJ_SUCCESS; +} + + + /* * Initialize transmit data (msg) with the headers and optional body. * This will just put the headers in the message as it is. Be carefull diff --git a/pjsip/src/pjsua-lib/pjsua_call.c b/pjsip/src/pjsua-lib/pjsua_call.c index 546be666..6831487d 100644 --- a/pjsip/src/pjsua-lib/pjsua_call.c +++ b/pjsip/src/pjsua-lib/pjsua_call.c @@ -65,6 +65,14 @@ static void pjsua_call_on_tsx_state_changed(pjsip_inv_session *inv, pjsip_transaction *tsx, pjsip_event *e); +/* + * Redirection handler. + */ +static void pjsua_call_on_redirected(pjsip_inv_session *inv, + const pjsip_uri *target, + pjsip_redirect_op *cmd, + const pjsip_event *e); + /* Create SDP for call hold. */ static pj_status_t create_sdp_of_call_hold(pjsua_call *call, @@ -156,7 +164,7 @@ pj_status_t pjsua_call_subsys_init(const pjsua_config *cfg) inv_cb.on_rx_offer = &pjsua_call_on_rx_offer; inv_cb.on_create_offer = &pjsua_call_on_create_offer; inv_cb.on_tsx_state_changed = &pjsua_call_on_tsx_state_changed; - + inv_cb.on_redirected = &pjsua_call_on_redirected; /* Initialize invite session module: */ status = pjsip_inv_usage_init(pjsua_var.endpt, &inv_cb); @@ -1434,6 +1442,32 @@ PJ_DEF(pj_status_t) pjsua_call_hangup(pjsua_call_id call_id, /* + * Accept or reject redirection. + */ +PJ_DEF(pj_status_t) pjsua_call_process_redirect( pjsua_call_id call_id, + pjsip_redirect_op cmd) +{ + pjsua_call *call; + pjsip_dialog *dlg; + pj_status_t status; + + PJ_ASSERT_RETURN(call_id>=0 && call_id<(int)pjsua_var.ua_cfg.max_calls, + PJ_EINVAL); + + status = acquire_call("pjsua_call_process_redirect()", call_id, + &call, &dlg); + if (status != PJ_SUCCESS) + return status; + + status = pjsip_inv_process_redirect(call->inv, cmd, NULL); + + pjsip_dlg_dec_lock(dlg); + + return status; +} + + +/* * Put the specified call on hold. */ PJ_DEF(pj_status_t) pjsua_call_set_hold(pjsua_call_id call_id, @@ -2827,12 +2861,19 @@ static void pjsua_call_on_state_changed(pjsip_inv_session *inv, pj_gettimeofday(&call->dis_time); if (call->res_time.sec == 0) pj_gettimeofday(&call->res_time); - if (e->body.tsx_state.tsx->status_code > call->last_code) { + if (e->type == PJSIP_EVENT_TSX_STATE && + e->body.tsx_state.tsx->status_code > call->last_code) + { call->last_code = (pjsip_status_code) e->body.tsx_state.tsx->status_code; pj_strncpy(&call->last_text, &e->body.tsx_state.tsx->status_text, sizeof(call->last_text_buf_)); + } else { + call->last_code = PJSIP_SC_REQUEST_TERMINATED; + pj_strncpy(&call->last_text, + pjsip_get_status_text(call->last_code), + sizeof(call->last_text_buf_)); } break; default: @@ -3770,3 +3811,28 @@ static void pjsua_call_on_tsx_state_changed(pjsip_inv_session *inv, PJSUA_UNLOCK(); } + + +/* Redirection handler */ +static void pjsua_call_on_redirected(pjsip_inv_session *inv, + const pjsip_uri *target, + pjsip_redirect_op *cmd, + const pjsip_event *e) +{ + pjsua_call *call = (pjsua_call*) inv->dlg->mod_data[pjsua_var.mod.id]; + + PJSUA_LOCK(); + + if (pjsua_var.ua_cfg.cb.on_call_redirected) { + (*pjsua_var.ua_cfg.cb.on_call_redirected)(call->index, target, cmd, e); + } else { + PJ_LOG(4,(THIS_FILE, "Unhandled redirection for call %d " + "(callback not implemented by application). Disconnecting " + "call.", + call->index)); + *cmd = PJSIP_REDIRECT_STOP; + } + + PJSUA_UNLOCK(); +} + |