]> git.ipfire.org Git - thirdparty/asterisk.git/commitdiff
res_pjsip: Backport pjsip uri utilities.
authorGeorge Joseph <gjoseph@sangoma.com>
Tue, 25 Mar 2025 22:22:04 +0000 (16:22 -0600)
committerAsterisk Development Team <asteriskteam@digium.com>
Mon, 2 Jun 2025 13:37:25 +0000 (13:37 +0000)
The following utilities have been backported:

ast_sip_is_uri_sip_sips
ast_sip_is_allowed_uri
ast_sip_pjsip_uri_get_username
ast_sip_pjsip_uri_get_hostname
ast_sip_pjsip_uri_get_other_param

They were originally included in the commit for supporting TEL uris.
Support for TEL uris is NOT included here however.

(cherry picked from commit e1a205074bd85176e23163fd5a8f548457b8d7d5)

include/asterisk/res_pjsip.h
res/res_pjsip.c

index 876bb1cfb63598e3889dcfca5ce3b767c6522981..e24be1c440cf721e3267799e7906e4a1463c6f5b 100644 (file)
@@ -120,6 +120,8 @@ struct pjsip_tpselector;
 
 AST_VECTOR(ast_sip_service_route_vector, char *);
 
+static const pj_str_t AST_PJ_STR_EMPTY = { "", 0 };
+
 /*!
  * \brief Structure for SIP transport information
  */
@@ -3757,6 +3759,79 @@ void ast_sip_transport_state_register(struct ast_sip_tpmgr_state_callback *eleme
  */
 void ast_sip_transport_state_unregister(struct ast_sip_tpmgr_state_callback *element);
 
+/*!
+ * \brief Check whether a pjsip_uri is SIP/SIPS or not
+ * \since 16.28.0
+ *
+ * \param uri The pjsip_uri to check
+ *
+ * \retval 1 if true
+ * \retval 0 if false
+ */
+int ast_sip_is_uri_sip_sips(pjsip_uri *uri);
+
+/*!
+ * \brief Check whether a pjsip_uri is allowed or not
+ * \since 16.28.0
+ *
+ * \param uri The pjsip_uri to check
+ *
+ * \retval 1 if allowed
+ * \retval 0 if not allowed
+ */
+int ast_sip_is_allowed_uri(pjsip_uri *uri);
+
+/*!
+ * \brief Get the user portion of the pjsip_uri
+ * \since 16.28.0
+ *
+ * \param uri The pjsip_uri to get the user from
+ *
+ * \note This function will check what kind of URI it receives and return
+ * the user based off of that
+ *
+ * \return User string or empty string if not present
+ */
+const pj_str_t *ast_sip_pjsip_uri_get_username(pjsip_uri *uri);
+
+/*!
+ * \brief Get the host portion of the pjsip_uri
+ * \since 16.28.0
+ *
+ * \param uri The pjsip_uri to get the host from
+ *
+ * \note This function will check what kind of URI it receives and return
+ * the host based off of that
+ *
+ * \return Host string or empty string if not present
+ */
+const pj_str_t *ast_sip_pjsip_uri_get_hostname(pjsip_uri *uri);
+
+/*!
+ * \brief Find an 'other' SIP/SIPS URI parameter by name
+ * \since 16.28.0
+ *
+ * A convenience function to find a named parameter from a SIP/SIPS URI. This
+ * function will not find the following standard SIP/SIPS URI parameters which
+ * are stored separately by PJSIP:
+ *
+ * \li `user`
+ * \li `method`
+ * \li `transport`
+ * \li `ttl`
+ * \li `lr`
+ * \li `maddr`
+ *
+ * \param uri The pjsip_uri to get the parameter from
+ * \param param_str The name of the parameter to find
+ *
+ * \note This function will check what kind of URI it receives and return
+ * the parameter based off of that
+ *
+ * \return Find parameter or NULL if not present
+ */
+struct pjsip_param *ast_sip_pjsip_uri_get_other_param(pjsip_uri *uri, const pj_str_t *param_str);
+
 /*!
  * \brief Convert name to SIP response code
  *
index e5ebcb536a95ff550b102196a60dddba420d6257..00a40f7be9b4be746c3a07c66a2adc24e098bcb1 100644 (file)
@@ -2504,6 +2504,54 @@ struct ast_threadpool *ast_sip_threadpool(void)
        return sip_threadpool;
 }
 
+int ast_sip_is_uri_sip_sips(pjsip_uri *uri)
+{
+       return (PJSIP_URI_SCHEME_IS_SIP(uri) || PJSIP_URI_SCHEME_IS_SIPS(uri));
+}
+
+int ast_sip_is_allowed_uri(pjsip_uri *uri)
+{
+       return (ast_sip_is_uri_sip_sips(uri));
+}
+
+const pj_str_t *ast_sip_pjsip_uri_get_username(pjsip_uri *uri)
+{
+       if (ast_sip_is_uri_sip_sips(uri)) {
+               pjsip_sip_uri *sip_uri = pjsip_uri_get_uri(uri);
+               if (!sip_uri) {
+                       return &AST_PJ_STR_EMPTY;
+               }
+               return &sip_uri->user;
+       }
+
+       return &AST_PJ_STR_EMPTY;
+}
+
+const pj_str_t *ast_sip_pjsip_uri_get_hostname(pjsip_uri *uri)
+{
+       if (ast_sip_is_uri_sip_sips(uri)) {
+               pjsip_sip_uri *sip_uri = pjsip_uri_get_uri(uri);
+               if (!sip_uri) {
+                       return &AST_PJ_STR_EMPTY;
+               }
+               return &sip_uri->host;
+       }
+
+       return &AST_PJ_STR_EMPTY;
+}
+
+struct pjsip_param *ast_sip_pjsip_uri_get_other_param(pjsip_uri *uri, const pj_str_t *param_str)
+{
+       if (ast_sip_is_uri_sip_sips(uri)) {
+               pjsip_sip_uri *sip_uri = pjsip_uri_get_uri(uri);
+               if (!sip_uri) {
+                       return NULL;
+               }
+               return pjsip_param_find(&sip_uri->other_param, param_str);
+       }
+
+       return NULL;
+}
 
 struct response_code_map {
        int code;