[asterisk-commits] mjordan: trunk r431718 - in /trunk: ./ channels/ include/asterisk/ res/ res/a...

SVN commits to the Asterisk project asterisk-commits at lists.digium.com
Thu Feb 12 14:34:39 CST 2015


Author: mjordan
Date: Thu Feb 12 14:34:37 2015
New Revision: 431718

URL: http://svnview.digium.com/svn/asterisk?view=rev&rev=431718
Log:
ARI/PJSIP: Add the ability to redirect (transfer) a channel in a Stasis app

This patch adds a new feature to ARI to redirect a channel to another server,
and fixes a few bugs in PJSIP's handling of the Transfer dialplan
application/ARI redirect capability.

*New Feature*
A new operation has been added to the ARI channels resource, redirect. With
this, a channel in a Stasis application can be redirected to another endpoint
of the same underlying channel technology.

*Bug fixes*
In the process of writing this new feature, two bugs were fixed in the PJSIP
stack:
(1) The existing .transfer channel callback had the limitation that it could
    only transfer channels to a SIP URI, i.e., you had to pass
    'PJSIP/sip:foo at my_provider.com' to the dialplan application. While this is
    still supported, it is somewhat unintuitive - particularly in a world full
    of endpoints. As such, we now also support specifying the PJSIP endpoint to
    transfer to.
(2) res_pjsip_multihomed was, unfortunately, trying to 'help' a 302 redirect by
    updating its Contact header. Alas, that resulted in the forwarding
    destination set by the dialplan application/ARI resource/whatever being
    rewritten with very incorrect information. Hence, we now don't bother
    updating an outgoing response if it is a 302. Since this took a looong time
    to find, some additional debug statements have been added to those modules
    that update the Contact headers.

Review: https://reviewboard.asterisk.org/r/4316/

ASTERISK-24015 #close
Reported by: Private Name

ASTERISK-24703 #close
Reported by: Matt Jordan
........

Merged revisions 431717 from http://svn.asterisk.org/svn/asterisk/branches/13

Modified:
    trunk/   (props changed)
    trunk/CHANGES
    trunk/channels/chan_pjsip.c
    trunk/include/asterisk/stasis_app.h
    trunk/res/ari/resource_channels.c
    trunk/res/ari/resource_channels.h
    trunk/res/res_ari_channels.c
    trunk/res/res_pjsip_multihomed.c
    trunk/res/res_pjsip_nat.c
    trunk/res/res_pjsip_transport_websocket.c
    trunk/res/stasis/control.c
    trunk/rest-api/api-docs/channels.json

Propchange: trunk/
------------------------------------------------------------------------------
Binary property 'branch-13-merged' - no diff available.

Modified: trunk/CHANGES
URL: http://svnview.digium.com/svn/asterisk/trunk/CHANGES?view=diff&rev=431718&r1=431717&r2=431718
==============================================================================
--- trunk/CHANGES (original)
+++ trunk/CHANGES Thu Feb 12 14:34:37 2015
@@ -99,6 +99,24 @@
  * Added preferchannelclass=no option to prefer the application-passed class
    over the channel-set musicclass. This allows separate hold-music from
    application (e.g. Queue or Dial) specified music.
+
+------------------------------------------------------------------------------
+--- Functionality changes from Asterisk 13.2.0 to Asterisk 13.3.0 ------------
+------------------------------------------------------------------------------
+
+chan_pjsip/app_transfer
+------------------
+ * The Transfer application, when used with chan_pjsip, now supports using
+   a PJSIP endpoint as the transfer destination. This is in addition to
+   explicitly specifying a SIP URI to transfer to.
+
+res_ari_channels
+------------------
+ * The ARI /channels resource now supports a new operation, 'redirect'. The
+   redirect operation will perform a technology and state specific redirection
+   on the channel to a specified endpoint or destination. In the case of SIP
+   technologies, this is either a 302 Redirect response to an on-going INVITE
+   dialog or a SIP REFER request.
 
 ------------------------------------------------------------------------------
 --- Functionality changes from Asterisk 13.1.0 to Asterisk 13.2.0 ------------

Modified: trunk/channels/chan_pjsip.c
URL: http://svnview.digium.com/svn/asterisk/trunk/channels/chan_pjsip.c?view=diff&rev=431718&r1=431717&r2=431718
==============================================================================
--- trunk/channels/chan_pjsip.c (original)
+++ trunk/channels/chan_pjsip.c Thu Feb 12 14:34:37 2015
@@ -1378,6 +1378,8 @@
 	pj_str_t tmp;
 
 	if (pjsip_inv_end_session(session->inv_session, 302, NULL, &packet) != PJ_SUCCESS) {
+		ast_log(LOG_WARNING, "Failed to redirect PJSIP session for channel %s\n",
+			ast_channel_name(session->channel));
 		message = AST_TRANSFER_FAILED;
 		ast_queue_control_data(session->channel, AST_CONTROL_TRANSFER, &message, sizeof(message));
 
@@ -1390,6 +1392,8 @@
 
 	pj_strdup2_with_null(packet->pool, &tmp, target);
 	if (!(contact->uri = pjsip_parse_uri(packet->pool, tmp.ptr, tmp.slen, PJSIP_PARSE_URI_AS_NAMEADDR))) {
+		ast_log(LOG_WARNING, "Failed to parse destination URI '%s' for channel %s\n",
+			target, ast_channel_name(session->channel));
 		message = AST_TRANSFER_FAILED;
 		ast_queue_control_data(session->channel, AST_CONTROL_TRANSFER, &message, sizeof(message));
 		pjsip_tx_data_dec_ref(packet);
@@ -1431,14 +1435,28 @@
 static int transfer(void *data)
 {
 	struct transfer_data *trnf_data = data;
+	struct ast_sip_endpoint *endpoint = NULL;
+	struct ast_sip_contact *contact = NULL;
+	const char *target = trnf_data->target;
+
+	/* See if we have an endpoint; if so, use its contact */
+	endpoint = ast_sorcery_retrieve_by_id(ast_sip_get_sorcery(), "endpoint", target);
+	if (endpoint) {
+		contact = ast_sip_location_retrieve_contact_from_aor_list(endpoint->aors);
+		if (contact && !ast_strlen_zero(contact->uri)) {
+			target = contact->uri;
+		}
+	}
 
 	if (ast_channel_state(trnf_data->session->channel) == AST_STATE_RING) {
-		transfer_redirect(trnf_data->session, trnf_data->target);
+		transfer_redirect(trnf_data->session, target);
 	} else {
-		transfer_refer(trnf_data->session, trnf_data->target);
+		transfer_refer(trnf_data->session, target);
 	}
 
 	ao2_ref(trnf_data, -1);
+	ao2_cleanup(endpoint);
+	ao2_cleanup(contact);
 	return 0;
 }
 

Modified: trunk/include/asterisk/stasis_app.h
URL: http://svnview.digium.com/svn/asterisk/trunk/include/asterisk/stasis_app.h?view=diff&rev=431718&r1=431717&r2=431718
==============================================================================
--- trunk/include/asterisk/stasis_app.h (original)
+++ trunk/include/asterisk/stasis_app.h Thu Feb 12 14:34:37 2015
@@ -486,6 +486,17 @@
 int stasis_app_control_continue(struct stasis_app_control *control, const char *context, const char *extension, int priority);
 
 /*!
+ * \brief Redirect a channel in \c res_stasis to a particular endpoint
+ *
+ * \param control Control for \c res_stasis
+ * \param endpoint The endpoint transfer string where the channel should be sent to
+ *
+ * \return 0 for success
+ * \return -1 for error
+ */
+int stasis_app_control_redirect(struct stasis_app_control *control, const char *endpoint);
+
+/*!
  * \brief Indicate ringing to the channel associated with this control.
  *
  * \param control Control for \c res_stasis.

Modified: trunk/res/ari/resource_channels.c
URL: http://svnview.digium.com/svn/asterisk/trunk/res/ari/resource_channels.c?view=diff&rev=431718&r1=431717&r2=431718
==============================================================================
--- trunk/res/ari/resource_channels.c (original)
+++ trunk/res/ari/resource_channels.c Thu Feb 12 14:34:37 2015
@@ -150,6 +150,64 @@
 
 	if (stasis_app_control_continue(control, context, exten, ipri)) {
 		ast_ari_response_alloc_failed(response);
+		return;
+	}
+
+	ast_ari_response_no_content(response);
+}
+
+void ast_ari_channels_redirect(struct ast_variable *headers,
+	struct ast_ari_channels_redirect_args *args,
+	struct ast_ari_response *response)
+{
+	RAII_VAR(struct stasis_app_control *, control, NULL, ao2_cleanup);
+	RAII_VAR(struct ast_channel_snapshot *, chan_snapshot, NULL, ao2_cleanup);
+	char *tech;
+	char *resource;
+	int tech_len;
+
+	control = find_control(response, args->channel_id);
+	if (!control) {
+		return;
+	}
+
+	if (ast_strlen_zero(args->endpoint)) {
+		ast_ari_response_error(response, 400, "Not Found",
+			"Required parameter 'endpoint' not provided.");
+		return;
+	}
+
+	tech = ast_strdupa(args->endpoint);
+	if (!(resource = strchr(tech, '/')) || !(tech_len = resource - tech)) {
+		ast_ari_response_error(response, 422, "Unprocessable Entity",
+			"Endpoint parameter '%s' does not contain tech/resource", args->endpoint);
+		return;
+	}
+
+	*resource++ = '\0';
+	if (ast_strlen_zero(resource)) {
+		ast_ari_response_error(response, 422, "Unprocessable Entity",
+			"No resource provided in endpoint parameter '%s'", args->endpoint);
+		return;
+	}
+
+	chan_snapshot = ast_channel_snapshot_get_latest(args->channel_id);
+	if (!chan_snapshot) {
+		ast_ari_response_error(response, 500, "Internal Server Error",
+			"Unable to find channel snapshot for '%s'", args->channel_id);
+		return;
+	}
+
+	if (strncasecmp(chan_snapshot->type, tech, tech_len)) {
+		ast_ari_response_error(response, 422, "Unprocessable Entity",
+			"Endpoint technology '%s' does not match channel technology '%s'",
+			tech, chan_snapshot->type);
+		return;
+	}
+
+	if (stasis_app_control_redirect(control, resource)) {
+		ast_ari_response_error(response, 500, "Internal Server Error",
+			"Failed to redirect channel");
 		return;
 	}
 

Modified: trunk/res/ari/resource_channels.h
URL: http://svnview.digium.com/svn/asterisk/trunk/res/ari/resource_channels.h?view=diff&rev=431718&r1=431717&r2=431718
==============================================================================
--- trunk/res/ari/resource_channels.h (original)
+++ trunk/res/ari/resource_channels.h Thu Feb 12 14:34:37 2015
@@ -221,6 +221,32 @@
  * \param[out] response HTTP response
  */
 void ast_ari_channels_continue_in_dialplan(struct ast_variable *headers, struct ast_ari_channels_continue_in_dialplan_args *args, struct ast_ari_response *response);
+/*! Argument struct for ast_ari_channels_redirect() */
+struct ast_ari_channels_redirect_args {
+	/*! Channel's id */
+	const char *channel_id;
+	/*! The endpoint to redirect the channel to */
+	const char *endpoint;
+};
+/*!
+ * \brief Body parsing function for /channels/{channelId}/redirect.
+ * \param body The JSON body from which to parse parameters.
+ * \param[out] args The args structure to parse into.
+ * \retval zero on success
+ * \retval non-zero on failure
+ */
+int ast_ari_channels_redirect_parse_body(
+	struct ast_json *body,
+	struct ast_ari_channels_redirect_args *args);
+
+/*!
+ * \brief Redirect the channel to a different location.
+ *
+ * \param headers HTTP headers
+ * \param args Swagger parameters
+ * \param[out] response HTTP response
+ */
+void ast_ari_channels_redirect(struct ast_variable *headers, struct ast_ari_channels_redirect_args *args, struct ast_ari_response *response);
 /*! Argument struct for ast_ari_channels_answer() */
 struct ast_ari_channels_answer_args {
 	/*! Channel's id */

Modified: trunk/res/res_ari_channels.c
URL: http://svnview.digium.com/svn/asterisk/trunk/res/res_ari_channels.c?view=diff&rev=431718&r1=431717&r2=431718
==============================================================================
--- trunk/res/res_ari_channels.c (original)
+++ trunk/res/res_ari_channels.c Thu Feb 12 14:34:37 2015
@@ -707,6 +707,106 @@
 fin: __attribute__((unused))
 	return;
 }
+int ast_ari_channels_redirect_parse_body(
+	struct ast_json *body,
+	struct ast_ari_channels_redirect_args *args)
+{
+	struct ast_json *field;
+	/* Parse query parameters out of it */
+	field = ast_json_object_get(body, "endpoint");
+	if (field) {
+		args->endpoint = ast_json_string_get(field);
+	}
+	return 0;
+}
+
+/*!
+ * \brief Parameter parsing callback for /channels/{channelId}/redirect.
+ * \param get_params GET parameters in the HTTP request.
+ * \param path_vars Path variables extracted from the request.
+ * \param headers HTTP headers.
+ * \param[out] response Response to the HTTP request.
+ */
+static void ast_ari_channels_redirect_cb(
+	struct ast_tcptls_session_instance *ser,
+	struct ast_variable *get_params, struct ast_variable *path_vars,
+	struct ast_variable *headers, struct ast_ari_response *response)
+{
+	struct ast_ari_channels_redirect_args args = {};
+	struct ast_variable *i;
+	RAII_VAR(struct ast_json *, body, NULL, ast_json_unref);
+#if defined(AST_DEVMODE)
+	int is_valid;
+	int code;
+#endif /* AST_DEVMODE */
+
+	for (i = get_params; i; i = i->next) {
+		if (strcmp(i->name, "endpoint") == 0) {
+			args.endpoint = (i->value);
+		} else
+		{}
+	}
+	for (i = path_vars; i; i = i->next) {
+		if (strcmp(i->name, "channelId") == 0) {
+			args.channel_id = (i->value);
+		} else
+		{}
+	}
+	/* Look for a JSON request entity */
+	body = ast_http_get_json(ser, headers);
+	if (!body) {
+		switch (errno) {
+		case EFBIG:
+			ast_ari_response_error(response, 413, "Request Entity Too Large", "Request body too large");
+			goto fin;
+		case ENOMEM:
+			ast_ari_response_error(response, 500, "Internal Server Error", "Error processing request");
+			goto fin;
+		case EIO:
+			ast_ari_response_error(response, 400, "Bad Request", "Error parsing request body");
+			goto fin;
+		}
+	}
+	if (ast_ari_channels_redirect_parse_body(body, &args)) {
+		ast_ari_response_alloc_failed(response);
+		goto fin;
+	}
+	ast_ari_channels_redirect(headers, &args, response);
+#if defined(AST_DEVMODE)
+	code = response->response_code;
+
+	switch (code) {
+	case 0: /* Implementation is still a stub, or the code wasn't set */
+		is_valid = response->message == NULL;
+		break;
+	case 500: /* Internal Server Error */
+	case 501: /* Not Implemented */
+	case 400: /* Endpoint parameter not provided */
+	case 404: /* Channel or endpoint not found */
+	case 409: /* Channel not in a Stasis application */
+	case 422: /* Endpoint is not the same type as the channel */
+		is_valid = 1;
+		break;
+	default:
+		if (200 <= code && code <= 299) {
+			is_valid = ast_ari_validate_void(
+				response->message);
+		} else {
+			ast_log(LOG_ERROR, "Invalid error response %d for /channels/{channelId}/redirect\n", code);
+			is_valid = 0;
+		}
+	}
+
+	if (!is_valid) {
+		ast_log(LOG_ERROR, "Response validation failed for /channels/{channelId}/redirect\n");
+		ast_ari_response_error(response, 500,
+			"Internal Server Error", "Response validation failed");
+	}
+#endif /* AST_DEVMODE */
+
+fin: __attribute__((unused))
+	return;
+}
 /*!
  * \brief Parameter parsing callback for /channels/{channelId}/answer.
  * \param get_params GET parameters in the HTTP request.
@@ -2457,6 +2557,15 @@
 	.path_segment = "continue",
 	.callbacks = {
 		[AST_HTTP_POST] = ast_ari_channels_continue_in_dialplan_cb,
+	},
+	.num_children = 0,
+	.children = {  }
+};
+/*! \brief REST handler for /api-docs/channels.{format} */
+static struct stasis_rest_handlers channels_channelId_redirect = {
+	.path_segment = "redirect",
+	.callbacks = {
+		[AST_HTTP_POST] = ast_ari_channels_redirect_cb,
 	},
 	.num_children = 0,
 	.children = {  }
@@ -2595,8 +2704,8 @@
 		[AST_HTTP_POST] = ast_ari_channels_originate_with_id_cb,
 		[AST_HTTP_DELETE] = ast_ari_channels_hangup_cb,
 	},
-	.num_children = 12,
-	.children = { &channels_channelId_continue,&channels_channelId_answer,&channels_channelId_ring,&channels_channelId_dtmf,&channels_channelId_mute,&channels_channelId_hold,&channels_channelId_moh,&channels_channelId_silence,&channels_channelId_play,&channels_channelId_record,&channels_channelId_variable,&channels_channelId_snoop, }
+	.num_children = 13,
+	.children = { &channels_channelId_continue,&channels_channelId_redirect,&channels_channelId_answer,&channels_channelId_ring,&channels_channelId_dtmf,&channels_channelId_mute,&channels_channelId_hold,&channels_channelId_moh,&channels_channelId_silence,&channels_channelId_play,&channels_channelId_record,&channels_channelId_variable,&channels_channelId_snoop, }
 };
 /*! \brief REST handler for /api-docs/channels.{format} */
 static struct stasis_rest_handlers channels = {

Modified: trunk/res/res_pjsip_multihomed.c
URL: http://svnview.digium.com/svn/asterisk/trunk/res/res_pjsip_multihomed.c?view=diff&rev=431718&r1=431717&r2=431718
==============================================================================
--- trunk/res/res_pjsip_multihomed.c (original)
+++ trunk/res/res_pjsip_multihomed.c Thu Feb 12 14:34:37 2015
@@ -146,12 +146,15 @@
 	if (tdata->msg->type == PJSIP_REQUEST_MSG || !(cseq = pjsip_msg_find_hdr(tdata->msg, PJSIP_H_CSEQ, NULL)) ||
 		pj_strcmp2(&cseq->method.name, "REGISTER")) {
 		pjsip_contact_hdr *contact = pjsip_msg_find_hdr(tdata->msg, PJSIP_H_CONTACT, NULL);
-		if (contact && (PJSIP_URI_SCHEME_IS_SIP(contact->uri) || PJSIP_URI_SCHEME_IS_SIPS(contact->uri))) {
+		if (contact && (PJSIP_URI_SCHEME_IS_SIP(contact->uri) || PJSIP_URI_SCHEME_IS_SIPS(contact->uri))
+			&& !(tdata->msg->type == PJSIP_RESPONSE_MSG && tdata->msg->line.status.code / 100 == 3)) {
 			pjsip_sip_uri *uri = pjsip_uri_get_uri(contact->uri);
 
 			/* prm.ret_addr is allocated from the tdata pool OR the transport so it is perfectly fine to just do an assignment like this */
 			pj_strassign(&uri->host, &prm.ret_addr);
 			uri->port = prm.ret_port;
+			ast_debug(4, "Re-wrote Contact URI host/port to %.*s:%d\n",
+				(int)pj_strlen(&uri->host), pj_strbuf(&uri->host), uri->port);
 
 			pjsip_tx_data_invalidate_msg(tdata);
 		}

Modified: trunk/res/res_pjsip_nat.c
URL: http://svnview.digium.com/svn/asterisk/trunk/res/res_pjsip_nat.c?view=diff&rev=431718&r1=431717&r2=431718
==============================================================================
--- trunk/res/res_pjsip_nat.c (original)
+++ trunk/res/res_pjsip_nat.c Thu Feb 12 14:34:37 2015
@@ -52,6 +52,8 @@
 			uri->transport_param.slen = 0;
 		}
 		uri->port = rdata->pkt_info.src_port;
+		ast_debug(4, "Re-wrote Contact URI host/port to %.*s:%d\n",
+			(int)pj_strlen(&uri->host), pj_strbuf(&uri->host), uri->port);
 
 		/* rewrite the session target since it may have already been pulled from the contact header */
 		if (dlg && (!dlg->remote.contact
@@ -205,6 +207,7 @@
 		pj_strdup2(tdata->pool, &uri->host, ast_sockaddr_stringify_host(&transport->external_address));
 		if (transport->external_signaling_port) {
 			uri->port = transport->external_signaling_port;
+			ast_debug(4, "Re-wrote Contact URI port to %d\n", uri->port);
 		}
 	}
 

Modified: trunk/res/res_pjsip_transport_websocket.c
URL: http://svnview.digium.com/svn/asterisk/trunk/res/res_pjsip_transport_websocket.c?view=diff&rev=431718&r1=431717&r2=431718
==============================================================================
--- trunk/res/res_pjsip_transport_websocket.c (original)
+++ trunk/res/res_pjsip_transport_websocket.c Thu Feb 12 14:34:37 2015
@@ -329,6 +329,8 @@
 
 		pj_cstr(&uri->host, rdata->pkt_info.src_name);
 		uri->port = rdata->pkt_info.src_port;
+		ast_debug(4, "Re-wrote Contact URI host/port to %.*s:%d\n",
+			(int)pj_strlen(&uri->host), pj_strbuf(&uri->host), uri->port);
 		pj_strdup(rdata->tp_info.pool, &uri->transport_param, (type == (long)transport_type_ws) ? &STR_WS : &STR_WSS);
 	}
 

Modified: trunk/res/stasis/control.c
URL: http://svnview.digium.com/svn/asterisk/trunk/res/stasis/control.c?view=diff&rev=431718&r1=431717&r2=431718
==============================================================================
--- trunk/res/stasis/control.c (original)
+++ trunk/res/stasis/control.c Thu Feb 12 14:34:37 2015
@@ -428,6 +428,38 @@
 	return 0;
 }
 
+static int app_control_redirect(struct stasis_app_control *control,
+	struct ast_channel *chan, void *data)
+{
+	char *endpoint = data;
+	int res;
+
+	ast_assert(control->channel != NULL);
+	ast_assert(endpoint != NULL);
+
+	res = ast_transfer(control->channel, endpoint);
+	if (!res) {
+		ast_log(LOG_NOTICE, "Unsupported transfer requested on channel '%s'\n",
+			ast_channel_name(control->channel));
+		return 0;
+	}
+
+	return 0;
+}
+
+int stasis_app_control_redirect(struct stasis_app_control *control, const char *endpoint)
+{
+	char *endpoint_data = ast_strdup(endpoint);
+
+	if (!endpoint_data) {
+		return -1;
+	}
+
+	stasis_app_send_command_async(control, app_control_redirect, endpoint_data, ast_free_ptr);
+
+	return 0;
+}
+
 struct stasis_app_control_dtmf_data {
 	int before;
 	int between;

Modified: trunk/rest-api/api-docs/channels.json
URL: http://svnview.digium.com/svn/asterisk/trunk/rest-api/api-docs/channels.json?view=diff&rev=431718&r1=431717&r2=431718
==============================================================================
--- trunk/rest-api/api-docs/channels.json (original)
+++ trunk/rest-api/api-docs/channels.json Thu Feb 12 14:34:37 2015
@@ -391,6 +391,54 @@
 						{
 							"code": 409,
 							"reason": "Channel not in a Stasis application"
+						}
+					]
+				}
+			]
+		},
+		{
+			"path": "/channels/{channelId}/redirect",
+			"description": "Inform the channel that it should redirect itself to a different location. Note that this will almost certainly cause the channel to exit the application.",
+			"operations": [
+				{
+					"httpMethod": "POST",
+					"summary": "Redirect the channel to a different location.",
+					"nickname": "redirect",
+					"responseClass": "void",
+					"parameters": [
+						{
+							"name": "channelId",
+							"description": "Channel's id",
+							"paramType": "path",
+							"required": true,
+							"allowMultiple": false,
+							"dataType": "string"
+						},
+						{
+							"name": "endpoint",
+							"description": "The endpoint to redirect the channel to",
+							"paramType": "query",
+							"required": true,
+							"allowMultiple": false,
+							"dataType": "string"
+						}
+					],
+					"errorResponses": [
+						{
+							"code": 400,
+							"reason": "Endpoint parameter not provided"
+						},
+						{
+							"code": 404,
+							"reason": "Channel or endpoint not found"
+						},
+						{
+							"code": 409,
+							"reason": "Channel not in a Stasis application"
+						},
+						{
+							"code": 422,
+							"reason": "Endpoint is not the same type as the channel"
 						}
 					]
 				}




More information about the asterisk-commits mailing list