[asterisk-commits] file: branch file/gulp_transfer r387799 - in /team/file/gulp_transfer: includ...
SVN commits to the Asterisk project
asterisk-commits at lists.digium.com
Tue May 7 09:31:03 CDT 2013
Author: file
Date: Tue May 7 09:30:59 2013
New Revision: 387799
URL: http://svnview.digium.com/svn/asterisk?view=rev&rev=387799
Log:
Add initial support for attended transfers. This branch doesn't have the implementation of the API call but from the SIP side it does what I want it to.
Modified:
team/file/gulp_transfer/include/asterisk/res_sip_session.h
team/file/gulp_transfer/res/res_sip_refer.c
team/file/gulp_transfer/res/res_sip_session.c
team/file/gulp_transfer/res/res_sip_session.exports.in
Modified: team/file/gulp_transfer/include/asterisk/res_sip_session.h
URL: http://svnview.digium.com/svn/asterisk/team/file/gulp_transfer/include/asterisk/res_sip_session.h?view=diff&rev=387799&r1=387798&r2=387799
==============================================================================
--- team/file/gulp_transfer/include/asterisk/res_sip_session.h (original)
+++ team/file/gulp_transfer/include/asterisk/res_sip_session.h Tue May 7 09:30:59 2013
@@ -467,4 +467,18 @@
void ast_sip_session_send_request_with_cb(struct ast_sip_session *session, pjsip_tx_data *tdata,
ast_sip_session_response_cb on_response);
+/*!
+ * \brief Retrieves a session from a dialog
+ *
+ * \param dlg The dialog to retrieve the session from
+ *
+ * \retval non-NULL if session exists
+ * \retval NULL if no session
+ *
+ * \note The reference count of the session is increased when returned
+ *
+ * \note This function *must* be called with the dialog locked
+ */
+struct ast_sip_session *ast_sip_dialog_get_session(pjsip_dialog *dlg);
+
#endif /* _RES_SIP_SESSION_H */
Modified: team/file/gulp_transfer/res/res_sip_refer.c
URL: http://svnview.digium.com/svn/asterisk/team/file/gulp_transfer/res/res_sip_refer.c?view=diff&rev=387799&r1=387798&r2=387799
==============================================================================
--- team/file/gulp_transfer/res/res_sip_refer.c (original)
+++ team/file/gulp_transfer/res/res_sip_refer.c Tue May 7 09:30:59 2013
@@ -289,97 +289,60 @@
return progress;
}
-static int refer_incoming_attended_request(struct ast_sip_session *session, pjsip_rx_data *rdata, pjsip_param *replaces_param)
-{
- return 0;
-}
-
-/*! \brief Structure for blind transfer callback details */
-struct refer_blind {
- /*! \brief Context being used for transfer */
- const char *context;
- /*! \brief Optional progress structure */
+/*! \brief Structure for attended transfer task */
+struct refer_attended {
+ /*! \brief Transferer session */
+ struct ast_sip_session *transferer;
+ /*! \brief Transferer channel */
+ struct ast_channel *transferer_chan;
+ /*! \brief Transferee session */
+ struct ast_sip_session *transferee;
+ /*! \brief Optional refer progress structure */
struct refer_progress *progress;
- /*! \brief REFER message */
- pjsip_rx_data *rdata;
};
-/*! \brief Blind transfer callback function */
-static void refer_blind_callback(struct ast_channel *chan, void *user_data)
-{
- struct refer_blind *refer = user_data;
- const pj_str_t str_referred_by = { "Referred-By", 11 };
- pjsip_generic_string_hdr *referred_by = pjsip_msg_find_hdr_by_name(refer->rdata->msg_info.msg, &str_referred_by, NULL);
-
- /* If progress monitoring is being done attach a frame hook so we can monitor it */
- if (refer->progress) {
- struct ast_framehook_interface hook = {
- .version = AST_FRAMEHOOK_INTERFACE_VERSION,
- .event_cb = refer_progress_framehook,
- .destroy_cb = refer_progress_framehook_destroy,
- .data = refer->progress,
- };
-
- /* We need to bump the reference count up on the progress structure since it is in the frame hook now */
- 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) {
- struct refer_progress_notification *notification = refer_progress_notification_alloc(refer->progress, 200,
- PJSIP_EVSUB_STATE_TERMINATED);
-
- if (notification) {
- refer_progress_notify(notification);
- }
-
- ao2_cleanup(refer->progress);
- }
- }
-
- pbx_builtin_setvar_helper(chan, "SIPREFERRINGCONTEXT", refer->context);
-
- if (referred_by) {
- char *uri = referred_by->hvalue.ptr;
-
- uri[referred_by->hvalue.slen] = '\0';
- pbx_builtin_setvar_helper(chan, "SIPREFERREDBYHDR", uri);
- }
-}
-
-static int refer_incoming_blind_request(struct ast_sip_session *session, pjsip_rx_data *rdata, pjsip_sip_uri *target)
-{
- const char *context = (session->channel ? pbx_builtin_getvar_helper(session->channel, "TRANSFER_CONTEXT") : "");
- char exten[AST_MAX_EXTENSION];
- RAII_VAR(struct refer_progress *, progress, NULL, ao2_cleanup);
- struct refer_blind refer;
+/*! \brief Destructor for attended transfer task */
+static void refer_attended_destroy(void *obj)
+{
+ struct refer_attended *attended = obj;
+
+ ao2_cleanup(attended->transferer);
+ ast_channel_unref(attended->transferer_chan);
+ ao2_cleanup(attended->transferee);
+}
+
+/*! \brief Allocator for attended transfer task */
+static struct refer_attended *refer_attended_alloc(struct ast_sip_session *transferer, struct ast_sip_session *transferee,
+ struct refer_progress *progress)
+{
+ struct refer_attended *attended = ao2_alloc(sizeof(*attended), refer_attended_destroy);
+
+ if (!attended) {
+ return NULL;
+ }
+
+ ao2_ref(transferer, +1);
+ attended->transferer = transferer;
+ ast_channel_ref(transferer->channel);
+ attended->transferer_chan = transferer->channel;
+ ao2_ref(transferee, +1);
+ attended->transferee = transferee;
+
+ if (progress) {
+ ao2_ref(progress, +1);
+ attended->progress = progress;
+ }
+
+ return attended;
+}
+
+/*! \brief Task for attended transfer */
+static int refer_attended(void *data)
+{
+ RAII_VAR(struct refer_attended *, attended, data, ao2_cleanup);
int response = 0;
- /* If no explicit transfer context has been provided use their configured context */
- if (ast_strlen_zero(context)) {
- context = session->endpoint->context;
- }
-
- /* 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));
- if (!ast_exists_extension(NULL, context, exten, 1, NULL)) {
- pjsip_dlg_respond(session->inv_session->dlg, rdata, 404, NULL, NULL, NULL);
- return 0;
- }
-
- /* Set up REFER progress subscription if requested/possible */
- progress = refer_progress_alloc(session, rdata);
-
- /* If REFER progress monitoring *should* occur but an error occurred report it */
- if (progress && !progress->sub) {
- pjsip_dlg_respond(session->inv_session->dlg, rdata, 500, NULL, NULL, NULL);
- return 0;
- }
-
- refer.context = context;
- refer.progress = progress;
- refer.rdata = rdata;
-
- switch (ast_bridge_transfer_blind(session->channel, exten, context, refer_blind_callback, &refer)) {
+ switch (ast_bridge_transfer_attended(attended->transferer_chan, attended->transferee->channel, NULL)) {
case AST_BRIDGE_TRANSFER_INVALID:
response = 503;
break;
@@ -390,9 +353,202 @@
response = 500;
break;
case AST_BRIDGE_TRANSFER_SUCCESS:
+ attended->transferer->defer_terminate = 1;
+ break;
+ }
+
+ if (attended->progress && response) {
+ struct refer_progress_notification *notification = refer_progress_notification_alloc(attended->progress, response, PJSIP_EVSUB_STATE_TERMINATED);
+
+ if (notification) {
+ refer_progress_notify(notification);
+ }
+ }
+
+ return 0;
+}
+
+static int refer_incoming_attended_request(struct ast_sip_session *session, pjsip_rx_data *rdata, pjsip_param *replaces_param,
+ struct refer_progress *progress)
+{
+ const pj_str_t str_replaces = { "Replaces", 8 };
+ pj_str_t replaces_content;
+ pjsip_replaces_hdr *replaces;
+ int parsed_len;
+ pjsip_dialog *dlg;
+
+ pj_strdup_with_null(rdata->tp_info.pool, &replaces_content, &replaces_param->value);
+
+ /* Parsing the parameter as a Replaces header easily grabs the needed information */
+ if (!(replaces = pjsip_parse_hdr(rdata->tp_info.pool, &str_replaces, replaces_content.ptr,
+ pj_strlen(&replaces_content), &parsed_len))) {
+ return 400;
+ }
+
+ /* See if the dialog is local, or remote */
+ if ((dlg = pjsip_ua_find_dialog(&replaces->call_id, &replaces->to_tag, &replaces->from_tag, PJ_TRUE))) {
+ RAII_VAR(struct ast_sip_session *, other_session, ast_sip_dialog_get_session(dlg), ao2_cleanup);
+ struct refer_attended *attended;
+
+ pjsip_dlg_dec_lock(dlg);
+
+ if (!other_session) {
+ return 603;
+ }
+
+ /* We defer actually doing the attended transfer to the other session so no deadlock can occur */
+ if (!(attended = refer_attended_alloc(session, other_session, progress))) {
+ return 500;
+ }
+
+ /* Push it to the other session, which will have both channels with minimal locking */
+ if (ast_sip_push_task(other_session->serializer, refer_attended, attended)) {
+ ao2_cleanup(attended);
+ return 500;
+ }
+
+ return 200;
+ } else {
+ }
+
+ return 0;
+}
+
+/*! \brief Structure for blind transfer callback details */
+struct refer_blind {
+ /*! \brief Context being used for transfer */
+ const char *context;
+ /*! \brief Optional progress structure */
+ struct refer_progress *progress;
+ /*! \brief REFER message */
+ pjsip_rx_data *rdata;
+};
+
+/*! \brief Blind transfer callback function */
+static void refer_blind_callback(struct ast_channel *chan, void *user_data)
+{
+ struct refer_blind *refer = user_data;
+ const pj_str_t str_referred_by = { "Referred-By", 11 };
+ pjsip_generic_string_hdr *referred_by = pjsip_msg_find_hdr_by_name(refer->rdata->msg_info.msg, &str_referred_by, NULL);
+
+ /* If progress monitoring is being done attach a frame hook so we can monitor it */
+ if (refer->progress) {
+ struct ast_framehook_interface hook = {
+ .version = AST_FRAMEHOOK_INTERFACE_VERSION,
+ .event_cb = refer_progress_framehook,
+ .destroy_cb = refer_progress_framehook_destroy,
+ .data = refer->progress,
+ };
+
+ /* We need to bump the reference count up on the progress structure since it is in the frame hook now */
+ 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) {
+ struct refer_progress_notification *notification = refer_progress_notification_alloc(refer->progress, 200,
+ PJSIP_EVSUB_STATE_TERMINATED);
+
+ if (notification) {
+ refer_progress_notify(notification);
+ }
+
+ ao2_cleanup(refer->progress);
+ }
+ }
+
+ if (!ast_strlen_zero(refer->context)) {
+ pbx_builtin_setvar_helper(chan, "SIPREFERRINGCONTEXT", refer->context);
+ }
+
+ if (referred_by) {
+ char *uri = referred_by->hvalue.ptr;
+
+ uri[referred_by->hvalue.slen] = '\0';
+ pbx_builtin_setvar_helper(chan, "SIPREFERREDBYHDR", uri);
+ }
+}
+
+static int refer_incoming_blind_request(struct ast_sip_session *session, pjsip_rx_data *rdata, pjsip_sip_uri *target,
+ struct refer_progress *progress)
+{
+ const char *context = (session->channel ? pbx_builtin_getvar_helper(session->channel, "TRANSFER_CONTEXT") : "");
+ char exten[AST_MAX_EXTENSION];
+ struct refer_blind refer;
+
+ /* If no explicit transfer context has been provided use their configured context */
+ if (ast_strlen_zero(context)) {
+ context = session->endpoint->context;
+ }
+
+ /* 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));
+ if (!ast_exists_extension(NULL, context, exten, 1, NULL)) {
+ return 404;
+ }
+
+ refer.context = context;
+ refer.progress = progress;
+ refer.rdata = rdata;
+
+ switch (ast_bridge_transfer_blind(session->channel, exten, context, refer_blind_callback, &refer)) {
+ case AST_BRIDGE_TRANSFER_INVALID:
+ return 503;
+ case AST_BRIDGE_TRANSFER_NOT_PERMITTED:
+ return 403;
+ case AST_BRIDGE_TRANSFER_FAIL:
+ return 500;
+ case AST_BRIDGE_TRANSFER_SUCCESS:
session->defer_terminate = 1;
- response = 200;
- break;
+ return 200;
+ }
+
+ return 503;
+}
+
+static int refer_incoming_request(struct ast_sip_session *session, struct pjsip_rx_data *rdata)
+{
+ const pj_str_t str_refer_to = { "Refer-To", 8 };
+ pjsip_generic_string_hdr *refer_to;
+ char *uri;
+ const pj_str_t str_to = { "To", 2 };
+ pjsip_fromto_hdr *target;
+ pjsip_sip_uri *target_uri;
+ RAII_VAR(struct refer_progress *, progress, NULL, ao2_cleanup);
+ const pj_str_t str_replaces = { "Replaces", 8 };
+ pjsip_param *replaces;
+ int response;
+
+ /* A Refer-To header is required */
+ if (!(refer_to = pjsip_msg_find_hdr_by_name(rdata->msg_info.msg, &str_refer_to, NULL))) {
+ pjsip_dlg_respond(session->inv_session->dlg, rdata, 400, NULL, NULL, NULL);
+ return 0;
+ }
+ uri = refer_to->hvalue.ptr;
+ uri[refer_to->hvalue.slen] = '\0';
+
+ /* Parse the provided URI string as a To header so we can get the target */
+ if (!(target = pjsip_parse_hdr(rdata->tp_info.pool, &str_to, refer_to->hvalue.ptr, refer_to->hvalue.slen, NULL)) ||
+ (!PJSIP_URI_SCHEME_IS_SIP(target->uri) && !PJSIP_URI_SCHEME_IS_SIPS(target->uri))) {
+ pjsip_dlg_respond(session->inv_session->dlg, rdata, 400, NULL, NULL, NULL);
+ return 0;
+ }
+ target_uri = pjsip_uri_get_uri(target->uri);
+
+ /* Set up REFER progress subscription if requested/possible */
+ progress = refer_progress_alloc(session, rdata);
+
+ /* If REFER progress monitoring *should* occur but an error occurred report it */
+ if (progress && !progress->sub) {
+ pjsip_dlg_respond(session->inv_session->dlg, rdata, 500, NULL, NULL, NULL);
+ return 0;
+ }
+
+ /* Determine if this is an attended or blind transfer */
+ if ((replaces = pjsip_param_find(&target_uri->header_param, &str_replaces)) ||
+ (replaces = pjsip_param_find(&target_uri->other_param, &str_replaces))) {
+ response = refer_incoming_attended_request(session, rdata, replaces, progress);
+ } else {
+ response = refer_incoming_blind_request(session, rdata, target_uri, progress);
}
if (!progress) {
@@ -424,41 +580,6 @@
return 0;
}
-static int refer_incoming_request(struct ast_sip_session *session, struct pjsip_rx_data *rdata)
-{
- const pj_str_t str_refer_to = { "Refer-To", 8 };
- pjsip_generic_string_hdr *refer_to;
- char *uri;
- const pj_str_t str_to = { "To", 2 };
- pjsip_fromto_hdr *target;
- pjsip_sip_uri *target_uri;
- const pj_str_t str_replaces = { "Replaces", 8 };
- pjsip_param *replaces;
-
- /* A Refer-To header is required */
- if (!(refer_to = pjsip_msg_find_hdr_by_name(rdata->msg_info.msg, &str_refer_to, NULL))) {
- pjsip_dlg_respond(session->inv_session->dlg, rdata, 400, NULL, NULL, NULL);
- return 0;
- }
- uri = refer_to->hvalue.ptr;
- uri[refer_to->hvalue.slen] = '\0';
-
- /* Parse the provided URI string as a To header so we can get the target */
- if (!(target = pjsip_parse_hdr(rdata->tp_info.pool, &str_to, refer_to->hvalue.ptr, refer_to->hvalue.slen, NULL)) ||
- (!PJSIP_URI_SCHEME_IS_SIP(target->uri) && !PJSIP_URI_SCHEME_IS_SIPS(target->uri))) {
- pjsip_dlg_respond(session->inv_session->dlg, rdata, 400, NULL, NULL, NULL);
- return 0;
- }
- target_uri = pjsip_uri_get_uri(target->uri);
-
- /* Determine if this is an attended or blind transfer */
- if ((replaces = pjsip_param_find(&target_uri->header_param, &str_replaces))) {
- return refer_incoming_attended_request(session, rdata, replaces);
- } else {
- return refer_incoming_blind_request(session, rdata, target_uri);
- }
-}
-
static struct ast_sip_session_supplement refer_supplement = {
.method = "REFER",
.incoming_request = refer_incoming_request,
Modified: team/file/gulp_transfer/res/res_sip_session.c
URL: http://svnview.digium.com/svn/asterisk/team/file/gulp_transfer/res/res_sip_session.c?view=diff&rev=387799&r1=387798&r2=387799
==============================================================================
--- team/file/gulp_transfer/res/res_sip_session.c (original)
+++ team/file/gulp_transfer/res/res_sip_session.c Tue May 7 09:30:59 2013
@@ -1037,6 +1037,21 @@
pjsip_inv_set_local_sdp(inv_session, offer);
ao2_ref(session, +1);
+ return session;
+}
+
+struct ast_sip_session *ast_sip_dialog_get_session(pjsip_dialog *dlg)
+{
+ pjsip_inv_session *inv_session = pjsip_dlg_get_inv_session(dlg);
+ struct ast_sip_session *session;
+
+ if (!inv_session ||
+ !(session = inv_session->mod_data[session_module.id])) {
+ return NULL;
+ }
+
+ ao2_ref(session, +1);
+
return session;
}
Modified: team/file/gulp_transfer/res/res_sip_session.exports.in
URL: http://svnview.digium.com/svn/asterisk/team/file/gulp_transfer/res/res_sip_session.exports.in?view=diff&rev=387799&r1=387798&r2=387799
==============================================================================
--- team/file/gulp_transfer/res/res_sip_session.exports.in (original)
+++ team/file/gulp_transfer/res/res_sip_session.exports.in Tue May 7 09:30:59 2013
@@ -13,6 +13,7 @@
LINKER_SYMBOL_PREFIXast_sip_session_send_response;
LINKER_SYMBOL_PREFIXast_sip_session_send_request;
LINKER_SYMBOL_PREFIXast_sip_session_create_outgoing;
+ LINKER_SYMBOL_PREFIXast_sip_dialog_get_session;
local:
*;
};
More information about the asterisk-commits
mailing list