From afc37f2986bfec4aa62ef085935c40e6d278c864 Mon Sep 17 00:00:00 2001 From: Sean Bright Date: Tue, 16 Mar 2021 12:19:48 -0400 Subject: [PATCH] WIP Change-Id: Icd4222176269228309559aea8aa287a080f6bacd --- res/res_pjsip_dialog_info_body_generator.c | 92 +++++++++++++++++++++- 1 file changed, 91 insertions(+), 1 deletion(-) diff --git a/res/res_pjsip_dialog_info_body_generator.c b/res/res_pjsip_dialog_info_body_generator.c index 3934a8a0fb..2ee919b1f3 100644 --- a/res/res_pjsip_dialog_info_body_generator.c +++ b/res/res_pjsip_dialog_info_body_generator.c @@ -30,6 +30,7 @@ #include #include "asterisk/module.h" +#include "asterisk/callerid.h" #include "asterisk/res_pjsip.h" #include "asterisk/res_pjsip_pubsub.h" #include "asterisk/res_pjsip_presence_xml.h" @@ -60,20 +61,56 @@ static void *dialog_info_allocate_body(void *data) return ast_sip_presence_xml_create_node(state_data->pool, NULL, "dialog-info"); } +/*! + * \internal + * \brief Find the channel that is causing the RINGING update, ref'd + */ +static struct ast_channel *find_ringing_channel(struct ao2_container *device_state_info) +{ + struct ao2_iterator citer; + struct ast_device_state_info *device_state; + struct ast_channel *c = NULL; + struct timeval tv = {0,}; + + /* iterate ringing devices and get the oldest of all causing channels */ + citer = ao2_iterator_init(device_state_info, 0); + for (; (device_state = ao2_iterator_next(&citer)); ao2_ref(device_state, -1)) { + if (!device_state->causing_channel || (device_state->device_state != AST_DEVICE_RINGING && + device_state->device_state != AST_DEVICE_RINGINUSE)) { + continue; + } + ast_channel_lock(device_state->causing_channel); + if (ast_tvzero(tv) || ast_tvcmp(ast_channel_creationtime(device_state->causing_channel), tv) < 0) { + c = device_state->causing_channel; + tv = ast_channel_creationtime(c); + } + ast_channel_unlock(device_state->causing_channel); + } + ao2_iterator_destroy(&citer); + return c ? ast_channel_ref(c) : NULL; +} + static int dialog_info_generate_body_content(void *body, void *data) { pj_xml_node *dialog_info = body, *dialog, *state; + pj_xml_node *remote_node, *local_node, *remote_identity_node; + pj_xml_node *remote_target_node, *local_identity_node, *local_target_node; struct ast_datastore *datastore; struct dialog_info_xml_state *datastore_state; struct ast_sip_exten_state_data *state_data = data; char *local = ast_strdupa(state_data->local), *stripped, *statestring = NULL; + char *remote = ast_strdupa(state_data->remote), *stripped_remote; char *pidfstate = NULL, *pidfnote = NULL; enum ast_sip_pidf_state local_state; char version_str[32], sanitized[PJSIP_MAX_URL_SIZE]; + char remote_target[PJSIP_MAX_URL_SIZE], sanitized_remote[PJSIP_MAX_URL_SIZE]; struct ast_sip_endpoint *endpoint = NULL; unsigned int notify_early_inuse_ringing = 0; + struct ast_channel *callee; + const char *from_domain = NULL; + static char *invalid = "anonymous.invalid"; - if (!local || !state_data->datastores) { + if (!local || !remote || !state_data->datastores) { return -1; } @@ -111,8 +148,12 @@ static int dialog_info_generate_body_content(void *body, void *data) stripped = ast_strip_quoted(local, "<", ">"); ast_sip_sanitize_xml(stripped, sanitized, sizeof(sanitized)); + stripped_remote = ast_strip_quoted(remote, "<", ">"); + ast_sip_sanitize_xml(stripped_remote, sanitized_remote, sizeof(sanitized_remote)); + if (state_data->sub && (endpoint = ast_sip_subscription_get_endpoint(state_data->sub))) { notify_early_inuse_ringing = endpoint->notify_early_inuse_ringing; + from_domain = endpoint ? (!ast_strlen_zero(endpoint->fromdomain) ? endpoint->fromdomain : invalid) : NULL; ao2_cleanup(endpoint); } ast_sip_presence_exten_state_to_str(state_data->exten_state, &statestring, @@ -134,6 +175,55 @@ static int dialog_info_generate_body_content(void *body, void *data) ast_sip_presence_xml_create_attr(state_data->pool, dialog, "id", state_data->exten); if (!ast_strlen_zero(statestring) && !strcmp(statestring, "early")) { ast_sip_presence_xml_create_attr(state_data->pool, dialog, "direction", "recipient"); + + callee = find_ringing_channel(state_data->device_state_info); + + if (callee) { + static char *anonymous = "anonymous"; + char *cid_name; + char *connected_num; + int need; + int connected_num_restricted; + + remote_node = ast_sip_presence_xml_create_node(state_data->pool, dialog, "remote"); + remote_identity_node = ast_sip_presence_xml_create_node(state_data->pool, remote_node, "identity"); + remote_target_node = ast_sip_presence_xml_create_node(state_data->pool, remote_node, "target"); + + ast_debug(3, "From Domain remote URI domain '%s'\n", sanitized_remote); + + ast_channel_lock(callee); + + cid_name = S_COR(ast_channel_connected(callee)->id.name.valid, + S_COR((ast_channel_connected(callee)->id.name.presentation & + AST_PRES_RESTRICTION) == AST_PRES_RESTRICTED, anonymous, + ast_channel_connected(callee)->id.name.str), ""); + + connected_num_restricted = (ast_channel_connected(callee)->id.number.presentation & + AST_PRES_RESTRICTION) == AST_PRES_RESTRICTED; + connected_num = S_COR(ast_channel_connected(callee)->id.number.valid, + S_COR(connected_num_restricted, anonymous, + ast_channel_connected(callee)->id.number.str), ""); + + need = strlen(connected_num) + (connected_num_restricted ? strlen(invalid) : + strlen(from_domain)) + sizeof("sip:@"); + + snprintf(remote_target, need, "sip:%s@%s", connected_num, + connected_num_restricted ? invalid : from_domain); + + ast_channel_unlock(callee); + callee = ast_channel_unref(callee); + + pj_strdup2(state_data->pool, &remote_identity_node->content, remote_target); + ast_sip_presence_xml_create_attr(state_data->pool, remote_identity_node, "display", cid_name); + ast_sip_presence_xml_create_attr(state_data->pool, remote_target_node, "uri", remote_target); + } + + local_node = ast_sip_presence_xml_create_node(state_data->pool, dialog, "local"); + local_identity_node = ast_sip_presence_xml_create_node(state_data->pool, local_node, "identity"); + local_target_node = ast_sip_presence_xml_create_node(state_data->pool, local_node, "target"); + + pj_strdup2(state_data->pool, &local_identity_node->content, sanitized); + ast_sip_presence_xml_create_attr(state_data->pool, local_target_node, "uri", sanitized); } state = ast_sip_presence_xml_create_node(state_data->pool, dialog, "state"); -- 2.25.1