[Asterisk-code-review] ASTERISK-24601 Include LOCAL/REMOTE tags in BLF NOTIFY XML Patch Orig... (asterisk[16.8])

Joseph Nadiv asteriskteam at digium.com
Wed Apr 14 09:48:05 CDT 2021


Joseph Nadiv has uploaded this change for review. ( https://gerrit.asterisk.org/c/asterisk/+/15763 )


Change subject: ASTERISK-24601 Include LOCAL/REMOTE tags in BLF NOTIFY XML Patch Originally submitted by Joshua Elson Modified by Joseph Nadiv and Sean Bright
......................................................................

ASTERISK-24601 Include LOCAL/REMOTE tags in BLF NOTIFY XML
Patch Originally submitted by Joshua Elson
Modified by Joseph Nadiv and Sean Bright

modified:   res/res_pjsip_dialog_info_body_generator.c

Change-Id: I20c5cf5b45f34d7179df6573c5abf863eb72964b
---
M res/res_pjsip_dialog_info_body_generator.c
1 file changed, 91 insertions(+), 1 deletion(-)



  git pull ssh://gerrit.asterisk.org:29418/asterisk refs/changes/63/15763/1

diff --git a/res/res_pjsip_dialog_info_body_generator.c b/res/res_pjsip_dialog_info_body_generator.c
index 3934a8a..2ee919b 100644
--- a/res/res_pjsip_dialog_info_body_generator.c
+++ b/res/res_pjsip_dialog_info_body_generator.c
@@ -30,6 +30,7 @@
 #include <pjlib.h>
 
 #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 @@
 	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 @@
 	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 @@
 	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");

-- 
To view, visit https://gerrit.asterisk.org/c/asterisk/+/15763
To unsubscribe, or for help writing mail filters, visit https://gerrit.asterisk.org/settings

Gerrit-Project: asterisk
Gerrit-Branch: 16.8
Gerrit-Change-Id: I20c5cf5b45f34d7179df6573c5abf863eb72964b
Gerrit-Change-Number: 15763
Gerrit-PatchSet: 1
Gerrit-Owner: Joseph Nadiv <ynadiv at corpit.xyz>
Gerrit-MessageType: newchange
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.digium.com/pipermail/asterisk-code-review/attachments/20210414/993db129/attachment.html>


More information about the asterisk-code-review mailing list