From fa4a7dcc45c92c799a012cd223232836c3fecd1a Mon Sep 17 00:00:00 2001
From: Matthew Jordan <mjordan@digium.com>
Date: Fri, 2 Dec 2011 23:30:21 +0000
Subject: [PATCH] Update SIP MESSAGE To parsing to correctly handle URI

The previous patch (r346040) incorrectly parsed the URI in the presence
of a port, e.g., user@hostname:port would fail as the port would be
double appended to the SIP message.  This patch uses the parse_uri function
to correctly parse the URI into its username and hostname parts, and places
them in the correct fields in the sip_pvt structure.

(issue ASTERISK-18903)
Review: https://reviewboard.asterisk.org/r/1597/
........

Merged revisions 346856 from http://svn.asterisk.org/svn/asterisk/branches/10


git-svn-id: https://origsvn.digium.com/svn/asterisk/trunk@346857 65c4cc65-6c06-0410-ace0-fbb531ad65f3
---
 channels/chan_sip.c | 23 ++++++++---------------
 1 file changed, 8 insertions(+), 15 deletions(-)

diff --git a/channels/chan_sip.c b/channels/chan_sip.c
index a181ec74cd..6d28fe17a3 100644
--- a/channels/chan_sip.c
+++ b/channels/chan_sip.c
@@ -24332,25 +24332,17 @@ static int sip_msg_send(const struct ast_msg *msg, const char *to, const char *f
 {
 	struct sip_pvt *pvt;
 	int res;
-	char *uri, *host;
+	char *to_uri, *to_host, *to_user;
 	struct sip_peer *peer_ptr;
 
 	if (!(pvt = sip_alloc(NULL, NULL, 0, SIP_MESSAGE, NULL))) {
 		return -1;
 	}
 
-	uri = ast_strdupa(to);
-	if (!strncasecmp(uri, "sip:", 4)) {
-		uri += 4;
-	} else if (!strncasecmp(uri, "sips:", 5)) {
-		uri += 5;
-	}
-	host = ast_strdupa(uri);
-	if (strchr(host, '@')) {
-		strsep(&host, "@");
-	}
+	to_uri = ast_strdupa(to);
+	parse_uri(to_uri, "sip:,sips:", &to_user, NULL, &to_host, NULL);
 
-	if (ast_strlen_zero(host)) {
+	if (ast_strlen_zero(to_host)) {
 		ast_log(LOG_WARNING, "MESSAGE(to) is invalid for SIP - '%s'\n", to);
 		dialog_unlink_all(pvt);
 		dialog_unref(pvt, "MESSAGE(to) is invalid for SIP");
@@ -24387,15 +24379,16 @@ static int sip_msg_send(const struct ast_msg *msg, const char *to, const char *f
 	sip_pvt_lock(pvt);
 
 	/* Look up the host to contact */
-	if (create_addr(pvt, host, NULL, TRUE, NULL)) {
+	if (create_addr(pvt, to_host, NULL, TRUE, NULL)) {
 		sip_pvt_unlock(pvt);
 		dialog_unlink_all(pvt);
 		dialog_unref(pvt, "create_addr failed sending a MESSAGE");
 		return -1;
 	}
 
-	/* Set the tohost to the full URI provided */
-	ast_string_field_set(pvt, tohost, uri);
+	if (!ast_strlen_zero(to_user)) {
+		ast_string_field_set(pvt, username, to_user);
+	}
 	ast_sip_ouraddrfor(&pvt->sa, &pvt->ourip, pvt);
 	ast_set_flag(&pvt->flags[0], SIP_OUTGOING);
 
-- 
GitLab