[Asterisk-code-review] app_queue: track masquerades in app_queue to avoid leaked stasis subs... (asterisk[16])

Nathan Bruning asteriskteam at digium.com
Wed Apr 22 15:04:38 CDT 2020


Nathan Bruning has uploaded this change for review. ( https://gerrit.asterisk.org/c/asterisk/+/14321 )


Change subject: app_queue: track masquerades in app_queue to avoid leaked stasis subscriptions
......................................................................

app_queue: track masquerades in app_queue to avoid leaked stasis subscriptions

Add a new "masquarade" channel event, and use it in app_queue to track unique id's.

Testcase is submitted as https://gerrit.asterisk.org/c/testsuite/+/14210

ASTERISK-28829 #close
ASTERISK-25844 #close

Change-Id: Ifc5f9f9fd70903f3c6e49738d3bc632b085d2df6
---
M apps/app_queue.c
M include/asterisk/stasis_channels.h
M main/channel.c
M main/stasis_channels.c
4 files changed, 56 insertions(+), 0 deletions(-)



  git pull ssh://gerrit.asterisk.org:29418/asterisk refs/changes/21/14321/1

diff --git a/apps/app_queue.c b/apps/app_queue.c
index 1883e1f..f3d44ca 100644
--- a/apps/app_queue.c
+++ b/apps/app_queue.c
@@ -6452,6 +6452,34 @@
 	remove_stasis_subscriptions(queue_data);
 }
 
+static void handle_masquerade(void *userdata, struct stasis_subscription *sub,
+		struct stasis_message *msg)
+{
+	struct queue_stasis_data *queue_data = userdata;
+	struct ast_channel_blob *channel_blob = stasis_message_data(msg);
+	const char *old_channel_id, *new_channel_id;
+
+	old_channel_id = ast_json_string_get(ast_json_object_get(channel_blob->blob, "oldchanneluniqueid"));
+	new_channel_id = ast_json_string_get(ast_json_object_get(channel_blob->blob, "newchanneluniqueid"));
+
+	ao2_lock(queue_data);
+
+	if (queue_data->dying) {
+		ao2_unlock(queue_data);
+		return;
+	}
+
+	if (!strcmp(channel_blob->snapshot->uniqueid, queue_data->caller_uniqueid)) {
+		ast_debug(1, "Replacing caller channel %s with %s due to masquerade\n", queue_data->caller_uniqueid, new_channel_id);
+		ast_string_field_set(queue_data, caller_uniqueid, new_channel_id);
+	} else if (!strcmp(channel_blob->snapshot->uniqueid, queue_data->member_uniqueid)) {
+		ast_debug(1, "Replacing member channel %s with %s due to masquerade\n", queue_data->member_uniqueid, new_channel_id);
+		ast_string_field_set(queue_data, member_uniqueid, new_channel_id);
+	}
+
+	ao2_unlock(queue_data);
+}
+
 /*!
  * \internal
  * \brief Callback for all stasis channel events
@@ -6525,6 +6553,8 @@
 			handle_local_optimization_end, queue_data);
 	stasis_message_router_add(queue_data->channel_router, ast_channel_hangup_request_type(),
 			handle_hangup, queue_data);
+	stasis_message_router_add(queue_data->channel_router, ast_channel_masquerade_type(),
+			handle_masquerade, queue_data);
 	stasis_message_router_set_default(queue_data->channel_router,
 			queue_channel_cb, queue_data);
 
diff --git a/include/asterisk/stasis_channels.h b/include/asterisk/stasis_channels.h
index 4843617..6159f10 100644
--- a/include/asterisk/stasis_channels.h
+++ b/include/asterisk/stasis_channels.h
@@ -393,6 +393,14 @@
 struct stasis_message_type *ast_channel_hangup_request_type(void);
 
 /*!
+ * \since 16
+ * \brief Message type for when a channel is being masqueraded
+ *
+ * \retval A stasis message type
+ */
+struct stasis_message_type *ast_channel_masquerade_type(void);
+
+/*!
  * \since 12
  * \brief Message type for when DTMF begins on a channel.
  *
diff --git a/main/channel.c b/main/channel.c
index 3390917..0cf55d9 100644
--- a/main/channel.c
+++ b/main/channel.c
@@ -10714,6 +10714,7 @@
 
 int ast_channel_move(struct ast_channel *dest, struct ast_channel *source)
 {
+	RAII_VAR(struct ast_json *, blob, NULL, ast_json_unref);
 	SCOPED_MUTEX(lock, &channel_move_lock);
 
 	if (dest == source) {
@@ -10738,6 +10739,11 @@
 	ast_channel_masq_set(dest, source);
 	ast_channel_masqr_set(source, dest);
 
+	blob = ast_json_pack("{s: s, s: s}",
+			"oldchanneluniqueid", ast_channel_uniqueid(source),
+			"newchanneluniqueid", ast_channel_uniqueid(dest));
+	ast_channel_publish_blob(source, ast_channel_masquerade_type(), blob);
+
 	ast_channel_unlock(dest);
 	ast_channel_unlock(source);
 
diff --git a/main/stasis_channels.c b/main/stasis_channels.c
index 03b8c31..10baa01 100644
--- a/main/stasis_channels.c
+++ b/main/stasis_channels.c
@@ -1139,6 +1139,13 @@
 	return channel_blob_to_json(message, "ChannelHangupRequest", sanitize);
 }
 
+static struct ast_json *masquerade_to_json(
+	struct stasis_message *message,
+	const struct stasis_message_sanitizer *sanitize)
+{
+	return channel_blob_to_json(message, "ChannelMasquerade", sanitize);
+}
+
 static struct ast_json *dial_to_json(
 	struct stasis_message *message,
 	const struct stasis_message_sanitizer *sanitize)
@@ -1296,6 +1303,9 @@
 STASIS_MESSAGE_TYPE_DEFN(ast_channel_hangup_request_type,
 	.to_json = hangup_request_to_json,
 	);
+STASIS_MESSAGE_TYPE_DEFN(ast_channel_masquerade_type,
+	.to_json = masquerade_to_json,
+);
 STASIS_MESSAGE_TYPE_DEFN(ast_channel_dtmf_begin_type);
 STASIS_MESSAGE_TYPE_DEFN(ast_channel_dtmf_end_type,
 	.to_json = dtmf_end_to_json,
@@ -1344,6 +1354,7 @@
 	STASIS_MESSAGE_TYPE_CLEANUP(ast_channel_dial_type);
 	STASIS_MESSAGE_TYPE_CLEANUP(ast_channel_varset_type);
 	STASIS_MESSAGE_TYPE_CLEANUP(ast_channel_hangup_request_type);
+	STASIS_MESSAGE_TYPE_CLEANUP(ast_channel_masquerade_type);
 	STASIS_MESSAGE_TYPE_CLEANUP(ast_channel_dtmf_begin_type);
 	STASIS_MESSAGE_TYPE_CLEANUP(ast_channel_dtmf_end_type);
 	STASIS_MESSAGE_TYPE_CLEANUP(ast_channel_hold_type);
@@ -1394,6 +1405,7 @@
 	res |= STASIS_MESSAGE_TYPE_INIT(ast_channel_dial_type);
 	res |= STASIS_MESSAGE_TYPE_INIT(ast_channel_varset_type);
 	res |= STASIS_MESSAGE_TYPE_INIT(ast_channel_hangup_request_type);
+	res |= STASIS_MESSAGE_TYPE_INIT(ast_channel_masquerade_type);
 	res |= STASIS_MESSAGE_TYPE_INIT(ast_channel_dtmf_begin_type);
 	res |= STASIS_MESSAGE_TYPE_INIT(ast_channel_dtmf_end_type);
 	res |= STASIS_MESSAGE_TYPE_INIT(ast_channel_hold_type);

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

Gerrit-Project: asterisk
Gerrit-Branch: 16
Gerrit-Change-Id: Ifc5f9f9fd70903f3c6e49738d3bc632b085d2df6
Gerrit-Change-Number: 14321
Gerrit-PatchSet: 1
Gerrit-Owner: Nathan Bruning <nathan at iperity.com>
Gerrit-MessageType: newchange
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.digium.com/pipermail/asterisk-code-review/attachments/20200422/4abf204f/attachment.html>


More information about the asterisk-code-review mailing list