[Asterisk-Dev] Patch to support an "outgoing proxy" on SIP register

Dietmar Zlabinger dietmar.astlist2 at eggern.at
Tue Aug 24 13:40:02 MST 2004


Hi,

the sip-channel-module of asterisk supports sending register requests to a host different from the realm used in the SIP-message:

"register => user[:secret[:authuser]]@host[:port][/extension]"

But when I tried this syntax I failed to register at my SIP provider, so I traced the messages sent out and found that "host" is
used
in the SIP-uri (and not "REGISTER sip:authuser SIP/2.0") and that "user at authuser" is used when authenticating (and not
"Authorization: Digest username="user", realm="authuser""),
therefore I modified the chan_sip.c so that I could register successfully, I believe that the modifications I did are required
to register using an "outgoing proxy".

Please give me feedback if you believe that my modifications are of general interest and if they should be included into the
asterisk code.

The patch I have attached below is against todays cvs-repository,
you can also find the patch at http://www.zlabinger.at/chan_sip.patch

Best regards,

Dietmar Zlabinger




-- 
--- ../../cvs_asterisk/asterisk/channels/chan_sip.c	2004-08-23 16:31:20.000000000 +0200
+++ channels/chan_sip.c	2004-08-24 16:58:29.000000000 +0200
@@ -12,6 +12,29 @@
  */


+/*
+
+Modifications dz 2004 08 24
+
+Using an outgoing proxy means sending the message to host different
+from the realm (uri) used inside the sip-message. To do this
+the following syntax is supported inside sip.conf:
+"user[:secret[:authuser]]@host[:port][/extension]"
+
+The request will be sent to "host" while "authuser" will be used inside
+the sip-message.
+When trying to use this at my provider I noticed to problems, this
+patch should fix them:
+- the first line of the REGISTER request contained "host" (outgoing proxy)
+while "authuser" was expected, therefore I replaced this by the authuser variable
+- when authenticating "user at authuser" was used instead of "user", therefore
+authentication failed, now only "user" is used.
+
+
+*/
+
+
+
 #include <stdio.h>
 #include <ctype.h>
 #include <string.h>
@@ -1284,6 +1307,10 @@
 			}
 			strncpy(r->peername, p->username, sizeof(r->peername)-1);
 			strncpy(r->authname, p->username, sizeof(r->authname)-1);
+			/* dz 2004-08-23*/
+			/* authname shall always be without realm (never use user at realm) */
+			if (strchr(r->authname,'@'))
+				*(strchr(r->authname,'@'))=0;
 			strncpy(r->peersecret, p->secret, sizeof(r->peersecret)-1);
 			strncpy(r->peermd5secret, p->md5secret, sizeof(r->peermd5secret)-1);
 			strncpy(r->username, p->username, sizeof(r->username)-1);
@@ -3993,6 +4020,7 @@
 	char tmp[80];
 	char via[80];
 	char addr[80];
+	char *hostpart=NULL; /* dz pointer to hostpart of username */
 	char iabuf[INET_ADDRSTRLEN];
 	struct sip_pvt *p;

@@ -4038,6 +4066,10 @@
 			if (!ast_strlen_zero(r->username)) {
 				strncpy(p->peername, r->username, sizeof(p->peername)-1);
 				strncpy(p->authname, r->username, sizeof(p->authname)-1);
+				/* dz 2004-08-23*/
+				/* authname shall always be without realm (never use user at realm) */
+				if (strchr(p->authname,'@'))
+				*(strchr(p->authname,'@'))=0;
 			}
 		}
 		if (!ast_strlen_zero(r->username))
@@ -4067,12 +4099,17 @@
 	if (strchr(r->username, '@')) {
 		snprintf(from, sizeof(from), "<sip:%s>;tag=as%08x", r->username, p->tag);
 		snprintf(to, sizeof(to),     "<sip:%s>", r->username);
+		/* dz 2004 08 23 use auth and not host in SIP-uri*/
+		hostpart = strrchr(r->username, '@')+1;
+		snprintf(addr, sizeof(addr), "sip:%s", hostpart);
 	} else {
 		snprintf(from, sizeof(from), "<sip:%s@%s>;tag=as%08x", r->username, p->tohost, p->tag);
 		snprintf(to, sizeof(to),     "<sip:%s@%s>", r->username, p->tohost);
+		/* no outgoing proxy defined */
+		snprintf(addr, sizeof(addr), "sip:%s", r->hostname);
+
 	}

-	snprintf(addr, sizeof(addr), "sip:%s", r->hostname);
 	strncpy(p->uri, addr, sizeof(p->uri) - 1);

 	p->branch ^= rand();
@@ -5346,6 +5383,9 @@
 				if (!ast_strlen_zero(peer->username)) {
 					strncpy(p->username, peer->username, sizeof(p->username) - 1);
 					strncpy(p->authname, peer->username, sizeof(p->authname) - 1);
+					/* dz 2004-08-23*/
+					if (strchr(p->authname,'@'))
+						*(strchr(p->authname,'@'))=0;
 				}
 				if (!ast_strlen_zero(peer->context))
 					strncpy(p->context, peer->context, sizeof(p->context) - 1);
@@ -5467,7 +5507,7 @@
 /*--- sip_show_users: CLI Command 'SIP Show Users' ---*/
 static int sip_show_users(int fd, int argc, char *argv[])
 {
-#define FORMAT  "%-15.15s  %-15.15s  %-15.15s %-15.15s %-5.5s%-5.5s\n"
+#define FORMAT  "%-15.15s  %-22.22s  %-15.15s %-15.15s %-5.5s%-5.5s\n"
 	struct sip_user *user;
 	if (argc != 3)
 		return RESULT_SHOWUSAGE;
@@ -5489,8 +5529,8 @@
 /*--- sip_show_peers: CLI Show Peers command */
 static int sip_show_peers(int fd, int argc, char *argv[])
 {
-#define FORMAT2 "%-15.15s  %-15.15s %s %s %s %-15.15s  %-8s %-10s\n"
-#define FORMAT  "%-15.15s  %-15.15s %s %s %s %-15.15s  %-8d %-10s\n"
+#define FORMAT2 "%-15.15s  %-25.25s %s %s %s %-15.15s  %-8s %-10s\n"
+#define FORMAT  "%-15.15s  %-25.25s %s %s %s %-15.15s  %-8d %-10s\n"
 	struct sip_peer *peer;
 	char name[256] = "";
 	char iabuf[INET_ADDRSTRLEN];
-- 
-------------- next part --------------
A non-text attachment was scrubbed...
Name: chan_sip.patch
Type: application/octet-stream
Size: 4123 bytes
Desc: not available
Url : http://lists.digium.com/pipermail/asterisk-dev/attachments/20040824/5ae7aeea/chan_sip.obj


More information about the asterisk-dev mailing list