res_pjsip: Fix leak on error in ast_sip_auth_vector_init.
[asterisk/asterisk.git] / res / res_pjsip_refer.c
index 107a3e7..62f8b67 100644 (file)
@@ -61,6 +61,8 @@ struct refer_progress {
        char *transferee;
        /*! \brief Non-zero if the 100 notify has been sent */
        int sent_100;
+       /*! \brief Whether to notifies all the progress details on blind transfer */
+       unsigned int refer_blind_progress;
 };
 
 /*! \brief REFER Progress notification structure */
@@ -372,6 +374,8 @@ static int refer_progress_alloc(struct ast_sip_session *session, pjsip_rx_data *
        ast_debug(3, "Created progress monitor '%p' for transfer occurring from channel '%s' and endpoint '%s'\n",
                progress, ast_channel_name(session->channel), ast_sorcery_object_get_id(session->endpoint));
 
+       (*progress)->refer_blind_progress = session->endpoint->refer_blind_progress;
+
        (*progress)->framehook = -1;
 
        /* To prevent a potential deadlock we need the dialog so we can lock/unlock */
@@ -539,6 +543,7 @@ static int refer_attended_task(void *data)
                }
        }
 
+       ast_sip_session_end_if_deferred(attended->transferer);
        if (response != 200) {
                if (!ast_sip_push_task(attended->transferer->serializer,
                        defer_termination_cancel, attended->transferer)) {
@@ -563,6 +568,8 @@ struct refer_blind {
        pjsip_replaces_hdr *replaces;
        /*! \brief Optional Refer-To header */
        pjsip_sip_uri *refer_to;
+       /*! \brief Attended transfer flag */
+       unsigned int attended:1;
 };
 
 /*! \brief Blind transfer callback function */
@@ -577,8 +584,16 @@ static void refer_blind_callback(struct ast_channel *chan, struct transfer_chann
 
        pbx_builtin_setvar_helper(chan, "SIPTRANSFER", "yes");
 
-       /* If progress monitoring is being done attach a frame hook so we can monitor it */
-       if (refer->progress) {
+       if (refer->progress && !refer->attended && !refer->progress->refer_blind_progress) {
+               /* If blind transfer and endpoint doesn't want to receive all the progress details */
+               struct refer_progress_notification *notification = refer_progress_notification_alloc(refer->progress, 200,
+                       PJSIP_EVSUB_STATE_TERMINATED);
+
+               if (notification) {
+                       refer_progress_notify(notification);
+               }
+       } else if (refer->progress) {
+               /* If attended transfer and progress monitoring is being done attach a frame hook so we can monitor it */
                struct ast_framehook_interface hook = {
                        .version = AST_FRAMEHOOK_INTERFACE_VERSION,
                        .event_cb = refer_progress_framehook,
@@ -758,6 +773,7 @@ static int refer_incoming_attended_request(struct ast_sip_session *session, pjsi
 
                /* Push it to the other session, which will have both channels with minimal locking */
                if (ast_sip_push_task(other_session->serializer, refer_attended_task, attended)) {
+                       ast_sip_session_end_if_deferred(session);
                        ast_sip_session_defer_termination_cancel(session);
                        ao2_cleanup(attended);
                        return 500;
@@ -785,6 +801,7 @@ static int refer_incoming_attended_request(struct ast_sip_session *session, pjsi
                refer.rdata = rdata;
                refer.replaces = replaces;
                refer.refer_to = target_uri;
+               refer.attended = 1;
 
                if (ast_sip_session_defer_termination(session)) {
                        ast_log(LOG_ERROR, "Received REFER for remote session on channel '%s' from endpoint '%s' but could not defer termination, rejecting\n",
@@ -795,9 +812,12 @@ static int refer_incoming_attended_request(struct ast_sip_session *session, pjsi
 
                response = xfer_response_code2sip(ast_bridge_transfer_blind(1, session->channel,
                        "external_replaces", context, refer_blind_callback, &refer));
+
+               ast_sip_session_end_if_deferred(session);
                if (response != 200) {
                        ast_sip_session_defer_termination_cancel(session);
                }
+
                return response;
        }
 }
@@ -822,6 +842,13 @@ static int refer_incoming_blind_request(struct ast_sip_session *session, pjsip_r
         */
        AST_SIP_USER_OPTIONS_TRUNCATE_CHECK(exten);
 
+       /* Uri without exten */
+       if (ast_strlen_zero(exten)) {
+               ast_copy_string(exten, "s", sizeof(exten));
+               ast_debug(3, "Channel '%s' from endpoint '%s' attempted blind transfer to a target without extension. Target was set to 's@%s'\n",
+                       ast_channel_name(session->channel), ast_sorcery_object_get_id(session->endpoint), context);
+       }
+
        if (!ast_exists_extension(NULL, context, exten, 1, NULL)) {
                ast_log(LOG_ERROR, "Channel '%s' from endpoint '%s' attempted blind transfer to '%s@%s' but target does not exist\n",
                        ast_channel_name(session->channel), ast_sorcery_object_get_id(session->endpoint), exten, context);
@@ -832,6 +859,7 @@ static int refer_incoming_blind_request(struct ast_sip_session *session, pjsip_r
        refer.progress = progress;
        refer.rdata = rdata;
        refer.refer_to = target;
+       refer.attended = 0;
 
        if (ast_sip_session_defer_termination(session)) {
                ast_log(LOG_ERROR, "Channel '%s' from endpoint '%s' attempted blind transfer but could not defer termination, rejecting\n",
@@ -842,9 +870,12 @@ static int refer_incoming_blind_request(struct ast_sip_session *session, pjsip_r
 
        response = xfer_response_code2sip(ast_bridge_transfer_blind(1, session->channel,
                exten, context, refer_blind_callback, &refer));
+
+       ast_sip_session_end_if_deferred(session);
        if (response != 200) {
                ast_sip_session_defer_termination_cancel(session);
        }
+
        return response;
 }
 
@@ -1007,6 +1038,7 @@ static int refer_incoming_refer_request(struct ast_sip_session *session, struct
        int response;
 
        static const pj_str_t str_refer_to = { "Refer-To", 8 };
+       static const pj_str_t str_refer_to_s = { "r", 1 };
        static const pj_str_t str_replaces = { "Replaces", 8 };
 
        if (!session->channel) {
@@ -1025,7 +1057,7 @@ static int refer_incoming_refer_request(struct ast_sip_session *session, struct
        }
 
        /* A Refer-To header is required */
-       refer_to = pjsip_msg_find_hdr_by_name(rdata->msg_info.msg, &str_refer_to, NULL);
+       refer_to = pjsip_msg_find_hdr_by_names(rdata->msg_info.msg, &str_refer_to, &str_refer_to_s, NULL);
        if (!refer_to) {
                pjsip_dlg_respond(session->inv_session->dlg, rdata, 400, NULL, NULL, NULL);
                ast_debug(3, "Received a REFER without Refer-To on channel '%s' from endpoint '%s'\n",
@@ -1179,6 +1211,8 @@ static int load_module(void)
        ast_sip_register_service(&refer_progress_module);
        ast_sip_session_register_supplement(&refer_supplement);
 
+       ast_module_shutdown_ref(ast_module_info->self);
+
        return AST_MODULE_LOAD_SUCCESS;
 }