[Asterisk-Dev] Evaluating trailing numbers in extensions.conf

Harald Milz hm at seneca.muc.de
Sun Mar 13 05:47:06 MST 2005


Harald Milz <hm at seneca.muc.de> wrote:
> Abhishek Tiwari <abhitiwari at gmail.com> wrote:
> > additionally, can have your own variables, just look at
> > pbx_retrieve_variable in pbx.c
> That would probably be the best solution, but I can't find which internal *
> variable holds the called number. There is no such struct member in

Well I did find what is needed. Please see the attached patch against
CVS-HEAD-03/13/05-12:07:55. 

I'm using it right now with the following extension for everything that
comes in via SIP. The idea is to route it to extension 1X on my ISDN PBX,
depending on the trailing number X

[isdn]
exten => _1[1-8],1,Dial(Modem/g1/20:${EXTEN},15,r)
exten => _1[1-8],2,Congestion
exten => _1[1-8],102,Busy

[incoming]
exten => sip,1,Goto(isdn,1${CALLEDNUM:-1},1)
exten => sip,2,Voicemail(u100)
exten => sip,102,Voicemail(b100)

Hmmm - what do you folks think? It appears quite simple and straightforward
to me. The variable CALLEDNUM makes not always sense, but if you want to
catch calls via a SIP provider from the PSTN it works fine. Hey, a single
number from the SIP provider and a whole virtual PBX behind it. :-) 

The example above is a simple one - you can extend it as you like, e.g. use
multi-digit "extension numbers" which point at arbitrary * extensions. You
just need to know how many digits your official number has (in my case,
10) and you're all set. 

It depends on your SIP provider, though. web.de sends your SIP user name in
the To: header, and if you append digits to the official number you get an
error message "this number is not available". But then, a web.de 01212
number is only of limited usability anyway - you can't reach it from the
German D2/Vodafone network. 

Hmmm - I hope sipgate leaves everything as it is now. 


---------------------------------- snip ----------------------------------

diff -ur asterisk-ORIG/channels/chan_sip.c asterisk/channels/chan_sip.c
--- asterisk-ORIG/channels/chan_sip.c	2005-03-12 22:28:53.513437000 +0100
+++ asterisk/channels/chan_sip.c	2005-03-13 13:24:53.707682831 +0100
@@ -231,7 +231,6 @@
 
 static struct ast_codec_pref prefs;
 
-
 /* sip_request: The data grabbed from the UDP socket */
 struct sip_request {
 	char *rlPart1; 		/* SIP Method Name or "SIP/2.0" protocol version */
@@ -360,6 +359,7 @@
 	char tohost[AST_MAX_EXTENSION];		/* Host we should put in the "to" field */
 	char language[MAX_LANGUAGE];		/* Default language for this call */
 	char musicclass[MAX_LANGUAGE];          /* Music on Hold class */
+	char callednum[256];			/* The number we were called with */
 	char rdnis[256];			/* Referring DNIS */
 	char theirtag[256];			/* Their tag */
 	char username[256];			/* [user] name */
@@ -626,6 +626,7 @@
 static int sip_fixup(struct ast_channel *oldchan, struct ast_channel *newchan);
 static int sip_senddigit(struct ast_channel *ast, char digit);
 static int sip_sendtext(struct ast_channel *ast, char *text);
+static char *get_callednum(struct sip_request *req); 
 
 static const struct ast_channel_tech sip_tech = {
 	.type = channeltype,
@@ -2176,6 +2177,9 @@
 		if (!ast_strlen_zero(i->callid)) {
 			pbx_builtin_setvar_helper(tmp, "SIPCALLID", i->callid);
 		}
+		if (!ast_strlen_zero(i->callednum)) {
+			pbx_builtin_setvar_helper(tmp, "CALLEDNUM", i->callednum);
+		}
 		ast_setstate(tmp, state);
 		if (state != AST_STATE_DOWN) {
 			if (ast_pbx_start(tmp)) {
@@ -8492,6 +8496,29 @@
 	return 0;
 }
 
+
+
+static char *get_callednum(struct sip_request *req) {
+
+	char *tmp, *p, *q;
+
+	tmp = get_header(req, "To");
+	p = strstr(tmp, "sip:");	/* find sip: */
+	if (p == NULL) return ""; 	/* nothing found, strange */
+	p += 4;				/* points now at start of number */
+	/* do we have a @ or ; at all, ever ? */
+	q = strstr(p, "@");		/* find the @ */
+	if (q == NULL) 
+		q = strstr(p, ";");	/* or a ; */
+	if (q != NULL) 
+		*q = '\0';		/* and end the string */
+	return p;
+
+}
+
+
+
+
 /*--- sipsock_read: Read data from SIP socket ---*/
 /*    Successful messages is connected to SIP call and forwarded to handle_request() */
 static int sipsock_read(int *id, int fd, short events, void *ignore)
@@ -8534,6 +8561,11 @@
 	ast_mutex_lock(&netlock);
 	p = find_call(&req, &sin);
 	if (p) {
+		/* determine the called number (if we were called from the PSTN) */
+		strncpy (p->callednum, get_callednum(&req), sizeof(p->callednum));
+		if (debug) 
+			ast_verbose("\nTo-Header: %s\ncalled number: %s\n", 
+					get_header(&req, "To"), p->callednum);
 		/* Go ahead and lock the owner if it has one -- we may need it */
 		if (p->owner && ast_mutex_trylock(&p->owner->lock)) {
 			ast_log(LOG_DEBUG, "Failed to grab lock, trying again...\n");

---------------------------------- snip ----------------------------------


-- 
Any sufficiently advanced technology is indistinguishable from magic.
		-- Arthur C. Clarke



More information about the asterisk-dev mailing list