[Asterisk-code-review] res_pjsip_messaging: Refactor outgoing URI processing (asterisk[16])

George Joseph asteriskteam at digium.com
Tue Apr 27 12:27:48 CDT 2021


George Joseph has uploaded this change for review. ( https://gerrit.asterisk.org/c/asterisk/+/15828 )


Change subject: res_pjsip_messaging: Refactor outgoing URI processing
......................................................................

res_pjsip_messaging: Refactor outgoing URI processing

 * Implemented the new "to" parameter of the MessageSend()
   dialplan application.  This allows a user to specify
   a complete SIP "To" header separate from the Request URI.

 * Completely refactored the get_outbound_endpoint() function
   to actually handle all the destination combinations that
   we advertized as supporting.

 * Added lots of debugging.

ASTERISK-29404
Reported by Brian J. Murrell

Change-Id: I67a485196d9199916468f7f98bfb9a0b993a4cce
---
M res/res_pjsip_messaging.c
1 file changed, 444 insertions(+), 103 deletions(-)



  git pull ssh://gerrit.asterisk.org:29418/asterisk refs/changes/28/15828/1

diff --git a/res/res_pjsip_messaging.c b/res/res_pjsip_messaging.c
index 9287324..8a01e9c 100644
--- a/res/res_pjsip_messaging.c
+++ b/res/res_pjsip_messaging.c
@@ -25,15 +25,83 @@
 
 /*** DOCUMENTATION
 	<info name="MessageDestinationInfo" language="en_US" tech="PJSIP">
-		<para>Specifying a prefix of <literal>pjsip:</literal> will send the
-		message as a SIP MESSAGE request.</para>
+		<para>The <literal>destination</literal> parameter is used to construct
+		the Request URI for an outgoing message.  It can be in one of the following
+		formats, all prefixed with the <literal>pjsip:</literal> message tech.</para>
+		<para>
+		</para>
+		<enumlist>
+			<enum name="endpoint">
+				<para>Request URI comes from the endpoint's default aor and contact.</para>
+			</enum>
+			<enum name="endpoint/aor">
+				<para>Request URI comes from the specific aor/contact.</para>
+			</enum>
+			<enum name="endpoint at domain">
+				<para>Request URI from the endpoint's default aor and contact.  The domain is discarded.</para>
+			</enum>
+		</enumlist>
+		<para>
+		</para>
+		<para>These all use the endpoint to send the message with the specified URI:</para>
+		<para>
+		</para>
+		<enumlist>
+ 			<enum name="endpoint/<sip[s]:host>>"/>
+ 			<enum name="endpoint/<sip[s]:user at host>"/>
+ 			<enum name="endpoint/"display name" <sip[s]:host>"/>
+ 			<enum name="endpoint/"display name" <sip[s]:user at host>"/>
+ 			<enum name="endpoint/sip[s]:host"/>
+ 			<enum name="endpoint/sip[s]:user at host"/>
+ 			<enum name="endpoint/host"/>
+ 			<enum name="endpoint/user at host"/>
+ 		</enumlist>
+		<para>
+		</para>
+		<para>These all use the default endpoint to send the message with the specified URI:</para>
+		<para>
+		</para>
+ 	 	<enumlist>
+ 			<enum name="<sip[s]:host>"/>
+ 			<enum name="<sip[s]:user at host>"/>
+ 			<enum name=""display name" <sip[s]:host>"/>
+ 			<enum name=""display name" <sip[s]:user at host>"/>
+ 			<enum name="sip[s]:host"/>
+ 			<enum name="sip[s]:user at host"/>
+ 	 	</enumlist>
+		<para>
+		</para>
+ 	 	<para>These use the default endpoint to send the message with the specified host:</para>
+		<para>
+		</para>
+ 	 	<enumlist>
+ 			<enum name="host"/>
+ 			<enum name="user at host"/>
+ 	 	</enumlist>
+ 		<para>
+ 		</para>
+ 		<para>In all cases URIs not already prefixed with <literal>sip:</literal> or
+			<literal>sips:</literal> will automatically be prefixed with <literal>sip:</literal>.</para>
+ 		<para>
+ 		</para>
 	</info>
 	<info name="MessageFromInfo" language="en_US" tech="PJSIP">
-		<para>The <literal>from</literal> parameter can be a configured endpoint
-		or in the form of "display-name" <URI>.</para>
+		<para>The <literal>from</literal> parameter is used to specity the <literal>From:</literal>
+		header in the outgoing SIP MESSAGE.  It will override the value specified in
+		MESSAGE(from) which itself will override any <literal>from</literal> value from
+		an incoming SIP MESSAGE.
+		</para>
+ 		<para>
+ 		</para>
 	</info>
 	<info name="MessageToInfo" language="en_US" tech="PJSIP">
-		<para>Ignored</para>
+		<para>The <literal>to</literal> parameter is used to specity the <literal>To:</literal>
+		header in the outgoing SIP MESSAGE.  It will override the value specified in
+		MESSAGE(to) which itself will override any <literal>to</literal> value from
+		an incoming SIP MESSAGE.
+		</para>
+ 		<para>
+ 		</para>
 	</info>
  ***/
 #include "asterisk.h"
@@ -47,6 +115,7 @@
 #include "asterisk/res_pjsip.h"
 #include "asterisk/res_pjsip_session.h"
 #include "asterisk/taskprocessor.h"
+#include "asterisk/uri.h"
 
 const pjsip_method pjsip_message_method = {PJSIP_OTHER_METHOD, {"MESSAGE", 7} };
 
@@ -114,133 +183,347 @@
 
 /*!
  * \internal
- * \brief Puts pointer past 'sip[s]:' string that should be at the
- * front of the given 'fromto' parameter
+ * \brief Retrieves an endpoint and URI from the "to" string.
  *
- * \param fromto 'From' or 'To' field containing 'sip:'
- */
-static const char *skip_sip(const char *fromto)
-{
-	const char *p;
-
-	/* need to be one past 'sip:' or 'sips:' */
-	if (!(p = strstr(fromto, "sip"))) {
-		return fromto;
-	}
-
-	p += 3;
-	if (*p == 's') {
-		++p;
-	}
-
-	return ++p;
-}
-
-/*!
- * \internal
- * \brief Retrieves an endpoint if specified in the given 'to'
+ * This URI is used as the Request URI.
  *
  * Expects the given 'to' to be in one of the following formats:
- *      sip[s]:endpoint[/aor]
- *      sip[s]:endpoint[/uri] - Where uri is: sip[s]:user at domain
- *      sip[s]:endpoint[@domain]
- *      sip[s]:unknown_user at domain <-- will use default outbound endpoint
+ * Why we allow so many is a mystery.
  *
- * If an optional aor is given it will try to find an associated uri
- * to return.  If an optional uri is given then that will be returned,
- * otherwise uri will be NULL.
+ *      endpoint             - We'll get URI from the default aor/contact
+ *      endpoint/aor         - We'll get the URI from the specific aor/contact
+ *      endpoint at domain      - We toss the domain part and just use the endpoint
  *
- * \param to 'From' or 'To' field with possible endpoint
- * \param uri Optional uri to return
+ *      These all use the endpoint and specified URI
+ *      endpoint/<sip[s]:host>
+ *      endpoint/<sip[s]:user at host>
+ *      endpoint/"Bob" <sip[s]:host>
+ *      endpoint/"Bob" <sip[s]:user at host>
+ *      endpoint/sip[s]:host
+ *      endpoint/sip[s]:user at host
+ *      endpoint/host
+ *      endpoint/user at host
+ *
+ *      These all use the default endpoint and specified URI
+ *      <sip[s]:host>
+ *      <sip[s]:user at host>
+ *      "Bob" <sip[s]:host>
+ *      "Bob" <sip[s]:user at host>
+ *      sip[s]:host
+ *      sip[s]:user at host
+ *
+ *      These use the default endpoint and specified host
+ *      host
+ *      user at host
+ *
+ * The ones that have the sip[s] scheme are the easiest to parse.
+ * The rest all have some issue.
+ *
+ *      endpoint vs host              : We have to test for endpoint first
+ *      endpoint/aor vs endpoint/host : We have to test for aor first
+ *                                      What if there's an aor with the same
+ *                                      name as the host?
+ *      endpoint at domain vs user at host  : We have to test for endpoint first.
+ *                                      What if there's an endpoint with the
+ *                                      same name as the user?
+ *
+ * \param to 'To' field with possible endpoint
+ * \param uri Pointer to a char* which will be set to the URI.
+ *            Must be ast_free'd by the caller.
+ *
+ * \note  The logic below could probably be condensed but then it wouldn't be
+ * as clear.
  */
 static struct ast_sip_endpoint *get_outbound_endpoint(const char *to, char **uri)
 {
-	char *name;
-	char *aor_uri;
-	struct ast_sip_endpoint *endpoint;
+	char *destination;
+	char *endpoint_name = NULL;
+	char *slash = NULL;
+	char *atsign = NULL;
+	char *scheme = NULL;
+	struct ast_sip_endpoint *endpoint = NULL;
 
-	name = ast_strdupa(skip_sip(to));
+	destination = ast_strdupa(to);
 
-	/* attempt to extract the endpoint name */
-	if ((aor_uri = strchr(name, '/'))) {
-		/* format was 'endpoint/(aor_name | uri)' */
-		*aor_uri++ = '\0';
-	} else if ((aor_uri = strchr(name, '@'))) {
-		/* format was 'endpoint at domain' - discard the domain */
-		*aor_uri = '\0';
+	slash = strchr(destination, '/');
+	atsign = strchr(destination, '@');
+	scheme = S_OR(strstr(destination, "sip:"), strstr(destination, "sips:"));
+
+	/* If it's just one word, it could be an endpoint name or just a hostname */
+	if (!slash && !atsign && !scheme) {
+		endpoint = ast_sorcery_retrieve_by_id(ast_sip_get_sorcery(), "endpoint", destination);
+		if (endpoint) {
+			/* It's an endpoint */
+			struct ast_sip_contact *contact = NULL;
+
+			endpoint_name = destination;
+			contact = ast_sip_location_retrieve_contact_from_aor_list(endpoint->aors);
+			if (!contact) {
+				/*
+				 * We're getting the contact using the same method as
+				 * ast_sip_create_request() so if there's no contact
+				 * we can never send this message.
+				 */
+				ao2_cleanup(endpoint);
+				endpoint = NULL;
+				*uri = NULL;
+				ast_debug(3, "Dest: FAIL: '%s' Found endpoint '%s' but didn't find an aor/contact for it\n",
+					to, endpoint_name);
+			} else {
+				ao2_cleanup(contact);
+				*uri = ast_strdup(contact->uri);
+				ast_debug(3, "Dest: '%s' Found endpoint '%s' and found contact with URI '%s'\n",
+					to, endpoint_name, *uri);
+			}
+		} else {
+			/*
+			 * It's a hostname that we need to add a scheme to
+			 */
+			char *temp_uri = ast_malloc(strlen(destination) + strlen("sip:") + 1);
+
+			sprintf(temp_uri, "sip:%s", destination);
+			*uri = temp_uri;
+			endpoint = ast_sip_default_outbound_endpoint();
+			ast_debug(3, "Dest: '%s' Didn't find endpoint so adding scheme and using URI '%s' with default endpoint\n",
+				to, *uri);
+		}
+		/* There's nothing left to parse so... */
+		return endpoint;
+	}
+
+	/* If there's a '/', it MUST be following an endpoint name */
+	if (slash) {
+		char *afterslash = slash + 1;
+		*slash = '\0';
+		endpoint_name = destination;
+		endpoint = ast_sorcery_retrieve_by_id(ast_sip_get_sorcery(), "endpoint", endpoint_name);
+		/*
+		 * If there's a slash but there's isn't a valid endpoint name
+		 * before it, there's nothing else we can do.
+		 */
+		if (!endpoint) {
+			*uri = NULL;
+			ast_debug(3, "Dest: '%s' FAIL: Didn't find endpoint with name '%s' before the slash\n",
+				to, endpoint_name);
+
+			return NULL;
+		}
 
 		/*
-		 * We may want to match without any user options getting
-		 * in the way.
+		 * If we found a scheme, then everything after the slash MUST be a URI.
+		 * We don't need to do any further modification.
 		 */
-		AST_SIP_USER_OPTIONS_TRUNCATE_CHECK(name);
+		if (scheme) {
+			*uri = ast_strdup(afterslash);
+			ast_debug(3, "Dest: '%s' Found endpoint '%s' and found URI '%s' after slash\n",
+				to, endpoint_name, *uri);
+		} else {
+			/* It now may be an aor or a URI without a scheme */
+			struct ast_sip_aor *aor;
+			struct ast_sip_contact *contact = NULL;
+
+			aor = ast_sip_location_retrieve_aor(afterslash);
+			if (aor) {
+				ast_debug(3, "Dest: '%s' Found endpoint '%s' and found aor '%s' after slash\n",
+					to, endpoint_name, ast_sorcery_object_get_id(aor));
+
+				contact = ast_sip_location_retrieve_first_aor_contact(aor);
+				if (contact) {
+					*uri = ast_strdup(contact->uri);
+					ast_debug(3, "Dest: '%s' Found endpoint '%s' and found contact with URI '%s' for aor '%s'\n",
+						to, endpoint_name, *uri, ast_sorcery_object_get_id(aor));
+				} else {
+					/*
+					 * An aor without a contact is useless and since
+					 * ast_sip_create_message() won't be able to find one
+					 * either, we just need to bail.
+					 */
+					ao2_cleanup(endpoint);
+					endpoint = NULL;
+					*uri = NULL;
+					ast_debug(3, "Dest: '%s' FAIL: Found endpoint '%s' but didn't find contact for aor '%s'\n",
+						to, endpoint_name, ast_sorcery_object_get_id(aor));
+				}
+				ao2_cleanup(contact);
+				ao2_cleanup(aor);
+			} else {
+				/*
+				 * It's probably a uri without a scheme but we don't have a way to tell.
+				 * We're going to assume it is and prepend it with a scheme.
+				 */
+				*uri = ast_malloc(strlen(afterslash) + strlen("sip:") + 1);
+				sprintf(*uri, "sip:%s", afterslash);
+				ast_debug(3, "Dest: '%s' Found endpoint '%s' but didn't find aor after slash so using URI '%s'\n",
+					to, endpoint_name, *uri);
+			}
+		}
+
+		return endpoint;
 	}
 
-	/* at this point, if name is not empty then it
-	   might be an endpoint, so try to retrieve it */
-	if (ast_strlen_zero(name)
-		|| !(endpoint = ast_sorcery_retrieve_by_id(ast_sip_get_sorcery(), "endpoint",
-			name))) {
-		/* an endpoint was not found, so assume sending directly
-		   to a uri and use the default outbound endpoint */
-		*uri = ast_strdup(to);
-		return ast_sip_default_outbound_endpoint();
+	/*
+	 * If there's an '@' but no scheme then it's either following an endpoint name
+	 * and being followed by a domain name (which we discard).
+	 * OR is's a user at host uri without a scheme.  It's probably the latter but because
+	 * endpoint at domain looks just line user at host, we'll test for endpoint first.
+	 */
+	if (!endpoint && atsign && !scheme) {
+		char *afterat = atsign + 1;
+		*atsign = '\0';
+		endpoint_name = destination;
+
+		/* Apprently there may be ';<user_options>' after the endpoint name ??? */
+		AST_SIP_USER_OPTIONS_TRUNCATE_CHECK(endpoint_name);
+		endpoint = ast_sorcery_retrieve_by_id(ast_sip_get_sorcery(), "endpoint", endpoint_name);
+		/* If it's an endpoint, just return it because we discard the domain. */
+		if (endpoint) {
+			struct ast_sip_contact *contact =
+				ast_sip_location_retrieve_contact_from_aor_list(endpoint->aors);
+			if (!contact) {
+				/*
+				 * We're getting the contact using the same method as
+				 * ast_sip_create_request() so if there's no contact
+				 * we can never send this message.
+				 */
+				ao2_cleanup(endpoint);
+				endpoint = NULL;
+				*uri = NULL;
+				ast_debug(3, "Dest: '%s' FAIL: Found endpoint '%s' but didn't find contact\n",
+					to, endpoint_name);
+			} else {
+				*uri = ast_strdup(contact->uri);
+				ao2_cleanup(contact);
+				ast_debug(3, "Dest: '%s' Found endpoint '%s' and found contact with URI '%s' (discarding domain %s)\n",
+					to, endpoint_name, *uri, afterat);
+			}
+		} else {
+			/*
+			 * It's probably a uri with a user but without a scheme but we don't have a way to tell.
+			 * We're going to assume it is and prepend it with a scheme.
+			 */
+			*uri = ast_malloc(strlen(to) + strlen("sip:") + 1);
+			sprintf(*uri, "sip:%s", to);
+			endpoint = ast_sip_default_outbound_endpoint();
+			ast_debug(3, "Dest: '%s' Didn't find endpoint before the '@' so using URI '%s' with default endpoint\n",
+				to, *uri);
+		}
+
+		return endpoint;
 	}
 
-	if (ast_strlen_zero(aor_uri)) {
-		*uri = NULL;
+	/*
+	 * If all else fails, we assume it's a URI or just a hostname.
+	 */
+	if (scheme) {
+		*uri = ast_strdup(destination);
+		ast_debug(3, "Dest: '%s' Didn't find an endpoint but did find a scheme so using URI '%s' with default endpoint\n",
+			to, *uri);
 	} else {
-		struct ast_sip_aor *aor;
-		struct ast_sip_contact *contact = NULL;
-		char *end;
-
-		/* Trim off any stray angle bracket that shouldn't be here */
-		end = strchr(aor_uri, '>');
-		if (end) {
-			*end = '\0';
-		}
-
-		/*
-		 * if what's in 'uri' is a retrievable aor use the uri on it
-		 * instead, otherwise assume what's there is already a uri
-		 */
-		aor = ast_sip_location_retrieve_aor(aor_uri);
-		if (aor && (contact = ast_sip_location_retrieve_first_aor_contact(aor))) {
-			aor_uri = (char *) contact->uri;
-		}
-		/* need to copy because underlying uri goes away */
-		*uri = ast_strdup(aor_uri);
-
-		ao2_cleanup(contact);
-		ao2_cleanup(aor);
+		*uri = ast_malloc(strlen(destination) + strlen("sip:") + 1);
+		sprintf(*uri, "sip:%s", destination);
+		ast_debug(3, "Dest: '%s' Didn't find an endpoint and didn't find scheme so adding scheme and using URI '%s' with default endpoint\n",
+			to, *uri);
 	}
+	endpoint = ast_sip_default_outbound_endpoint();
 
 	return endpoint;
 }
 
 /*!
  * \internal
- * \brief Overwrite fields in the outbound 'To' header
- *
- * Updates display name in an outgoing To header.
+ * \brief Replace the To URI in the tdata with the supplied one
  *
  * \param tdata the outbound message data structure
- * \param to info to copy into the header
+ * \param to URI to replace the To URI with
+ *
+ * \return 0: success, -1: failure
  */
-static void update_to(pjsip_tx_data *tdata, char *to)
+static int update_to_uri(pjsip_tx_data *tdata, char *to)
+{
+	pjsip_name_addr *parsed_name_addr;
+	pjsip_sip_uri *sip_uri;
+	pjsip_name_addr *tdata_name_addr;
+	pjsip_sip_uri *tdata_sip_uri;
+	char *buf = NULL;
+#define DEBUG_BUF_SIZE 256
+
+	parsed_name_addr = (pjsip_name_addr *) pjsip_parse_uri(tdata->pool, to, strlen(to),
+		PJSIP_PARSE_URI_AS_NAMEADDR);
+
+	if (!parsed_name_addr || (!PJSIP_URI_SCHEME_IS_SIP(parsed_name_addr->uri)
+			&& !PJSIP_URI_SCHEME_IS_SIPS(parsed_name_addr->uri))) {
+		ast_log(LOG_WARNING, "To address '%s' is not a valid SIP/SIPS URI\n", to);
+		return -1;
+	}
+
+	sip_uri = pjsip_uri_get_uri(parsed_name_addr->uri);
+	if (DEBUG_ATLEAST(3)) {
+		buf = ast_alloca(DEBUG_BUF_SIZE);
+		pjsip_uri_print(PJSIP_URI_IN_FROMTO_HDR, sip_uri, buf, DEBUG_BUF_SIZE);
+		ast_debug(3, "Parsed To: %.*s  %s\n", (int)parsed_name_addr->display.slen,
+			parsed_name_addr->display.ptr, buf);
+	}
+
+	tdata_name_addr = (pjsip_name_addr *) PJSIP_MSG_TO_HDR(tdata->msg)->uri;
+	if (!tdata_name_addr || (!PJSIP_URI_SCHEME_IS_SIP(tdata_name_addr->uri)
+			&& !PJSIP_URI_SCHEME_IS_SIPS(tdata_name_addr->uri))) {
+		/* Highly unlikely but we have to check */
+		ast_log(LOG_WARNING, "tdata To address '%s' is not a valid SIP/SIPS URI\n", to);
+		return -1;
+	}
+
+	tdata_sip_uri = pjsip_uri_get_uri(tdata_name_addr->uri);
+	if (DEBUG_ATLEAST(3)) {
+		buf[0] = '\0';
+		pjsip_uri_print(PJSIP_URI_IN_FROMTO_HDR, tdata_sip_uri, buf, DEBUG_BUF_SIZE);
+		ast_debug(3, "Original tdata To: %.*s  %s\n", (int)tdata_name_addr->display.slen,
+			tdata_name_addr->display.ptr, buf);
+	}
+
+	/* Replace the uri */
+	pjsip_sip_uri_assign(tdata->pool, tdata_sip_uri, sip_uri);
+	/* The display name isn't part of the URI so we need to replace it separately */
+	pj_strdup(tdata->pool, &tdata_name_addr->display, &parsed_name_addr->display);
+
+	if (DEBUG_ATLEAST(3)) {
+		buf[0] = '\0';
+		pjsip_uri_print(PJSIP_URI_IN_FROMTO_HDR, tdata_sip_uri, buf, 256);
+		ast_debug(3, "New tdata To: %.*s  %s\n", (int)tdata_name_addr->display.slen,
+			tdata_name_addr->display.ptr, buf);
+	}
+
+	return 0;
+#undef DEBUG_BUF_SIZE
+}
+
+/*!
+ * \internal
+ * \brief Update the display name in the To uri in the tdata with the one from the supplied uri
+ *
+ * \param tdata the outbound message data structure
+ * \param to uri containing the display name to replace in the the To uri
+ *
+ * \return 0: success, -1: failure
+ */
+static int update_to_display_name(pjsip_tx_data *tdata, char *to)
 {
 	pjsip_name_addr *parsed_name_addr;
 
 	parsed_name_addr = (pjsip_name_addr *) pjsip_parse_uri(tdata->pool, to, strlen(to),
 		PJSIP_PARSE_URI_AS_NAMEADDR);
+
 	if (parsed_name_addr) {
 		if (pj_strlen(&parsed_name_addr->display)) {
 			pjsip_name_addr *name_addr =
 				(pjsip_name_addr *) PJSIP_MSG_TO_HDR(tdata->msg)->uri;
 
 			pj_strdup(tdata->pool, &name_addr->display, &parsed_name_addr->display);
+
 		}
+		return 0;
 	}
+
+	return -1;
 }
 
 /*!
@@ -254,15 +537,17 @@
  *
  * \param tdata the outbound message data structure
  * \param from info to copy into the header
+ *
+ * \return 0: success, -1: failure
  */
-static void update_from(pjsip_tx_data *tdata, char *from)
+static int update_from(pjsip_tx_data *tdata, char *from)
 {
 	pjsip_name_addr *name_addr;
 	pjsip_sip_uri *uri;
 	pjsip_name_addr *parsed_name_addr;
 
 	if (ast_strlen_zero(from)) {
-		return;
+		return 0;
 	}
 
 	name_addr = (pjsip_name_addr *) PJSIP_MSG_FROM_HDR(tdata->msg)->uri;
@@ -276,7 +561,7 @@
 		if (!PJSIP_URI_SCHEME_IS_SIP(parsed_name_addr->uri)
 				&& !PJSIP_URI_SCHEME_IS_SIPS(parsed_name_addr->uri)) {
 			ast_log(LOG_WARNING, "From address '%s' is not a valid SIP/SIPS URI\n", from);
-			return;
+			return -1;
 		}
 
 		parsed_uri = pjsip_uri_get_uri(parsed_name_addr->uri);
@@ -285,9 +570,12 @@
 			pj_strdup(tdata->pool, &name_addr->display, &parsed_name_addr->display);
 		}
 
+		/* Unlike the To header, we only want to replace the user, host and port */
 		pj_strdup(tdata->pool, &uri->user, &parsed_uri->user);
 		pj_strdup(tdata->pool, &uri->host, &parsed_uri->host);
 		uri->port = parsed_uri->port;
+
+		return 0;
 	} else {
 		/* assume it is 'user[@domain]' format */
 		char *domain = strchr(from, '@');
@@ -302,7 +590,11 @@
 		} else {
 			pj_strdup2(tdata->pool, &uri->user, from);
 		}
+
+		return 0;
 	}
+
+	return -1;
 }
 
 /*!
@@ -618,13 +910,8 @@
 	}
 	++to;/* Now skip the ':' */
 
-	/* Make sure we start with sip: */
-	mdata->to = ast_begins_with(to, "sip:") ? ast_strdup(to) : ast_strdup(to - 4);
+	mdata->to = ast_strdup(to);
 	mdata->from = ast_strdup(from);
-	if (!mdata->to || !mdata->from) {
-		ao2_ref(mdata, -1);
-		return NULL;
-	}
 
 	/*
 	 * Sometimes from URI can contain URI parameters, so remove them.
@@ -667,6 +954,25 @@
 	}
 }
 
+/*!
+ * \internal
+ * \brief Send a MESSAGE
+ *
+ * \param mdata The outbound message data structure
+ *
+ * \return 0: success, -1: failure
+ *
+ * mdata contains the To and From specified in the call to the MessageSend
+ * dialplan app.  It also contains the ast_msg object that contains the
+ * message body and may contain the To and From from the channel datastore,
+ * usually set with the MESSAGE or MESSAGE_DATA dialplan functions but
+ * could also come from an incoming sip MESSAGE.
+ *
+ * The mdata->to is always used as the basis for the Request URI
+ * while the mdata->msg->to is used for the To header.  If
+ * mdata->msg->to isn't available, mdata->to is used for the To header.
+ *
+ */
 static int msg_send(void *data)
 {
 	struct msg_data *mdata = data; /* The caller holds a reference */
@@ -681,6 +987,9 @@
 	RAII_VAR(char *, uri, NULL, ast_free);
 	RAII_VAR(struct ast_sip_endpoint *, endpoint, NULL, ao2_cleanup);
 
+	ast_debug(3, "mdata From: %s msg From: %s mdata Destination: %s msg To: %s\n",
+		mdata->from, ast_msg_get_from(mdata->msg), mdata->to, ast_msg_get_to(mdata->msg));
+
 	endpoint = get_outbound_endpoint(mdata->to, &uri);
 	if (!endpoint) {
 		ast_log(LOG_ERROR,
@@ -689,13 +998,41 @@
 		return -1;
 	}
 
+	ast_debug(3, "Request URI: %s\n", uri);
+
 	if (ast_sip_create_request("MESSAGE", NULL, endpoint, uri, NULL, &tdata)) {
 		ast_log(LOG_WARNING, "PJSIP MESSAGE - Could not create request\n");
 		return -1;
 	}
 
-	update_to(tdata, mdata->to);
-	update_from(tdata, mdata->from);
+	/* If there was a To in the actual message, */
+	if (!ast_strlen_zero(ast_msg_get_to(mdata->msg))) {
+		char *msg_to = ast_strdupa(ast_msg_get_to(mdata->msg));
+
+		/*
+		 * It's possible that the message To was copied from
+		 * an incoming MESSAGE in which case it'll have the
+		 * pjsip: tech prepended to it.  We need to remove it.
+		 */
+		if (ast_begins_with(msg_to, "pjsip:")) {
+			msg_to += 6;
+		}
+		update_to_uri(tdata, msg_to);
+	} else {
+		/*
+		 * If there was no To in the message, it's still possible
+		 * that there is a display name in the mdata To.  If so,
+		 * we'll copy the URI display name to the tdata To.
+		 */
+		update_to_display_name(tdata, uri);
+	}
+
+	if (!ast_strlen_zero(mdata->from)) {
+		update_from(tdata, mdata->from);
+	} else if (!ast_strlen_zero(ast_msg_get_from(mdata->msg))) {
+		update_from(tdata, (char *)ast_msg_get_from(mdata->msg));
+	}
+
 	update_content_type(tdata, mdata->msg, &body);
 
 	if (ast_sip_add_body(tdata, &body)) {
@@ -704,10 +1041,14 @@
 		return -1;
 	}
 
+	/*
+	 * This copies any headers set with MESSAGE_DATA() to the
+	 * tdata.
+	 */
 	vars_to_headers(mdata->msg, tdata);
 
 	ast_debug(1, "Sending message to '%s' (via endpoint %s) from '%s'\n",
-		mdata->to, ast_sorcery_object_get_id(endpoint), mdata->from);
+		uri, ast_sorcery_object_get_id(endpoint), mdata->from);
 
 	if (ast_sip_send_request(tdata, NULL, endpoint, NULL, NULL)) {
 		ast_log(LOG_ERROR, "PJSIP MESSAGE - Could not send request\n");

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

Gerrit-Project: asterisk
Gerrit-Branch: 16
Gerrit-Change-Id: I67a485196d9199916468f7f98bfb9a0b993a4cce
Gerrit-Change-Number: 15828
Gerrit-PatchSet: 1
Gerrit-Owner: George Joseph <gjoseph at digium.com>
Gerrit-MessageType: newchange
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.digium.com/pipermail/asterisk-code-review/attachments/20210427/bc03b841/attachment-0001.html>


More information about the asterisk-code-review mailing list