[Asterisk-code-review] res_pjsip: Add utils for checking media types (asterisk[19])

Joshua Colp asteriskteam at digium.com
Mon Jan 17 08:27:31 CST 2022


Joshua Colp has submitted this change. ( https://gerrit.asterisk.org/c/asterisk/+/17857 )

Change subject: res_pjsip: Add utils for checking media types
......................................................................

res_pjsip: Add utils for checking media types

Added two new functions to assist checking media types...

* ast_sip_are_media_types_equal compares two pjsip_media_types.
* ast_sip_is_media_type_in tests if one media type is in a list
  of others.

Added static definitions for commonly used media types to
res_pjsip.h.

Changed several modules to use the new functions and static
definitions.

ASTERISK_29813
(not ready to close)

Change-Id: Ief77675235bd3bf00a6b095d4673fd878d0801b9
---
M include/asterisk/res_pjsip.h
M res/res_pjsip.c
M res/res_pjsip.exports.in
M res/res_pjsip_sdp_rtp.c
M res/res_pjsip_session.c
5 files changed, 97 insertions(+), 7 deletions(-)

Approvals:
  Joshua Colp: Looks good to me, but someone else must approve; Approved for Submit
  George Joseph: Looks good to me, approved



diff --git a/include/asterisk/res_pjsip.h b/include/asterisk/res_pjsip.h
index ac4725e..9f35393 100644
--- a/include/asterisk/res_pjsip.h
+++ b/include/asterisk/res_pjsip.h
@@ -953,6 +953,40 @@
 #define AST_SIP_X_AST_TXP "x-ast-txp"
 #define AST_SIP_X_AST_TXP_LEN 9
 
+/*! Common media types used throughout res_pjsip and pjproject */
+extern pjsip_media_type pjsip_media_type_application_json;
+extern pjsip_media_type pjsip_media_type_application_media_control_xml;
+extern pjsip_media_type pjsip_media_type_application_pidf_xml;
+extern pjsip_media_type pjsip_media_type_application_xpidf_xml;
+extern pjsip_media_type pjsip_media_type_application_cpim_xpidf_xml;
+extern pjsip_media_type pjsip_media_type_application_rlmi_xml;
+extern pjsip_media_type pjsip_media_type_application_simple_message_summary;
+extern pjsip_media_type pjsip_media_type_application_sdp;
+extern pjsip_media_type pjsip_media_type_multipart_alternative;
+extern pjsip_media_type pjsip_media_type_multipart_mixed;
+extern pjsip_media_type pjsip_media_type_multipart_related;
+extern pjsip_media_type pjsip_media_type_text_plain;
+
+/*!
+ * \brief Compare pjsip media types
+ *
+ * \param pjsip_media_type a
+ * \param pjsip_media_type b
+ * \retval 1 Media types are equal
+ * \retval 0 Media types are not equal
+ */
+int ast_sip_are_media_types_equal(pjsip_media_type *a, pjsip_media_type *b);
+
+/*!
+ * \brief Check if a media type is in a list of others
+ *
+ * \param a pjsip_media_type to search for
+ * \param ... one or more pointers to pjsip_media_types the last of which must be "SENTINEL"
+ * \retval 1 Media types are equal
+ * \retval 0 Media types are not equal
+ */
+int ast_sip_is_media_type_in(pjsip_media_type *a, ...) attribute_sentinel;
+
 /*!
  * \brief Initialize an auth vector with the configured values.
  *
diff --git a/res/res_pjsip.c b/res/res_pjsip.c
index 697767d..2f9edfb 100644
--- a/res/res_pjsip.c
+++ b/res/res_pjsip.c
@@ -5292,6 +5292,34 @@
 	return res;
 }
 
+int ast_sip_are_media_types_equal(pjsip_media_type *a, pjsip_media_type *b)
+{
+	int rc = 0;
+	if (a != NULL && b != NULL) {
+	    rc = pjsip_media_type_cmp(a, b, 0) ? 0 : 1;
+	}
+	return rc;
+}
+
+int ast_sip_is_media_type_in(pjsip_media_type *a, ...)
+{
+	int rc = 0;
+	pjsip_media_type *b = NULL;
+	va_list ap;
+
+	ast_assert(a != NULL);
+	va_start(ap, a);
+
+	while ((b = va_arg(ap, pjsip_media_type *)) != (pjsip_media_type *)SENTINEL) {
+		if (pjsip_media_type_cmp(a, b, 0) == 0) {
+			rc = 1;
+			break;
+		}
+	}
+	va_end(ap);
+
+	return rc;
+}
 
 int ast_sip_is_content_type(pjsip_media_type *content_type, char *type, char *subtype)
 {
@@ -5844,6 +5872,20 @@
 	pjmedia_strerror(0, NULL, 0);
 }
 
+/* Definitions of media types declared "extern" in res_pjsip.h */
+pjsip_media_type pjsip_media_type_application_json;
+pjsip_media_type pjsip_media_type_application_media_control_xml;
+pjsip_media_type pjsip_media_type_application_pidf_xml;
+pjsip_media_type pjsip_media_type_application_xpidf_xml;
+pjsip_media_type pjsip_media_type_application_cpim_xpidf_xml;
+pjsip_media_type pjsip_media_type_application_rlmi_xml;
+pjsip_media_type pjsip_media_type_application_simple_message_summary;
+pjsip_media_type pjsip_media_type_application_sdp;
+pjsip_media_type pjsip_media_type_multipart_alternative;
+pjsip_media_type pjsip_media_type_multipart_mixed;
+pjsip_media_type pjsip_media_type_multipart_related;
+pjsip_media_type pjsip_media_type_text_plain;
+
 static int load_module(void)
 {
 	struct ast_threadpool_options options;
@@ -5863,6 +5905,21 @@
 		ast_log(LOG_WARNING, "Failed to register pjmedia error codes.  Codes will not be decoded.\n");
 	}
 
+	/* Initialize common media types */
+	pjsip_media_type_init2(&pjsip_media_type_application_json, "application", "json");
+	pjsip_media_type_init2(&pjsip_media_type_application_media_control_xml, "application", "media_control+xml");
+	pjsip_media_type_init2(&pjsip_media_type_application_pidf_xml, "application", "pidf+xml");
+	pjsip_media_type_init2(&pjsip_media_type_application_xpidf_xml, "application", "xpidf+xml");
+	pjsip_media_type_init2(&pjsip_media_type_application_cpim_xpidf_xml, "application", "cpim-xpidf+xml");
+	pjsip_media_type_init2(&pjsip_media_type_application_rlmi_xml, "application", "rlmi+xml");
+	pjsip_media_type_init2(&pjsip_media_type_application_sdp, "application", "sdp");
+	pjsip_media_type_init2(&pjsip_media_type_application_simple_message_summary, "application",	"simple-message-summary");
+	pjsip_media_type_init2(&pjsip_media_type_multipart_alternative, "multipart", "alternative");
+	pjsip_media_type_init2(&pjsip_media_type_multipart_mixed, "multipart", "mixed");
+	pjsip_media_type_init2(&pjsip_media_type_multipart_related, "multipart", "related");
+	pjsip_media_type_init2(&pjsip_media_type_text_plain, "text", "plain");
+
+
 	if (ast_sip_initialize_system()) {
 		ast_log(LOG_ERROR, "Failed to initialize SIP 'system' configuration section. Aborting load\n");
 		goto error;
diff --git a/res/res_pjsip.exports.in b/res/res_pjsip.exports.in
index 7ac2b7e..58868e3 100644
--- a/res/res_pjsip.exports.in
+++ b/res/res_pjsip.exports.in
@@ -2,6 +2,7 @@
 	global:
 		LINKER_SYMBOL_PREFIXast_sip_*;
 		LINKER_SYMBOL_PREFIX__ast_sip_*;
+		LINKER_SYMBOL_PREFIXpjsip_media_type_*;
 		LINKER_SYMBOL_PREFIXast_copy_pj_str;
 		LINKER_SYMBOL_PREFIXast_copy_pj_str2;
 		LINKER_SYMBOL_PREFIXast_pjsip_rdata_get_endpoint;
diff --git a/res/res_pjsip_sdp_rtp.c b/res/res_pjsip_sdp_rtp.c
index 10db010..a5e0b78 100644
--- a/res/res_pjsip_sdp_rtp.c
+++ b/res/res_pjsip_sdp_rtp.c
@@ -2317,9 +2317,8 @@
 	pjsip_tx_data *tdata;
 
 	if (!session->channel
-		|| !ast_sip_is_content_type(&rdata->msg_info.msg->body->content_type,
-			"application",
-			"media_control+xml")) {
+		|| !ast_sip_are_media_types_equal(&rdata->msg_info.msg->body->content_type,
+			&pjsip_media_type_application_media_control_xml)) {
 		return 0;
 	}
 
diff --git a/res/res_pjsip_session.c b/res/res_pjsip_session.c
index e915429..cfd71fb 100644
--- a/res/res_pjsip_session.c
+++ b/res/res_pjsip_session.c
@@ -3893,9 +3893,8 @@
 	pjsip_ctype_hdr *ctype_hdr = rdata->msg_info.ctype;
 
 	if (body && ctype_hdr &&
-		!pj_stricmp2(&ctype_hdr->media.type, "multipart") &&
-		(!pj_stricmp2(&ctype_hdr->media.subtype, "mixed") ||
-		 !pj_stricmp2(&ctype_hdr->media.subtype, "alternative"))) {
+		ast_sip_is_media_type_in(&ctype_hdr->media, &pjsip_media_type_multipart_mixed,
+			&pjsip_media_type_multipart_alternative, SENTINEL)) {
 		pjsip_multipart_part *part = pjsip_multipart_get_first_part(body);
 		while (part != NULL) {
 			if (check_content_disposition_in_multipart(part)) {
@@ -5488,7 +5487,7 @@
 
 	/* SDP produced by us directly will never be multipart */
 	if (!transport_state || hook || !tdata->msg->body ||
-		!ast_sip_is_content_type(&tdata->msg->body->content_type, "application", "sdp") ||
+		!ast_sip_are_media_types_equal(&tdata->msg->body->content_type, &pjsip_media_type_application_sdp) ||
 		ast_strlen_zero(transport->external_media_address)) {
 		return;
 	}

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

Gerrit-Project: asterisk
Gerrit-Branch: 19
Gerrit-Change-Id: Ief77675235bd3bf00a6b095d4673fd878d0801b9
Gerrit-Change-Number: 17857
Gerrit-PatchSet: 3
Gerrit-Owner: George Joseph <gjoseph at digium.com>
Gerrit-Reviewer: Friendly Automation
Gerrit-Reviewer: George Joseph <gjoseph at digium.com>
Gerrit-Reviewer: Joshua Colp <jcolp at sangoma.com>
Gerrit-MessageType: merged
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.digium.com/pipermail/asterisk-code-review/attachments/20220117/05ad6f8d/attachment-0001.html>


More information about the asterisk-code-review mailing list