[asterisk-commits] file: branch file/pjsip-subscription-persistence r415656 - in /team/file/pjsi...

SVN commits to the Asterisk project asterisk-commits at lists.digium.com
Tue Jun 10 08:19:46 CDT 2014


Author: file
Date: Tue Jun 10 08:19:35 2014
New Revision: 415656

URL: http://svnview.digium.com/svn/asterisk?view=rev&rev=415656
Log:
Incorporate feedback.

Modified:
    team/file/pjsip-subscription-persistence/include/asterisk/res_pjsip.h
    team/file/pjsip-subscription-persistence/res/res_pjsip.c
    team/file/pjsip-subscription-persistence/res/res_pjsip_pubsub.c

Modified: team/file/pjsip-subscription-persistence/include/asterisk/res_pjsip.h
URL: http://svnview.digium.com/svn/asterisk/team/file/pjsip-subscription-persistence/include/asterisk/res_pjsip.h?view=diff&rev=415656&r1=415655&r2=415656
==============================================================================
--- team/file/pjsip-subscription-persistence/include/asterisk/res_pjsip.h (original)
+++ team/file/pjsip-subscription-persistence/include/asterisk/res_pjsip.h Tue Jun 10 08:19:35 2014
@@ -1224,6 +1224,23 @@
 pjsip_dialog *ast_sip_create_dialog_uas(const struct ast_sip_endpoint *endpoint, pjsip_rx_data *rdata);
 
 /*!
+ * \brief General purpose method for creating an rdata structure using specific information
+ *
+ * \param rdata The rdata structure that will be populated
+ * \param packet A SIP message
+ * \param src_name The source IP address of the message
+ * \param src_port The source port of the message
+ * \param transport_type The type of transport the message was received on
+ * \param local_name The local IP address the message was received on
+ * \param local_port The local port the message was received on
+ *
+ * \retval 0 success
+ * \retval -1 failure
+ */
+int ast_sip_create_rdata(pjsip_rx_data *rdata, char *packet, const char *src_name, int src_port, char *transport_type,
+	const char *local_name, int local_port);
+
+/*!
  * \brief General purpose method for creating a SIP request
  *
  * Its typical use would be to create one-off requests such as an out of dialog

Modified: team/file/pjsip-subscription-persistence/res/res_pjsip.c
URL: http://svnview.digium.com/svn/asterisk/team/file/pjsip-subscription-persistence/res/res_pjsip.c?view=diff&rev=415656&r1=415655&r2=415656
==============================================================================
--- team/file/pjsip-subscription-persistence/res/res_pjsip.c (original)
+++ team/file/pjsip-subscription-persistence/res/res_pjsip.c Tue Jun 10 08:19:35 2014
@@ -1613,6 +1613,37 @@
 	return dlg;
 }
 
+int ast_sip_create_rdata(pjsip_rx_data *rdata, char *packet, const char *src_name, int src_port,
+	char *transport_type, const char *local_name, int local_port)
+{
+	pj_str_t tmp;
+
+	rdata->tp_info.transport = PJ_POOL_ZALLOC_T(rdata->tp_info.pool, pjsip_transport);
+	if (!rdata->tp_info.transport) {
+		return -1;
+	}
+
+	/* The following code constructs a basic rdata structure using persisted information */
+	ast_copy_string(rdata->pkt_info.packet, packet, sizeof(rdata->pkt_info.packet));
+	ast_copy_string(rdata->pkt_info.src_name, src_name, sizeof(rdata->pkt_info.src_name));
+	rdata->pkt_info.src_port = src_port;
+
+	pjsip_parse_rdata(packet, strlen(packet), rdata);
+	if (!rdata->msg_info.msg) {
+		return -1;
+	}
+
+	pj_strdup2(rdata->tp_info.pool, &rdata->msg_info.via->recvd_param, rdata->pkt_info.src_name);
+	rdata->msg_info.via->rport_param = -1;
+
+	rdata->tp_info.transport->key.type = pjsip_transport_get_type_from_name(pj_cstr(&tmp, transport_type));
+	rdata->tp_info.transport->type_name = transport_type;
+	pj_strdup2(rdata->tp_info.pool, &rdata->tp_info.transport->local_name.host, local_name);
+	rdata->tp_info.transport->local_name.port = local_port;
+
+	return 0;
+}
+
 /* PJSIP doesn't know about the INFO method, so we have to define it ourselves */
 static const pjsip_method info_method = {PJSIP_OTHER_METHOD, {"INFO", 4} };
 static const pjsip_method message_method = {PJSIP_OTHER_METHOD, {"MESSAGE", 7} };

Modified: team/file/pjsip-subscription-persistence/res/res_pjsip_pubsub.c
URL: http://svnview.digium.com/svn/asterisk/team/file/pjsip-subscription-persistence/res/res_pjsip_pubsub.c?view=diff&rev=415656&r1=415655&r2=415656
==============================================================================
--- team/file/pjsip-subscription-persistence/res/res_pjsip_pubsub.c (original)
+++ team/file/pjsip-subscription-persistence/res/res_pjsip_pubsub.c Tue Jun 10 08:19:35 2014
@@ -363,85 +363,42 @@
 	ao2_ref(sub->persistence, -1);
 }
 
+
 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);
 
-/*! \brief Callback function to perform the actual recreation of a subscription */
-static int subscription_persistence_recreate(void *obj, void *arg, int flags)
-{
-	struct subscription_persistence *persistence = obj;
-	pj_pool_t *pool = arg;
-	pjsip_transport transport = { "", };
-	pjsip_rx_data rdata = { { 0, }, };
-	pj_str_t tmp;
+/*! \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)
+{
+	pjsip_event_hdr *event_header;
 	char event[32];
-	char accept[AST_SIP_MAX_ACCEPT][64];
-	pjsip_accept_hdr *accept_header;
-	pjsip_expires_hdr *expires_header;
-	pjsip_event_hdr *event_header;
 	struct ast_sip_subscription_handler *handler;
-	RAII_VAR(struct ast_sip_endpoint *, endpoint, NULL, ao2_cleanup);
-	struct ast_sip_subscription *sub;
-	size_t num_accept_headers;
-	struct ast_sip_pubsub_body_generator *generator;
-
-	/* If this subscription has already expired remove it */
-	if (ast_tvdiff_ms(persistence->expires, ast_tvnow()) <= 0) {
-		ast_sorcery_delete(ast_sip_get_sorcery(), persistence);
-		return 0;
-	}
-
-	endpoint = ast_sorcery_retrieve_by_id(ast_sip_get_sorcery(), "endpoint", persistence->endpoint);
-	if (!endpoint) {
-		ast_log(LOG_WARNING, "A subscription for '%s' could not be recreated as the endpoint was not found\n",
-			persistence->endpoint);
-		ast_sorcery_delete(ast_sip_get_sorcery(), persistence);
-		return 0;
-	}
-
-	pj_pool_reset(pool);
-	rdata.tp_info.pool = pool;
-	rdata.tp_info.transport = &transport;
-
-	/* The following code constructs a basic rdata structure using persisted information */
-	ast_copy_string(rdata.pkt_info.packet, persistence->packet, sizeof(rdata.pkt_info.packet));
-	ast_copy_string(rdata.pkt_info.src_name, persistence->src_name, sizeof(rdata.pkt_info.src_name));
-	rdata.pkt_info.src_port = persistence->src_port;
-
-	pjsip_parse_rdata(persistence->packet, strlen(persistence->packet), &rdata);
-	if (!rdata.msg_info.msg) {
-		return 0;
-	}
-	pj_strdup2(rdata.tp_info.pool, &rdata.msg_info.via->recvd_param, rdata.pkt_info.src_name);
-	rdata.msg_info.via->rport_param = -1;
-
-	rdata.tp_info.transport->key.type = pjsip_transport_get_type_from_name(pj_cstr(&tmp, persistence->transport_key));
-	rdata.tp_info.transport->type_name = persistence->transport_key;
-	pj_strset2(&rdata.tp_info.transport->local_name.host, persistence->local_name);
-	rdata.tp_info.transport->local_name.port = persistence->local_port;
-
-	/* Update the expiration header with the new expiration */
-	expires_header = pjsip_msg_find_hdr(rdata.msg_info.msg, PJSIP_H_EXPIRES, rdata.msg_info.msg->hdr.next);
-	if (!expires_header) {
-		expires_header = pjsip_expires_hdr_create(pool, 0);
-		if (!expires_header) {
-			return 0;
-		}
-		pjsip_msg_add_hdr(rdata.msg_info.msg, (pjsip_hdr*)expires_header);
-	}
-	expires_header->ivalue = (ast_tvdiff_ms(persistence->expires, ast_tvnow()) / 1000);
-
-	event_header = pjsip_msg_find_hdr_by_name(rdata.msg_info.msg, &str_event_name, rdata.msg_info.msg->hdr.next);
+
+	event_header = pjsip_msg_find_hdr_by_name(rdata->msg_info.msg, &str_event_name, rdata->msg_info.msg->hdr.next);
+	if (!event_header) {
+		ast_log(LOG_WARNING, "Incoming SUBSCRIBE request with no Event header\n");
+		return NULL;
+	}
 	ast_copy_pj_str(event, &event_header->event_type, sizeof(event));
 
 	handler = find_sub_handler_for_event_name(event);
 	if (!handler) {
-		/* This may eventually get a handler once everything is loaded */
-		return 0;
-	}
-
-	accept_header = pjsip_msg_find_hdr(rdata.msg_info.msg, PJSIP_H_ACCEPT, rdata.msg_info.msg->hdr.next);
+		ast_log(LOG_WARNING, "No registered subscribe handler for event %s\n", event);
+	}
+
+	return handler;
+}
+
+/*! \brief Retrieve a body generator using the Accept header of an rdata message */
+static struct ast_sip_pubsub_body_generator *subscription_get_generator_from_rdata(pjsip_rx_data *rdata,
+	const struct ast_sip_subscription_handler *handler)
+{
+	pjsip_accept_hdr *accept_header;
+	char accept[AST_SIP_MAX_ACCEPT][64];
+	size_t num_accept_headers;
+
+	accept_header = pjsip_msg_find_hdr(rdata->msg_info.msg, PJSIP_H_ACCEPT, rdata->msg_info.msg->hdr.next);
 	if (accept_header) {
 		int i;
 
@@ -457,7 +414,64 @@
 		num_accept_headers = 1;
 	}
 
-	generator = find_body_generator(accept, num_accept_headers);
+	return find_body_generator(accept, num_accept_headers);
+}
+
+/*! \brief Callback function to perform the actual recreation of a subscription */
+static int subscription_persistence_recreate(void *obj, void *arg, int flags)
+{
+	struct subscription_persistence *persistence = obj;
+	pj_pool_t *pool = arg;
+	pjsip_rx_data rdata = { { 0, }, };
+	pjsip_expires_hdr *expires_header;
+	struct ast_sip_subscription_handler *handler;
+	RAII_VAR(struct ast_sip_endpoint *, endpoint, NULL, ao2_cleanup);
+	struct ast_sip_subscription *sub;
+	struct ast_sip_pubsub_body_generator *generator;
+
+	/* If this subscription has already expired remove it */
+	if (ast_tvdiff_ms(persistence->expires, ast_tvnow()) <= 0) {
+		ast_sorcery_delete(ast_sip_get_sorcery(), persistence);
+		return 0;
+	}
+
+	endpoint = ast_sorcery_retrieve_by_id(ast_sip_get_sorcery(), "endpoint", persistence->endpoint);
+	if (!endpoint) {
+		ast_log(LOG_WARNING, "A subscription for '%s' could not be recreated as the endpoint was not found\n",
+			persistence->endpoint);
+		ast_sorcery_delete(ast_sip_get_sorcery(), persistence);
+		return 0;
+	}
+
+	pj_pool_reset(pool);
+	rdata.tp_info.pool = pool;
+
+	if (ast_sip_create_rdata(&rdata, persistence->packet, persistence->src_name, persistence->src_port,
+		persistence->transport_key, persistence->local_name, persistence->local_port)) {
+		ast_log(LOG_WARNING, "A subscription for '%s' could not be recreated as the message could not be parsed\n",
+			persistence->endpoint);
+		ast_sorcery_delete(ast_sip_get_sorcery(), persistence);
+		return 0;
+	}
+
+	/* Update the expiration header with the new expiration */
+	expires_header = pjsip_msg_find_hdr(rdata.msg_info.msg, PJSIP_H_EXPIRES, rdata.msg_info.msg->hdr.next);
+	if (!expires_header) {
+		expires_header = pjsip_expires_hdr_create(pool, 0);
+		if (!expires_header) {
+			return 0;
+		}
+		pjsip_msg_add_hdr(rdata.msg_info.msg, (pjsip_hdr*)expires_header);
+	}
+	expires_header->ivalue = (ast_tvdiff_ms(persistence->expires, ast_tvnow()) / 1000);
+
+	handler = subscription_get_handler_from_rdata(&rdata);
+	if (!handler) {
+		/* This may eventually get a handler once everything is loaded */
+		return 0;
+	}
+
+	generator = subscription_get_generator_from_rdata(&rdata, handler);
 	if (!generator) {
 		return 0;
 	}
@@ -1072,15 +1086,10 @@
 
 static pj_bool_t pubsub_on_rx_subscribe_request(pjsip_rx_data *rdata)
 {
-	char event[32];
-	char accept[AST_SIP_MAX_ACCEPT][64];
-	pjsip_accept_hdr *accept_header;
-	pjsip_event_hdr *event_header;
 	pjsip_expires_hdr *expires_header;
 	struct ast_sip_subscription_handler *handler;
 	RAII_VAR(struct ast_sip_endpoint *, endpoint, NULL, ao2_cleanup);
 	struct ast_sip_subscription *sub;
-	size_t num_accept_headers;
 	struct ast_sip_pubsub_body_generator *generator;
 
 	endpoint = ast_pjsip_rdata_get_endpoint(rdata);
@@ -1101,38 +1110,13 @@
 		return PJ_TRUE;
 	}
 
-	event_header = pjsip_msg_find_hdr_by_name(rdata->msg_info.msg, &str_event_name, rdata->msg_info.msg->hdr.next);
-	if (!event_header) {
-		ast_log(LOG_WARNING, "Incoming SUBSCRIBE request with no Event header\n");
+	handler = subscription_get_handler_from_rdata(rdata);
+	if (!handler) {
 		pjsip_endpt_respond_stateless(ast_sip_get_pjsip_endpoint(), rdata, 489, NULL, NULL, NULL);
 		return PJ_TRUE;
 	}
-	ast_copy_pj_str(event, &event_header->event_type, sizeof(event));
-
-	handler = find_sub_handler_for_event_name(event);
-	if (!handler) {
-		ast_log(LOG_WARNING, "No registered subscribe handler for event %s\n", event);
-		pjsip_endpt_respond_stateless(ast_sip_get_pjsip_endpoint(), rdata, 489, NULL, NULL, NULL);
-		return PJ_TRUE;
-	}
-
-	accept_header = pjsip_msg_find_hdr(rdata->msg_info.msg, PJSIP_H_ACCEPT, rdata->msg_info.msg->hdr.next);
-	if (accept_header) {
-		int i;
-
-		for (i = 0; i < accept_header->count; ++i) {
-			ast_copy_pj_str(accept[i], &accept_header->values[i], sizeof(accept[i]));
-		}
-		num_accept_headers = accept_header->count;
-	} else {
-		/* If a SUBSCRIBE contains no Accept headers, then we must assume that
-		 * the default accept type for the event package is to be used.
-		 */
-		ast_copy_string(accept[0], handler->default_accept, sizeof(accept[0]));
-		num_accept_headers = 1;
-	}
-
-	generator = find_body_generator(accept, num_accept_headers);
+
+	generator = subscription_get_generator_from_rdata(rdata, handler);
 	if (!generator) {
 		pjsip_endpt_respond_stateless(ast_sip_get_pjsip_endpoint(), rdata, 489, NULL, NULL, NULL);
 		return PJ_TRUE;
@@ -1869,7 +1853,7 @@
 		ast_log(LOG_ERROR, "Could not register subscription persistence object support\n");
 		ast_sip_unregister_service(&pubsub_module);
 		ast_sched_context_destroy(sched);
-		return AST_MODULE_LOAD_FAILURE;	
+		return AST_MODULE_LOAD_FAILURE;
 	}
 	ast_sorcery_object_field_register(sorcery, "subscription_persistence", "packet", "", OPT_CHAR_ARRAY_T, 0,
 		CHARFLDSET(struct subscription_persistence, packet));




More information about the asterisk-commits mailing list