[asterisk-commits] mmichelson: branch group/CCSS r232949 - /team/group/CCSS/channels/chan_sip.c

SVN commits to the Asterisk project asterisk-commits at lists.digium.com
Thu Dec 3 18:41:19 CST 2009


Author: mmichelson
Date: Thu Dec  3 18:41:15 2009
New Revision: 232949

URL: http://svnview.digium.com/svn/asterisk?view=rev&rev=232949
Log:
Break the PIDF validation into chunks.


Modified:
    team/group/CCSS/channels/chan_sip.c

Modified: team/group/CCSS/channels/chan_sip.c
URL: http://svnview.digium.com/svn/asterisk/team/group/CCSS/channels/chan_sip.c?view=diff&rev=232949&r1=232948&r2=232949
==============================================================================
--- team/group/CCSS/channels/chan_sip.c (original)
+++ team/group/CCSS/channels/chan_sip.c Thu Dec  3 18:41:15 2009
@@ -23317,6 +23317,113 @@
 	return SIP_PUBLISH_UNKNOWN;
 }
 
+static int pidf_validate_tuple(struct ast_xml_node *tuple_node)
+{
+	const char *id;
+	int status_found = FALSE;
+	struct ast_xml_node *tuple_children;
+	struct ast_xml_node *tuple_children_iterator;
+	/* Tuples have to have an id attribute or they're invalid */
+	if (!(id = ast_xml_get_attribute(tuple_node, "id"))) {
+		return FALSE;
+	}
+	/* We don't care what it actually is, just that it's there */
+	ast_xml_free_attr(id);
+	/* This is a tuple. It must have a status element */
+	if (!(tuple_children = ast_xml_node_get_children(tuple_node))) {
+		/* The tuple has no children. It sucks */
+		return FALSE;
+	}
+	for (tuple_children_iterator = tuple_children; tuple_children_iterator;
+			tuple_children_iterator = ast_xml_node_get_next(tuple_children_iterator)) {
+		/* Similar to the wording used regarding tuples, the status element should appear
+		 * first. However, we will once again relax things and accept the status at any
+		 * position. We will enforce that only a single status element can be present.
+		 */
+		if (strcmp(ast_xml_node_get_name(tuple_children_iterator), "status")) {
+			/* Not the status, we don't care */
+			continue;
+		}
+		if (status_found == TRUE) {
+			/* THERE CAN BE ONLY ONE!!! */
+			return FALSE;
+		}
+		status_found = TRUE;
+	}
+	return status_found;
+}
+
+
+static int pidf_validate_presence(struct ast_xml_doc *doc)
+{
+	struct ast_xml_node *presence_node = ast_xml_get_root(doc);
+	struct ast_xml_node *child_nodes;
+	struct ast_xml_node *node_iterator;
+	const char *entity;
+	const char *namespace;
+	
+	if (!presence_node) {
+		return FALSE;
+	}
+	/* Okay, we managed to open the document! YAY! Now, let's start making sure it's all PIDF-ified
+	 * correctly.
+	 */
+	presence_node = ast_xml_get_root(doc);
+	if (strcmp(ast_xml_node_get_name(presence_node), "presence")) {
+		ast_log(LOG_WARNING, "Root node of PIDF document is not 'presence'. Invalid\n");
+		return FALSE;
+	}
+
+	/* The presence element must have an entity attribute and an xmlns attribute. Furthermore
+	 * the xmlns attribute must be "urn:ietf:params:xml:ns:pidf"
+	 */
+	if (!(entity = ast_xml_get_attribute(presence_node, "entity"))) {
+		ast_log(LOG_WARNING, "Presence element of PIDF document has no 'entity' attribute\n");
+		return FALSE;
+	}
+	/* We're not interested in what the entity is, just that it exists */
+	ast_xml_free_attr(entity);
+
+	if (!(namespace = ast_xml_get_attribute(presence_node, "xmlns"))) {
+		ast_log(LOG_WARNING, "Presence element of PIDF document has no 'xmlns' attribute\n");
+		return FALSE;
+	}
+
+	if (strcmp(namespace, "urn:ietf:params:xml:ns:pidf")) {
+		ast_log(LOG_WARNING, "Improper XML namespace in PIDF document\n");
+		ast_xml_free_attr(namespace);
+		return FALSE;
+	}
+
+	ast_xml_free_attr(namespace);
+
+	if (!(child_nodes = ast_xml_node_get_children(presence_node))) {
+		ast_log(LOG_WARNING, "PIDF document has no elements as children of 'presence'. Invalid\n");
+		return FALSE;
+	}
+
+	/* Check for tuple elements. RFC 3863 says that PIDF documents can have any number of
+	 * tuples, including 0. The big thing here is that if there are tuple elements present,
+	 * they have to have a single status element within.
+	 *
+	 * The RFC is worded such that tuples should appear as the first elements as children of
+	 * the presence element. However, we'll be accepting of documents which may place other elements
+	 * before the tuple(s).
+	 */
+	for (node_iterator = child_nodes; node_iterator;
+			node_iterator = ast_xml_node_get_next(node_iterator)) {
+		if (strcmp(ast_xml_node_get_name(node_iterator), "tuple")) {
+			/* Not a tuple. We don't give a rat's hind quarters */
+			continue;
+		}
+		if (pidf_validate_tuple(node_iterator) == FALSE) {
+			return FALSE;
+		}
+	}
+
+	return TRUE;
+}
+
 /*!
  * \brief Makes sure that body is properly formatted PIDF
  *
@@ -23334,14 +23441,10 @@
 static int sip_pidf_validate(struct sip_request *req)
 {
 	struct ast_xml_doc *doc;
-	struct ast_xml_node *root_node;
-	struct ast_xml_node *node_iterator;
-	struct ast_xml_node *child_nodes;
 	int content_length;
 	const char *content_length_str = get_header(req, "Content-Length");
 	const char *content_type = get_header(req, "Content-Type");
-	const char *entity;
-	const char *namespace;
+	int res;
 
 	if (ast_strlen_zero(content_type) || strcmp(content_type, "application/xml+pidf")) {
 		ast_log(LOG_WARNING, "Content type is not PIDF\n");
@@ -23363,100 +23466,9 @@
 		return FALSE;
 	}
 
-	/* Okay, we managed to open the document! YAY! Now, let's start making sure it's all PIDF-ified
-	 * correctly.
-	 */
-	root_node = ast_xml_get_root(doc);
-	if (strcmp(ast_xml_node_get_name(root_node), "presence")) {
-		ast_log(LOG_WARNING, "Root node of PIDF document is not 'presence'. Invalid\n");
-		goto fail;
-	}
-
-	/* The presence element must have an entity attribute and an xmlns attribute. Furthermore
-	 * the xmlns attribute must be "urn:ietf:params:xml:ns:pidf"
-	 */
-	if (!(entity = ast_xml_get_attribute(root_node, "entity"))) {
-		ast_log(LOG_WARNING, "Presence element of PIDF document has no 'entity' attribute\n");
-		goto fail;
-	}
-	/* We're not interested in what the entity is, just that it exists */
-	ast_xml_free_attr(entity);
-
-	if (!(namespace = ast_xml_get_attribute(root_node, "xmlns"))) {
-		ast_log(LOG_WARNING, "Presence element of PIDF document has no 'xmlns' attribute\n");
-		goto fail;
-	}
-
-	if (strcmp(namespace, "urn:ietf:params:xml:ns:pidf")) {
-		ast_log(LOG_WARNING, "Improper XML namespace in PIDF document\n");
-		ast_xml_free_attr(namespace);
-		goto fail;
-	}
-
-	ast_xml_free_attr(namespace);
-
-	if (!(child_nodes = ast_xml_node_get_children(root_node))) {
-		ast_log(LOG_WARNING, "PIDF document has no elements as children of 'presence'. Invalid\n");
-		goto fail;
-	}
-
-	/* Check for tuple elements. RFC 3863 says that PIDF documents can have any number of
-	 * tuples, including 0. The big thing here is that if there are tuple elements present,
-	 * they have to have a single status element within.
-	 *
-	 * The RFC is worded such that tuples should appear as the first elements as children of
-	 * the presence element. However, we'll be accepting of documents which may place other elements
-	 * before the tuple(s).
-	 */
-	for (node_iterator = child_nodes; node_iterator;
-			node_iterator = ast_xml_node_get_next(node_iterator)) {
-		struct ast_xml_node *tuple_children;
-		struct ast_xml_node *tuple_children_iterator;
-		const char *id;
-		int status_found = FALSE;
-		if (strcmp(ast_xml_node_get_name(node_iterator), "tuple")) {
-			/* Not a tuple. We don't give a rat's hind quarters */
-			continue;
-		}
-		/* Tuples have to have an id attribute or they're invalid */
-		if (!(id = ast_xml_get_attribute(node_iterator, "id"))) {
-			goto fail;
-		}
-		/* We don't care what it actually is, just that it's there */
-		ast_xml_free_attr(entity);
-		/* This is a tuple. It must have a status element */
-		if (!(tuple_children = ast_xml_node_get_children(node_iterator))) {
-			/* The tuple has no children. It sucks */
-			goto fail;
-		}
-		for (tuple_children_iterator = tuple_children; tuple_children_iterator;
-				tuple_children_iterator = ast_xml_node_get_next(tuple_children_iterator)) {
-			/* Similar to the wording used regarding tuples, the status element should appear
-			 * first. However, we will once again relax things and accept the status at any
-			 * position. We will enforce that only a single status element can be present.
-			 */
-			if (strcmp(ast_xml_node_get_name(tuple_children_iterator), "status")) {
-				/* Not the status, we don't care */
-				continue;
-			}
-			if (status_found == TRUE) {
-				/* THERE CAN BE ONLY ONE!!! */
-				goto fail;
-			}
-			status_found = TRUE;
-		}
-		if (status_found == FALSE) {
-			/* The tuple had no status element... */
-			goto fail;
-		}
-	}
-
+	res = pidf_validate_presence(doc);
 	ast_xml_close(doc);
-	return TRUE;
-
-fail:
-	ast_xml_close(doc);
-	return FALSE;
+	return res;
 }
 
 static int cc_esc_publish_initial_handler(struct sip_pvt *pvt, struct sip_request *req, struct event_state_compositor *esc, struct sip_esc_entry *esc_entry)




More information about the asterisk-commits mailing list