[asterisk-commits] mmichelson: branch 13 r423348 - in /branches/13: ./ include/asterisk/ res/

SVN commits to the Asterisk project asterisk-commits at lists.digium.com
Thu Sep 18 10:55:19 CDT 2014


Author: mmichelson
Date: Thu Sep 18 10:55:13 2014
New Revision: 423348

URL: http://svnview.digium.com/svn/asterisk?view=rev&rev=423348
Log:
res_pjsip_pubsub: Add some type safety when generating NOTIFY bodies.

res_pjsip_pubsub has two separate checks that it makes when a SUBSCRIBE
arrives.
* It checks that there is a subscription handler for the Event
* It checks that there are body generators for the types in the Accept header

The problem is, there's nothing that ensures that these two things will
actually mesh with each other. For instance, Asterisk will accept a subscription
to MWI that accepts pidf+xml bodies. That doesn't make sense.

With this commit, we add some type information to the mix. Subscription
handlers state they generate data of type X, and body generators state
that they consume data of type X. This way, Asterisk doesn't end up in
some hilariously mismatched situation like the one in the previous paragraph.

ASTERISK-24136 #close
Reported by Mark Michelson

Review: https://reviewboard.asterisk.org/r/3877
Review: https://reviewboard.asterisk.org/r/3878
........

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

Modified:
    branches/13/   (props changed)
    branches/13/include/asterisk/res_pjsip_pubsub.h
    branches/13/res/res_pjsip_dialog_info_body_generator.c
    branches/13/res/res_pjsip_exten_state.c
    branches/13/res/res_pjsip_mwi.c
    branches/13/res/res_pjsip_mwi_body_generator.c
    branches/13/res/res_pjsip_pidf_body_generator.c
    branches/13/res/res_pjsip_pubsub.c
    branches/13/res/res_pjsip_xpidf_body_generator.c

Propchange: branches/13/
------------------------------------------------------------------------------
Binary property 'branch-12-merged' - no diff available.

Modified: branches/13/include/asterisk/res_pjsip_pubsub.h
URL: http://svnview.digium.com/svn/asterisk/branches/13/include/asterisk/res_pjsip_pubsub.h?view=diff&rev=423348&r1=423347&r2=423348
==============================================================================
--- branches/13/include/asterisk/res_pjsip_pubsub.h (original)
+++ branches/13/include/asterisk/res_pjsip_pubsub.h Thu Sep 18 10:55:13 2014
@@ -214,6 +214,21 @@
 	AST_SIP_SUBSCRIPTION_NOTIFY_REASON_OTHER
 };
 
+/*! Type used for conveying mailbox state */
+#define AST_SIP_EXTEN_STATE_DATA "ast_sip_exten_state_data"
+/*! Type used for extension state/presence */
+#define AST_SIP_MESSAGE_ACCUMULATOR "ast_sip_message_accumulator"
+
+/*!
+ * \brief Data used to create bodies for NOTIFY/PUBLISH requests.
+ */
+struct ast_sip_body_data {
+	/*! The type of the data */
+	const char *body_type;
+	/*! The actual data from which the body is generated */
+	void *body_data;
+};
+
 struct ast_sip_notifier {
 	/*!
 	 * \brief Default body type defined for the event package this notifier handles.
@@ -285,6 +300,8 @@
 struct ast_sip_subscription_handler {
 	/*! The name of the event this subscriber deals with */
 	const char *event_name;
+	/*! Type of data used to generate NOTIFY bodies */
+	const char *body_type;
 	/*! The types of body this subscriber accepts. */
 	const char *accept[AST_SIP_MAX_ACCEPT];
 	/*!
@@ -359,7 +376,7 @@
  * \retval 0 Success
  * \retval non-zero Failure
  */
-int ast_sip_subscription_notify(struct ast_sip_subscription *sub, void *notify_data, int terminate);
+int ast_sip_subscription_notify(struct ast_sip_subscription *sub, struct ast_sip_body_data *notify_data, int terminate);
 
 /*!
  * \brief Retrieve the local URI for this subscription
@@ -501,6 +518,8 @@
 	 * In "plain/text", "text" is the subtype
 	 */
 	const char *subtype;
+	/*! Type of data the body generator takes as input */
+	const char *body_type;
 	/*!
 	 * \brief allocate body structure.
 	 *
@@ -597,7 +616,7 @@
  * \retval non-zero Failure
  */
 int ast_sip_pubsub_generate_body_content(const char *content_type,
-		const char *content_subtype, void *data, struct ast_str **str);
+		const char *content_subtype, struct ast_sip_body_data *data, struct ast_str **str);
 
 /*!
  * \since 13.0.0

Modified: branches/13/res/res_pjsip_dialog_info_body_generator.c
URL: http://svnview.digium.com/svn/asterisk/branches/13/res/res_pjsip_dialog_info_body_generator.c?view=diff&rev=423348&r1=423347&r2=423348
==============================================================================
--- branches/13/res/res_pjsip_dialog_info_body_generator.c (original)
+++ branches/13/res/res_pjsip_dialog_info_body_generator.c Thu Sep 18 10:55:13 2014
@@ -182,6 +182,7 @@
 static struct ast_sip_pubsub_body_generator dialog_info_body_generator = {
 	.type = "application",
 	.subtype = "dialog-info+xml",
+	.body_type = AST_SIP_EXTEN_STATE_DATA,
 	.allocate_body = dialog_info_allocate_body,
 	.generate_body_content = dialog_info_generate_body_content,
 	.to_string = dialog_info_to_string,

Modified: branches/13/res/res_pjsip_exten_state.c
URL: http://svnview.digium.com/svn/asterisk/branches/13/res/res_pjsip_exten_state.c?view=diff&rev=423348&r1=423347&r2=423348
==============================================================================
--- branches/13/res/res_pjsip_exten_state.c (original)
+++ branches/13/res/res_pjsip_exten_state.c Thu Sep 18 10:55:13 2014
@@ -91,6 +91,7 @@
 
 struct ast_sip_subscription_handler presence_handler = {
 	.event_name = "presence",
+	.body_type = AST_SIP_EXTEN_STATE_DATA,
 	.accept = { DEFAULT_PRESENCE_BODY, },
 	.subscription_shutdown = subscription_shutdown,
 	.to_ami = to_ami,
@@ -99,6 +100,7 @@
 
 struct ast_sip_subscription_handler dialog_handler = {
 	.event_name = "dialog",
+	.body_type = AST_SIP_EXTEN_STATE_DATA,
 	.accept = { DEFAULT_DIALOG_BODY, },
 	.subscription_shutdown = subscription_shutdown,
 	.to_ami = to_ami,
@@ -221,6 +223,10 @@
 static int notify_task(void *obj)
 {
 	RAII_VAR(struct notify_task_data *, task_data, obj, ao2_cleanup);
+	struct ast_sip_body_data data = {
+		.body_type = AST_SIP_EXTEN_STATE_DATA,
+		.body_data = &task_data->exten_state_data,
+	};
 
 	/* Pool allocation has to happen here so that we allocate within a PJLIB thread */
 	task_data->exten_state_data.pool = pjsip_endpt_create_pool(ast_sip_get_pjsip_endpoint(),
@@ -231,7 +237,7 @@
 
 	task_data->exten_state_data.sub = task_data->exten_state_sub->sip_sub;
 
-	ast_sip_subscription_notify(task_data->exten_state_sub->sip_sub, &task_data->exten_state_data,
+	ast_sip_subscription_notify(task_data->exten_state_sub->sip_sub, &data,
 			task_data->terminate);
 
 	pjsip_endpt_release_pool(ast_sip_get_pjsip_endpoint(),

Modified: branches/13/res/res_pjsip_mwi.c
URL: http://svnview.digium.com/svn/asterisk/branches/13/res/res_pjsip_mwi.c?view=diff&rev=423348&r1=423347&r2=423348
==============================================================================
--- branches/13/res/res_pjsip_mwi.c (original)
+++ branches/13/res/res_pjsip_mwi.c Thu Sep 18 10:55:13 2014
@@ -66,6 +66,7 @@
 
 static struct ast_sip_subscription_handler mwi_handler = {
 	.event_name = "message-summary",
+	.body_type = AST_SIP_MESSAGE_ACCUMULATOR,
 	.accept = { MWI_TYPE"/"MWI_SUBTYPE, },
 	.subscription_shutdown = mwi_subscription_shutdown,
 	.to_ami = mwi_to_ami,
@@ -369,6 +370,10 @@
 	char *aor_name;
 	struct ast_sip_body body;
 	struct ast_str *body_text;
+	struct ast_sip_body_data body_data = {
+		.body_type = AST_SIP_MESSAGE_ACCUMULATOR,
+		.body_data = counter,
+	};
 
 	if (!endpoint) {
 		ast_log(LOG_WARNING, "Unable to send unsolicited MWI to %s because endpoint does not exist\n",
@@ -390,7 +395,7 @@
 		return;
 	}
 
-	if (ast_sip_pubsub_generate_body_content(body.type, body.subtype, counter, &body_text)) {
+	if (ast_sip_pubsub_generate_body_content(body.type, body.subtype, &body_data, &body_text)) {
 		ast_log(LOG_WARNING, "Unable to generate SIP MWI NOTIFY body.\n");
 		ast_free(body_text);
 		return;
@@ -435,11 +440,15 @@
 		.old_msgs = 0,
 		.new_msgs = 0,
 	};
+	struct ast_sip_body_data data = {
+		.body_type = AST_SIP_MESSAGE_ACCUMULATOR,
+		.body_data = &counter,
+	};
 
 	ao2_callback(sub->stasis_subs, OBJ_NODATA, get_message_count, &counter);
 
 	if (sub->is_solicited) {
-		ast_sip_subscription_notify(sub->sip_sub, &counter, 0);
+		ast_sip_subscription_notify(sub->sip_sub, &data, 0);
 		return;
 	}
 

Modified: branches/13/res/res_pjsip_mwi_body_generator.c
URL: http://svnview.digium.com/svn/asterisk/branches/13/res/res_pjsip_mwi_body_generator.c?view=diff&rev=423348&r1=423347&r2=423348
==============================================================================
--- branches/13/res/res_pjsip_mwi_body_generator.c (original)
+++ branches/13/res/res_pjsip_mwi_body_generator.c Thu Sep 18 10:55:13 2014
@@ -85,6 +85,7 @@
 static struct ast_sip_pubsub_body_generator mwi_generator = {
 	.type = MWI_TYPE,
 	.subtype = MWI_SUBTYPE,
+	.body_type = AST_SIP_MESSAGE_ACCUMULATOR,
 	.allocate_body = mwi_allocate_body,
 	.generate_body_content = mwi_generate_body_content,
 	.to_string = mwi_to_string,

Modified: branches/13/res/res_pjsip_pidf_body_generator.c
URL: http://svnview.digium.com/svn/asterisk/branches/13/res/res_pjsip_pidf_body_generator.c?view=diff&rev=423348&r1=423347&r2=423348
==============================================================================
--- branches/13/res/res_pjsip_pidf_body_generator.c (original)
+++ branches/13/res/res_pjsip_pidf_body_generator.c Thu Sep 18 10:55:13 2014
@@ -108,6 +108,7 @@
 static struct ast_sip_pubsub_body_generator pidf_body_generator = {
 	.type = "application",
 	.subtype = "pidf+xml",
+	.body_type = AST_SIP_EXTEN_STATE_DATA,
 	.allocate_body = pidf_allocate_body,
 	.generate_body_content = pidf_generate_body_content,
 	.to_string = pidf_to_string,

Modified: branches/13/res/res_pjsip_pubsub.c
URL: http://svnview.digium.com/svn/asterisk/branches/13/res/res_pjsip_pubsub.c?view=diff&rev=423348&r1=423347&r2=423348
==============================================================================
--- branches/13/res/res_pjsip_pubsub.c (original)
+++ branches/13/res/res_pjsip_pubsub.c Thu Sep 18 10:55:13 2014
@@ -587,7 +587,7 @@
 
 static struct ast_sip_subscription_handler *find_sub_handler_for_event_name(const char *event_name);
 static struct ast_sip_pubsub_body_generator *find_body_generator(char accept[AST_SIP_MAX_ACCEPT][64],
-		size_t num_accept);
+		size_t num_accept, const char *body_type);
 
 /*! \brief Retrieve a handler using the Event header of an rdata message */
 static struct ast_sip_subscription_handler *subscription_get_handler_from_rdata(pjsip_rx_data *rdata)
@@ -674,7 +674,7 @@
 		num_accept_headers = 1;
 	}
 
-	return find_body_generator(accept, num_accept_headers);
+	return find_body_generator(accept, num_accept_headers, handler->body_type);
 }
 
 /*! \brief Check if the rdata has a Supported header containing 'eventlist'
@@ -2105,7 +2105,7 @@
 	return 0;
 }
 
-int ast_sip_subscription_notify(struct ast_sip_subscription *sub, void *notify_data,
+int ast_sip_subscription_notify(struct ast_sip_subscription *sub, struct ast_sip_body_data *notify_data,
 		int terminate)
 {
 	if (ast_sip_pubsub_generate_body_content(ast_sip_subscription_get_body_type(sub),
@@ -2418,7 +2418,7 @@
 }
 
 static struct ast_sip_pubsub_body_generator *find_body_generator(char accept[AST_SIP_MAX_ACCEPT][64],
-		size_t num_accept)
+		size_t num_accept, const char *body_type)
 {
 	int i;
 	struct ast_sip_pubsub_body_generator *generator = NULL;
@@ -2427,6 +2427,12 @@
 		generator = find_body_generator_accept(accept[i]);
 		if (generator) {
 			ast_debug(3, "Body generator %p found for accept type %s\n", generator, accept[i]);
+			if (strcmp(generator->body_type, body_type)) {
+				ast_log(LOG_WARNING, "Body generator '%s/%s'(%p) does not accept the type of data this event generates\n",
+						generator->type, generator->subtype, generator);
+				generator = NULL;
+				continue;
+			}
 			break;
 		} else {
 			ast_debug(3, "No body generator found for accept type %s\n", accept[i]);
@@ -2440,6 +2446,9 @@
 {
 	void *notify_data;
 	int res;
+	struct ast_sip_body_data data = {
+		.body_type = sub->handler->body_type,
+	};
 
 	if (AST_VECTOR_SIZE(&sub->children) > 0) {
 		int i;
@@ -2462,8 +2471,10 @@
 		return -1;
 	}
 
+	data.body_data = notify_data;
+
 	res = ast_sip_pubsub_generate_body_content(ast_sip_subscription_get_body_type(sub),
-			ast_sip_subscription_get_body_subtype(sub), notify_data, &sub->body_text);
+			ast_sip_subscription_get_body_subtype(sub), &data, &sub->body_text);
 
 	ao2_cleanup(notify_data);
 
@@ -2981,7 +2992,7 @@
 }
 
 int ast_sip_pubsub_generate_body_content(const char *type, const char *subtype,
-		void *data, struct ast_str **str)
+		struct ast_sip_body_data *data, struct ast_str **str)
 {
 	struct ast_sip_pubsub_body_supplement *supplement;
 	struct ast_sip_pubsub_body_generator *generator;
@@ -2995,14 +3006,19 @@
 		return -1;
 	}
 
-	body = generator->allocate_body(data);
+	if (strcmp(data->body_type, generator->body_type)) {
+		ast_log(LOG_WARNING, "Body generator does not accept the type of data provided\n");
+		return -1;
+	}
+
+	body = generator->allocate_body(data->body_data);
 	if (!body) {
 		ast_log(LOG_WARNING, "Unable to allocate a NOTIFY body of type %s/%s\n",
 				type, subtype);
 		return -1;
 	}
 
-	if (generator->generate_body_content(body, data)) {
+	if (generator->generate_body_content(body, data->body_data)) {
 		res = -1;
 		goto end;
 	}
@@ -3011,7 +3027,7 @@
 	AST_RWLIST_TRAVERSE(&body_supplements, supplement, list) {
 		if (!strcmp(generator->type, supplement->type) &&
 				!strcmp(generator->subtype, supplement->subtype)) {
-			res = supplement->supplement_body(body, data);
+			res = supplement->supplement_body(body, data->body_data);
 			if (res) {
 				break;
 			}

Modified: branches/13/res/res_pjsip_xpidf_body_generator.c
URL: http://svnview.digium.com/svn/asterisk/branches/13/res/res_pjsip_xpidf_body_generator.c?view=diff&rev=423348&r1=423347&r2=423348
==============================================================================
--- branches/13/res/res_pjsip_xpidf_body_generator.c (original)
+++ branches/13/res/res_pjsip_xpidf_body_generator.c Thu Sep 18 10:55:13 2014
@@ -125,6 +125,7 @@
 static struct ast_sip_pubsub_body_generator xpidf_body_generator = {
 	.type = "application",
 	.subtype = "xpidf+xml",
+	.body_type = AST_SIP_EXTEN_STATE_DATA,
 	.allocate_body = xpidf_allocate_body,
 	.generate_body_content = xpidf_generate_body_content,
 	.to_string = xpidf_to_string,
@@ -134,6 +135,7 @@
 static struct ast_sip_pubsub_body_generator cpim_pidf_body_generator = {
 	.type = "application",
 	.subtype = "cpim-pidf+xml",
+	.body_type = AST_SIP_EXTEN_STATE_DATA,
 	.allocate_body = xpidf_allocate_body,
 	.generate_body_content = xpidf_generate_body_content,
 	.to_string = xpidf_to_string,




More information about the asterisk-commits mailing list