[asterisk-commits] kmoore: trunk r403070 - in /trunk: ./ include/asterisk/ main/ res/ res/ari/ r...

SVN commits to the Asterisk project asterisk-commits at lists.digium.com
Fri Nov 22 14:10:48 CST 2013


Author: kmoore
Date: Fri Nov 22 14:10:46 2013
New Revision: 403070

URL: http://svnview.digium.com/svn/asterisk?view=rev&rev=403070
Log:
ARI: Don't leak implementation details

This change prevents channels used as implementation details from
leaking out to ARI. It does this by preventing creation of JSON blobs
of channel snapshots created from those channels and sanitizing JSON
blobs of bridge snapshots as they are created. This introduces a
framework for excluding information from output targeted at Stasis
applications on a consumer-by-consumer basis using channel sanitization
callbacks which could be extended to bridges or endpoints if necessary.

This prevents unhelpful error messages from being generated by
ast_json_pack.

This also corrects a bug where BridgeCreated events would not be
created.

(closes issue ASTERISK-22744)
Review: https://reviewboard.asterisk.org/r/2987/
Reported by: David M. Lee
........

Merged revisions 403069 from http://svn.asterisk.org/svn/asterisk/branches/12

Modified:
    trunk/   (props changed)
    trunk/include/asterisk/stasis.h
    trunk/include/asterisk/stasis_app.h
    trunk/include/asterisk/stasis_bridges.h
    trunk/include/asterisk/stasis_channels.h
    trunk/include/asterisk/stasis_endpoints.h
    trunk/main/json.c
    trunk/main/rtp_engine.c
    trunk/main/stasis_bridges.c
    trunk/main/stasis_channels.c
    trunk/main/stasis_endpoints.c
    trunk/main/stasis_message.c
    trunk/res/ari/resource_bridges.c
    trunk/res/ari/resource_channels.c
    trunk/res/ari/resource_endpoints.c
    trunk/res/res_stasis.c
    trunk/res/stasis/app.c

Propchange: trunk/
------------------------------------------------------------------------------
--- branch-12-merged (original)
+++ branch-12-merged Fri Nov 22 14:10:46 2013
@@ -1,1 +1,1 @@
-/branches/12:1-398558,398560-398577,398579-399305,399307-401390,401392-402993,403016,403022
+/branches/12:1-398558,398560-398577,398579-399305,399307-401390,401392-402993,403016,403022,403069

Modified: trunk/include/asterisk/stasis.h
URL: http://svnview.digium.com/svn/asterisk/trunk/include/asterisk/stasis.h?view=diff&rev=403070&r1=403069&r2=403070
==============================================================================
--- trunk/include/asterisk/stasis.h (original)
+++ trunk/include/asterisk/stasis.h Fri Nov 22 14:10:46 2013
@@ -187,6 +187,36 @@
 struct stasis_message;
 
 /*!
+ * \brief Structure containing callbacks for Stasis message sanitization
+ *
+ * \note If either callback is implemented, both should be implemented since
+ * not all callers may have access to the full snapshot.
+ */
+struct stasis_message_sanitizer {
+	/*!
+	 * \brief Callback which determines whether a channel should be sanitized from
+	 * a message based on the channel's unique ID
+	 *
+	 * \param channel_id The unique ID of the channel
+	 *
+	 * \retval non-zero if the channel should be left out of the message
+	 * \retval zero if the channel should remain in the message
+	 */
+	int (*channel_id)(const char *channel_id);
+
+	/*!
+	 * \brief Callback which determines whether a channel should be sanitized from
+	 * a message based on the channel's snapshot
+	 *
+	 * \param snapshot A snapshot generated from the channel
+	 *
+	 * \retval non-zero if the channel should be left out of the message
+	 * \retval zero if the channel should remain in the message
+	 */
+	int (*channel_snapshot)(const struct ast_channel_snapshot *snapshot);
+};
+
+/*!
  * \brief Virtual table providing methods for messages.
  * \since 12
  */
@@ -198,17 +228,19 @@
 	 * The returned object should be ast_json_unref()'ed.
 	 *
 	 * \param message Message to convert to JSON string.
+	 * \param sanitize Snapshot sanitization callback.
+	 *
 	 * \return Newly allocated JSON message.
 	 * \return \c NULL on error.
 	 * \return \c NULL if JSON format is not supported.
 	 */
-	struct ast_json *(*to_json)(struct stasis_message *message);
+	struct ast_json *(*to_json)(struct stasis_message *message, const struct stasis_message_sanitizer *sanitize);
 
 	/*!
 	 * \brief Build the AMI representation of the message.
 	 *
 	 * May be \c NULL, or may return \c NULL, to indicate no representation.
-	 * The returned object should be ao2_cleankup()'ed.
+	 * The returned object should be ao2_cleanup()'ed.
 	 *
 	 * \param message Message to convert to AMI string.
 	 * \return Newly allocated \ref ast_manager_event_blob.
@@ -292,11 +324,13 @@
  * be ast_json_unref()'ed.
  *
  * \param message Message to convert to JSON string.
+ * \param sanitize Snapshot sanitization callback.
+ *
  * \return Newly allocated string with JSON message.
  * \return \c NULL on error.
  * \return \c NULL if JSON format is not supported.
  */
-struct ast_json *stasis_message_to_json(struct stasis_message *message);
+struct ast_json *stasis_message_to_json(struct stasis_message *message, struct stasis_message_sanitizer *sanitize);
 
 /*!
  * \brief Build the AMI representation of the message.

Modified: trunk/include/asterisk/stasis_app.h
URL: http://svnview.digium.com/svn/asterisk/trunk/include/asterisk/stasis_app.h?view=diff&rev=403070&r1=403069&r2=403070
==============================================================================
--- trunk/include/asterisk/stasis_app.h (original)
+++ trunk/include/asterisk/stasis_app.h Fri Nov 22 14:10:46 2013
@@ -532,6 +532,13 @@
  */
 void stasis_app_unref(void);
 
+/*!
+ * \brief Get the Stasis message sanitizer for app_stasis applications
+ *
+ * \retval The stasis message sanitizer
+ */
+struct stasis_message_sanitizer *stasis_app_get_sanitizer(void);
+
 /*! @} */
 
 #endif /* _ASTERISK_STASIS_APP_H */

Modified: trunk/include/asterisk/stasis_bridges.h
URL: http://svnview.digium.com/svn/asterisk/trunk/include/asterisk/stasis_bridges.h?view=diff&rev=403070&r1=403069&r2=403070
==============================================================================
--- trunk/include/asterisk/stasis_bridges.h (original)
+++ trunk/include/asterisk/stasis_bridges.h Fri Nov 22 14:10:46 2013
@@ -231,10 +231,15 @@
 
 /*!
  * \brief Build a JSON object from a \ref ast_bridge_snapshot.
+ *
+ * \param snapshot The bridge snapshot to convert to JSON
+ * \param sanitize The message sanitizer to use on the snapshot
+ *
  * \return JSON object representing bridge snapshot.
  * \return \c NULL on error
  */
-struct ast_json *ast_bridge_snapshot_to_json(const struct ast_bridge_snapshot *snapshot);
+struct ast_json *ast_bridge_snapshot_to_json(const struct ast_bridge_snapshot *snapshot,
+	const struct stasis_message_sanitizer *sanitize);
 
 /*!
  * \brief Pair showing a bridge snapshot and a specific channel snapshot belonging to the bridge

Modified: trunk/include/asterisk/stasis_channels.h
URL: http://svnview.digium.com/svn/asterisk/trunk/include/asterisk/stasis_channels.h?view=diff&rev=403070&r1=403069&r2=403070
==============================================================================
--- trunk/include/asterisk/stasis_channels.h (original)
+++ trunk/include/asterisk/stasis_channels.h Fri Nov 22 14:10:46 2013
@@ -541,10 +541,15 @@
 
 /*!
  * \brief Build a JSON object from a \ref ast_channel_snapshot.
+ *
+ * \param snapshot The snapshot to convert to JSON
+ * \param sanitize The message sanitizer to use on the snapshot
+ *
  * \return JSON object representing channel snapshot.
  * \return \c NULL on error
  */
-struct ast_json *ast_channel_snapshot_to_json(const struct ast_channel_snapshot *snapshot);
+struct ast_json *ast_channel_snapshot_to_json(const struct ast_channel_snapshot *snapshot,
+	const struct stasis_message_sanitizer *sanitize);
 
 /*!
  * \brief Compares the context, exten and priority of two snapshots.

Modified: trunk/include/asterisk/stasis_endpoints.h
URL: http://svnview.digium.com/svn/asterisk/trunk/include/asterisk/stasis_endpoints.h?view=diff&rev=403070&r1=403069&r2=403070
==============================================================================
--- trunk/include/asterisk/stasis_endpoints.h (original)
+++ trunk/include/asterisk/stasis_endpoints.h Fri Nov 22 14:10:46 2013
@@ -208,11 +208,14 @@
  * \brief Build a JSON object from a \ref ast_endpoint_snapshot.
  *
  * \param snapshot Endpoint snapshot.
+ * \param sanitize The message sanitizer to use on the snapshot
+ *
  * \return JSON object representing endpoint snapshot.
  * \return \c NULL on error
  */
 struct ast_json *ast_endpoint_snapshot_to_json(
-	const struct ast_endpoint_snapshot *snapshot);
+	const struct ast_endpoint_snapshot *snapshot,
+	const struct stasis_message_sanitizer *sanitize);
 
 /*!
  * \brief Initialization function for endpoint stasis support.

Modified: trunk/main/json.c
URL: http://svnview.digium.com/svn/asterisk/trunk/main/json.c?view=diff&rev=403070&r1=403069&r2=403070
==============================================================================
--- trunk/main/json.c (original)
+++ trunk/main/json.c Fri Nov 22 14:10:46 2013
@@ -690,7 +690,7 @@
 	struct ast_json *r = NULL;
 	if (format) {
 		r = (struct ast_json *)json_vpack_ex(&error, 0, format, ap);
-		if (!r) {
+		if (!r && !ast_strlen_zero(error.text)) {
 			ast_log(LOG_ERROR,
 				"Error building JSON from '%s': %s.\n",
 				format, error.text);

Modified: trunk/main/rtp_engine.c
URL: http://svnview.digium.com/svn/asterisk/trunk/main/rtp_engine.c?view=diff&rev=403070&r1=403069&r2=403070
==============================================================================
--- trunk/main/rtp_engine.c (original)
+++ trunk/main/rtp_engine.c Fri Nov 22 14:10:46 2013
@@ -1780,13 +1780,14 @@
 		ast_str_buffer(packet_string));
 }
 
-static struct ast_json *rtcp_report_to_json(struct stasis_message *msg)
+static struct ast_json *rtcp_report_to_json(struct stasis_message *msg,
+	const struct stasis_message_sanitizer *sanitize)
 {
 	struct rtcp_message_payload *payload = stasis_message_data(msg);
 	RAII_VAR(struct ast_json *, json_rtcp_report, NULL, ast_json_unref);
 	RAII_VAR(struct ast_json *, json_rtcp_report_blocks, NULL, ast_json_unref);
 	RAII_VAR(struct ast_json *, json_rtcp_sender_info, NULL, ast_json_unref);
-	struct ast_json * json_payload;
+	RAII_VAR(struct ast_json *, json_channel, NULL, ast_json_unref);
 	int i;
 
 	json_rtcp_report_blocks = ast_json_array_create();
@@ -1835,11 +1836,17 @@
 		return NULL;
 	}
 
-	json_payload = ast_json_pack("{s: O, s: O, s: O}",
-		"channel", payload->snapshot ? ast_channel_snapshot_to_json(payload->snapshot) : ast_json_null(),
+	if (payload->snapshot) {
+		json_channel = ast_channel_snapshot_to_json(payload->snapshot, sanitize);
+		if (!json_channel) {
+			return NULL;
+		}
+	}
+
+	return ast_json_pack("{s: O, s: O, s: O}",
+		"channel", payload->snapshot ? json_channel : ast_json_null(),
 		"rtcp_report", json_rtcp_report,
 		"blob", payload->blob);
-	return json_payload;
 }
 
 static void rtp_rtcp_report_dtor(void *obj)

Modified: trunk/main/stasis_bridges.c
URL: http://svnview.digium.com/svn/asterisk/trunk/main/stasis_bridges.c?view=diff&rev=403070&r1=403069&r2=403070
==============================================================================
--- trunk/main/stasis_bridges.c (original)
+++ trunk/main/stasis_bridges.c Fri Nov 22 14:10:46 2013
@@ -135,9 +135,15 @@
 
 static struct ast_manager_event_blob *attended_transfer_to_ami(struct stasis_message *message);
 static struct ast_manager_event_blob *blind_transfer_to_ami(struct stasis_message *message);
-static struct ast_json *ast_channel_entered_bridge_to_json(struct stasis_message *msg);
-static struct ast_json *ast_channel_left_bridge_to_json(struct stasis_message *msg);
-static struct ast_json *ast_bridge_merge_message_to_json(struct stasis_message *msg);
+static struct ast_json *ast_channel_entered_bridge_to_json(
+	struct stasis_message *msg,
+	const struct stasis_message_sanitizer *sanitize);
+static struct ast_json *ast_channel_left_bridge_to_json(
+	struct stasis_message *msg,
+	const struct stasis_message_sanitizer *sanitize);
+static struct ast_json *ast_bridge_merge_message_to_json(
+	struct stasis_message *msg,
+	const struct stasis_message_sanitizer *sanitize);
 
 static struct stasis_cp_all *bridge_cache_all;
 
@@ -316,17 +322,25 @@
 	return msg;
 }
 
-static struct ast_json *ast_bridge_merge_message_to_json(struct stasis_message *msg)
-{
-	struct ast_bridge_merge_message *merge;
-
-	merge = stasis_message_data(msg);
-
-        return ast_json_pack("{s: s, s: o, s: o, s: o}",
+static struct ast_json *ast_bridge_merge_message_to_json(
+	struct stasis_message *msg,
+	const struct stasis_message_sanitizer *sanitize)
+{
+	struct ast_bridge_merge_message *merge = stasis_message_data(msg);
+	RAII_VAR(struct ast_json *, json_bridge_to,
+		ast_bridge_snapshot_to_json(merge->to, sanitize), ast_json_unref);
+	RAII_VAR(struct ast_json *, json_bridge_from,
+		ast_bridge_snapshot_to_json(merge->from, sanitize), ast_json_unref);
+
+	if (!json_bridge_to || !json_bridge_from) {
+		return NULL;
+	}
+
+        return ast_json_pack("{s: s, s: o, s: O, s: O}",
                 "type", "BridgeMerged",
                 "timestamp", ast_json_timeval(*stasis_message_timestamp(msg), NULL),
-                "bridge", ast_bridge_snapshot_to_json(merge->to),
-                "bridge_from", ast_bridge_snapshot_to_json(merge->from));
+                "bridge", json_bridge_to,
+                "bridge_from", json_bridge_from);
 }
 
 void ast_bridge_publish_merge(struct ast_bridge *to, struct ast_bridge *from)
@@ -443,45 +457,63 @@
         const char *type,
         struct ast_bridge_snapshot *bridge_snapshot,
         struct ast_channel_snapshot *channel_snapshot,
-        const struct timeval *tv)
-{
-        return ast_json_pack("{s: s, s: o, s: o, s: o}",
+        const struct timeval *tv,
+	const struct stasis_message_sanitizer *sanitize)
+{
+	RAII_VAR(struct ast_json *, json_bridge,
+		ast_bridge_snapshot_to_json(bridge_snapshot, sanitize), ast_json_unref);
+	RAII_VAR(struct ast_json *, json_channel,
+		ast_channel_snapshot_to_json(channel_snapshot, sanitize), ast_json_unref);
+
+	if (!json_bridge || !json_channel) {
+		return NULL;
+	}
+
+        return ast_json_pack("{s: s, s: o, s: O, s: O}",
                 "type", type,
                 "timestamp", ast_json_timeval(*tv, NULL),
-                "bridge", ast_bridge_snapshot_to_json(bridge_snapshot),
-                "channel", ast_channel_snapshot_to_json(channel_snapshot));
-}
-
-struct ast_json *ast_channel_entered_bridge_to_json(struct stasis_message *msg)
+                "bridge", json_bridge,
+                "channel", json_channel);
+}
+
+struct ast_json *ast_channel_entered_bridge_to_json(
+	struct stasis_message *msg,
+	const struct stasis_message_sanitizer *sanitize)
 {
         struct ast_bridge_blob *obj = stasis_message_data(msg);
 
 	return simple_bridge_channel_event("ChannelEnteredBridge", obj->bridge,
-                obj->channel, stasis_message_timestamp(msg));
-}
-
-struct ast_json *ast_channel_left_bridge_to_json(struct stasis_message *msg)
+                obj->channel, stasis_message_timestamp(msg), sanitize);
+}
+
+struct ast_json *ast_channel_left_bridge_to_json(
+	struct stasis_message *msg,
+	const struct stasis_message_sanitizer *sanitize)
 {
         struct ast_bridge_blob *obj = stasis_message_data(msg);
 
 	return simple_bridge_channel_event("ChannelLeftBridge", obj->bridge,
-                obj->channel, stasis_message_timestamp(msg));
-}
-
-typedef struct ast_json *(*json_item_serializer_cb)(void *obj);
-
-static struct ast_json *container_to_json_array(struct ao2_container *items, json_item_serializer_cb item_cb)
+                obj->channel, stasis_message_timestamp(msg), sanitize);
+}
+
+static struct ast_json *container_to_json_array(struct ao2_container *items,
+	const struct stasis_message_sanitizer *sanitize)
 {
 	RAII_VAR(struct ast_json *, json_items, ast_json_array_create(), ast_json_unref);
-	void *item;
+	char *item;
 	struct ao2_iterator it;
 	if (!json_items) {
 		return NULL;
 	}
 
-	it = ao2_iterator_init(items, 0);
-	while ((item = ao2_iterator_next(&it))) {
-		if (ast_json_array_append(json_items, item_cb(item))) {
+	for (it = ao2_iterator_init(items, 0);
+		(item = ao2_iterator_next(&it)); ao2_cleanup(item)) {
+		if (sanitize && sanitize->channel_id && sanitize->channel_id(item)) {
+			continue;
+		}
+
+		if (ast_json_array_append(json_items, ast_json_string_create(item))) {
+			ao2_cleanup(item);
 			ao2_iterator_destroy(&it);
 			return NULL;
 		}
@@ -500,7 +532,9 @@
 	}
 }
 
-struct ast_json *ast_bridge_snapshot_to_json(const struct ast_bridge_snapshot *snapshot)
+struct ast_json *ast_bridge_snapshot_to_json(
+	const struct ast_bridge_snapshot *snapshot,
+	const struct stasis_message_sanitizer *sanitize)
 {
 	RAII_VAR(struct ast_json *, json_bridge, NULL, ast_json_unref);
 	struct ast_json *json_channels;
@@ -509,8 +543,7 @@
 		return NULL;
 	}
 
-	json_channels = container_to_json_array(snapshot->channels,
-		(json_item_serializer_cb)ast_json_string_create);
+	json_channels = container_to_json_array(snapshot->channels, sanitize);
 	if (!json_channels) {
 		return NULL;
 	}

Modified: trunk/main/stasis_channels.c
URL: http://svnview.digium.com/svn/asterisk/trunk/main/stasis_channels.c?view=diff&rev=403070&r1=403069&r2=403070
==============================================================================
--- trunk/main/stasis_channels.c (original)
+++ trunk/main/stasis_channels.c Fri Nov 22 14:10:46 2013
@@ -755,11 +755,15 @@
 	stasis_publish(ast_channel_topic(chan), message);
 }
 
-struct ast_json *ast_channel_snapshot_to_json(const struct ast_channel_snapshot *snapshot)
+struct ast_json *ast_channel_snapshot_to_json(
+	const struct ast_channel_snapshot *snapshot,
+	const struct stasis_message_sanitizer *sanitize)
 {
 	RAII_VAR(struct ast_json *, json_chan, NULL, ast_json_unref);
 
-	if (snapshot == NULL) {
+	if (snapshot == NULL
+		|| (sanitize && sanitize->channel_snapshot
+		&& sanitize->channel_snapshot(snapshot))) {
 		return NULL;
 	}
 
@@ -817,8 +821,10 @@
 		strcmp(old_snapshot->caller_name, new_snapshot->caller_name) == 0;
 }
 
-static struct ast_json *channel_blob_to_json(struct stasis_message *message,
-	const char *type)
+static struct ast_json *channel_blob_to_json(
+	struct stasis_message *message,
+	const char *type,
+	const struct stasis_message_sanitizer *sanitize)
 {
 	RAII_VAR(struct ast_json *, out, NULL, ast_json_unref);
 	struct ast_channel_blob *channel_blob = stasis_message_data(message);
@@ -844,8 +850,13 @@
 
 	/* For global channel messages, the snapshot is optional */
 	if (snapshot) {
-		res |= ast_json_object_set(out, "channel",
-			ast_channel_snapshot_to_json(snapshot));
+		struct ast_json *json_channel = ast_channel_snapshot_to_json(snapshot, sanitize);
+
+		if (!json_channel) {
+			return NULL;
+		}
+
+		res |= ast_json_object_set(out, "channel", json_channel);
 	}
 
 	if (res != 0) {
@@ -855,7 +866,9 @@
 	return ast_json_ref(out);
 }
 
-static struct ast_json *dtmf_end_to_json(struct stasis_message *message)
+static struct ast_json *dtmf_end_to_json(
+	struct stasis_message *message,
+	const struct stasis_message_sanitizer *sanitize)
 {
 	struct ast_channel_blob *channel_blob = stasis_message_data(message);
 	struct ast_json *blob = channel_blob->blob;
@@ -863,9 +876,14 @@
 	const char *direction =
 		ast_json_string_get(ast_json_object_get(blob, "direction"));
 	const struct timeval *tv = stasis_message_timestamp(message);
+	struct ast_json *json_channel = ast_channel_snapshot_to_json(snapshot, sanitize);
 
 	/* Only present received DTMF end events as JSON */
 	if (strcasecmp("Received", direction) != 0) {
+		return NULL;
+	}
+
+	if (!json_channel) {
 		return NULL;
 	}
 
@@ -874,32 +892,43 @@
 		"timestamp", ast_json_timeval(*tv, NULL),
 		"digit", ast_json_object_get(blob, "digit"),
 		"duration_ms", ast_json_object_get(blob, "duration_ms"),
-		"channel", ast_channel_snapshot_to_json(snapshot));
-}
-
-static struct ast_json *user_event_to_json(struct stasis_message *message)
+		"channel", json_channel);
+}
+
+static struct ast_json *user_event_to_json(
+	struct stasis_message *message,
+	const struct stasis_message_sanitizer *sanitize)
 {
 	struct ast_channel_blob *channel_blob = stasis_message_data(message);
 	struct ast_json *blob = channel_blob->blob;
 	struct ast_channel_snapshot *snapshot = channel_blob->snapshot;
 	const struct timeval *tv = stasis_message_timestamp(message);
+	struct ast_json *json_channel = ast_channel_snapshot_to_json(snapshot, sanitize);
+
+	if (!json_channel) {
+		return NULL;
+	}
 
 	return ast_json_pack("{s: s, s: o, s: O, s: O, s: o}",
 		"type", "ChannelUserevent",
 		"timestamp", ast_json_timeval(*tv, NULL),
 		"eventname", ast_json_object_get(blob, "eventname"),
 		"userevent", blob,
-		"channel", ast_channel_snapshot_to_json(snapshot));
-}
-
-static struct ast_json *varset_to_json(struct stasis_message *message)
-{
-	return channel_blob_to_json(message, "ChannelVarset");
-}
-
-static struct ast_json *hangup_request_to_json(struct stasis_message *message)
-{
-	return channel_blob_to_json(message, "ChannelHangupRequest");
+		"channel", json_channel);
+}
+
+static struct ast_json *varset_to_json(
+	struct stasis_message *message,
+	const struct stasis_message_sanitizer *sanitize)
+{
+	return channel_blob_to_json(message, "ChannelVarset", sanitize);
+}
+
+static struct ast_json *hangup_request_to_json(
+	struct stasis_message *message,
+	const struct stasis_message_sanitizer *sanitize)
+{
+	return channel_blob_to_json(message, "ChannelHangupRequest", sanitize);
 }
 
 /*!

Modified: trunk/main/stasis_endpoints.c
URL: http://svnview.digium.com/svn/asterisk/trunk/main/stasis_endpoints.c?view=diff&rev=403070&r1=403069&r2=403070
==============================================================================
--- trunk/main/stasis_endpoints.c (original)
+++ trunk/main/stasis_endpoints.c Fri Nov 22 14:10:46 2013
@@ -237,7 +237,8 @@
 
 
 struct ast_json *ast_endpoint_snapshot_to_json(
-	const struct ast_endpoint_snapshot *snapshot)
+	const struct ast_endpoint_snapshot *snapshot,
+	const struct stasis_message_sanitizer *sanitize)
 {
 	RAII_VAR(struct ast_json *, json, NULL, ast_json_unref);
 	struct ast_json *channel_array;
@@ -264,7 +265,14 @@
 	channel_array = ast_json_object_get(json, "channel_ids");
 	ast_assert(channel_array != NULL);
 	for (i = 0; i < snapshot->num_channels; ++i) {
-		int res = ast_json_array_append(channel_array,
+		int res;
+
+		if (sanitize && sanitize->channel_id
+			&& sanitize->channel_id(snapshot->channel_ids[i])) {
+			continue;
+		}
+
+		res = ast_json_array_append(channel_array,
 			ast_json_string_create(snapshot->channel_ids[i]));
 		if (res != 0) {
 			return NULL;

Modified: trunk/main/stasis_message.c
URL: http://svnview.digium.com/svn/asterisk/trunk/main/stasis_message.c?view=diff&rev=403070&r1=403069&r2=403070
==============================================================================
--- trunk/main/stasis_message.c (original)
+++ trunk/main/stasis_message.c Fri Nov 22 14:10:46 2013
@@ -161,7 +161,9 @@
 	return INVOKE_VIRTUAL(to_ami, msg);
 }
 
-struct ast_json *stasis_message_to_json(struct stasis_message *msg)
+struct ast_json *stasis_message_to_json(
+	struct stasis_message *msg,
+	struct stasis_message_sanitizer *sanitize)
 {
-	return INVOKE_VIRTUAL(to_json, msg);
+	return INVOKE_VIRTUAL(to_json, msg, sanitize);
 }

Modified: trunk/res/ari/resource_bridges.c
URL: http://svnview.digium.com/svn/asterisk/trunk/res/ari/resource_bridges.c?view=diff&rev=403070&r1=403069&r2=403070
==============================================================================
--- trunk/res/ari/resource_bridges.c (original)
+++ trunk/res/ari/resource_bridges.c Fri Nov 22 14:10:46 2013
@@ -605,7 +605,7 @@
 	}
 
 	ast_ari_response_ok(response,
-		ast_bridge_snapshot_to_json(snapshot));
+		ast_bridge_snapshot_to_json(snapshot, stasis_app_get_sanitizer()));
 }
 
 void ast_ari_bridges_destroy(struct ast_variable *headers,
@@ -656,7 +656,9 @@
 	while ((obj = ao2_iterator_next(&i))) {
 		RAII_VAR(struct stasis_message *, msg, obj, ao2_cleanup);
 		struct ast_bridge_snapshot *snapshot = stasis_message_data(msg);
-		if (ast_json_array_append(json, ast_bridge_snapshot_to_json(snapshot))) {
+		struct ast_json *json_bridge = ast_bridge_snapshot_to_json(snapshot, stasis_app_get_sanitizer());
+
+		if (!json_bridge || ast_json_array_append(json, json_bridge)) {
 			ast_ari_response_alloc_failed(response);
 			return;
 		}
@@ -689,5 +691,5 @@
 	}
 
 	ast_ari_response_ok(response,
-		ast_bridge_snapshot_to_json(snapshot));
-}
+		ast_bridge_snapshot_to_json(snapshot, stasis_app_get_sanitizer()));
+}

Modified: trunk/res/ari/resource_channels.c
URL: http://svnview.digium.com/svn/asterisk/trunk/res/ari/resource_channels.c?view=diff&rev=403070&r1=403069&r2=403070
==============================================================================
--- trunk/res/ari/resource_channels.c (original)
+++ trunk/res/ari/resource_channels.c Fri Nov 22 14:10:46 2013
@@ -593,7 +593,7 @@
 	ast_assert(snapshot != NULL);
 
 	ast_ari_response_ok(response,
-				ast_channel_snapshot_to_json(snapshot));
+				ast_channel_snapshot_to_json(snapshot, NULL));
 }
 
 void ast_ari_channels_hangup(struct ast_variable *headers,
@@ -639,6 +639,7 @@
 	RAII_VAR(struct ast_json *, json, NULL, ast_json_unref);
 	struct ao2_iterator i;
 	void *obj;
+	struct stasis_message_sanitizer *sanitize = stasis_app_get_sanitizer();
 
 	cache = ast_channel_cache();
 	if (!cache) {
@@ -661,14 +662,23 @@
 		return;
 	}
 
-	i = ao2_iterator_init(snapshots, 0);
-	while ((obj = ao2_iterator_next(&i))) {
+	for (i = ao2_iterator_init(snapshots, 0);
+		(obj = ao2_iterator_next(&i)); ao2_cleanup(obj)) {
 		RAII_VAR(struct stasis_message *, msg, obj, ao2_cleanup);
 		struct ast_channel_snapshot *snapshot = stasis_message_data(msg);
-		int r = ast_json_array_append(
-			json, ast_channel_snapshot_to_json(snapshot));
+		int r;
+
+		if (sanitize && sanitize->channel_snapshot
+			&& sanitize->channel_snapshot(snapshot)) {
+			continue;
+		}
+
+		r = ast_json_array_append(
+			json, ast_channel_snapshot_to_json(snapshot, NULL));
 		if (r != 0) {
 			ast_ari_response_alloc_failed(response);
+			ao2_cleanup(obj);
+			ao2_iterator_destroy(&i);
 			return;
 		}
 	}
@@ -769,7 +779,7 @@
 		stasis_app_subscribe(args->app, uris, 1, NULL);
 	}
 
-	ast_ari_response_ok(response, ast_channel_snapshot_to_json(snapshot));
+	ast_ari_response_ok(response, ast_channel_snapshot_to_json(snapshot, NULL));
 	ast_channel_unref(chan);
 }
 

Modified: trunk/res/ari/resource_endpoints.c
URL: http://svnview.digium.com/svn/asterisk/trunk/res/ari/resource_endpoints.c?view=diff&rev=403070&r1=403069&r2=403070
==============================================================================
--- trunk/res/ari/resource_endpoints.c (original)
+++ trunk/res/ari/resource_endpoints.c Fri Nov 22 14:10:46 2013
@@ -31,6 +31,7 @@
 
 #include "asterisk/astobj2.h"
 #include "asterisk/stasis.h"
+#include "asterisk/stasis_app.h"
 #include "asterisk/stasis_endpoints.h"
 #include "asterisk/channel.h"
 
@@ -69,8 +70,15 @@
 	while ((obj = ao2_iterator_next(&i))) {
 		RAII_VAR(struct stasis_message *, msg, obj, ao2_cleanup);
 		struct ast_endpoint_snapshot *snapshot = stasis_message_data(msg);
-		int r = ast_json_array_append(
-			json, ast_endpoint_snapshot_to_json(snapshot));
+		struct ast_json *json_endpoint = ast_endpoint_snapshot_to_json(snapshot, stasis_app_get_sanitizer());
+		int r;
+
+		if (!json_endpoint) {
+			return;
+		}
+
+		r = ast_json_array_append(
+			json, json_endpoint);
 		if (r != 0) {
 			ast_ari_response_alloc_failed(response);
 			return;
@@ -121,14 +129,20 @@
 	while ((obj = ao2_iterator_next(&i))) {
 		RAII_VAR(struct stasis_message *, msg, obj, ao2_cleanup);
 		struct ast_endpoint_snapshot *snapshot = stasis_message_data(msg);
+		struct ast_json *json_endpoint;
 		int r;
 
 		if (strcasecmp(args->tech, snapshot->tech) != 0) {
 			continue;
 		}
 
+		json_endpoint = ast_endpoint_snapshot_to_json(snapshot, stasis_app_get_sanitizer());
+		if (!json_endpoint) {
+			continue;
+		}
+
 		r = ast_json_array_append(
-			json, ast_endpoint_snapshot_to_json(snapshot));
+			json, json_endpoint);
 		if (r != 0) {
 			ast_ari_response_alloc_failed(response);
 			return;
@@ -151,7 +165,7 @@
 		return;
 	}
 
-	json = ast_endpoint_snapshot_to_json(snapshot);
+	json = ast_endpoint_snapshot_to_json(snapshot, stasis_app_get_sanitizer());
 	if (!json) {
 		ast_ari_response_alloc_failed(response);
 		return;

Modified: trunk/res/res_stasis.c
URL: http://svnview.digium.com/svn/asterisk/trunk/res/res_stasis.c?view=diff&rev=403070&r1=403069&r2=403070
==============================================================================
--- trunk/res/res_stasis.c (original)
+++ trunk/res/res_stasis.c Fri Nov 22 14:10:46 2013
@@ -627,6 +627,7 @@
 
 	struct ast_json *json_args;
 	int i;
+	struct stasis_message_sanitizer *sanitize = stasis_app_get_sanitizer();
 
 	ast_assert(chan != NULL);
 
@@ -636,11 +637,16 @@
 		return -1;
 	}
 
+	if (sanitize && sanitize->channel_snapshot
+		&& sanitize->channel_snapshot(snapshot)) {
+		return 0;
+	}
+
 	msg = ast_json_pack("{s: s, s: o, s: [], s: o}",
 		"type", "StasisStart",
 		"timestamp", ast_json_timeval(ast_tvnow(), NULL),
 		"args",
-		"channel", ast_channel_snapshot_to_json(snapshot));
+		"channel", ast_channel_snapshot_to_json(snapshot, NULL));
 	if (!msg) {
 		return -1;
 	}
@@ -665,6 +671,7 @@
 {
 	RAII_VAR(struct ast_json *, msg, NULL, ast_json_unref);
 	RAII_VAR(struct ast_channel_snapshot *, snapshot, NULL, ao2_cleanup);
+	struct stasis_message_sanitizer *sanitize = stasis_app_get_sanitizer();
 
 	ast_assert(chan != NULL);
 
@@ -674,10 +681,15 @@
 		return -1;
 	}
 
+	if (sanitize && sanitize->channel_snapshot
+		&& sanitize->channel_snapshot(snapshot)) {
+		return 0;
+	}
+
 	msg = ast_json_pack("{s: s, s: o, s: o}",
 		"type", "StasisEnd",
 		"timestamp", ast_json_timeval(ast_tvnow(), NULL),
-		"channel", ast_channel_snapshot_to_json(snapshot));
+		"channel", ast_channel_snapshot_to_json(snapshot, NULL));
 	if (!msg) {
 		return -1;
 	}
@@ -1153,6 +1165,34 @@
 	return 0;
 }
 
+/* \brief Sanitization callback for channel snapshots */
+static int channel_snapshot_sanitizer(const struct ast_channel_snapshot *snapshot)
+{
+	if (!snapshot || !(snapshot->tech_properties & AST_CHAN_TP_INTERNAL)) {
+		return 0;
+	}
+	return 1;
+}
+
+/* \brief Sanitization callback for channel unique IDs */
+static int channel_id_sanitizer(const char *id)
+{
+	RAII_VAR(struct ast_channel_snapshot *, snapshot, ast_channel_snapshot_get_latest(id), ao2_cleanup);
+
+	return channel_snapshot_sanitizer(snapshot);
+}
+
+/* \brief Sanitization callbacks for communication to Stasis applications */
+struct stasis_message_sanitizer app_sanitizer = {
+	.channel_id = channel_id_sanitizer,
+	.channel_snapshot = channel_snapshot_sanitizer,
+};
+
+struct stasis_message_sanitizer *stasis_app_get_sanitizer(void)
+{
+	return &app_sanitizer;
+}
+
 static int load_module(void)
 {
 	apps_registry = ao2_container_alloc(APPS_NUM_BUCKETS, app_hash, app_compare);

Modified: trunk/res/stasis/app.c
URL: http://svnview.digium.com/svn/asterisk/trunk/res/stasis/app.c?view=diff&rev=403070&r1=403069&r2=403070
==============================================================================
--- trunk/res/stasis/app.c (original)
+++ trunk/res/stasis/app.c Fri Nov 22 14:10:46 2013
@@ -276,7 +276,7 @@
 	}
 
 	/* By default, send any message that has a JSON representation */
-	json = stasis_message_to_json(message);
+	json = stasis_message_to_json(message, stasis_app_get_sanitizer());
 	if (!json) {
 		return;
 	}
@@ -295,10 +295,16 @@
 	struct ast_channel_snapshot *snapshot,
 	const struct timeval *tv)
 {
+	struct ast_json *json_channel = ast_channel_snapshot_to_json(snapshot, stasis_app_get_sanitizer());
+
+	if (!json_channel) {
+		return NULL;
+	}
+
 	return ast_json_pack("{s: s, s: o, s: o}",
 		"type", type,
 		"timestamp", ast_json_timeval(*tv, NULL),
-		"channel", ast_channel_snapshot_to_json(snapshot));
+		"channel", json_channel);
 }
 
 static struct ast_json *channel_created_event(
@@ -312,12 +318,18 @@
 	struct ast_channel_snapshot *snapshot,
 	const struct timeval *tv)
 {
+	struct ast_json *json_channel = ast_channel_snapshot_to_json(snapshot, stasis_app_get_sanitizer());
+
+	if (!json_channel) {
+		return NULL;
+	}
+
 	return ast_json_pack("{s: s, s: o, s: i, s: s, s: o}",
 		"type", "ChannelDestroyed",
 		"timestamp", ast_json_timeval(*tv, NULL),
 		"cause", snapshot->hangupcause,
 		"cause_txt", ast_cause2str(snapshot->hangupcause),
-		"channel", ast_channel_snapshot_to_json(snapshot));
+		"channel", json_channel);
 }
 
 static struct ast_json *channel_state_change_event(
@@ -353,6 +365,7 @@
 	const struct timeval *tv)
 {
 	RAII_VAR(struct ast_json *, json, NULL, ast_json_unref);
+	struct ast_json *json_channel;
 
 	/* No Newexten event on cache clear or first event */
 	if (!old_snapshot || !new_snapshot) {
@@ -365,6 +378,11 @@
 	}
 
 	if (ast_channel_snapshot_cep_equal(old_snapshot, new_snapshot)) {
+		return NULL;
+	}
+
+	json_channel = ast_channel_snapshot_to_json(new_snapshot, stasis_app_get_sanitizer());
+	if (!json_channel) {
 		return NULL;
 	}
 
@@ -373,7 +391,7 @@
 		"timestamp", ast_json_timeval(*tv, NULL),
 		"dialplan_app", new_snapshot->appl,
 		"dialplan_app_data", new_snapshot->data,
-		"channel", ast_channel_snapshot_to_json(new_snapshot));
+		"channel", json_channel);
 }
 
 static struct ast_json *channel_callerid(
@@ -382,6 +400,7 @@
 	const struct timeval *tv)
 {
 	RAII_VAR(struct ast_json *, json, NULL, ast_json_unref);
+	struct ast_json *json_channel;
 
 	/* No NewCallerid event on cache clear or first event */
 	if (!old_snapshot || !new_snapshot) {
@@ -389,6 +408,11 @@
 	}
 
 	if (ast_channel_snapshot_caller_id_equal(old_snapshot, new_snapshot)) {
+		return NULL;
+	}
+
+	json_channel = ast_channel_snapshot_to_json(new_snapshot, stasis_app_get_sanitizer());
+	if (!json_channel) {
 		return NULL;
 	}
 
@@ -398,7 +422,7 @@
 		"caller_presentation", new_snapshot->caller_pres,
 		"caller_presentation_txt", ast_describe_caller_presentation(
 			new_snapshot->caller_pres),
-		"channel", ast_channel_snapshot_to_json(new_snapshot));
+		"channel", json_channel);
 }
 
 static channel_snapshot_monitor channel_monitors[] = {
@@ -448,10 +472,16 @@
 	struct ast_endpoint_snapshot *snapshot,
 	const struct timeval *tv)
 {
+	struct ast_json *json_endpoint = ast_endpoint_snapshot_to_json(snapshot, stasis_app_get_sanitizer());
+
+	if (!json_endpoint) {
+		return NULL;
+	}
+
 	return ast_json_pack("{s: s, s: o, s: o}",
 		"type", type,
 		"timestamp", ast_json_timeval(*tv, NULL),
-		"endpoint", ast_endpoint_snapshot_to_json(snapshot));
+		"endpoint", json_endpoint);
 }
 
 static void sub_endpoint_update_handler(void *data,
@@ -489,10 +519,15 @@
 	struct ast_bridge_snapshot *snapshot,
 	const struct timeval *tv)
 {
+	struct ast_json *json_bridge = ast_bridge_snapshot_to_json(snapshot, stasis_app_get_sanitizer());
+	if (!json_bridge) {
+		return NULL;
+	}
+
 	return ast_json_pack("{s: s, s: o, s: o}",
 		"type", type,
 		"timestamp", ast_json_timeval(*tv, NULL),
-		"bridge", ast_bridge_snapshot_to_json(snapshot));
+		"bridge", json_bridge);
 }
 
 static void sub_bridge_update_handler(void *data,
@@ -521,7 +556,7 @@
 	if (!new_snapshot) {
 		json = simple_bridge_event("BridgeDestroyed", old_snapshot, tv);
 	} else if (!old_snapshot) {
-		json = simple_bridge_event("BridgeCreated", old_snapshot, tv);
+		json = simple_bridge_event("BridgeCreated", new_snapshot, tv);
 	}
 
 	if (!json) {




More information about the asterisk-commits mailing list