ari/pjsip: Make it possible to control transfers through ARI

Introduce a ChannelTransfer event and the ability to notify progress to
ARI. Implement emitting this event from the PJSIP channel instead of
handling the transfer in Asterisk when configured.

Introduce a dialplan function to the PJSIP channel to switch between the
"core" and "ari-only" behavior.

UserNote: Call transfers on the PJSIP channel can now be controlled by
ARI. This can be enabled by using the PJSIP_TRANSFER_HANDLING(ari-only)
dialplan function.

(cherry picked from commit 5e4fca062c)
This commit is contained in:
Holger Hans Peter Freyther
2024-06-15 16:01:58 +08:00
committed by Asterisk Development Team
parent af966a98a1
commit 6f17f61394
18 changed files with 1462 additions and 6 deletions

View File

@@ -2428,6 +2428,60 @@ ari_validator ast_ari_validate_mailbox_fn(void)
return ast_ari_validate_mailbox;
}
int ast_ari_validate_additional_param(struct ast_json *json)
{
int res = 1;
struct ast_json_iter *iter;
int has_parameter_name = 0;
int has_parameter_value = 0;
for (iter = ast_json_object_iter(json); iter; iter = ast_json_object_iter_next(json, iter)) {
if (strcmp("parameter_name", ast_json_object_iter_key(iter)) == 0) {
int prop_is_valid;
has_parameter_name = 1;
prop_is_valid = ast_ari_validate_string(
ast_json_object_iter_value(iter));
if (!prop_is_valid) {
ast_log(LOG_ERROR, "ARI AdditionalParam field parameter_name failed validation\n");
res = 0;
}
} else
if (strcmp("parameter_value", ast_json_object_iter_key(iter)) == 0) {
int prop_is_valid;
has_parameter_value = 1;
prop_is_valid = ast_ari_validate_string(
ast_json_object_iter_value(iter));
if (!prop_is_valid) {
ast_log(LOG_ERROR, "ARI AdditionalParam field parameter_value failed validation\n");
res = 0;
}
} else
{
ast_log(LOG_ERROR,
"ARI AdditionalParam has undocumented field %s\n",
ast_json_object_iter_key(iter));
res = 0;
}
}
if (!has_parameter_name) {
ast_log(LOG_ERROR, "ARI AdditionalParam missing required field parameter_name\n");
res = 0;
}
if (!has_parameter_value) {
ast_log(LOG_ERROR, "ARI AdditionalParam missing required field parameter_value\n");
res = 0;
}
return res;
}
ari_validator ast_ari_validate_additional_param_fn(void)
{
return ast_ari_validate_additional_param;
}
int ast_ari_validate_application_move_failed(struct ast_json *json)
{
int res = 1;
@@ -4915,6 +4969,126 @@ ari_validator ast_ari_validate_channel_talking_started_fn(void)
return ast_ari_validate_channel_talking_started;
}
int ast_ari_validate_channel_transfer(struct ast_json *json)
{
int res = 1;
struct ast_json_iter *iter;
int has_type = 0;
int has_application = 0;
int has_timestamp = 0;
int has_refer_to = 0;
int has_referred_by = 0;
for (iter = ast_json_object_iter(json); iter; iter = ast_json_object_iter_next(json, iter)) {
if (strcmp("asterisk_id", ast_json_object_iter_key(iter)) == 0) {
int prop_is_valid;
prop_is_valid = ast_ari_validate_string(
ast_json_object_iter_value(iter));
if (!prop_is_valid) {
ast_log(LOG_ERROR, "ARI ChannelTransfer field asterisk_id failed validation\n");
res = 0;
}
} else
if (strcmp("type", ast_json_object_iter_key(iter)) == 0) {
int prop_is_valid;
has_type = 1;
prop_is_valid = ast_ari_validate_string(
ast_json_object_iter_value(iter));
if (!prop_is_valid) {
ast_log(LOG_ERROR, "ARI ChannelTransfer field type failed validation\n");
res = 0;
}
} else
if (strcmp("application", ast_json_object_iter_key(iter)) == 0) {
int prop_is_valid;
has_application = 1;
prop_is_valid = ast_ari_validate_string(
ast_json_object_iter_value(iter));
if (!prop_is_valid) {
ast_log(LOG_ERROR, "ARI ChannelTransfer field application failed validation\n");
res = 0;
}
} else
if (strcmp("timestamp", ast_json_object_iter_key(iter)) == 0) {
int prop_is_valid;
has_timestamp = 1;
prop_is_valid = ast_ari_validate_date(
ast_json_object_iter_value(iter));
if (!prop_is_valid) {
ast_log(LOG_ERROR, "ARI ChannelTransfer field timestamp failed validation\n");
res = 0;
}
} else
if (strcmp("refer_to", ast_json_object_iter_key(iter)) == 0) {
int prop_is_valid;
has_refer_to = 1;
prop_is_valid = ast_ari_validate_refer_to(
ast_json_object_iter_value(iter));
if (!prop_is_valid) {
ast_log(LOG_ERROR, "ARI ChannelTransfer field refer_to failed validation\n");
res = 0;
}
} else
if (strcmp("referred_by", ast_json_object_iter_key(iter)) == 0) {
int prop_is_valid;
has_referred_by = 1;
prop_is_valid = ast_ari_validate_referred_by(
ast_json_object_iter_value(iter));
if (!prop_is_valid) {
ast_log(LOG_ERROR, "ARI ChannelTransfer field referred_by failed validation\n");
res = 0;
}
} else
if (strcmp("state", ast_json_object_iter_key(iter)) == 0) {
int prop_is_valid;
prop_is_valid = ast_ari_validate_string(
ast_json_object_iter_value(iter));
if (!prop_is_valid) {
ast_log(LOG_ERROR, "ARI ChannelTransfer field state failed validation\n");
res = 0;
}
} else
{
ast_log(LOG_ERROR,
"ARI ChannelTransfer has undocumented field %s\n",
ast_json_object_iter_key(iter));
res = 0;
}
}
if (!has_type) {
ast_log(LOG_ERROR, "ARI ChannelTransfer missing required field type\n");
res = 0;
}
if (!has_application) {
ast_log(LOG_ERROR, "ARI ChannelTransfer missing required field application\n");
res = 0;
}
if (!has_timestamp) {
ast_log(LOG_ERROR, "ARI ChannelTransfer missing required field timestamp\n");
res = 0;
}
if (!has_refer_to) {
ast_log(LOG_ERROR, "ARI ChannelTransfer missing required field refer_to\n");
res = 0;
}
if (!has_referred_by) {
ast_log(LOG_ERROR, "ARI ChannelTransfer missing required field referred_by\n");
res = 0;
}
return res;
}
ari_validator ast_ari_validate_channel_transfer_fn(void)
{
return ast_ari_validate_channel_transfer;
}
int ast_ari_validate_channel_unhold(struct ast_json *json)
{
int res = 1;
@@ -5876,6 +6050,9 @@ int ast_ari_validate_event(struct ast_json *json)
if (strcmp("ChannelTalkingStarted", discriminator) == 0) {
return ast_ari_validate_channel_talking_started(json);
} else
if (strcmp("ChannelTransfer", discriminator) == 0) {
return ast_ari_validate_channel_transfer(json);
} else
if (strcmp("ChannelUnhold", discriminator) == 0) {
return ast_ari_validate_channel_unhold(json);
} else
@@ -6083,6 +6260,9 @@ int ast_ari_validate_message(struct ast_json *json)
if (strcmp("ChannelTalkingStarted", discriminator) == 0) {
return ast_ari_validate_channel_talking_started(json);
} else
if (strcmp("ChannelTransfer", discriminator) == 0) {
return ast_ari_validate_channel_transfer(json);
} else
if (strcmp("ChannelUnhold", discriminator) == 0) {
return ast_ari_validate_channel_unhold(json);
} else
@@ -7006,6 +7186,177 @@ ari_validator ast_ari_validate_recording_started_fn(void)
return ast_ari_validate_recording_started;
}
int ast_ari_validate_refer_to(struct ast_json *json)
{
int res = 1;
struct ast_json_iter *iter;
int has_requested_destination = 0;
for (iter = ast_json_object_iter(json); iter; iter = ast_json_object_iter_next(json, iter)) {
if (strcmp("bridge", ast_json_object_iter_key(iter)) == 0) {
int prop_is_valid;
prop_is_valid = ast_ari_validate_bridge(
ast_json_object_iter_value(iter));
if (!prop_is_valid) {
ast_log(LOG_ERROR, "ARI ReferTo field bridge failed validation\n");
res = 0;
}
} else
if (strcmp("connected_channel", ast_json_object_iter_key(iter)) == 0) {
int prop_is_valid;
prop_is_valid = ast_ari_validate_channel(
ast_json_object_iter_value(iter));
if (!prop_is_valid) {
ast_log(LOG_ERROR, "ARI ReferTo field connected_channel failed validation\n");
res = 0;
}
} else
if (strcmp("destination_channel", ast_json_object_iter_key(iter)) == 0) {
int prop_is_valid;
prop_is_valid = ast_ari_validate_channel(
ast_json_object_iter_value(iter));
if (!prop_is_valid) {
ast_log(LOG_ERROR, "ARI ReferTo field destination_channel failed validation\n");
res = 0;
}
} else
if (strcmp("requested_destination", ast_json_object_iter_key(iter)) == 0) {
int prop_is_valid;
has_requested_destination = 1;
prop_is_valid = ast_ari_validate_required_destination(
ast_json_object_iter_value(iter));
if (!prop_is_valid) {
ast_log(LOG_ERROR, "ARI ReferTo field requested_destination failed validation\n");
res = 0;
}
} else
{
ast_log(LOG_ERROR,
"ARI ReferTo has undocumented field %s\n",
ast_json_object_iter_key(iter));
res = 0;
}
}
if (!has_requested_destination) {
ast_log(LOG_ERROR, "ARI ReferTo missing required field requested_destination\n");
res = 0;
}
return res;
}
ari_validator ast_ari_validate_refer_to_fn(void)
{
return ast_ari_validate_refer_to;
}
int ast_ari_validate_referred_by(struct ast_json *json)
{
int res = 1;
struct ast_json_iter *iter;
int has_source_channel = 0;
for (iter = ast_json_object_iter(json); iter; iter = ast_json_object_iter_next(json, iter)) {
if (strcmp("bridge", ast_json_object_iter_key(iter)) == 0) {
int prop_is_valid;
prop_is_valid = ast_ari_validate_bridge(
ast_json_object_iter_value(iter));
if (!prop_is_valid) {
ast_log(LOG_ERROR, "ARI ReferredBy field bridge failed validation\n");
res = 0;
}
} else
if (strcmp("connected_channel", ast_json_object_iter_key(iter)) == 0) {
int prop_is_valid;
prop_is_valid = ast_ari_validate_channel(
ast_json_object_iter_value(iter));
if (!prop_is_valid) {
ast_log(LOG_ERROR, "ARI ReferredBy field connected_channel failed validation\n");
res = 0;
}
} else
if (strcmp("source_channel", ast_json_object_iter_key(iter)) == 0) {
int prop_is_valid;
has_source_channel = 1;
prop_is_valid = ast_ari_validate_channel(
ast_json_object_iter_value(iter));
if (!prop_is_valid) {
ast_log(LOG_ERROR, "ARI ReferredBy field source_channel failed validation\n");
res = 0;
}
} else
{
ast_log(LOG_ERROR,
"ARI ReferredBy has undocumented field %s\n",
ast_json_object_iter_key(iter));
res = 0;
}
}
if (!has_source_channel) {
ast_log(LOG_ERROR, "ARI ReferredBy missing required field source_channel\n");
res = 0;
}
return res;
}
ari_validator ast_ari_validate_referred_by_fn(void)
{
return ast_ari_validate_referred_by;
}
int ast_ari_validate_required_destination(struct ast_json *json)
{
int res = 1;
struct ast_json_iter *iter;
for (iter = ast_json_object_iter(json); iter; iter = ast_json_object_iter_next(json, iter)) {
if (strcmp("additional_protocol_params", ast_json_object_iter_key(iter)) == 0) {
int prop_is_valid;
prop_is_valid = ast_ari_validate_list(
ast_json_object_iter_value(iter),
ast_ari_validate_additional_param);
if (!prop_is_valid) {
ast_log(LOG_ERROR, "ARI RequiredDestination field additional_protocol_params failed validation\n");
res = 0;
}
} else
if (strcmp("destination", ast_json_object_iter_key(iter)) == 0) {
int prop_is_valid;
prop_is_valid = ast_ari_validate_string(
ast_json_object_iter_value(iter));
if (!prop_is_valid) {
ast_log(LOG_ERROR, "ARI RequiredDestination field destination failed validation\n");
res = 0;
}
} else
if (strcmp("protocol_id", ast_json_object_iter_key(iter)) == 0) {
int prop_is_valid;
prop_is_valid = ast_ari_validate_string(
ast_json_object_iter_value(iter));
if (!prop_is_valid) {
ast_log(LOG_ERROR, "ARI RequiredDestination field protocol_id failed validation\n");
res = 0;
}
} else
{
ast_log(LOG_ERROR,
"ARI RequiredDestination has undocumented field %s\n",
ast_json_object_iter_key(iter));
res = 0;
}
}
return res;
}
ari_validator ast_ari_validate_required_destination_fn(void)
{
return ast_ari_validate_required_destination;
}
int ast_ari_validate_stasis_end(struct ast_json *json)
{
int res = 1;

View File

@@ -571,6 +571,22 @@ int ast_ari_validate_mailbox(struct ast_json *json);
*/
ari_validator ast_ari_validate_mailbox_fn(void);
/*!
* \brief Validator for AdditionalParam.
*
* Protocol specific additional parameter
*
* \param json JSON object to validate.
* \retval True (non-zero) if valid.
* \retval False (zero) if invalid.
*/
int ast_ari_validate_additional_param(struct ast_json *json);
/*!
* \brief Function pointer to ast_ari_validate_additional_param().
*/
ari_validator ast_ari_validate_additional_param_fn(void);
/*!
* \brief Validator for ApplicationMoveFailed.
*
@@ -911,6 +927,22 @@ int ast_ari_validate_channel_talking_started(struct ast_json *json);
*/
ari_validator ast_ari_validate_channel_talking_started_fn(void);
/*!
* \brief Validator for ChannelTransfer.
*
* transfer on a channel.
*
* \param json JSON object to validate.
* \retval True (non-zero) if valid.
* \retval False (zero) if invalid.
*/
int ast_ari_validate_channel_transfer(struct ast_json *json);
/*!
* \brief Function pointer to ast_ari_validate_channel_transfer().
*/
ari_validator ast_ari_validate_channel_transfer_fn(void);
/*!
* \brief Validator for ChannelUnhold.
*
@@ -1215,6 +1247,54 @@ int ast_ari_validate_recording_started(struct ast_json *json);
*/
ari_validator ast_ari_validate_recording_started_fn(void);
/*!
* \brief Validator for ReferTo.
*
* transfer destination requested by transferee
*
* \param json JSON object to validate.
* \retval True (non-zero) if valid.
* \retval False (zero) if invalid.
*/
int ast_ari_validate_refer_to(struct ast_json *json);
/*!
* \brief Function pointer to ast_ari_validate_refer_to().
*/
ari_validator ast_ari_validate_refer_to_fn(void);
/*!
* \brief Validator for ReferredBy.
*
* transfer destination requested by transferee
*
* \param json JSON object to validate.
* \retval True (non-zero) if valid.
* \retval False (zero) if invalid.
*/
int ast_ari_validate_referred_by(struct ast_json *json);
/*!
* \brief Function pointer to ast_ari_validate_referred_by().
*/
ari_validator ast_ari_validate_referred_by_fn(void);
/*!
* \brief Validator for RequiredDestination.
*
* Information about the requested destination
*
* \param json JSON object to validate.
* \retval True (non-zero) if valid.
* \retval False (zero) if invalid.
*/
int ast_ari_validate_required_destination(struct ast_json *json);
/*!
* \brief Function pointer to ast_ari_validate_required_destination().
*/
ari_validator ast_ari_validate_required_destination_fn(void);
/*!
* \brief Validator for StasisEnd.
*
@@ -1441,6 +1521,9 @@ ari_validator ast_ari_validate_application_fn(void);
* - name: string (required)
* - new_messages: int (required)
* - old_messages: int (required)
* AdditionalParam
* - parameter_name: string (required)
* - parameter_value: string (required)
* ApplicationMoveFailed
* - asterisk_id: string
* - type: string (required)
@@ -1606,6 +1689,14 @@ ari_validator ast_ari_validate_application_fn(void);
* - application: string (required)
* - timestamp: Date (required)
* - channel: Channel (required)
* ChannelTransfer
* - asterisk_id: string
* - type: string (required)
* - application: string (required)
* - timestamp: Date (required)
* - refer_to: ReferTo (required)
* - referred_by: ReferredBy (required)
* - state: string
* ChannelUnhold
* - asterisk_id: string
* - type: string (required)
@@ -1726,6 +1817,19 @@ ari_validator ast_ari_validate_application_fn(void);
* - application: string (required)
* - timestamp: Date (required)
* - recording: LiveRecording (required)
* ReferTo
* - bridge: Bridge
* - connected_channel: Channel
* - destination_channel: Channel
* - requested_destination: RequiredDestination (required)
* ReferredBy
* - bridge: Bridge
* - connected_channel: Channel
* - source_channel: Channel (required)
* RequiredDestination
* - additional_protocol_params: List[AdditionalParam]
* - destination: string
* - protocol_id: string
* StasisEnd
* - asterisk_id: string
* - type: string (required)

View File

@@ -2252,3 +2252,44 @@ void ast_ari_channels_external_media(struct ast_variable *headers,
"The encapsulation and/or transport is not supported");
}
}
void ast_ari_channels_transfer_progress(struct ast_variable *headers, struct ast_ari_channels_transfer_progress_args *args, struct ast_ari_response *response)
{
enum ast_control_transfer message;
RAII_VAR(struct stasis_app_control *, control, NULL, ao2_cleanup);
RAII_VAR(struct ast_channel *, chan, NULL, ast_channel_cleanup);
control = find_control(response, args->channel_id);
if (control == NULL) {
/* Response filled in by find_control */
return;
}
chan = ast_channel_get_by_name(args->channel_id);
if (!chan) {
ast_ari_response_error(response, 404, "Not Found",
"Callee not found");
return;
}
if (ast_strlen_zero(args->states)) {
ast_ari_response_error(response, 400, "Bad Request", "states must not be empty");
return;
}
if (strcasecmp(args->states, "channel_progress") == 0) {
message = AST_TRANSFER_PROGRESS;
} else if (strcasecmp(args->states, "channel_answered") == 0) {
message = AST_TRANSFER_SUCCESS;
} else if (strcasecmp(args->states, "channel_unavailable") == 0) {
message = AST_TRANSFER_UNAVAILABLE;
} else if (strcasecmp(args->states, "channel_declined") == 0) {
message = AST_TRANSFER_FAILED;
} else {
ast_ari_response_error(response, 400, "Bad Request", "Invalid states value");
return;
}
ast_indicate_data(chan, AST_CONTROL_TRANSFER, &message, sizeof(message));
ast_ari_response_no_content(response);
}

View File

@@ -870,5 +870,31 @@ int ast_ari_channels_external_media_parse_body(
* \param[out] response HTTP response
*/
void ast_ari_channels_external_media(struct ast_variable *headers, struct ast_ari_channels_external_media_args *args, struct ast_ari_response *response);
/*! Argument struct for ast_ari_channels_transfer_progress() */
struct ast_ari_channels_transfer_progress_args {
/*! Channel's id */
const char *channel_id;
/*! The state of the progress */
const char *states;
};
/*!
* \brief Body parsing function for /channels/{channelId}/transfer_progress.
* \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_transfer_progress_parse_body(
struct ast_json *body,
struct ast_ari_channels_transfer_progress_args *args);
/*!
* \brief Inform the channel about the progress of the attended/blind transfer.
*
* \param headers HTTP headers
* \param args Swagger parameters
* \param[out] response HTTP response
*/
void ast_ari_channels_transfer_progress(struct ast_variable *headers, struct ast_ari_channels_transfer_progress_args *args, struct ast_ari_response *response);
#endif /* _ASTERISK_RESOURCE_CHANNELS_H */

View File

@@ -2999,6 +2999,92 @@ static void ast_ari_channels_external_media_cb(
}
#endif /* AST_DEVMODE */
fin: __attribute__((unused))
return;
}
int ast_ari_channels_transfer_progress_parse_body(
struct ast_json *body,
struct ast_ari_channels_transfer_progress_args *args)
{
struct ast_json *field;
/* Parse query parameters out of it */
field = ast_json_object_get(body, "states");
if (field) {
args->states = ast_json_string_get(field);
}
return 0;
}
/*!
* \brief Parameter parsing callback for /channels/{channelId}/transfer_progress.
* \param ser TCP/TLS session object
* \param get_params GET parameters in the HTTP request.
* \param path_vars Path variables extracted from the request.
* \param headers HTTP headers.
* \param body
* \param[out] response Response to the HTTP request.
*/
static void ast_ari_channels_transfer_progress_cb(
struct ast_tcptls_session_instance *ser,
struct ast_variable *get_params, struct ast_variable *path_vars,
struct ast_variable *headers, struct ast_json *body, struct ast_ari_response *response)
{
struct ast_ari_channels_transfer_progress_args args = {};
struct ast_variable *i;
#if defined(AST_DEVMODE)
int is_valid;
int code;
#endif /* AST_DEVMODE */
for (i = get_params; i; i = i->next) {
if (strcmp(i->name, "states") == 0) {
args.states = (i->value);
} else
{}
}
for (i = path_vars; i; i = i->next) {
if (strcmp(i->name, "channelId") == 0) {
args.channel_id = (i->value);
} else
{}
}
if (ast_ari_channels_transfer_progress_parse_body(body, &args)) {
ast_ari_response_alloc_failed(response);
goto fin;
}
ast_ari_channels_transfer_progress(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 412: /* Channel in invalid state */
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}/transfer_progress\n", code);
is_valid = 0;
}
}
if (!is_valid) {
ast_log(LOG_ERROR, "Response validation failed for /channels/{channelId}/transfer_progress\n");
ast_ari_response_error(response, 500,
"Internal Server Error", "Response validation failed");
}
#endif /* AST_DEVMODE */
fin: __attribute__((unused))
return;
}
@@ -3183,6 +3269,15 @@ static struct stasis_rest_handlers channels_channelId_rtp_statistics = {
.children = { }
};
/*! \brief REST handler for /api-docs/channels.json */
static struct stasis_rest_handlers channels_channelId_transfer_progress = {
.path_segment = "transfer_progress",
.callbacks = {
[AST_HTTP_POST] = ast_ari_channels_transfer_progress_cb,
},
.num_children = 0,
.children = { }
};
/*! \brief REST handler for /api-docs/channels.json */
static struct stasis_rest_handlers channels_channelId = {
.path_segment = "channelId",
.is_wildcard = 1,
@@ -3191,8 +3286,8 @@ static struct stasis_rest_handlers channels_channelId = {
[AST_HTTP_POST] = ast_ari_channels_originate_with_id_cb,
[AST_HTTP_DELETE] = ast_ari_channels_hangup_cb,
},
.num_children = 16,
.children = { &channels_channelId_continue,&channels_channelId_move,&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,&channels_channelId_dial,&channels_channelId_rtp_statistics, }
.num_children = 17,
.children = { &channels_channelId_continue,&channels_channelId_move,&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,&channels_channelId_dial,&channels_channelId_rtp_statistics,&channels_channelId_transfer_progress, }
};
/*! \brief REST handler for /api-docs/channels.json */
static struct stasis_rest_handlers channels_externalMedia = {

View File

@@ -44,6 +44,22 @@
static struct ast_taskprocessor *refer_serializer;
static pj_status_t refer_on_tx_request(pjsip_tx_data *tdata);
static int defer_termination_cancel_task(void *data);
struct transfer_ari_state {
/*! \brief A deferred session used by the ARI only mode */
struct ast_sip_session *transferer;
struct ast_channel *transferer_chan;
struct ast_sip_session *other_session;
char exten[AST_MAX_EXTENSION];
char *referred_by;
char *protocol_id;
struct ast_refer_params *params;
int last_response;
};
/*! \brief REFER Progress structure */
struct refer_progress {
@@ -69,6 +85,8 @@ struct refer_progress {
int sent_100;
/*! \brief Whether to notifies all the progress details on blind transfer */
unsigned int refer_blind_progress;
/*! \brief State related to the transfer in ARI only mode */
struct transfer_ari_state *ari_state;
};
/*! \brief REFER Progress notification structure */
@@ -87,6 +105,30 @@ static pjsip_module refer_progress_module = {
.id = -1,
};
/*! \brief Destructor of the state used for the ARI transfer */
static void transfer_ari_state_destroy(void *obj)
{
struct transfer_ari_state *state = obj;
ao2_cleanup(state->transferer);
ao2_cleanup(state->other_session);
ast_channel_cleanup(state->transferer_chan);
ast_free(state->referred_by);
ast_free(state->protocol_id);
ao2_cleanup(state->params);
}
static void refer_params_destroy(void *obj)
{
struct ast_refer_params *params = obj;
for (int i = 0; i < AST_VECTOR_SIZE(params); ++i) {
struct ast_refer_param param = AST_VECTOR_GET(params, i);
ast_free((char *) param.param_name);
ast_free((char *) param.param_value);
}
}
/*! \brief Destructor for REFER Progress notification structure */
static void refer_progress_notification_destroy(void *obj)
{
@@ -95,6 +137,14 @@ static void refer_progress_notification_destroy(void *obj)
ao2_cleanup(notification->progress);
}
static int ari_notify(struct transfer_ari_state *state)
{
return ast_refer_notify_transfer_request(state->transferer_chan, state->referred_by,
state->exten, state->protocol_id,
state->other_session ? state->other_session->channel : NULL,
state->params, state->last_response);
}
/*! \brief Allocator for REFER Progress notification structure */
static struct refer_progress_notification *refer_progress_notification_alloc(struct refer_progress *progress, int response,
pjsip_evsub_state state)
@@ -178,6 +228,18 @@ static int refer_progress_notify(void *data)
pjsip_xfer_send_request(sub, tdata);
}
if (notification->progress->ari_state) {
struct transfer_ari_state *ari_state = notification->progress->ari_state;
if (ari_state->transferer && notification->state == PJSIP_EVSUB_STATE_TERMINATED) {
if (!ast_sip_push_task(ari_state->transferer->serializer, defer_termination_cancel_task, ari_state->transferer)) {
/* Gave the ref to the pushed task. */
ari_state->transferer = NULL;
}
}
ari_notify(ari_state);
}
pjsip_dlg_dec_lock(notification->progress->dlg);
return 0;
@@ -298,6 +360,58 @@ static struct ast_frame *refer_progress_framehook(struct ast_channel *chan, stru
return f;
}
/*! \brief Progress monitoring frame hook - examines frames to determine state of transfer. Used for the ari-only mode */
static struct ast_frame *refer_ari_progress_framehook(struct ast_channel *chan, struct ast_frame *f, enum ast_framehook_event event, void *data)
{
struct refer_progress *progress = data;
struct refer_progress_notification *notification = NULL;
/* We only care about frames *to* the channel */
if (!f || (event != AST_FRAMEHOOK_EVENT_WRITE)) {
return f;
}
/* Determine the state of the REFER based on the control frames (or voice frames) passing */
if (f->frametype == AST_FRAME_CONTROL
&& f->subclass.integer == AST_CONTROL_TRANSFER
&& f->datalen >= sizeof(enum ast_control_transfer)) {
enum ast_control_transfer *message = f->data.ptr;
switch (*message) {
case AST_TRANSFER_FAILED:
notification = refer_progress_notification_alloc(progress, 603, PJSIP_EVSUB_STATE_TERMINATED);
break;
case AST_TRANSFER_SUCCESS:
notification = refer_progress_notification_alloc(progress, 200, PJSIP_EVSUB_STATE_TERMINATED);
break;
case AST_TRANSFER_PROGRESS:
notification = refer_progress_notification_alloc(progress, 100, PJSIP_EVSUB_STATE_ACTIVE);
break;
case AST_TRANSFER_UNAVAILABLE:
notification = refer_progress_notification_alloc(progress, 503, PJSIP_EVSUB_STATE_TERMINATED);
break;
case AST_TRANSFER_INVALID:
break;
}
progress->ari_state->last_response = *message;
}
/* If a notification is due to be sent push it to the thread pool */
if (notification) {
/* If the subscription is being terminated we don't need the frame hook any longer */
if (notification->state == PJSIP_EVSUB_STATE_TERMINATED) {
ast_debug(3, "Detaching REFER progress monitoring hook from '%s' as subscription is being terminated\n",
ast_channel_name(chan));
ast_framehook_detach(chan, progress->framehook);
}
if (ast_sip_push_task(progress->serializer, refer_progress_notify, notification)) {
ao2_cleanup(notification);
}
}
return f;
}
/*! \brief Destroy callback for monitoring framehook */
static void refer_progress_framehook_destroy(void *data)
{
@@ -378,6 +492,7 @@ static void refer_progress_destroy(void *obj)
}
ao2_cleanup(progress->transfer_data);
ao2_cleanup(progress->ari_state);
ast_free(progress->transferee);
ast_taskprocessor_unreference(progress->serializer);
@@ -1339,6 +1454,169 @@ static const struct ast_refer_tech refer_tech = {
.refer_send = sip_refer_send,
};
static char *copy_string(struct pj_str_t *str)
{
int len = pj_strlen(str) + 1;
char *dst = ast_malloc(len);
if (!dst) {
return NULL;
}
ast_copy_pj_str(dst, str, len);
return dst;
}
static int add_refer_param(struct ast_refer_params *params, const char *key, struct pj_str_t *str)
{
struct ast_refer_param param;
param.param_name = ast_strdup(key);
if (!param.param_name) {
return 0;
}
param.param_value = copy_string(str);
if (!param.param_value) {
ast_free((char *) param.param_name);
return 0;
}
if (AST_VECTOR_APPEND(params, param) != 0) {
ast_free((char *) param.param_name);
ast_free((char *) param.param_value);
return 0;
}
return 1;
}
static int refer_incoming_ari_request(struct ast_sip_session *session, pjsip_rx_data *rdata, pjsip_sip_uri *target,
pjsip_param *replaces_param, struct refer_progress *progress)
{
int parsed_len;
pjsip_replaces_hdr *replaces;
pjsip_generic_string_hdr *referred_hdr;
RAII_VAR(struct transfer_ari_state *, state, NULL, ao2_cleanup);
struct ast_framehook_interface hook = {
.version = AST_FRAMEHOOK_INTERFACE_VERSION,
.event_cb = refer_ari_progress_framehook,
.destroy_cb = refer_progress_framehook_destroy,
.data = progress,
.disable_inheritance = 1,
};
static const pj_str_t str_referred_by = { "Referred-By", 11 };
static const pj_str_t str_referred_by_s = { "b", 1 };
static const pj_str_t str_replaces = { "Replaces", 8 };
state = ao2_alloc(sizeof(struct transfer_ari_state), transfer_ari_state_destroy);
if (!state) {
return 500;
}
state->last_response = AST_TRANSFER_INVALID;
state->params = ao2_alloc(sizeof(struct ast_refer_params), refer_params_destroy);
if (!state->params) {
return 500;
}
AST_VECTOR_INIT(state->params, 0);
ast_channel_ref(session->channel);
state->transferer_chan = session->channel;
/* Using the user portion of the target URI see if it exists as a valid extension in their context */
ast_copy_pj_str(state->exten, &target->user, sizeof(state->exten));
/*
* We may want to match in the dialplan without any user
* options getting in the way.
*/
AST_SIP_USER_OPTIONS_TRUNCATE_CHECK(state->exten);
referred_hdr = pjsip_msg_find_hdr_by_names(rdata->msg_info.msg,
&str_referred_by, &str_referred_by_s, NULL);
if (referred_hdr) {
state->referred_by = copy_string(&referred_hdr->hvalue);
if (!state->referred_by) {
return 500;
}
}
if (replaces_param) {
pjsip_dialog *dlg;
pj_str_t replaces_content = { 0, };
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))) {
ast_log(LOG_ERROR, "Received REFER request on channel '%s' from endpoint '%s' with invalid Replaces header, rejecting\n",
ast_channel_name(session->channel), ast_sorcery_object_get_id(session->endpoint));
return 400;
}
dlg = pjsip_ua_find_dialog(&replaces->call_id, &replaces->to_tag, &replaces->from_tag, PJ_TRUE);
if (dlg) {
state->other_session = ast_sip_dialog_get_session(dlg);
pjsip_dlg_dec_lock(dlg);
}
state->protocol_id = copy_string(&replaces->call_id);
if (!state->protocol_id) {
return 500;
}
if (!add_refer_param(state->params, "from", &replaces->from_tag)) {
return 500;
}
if (!add_refer_param(state->params, "to", &replaces->to_tag)) {
return 500;
}
}
ao2_ref(session, +1);
if (ast_sip_session_defer_termination(session)) {
ast_log(LOG_ERROR, "Channel '%s' from endpoint '%s' attempted ari-only transfer but could not defer termination, rejecting\n",
ast_channel_name(session->channel),
ast_sorcery_object_get_id(session->endpoint));
ao2_cleanup(session);
return 500;
}
state->transferer = session;
/* We need to bump the reference count up on the progress structure since it is in the frame hook now */
ao2_ref(progress, +1);
ast_channel_lock(session->channel);
progress->framehook = ast_framehook_attach(session->channel, &hook);
ast_channel_unlock(session->channel);
if (progress->framehook < 0) {
ao2_cleanup(progress);
return 500;
}
if (ari_notify(state)) {
ast_channel_lock(session->channel);
ast_framehook_detach(session->channel, progress->framehook);
progress->framehook = -1;
ao2_cleanup(progress);
ast_channel_unlock(session->channel);
return 500;
}
/* Transfer ownership to the progress */
progress->ari_state = state;
state = NULL;
return 200;
}
static int refer_incoming_attended_request(struct ast_sip_session *session, pjsip_rx_data *rdata, pjsip_sip_uri *target_uri,
pjsip_param *replaces_param, struct refer_progress *progress)
{
@@ -1710,9 +1988,15 @@ static int refer_incoming_refer_request(struct ast_sip_session *session, struct
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))) {
replaces = pjsip_param_find(&target_uri->header_param, &str_replaces);
if (!replaces) {
replaces = pjsip_param_find(&target_uri->other_param, &str_replaces);
}
/* Determine if this is handled externally or an attended or blind transfer */
if (session->transferhandling_ari) {
response = refer_incoming_ari_request(session, rdata, target_uri, replaces, progress);
} else if (replaces) {
response = refer_incoming_attended_request(session, rdata, target_uri, replaces, progress);
} else {
response = refer_incoming_blind_request(session, rdata, target_uri, progress);