core/ari/pjsip: Add refer mechanism

This change adds support for refers that are not session based. It
includes a refer implementation for the PJSIP technology which results
in out-of-dialog REFERs being sent to a PJSIP endpoint. These can be
triggered using the new ARI endpoint `/endpoints/refer`.

Resolves: #71

UserNote: There is a new ARI endpoint `/endpoints/refer` for referring
an endpoint to some URI or endpoint.
This commit is contained in:
Maximilian Fridrich
2023-05-10 15:53:33 +02:00
committed by asterisk-org-access-app[bot]
parent d16046e41f
commit 51a7b18038
13 changed files with 2815 additions and 736 deletions

View File

@@ -33,6 +33,7 @@
#include "asterisk/stasis_endpoints.h"
#include "asterisk/channel.h"
#include "asterisk/message.h"
#include "asterisk/refer.h"
void ast_ari_endpoints_list(struct ast_variable *headers,
struct ast_ari_endpoints_list_args *args,
@@ -307,3 +308,135 @@ void ast_ari_endpoints_send_message_to_endpoint(struct ast_variable *headers,
send_message(msg_to, args->from, args->body, variables, response);
ast_variables_destroy(variables);
}
static void send_refer(const char *to, const char *from, const char *refer_to, int to_self, struct ast_variable *variables, struct ast_ari_response *response)
{
struct ast_variable *current;
struct ast_refer *refer;
int res = 0;
if (ast_strlen_zero(to)) {
ast_ari_response_error(response, 400, "Bad Request",
"To must be specified");
return;
}
refer = ast_refer_alloc();
if (!refer) {
ast_ari_response_alloc_failed(response);
return;
}
ast_refer_set_to(refer, "%s", to);
ast_refer_set_to_self(refer, to_self);
if (!ast_strlen_zero(from)) {
ast_refer_set_from(refer, "%s", from);
}
if (!ast_strlen_zero(refer_to)) {
ast_refer_set_refer_to(refer, "%s", refer_to);
}
for (current = variables; current; current = current->next) {
res |= ast_refer_set_var_outbound(refer, current->name, current->value);
}
if (res) {
ast_ari_response_alloc_failed(response);
ast_refer_destroy(refer);
return;
}
if (ast_refer_send(refer)) {
ast_ari_response_error(response, 404, "Not Found",
"Endpoint not found");
return;
}
response->message = ast_json_null();
response->response_code = 202;
response->response_text = "Accepted";
}
static int parse_refer_json(struct ast_json *body,
struct ast_ari_response *response,
struct ast_variable **variables)
{
const char *known_variables[] = { "display_name" };
const char *value;
struct ast_variable *new_var;
struct ast_json *json_variable;
int err = 0;
int i;
if (!body) {
return 0;
}
json_variable = ast_json_object_get(body, "variables");
if (json_variable) {
err = json_to_ast_variables(response, json_variable, variables);
if (err) {
return err;
}
}
for (i = 0; i < sizeof(known_variables) / sizeof(*known_variables); ++i) {
json_variable = ast_json_object_get(body, known_variables[i]);
if (json_variable && ast_json_typeof(json_variable) == AST_JSON_STRING) {
value = ast_json_string_get(json_variable);
new_var = ast_variable_new(known_variables[i], value, "");
if (new_var) {
ast_variable_list_append(variables, new_var);
}
}
}
return err;
}
void ast_ari_endpoints_refer(struct ast_variable *headers,
struct ast_ari_endpoints_refer_args *args,
struct ast_ari_response *response)
{
struct ast_variable *variables = NULL;
ast_ari_endpoints_refer_parse_body(args->variables, args);
if (parse_refer_json(args->variables, response, &variables)) {
return;
}
send_refer(args->to, args->from, args->refer_to, args->to_self, variables, response);
ast_variables_destroy(variables);
}
void ast_ari_endpoints_refer_to_endpoint(struct ast_variable *headers,
struct ast_ari_endpoints_refer_to_endpoint_args *args,
struct ast_ari_response *response)
{
struct ast_variable *variables = NULL;
struct ast_endpoint_snapshot *snapshot;
char to[128];
char *tech = ast_strdupa(args->tech);
/* Really, we just want to know if this thing exists */
snapshot = ast_endpoint_latest_snapshot(args->tech, args->resource);
if (!snapshot) {
ast_ari_response_error(response, 404, "Not Found",
"Endpoint not found");
return;
}
ao2_ref(snapshot, -1);
ast_ari_endpoints_refer_to_endpoint_parse_body(args->variables, args);
if (parse_refer_json(args->variables, response, &variables)) {
return;
}
snprintf(to, sizeof(to), "%s:%s", ast_str_to_lower(tech), args->resource);
send_refer(to, args->from, args->refer_to, args->to_self, variables, response);
ast_variables_destroy(variables);
}

View File

@@ -58,6 +58,7 @@ struct ast_ari_endpoints_send_message_args {
const char *from;
/*! The body of the message */
const char *body;
/*! The "variables" key in the body object holds technology specific key/value pairs to append to the message. These can be interpreted and used by the various resource types; for example, pjsip and sip resource types will add the key/value pairs as SIP headers, */
struct ast_json *variables;
};
/*!
@@ -79,6 +80,38 @@ int ast_ari_endpoints_send_message_parse_body(
* \param[out] response HTTP response
*/
void ast_ari_endpoints_send_message(struct ast_variable *headers, struct ast_ari_endpoints_send_message_args *args, struct ast_ari_response *response);
/*! Argument struct for ast_ari_endpoints_refer() */
struct ast_ari_endpoints_refer_args {
/*! The endpoint resource or technology specific URI that should be referred to somewhere. Valid resource is pjsip. */
const char *to;
/*! The endpoint resource or technology specific identity to refer from. */
const char *from;
/*! The endpoint resource or technology specific URI to refer to. */
const char *refer_to;
/*! If true and "refer_to" refers to an Asterisk endpoint, the "refer_to" value is set to point to this Asterisk endpoint - so the referee is referred to Asterisk. Otherwise, use the contact URI associated with the endpoint. */
int to_self;
/*! The "variables" key in the body object holds technology specific key/value pairs to append to the message. These can be interpreted and used by the various resource types; for example, the pjsip resource type will add the key/value pairs as SIP headers. The "display_name" key is used by the PJSIP technology. Its value will be prepended as a display name to the Refer-To URI. */
struct ast_json *variables;
};
/*!
* \brief Body parsing function for /endpoints/refer.
* \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_endpoints_refer_parse_body(
struct ast_json *body,
struct ast_ari_endpoints_refer_args *args);
/*!
* \brief Refer an endpoint or technology URI to some technology URI or endpoint.
*
* \param headers HTTP headers
* \param args Swagger parameters
* \param[out] response HTTP response
*/
void ast_ari_endpoints_refer(struct ast_variable *headers, struct ast_ari_endpoints_refer_args *args, struct ast_ari_response *response);
/*! Argument struct for ast_ari_endpoints_list_by_tech() */
struct ast_ari_endpoints_list_by_tech_args {
/*! Technology of the endpoints (pjsip,iax2,...) */
@@ -117,6 +150,7 @@ struct ast_ari_endpoints_send_message_to_endpoint_args {
const char *from;
/*! The body of the message */
const char *body;
/*! The "variables" key in the body object holds technology specific key/value pairs to append to the message. These can be interpreted and used by the various resource types; for example, pjsip and sip resource types will add the key/value pairs as SIP headers, */
struct ast_json *variables;
};
/*!
@@ -138,5 +172,39 @@ int ast_ari_endpoints_send_message_to_endpoint_parse_body(
* \param[out] response HTTP response
*/
void ast_ari_endpoints_send_message_to_endpoint(struct ast_variable *headers, struct ast_ari_endpoints_send_message_to_endpoint_args *args, struct ast_ari_response *response);
/*! Argument struct for ast_ari_endpoints_refer_to_endpoint() */
struct ast_ari_endpoints_refer_to_endpoint_args {
/*! Technology of the endpoint */
const char *tech;
/*! ID of the endpoint */
const char *resource;
/*! The endpoint resource or technology specific identity to refer from. */
const char *from;
/*! The endpoint resource or technology specific URI to refer to. */
const char *refer_to;
/*! If true and "refer_to" refers to an Asterisk endpoint, the "refer_to" value is set to point to this Asterisk endpoint - so the referee is referred to Asterisk. Otherwise, use the contact URI associated with the endpoint. */
int to_self;
/*! The "variables" key in the body object holds technology specific key/value pairs to append to the message. These can be interpreted and used by the various resource types; for example, the pjsip resource type will add the key/value pairs as SIP headers, */
struct ast_json *variables;
};
/*!
* \brief Body parsing function for /endpoints/{tech}/{resource}/refer.
* \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_endpoints_refer_to_endpoint_parse_body(
struct ast_json *body,
struct ast_ari_endpoints_refer_to_endpoint_args *args);
/*!
* \brief Refer an endpoint or technology URI to some technology URI or endpoint.
*
* \param headers HTTP headers
* \param args Swagger parameters
* \param[out] response HTTP response
*/
void ast_ari_endpoints_refer_to_endpoint(struct ast_variable *headers, struct ast_ari_endpoints_refer_to_endpoint_args *args, struct ast_ari_response *response);
#endif /* _ASTERISK_RESOURCE_ENDPOINTS_H */