res_pjsip: Fix leak on error in ast_sip_auth_vector_init.
[asterisk/asterisk.git] / res / res_pjsip_refer.c
index f89f901..62f8b67 100644 (file)
@@ -59,6 +59,10 @@ struct refer_progress {
        struct transfer_channel_data *transfer_data;
        /*! \brief Uniqueid of transferee channel */
        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 */
@@ -133,6 +137,18 @@ static int refer_progress_notify(void *data)
                notification->progress->sub = NULL;
        }
 
+       /* Send a deferred initial 100 Trying SIP frag NOTIFY if we haven't already. */
+       if (!notification->progress->sent_100) {
+               notification->progress->sent_100 = 1;
+               if (notification->response != 100) {
+                       ast_debug(3, "Sending initial 100 Trying NOTIFY for progress monitor '%p'\n",
+                               notification->progress);
+                       if (pjsip_xfer_notify(sub, PJSIP_EVSUB_STATE_ACTIVE, 100, NULL, &tdata) == PJ_SUCCESS) {
+                               pjsip_xfer_send_request(sub, tdata);
+                       }
+               }
+       }
+
        ast_debug(3, "Sending NOTIFY with response '%d' and state '%u' on subscription '%p' and progress monitor '%p'\n",
                notification->response, notification->state, sub, notification->progress);
 
@@ -340,7 +356,6 @@ static int refer_progress_alloc(struct ast_sip_session *session, pjsip_rx_data *
        const pj_str_t str_refer_sub = { "Refer-Sub", 9 };
        pjsip_generic_string_hdr *refer_sub = NULL;
        const pj_str_t str_true = { "true", 4 };
-       pjsip_tx_data *tdata;
        pjsip_hdr hdr_list;
        char tps_name[AST_TASKPROCESSOR_MAX_NAME + 1];
 
@@ -359,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 */
@@ -392,12 +409,6 @@ static int refer_progress_alloc(struct ast_sip_session *session, pjsip_rx_data *
        ast_debug(3, "Accepting REFER request for progress monitor '%p'\n", *progress);
        pjsip_xfer_accept((*progress)->sub, rdata, 202, &hdr_list);
 
-       /* Send initial NOTIFY Request */
-       ast_debug(3, "Sending initial 100 Trying NOTIFY for progress monitor '%p'\n", *progress);
-       if (pjsip_xfer_notify((*progress)->sub, PJSIP_EVSUB_STATE_ACTIVE, 100, NULL, &tdata) == PJ_SUCCESS) {
-               pjsip_xfer_send_request((*progress)->sub, tdata);
-       }
-
        return 0;
 
 error:
@@ -532,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)) {
@@ -556,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 */
@@ -566,11 +580,20 @@ static void refer_blind_callback(struct ast_channel *chan, struct transfer_chann
        pjsip_generic_string_hdr *referred_by;
 
        static const pj_str_t str_referred_by = { "Referred-By", 11 };
+       static const pj_str_t str_referred_by_s = { "b", 1 };
 
        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,
@@ -600,7 +623,10 @@ static void refer_blind_callback(struct ast_channel *chan, struct transfer_chann
                ao2_ref(refer->progress, +1);
 
                /* If we can't attach a frame hook for whatever reason send a notification of success immediately */
-               if ((refer->progress->framehook = ast_framehook_attach(chan, &hook)) < 0) {
+               ast_channel_lock(chan);
+               refer->progress->framehook = ast_framehook_attach(chan, &hook);
+               ast_channel_unlock(chan);
+               if (refer->progress->framehook < 0) {
                        struct refer_progress_notification *notification = refer_progress_notification_alloc(refer->progress, 200,
                                PJSIP_EVSUB_STATE_TERMINATED);
 
@@ -631,7 +657,9 @@ static void refer_blind_callback(struct ast_channel *chan, struct transfer_chann
                                refer_progress_notify(notification);
                        }
 
+                       ast_channel_lock(chan);
                        ast_framehook_detach(chan, refer->progress->framehook);
+                       ast_channel_unlock(chan);
 
                        ao2_cleanup(refer->progress);
                }
@@ -639,8 +667,8 @@ static void refer_blind_callback(struct ast_channel *chan, struct transfer_chann
 
        pbx_builtin_setvar_helper(chan, "SIPREFERRINGCONTEXT", S_OR(refer->context, NULL));
 
-       referred_by = pjsip_msg_find_hdr_by_name(refer->rdata->msg_info.msg,
-               &str_referred_by, NULL);
+       referred_by = pjsip_msg_find_hdr_by_names(refer->rdata->msg_info.msg,
+               &str_referred_by, &str_referred_by_s, NULL);
        if (referred_by) {
                size_t uri_size = pj_strlen(&referred_by->hvalue) + 1;
                char *uri = ast_alloca(uri_size);
@@ -745,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;
@@ -772,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",
@@ -782,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;
        }
 }
@@ -802,6 +835,20 @@ static int refer_incoming_blind_request(struct ast_sip_session *session, pjsip_r
 
        /* Using the user portion of the target URI see if it exists as a valid extension in their context */
        ast_copy_pj_str(exten, &target->user, sizeof(exten));
+
+       /*
+        * We may want to match in the dialplan without any user
+        * options getting in the way.
+        */
+       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);
@@ -812,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",
@@ -822,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;
 }
 
@@ -962,7 +1013,8 @@ inv_replace_failed:
                session->defer_terminate = 1;
                ast_hangup(session->channel);
 
-               if (pjsip_inv_end_session(session->inv_session, response, NULL, &packet) == PJ_SUCCESS) {
+               if (pjsip_inv_end_session(session->inv_session, response, NULL, &packet) == PJ_SUCCESS
+                       && packet) {
                        ast_sip_session_send_response(session, packet);
                }
        } else {
@@ -978,6 +1030,7 @@ static int refer_incoming_refer_request(struct ast_sip_session *session, struct
 {
        pjsip_generic_string_hdr *refer_to;
        char *uri;
+       size_t uri_size;
        pjsip_uri *target;
        pjsip_sip_uri *target_uri;
        RAII_VAR(struct refer_progress *, progress, NULL, ao2_cleanup);
@@ -985,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) {
@@ -1003,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",
@@ -1011,20 +1065,19 @@ static int refer_incoming_refer_request(struct ast_sip_session *session, struct
                return 0;
        }
 
-       /* This is done on purpose (and is safe) - it's done so that the value passed to
-        * pjsip_parse_uri is NULL terminated as required
+       /* The ast_copy_pj_str to uri is needed because it puts the NULL terminator to the uri
+        * as pjsip_parse_uri require a NULL terminated uri
         */
-       uri = refer_to->hvalue.ptr;
-       uri[refer_to->hvalue.slen] = '\0';
 
-       target = pjsip_parse_uri(rdata->tp_info.pool, refer_to->hvalue.ptr, refer_to->hvalue.slen, 0);
+       uri_size = pj_strlen(&refer_to->hvalue) + 1;
+       uri = ast_alloca(uri_size);
+       ast_copy_pj_str(uri, &refer_to->hvalue, uri_size);
+
+       target = pjsip_parse_uri(rdata->tp_info.pool, uri, uri_size - 1, 0);
+
        if (!target
                || (!PJSIP_URI_SCHEME_IS_SIP(target)
                        && !PJSIP_URI_SCHEME_IS_SIPS(target))) {
-               size_t uri_size = pj_strlen(&refer_to->hvalue) + 1;
-               char *uri = ast_alloca(uri_size);
-
-               ast_copy_pj_str(uri, &refer_to->hvalue, uri_size);
 
                pjsip_dlg_respond(session->inv_session->dlg, rdata, 400, NULL, NULL, NULL);
                ast_debug(3, "Received a REFER without a parseable Refer-To ('%s') on channel '%s' from endpoint '%s'\n",
@@ -1158,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;
 }