[svn-commits] rizzo: trunk r44566 - in /trunk: channels/chan_sip.c configs/sip.conf.sample

svn-commits at lists.digium.com svn-commits at lists.digium.com
Fri Oct 6 08:41:13 MST 2006


Author: rizzo
Date: Fri Oct  6 10:41:12 2006
New Revision: 44566

URL: http://svn.digium.com/view/asterisk?rev=44566&view=rev
Log:
Two things:
1. slightly rearrange/simplify the parsing of the argument in sip_register.
   This brings in a patch that has been in Mantis (5834)  for ages,
   and is the larger part of the commit;

2. implement the "contact" option for peers, similar to the one in users.conf:

   If you put a "contact" option with a non-empty argument (e.g. contact=123)
   in a peer section, asterisk will register with the provider as if you had a     

        register= username:secret at host/contact 

   line in the general section.

The latter is a very small is a new feature so i am not putting it
in the 1.4 branch, although the "contact" option in user.conf is
already in the 1.4 branch and so it wouldn't be too strange to
merge it.

Note that the implementation of "contact" is much simpler than
the one in 5834, and limited to a few lines in build_peer().


Modified:
    trunk/channels/chan_sip.c
    trunk/configs/sip.conf.sample

Modified: trunk/channels/chan_sip.c
URL: http://svn.digium.com/view/asterisk/trunk/channels/chan_sip.c?rev=44566&r1=44565&r2=44566&view=diff
==============================================================================
--- trunk/channels/chan_sip.c (original)
+++ trunk/channels/chan_sip.c Fri Oct  6 10:41:12 2006
@@ -4243,44 +4243,45 @@
 static int sip_register(char *value, int lineno)
 {
 	struct sip_registry *reg;
-	char copy[256];
-	char *username=NULL, *hostname=NULL, *secret=NULL, *authuser=NULL;
+	int portnum = 0;
+	char username[256] = "";
+	char *hostname=NULL, *secret=NULL, *authuser=NULL;
 	char *porta=NULL;
 	char *contact=NULL;
-	char *stringp=NULL;
-	
+
 	if (!value)
 		return -1;
-	ast_copy_string(copy, value, sizeof(copy));
-	stringp=copy;
-	username = stringp;
-	hostname = strrchr(stringp, '@');
+	ast_copy_string(username, value, sizeof(username));
+	/* First split around the last '@' then parse the two components. */
+	hostname = strrchr(username, '@'); /* allow @ in the first part */
 	if (hostname)
 		*hostname++ = '\0';
 	if (ast_strlen_zero(username) || ast_strlen_zero(hostname)) {
 		ast_log(LOG_WARNING, "Format for registration is user[:secret[:authuser]]@host[:port][/contact] at line %d\n", lineno);
 		return -1;
 	}
-	stringp = username;
-	username = strsep(&stringp, ":");
-	if (username) {
-		secret = strsep(&stringp, ":");
-		if (secret) 
-			authuser = strsep(&stringp, ":");
-	}
-	stringp = hostname;
-	hostname = strsep(&stringp, "/");
-	if (hostname) 
-		contact = strsep(&stringp, "/");
+	/* split user[:secret[:authuser]] */
+	secret = strchr(username, ':');
+	if (secret) {
+		*secret++ = '\0';
+		authuser = strchr(secret, ':');
+		if (authuser)
+			*authuser++ = '\0';
+	}
+	/* split host[:port][/contact] */
+	contact = strchr(hostname, '/');
+	if (contact)
+		*contact++ = '\0';
 	if (ast_strlen_zero(contact))
 		contact = "s";
-	stringp=hostname;
-	hostname = strsep(&stringp, ":");
-	porta = strsep(&stringp, ":");
-	
-	if (porta && !atoi(porta)) {
-		ast_log(LOG_WARNING, "%s is not a valid port number at line %d\n", porta, lineno);
-		return -1;
+	porta = strchr(hostname, ':');
+	if (porta) {
+		*porta++ = '\0';
+		portnum = atoi(porta);
+		if (portnum == 0) {
+			ast_log(LOG_WARNING, "%s is not a valid port number at line %d\n", porta, lineno);
+			return -1;
+		}
 	}
 	if (!(reg = ast_calloc(1, sizeof(*reg)))) {
 		ast_log(LOG_ERROR, "Out of memory. Can't allocate SIP registry entry\n");
@@ -4307,7 +4308,7 @@
 	reg->expire = -1;
 	reg->timeout =  -1;
 	reg->refresh = default_expiry;
-	reg->portno = porta ? atoi(porta) : 0;
+	reg->portno = portnum;
 	reg->callid_valid = FALSE;
 	reg->ocseq = INITIAL_CSEQ;
 	ASTOBJ_CONTAINER_LINK(&regl, reg);	/* Add the new registry entry to the list */
@@ -15368,6 +15369,7 @@
 	struct ast_variable *tmpvar = NULL;
 	struct ast_flags peerflags[2] = {{(0)}};
 	struct ast_flags mask[2] = {{(0)}};
+	char contact[256] = "";
 
 
 	if (!realtime)
@@ -15503,6 +15505,8 @@
 			ast_copy_string(peer->language, v->value, sizeof(peer->language));
 		} else if (!strcasecmp(v->name, "regexten")) {
 			ast_copy_string(peer->regexten, v->value, sizeof(peer->regexten));
+		} else if (!strcasecmp(v->name, "contact")) {
+			ast_copy_string(contact, v->value, sizeof(contact));
 		} else if (!strcasecmp(v->name, "call-limit")) {
 			peer->call_limit = atoi(v->value);
 			if (peer->call_limit < 0)
@@ -15612,6 +15616,15 @@
 		reg_source_db(peer);
 	ASTOBJ_UNMARK(peer);
 	ast_free_ha(oldha);
+	if (!ast_strlen_zero(contact)) { /* build string from peer info */
+		char *reg_string;
+
+		asprintf(&reg_string, "%s:%s@%s/%s", peer->username, peer->secret, peer->tohost, contact);
+		if (reg_string) {
+			sip_register(reg_string, 0); /* XXX TODO: count in registry_count */
+			free(reg_string);
+		}
+	}
 	return peer;
 }
 

Modified: trunk/configs/sip.conf.sample
URL: http://svn.digium.com/view/asterisk/trunk/configs/sip.conf.sample?rev=44566&r1=44565&r2=44566&view=diff
==============================================================================
--- trunk/configs/sip.conf.sample (original)
+++ trunk/configs/sip.conf.sample Fri Oct  6 10:41:12 2006
@@ -432,6 +432,7 @@
 ;                             sendrpid
 ;                             outboundproxy
 ;                             rfc2833compensate
+;                             contact
 
 ;[sip_proxy]
 ; For incoming calls only. Example: FWD (Free World Dialup)
@@ -453,6 +454,14 @@
 ;outboundproxy=proxy.provider.domain	; send outbound signaling to this proxy, not directly to the peer
 				; Call-limits will not be enforced on real-time peers,
 				; since they are not stored in-memory
+
+;--- sample definition for a provider
+;[provider1]
+;type=peer
+;host=sip.provider1.com
+;username=4015552299		; how your provider knows you
+;secret=youwillneverguessit
+;contact=123			; tell asterisk to register as username:secret at host/contact
 
 ;------------------------------------------------------------------------------
 ; Definitions of locally connected SIP devices



More information about the svn-commits mailing list